OSDN Git Service

366196d686a58ef50fbcd4b30ec3b4ea5a22b9b4
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.h
1 /* Definitions for CPP library.
2    Copyright (C) 1995, 96-99, 2000 Free Software Foundation, Inc.
3    Written by Per Bothner, 1994-95.
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  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!  */
22 #ifndef __GCC_CPPLIB__
23 #define __GCC_CPPLIB__
24
25 #include <sys/types.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 typedef struct cpp_reader cpp_reader;
32 typedef struct cpp_buffer cpp_buffer;
33 typedef struct cpp_options cpp_options;
34 typedef struct cpp_printer cpp_printer;
35
36 enum cpp_ttype
37 {
38   CPP_EOF = -1,
39   CPP_OTHER = 0,
40   CPP_COMMENT = 1,
41   CPP_HSPACE,
42   CPP_VSPACE, /* newlines and #line directives */
43   CPP_NAME,
44   CPP_MACRO,
45   CPP_NUMBER,
46   CPP_CHAR,
47   CPP_WCHAR,
48   CPP_STRING,
49   CPP_WSTRING,
50   CPP_DIRECTIVE,
51   CPP_ASSERTION,        /* #machine(a29k) */
52   CPP_STRINGIZE,        /* stringize macro argument */
53   CPP_TOKPASTE,         /* paste macro arg with next/prev token */
54   CPP_LPAREN,           /* "(" */
55   CPP_RPAREN,           /* ")" */
56   CPP_LBRACE,           /* "{" */
57   CPP_RBRACE,           /* "}" */
58   CPP_COMMA,            /* "," */
59   CPP_SEMICOLON,        /* ";" */
60   CPP_3DOTS,            /* "..." */
61   CPP_POP               /* We're about to pop the buffer stack.  */
62 };
63
64 typedef int (*parse_cleanup_t) PARAMS((cpp_buffer *, cpp_reader *));
65
66 struct cpp_buffer
67 {
68   const unsigned char *cur;      /* current position */
69   const unsigned char *rlimit; /* end of valid data */
70   const unsigned char *buf;      /* entire buffer */
71   const unsigned char *line_base; /* start of current line */
72   const unsigned char *mark;  /* Saved position for lengthy backtrack. */
73
74   struct cpp_buffer *prev;
75
76   /* Filename specified with #line command.  */
77   const char *nominal_fname;
78   /* Actual directory of this file, used only for "" includes */
79   struct file_name_list *actual_dir;
80
81   /* Pointer into the include hash table.  Used for include_next and
82      to record control macros. */
83   struct ihash *ihash;
84
85   parse_cleanup_t cleanup;
86
87   /* If the buffer is the expansion of a macro, this points to the
88      macro's hash table entry.  */
89   struct hashnode *macro;
90
91   /* Value of if_stack at start of this file.
92      Used to prohibit unmatched #endif (etc) in an include file.  */
93   struct if_stack *if_stack;
94
95   /* Line number at line_base (above). */
96   unsigned int lineno;
97
98   /* True if this is a header file included using <FILENAME>.  */
99   char system_header_p;
100
101   /* True if end-of-file has already been hit once in this buffer.  */
102   char seen_eof;
103
104   /* True if buffer contains escape sequences.
105      Currently there are two kinds:
106      "\r-" means following identifier should not be macro-expanded.
107      "\r " means a token-separator.  This turns into " " in final output
108           if not stringizing and needed to separate tokens; otherwise nothing.
109      Any other two-character sequence beginning with \r is an error.
110
111      If this is NOT set, then \r is a one-character escape meaning backslash
112      newline.  This is guaranteed not to occur in the middle of a token.
113      The two interpretations of \r do not conflict, because the two-character
114      escapes are used only in macro buffers, and backslash-newline is removed
115      from macro expansion text in collect_expansion and/or macarg.  */
116   char has_escapes;
117
118   /* Used by the C++ frontend to implement redirected input (such as for
119      default argument and/or template parsing).  */
120   char manual_pop;
121
122   /* True if we have already warned about C++ comments in this file.
123      The warning happens only for C89 extended mode with -pedantic on,
124      and only once per file (otherwise it would be far too noisy).  */
125   char warned_cplusplus_comments;
126 };
127
128 struct file_name_map_list;
129 struct htab;
130
131 /* Maximum nesting of cpp_buffers.  We use a static limit, partly for
132    efficiency, and partly to limit runaway recursion.  */
133 #define CPP_STACK_MAX 200
134
135 /* Values for opts.dump_macros.
136   dump_only means inhibit output of the preprocessed text
137              and instead output the definitions of all user-defined
138              macros in a form suitable for use as input to cccp.
139    dump_names means pass #define and the macro name through to output.
140    dump_definitions means pass the whole definition (plus #define) through
141 */
142 enum { dump_none = 0, dump_only, dump_names, dump_definitions };
143
144 /* This structure is nested inside struct cpp_reader, and
145    carries all the options visible to the command line.  */
146 struct cpp_options
147 {
148   /* Name of input and output files.  */
149   const char *in_fname;
150   const char *out_fname;
151
152   /* Pending options - -D, -U, -A, -I, -ixxx. */
153   struct cpp_pending *pending;
154
155   /* File name which deps are being written to.  This is 0 if deps are
156      being written to stdout.  */
157   const char *deps_file;
158
159   /* Target-name to write with the dependency information.  */
160   char *deps_target;
161
162   /* Search paths for include files.  */
163   struct file_name_list *quote_include;  /* First dir to search for "file" */
164   struct file_name_list *bracket_include;/* First dir to search for <file> */
165
166   /* Map between header names and file names, used only on DOS where
167      file names are limited in length.  */
168   struct file_name_map_list *map_list;
169
170   /* Directory prefix that should replace `/usr/lib/gcc-lib/TARGET/VERSION'
171      in the standard include file directories.  */
172   const char *include_prefix;
173   unsigned int include_prefix_len;
174
175   /* Non-0 means -v, so print the full set of include dirs.  */
176   unsigned char verbose;
177
178   /* Nonzero means use extra default include directories for C++.  */
179   unsigned char cplusplus;
180
181   /* Nonzero means handle cplusplus style comments */
182   unsigned char cplusplus_comments;
183
184   /* Nonzero means handle #import, for objective C.  */
185   unsigned char objc;
186
187   /* Nonzero means this is an assembly file, so ignore unrecognized
188      directives and the "# 33" form of #line, both of which are
189      probably comments.  Also, permit unbalanced ' strings (again,
190      likely to be in comments).  */
191   unsigned char lang_asm;
192
193   /* Nonzero means this is Fortran, and we don't know where the
194      comments are, so permit unbalanced ' strings.  Unlike lang_asm,
195      this does not ignore unrecognized directives.  */
196   unsigned char lang_fortran;
197
198   /* Nonzero means handle CHILL comment syntax and output CHILL string
199      delimiters for __DATE__ etc. */
200   unsigned char chill;
201
202   /* Nonzero means don't copy comments into the output file.  */
203   unsigned char discard_comments;
204
205   /* Nonzero means process the ANSI trigraph sequences.  */
206   unsigned char trigraphs;
207
208   /* Nonzero means print the names of included files rather than the
209      preprocessed output.  1 means just the #include "...", 2 means
210      #include <...> as well.  */
211   unsigned char print_deps;
212
213   /* Nonzero if missing .h files in -M output are assumed to be
214      generated files and not errors.  */
215   unsigned char print_deps_missing_files;
216
217   /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
218   unsigned char print_deps_append;
219
220   /* Nonzero means print names of header files (-H).  */
221   unsigned char print_include_names;
222
223   /* Nonzero means cpp_pedwarn causes a hard error.  */
224   unsigned char pedantic_errors;
225
226   /* Nonzero means don't print warning messages.  */
227   unsigned char inhibit_warnings;
228
229   /* Nonzero means don't print error messages.  Has no option to
230      select it, but can be set by a user of cpplib (e.g. fix-header).  */
231   unsigned char inhibit_errors;
232
233   /* Nonzero means warn if slash-star appears in a comment.  */
234   unsigned char warn_comments;
235
236   /* Nonzero means warn if there are any trigraphs.  */
237   unsigned char warn_trigraphs;
238
239   /* Nonzero means warn if #import is used.  */
240   unsigned char warn_import;
241
242   /* Nonzero means warn if a macro argument is (or would be)
243      stringified with -traditional, and warn about directives
244      with the # indented from the beginning of the line.  */
245   unsigned char warn_traditional;
246
247   /* Nonzero means turn warnings into errors.  */
248   unsigned char warnings_are_errors;
249
250   /* Nonzero causes output not to be done, but directives such as
251      #define that have side effects are still obeyed.  */
252   unsigned char no_output;
253
254   /* Nonzero means we should look for header.gcc files that remap file
255      names.  */
256   unsigned char remap;
257
258   /* Nonzero means don't output line number information.  */
259   unsigned char no_line_commands;
260
261   /* Nonzero means -I- has been seen, so don't look for #include "foo"
262      the source-file directory.  */
263   unsigned char ignore_srcdir;
264
265   /* Zero means dollar signs are punctuation. */
266   unsigned char dollars_in_ident;
267
268   /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
269   unsigned char traditional;
270
271   /* Nonzero means warn if undefined identifiers are evaluated in an #if.  */
272   unsigned char warn_undef;
273
274   /* Nonzero for the 1989 C Standard, including corrigenda and amendments.  */
275   unsigned char c89;
276
277   /* Nonzero for the 1999 C Standard, including corrigenda and amendments.  */
278   unsigned char c99;
279
280   /* Nonzero means give all the error messages the ANSI standard requires.  */
281   unsigned char pedantic;
282
283   /* Nonzero means we're looking at already preprocessed code, so don't
284      bother trying to do macro expansion and whatnot.  */
285   unsigned char preprocessed;
286
287   /* Nonzero disables all the standard directories for headers.  */
288   unsigned char no_standard_includes;
289
290   /* Nonzero disables the C++-specific standard directories for headers.  */
291   unsigned char no_standard_cplusplus_includes;
292
293   /* Nonzero means dump macros in some fashion - see above.  */
294   unsigned char dump_macros;
295
296   /* Nonzero means pass all #define and #undef directives which we
297      actually process through to the output stream.  This feature is
298      used primarily to allow cc1 to record the #defines and #undefs
299      for the sake of debuggers which understand about preprocessor
300      macros, but it may also be useful with -E to figure out how
301      symbols are defined, and where they are defined.  */
302   unsigned char debug_output;
303
304   /* Nonzero means pass #include lines through to the output.  */
305   unsigned char dump_includes;
306
307   /* Print column number in error messages.  */
308   unsigned char show_column;
309 };
310
311
312 /* A cpp_reader encapsulates the "state" of a pre-processor run.
313    Applying cpp_get_token repeatedly yields a stream of pre-processor
314    tokens.  Usually, there is only one cpp_reader object active. */
315
316 struct cpp_reader
317 {
318   cpp_buffer *buffer;
319
320   /* A buffer used for both for cpp_get_token's output, and also internally. */
321   unsigned char *token_buffer;
322   /* Allocated size of token_buffer.  CPP_RESERVE allocates space.  */
323   unsigned int token_buffer_size;
324   /* End of the written part of token_buffer. */
325   unsigned char *limit;
326
327   /* Error counter for exit code */
328   unsigned int errors;
329
330   /* Line where a newline was first seen in a string constant.  */
331   unsigned int multiline_string_line;
332
333   /* Current depth in #include directives that use <...>.  */
334   unsigned int system_include_depth;
335
336   /* Current depth of buffer stack. */
337   unsigned int buffer_stack_depth;
338
339   /* Hash table of macros and assertions.  See cpphash.c */
340   struct htab *hashtab;
341
342   /* Hash table of other included files.  See cppfiles.c */
343   struct htab *all_include_files;
344
345   /* Chain of `actual directory' file_name_list entries,
346      for "" inclusion. */
347   struct file_name_list *actual_dirs;
348
349   /* Current maximum length of directory names in the search path
350      for include files.  (Altered as we get more of them.)  */
351   unsigned int max_include_len;
352
353   struct if_stack *if_stack;
354   const unsigned char *potential_control_macro;
355
356   /* Buffer of -M output.  */
357   struct deps *deps;
358
359   /* A buffer used only by read_and_prescan (in cppfiles.c), which is
360      allocated once per cpp_reader object to keep it off the stack.  */
361   unsigned char *input_buffer;
362   size_t input_buffer_len;
363
364   /* User visible options.  */
365   struct cpp_options opts;
366
367   /* Nonzero means we have printed (while error reporting) a list of
368      containing files that matches the current status.  */
369   unsigned char input_stack_listing_current;
370
371   /* If non-zero, macros are not expanded.  */
372   unsigned char no_macro_expand;
373
374   /* If non-zero, directives cause a hard error.  Used when parsing
375      macro arguments.  */
376   unsigned char no_directives;
377
378   /* We're printed a warning recommending against using #import.  */
379   unsigned char import_warning;
380
381   /* If true, characters between '<' and '>' are a single (string) token.  */
382   unsigned char parsing_include_directive;
383
384   /* If true, # introduces an assertion (see do_assert) */
385   unsigned char parsing_if_directive;
386
387   /* If true, # and ## are the STRINGIZE and TOKPASTE operators */
388   unsigned char parsing_define_directive;
389
390   /* True if escape sequences (as described for has_escapes in
391      parse_buffer) should be emitted.  */
392   unsigned char output_escapes;
393
394   /* 0: Have seen non-white-space on this line.
395      1: Only seen white space so far on this line.
396      2: Only seen white space so far in this file.  */
397   unsigned char only_seen_white;
398
399   /* True after cpp_start_read completes.  Used to inhibit some
400      warnings while parsing the command line.  */
401   unsigned char done_initializing;
402 };
403
404 /* struct cpp_printer encapsulates state used to convert the stream of
405    tokens coming from cpp_get_token back into a text file.  Not
406    everyone wants to do that, hence we separate the function.  */
407
408 struct cpp_printer
409 {
410   FILE *outf;                   /* stream to write to */
411   const char *last_fname;       /* previous file name */
412   unsigned int last_bsd;        /* did we just push? */
413   unsigned int lineno;          /* line currently being written */
414   unsigned int written;         /* low water mark in token buffer */
415 };
416
417 #define CPP_FATAL_LIMIT 1000
418 /* True if we have seen a "fatal" error. */
419 #define CPP_FATAL_ERRORS(READER) ((READER)->errors >= CPP_FATAL_LIMIT)
420
421 /* Macros for manipulating the token_buffer. */
422
423 /* Number of characters currently in PFILE's output buffer. */
424 #define CPP_WRITTEN(PFILE) ((size_t)((PFILE)->limit - (PFILE)->token_buffer))
425 #define CPP_PWRITTEN(PFILE) ((PFILE)->limit)
426 #define CPP_ADJUST_WRITTEN(PFILE,DELTA) ((PFILE)->limit += (DELTA))
427 #define CPP_SET_WRITTEN(PFILE,N) ((PFILE)->limit = (PFILE)->token_buffer + (N))
428
429 #define CPP_OPTION(PFILE, OPTION) ((PFILE)->opts.OPTION)
430 #define CPP_BUFFER(PFILE) ((PFILE)->buffer)
431 #define CPP_BUF_LINE(BUF) ((BUF)->lineno)
432 #define CPP_BUF_COL(BUF) ((BUF)->cur - (BUF)->line_base)
433
434 /* Name under which this program was invoked.  */
435 extern const char *progname;
436
437 extern int cpp_handle_options PARAMS ((cpp_reader *, int, char **));
438 extern enum cpp_ttype cpp_get_token PARAMS ((cpp_reader *));
439 extern enum cpp_ttype cpp_get_non_space_token PARAMS ((cpp_reader *));
440
441 extern void cpp_reader_init PARAMS ((cpp_reader *));
442 extern cpp_printer *cpp_printer_init PARAMS ((cpp_reader *, cpp_printer *));
443 extern int cpp_start_read PARAMS ((cpp_reader *, cpp_printer *, const char *));
444 extern void cpp_output_tokens PARAMS ((cpp_reader *, cpp_printer *));
445 extern void cpp_finish PARAMS ((cpp_reader *, cpp_printer *));
446 extern void cpp_cleanup PARAMS ((cpp_reader *));
447
448 extern cpp_buffer *cpp_file_buffer PARAMS((cpp_reader *));
449 extern void cpp_define PARAMS ((cpp_reader *, const char *));
450 extern void cpp_assert PARAMS ((cpp_reader *, const char *));
451 extern void cpp_undef  PARAMS ((cpp_reader *, const char *));
452 extern void cpp_unassert PARAMS ((cpp_reader *, const char *));
453
454 /* N.B. The error-message-printer prototypes have not been nicely
455    formatted because exgettext needs to see 'msgid' on the same line
456    as the name of the function in order to work properly.  Only the
457    string argument gets a name in an effort to keep the lines from
458    getting ridiculously oversized.  */
459
460 extern void cpp_ice PARAMS ((cpp_reader *, const char *msgid, ...))
461   ATTRIBUTE_PRINTF_2;
462 extern void cpp_fatal PARAMS ((cpp_reader *, const char *msgid, ...))
463   ATTRIBUTE_PRINTF_2;
464 extern void cpp_error PARAMS ((cpp_reader *, const char *msgid, ...))
465   ATTRIBUTE_PRINTF_2;
466 extern void cpp_warning PARAMS ((cpp_reader *, const char *msgid, ...))
467   ATTRIBUTE_PRINTF_2;
468 extern void cpp_pedwarn PARAMS ((cpp_reader *, const char *msgid, ...))
469   ATTRIBUTE_PRINTF_2;
470 extern void cpp_notice PARAMS ((cpp_reader *, const char *msgid, ...))
471   ATTRIBUTE_PRINTF_2;
472 extern void cpp_error_with_line PARAMS ((cpp_reader *, int, int, const char *msgid, ...))
473   ATTRIBUTE_PRINTF_4;
474 extern void cpp_warning_with_line PARAMS ((cpp_reader *, int, int, const char *msgid, ...))
475   ATTRIBUTE_PRINTF_4;
476 extern void cpp_pedwarn_with_line PARAMS ((cpp_reader *, int, int, const char *msgid, ...))
477   ATTRIBUTE_PRINTF_4;
478 extern void cpp_pedwarn_with_file_and_line PARAMS ((cpp_reader *, const char *, int, int, const char *msgid, ...))
479   ATTRIBUTE_PRINTF_5;
480 extern void cpp_error_from_errno        PARAMS ((cpp_reader *, const char *));
481 extern void cpp_notice_from_errno       PARAMS ((cpp_reader *, const char *));
482
483 /* In cpplex.c */
484 extern cpp_buffer *cpp_push_buffer      PARAMS ((cpp_reader *,
485                                                  const unsigned char *, long));
486 extern cpp_buffer *cpp_pop_buffer       PARAMS ((cpp_reader *));
487 extern void cpp_scan_buffer             PARAMS ((cpp_reader *, cpp_printer *));
488 extern void cpp_scan_buffer_nooutput    PARAMS ((cpp_reader *));
489
490 /* In cpphash.c */
491 extern int cpp_defined                  PARAMS ((cpp_reader *,
492                                                  const unsigned char *, int));
493
494 /* In cppfiles.c */
495 extern int cpp_included                 PARAMS ((cpp_reader *, const char *));
496 extern int cpp_read_file                PARAMS ((cpp_reader *, const char *));
497
498 #ifdef __cplusplus
499 }
500 #endif
501 #endif /* __GCC_CPPLIB__ */