1/* Data structures and function exported by the C++ Parser.
2 Copyright (C) 2010-2022 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef GCC_CP_PARSER_H
21#define GCC_CP_PARSER_H
22
23#include "tree.h"
24#include "cp/cp-tree.h"
25#include "c-family/c-pragma.h"
26
27/* A token's value and its associated deferred access checks and
28 qualifying scope. */
29
30struct GTY(()) tree_check {
31 /* The value associated with the token. */
32 tree value;
33 /* The checks that have been associated with value. */
34 vec<deferred_access_check, va_gc> *checks;
35 /* The token's qualifying scope (used when it is a
36 CPP_NESTED_NAME_SPECIFIER). */
37 tree qualifying_scope;
38};
39
40/* A C++ token. */
41
42struct GTY (()) cp_token {
43 /* The kind of token. */
44 enum cpp_ttype type : 8;
45 /* If this token is a keyword, this value indicates which keyword.
46 Otherwise, this value is RID_MAX. */
47 enum rid keyword : 8;
48 /* Token flags. */
49 unsigned char flags;
50 /* True if this token is from a context where it is implicitly extern "C" */
51 bool implicit_extern_c : 1;
52 /* True if an error has already been reported for this token, such as a
53 CPP_NAME token that is not a keyword (i.e., for which KEYWORD is
54 RID_MAX) iff this name was looked up and found to be ambiguous. */
55 bool error_reported : 1;
56 /* True for a token that has been purged. If a token is purged,
57 it is no longer a valid token and it should be considered
58 deleted. */
59 bool purged_p : 1;
60 bool tree_check_p : 1;
61 bool main_source_p : 1;
62 /* 3 unused bits. */
63
64 /* The location at which this token was found. */
65 location_t location;
66 /* The value associated with this token, if any. */
67 union cp_token_value {
68 /* Used for compound tokens such as CPP_NESTED_NAME_SPECIFIER. */
69 struct tree_check* GTY((tag ("true"))) tree_check_value;
70 /* Use for all other tokens. */
71 tree GTY((tag ("false"))) value;
72 } GTY((desc ("%1.tree_check_p"))) u;
73};
74
75
76/* We use a stack of token pointer for saving token sets. */
77typedef struct cp_token *cp_token_position;
78
79/* The cp_lexer structure represents the C++ lexer. It is responsible
80 for managing the token stream from the preprocessor and supplying
81 it to the parser. Tokens are never added to the cp_lexer after
82 it is created. */
83
84struct GTY (()) cp_lexer {
85 /* The memory allocated for the buffer. NULL if this lexer does not
86 own the token buffer. */
87 vec<cp_token, va_gc> *buffer;
88
89 /* A pointer just past the last available token. The tokens
90 in this lexer are [buffer, last_token). */
91 cp_token_position GTY ((skip)) last_token;
92
93 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
94 no more available tokens. */
95 cp_token_position GTY ((skip)) next_token;
96
97 /* A stack indicating positions at which cp_lexer_save_tokens was
98 called. The top entry is the most recent position at which we
99 began saving tokens. If the stack is non-empty, we are saving
100 tokens. */
101 vec<cp_token_position> GTY ((skip)) saved_tokens;
102
103 /* Saved pieces of end token we replaced with the eof token. */
104 enum cpp_ttype saved_type : 8;
105 enum rid saved_keyword : 8;
106
107 /* The next lexer in a linked list of lexers. */
108 struct cp_lexer *next;
109
110 /* True if we should output debugging information. */
111 bool debugging_p;
112
113 /* True if we're in the context of parsing a pragma, and should not
114 increment past the end-of-line marker. */
115 bool in_pragma;
116
117 /* True if we're in the context of OpenMP directives written as C++11
118 attributes turned into pragma. */
119 bool in_omp_attribute_pragma;
120
121 /* True for in_omp_attribute_pragma lexer that should be destroyed
122 when it is no longer in use. */
123 bool orphan_p;
124};
125
126
127/* cp_token_cache is a range of tokens. There is no need to represent
128 allocate heap memory for it, since tokens are never removed from the
129 lexer's array. There is also no need for the GC to walk through
130 a cp_token_cache, since everything in here is referenced through
131 a lexer. */
132
133struct GTY(()) cp_token_cache {
134 /* The beginning of the token range. */
135 cp_token * GTY((skip)) first;
136
137 /* Points immediately after the last token in the range. */
138 cp_token * GTY ((skip)) last;
139};
140
141typedef cp_token_cache *cp_token_cache_ptr;
142
143struct cp_token_ident
144{
145 unsigned int ident_len;
146 const char *ident_str;
147 unsigned int before_len;
148 const char *before_str;
149 unsigned int after_len;
150 const char *after_str;
151};
152
153/* An entry in a queue of function arguments that require post-processing. */
154
155struct GTY(()) cp_default_arg_entry {
156 /* The current_class_type when we parsed this arg. */
157 tree class_type;
158
159 /* The function decl itself. */
160 tree decl;
161};
162
163
164/* An entry in a stack for member functions defined within their classes. */
165
166struct GTY(()) cp_unparsed_functions_entry {
167 /* Functions with default arguments that require post-processing.
168 Functions appear in this list in declaration order. */
169 vec<cp_default_arg_entry, va_gc> *funs_with_default_args;
170
171 /* Functions with defintions that require post-processing. Functions
172 appear in this list in declaration order. */
173 vec<tree, va_gc> *funs_with_definitions;
174
175 /* Non-static data members with initializers that require post-processing.
176 FIELD_DECLs appear in this list in declaration order. */
177 vec<tree, va_gc> *nsdmis;
178
179 /* Functions with noexcept-specifiers that require post-processing. */
180 vec<tree, va_gc> *noexcepts;
181};
182
183
184/* The status of a tentative parse. */
185
186enum cp_parser_status_kind
187{
188 /* No errors have occurred. */
189 CP_PARSER_STATUS_KIND_NO_ERROR,
190 /* An error has occurred. */
191 CP_PARSER_STATUS_KIND_ERROR,
192 /* We are committed to this tentative parse, whether or not an error
193 has occurred. */
194 CP_PARSER_STATUS_KIND_COMMITTED
195};
196
197
198/* Context that is saved and restored when parsing tentatively. */
199struct GTY (()) cp_parser_context {
200 /* If this is a tentative parsing context, the status of the
201 tentative parse. */
202 enum cp_parser_status_kind status;
203 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
204 that are looked up in this context must be looked up both in the
205 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
206 the context of the containing expression. */
207 tree object_type;
208
209 /* The next parsing context in the stack. */
210 struct cp_parser_context *next;
211};
212
213
214/* Helper data structure for parsing #pragma omp declare {simd,variant}. */
215struct cp_omp_declare_simd_data {
216 bool error_seen; /* Set if error has been reported. */
217 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
218 bool variant_p; /* Set for #pragma omp declare variant. */
219 location_t loc;
220 vec<cp_token_cache_ptr> tokens;
221 tree *attribs[2];
222};
223
224/* Helper data structure for parsing #pragma acc routine. */
225struct cp_oacc_routine_data : cp_omp_declare_simd_data {
226 tree clauses;
227};
228
229/* The cp_parser structure represents the C++ parser. */
230
231struct GTY(()) cp_parser {
232 /* The lexer from which we are obtaining tokens. */
233 cp_lexer *lexer;
234
235 /* The scope in which names should be looked up. If NULL_TREE, then
236 we look up names in the scope that is currently open in the
237 source program. If non-NULL, this is either a TYPE or
238 NAMESPACE_DECL for the scope in which we should look. It can
239 also be ERROR_MARK, when we've parsed a bogus scope.
240
241 This value is not cleared automatically after a name is looked
242 up, so we must be careful to clear it before starting a new look
243 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
244 will look up `Z' in the scope of `X', rather than the current
245 scope.) Unfortunately, it is difficult to tell when name lookup
246 is complete, because we sometimes peek at a token, look it up,
247 and then decide not to consume it. */
248 tree scope;
249
250 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
251 last lookup took place. OBJECT_SCOPE is used if an expression
252 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
253 respectively. QUALIFYING_SCOPE is used for an expression of the
254 form "X::Y"; it refers to X. */
255 tree object_scope;
256 tree qualifying_scope;
257
258 /* A stack of parsing contexts. All but the bottom entry on the
259 stack will be tentative contexts.
260
261 We parse tentatively in order to determine which construct is in
262 use in some situations. For example, in order to determine
263 whether a statement is an expression-statement or a
264 declaration-statement we parse it tentatively as a
265 declaration-statement. If that fails, we then reparse the same
266 token stream as an expression-statement. */
267 cp_parser_context *context;
268
269 /* True if we are parsing GNU C++. If this flag is not set, then
270 GNU extensions are not recognized. */
271 bool allow_gnu_extensions_p;
272
273 /* TRUE if the `>' token should be interpreted as the greater-than
274 operator. FALSE if it is the end of a template-id or
275 template-parameter-list. In C++0x mode, this flag also applies to
276 `>>' tokens, which are viewed as two consecutive `>' tokens when
277 this flag is FALSE. */
278 bool greater_than_is_operator_p;
279
280 /* TRUE if default arguments are allowed within a parameter list
281 that starts at this point. FALSE if only a gnu extension makes
282 them permissible. */
283 bool default_arg_ok_p;
284
285 /* TRUE if we are parsing an integral constant-expression. See
286 [expr.const] for a precise definition. */
287 bool integral_constant_expression_p;
288
289 /* TRUE if we are parsing an integral constant-expression -- but a
290 non-constant expression should be permitted as well. This flag
291 is used when parsing an array bound so that GNU variable-length
292 arrays are tolerated. */
293 bool allow_non_integral_constant_expression_p;
294
295 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
296 been seen that makes the expression non-constant. */
297 bool non_integral_constant_expression_p;
298
299 /* Used to track if local variable names and/or `this' are forbidden
300 in the current context. */
301#define LOCAL_VARS_FORBIDDEN (1 << 0)
302#define THIS_FORBIDDEN (1 << 1)
303#define LOCAL_VARS_AND_THIS_FORBIDDEN (LOCAL_VARS_FORBIDDEN | THIS_FORBIDDEN)
304 unsigned char local_variables_forbidden_p;
305
306 /* TRUE if the declaration we are parsing is part of a
307 linkage-specification of the form `extern string-literal
308 declaration'. */
309 bool in_unbraced_linkage_specification_p;
310
311 /* TRUE if we are presently parsing a declarator, after the
312 direct-declarator. */
313 bool in_declarator_p;
314
315 /* TRUE if we are presently parsing a template-argument-list. */
316 bool in_template_argument_list_p;
317
318 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
319 to IN_OMP_BLOCK if parsing OpenMP structured block and
320 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
321 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
322 iteration-statement, OpenMP block or loop within that switch. */
323#define IN_SWITCH_STMT 1
324#define IN_ITERATION_STMT 2
325#define IN_OMP_BLOCK 4
326#define IN_OMP_FOR 8
327#define IN_IF_STMT 16
328 unsigned char in_statement;
329
330 /* TRUE if we are presently parsing the body of a switch statement.
331 Note that this doesn't quite overlap with in_statement above.
332 The difference relates to giving the right sets of error messages:
333 "case not in switch" vs "break statement used with OpenMP...". */
334 bool in_switch_statement_p;
335
336 /* TRUE if we are parsing a type-id in an expression context. In
337 such a situation, both "type (expr)" and "type (type)" are valid
338 alternatives. */
339 bool in_type_id_in_expr_p;
340
341 /* TRUE if strings in expressions should be translated to the execution
342 character set. */
343 bool translate_strings_p;
344
345 /* TRUE if we are presently parsing the body of a function, but not
346 a local class. */
347 bool in_function_body;
348
349 /* Nonzero if we're processing a __transaction_atomic or
350 __transaction_relaxed statement. */
351 unsigned char in_transaction;
352
353 /* TRUE if we can auto-correct a colon to a scope operator. */
354 bool colon_corrects_to_scope_p;
355
356 /* TRUE if : doesn't start a class definition. Should be only used
357 together with type_definition_forbidden_message non-NULL, in
358 contexts where new types may not be defined, and the type list
359 is terminated by colon. */
360 bool colon_doesnt_start_class_def_p;
361
362 /* TRUE if we are parsing an objective c message, and ':' is permitted
363 to terminate an assignment-expression. */
364 bool objective_c_message_context_p;
365
366 /* If non-NULL, then we are parsing a construct where new type
367 definitions are not permitted. The string stored here will be
368 issued as an error message if a type is defined. */
369 const char *type_definition_forbidden_message;
370
371 /* Argument for type_definition_forbidden_message if needed. */
372 const char *type_definition_forbidden_message_arg;
373
374 /* A stack used for member functions of local classes. The lists
375 contained in an individual entry can only be processed once the
376 outermost class being defined is complete. */
377 vec<cp_unparsed_functions_entry, va_gc> *unparsed_queues;
378
379 /* The number of classes whose definitions are currently in
380 progress. */
381 unsigned num_classes_being_defined;
382
383 /* The number of template parameter lists that apply directly to the
384 current declaration. */
385 unsigned num_template_parameter_lists;
386
387 /* When parsing #pragma omp declare simd, this is a pointer to a
388 helper data structure. */
389 cp_omp_declare_simd_data * GTY((skip)) omp_declare_simd;
390
391 /* When parsing #pragma acc routine, this is a pointer to a helper data
392 structure. */
393 cp_oacc_routine_data * GTY((skip)) oacc_routine;
394
395 /* Nonzero if parsing a parameter list where 'auto' should trigger an implicit
396 template parameter. */
397 bool auto_is_implicit_function_template_parm_p;
398
399 /* TRUE if the function being declared was made a template due to its
400 parameter list containing generic type specifiers (`auto' or concept
401 identifiers) rather than an explicit template parameter list. */
402 bool fully_implicit_function_template_p;
403
404 /* TRUE if omp::directive or omp::sequence attributes may not appear. */
405 bool omp_attrs_forbidden_p;
406
407 /* Tracks the function's template parameter list when declaring a function
408 using generic type parameters. This is either a new chain in the case of a
409 fully implicit function template or an extension of the function's existing
410 template parameter list. This is tracked to optimize calls subsequent
411 calls to synthesize_implicit_template_parm during
412 cp_parser_parameter_declaration. */
413 tree implicit_template_parms;
414
415 /* The scope into which an implicit template parameter list has been
416 introduced or an existing template parameter list is being extended with
417 implicit template parameters. In most cases this is the sk_function_parms
418 scope containing the use of a generic type. In the case of an out-of-line
419 member definition using a generic type, it is the sk_class scope. */
420 cp_binding_level* implicit_template_scope;
421
422 /* True if parsing a result type in a compound requirement. This permits
423 constrained-type-specifiers inside what would normally be a trailing
424 return type. */
425 bool in_result_type_constraint_p;
426
427 /* True if a constrained-type-specifier is not allowed in this
428 context e.g., because they could never be deduced. */
429 int prevent_constrained_type_specifiers;
430
431 /* Location of the string-literal token within the current linkage
432 specification, if any, or UNKNOWN_LOCATION otherwise. */
433 location_t innermost_linkage_specification_location;
434
435};
436
437/* In parser.cc */
438extern void debug (cp_token &ref);
439extern void debug (cp_token *ptr);
440extern void cp_lexer_debug_tokens (vec<cp_token, va_gc> *);
441extern void debug (vec<cp_token, va_gc> &ref);
442extern void debug (vec<cp_token, va_gc> *ptr);
443extern void cp_debug_parser (FILE *, cp_parser *);
444extern void debug (cp_parser &ref);
445extern void debug (cp_parser *ptr);
446extern bool cp_keyword_starts_decl_specifier_p (enum rid keyword);
447
448#endif /* GCC_CP_PARSER_H */
449

source code of gcc/cp/parser.h