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