c-common.c (c_common_reswords): Reorder.
[gcc.git] / gcc / cp / parser.h
1 /* Data structures and function exported by the C++ Parser.
2 Copyright (C) 2010 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
16 You should have received a copy of the GNU General Public License
17 along 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 "c-family/c-pragma.h"
25
26 /* A token type for keywords, as opposed to ordinary identifiers. */
27 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
28
29 /* A token type for template-ids. If a template-id is processed while
30 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
31 the value of the CPP_TEMPLATE_ID is whatever was returned by
32 cp_parser_template_id. */
33 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
34
35 /* A token type for nested-name-specifiers. If a
36 nested-name-specifier is processed while parsing tentatively, it is
37 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
38 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
39 cp_parser_nested_name_specifier_opt. */
40 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
41
42 /* The number of token types, including C++-specific ones. */
43 #define N_CP_TTYPES ((int) (CPP_NESTED_NAME_SPECIFIER + 1))
44
45 /* A token's value and its associated deferred access checks and
46 qualifying scope. */
47
48 struct GTY(()) tree_check {
49 /* The value associated with the token. */
50 tree value;
51 /* The checks that have been associated with value. */
52 VEC (deferred_access_check, gc)* checks;
53 /* The token's qualifying scope (used when it is a
54 CPP_NESTED_NAME_SPECIFIER). */
55 tree qualifying_scope;
56 };
57
58 /* A C++ token. */
59
60 typedef struct GTY (()) cp_token {
61 /* The kind of token. */
62 ENUM_BITFIELD (cpp_ttype) type : 8;
63 /* If this token is a keyword, this value indicates which keyword.
64 Otherwise, this value is RID_MAX. */
65 ENUM_BITFIELD (rid) keyword : 8;
66 /* Token flags. */
67 unsigned char flags;
68 /* Identifier for the pragma. */
69 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
70 /* True if this token is from a context where it is implicitly extern "C" */
71 BOOL_BITFIELD implicit_extern_c : 1;
72 /* True for a CPP_NAME token that is not a keyword (i.e., for which
73 KEYWORD is RID_MAX) iff this name was looked up and found to be
74 ambiguous. An error has already been reported. */
75 BOOL_BITFIELD ambiguous_p : 1;
76 /* True for a token that has been purged. If a token is purged,
77 it is no longer a valid token and it should be considered
78 deleted. */
79 BOOL_BITFIELD purged_p : 1;
80 /* The location at which this token was found. */
81 location_t location;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 DEF_VEC_O (cp_token);
92 DEF_VEC_ALLOC_O (cp_token,gc);
93 DEF_VEC_ALLOC_O (cp_token,heap);
94
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
99
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
103 it is created. */
104
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 VEC(cp_token,gc) *buffer;
109
110 /* A pointer just past the last available token. The tokens
111 in this lexer are [buffer, last_token). */
112 cp_token_position GTY ((skip)) last_token;
113
114 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
115 no more available tokens. */
116 cp_token_position GTY ((skip)) next_token;
117
118 /* A stack indicating positions at which cp_lexer_save_tokens was
119 called. The top entry is the most recent position at which we
120 began saving tokens. If the stack is non-empty, we are saving
121 tokens. */
122 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
123
124 /* The next lexer in a linked list of lexers. */
125 struct cp_lexer *next;
126
127 /* True if we should output debugging information. */
128 bool debugging_p;
129
130 /* True if we're in the context of parsing a pragma, and should not
131 increment past the end-of-line marker. */
132 bool in_pragma;
133 } cp_lexer;
134
135 DEF_VEC_O (cp_lexer);
136 DEF_VEC_ALLOC_O (cp_lexer,heap);
137
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
142 a lexer. */
143
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
147
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 typedef cp_token_cache *cp_token_cache_ptr;
153 DEF_VEC_P (cp_token_cache_ptr);
154 DEF_VEC_ALLOC_P (cp_token_cache_ptr,gc);
155
156 struct cp_token_ident_d
157 {
158 unsigned int ident_len;
159 const char *ident_str;
160 unsigned int before_len;
161 const char *before_str;
162 unsigned int after_len;
163 const char *after_str;
164 };
165
166 typedef struct cp_token_ident_d cp_token_ident;
167
168 /* An entry in a queue of function arguments that require post-processing. */
169
170 typedef struct GTY(()) cp_default_arg_entry_d {
171 /* The current_class_type when we parsed this arg. */
172 tree class_type;
173
174 /* The function decl itself. */
175 tree decl;
176 } cp_default_arg_entry;
177
178 DEF_VEC_O(cp_default_arg_entry);
179 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
180
181 /* An entry in a stack for member functions of local classes. */
182
183 typedef struct GTY(()) cp_unparsed_functions_entry_d {
184 /* Functions with default arguments that require post-processing.
185 Functions appear in this list in declaration order. */
186 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
187
188 /* Functions with defintions that require post-processing. Functions
189 appear in this list in declaration order. */
190 VEC(tree,gc) *funs_with_definitions;
191 } cp_unparsed_functions_entry;
192
193 DEF_VEC_O(cp_unparsed_functions_entry);
194 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
195
196 /* The status of a tentative parse. */
197
198 typedef enum cp_parser_status_kind
199 {
200 /* No errors have occurred. */
201 CP_PARSER_STATUS_KIND_NO_ERROR,
202 /* An error has occurred. */
203 CP_PARSER_STATUS_KIND_ERROR,
204 /* We are committed to this tentative parse, whether or not an error
205 has occurred. */
206 CP_PARSER_STATUS_KIND_COMMITTED
207 } cp_parser_status_kind;
208
209
210 /* Context that is saved and restored when parsing tentatively. */
211 typedef struct GTY (()) cp_parser_context {
212 /* If this is a tentative parsing context, the status of the
213 tentative parse. */
214 enum cp_parser_status_kind status;
215 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
216 that are looked up in this context must be looked up both in the
217 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
218 the context of the containing expression. */
219 tree object_type;
220
221 /* The next parsing context in the stack. */
222 struct cp_parser_context *next;
223 } cp_parser_context;
224
225
226 /* The cp_parser structure represents the C++ parser. */
227
228 typedef struct GTY(()) cp_parser {
229 /* The lexer from which we are obtaining tokens. */
230 cp_lexer *lexer;
231
232 /* The scope in which names should be looked up. If NULL_TREE, then
233 we look up names in the scope that is currently open in the
234 source program. If non-NULL, this is either a TYPE or
235 NAMESPACE_DECL for the scope in which we should look. It can
236 also be ERROR_MARK, when we've parsed a bogus scope.
237
238 This value is not cleared automatically after a name is looked
239 up, so we must be careful to clear it before starting a new look
240 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
241 will look up `Z' in the scope of `X', rather than the current
242 scope.) Unfortunately, it is difficult to tell when name lookup
243 is complete, because we sometimes peek at a token, look it up,
244 and then decide not to consume it. */
245 tree scope;
246
247 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
248 last lookup took place. OBJECT_SCOPE is used if an expression
249 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
250 respectively. QUALIFYING_SCOPE is used for an expression of the
251 form "X::Y"; it refers to X. */
252 tree object_scope;
253 tree qualifying_scope;
254
255 /* A stack of parsing contexts. All but the bottom entry on the
256 stack will be tentative contexts.
257
258 We parse tentatively in order to determine which construct is in
259 use in some situations. For example, in order to determine
260 whether a statement is an expression-statement or a
261 declaration-statement we parse it tentatively as a
262 declaration-statement. If that fails, we then reparse the same
263 token stream as an expression-statement. */
264 cp_parser_context *context;
265
266 /* True if we are parsing GNU C++. If this flag is not set, then
267 GNU extensions are not recognized. */
268 bool allow_gnu_extensions_p;
269
270 /* TRUE if the `>' token should be interpreted as the greater-than
271 operator. FALSE if it is the end of a template-id or
272 template-parameter-list. In C++0x mode, this flag also applies to
273 `>>' tokens, which are viewed as two consecutive `>' tokens when
274 this flag is FALSE. */
275 bool greater_than_is_operator_p;
276
277 /* TRUE if default arguments are allowed within a parameter list
278 that starts at this point. FALSE if only a gnu extension makes
279 them permissible. */
280 bool default_arg_ok_p;
281
282 /* TRUE if we are parsing an integral constant-expression. See
283 [expr.const] for a precise definition. */
284 bool integral_constant_expression_p;
285
286 /* TRUE if we are parsing an integral constant-expression -- but a
287 non-constant expression should be permitted as well. This flag
288 is used when parsing an array bound so that GNU variable-length
289 arrays are tolerated. */
290 bool allow_non_integral_constant_expression_p;
291
292 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
293 been seen that makes the expression non-constant. */
294 bool non_integral_constant_expression_p;
295
296 /* TRUE if local variable names and `this' are forbidden in the
297 current context. */
298 bool local_variables_forbidden_p;
299
300 /* TRUE if the declaration we are parsing is part of a
301 linkage-specification of the form `extern string-literal
302 declaration'. */
303 bool in_unbraced_linkage_specification_p;
304
305 /* TRUE if we are presently parsing a declarator, after the
306 direct-declarator. */
307 bool in_declarator_p;
308
309 /* TRUE if we are presently parsing a template-argument-list. */
310 bool in_template_argument_list_p;
311
312 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
313 to IN_OMP_BLOCK if parsing OpenMP structured block and
314 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
315 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
316 iteration-statement, OpenMP block or loop within that switch. */
317 #define IN_SWITCH_STMT 1
318 #define IN_ITERATION_STMT 2
319 #define IN_OMP_BLOCK 4
320 #define IN_OMP_FOR 8
321 #define IN_IF_STMT 16
322 unsigned char in_statement;
323
324 /* TRUE if we are presently parsing the body of a switch statement.
325 Note that this doesn't quite overlap with in_statement above.
326 The difference relates to giving the right sets of error messages:
327 "case not in switch" vs "break statement used with OpenMP...". */
328 bool in_switch_statement_p;
329
330 /* TRUE if we are parsing a type-id in an expression context. In
331 such a situation, both "type (expr)" and "type (type)" are valid
332 alternatives. */
333 bool in_type_id_in_expr_p;
334
335 /* TRUE if we are currently in a header file where declarations are
336 implicitly extern "C". */
337 bool implicit_extern_c;
338
339 /* TRUE if strings in expressions should be translated to the execution
340 character set. */
341 bool translate_strings_p;
342
343 /* TRUE if we are presently parsing the body of a function, but not
344 a local class. */
345 bool in_function_body;
346
347 /* TRUE if we can auto-correct a colon to a scope operator. */
348 bool colon_corrects_to_scope_p;
349
350 /* If non-NULL, then we are parsing a construct where new type
351 definitions are not permitted. The string stored here will be
352 issued as an error message if a type is defined. */
353 const char *type_definition_forbidden_message;
354
355 /* A stack used for member functions of local classes. The lists
356 contained in an individual entry can only be processed once the
357 outermost class being defined is complete. */
358 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
359
360 /* The number of classes whose definitions are currently in
361 progress. */
362 unsigned num_classes_being_defined;
363
364 /* The number of template parameter lists that apply directly to the
365 current declaration. */
366 unsigned num_template_parameter_lists;
367 } cp_parser;
368
369 /* In parser.c */
370 #ifdef ENABLE_CHECKING
371 extern void cp_lexer_dump_tokens (FILE *, VEC(cp_token,gc) *, unsigned);
372 extern void cp_lexer_debug_tokens (VEC(cp_token,gc) *);
373 #endif
374
375 #endif /* GCC_CP_PARSER_H */