xref: /aoo42x/main/soltools/cpp/cpp.h (revision 7b4f4066)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 /* $Id$ */
22 
23 #define	INS			32768	/* input buffer								*/
24 #define	OBS			8092    /* outbut buffer							*/
25 #define	NARG		32      /* Max number arguments to a macro			*/
26 #define	NINCLUDE	48      /* Max number of include directories (-I)	*/
27 #define	NIF			64		/* depth of nesting of #if					*/
28 #define	NINC		32		/* depth of nesting of #include				*/
29 
30 #ifndef EOF
31 #define	EOF	(-1)
32 #endif
33 
34 #ifndef NULL
35 #define NULL	0
36 #endif
37 
38 typedef unsigned char uchar;
39 
40 enum toktype
41 {
42     END, UNCLASS, NAME, NUMBER, STRING, CCON, NL, WS, DSHARP,
43     EQ, NEQ, LEQ, GEQ, LSH, RSH, LAND, LOR, PPLUS, MMINUS,
44     ARROW, SBRA, SKET, LP, RP, DOT, AND, STAR, PLUS, MINUS,
45     TILDE, NOT, SLASH, PCT, LT, GT, CIRC, OR, QUEST,
46     COLON, ASGN, COMMA, SHARP, SEMIC, CBRA, CKET,
47     ASPLUS, ASMINUS, ASSTAR, ASSLASH, ASPCT, ASCIRC, ASLSH,
48     ASRSH, ASOR, ASAND, ELLIPS,
49     DSHARP1, NAME1, NAME2, DEFINED, UMINUS, ARCHITECTURE, IDENT,
50 	COMMENT
51 };
52 
53 enum kwtype
54 {
55     KIF, KIFDEF, KIFNDEF, KELIF, KELSE, KENDIF, KINCLUDE, KINCLUDENEXT,
56     KIMPORT, KDEFINE, KUNDEF, KLINE, KERROR, KPRAGMA, KIDENT, KDEFINED,
57 	KMACHINE, KLINENO, KFILE, KDATE, KTIME, KSTDC, KEVAL
58 };
59 
60 #define	ISDEFINED		0x01            /* has #defined value */
61 #define	ISKW			0x02            /* is PP keyword */
62 #define	ISUNCHANGE		0x04            /* can't be #defined in PP */
63 #define	ISMAC			0x08            /* builtin macro, e.g. __LINE__ */
64 #define	ISARCHITECTURE	0x10            /* architecture */
65 #define ISACTIVE        0x80            /* is macro currently expanded */
66 
67 #define	EOB		0xFE                    /* sentinel for end of input buffer */
68 #define	EOFC	0xFD                    /* sentinel for end of input file */
69 #define	XPWS	1                       /* token flag: white space to assure token sep. */
70 #define XTWS	2
71 
72 typedef struct token
73 {
74     unsigned char type;
75     unsigned char flag;
76     unsigned int wslen;
77     unsigned int len;
78 	uchar *t;
79 	unsigned int identifier;			/* used from macro processor to identify where a macro becomes valid again. */
80 }   Token;
81 
82 typedef struct tokenrow
83 {
84 	Token *tp;                          /* current one to scan */
85 	Token *bp;                          /* base (allocated value) */
86 	Token *lp;                          /* last+1 token used */
87 	int max;                            /* number allocated */
88 }   Tokenrow;
89 
90 typedef struct source
91 {
92 	char *filename;                     /* name of file of the source */
93 	int line;                           /* current line number */
94 	int lineinc;                        /* adjustment for \\n lines */
95 	uchar *inb;                         /* input buffer */
96 	uchar *inp;                         /* input pointer */
97 	uchar *inl;                         /* end of input */
98 	int fd;                             /* input source */
99 	int ifdepth;                        /* conditional nesting in include */
100 	int pathdepth;
101 	int wrap;
102 	struct source *next;                /* stack for #include */
103 }   Source;
104 
105 typedef struct nlist
106 {
107 	struct nlist *next;
108 	uchar *name;
109 	int len;
110 	Tokenrow *vp;                       /* value as macro */
111 	Tokenrow *ap;                       /* list of argument names, if any */
112 	char val;                           /* value as preprocessor name */
113 	char flag;                          /* is defined, is pp name */
114 	uchar *loc;							/* location of definition */
115 }   Nlist;
116 
117 typedef struct includelist
118 {
119 	char deleted;
120 	char always;
121 	char *file;
122 }   Includelist;
123 
124 typedef struct wraplist
125 {
126 	char *file;
127 }   Wraplist;
128 
129 #define	new(t)	(t *)domalloc(sizeof(t))
130 #define	quicklook(a,b)	(namebit[(a)&077] & (1<<((b)&037)))
131 #define	quickset(a,b)	namebit[(a)&077] |= (1<<((b)&037))
132 extern unsigned long namebit[077 + 1];
133 
134 enum errtype
135 {
136 	INFO, WARNING, ERROR, FATAL
137 };
138 
139 
140 typedef struct macroValidator
141 {
142 	Nlist *				pMacro;
143 	unsigned int		nTokenWhereMacroBecomesValid;
144 	struct macroValidator *
145 						pNext;
146 } MacroValidator;
147 typedef struct mvl
148 {
149 	MacroValidator * pFirst;
150     unsigned int	 nextFreeIdentifier;
151 } MacroValidatorList;
152 
153 void		mvl_init(
154 				MacroValidatorList *
155 								out_pValidators);
156 void		mvl_destruct(
157 				MacroValidatorList *
158 								out_pValidators);
159 /* 	Adds MacroValidator to the list.
160 */
161 void 		mvl_add(
162 				MacroValidatorList *
163 								inout_pValidators,
164 				Nlist *         in_pMacro,
165 				Token *			in_pTokenWhereMacroBecomesValid);
166 /* 	Updates all token pointers within the list, when the tokens have
167 	moved, by
168 		pTokenWhereMacroBecomesValid += in_nNrofTokens;
169 	.
170 
171 void		mvl_move(
172 				MacroValidatorList *
173 								inout_pValidators,
174 				int				in_nSpace);	// in pointer units.
175 */
176 /*	Checks if one of the validators within the list points to
177 	the token in_pTokenToCheck. If so, the macro is set valid and
178 	the validator is removed.
179 */
180 void		mvl_check(
181 				MacroValidatorList *
182 								inout_pValidators,
183 				Token *			inout_pTokenToCheck);
184 
185 void tokenrow_zeroTokenIdentifiers(Tokenrow* trp);
186 
187 void expandlex(void);
188 void fixlex(void);
189 void setup(int, char **);
190 int gettokens(Tokenrow *, int);
191 int comparetokens(Tokenrow *, Tokenrow *);
192 Source *setsource(char *, int, int, char *, int);
193 void unsetsource(void);
194 void puttokens(Tokenrow *);
195 void process(Tokenrow *);
196 void *domalloc(int);
197 void dofree(void *);
198 void error(enum errtype, char *,...);
199 void flushout(void);
200 int fillbuf(Source *);
201 int trigraph(Source *);
202 int foldline(Source *);
203 Nlist *lookup(Token *, int);
204 void control(Tokenrow *);
205 void dodefine(Tokenrow *);
206 void doadefine(Tokenrow *, int);
207 void doinclude(Tokenrow *, int, int);
208 void doif(Tokenrow *, enum kwtype);
209 void expand(Tokenrow *, Nlist *, MacroValidatorList *);
210 void builtin(Tokenrow *, int);
211 int gatherargs(Tokenrow *, Tokenrow **, int *);
212 void substargs(Nlist *, Tokenrow *, Tokenrow **);
213 void expandrow(Tokenrow *, char *);
214 void maketokenrow(int, Tokenrow *);
215 Tokenrow *copytokenrow(Tokenrow *, Tokenrow *);
216 Token *growtokenrow(Tokenrow *);
217 Tokenrow *normtokenrow(Tokenrow *);
218 void adjustrow(Tokenrow *, int);
219 void movetokenrow(Tokenrow *, Tokenrow *);
220 void insertrow(Tokenrow *, int, Tokenrow *);
221 void peektokens(Tokenrow *, char *);
222 void doconcat(Tokenrow *);
223 Tokenrow *stringify(Tokenrow *);
224 int lookuparg(Nlist *, Token *);
225 long eval(Tokenrow *, int);
226 void genline(void);
227 void genimport(char *, int, char *, int);
228 void genwrap(int);
229 void setempty(Tokenrow *);
230 void makespace(Tokenrow *, Token *);
231 char *outnum(char *, int);
232 int digit(int);
233 uchar *newstring(uchar *, int, int);
234 
235 #define	rowlen(tokrow)	((tokrow)->lp - (tokrow)->bp)
236 
237 extern char *outptr;
238 extern Token nltoken;
239 extern Source *cursource;
240 extern char *curtime;
241 extern int incdepth;
242 extern int ifdepth;
243 extern int ifsatisfied[NIF];
244 extern int Mflag;
245 extern int Iflag;
246 extern int Pflag;
247 extern int Aflag;
248 extern int Lflag;
249 extern int Xflag;
250 extern int Vflag;
251 extern int Cflag;
252 extern int Dflag;
253 extern int Cplusplus;
254 extern int skipping;
255 extern Nlist *kwdefined;
256 extern Includelist includelist[NINCLUDE];
257 extern Wraplist wraplist[NINCLUDE];
258 extern char wd[];
259 
260