OSDN Git Service

* cppfiles.c (_cpp_fake_include): New function.
[pf3gnuchains/gcc-fork.git] / gcc / cpphash.h
1 /* Part of CPP library.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
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
22 #ifndef __GCC_CPPHASH__
23 #define __GCC_CPPHASH__
24
25 struct directive;               /* These are deliberately incomplete.  */
26 struct htab;
27
28 /* Test if a sign is valid within a preprocessing number.  */
29 #define VALID_SIGN(c, prevc) \
30   (((c) == '+' || (c) == '-') && \
31    ((prevc) == 'e' || (prevc) == 'E' \
32     || (((prevc) == 'p' || (prevc) == 'P') \
33         && CPP_OPTION (pfile, extended_numbers))))
34
35 #define CPP_OPTION(PFILE, OPTION) ((PFILE)->opts.OPTION)
36 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
37 #define CPP_BUF_LINE(BUF) ((BUF)->lineno)
38 #define CPP_BUF_COLUMN(BUF, CUR) ((CUR) - (BUF)->line_base + (BUF)->col_adjust)
39 #define CPP_BUF_COL(BUF) CPP_BUF_COLUMN(BUF, (BUF)->cur)
40
41 /* Maximum nesting of cpp_buffers.  We use a static limit, partly for
42    efficiency, and partly to limit runaway recursion.  */
43 #define CPP_STACK_MAX 200
44
45 /* Memory pools.  */
46 #define ALIGN(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
47 #define POOL_FRONT(p) ((p)->cur->front)
48 #define POOL_LIMIT(p) ((p)->cur->limit)
49 #define POOL_BASE(p)  ((p)->cur->base)
50 #define POOL_SIZE(p)  ((p)->cur->limit - (p)->cur->base)
51 #define POOL_ROOM(p)  ((p)->cur->limit - (p)->cur->front)
52 #define POOL_USED(p)  ((p)->cur->front - (p)->cur->base)
53 #define POOL_COMMIT(p, len) do {((p)->cur->front += ALIGN (len, (p)->align));\
54   if ((p)->cur->front > (p)->cur->limit) abort ();} while (0)
55
56 typedef struct cpp_chunk cpp_chunk;
57 struct cpp_chunk
58 {
59   cpp_chunk *next;
60   unsigned char *front;
61   unsigned char *limit;
62   unsigned char *base;
63 };
64
65 typedef struct cpp_pool cpp_pool;
66 struct cpp_pool
67 {
68   struct cpp_chunk *cur, *locked;
69   unsigned char *pos;           /* Current position.  */
70   unsigned int align;
71   unsigned int locks;
72 };
73
74 /* List of directories to look for include files in. */
75 struct file_name_list
76 {
77   struct file_name_list *next;
78   struct file_name_list *alloc; /* for the cache of
79                                    current directory entries */
80   char *name;
81   unsigned int nlen;
82   /* We use these to tell if the directory mentioned here is a duplicate
83      of an earlier directory on the search path. */
84   ino_t ino;
85   dev_t dev;
86   /* If the following is nonzero, it is a C-language system include
87      directory.  */
88   int sysp;
89   /* Mapping of file names for this directory.
90      Only used on MS-DOS and related platforms. */
91   struct file_name_map *name_map;
92 };
93
94 /* Multiple-include optimisation.  */
95 enum mi_state {MI_FAILED = 0, MI_OUTSIDE};
96 enum mi_ind {MI_IND_NONE = 0, MI_IND_NOT};
97
98 typedef struct toklist toklist;
99 struct toklist
100 {
101   cpp_token *first;
102   cpp_token *limit;
103 };
104
105 typedef struct cpp_context cpp_context;
106 struct cpp_context
107 {
108   /* Doubly-linked list.  */
109   cpp_context *next, *prev;
110
111   /* Contexts other than the base context are contiguous tokens.
112      e.g. macro expansions, expanded argument tokens.  */
113   struct toklist list;
114
115   /* For a macro context, these are the macro and its arguments.  */
116   cpp_macro *macro;
117 };
118
119 struct lexer_state
120 {
121   /* Nonzero if first token on line is CPP_HASH.  */
122   unsigned char in_directive;
123
124   /* Nonzero if in a directive that takes angle-bracketed headers.  */
125   unsigned char angled_headers;
126
127   /* Nonzero to save comments.  Turned off if discard_comments, and in
128      all directives apart from #define.  */
129   unsigned char save_comments;
130
131   /* If nonzero the next token is at the beginning of the line.  */
132   unsigned char next_bol;
133
134   /* Nonzero if we're mid-comment.  */
135   unsigned char lexing_comment;
136
137   /* Nonzero if lexing __VA_ARGS__ is valid.  */
138   unsigned char va_args_ok;
139
140   /* Nonzero if lexing poisoned identifiers is valid.  */
141   unsigned char poisoned_ok;
142
143   /* Nonzero to prevent macro expansion.  */
144   unsigned char prevent_expansion;  
145
146   /* Nonzero when parsing arguments to a function-like macro.  */
147   unsigned char parsing_args;
148
149   /* Nonzero when in a # NUMBER directive.  */
150   unsigned char line_extension;
151 };
152
153 /* Special nodes - identifiers with predefined significance.  */
154 struct spec_nodes
155 {
156   cpp_hashnode *n_L;                    /* L"str" */
157   cpp_hashnode *n_defined;              /* defined operator */
158   cpp_hashnode *n__Pragma;              /* _Pragma operator */
159   cpp_hashnode *n__STRICT_ANSI__;       /* STDC_0_IN_SYSTEM_HEADERS */
160   cpp_hashnode *n__CHAR_UNSIGNED__;     /* plain char is unsigned */
161   cpp_hashnode *n__VA_ARGS__;           /* C99 vararg macros */
162 };
163
164 struct cpp_buffer
165 {
166   const unsigned char *cur;      /* current position */
167   const unsigned char *rlimit; /* end of valid data */
168   const unsigned char *line_base; /* start of current line */
169   cppchar_t read_ahead;         /* read ahead character */
170   cppchar_t extra_char;         /* extra read-ahead for long tokens.  */
171
172   struct cpp_reader *pfile;     /* Owns this buffer.  */
173   struct cpp_buffer *prev;
174
175   const unsigned char *buf;      /* entire buffer */
176
177   /* Filename specified with #line command.  */
178   const char *nominal_fname;
179
180   /* Actual directory of this file, used only for "" includes */
181   struct file_name_list *actual_dir;
182
183   /* Pointer into the include table.  Used for include_next and
184      to record control macros. */
185   struct include_file *inc;
186
187   /* Value of if_stack at start of this file.
188      Used to prohibit unmatched #endif (etc) in an include file.  */
189   struct if_stack *if_stack;
190
191   /* Token column position adjustment owing to tabs in whitespace.  */
192   unsigned int col_adjust;
193
194   /* Line number at line_base (above). */
195   unsigned int lineno;
196
197   /* Because of the way the lexer works, -Wtrigraphs can sometimes
198      warn twice for the same trigraph.  This helps prevent that.  */
199   const unsigned char *last_Wtrigraphs;
200
201   /* True if we have already warned about C++ comments in this file.
202      The warning happens only for C89 extended mode with -pedantic on,
203      or for -Wtraditional, and only once per file (otherwise it would
204      be far too noisy).  */
205   unsigned char warned_cplusplus_comments;
206
207   /* True if we don't process trigraphs and escaped newlines.  True
208      for preprocessed input, command line directives, and _Pragma
209      buffers.  */
210   unsigned char from_stage3;
211
212   /* Temporary storage for pfile->skipping whilst in a directive.  */
213   unsigned char was_skipping;
214
215   /* 1 = system header file, 2 = C system header file used for C++.  */
216   unsigned char sysp;
217
218   /* Nonzero means we have printed (while error reporting) a list of
219      containing files that matches the current status.  */
220   unsigned char include_stack_listed;
221
222   /* Buffer type.  */
223   ENUM_BITFIELD (cpp_buffer_type) type : 8;
224 };
225
226 /* A cpp_reader encapsulates the "state" of a pre-processor run.
227    Applying cpp_get_token repeatedly yields a stream of pre-processor
228    tokens.  Usually, there is only one cpp_reader object active.  */
229
230 struct cpp_reader
231 {
232   /* Top of buffer stack.  */
233   cpp_buffer *buffer;
234
235   /* Lexer state.  */
236   struct lexer_state state;
237
238   /* The position of the last lexed token and last lexed directive.  */
239   cpp_lexer_pos lexer_pos;
240   cpp_lexer_pos directive_pos;
241
242   /* Memory pools.  */
243   cpp_pool ident_pool;          /* For all identifiers, and permanent
244                                    numbers and strings.  */
245   cpp_pool macro_pool;          /* For macro definitions.  Permanent.  */
246   cpp_pool argument_pool;       /* For macro arguments.  Temporary.   */
247
248   /* Context stack.  */
249   struct cpp_context base_context;
250   struct cpp_context *context;
251
252   /* If in_directive, the directive if known.  */
253   const struct directive *directive;
254
255   /* Multiple inlcude optimisation.  */
256   enum mi_state mi_state;
257   enum mi_ind mi_if_not_defined;
258   unsigned int mi_lexed;
259   const cpp_hashnode *mi_cmacro;
260   const cpp_hashnode *mi_ind_cmacro;
261
262   /* Token lookahead.  */
263   struct cpp_lookahead *la_read;        /* Read from this lookahead.  */
264   struct cpp_lookahead *la_write;       /* Write to this lookahead.  */
265   struct cpp_lookahead *la_unused;      /* Free store.  */
266   struct cpp_lookahead *la_saved;       /* Backup when entering directive.  */
267
268   /* Error counter for exit code.  */
269   unsigned int errors;
270
271   /* Line and column where a newline was first seen in a string
272      constant (multi-line strings).  */
273   cpp_lexer_pos mlstring_pos;
274
275   /* Buffer to hold macro definition string.  */
276   unsigned char *macro_buffer;
277   unsigned int macro_buffer_len;
278
279   /* Current depth in #include directives that use <...>.  */
280   unsigned int system_include_depth;
281
282   /* Current depth of buffer stack.  */
283   unsigned int buffer_stack_depth;
284
285   /* Current depth in #include directives.  */
286   unsigned int include_depth;
287
288   /* Hash table of macros and assertions.  See cpphash.c.  */
289   struct htab *hashtab;
290
291   /* Tree of other included files.  See cppfiles.c.  */
292   struct splay_tree_s *all_include_files;
293
294   /* Chain of `actual directory' file_name_list entries, for ""
295      inclusion.  */
296   struct file_name_list *actual_dirs;
297
298   /* Current maximum length of directory names in the search path
299      for include files.  (Altered as we get more of them.)  */
300   unsigned int max_include_len;
301
302   /* Date and time tokens.  Calculated together if either is requested.  */
303   cpp_token date;
304   cpp_token time;
305
306   /* Opaque handle to the dependencies of mkdeps.c.  Used by -M etc.  */
307   struct deps *deps;
308
309   /* Obstack holding all macro hash nodes.  This never shrinks.
310      See cpphash.c */
311   struct obstack *hash_ob;
312
313   /* Obstack holding buffer and conditional structures.  This is a
314      real stack.  See cpplib.c */
315   struct obstack *buffer_ob;
316
317   /* Pragma table - dynamic, because a library user can add to the
318      list of recognized pragmas.  */
319   struct pragma_entry *pragmas;
320
321   /* Call backs.  */
322   struct cpp_callbacks cb;
323
324   /* User visible options.  */
325   struct cpp_options opts;
326
327   /* Special nodes - identifiers with predefined significance to the
328      preprocessor.  */
329   struct spec_nodes spec_nodes;
330
331   /* We're printed a warning recommending against using #import.  */
332   unsigned char import_warning;
333
334   /* True after cpp_start_read completes.  Used to inhibit some
335      warnings while parsing the command line.  */
336   unsigned char done_initializing;
337
338   /* True if we are skipping a failed conditional group.  */
339   unsigned char skipping;
340 };
341
342 /* Character classes.  Based on the more primitive macros in safe-ctype.h.
343    If the definition of `numchar' looks odd to you, please look up the
344    definition of a pp-number in the C standard [section 6.4.8 of C99].
345
346    In the unlikely event that characters other than \r and \n enter
347    the set is_vspace, the macro handle_newline() in cpplex.c must be
348    updated.  */
349 #define _dollar_ok(x)   ((x) == '$' && CPP_OPTION (pfile, dollars_in_ident))
350
351 #define is_idchar(x)    (ISIDNUM(x) || _dollar_ok(x))
352 #define is_numchar(x)   ISIDNUM(x)
353 #define is_idstart(x)   (ISIDST(x) || _dollar_ok(x))
354 #define is_numstart(x)  ISDIGIT(x)
355 #define is_hspace(x)    ISBLANK(x)
356 #define is_vspace(x)    IS_VSPACE(x)
357 #define is_nvspace(x)   IS_NVSPACE(x)
358 #define is_space(x)     IS_SPACE_OR_NUL(x)
359
360 /* This table is constant if it can be initialized at compile time,
361    which is the case if cpp was compiled with GCC >=2.7, or another
362    compiler that supports C99.  */
363 #if HAVE_DESIGNATED_INITIALIZERS
364 extern const unsigned char _cpp_trigraph_map[UCHAR_MAX + 1];
365 #else
366 extern unsigned char _cpp_trigraph_map[UCHAR_MAX + 1];
367 #endif
368
369 /* Macros.  */
370
371 #define CPP_PREV_BUFFER(BUFFER) ((BUFFER)->prev)
372 #define CPP_PRINT_DEPS(PFILE) CPP_OPTION (PFILE, print_deps)
373 #define CPP_IN_SYSTEM_HEADER(PFILE) \
374   (CPP_BUFFER (PFILE) && CPP_BUFFER (PFILE)->sysp)
375 #define CPP_PEDANTIC(PF) CPP_OPTION (PF, pedantic)
376 #define CPP_WTRADITIONAL(PF) CPP_OPTION (PF, warn_traditional)
377
378 /* Hash step.  The hash calculation is duplicated in cpp_lookup and
379    parse_name.  */
380 #define HASHSTEP(r, c) ((r) * 67 + (c - 113));
381
382 /* In cpperror.c  */
383 enum error_type { WARNING = 0, PEDWARN, ERROR, FATAL, ICE };
384 extern int _cpp_begin_message PARAMS ((cpp_reader *, enum error_type,
385                                        const char *, const cpp_lexer_pos *));
386
387 /* In cppmacro.c */
388 extern void _cpp_free_definition        PARAMS ((cpp_hashnode *));
389 extern int _cpp_create_definition       PARAMS ((cpp_reader *, cpp_hashnode *));
390 extern void _cpp_pop_context            PARAMS ((cpp_reader *));
391 extern void _cpp_free_lookaheads        PARAMS ((cpp_reader *));
392 extern void _cpp_release_lookahead      PARAMS ((cpp_reader *));
393 extern void _cpp_push_token             PARAMS ((cpp_reader *, const cpp_token *,
394                                                  const cpp_lexer_pos *));
395
396 /* In cpphash.c */
397 extern void _cpp_init_hashtable         PARAMS ((cpp_reader *));
398 extern void _cpp_cleanup_hashtable      PARAMS ((cpp_reader *));
399 extern cpp_hashnode *_cpp_lookup_with_hash PARAMS ((cpp_reader*, size_t,
400                                                     unsigned int));
401
402 /* In cppfiles.c */
403 extern void _cpp_fake_include           PARAMS ((cpp_reader *, const char *));
404 extern void _cpp_never_reread           PARAMS ((struct include_file *));
405 extern void _cpp_simplify_pathname      PARAMS ((char *));
406 extern int _cpp_read_file               PARAMS ((cpp_reader *, const char *));
407 extern void _cpp_execute_include        PARAMS ((cpp_reader *,
408                                                  const cpp_token *, int, int));
409 extern int _cpp_compare_file_date       PARAMS ((cpp_reader *,
410                                                  const cpp_token *));
411 extern void _cpp_report_missing_guards  PARAMS ((cpp_reader *));
412 extern void _cpp_init_includes          PARAMS ((cpp_reader *));
413 extern void _cpp_cleanup_includes       PARAMS ((cpp_reader *));
414 extern void _cpp_pop_file_buffer        PARAMS ((cpp_reader *, cpp_buffer *));
415
416 /* In cppexp.c */
417 extern int _cpp_parse_expr              PARAMS ((cpp_reader *));
418
419 /* In cpplex.c */
420 extern void _cpp_lex_token              PARAMS ((cpp_reader *, cpp_token *));
421 extern int _cpp_equiv_tokens            PARAMS ((const cpp_token *,
422                                                  const cpp_token *));
423 extern void _cpp_init_pool              PARAMS ((cpp_pool *, unsigned int,
424                                                   unsigned int, unsigned int));
425 extern void _cpp_free_pool              PARAMS ((cpp_pool *));
426 extern unsigned char *_cpp_pool_reserve PARAMS ((cpp_pool *, unsigned int));
427 extern unsigned char *_cpp_pool_alloc   PARAMS ((cpp_pool *, unsigned int));
428 extern unsigned char *_cpp_next_chunk   PARAMS ((cpp_pool *, unsigned int,
429                                                  unsigned char **));
430 extern void _cpp_lock_pool              PARAMS ((cpp_pool *));
431 extern void _cpp_unlock_pool            PARAMS ((cpp_pool *));
432
433 /* In cpplib.c */
434 extern int _cpp_test_assertion PARAMS ((cpp_reader *, int *));
435 extern int _cpp_handle_directive PARAMS ((cpp_reader *, int));
436 extern void _cpp_define_builtin PARAMS ((cpp_reader *, const char *));
437 extern void _cpp_do__Pragma     PARAMS ((cpp_reader *));
438 extern void _cpp_init_stacks    PARAMS ((cpp_reader *));
439 extern void _cpp_cleanup_stacks PARAMS ((cpp_reader *));
440 extern void _cpp_init_internal_pragmas PARAMS ((cpp_reader *));
441 extern void _cpp_do_file_change PARAMS ((cpp_reader *, enum cpp_fc_reason,
442                                          const char *, unsigned int));
443
444 /* Utility routines and macros.  */
445 #define DSC(str) (const U_CHAR *)str, sizeof str - 1
446 #define xnew(T)         (T *) xmalloc (sizeof(T))
447 #define xcnew(T)        (T *) xcalloc (1, sizeof(T))
448 #define xnewvec(T, N)   (T *) xmalloc (sizeof(T) * (N))
449 #define xcnewvec(T, N)  (T *) xcalloc (N, sizeof(T))
450 #define xobnew(O, T)    (T *) obstack_alloc (O, sizeof(T))
451
452 /* These are inline functions instead of macros so we can get type
453    checking.  */
454 typedef unsigned char U_CHAR;
455 #define U (const U_CHAR *)  /* Intended use: U"string" */
456
457 static inline int ustrcmp       PARAMS ((const U_CHAR *, const U_CHAR *));
458 static inline int ustrncmp      PARAMS ((const U_CHAR *, const U_CHAR *,
459                                          size_t));
460 static inline size_t ustrlen    PARAMS ((const U_CHAR *));
461 static inline U_CHAR *uxstrdup  PARAMS ((const U_CHAR *));
462 static inline U_CHAR *ustrchr   PARAMS ((const U_CHAR *, int));
463 static inline int ufputs        PARAMS ((const U_CHAR *, FILE *));
464
465 static inline int
466 ustrcmp (s1, s2)
467      const U_CHAR *s1, *s2;
468 {
469   return strcmp ((const char *)s1, (const char *)s2);
470 }
471
472 static inline int
473 ustrncmp (s1, s2, n)
474      const U_CHAR *s1, *s2;
475      size_t n;
476 {
477   return strncmp ((const char *)s1, (const char *)s2, n);
478 }
479
480 static inline size_t
481 ustrlen (s1)
482      const U_CHAR *s1;
483 {
484   return strlen ((const char *)s1);
485 }
486
487 static inline U_CHAR *
488 uxstrdup (s1)
489      const U_CHAR *s1;
490 {
491   return (U_CHAR *) xstrdup ((const char *)s1);
492 }
493
494 static inline U_CHAR *
495 ustrchr (s1, c)
496      const U_CHAR *s1;
497      int c;
498 {
499   return (U_CHAR *) strchr ((const char *)s1, c);
500 }
501
502 static inline int
503 ufputs (s, f)
504      const U_CHAR *s;
505      FILE *f;
506 {
507   return fputs ((const char *)s, f);
508 }
509
510 #endif