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