1 /* Part of CPP library.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* This header defines all the internal data structures and functions
20 that need to be visible across files. It's called cpphash.h for
21 historical reasons. */
22
23 #ifndef GCC_CPPHASH_H
24 #define GCC_CPPHASH_H
25
26 #include "hashtable.h"
27
28 struct directive; /* Deliberately incomplete. */
29 struct pending_option;
30 struct op;
31
32 #ifndef HAVE_UCHAR
33 typedef unsigned char uchar;
34 #endif
35 #define U (const uchar *) /* Intended use: U"string" */
36
37 #define BITS_PER_CPPCHAR_T (CHAR_BIT * sizeof (cppchar_t))
38
39 /* Test if a sign is valid within a preprocessing number. */
40 #define VALID_SIGN(c, prevc) \
41 (((c) == '+' || (c) == '-') && \
42 ((prevc) == 'e' || (prevc) == 'E' \
43 || (((prevc) == 'p' || (prevc) == 'P') \
44 && CPP_OPTION (pfile, extended_numbers))))
45
46 #define CPP_OPTION(PFILE, OPTION) ((PFILE)->opts.OPTION)
47 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
48 #define CPP_BUF_COLUMN(BUF, CUR) ((CUR) - (BUF)->line_base + (BUF)->col_adjust)
49 #define CPP_BUF_COL(BUF) CPP_BUF_COLUMN(BUF, (BUF)->cur)
50
51 /* Maximum nesting of cpp_buffers. We use a static limit, partly for
52 efficiency, and partly to limit runaway recursion. */
53 #define CPP_STACK_MAX 200
54
55 /* Host alignment handling. */
56 struct dummy
57 {
58 char c;
59 union
60 {
61 double d;
62 int *p;
63 } u;
64 };
65
66 #define DEFAULT_ALIGNMENT offsetof (struct dummy, u)
67 #define CPP_ALIGN2(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
68 #define CPP_ALIGN(size) CPP_ALIGN2 (size, DEFAULT_ALIGNMENT)
69
70 /* Each macro definition is recorded in a cpp_macro structure.
71 Variadic macros cannot occur with traditional cpp. */
72 struct cpp_macro
73 {
74 /* Parameters, if any. */
75 cpp_hashnode **params;
76
77 /* Replacement tokens (ISO) or replacement text (traditional). See
78 comment at top of cpptrad.c for how traditional function-like
79 macros are encoded. */
80 union
81 {
82 cpp_token *tokens;
83 const uchar *text;
84 } exp;
85
86 /* Definition line number. */
87 unsigned int line;
88
89 /* Number of tokens in expansion, or bytes for traditional macros. */
90 unsigned int count;
91
92 /* Number of parameters. */
93 unsigned short paramc;
94
95 /* If a function-like macro. */
96 unsigned int fun_like : 1;
97
98 /* If a variadic macro. */
99 unsigned int variadic : 1;
100
101 /* If macro defined in system header. */
102 unsigned int syshdr : 1;
103
104 /* Nonzero if it has been expanded or had its existence tested. */
105 unsigned int used : 1;
106 };
107
108 #define _cpp_mark_macro_used(NODE) do { \
109 if ((NODE)->type == NT_MACRO && !((NODE)->flags & NODE_BUILTIN)) \
110 (NODE)->value.macro->used = 1; } while (0)
111
112 /* A generic memory buffer, and operations on it. */
113 typedef struct _cpp_buff _cpp_buff;
114 struct _cpp_buff
115 {
116 struct _cpp_buff *next;
117 unsigned char *base, *cur, *limit;
118 };
119
120 extern _cpp_buff *_cpp_get_buff PARAMS ((cpp_reader *, size_t));
121 extern void _cpp_release_buff PARAMS ((cpp_reader *, _cpp_buff *));
122 extern void _cpp_extend_buff PARAMS ((cpp_reader *, _cpp_buff **, size_t));
123 extern _cpp_buff *_cpp_append_extend_buff PARAMS ((cpp_reader *, _cpp_buff *,
124 size_t));
125 extern void _cpp_free_buff PARAMS ((_cpp_buff *));
126 extern unsigned char *_cpp_aligned_alloc PARAMS ((cpp_reader *, size_t));
127 extern unsigned char *_cpp_unaligned_alloc PARAMS ((cpp_reader *, size_t));
128
129 #define BUFF_ROOM(BUFF) (size_t) ((BUFF)->limit - (BUFF)->cur)
130 #define BUFF_FRONT(BUFF) ((BUFF)->cur)
131 #define BUFF_LIMIT(BUFF) ((BUFF)->limit)
132
133 /* List of directories to look for include files in. */
134 struct search_path
135 {
136 struct search_path *next;
137
138 /* NOTE: NAME may not be null terminated for the case of the current
139 file's directory! */
140 const char *name;
141 unsigned int len;
142 /* We use these to tell if the directory mentioned here is a duplicate
143 of an earlier directory on the search path. */
144 ino_t ino;
145 dev_t dev;
146 /* Nonzero if it is a system include directory. */
147 int sysp;
148 /* Mapping of file names for this directory. Only used on MS-DOS
149 and related platforms. */
150 struct file_name_map *name_map;
151 };
152
153 /* #include types. */
154 enum include_type {IT_INCLUDE, IT_INCLUDE_NEXT, IT_IMPORT, IT_CMDLINE};
155
156 union utoken
157 {
158 const cpp_token *token;
159 const cpp_token **ptoken;
160 };
161
162 /* A "run" of tokens; part of a chain of runs. */
163 typedef struct tokenrun tokenrun;
164 struct tokenrun
165 {
166 tokenrun *next, *prev;
167 cpp_token *base, *limit;
168 };
169
170 /* Accessor macros for struct cpp_context. */
171 #define FIRST(c) ((c)->u.iso.first)
172 #define LAST(c) ((c)->u.iso.last)
173 #define CUR(c) ((c)->u.trad.cur)
174 #define RLIMIT(c) ((c)->u.trad.rlimit)
175
176 typedef struct cpp_context cpp_context;
177 struct cpp_context
178 {
179 /* Doubly-linked list. */
180 cpp_context *next, *prev;
181
182 union
183 {
184 /* For ISO macro expansion. Contexts other than the base context
185 are contiguous tokens. e.g. macro expansions, expanded
186 argument tokens. */
187 struct
188 {
189 union utoken first;
190 union utoken last;
191 } iso;
192
193 /* For traditional macro expansion. */
194 struct
195 {
196 const uchar *cur;
197 const uchar *rlimit;
198 } trad;
199 } u;
200
201 /* If non-NULL, a buffer used for storage related to this context.
202 When the context is popped, the buffer is released. */
203 _cpp_buff *buff;
204
205 /* For a macro context, the macro node, otherwise NULL. */
206 cpp_hashnode *macro;
207
208 /* True if utoken element is token, else ptoken. */
209 bool direct_p;
210 };
211
212 struct lexer_state
213 {
214 /* Nonzero if first token on line is CPP_HASH. */
215 unsigned char in_directive;
216
217 /* Nonzero if in a directive that will handle padding tokens itself.
218 #include needs this to avoid problems with computed include and
219 spacing between tokens. */
220 unsigned char directive_wants_padding;
221
222 /* True if we are skipping a failed conditional group. */
223 unsigned char skipping;
224
225 /* Nonzero if in a directive that takes angle-bracketed headers. */
226 unsigned char angled_headers;
227
228 /* Nonzero if in a #if or #elif directive. */
229 unsigned char in_expression;
230
231 /* Nonzero to save comments. Turned off if discard_comments, and in
232 all directives apart from #define. */
233 unsigned char save_comments;
234
235 /* Nonzero if we're mid-comment. */
236 unsigned char lexing_comment;
237
238 /* Nonzero if lexing __VA_ARGS__ is valid. */
239 unsigned char va_args_ok;
240
241 /* Nonzero if lexing poisoned identifiers is valid. */
242 unsigned char poisoned_ok;
243
244 /* Nonzero to prevent macro expansion. */
245 unsigned char prevent_expansion;
246
247 /* Nonzero when parsing arguments to a function-like macro. */
248 unsigned char parsing_args;
249
250 /* Nonzero to skip evaluating part of an expression. */
251 unsigned int skip_eval;
252 };
253
254 /* Special nodes - identifiers with predefined significance. */
255 struct spec_nodes
256 {
257 cpp_hashnode *n_defined; /* defined operator */
258 cpp_hashnode *n_true; /* C++ keyword true */
259 cpp_hashnode *n_false; /* C++ keyword false */
260 cpp_hashnode *n__VA_ARGS__; /* C99 vararg macros */
261 };
262
263 /* Encapsulates state used to convert a stream of tokens into a text
264 file. */
265 struct printer
266 {
267 FILE *outf; /* Stream to write to. */
268 const struct line_map *map; /* Logical to physical line mappings. */
269 const cpp_token *prev; /* Previous token. */
270 const cpp_token *source; /* Source token for spacing. */
271 unsigned int line; /* Line currently being written. */
272 unsigned char printed; /* Nonzero if something output at line. */
273 };
274
275 /* Represents the contents of a file cpplib has read in. */
276 struct cpp_buffer
277 {
278 const unsigned char *cur; /* current position */
279 const unsigned char *backup_to; /* if peeked character is not wanted */
280 const unsigned char *rlimit; /* end of valid data */
281 const unsigned char *line_base; /* start of current line */
282
283 struct cpp_buffer *prev;
284
285 const unsigned char *buf; /* Entire character buffer. */
286
287 /* Pointer into the include table; non-NULL if this is a file
288 buffer. Used for include_next and to record control macros. */
289 struct include_file *inc;
290
291 /* Value of if_stack at start of this file.
292 Used to prohibit unmatched #endif (etc) in an include file. */
293 struct if_stack *if_stack;
294
295 /* Token column position adjustment owing to tabs in whitespace. */
296 unsigned int col_adjust;
297
298 /* Contains PREV_WHITE and/or AVOID_LPASTE. */
299 unsigned char saved_flags;
300
301 /* Because of the way the lexer works, -Wtrigraphs can sometimes
302 warn twice for the same trigraph. This helps prevent that. */
303 const unsigned char *last_Wtrigraphs;
304
305 /* True if we have already warned about C++ comments in this file.
306 The warning happens only for C89 extended mode with -pedantic on,
307 or for -Wtraditional, and only once per file (otherwise it would
308 be far too noisy). */
309 unsigned char warned_cplusplus_comments;
310
311 /* True if we don't process trigraphs and escaped newlines. True
312 for preprocessed input, command line directives, and _Pragma
313 buffers. */
314 unsigned char from_stage3;
315
316 /* Nonzero means that the directory to start searching for ""
317 include files has been calculated and stored in "dir" below. */
318 unsigned char search_cached;
319
320 /* At EOF, a buffer is automatically popped. If RETURN_AT_EOF is
321 true, a CPP_EOF token is then returned. Otherwise, the next
322 token from the enclosing buffer is returned. */
323 bool return_at_eof;
324
325 /* The directory of the this buffer's file. Its NAME member is not
326 allocated, so we don't need to worry about freeing it. */
327 struct search_path dir;
328
329 /* Used for buffer overlays by cpptrad.c. */
330 const uchar *saved_cur, *saved_rlimit;
331 };
332
333 /* A cpp_reader encapsulates the "state" of a pre-processor run.
334 Applying cpp_get_token repeatedly yields a stream of pre-processor
335 tokens. Usually, there is only one cpp_reader object active. */
336 struct cpp_reader
337 {
338 /* Top of buffer stack. */
339 cpp_buffer *buffer;
340
341 /* Overlaid buffer (can be different after processing #include). */
342 cpp_buffer *overlaid_buffer;
343
344 /* Lexer state. */
345 struct lexer_state state;
346
347 /* Source line tracking. */
348 struct line_maps line_maps;
349 const struct line_map *map;
350 unsigned int line;
351
352 /* The line of the '#' of the current directive. */
353 unsigned int directive_line;
354
355 /* Memory buffers. */
356 _cpp_buff *a_buff; /* Aligned permanent storage. */
357 _cpp_buff *u_buff; /* Unaligned permanent storage. */
358 _cpp_buff *free_buffs; /* Free buffer chain. */
359
360 /* Context stack. */
361 struct cpp_context base_context;
362 struct cpp_context *context;
363
364 /* If in_directive, the directive if known. */
365 const struct directive *directive;
366
367 /* The next -include-d file; NULL if they all are done. If it
368 points to NULL, the last one is in progress, and
369 _cpp_maybe_push_include_file has yet to restore the line map. */
370 struct pending_option **next_include_file;
371
372 /* Multiple inlcude optimisation. */
373 const cpp_hashnode *mi_cmacro;
374 const cpp_hashnode *mi_ind_cmacro;
375 bool mi_valid;
376
377 /* Lexing. */
378 cpp_token *cur_token;
379 tokenrun base_run, *cur_run;
380 unsigned int lookaheads;
381
382 /* Nonzero prevents the lexer from re-using the token runs. */
383 unsigned int keep_tokens;
384
385 /* Error counter for exit code. */
386 unsigned int errors;
387
388 /* Buffer to hold macro definition string. */
389 unsigned char *macro_buffer;
390 unsigned int macro_buffer_len;
391
392 /* Tree of other included files. See cppfiles.c. */
393 struct splay_tree_s *all_include_files;
394
395 /* Current maximum length of directory names in the search path
396 for include files. (Altered as we get more of them.) */
397 unsigned int max_include_len;
398
399 /* Macros on or after this line are warned about if unused. */
400 unsigned int first_unused_line;
401
402 /* Date and time text. Calculated together if either is requested. */
403 const uchar *date;
404 const uchar *time;
405
406 /* EOF token, and a token forcing paste avoidance. */
407 cpp_token avoid_paste;
408 cpp_token eof;
409
410 /* Opaque handle to the dependencies of mkdeps.c. */
411 struct deps *deps;
412
413 /* Obstack holding all macro hash nodes. This never shrinks.
414 See cpphash.c */
415 struct obstack hash_ob;
416
417 /* Obstack holding buffer and conditional structures. This is a
418 real stack. See cpplib.c. */
419 struct obstack buffer_ob;
420
421 /* Pragma table - dynamic, because a library user can add to the
422 list of recognized pragmas. */
423 struct pragma_entry *pragmas;
424
425 /* Call backs. */
426 struct cpp_callbacks cb;
427
428 /* Identifier hash table. */
429 struct ht *hash_table;
430
431 /* Expression parser stack. */
432 struct op *op_stack, *op_limit;
433
434 /* User visible options. */
435 struct cpp_options opts;
436
437 /* Special nodes - identifiers with predefined significance to the
438 preprocessor. */
439 struct spec_nodes spec_nodes;
440
441 /* Used when doing preprocessed output. */
442 struct printer print;
443
444 /* Whether cpplib owns the hashtable. */
445 unsigned char our_hashtable;
446
447 /* Traditional preprocessing output buffer (a logical line). */
448 struct
449 {
450 uchar *base;
451 uchar *limit;
452 uchar *cur;
453 unsigned int first_line;
454 } out;
455
456 /* Used to save the original line number during traditional
457 preprocessing. */
458 unsigned int saved_line;
459 };
460
461 /* Character classes. Based on the more primitive macros in safe-ctype.h.
462 If the definition of `numchar' looks odd to you, please look up the
463 definition of a pp-number in the C standard [section 6.4.8 of C99].
464
465 In the unlikely event that characters other than \r and \n enter
466 the set is_vspace, the macro handle_newline() in cpplex.c must be
467 updated. */
468 #define _dollar_ok(x) ((x) == '$' && CPP_OPTION (pfile, dollars_in_ident))
469
470 #define is_idchar(x) (ISIDNUM(x) || _dollar_ok(x))
471 #define is_numchar(x) ISIDNUM(x)
472 #define is_idstart(x) (ISIDST(x) || _dollar_ok(x))
473 #define is_numstart(x) ISDIGIT(x)
474 #define is_hspace(x) ISBLANK(x)
475 #define is_vspace(x) IS_VSPACE(x)
476 #define is_nvspace(x) IS_NVSPACE(x)
477 #define is_space(x) IS_SPACE_OR_NUL(x)
478
479 /* This table is constant if it can be initialized at compile time,
480 which is the case if cpp was compiled with GCC >=2.7, or another
481 compiler that supports C99. */
482 #if HAVE_DESIGNATED_INITIALIZERS
483 extern const unsigned char _cpp_trigraph_map[UCHAR_MAX + 1];
484 #else
485 extern unsigned char _cpp_trigraph_map[UCHAR_MAX + 1];
486 #endif
487
488 /* Macros. */
489
490 #define CPP_IN_SYSTEM_HEADER(PFILE) ((PFILE)->map && (PFILE)->map->sysp)
491 #define CPP_PEDANTIC(PF) CPP_OPTION (PF, pedantic)
492 #define CPP_WTRADITIONAL(PF) CPP_OPTION (PF, warn_traditional)
493
494 /* In cpperror.c */
495 extern int _cpp_begin_message PARAMS ((cpp_reader *, int,
496 unsigned int, unsigned int));
497
498 /* In cppmacro.c */
499 extern void _cpp_free_definition PARAMS ((cpp_hashnode *));
500 extern bool _cpp_create_definition PARAMS ((cpp_reader *, cpp_hashnode *));
501 extern void _cpp_pop_context PARAMS ((cpp_reader *));
502 extern void _cpp_push_text_context PARAMS ((cpp_reader *, cpp_hashnode *,
503 const uchar *, size_t));
504 extern bool _cpp_save_parameter PARAMS ((cpp_reader *, cpp_macro *,
505 cpp_hashnode *));
506 extern bool _cpp_arguments_ok PARAMS ((cpp_reader *, cpp_macro *,
507 const cpp_hashnode *,
508 unsigned int));
509 extern const uchar *_cpp_builtin_macro_text PARAMS ((cpp_reader *,
510 cpp_hashnode *));
511 int _cpp_warn_if_unused_macro PARAMS ((cpp_reader *, cpp_hashnode *,
512 void *));
513 /* In cpphash.c */
514 extern void _cpp_init_hashtable PARAMS ((cpp_reader *, hash_table *));
515 extern void _cpp_destroy_hashtable PARAMS ((cpp_reader *));
516
517 /* In cppfiles.c */
518 extern void _cpp_fake_include PARAMS ((cpp_reader *, const char *));
519 extern void _cpp_never_reread PARAMS ((struct include_file *));
520 extern char *_cpp_simplify_pathname PARAMS ((char *));
521 extern bool _cpp_read_file PARAMS ((cpp_reader *, const char *));
522 extern bool _cpp_execute_include PARAMS ((cpp_reader *,
523 const cpp_token *,
524 enum include_type));
525 extern int _cpp_compare_file_date PARAMS ((cpp_reader *,
526 const cpp_token *));
527 extern void _cpp_report_missing_guards PARAMS ((cpp_reader *));
528 extern void _cpp_init_includes PARAMS ((cpp_reader *));
529 extern void _cpp_cleanup_includes PARAMS ((cpp_reader *));
530 extern void _cpp_pop_file_buffer PARAMS ((cpp_reader *,
531 struct include_file *));
532
533 /* In cppexp.c */
534 extern bool _cpp_parse_expr PARAMS ((cpp_reader *));
535 extern struct op *_cpp_expand_op_stack PARAMS ((cpp_reader *));
536
537 /* In cpplex.c */
538 extern cpp_token *_cpp_temp_token PARAMS ((cpp_reader *));
539 extern const cpp_token *_cpp_lex_token PARAMS ((cpp_reader *));
540 extern cpp_token *_cpp_lex_direct PARAMS ((cpp_reader *));
541 extern int _cpp_equiv_tokens PARAMS ((const cpp_token *,
542 const cpp_token *));
543 extern void _cpp_init_tokenrun PARAMS ((tokenrun *, unsigned int));
544
545 /* In cppinit.c. */
546 extern void _cpp_maybe_push_include_file PARAMS ((cpp_reader *));
547
548 /* In cpplib.c */
549 extern int _cpp_test_assertion PARAMS ((cpp_reader *, unsigned int *));
550 extern int _cpp_handle_directive PARAMS ((cpp_reader *, int));
551 extern void _cpp_define_builtin PARAMS ((cpp_reader *, const char *));
552 extern void _cpp_do__Pragma PARAMS ((cpp_reader *));
553 extern void _cpp_init_directives PARAMS ((cpp_reader *));
554 extern void _cpp_init_internal_pragmas PARAMS ((cpp_reader *));
555 extern void _cpp_do_file_change PARAMS ((cpp_reader *, enum lc_reason,
556 const char *,
557 unsigned int, unsigned int));
558 extern void _cpp_pop_buffer PARAMS ((cpp_reader *));
559
560 /* In cpptrad.c. */
561 extern bool _cpp_read_logical_line_trad PARAMS ((cpp_reader *));
562 extern void _cpp_overlay_buffer PARAMS ((cpp_reader *pfile, const uchar *,
563 size_t));
564 extern void _cpp_remove_overlay PARAMS ((cpp_reader *));
565 extern bool _cpp_create_trad_definition PARAMS ((cpp_reader *, cpp_macro *));
566 extern bool _cpp_expansions_different_trad PARAMS ((const cpp_macro *,
567 const cpp_macro *));
568 extern uchar *_cpp_copy_replacement_text PARAMS ((const cpp_macro *, uchar *));
569 extern size_t _cpp_replacement_text_len PARAMS ((const cpp_macro *));
570
571 /* Utility routines and macros. */
572 #define DSC(str) (const uchar *)str, sizeof str - 1
573 #define xnew(T) (T *) xmalloc (sizeof(T))
574 #define xcnew(T) (T *) xcalloc (1, sizeof(T))
575 #define xnewvec(T, N) (T *) xmalloc (sizeof(T) * (N))
576 #define xcnewvec(T, N) (T *) xcalloc (N, sizeof(T))
577 #define xobnew(O, T) (T *) obstack_alloc (O, sizeof(T))
578
579 /* These are inline functions instead of macros so we can get type
580 checking. */
581 static inline int ustrcmp PARAMS ((const uchar *, const uchar *));
582 static inline int ustrncmp PARAMS ((const uchar *, const uchar *,
583 size_t));
584 static inline size_t ustrlen PARAMS ((const uchar *));
585 static inline uchar *uxstrdup PARAMS ((const uchar *));
586 static inline uchar *ustrchr PARAMS ((const uchar *, int));
587 static inline int ufputs PARAMS ((const uchar *, FILE *));
588
589 static inline int
ustrcmp(s1,s2)590 ustrcmp (s1, s2)
591 const uchar *s1, *s2;
592 {
593 return strcmp ((const char *)s1, (const char *)s2);
594 }
595
596 static inline int
ustrncmp(s1,s2,n)597 ustrncmp (s1, s2, n)
598 const uchar *s1, *s2;
599 size_t n;
600 {
601 return strncmp ((const char *)s1, (const char *)s2, n);
602 }
603
604 static inline size_t
ustrlen(s1)605 ustrlen (s1)
606 const uchar *s1;
607 {
608 return strlen ((const char *)s1);
609 }
610
611 static inline uchar *
uxstrdup(s1)612 uxstrdup (s1)
613 const uchar *s1;
614 {
615 return (uchar *) xstrdup ((const char *)s1);
616 }
617
618 static inline uchar *
ustrchr(s1,c)619 ustrchr (s1, c)
620 const uchar *s1;
621 int c;
622 {
623 return (uchar *) strchr ((const char *)s1, c);
624 }
625
626 static inline int
ufputs(s,f)627 ufputs (s, f)
628 const uchar *s;
629 FILE *f;
630 {
631 return fputs ((const char *)s, f);
632 }
633
634 #endif /* ! GCC_CPPHASH_H */
635