1 | /* Part of CPP library. |
2 | Copyright (C) 1997-2023 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 3, 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; see the file COPYING3. If not see |
16 | <http://www.gnu.org/licenses/>. */ |
17 | |
18 | /* This header defines all the internal data structures and functions |
19 | that need to be visible across files. It should not be used outside |
20 | cpplib. */ |
21 | |
22 | #ifndef LIBCPP_INTERNAL_H |
23 | #define LIBCPP_INTERNAL_H |
24 | |
25 | #include "symtab.h" |
26 | #include "cpplib.h" |
27 | |
28 | #if HAVE_ICONV |
29 | #include <iconv.h> |
30 | #else |
31 | #define HAVE_ICONV 0 |
32 | typedef int iconv_t; /* dummy */ |
33 | #endif |
34 | |
35 | #ifdef __cplusplus |
36 | extern "C" { |
37 | #endif |
38 | |
39 | struct directive; /* Deliberately incomplete. */ |
40 | struct pending_option; |
41 | struct op; |
42 | struct _cpp_strbuf; |
43 | |
44 | typedef bool (*convert_f) (iconv_t, const unsigned char *, size_t, |
45 | struct _cpp_strbuf *); |
46 | struct cset_converter |
47 | { |
48 | convert_f func; |
49 | iconv_t cd; |
50 | int width; |
51 | const char* from; |
52 | const char* to; |
53 | }; |
54 | |
55 | #define BITS_PER_CPPCHAR_T (CHAR_BIT * sizeof (cppchar_t)) |
56 | |
57 | /* Test if a sign is valid within a preprocessing number. */ |
58 | #define VALID_SIGN(c, prevc) \ |
59 | (((c) == '+' || (c) == '-') && \ |
60 | ((prevc) == 'e' || (prevc) == 'E' \ |
61 | || (((prevc) == 'p' || (prevc) == 'P') \ |
62 | && CPP_OPTION (pfile, extended_numbers)))) |
63 | |
64 | #define DIGIT_SEP(c) ((c) == '\'' && CPP_OPTION (pfile, digit_separators)) |
65 | |
66 | #define CPP_OPTION(PFILE, OPTION) ((PFILE)->opts.OPTION) |
67 | #define CPP_BUFFER(PFILE) ((PFILE)->buffer) |
68 | #define CPP_BUF_COLUMN(BUF, CUR) ((CUR) - (BUF)->line_base) |
69 | #define CPP_BUF_COL(BUF) CPP_BUF_COLUMN(BUF, (BUF)->cur) |
70 | |
71 | #define CPP_INCREMENT_LINE(PFILE, COLS_HINT) do { \ |
72 | const class line_maps *line_table = PFILE->line_table; \ |
73 | const struct line_map_ordinary *map = \ |
74 | LINEMAPS_LAST_ORDINARY_MAP (line_table); \ |
75 | linenum_type line = SOURCE_LINE (map, line_table->highest_line); \ |
76 | linemap_line_start (PFILE->line_table, line + 1, COLS_HINT); \ |
77 | } while (0) |
78 | |
79 | /* Host alignment handling. */ |
80 | struct dummy |
81 | { |
82 | char c; |
83 | union |
84 | { |
85 | double d; |
86 | int *p; |
87 | } u; |
88 | }; |
89 | |
90 | #define DEFAULT_ALIGNMENT offsetof (struct dummy, u) |
91 | #define CPP_ALIGN2(size, align) (((size) + ((align) - 1)) & ~((align) - 1)) |
92 | #define CPP_ALIGN(size) CPP_ALIGN2 (size, DEFAULT_ALIGNMENT) |
93 | |
94 | #define _cpp_mark_macro_used(NODE) \ |
95 | (cpp_user_macro_p (NODE) ? (NODE)->value.macro->used = 1 : 0) |
96 | |
97 | /* A generic memory buffer, and operations on it. */ |
98 | typedef struct _cpp_buff _cpp_buff; |
99 | struct _cpp_buff |
100 | { |
101 | struct _cpp_buff *next; |
102 | unsigned char *base, *cur, *limit; |
103 | }; |
104 | |
105 | extern _cpp_buff *_cpp_get_buff (cpp_reader *, size_t); |
106 | extern void _cpp_release_buff (cpp_reader *, _cpp_buff *); |
107 | extern void _cpp_extend_buff (cpp_reader *, _cpp_buff **, size_t); |
108 | extern _cpp_buff *_cpp_append_extend_buff (cpp_reader *, _cpp_buff *, size_t); |
109 | extern void _cpp_free_buff (_cpp_buff *); |
110 | extern unsigned char *_cpp_aligned_alloc (cpp_reader *, size_t); |
111 | extern unsigned char *_cpp_unaligned_alloc (cpp_reader *, size_t); |
112 | |
113 | #define BUFF_ROOM(BUFF) (size_t) ((BUFF)->limit - (BUFF)->cur) |
114 | #define BUFF_FRONT(BUFF) ((BUFF)->cur) |
115 | #define BUFF_LIMIT(BUFF) ((BUFF)->limit) |
116 | |
117 | /* #include types. */ |
118 | enum include_type |
119 | { |
120 | /* Directive-based including mechanisms. */ |
121 | IT_INCLUDE, /* #include */ |
122 | IT_INCLUDE_NEXT, /* #include_next */ |
123 | IT_IMPORT, /* #import */ |
124 | |
125 | /* Non-directive including mechanisms. */ |
126 | IT_CMDLINE, /* -include */ |
127 | IT_DEFAULT, /* forced header */ |
128 | IT_MAIN, /* main, start on line 1 */ |
129 | IT_PRE_MAIN, /* main, but there will be a preamble before line |
130 | 1 */ |
131 | |
132 | IT_DIRECTIVE_HWM = IT_IMPORT + 1, /* Directives below this. */ |
133 | = IT_DEFAULT + 1 /* Header files below this. */ |
134 | }; |
135 | |
136 | union utoken |
137 | { |
138 | const cpp_token *token; |
139 | const cpp_token **ptoken; |
140 | }; |
141 | |
142 | /* A "run" of tokens; part of a chain of runs. */ |
143 | typedef struct tokenrun tokenrun; |
144 | struct tokenrun |
145 | { |
146 | tokenrun *next, *prev; |
147 | cpp_token *base, *limit; |
148 | }; |
149 | |
150 | /* Accessor macros for struct cpp_context. */ |
151 | #define FIRST(c) ((c)->u.iso.first) |
152 | #define LAST(c) ((c)->u.iso.last) |
153 | #define CUR(c) ((c)->u.trad.cur) |
154 | #define RLIMIT(c) ((c)->u.trad.rlimit) |
155 | |
156 | /* This describes some additional data that is added to the macro |
157 | token context of type cpp_context, when -ftrack-macro-expansion is |
158 | on. */ |
159 | typedef struct |
160 | { |
161 | /* The node of the macro we are referring to. */ |
162 | cpp_hashnode *macro_node; |
163 | /* This buffer contains an array of virtual locations. The virtual |
164 | location at index 0 is the virtual location of the token at index |
165 | 0 in the current instance of cpp_context; similarly for all the |
166 | other virtual locations. */ |
167 | location_t *virt_locs; |
168 | /* This is a pointer to the current virtual location. This is used |
169 | to iterate over the virtual locations while we iterate over the |
170 | tokens they belong to. */ |
171 | location_t *cur_virt_loc; |
172 | } macro_context; |
173 | |
174 | /* The kind of tokens carried by a cpp_context. */ |
175 | enum context_tokens_kind { |
176 | /* This is the value of cpp_context::tokens_kind if u.iso.first |
177 | contains an instance of cpp_token **. */ |
178 | TOKENS_KIND_INDIRECT, |
179 | /* This is the value of cpp_context::tokens_kind if u.iso.first |
180 | contains an instance of cpp_token *. */ |
181 | TOKENS_KIND_DIRECT, |
182 | /* This is the value of cpp_context::tokens_kind when the token |
183 | context contains tokens resulting from macro expansion. In that |
184 | case struct cpp_context::macro points to an instance of struct |
185 | macro_context. This is used only when the |
186 | -ftrack-macro-expansion flag is on. */ |
187 | TOKENS_KIND_EXTENDED |
188 | }; |
189 | |
190 | typedef struct cpp_context cpp_context; |
191 | struct cpp_context |
192 | { |
193 | /* Doubly-linked list. */ |
194 | cpp_context *next, *prev; |
195 | |
196 | union |
197 | { |
198 | /* For ISO macro expansion. Contexts other than the base context |
199 | are contiguous tokens. e.g. macro expansions, expanded |
200 | argument tokens. */ |
201 | struct |
202 | { |
203 | union utoken first; |
204 | union utoken last; |
205 | } iso; |
206 | |
207 | /* For traditional macro expansion. */ |
208 | struct |
209 | { |
210 | const unsigned char *cur; |
211 | const unsigned char *rlimit; |
212 | } trad; |
213 | } u; |
214 | |
215 | /* If non-NULL, a buffer used for storage related to this context. |
216 | When the context is popped, the buffer is released. */ |
217 | _cpp_buff *buff; |
218 | |
219 | /* If tokens_kind is TOKEN_KIND_EXTENDED, then (as we thus are in a |
220 | macro context) this is a pointer to an instance of macro_context. |
221 | Otherwise if tokens_kind is *not* TOKEN_KIND_EXTENDED, then, if |
222 | we are in a macro context, this is a pointer to an instance of |
223 | cpp_hashnode, representing the name of the macro this context is |
224 | for. If we are not in a macro context, then this is just NULL. |
225 | Note that when tokens_kind is TOKEN_KIND_EXTENDED, the memory |
226 | used by the instance of macro_context pointed to by this member |
227 | is de-allocated upon de-allocation of the instance of struct |
228 | cpp_context. */ |
229 | union |
230 | { |
231 | macro_context *mc; |
232 | cpp_hashnode *macro; |
233 | } c; |
234 | |
235 | /* This determines the type of tokens held by this context. */ |
236 | enum context_tokens_kind tokens_kind; |
237 | }; |
238 | |
239 | struct lexer_state |
240 | { |
241 | /* 1 if we're handling a directive. 2 if it's an include-like |
242 | directive. */ |
243 | unsigned char in_directive; |
244 | |
245 | /* Nonzero if in a directive that will handle padding tokens itself. |
246 | #include needs this to avoid problems with computed include and |
247 | spacing between tokens. */ |
248 | unsigned char directive_wants_padding; |
249 | |
250 | /* True if we are skipping a failed conditional group. */ |
251 | unsigned char skipping; |
252 | |
253 | /* Nonzero if in a directive that takes angle-bracketed headers. */ |
254 | unsigned char ; |
255 | |
256 | /* Nonzero if in a #if or #elif directive. */ |
257 | unsigned char in_expression; |
258 | |
259 | /* Nonzero to save comments. Turned off if discard_comments, and in |
260 | all directives apart from #define. */ |
261 | unsigned char ; |
262 | |
263 | /* Nonzero if lexing __VA_ARGS__ and __VA_OPT__ are valid. */ |
264 | unsigned char va_args_ok; |
265 | |
266 | /* Nonzero if lexing poisoned identifiers is valid. */ |
267 | unsigned char poisoned_ok; |
268 | |
269 | /* Nonzero to prevent macro expansion. */ |
270 | unsigned char prevent_expansion; |
271 | |
272 | /* Nonzero when parsing arguments to a function-like macro. */ |
273 | unsigned char parsing_args; |
274 | |
275 | /* Nonzero if prevent_expansion is true only because output is |
276 | being discarded. */ |
277 | unsigned char discarding_output; |
278 | |
279 | /* Nonzero to skip evaluating part of an expression. */ |
280 | unsigned int skip_eval; |
281 | |
282 | /* Nonzero when tokenizing a deferred pragma. */ |
283 | unsigned char in_deferred_pragma; |
284 | |
285 | /* Count to token that is a header-name. */ |
286 | unsigned char directive_file_token; |
287 | |
288 | /* Nonzero if the deferred pragma being handled allows macro expansion. */ |
289 | unsigned char pragma_allow_expansion; |
290 | |
291 | /* Nonzero if _Pragma should not be interpreted. */ |
292 | unsigned char ignore__Pragma; |
293 | }; |
294 | |
295 | /* Special nodes - identifiers with predefined significance. */ |
296 | struct spec_nodes |
297 | { |
298 | cpp_hashnode *n_defined; /* defined operator */ |
299 | cpp_hashnode *n_true; /* C++ keyword true */ |
300 | cpp_hashnode *n_false; /* C++ keyword false */ |
301 | cpp_hashnode *n__VA_ARGS__; /* C99 vararg macros */ |
302 | cpp_hashnode *n__VA_OPT__; /* C++ vararg macros */ |
303 | |
304 | enum {M_EXPORT, M_MODULE, M_IMPORT, M__IMPORT, M_HWM}; |
305 | |
306 | /* C++20 modules, only set when module_directives is in effect. |
307 | incoming variants [0], outgoing ones [1] */ |
308 | cpp_hashnode *n_modules[M_HWM][2]; |
309 | }; |
310 | |
311 | typedef struct _cpp_line_note _cpp_line_note; |
312 | struct _cpp_line_note |
313 | { |
314 | /* Location in the clean line the note refers to. */ |
315 | const unsigned char *pos; |
316 | |
317 | /* Type of note. The 9 'from' trigraph characters represent those |
318 | trigraphs, '\\' an escaped newline, ' ' an escaped newline with |
319 | intervening space, 0 represents a note that has already been handled, |
320 | and anything else is invalid. */ |
321 | unsigned int type; |
322 | }; |
323 | |
324 | /* Represents the contents of a file cpplib has read in. */ |
325 | struct cpp_buffer |
326 | { |
327 | const unsigned char *cur; /* Current location. */ |
328 | const unsigned char *line_base; /* Start of current physical line. */ |
329 | const unsigned char *next_line; /* Start of to-be-cleaned logical line. */ |
330 | |
331 | const unsigned char *buf; /* Entire character buffer. */ |
332 | const unsigned char *rlimit; /* Writable byte at end of file. */ |
333 | const unsigned char *to_free; /* Pointer that should be freed when |
334 | popping the buffer. */ |
335 | |
336 | _cpp_line_note *notes; /* Array of notes. */ |
337 | unsigned int cur_note; /* Next note to process. */ |
338 | unsigned int notes_used; /* Number of notes. */ |
339 | unsigned int notes_cap; /* Size of allocated array. */ |
340 | |
341 | struct cpp_buffer *prev; |
342 | |
343 | /* Pointer into the file table; non-NULL if this is a file buffer. |
344 | Used for include_next and to record control macros. */ |
345 | struct _cpp_file *file; |
346 | |
347 | /* Saved value of __TIMESTAMP__ macro - date and time of last modification |
348 | of the assotiated file. */ |
349 | const unsigned char *timestamp; |
350 | |
351 | /* Value of if_stack at start of this file. |
352 | Used to prohibit unmatched #endif (etc) in an include file. */ |
353 | struct if_stack *if_stack; |
354 | |
355 | /* True if we need to get the next clean line. */ |
356 | bool need_line : 1; |
357 | |
358 | /* True if we have already warned about C++ comments in this file. |
359 | The warning happens only for C89 extended mode with -pedantic on, |
360 | or for -Wtraditional, and only once per file (otherwise it would |
361 | be far too noisy). */ |
362 | bool : 1; |
363 | |
364 | /* True if we don't process trigraphs and escaped newlines. True |
365 | for preprocessed input, command line directives, and _Pragma |
366 | buffers. */ |
367 | bool from_stage3 : 1; |
368 | |
369 | /* At EOF, a buffer is automatically popped. If RETURN_AT_EOF is |
370 | true, a CPP_EOF token is then returned. Otherwise, the next |
371 | token from the enclosing buffer is returned. */ |
372 | bool return_at_eof : 1; |
373 | |
374 | /* One for a system header, two for a C system header file that therefore |
375 | needs to be extern "C" protected in C++, and zero otherwise. */ |
376 | unsigned char sysp; |
377 | |
378 | /* The directory of the this buffer's file. Its NAME member is not |
379 | allocated, so we don't need to worry about freeing it. */ |
380 | struct cpp_dir dir; |
381 | |
382 | /* Descriptor for converting from the input character set to the |
383 | source character set. */ |
384 | struct cset_converter input_cset_desc; |
385 | }; |
386 | |
387 | /* The list of saved macros by push_macro pragma. */ |
388 | struct def_pragma_macro { |
389 | /* Chain element to previous saved macro. */ |
390 | struct def_pragma_macro *next; |
391 | /* Name of the macro. */ |
392 | char *name; |
393 | /* The stored macro content. */ |
394 | unsigned char *definition; |
395 | |
396 | /* Definition line number. */ |
397 | location_t line; |
398 | /* If macro defined in system header. */ |
399 | unsigned int syshdr : 1; |
400 | /* Nonzero if it has been expanded or had its existence tested. */ |
401 | unsigned int used : 1; |
402 | |
403 | /* Mark if we save an undefined macro. */ |
404 | unsigned int is_undef : 1; |
405 | /* Nonzero if it was a builtin macro. */ |
406 | unsigned int is_builtin : 1; |
407 | }; |
408 | |
409 | /* A cpp_reader encapsulates the "state" of a pre-processor run. |
410 | Applying cpp_get_token repeatedly yields a stream of pre-processor |
411 | tokens. Usually, there is only one cpp_reader object active. */ |
412 | struct cpp_reader |
413 | { |
414 | /* Top of buffer stack. */ |
415 | cpp_buffer *buffer; |
416 | |
417 | /* Overlaid buffer (can be different after processing #include). */ |
418 | cpp_buffer *overlaid_buffer; |
419 | |
420 | /* Lexer state. */ |
421 | struct lexer_state state; |
422 | |
423 | /* Source line tracking. */ |
424 | class line_maps *line_table; |
425 | |
426 | /* The line of the '#' of the current directive. */ |
427 | location_t directive_line; |
428 | |
429 | /* Memory buffers. */ |
430 | _cpp_buff *a_buff; /* Aligned permanent storage. */ |
431 | _cpp_buff *u_buff; /* Unaligned permanent storage. */ |
432 | _cpp_buff *free_buffs; /* Free buffer chain. */ |
433 | |
434 | /* Context stack. */ |
435 | struct cpp_context base_context; |
436 | struct cpp_context *context; |
437 | |
438 | /* If in_directive, the directive if known. */ |
439 | const struct directive *directive; |
440 | |
441 | /* Token generated while handling a directive, if any. */ |
442 | cpp_token directive_result; |
443 | |
444 | /* When expanding a macro at top-level, this is the location of the |
445 | macro invocation. */ |
446 | location_t invocation_location; |
447 | |
448 | /* This is the node representing the macro being expanded at |
449 | top-level. The value of this data member is valid iff |
450 | cpp_in_macro_expansion_p() returns TRUE. */ |
451 | cpp_hashnode *top_most_macro_node; |
452 | |
453 | /* Nonzero if we are about to expand a macro. Note that if we are |
454 | really expanding a macro, the function macro_of_context returns |
455 | the macro being expanded and this flag is set to false. Client |
456 | code should use the function cpp_in_macro_expansion_p to know if we |
457 | are either about to expand a macro, or are actually expanding |
458 | one. */ |
459 | bool about_to_expand_macro_p; |
460 | |
461 | /* Search paths for include files. */ |
462 | struct cpp_dir *quote_include; /* "" */ |
463 | struct cpp_dir *bracket_include; /* <> */ |
464 | struct cpp_dir no_search_path; /* No path. */ |
465 | |
466 | /* Chain of all hashed _cpp_file instances. */ |
467 | struct _cpp_file *all_files; |
468 | |
469 | struct _cpp_file *main_file; |
470 | |
471 | /* File and directory hash table. */ |
472 | struct htab *file_hash; |
473 | struct htab *dir_hash; |
474 | struct file_hash_entry_pool *file_hash_entries; |
475 | |
476 | /* Negative path lookup hash table. */ |
477 | struct htab *nonexistent_file_hash; |
478 | struct obstack nonexistent_file_ob; |
479 | |
480 | /* Nonzero means don't look for #include "foo" the source-file |
481 | directory. */ |
482 | bool quote_ignores_source_dir; |
483 | |
484 | /* Nonzero if any file has contained #pragma once or #import has |
485 | been used. */ |
486 | bool seen_once_only; |
487 | |
488 | /* Multiple include optimization. */ |
489 | const cpp_hashnode *mi_cmacro; |
490 | const cpp_hashnode *mi_ind_cmacro; |
491 | bool mi_valid; |
492 | |
493 | /* Lexing. */ |
494 | cpp_token *cur_token; |
495 | tokenrun base_run, *cur_run; |
496 | unsigned int lookaheads; |
497 | |
498 | /* Nonzero prevents the lexer from re-using the token runs. */ |
499 | unsigned int keep_tokens; |
500 | |
501 | /* Buffer to hold macro definition string. */ |
502 | unsigned char *macro_buffer; |
503 | unsigned int macro_buffer_len; |
504 | |
505 | /* Descriptor for converting from the source character set to the |
506 | execution character set. */ |
507 | struct cset_converter narrow_cset_desc; |
508 | |
509 | /* Descriptor for converting from the source character set to the |
510 | UTF-8 execution character set. */ |
511 | struct cset_converter utf8_cset_desc; |
512 | |
513 | /* Descriptor for converting from the source character set to the |
514 | UTF-16 execution character set. */ |
515 | struct cset_converter char16_cset_desc; |
516 | |
517 | /* Descriptor for converting from the source character set to the |
518 | UTF-32 execution character set. */ |
519 | struct cset_converter char32_cset_desc; |
520 | |
521 | /* Descriptor for converting from the source character set to the |
522 | wide execution character set. */ |
523 | struct cset_converter wide_cset_desc; |
524 | |
525 | /* Date and time text. Calculated together if either is requested. */ |
526 | const unsigned char *date; |
527 | const unsigned char *time; |
528 | |
529 | /* Time stamp, set idempotently lazily. */ |
530 | time_t time_stamp; |
531 | int time_stamp_kind; /* Or errno. */ |
532 | |
533 | /* A token forcing paste avoidance, and one demarking macro arguments. */ |
534 | cpp_token avoid_paste; |
535 | cpp_token endarg; |
536 | |
537 | /* Opaque handle to the dependencies of mkdeps.cc. */ |
538 | class mkdeps *deps; |
539 | |
540 | /* Obstack holding all macro hash nodes. This never shrinks. |
541 | See identifiers.cc */ |
542 | struct obstack hash_ob; |
543 | |
544 | /* Obstack holding buffer and conditional structures. This is a |
545 | real stack. See directives.cc. */ |
546 | struct obstack buffer_ob; |
547 | |
548 | /* Pragma table - dynamic, because a library user can add to the |
549 | list of recognized pragmas. */ |
550 | struct pragma_entry *pragmas; |
551 | |
552 | /* Call backs to cpplib client. */ |
553 | struct cpp_callbacks cb; |
554 | |
555 | /* Identifier hash table. */ |
556 | struct ht *hash_table; |
557 | |
558 | /* Identifier ancillary data hash table. */ |
559 | struct ht *; |
560 | |
561 | /* Expression parser stack. */ |
562 | struct op *op_stack, *op_limit; |
563 | |
564 | /* User visible options. */ |
565 | struct cpp_options opts; |
566 | |
567 | /* Special nodes - identifiers with predefined significance to the |
568 | preprocessor. */ |
569 | struct spec_nodes spec_nodes; |
570 | |
571 | /* Whether cpplib owns the hashtable. */ |
572 | bool our_hashtable, ; |
573 | |
574 | /* Traditional preprocessing output buffer (a logical line). */ |
575 | struct |
576 | { |
577 | unsigned char *base; |
578 | unsigned char *limit; |
579 | unsigned char *cur; |
580 | location_t first_line; |
581 | } out; |
582 | |
583 | /* Used for buffer overlays by traditional.cc. */ |
584 | const unsigned char *saved_cur, *saved_rlimit, *saved_line_base; |
585 | |
586 | /* A saved list of the defined macros, for dependency checking |
587 | of precompiled headers. */ |
588 | struct cpp_savedstate *savedstate; |
589 | |
590 | /* Next value of __COUNTER__ macro. */ |
591 | unsigned int counter; |
592 | |
593 | /* Table of comments, when state.save_comments is true. */ |
594 | cpp_comment_table ; |
595 | |
596 | /* List of saved macros by push_macro. */ |
597 | struct def_pragma_macro *pushed_macros; |
598 | |
599 | /* If non-zero, the lexer will use this location for the next token |
600 | instead of getting a location from the linemap. */ |
601 | location_t forced_token_location; |
602 | |
603 | /* Location identifying the main source file -- intended to be line |
604 | zero of said file. */ |
605 | location_t main_loc; |
606 | |
607 | /* Returns true iff we should warn about UTF-8 bidirectional control |
608 | characters. */ |
609 | bool warn_bidi_p () const |
610 | { |
611 | return (CPP_OPTION (this, cpp_warn_bidirectional) |
612 | & (bidirectional_unpaired|bidirectional_any)); |
613 | } |
614 | }; |
615 | |
616 | /* Character classes. Based on the more primitive macros in safe-ctype.h. |
617 | If the definition of `numchar' looks odd to you, please look up the |
618 | definition of a pp-number in the C standard [section 6.4.8 of C99]. |
619 | |
620 | In the unlikely event that characters other than \r and \n enter |
621 | the set is_vspace, the macro handle_newline() in lex.cc must be |
622 | updated. */ |
623 | #define _dollar_ok(x) ((x) == '$' && CPP_OPTION (pfile, dollars_in_ident)) |
624 | |
625 | #define is_idchar(x) (ISIDNUM(x) || _dollar_ok(x)) |
626 | #define is_numchar(x) ISIDNUM(x) |
627 | #define is_idstart(x) (ISIDST(x) || _dollar_ok(x)) |
628 | #define is_numstart(x) ISDIGIT(x) |
629 | #define is_hspace(x) ISBLANK(x) |
630 | #define is_vspace(x) IS_VSPACE(x) |
631 | #define is_nvspace(x) IS_NVSPACE(x) |
632 | #define is_space(x) IS_SPACE_OR_NUL(x) |
633 | |
634 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
635 | |
636 | /* This table is constant if it can be initialized at compile time, |
637 | which is the case if cpp was compiled with GCC >=2.7, or another |
638 | compiler that supports C99. */ |
639 | #if HAVE_DESIGNATED_INITIALIZERS |
640 | extern const unsigned char _cpp_trigraph_map[UCHAR_MAX + 1]; |
641 | #else |
642 | extern unsigned char _cpp_trigraph_map[UCHAR_MAX + 1]; |
643 | #endif |
644 | |
645 | #if !defined (HAVE_UCHAR) && !defined (IN_GCC) |
646 | typedef unsigned char uchar; |
647 | #endif |
648 | |
649 | #define UC (const uchar *) /* Intended use: UC"string" */ |
650 | |
651 | /* Accessors. */ |
652 | |
653 | inline int |
654 | (cpp_reader *pfile) |
655 | { |
656 | return pfile->buffer ? pfile->buffer->sysp : 0; |
657 | } |
658 | #define CPP_PEDANTIC(PF) CPP_OPTION (PF, cpp_pedantic) |
659 | #define CPP_WTRADITIONAL(PF) CPP_OPTION (PF, cpp_warn_traditional) |
660 | |
661 | /* Return true if we're in the main file (unless it's considered to be |
662 | an include file in its own right. */ |
663 | inline int |
664 | _cpp_in_main_source_file (cpp_reader *pfile) |
665 | { |
666 | return (!CPP_OPTION (pfile, main_search) |
667 | && pfile->buffer->file == pfile->main_file); |
668 | } |
669 | |
670 | /* True if NODE is a macro for the purposes of ifdef, defined etc. */ |
671 | inline bool _cpp_defined_macro_p (cpp_hashnode *node) |
672 | { |
673 | /* Do not treat conditional macros as being defined. This is due to |
674 | the powerpc port using conditional macros for 'vector', 'bool', |
675 | and 'pixel' to act as conditional keywords. This messes up tests |
676 | like #ifndef bool. */ |
677 | return cpp_macro_p (node) && !(node->flags & NODE_CONDITIONAL); |
678 | } |
679 | |
680 | /* In macro.cc */ |
681 | extern bool _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, |
682 | location_t); |
683 | inline bool _cpp_maybe_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, |
684 | location_t loc) |
685 | { |
686 | if (!(node->flags & NODE_USED)) |
687 | return _cpp_notify_macro_use (pfile, node, loc); |
688 | return true; |
689 | } |
690 | extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind, void *); |
691 | extern void _cpp_free_definition (cpp_hashnode *); |
692 | extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *, location_t); |
693 | extern void _cpp_pop_context (cpp_reader *); |
694 | extern void _cpp_push_text_context (cpp_reader *, cpp_hashnode *, |
695 | const unsigned char *, size_t); |
696 | extern bool _cpp_save_parameter (cpp_reader *, unsigned, cpp_hashnode *, |
697 | cpp_hashnode *); |
698 | extern void _cpp_unsave_parameters (cpp_reader *, unsigned); |
699 | extern bool _cpp_arguments_ok (cpp_reader *, cpp_macro *, const cpp_hashnode *, |
700 | unsigned int); |
701 | extern const unsigned char *_cpp_builtin_macro_text (cpp_reader *, |
702 | cpp_hashnode *, |
703 | location_t = 0); |
704 | extern int _cpp_warn_if_unused_macro (cpp_reader *, cpp_hashnode *, void *); |
705 | extern void _cpp_push_token_context (cpp_reader *, cpp_hashnode *, |
706 | const cpp_token *, unsigned int); |
707 | extern void _cpp_backup_tokens_direct (cpp_reader *, unsigned int); |
708 | |
709 | /* In identifiers.cc */ |
710 | extern void |
711 | _cpp_init_hashtable (cpp_reader *, cpp_hash_table *, cpp_hash_table *); |
712 | extern void _cpp_destroy_hashtable (cpp_reader *); |
713 | |
714 | /* In files.cc */ |
715 | enum _cpp_find_file_kind |
716 | { _cpp_FFK_NORMAL, _cpp_FFK_FAKE, _cpp_FFK_PRE_INCLUDE, _cpp_FFK_HAS_INCLUDE }; |
717 | extern _cpp_file *_cpp_find_file (cpp_reader *, const char *, cpp_dir *, |
718 | int angle, _cpp_find_file_kind, location_t); |
719 | extern bool _cpp_find_failed (_cpp_file *); |
720 | extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *); |
721 | extern const char * (cpp_reader *, const char *file, |
722 | bool angle_p, location_t); |
723 | extern void _cpp_fake_include (cpp_reader *, const char *); |
724 | extern bool _cpp_stack_file (cpp_reader *, _cpp_file*, include_type, location_t); |
725 | extern bool _cpp_stack_include (cpp_reader *, const char *, int, |
726 | enum include_type, location_t); |
727 | extern int _cpp_compare_file_date (cpp_reader *, const char *, int); |
728 | extern void _cpp_report_missing_guards (cpp_reader *); |
729 | extern void _cpp_init_files (cpp_reader *); |
730 | extern void _cpp_cleanup_files (cpp_reader *); |
731 | extern void _cpp_pop_file_buffer (cpp_reader *, struct _cpp_file *, |
732 | const unsigned char *); |
733 | extern bool _cpp_save_file_entries (cpp_reader *pfile, FILE *f); |
734 | extern bool _cpp_read_file_entries (cpp_reader *, FILE *); |
735 | extern const char *_cpp_get_file_name (_cpp_file *); |
736 | extern struct stat *_cpp_get_file_stat (_cpp_file *); |
737 | extern bool (cpp_reader *, const char *, int, |
738 | enum include_type); |
739 | |
740 | /* In expr.cc */ |
741 | extern bool _cpp_parse_expr (cpp_reader *, bool); |
742 | extern struct op *_cpp_expand_op_stack (cpp_reader *); |
743 | |
744 | /* In lex.cc */ |
745 | extern void _cpp_process_line_notes (cpp_reader *, int); |
746 | extern void _cpp_clean_line (cpp_reader *); |
747 | extern bool _cpp_get_fresh_line (cpp_reader *); |
748 | extern bool (cpp_reader *); |
749 | extern cpp_token *_cpp_temp_token (cpp_reader *); |
750 | extern const cpp_token *_cpp_lex_token (cpp_reader *); |
751 | extern cpp_token *_cpp_lex_direct (cpp_reader *); |
752 | extern unsigned char *_cpp_spell_ident_ucns (unsigned char *, cpp_hashnode *); |
753 | extern int _cpp_equiv_tokens (const cpp_token *, const cpp_token *); |
754 | extern void _cpp_init_tokenrun (tokenrun *, unsigned int); |
755 | extern cpp_hashnode *_cpp_lex_identifier (cpp_reader *, const char *); |
756 | extern int _cpp_remaining_tokens_num_in_context (cpp_context *); |
757 | extern void _cpp_init_lexer (void); |
758 | static inline void *_cpp_reserve_room (cpp_reader *pfile, size_t have, |
759 | size_t ) |
760 | { |
761 | if (BUFF_ROOM (pfile->a_buff) < (have + extra)) |
762 | _cpp_extend_buff (pfile, &pfile->a_buff, extra); |
763 | return BUFF_FRONT (pfile->a_buff); |
764 | } |
765 | extern void *_cpp_commit_buff (cpp_reader *pfile, size_t size); |
766 | |
767 | /* In init.cc. */ |
768 | extern void _cpp_maybe_push_include_file (cpp_reader *); |
769 | extern const char *cpp_named_operator2name (enum cpp_ttype type); |
770 | extern void _cpp_restore_special_builtin (cpp_reader *pfile, |
771 | struct def_pragma_macro *); |
772 | |
773 | /* In directives.cc */ |
774 | extern int _cpp_test_assertion (cpp_reader *, unsigned int *); |
775 | extern int _cpp_handle_directive (cpp_reader *, bool); |
776 | extern void _cpp_define_builtin (cpp_reader *, const char *); |
777 | extern char ** _cpp_save_pragma_names (cpp_reader *); |
778 | extern void _cpp_restore_pragma_names (cpp_reader *, char **); |
779 | extern int _cpp_do__Pragma (cpp_reader *, location_t); |
780 | extern void _cpp_init_directives (cpp_reader *); |
781 | extern void _cpp_init_internal_pragmas (cpp_reader *); |
782 | extern void _cpp_do_file_change (cpp_reader *, enum lc_reason, const char *, |
783 | linenum_type, unsigned int); |
784 | extern void _cpp_pop_buffer (cpp_reader *); |
785 | extern char *_cpp_bracket_include (cpp_reader *); |
786 | |
787 | /* In errors.cc */ |
788 | extern location_t cpp_diagnostic_get_current_location (cpp_reader *); |
789 | |
790 | /* In traditional.cc. */ |
791 | extern bool _cpp_scan_out_logical_line (cpp_reader *, cpp_macro *, bool); |
792 | extern bool _cpp_read_logical_line_trad (cpp_reader *); |
793 | extern void _cpp_overlay_buffer (cpp_reader *pfile, const unsigned char *, |
794 | size_t); |
795 | extern void _cpp_remove_overlay (cpp_reader *); |
796 | extern cpp_macro *_cpp_create_trad_definition (cpp_reader *); |
797 | extern bool _cpp_expansions_different_trad (const cpp_macro *, |
798 | const cpp_macro *); |
799 | extern unsigned char *_cpp_copy_replacement_text (const cpp_macro *, |
800 | unsigned char *); |
801 | extern size_t _cpp_replacement_text_len (const cpp_macro *); |
802 | |
803 | /* In charset.cc. */ |
804 | |
805 | /* The normalization state at this point in the sequence. |
806 | It starts initialized to all zeros, and at the end |
807 | 'level' is the normalization level of the sequence. */ |
808 | |
809 | struct normalize_state |
810 | { |
811 | /* The previous starter character. */ |
812 | cppchar_t previous; |
813 | /* The combining class of the previous character (whether or not a |
814 | starter). */ |
815 | unsigned char prev_class; |
816 | /* The lowest normalization level so far. */ |
817 | enum cpp_normalize_level level; |
818 | }; |
819 | #define INITIAL_NORMALIZE_STATE { 0, 0, normalized_KC } |
820 | #define NORMALIZE_STATE_RESULT(st) ((st)->level) |
821 | |
822 | /* We saw a character C that matches ISIDNUM(), update a |
823 | normalize_state appropriately. */ |
824 | #define NORMALIZE_STATE_UPDATE_IDNUM(st, c) \ |
825 | ((st)->previous = (c), (st)->prev_class = 0) |
826 | |
827 | extern bool _cpp_valid_ucn (cpp_reader *, const unsigned char **, |
828 | const unsigned char *, int, |
829 | struct normalize_state *state, |
830 | cppchar_t *, |
831 | source_range *char_range, |
832 | cpp_string_location_reader *loc_reader); |
833 | |
834 | extern bool _cpp_valid_utf8 (cpp_reader *pfile, |
835 | const uchar **pstr, |
836 | const uchar *limit, |
837 | int identifier_pos, |
838 | struct normalize_state *nst, |
839 | cppchar_t *cp); |
840 | |
841 | extern void _cpp_destroy_iconv (cpp_reader *); |
842 | extern unsigned char *_cpp_convert_input (cpp_reader *, const char *, |
843 | unsigned char *, size_t, size_t, |
844 | const unsigned char **, off_t *); |
845 | extern const char *_cpp_default_encoding (void); |
846 | extern cpp_hashnode * _cpp_interpret_identifier (cpp_reader *pfile, |
847 | const unsigned char *id, |
848 | size_t len); |
849 | |
850 | /* Utility routines and macros. */ |
851 | #define DSC(str) (const unsigned char *)str, sizeof str - 1 |
852 | |
853 | /* These are inline functions instead of macros so we can get type |
854 | checking. */ |
855 | static inline int ustrcmp (const unsigned char *, const unsigned char *); |
856 | static inline int ustrncmp (const unsigned char *, const unsigned char *, |
857 | size_t); |
858 | static inline size_t ustrlen (const unsigned char *); |
859 | static inline const unsigned char *uxstrdup (const unsigned char *); |
860 | static inline const unsigned char *ustrchr (const unsigned char *, int); |
861 | static inline int ufputs (const unsigned char *, FILE *); |
862 | |
863 | /* Use a const char for the second parameter since it is usually a literal. */ |
864 | static inline int ustrcspn (const unsigned char *, const char *); |
865 | |
866 | static inline int |
867 | ustrcmp (const unsigned char *s1, const unsigned char *s2) |
868 | { |
869 | return strcmp (s1: (const char *)s1, s2: (const char *)s2); |
870 | } |
871 | |
872 | static inline int |
873 | ustrncmp (const unsigned char *s1, const unsigned char *s2, size_t n) |
874 | { |
875 | return strncmp (s1: (const char *)s1, s2: (const char *)s2, n: n); |
876 | } |
877 | |
878 | static inline int |
879 | ustrcspn (const unsigned char *s1, const char *s2) |
880 | { |
881 | return strcspn (s: (const char *)s1, reject: s2); |
882 | } |
883 | |
884 | static inline size_t |
885 | ustrlen (const unsigned char *s1) |
886 | { |
887 | return strlen (s: (const char *)s1); |
888 | } |
889 | |
890 | static inline const unsigned char * |
891 | uxstrdup (const unsigned char *s1) |
892 | { |
893 | return (const unsigned char *) xstrdup ((const char *)s1); |
894 | } |
895 | |
896 | static inline const unsigned char * |
897 | ustrchr (const unsigned char *s1, int c) |
898 | { |
899 | return (const unsigned char *) strchr (s: (const char *)s1, c: c); |
900 | } |
901 | |
902 | static inline int |
903 | ufputs (const unsigned char *s, FILE *f) |
904 | { |
905 | return fputs ((const char *)s, f); |
906 | } |
907 | |
908 | /* In line-map.cc. */ |
909 | |
910 | /* Create and return a virtual location for a token that is part of a |
911 | macro expansion-list at a macro expansion point. See the comment |
912 | inside struct line_map_macro to see what an expansion-list exactly |
913 | is. |
914 | |
915 | A call to this function must come after a call to |
916 | linemap_enter_macro. |
917 | |
918 | MAP is the map into which the source location is created. TOKEN_NO |
919 | is the index of the token in the macro replacement-list, starting |
920 | at number 0. |
921 | |
922 | ORIG_LOC is the location of the token outside of this macro |
923 | expansion. If the token comes originally from the macro |
924 | definition, it is the locus in the macro definition; otherwise it |
925 | is a location in the context of the caller of this macro expansion |
926 | (which is a virtual location or a source location if the caller is |
927 | itself a macro expansion or not). |
928 | |
929 | MACRO_DEFINITION_LOC is the location in the macro definition, |
930 | either of the token itself or of a macro parameter that it |
931 | replaces. */ |
932 | location_t linemap_add_macro_token (const line_map_macro *, |
933 | unsigned int, |
934 | location_t, |
935 | location_t); |
936 | |
937 | /* Return the source line number corresponding to source location |
938 | LOCATION. SET is the line map set LOCATION comes from. If |
939 | LOCATION is the location of token that is part of the |
940 | expansion-list of a macro expansion return the line number of the |
941 | macro expansion point. */ |
942 | int linemap_get_expansion_line (const line_maps *, |
943 | location_t); |
944 | |
945 | /* Return the path of the file corresponding to source code location |
946 | LOCATION. |
947 | |
948 | If LOCATION is the location of a token that is part of the |
949 | replacement-list of a macro expansion return the file path of the |
950 | macro expansion point. |
951 | |
952 | SET is the line map set LOCATION comes from. */ |
953 | const char* linemap_get_expansion_filename (const line_maps *, |
954 | location_t); |
955 | |
956 | /* A subclass of rich_location for emitting a diagnostic |
957 | at the current location of the reader, but flagging |
958 | it with set_escape_on_output (true). */ |
959 | class encoding_rich_location : public rich_location |
960 | { |
961 | public: |
962 | encoding_rich_location (cpp_reader *pfile) |
963 | : rich_location (pfile->line_table, |
964 | cpp_diagnostic_get_current_location (pfile)) |
965 | { |
966 | set_escape_on_output (true); |
967 | } |
968 | |
969 | encoding_rich_location (cpp_reader *pfile, location_t loc) |
970 | : rich_location (pfile->line_table, loc) |
971 | { |
972 | set_escape_on_output (true); |
973 | } |
974 | }; |
975 | |
976 | #ifdef __cplusplus |
977 | } |
978 | #endif |
979 | |
980 | #endif /* ! LIBCPP_INTERNAL_H */ |
981 | |