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