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