1 /*
2  * BEGIN illumos section
3  *   This is an unstable interface; changes may be made
4  *   without notice.
5  * END illumos section
6  */
7 /***********************************************************************
8 *                                                                      *
9 *               This software is part of the ast package               *
10 *          Copyright (c) 1986-2011 AT&T Intellectual Property          *
11 *                      and is licensed under the                       *
12 *                 Eclipse Public License, Version 1.0                  *
13 *                    by AT&T Intellectual Property                     *
14 *                                                                      *
15 *                A copy of the License is available at                 *
16 *          http://www.eclipse.org/org/documents/epl-v10.html           *
17 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
18 *                                                                      *
19 *              Information and Software Systems Research               *
20 *                            AT&T Research                             *
21 *                           Florham Park NJ                            *
22 *                                                                      *
23 *                 Glenn Fowler <gsf@research.att.com>                  *
24 *                                                                      *
25 ***********************************************************************/
26 #pragma prototyped
27 /*
28  * Glenn Fowler
29  * AT&T Research
30  *
31  * preprocessor library public definitions
32  */
33 
34 #ifndef _PP_H
35 #define _PP_H
36 
37 #ifdef ppsymbol
38 /*
39  * undo old nmake cpp name-space intrusion
40  * this disables __LINE__, __FILE__, __DATE__ and __TIME__
41  */
42 #undef	ppsymbol
43 #undef	__LINE__
44 #define __LINE__	0
45 #undef	__FILE__
46 #define __FILE__	"libpp"
47 #undef	__DATE__
48 #define __DATE__	"MMM DD YYYY"
49 #undef	__TIME__
50 #define __TIME__	"HH:MM:SS"
51 #endif
52 
53 
54 #if PROTOMAIN
55 #define HASH_HEADER	int	hash_header
56 #define Hash_table_t	char
57 #define Sfio_t		char
58 #define CC_bel		(('A'==0301)?0057:0007)
59 #define CC_esc		(('A'==0301)?0047:0033)
60 #define CC_vt		0013
61 #else
62 #include <limits.h>
63 #include <hash.h>
64 #include <error.h>
65 #include <ccode.h>
66 #endif
67 
68 #define PPDEFAULT	"pp_default.h"		/* runtime definitions	*/
69 #define PPPROBE		"cc"			/* default probe key	*/
70 #define PPSTANDARD	"/usr/include"		/* standard include dir	*/
71 
72 #define PPBLKSIZ	1024			/* unit block size	*/
73 #define PPBAKSIZ	(1*PPBLKSIZ)		/* input pushback size	*/
74 #define PPBUFSIZ	(32*PPBLKSIZ)		/* io buffer size	*/
75 #define PPTOKSIZ	((PPBUFSIZ/2)-1)	/* max token size	*/
76 
77 #define PPWRITE(n)	do{if(write(1,pp.outbuf,n)!=(n))pperror(ERROR_SYSTEM|3,"%s: write error",pp.outfile);pp.offset+=(n);pp.lastout=pp.outbuf[(n)-1];}while(0)
78 
79 #define pplastout()	((pp.outp>pp.outbuf)?*(pp.outp-1):pp.lastout)
80 #define ppoffset()	(pp.offset+pppendout())
81 #define pppendout()	(pp.outp-pp.outbuf)
82 #define ppputchar(c)	(*pp.outp++=(c))
83 #define ppflushout()	do{if(pp.outp>pp.outbuf){PPWRITE(pp.outp-pp.outbuf);pp.outp=pp.outbuf;}}while(0)
84 #define ppcheckout()	do{if(pp.outp>pp.oute){PPWRITE(PPBUFSIZ);if(pp.outbuf==pp.outb){pp.outbuf+=PPBUFSIZ;pp.oute+=PPBUFSIZ;}else{pp.outbuf-=PPBUFSIZ;memcpy(pp.outbuf,pp.oute,pp.outp-pp.oute);pp.oute-=PPBUFSIZ;pp.outp-=2*PPBUFSIZ;}}}while(0)
85 
86 #define ppsymget(t,n)	(struct ppsymbol*)hashlook(t,n,HASH_LOOKUP,NiL)
87 #define ppsymref(t,n)	(struct ppsymbol*)hashlook(t,n,pp.truncate?HASH_LOOKUP:HASH_LOOKUP|HASH_INTERNAL,NiL)
88 #define ppsymset(t,n)	(struct ppsymbol*)hashlook(t,n,HASH_CREATE|HASH_SIZE(sizeof(struct ppsymbol)),NiL)
89 
90 #if CHAR_MIN < 0
91 #define pptype		(ppctype-(CHAR_MIN))
92 #else
93 #define pptype		(ppctype)
94 #endif
95 
96 #define C_ID		(1<<0)
97 #define C_DIG		(1<<1)
98 #define C_SPLICE	(1<<2)
99 
100 #define ppisdig(c)	((pptype)[c]&C_DIG)
101 #define ppisid(c)	((pptype)[c]&C_ID)
102 #define ppisidig(c)	((pptype)[c]&(C_ID|C_DIG))
103 #define ppismac(c)	((pptype)[c]&(C_ID|C_DIG|C_SPLICE))
104 #define ppissplice(c)	((pptype)[c]&C_SPLICE)
105 
106 #define setid(c)	((pptype)[c]|=C_ID)
107 #define clrid(c)	((pptype)[c]&=~C_ID)
108 #define setdig(c)	((pptype)[c]|=C_DIG)
109 #define setsplice(c)	((pptype)[c]|=C_SPLICE)
110 
111 #define REF_CREATE	(REF_NORMAL+1)	/* include wrapper (internal)	*/
112 #define REF_DELETE	(REF_NORMAL+2)	/* macro definition (internal)	*/
113 #define REF_NORMAL	0		/* normal macro reference	*/
114 #define REF_IF		(-1)		/* if, ifdef, ifndef, elif	*/
115 #define REF_UNDEF	(-2)		/* undef			*/
116 
117 #define SYM_ACTIVE	(1L<<0)		/* active macro lock		*/
118 #define SYM_BUILTIN	(1L<<1)		/* builtin macro		*/
119 #define SYM_DISABLED	(1L<<2)		/* macro expansion disabled	*/
120 #define SYM_EMPTY	(1L<<3)		/* allow empty/missing actuals	*/
121 #define SYM_FINAL	(1L<<4)		/* final hosted value		*/
122 #define SYM_FUNCTION	(1L<<5)		/* macro with args		*/
123 #define SYM_INIT	(1L<<6)		/* initialization macro		*/
124 #define SYM_INITIAL	(1L<<7)		/* initial hosted value		*/
125 #define SYM_KEYWORD	(1L<<8)		/* keyword identifier		*/
126 #define SYM_LEX		(1L<<9)		/* ppsymkey with lex field	*/
127 #define SYM_MULTILINE	(1L<<10)	/* multi-line macro		*/
128 #define SYM_NOEXPAND	(1L<<11)	/* no identifiers in macro body	*/
129 #define SYM_NOTICED	(1L<<12)	/* symbol noticed in output	*/
130 #define SYM_PREDEFINED	(1L<<13)	/* predefined macro		*/
131 #define SYM_PREDICATE	(1L<<14)	/* also a predicate		*/
132 #define SYM_READONLY	(1L<<15)	/* readonly macro		*/
133 #define SYM_REDEFINE	(1L<<16)	/* ok to redefine		*/
134 #define SYM_VARIADIC	(1L<<17)	/* variadic macro with args	*/
135 #define SYM_UNUSED	24		/* first unused symbol flag bit	*/
136 
137 #define PP_ASSERT		1	/* preassert symbol		*/
138 #define PP_BUILTIN		2	/* #(<id>) handler		*/
139 #define PP_CDIR			3	/* C (vs. C++) file dirs follow	*/
140 #define PP_CHOP			4	/* include prefix chop		*/
141 #define PP_COMMENT		5	/* passed comment handler	*/
142 #define PP_COMPATIBILITY	6	/* old (Reiser) dialect		*/
143 #define PP_COMPILE		7	/* tokenize for front end	*/
144 #define PP_DEBUG		8	/* set debug trace level	*/
145 #define PP_DEFINE		9	/* predefine symbol		*/
146 #define PP_DEFAULT		10	/* read default include files	*/
147 #define PP_DIRECTIVE		11	/* initialization directive	*/
148 #define PP_DONE			12	/* all processing done		*/
149 #define PP_DUMP			13	/* do checkpoint dump		*/
150 #define PP_FILEDEPS		14	/* output file dependencies	*/
151 #define PP_FILENAME		15	/* set input file name		*/
152 #define PP_HOSTDIR		16	/* hosted file dirs follow	*/
153 #define PP_ID			17	/* add to identifier set	*/
154 #define PP_IGNORE		18	/* ignore this include file	*/
155 #define PP_IGNORELIST		19	/* include ignore list file	*/
156 #define PP_INCLUDE		20	/* add dir to include search	*/
157 #define PP_INCREF		21	/* include file push/ret handler*/
158 #define PP_INIT			22	/* one time initialization	*/
159 #define PP_INPUT		23	/* set input source file	*/
160 #define PP_KEYARGS		24	/* name=value macro args	*/
161 #define PP_LINE			25	/* line sync handler		*/
162 #define PP_LINEBASE		26	/* base name in line sync	*/
163 #define PP_LINEFILE		27	/* line sync requires file arg	*/
164 #define PP_LINEID		28	/* PP_LINE directive id		*/
165 #define PP_LINETYPE		29	/* # extra line sync type args	*/
166 #define PP_LOCAL		30	/* previous PP_INCLUDE for ""	*/
167 #define PP_MACREF		31	/* macro def/ref handler	*/
168 #define PP_MULTIPLE		32	/* set all files multiple	*/
169 #define PP_NOHASH		33	/* don't hash PP_COMPILE T_ID's	*/
170 #define PP_NOISE		34	/* convert T_X_* to T_NOISE	*/
171 #define PP_OPTION		35	/* set pragma option		*/
172 #define PP_OPTARG		36	/* unknown option arg handler	*/
173 #define PP_OUTPUT		37	/* set output file sink		*/
174 #define PP_PASSTHROUGH		38	/* ppcpp() expands # lines only	*/
175 #define PP_PEDANTIC		39	/* pedantic non-hosted warnings	*/
176 #define PP_PLUSCOMMENT		40	/* enable C++ comments		*/
177 #define PP_PLUSPLUS		41	/* tokenize for C++		*/
178 #define PP_POOL			42	/* pool for multiple io passes	*/
179 #define PP_PRAGMA		43	/* passed pragma handler	*/
180 #define PP_PRAGMAFLAGS		44	/* global pragma flags		*/
181 #define PP_PROBE		45	/* ppdefault probe key		*/
182 #define PP_QUOTE		46	/* add to quote set		*/
183 #define PP_READ			47	/* include file without output	*/
184 #define PP_REGUARD		48	/* file pop emits guard define	*/
185 #define PP_RESERVED		49	/* COMPILE reserved keyword	*/
186 #define PP_RESET		50	/* reset to initiali predefs	*/
187 #define PP_SPACEOUT		51	/* pplex returns space,newline	*/
188 #define PP_STANDALONE		52	/* standalone preprocessor	*/
189 #define PP_STANDARD		53	/* standard include dir		*/
190 #define PP_STRICT		54	/* strict implementation	*/
191 #define PP_TEST			55	/* enable (undocumented) tests	*/
192 #define PP_TEXT			56	/* include file with output	*/
193 #define PP_TRANSITION		57	/* on COMPATIBILITY boundary	*/
194 #define PP_TRUNCATE		58	/* truncate macro names		*/
195 #define PP_UNDEF		59	/* undef symbol after ppdefault	*/
196 #define PP_VENDOR		60	/* vendor file dirs follow	*/
197 #define PP_WARN			61	/* enable annoying warnings	*/
198 
199 #define PP_comment		(1<<0)	/* PP_COMMENT is set		*/
200 #define PP_compatibility	(1<<1)	/* PP_COMPATIBILITY is set	*/
201 #define PP_hosted		(1<<2)	/* current file is hosted	*/
202 #define PP_linebase		(1<<3)	/* base name in line sync	*/
203 #define PP_linefile		(1<<4)	/* line sync file arg required	*/
204 #define PP_linehosted		(1<<5)	/* line sync hosted arg required*/
205 #define PP_lineignore		(1<<6)	/* line sync for ignored file	*/
206 #define PP_linetype		(1<<7)	/* line sync type arg required	*/
207 #define PP_strict		(1<<8)	/* PP_STRICT is set		*/
208 #define PP_transition		(1<<9)	/* PP_TRANSITION is set		*/
209 
210 #define PP_deps			(1<<0)	/* generate header deps		*/
211 #define PP_deps_file		(1<<1)	/* write deps to separate file	*/
212 #define PP_deps_generated	(1<<2)	/* missing deps are generated	*/
213 #define PP_deps_local		(1<<3)	/* only local header deps	*/
214 
215 #define PP_sync			0	/* normal line sync		*/
216 #define PP_sync_push		'1'	/* [3] include file push	*/
217 #define PP_sync_pop		'2'	/* [3] include file pop		*/
218 #define PP_sync_ignore		'3'	/* [3] ignored include file	*/
219 #define PP_sync_hosted		'3'	/* [4] hosted include file	*/
220 
221 #define PP_SYNC_PUSH		(1<<0)	/* pp.incref PP_sync_push type	*/
222 #define PP_SYNC_POP		(1<<1)	/* pp.incref PP_sync_pop type	*/
223 #define PP_SYNC_IGNORE		(1<<2)	/* pp.incref PP_sync_ignore type*/
224 #define PP_SYNC_HOSTED		(1<<3)	/* pp.incref PP_sync_hosted type*/
225 #define PP_SYNC_INSERT		(1<<4)	/* pinserted by other means	*/
226 
227 /*
228  * numeric modifiers
229  *
230  * NOTE: 0400 is claimed by error in yacc
231  * 	 (N_PP+30) is the largest valid pp token
232  *	 free tokens start at T_TOKEN
233  */
234 
235 #define N_PP			0401		/* pp tokens 0401..0437	*/
236 #define N_NUMBER		0440		/* numbers 0440..0477	*/
237 #define N_TEST			(N_NUMBER|07700)/* number test mask	*/
238 #define N_TOKEN			0500		/* free 0500..07777	*/
239 #define N_WIDE			1		/* wide quoted constant	*/
240 
241 /*
242  * NOTE: preserve the token ranges and encodings for is*(x)
243  */
244 
245 #define ppisnumber(x)		(((x)&N_TEST)==N_NUMBER)
246 #define ppisinteger(x)		(((x)&(N_TEST|N_REAL))==N_NUMBER)
247 #define ppisreal(x)		(((x)&(N_TEST|N_REAL))==(N_NUMBER|N_REAL))
248 #define ppisassignop(x)		(((x)>=T_MPYEQ)&&((x)<=T_OREQ))
249 #define ppisseparate(x)		(((x)>=N_PP)&&((x)<=T_WSTRING)||((x)>=N_NUMBER)||((x)=='+')||((x)=='-'))
250 
251 #define N_LONG			0001
252 #define N_UNSIGNED		0002		/* if ppisinteger(x)	*/
253 #define N_FLOAT			0002		/* if ppisreal(x)		*/
254 
255 #define N_REAL			0004
256 #define N_OCTAL			0010
257 #define N_HEXADECIMAL		0020
258 
259 #define N_EXPONENT		010000		/* for lexing only	*/
260 #define N_SIGN			020000		/* for lexing only	*/
261 #define N_TRAILING		040000		/* for lexing only	*/
262 
263 #if !defined(T_DOUBLE)
264 
265 /*
266  * numeric constants
267  */
268 
269 #define T_DOUBLE		(N_NUMBER|N_REAL)
270 #define T_DOUBLE_L		(N_NUMBER|N_REAL|N_LONG)
271 #define T_FLOAT			(N_NUMBER|N_REAL|N_FLOAT)
272 #define T_DECIMAL		(N_NUMBER)
273 #define T_DECIMAL_L		(N_NUMBER|N_LONG)
274 #define T_DECIMAL_U		(N_NUMBER|N_UNSIGNED)
275 #define T_DECIMAL_UL		(N_NUMBER|N_UNSIGNED|N_LONG)
276 #define T_OCTAL			(N_NUMBER|N_OCTAL)
277 #define T_OCTAL_L		(N_NUMBER|N_OCTAL|N_LONG)
278 #define T_OCTAL_U		(N_NUMBER|N_OCTAL|N_UNSIGNED)
279 #define T_OCTAL_UL		(N_NUMBER|N_OCTAL|N_UNSIGNED|N_LONG)
280 #define T_HEXADECIMAL		(N_NUMBER|N_HEXADECIMAL)
281 #define T_HEXADECIMAL_L		(N_NUMBER|N_HEXADECIMAL|N_LONG)
282 #define T_HEXADECIMAL_U		(N_NUMBER|N_HEXADECIMAL|N_UNSIGNED)
283 #define T_HEXADECIMAL_UL	(N_NUMBER|N_HEXADECIMAL|N_UNSIGNED|N_LONG)
284 #define T_HEXDOUBLE		(N_NUMBER|N_HEXADECIMAL|N_REAL)
285 #define T_HEXDOUBLE_L		(N_NUMBER|N_HEXADECIMAL|N_REAL|N_LONG)
286 
287 /*
288  * identifier and invalid token
289  */
290 
291 #define T_ID			(N_PP+0)
292 #define T_INVALID		(N_PP+1)
293 
294 /*
295  * quoted constants
296  */
297 
298 #define T_HEADER		(N_PP+2)		/*	<..>	*/
299 #define T_CHARCONST		(N_PP+3)		/*	'..'	*/
300 #define T_WCHARCONST		(T_CHARCONST|N_WIDE)	/*	L'..'	*/
301 #define T_STRING		(N_PP+5)		/*	".."	*/
302 #define T_WSTRING		(T_STRING|N_WIDE)	/*	L".."	*/
303 
304 /*
305  * multichar operators
306  */
307 
308 #define T_PTRMEM		(N_PP+7)	/*	->	*/
309 #define T_ADDADD		(N_PP+8)	/*	++	*/
310 #define T_SUBSUB		(N_PP+9)	/*	--	*/
311 #define T_LSHIFT		(N_PP+10)	/*	<<	*/
312 #define T_RSHIFT		(N_PP+11)	/*	>>	*/
313 #define T_LE			(N_PP+12)	/*	<=	*/
314 #define T_GE			(N_PP+13)	/*	>=	*/
315 #define T_EQ			(N_PP+14)	/*	==	*/
316 #define T_NE			(N_PP+15)	/*	!=	*/
317 #define T_ANDAND		(N_PP+16)	/*	&&	*/
318 #define T_OROR			(N_PP+17)	/*	||	*/
319 #define T_MPYEQ			(N_PP+18)	/*	*=	*/
320 #define T_DIVEQ			(N_PP+19)	/*	/=	*/
321 #define T_MODEQ			(N_PP+20)	/*	%=	*/
322 #define T_ADDEQ			(N_PP+21)	/*	+=	*/
323 #define T_SUBEQ			(N_PP+22)	/*	-=	*/
324 #define T_LSHIFTEQ		(N_PP+23)	/*	<<=	*/
325 #define T_RSHIFTEQ		(N_PP+24)	/*	>>=	*/
326 #define T_ANDEQ			(N_PP+25)	/*	&=	*/
327 #define T_XOREQ			(N_PP+26)	/*	^=	*/
328 #define T_OREQ			(N_PP+27)	/*	|=	*/
329 #define T_TOKCAT		(N_PP+28)	/*	##	*/
330 #define T_VARIADIC		(N_PP+29)	/*	...	*/
331 
332 /*
333  * C++ tokens
334  */
335 
336 #define T_DOTREF		(N_TOKEN+0)	/*	.*	*/
337 #define T_PTRMEMREF		(N_TOKEN+1)	/*	->*	*/
338 #define T_SCOPE			(N_TOKEN+2)	/*	::	*/
339 
340 /*
341  * compiler tokens
342  */
343 
344 #define T_UMINUS		(N_TOKEN+3)
345 
346 #endif
347 
348 /*
349  * start of free tokens
350  */
351 
352 #define T_TOKEN			(N_TOKEN+4)
353 
354 struct ppdirs				/* directory list		*/
355 {
356 	char*		name;		/* directory name		*/
357 	struct ppdirs*	next;		/* next in list			*/
358 
359 #ifdef _PP_DIRS_PRIVATE_
360 	_PP_DIRS_PRIVATE_
361 #endif
362 
363 };
364 
365 struct ppkeyword			/* pp keyword info		*/
366 {
367 	char*		name;		/* keyword name			*/
368 	int		value;		/* keyword token value		*/
369 };
370 
371 struct ppmacro				/* pp macro info		*/
372 {
373 	int		arity;		/* # formal arguments		*/
374 	char*		value;		/* definition value		*/
375 
376 #ifdef _PP_MACRO_PRIVATE_
377 	_PP_MACRO_PRIVATE_
378 #endif
379 
380 };
381 
382 struct ppsymbol				/* pp symbol info		*/
383 {
384 	HASH_HEADER;			/* hash stuff and symbol name	*/
385 	unsigned long	flags;		/* SYM_* status			*/
386 	struct ppmacro*	macro;		/* macro info			*/
387 	void*		value;		/* value (for other passes)	*/
388 
389 #ifdef _PP_SYMBOL_PRIVATE_
390 	_PP_SYMBOL_PRIVATE_
391 #endif
392 
393 };
394 
395 #define _PP_CONTEXT_BASE_	((char*)&pp.lcldirs)
396 
397 #define _PP_CONTEXT_PUBLIC_ \
398 	struct ppdirs*	lcldirs;	/* the "..." dir list		*/ \
399 	struct ppdirs*	stddirs;	/* next is the <...> dir list	*/ \
400 	int		flags;		/* PP_[a-z]* flags		*/ \
401 	Hash_table_t*	symtab;		/* macro and id hash table	*/
402 
403 struct ppglobals			/* globals accessed by pp.*	*/
404 {
405 	const char*	version;	/* version stamp		*/
406 	char*		lineid;		/* line sync directive id	*/
407 	char*		outfile;	/* output file name		*/
408 	char*		pass;		/* pass name			*/
409 	char*		token;		/* pplex() token name		*/
410 	struct ppsymbol* symbol;	/* last symbol if PP_COMPILE	*/
411 
412 	/* exposed for the output macros */
413 
414 	char*		outb;		/* output buffer base		*/
415 	char*		outbuf;		/* output buffer		*/
416 	char*		outp;	    	/* outbuf pointer		*/
417 	char*		oute;	    	/* outbuf end			*/
418 	unsigned long	offset;		/* output offset		*/
419 
420 #ifdef _PP_CONTEXT_PUBLIC_
421 	_PP_CONTEXT_PUBLIC_		/* public context		*/
422 #endif
423 
424 #ifdef _PP_CONTEXT_PRIVATE_
425 	_PP_CONTEXT_PRIVATE_		/* library private context	*/
426 #endif
427 
428 #ifdef _PP_GLOBALS_PRIVATE_
429 	_PP_GLOBALS_PRIVATE_		/* library private additions	*/
430 #endif
431 
432 };
433 
434 /*
435  * library interface globals
436  */
437 
438 #define ppctype		_pp_ctype
439 
440 extern struct ppglobals	pp;
441 extern char		ppctype[];
442 
443 extern int		ppargs(char**, int);
444 extern void		ppcpp(void);
445 extern void		ppcomment(char*, char*, char*, int);
446 extern void*		ppcontext(void*, int);
447 extern void		pperror(int, ...);
448 extern void		ppincref(char*, char*, int, int);
449 extern void		ppinput(char*, char*, int);
450 extern int		pplex(void);
451 extern void		ppline(int, char*);
452 extern void		ppmacref(struct ppsymbol*, char*, int, int, unsigned long);
453 extern void		ppop(int, ...);
454 extern void		pppragma(char*, char*, char*, char*, int);
455 extern int		ppprintf(char*, ...);
456 extern int		ppsync(void);
457 
458 #endif
459