]>
Commit | Line | Data |
---|---|---|
88ae23e7 ZW |
1 | /* Part of CPP library. |
2 | Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. | |
4283012f JL |
3 | |
4 | This program is free software; you can redistribute it and/or modify it | |
5 | under the terms of the GNU General Public License as published by the | |
6 | Free Software Foundation; either version 2, or (at your option) any | |
7 | later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
42b17236 | 16 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
4283012f | 17 | |
88ae23e7 ZW |
18 | /* This header defines all the internal data structures and functions |
19 | that need to be visible across files. It's called cpphash.h for | |
20 | historical reasons. */ | |
21 | ||
bb52fa7f ZW |
22 | #ifndef __GCC_CPPHASH__ |
23 | #define __GCC_CPPHASH__ | |
24 | ||
88ae23e7 ZW |
25 | typedef unsigned char U_CHAR; |
26 | ||
bb52fa7f ZW |
27 | /* Structure allocated for every #define. For a simple replacement |
28 | such as | |
29 | #define foo bar , | |
30 | nargs = -1, the `pattern' list is null, and the expansion is just | |
31 | the replacement text. Nargs = 0 means a functionlike macro with no args, | |
32 | e.g., | |
33 | #define getchar() getc (stdin) . | |
34 | When there are args, the expansion is the replacement text with the | |
35 | args squashed out, and the reflist is a list describing how to | |
36 | build the output from the input: e.g., "3 chars, then the 1st arg, | |
37 | then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg". | |
38 | The chars here come from the expansion. Whatever is left of the | |
39 | expansion after the last arg-occurrence is copied after that arg. | |
40 | Note that the reflist can be arbitrarily long--- | |
41 | its length depends on the number of times the arguments appear in | |
42 | the replacement text, not how many args there are. Example: | |
43 | #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and | |
44 | pattern list | |
45 | { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL } | |
46 | where (x, y) means (nchars, argno). */ | |
47 | ||
ba412f14 ZW |
48 | struct reflist |
49 | { | |
50 | struct reflist *next; | |
51 | char stringify; /* nonzero if this arg was preceded by a | |
52 | # operator. */ | |
53 | char raw_before; /* Nonzero if a ## operator before arg. */ | |
54 | char raw_after; /* Nonzero if a ## operator after arg. */ | |
55 | char rest_args; /* Nonzero if this arg. absorbs the rest */ | |
56 | int nchars; /* Number of literal chars to copy before | |
57 | this arg occurrence. */ | |
58 | int argno; /* Number of arg to substitute (origin-0) */ | |
59 | }; | |
60 | ||
bb52fa7f | 61 | typedef struct definition DEFINITION; |
ba412f14 ZW |
62 | struct definition |
63 | { | |
bb52fa7f ZW |
64 | int nargs; |
65 | int length; /* length of expansion string */ | |
ba412f14 | 66 | U_CHAR *expansion; |
bb52fa7f | 67 | char rest_args; /* Nonzero if last arg. absorbs the rest */ |
ba412f14 ZW |
68 | struct reflist *pattern; |
69 | ||
38b24ee2 | 70 | /* Names of macro args, concatenated in order with \0 between |
ba412f14 ZW |
71 | them. The only use of this is that we warn on redefinition if |
72 | this differs between the old and new definitions. */ | |
73 | U_CHAR *argnames; | |
bb52fa7f ZW |
74 | }; |
75 | ||
88ae23e7 ZW |
76 | /* The structure of a node in the hash table. The hash table |
77 | has entries for all tokens defined by #define commands (type T_MACRO), | |
78 | plus some special tokens like __LINE__ (these each have their own | |
79 | type, and the appropriate code is run when that type of node is seen. | |
80 | It does not contain control words like "#define", which are recognized | |
81 | by a separate piece of code. */ | |
82 | ||
168d3732 | 83 | /* different flavors of hash nodes */ |
88ae23e7 ZW |
84 | enum node_type |
85 | { | |
d9e0bd53 | 86 | T_VOID = 0, /* no definition yet */ |
88ae23e7 ZW |
87 | T_SPECLINE, /* `__LINE__' */ |
88 | T_DATE, /* `__DATE__' */ | |
89 | T_FILE, /* `__FILE__' */ | |
90 | T_BASE_FILE, /* `__BASE_FILE__' */ | |
91 | T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */ | |
88ae23e7 ZW |
92 | T_TIME, /* `__TIME__' */ |
93 | T_STDC, /* `__STDC__' */ | |
94 | T_CONST, /* Constant string, used by `__SIZE_TYPE__' etc */ | |
d9e0bd53 ZW |
95 | T_XCONST, /* Ditto, but the string is malloced memory */ |
96 | T_POISON, /* poisoned identifier */ | |
97 | T_MCONST, /* object-like macro defined to a single identifier */ | |
98 | T_MACRO, /* general object-like macro */ | |
99 | T_FMACRO, /* general function-like macro */ | |
100 | T_IDENTITY, /* macro defined to itself */ | |
ff2b53ef | 101 | T_EMPTY /* macro defined to nothing */ |
88ae23e7 ZW |
102 | }; |
103 | ||
7f2935c7 | 104 | /* different kinds of things that can appear in the value field |
5dfa4da1 ZW |
105 | of a hash node. */ |
106 | union hashval | |
107 | { | |
108 | const char *cpval; /* some predefined macros */ | |
109 | DEFINITION *defn; /* #define */ | |
110 | struct hashnode *aschain; /* #assert */ | |
7f2935c7 PB |
111 | }; |
112 | ||
cf4ed945 | 113 | typedef struct hashnode HASHNODE; |
88ae23e7 ZW |
114 | struct hashnode |
115 | { | |
d35364d1 ZW |
116 | const U_CHAR *name; /* the actual name */ |
117 | size_t length; /* length of token, for quick comparison */ | |
118 | unsigned long hash; /* cached hash value */ | |
7f2935c7 | 119 | union hashval value; /* pointer to expansion, or whatever */ |
d35364d1 | 120 | enum node_type type; /* type of special token */ |
d9e0bd53 ZW |
121 | int disabled; /* macro turned off for rescan? */ |
122 | ||
123 | const char *file; /* File, line, column of definition; */ | |
124 | int line; | |
125 | int col; | |
7f2935c7 PB |
126 | }; |
127 | ||
88ae23e7 ZW |
128 | /* List of directories to look for include files in. */ |
129 | struct file_name_list | |
130 | { | |
131 | struct file_name_list *next; | |
132 | struct file_name_list *alloc; /* for the cache of | |
133 | current directory entries */ | |
134 | char *name; | |
135 | unsigned int nlen; | |
136 | /* We use these to tell if the directory mentioned here is a duplicate | |
137 | of an earlier directory on the search path. */ | |
138 | ino_t ino; | |
139 | dev_t dev; | |
140 | /* If the following is nonzero, it is a C-language system include | |
141 | directory. */ | |
142 | int sysp; | |
143 | /* Mapping of file names for this directory. | |
144 | Only used on MS-DOS and related platforms. */ | |
145 | struct file_name_map *name_map; | |
146 | }; | |
147 | #define ABSOLUTE_PATH ((struct file_name_list *)-1) | |
148 | ||
149 | /* This structure is used for the table of all includes. It is | |
150 | indexed by the `short name' (the name as it appeared in the | |
151 | #include statement) which is stored in *nshort. */ | |
152 | struct ihash | |
153 | { | |
88ae23e7 ZW |
154 | /* Next file with the same short name but a |
155 | different (partial) pathname). */ | |
156 | struct ihash *next_this_file; | |
157 | ||
158 | /* Location of the file in the include search path. | |
159 | Used for include_next */ | |
160 | struct file_name_list *foundhere; | |
d35364d1 ZW |
161 | |
162 | unsigned long hash; /* save hash value for future reference */ | |
163 | const char *nshort; /* name of file as referenced in #include; | |
164 | points into name[] */ | |
88ae23e7 ZW |
165 | const U_CHAR *control_macro; /* macro, if any, preventing reinclusion - |
166 | see redundant_include_p */ | |
d35364d1 | 167 | const char name[1]; /* (partial) pathname of file */ |
88ae23e7 ZW |
168 | }; |
169 | typedef struct ihash IHASH; | |
170 | ||
171 | /* Character classes. | |
172 | If the definition of `numchar' looks odd to you, please look up the | |
173 | definition of a pp-number in the C standard [section 6.4.8 of C99] */ | |
174 | #define ISidnum 0x01 /* a-zA-Z0-9_ */ | |
175 | #define ISidstart 0x02 /* _a-zA-Z */ | |
176 | #define ISnumstart 0x04 /* 0-9 */ | |
177 | #define IShspace 0x08 /* ' ' \t \f \v */ | |
178 | #define ISspace 0x10 /* ' ' \t \f \v \n */ | |
179 | ||
ae79697b | 180 | #define _dollar_ok(x) ((x) == '$' && CPP_OPTION (pfile, dollars_in_ident)) |
88ae23e7 ZW |
181 | |
182 | #define is_idchar(x) ((_cpp_IStable[x] & ISidnum) || _dollar_ok(x)) | |
183 | #define is_idstart(x) ((_cpp_IStable[x] & ISidstart) || _dollar_ok(x)) | |
184 | #define is_numchar(x) (_cpp_IStable[x] & ISidnum) | |
185 | #define is_numstart(x) (_cpp_IStable[x] & ISnumstart) | |
186 | #define is_hspace(x) (_cpp_IStable[x] & IShspace) | |
187 | #define is_space(x) (_cpp_IStable[x] & ISspace) | |
188 | ||
189 | /* This table is constant if it can be initialized at compile time, | |
190 | which is the case if cpp was compiled with GCC >=2.7, or another | |
191 | compiler that supports C99. */ | |
192 | #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L) | |
193 | extern const unsigned char _cpp_IStable[256]; | |
194 | #else | |
195 | extern unsigned char _cpp_IStable[256]; | |
196 | #endif | |
197 | ||
198 | /* Macros. */ | |
199 | ||
ff2b53ef ZW |
200 | /* One character lookahead in the input buffer. Note that if this |
201 | returns EOF, it does *not* necessarily mean the file's end has been | |
202 | reached. */ | |
88ae23e7 ZW |
203 | #define CPP_BUF_PEEK(BUFFER) \ |
204 | ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur : EOF) | |
88ae23e7 | 205 | |
45b966db ZW |
206 | /* Make sure PFILE->token_buffer has space for at least N more characters. */ |
207 | #define CPP_RESERVE(PFILE, N) \ | |
208 | (CPP_WRITTEN (PFILE) + (size_t)(N) > (PFILE)->token_buffer_size \ | |
209 | && (_cpp_grow_token_buffer (PFILE, N), 0)) | |
210 | ||
88ae23e7 ZW |
211 | /* Append string STR (of length N) to PFILE's output buffer. |
212 | Assume there is enough space. */ | |
213 | #define CPP_PUTS_Q(PFILE, STR, N) \ | |
214 | (memcpy ((PFILE)->limit, STR, (N)), (PFILE)->limit += (N)) | |
215 | /* Append string STR (of length N) to PFILE's output buffer. Make space. */ | |
216 | #define CPP_PUTS(PFILE, STR, N) CPP_RESERVE(PFILE, N), CPP_PUTS_Q(PFILE, STR,N) | |
217 | /* Append character CH to PFILE's output buffer. Assume sufficient space. */ | |
218 | #define CPP_PUTC_Q(PFILE, CH) (*(PFILE)->limit++ = (CH)) | |
219 | /* Append character CH to PFILE's output buffer. Make space if need be. */ | |
220 | #define CPP_PUTC(PFILE, CH) (CPP_RESERVE (PFILE, 1), CPP_PUTC_Q (PFILE, CH)) | |
88ae23e7 ZW |
221 | |
222 | /* Advance the current line by one. */ | |
223 | #define CPP_BUMP_BUFFER_LINE(PBUF) ((PBUF)->lineno++,\ | |
224 | (PBUF)->line_base = (PBUF)->cur) | |
225 | #define CPP_BUMP_LINE(PFILE) CPP_BUMP_BUFFER_LINE(CPP_BUFFER(PFILE)) | |
61474454 NB |
226 | #define CPP_BUMP_BUFFER_LINE_CUR(PBUF, CUR) ((PBUF)->lineno++,\ |
227 | (PBUF)->line_base = CUR) | |
228 | #define CPP_BUMP_LINE_CUR(PFILE, CUR) \ | |
229 | CPP_BUMP_BUFFER_LINE_CUR(CPP_BUFFER(PFILE), CUR) | |
88ae23e7 ZW |
230 | #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)->prev) |
231 | ||
ff2b53ef ZW |
232 | /* Are we in column 1 right now? Used mainly for -traditional handling |
233 | of directives. */ | |
234 | #define CPP_IN_COLUMN_1(PFILE) \ | |
235 | (CPP_BUFFER (PFILE)->cur - CPP_BUFFER (PFILE)->line_base == 1) | |
236 | ||
ae79697b ZW |
237 | #define CPP_PRINT_DEPS(PFILE) CPP_OPTION (PFILE, print_deps) |
238 | #define CPP_TRADITIONAL(PFILE) CPP_OPTION (PFILE, traditional) | |
88ae23e7 | 239 | #define CPP_PEDANTIC(PFILE) \ |
07aa0b04 ZW |
240 | (CPP_OPTION (PFILE, pedantic) && !CPP_BUFFER (PFILE)->system_header_p) |
241 | #define CPP_WTRADITIONAL(PF) \ | |
242 | (CPP_OPTION (PF, warn_traditional) && !CPP_BUFFER (PF)->system_header_p) | |
88ae23e7 | 243 | |
45b966db ZW |
244 | /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion. |
245 | (Note that it is false while we're expanding macro *arguments*.) */ | |
ff2b53ef | 246 | #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->macro != NULL) |
45b966db ZW |
247 | |
248 | /* Remember the current position of PFILE so it may be returned to | |
249 | after looking ahead a bit. | |
250 | ||
251 | Note that when you set a mark, you _must_ return to that mark. You | |
252 | may not forget about it and continue parsing. You may not pop a | |
253 | buffer with an active mark. You may not call CPP_BUMP_LINE while a | |
254 | mark is active. */ | |
ff2b53ef ZW |
255 | #define CPP_SET_BUF_MARK(IP) ((IP)->mark = (IP)->cur) |
256 | #define CPP_GOTO_BUF_MARK(IP) ((IP)->cur = (IP)->mark, (IP)->mark = 0) | |
45b966db ZW |
257 | #define CPP_SET_MARK(PFILE) CPP_SET_BUF_MARK(CPP_BUFFER(PFILE)) |
258 | #define CPP_GOTO_MARK(PFILE) CPP_GOTO_BUF_MARK(CPP_BUFFER(PFILE)) | |
259 | ||
260 | /* ACTIVE_MARK_P is true if there's a live mark in the buffer. */ | |
ff2b53ef ZW |
261 | #define ACTIVE_MARK_P(PFILE) (CPP_BUFFER (PFILE)->mark != 0) |
262 | ||
263 | /* Are mark and point adjacent characters? Used mostly to deal with | |
264 | the somewhat annoying semantic of #define. */ | |
265 | #define ADJACENT_TO_MARK(PFILE) \ | |
266 | (CPP_BUFFER(PFILE)->cur - CPP_BUFFER(PFILE)->mark == 1) | |
45b966db | 267 | |
88ae23e7 | 268 | /* In cpphash.c */ |
d35364d1 ZW |
269 | extern HASHNODE *_cpp_make_hashnode PARAMS ((const U_CHAR *, size_t, |
270 | enum node_type, | |
271 | unsigned long)); | |
29a72a4f | 272 | extern unsigned int _cpp_calc_hash PARAMS ((const U_CHAR *, size_t)); |
d35364d1 ZW |
273 | extern HASHNODE *_cpp_lookup PARAMS ((cpp_reader *, |
274 | const U_CHAR *, int)); | |
275 | extern HASHNODE **_cpp_lookup_slot PARAMS ((cpp_reader *, | |
e38992e8 RK |
276 | const U_CHAR *, int, |
277 | enum insert_option, | |
d35364d1 ZW |
278 | unsigned long *)); |
279 | extern void _cpp_free_definition PARAMS ((DEFINITION *)); | |
d9e0bd53 ZW |
280 | extern int _cpp_create_definition PARAMS ((cpp_reader *, |
281 | cpp_toklist *, HASHNODE *)); | |
282 | extern void _cpp_dump_definition PARAMS ((cpp_reader *, HASHNODE *)); | |
45b966db | 283 | extern void _cpp_quote_string PARAMS ((cpp_reader *, const char *)); |
d35364d1 ZW |
284 | extern void _cpp_macroexpand PARAMS ((cpp_reader *, HASHNODE *)); |
285 | extern void _cpp_init_macro_hash PARAMS ((cpp_reader *)); | |
286 | extern void _cpp_dump_macro_hash PARAMS ((cpp_reader *)); | |
bb52fa7f | 287 | |
88ae23e7 ZW |
288 | /* In cppfiles.c */ |
289 | extern void _cpp_simplify_pathname PARAMS ((char *)); | |
168d3732 ZW |
290 | extern void _cpp_execute_include PARAMS ((cpp_reader *, char *, |
291 | unsigned int, int, | |
292 | struct file_name_list *)); | |
d35364d1 | 293 | extern void _cpp_init_include_hash PARAMS ((cpp_reader *)); |
f2d5f0cc | 294 | extern const char *_cpp_fake_ihash PARAMS ((cpp_reader *, const char *)); |
88ae23e7 ZW |
295 | |
296 | /* In cppexp.c */ | |
297 | extern int _cpp_parse_expr PARAMS ((cpp_reader *)); | |
298 | ||
45b966db ZW |
299 | /* In cpplex.c */ |
300 | extern void _cpp_parse_name PARAMS ((cpp_reader *, int)); | |
301 | extern void _cpp_skip_rest_of_line PARAMS ((cpp_reader *)); | |
302 | extern void _cpp_skip_hspace PARAMS ((cpp_reader *)); | |
f2d5f0cc ZW |
303 | extern void _cpp_expand_to_buffer PARAMS ((cpp_reader *, |
304 | const unsigned char *, int)); | |
45b966db | 305 | extern int _cpp_parse_assertion PARAMS ((cpp_reader *)); |
3a2b2c7a | 306 | extern enum cpp_ttype _cpp_lex_token PARAMS ((cpp_reader *)); |
45b966db ZW |
307 | extern long _cpp_read_and_prescan PARAMS ((cpp_reader *, cpp_buffer *, |
308 | int, size_t)); | |
309 | extern void _cpp_init_input_buffer PARAMS ((cpp_reader *)); | |
310 | extern void _cpp_grow_token_buffer PARAMS ((cpp_reader *, long)); | |
3a2b2c7a | 311 | extern enum cpp_ttype _cpp_get_directive_token |
45b966db | 312 | PARAMS ((cpp_reader *)); |
3a2b2c7a | 313 | extern enum cpp_ttype _cpp_get_define_token |
ff2b53ef | 314 | PARAMS ((cpp_reader *)); |
1368ee70 | 315 | extern void _cpp_scan_line PARAMS ((cpp_reader *, cpp_toklist *)); |
45b966db ZW |
316 | |
317 | /* In cpplib.c */ | |
318 | extern int _cpp_handle_directive PARAMS ((cpp_reader *)); | |
319 | extern void _cpp_handle_eof PARAMS ((cpp_reader *)); | |
c5a04734 | 320 | extern void _cpp_check_directive PARAMS((cpp_toklist *, cpp_token *)); |
45b966db | 321 | |
bb52fa7f | 322 | #endif |