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

source code of libcpp/internal.h