]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
4514aa8c NS |
2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, |
3 | 2005 Free Software Foundation, Inc. | |
a723baf1 MM |
4 | Written by Mark Mitchell <mark@codesourcery.com>. |
5 | ||
f5adbb8d | 6 | This file is part of GCC. |
a723baf1 | 7 | |
f5adbb8d | 8 | GCC is free software; you can redistribute it and/or modify it |
a723baf1 MM |
9 | under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
f5adbb8d | 13 | GCC is distributed in the hope that it will be useful, but |
a723baf1 MM |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 19 | along with GCC; see the file COPYING. If not, write to the Free |
a723baf1 MM |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
21 | 02111-1307, USA. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
25 | #include "coretypes.h" | |
26 | #include "tm.h" | |
27 | #include "dyn-string.h" | |
28 | #include "varray.h" | |
29 | #include "cpplib.h" | |
30 | #include "tree.h" | |
31 | #include "cp-tree.h" | |
32 | #include "c-pragma.h" | |
33 | #include "decl.h" | |
34 | #include "flags.h" | |
35 | #include "diagnostic.h" | |
a723baf1 MM |
36 | #include "toplev.h" |
37 | #include "output.h" | |
62d1db17 | 38 | #include "target.h" |
a723baf1 MM |
39 | |
40 | \f | |
41 | /* The lexer. */ | |
42 | ||
c162c75e MA |
43 | /* The cp_lexer_* routines mediate between the lexer proper (in libcpp |
44 | and c-lex.c) and the C++ parser. */ | |
a723baf1 MM |
45 | |
46 | /* A C++ token. */ | |
47 | ||
48 | typedef struct cp_token GTY (()) | |
49 | { | |
50 | /* The kind of token. */ | |
df2b750f | 51 | ENUM_BITFIELD (cpp_ttype) type : 8; |
a723baf1 MM |
52 | /* If this token is a keyword, this value indicates which keyword. |
53 | Otherwise, this value is RID_MAX. */ | |
df2b750f | 54 | ENUM_BITFIELD (rid) keyword : 8; |
f4abade9 GB |
55 | /* Token flags. */ |
56 | unsigned char flags; | |
03fd3f84 | 57 | /* True if this token is from a system header. */ |
c162c75e | 58 | BOOL_BITFIELD in_system_header : 1; |
7d381002 MA |
59 | /* True if this token is from a context where it is implicitly extern "C" */ |
60 | BOOL_BITFIELD implicit_extern_c : 1; | |
522df488 DN |
61 | /* The value associated with this token, if any. */ |
62 | tree value; | |
82a98427 NS |
63 | /* The location at which this token was found. */ |
64 | location_t location; | |
a723baf1 MM |
65 | } cp_token; |
66 | ||
0c5e4866 NS |
67 | /* We use a stack of token pointer for saving token sets. */ |
68 | typedef struct cp_token *cp_token_position; | |
69 | DEF_VEC_MALLOC_P (cp_token_position); | |
70 | ||
76aebc9f NS |
71 | static const cp_token eof_token = |
72 | { | |
73 | CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE, | |
74 | #if USE_MAPPED_LOCATION | |
75 | 0 | |
76 | #else | |
77 | {0, 0} | |
78 | #endif | |
79 | }; | |
0c5e4866 | 80 | |
a723baf1 MM |
81 | /* The cp_lexer structure represents the C++ lexer. It is responsible |
82 | for managing the token stream from the preprocessor and supplying | |
c162c75e | 83 | it to the parser. Tokens are never added to the cp_lexer after |
03fd3f84 | 84 | it is created. */ |
a723baf1 MM |
85 | |
86 | typedef struct cp_lexer GTY (()) | |
87 | { | |
0c5e4866 NS |
88 | /* The memory allocated for the buffer. NULL if this lexer does not |
89 | own the token buffer. */ | |
76aebc9f NS |
90 | cp_token * GTY ((length ("%h.buffer_length"))) buffer; |
91 | /* If the lexer owns the buffer, this is the number of tokens in the | |
92 | buffer. */ | |
93 | size_t buffer_length; | |
0c5e4866 | 94 | |
c162c75e | 95 | /* A pointer just past the last available token. The tokens |
03fd3f84 | 96 | in this lexer are [buffer, last_token). */ |
0c5e4866 | 97 | cp_token_position GTY ((skip)) last_token; |
c162c75e | 98 | |
0c5e4866 | 99 | /* The next available token. If NEXT_TOKEN is &eof_token, then there are |
a723baf1 | 100 | no more available tokens. */ |
0c5e4866 | 101 | cp_token_position GTY ((skip)) next_token; |
a723baf1 MM |
102 | |
103 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
104 | called. The top entry is the most recent position at which we | |
0c5e4866 NS |
105 | began saving tokens. If the stack is non-empty, we are saving |
106 | tokens. */ | |
107 | VEC (cp_token_position) *GTY ((skip)) saved_tokens; | |
a723baf1 | 108 | |
a723baf1 MM |
109 | /* True if we should output debugging information. */ |
110 | bool debugging_p; | |
111 | ||
112 | /* The next lexer in a linked list of lexers. */ | |
113 | struct cp_lexer *next; | |
114 | } cp_lexer; | |
115 | ||
c162c75e MA |
116 | /* cp_token_cache is a range of tokens. There is no need to represent |
117 | allocate heap memory for it, since tokens are never removed from the | |
118 | lexer's array. There is also no need for the GC to walk through | |
119 | a cp_token_cache, since everything in here is referenced through | |
03fd3f84 | 120 | a lexer. */ |
c162c75e MA |
121 | |
122 | typedef struct cp_token_cache GTY(()) | |
123 | { | |
03fd3f84 | 124 | /* The beginning of the token range. */ |
c162c75e MA |
125 | cp_token * GTY((skip)) first; |
126 | ||
03fd3f84 | 127 | /* Points immediately after the last token in the range. */ |
c162c75e MA |
128 | cp_token * GTY ((skip)) last; |
129 | } cp_token_cache; | |
130 | ||
a723baf1 MM |
131 | /* Prototypes. */ |
132 | ||
17211ab5 | 133 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 134 | (void); |
a723baf1 | 135 | static cp_lexer *cp_lexer_new_from_tokens |
c162c75e MA |
136 | (cp_token_cache *tokens); |
137 | static void cp_lexer_destroy | |
138 | (cp_lexer *); | |
a723baf1 | 139 | static int cp_lexer_saving_tokens |
94edc4ab | 140 | (const cp_lexer *); |
0c5e4866 NS |
141 | static cp_token_position cp_lexer_token_position |
142 | (cp_lexer *, bool); | |
143 | static cp_token *cp_lexer_token_at | |
144 | (cp_lexer *, cp_token_position); | |
a723baf1 | 145 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 146 | (cp_lexer *, cp_token *); |
c162c75e MA |
147 | static inline cp_token *cp_lexer_peek_token |
148 | (cp_lexer *); | |
a723baf1 | 149 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 150 | (cp_lexer *, size_t); |
f7b5ecd9 | 151 | static inline bool cp_lexer_next_token_is |
94edc4ab | 152 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 153 | static bool cp_lexer_next_token_is_not |
94edc4ab | 154 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 155 | static bool cp_lexer_next_token_is_keyword |
94edc4ab | 156 | (cp_lexer *, enum rid); |
21526606 | 157 | static cp_token *cp_lexer_consume_token |
94edc4ab | 158 | (cp_lexer *); |
a723baf1 MM |
159 | static void cp_lexer_purge_token |
160 | (cp_lexer *); | |
161 | static void cp_lexer_purge_tokens_after | |
0c5e4866 | 162 | (cp_lexer *, cp_token_position); |
c162c75e MA |
163 | static void cp_lexer_handle_pragma |
164 | (cp_lexer *); | |
a723baf1 | 165 | static void cp_lexer_save_tokens |
94edc4ab | 166 | (cp_lexer *); |
a723baf1 | 167 | static void cp_lexer_commit_tokens |
94edc4ab | 168 | (cp_lexer *); |
a723baf1 | 169 | static void cp_lexer_rollback_tokens |
94edc4ab | 170 | (cp_lexer *); |
6983ea08 | 171 | #ifdef ENABLE_CHECKING |
a723baf1 | 172 | static void cp_lexer_print_token |
94edc4ab | 173 | (FILE *, cp_token *); |
21526606 | 174 | static inline bool cp_lexer_debugging_p |
94edc4ab | 175 | (cp_lexer *); |
a723baf1 | 176 | static void cp_lexer_start_debugging |
94edc4ab | 177 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 178 | static void cp_lexer_stop_debugging |
94edc4ab | 179 | (cp_lexer *) ATTRIBUTE_UNUSED; |
6983ea08 | 180 | #else |
2cfe82fe ZW |
181 | /* If we define cp_lexer_debug_stream to NULL it will provoke warnings |
182 | about passing NULL to functions that require non-NULL arguments | |
183 | (fputs, fprintf). It will never be used, so all we need is a value | |
184 | of the right type that's guaranteed not to be NULL. */ | |
185 | #define cp_lexer_debug_stream stdout | |
186 | #define cp_lexer_print_token(str, tok) (void) 0 | |
6983ea08 MA |
187 | #define cp_lexer_debugging_p(lexer) 0 |
188 | #endif /* ENABLE_CHECKING */ | |
a723baf1 | 189 | |
c162c75e MA |
190 | static cp_token_cache *cp_token_cache_new |
191 | (cp_token *, cp_token *); | |
192 | ||
a723baf1 | 193 | /* Manifest constants. */ |
c162c75e | 194 | #define CP_LEXER_BUFFER_SIZE 10000 |
0c5e4866 | 195 | #define CP_SAVED_TOKEN_STACK 5 |
a723baf1 MM |
196 | |
197 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
198 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
199 | ||
200 | /* A token type for template-ids. If a template-id is processed while | |
201 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
202 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
203 | cp_parser_template_id. */ | |
204 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
205 | ||
206 | /* A token type for nested-name-specifiers. If a | |
207 | nested-name-specifier is processed while parsing tentatively, it is | |
208 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
209 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
210 | cp_parser_nested_name_specifier_opt. */ | |
211 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
212 | ||
213 | /* A token type for tokens that are not tokens at all; these are used | |
c162c75e | 214 | to represent slots in the array where there used to be a token |
03fd3f84 | 215 | that has now been deleted. */ |
b8b94c5b PB |
216 | #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1)) |
217 | ||
218 | /* The number of token types, including C++-specific ones. */ | |
219 | #define N_CP_TTYPES ((int) (CPP_PURGED + 1)) | |
a723baf1 MM |
220 | |
221 | /* Variables. */ | |
222 | ||
6983ea08 | 223 | #ifdef ENABLE_CHECKING |
a723baf1 MM |
224 | /* The stream to which debugging output should be written. */ |
225 | static FILE *cp_lexer_debug_stream; | |
6983ea08 | 226 | #endif /* ENABLE_CHECKING */ |
a723baf1 | 227 | |
17211ab5 GK |
228 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
229 | preprocessor. */ | |
a723baf1 MM |
230 | |
231 | static cp_lexer * | |
17211ab5 | 232 | cp_lexer_new_main (void) |
a723baf1 | 233 | { |
17211ab5 | 234 | cp_token first_token; |
76aebc9f NS |
235 | cp_lexer *lexer; |
236 | cp_token *pos; | |
237 | size_t alloc; | |
238 | size_t space; | |
239 | cp_token *buffer; | |
17211ab5 | 240 | |
3d8a8aad MS |
241 | /* It's possible that lexing the first token will load a PCH file, |
242 | which is a GC collection point. So we have to grab the first | |
243 | token before allocating any memory. Pragmas must not be deferred | |
244 | as -fpch-preprocess can generate a pragma to load the PCH file in | |
245 | the preprocessed output used by -save-temps. */ | |
246 | cp_lexer_get_preprocessor_token (NULL, &first_token); | |
247 | ||
03fd3f84 | 248 | /* Tell cpplib we want CPP_PRAGMA tokens. */ |
c162c75e MA |
249 | cpp_get_options (parse_in)->defer_pragmas = true; |
250 | ||
251 | /* Tell c_lex not to merge string constants. */ | |
252 | c_lex_return_raw_strings = true; | |
253 | ||
18c81520 | 254 | c_common_no_more_pch (); |
a723baf1 MM |
255 | |
256 | /* Allocate the memory. */ | |
99dd239f | 257 | lexer = GGC_CNEW (cp_lexer); |
a723baf1 | 258 | |
6983ea08 | 259 | #ifdef ENABLE_CHECKING |
c162c75e | 260 | /* Initially we are not debugging. */ |
a723baf1 | 261 | lexer->debugging_p = false; |
6983ea08 | 262 | #endif /* ENABLE_CHECKING */ |
76aebc9f NS |
263 | lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK); |
264 | ||
265 | /* Create the buffer. */ | |
266 | alloc = CP_LEXER_BUFFER_SIZE; | |
267 | buffer = ggc_alloc (alloc * sizeof (cp_token)); | |
a723baf1 | 268 | |
76aebc9f NS |
269 | /* Put the first token in the buffer. */ |
270 | space = alloc; | |
271 | pos = buffer; | |
272 | *pos = first_token; | |
273 | ||
03fd3f84 | 274 | /* Get the remaining tokens from the preprocessor. */ |
76aebc9f | 275 | while (pos->type != CPP_EOF) |
c162c75e | 276 | { |
76aebc9f NS |
277 | pos++; |
278 | if (!--space) | |
279 | { | |
280 | space = alloc; | |
281 | alloc *= 2; | |
282 | buffer = ggc_realloc (buffer, alloc * sizeof (cp_token)); | |
283 | pos = buffer + space; | |
284 | } | |
285 | cp_lexer_get_preprocessor_token (lexer, pos); | |
c162c75e | 286 | } |
76aebc9f NS |
287 | lexer->buffer = buffer; |
288 | lexer->buffer_length = alloc - space; | |
289 | lexer->last_token = pos; | |
290 | lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token; | |
c162c75e MA |
291 | |
292 | /* Pragma processing (via cpp_handle_deferred_pragma) may result in | |
293 | direct calls to c_lex. Those callers all expect c_lex to do | |
294 | string constant concatenation. */ | |
295 | c_lex_return_raw_strings = false; | |
296 | ||
2cfe82fe | 297 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
a723baf1 MM |
298 | return lexer; |
299 | } | |
300 | ||
c162c75e | 301 | /* Create a new lexer whose token stream is primed with the tokens in |
2cfe82fe | 302 | CACHE. When these tokens are exhausted, no new tokens will be read. */ |
a723baf1 MM |
303 | |
304 | static cp_lexer * | |
2cfe82fe | 305 | cp_lexer_new_from_tokens (cp_token_cache *cache) |
a723baf1 | 306 | { |
2cfe82fe ZW |
307 | cp_token *first = cache->first; |
308 | cp_token *last = cache->last; | |
c162c75e | 309 | cp_lexer *lexer = GGC_CNEW (cp_lexer); |
17211ab5 | 310 | |
0c5e4866 | 311 | /* We do not own the buffer. */ |
76aebc9f NS |
312 | lexer->buffer = NULL; |
313 | lexer->buffer_length = 0; | |
314 | lexer->next_token = first == last ? (cp_token *)&eof_token : first; | |
0c5e4866 NS |
315 | lexer->last_token = last; |
316 | ||
317 | lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK); | |
21526606 | 318 | |
6983ea08 | 319 | #ifdef ENABLE_CHECKING |
c162c75e | 320 | /* Initially we are not debugging. */ |
17211ab5 | 321 | lexer->debugging_p = false; |
c162c75e | 322 | #endif |
a723baf1 | 323 | |
2cfe82fe ZW |
324 | gcc_assert (lexer->next_token->type != CPP_PURGED); |
325 | return lexer; | |
c162c75e MA |
326 | } |
327 | ||
03fd3f84 | 328 | /* Frees all resources associated with LEXER. */ |
c162c75e MA |
329 | |
330 | static void | |
331 | cp_lexer_destroy (cp_lexer *lexer) | |
332 | { | |
0c5e4866 NS |
333 | if (lexer->buffer) |
334 | ggc_free (lexer->buffer); | |
335 | VEC_free (cp_token_position, lexer->saved_tokens); | |
c162c75e MA |
336 | ggc_free (lexer); |
337 | } | |
338 | ||
4de8668e | 339 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 340 | |
6983ea08 MA |
341 | #ifdef ENABLE_CHECKING |
342 | ||
f7b5ecd9 MM |
343 | static inline bool |
344 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 345 | { |
f7b5ecd9 MM |
346 | return lexer->debugging_p; |
347 | } | |
348 | ||
6983ea08 MA |
349 | #endif /* ENABLE_CHECKING */ |
350 | ||
0c5e4866 NS |
351 | static inline cp_token_position |
352 | cp_lexer_token_position (cp_lexer *lexer, bool previous_p) | |
a723baf1 | 353 | { |
0c5e4866 NS |
354 | gcc_assert (!previous_p || lexer->next_token != &eof_token); |
355 | ||
356 | return lexer->next_token - previous_p; | |
a723baf1 MM |
357 | } |
358 | ||
a668c6ad | 359 | static inline cp_token * |
0c5e4866 | 360 | cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos) |
a668c6ad | 361 | { |
0c5e4866 | 362 | return pos; |
a668c6ad MM |
363 | } |
364 | ||
4de8668e | 365 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 | 366 | |
0c5e4866 | 367 | static inline int |
94edc4ab | 368 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 | 369 | { |
0c5e4866 | 370 | return VEC_length (cp_token_position, lexer->saved_tokens) != 0; |
a723baf1 MM |
371 | } |
372 | ||
76aebc9f NS |
373 | /* Store the next token from the preprocessor in *TOKEN. Return true |
374 | if we reach EOF. */ | |
a723baf1 | 375 | |
21526606 | 376 | static void |
94edc4ab NN |
377 | cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
378 | cp_token *token) | |
a723baf1 | 379 | { |
7d381002 | 380 | static int is_extern_c = 0; |
a723baf1 | 381 | |
51e63e60 | 382 | /* Get a new token from the preprocessor. */ |
b68b6828 PB |
383 | token->type |
384 | = c_lex_with_flags (&token->value, &token->location, &token->flags); | |
c162c75e | 385 | token->in_system_header = in_system_header; |
a723baf1 | 386 | |
7d381002 MA |
387 | /* On some systems, some header files are surrounded by an |
388 | implicit extern "C" block. Set a flag in the token if it | |
03fd3f84 | 389 | comes from such a header. */ |
7d381002 MA |
390 | is_extern_c += pending_lang_change; |
391 | pending_lang_change = 0; | |
392 | token->implicit_extern_c = is_extern_c > 0; | |
393 | ||
a723baf1 | 394 | /* Check to see if this token is a keyword. */ |
21526606 | 395 | if (token->type == CPP_NAME |
a723baf1 MM |
396 | && C_IS_RESERVED_WORD (token->value)) |
397 | { | |
398 | /* Mark this token as a keyword. */ | |
399 | token->type = CPP_KEYWORD; | |
400 | /* Record which keyword. */ | |
401 | token->keyword = C_RID_CODE (token->value); | |
402 | /* Update the value. Some keywords are mapped to particular | |
403 | entities, rather than simply having the value of the | |
404 | corresponding IDENTIFIER_NODE. For example, `__const' is | |
405 | mapped to `const'. */ | |
406 | token->value = ridpointers[token->keyword]; | |
407 | } | |
408 | else | |
409 | token->keyword = RID_MAX; | |
410 | } | |
411 | ||
03fd3f84 | 412 | /* Update the globals input_location and in_system_header from TOKEN. */ |
2cfe82fe ZW |
413 | static inline void |
414 | cp_lexer_set_source_position_from_token (cp_token *token) | |
415 | { | |
416 | if (token->type != CPP_EOF) | |
417 | { | |
418 | input_location = token->location; | |
419 | in_system_header = token->in_system_header; | |
420 | } | |
421 | } | |
422 | ||
a723baf1 MM |
423 | /* Return a pointer to the next token in the token stream, but do not |
424 | consume it. */ | |
425 | ||
c162c75e MA |
426 | static inline cp_token * |
427 | cp_lexer_peek_token (cp_lexer *lexer) | |
a723baf1 | 428 | { |
a723baf1 | 429 | if (cp_lexer_debugging_p (lexer)) |
0c5e4866 NS |
430 | { |
431 | fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream); | |
432 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
433 | putc ('\n', cp_lexer_debug_stream); | |
434 | } | |
2cfe82fe | 435 | return lexer->next_token; |
a723baf1 MM |
436 | } |
437 | ||
438 | /* Return true if the next token has the indicated TYPE. */ | |
439 | ||
2cfe82fe | 440 | static inline bool |
94edc4ab | 441 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 | 442 | { |
2cfe82fe | 443 | return cp_lexer_peek_token (lexer)->type == type; |
a723baf1 MM |
444 | } |
445 | ||
446 | /* Return true if the next token does not have the indicated TYPE. */ | |
447 | ||
2cfe82fe | 448 | static inline bool |
94edc4ab | 449 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
450 | { |
451 | return !cp_lexer_next_token_is (lexer, type); | |
452 | } | |
453 | ||
454 | /* Return true if the next token is the indicated KEYWORD. */ | |
455 | ||
2cfe82fe | 456 | static inline bool |
94edc4ab | 457 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 MM |
458 | { |
459 | cp_token *token; | |
460 | ||
461 | /* Peek at the next token. */ | |
462 | token = cp_lexer_peek_token (lexer); | |
463 | /* Check to see if it is the indicated keyword. */ | |
464 | return token->keyword == keyword; | |
465 | } | |
466 | ||
467 | /* Return a pointer to the Nth token in the token stream. If N is 1, | |
2cfe82fe ZW |
468 | then this is precisely equivalent to cp_lexer_peek_token (except |
469 | that it is not inline). One would like to disallow that case, but | |
470 | there is one case (cp_parser_nth_token_starts_template_id) where | |
471 | the caller passes a variable for N and it might be 1. */ | |
a723baf1 MM |
472 | |
473 | static cp_token * | |
94edc4ab | 474 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
475 | { |
476 | cp_token *token; | |
477 | ||
478 | /* N is 1-based, not zero-based. */ | |
0c5e4866 | 479 | gcc_assert (n > 0 && lexer->next_token != &eof_token); |
a723baf1 | 480 | |
2cfe82fe ZW |
481 | if (cp_lexer_debugging_p (lexer)) |
482 | fprintf (cp_lexer_debug_stream, | |
483 | "cp_lexer: peeking ahead %ld at token: ", (long)n); | |
484 | ||
c162c75e | 485 | --n; |
a723baf1 | 486 | token = lexer->next_token; |
c162c75e | 487 | while (n != 0) |
a723baf1 | 488 | { |
c162c75e | 489 | ++token; |
0c5e4866 NS |
490 | if (token == lexer->last_token) |
491 | { | |
76aebc9f | 492 | token = (cp_token *)&eof_token; |
0c5e4866 NS |
493 | break; |
494 | } | |
495 | ||
c162c75e MA |
496 | if (token->type != CPP_PURGED) |
497 | --n; | |
a723baf1 MM |
498 | } |
499 | ||
2cfe82fe ZW |
500 | if (cp_lexer_debugging_p (lexer)) |
501 | { | |
502 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
503 | putc ('\n', cp_lexer_debug_stream); | |
504 | } | |
505 | ||
a723baf1 MM |
506 | return token; |
507 | } | |
508 | ||
2cfe82fe ZW |
509 | /* Return the next token, and advance the lexer's next_token pointer |
510 | to point to the next non-purged token. */ | |
a723baf1 MM |
511 | |
512 | static cp_token * | |
94edc4ab | 513 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 | 514 | { |
2cfe82fe | 515 | cp_token *token = lexer->next_token; |
a723baf1 | 516 | |
0c5e4866 NS |
517 | gcc_assert (token != &eof_token); |
518 | ||
2cfe82fe | 519 | do |
0c5e4866 NS |
520 | { |
521 | lexer->next_token++; | |
522 | if (lexer->next_token == lexer->last_token) | |
523 | { | |
76aebc9f | 524 | lexer->next_token = (cp_token *)&eof_token; |
0c5e4866 NS |
525 | break; |
526 | } | |
527 | ||
528 | } | |
2cfe82fe | 529 | while (lexer->next_token->type == CPP_PURGED); |
0c5e4866 | 530 | |
2cfe82fe | 531 | cp_lexer_set_source_position_from_token (token); |
0c5e4866 | 532 | |
a723baf1 MM |
533 | /* Provide debugging output. */ |
534 | if (cp_lexer_debugging_p (lexer)) | |
535 | { | |
2cfe82fe | 536 | fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream); |
a723baf1 | 537 | cp_lexer_print_token (cp_lexer_debug_stream, token); |
2cfe82fe | 538 | putc ('\n', cp_lexer_debug_stream); |
a723baf1 | 539 | } |
0c5e4866 | 540 | |
a723baf1 MM |
541 | return token; |
542 | } | |
543 | ||
2cfe82fe ZW |
544 | /* Permanently remove the next token from the token stream, and |
545 | advance the next_token pointer to refer to the next non-purged | |
546 | token. */ | |
a723baf1 MM |
547 | |
548 | static void | |
549 | cp_lexer_purge_token (cp_lexer *lexer) | |
550 | { | |
c162c75e | 551 | cp_token *tok = lexer->next_token; |
0c5e4866 NS |
552 | |
553 | gcc_assert (tok != &eof_token); | |
c162c75e MA |
554 | tok->type = CPP_PURGED; |
555 | tok->location = UNKNOWN_LOCATION; | |
556 | tok->value = NULL_TREE; | |
557 | tok->keyword = RID_MAX; | |
2cfe82fe ZW |
558 | |
559 | do | |
0c5e4866 NS |
560 | { |
561 | tok++; | |
562 | if (tok == lexer->last_token) | |
563 | { | |
76aebc9f | 564 | tok = (cp_token *)&eof_token; |
0c5e4866 NS |
565 | break; |
566 | } | |
567 | } | |
568 | while (tok->type == CPP_PURGED); | |
569 | lexer->next_token = tok; | |
a723baf1 MM |
570 | } |
571 | ||
c162c75e | 572 | /* Permanently remove all tokens after TOK, up to, but not |
a723baf1 MM |
573 | including, the token that will be returned next by |
574 | cp_lexer_peek_token. */ | |
575 | ||
576 | static void | |
c162c75e | 577 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok) |
a723baf1 | 578 | { |
0c5e4866 | 579 | cp_token *peek = lexer->next_token; |
a723baf1 | 580 | |
0c5e4866 NS |
581 | if (peek == &eof_token) |
582 | peek = lexer->last_token; | |
583 | ||
c162c75e MA |
584 | gcc_assert (tok < peek); |
585 | ||
586 | for ( tok += 1; tok != peek; tok += 1) | |
a723baf1 | 587 | { |
c162c75e MA |
588 | tok->type = CPP_PURGED; |
589 | tok->location = UNKNOWN_LOCATION; | |
590 | tok->value = NULL_TREE; | |
591 | tok->keyword = RID_MAX; | |
a723baf1 | 592 | } |
c162c75e MA |
593 | } |
594 | ||
03fd3f84 | 595 | /* Consume and handle a pragma token. */ |
c162c75e MA |
596 | static void |
597 | cp_lexer_handle_pragma (cp_lexer *lexer) | |
598 | { | |
36952dea ZW |
599 | cpp_string s; |
600 | cp_token *token = cp_lexer_consume_token (lexer); | |
601 | gcc_assert (token->type == CPP_PRAGMA); | |
602 | gcc_assert (token->value); | |
c162c75e | 603 | |
36952dea ZW |
604 | s.len = TREE_STRING_LENGTH (token->value); |
605 | s.text = (const unsigned char *) TREE_STRING_POINTER (token->value); | |
c162c75e | 606 | |
36952dea | 607 | cpp_handle_deferred_pragma (parse_in, &s); |
c162c75e | 608 | |
36952dea ZW |
609 | /* Clearing token->value here means that we will get an ICE if we |
610 | try to process this #pragma again (which should be impossible). */ | |
611 | token->value = NULL; | |
a723baf1 MM |
612 | } |
613 | ||
614 | /* Begin saving tokens. All tokens consumed after this point will be | |
615 | preserved. */ | |
616 | ||
617 | static void | |
94edc4ab | 618 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
619 | { |
620 | /* Provide debugging output. */ | |
621 | if (cp_lexer_debugging_p (lexer)) | |
622 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
623 | ||
0c5e4866 | 624 | VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token); |
a723baf1 MM |
625 | } |
626 | ||
627 | /* Commit to the portion of the token stream most recently saved. */ | |
628 | ||
629 | static void | |
94edc4ab | 630 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
631 | { |
632 | /* Provide debugging output. */ | |
633 | if (cp_lexer_debugging_p (lexer)) | |
634 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
635 | ||
0c5e4866 | 636 | VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
637 | } |
638 | ||
639 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
640 | to the token stream. Stop saving tokens. */ | |
641 | ||
642 | static void | |
94edc4ab | 643 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 | 644 | { |
a723baf1 MM |
645 | /* Provide debugging output. */ |
646 | if (cp_lexer_debugging_p (lexer)) | |
647 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
648 | ||
0c5e4866 | 649 | lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens); |
a723baf1 MM |
650 | } |
651 | ||
a723baf1 MM |
652 | /* Print a representation of the TOKEN on the STREAM. */ |
653 | ||
6983ea08 MA |
654 | #ifdef ENABLE_CHECKING |
655 | ||
a723baf1 | 656 | static void |
c162c75e MA |
657 | cp_lexer_print_token (FILE * stream, cp_token *token) |
658 | { | |
659 | /* We don't use cpp_type2name here because the parser defines | |
660 | a few tokens of its own. */ | |
661 | static const char *const token_names[] = { | |
662 | /* cpplib-defined token types */ | |
663 | #define OP(e, s) #e, | |
664 | #define TK(e, s) #e, | |
665 | TTYPE_TABLE | |
666 | #undef OP | |
667 | #undef TK | |
668 | /* C++ parser token types - see "Manifest constants", above. */ | |
669 | "KEYWORD", | |
670 | "TEMPLATE_ID", | |
671 | "NESTED_NAME_SPECIFIER", | |
672 | "PURGED" | |
673 | }; | |
674 | ||
675 | /* If we have a name for the token, print it out. Otherwise, we | |
676 | simply give the numeric code. */ | |
677 | gcc_assert (token->type < ARRAY_SIZE(token_names)); | |
678 | fputs (token_names[token->type], stream); | |
a723baf1 | 679 | |
c162c75e | 680 | /* For some tokens, print the associated data. */ |
a723baf1 MM |
681 | switch (token->type) |
682 | { | |
c162c75e MA |
683 | case CPP_KEYWORD: |
684 | /* Some keywords have a value that is not an IDENTIFIER_NODE. | |
685 | For example, `struct' is mapped to an INTEGER_CST. */ | |
686 | if (TREE_CODE (token->value) != IDENTIFIER_NODE) | |
687 | break; | |
688 | /* else fall through */ | |
a723baf1 | 689 | case CPP_NAME: |
c162c75e | 690 | fputs (IDENTIFIER_POINTER (token->value), stream); |
a723baf1 MM |
691 | break; |
692 | ||
c162c75e MA |
693 | case CPP_STRING: |
694 | case CPP_WSTRING: | |
695 | case CPP_PRAGMA: | |
696 | fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value)); | |
a723baf1 MM |
697 | break; |
698 | ||
a723baf1 MM |
699 | default: |
700 | break; | |
701 | } | |
a723baf1 MM |
702 | } |
703 | ||
a723baf1 MM |
704 | /* Start emitting debugging information. */ |
705 | ||
706 | static void | |
94edc4ab | 707 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 | 708 | { |
81122c44 | 709 | lexer->debugging_p = true; |
a723baf1 | 710 | } |
21526606 | 711 | |
a723baf1 MM |
712 | /* Stop emitting debugging information. */ |
713 | ||
714 | static void | |
94edc4ab | 715 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 | 716 | { |
81122c44 | 717 | lexer->debugging_p = false; |
a723baf1 MM |
718 | } |
719 | ||
6983ea08 MA |
720 | #endif /* ENABLE_CHECKING */ |
721 | ||
03fd3f84 | 722 | /* Create a new cp_token_cache, representing a range of tokens. */ |
c162c75e MA |
723 | |
724 | static cp_token_cache * | |
725 | cp_token_cache_new (cp_token *first, cp_token *last) | |
726 | { | |
727 | cp_token_cache *cache = GGC_NEW (cp_token_cache); | |
728 | cache->first = first; | |
729 | cache->last = last; | |
730 | return cache; | |
731 | } | |
732 | ||
a723baf1 | 733 | \f |
62d1db17 MM |
734 | /* Decl-specifiers. */ |
735 | ||
736 | static void clear_decl_specs | |
737 | (cp_decl_specifier_seq *); | |
738 | ||
739 | /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */ | |
740 | ||
741 | static void | |
742 | clear_decl_specs (cp_decl_specifier_seq *decl_specs) | |
743 | { | |
744 | memset (decl_specs, 0, sizeof (cp_decl_specifier_seq)); | |
745 | } | |
746 | ||
058b15c1 MM |
747 | /* Declarators. */ |
748 | ||
749 | /* Nothing other than the parser should be creating declarators; | |
750 | declarators are a semi-syntactic representation of C++ entities. | |
751 | Other parts of the front end that need to create entities (like | |
752 | VAR_DECLs or FUNCTION_DECLs) should do that directly. */ | |
753 | ||
98ca843c | 754 | static cp_declarator *make_call_declarator |
3c01e5df | 755 | (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree); |
98ca843c | 756 | static cp_declarator *make_array_declarator |
058b15c1 | 757 | (cp_declarator *, tree); |
98ca843c | 758 | static cp_declarator *make_pointer_declarator |
3c01e5df | 759 | (cp_cv_quals, cp_declarator *); |
98ca843c | 760 | static cp_declarator *make_reference_declarator |
3c01e5df | 761 | (cp_cv_quals, cp_declarator *); |
98ca843c | 762 | static cp_parameter_declarator *make_parameter_declarator |
62d1db17 | 763 | (cp_decl_specifier_seq *, cp_declarator *, tree); |
98ca843c | 764 | static cp_declarator *make_ptrmem_declarator |
3c01e5df | 765 | (cp_cv_quals, tree, cp_declarator *); |
058b15c1 MM |
766 | |
767 | cp_declarator *cp_error_declarator; | |
768 | ||
769 | /* The obstack on which declarators and related data structures are | |
770 | allocated. */ | |
771 | static struct obstack declarator_obstack; | |
772 | ||
773 | /* Alloc BYTES from the declarator memory pool. */ | |
774 | ||
775 | static inline void * | |
776 | alloc_declarator (size_t bytes) | |
777 | { | |
778 | return obstack_alloc (&declarator_obstack, bytes); | |
779 | } | |
780 | ||
781 | /* Allocate a declarator of the indicated KIND. Clear fields that are | |
782 | common to all declarators. */ | |
783 | ||
784 | static cp_declarator * | |
785 | make_declarator (cp_declarator_kind kind) | |
786 | { | |
787 | cp_declarator *declarator; | |
788 | ||
789 | declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator)); | |
790 | declarator->kind = kind; | |
791 | declarator->attributes = NULL_TREE; | |
792 | declarator->declarator = NULL; | |
793 | ||
794 | return declarator; | |
795 | } | |
796 | ||
1d786913 MM |
797 | /* Make a declarator for a generalized identifier. If non-NULL, the |
798 | identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is | |
799 | just UNQUALIFIED_NAME. */ | |
058b15c1 | 800 | |
1d786913 MM |
801 | static cp_declarator * |
802 | make_id_declarator (tree qualifying_scope, tree unqualified_name) | |
058b15c1 MM |
803 | { |
804 | cp_declarator *declarator; | |
98ca843c | 805 | |
1d786913 MM |
806 | /* It is valid to write: |
807 | ||
808 | class C { void f(); }; | |
809 | typedef C D; | |
810 | void D::f(); | |
811 | ||
812 | The standard is not clear about whether `typedef const C D' is | |
813 | legal; as of 2002-09-15 the committee is considering that | |
814 | question. EDG 3.0 allows that syntax. Therefore, we do as | |
815 | well. */ | |
816 | if (qualifying_scope && TYPE_P (qualifying_scope)) | |
817 | qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope); | |
818 | ||
058b15c1 | 819 | declarator = make_declarator (cdk_id); |
1d786913 MM |
820 | declarator->u.id.qualifying_scope = qualifying_scope; |
821 | declarator->u.id.unqualified_name = unqualified_name; | |
058b15c1 MM |
822 | declarator->u.id.sfk = sfk_none; |
823 | ||
824 | return declarator; | |
825 | } | |
826 | ||
827 | /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list | |
828 | of modifiers such as const or volatile to apply to the pointer | |
829 | type, represented as identifiers. */ | |
830 | ||
831 | cp_declarator * | |
3c01e5df | 832 | make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) |
058b15c1 MM |
833 | { |
834 | cp_declarator *declarator; | |
835 | ||
836 | declarator = make_declarator (cdk_pointer); | |
837 | declarator->declarator = target; | |
838 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
839 | declarator->u.pointer.class_type = NULL_TREE; | |
840 | ||
841 | return declarator; | |
842 | } | |
843 | ||
844 | /* Like make_pointer_declarator -- but for references. */ | |
845 | ||
846 | cp_declarator * | |
3c01e5df | 847 | make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) |
058b15c1 MM |
848 | { |
849 | cp_declarator *declarator; | |
850 | ||
851 | declarator = make_declarator (cdk_reference); | |
852 | declarator->declarator = target; | |
853 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
854 | declarator->u.pointer.class_type = NULL_TREE; | |
855 | ||
856 | return declarator; | |
857 | } | |
858 | ||
859 | /* Like make_pointer_declarator -- but for a pointer to a non-static | |
860 | member of CLASS_TYPE. */ | |
861 | ||
862 | cp_declarator * | |
3c01e5df | 863 | make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type, |
058b15c1 MM |
864 | cp_declarator *pointee) |
865 | { | |
866 | cp_declarator *declarator; | |
867 | ||
868 | declarator = make_declarator (cdk_ptrmem); | |
869 | declarator->declarator = pointee; | |
870 | declarator->u.pointer.qualifiers = cv_qualifiers; | |
871 | declarator->u.pointer.class_type = class_type; | |
872 | ||
873 | return declarator; | |
874 | } | |
875 | ||
876 | /* Make a declarator for the function given by TARGET, with the | |
877 | indicated PARMS. The CV_QUALIFIERS aply to the function, as in | |
878 | "const"-qualified member function. The EXCEPTION_SPECIFICATION | |
879 | indicates what exceptions can be thrown. */ | |
880 | ||
881 | cp_declarator * | |
98ca843c | 882 | make_call_declarator (cp_declarator *target, |
058b15c1 | 883 | cp_parameter_declarator *parms, |
3c01e5df | 884 | cp_cv_quals cv_qualifiers, |
058b15c1 MM |
885 | tree exception_specification) |
886 | { | |
887 | cp_declarator *declarator; | |
888 | ||
889 | declarator = make_declarator (cdk_function); | |
890 | declarator->declarator = target; | |
891 | declarator->u.function.parameters = parms; | |
892 | declarator->u.function.qualifiers = cv_qualifiers; | |
893 | declarator->u.function.exception_specification = exception_specification; | |
894 | ||
895 | return declarator; | |
896 | } | |
897 | ||
898 | /* Make a declarator for an array of BOUNDS elements, each of which is | |
899 | defined by ELEMENT. */ | |
900 | ||
901 | cp_declarator * | |
902 | make_array_declarator (cp_declarator *element, tree bounds) | |
903 | { | |
904 | cp_declarator *declarator; | |
905 | ||
906 | declarator = make_declarator (cdk_array); | |
907 | declarator->declarator = element; | |
908 | declarator->u.array.bounds = bounds; | |
909 | ||
910 | return declarator; | |
911 | } | |
912 | ||
913 | cp_parameter_declarator *no_parameters; | |
914 | ||
915 | /* Create a parameter declarator with the indicated DECL_SPECIFIERS, | |
916 | DECLARATOR and DEFAULT_ARGUMENT. */ | |
917 | ||
918 | cp_parameter_declarator * | |
98ca843c | 919 | make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers, |
058b15c1 MM |
920 | cp_declarator *declarator, |
921 | tree default_argument) | |
922 | { | |
923 | cp_parameter_declarator *parameter; | |
924 | ||
98ca843c | 925 | parameter = ((cp_parameter_declarator *) |
058b15c1 MM |
926 | alloc_declarator (sizeof (cp_parameter_declarator))); |
927 | parameter->next = NULL; | |
62d1db17 MM |
928 | if (decl_specifiers) |
929 | parameter->decl_specifiers = *decl_specifiers; | |
930 | else | |
931 | clear_decl_specs (¶meter->decl_specifiers); | |
058b15c1 MM |
932 | parameter->declarator = declarator; |
933 | parameter->default_argument = default_argument; | |
934 | parameter->ellipsis_p = false; | |
935 | ||
936 | return parameter; | |
937 | } | |
938 | ||
a723baf1 MM |
939 | /* The parser. */ |
940 | ||
941 | /* Overview | |
942 | -------- | |
943 | ||
944 | A cp_parser parses the token stream as specified by the C++ | |
945 | grammar. Its job is purely parsing, not semantic analysis. For | |
946 | example, the parser breaks the token stream into declarators, | |
947 | expressions, statements, and other similar syntactic constructs. | |
948 | It does not check that the types of the expressions on either side | |
949 | of an assignment-statement are compatible, or that a function is | |
950 | not declared with a parameter of type `void'. | |
951 | ||
952 | The parser invokes routines elsewhere in the compiler to perform | |
953 | semantic analysis and to build up the abstract syntax tree for the | |
21526606 | 954 | code processed. |
a723baf1 MM |
955 | |
956 | The parser (and the template instantiation code, which is, in a | |
957 | way, a close relative of parsing) are the only parts of the | |
958 | compiler that should be calling push_scope and pop_scope, or | |
959 | related functions. The parser (and template instantiation code) | |
960 | keeps track of what scope is presently active; everything else | |
961 | should simply honor that. (The code that generates static | |
962 | initializers may also need to set the scope, in order to check | |
963 | access control correctly when emitting the initializers.) | |
964 | ||
965 | Methodology | |
966 | ----------- | |
21526606 | 967 | |
a723baf1 MM |
968 | The parser is of the standard recursive-descent variety. Upcoming |
969 | tokens in the token stream are examined in order to determine which | |
970 | production to use when parsing a non-terminal. Some C++ constructs | |
971 | require arbitrary look ahead to disambiguate. For example, it is | |
972 | impossible, in the general case, to tell whether a statement is an | |
973 | expression or declaration without scanning the entire statement. | |
974 | Therefore, the parser is capable of "parsing tentatively." When the | |
975 | parser is not sure what construct comes next, it enters this mode. | |
976 | Then, while we attempt to parse the construct, the parser queues up | |
977 | error messages, rather than issuing them immediately, and saves the | |
978 | tokens it consumes. If the construct is parsed successfully, the | |
979 | parser "commits", i.e., it issues any queued error messages and | |
980 | the tokens that were being preserved are permanently discarded. | |
981 | If, however, the construct is not parsed successfully, the parser | |
982 | rolls back its state completely so that it can resume parsing using | |
983 | a different alternative. | |
984 | ||
985 | Future Improvements | |
986 | ------------------- | |
21526606 | 987 | |
b8b94c5b PB |
988 | The performance of the parser could probably be improved substantially. |
989 | We could often eliminate the need to parse tentatively by looking ahead | |
990 | a little bit. In some places, this approach might not entirely eliminate | |
991 | the need to parse tentatively, but it might still speed up the average | |
992 | case. */ | |
a723baf1 MM |
993 | |
994 | /* Flags that are passed to some parsing functions. These values can | |
995 | be bitwise-ored together. */ | |
996 | ||
997 | typedef enum cp_parser_flags | |
998 | { | |
999 | /* No flags. */ | |
1000 | CP_PARSER_FLAGS_NONE = 0x0, | |
1001 | /* The construct is optional. If it is not present, then no error | |
1002 | should be issued. */ | |
1003 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1004 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1005 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
1006 | } cp_parser_flags; | |
1007 | ||
62b8a44e NS |
1008 | /* The different kinds of declarators we want to parse. */ |
1009 | ||
1010 | typedef enum cp_parser_declarator_kind | |
1011 | { | |
852dcbdd | 1012 | /* We want an abstract declarator. */ |
62b8a44e NS |
1013 | CP_PARSER_DECLARATOR_ABSTRACT, |
1014 | /* We want a named declarator. */ | |
1015 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1016 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1017 | CP_PARSER_DECLARATOR_EITHER |
1018 | } cp_parser_declarator_kind; | |
1019 | ||
b8b94c5b PB |
1020 | /* The precedence values used to parse binary expressions. The minimum value |
1021 | of PREC must be 1, because zero is reserved to quickly discriminate | |
1022 | binary operators from other tokens. */ | |
a723baf1 | 1023 | |
b8b94c5b | 1024 | enum cp_parser_prec |
a723baf1 | 1025 | { |
b8b94c5b PB |
1026 | PREC_NOT_OPERATOR, |
1027 | PREC_LOGICAL_OR_EXPRESSION, | |
1028 | PREC_LOGICAL_AND_EXPRESSION, | |
1029 | PREC_INCLUSIVE_OR_EXPRESSION, | |
1030 | PREC_EXCLUSIVE_OR_EXPRESSION, | |
1031 | PREC_AND_EXPRESSION, | |
b8b94c5b | 1032 | PREC_EQUALITY_EXPRESSION, |
69475123 | 1033 | PREC_RELATIONAL_EXPRESSION, |
b8b94c5b PB |
1034 | PREC_SHIFT_EXPRESSION, |
1035 | PREC_ADDITIVE_EXPRESSION, | |
1036 | PREC_MULTIPLICATIVE_EXPRESSION, | |
1037 | PREC_PM_EXPRESSION, | |
1038 | NUM_PREC_VALUES = PREC_PM_EXPRESSION | |
1039 | }; | |
a723baf1 | 1040 | |
b8b94c5b PB |
1041 | /* A mapping from a token type to a corresponding tree node type, with a |
1042 | precedence value. */ | |
a723baf1 | 1043 | |
b8b94c5b PB |
1044 | typedef struct cp_parser_binary_operations_map_node |
1045 | { | |
1046 | /* The token type. */ | |
1047 | enum cpp_ttype token_type; | |
1048 | /* The corresponding tree code. */ | |
1049 | enum tree_code tree_type; | |
1050 | /* The precedence of this operator. */ | |
1051 | enum cp_parser_prec prec; | |
1052 | } cp_parser_binary_operations_map_node; | |
a723baf1 MM |
1053 | |
1054 | /* The status of a tentative parse. */ | |
1055 | ||
1056 | typedef enum cp_parser_status_kind | |
1057 | { | |
1058 | /* No errors have occurred. */ | |
1059 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1060 | /* An error has occurred. */ | |
1061 | CP_PARSER_STATUS_KIND_ERROR, | |
1062 | /* We are committed to this tentative parse, whether or not an error | |
1063 | has occurred. */ | |
1064 | CP_PARSER_STATUS_KIND_COMMITTED | |
1065 | } cp_parser_status_kind; | |
1066 | ||
b8b94c5b PB |
1067 | typedef struct cp_parser_expression_stack_entry |
1068 | { | |
1069 | tree lhs; | |
1070 | enum tree_code tree_type; | |
1071 | int prec; | |
1072 | } cp_parser_expression_stack_entry; | |
1073 | ||
43c2a69a PB |
1074 | /* The stack for storing partial expressions. We only need NUM_PREC_VALUES |
1075 | entries because precedence levels on the stack are monotonically | |
1076 | increasing. */ | |
b8b94c5b PB |
1077 | typedef struct cp_parser_expression_stack_entry |
1078 | cp_parser_expression_stack[NUM_PREC_VALUES]; | |
a723baf1 | 1079 | |
b8b94c5b | 1080 | /* Context that is saved and restored when parsing tentatively. */ |
a723baf1 MM |
1081 | typedef struct cp_parser_context GTY (()) |
1082 | { | |
1083 | /* If this is a tentative parsing context, the status of the | |
1084 | tentative parse. */ | |
1085 | enum cp_parser_status_kind status; | |
1086 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1087 | that are looked up in this context must be looked up both in the | |
1088 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1089 | the context of the containing expression. */ | |
1090 | tree object_type; | |
b8b94c5b | 1091 | |
a723baf1 MM |
1092 | /* The next parsing context in the stack. */ |
1093 | struct cp_parser_context *next; | |
1094 | } cp_parser_context; | |
1095 | ||
1096 | /* Prototypes. */ | |
1097 | ||
1098 | /* Constructors and destructors. */ | |
1099 | ||
1100 | static cp_parser_context *cp_parser_context_new | |
94edc4ab | 1101 | (cp_parser_context *); |
a723baf1 | 1102 | |
e5976695 MM |
1103 | /* Class variables. */ |
1104 | ||
1431042e | 1105 | static GTY((deletable)) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1106 | |
b8b94c5b PB |
1107 | /* The operator-precedence table used by cp_parser_binary_expression. |
1108 | Transformed into an associative array (binops_by_token) by | |
1109 | cp_parser_new. */ | |
1110 | ||
1111 | static const cp_parser_binary_operations_map_node binops[] = { | |
1112 | { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION }, | |
1113 | { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION }, | |
1114 | ||
1115 | { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1116 | { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1117 | { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, | |
1118 | ||
1119 | { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1120 | { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION }, | |
1121 | ||
1122 | { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1123 | { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, | |
1124 | ||
1125 | { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1126 | { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1127 | { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1128 | { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1129 | { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1130 | { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION }, | |
1131 | ||
1132 | { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1133 | { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION }, | |
1134 | ||
1135 | { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION }, | |
1136 | ||
1137 | { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION }, | |
1138 | ||
1139 | { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION }, | |
1140 | ||
1141 | { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION }, | |
1142 | ||
1143 | { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION } | |
1144 | }; | |
1145 | ||
1146 | /* The same as binops, but initialized by cp_parser_new so that | |
1147 | binops_by_token[N].token_type == N. Used in cp_parser_binary_expression | |
1148 | for speed. */ | |
1149 | static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES]; | |
1150 | ||
a723baf1 MM |
1151 | /* Constructors and destructors. */ |
1152 | ||
1153 | /* Construct a new context. The context below this one on the stack | |
1154 | is given by NEXT. */ | |
1155 | ||
1156 | static cp_parser_context * | |
94edc4ab | 1157 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1158 | { |
1159 | cp_parser_context *context; | |
1160 | ||
1161 | /* Allocate the storage. */ | |
e5976695 MM |
1162 | if (cp_parser_context_free_list != NULL) |
1163 | { | |
1164 | /* Pull the first entry from the free list. */ | |
1165 | context = cp_parser_context_free_list; | |
1166 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1167 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1168 | } |
1169 | else | |
99dd239f | 1170 | context = GGC_CNEW (cp_parser_context); |
b8b94c5b | 1171 | |
a723baf1 MM |
1172 | /* No errors have occurred yet in this context. */ |
1173 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
1174 | /* If this is not the bottomost context, copy information that we | |
1175 | need from the previous context. */ | |
1176 | if (next) | |
1177 | { | |
1178 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1179 | expression, then we are parsing one in this context, too. */ | |
1180 | context->object_type = next->object_type; | |
a723baf1 MM |
1181 | /* Thread the stack. */ |
1182 | context->next = next; | |
1183 | } | |
1184 | ||
1185 | return context; | |
1186 | } | |
1187 | ||
1188 | /* The cp_parser structure represents the C++ parser. */ | |
1189 | ||
1190 | typedef struct cp_parser GTY(()) | |
1191 | { | |
1192 | /* The lexer from which we are obtaining tokens. */ | |
1193 | cp_lexer *lexer; | |
1194 | ||
1195 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1196 | we look up names in the scope that is currently open in the | |
1197 | source program. If non-NULL, this is either a TYPE or | |
21526606 | 1198 | NAMESPACE_DECL for the scope in which we should look. |
a723baf1 MM |
1199 | |
1200 | This value is not cleared automatically after a name is looked | |
1201 | up, so we must be careful to clear it before starting a new look | |
1202 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1203 | will look up `Z' in the scope of `X', rather than the current | |
1204 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1205 | is complete, because we sometimes peek at a token, look it up, | |
1206 | and then decide not to consume it. */ | |
1207 | tree scope; | |
1208 | ||
1209 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1210 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1211 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
21526606 | 1212 | respectively. QUALIFYING_SCOPE is used for an expression of the |
a723baf1 MM |
1213 | form "X::Y"; it refers to X. */ |
1214 | tree object_scope; | |
1215 | tree qualifying_scope; | |
1216 | ||
1217 | /* A stack of parsing contexts. All but the bottom entry on the | |
1218 | stack will be tentative contexts. | |
1219 | ||
1220 | We parse tentatively in order to determine which construct is in | |
1221 | use in some situations. For example, in order to determine | |
1222 | whether a statement is an expression-statement or a | |
1223 | declaration-statement we parse it tentatively as a | |
1224 | declaration-statement. If that fails, we then reparse the same | |
1225 | token stream as an expression-statement. */ | |
1226 | cp_parser_context *context; | |
1227 | ||
1228 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1229 | GNU extensions are not recognized. */ | |
1230 | bool allow_gnu_extensions_p; | |
1231 | ||
1232 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1233 | operator. FALSE if it is the end of a template-id or | |
1234 | template-parameter-list. */ | |
1235 | bool greater_than_is_operator_p; | |
1236 | ||
1237 | /* TRUE if default arguments are allowed within a parameter list | |
1238 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1239 | them permissible. */ |
a723baf1 | 1240 | bool default_arg_ok_p; |
21526606 | 1241 | |
a723baf1 MM |
1242 | /* TRUE if we are parsing an integral constant-expression. See |
1243 | [expr.const] for a precise definition. */ | |
67c03833 | 1244 | bool integral_constant_expression_p; |
a723baf1 | 1245 | |
14d22dd6 MM |
1246 | /* TRUE if we are parsing an integral constant-expression -- but a |
1247 | non-constant expression should be permitted as well. This flag | |
1248 | is used when parsing an array bound so that GNU variable-length | |
1249 | arrays are tolerated. */ | |
67c03833 | 1250 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1251 | |
1252 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1253 | been seen that makes the expression non-constant. */ | |
67c03833 | 1254 | bool non_integral_constant_expression_p; |
14d22dd6 | 1255 | |
a723baf1 MM |
1256 | /* TRUE if local variable names and `this' are forbidden in the |
1257 | current context. */ | |
1258 | bool local_variables_forbidden_p; | |
1259 | ||
1260 | /* TRUE if the declaration we are parsing is part of a | |
1261 | linkage-specification of the form `extern string-literal | |
1262 | declaration'. */ | |
1263 | bool in_unbraced_linkage_specification_p; | |
1264 | ||
1265 | /* TRUE if we are presently parsing a declarator, after the | |
1266 | direct-declarator. */ | |
1267 | bool in_declarator_p; | |
1268 | ||
4bb8ca28 MM |
1269 | /* TRUE if we are presently parsing a template-argument-list. */ |
1270 | bool in_template_argument_list_p; | |
1271 | ||
0e59b3fb MM |
1272 | /* TRUE if we are presently parsing the body of an |
1273 | iteration-statement. */ | |
1274 | bool in_iteration_statement_p; | |
1275 | ||
1276 | /* TRUE if we are presently parsing the body of a switch | |
1277 | statement. */ | |
1278 | bool in_switch_statement_p; | |
1279 | ||
4f8163b1 MM |
1280 | /* TRUE if we are parsing a type-id in an expression context. In |
1281 | such a situation, both "type (expr)" and "type (type)" are valid | |
1282 | alternatives. */ | |
1283 | bool in_type_id_in_expr_p; | |
1284 | ||
7d381002 | 1285 | /* TRUE if we are currently in a header file where declarations are |
03fd3f84 | 1286 | implicitly extern "C". */ |
7d381002 MA |
1287 | bool implicit_extern_c; |
1288 | ||
c162c75e MA |
1289 | /* TRUE if strings in expressions should be translated to the execution |
1290 | character set. */ | |
1291 | bool translate_strings_p; | |
1292 | ||
a723baf1 MM |
1293 | /* If non-NULL, then we are parsing a construct where new type |
1294 | definitions are not permitted. The string stored here will be | |
1295 | issued as an error message if a type is defined. */ | |
1296 | const char *type_definition_forbidden_message; | |
1297 | ||
8db1028e NS |
1298 | /* A list of lists. The outer list is a stack, used for member |
1299 | functions of local classes. At each level there are two sub-list, | |
1300 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1301 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1302 | TREE_VALUE's. The functions are chained in reverse declaration | |
1303 | order. | |
1304 | ||
1305 | The TREE_PURPOSE sublist contains those functions with default | |
1306 | arguments that need post processing, and the TREE_VALUE sublist | |
1307 | contains those functions with definitions that need post | |
1308 | processing. | |
1309 | ||
1310 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1311 | defined is complete. */ |
a723baf1 MM |
1312 | tree unparsed_functions_queues; |
1313 | ||
1314 | /* The number of classes whose definitions are currently in | |
1315 | progress. */ | |
1316 | unsigned num_classes_being_defined; | |
1317 | ||
1318 | /* The number of template parameter lists that apply directly to the | |
1319 | current declaration. */ | |
1320 | unsigned num_template_parameter_lists; | |
1321 | } cp_parser; | |
1322 | ||
04c06002 | 1323 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1324 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1325 | |
1326 | /* Prototypes. */ | |
1327 | ||
1328 | /* Constructors and destructors. */ | |
1329 | ||
1330 | static cp_parser *cp_parser_new | |
94edc4ab | 1331 | (void); |
a723baf1 | 1332 | |
21526606 | 1333 | /* Routines to parse various constructs. |
a723baf1 MM |
1334 | |
1335 | Those that return `tree' will return the error_mark_node (rather | |
1336 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1337 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1338 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1339 | whether or not a parse error occurred, you should always use |
1340 | cp_parser_error_occurred. If the construct is optional (indicated | |
1341 | either by an `_opt' in the name of the function that does the | |
1342 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1343 | the construct is not present. */ | |
1344 | ||
1345 | /* Lexical conventions [gram.lex] */ | |
1346 | ||
1347 | static tree cp_parser_identifier | |
94edc4ab | 1348 | (cp_parser *); |
c162c75e MA |
1349 | static tree cp_parser_string_literal |
1350 | (cp_parser *, bool, bool); | |
a723baf1 MM |
1351 | |
1352 | /* Basic concepts [gram.basic] */ | |
1353 | ||
1354 | static bool cp_parser_translation_unit | |
94edc4ab | 1355 | (cp_parser *); |
a723baf1 MM |
1356 | |
1357 | /* Expressions [gram.expr] */ | |
1358 | ||
1359 | static tree cp_parser_primary_expression | |
93678513 | 1360 | (cp_parser *, bool, cp_id_kind *, tree *); |
a723baf1 | 1361 | static tree cp_parser_id_expression |
f3c2dfc6 | 1362 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1363 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1364 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1365 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1366 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1367 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1368 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1369 | static tree cp_parser_class_or_namespace_name |
1370 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 | 1371 | static tree cp_parser_postfix_expression |
93678513 | 1372 | (cp_parser *, bool, bool); |
7a3ea201 RH |
1373 | static tree cp_parser_postfix_open_square_expression |
1374 | (cp_parser *, tree, bool); | |
1375 | static tree cp_parser_postfix_dot_deref_expression | |
1376 | (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *); | |
7efa3e22 | 1377 | static tree cp_parser_parenthesized_expression_list |
93678513 | 1378 | (cp_parser *, bool, bool, bool *); |
a723baf1 | 1379 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1380 | (cp_parser *, tree *, tree *); |
a723baf1 | 1381 | static tree cp_parser_unary_expression |
93678513 | 1382 | (cp_parser *, bool, bool); |
a723baf1 | 1383 | static enum tree_code cp_parser_unary_operator |
94edc4ab | 1384 | (cp_token *); |
a723baf1 | 1385 | static tree cp_parser_new_expression |
94edc4ab | 1386 | (cp_parser *); |
a723baf1 | 1387 | static tree cp_parser_new_placement |
94edc4ab | 1388 | (cp_parser *); |
a723baf1 | 1389 | static tree cp_parser_new_type_id |
058b15c1 MM |
1390 | (cp_parser *, tree *); |
1391 | static cp_declarator *cp_parser_new_declarator_opt | |
94edc4ab | 1392 | (cp_parser *); |
058b15c1 | 1393 | static cp_declarator *cp_parser_direct_new_declarator |
94edc4ab | 1394 | (cp_parser *); |
a723baf1 | 1395 | static tree cp_parser_new_initializer |
94edc4ab | 1396 | (cp_parser *); |
a723baf1 | 1397 | static tree cp_parser_delete_expression |
94edc4ab | 1398 | (cp_parser *); |
21526606 | 1399 | static tree cp_parser_cast_expression |
93678513 | 1400 | (cp_parser *, bool, bool); |
b8b94c5b | 1401 | static tree cp_parser_binary_expression |
93678513 | 1402 | (cp_parser *, bool); |
a723baf1 | 1403 | static tree cp_parser_question_colon_clause |
94edc4ab | 1404 | (cp_parser *, tree); |
a723baf1 | 1405 | static tree cp_parser_assignment_expression |
93678513 | 1406 | (cp_parser *, bool); |
a723baf1 | 1407 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1408 | (cp_parser *); |
a723baf1 | 1409 | static tree cp_parser_expression |
93678513 | 1410 | (cp_parser *, bool); |
a723baf1 | 1411 | static tree cp_parser_constant_expression |
14d22dd6 | 1412 | (cp_parser *, bool, bool *); |
7a3ea201 RH |
1413 | static tree cp_parser_builtin_offsetof |
1414 | (cp_parser *); | |
a723baf1 MM |
1415 | |
1416 | /* Statements [gram.stmt.stmt] */ | |
1417 | ||
1418 | static void cp_parser_statement | |
325c3691 | 1419 | (cp_parser *, tree); |
a723baf1 | 1420 | static tree cp_parser_labeled_statement |
325c3691 | 1421 | (cp_parser *, tree); |
a723baf1 | 1422 | static tree cp_parser_expression_statement |
325c3691 | 1423 | (cp_parser *, tree); |
a723baf1 | 1424 | static tree cp_parser_compound_statement |
325c3691 | 1425 | (cp_parser *, tree, bool); |
a723baf1 | 1426 | static void cp_parser_statement_seq_opt |
325c3691 | 1427 | (cp_parser *, tree); |
a723baf1 | 1428 | static tree cp_parser_selection_statement |
94edc4ab | 1429 | (cp_parser *); |
a723baf1 | 1430 | static tree cp_parser_condition |
94edc4ab | 1431 | (cp_parser *); |
a723baf1 | 1432 | static tree cp_parser_iteration_statement |
94edc4ab | 1433 | (cp_parser *); |
a723baf1 | 1434 | static void cp_parser_for_init_statement |
94edc4ab | 1435 | (cp_parser *); |
a723baf1 | 1436 | static tree cp_parser_jump_statement |
94edc4ab | 1437 | (cp_parser *); |
a723baf1 | 1438 | static void cp_parser_declaration_statement |
94edc4ab | 1439 | (cp_parser *); |
a723baf1 MM |
1440 | |
1441 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1442 | (cp_parser *); |
a723baf1 | 1443 | static void cp_parser_already_scoped_statement |
94edc4ab | 1444 | (cp_parser *); |
a723baf1 MM |
1445 | |
1446 | /* Declarations [gram.dcl.dcl] */ | |
1447 | ||
1448 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1449 | (cp_parser *); |
a723baf1 | 1450 | static void cp_parser_declaration |
94edc4ab | 1451 | (cp_parser *); |
a723baf1 | 1452 | static void cp_parser_block_declaration |
94edc4ab | 1453 | (cp_parser *, bool); |
a723baf1 | 1454 | static void cp_parser_simple_declaration |
94edc4ab | 1455 | (cp_parser *, bool); |
62d1db17 MM |
1456 | static void cp_parser_decl_specifier_seq |
1457 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *); | |
a723baf1 | 1458 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1459 | (cp_parser *); |
a723baf1 | 1460 | static tree cp_parser_function_specifier_opt |
62d1db17 | 1461 | (cp_parser *, cp_decl_specifier_seq *); |
a723baf1 | 1462 | static tree cp_parser_type_specifier |
98ca843c | 1463 | (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool, |
62d1db17 | 1464 | int *, bool *); |
a723baf1 | 1465 | static tree cp_parser_simple_type_specifier |
62d1db17 | 1466 | (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags); |
a723baf1 | 1467 | static tree cp_parser_type_name |
94edc4ab | 1468 | (cp_parser *); |
a723baf1 | 1469 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1470 | (cp_parser *, bool, bool); |
a723baf1 | 1471 | static tree cp_parser_enum_specifier |
94edc4ab | 1472 | (cp_parser *); |
a723baf1 | 1473 | static void cp_parser_enumerator_list |
94edc4ab | 1474 | (cp_parser *, tree); |
21526606 | 1475 | static void cp_parser_enumerator_definition |
94edc4ab | 1476 | (cp_parser *, tree); |
a723baf1 | 1477 | static tree cp_parser_namespace_name |
94edc4ab | 1478 | (cp_parser *); |
a723baf1 | 1479 | static void cp_parser_namespace_definition |
94edc4ab | 1480 | (cp_parser *); |
a723baf1 | 1481 | static void cp_parser_namespace_body |
94edc4ab | 1482 | (cp_parser *); |
a723baf1 | 1483 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1484 | (cp_parser *); |
a723baf1 | 1485 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1486 | (cp_parser *); |
a723baf1 | 1487 | static void cp_parser_using_declaration |
94edc4ab | 1488 | (cp_parser *); |
a723baf1 | 1489 | static void cp_parser_using_directive |
94edc4ab | 1490 | (cp_parser *); |
a723baf1 | 1491 | static void cp_parser_asm_definition |
94edc4ab | 1492 | (cp_parser *); |
a723baf1 | 1493 | static void cp_parser_linkage_specification |
94edc4ab | 1494 | (cp_parser *); |
a723baf1 MM |
1495 | |
1496 | /* Declarators [gram.dcl.decl] */ | |
1497 | ||
1498 | static tree cp_parser_init_declarator | |
62d1db17 | 1499 | (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *); |
058b15c1 | 1500 | static cp_declarator *cp_parser_declarator |
db86dd14 | 1501 | (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool); |
058b15c1 | 1502 | static cp_declarator *cp_parser_direct_declarator |
db86dd14 | 1503 | (cp_parser *, cp_parser_declarator_kind, int *, bool); |
a723baf1 | 1504 | static enum tree_code cp_parser_ptr_operator |
3c01e5df MM |
1505 | (cp_parser *, tree *, cp_cv_quals *); |
1506 | static cp_cv_quals cp_parser_cv_qualifier_seq_opt | |
94edc4ab | 1507 | (cp_parser *); |
a723baf1 | 1508 | static tree cp_parser_declarator_id |
94edc4ab | 1509 | (cp_parser *); |
a723baf1 | 1510 | static tree cp_parser_type_id |
94edc4ab | 1511 | (cp_parser *); |
62d1db17 MM |
1512 | static void cp_parser_type_specifier_seq |
1513 | (cp_parser *, cp_decl_specifier_seq *); | |
058b15c1 | 1514 | static cp_parameter_declarator *cp_parser_parameter_declaration_clause |
94edc4ab | 1515 | (cp_parser *); |
058b15c1 MM |
1516 | static cp_parameter_declarator *cp_parser_parameter_declaration_list |
1517 | (cp_parser *, bool *); | |
1518 | static cp_parameter_declarator *cp_parser_parameter_declaration | |
4bb8ca28 | 1519 | (cp_parser *, bool, bool *); |
a723baf1 MM |
1520 | static void cp_parser_function_body |
1521 | (cp_parser *); | |
1522 | static tree cp_parser_initializer | |
39703eb9 | 1523 | (cp_parser *, bool *, bool *); |
a723baf1 | 1524 | static tree cp_parser_initializer_clause |
39703eb9 | 1525 | (cp_parser *, bool *); |
a723baf1 | 1526 | static tree cp_parser_initializer_list |
39703eb9 | 1527 | (cp_parser *, bool *); |
a723baf1 MM |
1528 | |
1529 | static bool cp_parser_ctor_initializer_opt_and_function_body | |
1530 | (cp_parser *); | |
1531 | ||
1532 | /* Classes [gram.class] */ | |
1533 | ||
1534 | static tree cp_parser_class_name | |
fc6a28d7 | 1535 | (cp_parser *, bool, bool, enum tag_types, bool, bool, bool); |
a723baf1 | 1536 | static tree cp_parser_class_specifier |
94edc4ab | 1537 | (cp_parser *); |
a723baf1 | 1538 | static tree cp_parser_class_head |
38b305d0 | 1539 | (cp_parser *, bool *, tree *); |
a723baf1 | 1540 | static enum tag_types cp_parser_class_key |
94edc4ab | 1541 | (cp_parser *); |
a723baf1 | 1542 | static void cp_parser_member_specification_opt |
94edc4ab | 1543 | (cp_parser *); |
a723baf1 | 1544 | static void cp_parser_member_declaration |
94edc4ab | 1545 | (cp_parser *); |
a723baf1 | 1546 | static tree cp_parser_pure_specifier |
94edc4ab | 1547 | (cp_parser *); |
a723baf1 | 1548 | static tree cp_parser_constant_initializer |
94edc4ab | 1549 | (cp_parser *); |
a723baf1 MM |
1550 | |
1551 | /* Derived classes [gram.class.derived] */ | |
1552 | ||
1553 | static tree cp_parser_base_clause | |
94edc4ab | 1554 | (cp_parser *); |
a723baf1 | 1555 | static tree cp_parser_base_specifier |
94edc4ab | 1556 | (cp_parser *); |
a723baf1 MM |
1557 | |
1558 | /* Special member functions [gram.special] */ | |
1559 | ||
1560 | static tree cp_parser_conversion_function_id | |
94edc4ab | 1561 | (cp_parser *); |
a723baf1 | 1562 | static tree cp_parser_conversion_type_id |
94edc4ab | 1563 | (cp_parser *); |
058b15c1 | 1564 | static cp_declarator *cp_parser_conversion_declarator_opt |
94edc4ab | 1565 | (cp_parser *); |
a723baf1 | 1566 | static bool cp_parser_ctor_initializer_opt |
94edc4ab | 1567 | (cp_parser *); |
a723baf1 | 1568 | static void cp_parser_mem_initializer_list |
94edc4ab | 1569 | (cp_parser *); |
a723baf1 | 1570 | static tree cp_parser_mem_initializer |
94edc4ab | 1571 | (cp_parser *); |
a723baf1 | 1572 | static tree cp_parser_mem_initializer_id |
94edc4ab | 1573 | (cp_parser *); |
a723baf1 MM |
1574 | |
1575 | /* Overloading [gram.over] */ | |
1576 | ||
1577 | static tree cp_parser_operator_function_id | |
94edc4ab | 1578 | (cp_parser *); |
a723baf1 | 1579 | static tree cp_parser_operator |
94edc4ab | 1580 | (cp_parser *); |
a723baf1 MM |
1581 | |
1582 | /* Templates [gram.temp] */ | |
1583 | ||
1584 | static void cp_parser_template_declaration | |
94edc4ab | 1585 | (cp_parser *, bool); |
a723baf1 | 1586 | static tree cp_parser_template_parameter_list |
94edc4ab | 1587 | (cp_parser *); |
a723baf1 | 1588 | static tree cp_parser_template_parameter |
058b15c1 | 1589 | (cp_parser *, bool *); |
a723baf1 | 1590 | static tree cp_parser_type_parameter |
94edc4ab | 1591 | (cp_parser *); |
a723baf1 | 1592 | static tree cp_parser_template_id |
a668c6ad | 1593 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1594 | static tree cp_parser_template_name |
a668c6ad | 1595 | (cp_parser *, bool, bool, bool, bool *); |
a723baf1 | 1596 | static tree cp_parser_template_argument_list |
94edc4ab | 1597 | (cp_parser *); |
a723baf1 | 1598 | static tree cp_parser_template_argument |
94edc4ab | 1599 | (cp_parser *); |
a723baf1 | 1600 | static void cp_parser_explicit_instantiation |
94edc4ab | 1601 | (cp_parser *); |
a723baf1 | 1602 | static void cp_parser_explicit_specialization |
94edc4ab | 1603 | (cp_parser *); |
a723baf1 MM |
1604 | |
1605 | /* Exception handling [gram.exception] */ | |
1606 | ||
21526606 | 1607 | static tree cp_parser_try_block |
94edc4ab | 1608 | (cp_parser *); |
a723baf1 | 1609 | static bool cp_parser_function_try_block |
94edc4ab | 1610 | (cp_parser *); |
a723baf1 | 1611 | static void cp_parser_handler_seq |
94edc4ab | 1612 | (cp_parser *); |
a723baf1 | 1613 | static void cp_parser_handler |
94edc4ab | 1614 | (cp_parser *); |
a723baf1 | 1615 | static tree cp_parser_exception_declaration |
94edc4ab | 1616 | (cp_parser *); |
a723baf1 | 1617 | static tree cp_parser_throw_expression |
94edc4ab | 1618 | (cp_parser *); |
a723baf1 | 1619 | static tree cp_parser_exception_specification_opt |
94edc4ab | 1620 | (cp_parser *); |
a723baf1 | 1621 | static tree cp_parser_type_id_list |
94edc4ab | 1622 | (cp_parser *); |
a723baf1 MM |
1623 | |
1624 | /* GNU Extensions */ | |
1625 | ||
1626 | static tree cp_parser_asm_specification_opt | |
94edc4ab | 1627 | (cp_parser *); |
a723baf1 | 1628 | static tree cp_parser_asm_operand_list |
94edc4ab | 1629 | (cp_parser *); |
a723baf1 | 1630 | static tree cp_parser_asm_clobber_list |
94edc4ab | 1631 | (cp_parser *); |
a723baf1 | 1632 | static tree cp_parser_attributes_opt |
94edc4ab | 1633 | (cp_parser *); |
a723baf1 | 1634 | static tree cp_parser_attribute_list |
94edc4ab | 1635 | (cp_parser *); |
a723baf1 | 1636 | static bool cp_parser_extension_opt |
94edc4ab | 1637 | (cp_parser *, int *); |
a723baf1 | 1638 | static void cp_parser_label_declaration |
94edc4ab | 1639 | (cp_parser *); |
a723baf1 MM |
1640 | |
1641 | /* Utility Routines */ | |
1642 | ||
1643 | static tree cp_parser_lookup_name | |
fc6a28d7 | 1644 | (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *); |
a723baf1 | 1645 | static tree cp_parser_lookup_name_simple |
94edc4ab | 1646 | (cp_parser *, tree); |
a723baf1 MM |
1647 | static tree cp_parser_maybe_treat_template_as_class |
1648 | (tree, bool); | |
1649 | static bool cp_parser_check_declarator_template_parameters | |
058b15c1 | 1650 | (cp_parser *, cp_declarator *); |
a723baf1 | 1651 | static bool cp_parser_check_template_parameters |
94edc4ab | 1652 | (cp_parser *, unsigned); |
d6b4ea85 MM |
1653 | static tree cp_parser_simple_cast_expression |
1654 | (cp_parser *); | |
a723baf1 | 1655 | static tree cp_parser_global_scope_opt |
94edc4ab | 1656 | (cp_parser *, bool); |
a723baf1 MM |
1657 | static bool cp_parser_constructor_declarator_p |
1658 | (cp_parser *, bool); | |
1659 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
62d1db17 | 1660 | (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *); |
a723baf1 | 1661 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1662 | (cp_parser *, bool); |
a723baf1 | 1663 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1664 | (cp_parser *, bool); |
a723baf1 | 1665 | static tree cp_parser_single_declaration |
94edc4ab | 1666 | (cp_parser *, bool, bool *); |
a723baf1 | 1667 | static tree cp_parser_functional_cast |
94edc4ab | 1668 | (cp_parser *, tree); |
4bb8ca28 | 1669 | static tree cp_parser_save_member_function_body |
62d1db17 | 1670 | (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree); |
ec75414f MM |
1671 | static tree cp_parser_enclosed_template_argument_list |
1672 | (cp_parser *); | |
8db1028e NS |
1673 | static void cp_parser_save_default_args |
1674 | (cp_parser *, tree); | |
a723baf1 | 1675 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1676 | (cp_parser *, tree); |
a723baf1 | 1677 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1678 | (cp_parser *, tree); |
a723baf1 | 1679 | static tree cp_parser_sizeof_operand |
94edc4ab | 1680 | (cp_parser *, enum rid); |
a723baf1 | 1681 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1682 | (cp_parser *); |
62d1db17 MM |
1683 | static void cp_parser_set_storage_class |
1684 | (cp_decl_specifier_seq *, cp_storage_class); | |
98ca843c | 1685 | static void cp_parser_set_decl_spec_type |
62d1db17 | 1686 | (cp_decl_specifier_seq *, tree, bool); |
a723baf1 | 1687 | static bool cp_parser_friend_p |
62d1db17 | 1688 | (const cp_decl_specifier_seq *); |
a723baf1 | 1689 | static cp_token *cp_parser_require |
94edc4ab | 1690 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1691 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1692 | (cp_parser *, enum rid, const char *); |
21526606 | 1693 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1694 | (cp_token *); |
a723baf1 MM |
1695 | static bool cp_parser_next_token_starts_class_definition_p |
1696 | (cp_parser *); | |
d17811fd MM |
1697 | static bool cp_parser_next_token_ends_template_argument_p |
1698 | (cp_parser *); | |
f4abade9 GB |
1699 | static bool cp_parser_nth_token_starts_template_argument_list_p |
1700 | (cp_parser *, size_t); | |
a723baf1 | 1701 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1702 | (cp_token *); |
a723baf1 MM |
1703 | static void cp_parser_check_class_key |
1704 | (enum tag_types, tree type); | |
37d407a1 KL |
1705 | static void cp_parser_check_access_in_redeclaration |
1706 | (tree type); | |
a723baf1 MM |
1707 | static bool cp_parser_optional_template_keyword |
1708 | (cp_parser *); | |
21526606 | 1709 | static void cp_parser_pre_parsed_nested_name_specifier |
2050a1bb | 1710 | (cp_parser *); |
a723baf1 | 1711 | static void cp_parser_cache_group |
c162c75e | 1712 | (cp_parser *, enum cpp_ttype, unsigned); |
21526606 | 1713 | static void cp_parser_parse_tentatively |
94edc4ab | 1714 | (cp_parser *); |
a723baf1 | 1715 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1716 | (cp_parser *); |
a723baf1 | 1717 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1718 | (cp_parser *); |
a723baf1 | 1719 | static bool cp_parser_parse_definitely |
94edc4ab | 1720 | (cp_parser *); |
f7b5ecd9 | 1721 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1722 | (cp_parser *); |
0b16f8f4 | 1723 | static bool cp_parser_uncommitted_to_tentative_parse_p |
94edc4ab | 1724 | (cp_parser *); |
a723baf1 | 1725 | static void cp_parser_error |
94edc4ab | 1726 | (cp_parser *, const char *); |
4bb8ca28 MM |
1727 | static void cp_parser_name_lookup_error |
1728 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1729 | static bool cp_parser_simulate_error |
94edc4ab | 1730 | (cp_parser *); |
a723baf1 | 1731 | static void cp_parser_check_type_definition |
94edc4ab | 1732 | (cp_parser *); |
560ad596 | 1733 | static void cp_parser_check_for_definition_in_return_type |
fc6a28d7 | 1734 | (cp_declarator *, tree); |
ee43dab5 MM |
1735 | static void cp_parser_check_for_invalid_template_id |
1736 | (cp_parser *, tree); | |
625cbf93 MM |
1737 | static bool cp_parser_non_integral_constant_expression |
1738 | (cp_parser *, const char *); | |
2097b5f2 GB |
1739 | static void cp_parser_diagnose_invalid_type_name |
1740 | (cp_parser *, tree, tree); | |
1741 | static bool cp_parser_parse_and_diagnose_invalid_type_name | |
8fbc5ae7 | 1742 | (cp_parser *); |
7efa3e22 | 1743 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1744 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1745 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1746 | (cp_parser *); |
e0860732 MM |
1747 | static void cp_parser_consume_semicolon_at_end_of_statement |
1748 | (cp_parser *); | |
a723baf1 | 1749 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1750 | (cp_parser *); |
a723baf1 MM |
1751 | static void cp_parser_skip_to_closing_brace |
1752 | (cp_parser *); | |
1753 | static void cp_parser_skip_until_found | |
94edc4ab | 1754 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1755 | static bool cp_parser_error_occurred |
94edc4ab | 1756 | (cp_parser *); |
a723baf1 | 1757 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1758 | (cp_parser *); |
a723baf1 | 1759 | static bool cp_parser_is_string_literal |
94edc4ab | 1760 | (cp_token *); |
21526606 | 1761 | static bool cp_parser_is_keyword |
94edc4ab | 1762 | (cp_token *, enum rid); |
2097b5f2 GB |
1763 | static tree cp_parser_make_typename_type |
1764 | (cp_parser *, tree, tree); | |
a723baf1 | 1765 | |
4de8668e | 1766 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1767 | |
1768 | static inline bool | |
94edc4ab | 1769 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1770 | { |
1771 | return parser->context->next != NULL; | |
1772 | } | |
1773 | ||
4de8668e | 1774 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1775 | |
1776 | static bool | |
94edc4ab | 1777 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1778 | { |
1779 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1780 | } | |
1781 | ||
4de8668e | 1782 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1783 | |
1784 | static bool | |
94edc4ab | 1785 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1786 | { |
1787 | return token->keyword == keyword; | |
1788 | } | |
1789 | ||
2cfe82fe ZW |
1790 | /* If not parsing tentatively, issue a diagnostic of the form |
1791 | FILE:LINE: MESSAGE before TOKEN | |
1792 | where TOKEN is the next token in the input stream. MESSAGE | |
1793 | (specified by the caller) is usually of the form "expected | |
1794 | OTHER-TOKEN". */ | |
a723baf1 MM |
1795 | |
1796 | static void | |
94edc4ab | 1797 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1798 | { |
e5976695 | 1799 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 | 1800 | { |
2cfe82fe ZW |
1801 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
1802 | /* This diagnostic makes more sense if it is tagged to the line | |
1803 | of the token we just peeked at. */ | |
1804 | cp_lexer_set_source_position_from_token (token); | |
0d63048c MM |
1805 | if (token->type == CPP_PRAGMA) |
1806 | { | |
1807 | error ("%<#pragma%> is not allowed here"); | |
1808 | cp_lexer_purge_token (parser->lexer); | |
1809 | return; | |
1810 | } | |
21526606 | 1811 | c_parse_error (message, |
5c832178 MM |
1812 | /* Because c_parser_error does not understand |
1813 | CPP_KEYWORD, keywords are treated like | |
1814 | identifiers. */ | |
21526606 | 1815 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), |
5c832178 | 1816 | token->value); |
4bb8ca28 MM |
1817 | } |
1818 | } | |
1819 | ||
1820 | /* Issue an error about name-lookup failing. NAME is the | |
1821 | IDENTIFIER_NODE DECL is the result of | |
1822 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1823 | the thing that we hoped to find. */ | |
1824 | ||
1825 | static void | |
1826 | cp_parser_name_lookup_error (cp_parser* parser, | |
1827 | tree name, | |
1828 | tree decl, | |
1829 | const char* desired) | |
1830 | { | |
1831 | /* If name lookup completely failed, tell the user that NAME was not | |
1832 | declared. */ | |
1833 | if (decl == error_mark_node) | |
1834 | { | |
1835 | if (parser->scope && parser->scope != global_namespace) | |
2a13a625 | 1836 | error ("%<%D::%D%> has not been declared", |
4bb8ca28 MM |
1837 | parser->scope, name); |
1838 | else if (parser->scope == global_namespace) | |
2a13a625 | 1839 | error ("%<::%D%> has not been declared", name); |
b14454ba MM |
1840 | else if (parser->object_scope |
1841 | && !CLASS_TYPE_P (parser->object_scope)) | |
2a13a625 | 1842 | error ("request for member %qD in non-class type %qT", |
b14454ba MM |
1843 | name, parser->object_scope); |
1844 | else if (parser->object_scope) | |
2a13a625 | 1845 | error ("%<%T::%D%> has not been declared", |
b14454ba | 1846 | parser->object_scope, name); |
4bb8ca28 | 1847 | else |
9e637a26 | 1848 | error ("%qD has not been declared", name); |
4bb8ca28 MM |
1849 | } |
1850 | else if (parser->scope && parser->scope != global_namespace) | |
2a13a625 | 1851 | error ("%<%D::%D%> %s", parser->scope, name, desired); |
4bb8ca28 | 1852 | else if (parser->scope == global_namespace) |
2a13a625 | 1853 | error ("%<::%D%> %s", name, desired); |
4bb8ca28 | 1854 | else |
2a13a625 | 1855 | error ("%qD %s", name, desired); |
a723baf1 MM |
1856 | } |
1857 | ||
1858 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 | 1859 | during this tentative parse. Returns true if the error was |
77077b39 | 1860 | simulated; false if a message should be issued by the caller. */ |
a723baf1 | 1861 | |
e5976695 | 1862 | static bool |
94edc4ab | 1863 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 | 1864 | { |
0b16f8f4 | 1865 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
e5976695 MM |
1866 | { |
1867 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1868 | return true; | |
1869 | } | |
1870 | return false; | |
a723baf1 MM |
1871 | } |
1872 | ||
1873 | /* This function is called when a type is defined. If type | |
1874 | definitions are forbidden at this point, an error message is | |
1875 | issued. */ | |
1876 | ||
1877 | static void | |
94edc4ab | 1878 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1879 | { |
1880 | /* If types are forbidden here, issue a message. */ | |
1881 | if (parser->type_definition_forbidden_message) | |
1882 | /* Use `%s' to print the string in case there are any escape | |
1883 | characters in the message. */ | |
1884 | error ("%s", parser->type_definition_forbidden_message); | |
1885 | } | |
1886 | ||
fc6a28d7 | 1887 | /* This function is called when the DECLARATOR is processed. The TYPE |
472c29c3 | 1888 | was a type defined in the decl-specifiers. If it is invalid to |
fc6a28d7 MM |
1889 | define a type in the decl-specifiers for DECLARATOR, an error is |
1890 | issued. */ | |
560ad596 MM |
1891 | |
1892 | static void | |
058b15c1 | 1893 | cp_parser_check_for_definition_in_return_type (cp_declarator *declarator, |
fc6a28d7 | 1894 | tree type) |
560ad596 MM |
1895 | { |
1896 | /* [dcl.fct] forbids type definitions in return types. | |
1897 | Unfortunately, it's not easy to know whether or not we are | |
1898 | processing a return type until after the fact. */ | |
1899 | while (declarator | |
058b15c1 MM |
1900 | && (declarator->kind == cdk_pointer |
1901 | || declarator->kind == cdk_reference | |
1902 | || declarator->kind == cdk_ptrmem)) | |
1903 | declarator = declarator->declarator; | |
560ad596 | 1904 | if (declarator |
fc6a28d7 MM |
1905 | && declarator->kind == cdk_function) |
1906 | { | |
1907 | error ("new types may not be defined in a return type"); | |
1908 | inform ("(perhaps a semicolon is missing after the definition of %qT)", | |
1909 | type); | |
1910 | } | |
560ad596 MM |
1911 | } |
1912 | ||
ee43dab5 MM |
1913 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1914 | "<" in any valid C++ program. If the next token is indeed "<", | |
1915 | issue a message warning the user about what appears to be an | |
1916 | invalid attempt to form a template-id. */ | |
1917 | ||
1918 | static void | |
21526606 | 1919 | cp_parser_check_for_invalid_template_id (cp_parser* parser, |
ee43dab5 MM |
1920 | tree type) |
1921 | { | |
0c5e4866 | 1922 | cp_token_position start = 0; |
ee43dab5 MM |
1923 | |
1924 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1925 | { | |
1926 | if (TYPE_P (type)) | |
2a13a625 | 1927 | error ("%qT is not a template", type); |
ee43dab5 | 1928 | else if (TREE_CODE (type) == IDENTIFIER_NODE) |
2a13a625 | 1929 | error ("%qE is not a template", type); |
ee43dab5 MM |
1930 | else |
1931 | error ("invalid template-id"); | |
1932 | /* Remember the location of the invalid "<". */ | |
0b16f8f4 | 1933 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 1934 | start = cp_lexer_token_position (parser->lexer, true); |
ee43dab5 MM |
1935 | /* Consume the "<". */ |
1936 | cp_lexer_consume_token (parser->lexer); | |
1937 | /* Parse the template arguments. */ | |
1938 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 1939 | /* Permanently remove the invalid template arguments so that |
ee43dab5 | 1940 | this error message is not issued again. */ |
0c5e4866 NS |
1941 | if (start) |
1942 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
ee43dab5 MM |
1943 | } |
1944 | } | |
1945 | ||
625cbf93 MM |
1946 | /* If parsing an integral constant-expression, issue an error message |
1947 | about the fact that THING appeared and return true. Otherwise, | |
93678513 MM |
1948 | return false. In either case, set |
1949 | PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */ | |
14d22dd6 | 1950 | |
625cbf93 MM |
1951 | static bool |
1952 | cp_parser_non_integral_constant_expression (cp_parser *parser, | |
1953 | const char *thing) | |
14d22dd6 | 1954 | { |
93678513 | 1955 | parser->non_integral_constant_expression_p = true; |
625cbf93 MM |
1956 | if (parser->integral_constant_expression_p) |
1957 | { | |
1958 | if (!parser->allow_non_integral_constant_expression_p) | |
1959 | { | |
1960 | error ("%s cannot appear in a constant-expression", thing); | |
1961 | return true; | |
1962 | } | |
625cbf93 MM |
1963 | } |
1964 | return false; | |
14d22dd6 MM |
1965 | } |
1966 | ||
0c88d886 | 1967 | /* Emit a diagnostic for an invalid type name. SCOPE is the |
6ca2d67f MM |
1968 | qualifying scope (or NULL, if none) for ID. This function commits |
1969 | to the current active tentative parse, if any. (Otherwise, the | |
1970 | problematic construct might be encountered again later, resulting | |
1971 | in duplicate error messages.) */ | |
8fbc5ae7 | 1972 | |
2097b5f2 GB |
1973 | static void |
1974 | cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
1975 | { |
1976 | tree decl, old_scope; | |
2097b5f2 GB |
1977 | /* Try to lookup the identifier. */ |
1978 | old_scope = parser->scope; | |
1979 | parser->scope = scope; | |
1980 | decl = cp_parser_lookup_name_simple (parser, id); | |
1981 | parser->scope = old_scope; | |
1982 | /* If the lookup found a template-name, it means that the user forgot | |
1983 | to specify an argument list. Emit an useful error message. */ | |
1984 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
2a13a625 | 1985 | error ("invalid use of template-name %qE without an argument list", |
6c0cc713 | 1986 | decl); |
2097b5f2 | 1987 | else if (!parser->scope) |
8fbc5ae7 | 1988 | { |
8fbc5ae7 | 1989 | /* Issue an error message. */ |
2a13a625 | 1990 | error ("%qE does not name a type", id); |
8fbc5ae7 MM |
1991 | /* If we're in a template class, it's possible that the user was |
1992 | referring to a type from a base class. For example: | |
1993 | ||
1994 | template <typename T> struct A { typedef T X; }; | |
1995 | template <typename T> struct B : public A<T> { X x; }; | |
1996 | ||
1997 | The user should have said "typename A<T>::X". */ | |
32db39c0 PC |
1998 | if (processing_template_decl && current_class_type |
1999 | && TYPE_BINFO (current_class_type)) | |
8fbc5ae7 MM |
2000 | { |
2001 | tree b; | |
2002 | ||
2003 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
2004 | b; | |
2005 | b = TREE_CHAIN (b)) | |
2006 | { | |
2007 | tree base_type = BINFO_TYPE (b); | |
21526606 | 2008 | if (CLASS_TYPE_P (base_type) |
1fb3244a | 2009 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
2010 | { |
2011 | tree field; | |
2012 | /* Go from a particular instantiation of the | |
2013 | template (which will have an empty TYPE_FIELDs), | |
2014 | to the main version. */ | |
353b4fc0 | 2015 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
2016 | for (field = TYPE_FIELDS (base_type); |
2017 | field; | |
2018 | field = TREE_CHAIN (field)) | |
2019 | if (TREE_CODE (field) == TYPE_DECL | |
2097b5f2 | 2020 | && DECL_NAME (field) == id) |
8fbc5ae7 | 2021 | { |
c4f73174 | 2022 | inform ("(perhaps %<typename %T::%E%> was intended)", |
2097b5f2 | 2023 | BINFO_TYPE (b), id); |
8fbc5ae7 MM |
2024 | break; |
2025 | } | |
2026 | if (field) | |
2027 | break; | |
2028 | } | |
2029 | } | |
2030 | } | |
8fbc5ae7 | 2031 | } |
2097b5f2 GB |
2032 | /* Here we diagnose qualified-ids where the scope is actually correct, |
2033 | but the identifier does not resolve to a valid type name. */ | |
21526606 | 2034 | else |
2097b5f2 GB |
2035 | { |
2036 | if (TREE_CODE (parser->scope) == NAMESPACE_DECL) | |
2a13a625 | 2037 | error ("%qE in namespace %qE does not name a type", |
2097b5f2 GB |
2038 | id, parser->scope); |
2039 | else if (TYPE_P (parser->scope)) | |
2fbe4889 | 2040 | error ("%qE in class %qT does not name a type", id, parser->scope); |
2097b5f2 | 2041 | else |
315fb5db | 2042 | gcc_unreachable (); |
2097b5f2 | 2043 | } |
6ca2d67f | 2044 | cp_parser_commit_to_tentative_parse (parser); |
2097b5f2 | 2045 | } |
8fbc5ae7 | 2046 | |
2097b5f2 GB |
2047 | /* Check for a common situation where a type-name should be present, |
2048 | but is not, and issue a sensible error message. Returns true if an | |
2049 | invalid type-name was detected. | |
21526606 | 2050 | |
2097b5f2 | 2051 | The situation handled by this function are variable declarations of the |
21526606 EC |
2052 | form `ID a', where `ID' is an id-expression and `a' is a plain identifier. |
2053 | Usually, `ID' should name a type, but if we got here it means that it | |
2097b5f2 GB |
2054 | does not. We try to emit the best possible error message depending on |
2055 | how exactly the id-expression looks like. | |
2056 | */ | |
2057 | ||
2058 | static bool | |
2059 | cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser) | |
2060 | { | |
2061 | tree id; | |
2062 | ||
2063 | cp_parser_parse_tentatively (parser); | |
21526606 | 2064 | id = cp_parser_id_expression (parser, |
2097b5f2 GB |
2065 | /*template_keyword_p=*/false, |
2066 | /*check_dependency_p=*/true, | |
2067 | /*template_p=*/NULL, | |
2068 | /*declarator_p=*/true); | |
2069 | /* After the id-expression, there should be a plain identifier, | |
2070 | otherwise this is not a simple variable declaration. Also, if | |
2071 | the scope is dependent, we cannot do much. */ | |
2072 | if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 2073 | || (parser->scope && TYPE_P (parser->scope) |
2097b5f2 GB |
2074 | && dependent_type_p (parser->scope))) |
2075 | { | |
2076 | cp_parser_abort_tentative_parse (parser); | |
2077 | return false; | |
2078 | } | |
3590f0a6 MM |
2079 | if (!cp_parser_parse_definitely (parser) |
2080 | || TREE_CODE (id) != IDENTIFIER_NODE) | |
2097b5f2 GB |
2081 | return false; |
2082 | ||
2097b5f2 GB |
2083 | /* Emit a diagnostic for the invalid type. */ |
2084 | cp_parser_diagnose_invalid_type_name (parser, parser->scope, id); | |
2085 | /* Skip to the end of the declaration; there's no point in | |
2086 | trying to process it. */ | |
2087 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
2088 | return true; | |
8fbc5ae7 MM |
2089 | } |
2090 | ||
21526606 | 2091 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2092 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2093 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2094 | found an unnested comma. */ | |
a723baf1 | 2095 | |
7efa3e22 NS |
2096 | static int |
2097 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
21526606 | 2098 | bool recovering, |
a668c6ad MM |
2099 | bool or_comma, |
2100 | bool consume_paren) | |
a723baf1 | 2101 | { |
7efa3e22 NS |
2102 | unsigned paren_depth = 0; |
2103 | unsigned brace_depth = 0; | |
0173bb6f | 2104 | int result; |
a723baf1 | 2105 | |
0b16f8f4 VR |
2106 | if (recovering && !or_comma |
2107 | && cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
7efa3e22 | 2108 | return 0; |
21526606 | 2109 | |
a723baf1 MM |
2110 | while (true) |
2111 | { | |
2112 | cp_token *token; | |
21526606 | 2113 | |
a723baf1 MM |
2114 | /* If we've run out of tokens, then there is no closing `)'. */ |
2115 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
0173bb6f AO |
2116 | { |
2117 | result = 0; | |
2118 | break; | |
2119 | } | |
a723baf1 | 2120 | |
a668c6ad | 2121 | token = cp_lexer_peek_token (parser->lexer); |
21526606 | 2122 | |
f4f206f4 | 2123 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad | 2124 | if (token->type == CPP_SEMICOLON && !brace_depth) |
0173bb6f AO |
2125 | { |
2126 | result = 0; | |
2127 | break; | |
2128 | } | |
a668c6ad MM |
2129 | if (token->type == CPP_OPEN_BRACE) |
2130 | ++brace_depth; | |
2131 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2132 | { |
a668c6ad | 2133 | if (!brace_depth--) |
0173bb6f AO |
2134 | { |
2135 | result = 0; | |
2136 | break; | |
2137 | } | |
7efa3e22 | 2138 | } |
a668c6ad MM |
2139 | if (recovering && or_comma && token->type == CPP_COMMA |
2140 | && !brace_depth && !paren_depth) | |
0173bb6f AO |
2141 | { |
2142 | result = -1; | |
2143 | break; | |
2144 | } | |
21526606 | 2145 | |
7efa3e22 NS |
2146 | if (!brace_depth) |
2147 | { | |
2148 | /* If it is an `(', we have entered another level of nesting. */ | |
2149 | if (token->type == CPP_OPEN_PAREN) | |
2150 | ++paren_depth; | |
2151 | /* If it is a `)', then we might be done. */ | |
2152 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2153 | { |
2154 | if (consume_paren) | |
2155 | cp_lexer_consume_token (parser->lexer); | |
0173bb6f AO |
2156 | { |
2157 | result = 1; | |
2158 | break; | |
2159 | } | |
a668c6ad | 2160 | } |
7efa3e22 | 2161 | } |
21526606 | 2162 | |
a668c6ad MM |
2163 | /* Consume the token. */ |
2164 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 2165 | } |
0173bb6f | 2166 | |
0173bb6f | 2167 | return result; |
a723baf1 MM |
2168 | } |
2169 | ||
2170 | /* Consume tokens until we reach the end of the current statement. | |
2171 | Normally, that will be just before consuming a `;'. However, if a | |
2172 | non-nested `}' comes first, then we stop before consuming that. */ | |
2173 | ||
2174 | static void | |
94edc4ab | 2175 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2176 | { |
2177 | unsigned nesting_depth = 0; | |
2178 | ||
2179 | while (true) | |
2180 | { | |
2181 | cp_token *token; | |
2182 | ||
2183 | /* Peek at the next token. */ | |
2184 | token = cp_lexer_peek_token (parser->lexer); | |
2185 | /* If we've run out of tokens, stop. */ | |
2186 | if (token->type == CPP_EOF) | |
2187 | break; | |
2188 | /* If the next token is a `;', we have reached the end of the | |
2189 | statement. */ | |
2190 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2191 | break; | |
2192 | /* If the next token is a non-nested `}', then we have reached | |
2193 | the end of the current block. */ | |
2194 | if (token->type == CPP_CLOSE_BRACE) | |
2195 | { | |
2196 | /* If this is a non-nested `}', stop before consuming it. | |
2197 | That way, when confronted with something like: | |
2198 | ||
21526606 | 2199 | { 3 + } |
a723baf1 MM |
2200 | |
2201 | we stop before consuming the closing `}', even though we | |
2202 | have not yet reached a `;'. */ | |
2203 | if (nesting_depth == 0) | |
2204 | break; | |
2205 | /* If it is the closing `}' for a block that we have | |
2206 | scanned, stop -- but only after consuming the token. | |
2207 | That way given: | |
2208 | ||
2209 | void f g () { ... } | |
2210 | typedef int I; | |
2211 | ||
2212 | we will stop after the body of the erroneously declared | |
2213 | function, but before consuming the following `typedef' | |
2214 | declaration. */ | |
2215 | if (--nesting_depth == 0) | |
2216 | { | |
2217 | cp_lexer_consume_token (parser->lexer); | |
2218 | break; | |
2219 | } | |
2220 | } | |
2221 | /* If it the next token is a `{', then we are entering a new | |
2222 | block. Consume the entire block. */ | |
2223 | else if (token->type == CPP_OPEN_BRACE) | |
2224 | ++nesting_depth; | |
2225 | /* Consume the token. */ | |
2226 | cp_lexer_consume_token (parser->lexer); | |
2227 | } | |
2228 | } | |
2229 | ||
e0860732 MM |
2230 | /* This function is called at the end of a statement or declaration. |
2231 | If the next token is a semicolon, it is consumed; otherwise, error | |
2232 | recovery is attempted. */ | |
2233 | ||
2234 | static void | |
2235 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2236 | { | |
2237 | /* Look for the trailing `;'. */ | |
2238 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2239 | { | |
2240 | /* If there is additional (erroneous) input, skip to the end of | |
2241 | the statement. */ | |
2242 | cp_parser_skip_to_end_of_statement (parser); | |
2243 | /* If the next token is now a `;', consume it. */ | |
2244 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2245 | cp_lexer_consume_token (parser->lexer); | |
2246 | } | |
2247 | } | |
2248 | ||
a723baf1 MM |
2249 | /* Skip tokens until we have consumed an entire block, or until we |
2250 | have consumed a non-nested `;'. */ | |
2251 | ||
2252 | static void | |
94edc4ab | 2253 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2254 | { |
2255 | unsigned nesting_depth = 0; | |
2256 | ||
2257 | while (true) | |
2258 | { | |
2259 | cp_token *token; | |
2260 | ||
2261 | /* Peek at the next token. */ | |
2262 | token = cp_lexer_peek_token (parser->lexer); | |
2263 | /* If we've run out of tokens, stop. */ | |
2264 | if (token->type == CPP_EOF) | |
2265 | break; | |
2266 | /* If the next token is a `;', we have reached the end of the | |
2267 | statement. */ | |
2268 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2269 | { | |
2270 | /* Consume the `;'. */ | |
2271 | cp_lexer_consume_token (parser->lexer); | |
2272 | break; | |
2273 | } | |
2274 | /* Consume the token. */ | |
2275 | token = cp_lexer_consume_token (parser->lexer); | |
2276 | /* If the next token is a non-nested `}', then we have reached | |
2277 | the end of the current block. */ | |
21526606 | 2278 | if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
2279 | && (nesting_depth == 0 || --nesting_depth == 0)) |
2280 | break; | |
2281 | /* If it the next token is a `{', then we are entering a new | |
2282 | block. Consume the entire block. */ | |
2283 | if (token->type == CPP_OPEN_BRACE) | |
2284 | ++nesting_depth; | |
2285 | } | |
2286 | } | |
2287 | ||
2288 | /* Skip tokens until a non-nested closing curly brace is the next | |
2289 | token. */ | |
2290 | ||
2291 | static void | |
2292 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2293 | { | |
2294 | unsigned nesting_depth = 0; | |
2295 | ||
2296 | while (true) | |
2297 | { | |
2298 | cp_token *token; | |
2299 | ||
2300 | /* Peek at the next token. */ | |
2301 | token = cp_lexer_peek_token (parser->lexer); | |
2302 | /* If we've run out of tokens, stop. */ | |
2303 | if (token->type == CPP_EOF) | |
2304 | break; | |
2305 | /* If the next token is a non-nested `}', then we have reached | |
2306 | the end of the current block. */ | |
2307 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2308 | break; | |
2309 | /* If it the next token is a `{', then we are entering a new | |
2310 | block. Consume the entire block. */ | |
2311 | else if (token->type == CPP_OPEN_BRACE) | |
2312 | ++nesting_depth; | |
2313 | /* Consume the token. */ | |
2314 | cp_lexer_consume_token (parser->lexer); | |
2315 | } | |
2316 | } | |
2317 | ||
2097b5f2 GB |
2318 | /* This is a simple wrapper around make_typename_type. When the id is |
2319 | an unresolved identifier node, we can provide a superior diagnostic | |
2320 | using cp_parser_diagnose_invalid_type_name. */ | |
2321 | ||
2322 | static tree | |
2323 | cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
2324 | { |
2325 | tree result; | |
2326 | if (TREE_CODE (id) == IDENTIFIER_NODE) | |
2327 | { | |
fc6a28d7 MM |
2328 | result = make_typename_type (scope, id, typename_type, |
2329 | /*complain=*/0); | |
6c0cc713 GB |
2330 | if (result == error_mark_node) |
2331 | cp_parser_diagnose_invalid_type_name (parser, scope, id); | |
2332 | return result; | |
2333 | } | |
fc6a28d7 | 2334 | return make_typename_type (scope, id, typename_type, tf_error); |
2097b5f2 GB |
2335 | } |
2336 | ||
2337 | ||
a723baf1 MM |
2338 | /* Create a new C++ parser. */ |
2339 | ||
2340 | static cp_parser * | |
94edc4ab | 2341 | cp_parser_new (void) |
a723baf1 MM |
2342 | { |
2343 | cp_parser *parser; | |
17211ab5 | 2344 | cp_lexer *lexer; |
b8b94c5b | 2345 | unsigned i; |
17211ab5 GK |
2346 | |
2347 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2348 | cp_lexer_new_main might load a PCH file. */ | |
2349 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2350 | |
b8b94c5b PB |
2351 | /* Initialize the binops_by_token so that we can get the tree |
2352 | directly from the token. */ | |
2353 | for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++) | |
2354 | binops_by_token[binops[i].token_type] = binops[i]; | |
2355 | ||
99dd239f | 2356 | parser = GGC_CNEW (cp_parser); |
17211ab5 | 2357 | parser->lexer = lexer; |
a723baf1 MM |
2358 | parser->context = cp_parser_context_new (NULL); |
2359 | ||
2360 | /* For now, we always accept GNU extensions. */ | |
2361 | parser->allow_gnu_extensions_p = 1; | |
2362 | ||
2363 | /* The `>' token is a greater-than operator, not the end of a | |
2364 | template-id. */ | |
2365 | parser->greater_than_is_operator_p = true; | |
2366 | ||
2367 | parser->default_arg_ok_p = true; | |
21526606 | 2368 | |
a723baf1 | 2369 | /* We are not parsing a constant-expression. */ |
67c03833 JM |
2370 | parser->integral_constant_expression_p = false; |
2371 | parser->allow_non_integral_constant_expression_p = false; | |
2372 | parser->non_integral_constant_expression_p = false; | |
a723baf1 MM |
2373 | |
2374 | /* Local variable names are not forbidden. */ | |
2375 | parser->local_variables_forbidden_p = false; | |
2376 | ||
34cd5ae7 | 2377 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2378 | parser->in_unbraced_linkage_specification_p = false; |
2379 | ||
2380 | /* We are not processing a declarator. */ | |
2381 | parser->in_declarator_p = false; | |
2382 | ||
4bb8ca28 MM |
2383 | /* We are not processing a template-argument-list. */ |
2384 | parser->in_template_argument_list_p = false; | |
2385 | ||
0e59b3fb MM |
2386 | /* We are not in an iteration statement. */ |
2387 | parser->in_iteration_statement_p = false; | |
2388 | ||
2389 | /* We are not in a switch statement. */ | |
2390 | parser->in_switch_statement_p = false; | |
2391 | ||
4f8163b1 MM |
2392 | /* We are not parsing a type-id inside an expression. */ |
2393 | parser->in_type_id_in_expr_p = false; | |
2394 | ||
03fd3f84 | 2395 | /* Declarations aren't implicitly extern "C". */ |
7d381002 MA |
2396 | parser->implicit_extern_c = false; |
2397 | ||
c162c75e MA |
2398 | /* String literals should be translated to the execution character set. */ |
2399 | parser->translate_strings_p = true; | |
2400 | ||
a723baf1 MM |
2401 | /* The unparsed function queue is empty. */ |
2402 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2403 | ||
2404 | /* There are no classes being defined. */ | |
2405 | parser->num_classes_being_defined = 0; | |
2406 | ||
2407 | /* No template parameters apply. */ | |
2408 | parser->num_template_parameter_lists = 0; | |
2409 | ||
2410 | return parser; | |
2411 | } | |
2412 | ||
2cfe82fe ZW |
2413 | /* Create a cp_lexer structure which will emit the tokens in CACHE |
2414 | and push it onto the parser's lexer stack. This is used for delayed | |
2415 | parsing of in-class method bodies and default arguments, and should | |
2416 | not be confused with tentative parsing. */ | |
2417 | static void | |
2418 | cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache) | |
2419 | { | |
2420 | cp_lexer *lexer = cp_lexer_new_from_tokens (cache); | |
2421 | lexer->next = parser->lexer; | |
2422 | parser->lexer = lexer; | |
2423 | ||
2424 | /* Move the current source position to that of the first token in the | |
2425 | new lexer. */ | |
2426 | cp_lexer_set_source_position_from_token (lexer->next_token); | |
2427 | } | |
2428 | ||
2429 | /* Pop the top lexer off the parser stack. This is never used for the | |
2430 | "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */ | |
2431 | static void | |
2432 | cp_parser_pop_lexer (cp_parser *parser) | |
2433 | { | |
2434 | cp_lexer *lexer = parser->lexer; | |
2435 | parser->lexer = lexer->next; | |
2436 | cp_lexer_destroy (lexer); | |
2437 | ||
2438 | /* Put the current source position back where it was before this | |
2439 | lexer was pushed. */ | |
2440 | cp_lexer_set_source_position_from_token (parser->lexer->next_token); | |
2441 | } | |
2442 | ||
a723baf1 MM |
2443 | /* Lexical conventions [gram.lex] */ |
2444 | ||
2445 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2446 | identifier. */ | |
2447 | ||
21526606 | 2448 | static tree |
94edc4ab | 2449 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2450 | { |
2451 | cp_token *token; | |
2452 | ||
2453 | /* Look for the identifier. */ | |
2454 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2455 | /* Return the value. */ | |
2456 | return token ? token->value : error_mark_node; | |
2457 | } | |
2458 | ||
c162c75e MA |
2459 | /* Parse a sequence of adjacent string constants. Returns a |
2460 | TREE_STRING representing the combined, nul-terminated string | |
2461 | constant. If TRANSLATE is true, translate the string to the | |
2462 | execution character set. If WIDE_OK is true, a wide string is | |
2463 | invalid here. | |
2464 | ||
2465 | C++98 [lex.string] says that if a narrow string literal token is | |
2466 | adjacent to a wide string literal token, the behavior is undefined. | |
2467 | However, C99 6.4.5p4 says that this results in a wide string literal. | |
2468 | We follow C99 here, for consistency with the C front end. | |
2469 | ||
2470 | This code is largely lifted from lex_string() in c-lex.c. | |
2471 | ||
2472 | FUTURE: ObjC++ will need to handle @-strings here. */ | |
2473 | static tree | |
2474 | cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok) | |
2475 | { | |
2476 | tree value; | |
2477 | bool wide = false; | |
2478 | size_t count; | |
2479 | struct obstack str_ob; | |
2480 | cpp_string str, istr, *strs; | |
2481 | cp_token *tok; | |
2482 | ||
2483 | tok = cp_lexer_peek_token (parser->lexer); | |
2484 | if (!cp_parser_is_string_literal (tok)) | |
2485 | { | |
2486 | cp_parser_error (parser, "expected string-literal"); | |
2487 | return error_mark_node; | |
2488 | } | |
2489 | ||
9688c3b8 | 2490 | /* Try to avoid the overhead of creating and destroying an obstack |
c162c75e | 2491 | for the common case of just one string. */ |
2cfe82fe ZW |
2492 | if (!cp_parser_is_string_literal |
2493 | (cp_lexer_peek_nth_token (parser->lexer, 2))) | |
c162c75e | 2494 | { |
2cfe82fe ZW |
2495 | cp_lexer_consume_token (parser->lexer); |
2496 | ||
c162c75e MA |
2497 | str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value); |
2498 | str.len = TREE_STRING_LENGTH (tok->value); | |
2499 | count = 1; | |
2500 | if (tok->type == CPP_WSTRING) | |
2501 | wide = true; | |
c162c75e MA |
2502 | |
2503 | strs = &str; | |
2504 | } | |
2505 | else | |
2506 | { | |
2507 | gcc_obstack_init (&str_ob); | |
2508 | count = 0; | |
2509 | ||
2510 | do | |
2511 | { | |
2cfe82fe | 2512 | cp_lexer_consume_token (parser->lexer); |
c162c75e MA |
2513 | count++; |
2514 | str.text = (unsigned char *)TREE_STRING_POINTER (tok->value); | |
2515 | str.len = TREE_STRING_LENGTH (tok->value); | |
2516 | if (tok->type == CPP_WSTRING) | |
2517 | wide = true; | |
2518 | ||
2519 | obstack_grow (&str_ob, &str, sizeof (cpp_string)); | |
2520 | ||
2cfe82fe | 2521 | tok = cp_lexer_peek_token (parser->lexer); |
c162c75e MA |
2522 | } |
2523 | while (cp_parser_is_string_literal (tok)); | |
2524 | ||
2525 | strs = (cpp_string *) obstack_finish (&str_ob); | |
2526 | } | |
2527 | ||
2528 | if (wide && !wide_ok) | |
2529 | { | |
2530 | cp_parser_error (parser, "a wide string is invalid in this context"); | |
2531 | wide = false; | |
2532 | } | |
2533 | ||
2534 | if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate) | |
2535 | (parse_in, strs, count, &istr, wide)) | |
2536 | { | |
2537 | value = build_string (istr.len, (char *)istr.text); | |
2538 | free ((void *)istr.text); | |
2539 | ||
2540 | TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node; | |
2541 | value = fix_string_type (value); | |
2542 | } | |
2543 | else | |
2544 | /* cpp_interpret_string has issued an error. */ | |
2545 | value = error_mark_node; | |
2546 | ||
2547 | if (count > 1) | |
2548 | obstack_free (&str_ob, 0); | |
2549 | ||
2550 | return value; | |
2551 | } | |
2552 | ||
2553 | ||
a723baf1 MM |
2554 | /* Basic concepts [gram.basic] */ |
2555 | ||
2556 | /* Parse a translation-unit. | |
2557 | ||
2558 | translation-unit: | |
21526606 | 2559 | declaration-seq [opt] |
a723baf1 MM |
2560 | |
2561 | Returns TRUE if all went well. */ | |
2562 | ||
2563 | static bool | |
94edc4ab | 2564 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 | 2565 | { |
058b15c1 MM |
2566 | /* The address of the first non-permanent object on the declarator |
2567 | obstack. */ | |
2568 | static void *declarator_obstack_base; | |
2569 | ||
2570 | bool success; | |
98ca843c | 2571 | |
058b15c1 MM |
2572 | /* Create the declarator obstack, if necessary. */ |
2573 | if (!cp_error_declarator) | |
2574 | { | |
2575 | gcc_obstack_init (&declarator_obstack); | |
2576 | /* Create the error declarator. */ | |
2577 | cp_error_declarator = make_declarator (cdk_error); | |
2578 | /* Create the empty parameter list. */ | |
62d1db17 | 2579 | no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE); |
058b15c1 MM |
2580 | /* Remember where the base of the declarator obstack lies. */ |
2581 | declarator_obstack_base = obstack_next_free (&declarator_obstack); | |
2582 | } | |
2583 | ||
a723baf1 MM |
2584 | while (true) |
2585 | { | |
2586 | cp_parser_declaration_seq_opt (parser); | |
2587 | ||
2588 | /* If there are no tokens left then all went well. */ | |
2589 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
058b15c1 | 2590 | { |
03fd3f84 | 2591 | /* Get rid of the token array; we don't need it any more. */ |
c162c75e MA |
2592 | cp_lexer_destroy (parser->lexer); |
2593 | parser->lexer = NULL; | |
2594 | ||
7d381002 MA |
2595 | /* This file might have been a context that's implicitly extern |
2596 | "C". If so, pop the lang context. (Only relevant for PCH.) */ | |
2597 | if (parser->implicit_extern_c) | |
2598 | { | |
2599 | pop_lang_context (); | |
2600 | parser->implicit_extern_c = false; | |
2601 | } | |
2602 | ||
058b15c1 MM |
2603 | /* Finish up. */ |
2604 | finish_translation_unit (); | |
21526606 | 2605 | |
058b15c1 MM |
2606 | success = true; |
2607 | break; | |
2608 | } | |
2609 | else | |
2610 | { | |
2611 | cp_parser_error (parser, "expected declaration"); | |
2612 | success = false; | |
2613 | break; | |
2614 | } | |
a723baf1 MM |
2615 | } |
2616 | ||
058b15c1 | 2617 | /* Make sure the declarator obstack was fully cleaned up. */ |
50bc768d NS |
2618 | gcc_assert (obstack_next_free (&declarator_obstack) |
2619 | == declarator_obstack_base); | |
a723baf1 MM |
2620 | |
2621 | /* All went well. */ | |
058b15c1 | 2622 | return success; |
a723baf1 MM |
2623 | } |
2624 | ||
2625 | /* Expressions [gram.expr] */ | |
2626 | ||
2627 | /* Parse a primary-expression. | |
2628 | ||
2629 | primary-expression: | |
2630 | literal | |
2631 | this | |
2632 | ( expression ) | |
2633 | id-expression | |
2634 | ||
2635 | GNU Extensions: | |
2636 | ||
2637 | primary-expression: | |
2638 | ( compound-statement ) | |
2639 | __builtin_va_arg ( assignment-expression , type-id ) | |
2640 | ||
2641 | literal: | |
2642 | __null | |
2643 | ||
93678513 MM |
2644 | CAST_P is true if this primary expression is the target of a cast. |
2645 | ||
21526606 | 2646 | Returns a representation of the expression. |
a723baf1 | 2647 | |
21526606 | 2648 | *IDK indicates what kind of id-expression (if any) was present. |
a723baf1 MM |
2649 | |
2650 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2651 | used as the operand of a pointer-to-member. In that case, | |
2652 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2653 | class in the pointer-to-member. */ | |
2654 | ||
2655 | static tree | |
21526606 | 2656 | cp_parser_primary_expression (cp_parser *parser, |
93678513 | 2657 | bool cast_p, |
b3445994 | 2658 | cp_id_kind *idk, |
a723baf1 MM |
2659 | tree *qualifying_class) |
2660 | { | |
2661 | cp_token *token; | |
2662 | ||
2663 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2664 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2665 | /* And that it cannot be used as pointer-to-member. */ |
2666 | *qualifying_class = NULL_TREE; | |
2667 | ||
2668 | /* Peek at the next token. */ | |
2669 | token = cp_lexer_peek_token (parser->lexer); | |
2670 | switch (token->type) | |
2671 | { | |
2672 | /* literal: | |
2673 | integer-literal | |
2674 | character-literal | |
2675 | floating-literal | |
2676 | string-literal | |
2677 | boolean-literal */ | |
2678 | case CPP_CHAR: | |
2679 | case CPP_WCHAR: | |
a723baf1 MM |
2680 | case CPP_NUMBER: |
2681 | token = cp_lexer_consume_token (parser->lexer); | |
93678513 MM |
2682 | /* Floating-point literals are only allowed in an integral |
2683 | constant expression if they are cast to an integral or | |
2684 | enumeration type. */ | |
2685 | if (TREE_CODE (token->value) == REAL_CST | |
8c94c75a MM |
2686 | && parser->integral_constant_expression_p |
2687 | && pedantic) | |
93678513 MM |
2688 | { |
2689 | /* CAST_P will be set even in invalid code like "int(2.7 + | |
2690 | ...)". Therefore, we have to check that the next token | |
2691 | is sure to end the cast. */ | |
2692 | if (cast_p) | |
2693 | { | |
2694 | cp_token *next_token; | |
2695 | ||
2696 | next_token = cp_lexer_peek_token (parser->lexer); | |
2697 | if (/* The comma at the end of an | |
2698 | enumerator-definition. */ | |
2699 | next_token->type != CPP_COMMA | |
2700 | /* The curly brace at the end of an enum-specifier. */ | |
2701 | && next_token->type != CPP_CLOSE_BRACE | |
2702 | /* The end of a statement. */ | |
2703 | && next_token->type != CPP_SEMICOLON | |
2704 | /* The end of the cast-expression. */ | |
2705 | && next_token->type != CPP_CLOSE_PAREN | |
2706 | /* The end of an array bound. */ | |
2707 | && next_token->type != CPP_CLOSE_SQUARE) | |
2708 | cast_p = false; | |
2709 | } | |
2710 | ||
2711 | /* If we are within a cast, then the constraint that the | |
2712 | cast is to an integral or enumeration type will be | |
2713 | checked at that point. If we are not within a cast, then | |
2714 | this code is invalid. */ | |
2715 | if (!cast_p) | |
2716 | cp_parser_non_integral_constant_expression | |
2717 | (parser, "floating-point literal"); | |
2718 | } | |
a723baf1 MM |
2719 | return token->value; |
2720 | ||
0173bb6f AO |
2721 | case CPP_STRING: |
2722 | case CPP_WSTRING: | |
c162c75e MA |
2723 | /* ??? Should wide strings be allowed when parser->translate_strings_p |
2724 | is false (i.e. in attributes)? If not, we can kill the third | |
2725 | argument to cp_parser_string_literal. */ | |
2726 | return cp_parser_string_literal (parser, | |
2727 | parser->translate_strings_p, | |
2728 | true); | |
0173bb6f | 2729 | |
a723baf1 MM |
2730 | case CPP_OPEN_PAREN: |
2731 | { | |
2732 | tree expr; | |
2733 | bool saved_greater_than_is_operator_p; | |
2734 | ||
2735 | /* Consume the `('. */ | |
2736 | cp_lexer_consume_token (parser->lexer); | |
2737 | /* Within a parenthesized expression, a `>' token is always | |
2738 | the greater-than operator. */ | |
21526606 | 2739 | saved_greater_than_is_operator_p |
a723baf1 MM |
2740 | = parser->greater_than_is_operator_p; |
2741 | parser->greater_than_is_operator_p = true; | |
2742 | /* If we see `( { ' then we are looking at the beginning of | |
2743 | a GNU statement-expression. */ | |
2744 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2745 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2746 | { | |
2747 | /* Statement-expressions are not allowed by the standard. */ | |
2748 | if (pedantic) | |
21526606 EC |
2749 | pedwarn ("ISO C++ forbids braced-groups within expressions"); |
2750 | ||
a723baf1 MM |
2751 | /* And they're not allowed outside of a function-body; you |
2752 | cannot, for example, write: | |
21526606 | 2753 | |
a723baf1 | 2754 | int i = ({ int j = 3; j + 1; }); |
21526606 | 2755 | |
a723baf1 MM |
2756 | at class or namespace scope. */ |
2757 | if (!at_function_scope_p ()) | |
2758 | error ("statement-expressions are allowed only inside functions"); | |
2759 | /* Start the statement-expression. */ | |
2760 | expr = begin_stmt_expr (); | |
2761 | /* Parse the compound-statement. */ | |
325c3691 | 2762 | cp_parser_compound_statement (parser, expr, false); |
a723baf1 | 2763 | /* Finish up. */ |
303b7406 | 2764 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2765 | } |
2766 | else | |
2767 | { | |
2768 | /* Parse the parenthesized expression. */ | |
93678513 | 2769 | expr = cp_parser_expression (parser, cast_p); |
a723baf1 MM |
2770 | /* Let the front end know that this expression was |
2771 | enclosed in parentheses. This matters in case, for | |
2772 | example, the expression is of the form `A::B', since | |
2773 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2774 | not. */ | |
2775 | finish_parenthesized_expr (expr); | |
2776 | } | |
2777 | /* The `>' token might be the end of a template-id or | |
2778 | template-parameter-list now. */ | |
21526606 | 2779 | parser->greater_than_is_operator_p |
a723baf1 MM |
2780 | = saved_greater_than_is_operator_p; |
2781 | /* Consume the `)'. */ | |
2782 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2783 | cp_parser_skip_to_end_of_statement (parser); | |
2784 | ||
2785 | return expr; | |
2786 | } | |
2787 | ||
2788 | case CPP_KEYWORD: | |
2789 | switch (token->keyword) | |
2790 | { | |
2791 | /* These two are the boolean literals. */ | |
2792 | case RID_TRUE: | |
2793 | cp_lexer_consume_token (parser->lexer); | |
2794 | return boolean_true_node; | |
2795 | case RID_FALSE: | |
2796 | cp_lexer_consume_token (parser->lexer); | |
2797 | return boolean_false_node; | |
21526606 | 2798 | |
a723baf1 MM |
2799 | /* The `__null' literal. */ |
2800 | case RID_NULL: | |
2801 | cp_lexer_consume_token (parser->lexer); | |
2802 | return null_node; | |
2803 | ||
2804 | /* Recognize the `this' keyword. */ | |
2805 | case RID_THIS: | |
2806 | cp_lexer_consume_token (parser->lexer); | |
2807 | if (parser->local_variables_forbidden_p) | |
2808 | { | |
2a13a625 | 2809 | error ("%<this%> may not be used in this context"); |
a723baf1 MM |
2810 | return error_mark_node; |
2811 | } | |
14d22dd6 | 2812 | /* Pointers cannot appear in constant-expressions. */ |
625cbf93 MM |
2813 | if (cp_parser_non_integral_constant_expression (parser, |
2814 | "`this'")) | |
2815 | return error_mark_node; | |
a723baf1 MM |
2816 | return finish_this_expr (); |
2817 | ||
2818 | /* The `operator' keyword can be the beginning of an | |
2819 | id-expression. */ | |
2820 | case RID_OPERATOR: | |
2821 | goto id_expression; | |
2822 | ||
2823 | case RID_FUNCTION_NAME: | |
2824 | case RID_PRETTY_FUNCTION_NAME: | |
2825 | case RID_C99_FUNCTION_NAME: | |
2826 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2827 | __func__ are the names of variables -- but they are | |
2828 | treated specially. Therefore, they are handled here, | |
2829 | rather than relying on the generic id-expression logic | |
21526606 | 2830 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2831 | |
2832 | Consume the token. */ | |
2833 | token = cp_lexer_consume_token (parser->lexer); | |
2834 | /* Look up the name. */ | |
2835 | return finish_fname (token->value); | |
2836 | ||
2837 | case RID_VA_ARG: | |
2838 | { | |
2839 | tree expression; | |
2840 | tree type; | |
2841 | ||
2842 | /* The `__builtin_va_arg' construct is used to handle | |
2843 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2844 | cp_lexer_consume_token (parser->lexer); | |
2845 | /* Look for the opening `('. */ | |
2846 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2847 | /* Now, parse the assignment-expression. */ | |
93678513 MM |
2848 | expression = cp_parser_assignment_expression (parser, |
2849 | /*cast_p=*/false); | |
a723baf1 MM |
2850 | /* Look for the `,'. */ |
2851 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2852 | /* Parse the type-id. */ | |
2853 | type = cp_parser_type_id (parser); | |
2854 | /* Look for the closing `)'. */ | |
2855 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2856 | /* Using `va_arg' in a constant-expression is not |
2857 | allowed. */ | |
625cbf93 MM |
2858 | if (cp_parser_non_integral_constant_expression (parser, |
2859 | "`va_arg'")) | |
2860 | return error_mark_node; | |
a723baf1 MM |
2861 | return build_x_va_arg (expression, type); |
2862 | } | |
2863 | ||
263ee052 | 2864 | case RID_OFFSETOF: |
7a3ea201 | 2865 | return cp_parser_builtin_offsetof (parser); |
263ee052 | 2866 | |
a723baf1 MM |
2867 | default: |
2868 | cp_parser_error (parser, "expected primary-expression"); | |
2869 | return error_mark_node; | |
2870 | } | |
a723baf1 MM |
2871 | |
2872 | /* An id-expression can start with either an identifier, a | |
2873 | `::' as the beginning of a qualified-id, or the "operator" | |
2874 | keyword. */ | |
2875 | case CPP_NAME: | |
2876 | case CPP_SCOPE: | |
2877 | case CPP_TEMPLATE_ID: | |
2878 | case CPP_NESTED_NAME_SPECIFIER: | |
2879 | { | |
2880 | tree id_expression; | |
2881 | tree decl; | |
b3445994 | 2882 | const char *error_msg; |
a723baf1 MM |
2883 | |
2884 | id_expression: | |
2885 | /* Parse the id-expression. */ | |
21526606 EC |
2886 | id_expression |
2887 | = cp_parser_id_expression (parser, | |
a723baf1 MM |
2888 | /*template_keyword_p=*/false, |
2889 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2890 | /*template_p=*/NULL, |
2891 | /*declarator_p=*/false); | |
a723baf1 MM |
2892 | if (id_expression == error_mark_node) |
2893 | return error_mark_node; | |
2894 | /* If we have a template-id, then no further lookup is | |
2895 | required. If the template-id was for a template-class, we | |
2896 | will sometimes have a TYPE_DECL at this point. */ | |
2897 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2898 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2899 | decl = id_expression; | |
2900 | /* Look up the name. */ | |
21526606 | 2901 | else |
a723baf1 | 2902 | { |
8f78f01f MM |
2903 | bool ambiguous_p; |
2904 | ||
2905 | decl = cp_parser_lookup_name (parser, id_expression, | |
fc6a28d7 | 2906 | none_type, |
8f78f01f MM |
2907 | /*is_template=*/false, |
2908 | /*is_namespace=*/false, | |
2909 | /*check_dependency=*/true, | |
2910 | &ambiguous_p); | |
2911 | /* If the lookup was ambiguous, an error will already have | |
2912 | been issued. */ | |
2913 | if (ambiguous_p) | |
2914 | return error_mark_node; | |
a723baf1 MM |
2915 | /* If name lookup gives us a SCOPE_REF, then the |
2916 | qualifying scope was dependent. Just propagate the | |
2917 | name. */ | |
2918 | if (TREE_CODE (decl) == SCOPE_REF) | |
2919 | { | |
2920 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2921 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2922 | return decl; | |
2923 | } | |
2924 | /* Check to see if DECL is a local variable in a context | |
2925 | where that is forbidden. */ | |
2926 | if (parser->local_variables_forbidden_p | |
2927 | && local_variable_p (decl)) | |
2928 | { | |
2929 | /* It might be that we only found DECL because we are | |
2930 | trying to be generous with pre-ISO scoping rules. | |
2931 | For example, consider: | |
2932 | ||
2933 | int i; | |
2934 | void g() { | |
2935 | for (int i = 0; i < 10; ++i) {} | |
2936 | extern void f(int j = i); | |
2937 | } | |
2938 | ||
21526606 | 2939 | Here, name look up will originally find the out |
a723baf1 MM |
2940 | of scope `i'. We need to issue a warning message, |
2941 | but then use the global `i'. */ | |
2942 | decl = check_for_out_of_scope_variable (decl); | |
2943 | if (local_variable_p (decl)) | |
2944 | { | |
2a13a625 | 2945 | error ("local variable %qD may not appear in this context", |
a723baf1 MM |
2946 | decl); |
2947 | return error_mark_node; | |
2948 | } | |
2949 | } | |
c006d942 | 2950 | } |
21526606 EC |
2951 | |
2952 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
b3445994 | 2953 | idk, qualifying_class, |
67c03833 JM |
2954 | parser->integral_constant_expression_p, |
2955 | parser->allow_non_integral_constant_expression_p, | |
2956 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
2957 | &error_msg); |
2958 | if (error_msg) | |
2959 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2960 | return decl; |
2961 | } | |
2962 | ||
2963 | /* Anything else is an error. */ | |
2964 | default: | |
2965 | cp_parser_error (parser, "expected primary-expression"); | |
2966 | return error_mark_node; | |
2967 | } | |
2968 | } | |
2969 | ||
2970 | /* Parse an id-expression. | |
2971 | ||
2972 | id-expression: | |
2973 | unqualified-id | |
2974 | qualified-id | |
2975 | ||
2976 | qualified-id: | |
2977 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2978 | :: identifier | |
2979 | :: operator-function-id | |
2980 | :: template-id | |
2981 | ||
2982 | Return a representation of the unqualified portion of the | |
2983 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2984 | a `::' or nested-name-specifier. | |
2985 | ||
2986 | Often, if the id-expression was a qualified-id, the caller will | |
2987 | want to make a SCOPE_REF to represent the qualified-id. This | |
2988 | function does not do this in order to avoid wastefully creating | |
2989 | SCOPE_REFs when they are not required. | |
2990 | ||
a723baf1 MM |
2991 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2992 | `template' keyword. | |
2993 | ||
2994 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
21526606 | 2995 | uninstantiated templates. |
a723baf1 | 2996 | |
15d2cb19 | 2997 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2998 | `template' keyword is used to explicitly indicate that the entity |
21526606 | 2999 | named is a template. |
f3c2dfc6 MM |
3000 | |
3001 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 3002 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
3003 | |
3004 | static tree | |
3005 | cp_parser_id_expression (cp_parser *parser, | |
3006 | bool template_keyword_p, | |
3007 | bool check_dependency_p, | |
f3c2dfc6 MM |
3008 | bool *template_p, |
3009 | bool declarator_p) | |
a723baf1 MM |
3010 | { |
3011 | bool global_scope_p; | |
3012 | bool nested_name_specifier_p; | |
3013 | ||
3014 | /* Assume the `template' keyword was not used. */ | |
3015 | if (template_p) | |
3016 | *template_p = false; | |
3017 | ||
3018 | /* Look for the optional `::' operator. */ | |
21526606 EC |
3019 | global_scope_p |
3020 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
a723baf1 MM |
3021 | != NULL_TREE); |
3022 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 3023 | nested_name_specifier_p |
a723baf1 MM |
3024 | = (cp_parser_nested_name_specifier_opt (parser, |
3025 | /*typename_keyword_p=*/false, | |
3026 | check_dependency_p, | |
a668c6ad | 3027 | /*type_p=*/false, |
a52eb3bc | 3028 | declarator_p) |
a723baf1 MM |
3029 | != NULL_TREE); |
3030 | /* If there is a nested-name-specifier, then we are looking at | |
3031 | the first qualified-id production. */ | |
3032 | if (nested_name_specifier_p) | |
3033 | { | |
3034 | tree saved_scope; | |
3035 | tree saved_object_scope; | |
3036 | tree saved_qualifying_scope; | |
3037 | tree unqualified_id; | |
3038 | bool is_template; | |
3039 | ||
3040 | /* See if the next token is the `template' keyword. */ | |
3041 | if (!template_p) | |
3042 | template_p = &is_template; | |
3043 | *template_p = cp_parser_optional_template_keyword (parser); | |
3044 | /* Name lookup we do during the processing of the | |
3045 | unqualified-id might obliterate SCOPE. */ | |
3046 | saved_scope = parser->scope; | |
3047 | saved_object_scope = parser->object_scope; | |
3048 | saved_qualifying_scope = parser->qualifying_scope; | |
3049 | /* Process the final unqualified-id. */ | |
3050 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
3051 | check_dependency_p, |
3052 | declarator_p); | |
a723baf1 MM |
3053 | /* Restore the SAVED_SCOPE for our caller. */ |
3054 | parser->scope = saved_scope; | |
3055 | parser->object_scope = saved_object_scope; | |
3056 | parser->qualifying_scope = saved_qualifying_scope; | |
3057 | ||
3058 | return unqualified_id; | |
3059 | } | |
3060 | /* Otherwise, if we are in global scope, then we are looking at one | |
3061 | of the other qualified-id productions. */ | |
3062 | else if (global_scope_p) | |
3063 | { | |
3064 | cp_token *token; | |
3065 | tree id; | |
3066 | ||
e5976695 MM |
3067 | /* Peek at the next token. */ |
3068 | token = cp_lexer_peek_token (parser->lexer); | |
3069 | ||
3070 | /* If it's an identifier, and the next token is not a "<", then | |
3071 | we can avoid the template-id case. This is an optimization | |
3072 | for this common case. */ | |
21526606 EC |
3073 | if (token->type == CPP_NAME |
3074 | && !cp_parser_nth_token_starts_template_argument_list_p | |
f4abade9 | 3075 | (parser, 2)) |
e5976695 MM |
3076 | return cp_parser_identifier (parser); |
3077 | ||
a723baf1 MM |
3078 | cp_parser_parse_tentatively (parser); |
3079 | /* Try a template-id. */ | |
21526606 | 3080 | id = cp_parser_template_id (parser, |
a723baf1 | 3081 | /*template_keyword_p=*/false, |
a668c6ad MM |
3082 | /*check_dependency_p=*/true, |
3083 | declarator_p); | |
a723baf1 MM |
3084 | /* If that worked, we're done. */ |
3085 | if (cp_parser_parse_definitely (parser)) | |
3086 | return id; | |
3087 | ||
e5976695 MM |
3088 | /* Peek at the next token. (Changes in the token buffer may |
3089 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
3090 | token = cp_lexer_peek_token (parser->lexer); |
3091 | ||
3092 | switch (token->type) | |
3093 | { | |
3094 | case CPP_NAME: | |
3095 | return cp_parser_identifier (parser); | |
3096 | ||
3097 | case CPP_KEYWORD: | |
3098 | if (token->keyword == RID_OPERATOR) | |
3099 | return cp_parser_operator_function_id (parser); | |
3100 | /* Fall through. */ | |
21526606 | 3101 | |
a723baf1 MM |
3102 | default: |
3103 | cp_parser_error (parser, "expected id-expression"); | |
3104 | return error_mark_node; | |
3105 | } | |
3106 | } | |
3107 | else | |
3108 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
3109 | /*check_dependency_p=*/true, |
3110 | declarator_p); | |
a723baf1 MM |
3111 | } |
3112 | ||
3113 | /* Parse an unqualified-id. | |
3114 | ||
3115 | unqualified-id: | |
3116 | identifier | |
3117 | operator-function-id | |
3118 | conversion-function-id | |
3119 | ~ class-name | |
3120 | template-id | |
3121 | ||
3122 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
3123 | keyword, in a construct like `A::template ...'. | |
3124 | ||
3125 | Returns a representation of unqualified-id. For the `identifier' | |
3126 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
3127 | production a BIT_NOT_EXPR is returned; the operand of the | |
3128 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
3129 | other productions, see the documentation accompanying the | |
3130 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
3131 | names are looked up in uninstantiated templates. If DECLARATOR_P |
3132 | is true, the unqualified-id is appearing as part of a declarator, | |
3133 | rather than as part of an expression. */ | |
a723baf1 MM |
3134 | |
3135 | static tree | |
21526606 | 3136 | cp_parser_unqualified_id (cp_parser* parser, |
94edc4ab | 3137 | bool template_keyword_p, |
f3c2dfc6 MM |
3138 | bool check_dependency_p, |
3139 | bool declarator_p) | |
a723baf1 MM |
3140 | { |
3141 | cp_token *token; | |
3142 | ||
3143 | /* Peek at the next token. */ | |
3144 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 3145 | |
a723baf1 MM |
3146 | switch (token->type) |
3147 | { | |
3148 | case CPP_NAME: | |
3149 | { | |
3150 | tree id; | |
3151 | ||
3152 | /* We don't know yet whether or not this will be a | |
3153 | template-id. */ | |
3154 | cp_parser_parse_tentatively (parser); | |
3155 | /* Try a template-id. */ | |
3156 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3157 | check_dependency_p, |
3158 | declarator_p); | |
a723baf1 MM |
3159 | /* If it worked, we're done. */ |
3160 | if (cp_parser_parse_definitely (parser)) | |
3161 | return id; | |
3162 | /* Otherwise, it's an ordinary identifier. */ | |
3163 | return cp_parser_identifier (parser); | |
3164 | } | |
3165 | ||
3166 | case CPP_TEMPLATE_ID: | |
3167 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3168 | check_dependency_p, |
3169 | declarator_p); | |
a723baf1 MM |
3170 | |
3171 | case CPP_COMPL: | |
3172 | { | |
3173 | tree type_decl; | |
3174 | tree qualifying_scope; | |
3175 | tree object_scope; | |
3176 | tree scope; | |
88e95ee3 | 3177 | bool done; |
a723baf1 MM |
3178 | |
3179 | /* Consume the `~' token. */ | |
3180 | cp_lexer_consume_token (parser->lexer); | |
3181 | /* Parse the class-name. The standard, as written, seems to | |
3182 | say that: | |
3183 | ||
3184 | template <typename T> struct S { ~S (); }; | |
3185 | template <typename T> S<T>::~S() {} | |
3186 | ||
3187 | is invalid, since `~' must be followed by a class-name, but | |
3188 | `S<T>' is dependent, and so not known to be a class. | |
3189 | That's not right; we need to look in uninstantiated | |
3190 | templates. A further complication arises from: | |
3191 | ||
3192 | template <typename T> void f(T t) { | |
3193 | t.T::~T(); | |
21526606 | 3194 | } |
a723baf1 MM |
3195 | |
3196 | Here, it is not possible to look up `T' in the scope of `T' | |
3197 | itself. We must look in both the current scope, and the | |
21526606 | 3198 | scope of the containing complete expression. |
a723baf1 MM |
3199 | |
3200 | Yet another issue is: | |
3201 | ||
3202 | struct S { | |
3203 | int S; | |
3204 | ~S(); | |
3205 | }; | |
3206 | ||
3207 | S::~S() {} | |
3208 | ||
3209 | The standard does not seem to say that the `S' in `~S' | |
3210 | should refer to the type `S' and not the data member | |
3211 | `S::S'. */ | |
3212 | ||
3213 | /* DR 244 says that we look up the name after the "~" in the | |
3214 | same scope as we looked up the qualifying name. That idea | |
3215 | isn't fully worked out; it's more complicated than that. */ | |
3216 | scope = parser->scope; | |
3217 | object_scope = parser->object_scope; | |
3218 | qualifying_scope = parser->qualifying_scope; | |
3219 | ||
3220 | /* If the name is of the form "X::~X" it's OK. */ | |
3221 | if (scope && TYPE_P (scope) | |
3222 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 3223 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3224 | == CPP_OPEN_PAREN) |
21526606 | 3225 | && (cp_lexer_peek_token (parser->lexer)->value |
a723baf1 MM |
3226 | == TYPE_IDENTIFIER (scope))) |
3227 | { | |
3228 | cp_lexer_consume_token (parser->lexer); | |
3229 | return build_nt (BIT_NOT_EXPR, scope); | |
3230 | } | |
3231 | ||
3232 | /* If there was an explicit qualification (S::~T), first look | |
3233 | in the scope given by the qualification (i.e., S). */ | |
88e95ee3 | 3234 | done = false; |
e3754f9c | 3235 | type_decl = NULL_TREE; |
a723baf1 MM |
3236 | if (scope) |
3237 | { | |
3238 | cp_parser_parse_tentatively (parser); | |
21526606 | 3239 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
3240 | /*typename_keyword_p=*/false, |
3241 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3242 | none_type, |
a723baf1 | 3243 | /*check_dependency=*/false, |
a668c6ad MM |
3244 | /*class_head_p=*/false, |
3245 | declarator_p); | |
a723baf1 | 3246 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3247 | done = true; |
a723baf1 MM |
3248 | } |
3249 | /* In "N::S::~S", look in "N" as well. */ | |
88e95ee3 | 3250 | if (!done && scope && qualifying_scope) |
a723baf1 MM |
3251 | { |
3252 | cp_parser_parse_tentatively (parser); | |
3253 | parser->scope = qualifying_scope; | |
3254 | parser->object_scope = NULL_TREE; | |
3255 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3256 | type_decl |
3257 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3258 | /*typename_keyword_p=*/false, |
3259 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3260 | none_type, |
a723baf1 | 3261 | /*check_dependency=*/false, |
a668c6ad MM |
3262 | /*class_head_p=*/false, |
3263 | declarator_p); | |
a723baf1 | 3264 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3265 | done = true; |
a723baf1 MM |
3266 | } |
3267 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
88e95ee3 | 3268 | else if (!done && object_scope) |
a723baf1 MM |
3269 | { |
3270 | cp_parser_parse_tentatively (parser); | |
3271 | parser->scope = object_scope; | |
3272 | parser->object_scope = NULL_TREE; | |
3273 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3274 | type_decl |
3275 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3276 | /*typename_keyword_p=*/false, |
3277 | /*template_keyword_p=*/false, | |
fc6a28d7 | 3278 | none_type, |
a723baf1 | 3279 | /*check_dependency=*/false, |
a668c6ad MM |
3280 | /*class_head_p=*/false, |
3281 | declarator_p); | |
a723baf1 | 3282 | if (cp_parser_parse_definitely (parser)) |
88e95ee3 | 3283 | done = true; |
a723baf1 MM |
3284 | } |
3285 | /* Look in the surrounding context. */ | |
88e95ee3 MM |
3286 | if (!done) |
3287 | { | |
3288 | parser->scope = NULL_TREE; | |
3289 | parser->object_scope = NULL_TREE; | |
3290 | parser->qualifying_scope = NULL_TREE; | |
3291 | type_decl | |
3292 | = cp_parser_class_name (parser, | |
3293 | /*typename_keyword_p=*/false, | |
3294 | /*template_keyword_p=*/false, | |
3295 | none_type, | |
3296 | /*check_dependency=*/false, | |
3297 | /*class_head_p=*/false, | |
3298 | declarator_p); | |
3299 | } | |
a723baf1 MM |
3300 | /* If an error occurred, assume that the name of the |
3301 | destructor is the same as the name of the qualifying | |
3302 | class. That allows us to keep parsing after running | |
3303 | into ill-formed destructor names. */ | |
3304 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
3305 | return build_nt (BIT_NOT_EXPR, scope); | |
3306 | else if (type_decl == error_mark_node) | |
3307 | return error_mark_node; | |
3308 | ||
f3c2dfc6 MM |
3309 | /* [class.dtor] |
3310 | ||
3311 | A typedef-name that names a class shall not be used as the | |
3312 | identifier in the declarator for a destructor declaration. */ | |
21526606 | 3313 | if (declarator_p |
f3c2dfc6 | 3314 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) |
33a69702 VR |
3315 | && !DECL_SELF_REFERENCE_P (type_decl) |
3316 | && !cp_parser_uncommitted_to_tentative_parse_p (parser)) | |
2a13a625 | 3317 | error ("typedef-name %qD used as destructor declarator", |
f3c2dfc6 MM |
3318 | type_decl); |
3319 | ||
a723baf1 MM |
3320 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
3321 | } | |
3322 | ||
3323 | case CPP_KEYWORD: | |
3324 | if (token->keyword == RID_OPERATOR) | |
3325 | { | |
3326 | tree id; | |
3327 | ||
3328 | /* This could be a template-id, so we try that first. */ | |
3329 | cp_parser_parse_tentatively (parser); | |
3330 | /* Try a template-id. */ | |
3331 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3332 | /*check_dependency_p=*/true, |
3333 | declarator_p); | |
a723baf1 MM |
3334 | /* If that worked, we're done. */ |
3335 | if (cp_parser_parse_definitely (parser)) | |
3336 | return id; | |
3337 | /* We still don't know whether we're looking at an | |
3338 | operator-function-id or a conversion-function-id. */ | |
3339 | cp_parser_parse_tentatively (parser); | |
3340 | /* Try an operator-function-id. */ | |
3341 | id = cp_parser_operator_function_id (parser); | |
3342 | /* If that didn't work, try a conversion-function-id. */ | |
3343 | if (!cp_parser_parse_definitely (parser)) | |
3344 | id = cp_parser_conversion_function_id (parser); | |
3345 | ||
3346 | return id; | |
3347 | } | |
3348 | /* Fall through. */ | |
3349 | ||
3350 | default: | |
3351 | cp_parser_error (parser, "expected unqualified-id"); | |
3352 | return error_mark_node; | |
3353 | } | |
3354 | } | |
3355 | ||
3356 | /* Parse an (optional) nested-name-specifier. | |
3357 | ||
3358 | nested-name-specifier: | |
3359 | class-or-namespace-name :: nested-name-specifier [opt] | |
3360 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3361 | ||
3362 | PARSER->SCOPE should be set appropriately before this function is | |
3363 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3364 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3365 | in name lookups. | |
3366 | ||
3367 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3368 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3369 | it unchanged if there is no nested-name-specifier. Returns the new | |
21526606 | 3370 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
a668c6ad MM |
3371 | |
3372 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3373 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3374 | |
3375 | static tree | |
21526606 EC |
3376 | cp_parser_nested_name_specifier_opt (cp_parser *parser, |
3377 | bool typename_keyword_p, | |
a723baf1 | 3378 | bool check_dependency_p, |
a668c6ad MM |
3379 | bool type_p, |
3380 | bool is_declaration) | |
a723baf1 MM |
3381 | { |
3382 | bool success = false; | |
3383 | tree access_check = NULL_TREE; | |
0c5e4866 NS |
3384 | cp_token_position start = 0; |
3385 | cp_token *token; | |
a723baf1 MM |
3386 | |
3387 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb | 3388 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
21526606 | 3389 | false, it may have been true before, in which case something |
2050a1bb MM |
3390 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier |
3391 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3392 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3393 | main loop. */ | |
3394 | if (check_dependency_p | |
3395 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3396 | { | |
3397 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3398 | return parser->scope; |
3399 | } | |
3400 | ||
3401 | /* Remember where the nested-name-specifier starts. */ | |
0b16f8f4 | 3402 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 3403 | start = cp_lexer_token_position (parser->lexer, false); |
a723baf1 | 3404 | |
8d241e0b | 3405 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3406 | |
a723baf1 MM |
3407 | while (true) |
3408 | { | |
3409 | tree new_scope; | |
3410 | tree old_scope; | |
3411 | tree saved_qualifying_scope; | |
a723baf1 MM |
3412 | bool template_keyword_p; |
3413 | ||
2050a1bb MM |
3414 | /* Spot cases that cannot be the beginning of a |
3415 | nested-name-specifier. */ | |
3416 | token = cp_lexer_peek_token (parser->lexer); | |
3417 | ||
3418 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3419 | the already parsed nested-name-specifier. */ | |
3420 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3421 | { | |
3422 | /* Grab the nested-name-specifier and continue the loop. */ | |
3423 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3424 | success = true; | |
3425 | continue; | |
3426 | } | |
3427 | ||
a723baf1 MM |
3428 | /* Spot cases that cannot be the beginning of a |
3429 | nested-name-specifier. On the second and subsequent times | |
3430 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3431 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3432 | ; |
3433 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3434 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3435 | ; |
3436 | else | |
3437 | { | |
3438 | /* If the next token is not an identifier, then it is | |
3439 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3440 | if (token->type != CPP_NAME) |
a723baf1 MM |
3441 | break; |
3442 | /* If the following token is neither a `<' (to begin a | |
3443 | template-id), nor a `::', then we are not looking at a | |
3444 | nested-name-specifier. */ | |
3445 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
f4abade9 GB |
3446 | if (token->type != CPP_SCOPE |
3447 | && !cp_parser_nth_token_starts_template_argument_list_p | |
3448 | (parser, 2)) | |
a723baf1 MM |
3449 | break; |
3450 | } | |
3451 | ||
3452 | /* The nested-name-specifier is optional, so we parse | |
3453 | tentatively. */ | |
3454 | cp_parser_parse_tentatively (parser); | |
3455 | ||
3456 | /* Look for the optional `template' keyword, if this isn't the | |
3457 | first time through the loop. */ | |
3458 | if (success) | |
3459 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3460 | else | |
3461 | template_keyword_p = false; | |
3462 | ||
3463 | /* Save the old scope since the name lookup we are about to do | |
3464 | might destroy it. */ | |
3465 | old_scope = parser->scope; | |
3466 | saved_qualifying_scope = parser->qualifying_scope; | |
a52eb3bc MM |
3467 | /* In a declarator-id like "X<T>::I::Y<T>" we must be able to |
3468 | look up names in "X<T>::I" in order to determine that "Y" is | |
3469 | a template. So, if we have a typename at this point, we make | |
3470 | an effort to look through it. */ | |
67bcc252 MM |
3471 | if (is_declaration |
3472 | && !typename_keyword_p | |
3473 | && parser->scope | |
a52eb3bc MM |
3474 | && TREE_CODE (parser->scope) == TYPENAME_TYPE) |
3475 | parser->scope = resolve_typename_type (parser->scope, | |
3476 | /*only_current_p=*/false); | |
a723baf1 | 3477 | /* Parse the qualifying entity. */ |
21526606 | 3478 | new_scope |
a723baf1 MM |
3479 | = cp_parser_class_or_namespace_name (parser, |
3480 | typename_keyword_p, | |
3481 | template_keyword_p, | |
3482 | check_dependency_p, | |
a668c6ad MM |
3483 | type_p, |
3484 | is_declaration); | |
a723baf1 MM |
3485 | /* Look for the `::' token. */ |
3486 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3487 | ||
3488 | /* If we found what we wanted, we keep going; otherwise, we're | |
3489 | done. */ | |
3490 | if (!cp_parser_parse_definitely (parser)) | |
3491 | { | |
3492 | bool error_p = false; | |
3493 | ||
3494 | /* Restore the OLD_SCOPE since it was valid before the | |
3495 | failed attempt at finding the last | |
3496 | class-or-namespace-name. */ | |
3497 | parser->scope = old_scope; | |
3498 | parser->qualifying_scope = saved_qualifying_scope; | |
3499 | /* If the next token is an identifier, and the one after | |
3500 | that is a `::', then any valid interpretation would have | |
3501 | found a class-or-namespace-name. */ | |
3502 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 3503 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3504 | == CPP_SCOPE) |
21526606 | 3505 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
3506 | != CPP_COMPL)) |
3507 | { | |
3508 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 3509 | if (!error_p) |
a723baf1 MM |
3510 | { |
3511 | tree decl; | |
3512 | ||
3513 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3514 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
2a13a625 | 3515 | error ("%qD used without template parameters", decl); |
a723baf1 | 3516 | else |
21526606 EC |
3517 | cp_parser_name_lookup_error |
3518 | (parser, token->value, decl, | |
4bb8ca28 | 3519 | "is not a class or namespace"); |
a723baf1 MM |
3520 | parser->scope = NULL_TREE; |
3521 | error_p = true; | |
eea9800f MM |
3522 | /* Treat this as a successful nested-name-specifier |
3523 | due to: | |
3524 | ||
3525 | [basic.lookup.qual] | |
3526 | ||
3527 | If the name found is not a class-name (clause | |
3528 | _class_) or namespace-name (_namespace.def_), the | |
3529 | program is ill-formed. */ | |
3530 | success = true; | |
a723baf1 MM |
3531 | } |
3532 | cp_lexer_consume_token (parser->lexer); | |
3533 | } | |
3534 | break; | |
3535 | } | |
3536 | ||
3537 | /* We've found one valid nested-name-specifier. */ | |
3538 | success = true; | |
3539 | /* Make sure we look in the right scope the next time through | |
3540 | the loop. */ | |
21526606 | 3541 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL |
a723baf1 MM |
3542 | ? TREE_TYPE (new_scope) |
3543 | : new_scope); | |
3544 | /* If it is a class scope, try to complete it; we are about to | |
3545 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3546 | if (TYPE_P (parser->scope) |
3547 | /* Since checking types for dependency can be expensive, | |
3548 | avoid doing it if the type is already complete. */ | |
3549 | && !COMPLETE_TYPE_P (parser->scope) | |
3550 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3551 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3552 | complete_type (parser->scope); |
3553 | } | |
3554 | ||
cf22909c KL |
3555 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3556 | so the memory will not be reclaimed during token replacing below. */ | |
3557 | access_check = get_deferred_access_checks (); | |
3558 | ||
a723baf1 MM |
3559 | /* If parsing tentatively, replace the sequence of tokens that makes |
3560 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3561 | token. That way, should we re-parse the token stream, we will | |
3562 | not have to repeat the effort required to do the parse, nor will | |
3563 | we issue duplicate error messages. */ | |
0c5e4866 | 3564 | if (success && start) |
a723baf1 | 3565 | { |
0c5e4866 NS |
3566 | cp_token *token = cp_lexer_token_at (parser->lexer, start); |
3567 | ||
a723baf1 MM |
3568 | /* Reset the contents of the START token. */ |
3569 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3570 | token->value = build_tree_list (access_check, parser->scope); | |
3571 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3572 | token->keyword = RID_MAX; | |
0c5e4866 | 3573 | |
a723baf1 | 3574 | /* Purge all subsequent tokens. */ |
0c5e4866 | 3575 | cp_lexer_purge_tokens_after (parser->lexer, start); |
a723baf1 MM |
3576 | } |
3577 | ||
cf22909c | 3578 | pop_deferring_access_checks (); |
a723baf1 MM |
3579 | return success ? parser->scope : NULL_TREE; |
3580 | } | |
3581 | ||
3582 | /* Parse a nested-name-specifier. See | |
3583 | cp_parser_nested_name_specifier_opt for details. This function | |
3584 | behaves identically, except that it will an issue an error if no | |
3585 | nested-name-specifier is present, and it will return | |
3586 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3587 | is present. */ | |
3588 | ||
3589 | static tree | |
21526606 EC |
3590 | cp_parser_nested_name_specifier (cp_parser *parser, |
3591 | bool typename_keyword_p, | |
a723baf1 | 3592 | bool check_dependency_p, |
a668c6ad MM |
3593 | bool type_p, |
3594 | bool is_declaration) | |
a723baf1 MM |
3595 | { |
3596 | tree scope; | |
3597 | ||
3598 | /* Look for the nested-name-specifier. */ | |
3599 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3600 | typename_keyword_p, | |
3601 | check_dependency_p, | |
a668c6ad MM |
3602 | type_p, |
3603 | is_declaration); | |
a723baf1 MM |
3604 | /* If it was not present, issue an error message. */ |
3605 | if (!scope) | |
3606 | { | |
3607 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3608 | parser->scope = NULL_TREE; |
a723baf1 MM |
3609 | return error_mark_node; |
3610 | } | |
3611 | ||
3612 | return scope; | |
3613 | } | |
3614 | ||
3615 | /* Parse a class-or-namespace-name. | |
3616 | ||
3617 | class-or-namespace-name: | |
3618 | class-name | |
3619 | namespace-name | |
3620 | ||
3621 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3622 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3623 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3624 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3625 | even the same name is declared to be another entity in the same | |
3626 | scope. | |
3627 | ||
3628 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3629 | specified by the class-or-namespace-name. If neither is found the |
3630 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3631 | |
3632 | static tree | |
21526606 | 3633 | cp_parser_class_or_namespace_name (cp_parser *parser, |
a723baf1 MM |
3634 | bool typename_keyword_p, |
3635 | bool template_keyword_p, | |
3636 | bool check_dependency_p, | |
a668c6ad MM |
3637 | bool type_p, |
3638 | bool is_declaration) | |
a723baf1 MM |
3639 | { |
3640 | tree saved_scope; | |
3641 | tree saved_qualifying_scope; | |
3642 | tree saved_object_scope; | |
3643 | tree scope; | |
eea9800f | 3644 | bool only_class_p; |
a723baf1 | 3645 | |
a723baf1 MM |
3646 | /* Before we try to parse the class-name, we must save away the |
3647 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3648 | it. */ | |
3649 | saved_scope = parser->scope; | |
3650 | saved_qualifying_scope = parser->qualifying_scope; | |
3651 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3652 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3653 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3654 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3655 | if (!only_class_p) |
3656 | cp_parser_parse_tentatively (parser); | |
21526606 | 3657 | scope = cp_parser_class_name (parser, |
a723baf1 MM |
3658 | typename_keyword_p, |
3659 | template_keyword_p, | |
fc6a28d7 | 3660 | type_p ? class_type : none_type, |
a723baf1 | 3661 | check_dependency_p, |
a668c6ad MM |
3662 | /*class_head_p=*/false, |
3663 | is_declaration); | |
a723baf1 | 3664 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3665 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3666 | { |
3667 | /* Restore the saved scope. */ | |
3668 | parser->scope = saved_scope; | |
3669 | parser->qualifying_scope = saved_qualifying_scope; | |
3670 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3671 | /* If we are not looking at an identifier followed by the scope |
3672 | resolution operator, then this is not part of a | |
3673 | nested-name-specifier. (Note that this function is only used | |
3674 | to parse the components of a nested-name-specifier.) */ | |
3675 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3676 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3677 | return error_mark_node; | |
a723baf1 MM |
3678 | scope = cp_parser_namespace_name (parser); |
3679 | } | |
3680 | ||
3681 | return scope; | |
3682 | } | |
3683 | ||
3684 | /* Parse a postfix-expression. | |
3685 | ||
3686 | postfix-expression: | |
3687 | primary-expression | |
3688 | postfix-expression [ expression ] | |
3689 | postfix-expression ( expression-list [opt] ) | |
3690 | simple-type-specifier ( expression-list [opt] ) | |
21526606 | 3691 | typename :: [opt] nested-name-specifier identifier |
a723baf1 MM |
3692 | ( expression-list [opt] ) |
3693 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3694 | ( expression-list [opt] ) | |
3695 | postfix-expression . template [opt] id-expression | |
3696 | postfix-expression -> template [opt] id-expression | |
3697 | postfix-expression . pseudo-destructor-name | |
3698 | postfix-expression -> pseudo-destructor-name | |
3699 | postfix-expression ++ | |
3700 | postfix-expression -- | |
3701 | dynamic_cast < type-id > ( expression ) | |
3702 | static_cast < type-id > ( expression ) | |
3703 | reinterpret_cast < type-id > ( expression ) | |
3704 | const_cast < type-id > ( expression ) | |
3705 | typeid ( expression ) | |
3706 | typeid ( type-id ) | |
3707 | ||
3708 | GNU Extension: | |
21526606 | 3709 | |
a723baf1 MM |
3710 | postfix-expression: |
3711 | ( type-id ) { initializer-list , [opt] } | |
3712 | ||
3713 | This extension is a GNU version of the C99 compound-literal | |
3714 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3715 | but they are essentially the same concept.) | |
3716 | ||
3717 | If ADDRESS_P is true, the postfix expression is the operand of the | |
93678513 MM |
3718 | `&' operator. CAST_P is true if this expression is the target of a |
3719 | cast. | |
a723baf1 MM |
3720 | |
3721 | Returns a representation of the expression. */ | |
3722 | ||
3723 | static tree | |
93678513 | 3724 | cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
3725 | { |
3726 | cp_token *token; | |
3727 | enum rid keyword; | |
b3445994 | 3728 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3729 | tree postfix_expression = NULL_TREE; |
3730 | /* Non-NULL only if the current postfix-expression can be used to | |
3731 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3732 | class used to qualify the member. */ | |
3733 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3734 | |
3735 | /* Peek at the next token. */ | |
3736 | token = cp_lexer_peek_token (parser->lexer); | |
3737 | /* Some of the productions are determined by keywords. */ | |
3738 | keyword = token->keyword; | |
3739 | switch (keyword) | |
3740 | { | |
3741 | case RID_DYNCAST: | |
3742 | case RID_STATCAST: | |
3743 | case RID_REINTCAST: | |
3744 | case RID_CONSTCAST: | |
3745 | { | |
3746 | tree type; | |
3747 | tree expression; | |
3748 | const char *saved_message; | |
3749 | ||
3750 | /* All of these can be handled in the same way from the point | |
3751 | of view of parsing. Begin by consuming the token | |
3752 | identifying the cast. */ | |
3753 | cp_lexer_consume_token (parser->lexer); | |
21526606 | 3754 | |
a723baf1 MM |
3755 | /* New types cannot be defined in the cast. */ |
3756 | saved_message = parser->type_definition_forbidden_message; | |
3757 | parser->type_definition_forbidden_message | |
3758 | = "types may not be defined in casts"; | |
3759 | ||
3760 | /* Look for the opening `<'. */ | |
3761 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3762 | /* Parse the type to which we are casting. */ | |
3763 | type = cp_parser_type_id (parser); | |
3764 | /* Look for the closing `>'. */ | |
3765 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3766 | /* Restore the old message. */ | |
3767 | parser->type_definition_forbidden_message = saved_message; | |
3768 | ||
3769 | /* And the expression which is being cast. */ | |
3770 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
93678513 | 3771 | expression = cp_parser_expression (parser, /*cast_p=*/true); |
a723baf1 MM |
3772 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); |
3773 | ||
14d22dd6 MM |
3774 | /* Only type conversions to integral or enumeration types |
3775 | can be used in constant-expressions. */ | |
67c03833 | 3776 | if (parser->integral_constant_expression_p |
14d22dd6 | 3777 | && !dependent_type_p (type) |
263ee052 | 3778 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
98ca843c | 3779 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
3780 | (parser, |
3781 | "a cast to a type other than an integral or " | |
3782 | "enumeration type"))) | |
3783 | return error_mark_node; | |
14d22dd6 | 3784 | |
a723baf1 MM |
3785 | switch (keyword) |
3786 | { | |
3787 | case RID_DYNCAST: | |
3788 | postfix_expression | |
3789 | = build_dynamic_cast (type, expression); | |
3790 | break; | |
3791 | case RID_STATCAST: | |
3792 | postfix_expression | |
3793 | = build_static_cast (type, expression); | |
3794 | break; | |
3795 | case RID_REINTCAST: | |
3796 | postfix_expression | |
3797 | = build_reinterpret_cast (type, expression); | |
3798 | break; | |
3799 | case RID_CONSTCAST: | |
3800 | postfix_expression | |
3801 | = build_const_cast (type, expression); | |
3802 | break; | |
3803 | default: | |
315fb5db | 3804 | gcc_unreachable (); |
a723baf1 MM |
3805 | } |
3806 | } | |
3807 | break; | |
3808 | ||
3809 | case RID_TYPEID: | |
3810 | { | |
3811 | tree type; | |
3812 | const char *saved_message; | |
4f8163b1 | 3813 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3814 | |
3815 | /* Consume the `typeid' token. */ | |
3816 | cp_lexer_consume_token (parser->lexer); | |
3817 | /* Look for the `(' token. */ | |
3818 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3819 | /* Types cannot be defined in a `typeid' expression. */ | |
3820 | saved_message = parser->type_definition_forbidden_message; | |
3821 | parser->type_definition_forbidden_message | |
3822 | = "types may not be defined in a `typeid\' expression"; | |
3823 | /* We can't be sure yet whether we're looking at a type-id or an | |
3824 | expression. */ | |
3825 | cp_parser_parse_tentatively (parser); | |
3826 | /* Try a type-id first. */ | |
4f8163b1 MM |
3827 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3828 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 3829 | type = cp_parser_type_id (parser); |
4f8163b1 | 3830 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
3831 | /* Look for the `)' token. Otherwise, we can't be sure that |
3832 | we're not looking at an expression: consider `typeid (int | |
3833 | (3))', for example. */ | |
3834 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3835 | /* If all went well, simply lookup the type-id. */ | |
3836 | if (cp_parser_parse_definitely (parser)) | |
3837 | postfix_expression = get_typeid (type); | |
3838 | /* Otherwise, fall back to the expression variant. */ | |
3839 | else | |
3840 | { | |
3841 | tree expression; | |
3842 | ||
3843 | /* Look for an expression. */ | |
93678513 | 3844 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
3845 | /* Compute its typeid. */ |
3846 | postfix_expression = build_typeid (expression); | |
3847 | /* Look for the `)' token. */ | |
3848 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3849 | } | |
4424e0da | 3850 | /* `typeid' may not appear in an integral constant expression. */ |
98ca843c | 3851 | if (cp_parser_non_integral_constant_expression(parser, |
4424e0da GB |
3852 | "`typeid' operator")) |
3853 | return error_mark_node; | |
a723baf1 MM |
3854 | /* Restore the saved message. */ |
3855 | parser->type_definition_forbidden_message = saved_message; | |
3856 | } | |
3857 | break; | |
21526606 | 3858 | |
a723baf1 MM |
3859 | case RID_TYPENAME: |
3860 | { | |
3861 | bool template_p = false; | |
3862 | tree id; | |
3863 | tree type; | |
26c895e7 | 3864 | tree scope; |
a723baf1 MM |
3865 | |
3866 | /* Consume the `typename' token. */ | |
3867 | cp_lexer_consume_token (parser->lexer); | |
3868 | /* Look for the optional `::' operator. */ | |
21526606 | 3869 | cp_parser_global_scope_opt (parser, |
a723baf1 | 3870 | /*current_scope_valid_p=*/false); |
26c895e7 KL |
3871 | /* Look for the nested-name-specifier. In case of error here, |
3872 | consume the trailing id to avoid subsequent error messages | |
3873 | for usual cases. */ | |
3874 | scope = cp_parser_nested_name_specifier (parser, | |
3875 | /*typename_keyword_p=*/true, | |
3876 | /*check_dependency_p=*/true, | |
3877 | /*type_p=*/true, | |
3878 | /*is_declaration=*/true); | |
3879 | ||
a723baf1 MM |
3880 | /* Look for the optional `template' keyword. */ |
3881 | template_p = cp_parser_optional_template_keyword (parser); | |
3882 | /* We don't know whether we're looking at a template-id or an | |
3883 | identifier. */ | |
3884 | cp_parser_parse_tentatively (parser); | |
3885 | /* Try a template-id. */ | |
3886 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3887 | /*check_dependency_p=*/true, |
3888 | /*is_declaration=*/true); | |
a723baf1 MM |
3889 | /* If that didn't work, try an identifier. */ |
3890 | if (!cp_parser_parse_definitely (parser)) | |
3891 | id = cp_parser_identifier (parser); | |
26c895e7 KL |
3892 | |
3893 | /* Don't process id if nested name specifier is invalid. */ | |
3894 | if (scope == error_mark_node) | |
3895 | return error_mark_node; | |
26bcf8fc MM |
3896 | /* If we look up a template-id in a non-dependent qualifying |
3897 | scope, there's no need to create a dependent type. */ | |
26c895e7 | 3898 | else if (TREE_CODE (id) == TYPE_DECL |
26bcf8fc MM |
3899 | && !dependent_type_p (parser->scope)) |
3900 | type = TREE_TYPE (id); | |
a723baf1 MM |
3901 | /* Create a TYPENAME_TYPE to represent the type to which the |
3902 | functional cast is being performed. */ | |
26bcf8fc | 3903 | else |
98ca843c | 3904 | type = make_typename_type (parser->scope, id, |
fc6a28d7 | 3905 | typename_type, |
26bcf8fc | 3906 | /*complain=*/1); |
a723baf1 MM |
3907 | |
3908 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3909 | } | |
3910 | break; | |
3911 | ||
3912 | default: | |
3913 | { | |
3914 | tree type; | |
3915 | ||
3916 | /* If the next thing is a simple-type-specifier, we may be | |
3917 | looking at a functional cast. We could also be looking at | |
3918 | an id-expression. So, we try the functional cast, and if | |
3919 | that doesn't work we fall back to the primary-expression. */ | |
3920 | cp_parser_parse_tentatively (parser); | |
3921 | /* Look for the simple-type-specifier. */ | |
21526606 | 3922 | type = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
3923 | /*decl_specs=*/NULL, |
3924 | CP_PARSER_FLAGS_NONE); | |
a723baf1 MM |
3925 | /* Parse the cast itself. */ |
3926 | if (!cp_parser_error_occurred (parser)) | |
21526606 | 3927 | postfix_expression |
a723baf1 MM |
3928 | = cp_parser_functional_cast (parser, type); |
3929 | /* If that worked, we're done. */ | |
3930 | if (cp_parser_parse_definitely (parser)) | |
3931 | break; | |
3932 | ||
3933 | /* If the functional-cast didn't work out, try a | |
3934 | compound-literal. */ | |
14d22dd6 MM |
3935 | if (cp_parser_allow_gnu_extensions_p (parser) |
3936 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3937 | { |
3938 | tree initializer_list = NULL_TREE; | |
4f8163b1 | 3939 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3940 | |
3941 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3942 | /* Consume the `('. */ |
3943 | cp_lexer_consume_token (parser->lexer); | |
3944 | /* Parse the type. */ | |
4f8163b1 MM |
3945 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3946 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 3947 | type = cp_parser_type_id (parser); |
4f8163b1 | 3948 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 MM |
3949 | /* Look for the `)'. */ |
3950 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3951 | /* Look for the `{'. */ | |
3952 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3953 | /* If things aren't going well, there's no need to | |
3954 | keep going. */ | |
3955 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3956 | { |
39703eb9 | 3957 | bool non_constant_p; |
14d22dd6 | 3958 | /* Parse the initializer-list. */ |
21526606 | 3959 | initializer_list |
39703eb9 | 3960 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3961 | /* Allow a trailing `,'. */ |
3962 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3963 | cp_lexer_consume_token (parser->lexer); | |
3964 | /* Look for the final `}'. */ | |
3965 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3966 | } |
3967 | /* If that worked, we're definitely looking at a | |
3968 | compound-literal expression. */ | |
3969 | if (cp_parser_parse_definitely (parser)) | |
3970 | { | |
3971 | /* Warn the user that a compound literal is not | |
3972 | allowed in standard C++. */ | |
3973 | if (pedantic) | |
3974 | pedwarn ("ISO C++ forbids compound-literals"); | |
3975 | /* Form the representation of the compound-literal. */ | |
21526606 | 3976 | postfix_expression |
a723baf1 MM |
3977 | = finish_compound_literal (type, initializer_list); |
3978 | break; | |
3979 | } | |
3980 | } | |
3981 | ||
3982 | /* It must be a primary-expression. */ | |
21526606 | 3983 | postfix_expression = cp_parser_primary_expression (parser, |
93678513 | 3984 | cast_p, |
a723baf1 MM |
3985 | &idk, |
3986 | &qualifying_class); | |
3987 | } | |
3988 | break; | |
3989 | } | |
3990 | ||
ee76b931 MM |
3991 | /* If we were avoiding committing to the processing of a |
3992 | qualified-id until we knew whether or not we had a | |
3993 | pointer-to-member, we now know. */ | |
089d6ea7 | 3994 | if (qualifying_class) |
a723baf1 | 3995 | { |
ee76b931 | 3996 | bool done; |
a723baf1 | 3997 | |
ee76b931 MM |
3998 | /* Peek at the next token. */ |
3999 | token = cp_lexer_peek_token (parser->lexer); | |
4000 | done = (token->type != CPP_OPEN_SQUARE | |
4001 | && token->type != CPP_OPEN_PAREN | |
4002 | && token->type != CPP_DOT | |
4003 | && token->type != CPP_DEREF | |
4004 | && token->type != CPP_PLUS_PLUS | |
4005 | && token->type != CPP_MINUS_MINUS); | |
4006 | ||
4007 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
4008 | postfix_expression, | |
4009 | done, | |
4010 | address_p); | |
4011 | if (done) | |
4012 | return postfix_expression; | |
a723baf1 MM |
4013 | } |
4014 | ||
a723baf1 MM |
4015 | /* Keep looping until the postfix-expression is complete. */ |
4016 | while (true) | |
4017 | { | |
10b1d5e7 MM |
4018 | if (idk == CP_ID_KIND_UNQUALIFIED |
4019 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 4020 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 | 4021 | /* It is not a Koenig lookup function call. */ |
21526606 | 4022 | postfix_expression |
b3445994 | 4023 | = unqualified_name_lookup_error (postfix_expression); |
21526606 | 4024 | |
a723baf1 MM |
4025 | /* Peek at the next token. */ |
4026 | token = cp_lexer_peek_token (parser->lexer); | |
4027 | ||
4028 | switch (token->type) | |
4029 | { | |
4030 | case CPP_OPEN_SQUARE: | |
7a3ea201 RH |
4031 | postfix_expression |
4032 | = cp_parser_postfix_open_square_expression (parser, | |
4033 | postfix_expression, | |
4034 | false); | |
4035 | idk = CP_ID_KIND_NONE; | |
a723baf1 MM |
4036 | break; |
4037 | ||
4038 | case CPP_OPEN_PAREN: | |
4039 | /* postfix-expression ( expression-list [opt] ) */ | |
4040 | { | |
6d80c4b9 | 4041 | bool koenig_p; |
21526606 | 4042 | tree args = (cp_parser_parenthesized_expression_list |
93678513 MM |
4043 | (parser, false, |
4044 | /*cast_p=*/false, | |
4045 | /*non_constant_p=*/NULL)); | |
a723baf1 | 4046 | |
7efa3e22 NS |
4047 | if (args == error_mark_node) |
4048 | { | |
4049 | postfix_expression = error_mark_node; | |
4050 | break; | |
4051 | } | |
21526606 | 4052 | |
14d22dd6 MM |
4053 | /* Function calls are not permitted in |
4054 | constant-expressions. */ | |
100d337a MA |
4055 | if (! builtin_valid_in_constant_expr_p (postfix_expression) |
4056 | && cp_parser_non_integral_constant_expression (parser, | |
4057 | "a function call")) | |
14d22dd6 | 4058 | { |
625cbf93 MM |
4059 | postfix_expression = error_mark_node; |
4060 | break; | |
14d22dd6 | 4061 | } |
a723baf1 | 4062 | |
6d80c4b9 | 4063 | koenig_p = false; |
399dedb9 NS |
4064 | if (idk == CP_ID_KIND_UNQUALIFIED) |
4065 | { | |
89d594a2 NS |
4066 | if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
4067 | { | |
4068 | if (args) | |
4069 | { | |
4070 | koenig_p = true; | |
4071 | postfix_expression | |
4072 | = perform_koenig_lookup (postfix_expression, args); | |
4073 | } | |
4074 | else | |
4075 | postfix_expression | |
4076 | = unqualified_fn_lookup_error (postfix_expression); | |
4077 | } | |
676e33ca MM |
4078 | /* We do not perform argument-dependent lookup if |
4079 | normal lookup finds a non-function, in accordance | |
4080 | with the expected resolution of DR 218. */ | |
89d594a2 | 4081 | else if (args && is_overloaded_fn (postfix_expression)) |
6d80c4b9 | 4082 | { |
89d594a2 NS |
4083 | tree fn = get_first_fn (postfix_expression); |
4084 | ||
4085 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
4086 | fn = OVL_CURRENT (TREE_OPERAND (fn, 0)); | |
4087 | ||
4088 | /* Only do argument dependent lookup if regular | |
4089 | lookup does not find a set of member functions. | |
4090 | [basic.lookup.koenig]/2a */ | |
4091 | if (!DECL_FUNCTION_MEMBER_P (fn)) | |
4092 | { | |
4093 | koenig_p = true; | |
4094 | postfix_expression | |
4095 | = perform_koenig_lookup (postfix_expression, args); | |
4096 | } | |
6d80c4b9 | 4097 | } |
399dedb9 | 4098 | } |
21526606 | 4099 | |
d17811fd | 4100 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 4101 | { |
d17811fd MM |
4102 | tree instance = TREE_OPERAND (postfix_expression, 0); |
4103 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
4104 | ||
4105 | if (processing_template_decl | |
4106 | && (type_dependent_expression_p (instance) | |
4107 | || (!BASELINK_P (fn) | |
4108 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 4109 | || type_dependent_expression_p (fn) |
d17811fd MM |
4110 | || any_type_dependent_arguments_p (args))) |
4111 | { | |
4112 | postfix_expression | |
6de9cd9a DN |
4113 | = build_min_nt (CALL_EXPR, postfix_expression, |
4114 | args, NULL_TREE); | |
d17811fd MM |
4115 | break; |
4116 | } | |
9f880ef9 MM |
4117 | |
4118 | if (BASELINK_P (fn)) | |
4119 | postfix_expression | |
21526606 EC |
4120 | = (build_new_method_call |
4121 | (instance, fn, args, NULL_TREE, | |
4122 | (idk == CP_ID_KIND_QUALIFIED | |
9f880ef9 MM |
4123 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
4124 | else | |
4125 | postfix_expression | |
4126 | = finish_call_expr (postfix_expression, args, | |
4127 | /*disallow_virtual=*/false, | |
4128 | /*koenig_p=*/false); | |
a723baf1 | 4129 | } |
d17811fd MM |
4130 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
4131 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
4132 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
4133 | postfix_expression = (build_offset_ref_call_from_tree |
4134 | (postfix_expression, args)); | |
b3445994 | 4135 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
4136 | /* A call to a static class member, or a namespace-scope |
4137 | function. */ | |
4138 | postfix_expression | |
4139 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
4140 | /*disallow_virtual=*/true, |
4141 | koenig_p); | |
a723baf1 | 4142 | else |
2050a1bb | 4143 | /* All other function calls. */ |
21526606 EC |
4144 | postfix_expression |
4145 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
4146 | /*disallow_virtual=*/false, |
4147 | koenig_p); | |
a723baf1 MM |
4148 | |
4149 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 4150 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4151 | } |
4152 | break; | |
21526606 | 4153 | |
a723baf1 MM |
4154 | case CPP_DOT: |
4155 | case CPP_DEREF: | |
21526606 EC |
4156 | /* postfix-expression . template [opt] id-expression |
4157 | postfix-expression . pseudo-destructor-name | |
a723baf1 MM |
4158 | postfix-expression -> template [opt] id-expression |
4159 | postfix-expression -> pseudo-destructor-name */ | |
98ca843c | 4160 | |
7a3ea201 RH |
4161 | /* Consume the `.' or `->' operator. */ |
4162 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 4163 | |
7a3ea201 RH |
4164 | postfix_expression |
4165 | = cp_parser_postfix_dot_deref_expression (parser, token->type, | |
4166 | postfix_expression, | |
4167 | false, &idk); | |
a723baf1 MM |
4168 | break; |
4169 | ||
4170 | case CPP_PLUS_PLUS: | |
4171 | /* postfix-expression ++ */ | |
4172 | /* Consume the `++' token. */ | |
4173 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4174 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4175 | postfix_expression |
4176 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4177 | POSTINCREMENT_EXPR); |
14d22dd6 | 4178 | /* Increments may not appear in constant-expressions. */ |
625cbf93 MM |
4179 | if (cp_parser_non_integral_constant_expression (parser, |
4180 | "an increment")) | |
4181 | postfix_expression = error_mark_node; | |
b3445994 | 4182 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4183 | break; |
4184 | ||
4185 | case CPP_MINUS_MINUS: | |
4186 | /* postfix-expression -- */ | |
4187 | /* Consume the `--' token. */ | |
4188 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 4189 | /* Generate a representation for the complete expression. */ |
21526606 EC |
4190 | postfix_expression |
4191 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 4192 | POSTDECREMENT_EXPR); |
14d22dd6 | 4193 | /* Decrements may not appear in constant-expressions. */ |
625cbf93 MM |
4194 | if (cp_parser_non_integral_constant_expression (parser, |
4195 | "a decrement")) | |
4196 | postfix_expression = error_mark_node; | |
b3445994 | 4197 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
4198 | break; |
4199 | ||
4200 | default: | |
4201 | return postfix_expression; | |
4202 | } | |
4203 | } | |
4204 | ||
4205 | /* We should never get here. */ | |
315fb5db | 4206 | gcc_unreachable (); |
a723baf1 MM |
4207 | return error_mark_node; |
4208 | } | |
4209 | ||
7a3ea201 RH |
4210 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked |
4211 | by cp_parser_builtin_offsetof. We're looking for | |
4212 | ||
4213 | postfix-expression [ expression ] | |
4214 | ||
4215 | FOR_OFFSETOF is set if we're being called in that context, which | |
4216 | changes how we deal with integer constant expressions. */ | |
4217 | ||
4218 | static tree | |
4219 | cp_parser_postfix_open_square_expression (cp_parser *parser, | |
4220 | tree postfix_expression, | |
4221 | bool for_offsetof) | |
4222 | { | |
4223 | tree index; | |
4224 | ||
4225 | /* Consume the `[' token. */ | |
4226 | cp_lexer_consume_token (parser->lexer); | |
4227 | ||
4228 | /* Parse the index expression. */ | |
4229 | /* ??? For offsetof, there is a question of what to allow here. If | |
4230 | offsetof is not being used in an integral constant expression context, | |
4231 | then we *could* get the right answer by computing the value at runtime. | |
4232 | If we are in an integral constant expression context, then we might | |
4233 | could accept any constant expression; hard to say without analysis. | |
4234 | Rather than open the barn door too wide right away, allow only integer | |
77880ae4 | 4235 | constant expressions here. */ |
7a3ea201 RH |
4236 | if (for_offsetof) |
4237 | index = cp_parser_constant_expression (parser, false, NULL); | |
4238 | else | |
93678513 | 4239 | index = cp_parser_expression (parser, /*cast_p=*/false); |
7a3ea201 RH |
4240 | |
4241 | /* Look for the closing `]'. */ | |
4242 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4243 | ||
4244 | /* Build the ARRAY_REF. */ | |
4245 | postfix_expression = grok_array_decl (postfix_expression, index); | |
4246 | ||
4247 | /* When not doing offsetof, array references are not permitted in | |
4248 | constant-expressions. */ | |
4249 | if (!for_offsetof | |
4250 | && (cp_parser_non_integral_constant_expression | |
4251 | (parser, "an array reference"))) | |
4252 | postfix_expression = error_mark_node; | |
4253 | ||
4254 | return postfix_expression; | |
4255 | } | |
4256 | ||
4257 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked | |
4258 | by cp_parser_builtin_offsetof. We're looking for | |
4259 | ||
4260 | postfix-expression . template [opt] id-expression | |
4261 | postfix-expression . pseudo-destructor-name | |
4262 | postfix-expression -> template [opt] id-expression | |
4263 | postfix-expression -> pseudo-destructor-name | |
4264 | ||
4265 | FOR_OFFSETOF is set if we're being called in that context. That sorta | |
4266 | limits what of the above we'll actually accept, but nevermind. | |
4267 | TOKEN_TYPE is the "." or "->" token, which will already have been | |
4268 | removed from the stream. */ | |
4269 | ||
4270 | static tree | |
4271 | cp_parser_postfix_dot_deref_expression (cp_parser *parser, | |
4272 | enum cpp_ttype token_type, | |
4273 | tree postfix_expression, | |
4274 | bool for_offsetof, cp_id_kind *idk) | |
4275 | { | |
4276 | tree name; | |
4277 | bool dependent_p; | |
4278 | bool template_p; | |
17a27b4f | 4279 | bool pseudo_destructor_p; |
7a3ea201 RH |
4280 | tree scope = NULL_TREE; |
4281 | ||
4282 | /* If this is a `->' operator, dereference the pointer. */ | |
4283 | if (token_type == CPP_DEREF) | |
4284 | postfix_expression = build_x_arrow (postfix_expression); | |
4285 | /* Check to see whether or not the expression is type-dependent. */ | |
4286 | dependent_p = type_dependent_expression_p (postfix_expression); | |
4287 | /* The identifier following the `->' or `.' is not qualified. */ | |
4288 | parser->scope = NULL_TREE; | |
4289 | parser->qualifying_scope = NULL_TREE; | |
4290 | parser->object_scope = NULL_TREE; | |
4291 | *idk = CP_ID_KIND_NONE; | |
4292 | /* Enter the scope corresponding to the type of the object | |
4293 | given by the POSTFIX_EXPRESSION. */ | |
4294 | if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE) | |
4295 | { | |
4296 | scope = TREE_TYPE (postfix_expression); | |
4297 | /* According to the standard, no expression should ever have | |
4298 | reference type. Unfortunately, we do not currently match | |
4299 | the standard in this respect in that our internal representation | |
4300 | of an expression may have reference type even when the standard | |
4301 | says it does not. Therefore, we have to manually obtain the | |
4302 | underlying type here. */ | |
4303 | scope = non_reference (scope); | |
4304 | /* The type of the POSTFIX_EXPRESSION must be complete. */ | |
4305 | scope = complete_type_or_else (scope, NULL_TREE); | |
4306 | /* Let the name lookup machinery know that we are processing a | |
4307 | class member access expression. */ | |
4308 | parser->context->object_type = scope; | |
4309 | /* If something went wrong, we want to be able to discern that case, | |
4310 | as opposed to the case where there was no SCOPE due to the type | |
4311 | of expression being dependent. */ | |
4312 | if (!scope) | |
4313 | scope = error_mark_node; | |
4314 | /* If the SCOPE was erroneous, make the various semantic analysis | |
4315 | functions exit quickly -- and without issuing additional error | |
4316 | messages. */ | |
4317 | if (scope == error_mark_node) | |
4318 | postfix_expression = error_mark_node; | |
4319 | } | |
4320 | ||
17a27b4f MM |
4321 | /* Assume this expression is not a pseudo-destructor access. */ |
4322 | pseudo_destructor_p = false; | |
4323 | ||
4324 | /* If the SCOPE is a scalar type, then, if this is a valid program, | |
4325 | we must be looking at a pseudo-destructor-name. */ | |
4326 | if (scope && SCALAR_TYPE_P (scope)) | |
7a3ea201 | 4327 | { |
17a27b4f MM |
4328 | tree s; |
4329 | tree type; | |
4330 | ||
4331 | cp_parser_parse_tentatively (parser); | |
4332 | /* Parse the pseudo-destructor-name. */ | |
4333 | s = NULL_TREE; | |
4334 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
4335 | if (cp_parser_parse_definitely (parser)) | |
4336 | { | |
4337 | pseudo_destructor_p = true; | |
4338 | postfix_expression | |
4339 | = finish_pseudo_destructor_expr (postfix_expression, | |
4340 | s, TREE_TYPE (type)); | |
4341 | } | |
4342 | } | |
4343 | ||
4344 | if (!pseudo_destructor_p) | |
4345 | { | |
4346 | /* If the SCOPE is not a scalar type, we are looking at an | |
4347 | ordinary class member access expression, rather than a | |
4348 | pseudo-destructor-name. */ | |
7a3ea201 RH |
4349 | template_p = cp_parser_optional_template_keyword (parser); |
4350 | /* Parse the id-expression. */ | |
4351 | name = cp_parser_id_expression (parser, template_p, | |
4352 | /*check_dependency_p=*/true, | |
4353 | /*template_p=*/NULL, | |
4354 | /*declarator_p=*/false); | |
4355 | /* In general, build a SCOPE_REF if the member name is qualified. | |
4356 | However, if the name was not dependent and has already been | |
4357 | resolved; there is no need to build the SCOPE_REF. For example; | |
4358 | ||
4359 | struct X { void f(); }; | |
4360 | template <typename T> void f(T* t) { t->X::f(); } | |
4361 | ||
4362 | Even though "t" is dependent, "X::f" is not and has been resolved | |
4363 | to a BASELINK; there is no need to include scope information. */ | |
4364 | ||
4365 | /* But we do need to remember that there was an explicit scope for | |
4366 | virtual function calls. */ | |
4367 | if (parser->scope) | |
4368 | *idk = CP_ID_KIND_QUALIFIED; | |
4369 | ||
fc6a28d7 MM |
4370 | /* If the name is a template-id that names a type, we will get a |
4371 | TYPE_DECL here. That is invalid code. */ | |
4372 | if (TREE_CODE (name) == TYPE_DECL) | |
7a3ea201 | 4373 | { |
fc6a28d7 MM |
4374 | error ("invalid use of %qD", name); |
4375 | postfix_expression = error_mark_node; | |
4376 | } | |
4377 | else | |
4378 | { | |
4379 | if (name != error_mark_node && !BASELINK_P (name) && parser->scope) | |
4380 | { | |
4381 | name = build_nt (SCOPE_REF, parser->scope, name); | |
4382 | parser->scope = NULL_TREE; | |
4383 | parser->qualifying_scope = NULL_TREE; | |
4384 | parser->object_scope = NULL_TREE; | |
4385 | } | |
4386 | if (scope && name && BASELINK_P (name)) | |
4387 | adjust_result_of_qualified_name_lookup | |
4388 | (name, BINFO_TYPE (BASELINK_BINFO (name)), scope); | |
4389 | postfix_expression | |
4390 | = finish_class_member_access_expr (postfix_expression, name); | |
7a3ea201 | 4391 | } |
7a3ea201 | 4392 | } |
7a3ea201 RH |
4393 | |
4394 | /* We no longer need to look up names in the scope of the object on | |
4395 | the left-hand side of the `.' or `->' operator. */ | |
4396 | parser->context->object_type = NULL_TREE; | |
4397 | ||
4398 | /* Outside of offsetof, these operators may not appear in | |
4399 | constant-expressions. */ | |
4400 | if (!for_offsetof | |
98ca843c | 4401 | && (cp_parser_non_integral_constant_expression |
7a3ea201 RH |
4402 | (parser, token_type == CPP_DEREF ? "'->'" : "`.'"))) |
4403 | postfix_expression = error_mark_node; | |
4404 | ||
4405 | return postfix_expression; | |
4406 | } | |
4407 | ||
7efa3e22 | 4408 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
4409 | |
4410 | expression-list: | |
4411 | assignment-expression | |
4412 | expression-list, assignment-expression | |
4413 | ||
7efa3e22 NS |
4414 | attribute-list: |
4415 | expression-list | |
4416 | identifier | |
4417 | identifier, expression-list | |
4418 | ||
93678513 MM |
4419 | CAST_P is true if this expression is the target of a cast. |
4420 | ||
a723baf1 MM |
4421 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
4422 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
4423 | is returned even if there is only a single expression in the list. |
4424 | error_mark_node is returned if the ( and or ) are | |
4425 | missing. NULL_TREE is returned on no expressions. The parentheses | |
4426 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
4427 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
4428 | indicates whether or not all of the expressions in the list were | |
4429 | constant. */ | |
a723baf1 MM |
4430 | |
4431 | static tree | |
21526606 | 4432 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
39703eb9 | 4433 | bool is_attribute_list, |
93678513 | 4434 | bool cast_p, |
39703eb9 | 4435 | bool *non_constant_p) |
a723baf1 MM |
4436 | { |
4437 | tree expression_list = NULL_TREE; | |
1ed3dfd5 | 4438 | bool fold_expr_p = is_attribute_list; |
7efa3e22 | 4439 | tree identifier = NULL_TREE; |
39703eb9 MM |
4440 | |
4441 | /* Assume all the expressions will be constant. */ | |
4442 | if (non_constant_p) | |
4443 | *non_constant_p = false; | |
4444 | ||
7efa3e22 NS |
4445 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
4446 | return error_mark_node; | |
21526606 | 4447 | |
a723baf1 | 4448 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
4449 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
4450 | while (true) | |
4451 | { | |
4452 | tree expr; | |
21526606 | 4453 | |
7efa3e22 NS |
4454 | /* At the beginning of attribute lists, check to see if the |
4455 | next token is an identifier. */ | |
4456 | if (is_attribute_list | |
4457 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4458 | { | |
4459 | cp_token *token; | |
21526606 | 4460 | |
7efa3e22 NS |
4461 | /* Consume the identifier. */ |
4462 | token = cp_lexer_consume_token (parser->lexer); | |
4463 | /* Save the identifier. */ | |
4464 | identifier = token->value; | |
4465 | } | |
4466 | else | |
4467 | { | |
4468 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4469 | if (non_constant_p) |
4470 | { | |
4471 | bool expr_non_constant_p; | |
21526606 | 4472 | expr = (cp_parser_constant_expression |
39703eb9 MM |
4473 | (parser, /*allow_non_constant_p=*/true, |
4474 | &expr_non_constant_p)); | |
4475 | if (expr_non_constant_p) | |
4476 | *non_constant_p = true; | |
4477 | } | |
4478 | else | |
93678513 | 4479 | expr = cp_parser_assignment_expression (parser, cast_p); |
a723baf1 | 4480 | |
1ed3dfd5 GB |
4481 | if (fold_expr_p) |
4482 | expr = fold_non_dependent_expr (expr); | |
4483 | ||
7efa3e22 NS |
4484 | /* Add it to the list. We add error_mark_node |
4485 | expressions to the list, so that we can still tell if | |
4486 | the correct form for a parenthesized expression-list | |
4487 | is found. That gives better errors. */ | |
4488 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4489 | |
7efa3e22 NS |
4490 | if (expr == error_mark_node) |
4491 | goto skip_comma; | |
4492 | } | |
a723baf1 | 4493 | |
7efa3e22 NS |
4494 | /* After the first item, attribute lists look the same as |
4495 | expression lists. */ | |
4496 | is_attribute_list = false; | |
21526606 | 4497 | |
7efa3e22 NS |
4498 | get_comma:; |
4499 | /* If the next token isn't a `,', then we are done. */ | |
4500 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4501 | break; | |
4502 | ||
4503 | /* Otherwise, consume the `,' and keep going. */ | |
4504 | cp_lexer_consume_token (parser->lexer); | |
4505 | } | |
21526606 | 4506 | |
7efa3e22 NS |
4507 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) |
4508 | { | |
4509 | int ending; | |
21526606 | 4510 | |
7efa3e22 NS |
4511 | skip_comma:; |
4512 | /* We try and resync to an unnested comma, as that will give the | |
4513 | user better diagnostics. */ | |
21526606 EC |
4514 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4515 | /*recovering=*/true, | |
4bb8ca28 | 4516 | /*or_comma=*/true, |
a668c6ad | 4517 | /*consume_paren=*/true); |
7efa3e22 NS |
4518 | if (ending < 0) |
4519 | goto get_comma; | |
4520 | if (!ending) | |
4521 | return error_mark_node; | |
a723baf1 MM |
4522 | } |
4523 | ||
4524 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4525 | expression_list = nreverse (expression_list); |
4526 | if (identifier) | |
4527 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
21526606 | 4528 | |
7efa3e22 | 4529 | return expression_list; |
a723baf1 MM |
4530 | } |
4531 | ||
4532 | /* Parse a pseudo-destructor-name. | |
4533 | ||
4534 | pseudo-destructor-name: | |
4535 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4536 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4537 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4538 | ||
4539 | If either of the first two productions is used, sets *SCOPE to the | |
4540 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4541 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
d6e57462 | 4542 | or ERROR_MARK_NODE if the parse fails. */ |
a723baf1 MM |
4543 | |
4544 | static void | |
21526606 EC |
4545 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4546 | tree* scope, | |
94edc4ab | 4547 | tree* type) |
a723baf1 MM |
4548 | { |
4549 | bool nested_name_specifier_p; | |
4550 | ||
b14454ba MM |
4551 | /* Assume that things will not work out. */ |
4552 | *type = error_mark_node; | |
4553 | ||
a723baf1 MM |
4554 | /* Look for the optional `::' operator. */ |
4555 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4556 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 4557 | nested_name_specifier_p |
a723baf1 MM |
4558 | = (cp_parser_nested_name_specifier_opt (parser, |
4559 | /*typename_keyword_p=*/false, | |
4560 | /*check_dependency_p=*/true, | |
a668c6ad | 4561 | /*type_p=*/false, |
21526606 | 4562 | /*is_declaration=*/true) |
a723baf1 MM |
4563 | != NULL_TREE); |
4564 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4565 | second production. */ | |
21526606 | 4566 | if (nested_name_specifier_p |
a723baf1 MM |
4567 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) |
4568 | { | |
4569 | /* Consume the `template' keyword. */ | |
4570 | cp_lexer_consume_token (parser->lexer); | |
4571 | /* Parse the template-id. */ | |
21526606 | 4572 | cp_parser_template_id (parser, |
a723baf1 | 4573 | /*template_keyword_p=*/true, |
a668c6ad MM |
4574 | /*check_dependency_p=*/false, |
4575 | /*is_declaration=*/true); | |
a723baf1 MM |
4576 | /* Look for the `::' token. */ |
4577 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4578 | } | |
4579 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4580 | additional qualification. */ |
a723baf1 MM |
4581 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4582 | { | |
4583 | /* Look for the type-name. */ | |
4584 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
d6e57462 | 4585 | |
b14454ba MM |
4586 | if (*scope == error_mark_node) |
4587 | return; | |
4588 | ||
4589 | /* If we don't have ::~, then something has gone wrong. Since | |
4590 | the only caller of this function is looking for something | |
4591 | after `.' or `->' after a scalar type, most likely the | |
4592 | program is trying to get a member of a non-aggregate | |
4593 | type. */ | |
4594 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE) | |
d6e57462 ILT |
4595 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL) |
4596 | { | |
4597 | cp_parser_error (parser, "request for member of non-aggregate type"); | |
d6e57462 ILT |
4598 | return; |
4599 | } | |
4600 | ||
a723baf1 MM |
4601 | /* Look for the `::' token. */ |
4602 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4603 | } | |
4604 | else | |
4605 | *scope = NULL_TREE; | |
4606 | ||
4607 | /* Look for the `~'. */ | |
4608 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4609 | /* Look for the type-name again. We are not responsible for | |
4610 | checking that it matches the first type-name. */ | |
4611 | *type = cp_parser_type_name (parser); | |
4612 | } | |
4613 | ||
4614 | /* Parse a unary-expression. | |
4615 | ||
4616 | unary-expression: | |
4617 | postfix-expression | |
4618 | ++ cast-expression | |
4619 | -- cast-expression | |
4620 | unary-operator cast-expression | |
4621 | sizeof unary-expression | |
4622 | sizeof ( type-id ) | |
4623 | new-expression | |
4624 | delete-expression | |
4625 | ||
4626 | GNU Extensions: | |
4627 | ||
4628 | unary-expression: | |
4629 | __extension__ cast-expression | |
4630 | __alignof__ unary-expression | |
4631 | __alignof__ ( type-id ) | |
4632 | __real__ cast-expression | |
4633 | __imag__ cast-expression | |
4634 | && identifier | |
4635 | ||
4636 | ADDRESS_P is true iff the unary-expression is appearing as the | |
93678513 MM |
4637 | operand of the `&' operator. CAST_P is true if this expression is |
4638 | the target of a cast. | |
a723baf1 | 4639 | |
34cd5ae7 | 4640 | Returns a representation of the expression. */ |
a723baf1 MM |
4641 | |
4642 | static tree | |
93678513 | 4643 | cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
4644 | { |
4645 | cp_token *token; | |
4646 | enum tree_code unary_operator; | |
4647 | ||
4648 | /* Peek at the next token. */ | |
4649 | token = cp_lexer_peek_token (parser->lexer); | |
4650 | /* Some keywords give away the kind of expression. */ | |
4651 | if (token->type == CPP_KEYWORD) | |
4652 | { | |
4653 | enum rid keyword = token->keyword; | |
4654 | ||
4655 | switch (keyword) | |
4656 | { | |
4657 | case RID_ALIGNOF: | |
a723baf1 MM |
4658 | case RID_SIZEOF: |
4659 | { | |
4660 | tree operand; | |
7a18b933 | 4661 | enum tree_code op; |
21526606 | 4662 | |
7a18b933 NS |
4663 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4664 | /* Consume the token. */ | |
a723baf1 MM |
4665 | cp_lexer_consume_token (parser->lexer); |
4666 | /* Parse the operand. */ | |
4667 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4668 | ||
7a18b933 NS |
4669 | if (TYPE_P (operand)) |
4670 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4671 | else |
7a18b933 | 4672 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4673 | } |
4674 | ||
4675 | case RID_NEW: | |
4676 | return cp_parser_new_expression (parser); | |
4677 | ||
4678 | case RID_DELETE: | |
4679 | return cp_parser_delete_expression (parser); | |
21526606 | 4680 | |
a723baf1 MM |
4681 | case RID_EXTENSION: |
4682 | { | |
4683 | /* The saved value of the PEDANTIC flag. */ | |
4684 | int saved_pedantic; | |
4685 | tree expr; | |
4686 | ||
4687 | /* Save away the PEDANTIC flag. */ | |
4688 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4689 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4690 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4691 | /* Restore the PEDANTIC flag. */ |
4692 | pedantic = saved_pedantic; | |
4693 | ||
4694 | return expr; | |
4695 | } | |
4696 | ||
4697 | case RID_REALPART: | |
4698 | case RID_IMAGPART: | |
4699 | { | |
4700 | tree expression; | |
4701 | ||
4702 | /* Consume the `__real__' or `__imag__' token. */ | |
4703 | cp_lexer_consume_token (parser->lexer); | |
4704 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4705 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4706 | /* Create the complete representation. */ |
4707 | return build_x_unary_op ((keyword == RID_REALPART | |
4708 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4709 | expression); | |
4710 | } | |
4711 | break; | |
4712 | ||
4713 | default: | |
4714 | break; | |
4715 | } | |
4716 | } | |
4717 | ||
4718 | /* Look for the `:: new' and `:: delete', which also signal the | |
4719 | beginning of a new-expression, or delete-expression, | |
4720 | respectively. If the next token is `::', then it might be one of | |
4721 | these. */ | |
4722 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4723 | { | |
4724 | enum rid keyword; | |
4725 | ||
4726 | /* See if the token after the `::' is one of the keywords in | |
4727 | which we're interested. */ | |
4728 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4729 | /* If it's `new', we have a new-expression. */ | |
4730 | if (keyword == RID_NEW) | |
4731 | return cp_parser_new_expression (parser); | |
4732 | /* Similarly, for `delete'. */ | |
4733 | else if (keyword == RID_DELETE) | |
4734 | return cp_parser_delete_expression (parser); | |
4735 | } | |
4736 | ||
4737 | /* Look for a unary operator. */ | |
4738 | unary_operator = cp_parser_unary_operator (token); | |
4739 | /* The `++' and `--' operators can be handled similarly, even though | |
4740 | they are not technically unary-operators in the grammar. */ | |
4741 | if (unary_operator == ERROR_MARK) | |
4742 | { | |
4743 | if (token->type == CPP_PLUS_PLUS) | |
4744 | unary_operator = PREINCREMENT_EXPR; | |
4745 | else if (token->type == CPP_MINUS_MINUS) | |
4746 | unary_operator = PREDECREMENT_EXPR; | |
4747 | /* Handle the GNU address-of-label extension. */ | |
4748 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4749 | && token->type == CPP_AND_AND) | |
4750 | { | |
4751 | tree identifier; | |
4752 | ||
4753 | /* Consume the '&&' token. */ | |
4754 | cp_lexer_consume_token (parser->lexer); | |
4755 | /* Look for the identifier. */ | |
4756 | identifier = cp_parser_identifier (parser); | |
4757 | /* Create an expression representing the address. */ | |
4758 | return finish_label_address_expr (identifier); | |
4759 | } | |
4760 | } | |
4761 | if (unary_operator != ERROR_MARK) | |
4762 | { | |
4763 | tree cast_expression; | |
a5ac3982 MM |
4764 | tree expression = error_mark_node; |
4765 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4766 | |
4767 | /* Consume the operator token. */ | |
4768 | token = cp_lexer_consume_token (parser->lexer); | |
4769 | /* Parse the cast-expression. */ | |
21526606 | 4770 | cast_expression |
93678513 MM |
4771 | = cp_parser_cast_expression (parser, |
4772 | unary_operator == ADDR_EXPR, | |
4773 | /*cast_p=*/false); | |
a723baf1 MM |
4774 | /* Now, build an appropriate representation. */ |
4775 | switch (unary_operator) | |
4776 | { | |
4777 | case INDIRECT_REF: | |
a5ac3982 MM |
4778 | non_constant_p = "`*'"; |
4779 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4780 | break; | |
4781 | ||
a723baf1 | 4782 | case ADDR_EXPR: |
7a3ea201 | 4783 | non_constant_p = "`&'"; |
a5ac3982 | 4784 | /* Fall through. */ |
d17811fd | 4785 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4786 | expression = build_x_unary_op (unary_operator, cast_expression); |
4787 | break; | |
4788 | ||
14d22dd6 MM |
4789 | case PREINCREMENT_EXPR: |
4790 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4791 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4792 | ? "`++'" : "`--'"); | |
14d22dd6 | 4793 | /* Fall through. */ |
a723baf1 MM |
4794 | case CONVERT_EXPR: |
4795 | case NEGATE_EXPR: | |
4796 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4797 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4798 | break; | |
a723baf1 | 4799 | |
a723baf1 | 4800 | default: |
315fb5db | 4801 | gcc_unreachable (); |
a723baf1 | 4802 | } |
a5ac3982 | 4803 | |
98ca843c | 4804 | if (non_constant_p |
625cbf93 MM |
4805 | && cp_parser_non_integral_constant_expression (parser, |
4806 | non_constant_p)) | |
4807 | expression = error_mark_node; | |
a5ac3982 MM |
4808 | |
4809 | return expression; | |
a723baf1 MM |
4810 | } |
4811 | ||
93678513 | 4812 | return cp_parser_postfix_expression (parser, address_p, cast_p); |
a723baf1 MM |
4813 | } |
4814 | ||
4815 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4816 | unary-operator, the corresponding tree code is returned. */ | |
4817 | ||
4818 | static enum tree_code | |
94edc4ab | 4819 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4820 | { |
4821 | switch (token->type) | |
4822 | { | |
4823 | case CPP_MULT: | |
4824 | return INDIRECT_REF; | |
4825 | ||
4826 | case CPP_AND: | |
4827 | return ADDR_EXPR; | |
4828 | ||
4829 | case CPP_PLUS: | |
4830 | return CONVERT_EXPR; | |
4831 | ||
4832 | case CPP_MINUS: | |
4833 | return NEGATE_EXPR; | |
4834 | ||
4835 | case CPP_NOT: | |
4836 | return TRUTH_NOT_EXPR; | |
21526606 | 4837 | |
a723baf1 MM |
4838 | case CPP_COMPL: |
4839 | return BIT_NOT_EXPR; | |
4840 | ||
4841 | default: | |
4842 | return ERROR_MARK; | |
4843 | } | |
4844 | } | |
4845 | ||
4846 | /* Parse a new-expression. | |
4847 | ||
ca099ac8 | 4848 | new-expression: |
a723baf1 MM |
4849 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4850 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4851 | ||
4852 | Returns a representation of the expression. */ | |
4853 | ||
4854 | static tree | |
94edc4ab | 4855 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4856 | { |
4857 | bool global_scope_p; | |
4858 | tree placement; | |
4859 | tree type; | |
4860 | tree initializer; | |
058b15c1 | 4861 | tree nelts; |
a723baf1 MM |
4862 | |
4863 | /* Look for the optional `::' operator. */ | |
21526606 | 4864 | global_scope_p |
a723baf1 MM |
4865 | = (cp_parser_global_scope_opt (parser, |
4866 | /*current_scope_valid_p=*/false) | |
4867 | != NULL_TREE); | |
4868 | /* Look for the `new' operator. */ | |
4869 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4870 | /* There's no easy way to tell a new-placement from the | |
4871 | `( type-id )' construct. */ | |
4872 | cp_parser_parse_tentatively (parser); | |
4873 | /* Look for a new-placement. */ | |
4874 | placement = cp_parser_new_placement (parser); | |
4875 | /* If that didn't work out, there's no new-placement. */ | |
4876 | if (!cp_parser_parse_definitely (parser)) | |
4877 | placement = NULL_TREE; | |
4878 | ||
4879 | /* If the next token is a `(', then we have a parenthesized | |
4880 | type-id. */ | |
4881 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4882 | { | |
4883 | /* Consume the `('. */ | |
4884 | cp_lexer_consume_token (parser->lexer); | |
4885 | /* Parse the type-id. */ | |
4886 | type = cp_parser_type_id (parser); | |
4887 | /* Look for the closing `)'. */ | |
4888 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
98ca843c | 4889 | /* There should not be a direct-new-declarator in this production, |
063e900f GB |
4890 | but GCC used to allowed this, so we check and emit a sensible error |
4891 | message for this case. */ | |
4892 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
0da99d4e GB |
4893 | { |
4894 | error ("array bound forbidden after parenthesized type-id"); | |
4895 | inform ("try removing the parentheses around the type-id"); | |
063e900f GB |
4896 | cp_parser_direct_new_declarator (parser); |
4897 | } | |
17a27b4f | 4898 | nelts = NULL_TREE; |
a723baf1 MM |
4899 | } |
4900 | /* Otherwise, there must be a new-type-id. */ | |
4901 | else | |
058b15c1 | 4902 | type = cp_parser_new_type_id (parser, &nelts); |
a723baf1 MM |
4903 | |
4904 | /* If the next token is a `(', then we have a new-initializer. */ | |
4905 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4906 | initializer = cp_parser_new_initializer (parser); | |
4907 | else | |
4908 | initializer = NULL_TREE; | |
4909 | ||
625cbf93 MM |
4910 | /* A new-expression may not appear in an integral constant |
4911 | expression. */ | |
4912 | if (cp_parser_non_integral_constant_expression (parser, "`new'")) | |
4913 | return error_mark_node; | |
4914 | ||
a723baf1 | 4915 | /* Create a representation of the new-expression. */ |
058b15c1 | 4916 | return build_new (placement, type, nelts, initializer, global_scope_p); |
a723baf1 MM |
4917 | } |
4918 | ||
4919 | /* Parse a new-placement. | |
4920 | ||
4921 | new-placement: | |
4922 | ( expression-list ) | |
4923 | ||
4924 | Returns the same representation as for an expression-list. */ | |
4925 | ||
4926 | static tree | |
94edc4ab | 4927 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4928 | { |
4929 | tree expression_list; | |
4930 | ||
a723baf1 | 4931 | /* Parse the expression-list. */ |
21526606 | 4932 | expression_list = (cp_parser_parenthesized_expression_list |
93678513 MM |
4933 | (parser, false, /*cast_p=*/false, |
4934 | /*non_constant_p=*/NULL)); | |
a723baf1 MM |
4935 | |
4936 | return expression_list; | |
4937 | } | |
4938 | ||
4939 | /* Parse a new-type-id. | |
4940 | ||
4941 | new-type-id: | |
4942 | type-specifier-seq new-declarator [opt] | |
4943 | ||
058b15c1 MM |
4944 | Returns the TYPE allocated. If the new-type-id indicates an array |
4945 | type, *NELTS is set to the number of elements in the last array | |
4946 | bound; the TYPE will not include the last array bound. */ | |
a723baf1 MM |
4947 | |
4948 | static tree | |
058b15c1 | 4949 | cp_parser_new_type_id (cp_parser* parser, tree *nelts) |
a723baf1 | 4950 | { |
62d1db17 | 4951 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 MM |
4952 | cp_declarator *new_declarator; |
4953 | cp_declarator *declarator; | |
4954 | cp_declarator *outer_declarator; | |
a723baf1 | 4955 | const char *saved_message; |
058b15c1 | 4956 | tree type; |
a723baf1 MM |
4957 | |
4958 | /* The type-specifier sequence must not contain type definitions. | |
4959 | (It cannot contain declarations of new types either, but if they | |
4960 | are not definitions we will catch that because they are not | |
4961 | complete.) */ | |
4962 | saved_message = parser->type_definition_forbidden_message; | |
4963 | parser->type_definition_forbidden_message | |
4964 | = "types may not be defined in a new-type-id"; | |
4965 | /* Parse the type-specifier-seq. */ | |
62d1db17 | 4966 | cp_parser_type_specifier_seq (parser, &type_specifier_seq); |
a723baf1 MM |
4967 | /* Restore the old message. */ |
4968 | parser->type_definition_forbidden_message = saved_message; | |
4969 | /* Parse the new-declarator. */ | |
058b15c1 MM |
4970 | new_declarator = cp_parser_new_declarator_opt (parser); |
4971 | ||
4972 | /* Determine the number of elements in the last array dimension, if | |
4973 | any. */ | |
4974 | *nelts = NULL_TREE; | |
4975 | /* Skip down to the last array dimension. */ | |
4976 | declarator = new_declarator; | |
4977 | outer_declarator = NULL; | |
4978 | while (declarator && (declarator->kind == cdk_pointer | |
4979 | || declarator->kind == cdk_ptrmem)) | |
4980 | { | |
4981 | outer_declarator = declarator; | |
4982 | declarator = declarator->declarator; | |
4983 | } | |
98ca843c | 4984 | while (declarator |
058b15c1 MM |
4985 | && declarator->kind == cdk_array |
4986 | && declarator->declarator | |
4987 | && declarator->declarator->kind == cdk_array) | |
4988 | { | |
4989 | outer_declarator = declarator; | |
4990 | declarator = declarator->declarator; | |
4991 | } | |
98ca843c | 4992 | |
058b15c1 MM |
4993 | if (declarator && declarator->kind == cdk_array) |
4994 | { | |
4995 | *nelts = declarator->u.array.bounds; | |
4996 | if (*nelts == error_mark_node) | |
4997 | *nelts = integer_one_node; | |
ad1063d5 | 4998 | |
058b15c1 MM |
4999 | if (outer_declarator) |
5000 | outer_declarator->declarator = declarator->declarator; | |
5001 | else | |
5002 | new_declarator = NULL; | |
5003 | } | |
a723baf1 | 5004 | |
62d1db17 | 5005 | type = groktypename (&type_specifier_seq, new_declarator); |
058b15c1 MM |
5006 | if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE) |
5007 | { | |
5008 | *nelts = array_type_nelts_top (type); | |
5009 | type = TREE_TYPE (type); | |
5010 | } | |
5011 | return type; | |
a723baf1 MM |
5012 | } |
5013 | ||
5014 | /* Parse an (optional) new-declarator. | |
5015 | ||
5016 | new-declarator: | |
5017 | ptr-operator new-declarator [opt] | |
5018 | direct-new-declarator | |
5019 | ||
058b15c1 | 5020 | Returns the declarator. */ |
a723baf1 | 5021 | |
058b15c1 | 5022 | static cp_declarator * |
94edc4ab | 5023 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
5024 | { |
5025 | enum tree_code code; | |
5026 | tree type; | |
3c01e5df | 5027 | cp_cv_quals cv_quals; |
a723baf1 MM |
5028 | |
5029 | /* We don't know if there's a ptr-operator next, or not. */ | |
5030 | cp_parser_parse_tentatively (parser); | |
5031 | /* Look for a ptr-operator. */ | |
3c01e5df | 5032 | code = cp_parser_ptr_operator (parser, &type, &cv_quals); |
a723baf1 MM |
5033 | /* If that worked, look for more new-declarators. */ |
5034 | if (cp_parser_parse_definitely (parser)) | |
5035 | { | |
058b15c1 | 5036 | cp_declarator *declarator; |
a723baf1 MM |
5037 | |
5038 | /* Parse another optional declarator. */ | |
5039 | declarator = cp_parser_new_declarator_opt (parser); | |
5040 | ||
5041 | /* Create the representation of the declarator. */ | |
058b15c1 | 5042 | if (type) |
3c01e5df | 5043 | declarator = make_ptrmem_declarator (cv_quals, type, declarator); |
058b15c1 | 5044 | else if (code == INDIRECT_REF) |
3c01e5df | 5045 | declarator = make_pointer_declarator (cv_quals, declarator); |
a723baf1 | 5046 | else |
3c01e5df | 5047 | declarator = make_reference_declarator (cv_quals, declarator); |
a723baf1 | 5048 | |
a723baf1 MM |
5049 | return declarator; |
5050 | } | |
5051 | ||
5052 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
5053 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5054 | return cp_parser_direct_new_declarator (parser); | |
5055 | ||
058b15c1 | 5056 | return NULL; |
a723baf1 MM |
5057 | } |
5058 | ||
5059 | /* Parse a direct-new-declarator. | |
5060 | ||
5061 | direct-new-declarator: | |
5062 | [ expression ] | |
21526606 | 5063 | direct-new-declarator [constant-expression] |
a723baf1 | 5064 | |
058b15c1 | 5065 | */ |
a723baf1 | 5066 | |
058b15c1 | 5067 | static cp_declarator * |
94edc4ab | 5068 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 | 5069 | { |
058b15c1 | 5070 | cp_declarator *declarator = NULL; |
a723baf1 MM |
5071 | |
5072 | while (true) | |
5073 | { | |
5074 | tree expression; | |
5075 | ||
5076 | /* Look for the opening `['. */ | |
5077 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
5078 | /* The first expression is not required to be constant. */ | |
5079 | if (!declarator) | |
5080 | { | |
93678513 | 5081 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
5082 | /* The standard requires that the expression have integral |
5083 | type. DR 74 adds enumeration types. We believe that the | |
5084 | real intent is that these expressions be handled like the | |
5085 | expression in a `switch' condition, which also allows | |
5086 | classes with a single conversion to integral or | |
5087 | enumeration type. */ | |
5088 | if (!processing_template_decl) | |
5089 | { | |
21526606 | 5090 | expression |
a723baf1 MM |
5091 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
5092 | expression, | |
b746c5dc | 5093 | /*complain=*/true); |
a723baf1 MM |
5094 | if (!expression) |
5095 | { | |
2a13a625 GDR |
5096 | error ("expression in new-declarator must have integral " |
5097 | "or enumeration type"); | |
a723baf1 MM |
5098 | expression = error_mark_node; |
5099 | } | |
5100 | } | |
5101 | } | |
5102 | /* But all the other expressions must be. */ | |
5103 | else | |
21526606 EC |
5104 | expression |
5105 | = cp_parser_constant_expression (parser, | |
14d22dd6 MM |
5106 | /*allow_non_constant=*/false, |
5107 | NULL); | |
a723baf1 MM |
5108 | /* Look for the closing `]'. */ |
5109 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
5110 | ||
5111 | /* Add this bound to the declarator. */ | |
058b15c1 | 5112 | declarator = make_array_declarator (declarator, expression); |
a723baf1 MM |
5113 | |
5114 | /* If the next token is not a `[', then there are no more | |
5115 | bounds. */ | |
5116 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
5117 | break; | |
5118 | } | |
5119 | ||
5120 | return declarator; | |
5121 | } | |
5122 | ||
5123 | /* Parse a new-initializer. | |
5124 | ||
5125 | new-initializer: | |
5126 | ( expression-list [opt] ) | |
5127 | ||
34cd5ae7 | 5128 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
5129 | expression-list, VOID_ZERO_NODE is returned. */ |
5130 | ||
5131 | static tree | |
94edc4ab | 5132 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
5133 | { |
5134 | tree expression_list; | |
5135 | ||
21526606 | 5136 | expression_list = (cp_parser_parenthesized_expression_list |
93678513 MM |
5137 | (parser, false, /*cast_p=*/false, |
5138 | /*non_constant_p=*/NULL)); | |
7efa3e22 | 5139 | if (!expression_list) |
a723baf1 | 5140 | expression_list = void_zero_node; |
a723baf1 MM |
5141 | |
5142 | return expression_list; | |
5143 | } | |
5144 | ||
5145 | /* Parse a delete-expression. | |
5146 | ||
5147 | delete-expression: | |
5148 | :: [opt] delete cast-expression | |
5149 | :: [opt] delete [ ] cast-expression | |
5150 | ||
5151 | Returns a representation of the expression. */ | |
5152 | ||
5153 | static tree | |
94edc4ab | 5154 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
5155 | { |
5156 | bool global_scope_p; | |
5157 | bool array_p; | |
5158 | tree expression; | |
5159 | ||
5160 | /* Look for the optional `::' operator. */ | |
5161 | global_scope_p | |
5162 | = (cp_parser_global_scope_opt (parser, | |
5163 | /*current_scope_valid_p=*/false) | |
5164 | != NULL_TREE); | |
5165 | /* Look for the `delete' keyword. */ | |
5166 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
5167 | /* See if the array syntax is in use. */ | |
5168 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
5169 | { | |
5170 | /* Consume the `[' token. */ | |
5171 | cp_lexer_consume_token (parser->lexer); | |
5172 | /* Look for the `]' token. */ | |
5173 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
5174 | /* Remember that this is the `[]' construct. */ | |
5175 | array_p = true; | |
5176 | } | |
5177 | else | |
5178 | array_p = false; | |
5179 | ||
5180 | /* Parse the cast-expression. */ | |
d6b4ea85 | 5181 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 | 5182 | |
625cbf93 MM |
5183 | /* A delete-expression may not appear in an integral constant |
5184 | expression. */ | |
5185 | if (cp_parser_non_integral_constant_expression (parser, "`delete'")) | |
5186 | return error_mark_node; | |
5187 | ||
a723baf1 MM |
5188 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); |
5189 | } | |
5190 | ||
5191 | /* Parse a cast-expression. | |
5192 | ||
5193 | cast-expression: | |
5194 | unary-expression | |
5195 | ( type-id ) cast-expression | |
5196 | ||
93678513 MM |
5197 | ADDRESS_P is true iff the unary-expression is appearing as the |
5198 | operand of the `&' operator. CAST_P is true if this expression is | |
5199 | the target of a cast. | |
5200 | ||
a723baf1 MM |
5201 | Returns a representation of the expression. */ |
5202 | ||
5203 | static tree | |
93678513 | 5204 | cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p) |
a723baf1 MM |
5205 | { |
5206 | /* If it's a `(', then we might be looking at a cast. */ | |
5207 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
5208 | { | |
5209 | tree type = NULL_TREE; | |
5210 | tree expr = NULL_TREE; | |
5211 | bool compound_literal_p; | |
5212 | const char *saved_message; | |
5213 | ||
5214 | /* There's no way to know yet whether or not this is a cast. | |
5215 | For example, `(int (3))' is a unary-expression, while `(int) | |
5216 | 3' is a cast. So, we resort to parsing tentatively. */ | |
5217 | cp_parser_parse_tentatively (parser); | |
5218 | /* Types may not be defined in a cast. */ | |
5219 | saved_message = parser->type_definition_forbidden_message; | |
5220 | parser->type_definition_forbidden_message | |
5221 | = "types may not be defined in casts"; | |
5222 | /* Consume the `('. */ | |
5223 | cp_lexer_consume_token (parser->lexer); | |
5224 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
5225 | compound-literal (which we permit in C++ as an extension). | |
5226 | But, that construct is not a cast-expression -- it is a | |
5227 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
5228 | is legal; if the compound-literal were a cast-expression, | |
5229 | you'd need an extra set of parentheses.) But, if we parse | |
5230 | the type-id, and it happens to be a class-specifier, then we | |
5231 | will commit to the parse at that point, because we cannot | |
5232 | undo the action that is done when creating a new class. So, | |
21526606 | 5233 | then we cannot back up and do a postfix-expression. |
a723baf1 MM |
5234 | |
5235 | Therefore, we scan ahead to the closing `)', and check to see | |
5236 | if the token after the `)' is a `{'. If so, we are not | |
21526606 | 5237 | looking at a cast-expression. |
a723baf1 MM |
5238 | |
5239 | Save tokens so that we can put them back. */ | |
5240 | cp_lexer_save_tokens (parser->lexer); | |
5241 | /* Skip tokens until the next token is a closing parenthesis. | |
5242 | If we find the closing `)', and the next token is a `{', then | |
5243 | we are looking at a compound-literal. */ | |
21526606 | 5244 | compound_literal_p |
a668c6ad MM |
5245 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
5246 | /*consume_paren=*/true) | |
a723baf1 MM |
5247 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
5248 | /* Roll back the tokens we skipped. */ | |
5249 | cp_lexer_rollback_tokens (parser->lexer); | |
5250 | /* If we were looking at a compound-literal, simulate an error | |
5251 | so that the call to cp_parser_parse_definitely below will | |
5252 | fail. */ | |
5253 | if (compound_literal_p) | |
5254 | cp_parser_simulate_error (parser); | |
5255 | else | |
5256 | { | |
4f8163b1 MM |
5257 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
5258 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
5259 | /* Look for the type-id. */ |
5260 | type = cp_parser_type_id (parser); | |
5261 | /* Look for the closing `)'. */ | |
5262 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4f8163b1 | 5263 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
5264 | } |
5265 | ||
5266 | /* Restore the saved message. */ | |
5267 | parser->type_definition_forbidden_message = saved_message; | |
5268 | ||
bbaab916 NS |
5269 | /* If ok so far, parse the dependent expression. We cannot be |
5270 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
5271 | ctor of T, but looks like a cast to function returning T | |
5272 | without a dependent expression. */ | |
5273 | if (!cp_parser_error_occurred (parser)) | |
93678513 MM |
5274 | expr = cp_parser_cast_expression (parser, |
5275 | /*address_p=*/false, | |
5276 | /*cast_p=*/true); | |
bbaab916 | 5277 | |
a723baf1 MM |
5278 | if (cp_parser_parse_definitely (parser)) |
5279 | { | |
a723baf1 | 5280 | /* Warn about old-style casts, if so requested. */ |
21526606 EC |
5281 | if (warn_old_style_cast |
5282 | && !in_system_header | |
5283 | && !VOID_TYPE_P (type) | |
a723baf1 MM |
5284 | && current_lang_name != lang_name_c) |
5285 | warning ("use of old-style cast"); | |
14d22dd6 MM |
5286 | |
5287 | /* Only type conversions to integral or enumeration types | |
5288 | can be used in constant-expressions. */ | |
67c03833 | 5289 | if (parser->integral_constant_expression_p |
14d22dd6 | 5290 | && !dependent_type_p (type) |
625cbf93 | 5291 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
98ca843c | 5292 | && (cp_parser_non_integral_constant_expression |
625cbf93 MM |
5293 | (parser, |
5294 | "a cast to a type other than an integral or " | |
5295 | "enumeration type"))) | |
5296 | return error_mark_node; | |
5297 | ||
a723baf1 MM |
5298 | /* Perform the cast. */ |
5299 | expr = build_c_cast (type, expr); | |
bbaab916 | 5300 | return expr; |
a723baf1 | 5301 | } |
a723baf1 MM |
5302 | } |
5303 | ||
5304 | /* If we get here, then it's not a cast, so it must be a | |
5305 | unary-expression. */ | |
93678513 | 5306 | return cp_parser_unary_expression (parser, address_p, cast_p); |
a723baf1 MM |
5307 | } |
5308 | ||
b8b94c5b | 5309 | /* Parse a binary expression of the general form: |
a723baf1 MM |
5310 | |
5311 | pm-expression: | |
5312 | cast-expression | |
5313 | pm-expression .* cast-expression | |
5314 | pm-expression ->* cast-expression | |
5315 | ||
77077b39 | 5316 | multiplicative-expression: |
a723baf1 MM |
5317 | pm-expression |
5318 | multiplicative-expression * pm-expression | |
5319 | multiplicative-expression / pm-expression | |
5320 | multiplicative-expression % pm-expression | |
5321 | ||
a723baf1 MM |
5322 | additive-expression: |
5323 | multiplicative-expression | |
5324 | additive-expression + multiplicative-expression | |
5325 | additive-expression - multiplicative-expression | |
5326 | ||
a723baf1 MM |
5327 | shift-expression: |
5328 | additive-expression | |
5329 | shift-expression << additive-expression | |
5330 | shift-expression >> additive-expression | |
5331 | ||
a723baf1 MM |
5332 | relational-expression: |
5333 | shift-expression | |
5334 | relational-expression < shift-expression | |
5335 | relational-expression > shift-expression | |
5336 | relational-expression <= shift-expression | |
5337 | relational-expression >= shift-expression | |
5338 | ||
b8b94c5b PB |
5339 | GNU Extension: |
5340 | ||
a723baf1 MM |
5341 | relational-expression: |
5342 | relational-expression <? shift-expression | |
5343 | relational-expression >? shift-expression | |
5344 | ||
a723baf1 MM |
5345 | equality-expression: |
5346 | relational-expression | |
5347 | equality-expression == relational-expression | |
5348 | equality-expression != relational-expression | |
5349 | ||
a723baf1 MM |
5350 | and-expression: |
5351 | equality-expression | |
5352 | and-expression & equality-expression | |
5353 | ||
a723baf1 MM |
5354 | exclusive-or-expression: |
5355 | and-expression | |
5356 | exclusive-or-expression ^ and-expression | |
5357 | ||
b8b94c5b PB |
5358 | inclusive-or-expression: |
5359 | exclusive-or-expression | |
5360 | inclusive-or-expression | exclusive-or-expression | |
a723baf1 | 5361 | |
b8b94c5b PB |
5362 | logical-and-expression: |
5363 | inclusive-or-expression | |
5364 | logical-and-expression && inclusive-or-expression | |
a723baf1 | 5365 | |
b8b94c5b PB |
5366 | logical-or-expression: |
5367 | logical-and-expression | |
5368 | logical-or-expression || logical-and-expression | |
a723baf1 | 5369 | |
b8b94c5b | 5370 | All these are implemented with a single function like: |
a723baf1 | 5371 | |
b8b94c5b PB |
5372 | binary-expression: |
5373 | simple-cast-expression | |
5374 | binary-expression <token> binary-expression | |
a723baf1 | 5375 | |
93678513 MM |
5376 | CAST_P is true if this expression is the target of a cast. |
5377 | ||
b8b94c5b PB |
5378 | The binops_by_token map is used to get the tree codes for each <token> type. |
5379 | binary-expressions are associated according to a precedence table. */ | |
a723baf1 | 5380 | |
b8b94c5b PB |
5381 | #define TOKEN_PRECEDENCE(token) \ |
5382 | ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \ | |
5383 | ? PREC_NOT_OPERATOR \ | |
5384 | : binops_by_token[token->type].prec) | |
a723baf1 MM |
5385 | |
5386 | static tree | |
93678513 | 5387 | cp_parser_binary_expression (cp_parser* parser, bool cast_p) |
a723baf1 | 5388 | { |
b8b94c5b PB |
5389 | cp_parser_expression_stack stack; |
5390 | cp_parser_expression_stack_entry *sp = &stack[0]; | |
5391 | tree lhs, rhs; | |
5392 | cp_token *token; | |
5393 | enum tree_code tree_type; | |
5394 | enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec; | |
5395 | bool overloaded_p; | |
a723baf1 | 5396 | |
b8b94c5b | 5397 | /* Parse the first expression. */ |
93678513 | 5398 | lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p); |
a723baf1 | 5399 | |
b8b94c5b PB |
5400 | for (;;) |
5401 | { | |
5402 | /* Get an operator token. */ | |
5403 | token = cp_lexer_peek_token (parser->lexer); | |
5404 | new_prec = TOKEN_PRECEDENCE (token); | |
5405 | ||
5406 | /* Popping an entry off the stack means we completed a subexpression: | |
5407 | - either we found a token which is not an operator (`>' where it is not | |
5408 | an operator, or prec == PREC_NOT_OPERATOR), in which case popping | |
5409 | will happen repeatedly; | |
5410 | - or, we found an operator which has lower priority. This is the case | |
5411 | where the recursive descent *ascends*, as in `3 * 4 + 5' after | |
03fd3f84 | 5412 | parsing `3 * 4'. */ |
b8b94c5b PB |
5413 | if (new_prec <= prec) |
5414 | { | |
5415 | if (sp == stack) | |
5416 | break; | |
5417 | else | |
5418 | goto pop; | |
5419 | } | |
a723baf1 | 5420 | |
b8b94c5b PB |
5421 | get_rhs: |
5422 | tree_type = binops_by_token[token->type].tree_type; | |
a723baf1 | 5423 | |
03fd3f84 | 5424 | /* We used the operator token. */ |
b8b94c5b | 5425 | cp_lexer_consume_token (parser->lexer); |
a723baf1 | 5426 | |
b8b94c5b PB |
5427 | /* Extract another operand. It may be the RHS of this expression |
5428 | or the LHS of a new, higher priority expression. */ | |
5429 | rhs = cp_parser_simple_cast_expression (parser); | |
a723baf1 | 5430 | |
b8b94c5b PB |
5431 | /* Get another operator token. Look up its precedence to avoid |
5432 | building a useless (immediately popped) stack entry for common | |
03fd3f84 | 5433 | cases such as 3 + 4 + 5 or 3 * 4 + 5. */ |
b8b94c5b PB |
5434 | token = cp_lexer_peek_token (parser->lexer); |
5435 | lookahead_prec = TOKEN_PRECEDENCE (token); | |
5436 | if (lookahead_prec > new_prec) | |
5437 | { | |
5438 | /* ... and prepare to parse the RHS of the new, higher priority | |
43c2a69a PB |
5439 | expression. Since precedence levels on the stack are |
5440 | monotonically increasing, we do not have to care about | |
5441 | stack overflows. */ | |
b8b94c5b PB |
5442 | sp->prec = prec; |
5443 | sp->tree_type = tree_type; | |
5444 | sp->lhs = lhs; | |
5445 | sp++; | |
5446 | lhs = rhs; | |
5447 | prec = new_prec; | |
5448 | new_prec = lookahead_prec; | |
5449 | goto get_rhs; | |
5450 | ||
5451 | pop: | |
5452 | /* If the stack is not empty, we have parsed into LHS the right side | |
5453 | (`4' in the example above) of an expression we had suspended. | |
5454 | We can use the information on the stack to recover the LHS (`3') | |
5455 | from the stack together with the tree code (`MULT_EXPR'), and | |
5456 | the precedence of the higher level subexpression | |
5457 | (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token, | |
5458 | which will be used to actually build the additive expression. */ | |
5459 | --sp; | |
5460 | prec = sp->prec; | |
5461 | tree_type = sp->tree_type; | |
5462 | rhs = lhs; | |
5463 | lhs = sp->lhs; | |
5464 | } | |
a723baf1 | 5465 | |
b8b94c5b PB |
5466 | overloaded_p = false; |
5467 | lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p); | |
a723baf1 | 5468 | |
b8b94c5b PB |
5469 | /* If the binary operator required the use of an overloaded operator, |
5470 | then this expression cannot be an integral constant-expression. | |
5471 | An overloaded operator can be used even if both operands are | |
5472 | otherwise permissible in an integral constant-expression if at | |
5473 | least one of the operands is of enumeration type. */ | |
a723baf1 | 5474 | |
b8b94c5b PB |
5475 | if (overloaded_p |
5476 | && (cp_parser_non_integral_constant_expression | |
5477 | (parser, "calls to overloaded operators"))) | |
5478 | return error_mark_node; | |
5479 | } | |
a723baf1 | 5480 | |
b8b94c5b | 5481 | return lhs; |
a723baf1 MM |
5482 | } |
5483 | ||
b8b94c5b | 5484 | |
a723baf1 MM |
5485 | /* Parse the `? expression : assignment-expression' part of a |
5486 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5487 | logical-or-expression that started the conditional-expression. | |
5488 | Returns a representation of the entire conditional-expression. | |
5489 | ||
39703eb9 | 5490 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5491 | |
5492 | ? expression : assignment-expression | |
21526606 | 5493 | |
a723baf1 | 5494 | GNU Extensions: |
21526606 | 5495 | |
a723baf1 MM |
5496 | ? : assignment-expression */ |
5497 | ||
5498 | static tree | |
94edc4ab | 5499 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5500 | { |
5501 | tree expr; | |
5502 | tree assignment_expr; | |
5503 | ||
5504 | /* Consume the `?' token. */ | |
5505 | cp_lexer_consume_token (parser->lexer); | |
5506 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5507 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5508 | /* Implicit true clause. */ | |
5509 | expr = NULL_TREE; | |
5510 | else | |
5511 | /* Parse the expression. */ | |
93678513 | 5512 | expr = cp_parser_expression (parser, /*cast_p=*/false); |
21526606 | 5513 | |
a723baf1 MM |
5514 | /* The next token should be a `:'. */ |
5515 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5516 | /* Parse the assignment-expression. */ | |
93678513 | 5517 | assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
5518 | |
5519 | /* Build the conditional-expression. */ | |
5520 | return build_x_conditional_expr (logical_or_expr, | |
5521 | expr, | |
5522 | assignment_expr); | |
5523 | } | |
5524 | ||
5525 | /* Parse an assignment-expression. | |
5526 | ||
5527 | assignment-expression: | |
5528 | conditional-expression | |
5529 | logical-or-expression assignment-operator assignment_expression | |
5530 | throw-expression | |
5531 | ||
93678513 MM |
5532 | CAST_P is true if this expression is the target of a cast. |
5533 | ||
a723baf1 MM |
5534 | Returns a representation for the expression. */ |
5535 | ||
5536 | static tree | |
93678513 | 5537 | cp_parser_assignment_expression (cp_parser* parser, bool cast_p) |
a723baf1 MM |
5538 | { |
5539 | tree expr; | |
5540 | ||
5541 | /* If the next token is the `throw' keyword, then we're looking at | |
5542 | a throw-expression. */ | |
5543 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5544 | expr = cp_parser_throw_expression (parser); | |
5545 | /* Otherwise, it must be that we are looking at a | |
5546 | logical-or-expression. */ | |
5547 | else | |
5548 | { | |
b8b94c5b | 5549 | /* Parse the binary expressions (logical-or-expression). */ |
93678513 | 5550 | expr = cp_parser_binary_expression (parser, cast_p); |
a723baf1 MM |
5551 | /* If the next token is a `?' then we're actually looking at a |
5552 | conditional-expression. */ | |
5553 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5554 | return cp_parser_question_colon_clause (parser, expr); | |
21526606 | 5555 | else |
a723baf1 MM |
5556 | { |
5557 | enum tree_code assignment_operator; | |
5558 | ||
5559 | /* If it's an assignment-operator, we're using the second | |
5560 | production. */ | |
21526606 | 5561 | assignment_operator |
a723baf1 MM |
5562 | = cp_parser_assignment_operator_opt (parser); |
5563 | if (assignment_operator != ERROR_MARK) | |
5564 | { | |
5565 | tree rhs; | |
5566 | ||
5567 | /* Parse the right-hand side of the assignment. */ | |
93678513 | 5568 | rhs = cp_parser_assignment_expression (parser, cast_p); |
14d22dd6 MM |
5569 | /* An assignment may not appear in a |
5570 | constant-expression. */ | |
625cbf93 MM |
5571 | if (cp_parser_non_integral_constant_expression (parser, |
5572 | "an assignment")) | |
5573 | return error_mark_node; | |
34cd5ae7 | 5574 | /* Build the assignment expression. */ |
21526606 EC |
5575 | expr = build_x_modify_expr (expr, |
5576 | assignment_operator, | |
a723baf1 MM |
5577 | rhs); |
5578 | } | |
5579 | } | |
5580 | } | |
5581 | ||
5582 | return expr; | |
5583 | } | |
5584 | ||
5585 | /* Parse an (optional) assignment-operator. | |
5586 | ||
21526606 EC |
5587 | assignment-operator: one of |
5588 | = *= /= %= += -= >>= <<= &= ^= |= | |
a723baf1 MM |
5589 | |
5590 | GNU Extension: | |
21526606 | 5591 | |
a723baf1 MM |
5592 | assignment-operator: one of |
5593 | <?= >?= | |
5594 | ||
5595 | If the next token is an assignment operator, the corresponding tree | |
5596 | code is returned, and the token is consumed. For example, for | |
5597 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5598 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5599 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5600 | operator, ERROR_MARK is returned. */ | |
5601 | ||
5602 | static enum tree_code | |
94edc4ab | 5603 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5604 | { |
5605 | enum tree_code op; | |
5606 | cp_token *token; | |
5607 | ||
5608 | /* Peek at the next toen. */ | |
5609 | token = cp_lexer_peek_token (parser->lexer); | |
5610 | ||
5611 | switch (token->type) | |
5612 | { | |
5613 | case CPP_EQ: | |
5614 | op = NOP_EXPR; | |
5615 | break; | |
5616 | ||
5617 | case CPP_MULT_EQ: | |
5618 | op = MULT_EXPR; | |
5619 | break; | |
5620 | ||
5621 | case CPP_DIV_EQ: | |
5622 | op = TRUNC_DIV_EXPR; | |
5623 | break; | |
5624 | ||
5625 | case CPP_MOD_EQ: | |
5626 | op = TRUNC_MOD_EXPR; | |
5627 | break; | |
5628 | ||
5629 | case CPP_PLUS_EQ: | |
5630 | op = PLUS_EXPR; | |
5631 | break; | |
5632 | ||
5633 | case CPP_MINUS_EQ: | |
5634 | op = MINUS_EXPR; | |
5635 | break; | |
5636 | ||
5637 | case CPP_RSHIFT_EQ: | |
5638 | op = RSHIFT_EXPR; | |
5639 | break; | |
5640 | ||
5641 | case CPP_LSHIFT_EQ: | |
5642 | op = LSHIFT_EXPR; | |
5643 | break; | |
5644 | ||
5645 | case CPP_AND_EQ: | |
5646 | op = BIT_AND_EXPR; | |
5647 | break; | |
5648 | ||
5649 | case CPP_XOR_EQ: | |
5650 | op = BIT_XOR_EXPR; | |
5651 | break; | |
5652 | ||
5653 | case CPP_OR_EQ: | |
5654 | op = BIT_IOR_EXPR; | |
5655 | break; | |
5656 | ||
5657 | case CPP_MIN_EQ: | |
5658 | op = MIN_EXPR; | |
5659 | break; | |
5660 | ||
5661 | case CPP_MAX_EQ: | |
5662 | op = MAX_EXPR; | |
5663 | break; | |
5664 | ||
21526606 | 5665 | default: |
a723baf1 MM |
5666 | /* Nothing else is an assignment operator. */ |
5667 | op = ERROR_MARK; | |
5668 | } | |
5669 | ||
5670 | /* If it was an assignment operator, consume it. */ | |
5671 | if (op != ERROR_MARK) | |
5672 | cp_lexer_consume_token (parser->lexer); | |
5673 | ||
5674 | return op; | |
5675 | } | |
5676 | ||
5677 | /* Parse an expression. | |
5678 | ||
5679 | expression: | |
5680 | assignment-expression | |
5681 | expression , assignment-expression | |
5682 | ||
93678513 MM |
5683 | CAST_P is true if this expression is the target of a cast. |
5684 | ||
a723baf1 MM |
5685 | Returns a representation of the expression. */ |
5686 | ||
5687 | static tree | |
93678513 | 5688 | cp_parser_expression (cp_parser* parser, bool cast_p) |
a723baf1 MM |
5689 | { |
5690 | tree expression = NULL_TREE; | |
a723baf1 MM |
5691 | |
5692 | while (true) | |
5693 | { | |
5694 | tree assignment_expression; | |
5695 | ||
5696 | /* Parse the next assignment-expression. */ | |
21526606 | 5697 | assignment_expression |
93678513 | 5698 | = cp_parser_assignment_expression (parser, cast_p); |
a723baf1 MM |
5699 | /* If this is the first assignment-expression, we can just |
5700 | save it away. */ | |
5701 | if (!expression) | |
5702 | expression = assignment_expression; | |
a723baf1 | 5703 | else |
d17811fd MM |
5704 | expression = build_x_compound_expr (expression, |
5705 | assignment_expression); | |
a723baf1 MM |
5706 | /* If the next token is not a comma, then we are done with the |
5707 | expression. */ | |
5708 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5709 | break; | |
5710 | /* Consume the `,'. */ | |
5711 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5712 | /* A comma operator cannot appear in a constant-expression. */ |
625cbf93 MM |
5713 | if (cp_parser_non_integral_constant_expression (parser, |
5714 | "a comma operator")) | |
5715 | expression = error_mark_node; | |
14d22dd6 | 5716 | } |
a723baf1 MM |
5717 | |
5718 | return expression; | |
5719 | } | |
5720 | ||
21526606 | 5721 | /* Parse a constant-expression. |
a723baf1 MM |
5722 | |
5723 | constant-expression: | |
21526606 | 5724 | conditional-expression |
14d22dd6 MM |
5725 | |
5726 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5727 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5728 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5729 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5730 | |
5731 | static tree | |
21526606 | 5732 | cp_parser_constant_expression (cp_parser* parser, |
14d22dd6 MM |
5733 | bool allow_non_constant_p, |
5734 | bool *non_constant_p) | |
a723baf1 | 5735 | { |
67c03833 JM |
5736 | bool saved_integral_constant_expression_p; |
5737 | bool saved_allow_non_integral_constant_expression_p; | |
5738 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5739 | tree expression; |
5740 | ||
5741 | /* It might seem that we could simply parse the | |
5742 | conditional-expression, and then check to see if it were | |
5743 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5744 | one that the compiler can figure out is constant, possibly after | |
5745 | doing some simplifications or optimizations. The standard has a | |
5746 | precise definition of constant-expression, and we must honor | |
5747 | that, even though it is somewhat more restrictive. | |
5748 | ||
5749 | For example: | |
5750 | ||
5751 | int i[(2, 3)]; | |
5752 | ||
5753 | is not a legal declaration, because `(2, 3)' is not a | |
5754 | constant-expression. The `,' operator is forbidden in a | |
5755 | constant-expression. However, GCC's constant-folding machinery | |
5756 | will fold this operation to an INTEGER_CST for `3'. */ | |
5757 | ||
14d22dd6 | 5758 | /* Save the old settings. */ |
67c03833 | 5759 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
21526606 | 5760 | saved_allow_non_integral_constant_expression_p |
67c03833 JM |
5761 | = parser->allow_non_integral_constant_expression_p; |
5762 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5763 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5764 | parser->integral_constant_expression_p = true; |
5765 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5766 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5767 | /* Although the grammar says "conditional-expression", we parse an |
5768 | "assignment-expression", which also permits "throw-expression" | |
5769 | and the use of assignment operators. In the case that | |
5770 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5771 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5772 | actually essential that we look for an assignment-expression. | |
5773 | For example, cp_parser_initializer_clauses uses this function to | |
5774 | determine whether a particular assignment-expression is in fact | |
5775 | constant. */ | |
93678513 | 5776 | expression = cp_parser_assignment_expression (parser, /*cast_p=*/false); |
14d22dd6 | 5777 | /* Restore the old settings. */ |
93678513 MM |
5778 | parser->integral_constant_expression_p |
5779 | = saved_integral_constant_expression_p; | |
21526606 | 5780 | parser->allow_non_integral_constant_expression_p |
67c03833 | 5781 | = saved_allow_non_integral_constant_expression_p; |
14d22dd6 | 5782 | if (allow_non_constant_p) |
67c03833 | 5783 | *non_constant_p = parser->non_integral_constant_expression_p; |
93678513 MM |
5784 | else if (parser->non_integral_constant_expression_p) |
5785 | expression = error_mark_node; | |
5786 | parser->non_integral_constant_expression_p | |
5787 | = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5788 | |
5789 | return expression; | |
5790 | } | |
5791 | ||
7a3ea201 RH |
5792 | /* Parse __builtin_offsetof. |
5793 | ||
5794 | offsetof-expression: | |
5795 | "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" | |
5796 | ||
5797 | offsetof-member-designator: | |
5798 | id-expression | |
5799 | | offsetof-member-designator "." id-expression | |
5800 | | offsetof-member-designator "[" expression "]" | |
5801 | */ | |
5802 | ||
5803 | static tree | |
5804 | cp_parser_builtin_offsetof (cp_parser *parser) | |
5805 | { | |
5806 | int save_ice_p, save_non_ice_p; | |
5807 | tree type, expr; | |
5808 | cp_id_kind dummy; | |
5809 | ||
5810 | /* We're about to accept non-integral-constant things, but will | |
5811 | definitely yield an integral constant expression. Save and | |
5812 | restore these values around our local parsing. */ | |
5813 | save_ice_p = parser->integral_constant_expression_p; | |
5814 | save_non_ice_p = parser->non_integral_constant_expression_p; | |
5815 | ||
5816 | /* Consume the "__builtin_offsetof" token. */ | |
5817 | cp_lexer_consume_token (parser->lexer); | |
5818 | /* Consume the opening `('. */ | |
5819 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5820 | /* Parse the type-id. */ | |
5821 | type = cp_parser_type_id (parser); | |
5822 | /* Look for the `,'. */ | |
5823 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
5824 | ||
5825 | /* Build the (type *)null that begins the traditional offsetof macro. */ | |
5826 | expr = build_static_cast (build_pointer_type (type), null_pointer_node); | |
5827 | ||
5828 | /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ | |
5829 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, | |
5830 | true, &dummy); | |
5831 | while (true) | |
5832 | { | |
5833 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
5834 | switch (token->type) | |
5835 | { | |
5836 | case CPP_OPEN_SQUARE: | |
5837 | /* offsetof-member-designator "[" expression "]" */ | |
5838 | expr = cp_parser_postfix_open_square_expression (parser, expr, true); | |
5839 | break; | |
5840 | ||
5841 | case CPP_DOT: | |
5842 | /* offsetof-member-designator "." identifier */ | |
5843 | cp_lexer_consume_token (parser->lexer); | |
5844 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr, | |
5845 | true, &dummy); | |
5846 | break; | |
5847 | ||
5848 | case CPP_CLOSE_PAREN: | |
5849 | /* Consume the ")" token. */ | |
5850 | cp_lexer_consume_token (parser->lexer); | |
5851 | goto success; | |
5852 | ||
5853 | default: | |
5854 | /* Error. We know the following require will fail, but | |
5855 | that gives the proper error message. */ | |
5856 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5857 | cp_parser_skip_to_closing_parenthesis (parser, true, false, true); | |
5858 | expr = error_mark_node; | |
5859 | goto failure; | |
5860 | } | |
5861 | } | |
5862 | ||
5863 | success: | |
42c244d8 RH |
5864 | /* If we're processing a template, we can't finish the semantics yet. |
5865 | Otherwise we can fold the entire expression now. */ | |
5866 | if (processing_template_decl) | |
5867 | expr = build1 (OFFSETOF_EXPR, size_type_node, expr); | |
5868 | else | |
5869 | expr = fold_offsetof (expr); | |
7a3ea201 RH |
5870 | |
5871 | failure: | |
5872 | parser->integral_constant_expression_p = save_ice_p; | |
5873 | parser->non_integral_constant_expression_p = save_non_ice_p; | |
5874 | ||
5875 | return expr; | |
5876 | } | |
5877 | ||
a723baf1 MM |
5878 | /* Statements [gram.stmt.stmt] */ |
5879 | ||
21526606 | 5880 | /* Parse a statement. |
a723baf1 MM |
5881 | |
5882 | statement: | |
5883 | labeled-statement | |
5884 | expression-statement | |
5885 | compound-statement | |
5886 | selection-statement | |
5887 | iteration-statement | |
5888 | jump-statement | |
5889 | declaration-statement | |
5890 | try-block */ | |
5891 | ||
5892 | static void | |
325c3691 | 5893 | cp_parser_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
5894 | { |
5895 | tree statement; | |
5896 | cp_token *token; | |
93409b8c | 5897 | location_t statement_location; |
a723baf1 MM |
5898 | |
5899 | /* There is no statement yet. */ | |
5900 | statement = NULL_TREE; | |
5901 | /* Peek at the next token. */ | |
5902 | token = cp_lexer_peek_token (parser->lexer); | |
6de9cd9a | 5903 | /* Remember the location of the first token in the statement. */ |
93409b8c | 5904 | statement_location = token->location; |
a723baf1 MM |
5905 | /* If this is a keyword, then that will often determine what kind of |
5906 | statement we have. */ | |
5907 | if (token->type == CPP_KEYWORD) | |
5908 | { | |
5909 | enum rid keyword = token->keyword; | |
5910 | ||
5911 | switch (keyword) | |
5912 | { | |
5913 | case RID_CASE: | |
5914 | case RID_DEFAULT: | |
a5bcc582 | 5915 | statement = cp_parser_labeled_statement (parser, |
325c3691 | 5916 | in_statement_expr); |
a723baf1 MM |
5917 | break; |
5918 | ||
5919 | case RID_IF: | |
5920 | case RID_SWITCH: | |
5921 | statement = cp_parser_selection_statement (parser); | |
5922 | break; | |
5923 | ||
5924 | case RID_WHILE: | |
5925 | case RID_DO: | |
5926 | case RID_FOR: | |
5927 | statement = cp_parser_iteration_statement (parser); | |
5928 | break; | |
5929 | ||
5930 | case RID_BREAK: | |
5931 | case RID_CONTINUE: | |
5932 | case RID_RETURN: | |
5933 | case RID_GOTO: | |
5934 | statement = cp_parser_jump_statement (parser); | |
5935 | break; | |
5936 | ||
5937 | case RID_TRY: | |
5938 | statement = cp_parser_try_block (parser); | |
5939 | break; | |
5940 | ||
5941 | default: | |
5942 | /* It might be a keyword like `int' that can start a | |
5943 | declaration-statement. */ | |
5944 | break; | |
5945 | } | |
5946 | } | |
5947 | else if (token->type == CPP_NAME) | |
5948 | { | |
5949 | /* If the next token is a `:', then we are looking at a | |
5950 | labeled-statement. */ | |
5951 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5952 | if (token->type == CPP_COLON) | |
325c3691 | 5953 | statement = cp_parser_labeled_statement (parser, in_statement_expr); |
a723baf1 MM |
5954 | } |
5955 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5956 | else if (token->type == CPP_OPEN_BRACE) | |
325c3691 | 5957 | statement = cp_parser_compound_statement (parser, NULL, false); |
36952dea ZW |
5958 | /* CPP_PRAGMA is a #pragma inside a function body, which constitutes |
5959 | a statement all its own. */ | |
5960 | else if (token->type == CPP_PRAGMA) | |
5961 | { | |
5962 | cp_lexer_handle_pragma (parser->lexer); | |
5963 | return; | |
5964 | } | |
a723baf1 MM |
5965 | |
5966 | /* Everything else must be a declaration-statement or an | |
21526606 | 5967 | expression-statement. Try for the declaration-statement |
a723baf1 MM |
5968 | first, unless we are looking at a `;', in which case we know that |
5969 | we have an expression-statement. */ | |
5970 | if (!statement) | |
5971 | { | |
5972 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5973 | { | |
5974 | cp_parser_parse_tentatively (parser); | |
5975 | /* Try to parse the declaration-statement. */ | |
5976 | cp_parser_declaration_statement (parser); | |
5977 | /* If that worked, we're done. */ | |
5978 | if (cp_parser_parse_definitely (parser)) | |
5979 | return; | |
5980 | } | |
5981 | /* Look for an expression-statement instead. */ | |
325c3691 | 5982 | statement = cp_parser_expression_statement (parser, in_statement_expr); |
a723baf1 MM |
5983 | } |
5984 | ||
5985 | /* Set the line number for the statement. */ | |
009ed910 | 5986 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
93409b8c | 5987 | SET_EXPR_LOCATION (statement, statement_location); |
a723baf1 MM |
5988 | } |
5989 | ||
5990 | /* Parse a labeled-statement. | |
5991 | ||
5992 | labeled-statement: | |
5993 | identifier : statement | |
5994 | case constant-expression : statement | |
98ce043b MM |
5995 | default : statement |
5996 | ||
5997 | GNU Extension: | |
21526606 | 5998 | |
98ce043b MM |
5999 | labeled-statement: |
6000 | case constant-expression ... constant-expression : statement | |
a723baf1 | 6001 | |
8c161995 RH |
6002 | Returns the new CASE_LABEL_EXPR, for a `case' or `default' label. |
6003 | For an ordinary label, returns a LABEL_EXPR. */ | |
a723baf1 MM |
6004 | |
6005 | static tree | |
325c3691 | 6006 | cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
6007 | { |
6008 | cp_token *token; | |
0e59b3fb | 6009 | tree statement = error_mark_node; |
a723baf1 MM |
6010 | |
6011 | /* The next token should be an identifier. */ | |
6012 | token = cp_lexer_peek_token (parser->lexer); | |
6013 | if (token->type != CPP_NAME | |
6014 | && token->type != CPP_KEYWORD) | |
6015 | { | |
6016 | cp_parser_error (parser, "expected labeled-statement"); | |
6017 | return error_mark_node; | |
6018 | } | |
6019 | ||
6020 | switch (token->keyword) | |
6021 | { | |
6022 | case RID_CASE: | |
6023 | { | |
98ce043b MM |
6024 | tree expr, expr_hi; |
6025 | cp_token *ellipsis; | |
a723baf1 MM |
6026 | |
6027 | /* Consume the `case' token. */ | |
6028 | cp_lexer_consume_token (parser->lexer); | |
6029 | /* Parse the constant-expression. */ | |
21526606 | 6030 | expr = cp_parser_constant_expression (parser, |
d17811fd | 6031 | /*allow_non_constant_p=*/false, |
14d22dd6 | 6032 | NULL); |
98ce043b MM |
6033 | |
6034 | ellipsis = cp_lexer_peek_token (parser->lexer); | |
6035 | if (ellipsis->type == CPP_ELLIPSIS) | |
6036 | { | |
6037 | /* Consume the `...' token. */ | |
6038 | cp_lexer_consume_token (parser->lexer); | |
6039 | expr_hi = | |
6040 | cp_parser_constant_expression (parser, | |
6041 | /*allow_non_constant_p=*/false, | |
6042 | NULL); | |
6043 | /* We don't need to emit warnings here, as the common code | |
6044 | will do this for us. */ | |
6045 | } | |
6046 | else | |
6047 | expr_hi = NULL_TREE; | |
6048 | ||
0e59b3fb | 6049 | if (!parser->in_switch_statement_p) |
2a13a625 | 6050 | error ("case label %qE not within a switch statement", expr); |
0e59b3fb | 6051 | else |
98ce043b | 6052 | statement = finish_case_label (expr, expr_hi); |
a723baf1 MM |
6053 | } |
6054 | break; | |
6055 | ||
6056 | case RID_DEFAULT: | |
6057 | /* Consume the `default' token. */ | |
6058 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
6059 | if (!parser->in_switch_statement_p) |
6060 | error ("case label not within a switch statement"); | |
6061 | else | |
6062 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
6063 | break; |
6064 | ||
6065 | default: | |
6066 | /* Anything else must be an ordinary label. */ | |
6067 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
6068 | break; | |
6069 | } | |
6070 | ||
6071 | /* Require the `:' token. */ | |
6072 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
6073 | /* Parse the labeled statement. */ | |
325c3691 | 6074 | cp_parser_statement (parser, in_statement_expr); |
a723baf1 MM |
6075 | |
6076 | /* Return the label, in the case of a `case' or `default' label. */ | |
6077 | return statement; | |
6078 | } | |
6079 | ||
6080 | /* Parse an expression-statement. | |
6081 | ||
6082 | expression-statement: | |
6083 | expression [opt] ; | |
6084 | ||
6085 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
6086 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
6087 | indicates whether this expression-statement is part of an | |
6088 | expression statement. */ | |
a723baf1 MM |
6089 | |
6090 | static tree | |
325c3691 | 6091 | cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr) |
a723baf1 | 6092 | { |
a5bcc582 | 6093 | tree statement = NULL_TREE; |
a723baf1 | 6094 | |
a5bcc582 | 6095 | /* If the next token is a ';', then there is no expression |
04c06002 | 6096 | statement. */ |
a723baf1 | 6097 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
93678513 | 6098 | statement = cp_parser_expression (parser, /*cast_p=*/false); |
21526606 | 6099 | |
a723baf1 | 6100 | /* Consume the final `;'. */ |
e0860732 | 6101 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 6102 | |
325c3691 | 6103 | if (in_statement_expr |
a5bcc582 | 6104 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) |
93678513 MM |
6105 | /* This is the final expression statement of a statement |
6106 | expression. */ | |
6107 | statement = finish_stmt_expr_expr (statement, in_statement_expr); | |
a5bcc582 NS |
6108 | else if (statement) |
6109 | statement = finish_expr_stmt (statement); | |
6110 | else | |
6111 | finish_stmt (); | |
21526606 | 6112 | |
a723baf1 MM |
6113 | return statement; |
6114 | } | |
6115 | ||
6116 | /* Parse a compound-statement. | |
6117 | ||
6118 | compound-statement: | |
6119 | { statement-seq [opt] } | |
21526606 | 6120 | |
5882f0f3 | 6121 | Returns a tree representing the statement. */ |
a723baf1 MM |
6122 | |
6123 | static tree | |
325c3691 RH |
6124 | cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr, |
6125 | bool in_try) | |
a723baf1 MM |
6126 | { |
6127 | tree compound_stmt; | |
6128 | ||
6129 | /* Consume the `{'. */ | |
6130 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
6131 | return error_mark_node; | |
6132 | /* Begin the compound-statement. */ | |
325c3691 | 6133 | compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0); |
a723baf1 | 6134 | /* Parse an (optional) statement-seq. */ |
325c3691 | 6135 | cp_parser_statement_seq_opt (parser, in_statement_expr); |
a723baf1 | 6136 | /* Finish the compound-statement. */ |
7a3397c7 | 6137 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
6138 | /* Consume the `}'. */ |
6139 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
6140 | ||
6141 | return compound_stmt; | |
6142 | } | |
6143 | ||
6144 | /* Parse an (optional) statement-seq. | |
6145 | ||
6146 | statement-seq: | |
6147 | statement | |
6148 | statement-seq [opt] statement */ | |
6149 | ||
6150 | static void | |
325c3691 | 6151 | cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) |
a723baf1 MM |
6152 | { |
6153 | /* Scan statements until there aren't any more. */ | |
6154 | while (true) | |
6155 | { | |
6156 | /* If we're looking at a `}', then we've run out of statements. */ | |
6157 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
6158 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
6159 | break; | |
6160 | ||
6161 | /* Parse the statement. */ | |
325c3691 | 6162 | cp_parser_statement (parser, in_statement_expr); |
a723baf1 MM |
6163 | } |
6164 | } | |
6165 | ||
6166 | /* Parse a selection-statement. | |
6167 | ||
6168 | selection-statement: | |
6169 | if ( condition ) statement | |
6170 | if ( condition ) statement else statement | |
21526606 | 6171 | switch ( condition ) statement |
a723baf1 MM |
6172 | |
6173 | Returns the new IF_STMT or SWITCH_STMT. */ | |
6174 | ||
6175 | static tree | |
94edc4ab | 6176 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
6177 | { |
6178 | cp_token *token; | |
6179 | enum rid keyword; | |
6180 | ||
6181 | /* Peek at the next token. */ | |
6182 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
6183 | ||
6184 | /* See what kind of keyword it is. */ | |
6185 | keyword = token->keyword; | |
6186 | switch (keyword) | |
6187 | { | |
6188 | case RID_IF: | |
6189 | case RID_SWITCH: | |
6190 | { | |
6191 | tree statement; | |
6192 | tree condition; | |
6193 | ||
6194 | /* Look for the `('. */ | |
6195 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
6196 | { | |
6197 | cp_parser_skip_to_end_of_statement (parser); | |
6198 | return error_mark_node; | |
6199 | } | |
6200 | ||
6201 | /* Begin the selection-statement. */ | |
6202 | if (keyword == RID_IF) | |
6203 | statement = begin_if_stmt (); | |
6204 | else | |
6205 | statement = begin_switch_stmt (); | |
6206 | ||
6207 | /* Parse the condition. */ | |
6208 | condition = cp_parser_condition (parser); | |
6209 | /* Look for the `)'. */ | |
6210 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
6211 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
6212 | /*consume_paren=*/true); | |
a723baf1 MM |
6213 | |
6214 | if (keyword == RID_IF) | |
6215 | { | |
a723baf1 MM |
6216 | /* Add the condition. */ |
6217 | finish_if_stmt_cond (condition, statement); | |
6218 | ||
6219 | /* Parse the then-clause. */ | |
325c3691 | 6220 | cp_parser_implicitly_scoped_statement (parser); |
a723baf1 MM |
6221 | finish_then_clause (statement); |
6222 | ||
6223 | /* If the next token is `else', parse the else-clause. */ | |
6224 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
6225 | RID_ELSE)) | |
6226 | { | |
a723baf1 MM |
6227 | /* Consume the `else' keyword. */ |
6228 | cp_lexer_consume_token (parser->lexer); | |
325c3691 | 6229 | begin_else_clause (statement); |
a723baf1 | 6230 | /* Parse the else-clause. */ |
325c3691 | 6231 | cp_parser_implicitly_scoped_statement (parser); |
a723baf1 MM |
6232 | finish_else_clause (statement); |
6233 | } | |
6234 | ||
6235 | /* Now we're all done with the if-statement. */ | |
325c3691 | 6236 | finish_if_stmt (statement); |
a723baf1 MM |
6237 | } |
6238 | else | |
6239 | { | |
0e59b3fb | 6240 | bool in_switch_statement_p; |
a723baf1 MM |
6241 | |
6242 | /* Add the condition. */ | |
6243 | finish_switch_cond (condition, statement); | |
6244 | ||
6245 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
6246 | in_switch_statement_p = parser->in_switch_statement_p; |
6247 | parser->in_switch_statement_p = true; | |
325c3691 | 6248 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 6249 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
6250 | |
6251 | /* Now we're all done with the switch-statement. */ | |
6252 | finish_switch_stmt (statement); | |
6253 | } | |
6254 | ||
6255 | return statement; | |
6256 | } | |
6257 | break; | |
6258 | ||
6259 | default: | |
6260 | cp_parser_error (parser, "expected selection-statement"); | |
6261 | return error_mark_node; | |
6262 | } | |
6263 | } | |
6264 | ||
21526606 | 6265 | /* Parse a condition. |
a723baf1 MM |
6266 | |
6267 | condition: | |
6268 | expression | |
21526606 | 6269 | type-specifier-seq declarator = assignment-expression |
a723baf1 MM |
6270 | |
6271 | GNU Extension: | |
21526606 | 6272 | |
a723baf1 | 6273 | condition: |
21526606 | 6274 | type-specifier-seq declarator asm-specification [opt] |
a723baf1 | 6275 | attributes [opt] = assignment-expression |
21526606 | 6276 | |
a723baf1 MM |
6277 | Returns the expression that should be tested. */ |
6278 | ||
6279 | static tree | |
94edc4ab | 6280 | cp_parser_condition (cp_parser* parser) |
a723baf1 | 6281 | { |
62d1db17 | 6282 | cp_decl_specifier_seq type_specifiers; |
a723baf1 MM |
6283 | const char *saved_message; |
6284 | ||
6285 | /* Try the declaration first. */ | |
6286 | cp_parser_parse_tentatively (parser); | |
6287 | /* New types are not allowed in the type-specifier-seq for a | |
6288 | condition. */ | |
6289 | saved_message = parser->type_definition_forbidden_message; | |
6290 | parser->type_definition_forbidden_message | |
6291 | = "types may not be defined in conditions"; | |
6292 | /* Parse the type-specifier-seq. */ | |
62d1db17 | 6293 | cp_parser_type_specifier_seq (parser, &type_specifiers); |
a723baf1 MM |
6294 | /* Restore the saved message. */ |
6295 | parser->type_definition_forbidden_message = saved_message; | |
6296 | /* If all is well, we might be looking at a declaration. */ | |
6297 | if (!cp_parser_error_occurred (parser)) | |
6298 | { | |
6299 | tree decl; | |
6300 | tree asm_specification; | |
6301 | tree attributes; | |
058b15c1 | 6302 | cp_declarator *declarator; |
a723baf1 | 6303 | tree initializer = NULL_TREE; |
21526606 | 6304 | |
a723baf1 | 6305 | /* Parse the declarator. */ |
62b8a44e | 6306 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 6307 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
6308 | /*parenthesized_p=*/NULL, |
6309 | /*member_p=*/false); | |
a723baf1 MM |
6310 | /* Parse the attributes. */ |
6311 | attributes = cp_parser_attributes_opt (parser); | |
6312 | /* Parse the asm-specification. */ | |
6313 | asm_specification = cp_parser_asm_specification_opt (parser); | |
6314 | /* If the next token is not an `=', then we might still be | |
6315 | looking at an expression. For example: | |
21526606 | 6316 | |
a723baf1 | 6317 | if (A(a).x) |
21526606 | 6318 | |
a723baf1 MM |
6319 | looks like a decl-specifier-seq and a declarator -- but then |
6320 | there is no `=', so this is an expression. */ | |
6321 | cp_parser_require (parser, CPP_EQ, "`='"); | |
6322 | /* If we did see an `=', then we are looking at a declaration | |
6323 | for sure. */ | |
6324 | if (cp_parser_parse_definitely (parser)) | |
6325 | { | |
4514aa8c | 6326 | tree pushed_scope; |
73a8adb6 | 6327 | |
a723baf1 | 6328 | /* Create the declaration. */ |
62d1db17 | 6329 | decl = start_decl (declarator, &type_specifiers, |
a723baf1 | 6330 | /*initialized_p=*/true, |
73a8adb6 | 6331 | attributes, /*prefix_attributes=*/NULL_TREE, |
4514aa8c | 6332 | &pushed_scope); |
a723baf1 | 6333 | /* Parse the assignment-expression. */ |
93678513 MM |
6334 | initializer = cp_parser_assignment_expression (parser, |
6335 | /*cast_p=*/false); | |
21526606 | 6336 | |
a723baf1 | 6337 | /* Process the initializer. */ |
21526606 EC |
6338 | cp_finish_decl (decl, |
6339 | initializer, | |
6340 | asm_specification, | |
a723baf1 | 6341 | LOOKUP_ONLYCONVERTING); |
c162c75e | 6342 | |
4514aa8c NS |
6343 | if (pushed_scope) |
6344 | pop_scope (pushed_scope); | |
21526606 | 6345 | |
a723baf1 MM |
6346 | return convert_from_reference (decl); |
6347 | } | |
6348 | } | |
6349 | /* If we didn't even get past the declarator successfully, we are | |
6350 | definitely not looking at a declaration. */ | |
6351 | else | |
6352 | cp_parser_abort_tentative_parse (parser); | |
6353 | ||
6354 | /* Otherwise, we are looking at an expression. */ | |
93678513 | 6355 | return cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6356 | } |
6357 | ||
6358 | /* Parse an iteration-statement. | |
6359 | ||
6360 | iteration-statement: | |
6361 | while ( condition ) statement | |
6362 | do statement while ( expression ) ; | |
6363 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
6364 | statement | |
6365 | ||
6366 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
6367 | ||
6368 | static tree | |
94edc4ab | 6369 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
6370 | { |
6371 | cp_token *token; | |
6372 | enum rid keyword; | |
6373 | tree statement; | |
0e59b3fb MM |
6374 | bool in_iteration_statement_p; |
6375 | ||
a723baf1 MM |
6376 | |
6377 | /* Peek at the next token. */ | |
6378 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
6379 | if (!token) | |
6380 | return error_mark_node; | |
6381 | ||
0e59b3fb | 6382 | /* Remember whether or not we are already within an iteration |
21526606 | 6383 | statement. */ |
0e59b3fb MM |
6384 | in_iteration_statement_p = parser->in_iteration_statement_p; |
6385 | ||
a723baf1 MM |
6386 | /* See what kind of keyword it is. */ |
6387 | keyword = token->keyword; | |
6388 | switch (keyword) | |
6389 | { | |
6390 | case RID_WHILE: | |
6391 | { | |
6392 | tree condition; | |
6393 | ||
6394 | /* Begin the while-statement. */ | |
6395 | statement = begin_while_stmt (); | |
6396 | /* Look for the `('. */ | |
6397 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6398 | /* Parse the condition. */ | |
6399 | condition = cp_parser_condition (parser); | |
6400 | finish_while_stmt_cond (condition, statement); | |
6401 | /* Look for the `)'. */ | |
6402 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6403 | /* Parse the dependent statement. */ | |
0e59b3fb | 6404 | parser->in_iteration_statement_p = true; |
a723baf1 | 6405 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6406 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6407 | /* We're done with the while-statement. */ |
6408 | finish_while_stmt (statement); | |
6409 | } | |
6410 | break; | |
6411 | ||
6412 | case RID_DO: | |
6413 | { | |
6414 | tree expression; | |
6415 | ||
6416 | /* Begin the do-statement. */ | |
6417 | statement = begin_do_stmt (); | |
6418 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 6419 | parser->in_iteration_statement_p = true; |
a723baf1 | 6420 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 6421 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6422 | finish_do_body (statement); |
6423 | /* Look for the `while' keyword. */ | |
6424 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
6425 | /* Look for the `('. */ | |
6426 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6427 | /* Parse the expression. */ | |
93678513 | 6428 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6429 | /* We're done with the do-statement. */ |
6430 | finish_do_stmt (expression, statement); | |
6431 | /* Look for the `)'. */ | |
6432 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6433 | /* Look for the `;'. */ | |
6434 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6435 | } | |
6436 | break; | |
6437 | ||
6438 | case RID_FOR: | |
6439 | { | |
6440 | tree condition = NULL_TREE; | |
6441 | tree expression = NULL_TREE; | |
6442 | ||
6443 | /* Begin the for-statement. */ | |
6444 | statement = begin_for_stmt (); | |
6445 | /* Look for the `('. */ | |
6446 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6447 | /* Parse the initialization. */ | |
6448 | cp_parser_for_init_statement (parser); | |
6449 | finish_for_init_stmt (statement); | |
6450 | ||
6451 | /* If there's a condition, process it. */ | |
6452 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6453 | condition = cp_parser_condition (parser); | |
6454 | finish_for_cond (condition, statement); | |
6455 | /* Look for the `;'. */ | |
6456 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6457 | ||
6458 | /* If there's an expression, process it. */ | |
6459 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
93678513 | 6460 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6461 | finish_for_expr (expression, statement); |
6462 | /* Look for the `)'. */ | |
d5a10cf0 | 6463 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); |
98ca843c | 6464 | |
a723baf1 | 6465 | /* Parse the body of the for-statement. */ |
0e59b3fb | 6466 | parser->in_iteration_statement_p = true; |
a723baf1 | 6467 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6468 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6469 | |
6470 | /* We're done with the for-statement. */ | |
6471 | finish_for_stmt (statement); | |
6472 | } | |
6473 | break; | |
6474 | ||
6475 | default: | |
6476 | cp_parser_error (parser, "expected iteration-statement"); | |
6477 | statement = error_mark_node; | |
6478 | break; | |
6479 | } | |
6480 | ||
6481 | return statement; | |
6482 | } | |
6483 | ||
6484 | /* Parse a for-init-statement. | |
6485 | ||
6486 | for-init-statement: | |
6487 | expression-statement | |
6488 | simple-declaration */ | |
6489 | ||
6490 | static void | |
94edc4ab | 6491 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
6492 | { |
6493 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 6494 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
6495 | simple-declaration, but an invalid one, because it does not |
6496 | declare anything. Therefore, if we did not handle this case | |
6497 | specially, we would issue an error message about an invalid | |
6498 | declaration. */ | |
6499 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6500 | { | |
6501 | /* We're going to speculatively look for a declaration, falling back | |
6502 | to an expression, if necessary. */ | |
6503 | cp_parser_parse_tentatively (parser); | |
6504 | /* Parse the declaration. */ | |
6505 | cp_parser_simple_declaration (parser, | |
6506 | /*function_definition_allowed_p=*/false); | |
6507 | /* If the tentative parse failed, then we shall need to look for an | |
6508 | expression-statement. */ | |
6509 | if (cp_parser_parse_definitely (parser)) | |
6510 | return; | |
6511 | } | |
6512 | ||
a5bcc582 | 6513 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
6514 | } |
6515 | ||
6516 | /* Parse a jump-statement. | |
6517 | ||
6518 | jump-statement: | |
6519 | break ; | |
6520 | continue ; | |
6521 | return expression [opt] ; | |
21526606 | 6522 | goto identifier ; |
a723baf1 MM |
6523 | |
6524 | GNU extension: | |
6525 | ||
6526 | jump-statement: | |
6527 | goto * expression ; | |
6528 | ||
5088b058 | 6529 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ |
a723baf1 MM |
6530 | |
6531 | static tree | |
94edc4ab | 6532 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
6533 | { |
6534 | tree statement = error_mark_node; | |
6535 | cp_token *token; | |
6536 | enum rid keyword; | |
6537 | ||
6538 | /* Peek at the next token. */ | |
6539 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
6540 | if (!token) | |
6541 | return error_mark_node; | |
6542 | ||
6543 | /* See what kind of keyword it is. */ | |
6544 | keyword = token->keyword; | |
6545 | switch (keyword) | |
6546 | { | |
6547 | case RID_BREAK: | |
0e59b3fb MM |
6548 | if (!parser->in_switch_statement_p |
6549 | && !parser->in_iteration_statement_p) | |
6550 | { | |
6551 | error ("break statement not within loop or switch"); | |
6552 | statement = error_mark_node; | |
6553 | } | |
6554 | else | |
6555 | statement = finish_break_stmt (); | |
2a13a625 | 6556 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6557 | break; |
6558 | ||
6559 | case RID_CONTINUE: | |
0e59b3fb MM |
6560 | if (!parser->in_iteration_statement_p) |
6561 | { | |
6562 | error ("continue statement not within a loop"); | |
6563 | statement = error_mark_node; | |
6564 | } | |
6565 | else | |
6566 | statement = finish_continue_stmt (); | |
2a13a625 | 6567 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6568 | break; |
6569 | ||
6570 | case RID_RETURN: | |
6571 | { | |
6572 | tree expr; | |
6573 | ||
21526606 | 6574 | /* If the next token is a `;', then there is no |
a723baf1 MM |
6575 | expression. */ |
6576 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
93678513 | 6577 | expr = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
6578 | else |
6579 | expr = NULL_TREE; | |
6580 | /* Build the return-statement. */ | |
6581 | statement = finish_return_stmt (expr); | |
6582 | /* Look for the final `;'. */ | |
2a13a625 | 6583 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6584 | } |
6585 | break; | |
6586 | ||
6587 | case RID_GOTO: | |
6588 | /* Create the goto-statement. */ | |
6589 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6590 | { | |
6591 | /* Issue a warning about this use of a GNU extension. */ | |
6592 | if (pedantic) | |
6593 | pedwarn ("ISO C++ forbids computed gotos"); | |
6594 | /* Consume the '*' token. */ | |
6595 | cp_lexer_consume_token (parser->lexer); | |
6596 | /* Parse the dependent expression. */ | |
93678513 | 6597 | finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false)); |
a723baf1 MM |
6598 | } |
6599 | else | |
6600 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6601 | /* Look for the final `;'. */ | |
2a13a625 | 6602 | cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"); |
a723baf1 MM |
6603 | break; |
6604 | ||
6605 | default: | |
6606 | cp_parser_error (parser, "expected jump-statement"); | |
6607 | break; | |
6608 | } | |
6609 | ||
6610 | return statement; | |
6611 | } | |
6612 | ||
6613 | /* Parse a declaration-statement. | |
6614 | ||
6615 | declaration-statement: | |
6616 | block-declaration */ | |
6617 | ||
6618 | static void | |
94edc4ab | 6619 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 | 6620 | { |
058b15c1 MM |
6621 | void *p; |
6622 | ||
6623 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ | |
6624 | p = obstack_alloc (&declarator_obstack, 0); | |
6625 | ||
6626 | /* Parse the block-declaration. */ | |
a723baf1 MM |
6627 | cp_parser_block_declaration (parser, /*statement_p=*/true); |
6628 | ||
058b15c1 MM |
6629 | /* Free any declarators allocated. */ |
6630 | obstack_free (&declarator_obstack, p); | |
6631 | ||
a723baf1 MM |
6632 | /* Finish off the statement. */ |
6633 | finish_stmt (); | |
6634 | } | |
6635 | ||
6636 | /* Some dependent statements (like `if (cond) statement'), are | |
6637 | implicitly in their own scope. In other words, if the statement is | |
6638 | a single statement (as opposed to a compound-statement), it is | |
6639 | none-the-less treated as if it were enclosed in braces. Any | |
6640 | declarations appearing in the dependent statement are out of scope | |
6641 | after control passes that point. This function parses a statement, | |
6642 | but ensures that is in its own scope, even if it is not a | |
21526606 | 6643 | compound-statement. |
a723baf1 MM |
6644 | |
6645 | Returns the new statement. */ | |
6646 | ||
6647 | static tree | |
94edc4ab | 6648 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6649 | { |
6650 | tree statement; | |
6651 | ||
6652 | /* If the token is not a `{', then we must take special action. */ | |
6653 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6654 | { | |
6655 | /* Create a compound-statement. */ | |
325c3691 | 6656 | statement = begin_compound_stmt (0); |
a723baf1 | 6657 | /* Parse the dependent-statement. */ |
a5bcc582 | 6658 | cp_parser_statement (parser, false); |
a723baf1 | 6659 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6660 | finish_compound_stmt (statement); |
a723baf1 MM |
6661 | } |
6662 | /* Otherwise, we simply parse the statement directly. */ | |
6663 | else | |
325c3691 | 6664 | statement = cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
6665 | |
6666 | /* Return the statement. */ | |
6667 | return statement; | |
6668 | } | |
6669 | ||
6670 | /* For some dependent statements (like `while (cond) statement'), we | |
6671 | have already created a scope. Therefore, even if the dependent | |
6672 | statement is a compound-statement, we do not want to create another | |
6673 | scope. */ | |
6674 | ||
6675 | static void | |
94edc4ab | 6676 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 | 6677 | { |
325c3691 RH |
6678 | /* If the token is a `{', then we must take special action. */ |
6679 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6680 | cp_parser_statement (parser, false); | |
6681 | else | |
a723baf1 | 6682 | { |
325c3691 RH |
6683 | /* Avoid calling cp_parser_compound_statement, so that we |
6684 | don't create a new scope. Do everything else by hand. */ | |
6685 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
6686 | cp_parser_statement_seq_opt (parser, false); | |
6687 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 | 6688 | } |
a723baf1 MM |
6689 | } |
6690 | ||
6691 | /* Declarations [gram.dcl.dcl] */ | |
6692 | ||
6693 | /* Parse an optional declaration-sequence. | |
6694 | ||
6695 | declaration-seq: | |
6696 | declaration | |
6697 | declaration-seq declaration */ | |
6698 | ||
6699 | static void | |
94edc4ab | 6700 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6701 | { |
6702 | while (true) | |
6703 | { | |
6704 | cp_token *token; | |
6705 | ||
6706 | token = cp_lexer_peek_token (parser->lexer); | |
6707 | ||
6708 | if (token->type == CPP_CLOSE_BRACE | |
6709 | || token->type == CPP_EOF) | |
6710 | break; | |
6711 | ||
21526606 | 6712 | if (token->type == CPP_SEMICOLON) |
a723baf1 MM |
6713 | { |
6714 | /* A declaration consisting of a single semicolon is | |
6715 | invalid. Allow it unless we're being pedantic. */ | |
a723baf1 | 6716 | cp_lexer_consume_token (parser->lexer); |
2cfe82fe ZW |
6717 | if (pedantic && !in_system_header) |
6718 | pedwarn ("extra %<;%>"); | |
a723baf1 MM |
6719 | continue; |
6720 | } | |
6721 | ||
7d381002 | 6722 | /* If we're entering or exiting a region that's implicitly |
03fd3f84 | 6723 | extern "C", modify the lang context appropriately. */ |
7d381002 MA |
6724 | if (!parser->implicit_extern_c && token->implicit_extern_c) |
6725 | { | |
6726 | push_lang_context (lang_name_c); | |
6727 | parser->implicit_extern_c = true; | |
6728 | } | |
6729 | else if (parser->implicit_extern_c && !token->implicit_extern_c) | |
6730 | { | |
6731 | pop_lang_context (); | |
6732 | parser->implicit_extern_c = false; | |
6733 | } | |
6734 | ||
36952dea ZW |
6735 | if (token->type == CPP_PRAGMA) |
6736 | { | |
6737 | /* A top-level declaration can consist solely of a #pragma. | |
6738 | A nested declaration cannot, so this is done here and not | |
6739 | in cp_parser_declaration. (A #pragma at block scope is | |
6740 | handled in cp_parser_statement.) */ | |
6741 | cp_lexer_handle_pragma (parser->lexer); | |
6742 | continue; | |
6743 | } | |
6744 | ||
c838d82f | 6745 | /* Parse the declaration itself. */ |
a723baf1 MM |
6746 | cp_parser_declaration (parser); |
6747 | } | |
6748 | } | |
6749 | ||
6750 | /* Parse a declaration. | |
6751 | ||
6752 | declaration: | |
6753 | block-declaration | |
6754 | function-definition | |
6755 | template-declaration | |
6756 | explicit-instantiation | |
6757 | explicit-specialization | |
6758 | linkage-specification | |
21526606 | 6759 | namespace-definition |
1092805d MM |
6760 | |
6761 | GNU extension: | |
6762 | ||
6763 | declaration: | |
6764 | __extension__ declaration */ | |
a723baf1 MM |
6765 | |
6766 | static void | |
94edc4ab | 6767 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6768 | { |
6769 | cp_token token1; | |
6770 | cp_token token2; | |
1092805d | 6771 | int saved_pedantic; |
058b15c1 | 6772 | void *p; |
1092805d MM |
6773 | |
6774 | /* Check for the `__extension__' keyword. */ | |
6775 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6776 | { | |
6777 | /* Parse the qualified declaration. */ | |
6778 | cp_parser_declaration (parser); | |
6779 | /* Restore the PEDANTIC flag. */ | |
6780 | pedantic = saved_pedantic; | |
6781 | ||
6782 | return; | |
6783 | } | |
a723baf1 MM |
6784 | |
6785 | /* Try to figure out what kind of declaration is present. */ | |
6786 | token1 = *cp_lexer_peek_token (parser->lexer); | |
21526606 | 6787 | |
a723baf1 MM |
6788 | if (token1.type != CPP_EOF) |
6789 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6790 | ||
058b15c1 MM |
6791 | /* Get the high-water mark for the DECLARATOR_OBSTACK. */ |
6792 | p = obstack_alloc (&declarator_obstack, 0); | |
6793 | ||
a723baf1 MM |
6794 | /* If the next token is `extern' and the following token is a string |
6795 | literal, then we have a linkage specification. */ | |
6796 | if (token1.keyword == RID_EXTERN | |
6797 | && cp_parser_is_string_literal (&token2)) | |
6798 | cp_parser_linkage_specification (parser); | |
6799 | /* If the next token is `template', then we have either a template | |
6800 | declaration, an explicit instantiation, or an explicit | |
6801 | specialization. */ | |
6802 | else if (token1.keyword == RID_TEMPLATE) | |
6803 | { | |
6804 | /* `template <>' indicates a template specialization. */ | |
6805 | if (token2.type == CPP_LESS | |
6806 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6807 | cp_parser_explicit_specialization (parser); | |
6808 | /* `template <' indicates a template declaration. */ | |
6809 | else if (token2.type == CPP_LESS) | |
6810 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6811 | /* Anything else must be an explicit instantiation. */ | |
6812 | else | |
6813 | cp_parser_explicit_instantiation (parser); | |
6814 | } | |
6815 | /* If the next token is `export', then we have a template | |
6816 | declaration. */ | |
6817 | else if (token1.keyword == RID_EXPORT) | |
6818 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6819 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6820 | after that is `template', we have a GNU extended explicit | |
6821 | instantiation directive. */ | |
6822 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6823 | && (token1.keyword == RID_EXTERN | |
6824 | || token1.keyword == RID_STATIC | |
6825 | || token1.keyword == RID_INLINE) | |
6826 | && token2.keyword == RID_TEMPLATE) | |
6827 | cp_parser_explicit_instantiation (parser); | |
6828 | /* If the next token is `namespace', check for a named or unnamed | |
6829 | namespace definition. */ | |
6830 | else if (token1.keyword == RID_NAMESPACE | |
6831 | && (/* A named namespace definition. */ | |
6832 | (token2.type == CPP_NAME | |
21526606 | 6833 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
6834 | == CPP_OPEN_BRACE)) |
6835 | /* An unnamed namespace definition. */ | |
6836 | || token2.type == CPP_OPEN_BRACE)) | |
6837 | cp_parser_namespace_definition (parser); | |
6838 | /* We must have either a block declaration or a function | |
6839 | definition. */ | |
6840 | else | |
6841 | /* Try to parse a block-declaration, or a function-definition. */ | |
6842 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
058b15c1 MM |
6843 | |
6844 | /* Free any declarators allocated. */ | |
6845 | obstack_free (&declarator_obstack, p); | |
a723baf1 MM |
6846 | } |
6847 | ||
21526606 | 6848 | /* Parse a block-declaration. |
a723baf1 MM |
6849 | |
6850 | block-declaration: | |
6851 | simple-declaration | |
6852 | asm-definition | |
6853 | namespace-alias-definition | |
6854 | using-declaration | |
21526606 | 6855 | using-directive |
a723baf1 MM |
6856 | |
6857 | GNU Extension: | |
6858 | ||
6859 | block-declaration: | |
21526606 | 6860 | __extension__ block-declaration |
a723baf1 MM |
6861 | label-declaration |
6862 | ||
34cd5ae7 | 6863 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6864 | part of a declaration-statement. */ |
6865 | ||
6866 | static void | |
21526606 | 6867 | cp_parser_block_declaration (cp_parser *parser, |
a723baf1 MM |
6868 | bool statement_p) |
6869 | { | |
6870 | cp_token *token1; | |
6871 | int saved_pedantic; | |
6872 | ||
6873 | /* Check for the `__extension__' keyword. */ | |
6874 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6875 | { | |
6876 | /* Parse the qualified declaration. */ | |
6877 | cp_parser_block_declaration (parser, statement_p); | |
6878 | /* Restore the PEDANTIC flag. */ | |
6879 | pedantic = saved_pedantic; | |
6880 | ||
6881 | return; | |
6882 | } | |
6883 | ||
6884 | /* Peek at the next token to figure out which kind of declaration is | |
6885 | present. */ | |
6886 | token1 = cp_lexer_peek_token (parser->lexer); | |
6887 | ||
6888 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6889 | if (token1->keyword == RID_ASM) | |
6890 | { | |
6891 | if (statement_p) | |
6892 | cp_parser_commit_to_tentative_parse (parser); | |
6893 | cp_parser_asm_definition (parser); | |
6894 | } | |
6895 | /* If the next keyword is `namespace', we have a | |
6896 | namespace-alias-definition. */ | |
6897 | else if (token1->keyword == RID_NAMESPACE) | |
6898 | cp_parser_namespace_alias_definition (parser); | |
6899 | /* If the next keyword is `using', we have either a | |
6900 | using-declaration or a using-directive. */ | |
6901 | else if (token1->keyword == RID_USING) | |
6902 | { | |
6903 | cp_token *token2; | |
6904 | ||
6905 | if (statement_p) | |
6906 | cp_parser_commit_to_tentative_parse (parser); | |
6907 | /* If the token after `using' is `namespace', then we have a | |
6908 | using-directive. */ | |
6909 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6910 | if (token2->keyword == RID_NAMESPACE) | |
6911 | cp_parser_using_directive (parser); | |
6912 | /* Otherwise, it's a using-declaration. */ | |
6913 | else | |
6914 | cp_parser_using_declaration (parser); | |
6915 | } | |
6916 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6917 | else if (token1->keyword == RID_LABEL) | |
6918 | { | |
6919 | if (statement_p) | |
6920 | cp_parser_commit_to_tentative_parse (parser); | |
6921 | cp_parser_label_declaration (parser); | |
6922 | } | |
6923 | /* Anything else must be a simple-declaration. */ | |
6924 | else | |
6925 | cp_parser_simple_declaration (parser, !statement_p); | |
6926 | } | |
6927 | ||
6928 | /* Parse a simple-declaration. | |
6929 | ||
6930 | simple-declaration: | |
21526606 | 6931 | decl-specifier-seq [opt] init-declarator-list [opt] ; |
a723baf1 MM |
6932 | |
6933 | init-declarator-list: | |
6934 | init-declarator | |
21526606 | 6935 | init-declarator-list , init-declarator |
a723baf1 | 6936 | |
34cd5ae7 | 6937 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6938 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6939 | |
6940 | static void | |
21526606 | 6941 | cp_parser_simple_declaration (cp_parser* parser, |
94edc4ab | 6942 | bool function_definition_allowed_p) |
a723baf1 | 6943 | { |
62d1db17 | 6944 | cp_decl_specifier_seq decl_specifiers; |
560ad596 | 6945 | int declares_class_or_enum; |
a723baf1 MM |
6946 | bool saw_declarator; |
6947 | ||
6948 | /* Defer access checks until we know what is being declared; the | |
6949 | checks for names appearing in the decl-specifier-seq should be | |
6950 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6951 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6952 | |
a723baf1 MM |
6953 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6954 | or not the decl-specifier-seq declares a named class or | |
6955 | enumeration type, since that is the only case in which the | |
21526606 | 6956 | init-declarator-list is allowed to be empty. |
a723baf1 MM |
6957 | |
6958 | [dcl.dcl] | |
6959 | ||
6960 | In a simple-declaration, the optional init-declarator-list can be | |
6961 | omitted only when declaring a class or enumeration, that is when | |
6962 | the decl-specifier-seq contains either a class-specifier, an | |
6963 | elaborated-type-specifier, or an enum-specifier. */ | |
62d1db17 MM |
6964 | cp_parser_decl_specifier_seq (parser, |
6965 | CP_PARSER_FLAGS_OPTIONAL, | |
6966 | &decl_specifiers, | |
6967 | &declares_class_or_enum); | |
a723baf1 | 6968 | /* We no longer need to defer access checks. */ |
cf22909c | 6969 | stop_deferring_access_checks (); |
24c0ef37 | 6970 | |
39703eb9 MM |
6971 | /* In a block scope, a valid declaration must always have a |
6972 | decl-specifier-seq. By not trying to parse declarators, we can | |
6973 | resolve the declaration/expression ambiguity more quickly. */ | |
98ca843c | 6974 | if (!function_definition_allowed_p |
62d1db17 | 6975 | && !decl_specifiers.any_specifiers_p) |
39703eb9 MM |
6976 | { |
6977 | cp_parser_error (parser, "expected declaration"); | |
6978 | goto done; | |
6979 | } | |
6980 | ||
8fbc5ae7 MM |
6981 | /* If the next two tokens are both identifiers, the code is |
6982 | erroneous. The usual cause of this situation is code like: | |
6983 | ||
6984 | T t; | |
6985 | ||
6986 | where "T" should name a type -- but does not. */ | |
de3fe73c MM |
6987 | if (!decl_specifiers.type |
6988 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 6989 | { |
8d241e0b | 6990 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6991 | looking at a declaration. */ |
6992 | cp_parser_commit_to_tentative_parse (parser); | |
6993 | /* Give up. */ | |
39703eb9 | 6994 | goto done; |
8fbc5ae7 | 6995 | } |
996c2b52 MM |
6996 | |
6997 | /* If we have seen at least one decl-specifier, and the next token | |
6998 | is not a parenthesis, then we must be looking at a declaration. | |
6999 | (After "int (" we might be looking at a functional cast.) */ | |
7000 | if (decl_specifiers.any_specifiers_p | |
7001 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
7002 | cp_parser_commit_to_tentative_parse (parser); | |
8fbc5ae7 | 7003 | |
a723baf1 MM |
7004 | /* Keep going until we hit the `;' at the end of the simple |
7005 | declaration. */ | |
7006 | saw_declarator = false; | |
21526606 | 7007 | while (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
7008 | CPP_SEMICOLON)) |
7009 | { | |
7010 | cp_token *token; | |
7011 | bool function_definition_p; | |
560ad596 | 7012 | tree decl; |
a723baf1 MM |
7013 | |
7014 | saw_declarator = true; | |
7015 | /* Parse the init-declarator. */ | |
62d1db17 | 7016 | decl = cp_parser_init_declarator (parser, &decl_specifiers, |
560ad596 MM |
7017 | function_definition_allowed_p, |
7018 | /*member_p=*/false, | |
7019 | declares_class_or_enum, | |
7020 | &function_definition_p); | |
1fb3244a MM |
7021 | /* If an error occurred while parsing tentatively, exit quickly. |
7022 | (That usually happens when in the body of a function; each | |
7023 | statement is treated as a declaration-statement until proven | |
7024 | otherwise.) */ | |
7025 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 7026 | goto done; |
a723baf1 MM |
7027 | /* Handle function definitions specially. */ |
7028 | if (function_definition_p) | |
7029 | { | |
7030 | /* If the next token is a `,', then we are probably | |
7031 | processing something like: | |
7032 | ||
7033 | void f() {}, *p; | |
7034 | ||
7035 | which is erroneous. */ | |
7036 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
7037 | error ("mixing declarations and function-definitions is forbidden"); | |
7038 | /* Otherwise, we're done with the list of declarators. */ | |
7039 | else | |
24c0ef37 | 7040 | { |
cf22909c | 7041 | pop_deferring_access_checks (); |
24c0ef37 GS |
7042 | return; |
7043 | } | |
a723baf1 MM |
7044 | } |
7045 | /* The next token should be either a `,' or a `;'. */ | |
7046 | token = cp_lexer_peek_token (parser->lexer); | |
7047 | /* If it's a `,', there are more declarators to come. */ | |
7048 | if (token->type == CPP_COMMA) | |
7049 | cp_lexer_consume_token (parser->lexer); | |
7050 | /* If it's a `;', we are done. */ | |
7051 | else if (token->type == CPP_SEMICOLON) | |
7052 | break; | |
7053 | /* Anything else is an error. */ | |
7054 | else | |
7055 | { | |
996c2b52 MM |
7056 | /* If we have already issued an error message we don't need |
7057 | to issue another one. */ | |
7058 | if (decl != error_mark_node | |
0b16f8f4 | 7059 | || cp_parser_uncommitted_to_tentative_parse_p (parser)) |
2a13a625 | 7060 | cp_parser_error (parser, "expected %<,%> or %<;%>"); |
a723baf1 MM |
7061 | /* Skip tokens until we reach the end of the statement. */ |
7062 | cp_parser_skip_to_end_of_statement (parser); | |
5a98fa7b MM |
7063 | /* If the next token is now a `;', consume it. */ |
7064 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
7065 | cp_lexer_consume_token (parser->lexer); | |
39703eb9 | 7066 | goto done; |
a723baf1 MM |
7067 | } |
7068 | /* After the first time around, a function-definition is not | |
7069 | allowed -- even if it was OK at first. For example: | |
7070 | ||
7071 | int i, f() {} | |
7072 | ||
7073 | is not valid. */ | |
7074 | function_definition_allowed_p = false; | |
7075 | } | |
7076 | ||
7077 | /* Issue an error message if no declarators are present, and the | |
7078 | decl-specifier-seq does not itself declare a class or | |
7079 | enumeration. */ | |
7080 | if (!saw_declarator) | |
7081 | { | |
7082 | if (cp_parser_declares_only_class_p (parser)) | |
62d1db17 | 7083 | shadow_tag (&decl_specifiers); |
a723baf1 | 7084 | /* Perform any deferred access checks. */ |
cf22909c | 7085 | perform_deferred_access_checks (); |
a723baf1 MM |
7086 | } |
7087 | ||
7088 | /* Consume the `;'. */ | |
7089 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
7090 | ||
39703eb9 MM |
7091 | done: |
7092 | pop_deferring_access_checks (); | |
a723baf1 MM |
7093 | } |
7094 | ||
7095 | /* Parse a decl-specifier-seq. | |
7096 | ||
7097 | decl-specifier-seq: | |
7098 | decl-specifier-seq [opt] decl-specifier | |
7099 | ||
7100 | decl-specifier: | |
7101 | storage-class-specifier | |
7102 | type-specifier | |
7103 | function-specifier | |
7104 | friend | |
21526606 | 7105 | typedef |
a723baf1 MM |
7106 | |
7107 | GNU Extension: | |
7108 | ||
15077df5 MM |
7109 | decl-specifier: |
7110 | attributes | |
a723baf1 | 7111 | |
62d1db17 | 7112 | Set *DECL_SPECS to a representation of the decl-specifier-seq. |
a723baf1 | 7113 | |
eb1aef53 | 7114 | The parser flags FLAGS is used to control type-specifier parsing. |
560ad596 MM |
7115 | |
7116 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
543ca912 | 7117 | flags: |
560ad596 MM |
7118 | |
7119 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
543ca912 | 7120 | (i.e., a type declaration) |
560ad596 | 7121 | 2: one of the decl-specifiers is an enum-specifier or a |
543ca912 | 7122 | class-specifier (i.e., a type definition) |
98ca843c | 7123 | |
560ad596 | 7124 | */ |
a723baf1 | 7125 | |
62d1db17 | 7126 | static void |
21526606 | 7127 | cp_parser_decl_specifier_seq (cp_parser* parser, |
62d1db17 MM |
7128 | cp_parser_flags flags, |
7129 | cp_decl_specifier_seq *decl_specs, | |
560ad596 | 7130 | int* declares_class_or_enum) |
a723baf1 | 7131 | { |
f2ce60b8 | 7132 | bool constructor_possible_p = !parser->in_declarator_p; |
21526606 | 7133 | |
62d1db17 MM |
7134 | /* Clear DECL_SPECS. */ |
7135 | clear_decl_specs (decl_specs); | |
7136 | ||
a723baf1 | 7137 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 7138 | *declares_class_or_enum = 0; |
a723baf1 | 7139 | |
a723baf1 MM |
7140 | /* Keep reading specifiers until there are no more to read. */ |
7141 | while (true) | |
7142 | { | |
a723baf1 | 7143 | bool constructor_p; |
62d1db17 | 7144 | bool found_decl_spec; |
a723baf1 MM |
7145 | cp_token *token; |
7146 | ||
7147 | /* Peek at the next token. */ | |
7148 | token = cp_lexer_peek_token (parser->lexer); | |
7149 | /* Handle attributes. */ | |
7150 | if (token->keyword == RID_ATTRIBUTE) | |
7151 | { | |
7152 | /* Parse the attributes. */ | |
98ca843c | 7153 | decl_specs->attributes |
62d1db17 MM |
7154 | = chainon (decl_specs->attributes, |
7155 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
7156 | continue; |
7157 | } | |
62d1db17 MM |
7158 | /* Assume we will find a decl-specifier keyword. */ |
7159 | found_decl_spec = true; | |
a723baf1 MM |
7160 | /* If the next token is an appropriate keyword, we can simply |
7161 | add it to the list. */ | |
7162 | switch (token->keyword) | |
7163 | { | |
a723baf1 MM |
7164 | /* decl-specifier: |
7165 | friend */ | |
62d1db17 MM |
7166 | case RID_FRIEND: |
7167 | if (decl_specs->specs[(int) ds_friend]++) | |
2a13a625 | 7168 | error ("duplicate %<friend%>"); |
a723baf1 MM |
7169 | /* Consume the token. */ |
7170 | cp_lexer_consume_token (parser->lexer); | |
7171 | break; | |
7172 | ||
7173 | /* function-specifier: | |
7174 | inline | |
7175 | virtual | |
7176 | explicit */ | |
7177 | case RID_INLINE: | |
7178 | case RID_VIRTUAL: | |
7179 | case RID_EXPLICIT: | |
62d1db17 | 7180 | cp_parser_function_specifier_opt (parser, decl_specs); |
a723baf1 | 7181 | break; |
21526606 | 7182 | |
a723baf1 MM |
7183 | /* decl-specifier: |
7184 | typedef */ | |
7185 | case RID_TYPEDEF: | |
62d1db17 | 7186 | ++decl_specs->specs[(int) ds_typedef]; |
a723baf1 MM |
7187 | /* Consume the token. */ |
7188 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
7189 | /* A constructor declarator cannot appear in a typedef. */ |
7190 | constructor_possible_p = false; | |
c006d942 MM |
7191 | /* The "typedef" keyword can only occur in a declaration; we |
7192 | may as well commit at this point. */ | |
7193 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
7194 | break; |
7195 | ||
7196 | /* storage-class-specifier: | |
7197 | auto | |
7198 | register | |
7199 | static | |
7200 | extern | |
21526606 | 7201 | mutable |
a723baf1 MM |
7202 | |
7203 | GNU Extension: | |
7204 | thread */ | |
7205 | case RID_AUTO: | |
62d1db17 MM |
7206 | /* Consume the token. */ |
7207 | cp_lexer_consume_token (parser->lexer); | |
7208 | cp_parser_set_storage_class (decl_specs, sc_auto); | |
7209 | break; | |
a723baf1 | 7210 | case RID_REGISTER: |
62d1db17 MM |
7211 | /* Consume the token. */ |
7212 | cp_lexer_consume_token (parser->lexer); | |
7213 | cp_parser_set_storage_class (decl_specs, sc_register); | |
7214 | break; | |
a723baf1 | 7215 | case RID_STATIC: |
62d1db17 MM |
7216 | /* Consume the token. */ |
7217 | cp_lexer_consume_token (parser->lexer); | |
7218 | if (decl_specs->specs[(int) ds_thread]) | |
f1b90a04 | 7219 | { |
2d01edd7 | 7220 | error ("%<__thread%> before %<static%>"); |
f1b90a04 MM |
7221 | decl_specs->specs[(int) ds_thread] = 0; |
7222 | } | |
7223 | cp_parser_set_storage_class (decl_specs, sc_static); | |
62d1db17 | 7224 | break; |
a723baf1 | 7225 | case RID_EXTERN: |
62d1db17 MM |
7226 | /* Consume the token. */ |
7227 | cp_lexer_consume_token (parser->lexer); | |
7228 | if (decl_specs->specs[(int) ds_thread]) | |
f1b90a04 | 7229 | { |
2d01edd7 | 7230 | error ("%<__thread%> before %<extern%>"); |
f1b90a04 MM |
7231 | decl_specs->specs[(int) ds_thread] = 0; |
7232 | } | |
7233 | cp_parser_set_storage_class (decl_specs, sc_extern); | |
62d1db17 | 7234 | break; |
a723baf1 | 7235 | case RID_MUTABLE: |
62d1db17 MM |
7236 | /* Consume the token. */ |
7237 | cp_lexer_consume_token (parser->lexer); | |
7238 | cp_parser_set_storage_class (decl_specs, sc_mutable); | |
7239 | break; | |
a723baf1 | 7240 | case RID_THREAD: |
62d1db17 MM |
7241 | /* Consume the token. */ |
7242 | cp_lexer_consume_token (parser->lexer); | |
7243 | ++decl_specs->specs[(int) ds_thread]; | |
a723baf1 | 7244 | break; |
21526606 | 7245 | |
a723baf1 | 7246 | default: |
62d1db17 MM |
7247 | /* We did not yet find a decl-specifier yet. */ |
7248 | found_decl_spec = false; | |
a723baf1 MM |
7249 | break; |
7250 | } | |
7251 | ||
7252 | /* Constructors are a special case. The `S' in `S()' is not a | |
7253 | decl-specifier; it is the beginning of the declarator. */ | |
98ca843c | 7254 | constructor_p |
62d1db17 MM |
7255 | = (!found_decl_spec |
7256 | && constructor_possible_p | |
98ca843c | 7257 | && (cp_parser_constructor_declarator_p |
62d1db17 | 7258 | (parser, decl_specs->specs[(int) ds_friend] != 0))); |
a723baf1 MM |
7259 | |
7260 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
7261 | a type-specifier. */ | |
62d1db17 | 7262 | if (!found_decl_spec && !constructor_p) |
a723baf1 | 7263 | { |
560ad596 | 7264 | int decl_spec_declares_class_or_enum; |
a723baf1 | 7265 | bool is_cv_qualifier; |
62d1db17 | 7266 | tree type_spec; |
a723baf1 | 7267 | |
62d1db17 | 7268 | type_spec |
a723baf1 | 7269 | = cp_parser_type_specifier (parser, flags, |
62d1db17 | 7270 | decl_specs, |
a723baf1 MM |
7271 | /*is_declaration=*/true, |
7272 | &decl_spec_declares_class_or_enum, | |
7273 | &is_cv_qualifier); | |
7274 | ||
7275 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
7276 | ||
7277 | /* If this type-specifier referenced a user-defined type | |
7278 | (a typedef, class-name, etc.), then we can't allow any | |
7279 | more such type-specifiers henceforth. | |
7280 | ||
7281 | [dcl.spec] | |
7282 | ||
7283 | The longest sequence of decl-specifiers that could | |
7284 | possibly be a type name is taken as the | |
7285 | decl-specifier-seq of a declaration. The sequence shall | |
7286 | be self-consistent as described below. | |
7287 | ||
7288 | [dcl.type] | |
7289 | ||
7290 | As a general rule, at most one type-specifier is allowed | |
7291 | in the complete decl-specifier-seq of a declaration. The | |
7292 | only exceptions are the following: | |
7293 | ||
7294 | -- const or volatile can be combined with any other | |
21526606 | 7295 | type-specifier. |
a723baf1 MM |
7296 | |
7297 | -- signed or unsigned can be combined with char, long, | |
7298 | short, or int. | |
7299 | ||
7300 | -- .. | |
7301 | ||
7302 | Example: | |
7303 | ||
7304 | typedef char* Pc; | |
7305 | void g (const int Pc); | |
7306 | ||
7307 | Here, Pc is *not* part of the decl-specifier seq; it's | |
7308 | the declarator. Therefore, once we see a type-specifier | |
7309 | (other than a cv-qualifier), we forbid any additional | |
7310 | user-defined types. We *do* still allow things like `int | |
7311 | int' to be considered a decl-specifier-seq, and issue the | |
7312 | error message later. */ | |
62d1db17 | 7313 | if (type_spec && !is_cv_qualifier) |
a723baf1 | 7314 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; |
2050a1bb | 7315 | /* A constructor declarator cannot follow a type-specifier. */ |
62d1db17 | 7316 | if (type_spec) |
a723baf1 | 7317 | { |
62d1db17 MM |
7318 | constructor_possible_p = false; |
7319 | found_decl_spec = true; | |
a723baf1 | 7320 | } |
a723baf1 MM |
7321 | } |
7322 | ||
62d1db17 MM |
7323 | /* If we still do not have a DECL_SPEC, then there are no more |
7324 | decl-specifiers. */ | |
7325 | if (!found_decl_spec) | |
7326 | break; | |
a723baf1 | 7327 | |
62d1db17 | 7328 | decl_specs->any_specifiers_p = true; |
a723baf1 MM |
7329 | /* After we see one decl-specifier, further decl-specifiers are |
7330 | always optional. */ | |
7331 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
7332 | } | |
7333 | ||
0426c4ca | 7334 | /* Don't allow a friend specifier with a class definition. */ |
62d1db17 MM |
7335 | if (decl_specs->specs[(int) ds_friend] != 0 |
7336 | && (*declares_class_or_enum & 2)) | |
0426c4ca | 7337 | error ("class definition may not be declared a friend"); |
a723baf1 MM |
7338 | } |
7339 | ||
21526606 | 7340 | /* Parse an (optional) storage-class-specifier. |
a723baf1 MM |
7341 | |
7342 | storage-class-specifier: | |
7343 | auto | |
7344 | register | |
7345 | static | |
7346 | extern | |
21526606 | 7347 | mutable |
a723baf1 MM |
7348 | |
7349 | GNU Extension: | |
7350 | ||
7351 | storage-class-specifier: | |
7352 | thread | |
7353 | ||
7354 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
21526606 | 7355 | |
a723baf1 | 7356 | static tree |
94edc4ab | 7357 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
7358 | { |
7359 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7360 | { | |
7361 | case RID_AUTO: | |
7362 | case RID_REGISTER: | |
7363 | case RID_STATIC: | |
7364 | case RID_EXTERN: | |
7365 | case RID_MUTABLE: | |
7366 | case RID_THREAD: | |
7367 | /* Consume the token. */ | |
7368 | return cp_lexer_consume_token (parser->lexer)->value; | |
7369 | ||
7370 | default: | |
7371 | return NULL_TREE; | |
7372 | } | |
7373 | } | |
7374 | ||
21526606 | 7375 | /* Parse an (optional) function-specifier. |
a723baf1 MM |
7376 | |
7377 | function-specifier: | |
7378 | inline | |
7379 | virtual | |
7380 | explicit | |
7381 | ||
62d1db17 MM |
7382 | Returns an IDENTIFIER_NODE corresponding to the keyword used. |
7383 | Updates DECL_SPECS, if it is non-NULL. */ | |
21526606 | 7384 | |
a723baf1 | 7385 | static tree |
62d1db17 MM |
7386 | cp_parser_function_specifier_opt (cp_parser* parser, |
7387 | cp_decl_specifier_seq *decl_specs) | |
a723baf1 MM |
7388 | { |
7389 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7390 | { | |
7391 | case RID_INLINE: | |
62d1db17 MM |
7392 | if (decl_specs) |
7393 | ++decl_specs->specs[(int) ds_inline]; | |
7394 | break; | |
7395 | ||
a723baf1 | 7396 | case RID_VIRTUAL: |
62d1db17 MM |
7397 | if (decl_specs) |
7398 | ++decl_specs->specs[(int) ds_virtual]; | |
7399 | break; | |
7400 | ||
a723baf1 | 7401 | case RID_EXPLICIT: |
62d1db17 MM |
7402 | if (decl_specs) |
7403 | ++decl_specs->specs[(int) ds_explicit]; | |
7404 | break; | |
a723baf1 MM |
7405 | |
7406 | default: | |
7407 | return NULL_TREE; | |
7408 | } | |
62d1db17 MM |
7409 | |
7410 | /* Consume the token. */ | |
7411 | return cp_lexer_consume_token (parser->lexer)->value; | |
a723baf1 MM |
7412 | } |
7413 | ||
7414 | /* Parse a linkage-specification. | |
7415 | ||
7416 | linkage-specification: | |
7417 | extern string-literal { declaration-seq [opt] } | |
7418 | extern string-literal declaration */ | |
7419 | ||
7420 | static void | |
94edc4ab | 7421 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 | 7422 | { |
a723baf1 MM |
7423 | tree linkage; |
7424 | ||
7425 | /* Look for the `extern' keyword. */ | |
7426 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
7427 | ||
c162c75e MA |
7428 | /* Look for the string-literal. */ |
7429 | linkage = cp_parser_string_literal (parser, false, false); | |
a723baf1 MM |
7430 | |
7431 | /* Transform the literal into an identifier. If the literal is a | |
7432 | wide-character string, or contains embedded NULs, then we can't | |
7433 | handle it as the user wants. */ | |
c162c75e MA |
7434 | if (strlen (TREE_STRING_POINTER (linkage)) |
7435 | != (size_t) (TREE_STRING_LENGTH (linkage) - 1)) | |
a723baf1 MM |
7436 | { |
7437 | cp_parser_error (parser, "invalid linkage-specification"); | |
7438 | /* Assume C++ linkage. */ | |
c162c75e | 7439 | linkage = lang_name_cplusplus; |
a723baf1 | 7440 | } |
a723baf1 | 7441 | else |
c162c75e | 7442 | linkage = get_identifier (TREE_STRING_POINTER (linkage)); |
a723baf1 MM |
7443 | |
7444 | /* We're now using the new linkage. */ | |
7445 | push_lang_context (linkage); | |
7446 | ||
7447 | /* If the next token is a `{', then we're using the first | |
7448 | production. */ | |
7449 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
7450 | { | |
7451 | /* Consume the `{' token. */ | |
7452 | cp_lexer_consume_token (parser->lexer); | |
7453 | /* Parse the declarations. */ | |
7454 | cp_parser_declaration_seq_opt (parser); | |
7455 | /* Look for the closing `}'. */ | |
7456 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
7457 | } | |
7458 | /* Otherwise, there's just one declaration. */ | |
7459 | else | |
7460 | { | |
7461 | bool saved_in_unbraced_linkage_specification_p; | |
7462 | ||
21526606 | 7463 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
7464 | = parser->in_unbraced_linkage_specification_p; |
7465 | parser->in_unbraced_linkage_specification_p = true; | |
7466 | have_extern_spec = true; | |
7467 | cp_parser_declaration (parser); | |
7468 | have_extern_spec = false; | |
21526606 | 7469 | parser->in_unbraced_linkage_specification_p |
a723baf1 MM |
7470 | = saved_in_unbraced_linkage_specification_p; |
7471 | } | |
7472 | ||
7473 | /* We're done with the linkage-specification. */ | |
7474 | pop_lang_context (); | |
7475 | } | |
7476 | ||
7477 | /* Special member functions [gram.special] */ | |
7478 | ||
7479 | /* Parse a conversion-function-id. | |
7480 | ||
7481 | conversion-function-id: | |
21526606 | 7482 | operator conversion-type-id |
a723baf1 MM |
7483 | |
7484 | Returns an IDENTIFIER_NODE representing the operator. */ | |
7485 | ||
21526606 | 7486 | static tree |
94edc4ab | 7487 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
7488 | { |
7489 | tree type; | |
7490 | tree saved_scope; | |
7491 | tree saved_qualifying_scope; | |
7492 | tree saved_object_scope; | |
4514aa8c | 7493 | tree pushed_scope = NULL_TREE; |
a723baf1 MM |
7494 | |
7495 | /* Look for the `operator' token. */ | |
7496 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7497 | return error_mark_node; | |
7498 | /* When we parse the conversion-type-id, the current scope will be | |
7499 | reset. However, we need that information in able to look up the | |
7500 | conversion function later, so we save it here. */ | |
7501 | saved_scope = parser->scope; | |
7502 | saved_qualifying_scope = parser->qualifying_scope; | |
7503 | saved_object_scope = parser->object_scope; | |
7504 | /* We must enter the scope of the class so that the names of | |
7505 | entities declared within the class are available in the | |
7506 | conversion-type-id. For example, consider: | |
7507 | ||
21526606 | 7508 | struct S { |
a723baf1 MM |
7509 | typedef int I; |
7510 | operator I(); | |
7511 | }; | |
7512 | ||
7513 | S::operator I() { ... } | |
7514 | ||
7515 | In order to see that `I' is a type-name in the definition, we | |
7516 | must be in the scope of `S'. */ | |
7517 | if (saved_scope) | |
4514aa8c | 7518 | pushed_scope = push_scope (saved_scope); |
a723baf1 MM |
7519 | /* Parse the conversion-type-id. */ |
7520 | type = cp_parser_conversion_type_id (parser); | |
7521 | /* Leave the scope of the class, if any. */ | |
4514aa8c NS |
7522 | if (pushed_scope) |
7523 | pop_scope (pushed_scope); | |
a723baf1 MM |
7524 | /* Restore the saved scope. */ |
7525 | parser->scope = saved_scope; | |
7526 | parser->qualifying_scope = saved_qualifying_scope; | |
7527 | parser->object_scope = saved_object_scope; | |
7528 | /* If the TYPE is invalid, indicate failure. */ | |
7529 | if (type == error_mark_node) | |
7530 | return error_mark_node; | |
7531 | return mangle_conv_op_name_for_type (type); | |
7532 | } | |
7533 | ||
7534 | /* Parse a conversion-type-id: | |
7535 | ||
7536 | conversion-type-id: | |
7537 | type-specifier-seq conversion-declarator [opt] | |
7538 | ||
7539 | Returns the TYPE specified. */ | |
7540 | ||
7541 | static tree | |
94edc4ab | 7542 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
7543 | { |
7544 | tree attributes; | |
62d1db17 | 7545 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 7546 | cp_declarator *declarator; |
037cc9c5 | 7547 | tree type_specified; |
a723baf1 MM |
7548 | |
7549 | /* Parse the attributes. */ | |
7550 | attributes = cp_parser_attributes_opt (parser); | |
7551 | /* Parse the type-specifiers. */ | |
62d1db17 | 7552 | cp_parser_type_specifier_seq (parser, &type_specifiers); |
a723baf1 | 7553 | /* If that didn't work, stop. */ |
62d1db17 | 7554 | if (type_specifiers.type == error_mark_node) |
a723baf1 MM |
7555 | return error_mark_node; |
7556 | /* Parse the conversion-declarator. */ | |
7557 | declarator = cp_parser_conversion_declarator_opt (parser); | |
7558 | ||
037cc9c5 FJ |
7559 | type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME, |
7560 | /*initialized=*/0, &attributes); | |
7561 | if (attributes) | |
7562 | cplus_decl_attributes (&type_specified, attributes, /*flags=*/0); | |
7563 | return type_specified; | |
a723baf1 MM |
7564 | } |
7565 | ||
7566 | /* Parse an (optional) conversion-declarator. | |
7567 | ||
7568 | conversion-declarator: | |
21526606 | 7569 | ptr-operator conversion-declarator [opt] |
a723baf1 | 7570 | |
058b15c1 | 7571 | */ |
a723baf1 | 7572 | |
058b15c1 | 7573 | static cp_declarator * |
94edc4ab | 7574 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
7575 | { |
7576 | enum tree_code code; | |
7577 | tree class_type; | |
3c01e5df | 7578 | cp_cv_quals cv_quals; |
a723baf1 MM |
7579 | |
7580 | /* We don't know if there's a ptr-operator next, or not. */ | |
7581 | cp_parser_parse_tentatively (parser); | |
7582 | /* Try the ptr-operator. */ | |
3c01e5df | 7583 | code = cp_parser_ptr_operator (parser, &class_type, &cv_quals); |
a723baf1 MM |
7584 | /* If it worked, look for more conversion-declarators. */ |
7585 | if (cp_parser_parse_definitely (parser)) | |
7586 | { | |
058b15c1 | 7587 | cp_declarator *declarator; |
98ca843c | 7588 | |
058b15c1 MM |
7589 | /* Parse another optional declarator. */ |
7590 | declarator = cp_parser_conversion_declarator_opt (parser); | |
98ca843c | 7591 | |
058b15c1 MM |
7592 | /* Create the representation of the declarator. */ |
7593 | if (class_type) | |
3c01e5df | 7594 | declarator = make_ptrmem_declarator (cv_quals, class_type, |
a723baf1 | 7595 | declarator); |
058b15c1 | 7596 | else if (code == INDIRECT_REF) |
3c01e5df | 7597 | declarator = make_pointer_declarator (cv_quals, declarator); |
058b15c1 | 7598 | else |
3c01e5df | 7599 | declarator = make_reference_declarator (cv_quals, declarator); |
98ca843c | 7600 | |
058b15c1 | 7601 | return declarator; |
a723baf1 MM |
7602 | } |
7603 | ||
058b15c1 | 7604 | return NULL; |
a723baf1 MM |
7605 | } |
7606 | ||
7607 | /* Parse an (optional) ctor-initializer. | |
7608 | ||
7609 | ctor-initializer: | |
21526606 | 7610 | : mem-initializer-list |
a723baf1 MM |
7611 | |
7612 | Returns TRUE iff the ctor-initializer was actually present. */ | |
7613 | ||
7614 | static bool | |
94edc4ab | 7615 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
7616 | { |
7617 | /* If the next token is not a `:', then there is no | |
7618 | ctor-initializer. */ | |
7619 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
7620 | { | |
7621 | /* Do default initialization of any bases and members. */ | |
7622 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
7623 | finish_mem_initializers (NULL_TREE); | |
7624 | ||
7625 | return false; | |
7626 | } | |
7627 | ||
7628 | /* Consume the `:' token. */ | |
7629 | cp_lexer_consume_token (parser->lexer); | |
7630 | /* And the mem-initializer-list. */ | |
7631 | cp_parser_mem_initializer_list (parser); | |
7632 | ||
7633 | return true; | |
7634 | } | |
7635 | ||
7636 | /* Parse a mem-initializer-list. | |
7637 | ||
7638 | mem-initializer-list: | |
7639 | mem-initializer | |
7640 | mem-initializer , mem-initializer-list */ | |
7641 | ||
7642 | static void | |
94edc4ab | 7643 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7644 | { |
7645 | tree mem_initializer_list = NULL_TREE; | |
7646 | ||
7647 | /* Let the semantic analysis code know that we are starting the | |
7648 | mem-initializer-list. */ | |
0e136342 MM |
7649 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7650 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7651 | |
7652 | /* Loop through the list. */ | |
7653 | while (true) | |
7654 | { | |
7655 | tree mem_initializer; | |
7656 | ||
7657 | /* Parse the mem-initializer. */ | |
7658 | mem_initializer = cp_parser_mem_initializer (parser); | |
7659 | /* Add it to the list, unless it was erroneous. */ | |
7660 | if (mem_initializer) | |
7661 | { | |
7662 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7663 | mem_initializer_list = mem_initializer; | |
7664 | } | |
7665 | /* If the next token is not a `,', we're done. */ | |
7666 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7667 | break; | |
7668 | /* Consume the `,' token. */ | |
7669 | cp_lexer_consume_token (parser->lexer); | |
7670 | } | |
7671 | ||
7672 | /* Perform semantic analysis. */ | |
0e136342 MM |
7673 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7674 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7675 | } |
7676 | ||
7677 | /* Parse a mem-initializer. | |
7678 | ||
7679 | mem-initializer: | |
21526606 | 7680 | mem-initializer-id ( expression-list [opt] ) |
a723baf1 MM |
7681 | |
7682 | GNU extension: | |
21526606 | 7683 | |
a723baf1 | 7684 | mem-initializer: |
34cd5ae7 | 7685 | ( expression-list [opt] ) |
a723baf1 MM |
7686 | |
7687 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7688 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7689 | the TREE_VALUE is the expression-list. */ | |
7690 | ||
7691 | static tree | |
94edc4ab | 7692 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7693 | { |
7694 | tree mem_initializer_id; | |
7695 | tree expression_list; | |
1f5a253a | 7696 | tree member; |
21526606 | 7697 | |
a723baf1 MM |
7698 | /* Find out what is being initialized. */ |
7699 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7700 | { | |
7701 | pedwarn ("anachronistic old-style base class initializer"); | |
7702 | mem_initializer_id = NULL_TREE; | |
7703 | } | |
7704 | else | |
7705 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7706 | member = expand_member_init (mem_initializer_id); |
7707 | if (member && !DECL_P (member)) | |
7708 | in_base_initializer = 1; | |
7efa3e22 | 7709 | |
21526606 | 7710 | expression_list |
39703eb9 | 7711 | = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 7712 | /*cast_p=*/false, |
39703eb9 | 7713 | /*non_constant_p=*/NULL); |
7efa3e22 | 7714 | if (!expression_list) |
a723baf1 | 7715 | expression_list = void_type_node; |
a723baf1 | 7716 | |
1f5a253a | 7717 | in_base_initializer = 0; |
21526606 | 7718 | |
1f5a253a | 7719 | return member ? build_tree_list (member, expression_list) : NULL_TREE; |
a723baf1 MM |
7720 | } |
7721 | ||
7722 | /* Parse a mem-initializer-id. | |
7723 | ||
7724 | mem-initializer-id: | |
7725 | :: [opt] nested-name-specifier [opt] class-name | |
21526606 | 7726 | identifier |
a723baf1 MM |
7727 | |
7728 | Returns a TYPE indicating the class to be initializer for the first | |
7729 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7730 | to be initialized for the second production. */ | |
7731 | ||
7732 | static tree | |
94edc4ab | 7733 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7734 | { |
7735 | bool global_scope_p; | |
7736 | bool nested_name_specifier_p; | |
8a83a693 | 7737 | bool template_p = false; |
a723baf1 MM |
7738 | tree id; |
7739 | ||
8a83a693 GB |
7740 | /* `typename' is not allowed in this context ([temp.res]). */ |
7741 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
7742 | { | |
2a13a625 | 7743 | error ("keyword %<typename%> not allowed in this context (a qualified " |
8a83a693 GB |
7744 | "member initializer is implicitly a type)"); |
7745 | cp_lexer_consume_token (parser->lexer); | |
7746 | } | |
a723baf1 | 7747 | /* Look for the optional `::' operator. */ |
21526606 EC |
7748 | global_scope_p |
7749 | = (cp_parser_global_scope_opt (parser, | |
7750 | /*current_scope_valid_p=*/false) | |
a723baf1 MM |
7751 | != NULL_TREE); |
7752 | /* Look for the optional nested-name-specifier. The simplest way to | |
7753 | implement: | |
7754 | ||
7755 | [temp.res] | |
7756 | ||
7757 | The keyword `typename' is not permitted in a base-specifier or | |
7758 | mem-initializer; in these contexts a qualified name that | |
7759 | depends on a template-parameter is implicitly assumed to be a | |
7760 | type name. | |
7761 | ||
7762 | is to assume that we have seen the `typename' keyword at this | |
7763 | point. */ | |
21526606 | 7764 | nested_name_specifier_p |
a723baf1 MM |
7765 | = (cp_parser_nested_name_specifier_opt (parser, |
7766 | /*typename_keyword_p=*/true, | |
7767 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7768 | /*type_p=*/true, |
7769 | /*is_declaration=*/true) | |
a723baf1 | 7770 | != NULL_TREE); |
8a83a693 GB |
7771 | if (nested_name_specifier_p) |
7772 | template_p = cp_parser_optional_template_keyword (parser); | |
a723baf1 MM |
7773 | /* If there is a `::' operator or a nested-name-specifier, then we |
7774 | are definitely looking for a class-name. */ | |
7775 | if (global_scope_p || nested_name_specifier_p) | |
7776 | return cp_parser_class_name (parser, | |
7777 | /*typename_keyword_p=*/true, | |
8a83a693 | 7778 | /*template_keyword_p=*/template_p, |
fc6a28d7 | 7779 | none_type, |
a723baf1 | 7780 | /*check_dependency_p=*/true, |
a668c6ad MM |
7781 | /*class_head_p=*/false, |
7782 | /*is_declaration=*/true); | |
a723baf1 MM |
7783 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7784 | cp_parser_parse_tentatively (parser); | |
7785 | /* Try a class-name. */ | |
21526606 | 7786 | id = cp_parser_class_name (parser, |
a723baf1 MM |
7787 | /*typename_keyword_p=*/true, |
7788 | /*template_keyword_p=*/false, | |
fc6a28d7 | 7789 | none_type, |
a723baf1 | 7790 | /*check_dependency_p=*/true, |
a668c6ad MM |
7791 | /*class_head_p=*/false, |
7792 | /*is_declaration=*/true); | |
a723baf1 MM |
7793 | /* If we found one, we're done. */ |
7794 | if (cp_parser_parse_definitely (parser)) | |
7795 | return id; | |
7796 | /* Otherwise, look for an ordinary identifier. */ | |
7797 | return cp_parser_identifier (parser); | |
7798 | } | |
7799 | ||
7800 | /* Overloading [gram.over] */ | |
7801 | ||
7802 | /* Parse an operator-function-id. | |
7803 | ||
7804 | operator-function-id: | |
21526606 | 7805 | operator operator |
a723baf1 MM |
7806 | |
7807 | Returns an IDENTIFIER_NODE for the operator which is a | |
7808 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7809 | ||
21526606 | 7810 | static tree |
94edc4ab | 7811 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7812 | { |
7813 | /* Look for the `operator' keyword. */ | |
7814 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7815 | return error_mark_node; | |
7816 | /* And then the name of the operator itself. */ | |
7817 | return cp_parser_operator (parser); | |
7818 | } | |
7819 | ||
7820 | /* Parse an operator. | |
7821 | ||
7822 | operator: | |
7823 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7824 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7825 | || ++ -- , ->* -> () [] | |
7826 | ||
7827 | GNU Extensions: | |
21526606 | 7828 | |
a723baf1 MM |
7829 | operator: |
7830 | <? >? <?= >?= | |
7831 | ||
7832 | Returns an IDENTIFIER_NODE for the operator which is a | |
7833 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
21526606 | 7834 | |
a723baf1 | 7835 | static tree |
94edc4ab | 7836 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7837 | { |
7838 | tree id = NULL_TREE; | |
7839 | cp_token *token; | |
7840 | ||
7841 | /* Peek at the next token. */ | |
7842 | token = cp_lexer_peek_token (parser->lexer); | |
7843 | /* Figure out which operator we have. */ | |
7844 | switch (token->type) | |
7845 | { | |
7846 | case CPP_KEYWORD: | |
7847 | { | |
7848 | enum tree_code op; | |
7849 | ||
7850 | /* The keyword should be either `new' or `delete'. */ | |
7851 | if (token->keyword == RID_NEW) | |
7852 | op = NEW_EXPR; | |
7853 | else if (token->keyword == RID_DELETE) | |
7854 | op = DELETE_EXPR; | |
7855 | else | |
7856 | break; | |
7857 | ||
7858 | /* Consume the `new' or `delete' token. */ | |
7859 | cp_lexer_consume_token (parser->lexer); | |
7860 | ||
7861 | /* Peek at the next token. */ | |
7862 | token = cp_lexer_peek_token (parser->lexer); | |
7863 | /* If it's a `[' token then this is the array variant of the | |
7864 | operator. */ | |
7865 | if (token->type == CPP_OPEN_SQUARE) | |
7866 | { | |
7867 | /* Consume the `[' token. */ | |
7868 | cp_lexer_consume_token (parser->lexer); | |
7869 | /* Look for the `]' token. */ | |
7870 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
21526606 | 7871 | id = ansi_opname (op == NEW_EXPR |
a723baf1 MM |
7872 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); |
7873 | } | |
7874 | /* Otherwise, we have the non-array variant. */ | |
7875 | else | |
7876 | id = ansi_opname (op); | |
7877 | ||
7878 | return id; | |
7879 | } | |
7880 | ||
7881 | case CPP_PLUS: | |
7882 | id = ansi_opname (PLUS_EXPR); | |
7883 | break; | |
7884 | ||
7885 | case CPP_MINUS: | |
7886 | id = ansi_opname (MINUS_EXPR); | |
7887 | break; | |
7888 | ||
7889 | case CPP_MULT: | |
7890 | id = ansi_opname (MULT_EXPR); | |
7891 | break; | |
7892 | ||
7893 | case CPP_DIV: | |
7894 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7895 | break; | |
7896 | ||
7897 | case CPP_MOD: | |
7898 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7899 | break; | |
7900 | ||
7901 | case CPP_XOR: | |
7902 | id = ansi_opname (BIT_XOR_EXPR); | |
7903 | break; | |
7904 | ||
7905 | case CPP_AND: | |
7906 | id = ansi_opname (BIT_AND_EXPR); | |
7907 | break; | |
7908 | ||
7909 | case CPP_OR: | |
7910 | id = ansi_opname (BIT_IOR_EXPR); | |
7911 | break; | |
7912 | ||
7913 | case CPP_COMPL: | |
7914 | id = ansi_opname (BIT_NOT_EXPR); | |
7915 | break; | |
21526606 | 7916 | |
a723baf1 MM |
7917 | case CPP_NOT: |
7918 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7919 | break; | |
7920 | ||
7921 | case CPP_EQ: | |
7922 | id = ansi_assopname (NOP_EXPR); | |
7923 | break; | |
7924 | ||
7925 | case CPP_LESS: | |
7926 | id = ansi_opname (LT_EXPR); | |
7927 | break; | |
7928 | ||
7929 | case CPP_GREATER: | |
7930 | id = ansi_opname (GT_EXPR); | |
7931 | break; | |
7932 | ||
7933 | case CPP_PLUS_EQ: | |
7934 | id = ansi_assopname (PLUS_EXPR); | |
7935 | break; | |
7936 | ||
7937 | case CPP_MINUS_EQ: | |
7938 | id = ansi_assopname (MINUS_EXPR); | |
7939 | break; | |
7940 | ||
7941 | case CPP_MULT_EQ: | |
7942 | id = ansi_assopname (MULT_EXPR); | |
7943 | break; | |
7944 | ||
7945 | case CPP_DIV_EQ: | |
7946 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7947 | break; | |
7948 | ||
7949 | case CPP_MOD_EQ: | |
7950 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7951 | break; | |
7952 | ||
7953 | case CPP_XOR_EQ: | |
7954 | id = ansi_assopname (BIT_XOR_EXPR); | |
7955 | break; | |
7956 | ||
7957 | case CPP_AND_EQ: | |
7958 | id = ansi_assopname (BIT_AND_EXPR); | |
7959 | break; | |
7960 | ||
7961 | case CPP_OR_EQ: | |
7962 | id = ansi_assopname (BIT_IOR_EXPR); | |
7963 | break; | |
7964 | ||
7965 | case CPP_LSHIFT: | |
7966 | id = ansi_opname (LSHIFT_EXPR); | |
7967 | break; | |
7968 | ||
7969 | case CPP_RSHIFT: | |
7970 | id = ansi_opname (RSHIFT_EXPR); | |
7971 | break; | |
7972 | ||
7973 | case CPP_LSHIFT_EQ: | |
7974 | id = ansi_assopname (LSHIFT_EXPR); | |
7975 | break; | |
7976 | ||
7977 | case CPP_RSHIFT_EQ: | |
7978 | id = ansi_assopname (RSHIFT_EXPR); | |
7979 | break; | |
7980 | ||
7981 | case CPP_EQ_EQ: | |
7982 | id = ansi_opname (EQ_EXPR); | |
7983 | break; | |
7984 | ||
7985 | case CPP_NOT_EQ: | |
7986 | id = ansi_opname (NE_EXPR); | |
7987 | break; | |
7988 | ||
7989 | case CPP_LESS_EQ: | |
7990 | id = ansi_opname (LE_EXPR); | |
7991 | break; | |
7992 | ||
7993 | case CPP_GREATER_EQ: | |
7994 | id = ansi_opname (GE_EXPR); | |
7995 | break; | |
7996 | ||
7997 | case CPP_AND_AND: | |
7998 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7999 | break; | |
8000 | ||
8001 | case CPP_OR_OR: | |
8002 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
8003 | break; | |
21526606 | 8004 | |
a723baf1 MM |
8005 | case CPP_PLUS_PLUS: |
8006 | id = ansi_opname (POSTINCREMENT_EXPR); | |
8007 | break; | |
8008 | ||
8009 | case CPP_MINUS_MINUS: | |
8010 | id = ansi_opname (PREDECREMENT_EXPR); | |
8011 | break; | |
8012 | ||
8013 | case CPP_COMMA: | |
8014 | id = ansi_opname (COMPOUND_EXPR); | |
8015 | break; | |
8016 | ||
8017 | case CPP_DEREF_STAR: | |
8018 | id = ansi_opname (MEMBER_REF); | |
8019 | break; | |
8020 | ||
8021 | case CPP_DEREF: | |
8022 | id = ansi_opname (COMPONENT_REF); | |
8023 | break; | |
8024 | ||
8025 | case CPP_OPEN_PAREN: | |
8026 | /* Consume the `('. */ | |
8027 | cp_lexer_consume_token (parser->lexer); | |
8028 | /* Look for the matching `)'. */ | |
8029 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
8030 | return ansi_opname (CALL_EXPR); | |
8031 | ||
8032 | case CPP_OPEN_SQUARE: | |
8033 | /* Consume the `['. */ | |
8034 | cp_lexer_consume_token (parser->lexer); | |
8035 | /* Look for the matching `]'. */ | |
8036 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
8037 | return ansi_opname (ARRAY_REF); | |
8038 | ||
8039 | /* Extensions. */ | |
8040 | case CPP_MIN: | |
8041 | id = ansi_opname (MIN_EXPR); | |
8042 | break; | |
8043 | ||
8044 | case CPP_MAX: | |
8045 | id = ansi_opname (MAX_EXPR); | |
8046 | break; | |
8047 | ||
8048 | case CPP_MIN_EQ: | |
8049 | id = ansi_assopname (MIN_EXPR); | |
8050 | break; | |
8051 | ||
8052 | case CPP_MAX_EQ: | |
8053 | id = ansi_assopname (MAX_EXPR); | |
8054 | break; | |
8055 | ||
8056 | default: | |
8057 | /* Anything else is an error. */ | |
8058 | break; | |
8059 | } | |
8060 | ||
8061 | /* If we have selected an identifier, we need to consume the | |
8062 | operator token. */ | |
8063 | if (id) | |
8064 | cp_lexer_consume_token (parser->lexer); | |
8065 | /* Otherwise, no valid operator name was present. */ | |
8066 | else | |
8067 | { | |
8068 | cp_parser_error (parser, "expected operator"); | |
8069 | id = error_mark_node; | |
8070 | } | |
8071 | ||
8072 | return id; | |
8073 | } | |
8074 | ||
8075 | /* Parse a template-declaration. | |
8076 | ||
8077 | template-declaration: | |
21526606 | 8078 | export [opt] template < template-parameter-list > declaration |
a723baf1 MM |
8079 | |
8080 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
21526606 | 8081 | class-specifier. |
a723baf1 MM |
8082 | |
8083 | The grammar rule given by the standard isn't correct. What | |
8084 | is really meant is: | |
8085 | ||
8086 | template-declaration: | |
21526606 | 8087 | export [opt] template-parameter-list-seq |
a723baf1 | 8088 | decl-specifier-seq [opt] init-declarator [opt] ; |
21526606 | 8089 | export [opt] template-parameter-list-seq |
a723baf1 MM |
8090 | function-definition |
8091 | ||
8092 | template-parameter-list-seq: | |
8093 | template-parameter-list-seq [opt] | |
8094 | template < template-parameter-list > */ | |
8095 | ||
8096 | static void | |
94edc4ab | 8097 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
8098 | { |
8099 | /* Check for `export'. */ | |
8100 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
8101 | { | |
8102 | /* Consume the `export' token. */ | |
8103 | cp_lexer_consume_token (parser->lexer); | |
8104 | /* Warn that we do not support `export'. */ | |
2a13a625 | 8105 | warning ("keyword %<export%> not implemented, and will be ignored"); |
a723baf1 MM |
8106 | } |
8107 | ||
8108 | cp_parser_template_declaration_after_export (parser, member_p); | |
8109 | } | |
8110 | ||
8111 | /* Parse a template-parameter-list. | |
8112 | ||
8113 | template-parameter-list: | |
8114 | template-parameter | |
8115 | template-parameter-list , template-parameter | |
8116 | ||
8117 | Returns a TREE_LIST. Each node represents a template parameter. | |
8118 | The nodes are connected via their TREE_CHAINs. */ | |
8119 | ||
8120 | static tree | |
94edc4ab | 8121 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
8122 | { |
8123 | tree parameter_list = NULL_TREE; | |
8124 | ||
8125 | while (true) | |
8126 | { | |
8127 | tree parameter; | |
8128 | cp_token *token; | |
058b15c1 | 8129 | bool is_non_type; |
a723baf1 MM |
8130 | |
8131 | /* Parse the template-parameter. */ | |
058b15c1 | 8132 | parameter = cp_parser_template_parameter (parser, &is_non_type); |
a723baf1 | 8133 | /* Add it to the list. */ |
943e3ede MM |
8134 | if (parameter != error_mark_node) |
8135 | parameter_list = process_template_parm (parameter_list, | |
8136 | parameter, | |
8137 | is_non_type); | |
a723baf1 MM |
8138 | /* Peek at the next token. */ |
8139 | token = cp_lexer_peek_token (parser->lexer); | |
8140 | /* If it's not a `,', we're done. */ | |
8141 | if (token->type != CPP_COMMA) | |
8142 | break; | |
8143 | /* Otherwise, consume the `,' token. */ | |
8144 | cp_lexer_consume_token (parser->lexer); | |
8145 | } | |
8146 | ||
8147 | return parameter_list; | |
8148 | } | |
8149 | ||
8150 | /* Parse a template-parameter. | |
8151 | ||
8152 | template-parameter: | |
8153 | type-parameter | |
8154 | parameter-declaration | |
8155 | ||
943e3ede MM |
8156 | If all goes well, returns a TREE_LIST. The TREE_VALUE represents |
8157 | the parameter. The TREE_PURPOSE is the default value, if any. | |
8158 | Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true | |
8159 | iff this parameter is a non-type parameter. */ | |
a723baf1 MM |
8160 | |
8161 | static tree | |
058b15c1 | 8162 | cp_parser_template_parameter (cp_parser* parser, bool *is_non_type) |
a723baf1 MM |
8163 | { |
8164 | cp_token *token; | |
62d1db17 | 8165 | cp_parameter_declarator *parameter_declarator; |
943e3ede | 8166 | tree parm; |
a723baf1 | 8167 | |
058b15c1 MM |
8168 | /* Assume it is a type parameter or a template parameter. */ |
8169 | *is_non_type = false; | |
a723baf1 MM |
8170 | /* Peek at the next token. */ |
8171 | token = cp_lexer_peek_token (parser->lexer); | |
8172 | /* If it is `class' or `template', we have a type-parameter. */ | |
8173 | if (token->keyword == RID_TEMPLATE) | |
8174 | return cp_parser_type_parameter (parser); | |
8175 | /* If it is `class' or `typename' we do not know yet whether it is a | |
8176 | type parameter or a non-type parameter. Consider: | |
8177 | ||
8178 | template <typename T, typename T::X X> ... | |
8179 | ||
8180 | or: | |
21526606 | 8181 | |
a723baf1 MM |
8182 | template <class C, class D*> ... |
8183 | ||
8184 | Here, the first parameter is a type parameter, and the second is | |
8185 | a non-type parameter. We can tell by looking at the token after | |
8186 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
8187 | parameter. */ | |
8188 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
8189 | { | |
8190 | /* Peek at the token after `class' or `typename'. */ | |
8191 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
8192 | /* If it's an identifier, skip it. */ | |
8193 | if (token->type == CPP_NAME) | |
8194 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
8195 | /* Now, see if the token looks like the end of a template | |
8196 | parameter. */ | |
21526606 | 8197 | if (token->type == CPP_COMMA |
a723baf1 MM |
8198 | || token->type == CPP_EQ |
8199 | || token->type == CPP_GREATER) | |
8200 | return cp_parser_type_parameter (parser); | |
8201 | } | |
8202 | ||
21526606 | 8203 | /* Otherwise, it is a non-type parameter. |
a723baf1 MM |
8204 | |
8205 | [temp.param] | |
8206 | ||
8207 | When parsing a default template-argument for a non-type | |
8208 | template-parameter, the first non-nested `>' is taken as the end | |
8209 | of the template parameter-list rather than a greater-than | |
8210 | operator. */ | |
058b15c1 MM |
8211 | *is_non_type = true; |
8212 | parameter_declarator | |
8213 | = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, | |
8214 | /*parenthesized_p=*/NULL); | |
943e3ede MM |
8215 | parm = grokdeclarator (parameter_declarator->declarator, |
8216 | ¶meter_declarator->decl_specifiers, | |
8217 | PARM, /*initialized=*/0, | |
8218 | /*attrlist=*/NULL); | |
8219 | if (parm == error_mark_node) | |
8220 | return error_mark_node; | |
8221 | return build_tree_list (parameter_declarator->default_argument, parm); | |
a723baf1 MM |
8222 | } |
8223 | ||
8224 | /* Parse a type-parameter. | |
8225 | ||
8226 | type-parameter: | |
8227 | class identifier [opt] | |
8228 | class identifier [opt] = type-id | |
8229 | typename identifier [opt] | |
8230 | typename identifier [opt] = type-id | |
8231 | template < template-parameter-list > class identifier [opt] | |
21526606 EC |
8232 | template < template-parameter-list > class identifier [opt] |
8233 | = id-expression | |
a723baf1 MM |
8234 | |
8235 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
8236 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
8237 | the declaration of the parameter. */ | |
8238 | ||
8239 | static tree | |
94edc4ab | 8240 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
8241 | { |
8242 | cp_token *token; | |
8243 | tree parameter; | |
8244 | ||
8245 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
21526606 | 8246 | token = cp_parser_require (parser, CPP_KEYWORD, |
8a6393df | 8247 | "`class', `typename', or `template'"); |
a723baf1 MM |
8248 | if (!token) |
8249 | return error_mark_node; | |
8250 | ||
8251 | switch (token->keyword) | |
8252 | { | |
8253 | case RID_CLASS: | |
8254 | case RID_TYPENAME: | |
8255 | { | |
8256 | tree identifier; | |
8257 | tree default_argument; | |
8258 | ||
8259 | /* If the next token is an identifier, then it names the | |
8260 | parameter. */ | |
8261 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8262 | identifier = cp_parser_identifier (parser); | |
8263 | else | |
8264 | identifier = NULL_TREE; | |
8265 | ||
8266 | /* Create the parameter. */ | |
8267 | parameter = finish_template_type_parm (class_type_node, identifier); | |
8268 | ||
8269 | /* If the next token is an `=', we have a default argument. */ | |
8270 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
8271 | { | |
8272 | /* Consume the `=' token. */ | |
8273 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 8274 | /* Parse the default-argument. */ |
a723baf1 MM |
8275 | default_argument = cp_parser_type_id (parser); |
8276 | } | |
8277 | else | |
8278 | default_argument = NULL_TREE; | |
8279 | ||
8280 | /* Create the combined representation of the parameter and the | |
8281 | default argument. */ | |
c67d36d0 | 8282 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
8283 | } |
8284 | break; | |
8285 | ||
8286 | case RID_TEMPLATE: | |
8287 | { | |
8288 | tree parameter_list; | |
8289 | tree identifier; | |
8290 | tree default_argument; | |
8291 | ||
8292 | /* Look for the `<'. */ | |
8293 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8294 | /* Parse the template-parameter-list. */ | |
8295 | begin_template_parm_list (); | |
21526606 | 8296 | parameter_list |
a723baf1 MM |
8297 | = cp_parser_template_parameter_list (parser); |
8298 | parameter_list = end_template_parm_list (parameter_list); | |
8299 | /* Look for the `>'. */ | |
8300 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8301 | /* Look for the `class' keyword. */ | |
8302 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
8303 | /* If the next token is an `=', then there is a | |
8304 | default-argument. If the next token is a `>', we are at | |
8305 | the end of the parameter-list. If the next token is a `,', | |
8306 | then we are at the end of this parameter. */ | |
8307 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
8308 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
8309 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
71bd7186 MM |
8310 | { |
8311 | identifier = cp_parser_identifier (parser); | |
03fd3f84 | 8312 | /* Treat invalid names as if the parameter were nameless. */ |
71bd7186 MM |
8313 | if (identifier == error_mark_node) |
8314 | identifier = NULL_TREE; | |
8315 | } | |
a723baf1 MM |
8316 | else |
8317 | identifier = NULL_TREE; | |
71bd7186 | 8318 | |
a723baf1 MM |
8319 | /* Create the template parameter. */ |
8320 | parameter = finish_template_template_parm (class_type_node, | |
8321 | identifier); | |
21526606 | 8322 | |
a723baf1 MM |
8323 | /* If the next token is an `=', then there is a |
8324 | default-argument. */ | |
8325 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
8326 | { | |
b0bc6e8e KL |
8327 | bool is_template; |
8328 | ||
a723baf1 MM |
8329 | /* Consume the `='. */ |
8330 | cp_lexer_consume_token (parser->lexer); | |
8331 | /* Parse the id-expression. */ | |
21526606 | 8332 | default_argument |
a723baf1 MM |
8333 | = cp_parser_id_expression (parser, |
8334 | /*template_keyword_p=*/false, | |
8335 | /*check_dependency_p=*/true, | |
b0bc6e8e | 8336 | /*template_p=*/&is_template, |
f3c2dfc6 | 8337 | /*declarator_p=*/false); |
a3a503a5 GB |
8338 | if (TREE_CODE (default_argument) == TYPE_DECL) |
8339 | /* If the id-expression was a template-id that refers to | |
8340 | a template-class, we already have the declaration here, | |
8341 | so no further lookup is needed. */ | |
8342 | ; | |
8343 | else | |
8344 | /* Look up the name. */ | |
21526606 | 8345 | default_argument |
a3a503a5 | 8346 | = cp_parser_lookup_name (parser, default_argument, |
fc6a28d7 MM |
8347 | none_type, |
8348 | /*is_template=*/is_template, | |
8349 | /*is_namespace=*/false, | |
8350 | /*check_dependency=*/true, | |
8351 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
8352 | /* See if the default argument is valid. */ |
8353 | default_argument | |
8354 | = check_template_template_default_arg (default_argument); | |
8355 | } | |
8356 | else | |
8357 | default_argument = NULL_TREE; | |
8358 | ||
8359 | /* Create the combined representation of the parameter and the | |
8360 | default argument. */ | |
71bd7186 | 8361 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
8362 | } |
8363 | break; | |
8364 | ||
8365 | default: | |
71bd7186 MM |
8366 | gcc_unreachable (); |
8367 | break; | |
a723baf1 | 8368 | } |
21526606 | 8369 | |
a723baf1 MM |
8370 | return parameter; |
8371 | } | |
8372 | ||
8373 | /* Parse a template-id. | |
8374 | ||
8375 | template-id: | |
8376 | template-name < template-argument-list [opt] > | |
8377 | ||
8378 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
8379 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
8380 | returned. Otherwise, if the template-name names a function, or set | |
8381 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
21526606 | 8382 | names a class, returns a TYPE_DECL for the specialization. |
a723baf1 MM |
8383 | |
8384 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
8385 | uninstantiated templates. */ | |
8386 | ||
8387 | static tree | |
21526606 EC |
8388 | cp_parser_template_id (cp_parser *parser, |
8389 | bool template_keyword_p, | |
a668c6ad MM |
8390 | bool check_dependency_p, |
8391 | bool is_declaration) | |
a723baf1 MM |
8392 | { |
8393 | tree template; | |
8394 | tree arguments; | |
a723baf1 | 8395 | tree template_id; |
0c5e4866 | 8396 | cp_token_position start_of_id = 0; |
a723baf1 | 8397 | tree access_check = NULL_TREE; |
f4abade9 | 8398 | cp_token *next_token, *next_token_2; |
a668c6ad | 8399 | bool is_identifier; |
a723baf1 MM |
8400 | |
8401 | /* If the next token corresponds to a template-id, there is no need | |
8402 | to reparse it. */ | |
2050a1bb MM |
8403 | next_token = cp_lexer_peek_token (parser->lexer); |
8404 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
8405 | { |
8406 | tree value; | |
8407 | tree check; | |
8408 | ||
8409 | /* Get the stored value. */ | |
8410 | value = cp_lexer_consume_token (parser->lexer)->value; | |
8411 | /* Perform any access checks that were deferred. */ | |
8412 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
8413 | perform_or_defer_access_check (TREE_PURPOSE (check), |
8414 | TREE_VALUE (check)); | |
a723baf1 MM |
8415 | /* Return the stored value. */ |
8416 | return TREE_VALUE (value); | |
8417 | } | |
8418 | ||
2050a1bb MM |
8419 | /* Avoid performing name lookup if there is no possibility of |
8420 | finding a template-id. */ | |
8421 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
8422 | || (next_token->type == CPP_NAME | |
21526606 | 8423 | && !cp_parser_nth_token_starts_template_argument_list_p |
f4abade9 | 8424 | (parser, 2))) |
2050a1bb MM |
8425 | { |
8426 | cp_parser_error (parser, "expected template-id"); | |
8427 | return error_mark_node; | |
8428 | } | |
8429 | ||
a723baf1 | 8430 | /* Remember where the template-id starts. */ |
0b16f8f4 | 8431 | if (cp_parser_uncommitted_to_tentative_parse_p (parser)) |
0c5e4866 | 8432 | start_of_id = cp_lexer_token_position (parser->lexer, false); |
a723baf1 | 8433 | |
8d241e0b | 8434 | push_deferring_access_checks (dk_deferred); |
cf22909c | 8435 | |
a723baf1 | 8436 | /* Parse the template-name. */ |
a668c6ad | 8437 | is_identifier = false; |
a723baf1 | 8438 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
8439 | check_dependency_p, |
8440 | is_declaration, | |
8441 | &is_identifier); | |
8442 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
8443 | { |
8444 | pop_deferring_access_checks (); | |
a668c6ad | 8445 | return template; |
cf22909c | 8446 | } |
a723baf1 | 8447 | |
21526606 | 8448 | /* If we find the sequence `[:' after a template-name, it's probably |
f4abade9 GB |
8449 | a digraph-typo for `< ::'. Substitute the tokens and check if we can |
8450 | parse correctly the argument list. */ | |
2cfe82fe | 8451 | next_token = cp_lexer_peek_token (parser->lexer); |
f4abade9 | 8452 | next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); |
21526606 | 8453 | if (next_token->type == CPP_OPEN_SQUARE |
f4abade9 | 8454 | && next_token->flags & DIGRAPH |
21526606 | 8455 | && next_token_2->type == CPP_COLON |
f4abade9 | 8456 | && !(next_token_2->flags & PREV_WHITE)) |
cf22909c | 8457 | { |
f4abade9 GB |
8458 | cp_parser_parse_tentatively (parser); |
8459 | /* Change `:' into `::'. */ | |
8460 | next_token_2->type = CPP_SCOPE; | |
8461 | /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is | |
8462 | CPP_LESS. */ | |
8463 | cp_lexer_consume_token (parser->lexer); | |
8464 | /* Parse the arguments. */ | |
8465 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
8466 | if (!cp_parser_parse_definitely (parser)) | |
8467 | { | |
8468 | /* If we couldn't parse an argument list, then we revert our changes | |
8469 | and return simply an error. Maybe this is not a template-id | |
8470 | after all. */ | |
8471 | next_token_2->type = CPP_COLON; | |
2a13a625 | 8472 | cp_parser_error (parser, "expected %<<%>"); |
f4abade9 GB |
8473 | pop_deferring_access_checks (); |
8474 | return error_mark_node; | |
8475 | } | |
8476 | /* Otherwise, emit an error about the invalid digraph, but continue | |
8477 | parsing because we got our argument list. */ | |
2a13a625 GDR |
8478 | pedwarn ("%<<::%> cannot begin a template-argument list"); |
8479 | inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace " | |
8480 | "between %<<%> and %<::%>"); | |
f4abade9 GB |
8481 | if (!flag_permissive) |
8482 | { | |
8483 | static bool hint; | |
8484 | if (!hint) | |
8485 | { | |
2a13a625 | 8486 | inform ("(if you use -fpermissive G++ will accept your code)"); |
f4abade9 GB |
8487 | hint = true; |
8488 | } | |
8489 | } | |
8490 | } | |
8491 | else | |
8492 | { | |
8493 | /* Look for the `<' that starts the template-argument-list. */ | |
8494 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
8495 | { | |
8496 | pop_deferring_access_checks (); | |
8497 | return error_mark_node; | |
8498 | } | |
8499 | /* Parse the arguments. */ | |
8500 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
cf22909c | 8501 | } |
a723baf1 MM |
8502 | |
8503 | /* Build a representation of the specialization. */ | |
8504 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
8505 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
8506 | else if (DECL_CLASS_TEMPLATE_P (template) | |
8507 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
21526606 EC |
8508 | template_id |
8509 | = finish_template_type (template, arguments, | |
8510 | cp_lexer_next_token_is (parser->lexer, | |
a723baf1 MM |
8511 | CPP_SCOPE)); |
8512 | else | |
8513 | { | |
8514 | /* If it's not a class-template or a template-template, it should be | |
8515 | a function-template. */ | |
50bc768d NS |
8516 | gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template) |
8517 | || TREE_CODE (template) == OVERLOAD | |
8518 | || BASELINK_P (template))); | |
21526606 | 8519 | |
a723baf1 MM |
8520 | template_id = lookup_template_function (template, arguments); |
8521 | } | |
21526606 | 8522 | |
cf22909c KL |
8523 | /* Retrieve any deferred checks. Do not pop this access checks yet |
8524 | so the memory will not be reclaimed during token replacing below. */ | |
8525 | access_check = get_deferred_access_checks (); | |
8526 | ||
a723baf1 MM |
8527 | /* If parsing tentatively, replace the sequence of tokens that makes |
8528 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
8529 | should we re-parse the token stream, we will not have to repeat | |
8530 | the effort required to do the parse, nor will we issue duplicate | |
8531 | error messages about problems during instantiation of the | |
e894ab29 | 8532 | template. */ |
c8a7ed43 | 8533 | if (start_of_id) |
a723baf1 | 8534 | { |
0c5e4866 NS |
8535 | cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); |
8536 | ||
a723baf1 MM |
8537 | /* Reset the contents of the START_OF_ID token. */ |
8538 | token->type = CPP_TEMPLATE_ID; | |
8539 | token->value = build_tree_list (access_check, template_id); | |
8540 | token->keyword = RID_MAX; | |
0c5e4866 | 8541 | |
a723baf1 | 8542 | /* Purge all subsequent tokens. */ |
0c5e4866 | 8543 | cp_lexer_purge_tokens_after (parser->lexer, start_of_id); |
c8a7ed43 AO |
8544 | |
8545 | /* ??? Can we actually assume that, if template_id == | |
8546 | error_mark_node, we will have issued a diagnostic to the | |
8547 | user, as opposed to simply marking the tentative parse as | |
8548 | failed? */ | |
8549 | if (cp_parser_error_occurred (parser) && template_id != error_mark_node) | |
8550 | error ("parse error in template argument list"); | |
a723baf1 MM |
8551 | } |
8552 | ||
cf22909c | 8553 | pop_deferring_access_checks (); |
a723baf1 MM |
8554 | return template_id; |
8555 | } | |
8556 | ||
8557 | /* Parse a template-name. | |
8558 | ||
8559 | template-name: | |
8560 | identifier | |
21526606 | 8561 | |
a723baf1 MM |
8562 | The standard should actually say: |
8563 | ||
8564 | template-name: | |
8565 | identifier | |
8566 | operator-function-id | |
a723baf1 MM |
8567 | |
8568 | A defect report has been filed about this issue. | |
8569 | ||
0d956474 GB |
8570 | A conversion-function-id cannot be a template name because they cannot |
8571 | be part of a template-id. In fact, looking at this code: | |
8572 | ||
8573 | a.operator K<int>() | |
8574 | ||
8575 | the conversion-function-id is "operator K<int>", and K<int> is a type-id. | |
21526606 | 8576 | It is impossible to call a templated conversion-function-id with an |
0d956474 GB |
8577 | explicit argument list, since the only allowed template parameter is |
8578 | the type to which it is converting. | |
8579 | ||
a723baf1 MM |
8580 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
8581 | `template' keyword, in a construction like: | |
8582 | ||
8583 | T::template f<3>() | |
8584 | ||
8585 | In that case `f' is taken to be a template-name, even though there | |
8586 | is no way of knowing for sure. | |
8587 | ||
8588 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
8589 | name refers to a set of overloaded functions, at least one of which | |
8590 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
8591 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
8592 | names are looked up inside uninstantiated templates. */ | |
8593 | ||
8594 | static tree | |
21526606 EC |
8595 | cp_parser_template_name (cp_parser* parser, |
8596 | bool template_keyword_p, | |
a668c6ad MM |
8597 | bool check_dependency_p, |
8598 | bool is_declaration, | |
8599 | bool *is_identifier) | |
a723baf1 MM |
8600 | { |
8601 | tree identifier; | |
8602 | tree decl; | |
8603 | tree fns; | |
8604 | ||
8605 | /* If the next token is `operator', then we have either an | |
8606 | operator-function-id or a conversion-function-id. */ | |
8607 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
8608 | { | |
8609 | /* We don't know whether we're looking at an | |
8610 | operator-function-id or a conversion-function-id. */ | |
8611 | cp_parser_parse_tentatively (parser); | |
8612 | /* Try an operator-function-id. */ | |
8613 | identifier = cp_parser_operator_function_id (parser); | |
8614 | /* If that didn't work, try a conversion-function-id. */ | |
8615 | if (!cp_parser_parse_definitely (parser)) | |
0d956474 GB |
8616 | { |
8617 | cp_parser_error (parser, "expected template-name"); | |
8618 | return error_mark_node; | |
8619 | } | |
a723baf1 MM |
8620 | } |
8621 | /* Look for the identifier. */ | |
8622 | else | |
8623 | identifier = cp_parser_identifier (parser); | |
21526606 | 8624 | |
a723baf1 MM |
8625 | /* If we didn't find an identifier, we don't have a template-id. */ |
8626 | if (identifier == error_mark_node) | |
8627 | return error_mark_node; | |
8628 | ||
8629 | /* If the name immediately followed the `template' keyword, then it | |
8630 | is a template-name. However, if the next token is not `<', then | |
8631 | we do not treat it as a template-name, since it is not being used | |
8632 | as part of a template-id. This enables us to handle constructs | |
8633 | like: | |
8634 | ||
8635 | template <typename T> struct S { S(); }; | |
8636 | template <typename T> S<T>::S(); | |
8637 | ||
8638 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
8639 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
8640 | |
8641 | if (processing_template_decl | |
f4abade9 | 8642 | && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) |
a668c6ad MM |
8643 | { |
8644 | /* In a declaration, in a dependent context, we pretend that the | |
8645 | "template" keyword was present in order to improve error | |
8646 | recovery. For example, given: | |
21526606 | 8647 | |
a668c6ad | 8648 | template <typename T> void f(T::X<int>); |
21526606 | 8649 | |
a668c6ad | 8650 | we want to treat "X<int>" as a template-id. */ |
21526606 EC |
8651 | if (is_declaration |
8652 | && !template_keyword_p | |
a668c6ad | 8653 | && parser->scope && TYPE_P (parser->scope) |
a52eb3bc | 8654 | && check_dependency_p |
4e0f4df5 GB |
8655 | && dependent_type_p (parser->scope) |
8656 | /* Do not do this for dtors (or ctors), since they never | |
8657 | need the template keyword before their name. */ | |
8658 | && !constructor_name_p (identifier, parser->scope)) | |
a668c6ad | 8659 | { |
0c5e4866 NS |
8660 | cp_token_position start = 0; |
8661 | ||
a668c6ad | 8662 | /* Explain what went wrong. */ |
2a13a625 GDR |
8663 | error ("non-template %qD used as template", identifier); |
8664 | inform ("use %<%T::template %D%> to indicate that it is a template", | |
4e0f4df5 | 8665 | parser->scope, identifier); |
0b16f8f4 VR |
8666 | /* If parsing tentatively, find the location of the "<" token. */ |
8667 | if (cp_parser_simulate_error (parser)) | |
8668 | start = cp_lexer_token_position (parser->lexer, true); | |
a668c6ad MM |
8669 | /* Parse the template arguments so that we can issue error |
8670 | messages about them. */ | |
8671 | cp_lexer_consume_token (parser->lexer); | |
8672 | cp_parser_enclosed_template_argument_list (parser); | |
8673 | /* Skip tokens until we find a good place from which to | |
8674 | continue parsing. */ | |
8675 | cp_parser_skip_to_closing_parenthesis (parser, | |
8676 | /*recovering=*/true, | |
8677 | /*or_comma=*/true, | |
8678 | /*consume_paren=*/false); | |
8679 | /* If parsing tentatively, permanently remove the | |
8680 | template argument list. That will prevent duplicate | |
8681 | error messages from being issued about the missing | |
8682 | "template" keyword. */ | |
0c5e4866 NS |
8683 | if (start) |
8684 | cp_lexer_purge_tokens_after (parser->lexer, start); | |
a668c6ad MM |
8685 | if (is_identifier) |
8686 | *is_identifier = true; | |
8687 | return identifier; | |
8688 | } | |
9d363a56 MM |
8689 | |
8690 | /* If the "template" keyword is present, then there is generally | |
8691 | no point in doing name-lookup, so we just return IDENTIFIER. | |
8692 | But, if the qualifying scope is non-dependent then we can | |
8693 | (and must) do name-lookup normally. */ | |
8694 | if (template_keyword_p | |
8695 | && (!parser->scope | |
98ca843c | 8696 | || (TYPE_P (parser->scope) |
9d363a56 | 8697 | && dependent_type_p (parser->scope)))) |
a668c6ad MM |
8698 | return identifier; |
8699 | } | |
a723baf1 MM |
8700 | |
8701 | /* Look up the name. */ | |
8702 | decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 8703 | none_type, |
b0bc6e8e | 8704 | /*is_template=*/false, |
eea9800f | 8705 | /*is_namespace=*/false, |
8f78f01f MM |
8706 | check_dependency_p, |
8707 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
8708 | decl = maybe_get_template_decl_from_type_decl (decl); |
8709 | ||
8710 | /* If DECL is a template, then the name was a template-name. */ | |
8711 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8712 | ; | |
21526606 | 8713 | else |
a723baf1 MM |
8714 | { |
8715 | /* The standard does not explicitly indicate whether a name that | |
8716 | names a set of overloaded declarations, some of which are | |
8717 | templates, is a template-name. However, such a name should | |
8718 | be a template-name; otherwise, there is no way to form a | |
8719 | template-id for the overloaded templates. */ | |
8720 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8721 | if (TREE_CODE (fns) == OVERLOAD) | |
8722 | { | |
8723 | tree fn; | |
21526606 | 8724 | |
a723baf1 MM |
8725 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
8726 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8727 | break; | |
8728 | } | |
8729 | else | |
8730 | { | |
8731 | /* Otherwise, the name does not name a template. */ | |
8732 | cp_parser_error (parser, "expected template-name"); | |
8733 | return error_mark_node; | |
8734 | } | |
8735 | } | |
8736 | ||
8737 | /* If DECL is dependent, and refers to a function, then just return | |
8738 | its name; we will look it up again during template instantiation. */ | |
8739 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8740 | { | |
8741 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8742 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8743 | return identifier; |
8744 | } | |
8745 | ||
8746 | return decl; | |
8747 | } | |
8748 | ||
8749 | /* Parse a template-argument-list. | |
8750 | ||
8751 | template-argument-list: | |
8752 | template-argument | |
8753 | template-argument-list , template-argument | |
8754 | ||
04c06002 | 8755 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8756 | |
8757 | static tree | |
94edc4ab | 8758 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8759 | { |
bf12d54d NS |
8760 | tree fixed_args[10]; |
8761 | unsigned n_args = 0; | |
8762 | unsigned alloced = 10; | |
8763 | tree *arg_ary = fixed_args; | |
8764 | tree vec; | |
4bb8ca28 | 8765 | bool saved_in_template_argument_list_p; |
a723baf1 | 8766 | |
4bb8ca28 MM |
8767 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8768 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8769 | do |
a723baf1 MM |
8770 | { |
8771 | tree argument; | |
8772 | ||
bf12d54d | 8773 | if (n_args) |
04c06002 | 8774 | /* Consume the comma. */ |
bf12d54d | 8775 | cp_lexer_consume_token (parser->lexer); |
21526606 | 8776 | |
a723baf1 MM |
8777 | /* Parse the template-argument. */ |
8778 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8779 | if (n_args == alloced) |
8780 | { | |
8781 | alloced *= 2; | |
21526606 | 8782 | |
bf12d54d NS |
8783 | if (arg_ary == fixed_args) |
8784 | { | |
8785 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8786 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8787 | } | |
8788 | else | |
8789 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8790 | } | |
8791 | arg_ary[n_args++] = argument; | |
a723baf1 | 8792 | } |
bf12d54d NS |
8793 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8794 | ||
8795 | vec = make_tree_vec (n_args); | |
a723baf1 | 8796 | |
bf12d54d NS |
8797 | while (n_args--) |
8798 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
21526606 | 8799 | |
bf12d54d NS |
8800 | if (arg_ary != fixed_args) |
8801 | free (arg_ary); | |
4bb8ca28 | 8802 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8803 | return vec; |
a723baf1 MM |
8804 | } |
8805 | ||
8806 | /* Parse a template-argument. | |
8807 | ||
8808 | template-argument: | |
8809 | assignment-expression | |
8810 | type-id | |
8811 | id-expression | |
8812 | ||
8813 | The representation is that of an assignment-expression, type-id, or | |
8814 | id-expression -- except that the qualified id-expression is | |
8815 | evaluated, so that the value returned is either a DECL or an | |
21526606 | 8816 | OVERLOAD. |
d17811fd MM |
8817 | |
8818 | Although the standard says "assignment-expression", it forbids | |
8819 | throw-expressions or assignments in the template argument. | |
8820 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8821 | |
8822 | static tree | |
94edc4ab | 8823 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8824 | { |
8825 | tree argument; | |
8826 | bool template_p; | |
d17811fd | 8827 | bool address_p; |
4d5297fa | 8828 | bool maybe_type_id = false; |
d17811fd | 8829 | cp_token *token; |
b3445994 | 8830 | cp_id_kind idk; |
d17811fd | 8831 | tree qualifying_class; |
a723baf1 MM |
8832 | |
8833 | /* There's really no way to know what we're looking at, so we just | |
21526606 | 8834 | try each alternative in order. |
a723baf1 MM |
8835 | |
8836 | [temp.arg] | |
8837 | ||
8838 | In a template-argument, an ambiguity between a type-id and an | |
8839 | expression is resolved to a type-id, regardless of the form of | |
21526606 | 8840 | the corresponding template-parameter. |
a723baf1 MM |
8841 | |
8842 | Therefore, we try a type-id first. */ | |
8843 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8844 | argument = cp_parser_type_id (parser); |
4d5297fa | 8845 | /* If there was no error parsing the type-id but the next token is a '>>', |
21526606 | 8846 | we probably found a typo for '> >'. But there are type-id which are |
4d5297fa GB |
8847 | also valid expressions. For instance: |
8848 | ||
8849 | struct X { int operator >> (int); }; | |
8850 | template <int V> struct Foo {}; | |
8851 | Foo<X () >> 5> r; | |
8852 | ||
8853 | Here 'X()' is a valid type-id of a function type, but the user just | |
8854 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8855 | found a valid type-id, but we still try to parse the argument as an | |
8856 | expression to see what happens. */ | |
8857 | if (!cp_parser_error_occurred (parser) | |
8858 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8859 | { | |
8860 | maybe_type_id = true; | |
8861 | cp_parser_abort_tentative_parse (parser); | |
8862 | } | |
8863 | else | |
8864 | { | |
8865 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8866 | really finished. This means that the argument is not a valid | |
8867 | type-id. */ | |
8868 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8869 | cp_parser_error (parser, "expected template-argument"); | |
8870 | /* If that worked, we're done. */ | |
8871 | if (cp_parser_parse_definitely (parser)) | |
8872 | return argument; | |
8873 | } | |
a723baf1 MM |
8874 | /* We're still not sure what the argument will be. */ |
8875 | cp_parser_parse_tentatively (parser); | |
8876 | /* Try a template. */ | |
21526606 | 8877 | argument = cp_parser_id_expression (parser, |
a723baf1 MM |
8878 | /*template_keyword_p=*/false, |
8879 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
8880 | &template_p, |
8881 | /*declarator_p=*/false); | |
a723baf1 MM |
8882 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
8883 | really finished. */ | |
d17811fd | 8884 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
8885 | cp_parser_error (parser, "expected template-argument"); |
8886 | if (!cp_parser_error_occurred (parser)) | |
8887 | { | |
f746161e MM |
8888 | /* Figure out what is being referred to. If the id-expression |
8889 | was for a class template specialization, then we will have a | |
8890 | TYPE_DECL at this point. There is no need to do name lookup | |
8891 | at this point in that case. */ | |
8892 | if (TREE_CODE (argument) != TYPE_DECL) | |
8893 | argument = cp_parser_lookup_name (parser, argument, | |
fc6a28d7 | 8894 | none_type, |
f746161e MM |
8895 | /*is_template=*/template_p, |
8896 | /*is_namespace=*/false, | |
8f78f01f MM |
8897 | /*check_dependency=*/true, |
8898 | /*ambiguous_p=*/NULL); | |
5b4acce1 KL |
8899 | if (TREE_CODE (argument) != TEMPLATE_DECL |
8900 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
8901 | cp_parser_error (parser, "expected template-name"); |
8902 | } | |
8903 | if (cp_parser_parse_definitely (parser)) | |
8904 | return argument; | |
d17811fd MM |
8905 | /* It must be a non-type argument. There permitted cases are given |
8906 | in [temp.arg.nontype]: | |
8907 | ||
8908 | -- an integral constant-expression of integral or enumeration | |
8909 | type; or | |
8910 | ||
8911 | -- the name of a non-type template-parameter; or | |
8912 | ||
8913 | -- the name of an object or function with external linkage... | |
8914 | ||
8915 | -- the address of an object or function with external linkage... | |
8916 | ||
04c06002 | 8917 | -- a pointer to member... */ |
d17811fd MM |
8918 | /* Look for a non-type template parameter. */ |
8919 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8920 | { | |
8921 | cp_parser_parse_tentatively (parser); | |
8922 | argument = cp_parser_primary_expression (parser, | |
93678513 | 8923 | /*cast_p=*/false, |
d17811fd MM |
8924 | &idk, |
8925 | &qualifying_class); | |
8926 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8927 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8928 | cp_parser_simulate_error (parser); | |
8929 | if (cp_parser_parse_definitely (parser)) | |
8930 | return argument; | |
8931 | } | |
db24eb1f | 8932 | |
d17811fd MM |
8933 | /* If the next token is "&", the argument must be the address of an |
8934 | object or function with external linkage. */ | |
8935 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8936 | if (address_p) | |
8937 | cp_lexer_consume_token (parser->lexer); | |
8938 | /* See if we might have an id-expression. */ | |
8939 | token = cp_lexer_peek_token (parser->lexer); | |
8940 | if (token->type == CPP_NAME | |
8941 | || token->keyword == RID_OPERATOR | |
8942 | || token->type == CPP_SCOPE | |
8943 | || token->type == CPP_TEMPLATE_ID | |
8944 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8945 | { | |
8946 | cp_parser_parse_tentatively (parser); | |
8947 | argument = cp_parser_primary_expression (parser, | |
93678513 | 8948 | /*cast_p=*/false, |
d17811fd MM |
8949 | &idk, |
8950 | &qualifying_class); | |
8951 | if (cp_parser_error_occurred (parser) | |
8952 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8953 | cp_parser_abort_tentative_parse (parser); | |
8954 | else | |
8955 | { | |
db24eb1f NS |
8956 | if (TREE_CODE (argument) == INDIRECT_REF) |
8957 | { | |
8958 | gcc_assert (REFERENCE_REF_P (argument)); | |
8959 | argument = TREE_OPERAND (argument, 0); | |
8960 | } | |
8961 | ||
d17811fd MM |
8962 | if (qualifying_class) |
8963 | argument = finish_qualified_id_expr (qualifying_class, | |
8964 | argument, | |
8965 | /*done=*/true, | |
8966 | address_p); | |
8967 | if (TREE_CODE (argument) == VAR_DECL) | |
8968 | { | |
8969 | /* A variable without external linkage might still be a | |
8970 | valid constant-expression, so no error is issued here | |
8971 | if the external-linkage check fails. */ | |
8972 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8973 | cp_parser_simulate_error (parser); | |
8974 | } | |
8975 | else if (is_overloaded_fn (argument)) | |
8976 | /* All overloaded functions are allowed; if the external | |
8977 | linkage test does not pass, an error will be issued | |
8978 | later. */ | |
8979 | ; | |
8980 | else if (address_p | |
21526606 | 8981 | && (TREE_CODE (argument) == OFFSET_REF |
d17811fd MM |
8982 | || TREE_CODE (argument) == SCOPE_REF)) |
8983 | /* A pointer-to-member. */ | |
8984 | ; | |
db24eb1f NS |
8985 | else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX) |
8986 | ; | |
d17811fd MM |
8987 | else |
8988 | cp_parser_simulate_error (parser); | |
8989 | ||
8990 | if (cp_parser_parse_definitely (parser)) | |
8991 | { | |
8992 | if (address_p) | |
8993 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8994 | return argument; | |
8995 | } | |
8996 | } | |
8997 | } | |
8998 | /* If the argument started with "&", there are no other valid | |
8999 | alternatives at this point. */ | |
9000 | if (address_p) | |
9001 | { | |
9002 | cp_parser_error (parser, "invalid non-type template argument"); | |
9003 | return error_mark_node; | |
9004 | } | |
db24eb1f | 9005 | |
4d5297fa | 9006 | /* If the argument wasn't successfully parsed as a type-id followed |
21526606 | 9007 | by '>>', the argument can only be a constant expression now. |
4d5297fa GB |
9008 | Otherwise, we try parsing the constant-expression tentatively, |
9009 | because the argument could really be a type-id. */ | |
9010 | if (maybe_type_id) | |
9011 | cp_parser_parse_tentatively (parser); | |
21526606 | 9012 | argument = cp_parser_constant_expression (parser, |
d17811fd MM |
9013 | /*allow_non_constant_p=*/false, |
9014 | /*non_constant_p=*/NULL); | |
9baa27a9 | 9015 | argument = fold_non_dependent_expr (argument); |
4d5297fa GB |
9016 | if (!maybe_type_id) |
9017 | return argument; | |
9018 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
9019 | cp_parser_error (parser, "expected template-argument"); | |
9020 | if (cp_parser_parse_definitely (parser)) | |
9021 | return argument; | |
9022 | /* We did our best to parse the argument as a non type-id, but that | |
9023 | was the only alternative that matched (albeit with a '>' after | |
21526606 | 9024 | it). We can assume it's just a typo from the user, and a |
4d5297fa GB |
9025 | diagnostic will then be issued. */ |
9026 | return cp_parser_type_id (parser); | |
a723baf1 MM |
9027 | } |
9028 | ||
9029 | /* Parse an explicit-instantiation. | |
9030 | ||
9031 | explicit-instantiation: | |
21526606 | 9032 | template declaration |
a723baf1 MM |
9033 | |
9034 | Although the standard says `declaration', what it really means is: | |
9035 | ||
9036 | explicit-instantiation: | |
21526606 | 9037 | template decl-specifier-seq [opt] declarator [opt] ; |
a723baf1 MM |
9038 | |
9039 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
9040 | supposed to be allowed. A defect report has been filed about this | |
21526606 | 9041 | issue. |
a723baf1 MM |
9042 | |
9043 | GNU Extension: | |
21526606 | 9044 | |
a723baf1 | 9045 | explicit-instantiation: |
21526606 | 9046 | storage-class-specifier template |
a723baf1 | 9047 | decl-specifier-seq [opt] declarator [opt] ; |
21526606 | 9048 | function-specifier template |
a723baf1 MM |
9049 | decl-specifier-seq [opt] declarator [opt] ; */ |
9050 | ||
9051 | static void | |
94edc4ab | 9052 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 9053 | { |
560ad596 | 9054 | int declares_class_or_enum; |
62d1db17 | 9055 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 MM |
9056 | tree extension_specifier = NULL_TREE; |
9057 | ||
9058 | /* Look for an (optional) storage-class-specifier or | |
9059 | function-specifier. */ | |
9060 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
9061 | { | |
21526606 | 9062 | extension_specifier |
a723baf1 MM |
9063 | = cp_parser_storage_class_specifier_opt (parser); |
9064 | if (!extension_specifier) | |
98ca843c | 9065 | extension_specifier |
62d1db17 MM |
9066 | = cp_parser_function_specifier_opt (parser, |
9067 | /*decl_specs=*/NULL); | |
a723baf1 MM |
9068 | } |
9069 | ||
9070 | /* Look for the `template' keyword. */ | |
9071 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
9072 | /* Let the front end know that we are processing an explicit | |
9073 | instantiation. */ | |
9074 | begin_explicit_instantiation (); | |
9075 | /* [temp.explicit] says that we are supposed to ignore access | |
9076 | control while processing explicit instantiation directives. */ | |
78757caa | 9077 | push_deferring_access_checks (dk_no_check); |
a723baf1 | 9078 | /* Parse a decl-specifier-seq. */ |
62d1db17 MM |
9079 | cp_parser_decl_specifier_seq (parser, |
9080 | CP_PARSER_FLAGS_OPTIONAL, | |
9081 | &decl_specifiers, | |
9082 | &declares_class_or_enum); | |
a723baf1 MM |
9083 | /* If there was exactly one decl-specifier, and it declared a class, |
9084 | and there's no declarator, then we have an explicit type | |
9085 | instantiation. */ | |
9086 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
9087 | { | |
9088 | tree type; | |
9089 | ||
62d1db17 | 9090 | type = check_tag_decl (&decl_specifiers); |
b7fc8b57 KL |
9091 | /* Turn access control back on for names used during |
9092 | template instantiation. */ | |
9093 | pop_deferring_access_checks (); | |
a723baf1 MM |
9094 | if (type) |
9095 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
9096 | } | |
9097 | else | |
9098 | { | |
058b15c1 | 9099 | cp_declarator *declarator; |
a723baf1 MM |
9100 | tree decl; |
9101 | ||
9102 | /* Parse the declarator. */ | |
21526606 | 9103 | declarator |
62b8a44e | 9104 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 9105 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
9106 | /*parenthesized_p=*/NULL, |
9107 | /*member_p=*/false); | |
fc6a28d7 MM |
9108 | if (declares_class_or_enum & 2) |
9109 | cp_parser_check_for_definition_in_return_type (declarator, | |
9110 | decl_specifiers.type); | |
058b15c1 | 9111 | if (declarator != cp_error_declarator) |
216bb6e1 | 9112 | { |
62d1db17 | 9113 | decl = grokdeclarator (declarator, &decl_specifiers, |
216bb6e1 MM |
9114 | NORMAL, 0, NULL); |
9115 | /* Turn access control back on for names used during | |
9116 | template instantiation. */ | |
9117 | pop_deferring_access_checks (); | |
9118 | /* Do the explicit instantiation. */ | |
9119 | do_decl_instantiation (decl, extension_specifier); | |
9120 | } | |
9121 | else | |
9122 | { | |
9123 | pop_deferring_access_checks (); | |
9124 | /* Skip the body of the explicit instantiation. */ | |
9125 | cp_parser_skip_to_end_of_statement (parser); | |
9126 | } | |
a723baf1 MM |
9127 | } |
9128 | /* We're done with the instantiation. */ | |
9129 | end_explicit_instantiation (); | |
a723baf1 | 9130 | |
e0860732 | 9131 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
9132 | } |
9133 | ||
9134 | /* Parse an explicit-specialization. | |
9135 | ||
9136 | explicit-specialization: | |
21526606 | 9137 | template < > declaration |
a723baf1 MM |
9138 | |
9139 | Although the standard says `declaration', what it really means is: | |
9140 | ||
9141 | explicit-specialization: | |
9142 | template <> decl-specifier [opt] init-declarator [opt] ; | |
21526606 | 9143 | template <> function-definition |
a723baf1 MM |
9144 | template <> explicit-specialization |
9145 | template <> template-declaration */ | |
9146 | ||
9147 | static void | |
94edc4ab | 9148 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
9149 | { |
9150 | /* Look for the `template' keyword. */ | |
9151 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
9152 | /* Look for the `<'. */ | |
9153 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
9154 | /* Look for the `>'. */ | |
9155 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
9156 | /* We have processed another parameter list. */ | |
9157 | ++parser->num_template_parameter_lists; | |
9158 | /* Let the front end know that we are beginning a specialization. */ | |
9159 | begin_specialization (); | |
9160 | ||
9161 | /* If the next keyword is `template', we need to figure out whether | |
9162 | or not we're looking a template-declaration. */ | |
9163 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
9164 | { | |
9165 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
9166 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
9167 | cp_parser_template_declaration_after_export (parser, | |
9168 | /*member_p=*/false); | |
9169 | else | |
9170 | cp_parser_explicit_specialization (parser); | |
9171 | } | |
9172 | else | |
9173 | /* Parse the dependent declaration. */ | |
21526606 | 9174 | cp_parser_single_declaration (parser, |
a723baf1 MM |
9175 | /*member_p=*/false, |
9176 | /*friend_p=*/NULL); | |
9177 | ||
9178 | /* We're done with the specialization. */ | |
9179 | end_specialization (); | |
9180 | /* We're done with this parameter list. */ | |
9181 | --parser->num_template_parameter_lists; | |
9182 | } | |
9183 | ||
9184 | /* Parse a type-specifier. | |
9185 | ||
9186 | type-specifier: | |
9187 | simple-type-specifier | |
9188 | class-specifier | |
9189 | enum-specifier | |
9190 | elaborated-type-specifier | |
9191 | cv-qualifier | |
9192 | ||
9193 | GNU Extension: | |
9194 | ||
9195 | type-specifier: | |
9196 | __complex__ | |
9197 | ||
62d1db17 MM |
9198 | Returns a representation of the type-specifier. For a |
9199 | class-specifier, enum-specifier, or elaborated-type-specifier, a | |
9200 | TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
a723baf1 | 9201 | |
eb1aef53 KL |
9202 | The parser flags FLAGS is used to control type-specifier parsing. |
9203 | ||
9204 | If IS_DECLARATION is TRUE, then this type-specifier is appearing | |
9205 | in a decl-specifier-seq. | |
a723baf1 MM |
9206 | |
9207 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
9208 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 9209 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
9210 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
9211 | zero. | |
a723baf1 MM |
9212 | |
9213 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
9214 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
9215 | is set to FALSE. */ | |
9216 | ||
9217 | static tree | |
21526606 EC |
9218 | cp_parser_type_specifier (cp_parser* parser, |
9219 | cp_parser_flags flags, | |
62d1db17 | 9220 | cp_decl_specifier_seq *decl_specs, |
94edc4ab | 9221 | bool is_declaration, |
560ad596 | 9222 | int* declares_class_or_enum, |
94edc4ab | 9223 | bool* is_cv_qualifier) |
a723baf1 MM |
9224 | { |
9225 | tree type_spec = NULL_TREE; | |
9226 | cp_token *token; | |
9227 | enum rid keyword; | |
62d1db17 | 9228 | cp_decl_spec ds = ds_last; |
a723baf1 MM |
9229 | |
9230 | /* Assume this type-specifier does not declare a new type. */ | |
9231 | if (declares_class_or_enum) | |
543ca912 | 9232 | *declares_class_or_enum = 0; |
a723baf1 MM |
9233 | /* And that it does not specify a cv-qualifier. */ |
9234 | if (is_cv_qualifier) | |
9235 | *is_cv_qualifier = false; | |
9236 | /* Peek at the next token. */ | |
9237 | token = cp_lexer_peek_token (parser->lexer); | |
9238 | ||
9239 | /* If we're looking at a keyword, we can use that to guide the | |
9240 | production we choose. */ | |
9241 | keyword = token->keyword; | |
9242 | switch (keyword) | |
9243 | { | |
ff4eb0b5 ZW |
9244 | case RID_ENUM: |
9245 | /* 'enum' [identifier] '{' introduces an enum-specifier; | |
9246 | 'enum' <anything else> introduces an elaborated-type-specifier. */ | |
9247 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE | |
9248 | || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME | |
9249 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type | |
9250 | == CPP_OPEN_BRACE)) | |
9251 | { | |
a5e51518 KL |
9252 | if (parser->num_template_parameter_lists) |
9253 | { | |
9254 | error ("template declaration of %qs", "enum"); | |
9255 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
9256 | type_spec = error_mark_node; | |
9257 | } | |
9258 | else | |
9259 | type_spec = cp_parser_enum_specifier (parser); | |
9260 | ||
ff4eb0b5 ZW |
9261 | if (declares_class_or_enum) |
9262 | *declares_class_or_enum = 2; | |
9263 | if (decl_specs) | |
9264 | cp_parser_set_decl_spec_type (decl_specs, | |
9265 | type_spec, | |
9266 | /*user_defined_p=*/true); | |
9267 | return type_spec; | |
9268 | } | |
9269 | else | |
9270 | goto elaborated_type_specifier; | |
9271 | ||
a723baf1 MM |
9272 | /* Any of these indicate either a class-specifier, or an |
9273 | elaborated-type-specifier. */ | |
9274 | case RID_CLASS: | |
9275 | case RID_STRUCT: | |
9276 | case RID_UNION: | |
a723baf1 | 9277 | /* Parse tentatively so that we can back up if we don't find a |
ff4eb0b5 | 9278 | class-specifier. */ |
a723baf1 | 9279 | cp_parser_parse_tentatively (parser); |
ff4eb0b5 ZW |
9280 | /* Look for the class-specifier. */ |
9281 | type_spec = cp_parser_class_specifier (parser); | |
a723baf1 MM |
9282 | /* If that worked, we're done. */ |
9283 | if (cp_parser_parse_definitely (parser)) | |
9284 | { | |
9285 | if (declares_class_or_enum) | |
560ad596 | 9286 | *declares_class_or_enum = 2; |
62d1db17 MM |
9287 | if (decl_specs) |
9288 | cp_parser_set_decl_spec_type (decl_specs, | |
9289 | type_spec, | |
9290 | /*user_defined_p=*/true); | |
a723baf1 MM |
9291 | return type_spec; |
9292 | } | |
9293 | ||
9294 | /* Fall through. */ | |
ff4eb0b5 ZW |
9295 | elaborated_type_specifier: |
9296 | /* We're declaring (not defining) a class or enum. */ | |
9297 | if (declares_class_or_enum) | |
9298 | *declares_class_or_enum = 1; | |
a723baf1 | 9299 | |
ff4eb0b5 | 9300 | /* Fall through. */ |
a723baf1 MM |
9301 | case RID_TYPENAME: |
9302 | /* Look for an elaborated-type-specifier. */ | |
98ca843c EC |
9303 | type_spec |
9304 | = (cp_parser_elaborated_type_specifier | |
62d1db17 MM |
9305 | (parser, |
9306 | decl_specs && decl_specs->specs[(int) ds_friend], | |
9307 | is_declaration)); | |
62d1db17 MM |
9308 | if (decl_specs) |
9309 | cp_parser_set_decl_spec_type (decl_specs, | |
9310 | type_spec, | |
9311 | /*user_defined_p=*/true); | |
a723baf1 MM |
9312 | return type_spec; |
9313 | ||
9314 | case RID_CONST: | |
62d1db17 MM |
9315 | ds = ds_const; |
9316 | if (is_cv_qualifier) | |
9317 | *is_cv_qualifier = true; | |
9318 | break; | |
98ca843c | 9319 | |
a723baf1 | 9320 | case RID_VOLATILE: |
62d1db17 | 9321 | ds = ds_volatile; |
a723baf1 MM |
9322 | if (is_cv_qualifier) |
9323 | *is_cv_qualifier = true; | |
62d1db17 | 9324 | break; |
a723baf1 | 9325 | |
62d1db17 MM |
9326 | case RID_RESTRICT: |
9327 | ds = ds_restrict; | |
9328 | if (is_cv_qualifier) | |
9329 | *is_cv_qualifier = true; | |
9330 | break; | |
a723baf1 MM |
9331 | |
9332 | case RID_COMPLEX: | |
9333 | /* The `__complex__' keyword is a GNU extension. */ | |
62d1db17 MM |
9334 | ds = ds_complex; |
9335 | break; | |
a723baf1 MM |
9336 | |
9337 | default: | |
9338 | break; | |
9339 | } | |
9340 | ||
62d1db17 MM |
9341 | /* Handle simple keywords. */ |
9342 | if (ds != ds_last) | |
9343 | { | |
9344 | if (decl_specs) | |
9345 | { | |
9346 | ++decl_specs->specs[(int)ds]; | |
9347 | decl_specs->any_specifiers_p = true; | |
9348 | } | |
9349 | return cp_lexer_consume_token (parser->lexer)->value; | |
9350 | } | |
9351 | ||
a723baf1 MM |
9352 | /* If we do not already have a type-specifier, assume we are looking |
9353 | at a simple-type-specifier. */ | |
98ca843c | 9354 | type_spec = cp_parser_simple_type_specifier (parser, |
62d1db17 MM |
9355 | decl_specs, |
9356 | flags); | |
a723baf1 MM |
9357 | |
9358 | /* If we didn't find a type-specifier, and a type-specifier was not | |
9359 | optional in this context, issue an error message. */ | |
9360 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
9361 | { | |
9362 | cp_parser_error (parser, "expected type specifier"); | |
9363 | return error_mark_node; | |
9364 | } | |
9365 | ||
9366 | return type_spec; | |
9367 | } | |
9368 | ||
9369 | /* Parse a simple-type-specifier. | |
9370 | ||
9371 | simple-type-specifier: | |
9372 | :: [opt] nested-name-specifier [opt] type-name | |
9373 | :: [opt] nested-name-specifier template template-id | |
9374 | char | |
9375 | wchar_t | |
9376 | bool | |
9377 | short | |
9378 | int | |
9379 | long | |
9380 | signed | |
9381 | unsigned | |
9382 | float | |
9383 | double | |
21526606 | 9384 | void |
a723baf1 MM |
9385 | |
9386 | GNU Extension: | |
9387 | ||
9388 | simple-type-specifier: | |
9389 | __typeof__ unary-expression | |
9390 | __typeof__ ( type-id ) | |
9391 | ||
62d1db17 MM |
9392 | Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is |
9393 | appropriately updated. */ | |
a723baf1 MM |
9394 | |
9395 | static tree | |
98ca843c | 9396 | cp_parser_simple_type_specifier (cp_parser* parser, |
62d1db17 MM |
9397 | cp_decl_specifier_seq *decl_specs, |
9398 | cp_parser_flags flags) | |
a723baf1 MM |
9399 | { |
9400 | tree type = NULL_TREE; | |
9401 | cp_token *token; | |
9402 | ||
9403 | /* Peek at the next token. */ | |
9404 | token = cp_lexer_peek_token (parser->lexer); | |
9405 | ||
9406 | /* If we're looking at a keyword, things are easy. */ | |
9407 | switch (token->keyword) | |
9408 | { | |
9409 | case RID_CHAR: | |
62d1db17 MM |
9410 | if (decl_specs) |
9411 | decl_specs->explicit_char_p = true; | |
4b0d3cbe MM |
9412 | type = char_type_node; |
9413 | break; | |
a723baf1 | 9414 | case RID_WCHAR: |
4b0d3cbe MM |
9415 | type = wchar_type_node; |
9416 | break; | |
a723baf1 | 9417 | case RID_BOOL: |
4b0d3cbe MM |
9418 | type = boolean_type_node; |
9419 | break; | |
a723baf1 | 9420 | case RID_SHORT: |
62d1db17 MM |
9421 | if (decl_specs) |
9422 | ++decl_specs->specs[(int) ds_short]; | |
4b0d3cbe MM |
9423 | type = short_integer_type_node; |
9424 | break; | |
a723baf1 | 9425 | case RID_INT: |
62d1db17 MM |
9426 | if (decl_specs) |
9427 | decl_specs->explicit_int_p = true; | |
4b0d3cbe MM |
9428 | type = integer_type_node; |
9429 | break; | |
a723baf1 | 9430 | case RID_LONG: |
62d1db17 MM |
9431 | if (decl_specs) |
9432 | ++decl_specs->specs[(int) ds_long]; | |
4b0d3cbe MM |
9433 | type = long_integer_type_node; |
9434 | break; | |
a723baf1 | 9435 | case RID_SIGNED: |
62d1db17 MM |
9436 | if (decl_specs) |
9437 | ++decl_specs->specs[(int) ds_signed]; | |
4b0d3cbe MM |
9438 | type = integer_type_node; |
9439 | break; | |
a723baf1 | 9440 | case RID_UNSIGNED: |
62d1db17 MM |
9441 | if (decl_specs) |
9442 | ++decl_specs->specs[(int) ds_unsigned]; | |
4b0d3cbe MM |
9443 | type = unsigned_type_node; |
9444 | break; | |
a723baf1 | 9445 | case RID_FLOAT: |
4b0d3cbe MM |
9446 | type = float_type_node; |
9447 | break; | |
a723baf1 | 9448 | case RID_DOUBLE: |
4b0d3cbe MM |
9449 | type = double_type_node; |
9450 | break; | |
a723baf1 | 9451 | case RID_VOID: |
4b0d3cbe MM |
9452 | type = void_type_node; |
9453 | break; | |
a723baf1 MM |
9454 | |
9455 | case RID_TYPEOF: | |
62d1db17 MM |
9456 | /* Consume the `typeof' token. */ |
9457 | cp_lexer_consume_token (parser->lexer); | |
9458 | /* Parse the operand to `typeof'. */ | |
9459 | type = cp_parser_sizeof_operand (parser, RID_TYPEOF); | |
9460 | /* If it is not already a TYPE, take its type. */ | |
9461 | if (!TYPE_P (type)) | |
9462 | type = finish_typeof (type); | |
9463 | ||
9464 | if (decl_specs) | |
9465 | cp_parser_set_decl_spec_type (decl_specs, type, | |
9466 | /*user_defined_p=*/true); | |
98ca843c | 9467 | |
62d1db17 | 9468 | return type; |
a723baf1 MM |
9469 | |
9470 | default: | |
9471 | break; | |
9472 | } | |
9473 | ||
4b0d3cbe MM |
9474 | /* If the type-specifier was for a built-in type, we're done. */ |
9475 | if (type) | |
9476 | { | |
9477 | tree id; | |
9478 | ||
62d1db17 MM |
9479 | /* Record the type. */ |
9480 | if (decl_specs | |
9481 | && (token->keyword != RID_SIGNED | |
9482 | && token->keyword != RID_UNSIGNED | |
9483 | && token->keyword != RID_SHORT | |
9484 | && token->keyword != RID_LONG)) | |
98ca843c | 9485 | cp_parser_set_decl_spec_type (decl_specs, |
62d1db17 MM |
9486 | type, |
9487 | /*user_defined=*/false); | |
9488 | if (decl_specs) | |
9489 | decl_specs->any_specifiers_p = true; | |
9490 | ||
4b0d3cbe MM |
9491 | /* Consume the token. */ |
9492 | id = cp_lexer_consume_token (parser->lexer)->value; | |
0d956474 GB |
9493 | |
9494 | /* There is no valid C++ program where a non-template type is | |
9495 | followed by a "<". That usually indicates that the user thought | |
9496 | that the type was a template. */ | |
9497 | cp_parser_check_for_invalid_template_id (parser, type); | |
9498 | ||
62d1db17 | 9499 | return TYPE_NAME (type); |
4b0d3cbe MM |
9500 | } |
9501 | ||
a723baf1 | 9502 | /* The type-specifier must be a user-defined type. */ |
21526606 | 9503 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) |
a723baf1 | 9504 | { |
0c1a1ecd | 9505 | bool qualified_p; |
f68e4dc8 | 9506 | bool global_p; |
0c1a1ecd | 9507 | |
a723baf1 MM |
9508 | /* Don't gobble tokens or issue error messages if this is an |
9509 | optional type-specifier. */ | |
9510 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
9511 | cp_parser_parse_tentatively (parser); | |
9512 | ||
9513 | /* Look for the optional `::' operator. */ | |
f68e4dc8 | 9514 | global_p |
da740453 MM |
9515 | = (cp_parser_global_scope_opt (parser, |
9516 | /*current_scope_valid_p=*/false) | |
9517 | != NULL_TREE); | |
a723baf1 | 9518 | /* Look for the nested-name specifier. */ |
0c1a1ecd MM |
9519 | qualified_p |
9520 | = (cp_parser_nested_name_specifier_opt (parser, | |
9521 | /*typename_keyword_p=*/false, | |
9522 | /*check_dependency_p=*/true, | |
9523 | /*type_p=*/false, | |
6661a85f EB |
9524 | /*is_declaration=*/false) |
9525 | != NULL_TREE); | |
a723baf1 MM |
9526 | /* If we have seen a nested-name-specifier, and the next token |
9527 | is `template', then we are using the template-id production. */ | |
21526606 | 9528 | if (parser->scope |
a723baf1 MM |
9529 | && cp_parser_optional_template_keyword (parser)) |
9530 | { | |
9531 | /* Look for the template-id. */ | |
21526606 | 9532 | type = cp_parser_template_id (parser, |
a723baf1 | 9533 | /*template_keyword_p=*/true, |
a668c6ad MM |
9534 | /*check_dependency_p=*/true, |
9535 | /*is_declaration=*/false); | |
a723baf1 MM |
9536 | /* If the template-id did not name a type, we are out of |
9537 | luck. */ | |
9538 | if (TREE_CODE (type) != TYPE_DECL) | |
9539 | { | |
9540 | cp_parser_error (parser, "expected template-id for type"); | |
9541 | type = NULL_TREE; | |
9542 | } | |
9543 | } | |
9544 | /* Otherwise, look for a type-name. */ | |
9545 | else | |
4bb8ca28 | 9546 | type = cp_parser_type_name (parser); |
0c1a1ecd | 9547 | /* Keep track of all name-lookups performed in class scopes. */ |
98ca843c | 9548 | if (type |
f68e4dc8 | 9549 | && !global_p |
0c1a1ecd | 9550 | && !qualified_p |
98ca843c | 9551 | && TREE_CODE (type) == TYPE_DECL |
0c1a1ecd MM |
9552 | && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) |
9553 | maybe_note_name_used_in_class (DECL_NAME (type), type); | |
a723baf1 | 9554 | /* If it didn't work out, we don't have a TYPE. */ |
21526606 | 9555 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) |
a723baf1 MM |
9556 | && !cp_parser_parse_definitely (parser)) |
9557 | type = NULL_TREE; | |
62d1db17 MM |
9558 | if (type && decl_specs) |
9559 | cp_parser_set_decl_spec_type (decl_specs, type, | |
9560 | /*user_defined=*/true); | |
a723baf1 MM |
9561 | } |
9562 | ||
9563 | /* If we didn't get a type-name, issue an error message. */ | |
9564 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
9565 | { | |
9566 | cp_parser_error (parser, "expected type-name"); | |
9567 | return error_mark_node; | |
9568 | } | |
9569 | ||
a668c6ad MM |
9570 | /* There is no valid C++ program where a non-template type is |
9571 | followed by a "<". That usually indicates that the user thought | |
9572 | that the type was a template. */ | |
4bb8ca28 | 9573 | if (type && type != error_mark_node) |
ee43dab5 | 9574 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); |
ec75414f | 9575 | |
a723baf1 MM |
9576 | return type; |
9577 | } | |
9578 | ||
9579 | /* Parse a type-name. | |
9580 | ||
9581 | type-name: | |
9582 | class-name | |
9583 | enum-name | |
21526606 | 9584 | typedef-name |
a723baf1 MM |
9585 | |
9586 | enum-name: | |
9587 | identifier | |
9588 | ||
9589 | typedef-name: | |
21526606 | 9590 | identifier |
a723baf1 | 9591 | |
78dcd41a | 9592 | Returns a TYPE_DECL for the type. */ |
a723baf1 MM |
9593 | |
9594 | static tree | |
94edc4ab | 9595 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
9596 | { |
9597 | tree type_decl; | |
9598 | tree identifier; | |
9599 | ||
9600 | /* We can't know yet whether it is a class-name or not. */ | |
9601 | cp_parser_parse_tentatively (parser); | |
9602 | /* Try a class-name. */ | |
21526606 | 9603 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
9604 | /*typename_keyword_p=*/false, |
9605 | /*template_keyword_p=*/false, | |
fc6a28d7 | 9606 | none_type, |
a723baf1 | 9607 | /*check_dependency_p=*/true, |
a668c6ad MM |
9608 | /*class_head_p=*/false, |
9609 | /*is_declaration=*/false); | |
a723baf1 MM |
9610 | /* If it's not a class-name, keep looking. */ |
9611 | if (!cp_parser_parse_definitely (parser)) | |
9612 | { | |
9613 | /* It must be a typedef-name or an enum-name. */ | |
9614 | identifier = cp_parser_identifier (parser); | |
9615 | if (identifier == error_mark_node) | |
9616 | return error_mark_node; | |
21526606 | 9617 | |
a723baf1 MM |
9618 | /* Look up the type-name. */ |
9619 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
9620 | /* Issue an error if we did not find a type-name. */ | |
9621 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
9622 | { | |
4bb8ca28 | 9623 | if (!cp_parser_simulate_error (parser)) |
21526606 | 9624 | cp_parser_name_lookup_error (parser, identifier, type_decl, |
4bb8ca28 | 9625 | "is not a type"); |
a723baf1 MM |
9626 | type_decl = error_mark_node; |
9627 | } | |
9628 | /* Remember that the name was used in the definition of the | |
9629 | current class so that we can check later to see if the | |
9630 | meaning would have been different after the class was | |
9631 | entirely defined. */ | |
9632 | else if (type_decl != error_mark_node | |
9633 | && !parser->scope) | |
9634 | maybe_note_name_used_in_class (identifier, type_decl); | |
9635 | } | |
21526606 | 9636 | |
a723baf1 MM |
9637 | return type_decl; |
9638 | } | |
9639 | ||
9640 | ||
9641 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
9642 | here incorporates the resolution to DR68. | |
9643 | ||
9644 | elaborated-type-specifier: | |
9645 | class-key :: [opt] nested-name-specifier [opt] identifier | |
9646 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
9647 | enum :: [opt] nested-name-specifier [opt] identifier | |
9648 | typename :: [opt] nested-name-specifier identifier | |
21526606 EC |
9649 | typename :: [opt] nested-name-specifier template [opt] |
9650 | template-id | |
a723baf1 | 9651 | |
360d1b99 MM |
9652 | GNU extension: |
9653 | ||
9654 | elaborated-type-specifier: | |
9655 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
21526606 | 9656 | class-key attributes :: [opt] nested-name-specifier [opt] |
360d1b99 MM |
9657 | template [opt] template-id |
9658 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
9659 | ||
a723baf1 MM |
9660 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
9661 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
9662 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
9663 | something is being declared. | |
9664 | ||
9665 | Returns the TYPE specified. */ | |
9666 | ||
9667 | static tree | |
21526606 EC |
9668 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
9669 | bool is_friend, | |
94edc4ab | 9670 | bool is_declaration) |
a723baf1 MM |
9671 | { |
9672 | enum tag_types tag_type; | |
9673 | tree identifier; | |
9674 | tree type = NULL_TREE; | |
360d1b99 | 9675 | tree attributes = NULL_TREE; |
a723baf1 MM |
9676 | |
9677 | /* See if we're looking at the `enum' keyword. */ | |
9678 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
9679 | { | |
9680 | /* Consume the `enum' token. */ | |
9681 | cp_lexer_consume_token (parser->lexer); | |
9682 | /* Remember that it's an enumeration type. */ | |
9683 | tag_type = enum_type; | |
360d1b99 MM |
9684 | /* Parse the attributes. */ |
9685 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9686 | } |
9687 | /* Or, it might be `typename'. */ | |
9688 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
9689 | RID_TYPENAME)) | |
9690 | { | |
9691 | /* Consume the `typename' token. */ | |
9692 | cp_lexer_consume_token (parser->lexer); | |
9693 | /* Remember that it's a `typename' type. */ | |
9694 | tag_type = typename_type; | |
9695 | /* The `typename' keyword is only allowed in templates. */ | |
9696 | if (!processing_template_decl) | |
2a13a625 | 9697 | pedwarn ("using %<typename%> outside of template"); |
a723baf1 MM |
9698 | } |
9699 | /* Otherwise it must be a class-key. */ | |
9700 | else | |
9701 | { | |
9702 | tag_type = cp_parser_class_key (parser); | |
9703 | if (tag_type == none_type) | |
9704 | return error_mark_node; | |
360d1b99 MM |
9705 | /* Parse the attributes. */ |
9706 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9707 | } |
9708 | ||
9709 | /* Look for the `::' operator. */ | |
21526606 | 9710 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
9711 | /*current_scope_valid_p=*/false); |
9712 | /* Look for the nested-name-specifier. */ | |
9713 | if (tag_type == typename_type) | |
8fa1ad0e MM |
9714 | { |
9715 | if (cp_parser_nested_name_specifier (parser, | |
9716 | /*typename_keyword_p=*/true, | |
9717 | /*check_dependency_p=*/true, | |
a668c6ad | 9718 | /*type_p=*/true, |
21526606 | 9719 | is_declaration) |
8fa1ad0e MM |
9720 | == error_mark_node) |
9721 | return error_mark_node; | |
9722 | } | |
a723baf1 MM |
9723 | else |
9724 | /* Even though `typename' is not present, the proposed resolution | |
9725 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
9726 | considered a type-name, even if `A<T>' is dependent. */ | |
9727 | cp_parser_nested_name_specifier_opt (parser, | |
9728 | /*typename_keyword_p=*/true, | |
9729 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9730 | /*type_p=*/true, |
9731 | is_declaration); | |
a723baf1 MM |
9732 | /* For everything but enumeration types, consider a template-id. */ |
9733 | if (tag_type != enum_type) | |
9734 | { | |
9735 | bool template_p = false; | |
9736 | tree decl; | |
9737 | ||
9738 | /* Allow the `template' keyword. */ | |
9739 | template_p = cp_parser_optional_template_keyword (parser); | |
9740 | /* If we didn't see `template', we don't know if there's a | |
9741 | template-id or not. */ | |
9742 | if (!template_p) | |
9743 | cp_parser_parse_tentatively (parser); | |
9744 | /* Parse the template-id. */ | |
9745 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
9746 | /*check_dependency_p=*/true, |
9747 | is_declaration); | |
a723baf1 MM |
9748 | /* If we didn't find a template-id, look for an ordinary |
9749 | identifier. */ | |
9750 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
9751 | ; | |
9752 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
9753 | in effect, then we must assume that, upon instantiation, the | |
9754 | template will correspond to a class. */ | |
9755 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
9756 | && tag_type == typename_type) | |
9757 | type = make_typename_type (parser->scope, decl, | |
fc6a28d7 | 9758 | typename_type, |
a723baf1 | 9759 | /*complain=*/1); |
21526606 | 9760 | else |
a723baf1 MM |
9761 | type = TREE_TYPE (decl); |
9762 | } | |
9763 | ||
9764 | /* For an enumeration type, consider only a plain identifier. */ | |
9765 | if (!type) | |
9766 | { | |
9767 | identifier = cp_parser_identifier (parser); | |
9768 | ||
9769 | if (identifier == error_mark_node) | |
eb5abb39 NS |
9770 | { |
9771 | parser->scope = NULL_TREE; | |
9772 | return error_mark_node; | |
9773 | } | |
a723baf1 MM |
9774 | |
9775 | /* For a `typename', we needn't call xref_tag. */ | |
0c88d886 MM |
9776 | if (tag_type == typename_type |
9777 | && TREE_CODE (parser->scope) != NAMESPACE_DECL) | |
21526606 | 9778 | return cp_parser_make_typename_type (parser, parser->scope, |
2097b5f2 | 9779 | identifier); |
a723baf1 MM |
9780 | /* Look up a qualified name in the usual way. */ |
9781 | if (parser->scope) | |
9782 | { | |
9783 | tree decl; | |
9784 | ||
21526606 | 9785 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 9786 | tag_type, |
b0bc6e8e | 9787 | /*is_template=*/false, |
eea9800f | 9788 | /*is_namespace=*/false, |
8f78f01f MM |
9789 | /*check_dependency=*/true, |
9790 | /*ambiguous_p=*/NULL); | |
710b73e6 KL |
9791 | |
9792 | /* If we are parsing friend declaration, DECL may be a | |
9793 | TEMPLATE_DECL tree node here. However, we need to check | |
9794 | whether this TEMPLATE_DECL results in valid code. Consider | |
9795 | the following example: | |
9796 | ||
9797 | namespace N { | |
9798 | template <class T> class C {}; | |
9799 | } | |
9800 | class X { | |
9801 | template <class T> friend class N::C; // #1, valid code | |
9802 | }; | |
9803 | template <class T> class Y { | |
9804 | friend class N::C; // #2, invalid code | |
9805 | }; | |
9806 | ||
9807 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
9808 | name lookup of `N::C'. We see that friend declaration must | |
9809 | be template for the code to be valid. Note that | |
9810 | processing_template_decl does not work here since it is | |
9811 | always 1 for the above two cases. */ | |
9812 | ||
21526606 | 9813 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
9814 | (decl, /*tag_name_p=*/is_friend |
9815 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
9816 | |
9817 | if (TREE_CODE (decl) != TYPE_DECL) | |
9818 | { | |
0c88d886 MM |
9819 | cp_parser_diagnose_invalid_type_name (parser, |
9820 | parser->scope, | |
9821 | identifier); | |
a723baf1 MM |
9822 | return error_mark_node; |
9823 | } | |
560ad596 MM |
9824 | |
9825 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
21526606 | 9826 | check_elaborated_type_specifier |
4b0d3cbe | 9827 | (tag_type, decl, |
560ad596 MM |
9828 | (parser->num_template_parameter_lists |
9829 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
9830 | |
9831 | type = TREE_TYPE (decl); | |
9832 | } | |
21526606 | 9833 | else |
a723baf1 MM |
9834 | { |
9835 | /* An elaborated-type-specifier sometimes introduces a new type and | |
9836 | sometimes names an existing type. Normally, the rule is that it | |
9837 | introduces a new type only if there is not an existing type of | |
9838 | the same name already in scope. For example, given: | |
9839 | ||
9840 | struct S {}; | |
9841 | void f() { struct S s; } | |
9842 | ||
9843 | the `struct S' in the body of `f' is the same `struct S' as in | |
9844 | the global scope; the existing definition is used. However, if | |
21526606 | 9845 | there were no global declaration, this would introduce a new |
a723baf1 MM |
9846 | local class named `S'. |
9847 | ||
9848 | An exception to this rule applies to the following code: | |
9849 | ||
9850 | namespace N { struct S; } | |
9851 | ||
9852 | Here, the elaborated-type-specifier names a new type | |
9853 | unconditionally; even if there is already an `S' in the | |
9854 | containing scope this declaration names a new type. | |
9855 | This exception only applies if the elaborated-type-specifier | |
9856 | forms the complete declaration: | |
9857 | ||
21526606 | 9858 | [class.name] |
a723baf1 MM |
9859 | |
9860 | A declaration consisting solely of `class-key identifier ;' is | |
9861 | either a redeclaration of the name in the current scope or a | |
9862 | forward declaration of the identifier as a class name. It | |
9863 | introduces the name into the current scope. | |
9864 | ||
9865 | We are in this situation precisely when the next token is a `;'. | |
9866 | ||
9867 | An exception to the exception is that a `friend' declaration does | |
9868 | *not* name a new type; i.e., given: | |
9869 | ||
9870 | struct S { friend struct T; }; | |
9871 | ||
21526606 | 9872 | `T' is not a new type in the scope of `S'. |
a723baf1 MM |
9873 | |
9874 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
9875 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 9876 | declaration context. */ |
a723baf1 | 9877 | |
29ef83de KL |
9878 | tag_scope ts; |
9879 | if (is_friend) | |
9880 | /* Friends have special name lookup rules. */ | |
9881 | ts = ts_within_enclosing_non_class; | |
9882 | else if (is_declaration | |
9883 | && cp_lexer_next_token_is (parser->lexer, | |
9884 | CPP_SEMICOLON)) | |
9885 | /* This is a `class-key identifier ;' */ | |
9886 | ts = ts_current; | |
9887 | else | |
9888 | ts = ts_global; | |
9889 | ||
e0fed25b DS |
9890 | /* Warn about attributes. They are ignored. */ |
9891 | if (attributes) | |
9892 | warning ("type attributes are honored only at type definition"); | |
9893 | ||
29ef83de | 9894 | type = xref_tag (tag_type, identifier, ts, |
cbd63935 | 9895 | parser->num_template_parameter_lists); |
a723baf1 MM |
9896 | } |
9897 | } | |
9898 | if (tag_type != enum_type) | |
9899 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
9900 | |
9901 | /* A "<" cannot follow an elaborated type specifier. If that | |
9902 | happens, the user was probably trying to form a template-id. */ | |
9903 | cp_parser_check_for_invalid_template_id (parser, type); | |
9904 | ||
a723baf1 MM |
9905 | return type; |
9906 | } | |
9907 | ||
9908 | /* Parse an enum-specifier. | |
9909 | ||
9910 | enum-specifier: | |
9911 | enum identifier [opt] { enumerator-list [opt] } | |
9912 | ||
f6af9a15 MA |
9913 | GNU Extensions: |
9914 | enum identifier [opt] { enumerator-list [opt] } attributes | |
9915 | ||
a723baf1 MM |
9916 | Returns an ENUM_TYPE representing the enumeration. */ |
9917 | ||
9918 | static tree | |
94edc4ab | 9919 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 | 9920 | { |
ff4eb0b5 | 9921 | tree identifier; |
a723baf1 MM |
9922 | tree type; |
9923 | ||
ff4eb0b5 ZW |
9924 | /* Caller guarantees that the current token is 'enum', an identifier |
9925 | possibly follows, and the token after that is an opening brace. | |
9926 | If we don't have an identifier, fabricate an anonymous name for | |
9927 | the enumeration being defined. */ | |
9928 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 9929 | |
ff4eb0b5 | 9930 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) |
a723baf1 | 9931 | identifier = cp_parser_identifier (parser); |
ff4eb0b5 ZW |
9932 | else |
9933 | identifier = make_anon_name (); | |
a723baf1 | 9934 | |
a723baf1 MM |
9935 | /* Issue an error message if type-definitions are forbidden here. */ |
9936 | cp_parser_check_type_definition (parser); | |
9937 | ||
2cfe82fe ZW |
9938 | /* Create the new type. We do this before consuming the opening brace |
9939 | so the enum will be recorded as being on the line of its tag (or the | |
9940 | 'enum' keyword, if there is no tag). */ | |
ff4eb0b5 | 9941 | type = start_enum (identifier); |
a723baf1 | 9942 | |
2cfe82fe ZW |
9943 | /* Consume the opening brace. */ |
9944 | cp_lexer_consume_token (parser->lexer); | |
9945 | ||
ff4eb0b5 ZW |
9946 | /* If the next token is not '}', then there are some enumerators. */ |
9947 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
a723baf1 | 9948 | cp_parser_enumerator_list (parser, type); |
ff4eb0b5 ZW |
9949 | |
9950 | /* Consume the final '}'. */ | |
a723baf1 MM |
9951 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); |
9952 | ||
f6af9a15 | 9953 | /* Look for trailing attributes to apply to this enumeration, and |
03fd3f84 | 9954 | apply them if appropriate. */ |
f6af9a15 MA |
9955 | if (cp_parser_allow_gnu_extensions_p (parser)) |
9956 | { | |
9957 | tree trailing_attr = cp_parser_attributes_opt (parser); | |
9958 | cplus_decl_attributes (&type, | |
9959 | trailing_attr, | |
9960 | (int) ATTR_FLAG_TYPE_IN_PLACE); | |
9961 | } | |
9962 | ||
a723baf1 MM |
9963 | /* Finish up the enumeration. */ |
9964 | finish_enum (type); | |
9965 | ||
9966 | return type; | |
9967 | } | |
9968 | ||
9969 | /* Parse an enumerator-list. The enumerators all have the indicated | |
21526606 | 9970 | TYPE. |
a723baf1 MM |
9971 | |
9972 | enumerator-list: | |
9973 | enumerator-definition | |
9974 | enumerator-list , enumerator-definition */ | |
9975 | ||
9976 | static void | |
94edc4ab | 9977 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
9978 | { |
9979 | while (true) | |
9980 | { | |
a723baf1 MM |
9981 | /* Parse an enumerator-definition. */ |
9982 | cp_parser_enumerator_definition (parser, type); | |
ff4eb0b5 ZW |
9983 | |
9984 | /* If the next token is not a ',', we've reached the end of | |
9985 | the list. */ | |
9986 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
a723baf1 MM |
9987 | break; |
9988 | /* Otherwise, consume the `,' and keep going. */ | |
9989 | cp_lexer_consume_token (parser->lexer); | |
9990 | /* If the next token is a `}', there is a trailing comma. */ | |
9991 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
9992 | { | |
9993 | if (pedantic && !in_system_header) | |
9994 | pedwarn ("comma at end of enumerator list"); | |
9995 | break; | |
9996 | } | |
9997 | } | |
9998 | } | |
9999 | ||
10000 | /* Parse an enumerator-definition. The enumerator has the indicated | |
10001 | TYPE. | |
10002 | ||
10003 | enumerator-definition: | |
10004 | enumerator | |
10005 | enumerator = constant-expression | |
21526606 | 10006 | |
a723baf1 MM |
10007 | enumerator: |
10008 | identifier */ | |
10009 | ||
10010 | static void | |
94edc4ab | 10011 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 | 10012 | { |
a723baf1 MM |
10013 | tree identifier; |
10014 | tree value; | |
10015 | ||
10016 | /* Look for the identifier. */ | |
10017 | identifier = cp_parser_identifier (parser); | |
10018 | if (identifier == error_mark_node) | |
10019 | return; | |
21526606 | 10020 | |
ff4eb0b5 ZW |
10021 | /* If the next token is an '=', then there is an explicit value. */ |
10022 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
a723baf1 MM |
10023 | { |
10024 | /* Consume the `=' token. */ | |
10025 | cp_lexer_consume_token (parser->lexer); | |
10026 | /* Parse the value. */ | |
21526606 | 10027 | value = cp_parser_constant_expression (parser, |
d17811fd | 10028 | /*allow_non_constant_p=*/false, |
14d22dd6 | 10029 | NULL); |
a723baf1 MM |
10030 | } |
10031 | else | |
10032 | value = NULL_TREE; | |
10033 | ||
10034 | /* Create the enumerator. */ | |
10035 | build_enumerator (identifier, value, type); | |
10036 | } | |
10037 | ||
10038 | /* Parse a namespace-name. | |
10039 | ||
10040 | namespace-name: | |
10041 | original-namespace-name | |
10042 | namespace-alias | |
10043 | ||
10044 | Returns the NAMESPACE_DECL for the namespace. */ | |
10045 | ||
10046 | static tree | |
94edc4ab | 10047 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
10048 | { |
10049 | tree identifier; | |
10050 | tree namespace_decl; | |
10051 | ||
10052 | /* Get the name of the namespace. */ | |
10053 | identifier = cp_parser_identifier (parser); | |
10054 | if (identifier == error_mark_node) | |
10055 | return error_mark_node; | |
10056 | ||
eea9800f MM |
10057 | /* Look up the identifier in the currently active scope. Look only |
10058 | for namespaces, due to: | |
10059 | ||
10060 | [basic.lookup.udir] | |
10061 | ||
10062 | When looking up a namespace-name in a using-directive or alias | |
21526606 | 10063 | definition, only namespace names are considered. |
eea9800f MM |
10064 | |
10065 | And: | |
10066 | ||
10067 | [basic.lookup.qual] | |
10068 | ||
10069 | During the lookup of a name preceding the :: scope resolution | |
21526606 | 10070 | operator, object, function, and enumerator names are ignored. |
eea9800f MM |
10071 | |
10072 | (Note that cp_parser_class_or_namespace_name only calls this | |
10073 | function if the token after the name is the scope resolution | |
10074 | operator.) */ | |
10075 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
fc6a28d7 | 10076 | none_type, |
b0bc6e8e | 10077 | /*is_template=*/false, |
eea9800f | 10078 | /*is_namespace=*/true, |
8f78f01f MM |
10079 | /*check_dependency=*/true, |
10080 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
10081 | /* If it's not a namespace, issue an error. */ |
10082 | if (namespace_decl == error_mark_node | |
10083 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
10084 | { | |
10085 | cp_parser_error (parser, "expected namespace-name"); | |
10086 | namespace_decl = error_mark_node; | |
10087 | } | |
21526606 | 10088 | |
a723baf1 MM |
10089 | return namespace_decl; |
10090 | } | |
10091 | ||
10092 | /* Parse a namespace-definition. | |
10093 | ||
10094 | namespace-definition: | |
10095 | named-namespace-definition | |
21526606 | 10096 | unnamed-namespace-definition |
a723baf1 MM |
10097 | |
10098 | named-namespace-definition: | |
10099 | original-namespace-definition | |
10100 | extension-namespace-definition | |
10101 | ||
10102 | original-namespace-definition: | |
10103 | namespace identifier { namespace-body } | |
21526606 | 10104 | |
a723baf1 MM |
10105 | extension-namespace-definition: |
10106 | namespace original-namespace-name { namespace-body } | |
21526606 | 10107 | |
a723baf1 MM |
10108 | unnamed-namespace-definition: |
10109 | namespace { namespace-body } */ | |
10110 | ||
10111 | static void | |
94edc4ab | 10112 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
10113 | { |
10114 | tree identifier; | |
10115 | ||
10116 | /* Look for the `namespace' keyword. */ | |
10117 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10118 | ||
10119 | /* Get the name of the namespace. We do not attempt to distinguish | |
10120 | between an original-namespace-definition and an | |
10121 | extension-namespace-definition at this point. The semantic | |
10122 | analysis routines are responsible for that. */ | |
10123 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
10124 | identifier = cp_parser_identifier (parser); | |
10125 | else | |
10126 | identifier = NULL_TREE; | |
10127 | ||
10128 | /* Look for the `{' to start the namespace. */ | |
10129 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
10130 | /* Start the namespace. */ | |
10131 | push_namespace (identifier); | |
10132 | /* Parse the body of the namespace. */ | |
10133 | cp_parser_namespace_body (parser); | |
10134 | /* Finish the namespace. */ | |
10135 | pop_namespace (); | |
10136 | /* Look for the final `}'. */ | |
10137 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
10138 | } | |
10139 | ||
10140 | /* Parse a namespace-body. | |
10141 | ||
10142 | namespace-body: | |
10143 | declaration-seq [opt] */ | |
10144 | ||
10145 | static void | |
94edc4ab | 10146 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
10147 | { |
10148 | cp_parser_declaration_seq_opt (parser); | |
10149 | } | |
10150 | ||
10151 | /* Parse a namespace-alias-definition. | |
10152 | ||
10153 | namespace-alias-definition: | |
10154 | namespace identifier = qualified-namespace-specifier ; */ | |
10155 | ||
10156 | static void | |
94edc4ab | 10157 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
10158 | { |
10159 | tree identifier; | |
10160 | tree namespace_specifier; | |
10161 | ||
10162 | /* Look for the `namespace' keyword. */ | |
10163 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10164 | /* Look for the identifier. */ | |
10165 | identifier = cp_parser_identifier (parser); | |
10166 | if (identifier == error_mark_node) | |
10167 | return; | |
10168 | /* Look for the `=' token. */ | |
10169 | cp_parser_require (parser, CPP_EQ, "`='"); | |
10170 | /* Look for the qualified-namespace-specifier. */ | |
21526606 | 10171 | namespace_specifier |
a723baf1 MM |
10172 | = cp_parser_qualified_namespace_specifier (parser); |
10173 | /* Look for the `;' token. */ | |
10174 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10175 | ||
10176 | /* Register the alias in the symbol table. */ | |
10177 | do_namespace_alias (identifier, namespace_specifier); | |
10178 | } | |
10179 | ||
10180 | /* Parse a qualified-namespace-specifier. | |
10181 | ||
10182 | qualified-namespace-specifier: | |
10183 | :: [opt] nested-name-specifier [opt] namespace-name | |
10184 | ||
10185 | Returns a NAMESPACE_DECL corresponding to the specified | |
10186 | namespace. */ | |
10187 | ||
10188 | static tree | |
94edc4ab | 10189 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
10190 | { |
10191 | /* Look for the optional `::'. */ | |
21526606 | 10192 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
10193 | /*current_scope_valid_p=*/false); |
10194 | ||
10195 | /* Look for the optional nested-name-specifier. */ | |
10196 | cp_parser_nested_name_specifier_opt (parser, | |
10197 | /*typename_keyword_p=*/false, | |
10198 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10199 | /*type_p=*/false, |
10200 | /*is_declaration=*/true); | |
a723baf1 MM |
10201 | |
10202 | return cp_parser_namespace_name (parser); | |
10203 | } | |
10204 | ||
10205 | /* Parse a using-declaration. | |
10206 | ||
10207 | using-declaration: | |
10208 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
10209 | using :: unqualified-id ; */ | |
10210 | ||
10211 | static void | |
94edc4ab | 10212 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
10213 | { |
10214 | cp_token *token; | |
10215 | bool typename_p = false; | |
10216 | bool global_scope_p; | |
10217 | tree decl; | |
10218 | tree identifier; | |
ed5f054f | 10219 | tree qscope; |
a723baf1 MM |
10220 | |
10221 | /* Look for the `using' keyword. */ | |
10222 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
21526606 | 10223 | |
a723baf1 MM |
10224 | /* Peek at the next token. */ |
10225 | token = cp_lexer_peek_token (parser->lexer); | |
10226 | /* See if it's `typename'. */ | |
10227 | if (token->keyword == RID_TYPENAME) | |
10228 | { | |
10229 | /* Remember that we've seen it. */ | |
10230 | typename_p = true; | |
10231 | /* Consume the `typename' token. */ | |
10232 | cp_lexer_consume_token (parser->lexer); | |
10233 | } | |
10234 | ||
10235 | /* Look for the optional global scope qualification. */ | |
21526606 | 10236 | global_scope_p |
a723baf1 | 10237 | = (cp_parser_global_scope_opt (parser, |
21526606 | 10238 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
10239 | != NULL_TREE); |
10240 | ||
10241 | /* If we saw `typename', or didn't see `::', then there must be a | |
10242 | nested-name-specifier present. */ | |
10243 | if (typename_p || !global_scope_p) | |
21526606 | 10244 | qscope = cp_parser_nested_name_specifier (parser, typename_p, |
ed5f054f AO |
10245 | /*check_dependency_p=*/true, |
10246 | /*type_p=*/false, | |
10247 | /*is_declaration=*/true); | |
a723baf1 MM |
10248 | /* Otherwise, we could be in either of the two productions. In that |
10249 | case, treat the nested-name-specifier as optional. */ | |
10250 | else | |
ed5f054f AO |
10251 | qscope = cp_parser_nested_name_specifier_opt (parser, |
10252 | /*typename_keyword_p=*/false, | |
10253 | /*check_dependency_p=*/true, | |
10254 | /*type_p=*/false, | |
10255 | /*is_declaration=*/true); | |
10256 | if (!qscope) | |
10257 | qscope = global_namespace; | |
a723baf1 MM |
10258 | |
10259 | /* Parse the unqualified-id. */ | |
21526606 | 10260 | identifier = cp_parser_unqualified_id (parser, |
a723baf1 | 10261 | /*template_keyword_p=*/false, |
f3c2dfc6 MM |
10262 | /*check_dependency_p=*/true, |
10263 | /*declarator_p=*/true); | |
a723baf1 MM |
10264 | |
10265 | /* The function we call to handle a using-declaration is different | |
10266 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
10267 | if (identifier == error_mark_node) |
10268 | ; | |
10269 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
10270 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
10271 | /* [namespace.udecl] | |
10272 | ||
10273 | A using declaration shall not name a template-id. */ | |
10274 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
10275 | else |
10276 | { | |
a5201a91 | 10277 | if (at_class_scope_p ()) |
4eb6d609 | 10278 | { |
f3c2dfc6 | 10279 | /* Create the USING_DECL. */ |
1d786913 | 10280 | decl = do_class_using_decl (parser->scope, identifier); |
f3c2dfc6 MM |
10281 | /* Add it to the list of members in this class. */ |
10282 | finish_member_declaration (decl); | |
4eb6d609 | 10283 | } |
a723baf1 | 10284 | else |
f3c2dfc6 MM |
10285 | { |
10286 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
10287 | if (decl == error_mark_node) | |
4bb8ca28 | 10288 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
a5201a91 | 10289 | else if (!at_namespace_scope_p ()) |
ed5f054f | 10290 | do_local_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 10291 | else |
ed5f054f | 10292 | do_toplevel_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 10293 | } |
a723baf1 MM |
10294 | } |
10295 | ||
10296 | /* Look for the final `;'. */ | |
10297 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10298 | } | |
10299 | ||
21526606 EC |
10300 | /* Parse a using-directive. |
10301 | ||
a723baf1 MM |
10302 | using-directive: |
10303 | using namespace :: [opt] nested-name-specifier [opt] | |
10304 | namespace-name ; */ | |
10305 | ||
10306 | static void | |
94edc4ab | 10307 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
10308 | { |
10309 | tree namespace_decl; | |
86098eb8 | 10310 | tree attribs; |
a723baf1 MM |
10311 | |
10312 | /* Look for the `using' keyword. */ | |
10313 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
10314 | /* And the `namespace' keyword. */ | |
10315 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
10316 | /* Look for the optional `::' operator. */ | |
10317 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 10318 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
10319 | cp_parser_nested_name_specifier_opt (parser, |
10320 | /*typename_keyword_p=*/false, | |
10321 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10322 | /*type_p=*/false, |
10323 | /*is_declaration=*/true); | |
a723baf1 MM |
10324 | /* Get the namespace being used. */ |
10325 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
10326 | /* And any specified attributes. */ |
10327 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 10328 | /* Update the symbol table. */ |
86098eb8 | 10329 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
10330 | /* Look for the final `;'. */ |
10331 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
10332 | } | |
10333 | ||
10334 | /* Parse an asm-definition. | |
10335 | ||
10336 | asm-definition: | |
21526606 | 10337 | asm ( string-literal ) ; |
a723baf1 MM |
10338 | |
10339 | GNU Extension: | |
10340 | ||
10341 | asm-definition: | |
10342 | asm volatile [opt] ( string-literal ) ; | |
10343 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
10344 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
10345 | : asm-operand-list [opt] ) ; | |
21526606 EC |
10346 | asm volatile [opt] ( string-literal : asm-operand-list [opt] |
10347 | : asm-operand-list [opt] | |
a723baf1 MM |
10348 | : asm-operand-list [opt] ) ; */ |
10349 | ||
10350 | static void | |
94edc4ab | 10351 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 | 10352 | { |
a723baf1 MM |
10353 | tree string; |
10354 | tree outputs = NULL_TREE; | |
10355 | tree inputs = NULL_TREE; | |
10356 | tree clobbers = NULL_TREE; | |
10357 | tree asm_stmt; | |
10358 | bool volatile_p = false; | |
10359 | bool extended_p = false; | |
10360 | ||
10361 | /* Look for the `asm' keyword. */ | |
10362 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
10363 | /* See if the next token is `volatile'. */ | |
10364 | if (cp_parser_allow_gnu_extensions_p (parser) | |
10365 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
10366 | { | |
10367 | /* Remember that we saw the `volatile' keyword. */ | |
10368 | volatile_p = true; | |
10369 | /* Consume the token. */ | |
10370 | cp_lexer_consume_token (parser->lexer); | |
10371 | } | |
10372 | /* Look for the opening `('. */ | |
c162c75e MA |
10373 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
10374 | return; | |
a723baf1 | 10375 | /* Look for the string. */ |
c162c75e MA |
10376 | string = cp_parser_string_literal (parser, false, false); |
10377 | if (string == error_mark_node) | |
10378 | { | |
10379 | cp_parser_skip_to_closing_parenthesis (parser, true, false, | |
10380 | /*consume_paren=*/true); | |
10381 | return; | |
10382 | } | |
10383 | ||
a723baf1 | 10384 | /* If we're allowing GNU extensions, check for the extended assembly |
21526606 | 10385 | syntax. Unfortunately, the `:' tokens need not be separated by |
a723baf1 MM |
10386 | a space in C, and so, for compatibility, we tolerate that here |
10387 | too. Doing that means that we have to treat the `::' operator as | |
10388 | two `:' tokens. */ | |
10389 | if (cp_parser_allow_gnu_extensions_p (parser) | |
10390 | && at_function_scope_p () | |
10391 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
10392 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
10393 | { | |
10394 | bool inputs_p = false; | |
10395 | bool clobbers_p = false; | |
10396 | ||
10397 | /* The extended syntax was used. */ | |
10398 | extended_p = true; | |
10399 | ||
10400 | /* Look for outputs. */ | |
10401 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
10402 | { | |
10403 | /* Consume the `:'. */ | |
10404 | cp_lexer_consume_token (parser->lexer); | |
10405 | /* Parse the output-operands. */ | |
21526606 | 10406 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
10407 | CPP_COLON) |
10408 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
10409 | CPP_SCOPE) |
10410 | && cp_lexer_next_token_is_not (parser->lexer, | |
10411 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
10412 | outputs = cp_parser_asm_operand_list (parser); |
10413 | } | |
10414 | /* If the next token is `::', there are no outputs, and the | |
10415 | next token is the beginning of the inputs. */ | |
10416 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
bf5930d4 JB |
10417 | /* The inputs are coming next. */ |
10418 | inputs_p = true; | |
a723baf1 MM |
10419 | |
10420 | /* Look for inputs. */ | |
10421 | if (inputs_p | |
10422 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
10423 | { | |
bf5930d4 JB |
10424 | /* Consume the `:' or `::'. */ |
10425 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 10426 | /* Parse the output-operands. */ |
21526606 | 10427 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 | 10428 | CPP_COLON) |
8caf4c38 MM |
10429 | && cp_lexer_next_token_is_not (parser->lexer, |
10430 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
10431 | inputs = cp_parser_asm_operand_list (parser); |
10432 | } | |
10433 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
10434 | /* The clobbers are coming next. */ | |
10435 | clobbers_p = true; | |
10436 | ||
10437 | /* Look for clobbers. */ | |
21526606 | 10438 | if (clobbers_p |
a723baf1 MM |
10439 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) |
10440 | { | |
bf5930d4 JB |
10441 | /* Consume the `:' or `::'. */ |
10442 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 10443 | /* Parse the clobbers. */ |
8caf4c38 MM |
10444 | if (cp_lexer_next_token_is_not (parser->lexer, |
10445 | CPP_CLOSE_PAREN)) | |
10446 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
10447 | } |
10448 | } | |
10449 | /* Look for the closing `)'. */ | |
10450 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
10451 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
10452 | /*consume_paren=*/true); | |
a723baf1 MM |
10453 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
10454 | ||
e130a54b | 10455 | /* Create the ASM_EXPR. */ |
a723baf1 MM |
10456 | if (at_function_scope_p ()) |
10457 | { | |
6de9cd9a DN |
10458 | asm_stmt = finish_asm_stmt (volatile_p, string, outputs, |
10459 | inputs, clobbers); | |
e130a54b | 10460 | /* If the extended syntax was not used, mark the ASM_EXPR. */ |
a723baf1 | 10461 | if (!extended_p) |
ca059043 AP |
10462 | { |
10463 | tree temp = asm_stmt; | |
10464 | if (TREE_CODE (temp) == CLEANUP_POINT_EXPR) | |
10465 | temp = TREE_OPERAND (temp, 0); | |
10466 | ||
10467 | ASM_INPUT_P (temp) = 1; | |
10468 | } | |
a723baf1 MM |
10469 | } |
10470 | else | |
10471 | assemble_asm (string); | |
10472 | } | |
10473 | ||
10474 | /* Declarators [gram.dcl.decl] */ | |
10475 | ||
10476 | /* Parse an init-declarator. | |
10477 | ||
10478 | init-declarator: | |
10479 | declarator initializer [opt] | |
10480 | ||
10481 | GNU Extension: | |
10482 | ||
10483 | init-declarator: | |
10484 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
10485 | ||
4bb8ca28 MM |
10486 | function-definition: |
10487 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
21526606 EC |
10488 | function-body |
10489 | decl-specifier-seq [opt] declarator function-try-block | |
4bb8ca28 MM |
10490 | |
10491 | GNU Extension: | |
10492 | ||
10493 | function-definition: | |
21526606 | 10494 | __extension__ function-definition |
4bb8ca28 | 10495 | |
a723baf1 | 10496 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 10497 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
10498 | then this declarator appears in a class scope. The new DECL created |
10499 | by this declarator is returned. | |
a723baf1 MM |
10500 | |
10501 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
10502 | for a function-definition here as well. If the declarator is a | |
10503 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
10504 | be TRUE upon return. By that point, the function-definition will | |
10505 | have been completely parsed. | |
10506 | ||
10507 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
10508 | is FALSE. */ | |
10509 | ||
10510 | static tree | |
21526606 | 10511 | cp_parser_init_declarator (cp_parser* parser, |
62d1db17 | 10512 | cp_decl_specifier_seq *decl_specifiers, |
94edc4ab NN |
10513 | bool function_definition_allowed_p, |
10514 | bool member_p, | |
560ad596 | 10515 | int declares_class_or_enum, |
94edc4ab | 10516 | bool* function_definition_p) |
a723baf1 MM |
10517 | { |
10518 | cp_token *token; | |
058b15c1 | 10519 | cp_declarator *declarator; |
62d1db17 | 10520 | tree prefix_attributes; |
a723baf1 MM |
10521 | tree attributes; |
10522 | tree asm_specification; | |
10523 | tree initializer; | |
10524 | tree decl = NULL_TREE; | |
10525 | tree scope; | |
a723baf1 MM |
10526 | bool is_initialized; |
10527 | bool is_parenthesized_init; | |
39703eb9 | 10528 | bool is_non_constant_init; |
7efa3e22 | 10529 | int ctor_dtor_or_conv_p; |
a723baf1 | 10530 | bool friend_p; |
4514aa8c | 10531 | tree pushed_scope = NULL; |
a723baf1 | 10532 | |
62d1db17 MM |
10533 | /* Gather the attributes that were provided with the |
10534 | decl-specifiers. */ | |
10535 | prefix_attributes = decl_specifiers->attributes; | |
62d1db17 | 10536 | |
a723baf1 MM |
10537 | /* Assume that this is not the declarator for a function |
10538 | definition. */ | |
10539 | if (function_definition_p) | |
10540 | *function_definition_p = false; | |
10541 | ||
10542 | /* Defer access checks while parsing the declarator; we cannot know | |
21526606 | 10543 | what names are accessible until we know what is being |
a723baf1 | 10544 | declared. */ |
cf22909c KL |
10545 | resume_deferring_access_checks (); |
10546 | ||
a723baf1 | 10547 | /* Parse the declarator. */ |
21526606 | 10548 | declarator |
62b8a44e | 10549 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 10550 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
10551 | /*parenthesized_p=*/NULL, |
10552 | /*member_p=*/false); | |
a723baf1 | 10553 | /* Gather up the deferred checks. */ |
cf22909c | 10554 | stop_deferring_access_checks (); |
24c0ef37 | 10555 | |
a723baf1 MM |
10556 | /* If the DECLARATOR was erroneous, there's no need to go |
10557 | further. */ | |
058b15c1 | 10558 | if (declarator == cp_error_declarator) |
cf22909c | 10559 | return error_mark_node; |
a723baf1 | 10560 | |
fc6a28d7 MM |
10561 | if (declares_class_or_enum & 2) |
10562 | cp_parser_check_for_definition_in_return_type (declarator, | |
10563 | decl_specifiers->type); | |
560ad596 | 10564 | |
a723baf1 MM |
10565 | /* Figure out what scope the entity declared by the DECLARATOR is |
10566 | located in. `grokdeclarator' sometimes changes the scope, so | |
10567 | we compute it now. */ | |
10568 | scope = get_scope_of_declarator (declarator); | |
10569 | ||
10570 | /* If we're allowing GNU extensions, look for an asm-specification | |
10571 | and attributes. */ | |
10572 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10573 | { | |
10574 | /* Look for an asm-specification. */ | |
10575 | asm_specification = cp_parser_asm_specification_opt (parser); | |
10576 | /* And attributes. */ | |
10577 | attributes = cp_parser_attributes_opt (parser); | |
10578 | } | |
10579 | else | |
10580 | { | |
10581 | asm_specification = NULL_TREE; | |
10582 | attributes = NULL_TREE; | |
10583 | } | |
10584 | ||
10585 | /* Peek at the next token. */ | |
10586 | token = cp_lexer_peek_token (parser->lexer); | |
10587 | /* Check to see if the token indicates the start of a | |
10588 | function-definition. */ | |
10589 | if (cp_parser_token_starts_function_definition_p (token)) | |
10590 | { | |
10591 | if (!function_definition_allowed_p) | |
10592 | { | |
10593 | /* If a function-definition should not appear here, issue an | |
10594 | error message. */ | |
10595 | cp_parser_error (parser, | |
10596 | "a function-definition is not allowed here"); | |
10597 | return error_mark_node; | |
10598 | } | |
10599 | else | |
10600 | { | |
a723baf1 MM |
10601 | /* Neither attributes nor an asm-specification are allowed |
10602 | on a function-definition. */ | |
10603 | if (asm_specification) | |
10604 | error ("an asm-specification is not allowed on a function-definition"); | |
10605 | if (attributes) | |
10606 | error ("attributes are not allowed on a function-definition"); | |
10607 | /* This is a function-definition. */ | |
10608 | *function_definition_p = true; | |
10609 | ||
a723baf1 | 10610 | /* Parse the function definition. */ |
4bb8ca28 MM |
10611 | if (member_p) |
10612 | decl = cp_parser_save_member_function_body (parser, | |
10613 | decl_specifiers, | |
10614 | declarator, | |
10615 | prefix_attributes); | |
10616 | else | |
21526606 | 10617 | decl |
4bb8ca28 MM |
10618 | = (cp_parser_function_definition_from_specifiers_and_declarator |
10619 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 10620 | |
a723baf1 MM |
10621 | return decl; |
10622 | } | |
10623 | } | |
10624 | ||
10625 | /* [dcl.dcl] | |
10626 | ||
10627 | Only in function declarations for constructors, destructors, and | |
21526606 | 10628 | type conversions can the decl-specifier-seq be omitted. |
a723baf1 MM |
10629 | |
10630 | We explicitly postpone this check past the point where we handle | |
10631 | function-definitions because we tolerate function-definitions | |
10632 | that are missing their return types in some modes. */ | |
62d1db17 | 10633 | if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0) |
a723baf1 | 10634 | { |
21526606 | 10635 | cp_parser_error (parser, |
a723baf1 MM |
10636 | "expected constructor, destructor, or type conversion"); |
10637 | return error_mark_node; | |
10638 | } | |
10639 | ||
10640 | /* An `=' or an `(' indicates an initializer. */ | |
21526606 | 10641 | is_initialized = (token->type == CPP_EQ |
a723baf1 MM |
10642 | || token->type == CPP_OPEN_PAREN); |
10643 | /* If the init-declarator isn't initialized and isn't followed by a | |
10644 | `,' or `;', it's not a valid init-declarator. */ | |
21526606 | 10645 | if (!is_initialized |
a723baf1 MM |
10646 | && token->type != CPP_COMMA |
10647 | && token->type != CPP_SEMICOLON) | |
10648 | { | |
996c2b52 | 10649 | cp_parser_error (parser, "expected initializer"); |
a723baf1 MM |
10650 | return error_mark_node; |
10651 | } | |
10652 | ||
10653 | /* Because start_decl has side-effects, we should only call it if we | |
10654 | know we're going ahead. By this point, we know that we cannot | |
10655 | possibly be looking at any other construct. */ | |
10656 | cp_parser_commit_to_tentative_parse (parser); | |
10657 | ||
e90c7b84 ILT |
10658 | /* If the decl specifiers were bad, issue an error now that we're |
10659 | sure this was intended to be a declarator. Then continue | |
10660 | declaring the variable(s), as int, to try to cut down on further | |
10661 | errors. */ | |
62d1db17 MM |
10662 | if (decl_specifiers->any_specifiers_p |
10663 | && decl_specifiers->type == error_mark_node) | |
e90c7b84 ILT |
10664 | { |
10665 | cp_parser_error (parser, "invalid type in declaration"); | |
62d1db17 | 10666 | decl_specifiers->type = integer_type_node; |
e90c7b84 ILT |
10667 | } |
10668 | ||
a723baf1 MM |
10669 | /* Check to see whether or not this declaration is a friend. */ |
10670 | friend_p = cp_parser_friend_p (decl_specifiers); | |
10671 | ||
10672 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 10673 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 10674 | return error_mark_node; |
a723baf1 MM |
10675 | |
10676 | /* Enter the newly declared entry in the symbol table. If we're | |
10677 | processing a declaration in a class-specifier, we wait until | |
10678 | after processing the initializer. */ | |
10679 | if (!member_p) | |
10680 | { | |
10681 | if (parser->in_unbraced_linkage_specification_p) | |
10682 | { | |
62d1db17 | 10683 | decl_specifiers->storage_class = sc_extern; |
a723baf1 MM |
10684 | have_extern_spec = false; |
10685 | } | |
ee3071ef | 10686 | decl = start_decl (declarator, decl_specifiers, |
73a8adb6 | 10687 | is_initialized, attributes, prefix_attributes, |
4514aa8c | 10688 | &pushed_scope); |
a723baf1 | 10689 | } |
73a8adb6 MM |
10690 | else if (scope) |
10691 | /* Enter the SCOPE. That way unqualified names appearing in the | |
10692 | initializer will be looked up in SCOPE. */ | |
4514aa8c | 10693 | pushed_scope = push_scope (scope); |
a723baf1 MM |
10694 | |
10695 | /* Perform deferred access control checks, now that we know in which | |
10696 | SCOPE the declared entity resides. */ | |
21526606 | 10697 | if (!member_p && decl) |
a723baf1 MM |
10698 | { |
10699 | tree saved_current_function_decl = NULL_TREE; | |
10700 | ||
10701 | /* If the entity being declared is a function, pretend that we | |
10702 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 10703 | things that would not otherwise be accessible. */ |
a723baf1 MM |
10704 | if (TREE_CODE (decl) == FUNCTION_DECL) |
10705 | { | |
10706 | saved_current_function_decl = current_function_decl; | |
10707 | current_function_decl = decl; | |
10708 | } | |
21526606 | 10709 | |
cf22909c KL |
10710 | /* Perform the access control checks for the declarator and the |
10711 | the decl-specifiers. */ | |
10712 | perform_deferred_access_checks (); | |
a723baf1 MM |
10713 | |
10714 | /* Restore the saved value. */ | |
10715 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10716 | current_function_decl = saved_current_function_decl; | |
10717 | } | |
10718 | ||
10719 | /* Parse the initializer. */ | |
10720 | if (is_initialized) | |
21526606 | 10721 | initializer = cp_parser_initializer (parser, |
39703eb9 MM |
10722 | &is_parenthesized_init, |
10723 | &is_non_constant_init); | |
a723baf1 MM |
10724 | else |
10725 | { | |
10726 | initializer = NULL_TREE; | |
10727 | is_parenthesized_init = false; | |
39703eb9 | 10728 | is_non_constant_init = true; |
a723baf1 MM |
10729 | } |
10730 | ||
10731 | /* The old parser allows attributes to appear after a parenthesized | |
10732 | initializer. Mark Mitchell proposed removing this functionality | |
10733 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
10734 | attributes -- but ignores them. */ | |
10735 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
10736 | if (cp_parser_attributes_opt (parser)) | |
10737 | warning ("attributes after parenthesized initializer ignored"); | |
10738 | ||
a723baf1 MM |
10739 | /* For an in-class declaration, use `grokfield' to create the |
10740 | declaration. */ | |
10741 | if (member_p) | |
8db1028e | 10742 | { |
4514aa8c | 10743 | if (pushed_scope) |
62a4d942 | 10744 | { |
4514aa8c NS |
10745 | pop_scope (pushed_scope); |
10746 | pushed_scope = false; | |
62a4d942 | 10747 | } |
8db1028e NS |
10748 | decl = grokfield (declarator, decl_specifiers, |
10749 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 10750 | /*attributes=*/NULL_TREE); |
8db1028e NS |
10751 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
10752 | cp_parser_save_default_args (parser, decl); | |
10753 | } | |
21526606 | 10754 | |
a723baf1 MM |
10755 | /* Finish processing the declaration. But, skip friend |
10756 | declarations. */ | |
550205c3 | 10757 | if (!friend_p && decl && decl != error_mark_node) |
73a8adb6 MM |
10758 | { |
10759 | cp_finish_decl (decl, | |
10760 | initializer, | |
10761 | asm_specification, | |
10762 | /* If the initializer is in parentheses, then this is | |
10763 | a direct-initialization, which means that an | |
10764 | `explicit' constructor is OK. Otherwise, an | |
10765 | `explicit' constructor cannot be used. */ | |
10766 | ((is_parenthesized_init || !is_initialized) | |
a723baf1 | 10767 | ? 0 : LOOKUP_ONLYCONVERTING)); |
73a8adb6 | 10768 | } |
4514aa8c NS |
10769 | if (!friend_p && pushed_scope) |
10770 | pop_scope (pushed_scope); | |
a723baf1 | 10771 | |
39703eb9 MM |
10772 | /* Remember whether or not variables were initialized by |
10773 | constant-expressions. */ | |
21526606 | 10774 | if (decl && TREE_CODE (decl) == VAR_DECL |
39703eb9 MM |
10775 | && is_initialized && !is_non_constant_init) |
10776 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
10777 | ||
a723baf1 MM |
10778 | return decl; |
10779 | } | |
10780 | ||
10781 | /* Parse a declarator. | |
21526606 | 10782 | |
a723baf1 MM |
10783 | declarator: |
10784 | direct-declarator | |
21526606 | 10785 | ptr-operator declarator |
a723baf1 MM |
10786 | |
10787 | abstract-declarator: | |
10788 | ptr-operator abstract-declarator [opt] | |
10789 | direct-abstract-declarator | |
10790 | ||
10791 | GNU Extensions: | |
10792 | ||
10793 | declarator: | |
10794 | attributes [opt] direct-declarator | |
21526606 | 10795 | attributes [opt] ptr-operator declarator |
a723baf1 MM |
10796 | |
10797 | abstract-declarator: | |
10798 | attributes [opt] ptr-operator abstract-declarator [opt] | |
10799 | attributes [opt] direct-abstract-declarator | |
21526606 | 10800 | |
7efa3e22 NS |
10801 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
10802 | detect constructor, destructor or conversion operators. It is set | |
10803 | to -1 if the declarator is a name, and +1 if it is a | |
10804 | function. Otherwise it is set to zero. Usually you just want to | |
10805 | test for >0, but internally the negative value is used. | |
21526606 | 10806 | |
a723baf1 MM |
10807 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
10808 | a decl-specifier-seq unless it declares a constructor, destructor, | |
10809 | or conversion. It might seem that we could check this condition in | |
10810 | semantic analysis, rather than parsing, but that makes it difficult | |
10811 | to handle something like `f()'. We want to notice that there are | |
10812 | no decl-specifiers, and therefore realize that this is an | |
21526606 EC |
10813 | expression, not a declaration.) |
10814 | ||
4bb8ca28 | 10815 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff |
db86dd14 MM |
10816 | the declarator is a direct-declarator of the form "(...)". |
10817 | ||
10818 | MEMBER_P is true iff this declarator is a member-declarator. */ | |
a723baf1 | 10819 | |
058b15c1 | 10820 | static cp_declarator * |
21526606 EC |
10821 | cp_parser_declarator (cp_parser* parser, |
10822 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 | 10823 | int* ctor_dtor_or_conv_p, |
db86dd14 MM |
10824 | bool* parenthesized_p, |
10825 | bool member_p) | |
a723baf1 MM |
10826 | { |
10827 | cp_token *token; | |
058b15c1 | 10828 | cp_declarator *declarator; |
a723baf1 | 10829 | enum tree_code code; |
3c01e5df | 10830 | cp_cv_quals cv_quals; |
a723baf1 MM |
10831 | tree class_type; |
10832 | tree attributes = NULL_TREE; | |
10833 | ||
10834 | /* Assume this is not a constructor, destructor, or type-conversion | |
10835 | operator. */ | |
10836 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 10837 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
10838 | |
10839 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10840 | attributes = cp_parser_attributes_opt (parser); | |
21526606 | 10841 | |
a723baf1 MM |
10842 | /* Peek at the next token. */ |
10843 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 10844 | |
a723baf1 MM |
10845 | /* Check for the ptr-operator production. */ |
10846 | cp_parser_parse_tentatively (parser); | |
10847 | /* Parse the ptr-operator. */ | |
21526606 EC |
10848 | code = cp_parser_ptr_operator (parser, |
10849 | &class_type, | |
3c01e5df | 10850 | &cv_quals); |
a723baf1 MM |
10851 | /* If that worked, then we have a ptr-operator. */ |
10852 | if (cp_parser_parse_definitely (parser)) | |
10853 | { | |
4bb8ca28 MM |
10854 | /* If a ptr-operator was found, then this declarator was not |
10855 | parenthesized. */ | |
10856 | if (parenthesized_p) | |
10857 | *parenthesized_p = true; | |
a723baf1 MM |
10858 | /* The dependent declarator is optional if we are parsing an |
10859 | abstract-declarator. */ | |
62b8a44e | 10860 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
10861 | cp_parser_parse_tentatively (parser); |
10862 | ||
10863 | /* Parse the dependent declarator. */ | |
62b8a44e | 10864 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 | 10865 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
10866 | /*parenthesized_p=*/NULL, |
10867 | /*member_p=*/false); | |
a723baf1 MM |
10868 | |
10869 | /* If we are parsing an abstract-declarator, we must handle the | |
10870 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
10871 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
10872 | && !cp_parser_parse_definitely (parser)) | |
058b15c1 | 10873 | declarator = NULL; |
21526606 | 10874 | |
a723baf1 | 10875 | /* Build the representation of the ptr-operator. */ |
058b15c1 | 10876 | if (class_type) |
3c01e5df | 10877 | declarator = make_ptrmem_declarator (cv_quals, |
058b15c1 MM |
10878 | class_type, |
10879 | declarator); | |
10880 | else if (code == INDIRECT_REF) | |
3c01e5df | 10881 | declarator = make_pointer_declarator (cv_quals, declarator); |
a723baf1 | 10882 | else |
3c01e5df | 10883 | declarator = make_reference_declarator (cv_quals, declarator); |
a723baf1 MM |
10884 | } |
10885 | /* Everything else is a direct-declarator. */ | |
10886 | else | |
4bb8ca28 MM |
10887 | { |
10888 | if (parenthesized_p) | |
10889 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
10890 | CPP_OPEN_PAREN); | |
10891 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
db86dd14 MM |
10892 | ctor_dtor_or_conv_p, |
10893 | member_p); | |
4bb8ca28 | 10894 | } |
a723baf1 | 10895 | |
058b15c1 MM |
10896 | if (attributes && declarator != cp_error_declarator) |
10897 | declarator->attributes = attributes; | |
21526606 | 10898 | |
a723baf1 MM |
10899 | return declarator; |
10900 | } | |
10901 | ||
10902 | /* Parse a direct-declarator or direct-abstract-declarator. | |
10903 | ||
10904 | direct-declarator: | |
10905 | declarator-id | |
10906 | direct-declarator ( parameter-declaration-clause ) | |
21526606 | 10907 | cv-qualifier-seq [opt] |
a723baf1 MM |
10908 | exception-specification [opt] |
10909 | direct-declarator [ constant-expression [opt] ] | |
21526606 | 10910 | ( declarator ) |
a723baf1 MM |
10911 | |
10912 | direct-abstract-declarator: | |
10913 | direct-abstract-declarator [opt] | |
21526606 | 10914 | ( parameter-declaration-clause ) |
a723baf1 MM |
10915 | cv-qualifier-seq [opt] |
10916 | exception-specification [opt] | |
10917 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
10918 | ( abstract-declarator ) | |
10919 | ||
62b8a44e NS |
10920 | Returns a representation of the declarator. DCL_KIND is |
10921 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
10922 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
10923 | we are parsing a direct-declarator. It is | |
10924 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
10925 | of ambiguity we prefer an abstract declarator, as per | |
db86dd14 | 10926 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for |
058b15c1 | 10927 | cp_parser_declarator. */ |
a723baf1 | 10928 | |
058b15c1 | 10929 | static cp_declarator * |
94edc4ab NN |
10930 | cp_parser_direct_declarator (cp_parser* parser, |
10931 | cp_parser_declarator_kind dcl_kind, | |
db86dd14 MM |
10932 | int* ctor_dtor_or_conv_p, |
10933 | bool member_p) | |
a723baf1 MM |
10934 | { |
10935 | cp_token *token; | |
058b15c1 | 10936 | cp_declarator *declarator = NULL; |
a723baf1 MM |
10937 | tree scope = NULL_TREE; |
10938 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10939 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e | 10940 | bool first = true; |
4514aa8c | 10941 | tree pushed_scope = NULL_TREE; |
21526606 | 10942 | |
62b8a44e | 10943 | while (true) |
a723baf1 | 10944 | { |
62b8a44e NS |
10945 | /* Peek at the next token. */ |
10946 | token = cp_lexer_peek_token (parser->lexer); | |
10947 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 10948 | { |
62b8a44e NS |
10949 | /* This is either a parameter-declaration-clause, or a |
10950 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 10951 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
10952 | if FIRST is true. For instance, `(int)' is a |
10953 | parameter-declaration-clause, with an omitted | |
10954 | direct-abstract-declarator. But `((*))', is a | |
10955 | parenthesized abstract declarator. Finally, when T is a | |
10956 | template parameter `(T)' is a | |
34cd5ae7 | 10957 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 10958 | named declarator. |
21526606 | 10959 | |
62b8a44e NS |
10960 | We first try and parse a parameter-declaration-clause, |
10961 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 10962 | |
62b8a44e NS |
10963 | It is not an error for it not to be a |
10964 | parameter-declaration-clause, even when FIRST is | |
10965 | false. Consider, | |
10966 | ||
10967 | int i (int); | |
10968 | int i (3); | |
10969 | ||
10970 | The first is the declaration of a function while the | |
10971 | second is a the definition of a variable, including its | |
10972 | initializer. | |
10973 | ||
10974 | Having seen only the parenthesis, we cannot know which of | |
10975 | these two alternatives should be selected. Even more | |
10976 | complex are examples like: | |
10977 | ||
10978 | int i (int (a)); | |
10979 | int i (int (3)); | |
10980 | ||
10981 | The former is a function-declaration; the latter is a | |
21526606 | 10982 | variable initialization. |
62b8a44e | 10983 | |
34cd5ae7 | 10984 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
10985 | that fails, we back out and return. */ |
10986 | ||
10987 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 10988 | { |
058b15c1 | 10989 | cp_parameter_declarator *params; |
4047b164 | 10990 | unsigned saved_num_template_parameter_lists; |
21526606 | 10991 | |
db86dd14 MM |
10992 | /* In a member-declarator, the only valid interpretation |
10993 | of a parenthesis is the start of a | |
10994 | parameter-declaration-clause. (It is invalid to | |
10995 | initialize a static data member with a parenthesized | |
10996 | initializer; only the "=" form of initialization is | |
10997 | permitted.) */ | |
10998 | if (!member_p) | |
10999 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 11000 | |
62b8a44e NS |
11001 | /* Consume the `('. */ |
11002 | cp_lexer_consume_token (parser->lexer); | |
11003 | if (first) | |
11004 | { | |
11005 | /* If this is going to be an abstract declarator, we're | |
11006 | in a declarator and we can't have default args. */ | |
11007 | parser->default_arg_ok_p = false; | |
11008 | parser->in_declarator_p = true; | |
11009 | } | |
21526606 | 11010 | |
4047b164 KL |
11011 | /* Inside the function parameter list, surrounding |
11012 | template-parameter-lists do not apply. */ | |
11013 | saved_num_template_parameter_lists | |
11014 | = parser->num_template_parameter_lists; | |
11015 | parser->num_template_parameter_lists = 0; | |
11016 | ||
62b8a44e NS |
11017 | /* Parse the parameter-declaration-clause. */ |
11018 | params = cp_parser_parameter_declaration_clause (parser); | |
11019 | ||
4047b164 KL |
11020 | parser->num_template_parameter_lists |
11021 | = saved_num_template_parameter_lists; | |
11022 | ||
62b8a44e | 11023 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 11024 | exception-specification. */ |
db86dd14 | 11025 | if (member_p || cp_parser_parse_definitely (parser)) |
62b8a44e | 11026 | { |
3c01e5df | 11027 | cp_cv_quals cv_quals; |
62b8a44e | 11028 | tree exception_specification; |
7efa3e22 NS |
11029 | |
11030 | if (ctor_dtor_or_conv_p) | |
11031 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
11032 | first = false; |
11033 | /* Consume the `)'. */ | |
11034 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
11035 | ||
11036 | /* Parse the cv-qualifier-seq. */ | |
3c01e5df | 11037 | cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
62b8a44e | 11038 | /* And the exception-specification. */ |
21526606 | 11039 | exception_specification |
62b8a44e NS |
11040 | = cp_parser_exception_specification_opt (parser); |
11041 | ||
11042 | /* Create the function-declarator. */ | |
11043 | declarator = make_call_declarator (declarator, | |
11044 | params, | |
3c01e5df | 11045 | cv_quals, |
62b8a44e NS |
11046 | exception_specification); |
11047 | /* Any subsequent parameter lists are to do with | |
11048 | return type, so are not those of the declared | |
11049 | function. */ | |
11050 | parser->default_arg_ok_p = false; | |
21526606 | 11051 | |
62b8a44e NS |
11052 | /* Repeat the main loop. */ |
11053 | continue; | |
11054 | } | |
11055 | } | |
21526606 | 11056 | |
62b8a44e NS |
11057 | /* If this is the first, we can try a parenthesized |
11058 | declarator. */ | |
11059 | if (first) | |
a723baf1 | 11060 | { |
a7324e75 MM |
11061 | bool saved_in_type_id_in_expr_p; |
11062 | ||
a723baf1 | 11063 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e | 11064 | parser->in_declarator_p = saved_in_declarator_p; |
21526606 | 11065 | |
62b8a44e NS |
11066 | /* Consume the `('. */ |
11067 | cp_lexer_consume_token (parser->lexer); | |
11068 | /* Parse the nested declarator. */ | |
a7324e75 MM |
11069 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
11070 | parser->in_type_id_in_expr_p = true; | |
21526606 | 11071 | declarator |
4bb8ca28 | 11072 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
db86dd14 MM |
11073 | /*parenthesized_p=*/NULL, |
11074 | member_p); | |
a7324e75 | 11075 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
62b8a44e NS |
11076 | first = false; |
11077 | /* Expect a `)'. */ | |
11078 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
058b15c1 MM |
11079 | declarator = cp_error_declarator; |
11080 | if (declarator == cp_error_declarator) | |
62b8a44e | 11081 | break; |
21526606 | 11082 | |
62b8a44e | 11083 | goto handle_declarator; |
a723baf1 | 11084 | } |
9bcb9aae | 11085 | /* Otherwise, we must be done. */ |
62b8a44e NS |
11086 | else |
11087 | break; | |
a723baf1 | 11088 | } |
62b8a44e NS |
11089 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
11090 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 11091 | { |
62b8a44e | 11092 | /* Parse an array-declarator. */ |
a723baf1 MM |
11093 | tree bounds; |
11094 | ||
7efa3e22 NS |
11095 | if (ctor_dtor_or_conv_p) |
11096 | *ctor_dtor_or_conv_p = 0; | |
21526606 | 11097 | |
62b8a44e NS |
11098 | first = false; |
11099 | parser->default_arg_ok_p = false; | |
11100 | parser->in_declarator_p = true; | |
a723baf1 MM |
11101 | /* Consume the `['. */ |
11102 | cp_lexer_consume_token (parser->lexer); | |
11103 | /* Peek at the next token. */ | |
11104 | token = cp_lexer_peek_token (parser->lexer); | |
11105 | /* If the next token is `]', then there is no | |
11106 | constant-expression. */ | |
11107 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
11108 | { |
11109 | bool non_constant_p; | |
11110 | ||
21526606 | 11111 | bounds |
14d22dd6 MM |
11112 | = cp_parser_constant_expression (parser, |
11113 | /*allow_non_constant=*/true, | |
11114 | &non_constant_p); | |
d17811fd | 11115 | if (!non_constant_p) |
9baa27a9 | 11116 | bounds = fold_non_dependent_expr (bounds); |
88e95ee3 MM |
11117 | /* Normally, the array bound must be an integral constant |
11118 | expression. However, as an extension, we allow VLAs | |
b671e5a4 MM |
11119 | in function scopes. */ |
11120 | else if (!at_function_scope_p ()) | |
44370687 MM |
11121 | { |
11122 | error ("array bound is not an integer constant"); | |
11123 | bounds = error_mark_node; | |
11124 | } | |
14d22dd6 | 11125 | } |
a723baf1 MM |
11126 | else |
11127 | bounds = NULL_TREE; | |
11128 | /* Look for the closing `]'. */ | |
62b8a44e NS |
11129 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
11130 | { | |
058b15c1 | 11131 | declarator = cp_error_declarator; |
62b8a44e NS |
11132 | break; |
11133 | } | |
a723baf1 | 11134 | |
058b15c1 | 11135 | declarator = make_array_declarator (declarator, bounds); |
a723baf1 | 11136 | } |
62b8a44e | 11137 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 11138 | { |
1d786913 MM |
11139 | tree qualifying_scope; |
11140 | tree unqualified_name; | |
058b15c1 | 11141 | |
a668c6ad | 11142 | /* Parse a declarator-id */ |
62b8a44e NS |
11143 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
11144 | cp_parser_parse_tentatively (parser); | |
1d786913 MM |
11145 | unqualified_name = cp_parser_declarator_id (parser); |
11146 | qualifying_scope = parser->scope; | |
712becab NS |
11147 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
11148 | { | |
11149 | if (!cp_parser_parse_definitely (parser)) | |
1d786913 MM |
11150 | unqualified_name = error_mark_node; |
11151 | else if (qualifying_scope | |
11152 | || (TREE_CODE (unqualified_name) | |
11153 | != IDENTIFIER_NODE)) | |
712becab NS |
11154 | { |
11155 | cp_parser_error (parser, "expected unqualified-id"); | |
1d786913 | 11156 | unqualified_name = error_mark_node; |
712becab NS |
11157 | } |
11158 | } | |
21526606 | 11159 | |
1d786913 | 11160 | if (unqualified_name == error_mark_node) |
058b15c1 MM |
11161 | { |
11162 | declarator = cp_error_declarator; | |
11163 | break; | |
11164 | } | |
21526606 | 11165 | |
1d786913 MM |
11166 | if (qualifying_scope && at_namespace_scope_p () |
11167 | && TREE_CODE (qualifying_scope) == TYPENAME_TYPE) | |
62b8a44e | 11168 | { |
62b8a44e NS |
11169 | /* In the declaration of a member of a template class |
11170 | outside of the class itself, the SCOPE will sometimes | |
11171 | be a TYPENAME_TYPE. For example, given: | |
21526606 | 11172 | |
62b8a44e NS |
11173 | template <typename T> |
11174 | int S<T>::R::i = 3; | |
21526606 | 11175 | |
62b8a44e NS |
11176 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In |
11177 | this context, we must resolve S<T>::R to an ordinary | |
11178 | type, rather than a typename type. | |
21526606 | 11179 | |
62b8a44e NS |
11180 | The reason we normally avoid resolving TYPENAME_TYPEs |
11181 | is that a specialization of `S' might render | |
11182 | `S<T>::R' not a type. However, if `S' is | |
11183 | specialized, then this `i' will not be used, so there | |
11184 | is no harm in resolving the types here. */ | |
1d786913 MM |
11185 | tree type; |
11186 | ||
11187 | /* Resolve the TYPENAME_TYPE. */ | |
11188 | type = resolve_typename_type (qualifying_scope, | |
11189 | /*only_current_p=*/false); | |
11190 | /* If that failed, the declarator is invalid. */ | |
11191 | if (type == error_mark_node) | |
11192 | error ("%<%T::%D%> is not a type", | |
11193 | TYPE_CONTEXT (qualifying_scope), | |
11194 | TYPE_IDENTIFIER (qualifying_scope)); | |
11195 | qualifying_scope = type; | |
62b8a44e | 11196 | } |
21526606 | 11197 | |
1d786913 MM |
11198 | declarator = make_id_declarator (qualifying_scope, |
11199 | unqualified_name); | |
b68b6828 | 11200 | declarator->id_loc = token->location; |
1d786913 | 11201 | if (unqualified_name) |
a723baf1 | 11202 | { |
62b8a44e NS |
11203 | tree class_type; |
11204 | ||
1d786913 MM |
11205 | if (qualifying_scope |
11206 | && CLASS_TYPE_P (qualifying_scope)) | |
11207 | class_type = qualifying_scope; | |
62b8a44e | 11208 | else |
1d786913 | 11209 | class_type = current_class_type; |
62b8a44e | 11210 | |
058b15c1 MM |
11211 | if (class_type) |
11212 | { | |
11213 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) | |
11214 | declarator->u.id.sfk = sfk_destructor; | |
11215 | else if (IDENTIFIER_TYPENAME_P (unqualified_name)) | |
11216 | declarator->u.id.sfk = sfk_conversion; | |
27d6592c MM |
11217 | else if (/* There's no way to declare a constructor |
11218 | for an anonymous type, even if the type | |
11219 | got a name for linkage purposes. */ | |
11220 | !TYPE_WAS_ANONYMOUS (class_type) | |
11221 | && (constructor_name_p (unqualified_name, | |
11222 | class_type) | |
11223 | || (TREE_CODE (unqualified_name) == TYPE_DECL | |
11224 | && (same_type_p | |
11225 | (TREE_TYPE (unqualified_name), | |
11226 | class_type))))) | |
058b15c1 MM |
11227 | declarator->u.id.sfk = sfk_constructor; |
11228 | ||
11229 | if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none) | |
11230 | *ctor_dtor_or_conv_p = -1; | |
1d786913 | 11231 | if (qualifying_scope |
98ca843c | 11232 | && TREE_CODE (unqualified_name) == TYPE_DECL |
058b15c1 MM |
11233 | && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name))) |
11234 | { | |
11235 | error ("invalid use of constructor as a template"); | |
2a13a625 GDR |
11236 | inform ("use %<%T::%D%> instead of %<%T::%T%> to name " |
11237 | "the constructor in a qualified name", | |
11238 | class_type, | |
058b15c1 MM |
11239 | DECL_NAME (TYPE_TI_TEMPLATE (class_type)), |
11240 | class_type, class_type); | |
11241 | } | |
11242 | } | |
a723baf1 | 11243 | } |
62b8a44e NS |
11244 | |
11245 | handle_declarator:; | |
11246 | scope = get_scope_of_declarator (declarator); | |
11247 | if (scope) | |
91b004e5 MM |
11248 | /* Any names that appear after the declarator-id for a |
11249 | member are looked up in the containing scope. */ | |
4514aa8c | 11250 | pushed_scope = push_scope (scope); |
62b8a44e NS |
11251 | parser->in_declarator_p = true; |
11252 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
058b15c1 | 11253 | || (declarator && declarator->kind == cdk_id)) |
62b8a44e NS |
11254 | /* Default args are only allowed on function |
11255 | declarations. */ | |
11256 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 11257 | else |
62b8a44e NS |
11258 | parser->default_arg_ok_p = false; |
11259 | ||
11260 | first = false; | |
a723baf1 | 11261 | } |
62b8a44e | 11262 | /* We're done. */ |
a723baf1 MM |
11263 | else |
11264 | break; | |
a723baf1 MM |
11265 | } |
11266 | ||
11267 | /* For an abstract declarator, we might wind up with nothing at this | |
11268 | point. That's an error; the declarator is not optional. */ | |
11269 | if (!declarator) | |
11270 | cp_parser_error (parser, "expected declarator"); | |
11271 | ||
11272 | /* If we entered a scope, we must exit it now. */ | |
4514aa8c NS |
11273 | if (pushed_scope) |
11274 | pop_scope (pushed_scope); | |
a723baf1 MM |
11275 | |
11276 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
11277 | parser->in_declarator_p = saved_in_declarator_p; | |
21526606 | 11278 | |
a723baf1 MM |
11279 | return declarator; |
11280 | } | |
11281 | ||
21526606 | 11282 | /* Parse a ptr-operator. |
a723baf1 MM |
11283 | |
11284 | ptr-operator: | |
11285 | * cv-qualifier-seq [opt] | |
11286 | & | |
11287 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
11288 | ||
11289 | GNU Extension: | |
11290 | ||
11291 | ptr-operator: | |
11292 | & cv-qualifier-seq [opt] | |
11293 | ||
3c01e5df MM |
11294 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was used. |
11295 | Returns ADDR_EXPR if a reference was used. In the case of a | |
11296 | pointer-to-member, *TYPE is filled in with the TYPE containing the | |
11297 | member. *CV_QUALS is filled in with the cv-qualifier-seq, or | |
11298 | TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns | |
11299 | ERROR_MARK if an error occurred. */ | |
21526606 | 11300 | |
a723baf1 | 11301 | static enum tree_code |
21526606 EC |
11302 | cp_parser_ptr_operator (cp_parser* parser, |
11303 | tree* type, | |
3c01e5df | 11304 | cp_cv_quals *cv_quals) |
a723baf1 MM |
11305 | { |
11306 | enum tree_code code = ERROR_MARK; | |
11307 | cp_token *token; | |
11308 | ||
11309 | /* Assume that it's not a pointer-to-member. */ | |
11310 | *type = NULL_TREE; | |
11311 | /* And that there are no cv-qualifiers. */ | |
3c01e5df | 11312 | *cv_quals = TYPE_UNQUALIFIED; |
a723baf1 MM |
11313 | |
11314 | /* Peek at the next token. */ | |
11315 | token = cp_lexer_peek_token (parser->lexer); | |
11316 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
11317 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
11318 | { | |
11319 | /* Remember which ptr-operator we were processing. */ | |
11320 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
11321 | ||
11322 | /* Consume the `*' or `&'. */ | |
11323 | cp_lexer_consume_token (parser->lexer); | |
11324 | ||
11325 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
11326 | `&', if we are allowing GNU extensions. (The only qualifier | |
11327 | that can legally appear after `&' is `restrict', but that is | |
11328 | enforced during semantic analysis. */ | |
21526606 | 11329 | if (code == INDIRECT_REF |
a723baf1 | 11330 | || cp_parser_allow_gnu_extensions_p (parser)) |
3c01e5df | 11331 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
a723baf1 MM |
11332 | } |
11333 | else | |
11334 | { | |
11335 | /* Try the pointer-to-member case. */ | |
11336 | cp_parser_parse_tentatively (parser); | |
11337 | /* Look for the optional `::' operator. */ | |
11338 | cp_parser_global_scope_opt (parser, | |
11339 | /*current_scope_valid_p=*/false); | |
11340 | /* Look for the nested-name specifier. */ | |
11341 | cp_parser_nested_name_specifier (parser, | |
11342 | /*typename_keyword_p=*/false, | |
11343 | /*check_dependency_p=*/true, | |
a668c6ad MM |
11344 | /*type_p=*/false, |
11345 | /*is_declaration=*/false); | |
a723baf1 MM |
11346 | /* If we found it, and the next token is a `*', then we are |
11347 | indeed looking at a pointer-to-member operator. */ | |
11348 | if (!cp_parser_error_occurred (parser) | |
11349 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
11350 | { | |
11351 | /* The type of which the member is a member is given by the | |
11352 | current SCOPE. */ | |
11353 | *type = parser->scope; | |
11354 | /* The next name will not be qualified. */ | |
11355 | parser->scope = NULL_TREE; | |
11356 | parser->qualifying_scope = NULL_TREE; | |
11357 | parser->object_scope = NULL_TREE; | |
11358 | /* Indicate that the `*' operator was used. */ | |
11359 | code = INDIRECT_REF; | |
11360 | /* Look for the optional cv-qualifier-seq. */ | |
3c01e5df | 11361 | *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); |
a723baf1 MM |
11362 | } |
11363 | /* If that didn't work we don't have a ptr-operator. */ | |
11364 | if (!cp_parser_parse_definitely (parser)) | |
11365 | cp_parser_error (parser, "expected ptr-operator"); | |
11366 | } | |
11367 | ||
11368 | return code; | |
11369 | } | |
11370 | ||
11371 | /* Parse an (optional) cv-qualifier-seq. | |
11372 | ||
11373 | cv-qualifier-seq: | |
21526606 | 11374 | cv-qualifier cv-qualifier-seq [opt] |
a723baf1 | 11375 | |
a723baf1 MM |
11376 | cv-qualifier: |
11377 | const | |
21526606 | 11378 | volatile |
a723baf1 MM |
11379 | |
11380 | GNU Extension: | |
11381 | ||
11382 | cv-qualifier: | |
98ca843c | 11383 | __restrict__ |
a723baf1 | 11384 | |
3c01e5df MM |
11385 | Returns a bitmask representing the cv-qualifiers. */ |
11386 | ||
11387 | static cp_cv_quals | |
11388 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) | |
a723baf1 | 11389 | { |
3c01e5df | 11390 | cp_cv_quals cv_quals = TYPE_UNQUALIFIED; |
a723baf1 | 11391 | |
3c01e5df | 11392 | while (true) |
a723baf1 | 11393 | { |
3c01e5df MM |
11394 | cp_token *token; |
11395 | cp_cv_quals cv_qualifier; | |
98ca843c | 11396 | |
3c01e5df MM |
11397 | /* Peek at the next token. */ |
11398 | token = cp_lexer_peek_token (parser->lexer); | |
11399 | /* See if it's a cv-qualifier. */ | |
11400 | switch (token->keyword) | |
11401 | { | |
11402 | case RID_CONST: | |
11403 | cv_qualifier = TYPE_QUAL_CONST; | |
11404 | break; | |
98ca843c | 11405 | |
3c01e5df MM |
11406 | case RID_VOLATILE: |
11407 | cv_qualifier = TYPE_QUAL_VOLATILE; | |
11408 | break; | |
98ca843c | 11409 | |
3c01e5df MM |
11410 | case RID_RESTRICT: |
11411 | cv_qualifier = TYPE_QUAL_RESTRICT; | |
11412 | break; | |
98ca843c | 11413 | |
3c01e5df MM |
11414 | default: |
11415 | cv_qualifier = TYPE_UNQUALIFIED; | |
11416 | break; | |
11417 | } | |
98ca843c | 11418 | |
3c01e5df MM |
11419 | if (!cv_qualifier) |
11420 | break; | |
a723baf1 | 11421 | |
3c01e5df MM |
11422 | if (cv_quals & cv_qualifier) |
11423 | { | |
11424 | error ("duplicate cv-qualifier"); | |
11425 | cp_lexer_purge_token (parser->lexer); | |
11426 | } | |
11427 | else | |
11428 | { | |
11429 | cp_lexer_consume_token (parser->lexer); | |
11430 | cv_quals |= cv_qualifier; | |
11431 | } | |
a723baf1 MM |
11432 | } |
11433 | ||
3c01e5df | 11434 | return cv_quals; |
a723baf1 MM |
11435 | } |
11436 | ||
11437 | /* Parse a declarator-id. | |
11438 | ||
11439 | declarator-id: | |
11440 | id-expression | |
21526606 | 11441 | :: [opt] nested-name-specifier [opt] type-name |
a723baf1 MM |
11442 | |
11443 | In the `id-expression' case, the value returned is as for | |
11444 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
11445 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
11446 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
11447 | or TREE_TYPE), but the second is still just a representation of an | |
11448 | unqualified-id. */ | |
11449 | ||
11450 | static tree | |
94edc4ab | 11451 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 | 11452 | { |
a723baf1 MM |
11453 | /* The expression must be an id-expression. Assume that qualified |
11454 | names are the names of types so that: | |
11455 | ||
11456 | template <class T> | |
11457 | int S<T>::R::i = 3; | |
11458 | ||
11459 | will work; we must treat `S<T>::R' as the name of a type. | |
11460 | Similarly, assume that qualified names are templates, where | |
11461 | required, so that: | |
11462 | ||
11463 | template <class T> | |
11464 | int S<T>::R<T>::i = 3; | |
11465 | ||
11466 | will work, too. */ | |
1d786913 MM |
11467 | return cp_parser_id_expression (parser, |
11468 | /*template_keyword_p=*/false, | |
11469 | /*check_dependency_p=*/false, | |
11470 | /*template_p=*/NULL, | |
11471 | /*declarator_p=*/true); | |
a723baf1 MM |
11472 | } |
11473 | ||
11474 | /* Parse a type-id. | |
11475 | ||
11476 | type-id: | |
11477 | type-specifier-seq abstract-declarator [opt] | |
11478 | ||
11479 | Returns the TYPE specified. */ | |
11480 | ||
11481 | static tree | |
94edc4ab | 11482 | cp_parser_type_id (cp_parser* parser) |
a723baf1 | 11483 | { |
62d1db17 | 11484 | cp_decl_specifier_seq type_specifier_seq; |
058b15c1 | 11485 | cp_declarator *abstract_declarator; |
a723baf1 MM |
11486 | |
11487 | /* Parse the type-specifier-seq. */ | |
62d1db17 MM |
11488 | cp_parser_type_specifier_seq (parser, &type_specifier_seq); |
11489 | if (type_specifier_seq.type == error_mark_node) | |
a723baf1 MM |
11490 | return error_mark_node; |
11491 | ||
11492 | /* There might or might not be an abstract declarator. */ | |
11493 | cp_parser_parse_tentatively (parser); | |
11494 | /* Look for the declarator. */ | |
21526606 | 11495 | abstract_declarator |
4bb8ca28 | 11496 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
db86dd14 MM |
11497 | /*parenthesized_p=*/NULL, |
11498 | /*member_p=*/false); | |
a723baf1 MM |
11499 | /* Check to see if there really was a declarator. */ |
11500 | if (!cp_parser_parse_definitely (parser)) | |
058b15c1 | 11501 | abstract_declarator = NULL; |
a723baf1 | 11502 | |
62d1db17 | 11503 | return groktypename (&type_specifier_seq, abstract_declarator); |
a723baf1 MM |
11504 | } |
11505 | ||
11506 | /* Parse a type-specifier-seq. | |
11507 | ||
11508 | type-specifier-seq: | |
11509 | type-specifier type-specifier-seq [opt] | |
11510 | ||
11511 | GNU extension: | |
11512 | ||
11513 | type-specifier-seq: | |
11514 | attributes type-specifier-seq [opt] | |
11515 | ||
62d1db17 | 11516 | Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */ |
a723baf1 | 11517 | |
62d1db17 MM |
11518 | static void |
11519 | cp_parser_type_specifier_seq (cp_parser* parser, | |
11520 | cp_decl_specifier_seq *type_specifier_seq) | |
a723baf1 MM |
11521 | { |
11522 | bool seen_type_specifier = false; | |
62d1db17 MM |
11523 | |
11524 | /* Clear the TYPE_SPECIFIER_SEQ. */ | |
11525 | clear_decl_specs (type_specifier_seq); | |
a723baf1 MM |
11526 | |
11527 | /* Parse the type-specifiers and attributes. */ | |
11528 | while (true) | |
11529 | { | |
11530 | tree type_specifier; | |
11531 | ||
11532 | /* Check for attributes first. */ | |
11533 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
11534 | { | |
98ca843c | 11535 | type_specifier_seq->attributes = |
62d1db17 MM |
11536 | chainon (type_specifier_seq->attributes, |
11537 | cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
11538 | continue; |
11539 | } | |
11540 | ||
a723baf1 | 11541 | /* Look for the type-specifier. */ |
21526606 | 11542 | type_specifier = cp_parser_type_specifier (parser, |
62d1db17 MM |
11543 | CP_PARSER_FLAGS_OPTIONAL, |
11544 | type_specifier_seq, | |
a723baf1 MM |
11545 | /*is_declaration=*/false, |
11546 | NULL, | |
11547 | NULL); | |
11548 | /* If the first type-specifier could not be found, this is not a | |
11549 | type-specifier-seq at all. */ | |
62d1db17 MM |
11550 | if (!seen_type_specifier && !type_specifier) |
11551 | { | |
11552 | cp_parser_error (parser, "expected type-specifier"); | |
11553 | type_specifier_seq->type = error_mark_node; | |
11554 | return; | |
11555 | } | |
a723baf1 MM |
11556 | /* If subsequent type-specifiers could not be found, the |
11557 | type-specifier-seq is complete. */ | |
62d1db17 | 11558 | else if (seen_type_specifier && !type_specifier) |
a723baf1 MM |
11559 | break; |
11560 | ||
a723baf1 MM |
11561 | seen_type_specifier = true; |
11562 | } | |
11563 | ||
62d1db17 | 11564 | return; |
a723baf1 MM |
11565 | } |
11566 | ||
11567 | /* Parse a parameter-declaration-clause. | |
11568 | ||
11569 | parameter-declaration-clause: | |
11570 | parameter-declaration-list [opt] ... [opt] | |
11571 | parameter-declaration-list , ... | |
11572 | ||
058b15c1 MM |
11573 | Returns a representation for the parameter declarations. A return |
11574 | value of NULL indicates a parameter-declaration-clause consisting | |
11575 | only of an ellipsis. */ | |
a723baf1 | 11576 | |
058b15c1 | 11577 | static cp_parameter_declarator * |
94edc4ab | 11578 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 | 11579 | { |
058b15c1 | 11580 | cp_parameter_declarator *parameters; |
a723baf1 MM |
11581 | cp_token *token; |
11582 | bool ellipsis_p; | |
058b15c1 | 11583 | bool is_error; |
a723baf1 MM |
11584 | |
11585 | /* Peek at the next token. */ | |
11586 | token = cp_lexer_peek_token (parser->lexer); | |
11587 | /* Check for trivial parameter-declaration-clauses. */ | |
11588 | if (token->type == CPP_ELLIPSIS) | |
11589 | { | |
11590 | /* Consume the `...' token. */ | |
11591 | cp_lexer_consume_token (parser->lexer); | |
058b15c1 | 11592 | return NULL; |
a723baf1 MM |
11593 | } |
11594 | else if (token->type == CPP_CLOSE_PAREN) | |
11595 | /* There are no parameters. */ | |
c73aecdf DE |
11596 | { |
11597 | #ifndef NO_IMPLICIT_EXTERN_C | |
11598 | if (in_system_header && current_class_type == NULL | |
11599 | && current_lang_name == lang_name_c) | |
058b15c1 | 11600 | return NULL; |
c73aecdf DE |
11601 | else |
11602 | #endif | |
058b15c1 | 11603 | return no_parameters; |
c73aecdf | 11604 | } |
a723baf1 MM |
11605 | /* Check for `(void)', too, which is a special case. */ |
11606 | else if (token->keyword == RID_VOID | |
21526606 | 11607 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
11608 | == CPP_CLOSE_PAREN)) |
11609 | { | |
11610 | /* Consume the `void' token. */ | |
11611 | cp_lexer_consume_token (parser->lexer); | |
11612 | /* There are no parameters. */ | |
058b15c1 | 11613 | return no_parameters; |
a723baf1 | 11614 | } |
21526606 | 11615 | |
a723baf1 | 11616 | /* Parse the parameter-declaration-list. */ |
058b15c1 | 11617 | parameters = cp_parser_parameter_declaration_list (parser, &is_error); |
a723baf1 MM |
11618 | /* If a parse error occurred while parsing the |
11619 | parameter-declaration-list, then the entire | |
11620 | parameter-declaration-clause is erroneous. */ | |
058b15c1 MM |
11621 | if (is_error) |
11622 | return NULL; | |
a723baf1 MM |
11623 | |
11624 | /* Peek at the next token. */ | |
11625 | token = cp_lexer_peek_token (parser->lexer); | |
11626 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
11627 | if (token->type == CPP_COMMA) | |
11628 | { | |
11629 | /* Consume the `,'. */ | |
11630 | cp_lexer_consume_token (parser->lexer); | |
11631 | /* Expect an ellipsis. */ | |
21526606 | 11632 | ellipsis_p |
a723baf1 MM |
11633 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); |
11634 | } | |
21526606 | 11635 | /* It might also be `...' if the optional trailing `,' was |
a723baf1 MM |
11636 | omitted. */ |
11637 | else if (token->type == CPP_ELLIPSIS) | |
11638 | { | |
11639 | /* Consume the `...' token. */ | |
11640 | cp_lexer_consume_token (parser->lexer); | |
11641 | /* And remember that we saw it. */ | |
11642 | ellipsis_p = true; | |
11643 | } | |
11644 | else | |
11645 | ellipsis_p = false; | |
11646 | ||
11647 | /* Finish the parameter list. */ | |
058b15c1 MM |
11648 | if (parameters && ellipsis_p) |
11649 | parameters->ellipsis_p = true; | |
98ca843c | 11650 | |
058b15c1 | 11651 | return parameters; |
a723baf1 MM |
11652 | } |
11653 | ||
11654 | /* Parse a parameter-declaration-list. | |
11655 | ||
11656 | parameter-declaration-list: | |
11657 | parameter-declaration | |
11658 | parameter-declaration-list , parameter-declaration | |
11659 | ||
11660 | Returns a representation of the parameter-declaration-list, as for | |
11661 | cp_parser_parameter_declaration_clause. However, the | |
058b15c1 MM |
11662 | `void_list_node' is never appended to the list. Upon return, |
11663 | *IS_ERROR will be true iff an error occurred. */ | |
a723baf1 | 11664 | |
058b15c1 MM |
11665 | static cp_parameter_declarator * |
11666 | cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error) | |
a723baf1 | 11667 | { |
058b15c1 MM |
11668 | cp_parameter_declarator *parameters = NULL; |
11669 | cp_parameter_declarator **tail = ¶meters; | |
11670 | ||
11671 | /* Assume all will go well. */ | |
11672 | *is_error = false; | |
a723baf1 MM |
11673 | |
11674 | /* Look for more parameters. */ | |
11675 | while (true) | |
11676 | { | |
058b15c1 | 11677 | cp_parameter_declarator *parameter; |
4bb8ca28 | 11678 | bool parenthesized_p; |
a723baf1 | 11679 | /* Parse the parameter. */ |
21526606 EC |
11680 | parameter |
11681 | = cp_parser_parameter_declaration (parser, | |
4bb8ca28 MM |
11682 | /*template_parm_p=*/false, |
11683 | &parenthesized_p); | |
ec194454 | 11684 | |
34cd5ae7 | 11685 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 | 11686 | then the entire parameter-declaration-list is erroneous. */ |
058b15c1 | 11687 | if (!parameter) |
a723baf1 | 11688 | { |
058b15c1 MM |
11689 | *is_error = true; |
11690 | parameters = NULL; | |
a723baf1 MM |
11691 | break; |
11692 | } | |
11693 | /* Add the new parameter to the list. */ | |
058b15c1 MM |
11694 | *tail = parameter; |
11695 | tail = ¶meter->next; | |
a723baf1 MM |
11696 | |
11697 | /* Peek at the next token. */ | |
11698 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
11699 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
11700 | /* The parameter-declaration-list is complete. */ | |
11701 | break; | |
11702 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11703 | { | |
11704 | cp_token *token; | |
11705 | ||
11706 | /* Peek at the next token. */ | |
11707 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11708 | /* If it's an ellipsis, then the list is complete. */ | |
11709 | if (token->type == CPP_ELLIPSIS) | |
11710 | break; | |
11711 | /* Otherwise, there must be more parameters. Consume the | |
11712 | `,'. */ | |
11713 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
11714 | /* When parsing something like: |
11715 | ||
11716 | int i(float f, double d) | |
21526606 | 11717 | |
4bb8ca28 MM |
11718 | we can tell after seeing the declaration for "f" that we |
11719 | are not looking at an initialization of a variable "i", | |
21526606 | 11720 | but rather at the declaration of a function "i". |
4bb8ca28 MM |
11721 | |
11722 | Due to the fact that the parsing of template arguments | |
11723 | (as specified to a template-id) requires backtracking we | |
11724 | cannot use this technique when inside a template argument | |
11725 | list. */ | |
11726 | if (!parser->in_template_argument_list_p | |
4d5fe289 | 11727 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 11728 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
4bb8ca28 MM |
11729 | /* However, a parameter-declaration of the form |
11730 | "foat(f)" (which is a valid declaration of a | |
11731 | parameter "f") can also be interpreted as an | |
11732 | expression (the conversion of "f" to "float"). */ | |
11733 | && !parenthesized_p) | |
11734 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
11735 | } |
11736 | else | |
11737 | { | |
2a13a625 | 11738 | cp_parser_error (parser, "expected %<,%> or %<...%>"); |
0b16f8f4 | 11739 | if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) |
21526606 | 11740 | cp_parser_skip_to_closing_parenthesis (parser, |
4bb8ca28 | 11741 | /*recovering=*/true, |
5c832178 | 11742 | /*or_comma=*/false, |
4bb8ca28 | 11743 | /*consume_paren=*/false); |
a723baf1 MM |
11744 | break; |
11745 | } | |
11746 | } | |
11747 | ||
058b15c1 | 11748 | return parameters; |
a723baf1 MM |
11749 | } |
11750 | ||
11751 | /* Parse a parameter declaration. | |
11752 | ||
11753 | parameter-declaration: | |
11754 | decl-specifier-seq declarator | |
11755 | decl-specifier-seq declarator = assignment-expression | |
11756 | decl-specifier-seq abstract-declarator [opt] | |
11757 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
11758 | ||
ec194454 MM |
11759 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
11760 | declares a template parameter. (In that case, a non-nested `>' | |
11761 | token encountered during the parsing of the assignment-expression | |
11762 | is not interpreted as a greater-than operator.) | |
a723baf1 | 11763 | |
058b15c1 MM |
11764 | Returns a representation of the parameter, or NULL if an error |
11765 | occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to | |
11766 | true iff the declarator is of the form "(p)". */ | |
a723baf1 | 11767 | |
058b15c1 | 11768 | static cp_parameter_declarator * |
21526606 | 11769 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
11770 | bool template_parm_p, |
11771 | bool *parenthesized_p) | |
a723baf1 | 11772 | { |
560ad596 | 11773 | int declares_class_or_enum; |
ec194454 | 11774 | bool greater_than_is_operator_p; |
62d1db17 | 11775 | cp_decl_specifier_seq decl_specifiers; |
058b15c1 | 11776 | cp_declarator *declarator; |
a723baf1 | 11777 | tree default_argument; |
a723baf1 MM |
11778 | cp_token *token; |
11779 | const char *saved_message; | |
11780 | ||
ec194454 MM |
11781 | /* In a template parameter, `>' is not an operator. |
11782 | ||
11783 | [temp.param] | |
11784 | ||
11785 | When parsing a default template-argument for a non-type | |
11786 | template-parameter, the first non-nested `>' is taken as the end | |
11787 | of the template parameter-list rather than a greater-than | |
11788 | operator. */ | |
11789 | greater_than_is_operator_p = !template_parm_p; | |
11790 | ||
a723baf1 MM |
11791 | /* Type definitions may not appear in parameter types. */ |
11792 | saved_message = parser->type_definition_forbidden_message; | |
21526606 | 11793 | parser->type_definition_forbidden_message |
a723baf1 MM |
11794 | = "types may not be defined in parameter types"; |
11795 | ||
11796 | /* Parse the declaration-specifiers. */ | |
62d1db17 MM |
11797 | cp_parser_decl_specifier_seq (parser, |
11798 | CP_PARSER_FLAGS_NONE, | |
11799 | &decl_specifiers, | |
11800 | &declares_class_or_enum); | |
a723baf1 MM |
11801 | /* If an error occurred, there's no reason to attempt to parse the |
11802 | rest of the declaration. */ | |
11803 | if (cp_parser_error_occurred (parser)) | |
11804 | { | |
11805 | parser->type_definition_forbidden_message = saved_message; | |
058b15c1 | 11806 | return NULL; |
a723baf1 MM |
11807 | } |
11808 | ||
11809 | /* Peek at the next token. */ | |
11810 | token = cp_lexer_peek_token (parser->lexer); | |
11811 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
11812 | is no declarator. */ | |
21526606 | 11813 | if (token->type == CPP_CLOSE_PAREN |
a723baf1 MM |
11814 | || token->type == CPP_COMMA |
11815 | || token->type == CPP_EQ | |
11816 | || token->type == CPP_ELLIPSIS | |
11817 | || token->type == CPP_GREATER) | |
4bb8ca28 | 11818 | { |
058b15c1 | 11819 | declarator = NULL; |
4bb8ca28 MM |
11820 | if (parenthesized_p) |
11821 | *parenthesized_p = false; | |
11822 | } | |
a723baf1 MM |
11823 | /* Otherwise, there should be a declarator. */ |
11824 | else | |
11825 | { | |
11826 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
11827 | parser->default_arg_ok_p = false; | |
21526606 | 11828 | |
5c832178 MM |
11829 | /* After seeing a decl-specifier-seq, if the next token is not a |
11830 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
11831 | expression. Therefore, if parsing tentatively, we commit at |
11832 | this point. */ | |
5c832178 | 11833 | if (!parser->in_template_argument_list_p |
643aee72 | 11834 | /* In an expression context, having seen: |
4f8163b1 | 11835 | |
a7324e75 | 11836 | (int((char ... |
4f8163b1 MM |
11837 | |
11838 | we cannot be sure whether we are looking at a | |
a7324e75 MM |
11839 | function-type (taking a "char" as a parameter) or a cast |
11840 | of some object of type "char" to "int". */ | |
4f8163b1 | 11841 | && !parser->in_type_id_in_expr_p |
0b16f8f4 | 11842 | && cp_parser_uncommitted_to_tentative_parse_p (parser) |
5c832178 MM |
11843 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
11844 | cp_parser_commit_to_tentative_parse (parser); | |
11845 | /* Parse the declarator. */ | |
a723baf1 | 11846 | declarator = cp_parser_declarator (parser, |
62b8a44e | 11847 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 11848 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
11849 | parenthesized_p, |
11850 | /*member_p=*/false); | |
a723baf1 | 11851 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d | 11852 | /* After the declarator, allow more attributes. */ |
62d1db17 | 11853 | decl_specifiers.attributes |
98ca843c | 11854 | = chainon (decl_specifiers.attributes, |
62d1db17 | 11855 | cp_parser_attributes_opt (parser)); |
a723baf1 MM |
11856 | } |
11857 | ||
62b8a44e | 11858 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
11859 | of the parameter, not to the default argument. */ |
11860 | parser->type_definition_forbidden_message = saved_message; | |
11861 | ||
11862 | /* If the next token is `=', then process a default argument. */ | |
11863 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
11864 | { | |
11865 | bool saved_greater_than_is_operator_p; | |
11866 | /* Consume the `='. */ | |
11867 | cp_lexer_consume_token (parser->lexer); | |
11868 | ||
11869 | /* If we are defining a class, then the tokens that make up the | |
11870 | default argument must be saved and processed later. */ | |
21526606 | 11871 | if (!template_parm_p && at_class_scope_p () |
ec194454 | 11872 | && TYPE_BEING_DEFINED (current_class_type)) |
a723baf1 MM |
11873 | { |
11874 | unsigned depth = 0; | |
c162c75e MA |
11875 | cp_token *first_token; |
11876 | cp_token *token; | |
a723baf1 MM |
11877 | |
11878 | /* Add tokens until we have processed the entire default | |
03fd3f84 | 11879 | argument. We add the range [first_token, token). */ |
c162c75e | 11880 | first_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
11881 | while (true) |
11882 | { | |
11883 | bool done = false; | |
a723baf1 MM |
11884 | |
11885 | /* Peek at the next token. */ | |
11886 | token = cp_lexer_peek_token (parser->lexer); | |
11887 | /* What we do depends on what token we have. */ | |
11888 | switch (token->type) | |
11889 | { | |
11890 | /* In valid code, a default argument must be | |
11891 | immediately followed by a `,' `)', or `...'. */ | |
11892 | case CPP_COMMA: | |
11893 | case CPP_CLOSE_PAREN: | |
11894 | case CPP_ELLIPSIS: | |
11895 | /* If we run into a non-nested `;', `}', or `]', | |
11896 | then the code is invalid -- but the default | |
11897 | argument is certainly over. */ | |
11898 | case CPP_SEMICOLON: | |
11899 | case CPP_CLOSE_BRACE: | |
11900 | case CPP_CLOSE_SQUARE: | |
11901 | if (depth == 0) | |
11902 | done = true; | |
11903 | /* Update DEPTH, if necessary. */ | |
11904 | else if (token->type == CPP_CLOSE_PAREN | |
11905 | || token->type == CPP_CLOSE_BRACE | |
11906 | || token->type == CPP_CLOSE_SQUARE) | |
11907 | --depth; | |
11908 | break; | |
11909 | ||
11910 | case CPP_OPEN_PAREN: | |
11911 | case CPP_OPEN_SQUARE: | |
11912 | case CPP_OPEN_BRACE: | |
11913 | ++depth; | |
11914 | break; | |
11915 | ||
11916 | case CPP_GREATER: | |
11917 | /* If we see a non-nested `>', and `>' is not an | |
11918 | operator, then it marks the end of the default | |
11919 | argument. */ | |
11920 | if (!depth && !greater_than_is_operator_p) | |
11921 | done = true; | |
11922 | break; | |
11923 | ||
11924 | /* If we run out of tokens, issue an error message. */ | |
11925 | case CPP_EOF: | |
11926 | error ("file ends in default argument"); | |
11927 | done = true; | |
11928 | break; | |
11929 | ||
11930 | case CPP_NAME: | |
11931 | case CPP_SCOPE: | |
11932 | /* In these cases, we should look for template-ids. | |
21526606 | 11933 | For example, if the default argument is |
a723baf1 MM |
11934 | `X<int, double>()', we need to do name lookup to |
11935 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 11936 | so, the `,' does not end the default argument. |
a723baf1 MM |
11937 | |
11938 | That is not yet done. */ | |
11939 | break; | |
11940 | ||
11941 | default: | |
11942 | break; | |
11943 | } | |
11944 | ||
11945 | /* If we've reached the end, stop. */ | |
11946 | if (done) | |
11947 | break; | |
21526606 | 11948 | |
a723baf1 MM |
11949 | /* Add the token to the token block. */ |
11950 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 11951 | } |
c162c75e MA |
11952 | |
11953 | /* Create a DEFAULT_ARG to represented the unparsed default | |
11954 | argument. */ | |
11955 | default_argument = make_node (DEFAULT_ARG); | |
11956 | DEFARG_TOKENS (default_argument) | |
11957 | = cp_token_cache_new (first_token, token); | |
a723baf1 MM |
11958 | } |
11959 | /* Outside of a class definition, we can just parse the | |
11960 | assignment-expression. */ | |
11961 | else | |
11962 | { | |
11963 | bool saved_local_variables_forbidden_p; | |
11964 | ||
11965 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
11966 | set correctly. */ | |
21526606 | 11967 | saved_greater_than_is_operator_p |
a723baf1 MM |
11968 | = parser->greater_than_is_operator_p; |
11969 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
11970 | /* Local variable names (and the `this' keyword) may not | |
11971 | appear in a default argument. */ | |
21526606 | 11972 | saved_local_variables_forbidden_p |
a723baf1 MM |
11973 | = parser->local_variables_forbidden_p; |
11974 | parser->local_variables_forbidden_p = true; | |
11975 | /* Parse the assignment-expression. */ | |
93678513 MM |
11976 | default_argument |
11977 | = cp_parser_assignment_expression (parser, /*cast_p=*/false); | |
a723baf1 | 11978 | /* Restore saved state. */ |
21526606 | 11979 | parser->greater_than_is_operator_p |
a723baf1 | 11980 | = saved_greater_than_is_operator_p; |
21526606 EC |
11981 | parser->local_variables_forbidden_p |
11982 | = saved_local_variables_forbidden_p; | |
a723baf1 MM |
11983 | } |
11984 | if (!parser->default_arg_ok_p) | |
11985 | { | |
c67d36d0 NS |
11986 | if (!flag_pedantic_errors) |
11987 | warning ("deprecated use of default argument for parameter of non-function"); | |
11988 | else | |
11989 | { | |
11990 | error ("default arguments are only permitted for function parameters"); | |
11991 | default_argument = NULL_TREE; | |
11992 | } | |
a723baf1 MM |
11993 | } |
11994 | } | |
11995 | else | |
11996 | default_argument = NULL_TREE; | |
21526606 | 11997 | |
62d1db17 | 11998 | return make_parameter_declarator (&decl_specifiers, |
058b15c1 MM |
11999 | declarator, |
12000 | default_argument); | |
a723baf1 MM |
12001 | } |
12002 | ||
a723baf1 MM |
12003 | /* Parse a function-body. |
12004 | ||
12005 | function-body: | |
12006 | compound_statement */ | |
12007 | ||
12008 | static void | |
12009 | cp_parser_function_body (cp_parser *parser) | |
12010 | { | |
325c3691 | 12011 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
12012 | } |
12013 | ||
12014 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
12015 | true if a ctor-initializer was present. */ | |
12016 | ||
12017 | static bool | |
12018 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
12019 | { | |
12020 | tree body; | |
12021 | bool ctor_initializer_p; | |
12022 | ||
12023 | /* Begin the function body. */ | |
12024 | body = begin_function_body (); | |
12025 | /* Parse the optional ctor-initializer. */ | |
12026 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
12027 | /* Parse the function-body. */ | |
12028 | cp_parser_function_body (parser); | |
12029 | /* Finish the function body. */ | |
12030 | finish_function_body (body); | |
12031 | ||
12032 | return ctor_initializer_p; | |
12033 | } | |
12034 | ||
12035 | /* Parse an initializer. | |
12036 | ||
12037 | initializer: | |
12038 | = initializer-clause | |
21526606 | 12039 | ( expression-list ) |
a723baf1 MM |
12040 | |
12041 | Returns a expression representing the initializer. If no | |
21526606 | 12042 | initializer is present, NULL_TREE is returned. |
a723baf1 MM |
12043 | |
12044 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
12045 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
12046 | set to FALSE if there is no initializer present. If there is an |
12047 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
12048 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
12049 | |
12050 | static tree | |
39703eb9 MM |
12051 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
12052 | bool* non_constant_p) | |
a723baf1 MM |
12053 | { |
12054 | cp_token *token; | |
12055 | tree init; | |
12056 | ||
12057 | /* Peek at the next token. */ | |
12058 | token = cp_lexer_peek_token (parser->lexer); | |
12059 | ||
12060 | /* Let our caller know whether or not this initializer was | |
12061 | parenthesized. */ | |
12062 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
12063 | /* Assume that the initializer is constant. */ |
12064 | *non_constant_p = false; | |
a723baf1 MM |
12065 | |
12066 | if (token->type == CPP_EQ) | |
12067 | { | |
12068 | /* Consume the `='. */ | |
12069 | cp_lexer_consume_token (parser->lexer); | |
12070 | /* Parse the initializer-clause. */ | |
39703eb9 | 12071 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
12072 | } |
12073 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 | 12074 | init = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 12075 | /*cast_p=*/false, |
39703eb9 | 12076 | non_constant_p); |
a723baf1 MM |
12077 | else |
12078 | { | |
12079 | /* Anything else is an error. */ | |
12080 | cp_parser_error (parser, "expected initializer"); | |
12081 | init = error_mark_node; | |
12082 | } | |
12083 | ||
12084 | return init; | |
12085 | } | |
12086 | ||
21526606 | 12087 | /* Parse an initializer-clause. |
a723baf1 MM |
12088 | |
12089 | initializer-clause: | |
12090 | assignment-expression | |
12091 | { initializer-list , [opt] } | |
12092 | { } | |
12093 | ||
21526606 | 12094 | Returns an expression representing the initializer. |
a723baf1 MM |
12095 | |
12096 | If the `assignment-expression' production is used the value | |
21526606 | 12097 | returned is simply a representation for the expression. |
a723baf1 MM |
12098 | |
12099 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
12100 | the elements of the initializer-list (or NULL_TREE, if the last | |
12101 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
12102 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
12103 | trailing `,' was provided. NON_CONSTANT_P is as for |
12104 | cp_parser_initializer. */ | |
a723baf1 MM |
12105 | |
12106 | static tree | |
39703eb9 | 12107 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
12108 | { |
12109 | tree initializer; | |
12110 | ||
b2802a4b R |
12111 | /* Assume the expression is constant. */ |
12112 | *non_constant_p = false; | |
12113 | ||
a723baf1 MM |
12114 | /* If it is not a `{', then we are looking at an |
12115 | assignment-expression. */ | |
12116 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
0da99d4e | 12117 | { |
98ca843c | 12118 | initializer |
0da99d4e GB |
12119 | = cp_parser_constant_expression (parser, |
12120 | /*allow_non_constant_p=*/true, | |
12121 | non_constant_p); | |
12122 | if (!*non_constant_p) | |
12123 | initializer = fold_non_dependent_expr (initializer); | |
12124 | } | |
a723baf1 MM |
12125 | else |
12126 | { | |
12127 | /* Consume the `{' token. */ | |
12128 | cp_lexer_consume_token (parser->lexer); | |
12129 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
12130 | initializer = make_node (CONSTRUCTOR); | |
a723baf1 MM |
12131 | /* If it's not a `}', then there is a non-trivial initializer. */ |
12132 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
12133 | { | |
12134 | /* Parse the initializer list. */ | |
12135 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 12136 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
12137 | /* A trailing `,' token is allowed. */ |
12138 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12139 | cp_lexer_consume_token (parser->lexer); | |
12140 | } | |
a723baf1 MM |
12141 | /* Now, there should be a trailing `}'. */ |
12142 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12143 | } | |
12144 | ||
12145 | return initializer; | |
12146 | } | |
12147 | ||
12148 | /* Parse an initializer-list. | |
12149 | ||
12150 | initializer-list: | |
12151 | initializer-clause | |
12152 | initializer-list , initializer-clause | |
12153 | ||
12154 | GNU Extension: | |
21526606 | 12155 | |
a723baf1 MM |
12156 | initializer-list: |
12157 | identifier : initializer-clause | |
12158 | initializer-list, identifier : initializer-clause | |
12159 | ||
12160 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
12161 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
12162 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
12163 | as for cp_parser_initializer. */ | |
a723baf1 MM |
12164 | |
12165 | static tree | |
39703eb9 | 12166 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
12167 | { |
12168 | tree initializers = NULL_TREE; | |
12169 | ||
39703eb9 MM |
12170 | /* Assume all of the expressions are constant. */ |
12171 | *non_constant_p = false; | |
12172 | ||
a723baf1 MM |
12173 | /* Parse the rest of the list. */ |
12174 | while (true) | |
12175 | { | |
12176 | cp_token *token; | |
12177 | tree identifier; | |
12178 | tree initializer; | |
39703eb9 MM |
12179 | bool clause_non_constant_p; |
12180 | ||
a723baf1 MM |
12181 | /* If the next token is an identifier and the following one is a |
12182 | colon, we are looking at the GNU designated-initializer | |
12183 | syntax. */ | |
12184 | if (cp_parser_allow_gnu_extensions_p (parser) | |
12185 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
12186 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
12187 | { | |
12188 | /* Consume the identifier. */ | |
12189 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
12190 | /* Consume the `:'. */ | |
12191 | cp_lexer_consume_token (parser->lexer); | |
12192 | } | |
12193 | else | |
12194 | identifier = NULL_TREE; | |
12195 | ||
12196 | /* Parse the initializer. */ | |
21526606 | 12197 | initializer = cp_parser_initializer_clause (parser, |
39703eb9 MM |
12198 | &clause_non_constant_p); |
12199 | /* If any clause is non-constant, so is the entire initializer. */ | |
12200 | if (clause_non_constant_p) | |
12201 | *non_constant_p = true; | |
a723baf1 MM |
12202 | /* Add it to the list. */ |
12203 | initializers = tree_cons (identifier, initializer, initializers); | |
12204 | ||
12205 | /* If the next token is not a comma, we have reached the end of | |
12206 | the list. */ | |
12207 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
12208 | break; | |
12209 | ||
12210 | /* Peek at the next token. */ | |
12211 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
12212 | /* If the next token is a `}', then we're still done. An | |
12213 | initializer-clause can have a trailing `,' after the | |
12214 | initializer-list and before the closing `}'. */ | |
12215 | if (token->type == CPP_CLOSE_BRACE) | |
12216 | break; | |
12217 | ||
12218 | /* Consume the `,' token. */ | |
12219 | cp_lexer_consume_token (parser->lexer); | |
12220 | } | |
12221 | ||
12222 | /* The initializers were built up in reverse order, so we need to | |
12223 | reverse them now. */ | |
12224 | return nreverse (initializers); | |
12225 | } | |
12226 | ||
12227 | /* Classes [gram.class] */ | |
12228 | ||
12229 | /* Parse a class-name. | |
12230 | ||
12231 | class-name: | |
12232 | identifier | |
12233 | template-id | |
12234 | ||
12235 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
12236 | to indicate that names looked up in dependent types should be | |
12237 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
12238 | keyword has been used to indicate that the name that appears next | |
fc6a28d7 MM |
12239 | is a template. TAG_TYPE indicates the explicit tag given before |
12240 | the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are | |
12241 | looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class | |
12242 | is the class being defined in a class-head. | |
a723baf1 MM |
12243 | |
12244 | Returns the TYPE_DECL representing the class. */ | |
12245 | ||
12246 | static tree | |
21526606 EC |
12247 | cp_parser_class_name (cp_parser *parser, |
12248 | bool typename_keyword_p, | |
12249 | bool template_keyword_p, | |
fc6a28d7 | 12250 | enum tag_types tag_type, |
a723baf1 | 12251 | bool check_dependency_p, |
a668c6ad MM |
12252 | bool class_head_p, |
12253 | bool is_declaration) | |
a723baf1 MM |
12254 | { |
12255 | tree decl; | |
12256 | tree scope; | |
12257 | bool typename_p; | |
e5976695 MM |
12258 | cp_token *token; |
12259 | ||
12260 | /* All class-names start with an identifier. */ | |
12261 | token = cp_lexer_peek_token (parser->lexer); | |
12262 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
12263 | { | |
12264 | cp_parser_error (parser, "expected class-name"); | |
12265 | return error_mark_node; | |
12266 | } | |
21526606 | 12267 | |
a723baf1 MM |
12268 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
12269 | to a template-id, so we save it here. */ | |
12270 | scope = parser->scope; | |
3adee96c KL |
12271 | if (scope == error_mark_node) |
12272 | return error_mark_node; | |
21526606 | 12273 | |
a723baf1 MM |
12274 | /* Any name names a type if we're following the `typename' keyword |
12275 | in a qualified name where the enclosing scope is type-dependent. */ | |
12276 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 12277 | && dependent_type_p (scope)); |
e5976695 MM |
12278 | /* Handle the common case (an identifier, but not a template-id) |
12279 | efficiently. */ | |
21526606 | 12280 | if (token->type == CPP_NAME |
f4abade9 | 12281 | && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) |
a723baf1 | 12282 | { |
a723baf1 MM |
12283 | tree identifier; |
12284 | ||
12285 | /* Look for the identifier. */ | |
12286 | identifier = cp_parser_identifier (parser); | |
12287 | /* If the next token isn't an identifier, we are certainly not | |
12288 | looking at a class-name. */ | |
12289 | if (identifier == error_mark_node) | |
12290 | decl = error_mark_node; | |
12291 | /* If we know this is a type-name, there's no need to look it | |
12292 | up. */ | |
12293 | else if (typename_p) | |
12294 | decl = identifier; | |
12295 | else | |
12296 | { | |
12297 | /* If the next token is a `::', then the name must be a type | |
12298 | name. | |
12299 | ||
12300 | [basic.lookup.qual] | |
12301 | ||
12302 | During the lookup for a name preceding the :: scope | |
12303 | resolution operator, object, function, and enumerator | |
12304 | names are ignored. */ | |
12305 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
fc6a28d7 | 12306 | tag_type = typename_type; |
a723baf1 | 12307 | /* Look up the name. */ |
21526606 | 12308 | decl = cp_parser_lookup_name (parser, identifier, |
fc6a28d7 | 12309 | tag_type, |
b0bc6e8e | 12310 | /*is_template=*/false, |
eea9800f | 12311 | /*is_namespace=*/false, |
8f78f01f MM |
12312 | check_dependency_p, |
12313 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
12314 | } |
12315 | } | |
e5976695 MM |
12316 | else |
12317 | { | |
12318 | /* Try a template-id. */ | |
12319 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
12320 | check_dependency_p, |
12321 | is_declaration); | |
e5976695 MM |
12322 | if (decl == error_mark_node) |
12323 | return error_mark_node; | |
12324 | } | |
a723baf1 MM |
12325 | |
12326 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
12327 | ||
12328 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
12329 | if (typename_p && decl != error_mark_node) | |
4bfb8bba | 12330 | { |
fc6a28d7 | 12331 | decl = make_typename_type (scope, decl, typename_type, /*complain=*/1); |
4bfb8bba MM |
12332 | if (decl != error_mark_node) |
12333 | decl = TYPE_NAME (decl); | |
12334 | } | |
a723baf1 MM |
12335 | |
12336 | /* Check to see that it is really the name of a class. */ | |
21526606 | 12337 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
a723baf1 MM |
12338 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE |
12339 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
12340 | /* Situations like this: | |
12341 | ||
12342 | template <typename T> struct A { | |
21526606 | 12343 | typename T::template X<int>::I i; |
a723baf1 MM |
12344 | }; |
12345 | ||
12346 | are problematic. Is `T::template X<int>' a class-name? The | |
12347 | standard does not seem to be definitive, but there is no other | |
12348 | valid interpretation of the following `::'. Therefore, those | |
12349 | names are considered class-names. */ | |
fc6a28d7 | 12350 | decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error)); |
a723baf1 MM |
12351 | else if (decl == error_mark_node |
12352 | || TREE_CODE (decl) != TYPE_DECL | |
07c65e00 | 12353 | || TREE_TYPE (decl) == error_mark_node |
a723baf1 MM |
12354 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) |
12355 | { | |
12356 | cp_parser_error (parser, "expected class-name"); | |
12357 | return error_mark_node; | |
12358 | } | |
12359 | ||
12360 | return decl; | |
12361 | } | |
12362 | ||
12363 | /* Parse a class-specifier. | |
12364 | ||
12365 | class-specifier: | |
12366 | class-head { member-specification [opt] } | |
12367 | ||
12368 | Returns the TREE_TYPE representing the class. */ | |
12369 | ||
12370 | static tree | |
94edc4ab | 12371 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
12372 | { |
12373 | cp_token *token; | |
12374 | tree type; | |
6de9cd9a | 12375 | tree attributes = NULL_TREE; |
a723baf1 MM |
12376 | int has_trailing_semicolon; |
12377 | bool nested_name_specifier_p; | |
a723baf1 | 12378 | unsigned saved_num_template_parameter_lists; |
87c465f5 | 12379 | tree old_scope = NULL_TREE; |
2436b51f | 12380 | tree scope = NULL_TREE; |
a723baf1 | 12381 | |
8d241e0b | 12382 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 12383 | |
a723baf1 MM |
12384 | /* Parse the class-head. */ |
12385 | type = cp_parser_class_head (parser, | |
38b305d0 JM |
12386 | &nested_name_specifier_p, |
12387 | &attributes); | |
a723baf1 MM |
12388 | /* If the class-head was a semantic disaster, skip the entire body |
12389 | of the class. */ | |
12390 | if (!type) | |
12391 | { | |
12392 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 12393 | pop_deferring_access_checks (); |
a723baf1 MM |
12394 | return error_mark_node; |
12395 | } | |
cf22909c | 12396 | |
a723baf1 MM |
12397 | /* Look for the `{'. */ |
12398 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
12399 | { |
12400 | pop_deferring_access_checks (); | |
12401 | return error_mark_node; | |
12402 | } | |
12403 | ||
a723baf1 MM |
12404 | /* Issue an error message if type-definitions are forbidden here. */ |
12405 | cp_parser_check_type_definition (parser); | |
12406 | /* Remember that we are defining one more class. */ | |
12407 | ++parser->num_classes_being_defined; | |
12408 | /* Inside the class, surrounding template-parameter-lists do not | |
12409 | apply. */ | |
21526606 EC |
12410 | saved_num_template_parameter_lists |
12411 | = parser->num_template_parameter_lists; | |
a723baf1 | 12412 | parser->num_template_parameter_lists = 0; |
78757caa | 12413 | |
a723baf1 | 12414 | /* Start the class. */ |
eeb23c11 | 12415 | if (nested_name_specifier_p) |
2436b51f MM |
12416 | { |
12417 | scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); | |
87c465f5 | 12418 | old_scope = push_inner_scope (scope); |
2436b51f | 12419 | } |
a723baf1 | 12420 | type = begin_class_definition (type); |
98ca843c | 12421 | |
a723baf1 | 12422 | if (type == error_mark_node) |
9bcb9aae | 12423 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
12424 | cp_parser_skip_to_closing_brace (parser); |
12425 | else | |
12426 | /* Parse the member-specification. */ | |
12427 | cp_parser_member_specification_opt (parser); | |
98ca843c | 12428 | |
a723baf1 MM |
12429 | /* Look for the trailing `}'. */ |
12430 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12431 | /* We get better error messages by noticing a common problem: a | |
12432 | missing trailing `;'. */ | |
12433 | token = cp_lexer_peek_token (parser->lexer); | |
12434 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
38b305d0 | 12435 | /* Look for trailing attributes to apply to this class. */ |
a723baf1 | 12436 | if (cp_parser_allow_gnu_extensions_p (parser)) |
560ad596 | 12437 | { |
38b305d0 JM |
12438 | tree sub_attr = cp_parser_attributes_opt (parser); |
12439 | attributes = chainon (attributes, sub_attr); | |
560ad596 | 12440 | } |
38b305d0 JM |
12441 | if (type != error_mark_node) |
12442 | type = finish_struct (type, attributes); | |
87c465f5 KL |
12443 | if (nested_name_specifier_p) |
12444 | pop_inner_scope (old_scope, scope); | |
a723baf1 MM |
12445 | /* If this class is not itself within the scope of another class, |
12446 | then we need to parse the bodies of all of the queued function | |
12447 | definitions. Note that the queued functions defined in a class | |
12448 | are not always processed immediately following the | |
12449 | class-specifier for that class. Consider: | |
12450 | ||
12451 | struct A { | |
12452 | struct B { void f() { sizeof (A); } }; | |
12453 | }; | |
12454 | ||
12455 | If `f' were processed before the processing of `A' were | |
12456 | completed, there would be no way to compute the size of `A'. | |
12457 | Note that the nesting we are interested in here is lexical -- | |
12458 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
12459 | for: | |
12460 | ||
12461 | struct A { struct B; }; | |
12462 | struct A::B { void f() { } }; | |
12463 | ||
12464 | there is no need to delay the parsing of `A::B::f'. */ | |
21526606 | 12465 | if (--parser->num_classes_being_defined == 0) |
a723baf1 | 12466 | { |
8218bd34 MM |
12467 | tree queue_entry; |
12468 | tree fn; | |
4514aa8c NS |
12469 | tree class_type = NULL_TREE; |
12470 | tree pushed_scope = NULL_TREE; | |
a723baf1 | 12471 | |
8218bd34 MM |
12472 | /* In a first pass, parse default arguments to the functions. |
12473 | Then, in a second pass, parse the bodies of the functions. | |
12474 | This two-phased approach handles cases like: | |
21526606 EC |
12475 | |
12476 | struct S { | |
12477 | void f() { g(); } | |
8218bd34 MM |
12478 | void g(int i = 3); |
12479 | }; | |
12480 | ||
12481 | */ | |
8db1028e NS |
12482 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
12483 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12484 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12485 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
12486 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
12487 | { |
12488 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
12489 | /* If there are default arguments that have not yet been processed, |
12490 | take care of them now. */ | |
f44b0c8e MM |
12491 | if (class_type != TREE_PURPOSE (queue_entry)) |
12492 | { | |
4514aa8c NS |
12493 | if (pushed_scope) |
12494 | pop_scope (pushed_scope); | |
f44b0c8e | 12495 | class_type = TREE_PURPOSE (queue_entry); |
4514aa8c | 12496 | pushed_scope = push_scope (class_type); |
f44b0c8e MM |
12497 | } |
12498 | /* Make sure that any template parameters are in scope. */ | |
12499 | maybe_begin_member_template_processing (fn); | |
12500 | /* Parse the default argument expressions. */ | |
8218bd34 MM |
12501 | cp_parser_late_parsing_default_args (parser, fn); |
12502 | /* Remove any template parameters from the symbol table. */ | |
12503 | maybe_end_member_template_processing (); | |
12504 | } | |
4514aa8c NS |
12505 | if (pushed_scope) |
12506 | pop_scope (pushed_scope); | |
8218bd34 | 12507 | /* Now parse the body of the functions. */ |
8db1028e NS |
12508 | for (TREE_VALUE (parser->unparsed_functions_queues) |
12509 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
12510 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
12511 | TREE_VALUE (parser->unparsed_functions_queues) | |
12512 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 12513 | { |
a723baf1 | 12514 | /* Figure out which function we need to process. */ |
a723baf1 MM |
12515 | fn = TREE_VALUE (queue_entry); |
12516 | ||
4543ee47 ZD |
12517 | /* A hack to prevent garbage collection. */ |
12518 | function_depth++; | |
12519 | ||
a723baf1 MM |
12520 | /* Parse the function. */ |
12521 | cp_parser_late_parsing_for_member (parser, fn); | |
4543ee47 | 12522 | function_depth--; |
a723baf1 | 12523 | } |
a723baf1 MM |
12524 | } |
12525 | ||
12526 | /* Put back any saved access checks. */ | |
cf22909c | 12527 | pop_deferring_access_checks (); |
a723baf1 MM |
12528 | |
12529 | /* Restore the count of active template-parameter-lists. */ | |
12530 | parser->num_template_parameter_lists | |
12531 | = saved_num_template_parameter_lists; | |
12532 | ||
12533 | return type; | |
12534 | } | |
12535 | ||
12536 | /* Parse a class-head. | |
12537 | ||
12538 | class-head: | |
12539 | class-key identifier [opt] base-clause [opt] | |
12540 | class-key nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12541 | class-key nested-name-specifier [opt] template-id |
12542 | base-clause [opt] | |
a723baf1 MM |
12543 | |
12544 | GNU Extensions: | |
12545 | class-key attributes identifier [opt] base-clause [opt] | |
12546 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12547 | class-key attributes nested-name-specifier [opt] template-id |
12548 | base-clause [opt] | |
a723baf1 MM |
12549 | |
12550 | Returns the TYPE of the indicated class. Sets | |
12551 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
12552 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 | 12553 | |
55dcbc12 NS |
12554 | Returns error_mark_node if this is not a class-head. |
12555 | ||
a723baf1 MM |
12556 | Returns NULL_TREE if the class-head is syntactically valid, but |
12557 | semantically invalid in a way that means we should skip the entire | |
12558 | body of the class. */ | |
12559 | ||
12560 | static tree | |
21526606 | 12561 | cp_parser_class_head (cp_parser* parser, |
38b305d0 JM |
12562 | bool* nested_name_specifier_p, |
12563 | tree *attributes_p) | |
a723baf1 | 12564 | { |
a723baf1 MM |
12565 | tree nested_name_specifier; |
12566 | enum tag_types class_key; | |
12567 | tree id = NULL_TREE; | |
12568 | tree type = NULL_TREE; | |
12569 | tree attributes; | |
12570 | bool template_id_p = false; | |
12571 | bool qualified_p = false; | |
12572 | bool invalid_nested_name_p = false; | |
afb0918a | 12573 | bool invalid_explicit_specialization_p = false; |
4514aa8c | 12574 | tree pushed_scope = NULL_TREE; |
a723baf1 | 12575 | unsigned num_templates; |
cad7e87b | 12576 | tree bases; |
a723baf1 MM |
12577 | |
12578 | /* Assume no nested-name-specifier will be present. */ | |
12579 | *nested_name_specifier_p = false; | |
12580 | /* Assume no template parameter lists will be used in defining the | |
12581 | type. */ | |
12582 | num_templates = 0; | |
12583 | ||
12584 | /* Look for the class-key. */ | |
12585 | class_key = cp_parser_class_key (parser); | |
12586 | if (class_key == none_type) | |
12587 | return error_mark_node; | |
12588 | ||
12589 | /* Parse the attributes. */ | |
12590 | attributes = cp_parser_attributes_opt (parser); | |
12591 | ||
12592 | /* If the next token is `::', that is invalid -- but sometimes | |
12593 | people do try to write: | |
12594 | ||
21526606 | 12595 | struct ::S {}; |
a723baf1 MM |
12596 | |
12597 | Handle this gracefully by accepting the extra qualifier, and then | |
12598 | issuing an error about it later if this really is a | |
2050a1bb | 12599 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
12600 | specifier, remain silent. */ |
12601 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
12602 | qualified_p = true; | |
12603 | ||
8d241e0b KL |
12604 | push_deferring_access_checks (dk_no_check); |
12605 | ||
a723baf1 MM |
12606 | /* Determine the name of the class. Begin by looking for an |
12607 | optional nested-name-specifier. */ | |
21526606 | 12608 | nested_name_specifier |
a723baf1 MM |
12609 | = cp_parser_nested_name_specifier_opt (parser, |
12610 | /*typename_keyword_p=*/false, | |
66d418e6 | 12611 | /*check_dependency_p=*/false, |
a668c6ad MM |
12612 | /*type_p=*/false, |
12613 | /*is_declaration=*/false); | |
a723baf1 MM |
12614 | /* If there was a nested-name-specifier, then there *must* be an |
12615 | identifier. */ | |
12616 | if (nested_name_specifier) | |
12617 | { | |
12618 | /* Although the grammar says `identifier', it really means | |
12619 | `class-name' or `template-name'. You are only allowed to | |
12620 | define a class that has already been declared with this | |
21526606 | 12621 | syntax. |
a723baf1 MM |
12622 | |
12623 | The proposed resolution for Core Issue 180 says that whever | |
12624 | you see `class T::X' you should treat `X' as a type-name. | |
21526606 | 12625 | |
a723baf1 | 12626 | It is OK to define an inaccessible class; for example: |
21526606 | 12627 | |
a723baf1 MM |
12628 | class A { class B; }; |
12629 | class A::B {}; | |
21526606 | 12630 | |
a723baf1 MM |
12631 | We do not know if we will see a class-name, or a |
12632 | template-name. We look for a class-name first, in case the | |
12633 | class-name is a template-id; if we looked for the | |
12634 | template-name first we would stop after the template-name. */ | |
12635 | cp_parser_parse_tentatively (parser); | |
12636 | type = cp_parser_class_name (parser, | |
12637 | /*typename_keyword_p=*/false, | |
12638 | /*template_keyword_p=*/false, | |
fc6a28d7 | 12639 | class_type, |
a723baf1 | 12640 | /*check_dependency_p=*/false, |
a668c6ad MM |
12641 | /*class_head_p=*/true, |
12642 | /*is_declaration=*/false); | |
a723baf1 MM |
12643 | /* If that didn't work, ignore the nested-name-specifier. */ |
12644 | if (!cp_parser_parse_definitely (parser)) | |
12645 | { | |
12646 | invalid_nested_name_p = true; | |
12647 | id = cp_parser_identifier (parser); | |
12648 | if (id == error_mark_node) | |
12649 | id = NULL_TREE; | |
12650 | } | |
12651 | /* If we could not find a corresponding TYPE, treat this | |
12652 | declaration like an unqualified declaration. */ | |
12653 | if (type == error_mark_node) | |
12654 | nested_name_specifier = NULL_TREE; | |
12655 | /* Otherwise, count the number of templates used in TYPE and its | |
12656 | containing scopes. */ | |
21526606 | 12657 | else |
a723baf1 MM |
12658 | { |
12659 | tree scope; | |
12660 | ||
21526606 | 12661 | for (scope = TREE_TYPE (type); |
a723baf1 | 12662 | scope && TREE_CODE (scope) != NAMESPACE_DECL; |
21526606 | 12663 | scope = (TYPE_P (scope) |
a723baf1 | 12664 | ? TYPE_CONTEXT (scope) |
21526606 EC |
12665 | : DECL_CONTEXT (scope))) |
12666 | if (TYPE_P (scope) | |
a723baf1 MM |
12667 | && CLASS_TYPE_P (scope) |
12668 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
12669 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
12670 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
12671 | ++num_templates; |
12672 | } | |
12673 | } | |
12674 | /* Otherwise, the identifier is optional. */ | |
12675 | else | |
12676 | { | |
12677 | /* We don't know whether what comes next is a template-id, | |
12678 | an identifier, or nothing at all. */ | |
12679 | cp_parser_parse_tentatively (parser); | |
12680 | /* Check for a template-id. */ | |
21526606 | 12681 | id = cp_parser_template_id (parser, |
a723baf1 | 12682 | /*template_keyword_p=*/false, |
a668c6ad MM |
12683 | /*check_dependency_p=*/true, |
12684 | /*is_declaration=*/true); | |
a723baf1 MM |
12685 | /* If that didn't work, it could still be an identifier. */ |
12686 | if (!cp_parser_parse_definitely (parser)) | |
12687 | { | |
12688 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
12689 | id = cp_parser_identifier (parser); | |
12690 | else | |
12691 | id = NULL_TREE; | |
12692 | } | |
12693 | else | |
12694 | { | |
12695 | template_id_p = true; | |
12696 | ++num_templates; | |
12697 | } | |
12698 | } | |
12699 | ||
8d241e0b KL |
12700 | pop_deferring_access_checks (); |
12701 | ||
15077df5 MM |
12702 | if (id) |
12703 | cp_parser_check_for_invalid_template_id (parser, id); | |
ee43dab5 | 12704 | |
a723baf1 MM |
12705 | /* If it's not a `:' or a `{' then we can't really be looking at a |
12706 | class-head, since a class-head only appears as part of a | |
12707 | class-specifier. We have to detect this situation before calling | |
12708 | xref_tag, since that has irreversible side-effects. */ | |
12709 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
12710 | { | |
2a13a625 | 12711 | cp_parser_error (parser, "expected %<{%> or %<:%>"); |
a723baf1 MM |
12712 | return error_mark_node; |
12713 | } | |
12714 | ||
12715 | /* At this point, we're going ahead with the class-specifier, even | |
12716 | if some other problem occurs. */ | |
12717 | cp_parser_commit_to_tentative_parse (parser); | |
12718 | /* Issue the error about the overly-qualified name now. */ | |
12719 | if (qualified_p) | |
12720 | cp_parser_error (parser, | |
12721 | "global qualification of class name is invalid"); | |
12722 | else if (invalid_nested_name_p) | |
12723 | cp_parser_error (parser, | |
12724 | "qualified name does not name a class"); | |
88081599 MM |
12725 | else if (nested_name_specifier) |
12726 | { | |
12727 | tree scope; | |
9bf0e588 VR |
12728 | |
12729 | /* Reject typedef-names in class heads. */ | |
12730 | if (!DECL_IMPLICIT_TYPEDEF_P (type)) | |
12731 | { | |
12732 | error ("invalid class name in declaration of %qD", type); | |
12733 | type = NULL_TREE; | |
12734 | goto done; | |
12735 | } | |
12736 | ||
88081599 MM |
12737 | /* Figure out in what scope the declaration is being placed. */ |
12738 | scope = current_scope (); | |
88081599 MM |
12739 | /* If that scope does not contain the scope in which the |
12740 | class was originally declared, the program is invalid. */ | |
12741 | if (scope && !is_ancestor (scope, nested_name_specifier)) | |
12742 | { | |
2a13a625 GDR |
12743 | error ("declaration of %qD in %qD which does not enclose %qD", |
12744 | type, scope, nested_name_specifier); | |
88081599 MM |
12745 | type = NULL_TREE; |
12746 | goto done; | |
12747 | } | |
12748 | /* [dcl.meaning] | |
12749 | ||
12750 | A declarator-id shall not be qualified exception of the | |
12751 | definition of a ... nested class outside of its class | |
12752 | ... [or] a the definition or explicit instantiation of a | |
12753 | class member of a namespace outside of its namespace. */ | |
12754 | if (scope == nested_name_specifier) | |
12755 | { | |
12756 | pedwarn ("extra qualification ignored"); | |
12757 | nested_name_specifier = NULL_TREE; | |
12758 | num_templates = 0; | |
12759 | } | |
12760 | } | |
afb0918a MM |
12761 | /* An explicit-specialization must be preceded by "template <>". If |
12762 | it is not, try to recover gracefully. */ | |
21526606 | 12763 | if (at_namespace_scope_p () |
afb0918a | 12764 | && parser->num_template_parameter_lists == 0 |
eeb23c11 | 12765 | && template_id_p) |
afb0918a | 12766 | { |
2a13a625 | 12767 | error ("an explicit specialization must be preceded by %<template <>%>"); |
afb0918a MM |
12768 | invalid_explicit_specialization_p = true; |
12769 | /* Take the same action that would have been taken by | |
12770 | cp_parser_explicit_specialization. */ | |
12771 | ++parser->num_template_parameter_lists; | |
12772 | begin_specialization (); | |
12773 | } | |
12774 | /* There must be no "return" statements between this point and the | |
12775 | end of this function; set "type "to the correct return value and | |
12776 | use "goto done;" to return. */ | |
a723baf1 MM |
12777 | /* Make sure that the right number of template parameters were |
12778 | present. */ | |
12779 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
12780 | { |
12781 | /* If something went wrong, there is no point in even trying to | |
12782 | process the class-definition. */ | |
12783 | type = NULL_TREE; | |
12784 | goto done; | |
12785 | } | |
a723baf1 | 12786 | |
a723baf1 MM |
12787 | /* Look up the type. */ |
12788 | if (template_id_p) | |
12789 | { | |
12790 | type = TREE_TYPE (id); | |
12791 | maybe_process_partial_specialization (type); | |
4514aa8c NS |
12792 | if (nested_name_specifier) |
12793 | pushed_scope = push_scope (nested_name_specifier); | |
a723baf1 | 12794 | } |
4514aa8c | 12795 | else if (nested_name_specifier) |
a723baf1 | 12796 | { |
a723baf1 MM |
12797 | tree class_type; |
12798 | ||
12799 | /* Given: | |
12800 | ||
12801 | template <typename T> struct S { struct T }; | |
14d22dd6 | 12802 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
12803 | |
12804 | we will get a TYPENAME_TYPE when processing the definition of | |
12805 | `S::T'. We need to resolve it to the actual type before we | |
12806 | try to define it. */ | |
12807 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
12808 | { | |
14d22dd6 MM |
12809 | class_type = resolve_typename_type (TREE_TYPE (type), |
12810 | /*only_current_p=*/false); | |
12811 | if (class_type != error_mark_node) | |
12812 | type = TYPE_NAME (class_type); | |
12813 | else | |
12814 | { | |
12815 | cp_parser_error (parser, "could not resolve typename type"); | |
12816 | type = error_mark_node; | |
12817 | } | |
a723baf1 MM |
12818 | } |
12819 | ||
560ad596 MM |
12820 | maybe_process_partial_specialization (TREE_TYPE (type)); |
12821 | class_type = current_class_type; | |
12822 | /* Enter the scope indicated by the nested-name-specifier. */ | |
4514aa8c | 12823 | pushed_scope = push_scope (nested_name_specifier); |
560ad596 MM |
12824 | /* Get the canonical version of this type. */ |
12825 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
12826 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
12827 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
55dcbc12 NS |
12828 | { |
12829 | type = push_template_decl (type); | |
12830 | if (type == error_mark_node) | |
12831 | { | |
12832 | type = NULL_TREE; | |
12833 | goto done; | |
12834 | } | |
12835 | } | |
12836 | ||
560ad596 | 12837 | type = TREE_TYPE (type); |
4514aa8c | 12838 | *nested_name_specifier_p = true; |
a723baf1 | 12839 | } |
4514aa8c NS |
12840 | else /* The name is not a nested name. */ |
12841 | { | |
12842 | /* If the class was unnamed, create a dummy name. */ | |
12843 | if (!id) | |
12844 | id = make_anon_name (); | |
12845 | type = xref_tag (class_key, id, /*tag_scope=*/ts_current, | |
12846 | parser->num_template_parameter_lists); | |
12847 | } | |
12848 | ||
a723baf1 MM |
12849 | /* Indicate whether this class was declared as a `class' or as a |
12850 | `struct'. */ | |
12851 | if (TREE_CODE (type) == RECORD_TYPE) | |
12852 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
12853 | cp_parser_check_class_key (class_key, type); | |
12854 | ||
744b12b6 MM |
12855 | /* If this type was already complete, and we see another definition, |
12856 | that's an error. */ | |
12857 | if (type != error_mark_node && COMPLETE_TYPE_P (type)) | |
12858 | { | |
12859 | error ("redefinition of %q#T", type); | |
12860 | cp_error_at ("previous definition of %q#T", type); | |
0f3744f8 VR |
12861 | type = NULL_TREE; |
12862 | goto done; | |
744b12b6 MM |
12863 | } |
12864 | ||
4514aa8c | 12865 | /* We will have entered the scope containing the class; the names of |
744b12b6 | 12866 | base classes should be looked up in that context. For example: |
a723baf1 MM |
12867 | |
12868 | struct A { struct B {}; struct C; }; | |
12869 | struct A::C : B {}; | |
12870 | ||
12871 | is valid. */ | |
cad7e87b | 12872 | bases = NULL_TREE; |
98ca843c | 12873 | |
cad7e87b NS |
12874 | /* Get the list of base-classes, if there is one. */ |
12875 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
12876 | bases = cp_parser_base_clause (parser); | |
98ca843c | 12877 | |
cad7e87b NS |
12878 | /* Process the base classes. */ |
12879 | xref_basetypes (type, bases); | |
a723baf1 | 12880 | |
4514aa8c | 12881 | done: |
a723baf1 MM |
12882 | /* Leave the scope given by the nested-name-specifier. We will |
12883 | enter the class scope itself while processing the members. */ | |
4514aa8c NS |
12884 | if (pushed_scope) |
12885 | pop_scope (pushed_scope); | |
a723baf1 | 12886 | |
afb0918a MM |
12887 | if (invalid_explicit_specialization_p) |
12888 | { | |
12889 | end_specialization (); | |
12890 | --parser->num_template_parameter_lists; | |
12891 | } | |
38b305d0 | 12892 | *attributes_p = attributes; |
a723baf1 MM |
12893 | return type; |
12894 | } | |
12895 | ||
12896 | /* Parse a class-key. | |
12897 | ||
12898 | class-key: | |
12899 | class | |
12900 | struct | |
12901 | union | |
12902 | ||
12903 | Returns the kind of class-key specified, or none_type to indicate | |
12904 | error. */ | |
12905 | ||
12906 | static enum tag_types | |
94edc4ab | 12907 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
12908 | { |
12909 | cp_token *token; | |
12910 | enum tag_types tag_type; | |
12911 | ||
12912 | /* Look for the class-key. */ | |
12913 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
12914 | if (!token) | |
12915 | return none_type; | |
12916 | ||
12917 | /* Check to see if the TOKEN is a class-key. */ | |
12918 | tag_type = cp_parser_token_is_class_key (token); | |
12919 | if (!tag_type) | |
12920 | cp_parser_error (parser, "expected class-key"); | |
12921 | return tag_type; | |
12922 | } | |
12923 | ||
12924 | /* Parse an (optional) member-specification. | |
12925 | ||
12926 | member-specification: | |
12927 | member-declaration member-specification [opt] | |
12928 | access-specifier : member-specification [opt] */ | |
12929 | ||
12930 | static void | |
94edc4ab | 12931 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
12932 | { |
12933 | while (true) | |
12934 | { | |
12935 | cp_token *token; | |
12936 | enum rid keyword; | |
12937 | ||
12938 | /* Peek at the next token. */ | |
12939 | token = cp_lexer_peek_token (parser->lexer); | |
12940 | /* If it's a `}', or EOF then we've seen all the members. */ | |
12941 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
12942 | break; | |
12943 | ||
12944 | /* See if this token is a keyword. */ | |
12945 | keyword = token->keyword; | |
12946 | switch (keyword) | |
12947 | { | |
12948 | case RID_PUBLIC: | |
12949 | case RID_PROTECTED: | |
12950 | case RID_PRIVATE: | |
12951 | /* Consume the access-specifier. */ | |
12952 | cp_lexer_consume_token (parser->lexer); | |
12953 | /* Remember which access-specifier is active. */ | |
12954 | current_access_specifier = token->value; | |
12955 | /* Look for the `:'. */ | |
12956 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12957 | break; | |
12958 | ||
12959 | default: | |
de3fe73c MM |
12960 | /* Accept #pragmas at class scope. */ |
12961 | if (token->type == CPP_PRAGMA) | |
12962 | { | |
12963 | cp_lexer_handle_pragma (parser->lexer); | |
12964 | break; | |
12965 | } | |
12966 | ||
a723baf1 MM |
12967 | /* Otherwise, the next construction must be a |
12968 | member-declaration. */ | |
12969 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
12970 | } |
12971 | } | |
12972 | } | |
12973 | ||
21526606 | 12974 | /* Parse a member-declaration. |
a723baf1 MM |
12975 | |
12976 | member-declaration: | |
12977 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
12978 | function-definition ; [opt] | |
12979 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
12980 | using-declaration | |
21526606 | 12981 | template-declaration |
a723baf1 MM |
12982 | |
12983 | member-declarator-list: | |
12984 | member-declarator | |
12985 | member-declarator-list , member-declarator | |
12986 | ||
12987 | member-declarator: | |
21526606 | 12988 | declarator pure-specifier [opt] |
a723baf1 | 12989 | declarator constant-initializer [opt] |
21526606 | 12990 | identifier [opt] : constant-expression |
a723baf1 MM |
12991 | |
12992 | GNU Extensions: | |
12993 | ||
12994 | member-declaration: | |
12995 | __extension__ member-declaration | |
12996 | ||
12997 | member-declarator: | |
12998 | declarator attributes [opt] pure-specifier [opt] | |
12999 | declarator attributes [opt] constant-initializer [opt] | |
13000 | identifier [opt] attributes [opt] : constant-expression */ | |
13001 | ||
13002 | static void | |
94edc4ab | 13003 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 | 13004 | { |
62d1db17 | 13005 | cp_decl_specifier_seq decl_specifiers; |
a723baf1 MM |
13006 | tree prefix_attributes; |
13007 | tree decl; | |
560ad596 | 13008 | int declares_class_or_enum; |
a723baf1 MM |
13009 | bool friend_p; |
13010 | cp_token *token; | |
13011 | int saved_pedantic; | |
13012 | ||
13013 | /* Check for the `__extension__' keyword. */ | |
13014 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
13015 | { | |
13016 | /* Recurse. */ | |
13017 | cp_parser_member_declaration (parser); | |
13018 | /* Restore the old value of the PEDANTIC flag. */ | |
13019 | pedantic = saved_pedantic; | |
13020 | ||
13021 | return; | |
13022 | } | |
13023 | ||
13024 | /* Check for a template-declaration. */ | |
13025 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
13026 | { | |
13027 | /* Parse the template-declaration. */ | |
13028 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
13029 | ||
13030 | return; | |
13031 | } | |
13032 | ||
13033 | /* Check for a using-declaration. */ | |
13034 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
13035 | { | |
13036 | /* Parse the using-declaration. */ | |
13037 | cp_parser_using_declaration (parser); | |
13038 | ||
13039 | return; | |
13040 | } | |
21526606 | 13041 | |
a723baf1 | 13042 | /* Parse the decl-specifier-seq. */ |
62d1db17 MM |
13043 | cp_parser_decl_specifier_seq (parser, |
13044 | CP_PARSER_FLAGS_OPTIONAL, | |
13045 | &decl_specifiers, | |
13046 | &declares_class_or_enum); | |
13047 | prefix_attributes = decl_specifiers.attributes; | |
13048 | decl_specifiers.attributes = NULL_TREE; | |
8fbc5ae7 | 13049 | /* Check for an invalid type-name. */ |
de3fe73c MM |
13050 | if (!decl_specifiers.type |
13051 | && cp_parser_parse_and_diagnose_invalid_type_name (parser)) | |
8fbc5ae7 | 13052 | return; |
a723baf1 MM |
13053 | /* If there is no declarator, then the decl-specifier-seq should |
13054 | specify a type. */ | |
13055 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13056 | { | |
13057 | /* If there was no decl-specifier-seq, and the next token is a | |
13058 | `;', then we have something like: | |
13059 | ||
13060 | struct S { ; }; | |
13061 | ||
13062 | [class.mem] | |
13063 | ||
13064 | Each member-declaration shall declare at least one member | |
13065 | name of the class. */ | |
62d1db17 | 13066 | if (!decl_specifiers.any_specifiers_p) |
a723baf1 | 13067 | { |
2cfe82fe ZW |
13068 | cp_token *token = cp_lexer_peek_token (parser->lexer); |
13069 | if (pedantic && !token->in_system_header) | |
13070 | pedwarn ("%Hextra %<;%>", &token->location); | |
a723baf1 | 13071 | } |
21526606 | 13072 | else |
a723baf1 MM |
13073 | { |
13074 | tree type; | |
21526606 | 13075 | |
a723baf1 | 13076 | /* See if this declaration is a friend. */ |
62d1db17 | 13077 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 MM |
13078 | /* If there were decl-specifiers, check to see if there was |
13079 | a class-declaration. */ | |
62d1db17 | 13080 | type = check_tag_decl (&decl_specifiers); |
a723baf1 MM |
13081 | /* Nested classes have already been added to the class, but |
13082 | a `friend' needs to be explicitly registered. */ | |
13083 | if (friend_p) | |
13084 | { | |
13085 | /* If the `friend' keyword was present, the friend must | |
13086 | be introduced with a class-key. */ | |
13087 | if (!declares_class_or_enum) | |
13088 | error ("a class-key must be used when declaring a friend"); | |
13089 | /* In this case: | |
13090 | ||
21526606 EC |
13091 | template <typename T> struct A { |
13092 | friend struct A<T>::B; | |
a723baf1 | 13093 | }; |
21526606 | 13094 | |
a723baf1 MM |
13095 | A<T>::B will be represented by a TYPENAME_TYPE, and |
13096 | therefore not recognized by check_tag_decl. */ | |
98ca843c | 13097 | if (!type |
62d1db17 MM |
13098 | && decl_specifiers.type |
13099 | && TYPE_P (decl_specifiers.type)) | |
13100 | type = decl_specifiers.type; | |
fdd09134 | 13101 | if (!type || !TYPE_P (type)) |
a723baf1 MM |
13102 | error ("friend declaration does not name a class or " |
13103 | "function"); | |
13104 | else | |
19db77ce KL |
13105 | make_friend_class (current_class_type, type, |
13106 | /*complain=*/true); | |
a723baf1 MM |
13107 | } |
13108 | /* If there is no TYPE, an error message will already have | |
13109 | been issued. */ | |
62d1db17 | 13110 | else if (!type || type == error_mark_node) |
a723baf1 MM |
13111 | ; |
13112 | /* An anonymous aggregate has to be handled specially; such | |
13113 | a declaration really declares a data member (with a | |
13114 | particular type), as opposed to a nested class. */ | |
13115 | else if (ANON_AGGR_TYPE_P (type)) | |
13116 | { | |
13117 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 13118 | know it is an anonymous aggregate. */ |
a723baf1 MM |
13119 | fixup_anonymous_aggr (type); |
13120 | /* And make the corresponding data member. */ | |
13121 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
13122 | /* Add it to the class. */ | |
13123 | finish_member_declaration (decl); | |
13124 | } | |
37d407a1 KL |
13125 | else |
13126 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
13127 | } |
13128 | } | |
13129 | else | |
13130 | { | |
13131 | /* See if these declarations will be friends. */ | |
62d1db17 | 13132 | friend_p = cp_parser_friend_p (&decl_specifiers); |
a723baf1 | 13133 | |
21526606 | 13134 | /* Keep going until we hit the `;' at the end of the |
a723baf1 MM |
13135 | declaration. */ |
13136 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
13137 | { | |
13138 | tree attributes = NULL_TREE; | |
13139 | tree first_attribute; | |
13140 | ||
13141 | /* Peek at the next token. */ | |
13142 | token = cp_lexer_peek_token (parser->lexer); | |
13143 | ||
13144 | /* Check for a bitfield declaration. */ | |
13145 | if (token->type == CPP_COLON | |
13146 | || (token->type == CPP_NAME | |
21526606 | 13147 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
13148 | == CPP_COLON)) |
13149 | { | |
13150 | tree identifier; | |
13151 | tree width; | |
13152 | ||
13153 | /* Get the name of the bitfield. Note that we cannot just | |
13154 | check TOKEN here because it may have been invalidated by | |
13155 | the call to cp_lexer_peek_nth_token above. */ | |
13156 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
13157 | identifier = cp_parser_identifier (parser); | |
13158 | else | |
13159 | identifier = NULL_TREE; | |
13160 | ||
13161 | /* Consume the `:' token. */ | |
13162 | cp_lexer_consume_token (parser->lexer); | |
13163 | /* Get the width of the bitfield. */ | |
21526606 | 13164 | width |
14d22dd6 MM |
13165 | = cp_parser_constant_expression (parser, |
13166 | /*allow_non_constant=*/false, | |
13167 | NULL); | |
a723baf1 MM |
13168 | |
13169 | /* Look for attributes that apply to the bitfield. */ | |
13170 | attributes = cp_parser_attributes_opt (parser); | |
13171 | /* Remember which attributes are prefix attributes and | |
13172 | which are not. */ | |
13173 | first_attribute = attributes; | |
13174 | /* Combine the attributes. */ | |
13175 | attributes = chainon (prefix_attributes, attributes); | |
13176 | ||
13177 | /* Create the bitfield declaration. */ | |
98ca843c | 13178 | decl = grokbitfield (identifier |
1d786913 MM |
13179 | ? make_id_declarator (NULL_TREE, |
13180 | identifier) | |
058b15c1 | 13181 | : NULL, |
62d1db17 | 13182 | &decl_specifiers, |
a723baf1 MM |
13183 | width); |
13184 | /* Apply the attributes. */ | |
13185 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
13186 | } | |
13187 | else | |
13188 | { | |
058b15c1 | 13189 | cp_declarator *declarator; |
a723baf1 MM |
13190 | tree initializer; |
13191 | tree asm_specification; | |
7efa3e22 | 13192 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
13193 | |
13194 | /* Parse the declarator. */ | |
21526606 | 13195 | declarator |
62b8a44e | 13196 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 | 13197 | &ctor_dtor_or_conv_p, |
db86dd14 MM |
13198 | /*parenthesized_p=*/NULL, |
13199 | /*member_p=*/true); | |
a723baf1 MM |
13200 | |
13201 | /* If something went wrong parsing the declarator, make sure | |
13202 | that we at least consume some tokens. */ | |
058b15c1 | 13203 | if (declarator == cp_error_declarator) |
a723baf1 MM |
13204 | { |
13205 | /* Skip to the end of the statement. */ | |
13206 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
13207 | /* If the next token is not a semicolon, that is |
13208 | probably because we just skipped over the body of | |
13209 | a function. So, we consume a semicolon if | |
13210 | present, but do not issue an error message if it | |
13211 | is not present. */ | |
13212 | if (cp_lexer_next_token_is (parser->lexer, | |
13213 | CPP_SEMICOLON)) | |
13214 | cp_lexer_consume_token (parser->lexer); | |
13215 | return; | |
a723baf1 MM |
13216 | } |
13217 | ||
fc6a28d7 MM |
13218 | if (declares_class_or_enum & 2) |
13219 | cp_parser_check_for_definition_in_return_type | |
13220 | (declarator, decl_specifiers.type); | |
560ad596 | 13221 | |
a723baf1 MM |
13222 | /* Look for an asm-specification. */ |
13223 | asm_specification = cp_parser_asm_specification_opt (parser); | |
13224 | /* Look for attributes that apply to the declaration. */ | |
13225 | attributes = cp_parser_attributes_opt (parser); | |
13226 | /* Remember which attributes are prefix attributes and | |
13227 | which are not. */ | |
13228 | first_attribute = attributes; | |
13229 | /* Combine the attributes. */ | |
13230 | attributes = chainon (prefix_attributes, attributes); | |
13231 | ||
13232 | /* If it's an `=', then we have a constant-initializer or a | |
13233 | pure-specifier. It is not correct to parse the | |
13234 | initializer before registering the member declaration | |
13235 | since the member declaration should be in scope while | |
13236 | its initializer is processed. However, the rest of the | |
13237 | front end does not yet provide an interface that allows | |
13238 | us to handle this correctly. */ | |
13239 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
13240 | { | |
13241 | /* In [class.mem]: | |
13242 | ||
13243 | A pure-specifier shall be used only in the declaration of | |
21526606 | 13244 | a virtual function. |
a723baf1 MM |
13245 | |
13246 | A member-declarator can contain a constant-initializer | |
13247 | only if it declares a static member of integral or | |
21526606 | 13248 | enumeration type. |
a723baf1 MM |
13249 | |
13250 | Therefore, if the DECLARATOR is for a function, we look | |
13251 | for a pure-specifier; otherwise, we look for a | |
13252 | constant-initializer. When we call `grokfield', it will | |
13253 | perform more stringent semantics checks. */ | |
058b15c1 | 13254 | if (declarator->kind == cdk_function) |
a723baf1 MM |
13255 | initializer = cp_parser_pure_specifier (parser); |
13256 | else | |
4bb8ca28 MM |
13257 | /* Parse the initializer. */ |
13258 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
13259 | } |
13260 | /* Otherwise, there is no initializer. */ | |
13261 | else | |
13262 | initializer = NULL_TREE; | |
13263 | ||
13264 | /* See if we are probably looking at a function | |
5a19910e | 13265 | definition. We are certainly not looking at a |
a723baf1 MM |
13266 | member-declarator. Calling `grokfield' has |
13267 | side-effects, so we must not do it unless we are sure | |
13268 | that we are looking at a member-declarator. */ | |
21526606 | 13269 | if (cp_parser_token_starts_function_definition_p |
a723baf1 | 13270 | (cp_lexer_peek_token (parser->lexer))) |
4bb8ca28 MM |
13271 | { |
13272 | /* The grammar does not allow a pure-specifier to be | |
13273 | used when a member function is defined. (It is | |
13274 | possible that this fact is an oversight in the | |
13275 | standard, since a pure function may be defined | |
13276 | outside of the class-specifier. */ | |
13277 | if (initializer) | |
13278 | error ("pure-specifier on function-definition"); | |
13279 | decl = cp_parser_save_member_function_body (parser, | |
62d1db17 | 13280 | &decl_specifiers, |
4bb8ca28 MM |
13281 | declarator, |
13282 | attributes); | |
13283 | /* If the member was not a friend, declare it here. */ | |
13284 | if (!friend_p) | |
13285 | finish_member_declaration (decl); | |
13286 | /* Peek at the next token. */ | |
13287 | token = cp_lexer_peek_token (parser->lexer); | |
13288 | /* If the next token is a semicolon, consume it. */ | |
13289 | if (token->type == CPP_SEMICOLON) | |
13290 | cp_lexer_consume_token (parser->lexer); | |
13291 | return; | |
13292 | } | |
a723baf1 | 13293 | else |
39703eb9 MM |
13294 | { |
13295 | /* Create the declaration. */ | |
62d1db17 | 13296 | decl = grokfield (declarator, &decl_specifiers, |
ee3071ef | 13297 | initializer, asm_specification, |
39703eb9 MM |
13298 | attributes); |
13299 | /* Any initialization must have been from a | |
13300 | constant-expression. */ | |
13301 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
13302 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
13303 | } | |
a723baf1 MM |
13304 | } |
13305 | ||
13306 | /* Reset PREFIX_ATTRIBUTES. */ | |
13307 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
13308 | attributes = TREE_CHAIN (attributes); | |
13309 | if (attributes) | |
13310 | TREE_CHAIN (attributes) = NULL_TREE; | |
13311 | ||
13312 | /* If there is any qualification still in effect, clear it | |
13313 | now; we will be starting fresh with the next declarator. */ | |
13314 | parser->scope = NULL_TREE; | |
13315 | parser->qualifying_scope = NULL_TREE; | |
13316 | parser->object_scope = NULL_TREE; | |
13317 | /* If it's a `,', then there are more declarators. */ | |
13318 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
13319 | cp_lexer_consume_token (parser->lexer); | |
13320 | /* If the next token isn't a `;', then we have a parse error. */ | |
13321 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
13322 | CPP_SEMICOLON)) | |
13323 | { | |
2a13a625 | 13324 | cp_parser_error (parser, "expected %<;%>"); |
04c06002 | 13325 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
13326 | cp_parser_skip_to_end_of_statement (parser); |
13327 | ||
13328 | break; | |
13329 | } | |
13330 | ||
13331 | if (decl) | |
13332 | { | |
13333 | /* Add DECL to the list of members. */ | |
13334 | if (!friend_p) | |
13335 | finish_member_declaration (decl); | |
13336 | ||
a723baf1 | 13337 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 13338 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
13339 | } |
13340 | } | |
13341 | } | |
13342 | ||
4bb8ca28 | 13343 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
13344 | } |
13345 | ||
13346 | /* Parse a pure-specifier. | |
13347 | ||
13348 | pure-specifier: | |
13349 | = 0 | |
13350 | ||
13351 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 13352 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
13353 | |
13354 | static tree | |
94edc4ab | 13355 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
13356 | { |
13357 | cp_token *token; | |
13358 | ||
13359 | /* Look for the `=' token. */ | |
13360 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
13361 | return error_mark_node; | |
13362 | /* Look for the `0' token. */ | |
515e6a84 GB |
13363 | token = cp_lexer_consume_token (parser->lexer); |
13364 | if (token->type != CPP_NUMBER || !integer_zerop (token->value)) | |
13365 | { | |
13366 | cp_parser_error (parser, | |
13367 | "invalid pure specifier (only `= 0' is allowed)"); | |
13368 | cp_parser_skip_to_end_of_statement (parser); | |
13369 | return error_mark_node; | |
13370 | } | |
a723baf1 | 13371 | |
515e6a84 GB |
13372 | /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well. |
13373 | We need to get information from the lexer about how the number | |
13374 | was spelled in order to fix this problem. */ | |
a723baf1 MM |
13375 | return integer_zero_node; |
13376 | } | |
13377 | ||
13378 | /* Parse a constant-initializer. | |
13379 | ||
13380 | constant-initializer: | |
13381 | = constant-expression | |
13382 | ||
13383 | Returns a representation of the constant-expression. */ | |
13384 | ||
13385 | static tree | |
94edc4ab | 13386 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
13387 | { |
13388 | /* Look for the `=' token. */ | |
13389 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
13390 | return error_mark_node; | |
13391 | ||
13392 | /* It is invalid to write: | |
13393 | ||
13394 | struct S { static const int i = { 7 }; }; | |
13395 | ||
13396 | */ | |
13397 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
13398 | { | |
13399 | cp_parser_error (parser, | |
13400 | "a brace-enclosed initializer is not allowed here"); | |
13401 | /* Consume the opening brace. */ | |
13402 | cp_lexer_consume_token (parser->lexer); | |
13403 | /* Skip the initializer. */ | |
13404 | cp_parser_skip_to_closing_brace (parser); | |
13405 | /* Look for the trailing `}'. */ | |
13406 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
21526606 | 13407 | |
a723baf1 MM |
13408 | return error_mark_node; |
13409 | } | |
13410 | ||
21526606 | 13411 | return cp_parser_constant_expression (parser, |
14d22dd6 MM |
13412 | /*allow_non_constant=*/false, |
13413 | NULL); | |
a723baf1 MM |
13414 | } |
13415 | ||
13416 | /* Derived classes [gram.class.derived] */ | |
13417 | ||
13418 | /* Parse a base-clause. | |
13419 | ||
13420 | base-clause: | |
21526606 | 13421 | : base-specifier-list |
a723baf1 MM |
13422 | |
13423 | base-specifier-list: | |
13424 | base-specifier | |
13425 | base-specifier-list , base-specifier | |
13426 | ||
13427 | Returns a TREE_LIST representing the base-classes, in the order in | |
13428 | which they were declared. The representation of each node is as | |
21526606 | 13429 | described by cp_parser_base_specifier. |
a723baf1 MM |
13430 | |
13431 | In the case that no bases are specified, this function will return | |
13432 | NULL_TREE, not ERROR_MARK_NODE. */ | |
13433 | ||
13434 | static tree | |
94edc4ab | 13435 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
13436 | { |
13437 | tree bases = NULL_TREE; | |
13438 | ||
13439 | /* Look for the `:' that begins the list. */ | |
13440 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
13441 | ||
13442 | /* Scan the base-specifier-list. */ | |
13443 | while (true) | |
13444 | { | |
13445 | cp_token *token; | |
13446 | tree base; | |
13447 | ||
13448 | /* Look for the base-specifier. */ | |
13449 | base = cp_parser_base_specifier (parser); | |
13450 | /* Add BASE to the front of the list. */ | |
13451 | if (base != error_mark_node) | |
13452 | { | |
13453 | TREE_CHAIN (base) = bases; | |
13454 | bases = base; | |
13455 | } | |
13456 | /* Peek at the next token. */ | |
13457 | token = cp_lexer_peek_token (parser->lexer); | |
13458 | /* If it's not a comma, then the list is complete. */ | |
13459 | if (token->type != CPP_COMMA) | |
13460 | break; | |
13461 | /* Consume the `,'. */ | |
13462 | cp_lexer_consume_token (parser->lexer); | |
13463 | } | |
13464 | ||
13465 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
13466 | base class had a qualified name. However, the next name that | |
13467 | appears is certainly not qualified. */ | |
13468 | parser->scope = NULL_TREE; | |
13469 | parser->qualifying_scope = NULL_TREE; | |
13470 | parser->object_scope = NULL_TREE; | |
13471 | ||
13472 | return nreverse (bases); | |
13473 | } | |
13474 | ||
13475 | /* Parse a base-specifier. | |
13476 | ||
13477 | base-specifier: | |
13478 | :: [opt] nested-name-specifier [opt] class-name | |
13479 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
13480 | [opt] class-name | |
13481 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
13482 | [opt] class-name | |
13483 | ||
13484 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
13485 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
13486 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
13487 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
21526606 | 13488 | |
a723baf1 | 13489 | static tree |
94edc4ab | 13490 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
13491 | { |
13492 | cp_token *token; | |
13493 | bool done = false; | |
13494 | bool virtual_p = false; | |
13495 | bool duplicate_virtual_error_issued_p = false; | |
13496 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 13497 | bool class_scope_p, template_p; |
dbbf88d1 | 13498 | tree access = access_default_node; |
a723baf1 MM |
13499 | tree type; |
13500 | ||
13501 | /* Process the optional `virtual' and `access-specifier'. */ | |
13502 | while (!done) | |
13503 | { | |
13504 | /* Peek at the next token. */ | |
13505 | token = cp_lexer_peek_token (parser->lexer); | |
13506 | /* Process `virtual'. */ | |
13507 | switch (token->keyword) | |
13508 | { | |
13509 | case RID_VIRTUAL: | |
13510 | /* If `virtual' appears more than once, issue an error. */ | |
13511 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
13512 | { | |
13513 | cp_parser_error (parser, | |
2a13a625 | 13514 | "%<virtual%> specified more than once in base-specified"); |
a723baf1 MM |
13515 | duplicate_virtual_error_issued_p = true; |
13516 | } | |
13517 | ||
13518 | virtual_p = true; | |
13519 | ||
13520 | /* Consume the `virtual' token. */ | |
13521 | cp_lexer_consume_token (parser->lexer); | |
13522 | ||
13523 | break; | |
13524 | ||
13525 | case RID_PUBLIC: | |
13526 | case RID_PROTECTED: | |
13527 | case RID_PRIVATE: | |
13528 | /* If more than one access specifier appears, issue an | |
13529 | error. */ | |
dbbf88d1 NS |
13530 | if (access != access_default_node |
13531 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
13532 | { |
13533 | cp_parser_error (parser, | |
13534 | "more than one access specifier in base-specified"); | |
13535 | duplicate_access_error_issued_p = true; | |
13536 | } | |
13537 | ||
dbbf88d1 | 13538 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
13539 | |
13540 | /* Consume the access-specifier. */ | |
13541 | cp_lexer_consume_token (parser->lexer); | |
13542 | ||
13543 | break; | |
13544 | ||
13545 | default: | |
13546 | done = true; | |
13547 | break; | |
13548 | } | |
13549 | } | |
852dcbdd | 13550 | /* It is not uncommon to see programs mechanically, erroneously, use |
a3a503a5 | 13551 | the 'typename' keyword to denote (dependent) qualified types |
1ed53ef3 GB |
13552 | as base classes. */ |
13553 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
13554 | { | |
13555 | if (!processing_template_decl) | |
2a13a625 | 13556 | error ("keyword %<typename%> not allowed outside of templates"); |
1ed53ef3 | 13557 | else |
2a13a625 | 13558 | error ("keyword %<typename%> not allowed in this context " |
1ed53ef3 GB |
13559 | "(the base class is implicitly a type)"); |
13560 | cp_lexer_consume_token (parser->lexer); | |
13561 | } | |
a723baf1 | 13562 | |
a723baf1 MM |
13563 | /* Look for the optional `::' operator. */ |
13564 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
13565 | /* Look for the nested-name-specifier. The simplest way to | |
13566 | implement: | |
13567 | ||
13568 | [temp.res] | |
13569 | ||
13570 | The keyword `typename' is not permitted in a base-specifier or | |
13571 | mem-initializer; in these contexts a qualified name that | |
13572 | depends on a template-parameter is implicitly assumed to be a | |
13573 | type name. | |
13574 | ||
13575 | is to pretend that we have seen the `typename' keyword at this | |
21526606 | 13576 | point. */ |
a723baf1 MM |
13577 | cp_parser_nested_name_specifier_opt (parser, |
13578 | /*typename_keyword_p=*/true, | |
13579 | /*check_dependency_p=*/true, | |
fc6a28d7 | 13580 | typename_type, |
a668c6ad | 13581 | /*is_declaration=*/true); |
a723baf1 MM |
13582 | /* If the base class is given by a qualified name, assume that names |
13583 | we see are type names or templates, as appropriate. */ | |
13584 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 | 13585 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
21526606 | 13586 | |
a723baf1 | 13587 | /* Finally, look for the class-name. */ |
21526606 | 13588 | type = cp_parser_class_name (parser, |
a723baf1 | 13589 | class_scope_p, |
bbaab916 | 13590 | template_p, |
fc6a28d7 | 13591 | typename_type, |
a723baf1 | 13592 | /*check_dependency_p=*/true, |
a668c6ad MM |
13593 | /*class_head_p=*/false, |
13594 | /*is_declaration=*/true); | |
a723baf1 MM |
13595 | |
13596 | if (type == error_mark_node) | |
13597 | return error_mark_node; | |
13598 | ||
dbbf88d1 | 13599 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
13600 | } |
13601 | ||
13602 | /* Exception handling [gram.exception] */ | |
13603 | ||
13604 | /* Parse an (optional) exception-specification. | |
13605 | ||
13606 | exception-specification: | |
13607 | throw ( type-id-list [opt] ) | |
13608 | ||
13609 | Returns a TREE_LIST representing the exception-specification. The | |
13610 | TREE_VALUE of each node is a type. */ | |
13611 | ||
13612 | static tree | |
94edc4ab | 13613 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
13614 | { |
13615 | cp_token *token; | |
13616 | tree type_id_list; | |
13617 | ||
13618 | /* Peek at the next token. */ | |
13619 | token = cp_lexer_peek_token (parser->lexer); | |
13620 | /* If it's not `throw', then there's no exception-specification. */ | |
13621 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
13622 | return NULL_TREE; | |
13623 | ||
13624 | /* Consume the `throw'. */ | |
13625 | cp_lexer_consume_token (parser->lexer); | |
13626 | ||
13627 | /* Look for the `('. */ | |
13628 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13629 | ||
13630 | /* Peek at the next token. */ | |
13631 | token = cp_lexer_peek_token (parser->lexer); | |
13632 | /* If it's not a `)', then there is a type-id-list. */ | |
13633 | if (token->type != CPP_CLOSE_PAREN) | |
13634 | { | |
13635 | const char *saved_message; | |
13636 | ||
13637 | /* Types may not be defined in an exception-specification. */ | |
13638 | saved_message = parser->type_definition_forbidden_message; | |
13639 | parser->type_definition_forbidden_message | |
13640 | = "types may not be defined in an exception-specification"; | |
13641 | /* Parse the type-id-list. */ | |
13642 | type_id_list = cp_parser_type_id_list (parser); | |
13643 | /* Restore the saved message. */ | |
13644 | parser->type_definition_forbidden_message = saved_message; | |
13645 | } | |
13646 | else | |
13647 | type_id_list = empty_except_spec; | |
13648 | ||
13649 | /* Look for the `)'. */ | |
13650 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13651 | ||
13652 | return type_id_list; | |
13653 | } | |
13654 | ||
13655 | /* Parse an (optional) type-id-list. | |
13656 | ||
13657 | type-id-list: | |
13658 | type-id | |
13659 | type-id-list , type-id | |
13660 | ||
13661 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
13662 | in the order that the types were presented. */ | |
13663 | ||
13664 | static tree | |
94edc4ab | 13665 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
13666 | { |
13667 | tree types = NULL_TREE; | |
13668 | ||
13669 | while (true) | |
13670 | { | |
13671 | cp_token *token; | |
13672 | tree type; | |
13673 | ||
13674 | /* Get the next type-id. */ | |
13675 | type = cp_parser_type_id (parser); | |
13676 | /* Add it to the list. */ | |
13677 | types = add_exception_specifier (types, type, /*complain=*/1); | |
13678 | /* Peek at the next token. */ | |
13679 | token = cp_lexer_peek_token (parser->lexer); | |
13680 | /* If it is not a `,', we are done. */ | |
13681 | if (token->type != CPP_COMMA) | |
13682 | break; | |
13683 | /* Consume the `,'. */ | |
13684 | cp_lexer_consume_token (parser->lexer); | |
13685 | } | |
13686 | ||
13687 | return nreverse (types); | |
13688 | } | |
13689 | ||
13690 | /* Parse a try-block. | |
13691 | ||
13692 | try-block: | |
13693 | try compound-statement handler-seq */ | |
13694 | ||
13695 | static tree | |
94edc4ab | 13696 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
13697 | { |
13698 | tree try_block; | |
13699 | ||
13700 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
13701 | try_block = begin_try_block (); | |
325c3691 | 13702 | cp_parser_compound_statement (parser, NULL, true); |
a723baf1 MM |
13703 | finish_try_block (try_block); |
13704 | cp_parser_handler_seq (parser); | |
13705 | finish_handler_sequence (try_block); | |
13706 | ||
13707 | return try_block; | |
13708 | } | |
13709 | ||
13710 | /* Parse a function-try-block. | |
13711 | ||
13712 | function-try-block: | |
13713 | try ctor-initializer [opt] function-body handler-seq */ | |
13714 | ||
13715 | static bool | |
94edc4ab | 13716 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
13717 | { |
13718 | tree try_block; | |
13719 | bool ctor_initializer_p; | |
13720 | ||
13721 | /* Look for the `try' keyword. */ | |
13722 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
13723 | return false; | |
13724 | /* Let the rest of the front-end know where we are. */ | |
13725 | try_block = begin_function_try_block (); | |
13726 | /* Parse the function-body. */ | |
21526606 | 13727 | ctor_initializer_p |
a723baf1 MM |
13728 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
13729 | /* We're done with the `try' part. */ | |
13730 | finish_function_try_block (try_block); | |
13731 | /* Parse the handlers. */ | |
13732 | cp_parser_handler_seq (parser); | |
13733 | /* We're done with the handlers. */ | |
13734 | finish_function_handler_sequence (try_block); | |
13735 | ||
13736 | return ctor_initializer_p; | |
13737 | } | |
13738 | ||
13739 | /* Parse a handler-seq. | |
13740 | ||
13741 | handler-seq: | |
13742 | handler handler-seq [opt] */ | |
13743 | ||
13744 | static void | |
94edc4ab | 13745 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
13746 | { |
13747 | while (true) | |
13748 | { | |
13749 | cp_token *token; | |
13750 | ||
13751 | /* Parse the handler. */ | |
13752 | cp_parser_handler (parser); | |
13753 | /* Peek at the next token. */ | |
13754 | token = cp_lexer_peek_token (parser->lexer); | |
13755 | /* If it's not `catch' then there are no more handlers. */ | |
13756 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
13757 | break; | |
13758 | } | |
13759 | } | |
13760 | ||
13761 | /* Parse a handler. | |
13762 | ||
13763 | handler: | |
13764 | catch ( exception-declaration ) compound-statement */ | |
13765 | ||
13766 | static void | |
94edc4ab | 13767 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
13768 | { |
13769 | tree handler; | |
13770 | tree declaration; | |
13771 | ||
13772 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
13773 | handler = begin_handler (); | |
13774 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13775 | declaration = cp_parser_exception_declaration (parser); | |
13776 | finish_handler_parms (declaration, handler); | |
13777 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
325c3691 | 13778 | cp_parser_compound_statement (parser, NULL, false); |
a723baf1 MM |
13779 | finish_handler (handler); |
13780 | } | |
13781 | ||
13782 | /* Parse an exception-declaration. | |
13783 | ||
13784 | exception-declaration: | |
13785 | type-specifier-seq declarator | |
13786 | type-specifier-seq abstract-declarator | |
13787 | type-specifier-seq | |
21526606 | 13788 | ... |
a723baf1 MM |
13789 | |
13790 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
13791 | ellipsis variant is used. */ | |
13792 | ||
13793 | static tree | |
94edc4ab | 13794 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 | 13795 | { |
058b15c1 | 13796 | tree decl; |
62d1db17 | 13797 | cp_decl_specifier_seq type_specifiers; |
058b15c1 | 13798 | cp_declarator *declarator; |
a723baf1 MM |
13799 | const char *saved_message; |
13800 | ||
13801 | /* If it's an ellipsis, it's easy to handle. */ | |
13802 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
13803 | { | |
13804 | /* Consume the `...' token. */ | |
13805 | cp_lexer_consume_token (parser->lexer); | |
13806 | return NULL_TREE; | |
13807 | } | |
13808 | ||
13809 | /* Types may not be defined in exception-declarations. */ | |
13810 | saved_message = parser->type_definition_forbidden_message; | |
13811 | parser->type_definition_forbidden_message | |
13812 | = "types may not be defined in exception-declarations"; | |
13813 | ||
13814 | /* Parse the type-specifier-seq. */ | |
62d1db17 | 13815 | cp_parser_type_specifier_seq (parser, &type_specifiers); |
a723baf1 MM |
13816 | /* If it's a `)', then there is no declarator. */ |
13817 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
058b15c1 | 13818 | declarator = NULL; |
a723baf1 | 13819 | else |
62b8a44e | 13820 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 | 13821 | /*ctor_dtor_or_conv_p=*/NULL, |
db86dd14 MM |
13822 | /*parenthesized_p=*/NULL, |
13823 | /*member_p=*/false); | |
a723baf1 MM |
13824 | |
13825 | /* Restore the saved message. */ | |
13826 | parser->type_definition_forbidden_message = saved_message; | |
13827 | ||
62d1db17 | 13828 | if (type_specifiers.any_specifiers_p) |
058b15c1 | 13829 | { |
62d1db17 | 13830 | decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL); |
058b15c1 MM |
13831 | if (decl == NULL_TREE) |
13832 | error ("invalid catch parameter"); | |
13833 | } | |
13834 | else | |
13835 | decl = NULL_TREE; | |
13836 | ||
13837 | return decl; | |
a723baf1 MM |
13838 | } |
13839 | ||
21526606 | 13840 | /* Parse a throw-expression. |
a723baf1 MM |
13841 | |
13842 | throw-expression: | |
34cd5ae7 | 13843 | throw assignment-expression [opt] |
a723baf1 MM |
13844 | |
13845 | Returns a THROW_EXPR representing the throw-expression. */ | |
13846 | ||
13847 | static tree | |
94edc4ab | 13848 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
13849 | { |
13850 | tree expression; | |
89f1a6ec | 13851 | cp_token* token; |
a723baf1 MM |
13852 | |
13853 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
13854 | token = cp_lexer_peek_token (parser->lexer); |
13855 | /* Figure out whether or not there is an assignment-expression | |
13856 | following the "throw" keyword. */ | |
13857 | if (token->type == CPP_COMMA | |
13858 | || token->type == CPP_SEMICOLON | |
13859 | || token->type == CPP_CLOSE_PAREN | |
13860 | || token->type == CPP_CLOSE_SQUARE | |
13861 | || token->type == CPP_CLOSE_BRACE | |
13862 | || token->type == CPP_COLON) | |
a723baf1 | 13863 | expression = NULL_TREE; |
89f1a6ec | 13864 | else |
93678513 MM |
13865 | expression = cp_parser_assignment_expression (parser, |
13866 | /*cast_p=*/false); | |
a723baf1 MM |
13867 | |
13868 | return build_throw (expression); | |
13869 | } | |
13870 | ||
13871 | /* GNU Extensions */ | |
13872 | ||
13873 | /* Parse an (optional) asm-specification. | |
13874 | ||
13875 | asm-specification: | |
13876 | asm ( string-literal ) | |
13877 | ||
13878 | If the asm-specification is present, returns a STRING_CST | |
13879 | corresponding to the string-literal. Otherwise, returns | |
13880 | NULL_TREE. */ | |
13881 | ||
13882 | static tree | |
94edc4ab | 13883 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
13884 | { |
13885 | cp_token *token; | |
13886 | tree asm_specification; | |
13887 | ||
13888 | /* Peek at the next token. */ | |
13889 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 13890 | /* If the next token isn't the `asm' keyword, then there's no |
a723baf1 MM |
13891 | asm-specification. */ |
13892 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
13893 | return NULL_TREE; | |
13894 | ||
13895 | /* Consume the `asm' token. */ | |
13896 | cp_lexer_consume_token (parser->lexer); | |
13897 | /* Look for the `('. */ | |
13898 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13899 | ||
13900 | /* Look for the string-literal. */ | |
c162c75e | 13901 | asm_specification = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
13902 | |
13903 | /* Look for the `)'. */ | |
13904 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
13905 | ||
13906 | return asm_specification; | |
13907 | } | |
13908 | ||
21526606 | 13909 | /* Parse an asm-operand-list. |
a723baf1 MM |
13910 | |
13911 | asm-operand-list: | |
13912 | asm-operand | |
13913 | asm-operand-list , asm-operand | |
21526606 | 13914 | |
a723baf1 | 13915 | asm-operand: |
21526606 | 13916 | string-literal ( expression ) |
a723baf1 MM |
13917 | [ string-literal ] string-literal ( expression ) |
13918 | ||
13919 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
13920 | each node is the expression. The TREE_PURPOSE is itself a | |
13921 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
13922 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
13923 | is a STRING_CST for the string literal before the parenthesis. */ | |
13924 | ||
13925 | static tree | |
94edc4ab | 13926 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
13927 | { |
13928 | tree asm_operands = NULL_TREE; | |
13929 | ||
13930 | while (true) | |
13931 | { | |
13932 | tree string_literal; | |
13933 | tree expression; | |
13934 | tree name; | |
21526606 | 13935 | |
21526606 | 13936 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) |
a723baf1 MM |
13937 | { |
13938 | /* Consume the `[' token. */ | |
13939 | cp_lexer_consume_token (parser->lexer); | |
13940 | /* Read the operand name. */ | |
13941 | name = cp_parser_identifier (parser); | |
21526606 | 13942 | if (name != error_mark_node) |
a723baf1 MM |
13943 | name = build_string (IDENTIFIER_LENGTH (name), |
13944 | IDENTIFIER_POINTER (name)); | |
13945 | /* Look for the closing `]'. */ | |
13946 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
13947 | } | |
13948 | else | |
13949 | name = NULL_TREE; | |
13950 | /* Look for the string-literal. */ | |
c162c75e MA |
13951 | string_literal = cp_parser_string_literal (parser, false, false); |
13952 | ||
a723baf1 MM |
13953 | /* Look for the `('. */ |
13954 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13955 | /* Parse the expression. */ | |
93678513 | 13956 | expression = cp_parser_expression (parser, /*cast_p=*/false); |
a723baf1 MM |
13957 | /* Look for the `)'. */ |
13958 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
c162c75e | 13959 | |
a723baf1 MM |
13960 | /* Add this operand to the list. */ |
13961 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
21526606 | 13962 | expression, |
a723baf1 | 13963 | asm_operands); |
21526606 | 13964 | /* If the next token is not a `,', there are no more |
a723baf1 MM |
13965 | operands. */ |
13966 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13967 | break; | |
13968 | /* Consume the `,'. */ | |
13969 | cp_lexer_consume_token (parser->lexer); | |
13970 | } | |
13971 | ||
13972 | return nreverse (asm_operands); | |
13973 | } | |
13974 | ||
21526606 | 13975 | /* Parse an asm-clobber-list. |
a723baf1 MM |
13976 | |
13977 | asm-clobber-list: | |
13978 | string-literal | |
21526606 | 13979 | asm-clobber-list , string-literal |
a723baf1 MM |
13980 | |
13981 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
13982 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
13983 | ||
13984 | static tree | |
94edc4ab | 13985 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
13986 | { |
13987 | tree clobbers = NULL_TREE; | |
13988 | ||
13989 | while (true) | |
13990 | { | |
a723baf1 MM |
13991 | tree string_literal; |
13992 | ||
13993 | /* Look for the string literal. */ | |
c162c75e | 13994 | string_literal = cp_parser_string_literal (parser, false, false); |
a723baf1 MM |
13995 | /* Add it to the list. */ |
13996 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
21526606 | 13997 | /* If the next token is not a `,', then the list is |
a723baf1 MM |
13998 | complete. */ |
13999 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
14000 | break; | |
14001 | /* Consume the `,' token. */ | |
14002 | cp_lexer_consume_token (parser->lexer); | |
14003 | } | |
14004 | ||
14005 | return clobbers; | |
14006 | } | |
14007 | ||
14008 | /* Parse an (optional) series of attributes. | |
14009 | ||
14010 | attributes: | |
14011 | attributes attribute | |
14012 | ||
14013 | attribute: | |
21526606 | 14014 | __attribute__ (( attribute-list [opt] )) |
a723baf1 MM |
14015 | |
14016 | The return value is as for cp_parser_attribute_list. */ | |
21526606 | 14017 | |
a723baf1 | 14018 | static tree |
94edc4ab | 14019 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
14020 | { |
14021 | tree attributes = NULL_TREE; | |
14022 | ||
14023 | while (true) | |
14024 | { | |
14025 | cp_token *token; | |
14026 | tree attribute_list; | |
14027 | ||
14028 | /* Peek at the next token. */ | |
14029 | token = cp_lexer_peek_token (parser->lexer); | |
14030 | /* If it's not `__attribute__', then we're done. */ | |
14031 | if (token->keyword != RID_ATTRIBUTE) | |
14032 | break; | |
14033 | ||
14034 | /* Consume the `__attribute__' keyword. */ | |
14035 | cp_lexer_consume_token (parser->lexer); | |
14036 | /* Look for the two `(' tokens. */ | |
14037 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14038 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
14039 | ||
14040 | /* Peek at the next token. */ | |
14041 | token = cp_lexer_peek_token (parser->lexer); | |
14042 | if (token->type != CPP_CLOSE_PAREN) | |
14043 | /* Parse the attribute-list. */ | |
14044 | attribute_list = cp_parser_attribute_list (parser); | |
14045 | else | |
14046 | /* If the next token is a `)', then there is no attribute | |
14047 | list. */ | |
14048 | attribute_list = NULL; | |
14049 | ||
14050 | /* Look for the two `)' tokens. */ | |
14051 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14052 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14053 | ||
14054 | /* Add these new attributes to the list. */ | |
14055 | attributes = chainon (attributes, attribute_list); | |
14056 | } | |
14057 | ||
14058 | return attributes; | |
14059 | } | |
14060 | ||
21526606 | 14061 | /* Parse an attribute-list. |
a723baf1 | 14062 | |
21526606 EC |
14063 | attribute-list: |
14064 | attribute | |
a723baf1 MM |
14065 | attribute-list , attribute |
14066 | ||
14067 | attribute: | |
21526606 | 14068 | identifier |
a723baf1 MM |
14069 | identifier ( identifier ) |
14070 | identifier ( identifier , expression-list ) | |
21526606 | 14071 | identifier ( expression-list ) |
a723baf1 | 14072 | |
88e95ee3 MM |
14073 | Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds |
14074 | to an attribute. The TREE_PURPOSE of each node is the identifier | |
14075 | indicating which attribute is in use. The TREE_VALUE represents | |
14076 | the arguments, if any. */ | |
a723baf1 MM |
14077 | |
14078 | static tree | |
94edc4ab | 14079 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
14080 | { |
14081 | tree attribute_list = NULL_TREE; | |
c162c75e | 14082 | bool save_translate_strings_p = parser->translate_strings_p; |
a723baf1 | 14083 | |
c162c75e | 14084 | parser->translate_strings_p = false; |
a723baf1 MM |
14085 | while (true) |
14086 | { | |
14087 | cp_token *token; | |
14088 | tree identifier; | |
14089 | tree attribute; | |
14090 | ||
14091 | /* Look for the identifier. We also allow keywords here; for | |
14092 | example `__attribute__ ((const))' is legal. */ | |
14093 | token = cp_lexer_peek_token (parser->lexer); | |
88e95ee3 MM |
14094 | if (token->type == CPP_NAME |
14095 | || token->type == CPP_KEYWORD) | |
14096 | { | |
14097 | /* Consume the token. */ | |
14098 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 14099 | |
88e95ee3 MM |
14100 | /* Save away the identifier that indicates which attribute |
14101 | this is. */ | |
14102 | identifier = token->value; | |
14103 | attribute = build_tree_list (identifier, NULL_TREE); | |
a723baf1 | 14104 | |
88e95ee3 MM |
14105 | /* Peek at the next token. */ |
14106 | token = cp_lexer_peek_token (parser->lexer); | |
14107 | /* If it's an `(', then parse the attribute arguments. */ | |
14108 | if (token->type == CPP_OPEN_PAREN) | |
14109 | { | |
14110 | tree arguments; | |
a723baf1 | 14111 | |
88e95ee3 MM |
14112 | arguments = (cp_parser_parenthesized_expression_list |
14113 | (parser, true, /*cast_p=*/false, | |
14114 | /*non_constant_p=*/NULL)); | |
14115 | /* Save the identifier and arguments away. */ | |
14116 | TREE_VALUE (attribute) = arguments; | |
14117 | } | |
a723baf1 | 14118 | |
88e95ee3 MM |
14119 | /* Add this attribute to the list. */ |
14120 | TREE_CHAIN (attribute) = attribute_list; | |
14121 | attribute_list = attribute; | |
a723baf1 | 14122 | |
88e95ee3 MM |
14123 | token = cp_lexer_peek_token (parser->lexer); |
14124 | } | |
14125 | /* Now, look for more attributes. If the next token isn't a | |
14126 | `,', we're done. */ | |
a723baf1 MM |
14127 | if (token->type != CPP_COMMA) |
14128 | break; | |
14129 | ||
cd0be382 | 14130 | /* Consume the comma and keep going. */ |
a723baf1 MM |
14131 | cp_lexer_consume_token (parser->lexer); |
14132 | } | |
c162c75e | 14133 | parser->translate_strings_p = save_translate_strings_p; |
a723baf1 MM |
14134 | |
14135 | /* We built up the list in reverse order. */ | |
14136 | return nreverse (attribute_list); | |
14137 | } | |
14138 | ||
14139 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
14140 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
14141 | current value of the PEDANTIC flag, regardless of whether or not | |
14142 | the `__extension__' keyword is present. The caller is responsible | |
14143 | for restoring the value of the PEDANTIC flag. */ | |
14144 | ||
14145 | static bool | |
94edc4ab | 14146 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
14147 | { |
14148 | /* Save the old value of the PEDANTIC flag. */ | |
14149 | *saved_pedantic = pedantic; | |
14150 | ||
14151 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
14152 | { | |
14153 | /* Consume the `__extension__' token. */ | |
14154 | cp_lexer_consume_token (parser->lexer); | |
14155 | /* We're not being pedantic while the `__extension__' keyword is | |
14156 | in effect. */ | |
14157 | pedantic = 0; | |
14158 | ||
14159 | return true; | |
14160 | } | |
14161 | ||
14162 | return false; | |
14163 | } | |
14164 | ||
14165 | /* Parse a label declaration. | |
14166 | ||
14167 | label-declaration: | |
14168 | __label__ label-declarator-seq ; | |
14169 | ||
14170 | label-declarator-seq: | |
14171 | identifier , label-declarator-seq | |
14172 | identifier */ | |
14173 | ||
14174 | static void | |
94edc4ab | 14175 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
14176 | { |
14177 | /* Look for the `__label__' keyword. */ | |
14178 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
14179 | ||
14180 | while (true) | |
14181 | { | |
14182 | tree identifier; | |
14183 | ||
14184 | /* Look for an identifier. */ | |
14185 | identifier = cp_parser_identifier (parser); | |
14186 | /* Declare it as a lobel. */ | |
14187 | finish_label_decl (identifier); | |
14188 | /* If the next token is a `;', stop. */ | |
14189 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
14190 | break; | |
14191 | /* Look for the `,' separating the label declarations. */ | |
14192 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
14193 | } | |
14194 | ||
14195 | /* Look for the final `;'. */ | |
14196 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
14197 | } | |
14198 | ||
14199 | /* Support Functions */ | |
14200 | ||
14201 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
14202 | NAME should have one of the representations used for an | |
14203 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
14204 | is returned. If PARSER->SCOPE is a dependent type, then a | |
14205 | SCOPE_REF is returned. | |
14206 | ||
14207 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
14208 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
14209 | was formed. Abstractly, such entities should not be passed to this | |
14210 | function, because they do not need to be looked up, but it is | |
14211 | simpler to check for this special case here, rather than at the | |
14212 | call-sites. | |
14213 | ||
14214 | In cases not explicitly covered above, this function returns a | |
14215 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
14216 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
14217 | is returned. | |
14218 | ||
472c29c3 | 14219 | If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword |
fc6a28d7 MM |
14220 | (e.g., "struct") that was used. In that case bindings that do not |
14221 | refer to types are ignored. | |
a723baf1 | 14222 | |
b0bc6e8e KL |
14223 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
14224 | ignored. | |
14225 | ||
eea9800f MM |
14226 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
14227 | are ignored. | |
14228 | ||
a723baf1 | 14229 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
8f78f01f MM |
14230 | types. |
14231 | ||
14232 | If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup | |
14233 | results in an ambiguity, and false otherwise. */ | |
a723baf1 MM |
14234 | |
14235 | static tree | |
21526606 | 14236 | cp_parser_lookup_name (cp_parser *parser, tree name, |
fc6a28d7 MM |
14237 | enum tag_types tag_type, |
14238 | bool is_template, bool is_namespace, | |
8f78f01f MM |
14239 | bool check_dependency, |
14240 | bool *ambiguous_p) | |
a723baf1 MM |
14241 | { |
14242 | tree decl; | |
14243 | tree object_type = parser->context->object_type; | |
14244 | ||
8f78f01f MM |
14245 | /* Assume that the lookup will be unambiguous. */ |
14246 | if (ambiguous_p) | |
14247 | *ambiguous_p = false; | |
14248 | ||
a723baf1 MM |
14249 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is |
14250 | no longer valid. Note that if we are parsing tentatively, and | |
14251 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
14252 | parser->context->object_type = NULL_TREE; | |
14253 | ||
14254 | if (name == error_mark_node) | |
14255 | return error_mark_node; | |
14256 | ||
14257 | /* A template-id has already been resolved; there is no lookup to | |
14258 | do. */ | |
14259 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
14260 | return name; | |
14261 | if (BASELINK_P (name)) | |
14262 | { | |
50bc768d NS |
14263 | gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) |
14264 | == TEMPLATE_ID_EXPR); | |
a723baf1 MM |
14265 | return name; |
14266 | } | |
14267 | ||
14268 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
14269 | it should already have been checked to make sure that the name | |
14270 | used matches the type being destroyed. */ | |
14271 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
14272 | { | |
14273 | tree type; | |
14274 | ||
14275 | /* Figure out to which type this destructor applies. */ | |
14276 | if (parser->scope) | |
14277 | type = parser->scope; | |
14278 | else if (object_type) | |
14279 | type = object_type; | |
14280 | else | |
14281 | type = current_class_type; | |
14282 | /* If that's not a class type, there is no destructor. */ | |
14283 | if (!type || !CLASS_TYPE_P (type)) | |
14284 | return error_mark_node; | |
9f4faeae MM |
14285 | if (CLASSTYPE_LAZY_DESTRUCTOR (type)) |
14286 | lazily_declare_fn (sfk_destructor, type); | |
fd6e3cce GB |
14287 | if (!CLASSTYPE_DESTRUCTORS (type)) |
14288 | return error_mark_node; | |
a723baf1 MM |
14289 | /* If it was a class type, return the destructor. */ |
14290 | return CLASSTYPE_DESTRUCTORS (type); | |
14291 | } | |
14292 | ||
14293 | /* By this point, the NAME should be an ordinary identifier. If | |
14294 | the id-expression was a qualified name, the qualifying scope is | |
14295 | stored in PARSER->SCOPE at this point. */ | |
50bc768d | 14296 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
21526606 | 14297 | |
a723baf1 MM |
14298 | /* Perform the lookup. */ |
14299 | if (parser->scope) | |
21526606 | 14300 | { |
1fb3244a | 14301 | bool dependent_p; |
a723baf1 MM |
14302 | |
14303 | if (parser->scope == error_mark_node) | |
14304 | return error_mark_node; | |
14305 | ||
14306 | /* If the SCOPE is dependent, the lookup must be deferred until | |
14307 | the template is instantiated -- unless we are explicitly | |
14308 | looking up names in uninstantiated templates. Even then, we | |
14309 | cannot look up the name if the scope is not a class type; it | |
14310 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
14311 | dependent_p = (TYPE_P (parser->scope) |
14312 | && !(parser->in_declarator_p | |
14313 | && currently_open_class (parser->scope)) | |
14314 | && dependent_type_p (parser->scope)); | |
a723baf1 | 14315 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 14316 | && dependent_p) |
a723baf1 | 14317 | { |
fc6a28d7 MM |
14318 | if (tag_type) |
14319 | { | |
14320 | tree type; | |
14321 | ||
14322 | /* The resolution to Core Issue 180 says that `struct | |
14323 | A::B' should be considered a type-name, even if `A' | |
14324 | is dependent. */ | |
14325 | type = make_typename_type (parser->scope, name, tag_type, | |
14326 | /*complain=*/1); | |
fc6a28d7 MM |
14327 | decl = TYPE_NAME (type); |
14328 | } | |
b0bc6e8e | 14329 | else if (is_template) |
5b4acce1 | 14330 | decl = make_unbound_class_template (parser->scope, |
b939a023 | 14331 | name, NULL_TREE, |
5b4acce1 | 14332 | /*complain=*/1); |
b0bc6e8e KL |
14333 | else |
14334 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
14335 | } |
14336 | else | |
14337 | { | |
4514aa8c | 14338 | tree pushed_scope = NULL_TREE; |
91b004e5 | 14339 | |
a723baf1 MM |
14340 | /* If PARSER->SCOPE is a dependent type, then it must be a |
14341 | class type, and we must not be checking dependencies; | |
14342 | otherwise, we would have processed this lookup above. So | |
14343 | that PARSER->SCOPE is not considered a dependent base by | |
14344 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 14345 | if (dependent_p) |
4514aa8c | 14346 | pushed_scope = push_scope (parser->scope); |
78dcd41a | 14347 | /* If the PARSER->SCOPE is a template specialization, it |
a723baf1 MM |
14348 | may be instantiated during name lookup. In that case, |
14349 | errors may be issued. Even if we rollback the current | |
14350 | tentative parse, those errors are valid. */ | |
fc6a28d7 MM |
14351 | decl = lookup_qualified_name (parser->scope, name, |
14352 | tag_type != none_type, | |
5e08432e | 14353 | /*complain=*/true); |
4514aa8c NS |
14354 | if (pushed_scope) |
14355 | pop_scope (pushed_scope); | |
a723baf1 MM |
14356 | } |
14357 | parser->qualifying_scope = parser->scope; | |
14358 | parser->object_scope = NULL_TREE; | |
14359 | } | |
14360 | else if (object_type) | |
14361 | { | |
14362 | tree object_decl = NULL_TREE; | |
14363 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
14364 | OBJECT_TYPE is not a class. */ | |
14365 | if (CLASS_TYPE_P (object_type)) | |
14366 | /* If the OBJECT_TYPE is a template specialization, it may | |
14367 | be instantiated during name lookup. In that case, errors | |
14368 | may be issued. Even if we rollback the current tentative | |
14369 | parse, those errors are valid. */ | |
14370 | object_decl = lookup_member (object_type, | |
14371 | name, | |
fc6a28d7 MM |
14372 | /*protect=*/0, |
14373 | tag_type != none_type); | |
a723baf1 | 14374 | /* Look it up in the enclosing context, too. */ |
fc6a28d7 MM |
14375 | decl = lookup_name_real (name, tag_type != none_type, |
14376 | /*nonclass=*/0, | |
12cf89fa | 14377 | /*block_p=*/true, is_namespace, |
a723baf1 MM |
14378 | /*flags=*/0); |
14379 | parser->object_scope = object_type; | |
14380 | parser->qualifying_scope = NULL_TREE; | |
14381 | if (object_decl) | |
14382 | decl = object_decl; | |
14383 | } | |
14384 | else | |
14385 | { | |
fc6a28d7 MM |
14386 | decl = lookup_name_real (name, tag_type != none_type, |
14387 | /*nonclass=*/0, | |
12cf89fa | 14388 | /*block_p=*/true, is_namespace, |
a723baf1 MM |
14389 | /*flags=*/0); |
14390 | parser->qualifying_scope = NULL_TREE; | |
14391 | parser->object_scope = NULL_TREE; | |
14392 | } | |
14393 | ||
14394 | /* If the lookup failed, let our caller know. */ | |
bd3d082e | 14395 | if (!decl || decl == error_mark_node) |
a723baf1 MM |
14396 | return error_mark_node; |
14397 | ||
14398 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
14399 | if (TREE_CODE (decl) == TREE_LIST) | |
14400 | { | |
8f78f01f MM |
14401 | if (ambiguous_p) |
14402 | *ambiguous_p = true; | |
a723baf1 MM |
14403 | /* The error message we have to print is too complicated for |
14404 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 14405 | if (!cp_parser_simulate_error (parser)) |
a723baf1 | 14406 | { |
2a13a625 | 14407 | error ("reference to %qD is ambiguous", name); |
a723baf1 MM |
14408 | print_candidates (decl); |
14409 | } | |
14410 | return error_mark_node; | |
14411 | } | |
14412 | ||
50bc768d NS |
14413 | gcc_assert (DECL_P (decl) |
14414 | || TREE_CODE (decl) == OVERLOAD | |
14415 | || TREE_CODE (decl) == SCOPE_REF | |
14416 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE | |
14417 | || BASELINK_P (decl)); | |
a723baf1 MM |
14418 | |
14419 | /* If we have resolved the name of a member declaration, check to | |
14420 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 14421 | set of overloaded functions, accessibility is checked when |
21526606 | 14422 | overload resolution is done. |
a723baf1 MM |
14423 | |
14424 | During an explicit instantiation, access is not checked at all, | |
14425 | as per [temp.explicit]. */ | |
8d241e0b | 14426 | if (DECL_P (decl)) |
ee76b931 | 14427 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
14428 | |
14429 | return decl; | |
14430 | } | |
14431 | ||
14432 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
14433 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
14434 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
14435 | |
14436 | static tree | |
94edc4ab | 14437 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 | 14438 | { |
21526606 | 14439 | return cp_parser_lookup_name (parser, name, |
fc6a28d7 | 14440 | none_type, |
b0bc6e8e | 14441 | /*is_template=*/false, |
eea9800f | 14442 | /*is_namespace=*/false, |
8f78f01f MM |
14443 | /*check_dependency=*/true, |
14444 | /*ambiguous_p=*/NULL); | |
a723baf1 MM |
14445 | } |
14446 | ||
a723baf1 MM |
14447 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
14448 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
14449 | true, the DECL indicates the class being defined in a class-head, | |
14450 | or declared in an elaborated-type-specifier. | |
14451 | ||
14452 | Otherwise, return DECL. */ | |
14453 | ||
14454 | static tree | |
14455 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
14456 | { | |
710b73e6 KL |
14457 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
14458 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 | 14459 | |
21526606 | 14460 | struct A { |
a723baf1 MM |
14461 | template <typename T> struct B; |
14462 | }; | |
14463 | ||
21526606 EC |
14464 | template <typename T> struct A::B {}; |
14465 | ||
a723baf1 MM |
14466 | Similarly, in a elaborated-type-specifier: |
14467 | ||
14468 | namespace N { struct X{}; } | |
14469 | ||
14470 | struct A { | |
14471 | template <typename T> friend struct N::X; | |
14472 | }; | |
14473 | ||
710b73e6 KL |
14474 | However, if the DECL refers to a class type, and we are in |
14475 | the scope of the class, then the name lookup automatically | |
14476 | finds the TYPE_DECL created by build_self_reference rather | |
14477 | than a TEMPLATE_DECL. For example, in: | |
14478 | ||
14479 | template <class T> struct S { | |
14480 | S s; | |
14481 | }; | |
14482 | ||
14483 | there is no need to handle such case. */ | |
14484 | ||
14485 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
14486 | return DECL_TEMPLATE_RESULT (decl); |
14487 | ||
14488 | return decl; | |
14489 | } | |
14490 | ||
14491 | /* If too many, or too few, template-parameter lists apply to the | |
14492 | declarator, issue an error message. Returns TRUE if all went well, | |
14493 | and FALSE otherwise. */ | |
14494 | ||
14495 | static bool | |
21526606 | 14496 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
058b15c1 | 14497 | cp_declarator *declarator) |
a723baf1 MM |
14498 | { |
14499 | unsigned num_templates; | |
14500 | ||
14501 | /* We haven't seen any classes that involve template parameters yet. */ | |
14502 | num_templates = 0; | |
14503 | ||
058b15c1 | 14504 | switch (declarator->kind) |
a723baf1 | 14505 | { |
058b15c1 | 14506 | case cdk_id: |
1d786913 | 14507 | if (declarator->u.id.qualifying_scope) |
058b15c1 MM |
14508 | { |
14509 | tree scope; | |
14510 | tree member; | |
a723baf1 | 14511 | |
1d786913 MM |
14512 | scope = declarator->u.id.qualifying_scope; |
14513 | member = declarator->u.id.unqualified_name; | |
a723baf1 | 14514 | |
058b15c1 MM |
14515 | while (scope && CLASS_TYPE_P (scope)) |
14516 | { | |
14517 | /* You're supposed to have one `template <...>' | |
14518 | for every template class, but you don't need one | |
14519 | for a full specialization. For example: | |
14520 | ||
14521 | template <class T> struct S{}; | |
14522 | template <> struct S<int> { void f(); }; | |
14523 | void S<int>::f () {} | |
14524 | ||
14525 | is correct; there shouldn't be a `template <>' for | |
14526 | the definition of `S<int>::f'. */ | |
14527 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
14528 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
14529 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
14530 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
14531 | ++num_templates; | |
14532 | ||
14533 | scope = TYPE_CONTEXT (scope); | |
14534 | } | |
14535 | } | |
1d786913 MM |
14536 | else if (TREE_CODE (declarator->u.id.unqualified_name) |
14537 | == TEMPLATE_ID_EXPR) | |
14538 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
14539 | additional level of template parameters. */ | |
a723baf1 MM |
14540 | ++num_templates; |
14541 | ||
21526606 | 14542 | return cp_parser_check_template_parameters (parser, |
a723baf1 | 14543 | num_templates); |
058b15c1 MM |
14544 | |
14545 | case cdk_function: | |
14546 | case cdk_array: | |
14547 | case cdk_pointer: | |
14548 | case cdk_reference: | |
14549 | case cdk_ptrmem: | |
98ca843c | 14550 | return (cp_parser_check_declarator_template_parameters |
058b15c1 MM |
14551 | (parser, declarator->declarator)); |
14552 | ||
14553 | case cdk_error: | |
14554 | return true; | |
14555 | ||
14556 | default: | |
315fb5db | 14557 | gcc_unreachable (); |
a723baf1 | 14558 | } |
315fb5db | 14559 | return false; |
a723baf1 MM |
14560 | } |
14561 | ||
14562 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
14563 | invalid, return FALSE and issue an error messages. Otherwise, | |
14564 | return TRUE. */ | |
14565 | ||
14566 | static bool | |
94edc4ab NN |
14567 | cp_parser_check_template_parameters (cp_parser* parser, |
14568 | unsigned num_templates) | |
a723baf1 MM |
14569 | { |
14570 | /* If there are more template classes than parameter lists, we have | |
14571 | something like: | |
21526606 | 14572 | |
a723baf1 MM |
14573 | template <class T> void S<T>::R<T>::f (); */ |
14574 | if (parser->num_template_parameter_lists < num_templates) | |
14575 | { | |
14576 | error ("too few template-parameter-lists"); | |
14577 | return false; | |
14578 | } | |
14579 | /* If there are the same number of template classes and parameter | |
14580 | lists, that's OK. */ | |
14581 | if (parser->num_template_parameter_lists == num_templates) | |
14582 | return true; | |
14583 | /* If there are more, but only one more, then we are referring to a | |
14584 | member template. That's OK too. */ | |
14585 | if (parser->num_template_parameter_lists == num_templates + 1) | |
14586 | return true; | |
14587 | /* Otherwise, there are too many template parameter lists. We have | |
14588 | something like: | |
14589 | ||
14590 | template <class T> template <class U> void S::f(); */ | |
14591 | error ("too many template-parameter-lists"); | |
14592 | return false; | |
14593 | } | |
14594 | ||
a723baf1 MM |
14595 | /* Parse an optional `::' token indicating that the following name is |
14596 | from the global namespace. If so, PARSER->SCOPE is set to the | |
14597 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
14598 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
14599 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
14600 | present, and NULL_TREE otherwise. */ | |
14601 | ||
14602 | static tree | |
94edc4ab | 14603 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
14604 | { |
14605 | cp_token *token; | |
14606 | ||
14607 | /* Peek at the next token. */ | |
14608 | token = cp_lexer_peek_token (parser->lexer); | |
14609 | /* If we're looking at a `::' token then we're starting from the | |
14610 | global namespace, not our current location. */ | |
14611 | if (token->type == CPP_SCOPE) | |
14612 | { | |
14613 | /* Consume the `::' token. */ | |
14614 | cp_lexer_consume_token (parser->lexer); | |
14615 | /* Set the SCOPE so that we know where to start the lookup. */ | |
14616 | parser->scope = global_namespace; | |
14617 | parser->qualifying_scope = global_namespace; | |
14618 | parser->object_scope = NULL_TREE; | |
14619 | ||
14620 | return parser->scope; | |
14621 | } | |
14622 | else if (!current_scope_valid_p) | |
14623 | { | |
14624 | parser->scope = NULL_TREE; | |
14625 | parser->qualifying_scope = NULL_TREE; | |
14626 | parser->object_scope = NULL_TREE; | |
14627 | } | |
14628 | ||
14629 | return NULL_TREE; | |
14630 | } | |
14631 | ||
14632 | /* Returns TRUE if the upcoming token sequence is the start of a | |
14633 | constructor declarator. If FRIEND_P is true, the declarator is | |
14634 | preceded by the `friend' specifier. */ | |
14635 | ||
14636 | static bool | |
14637 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
14638 | { | |
14639 | bool constructor_p; | |
14640 | tree type_decl = NULL_TREE; | |
14641 | bool nested_name_p; | |
2050a1bb MM |
14642 | cp_token *next_token; |
14643 | ||
14644 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
14645 | try to avoid doing lots of work if at all possible. It's not |
14646 | valid declare a constructor at function scope. */ | |
14647 | if (at_function_scope_p ()) | |
14648 | return false; | |
14649 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
14650 | next_token = cp_lexer_peek_token (parser->lexer); |
14651 | if (next_token->type != CPP_NAME | |
14652 | && next_token->type != CPP_SCOPE | |
14653 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
14654 | && next_token->type != CPP_TEMPLATE_ID) | |
14655 | return false; | |
a723baf1 MM |
14656 | |
14657 | /* Parse tentatively; we are going to roll back all of the tokens | |
14658 | consumed here. */ | |
14659 | cp_parser_parse_tentatively (parser); | |
14660 | /* Assume that we are looking at a constructor declarator. */ | |
14661 | constructor_p = true; | |
8d241e0b | 14662 | |
a723baf1 MM |
14663 | /* Look for the optional `::' operator. */ |
14664 | cp_parser_global_scope_opt (parser, | |
14665 | /*current_scope_valid_p=*/false); | |
14666 | /* Look for the nested-name-specifier. */ | |
21526606 | 14667 | nested_name_p |
a723baf1 MM |
14668 | = (cp_parser_nested_name_specifier_opt (parser, |
14669 | /*typename_keyword_p=*/false, | |
14670 | /*check_dependency_p=*/false, | |
a668c6ad MM |
14671 | /*type_p=*/false, |
14672 | /*is_declaration=*/false) | |
a723baf1 MM |
14673 | != NULL_TREE); |
14674 | /* Outside of a class-specifier, there must be a | |
14675 | nested-name-specifier. */ | |
21526606 | 14676 | if (!nested_name_p && |
a723baf1 MM |
14677 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) |
14678 | || friend_p)) | |
14679 | constructor_p = false; | |
14680 | /* If we still think that this might be a constructor-declarator, | |
14681 | look for a class-name. */ | |
14682 | if (constructor_p) | |
14683 | { | |
14684 | /* If we have: | |
14685 | ||
8fbc5ae7 | 14686 | template <typename T> struct S { S(); }; |
a723baf1 MM |
14687 | template <typename T> S<T>::S (); |
14688 | ||
14689 | we must recognize that the nested `S' names a class. | |
14690 | Similarly, for: | |
14691 | ||
14692 | template <typename T> S<T>::S<T> (); | |
14693 | ||
14694 | we must recognize that the nested `S' names a template. */ | |
14695 | type_decl = cp_parser_class_name (parser, | |
14696 | /*typename_keyword_p=*/false, | |
14697 | /*template_keyword_p=*/false, | |
fc6a28d7 | 14698 | none_type, |
a723baf1 | 14699 | /*check_dependency_p=*/false, |
a668c6ad MM |
14700 | /*class_head_p=*/false, |
14701 | /*is_declaration=*/false); | |
a723baf1 MM |
14702 | /* If there was no class-name, then this is not a constructor. */ |
14703 | constructor_p = !cp_parser_error_occurred (parser); | |
14704 | } | |
8d241e0b | 14705 | |
a723baf1 MM |
14706 | /* If we're still considering a constructor, we have to see a `(', |
14707 | to begin the parameter-declaration-clause, followed by either a | |
14708 | `)', an `...', or a decl-specifier. We need to check for a | |
14709 | type-specifier to avoid being fooled into thinking that: | |
14710 | ||
14711 | S::S (f) (int); | |
14712 | ||
14713 | is a constructor. (It is actually a function named `f' that | |
14714 | takes one parameter (of type `int') and returns a value of type | |
14715 | `S::S'. */ | |
21526606 | 14716 | if (constructor_p |
a723baf1 MM |
14717 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
14718 | { | |
14719 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
14720 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
15077df5 MM |
14721 | /* A parameter declaration begins with a decl-specifier, |
14722 | which is either the "attribute" keyword, a storage class | |
14723 | specifier, or (usually) a type-specifier. */ | |
14724 | && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE) | |
a723baf1 MM |
14725 | && !cp_parser_storage_class_specifier_opt (parser)) |
14726 | { | |
5dae1114 | 14727 | tree type; |
4514aa8c | 14728 | tree pushed_scope = NULL_TREE; |
4047b164 | 14729 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
14730 | |
14731 | /* Names appearing in the type-specifier should be looked up | |
14732 | in the scope of the class. */ | |
14733 | if (current_class_type) | |
14734 | type = NULL_TREE; | |
a723baf1 MM |
14735 | else |
14736 | { | |
5dae1114 MM |
14737 | type = TREE_TYPE (type_decl); |
14738 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 | 14739 | { |
21526606 | 14740 | type = resolve_typename_type (type, |
14d22dd6 MM |
14741 | /*only_current_p=*/false); |
14742 | if (type == error_mark_node) | |
14743 | { | |
14744 | cp_parser_abort_tentative_parse (parser); | |
14745 | return false; | |
14746 | } | |
14747 | } | |
4514aa8c | 14748 | pushed_scope = push_scope (type); |
a723baf1 | 14749 | } |
4047b164 KL |
14750 | |
14751 | /* Inside the constructor parameter list, surrounding | |
14752 | template-parameter-lists do not apply. */ | |
14753 | saved_num_template_parameter_lists | |
14754 | = parser->num_template_parameter_lists; | |
14755 | parser->num_template_parameter_lists = 0; | |
14756 | ||
5dae1114 MM |
14757 | /* Look for the type-specifier. */ |
14758 | cp_parser_type_specifier (parser, | |
14759 | CP_PARSER_FLAGS_NONE, | |
62d1db17 | 14760 | /*decl_specs=*/NULL, |
5dae1114 MM |
14761 | /*is_declarator=*/true, |
14762 | /*declares_class_or_enum=*/NULL, | |
14763 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
14764 | |
14765 | parser->num_template_parameter_lists | |
14766 | = saved_num_template_parameter_lists; | |
14767 | ||
5dae1114 | 14768 | /* Leave the scope of the class. */ |
4514aa8c NS |
14769 | if (pushed_scope) |
14770 | pop_scope (pushed_scope); | |
5dae1114 MM |
14771 | |
14772 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
14773 | } |
14774 | } | |
14775 | else | |
14776 | constructor_p = false; | |
14777 | /* We did not really want to consume any tokens. */ | |
14778 | cp_parser_abort_tentative_parse (parser); | |
14779 | ||
14780 | return constructor_p; | |
14781 | } | |
14782 | ||
14783 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 14784 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
14785 | they must be performed once we are in the scope of the function. |
14786 | ||
14787 | Returns the function defined. */ | |
14788 | ||
14789 | static tree | |
14790 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 14791 | (cp_parser* parser, |
62d1db17 | 14792 | cp_decl_specifier_seq *decl_specifiers, |
94edc4ab | 14793 | tree attributes, |
058b15c1 | 14794 | const cp_declarator *declarator) |
a723baf1 MM |
14795 | { |
14796 | tree fn; | |
14797 | bool success_p; | |
14798 | ||
14799 | /* Begin the function-definition. */ | |
058b15c1 MM |
14800 | success_p = start_function (decl_specifiers, declarator, attributes); |
14801 | ||
14802 | /* The things we're about to see are not directly qualified by any | |
14803 | template headers we've seen thus far. */ | |
14804 | reset_specialization (); | |
a723baf1 MM |
14805 | |
14806 | /* If there were names looked up in the decl-specifier-seq that we | |
14807 | did not check, check them now. We must wait until we are in the | |
14808 | scope of the function to perform the checks, since the function | |
14809 | might be a friend. */ | |
cf22909c | 14810 | perform_deferred_access_checks (); |
a723baf1 MM |
14811 | |
14812 | if (!success_p) | |
14813 | { | |
058b15c1 | 14814 | /* Skip the entire function. */ |
a723baf1 MM |
14815 | error ("invalid function declaration"); |
14816 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14817 | fn = error_mark_node; | |
14818 | } | |
14819 | else | |
14820 | fn = cp_parser_function_definition_after_declarator (parser, | |
14821 | /*inline_p=*/false); | |
14822 | ||
14823 | return fn; | |
14824 | } | |
14825 | ||
14826 | /* Parse the part of a function-definition that follows the | |
14827 | declarator. INLINE_P is TRUE iff this function is an inline | |
14828 | function defined with a class-specifier. | |
14829 | ||
14830 | Returns the function defined. */ | |
14831 | ||
21526606 EC |
14832 | static tree |
14833 | cp_parser_function_definition_after_declarator (cp_parser* parser, | |
94edc4ab | 14834 | bool inline_p) |
a723baf1 MM |
14835 | { |
14836 | tree fn; | |
14837 | bool ctor_initializer_p = false; | |
14838 | bool saved_in_unbraced_linkage_specification_p; | |
14839 | unsigned saved_num_template_parameter_lists; | |
14840 | ||
14841 | /* If the next token is `return', then the code may be trying to | |
14842 | make use of the "named return value" extension that G++ used to | |
14843 | support. */ | |
14844 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
14845 | { | |
14846 | /* Consume the `return' keyword. */ | |
14847 | cp_lexer_consume_token (parser->lexer); | |
14848 | /* Look for the identifier that indicates what value is to be | |
14849 | returned. */ | |
14850 | cp_parser_identifier (parser); | |
14851 | /* Issue an error message. */ | |
14852 | error ("named return values are no longer supported"); | |
14853 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
14854 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
14855 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
14856 | cp_lexer_consume_token (parser->lexer); |
14857 | } | |
14858 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
14859 | anything declared inside `f'. */ | |
21526606 | 14860 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
14861 | = parser->in_unbraced_linkage_specification_p; |
14862 | parser->in_unbraced_linkage_specification_p = false; | |
14863 | /* Inside the function, surrounding template-parameter-lists do not | |
14864 | apply. */ | |
21526606 EC |
14865 | saved_num_template_parameter_lists |
14866 | = parser->num_template_parameter_lists; | |
a723baf1 MM |
14867 | parser->num_template_parameter_lists = 0; |
14868 | /* If the next token is `try', then we are looking at a | |
14869 | function-try-block. */ | |
14870 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
14871 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
14872 | /* A function-try-block includes the function-body, so we only do | |
14873 | this next part if we're not processing a function-try-block. */ | |
14874 | else | |
21526606 | 14875 | ctor_initializer_p |
a723baf1 MM |
14876 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
14877 | ||
14878 | /* Finish the function. */ | |
21526606 | 14879 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | |
a723baf1 MM |
14880 | (inline_p ? 2 : 0)); |
14881 | /* Generate code for it, if necessary. */ | |
8cd2462c | 14882 | expand_or_defer_fn (fn); |
a723baf1 | 14883 | /* Restore the saved values. */ |
21526606 | 14884 | parser->in_unbraced_linkage_specification_p |
a723baf1 | 14885 | = saved_in_unbraced_linkage_specification_p; |
21526606 | 14886 | parser->num_template_parameter_lists |
a723baf1 MM |
14887 | = saved_num_template_parameter_lists; |
14888 | ||
14889 | return fn; | |
14890 | } | |
14891 | ||
14892 | /* Parse a template-declaration, assuming that the `export' (and | |
14893 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
14894 | is as for cp_parser_template_declaration. */ | |
14895 | ||
14896 | static void | |
94edc4ab | 14897 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
14898 | { |
14899 | tree decl = NULL_TREE; | |
14900 | tree parameter_list; | |
14901 | bool friend_p = false; | |
14902 | ||
14903 | /* Look for the `template' keyword. */ | |
14904 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
14905 | return; | |
21526606 | 14906 | |
a723baf1 MM |
14907 | /* And the `<'. */ |
14908 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
14909 | return; | |
21526606 | 14910 | |
a723baf1 MM |
14911 | /* If the next token is `>', then we have an invalid |
14912 | specialization. Rather than complain about an invalid template | |
14913 | parameter, issue an error message here. */ | |
14914 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14915 | { | |
14916 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 14917 | begin_specialization (); |
a723baf1 MM |
14918 | parameter_list = NULL_TREE; |
14919 | } | |
14920 | else | |
2f9afd51 KL |
14921 | { |
14922 | /* Parse the template parameters. */ | |
14923 | begin_template_parm_list (); | |
14924 | parameter_list = cp_parser_template_parameter_list (parser); | |
14925 | parameter_list = end_template_parm_list (parameter_list); | |
14926 | } | |
14927 | ||
a723baf1 MM |
14928 | /* Look for the `>'. */ |
14929 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
14930 | /* We just processed one more parameter list. */ | |
14931 | ++parser->num_template_parameter_lists; | |
14932 | /* If the next token is `template', there are more template | |
14933 | parameters. */ | |
21526606 | 14934 | if (cp_lexer_next_token_is_keyword (parser->lexer, |
a723baf1 MM |
14935 | RID_TEMPLATE)) |
14936 | cp_parser_template_declaration_after_export (parser, member_p); | |
14937 | else | |
14938 | { | |
fe88415f NS |
14939 | /* There are no access checks when parsing a template, as we do not |
14940 | know if a specialization will be a friend. */ | |
14941 | push_deferring_access_checks (dk_no_check); | |
98ca843c | 14942 | |
a723baf1 MM |
14943 | decl = cp_parser_single_declaration (parser, |
14944 | member_p, | |
14945 | &friend_p); | |
14946 | ||
fe88415f | 14947 | pop_deferring_access_checks (); |
98ca843c | 14948 | |
a723baf1 MM |
14949 | /* If this is a member template declaration, let the front |
14950 | end know. */ | |
14951 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
14952 | { |
14953 | if (TREE_CODE (decl) == TYPE_DECL) | |
14954 | cp_parser_check_access_in_redeclaration (decl); | |
14955 | ||
14956 | decl = finish_member_template_decl (decl); | |
14957 | } | |
a723baf1 | 14958 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
14959 | make_friend_class (current_class_type, TREE_TYPE (decl), |
14960 | /*complain=*/true); | |
a723baf1 MM |
14961 | } |
14962 | /* We are done with the current parameter list. */ | |
14963 | --parser->num_template_parameter_lists; | |
14964 | ||
14965 | /* Finish up. */ | |
14966 | finish_template_decl (parameter_list); | |
14967 | ||
14968 | /* Register member declarations. */ | |
14969 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
14970 | finish_member_declaration (decl); | |
14971 | ||
14972 | /* If DECL is a function template, we must return to parse it later. | |
14973 | (Even though there is no definition, there might be default | |
14974 | arguments that need handling.) */ | |
21526606 | 14975 | if (member_p && decl |
a723baf1 MM |
14976 | && (TREE_CODE (decl) == FUNCTION_DECL |
14977 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
14978 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 14979 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
14980 | TREE_VALUE (parser->unparsed_functions_queues)); |
14981 | } | |
14982 | ||
14983 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
14984 | `function-definition' sequence. MEMBER_P is true, this declaration | |
14985 | appears in a class scope. | |
14986 | ||
14987 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
14988 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
14989 | ||
14990 | static tree | |
21526606 | 14991 | cp_parser_single_declaration (cp_parser* parser, |
94edc4ab NN |
14992 | bool member_p, |
14993 | bool* friend_p) | |
a723baf1 | 14994 | { |
560ad596 | 14995 | int declares_class_or_enum; |
a723baf1 | 14996 | tree decl = NULL_TREE; |
62d1db17 | 14997 | cp_decl_specifier_seq decl_specifiers; |
4bb8ca28 | 14998 | bool function_definition_p = false; |
a723baf1 | 14999 | |
71bd7186 MM |
15000 | /* This function is only used when processing a template |
15001 | declaration. */ | |
15002 | gcc_assert (innermost_scope_kind () == sk_template_parms | |
15003 | || innermost_scope_kind () == sk_template_spec); | |
15004 | ||
a723baf1 | 15005 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 15006 | push_deferring_access_checks (dk_deferred); |
cf22909c | 15007 | |
a723baf1 MM |
15008 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
15009 | alternative. */ | |
62d1db17 MM |
15010 | cp_parser_decl_specifier_seq (parser, |
15011 | CP_PARSER_FLAGS_OPTIONAL, | |
15012 | &decl_specifiers, | |
15013 | &declares_class_or_enum); | |
4bb8ca28 | 15014 | if (friend_p) |
62d1db17 | 15015 | *friend_p = cp_parser_friend_p (&decl_specifiers); |
71bd7186 MM |
15016 | |
15017 | /* There are no template typedefs. */ | |
15018 | if (decl_specifiers.specs[(int) ds_typedef]) | |
15019 | { | |
15020 | error ("template declaration of %qs", "typedef"); | |
15021 | decl = error_mark_node; | |
15022 | } | |
15023 | ||
a723baf1 MM |
15024 | /* Gather up the access checks that occurred the |
15025 | decl-specifier-seq. */ | |
cf22909c KL |
15026 | stop_deferring_access_checks (); |
15027 | ||
a723baf1 MM |
15028 | /* Check for the declaration of a template class. */ |
15029 | if (declares_class_or_enum) | |
15030 | { | |
15031 | if (cp_parser_declares_only_class_p (parser)) | |
15032 | { | |
62d1db17 | 15033 | decl = shadow_tag (&decl_specifiers); |
b939a023 KL |
15034 | |
15035 | /* In this case: | |
15036 | ||
15037 | struct C { | |
15038 | friend template <typename T> struct A<T>::B; | |
15039 | }; | |
15040 | ||
15041 | A<T>::B will be represented by a TYPENAME_TYPE, and | |
15042 | therefore not recognized by shadow_tag. */ | |
15043 | if (friend_p && *friend_p | |
15044 | && !decl | |
15045 | && decl_specifiers.type | |
15046 | && TYPE_P (decl_specifiers.type)) | |
15047 | decl = decl_specifiers.type; | |
15048 | ||
62d1db17 | 15049 | if (decl && decl != error_mark_node) |
a723baf1 MM |
15050 | decl = TYPE_NAME (decl); |
15051 | else | |
15052 | decl = error_mark_node; | |
15053 | } | |
15054 | } | |
a723baf1 MM |
15055 | /* If it's not a template class, try for a template function. If |
15056 | the next token is a `;', then this declaration does not declare | |
15057 | anything. But, if there were errors in the decl-specifiers, then | |
15058 | the error might well have come from an attempted class-specifier. | |
15059 | In that case, there's no need to warn about a missing declarator. */ | |
15060 | if (!decl | |
15061 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
62d1db17 | 15062 | || decl_specifiers.type != error_mark_node)) |
21526606 | 15063 | decl = cp_parser_init_declarator (parser, |
62d1db17 | 15064 | &decl_specifiers, |
4bb8ca28 | 15065 | /*function_definition_allowed_p=*/true, |
a723baf1 | 15066 | member_p, |
560ad596 | 15067 | declares_class_or_enum, |
4bb8ca28 | 15068 | &function_definition_p); |
cf22909c KL |
15069 | |
15070 | pop_deferring_access_checks (); | |
15071 | ||
a723baf1 MM |
15072 | /* Clear any current qualification; whatever comes next is the start |
15073 | of something new. */ | |
15074 | parser->scope = NULL_TREE; | |
15075 | parser->qualifying_scope = NULL_TREE; | |
15076 | parser->object_scope = NULL_TREE; | |
15077 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 | 15078 | if (!function_definition_p |
71bd7186 MM |
15079 | && (decl == error_mark_node |
15080 | || !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))) | |
a723baf1 | 15081 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
15082 | |
15083 | return decl; | |
15084 | } | |
15085 | ||
d6b4ea85 MM |
15086 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
15087 | ||
15088 | static tree | |
15089 | cp_parser_simple_cast_expression (cp_parser *parser) | |
15090 | { | |
93678513 MM |
15091 | return cp_parser_cast_expression (parser, /*address_p=*/false, |
15092 | /*cast_p=*/false); | |
d6b4ea85 MM |
15093 | } |
15094 | ||
a723baf1 MM |
15095 | /* Parse a functional cast to TYPE. Returns an expression |
15096 | representing the cast. */ | |
15097 | ||
15098 | static tree | |
94edc4ab | 15099 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
15100 | { |
15101 | tree expression_list; | |
d36d5600 | 15102 | tree cast; |
a723baf1 | 15103 | |
21526606 | 15104 | expression_list |
39703eb9 | 15105 | = cp_parser_parenthesized_expression_list (parser, false, |
93678513 | 15106 | /*cast_p=*/true, |
39703eb9 | 15107 | /*non_constant_p=*/NULL); |
a723baf1 | 15108 | |
d36d5600 GB |
15109 | cast = build_functional_cast (type, expression_list); |
15110 | /* [expr.const]/1: In an integral constant expression "only type | |
15111 | conversions to integral or enumeration type can be used". */ | |
98ca843c | 15112 | if (cast != error_mark_node && !type_dependent_expression_p (type) |
d36d5600 GB |
15113 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type))) |
15114 | { | |
98ca843c | 15115 | if (cp_parser_non_integral_constant_expression |
d36d5600 GB |
15116 | (parser, "a call to a constructor")) |
15117 | return error_mark_node; | |
15118 | } | |
15119 | return cast; | |
a723baf1 MM |
15120 | } |
15121 | ||
4bb8ca28 MM |
15122 | /* Save the tokens that make up the body of a member function defined |
15123 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
15124 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
15125 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
15126 | for the member function. */ | |
15127 | ||
7ce27103 | 15128 | static tree |
4bb8ca28 | 15129 | cp_parser_save_member_function_body (cp_parser* parser, |
62d1db17 | 15130 | cp_decl_specifier_seq *decl_specifiers, |
058b15c1 | 15131 | cp_declarator *declarator, |
4bb8ca28 MM |
15132 | tree attributes) |
15133 | { | |
c162c75e MA |
15134 | cp_token *first; |
15135 | cp_token *last; | |
4bb8ca28 MM |
15136 | tree fn; |
15137 | ||
15138 | /* Create the function-declaration. */ | |
15139 | fn = start_method (decl_specifiers, declarator, attributes); | |
15140 | /* If something went badly wrong, bail out now. */ | |
15141 | if (fn == error_mark_node) | |
15142 | { | |
15143 | /* If there's a function-body, skip it. */ | |
21526606 | 15144 | if (cp_parser_token_starts_function_definition_p |
4bb8ca28 MM |
15145 | (cp_lexer_peek_token (parser->lexer))) |
15146 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
15147 | return error_mark_node; | |
15148 | } | |
15149 | ||
15150 | /* Remember it, if there default args to post process. */ | |
15151 | cp_parser_save_default_args (parser, fn); | |
15152 | ||
21526606 | 15153 | /* Save away the tokens that make up the body of the |
4bb8ca28 | 15154 | function. */ |
c162c75e MA |
15155 | first = parser->lexer->next_token; |
15156 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); | |
4bb8ca28 MM |
15157 | /* Handle function try blocks. */ |
15158 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
c162c75e MA |
15159 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); |
15160 | last = parser->lexer->next_token; | |
4bb8ca28 MM |
15161 | |
15162 | /* Save away the inline definition; we will process it when the | |
15163 | class is complete. */ | |
c162c75e | 15164 | DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last); |
4bb8ca28 MM |
15165 | DECL_PENDING_INLINE_P (fn) = 1; |
15166 | ||
15167 | /* We need to know that this was defined in the class, so that | |
15168 | friend templates are handled correctly. */ | |
15169 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
15170 | ||
15171 | /* We're done with the inline definition. */ | |
15172 | finish_method (fn); | |
15173 | ||
15174 | /* Add FN to the queue of functions to be parsed later. */ | |
15175 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 15176 | = tree_cons (NULL_TREE, fn, |
4bb8ca28 MM |
15177 | TREE_VALUE (parser->unparsed_functions_queues)); |
15178 | ||
15179 | return fn; | |
15180 | } | |
15181 | ||
ec75414f MM |
15182 | /* Parse a template-argument-list, as well as the trailing ">" (but |
15183 | not the opening ">"). See cp_parser_template_argument_list for the | |
15184 | return value. */ | |
15185 | ||
15186 | static tree | |
15187 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
15188 | { | |
15189 | tree arguments; | |
15190 | tree saved_scope; | |
15191 | tree saved_qualifying_scope; | |
15192 | tree saved_object_scope; | |
15193 | bool saved_greater_than_is_operator_p; | |
15194 | ||
15195 | /* [temp.names] | |
15196 | ||
15197 | When parsing a template-id, the first non-nested `>' is taken as | |
15198 | the end of the template-argument-list rather than a greater-than | |
15199 | operator. */ | |
21526606 | 15200 | saved_greater_than_is_operator_p |
ec75414f MM |
15201 | = parser->greater_than_is_operator_p; |
15202 | parser->greater_than_is_operator_p = false; | |
15203 | /* Parsing the argument list may modify SCOPE, so we save it | |
15204 | here. */ | |
15205 | saved_scope = parser->scope; | |
15206 | saved_qualifying_scope = parser->qualifying_scope; | |
15207 | saved_object_scope = parser->object_scope; | |
15208 | /* Parse the template-argument-list itself. */ | |
15209 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
15210 | arguments = NULL_TREE; | |
15211 | else | |
15212 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
15213 | /* Look for the `>' that ends the template-argument-list. If we find |
15214 | a '>>' instead, it's probably just a typo. */ | |
15215 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
15216 | { | |
15217 | if (!saved_greater_than_is_operator_p) | |
15218 | { | |
2cfe82fe ZW |
15219 | /* If we're in a nested template argument list, the '>>' has |
15220 | to be a typo for '> >'. We emit the error message, but we | |
15221 | continue parsing and we push a '>' as next token, so that | |
15222 | the argument list will be parsed correctly. Note that the | |
15223 | global source location is still on the token before the | |
15224 | '>>', so we need to say explicitly where we want it. */ | |
15225 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
15226 | error ("%H%<>>%> should be %<> >%> " | |
15227 | "within a nested template argument list", | |
15228 | &token->location); | |
15229 | ||
15230 | /* ??? Proper recovery should terminate two levels of | |
15231 | template argument list here. */ | |
4d5297fa GB |
15232 | token->type = CPP_GREATER; |
15233 | } | |
15234 | else | |
15235 | { | |
2cfe82fe ZW |
15236 | /* If this is not a nested template argument list, the '>>' |
15237 | is a typo for '>'. Emit an error message and continue. | |
15238 | Same deal about the token location, but here we can get it | |
15239 | right by consuming the '>>' before issuing the diagnostic. */ | |
4d5297fa | 15240 | cp_lexer_consume_token (parser->lexer); |
2cfe82fe ZW |
15241 | error ("spurious %<>>%>, use %<>%> to terminate " |
15242 | "a template argument list"); | |
4d5297fa GB |
15243 | } |
15244 | } | |
2cfe82fe ZW |
15245 | else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) |
15246 | error ("missing %<>%> to terminate the template argument list"); | |
15247 | else | |
15248 | /* It's what we want, a '>'; consume it. */ | |
15249 | cp_lexer_consume_token (parser->lexer); | |
ec75414f | 15250 | /* The `>' token might be a greater-than operator again now. */ |
21526606 | 15251 | parser->greater_than_is_operator_p |
ec75414f MM |
15252 | = saved_greater_than_is_operator_p; |
15253 | /* Restore the SAVED_SCOPE. */ | |
15254 | parser->scope = saved_scope; | |
15255 | parser->qualifying_scope = saved_qualifying_scope; | |
15256 | parser->object_scope = saved_object_scope; | |
15257 | ||
15258 | return arguments; | |
15259 | } | |
15260 | ||
a723baf1 MM |
15261 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
15262 | arguments, or the body of the function have not yet been parsed, | |
15263 | parse them now. */ | |
15264 | ||
15265 | static void | |
94edc4ab | 15266 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 | 15267 | { |
a723baf1 MM |
15268 | /* If this member is a template, get the underlying |
15269 | FUNCTION_DECL. */ | |
15270 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
15271 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
15272 | ||
15273 | /* There should not be any class definitions in progress at this | |
15274 | point; the bodies of members are only parsed outside of all class | |
15275 | definitions. */ | |
50bc768d | 15276 | gcc_assert (parser->num_classes_being_defined == 0); |
a723baf1 MM |
15277 | /* While we're parsing the member functions we might encounter more |
15278 | classes. We want to handle them right away, but we don't want | |
15279 | them getting mixed up with functions that are currently in the | |
15280 | queue. */ | |
15281 | parser->unparsed_functions_queues | |
15282 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
15283 | ||
15284 | /* Make sure that any template parameters are in scope. */ | |
15285 | maybe_begin_member_template_processing (member_function); | |
15286 | ||
a723baf1 MM |
15287 | /* If the body of the function has not yet been parsed, parse it |
15288 | now. */ | |
15289 | if (DECL_PENDING_INLINE_P (member_function)) | |
15290 | { | |
15291 | tree function_scope; | |
15292 | cp_token_cache *tokens; | |
15293 | ||
15294 | /* The function is no longer pending; we are processing it. */ | |
15295 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
15296 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
15297 | DECL_PENDING_INLINE_P (member_function) = 0; | |
f769035f NS |
15298 | |
15299 | /* If this is a local class, enter the scope of the containing | |
15300 | function. */ | |
15301 | function_scope = current_function_decl; | |
a723baf1 MM |
15302 | if (function_scope) |
15303 | push_function_context_to (function_scope); | |
21526606 | 15304 | |
2cfe82fe ZW |
15305 | /* Push the body of the function onto the lexer stack. */ |
15306 | cp_parser_push_lexer_for_tokens (parser, tokens); | |
21526606 | 15307 | |
a723baf1 MM |
15308 | /* Let the front end know that we going to be defining this |
15309 | function. */ | |
058b15c1 MM |
15310 | start_preparsed_function (member_function, NULL_TREE, |
15311 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
21526606 | 15312 | |
a723baf1 MM |
15313 | /* Now, parse the body of the function. */ |
15314 | cp_parser_function_definition_after_declarator (parser, | |
15315 | /*inline_p=*/true); | |
21526606 | 15316 | |
a723baf1 MM |
15317 | /* Leave the scope of the containing function. */ |
15318 | if (function_scope) | |
15319 | pop_function_context_from (function_scope); | |
2cfe82fe | 15320 | cp_parser_pop_lexer (parser); |
a723baf1 MM |
15321 | } |
15322 | ||
15323 | /* Remove any template parameters from the symbol table. */ | |
15324 | maybe_end_member_template_processing (); | |
15325 | ||
15326 | /* Restore the queue. */ | |
21526606 | 15327 | parser->unparsed_functions_queues |
a723baf1 MM |
15328 | = TREE_CHAIN (parser->unparsed_functions_queues); |
15329 | } | |
15330 | ||
cd0be382 | 15331 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
15332 | functions queue. */ |
15333 | ||
15334 | static void | |
15335 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
15336 | { | |
15337 | tree probe; | |
15338 | ||
15339 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
15340 | probe; | |
15341 | probe = TREE_CHAIN (probe)) | |
15342 | if (TREE_PURPOSE (probe)) | |
15343 | { | |
15344 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
f44b0c8e | 15345 | = tree_cons (current_class_type, decl, |
8db1028e NS |
15346 | TREE_PURPOSE (parser->unparsed_functions_queues)); |
15347 | break; | |
15348 | } | |
15349 | return; | |
15350 | } | |
15351 | ||
8218bd34 | 15352 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
f44b0c8e MM |
15353 | unparsed DEFAULT_ARG. Parse the default args now. This function |
15354 | assumes that the current scope is the scope in which the default | |
15355 | argument should be processed. */ | |
a723baf1 MM |
15356 | |
15357 | static void | |
8218bd34 | 15358 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 | 15359 | { |
a723baf1 | 15360 | bool saved_local_variables_forbidden_p; |
2cfe82fe | 15361 | tree parm; |
8218bd34 | 15362 | |
b92bc2a0 NS |
15363 | /* While we're parsing the default args, we might (due to the |
15364 | statement expression extension) encounter more classes. We want | |
15365 | to handle them right away, but we don't want them getting mixed | |
15366 | up with default args that are currently in the queue. */ | |
15367 | parser->unparsed_functions_queues | |
15368 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
15369 | ||
2cfe82fe ZW |
15370 | /* Local variable names (and the `this' keyword) may not appear |
15371 | in a default argument. */ | |
15372 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
15373 | parser->local_variables_forbidden_p = true; | |
15374 | ||
15375 | for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
15376 | parm; | |
15377 | parm = TREE_CHAIN (parm)) | |
a723baf1 | 15378 | { |
2cfe82fe | 15379 | cp_token_cache *tokens; |
21526606 | 15380 | |
2cfe82fe ZW |
15381 | if (!TREE_PURPOSE (parm) |
15382 | || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG) | |
15383 | continue; | |
a723baf1 | 15384 | |
2cfe82fe ZW |
15385 | /* Push the saved tokens for the default argument onto the parser's |
15386 | lexer stack. */ | |
15387 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parm)); | |
15388 | cp_parser_push_lexer_for_tokens (parser, tokens); | |
a723baf1 | 15389 | |
2cfe82fe | 15390 | /* Parse the assignment-expression. */ |
93678513 MM |
15391 | TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser, |
15392 | /*cast_p=*/false); | |
a723baf1 | 15393 | |
676e33ca MM |
15394 | /* If the token stream has not been completely used up, then |
15395 | there was extra junk after the end of the default | |
15396 | argument. */ | |
15397 | if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2a13a625 | 15398 | cp_parser_error (parser, "expected %<,%>"); |
676e33ca | 15399 | |
2cfe82fe ZW |
15400 | /* Revert to the main lexer. */ |
15401 | cp_parser_pop_lexer (parser); | |
a723baf1 | 15402 | } |
b92bc2a0 | 15403 | |
2cfe82fe ZW |
15404 | /* Restore the state of local_variables_forbidden_p. */ |
15405 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
15406 | ||
b92bc2a0 | 15407 | /* Restore the queue. */ |
21526606 | 15408 | parser->unparsed_functions_queues |
b92bc2a0 | 15409 | = TREE_CHAIN (parser->unparsed_functions_queues); |
a723baf1 MM |
15410 | } |
15411 | ||
15412 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
15413 | either a TYPE or an expression, depending on the form of the | |
15414 | input. The KEYWORD indicates which kind of expression we have | |
15415 | encountered. */ | |
15416 | ||
15417 | static tree | |
94edc4ab | 15418 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
15419 | { |
15420 | static const char *format; | |
15421 | tree expr = NULL_TREE; | |
15422 | const char *saved_message; | |
67c03833 | 15423 | bool saved_integral_constant_expression_p; |
93678513 | 15424 | bool saved_non_integral_constant_expression_p; |
a723baf1 MM |
15425 | |
15426 | /* Initialize FORMAT the first time we get here. */ | |
15427 | if (!format) | |
9e637a26 | 15428 | format = "types may not be defined in '%s' expressions"; |
a723baf1 MM |
15429 | |
15430 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
15431 | old message. */ | |
15432 | saved_message = parser->type_definition_forbidden_message; | |
15433 | /* And create the new one. */ | |
21526606 EC |
15434 | parser->type_definition_forbidden_message |
15435 | = xmalloc (strlen (format) | |
c68b0a84 KG |
15436 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) |
15437 | + 1 /* `\0' */); | |
a723baf1 MM |
15438 | sprintf ((char *) parser->type_definition_forbidden_message, |
15439 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
15440 | ||
15441 | /* The restrictions on constant-expressions do not apply inside | |
15442 | sizeof expressions. */ | |
93678513 MM |
15443 | saved_integral_constant_expression_p |
15444 | = parser->integral_constant_expression_p; | |
15445 | saved_non_integral_constant_expression_p | |
15446 | = parser->non_integral_constant_expression_p; | |
67c03833 | 15447 | parser->integral_constant_expression_p = false; |
a723baf1 | 15448 | |
3beb3abf MM |
15449 | /* Do not actually evaluate the expression. */ |
15450 | ++skip_evaluation; | |
a723baf1 MM |
15451 | /* If it's a `(', then we might be looking at the type-id |
15452 | construction. */ | |
15453 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
15454 | { | |
15455 | tree type; | |
4f8163b1 | 15456 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
15457 | |
15458 | /* We can't be sure yet whether we're looking at a type-id or an | |
15459 | expression. */ | |
15460 | cp_parser_parse_tentatively (parser); | |
15461 | /* Consume the `('. */ | |
15462 | cp_lexer_consume_token (parser->lexer); | |
15463 | /* Parse the type-id. */ | |
4f8163b1 MM |
15464 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
15465 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 15466 | type = cp_parser_type_id (parser); |
4f8163b1 | 15467 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 | 15468 | /* Now, look for the trailing `)'. */ |
9e637a26 | 15469 | cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"); |
a723baf1 MM |
15470 | /* If all went well, then we're done. */ |
15471 | if (cp_parser_parse_definitely (parser)) | |
15472 | { | |
62d1db17 MM |
15473 | cp_decl_specifier_seq decl_specs; |
15474 | ||
15475 | /* Build a trivial decl-specifier-seq. */ | |
15476 | clear_decl_specs (&decl_specs); | |
15477 | decl_specs.type = type; | |
a723baf1 MM |
15478 | |
15479 | /* Call grokdeclarator to figure out what type this is. */ | |
058b15c1 | 15480 | expr = grokdeclarator (NULL, |
62d1db17 | 15481 | &decl_specs, |
a723baf1 MM |
15482 | TYPENAME, |
15483 | /*initialized=*/0, | |
15484 | /*attrlist=*/NULL); | |
15485 | } | |
15486 | } | |
15487 | ||
15488 | /* If the type-id production did not work out, then we must be | |
15489 | looking at the unary-expression production. */ | |
15490 | if (!expr) | |
93678513 MM |
15491 | expr = cp_parser_unary_expression (parser, /*address_p=*/false, |
15492 | /*cast_p=*/false); | |
3beb3abf MM |
15493 | /* Go back to evaluating expressions. */ |
15494 | --skip_evaluation; | |
a723baf1 MM |
15495 | |
15496 | /* Free the message we created. */ | |
15497 | free ((char *) parser->type_definition_forbidden_message); | |
15498 | /* And restore the old one. */ | |
15499 | parser->type_definition_forbidden_message = saved_message; | |
93678513 MM |
15500 | parser->integral_constant_expression_p |
15501 | = saved_integral_constant_expression_p; | |
15502 | parser->non_integral_constant_expression_p | |
15503 | = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
15504 | |
15505 | return expr; | |
15506 | } | |
15507 | ||
15508 | /* If the current declaration has no declarator, return true. */ | |
15509 | ||
15510 | static bool | |
15511 | cp_parser_declares_only_class_p (cp_parser *parser) | |
15512 | { | |
21526606 | 15513 | /* If the next token is a `;' or a `,' then there is no |
a723baf1 MM |
15514 | declarator. */ |
15515 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
15516 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
15517 | } | |
15518 | ||
62d1db17 | 15519 | /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */ |
a723baf1 | 15520 | |
62d1db17 MM |
15521 | static void |
15522 | cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs, | |
15523 | cp_storage_class storage_class) | |
a723baf1 | 15524 | { |
62d1db17 MM |
15525 | if (decl_specs->storage_class != sc_none) |
15526 | decl_specs->multiple_storage_classes_p = true; | |
15527 | else | |
15528 | decl_specs->storage_class = storage_class; | |
15529 | } | |
15530 | ||
15531 | /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P | |
15532 | is true, the type is a user-defined type; otherwise it is a | |
15533 | built-in type specified by a keyword. */ | |
a723baf1 | 15534 | |
62d1db17 MM |
15535 | static void |
15536 | cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs, | |
15537 | tree type_spec, | |
15538 | bool user_defined_p) | |
15539 | { | |
15540 | decl_specs->any_specifiers_p = true; | |
98ca843c | 15541 | |
9306cccb MM |
15542 | /* If the user tries to redeclare bool or wchar_t (with, for |
15543 | example, in "typedef int wchar_t;") we remember that this is what | |
f84b6c96 MM |
15544 | happened. In system headers, we ignore these declarations so |
15545 | that G++ can work with system headers that are not C++-safe. */ | |
98ca843c | 15546 | if (decl_specs->specs[(int) ds_typedef] |
f84b6c96 | 15547 | && !user_defined_p |
9306cccb MM |
15548 | && (type_spec == boolean_type_node |
15549 | || type_spec == wchar_type_node) | |
f84b6c96 MM |
15550 | && (decl_specs->type |
15551 | || decl_specs->specs[(int) ds_long] | |
15552 | || decl_specs->specs[(int) ds_short] | |
15553 | || decl_specs->specs[(int) ds_unsigned] | |
15554 | || decl_specs->specs[(int) ds_signed])) | |
0a73e37f MM |
15555 | { |
15556 | decl_specs->redefined_builtin_type = type_spec; | |
15557 | if (!decl_specs->type) | |
15558 | { | |
15559 | decl_specs->type = type_spec; | |
15560 | decl_specs->user_defined_type_p = false; | |
15561 | } | |
15562 | } | |
f84b6c96 MM |
15563 | else if (decl_specs->type) |
15564 | decl_specs->multiple_types_p = true; | |
62d1db17 MM |
15565 | else |
15566 | { | |
15567 | decl_specs->type = type_spec; | |
15568 | decl_specs->user_defined_type_p = user_defined_p; | |
0a73e37f | 15569 | decl_specs->redefined_builtin_type = NULL_TREE; |
a723baf1 | 15570 | } |
62d1db17 | 15571 | } |
a723baf1 | 15572 | |
62d1db17 MM |
15573 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. |
15574 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
15575 | ||
15576 | static bool | |
15577 | cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers) | |
15578 | { | |
15579 | return decl_specifiers->specs[(int) ds_friend] != 0; | |
a723baf1 MM |
15580 | } |
15581 | ||
15582 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
15583 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15584 | |
a723baf1 MM |
15585 | Returns the token consumed, if the token had the appropriate type. |
15586 | Otherwise, returns NULL. */ | |
15587 | ||
15588 | static cp_token * | |
94edc4ab NN |
15589 | cp_parser_require (cp_parser* parser, |
15590 | enum cpp_ttype type, | |
15591 | const char* token_desc) | |
a723baf1 MM |
15592 | { |
15593 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
15594 | return cp_lexer_consume_token (parser->lexer); | |
15595 | else | |
15596 | { | |
e5976695 MM |
15597 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
15598 | if (!cp_parser_simulate_error (parser)) | |
216bb6e1 MM |
15599 | { |
15600 | char *message = concat ("expected ", token_desc, NULL); | |
15601 | cp_parser_error (parser, message); | |
15602 | free (message); | |
15603 | } | |
a723baf1 MM |
15604 | return NULL; |
15605 | } | |
15606 | } | |
15607 | ||
15608 | /* Like cp_parser_require, except that tokens will be skipped until | |
15609 | the desired token is found. An error message is still produced if | |
15610 | the next token is not as expected. */ | |
15611 | ||
15612 | static void | |
21526606 EC |
15613 | cp_parser_skip_until_found (cp_parser* parser, |
15614 | enum cpp_ttype type, | |
94edc4ab | 15615 | const char* token_desc) |
a723baf1 MM |
15616 | { |
15617 | cp_token *token; | |
15618 | unsigned nesting_depth = 0; | |
15619 | ||
15620 | if (cp_parser_require (parser, type, token_desc)) | |
15621 | return; | |
15622 | ||
15623 | /* Skip tokens until the desired token is found. */ | |
15624 | while (true) | |
15625 | { | |
15626 | /* Peek at the next token. */ | |
15627 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 15628 | /* If we've reached the token we want, consume it and |
a723baf1 MM |
15629 | stop. */ |
15630 | if (token->type == type && !nesting_depth) | |
15631 | { | |
15632 | cp_lexer_consume_token (parser->lexer); | |
15633 | return; | |
15634 | } | |
15635 | /* If we've run out of tokens, stop. */ | |
15636 | if (token->type == CPP_EOF) | |
15637 | return; | |
21526606 | 15638 | if (token->type == CPP_OPEN_BRACE |
a723baf1 MM |
15639 | || token->type == CPP_OPEN_PAREN |
15640 | || token->type == CPP_OPEN_SQUARE) | |
15641 | ++nesting_depth; | |
21526606 | 15642 | else if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
15643 | || token->type == CPP_CLOSE_PAREN |
15644 | || token->type == CPP_CLOSE_SQUARE) | |
15645 | { | |
15646 | if (nesting_depth-- == 0) | |
15647 | return; | |
15648 | } | |
15649 | /* Consume this token. */ | |
15650 | cp_lexer_consume_token (parser->lexer); | |
15651 | } | |
15652 | } | |
15653 | ||
15654 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
15655 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15656 | |
a723baf1 MM |
15657 | Returns the token consumed, if the token had the appropriate type. |
15658 | Otherwise, returns NULL. */ | |
15659 | ||
15660 | static cp_token * | |
94edc4ab NN |
15661 | cp_parser_require_keyword (cp_parser* parser, |
15662 | enum rid keyword, | |
15663 | const char* token_desc) | |
a723baf1 MM |
15664 | { |
15665 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
15666 | ||
15667 | if (token && token->keyword != keyword) | |
15668 | { | |
15669 | dyn_string_t error_msg; | |
15670 | ||
15671 | /* Format the error message. */ | |
15672 | error_msg = dyn_string_new (0); | |
15673 | dyn_string_append_cstr (error_msg, "expected "); | |
15674 | dyn_string_append_cstr (error_msg, token_desc); | |
15675 | cp_parser_error (parser, error_msg->s); | |
15676 | dyn_string_delete (error_msg); | |
15677 | return NULL; | |
15678 | } | |
15679 | ||
15680 | return token; | |
15681 | } | |
15682 | ||
15683 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
15684 | function-definition. */ | |
15685 | ||
21526606 | 15686 | static bool |
94edc4ab | 15687 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
15688 | { |
15689 | return (/* An ordinary function-body begins with an `{'. */ | |
15690 | token->type == CPP_OPEN_BRACE | |
15691 | /* A ctor-initializer begins with a `:'. */ | |
15692 | || token->type == CPP_COLON | |
15693 | /* A function-try-block begins with `try'. */ | |
15694 | || token->keyword == RID_TRY | |
15695 | /* The named return value extension begins with `return'. */ | |
15696 | || token->keyword == RID_RETURN); | |
15697 | } | |
15698 | ||
15699 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
15700 | definition. */ | |
15701 | ||
15702 | static bool | |
15703 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
15704 | { | |
15705 | cp_token *token; | |
15706 | ||
15707 | token = cp_lexer_peek_token (parser->lexer); | |
15708 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
15709 | } | |
15710 | ||
d17811fd | 15711 | /* Returns TRUE iff the next token is the "," or ">" ending a |
03fd3f84 | 15712 | template-argument. */ |
d17811fd MM |
15713 | |
15714 | static bool | |
15715 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
15716 | { | |
15717 | cp_token *token; | |
15718 | ||
15719 | token = cp_lexer_peek_token (parser->lexer); | |
391c4bc5 | 15720 | return (token->type == CPP_COMMA || token->type == CPP_GREATER); |
d17811fd | 15721 | } |
f4abade9 GB |
15722 | |
15723 | /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the | |
15724 | (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ | |
15725 | ||
15726 | static bool | |
21526606 | 15727 | cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, |
f4abade9 GB |
15728 | size_t n) |
15729 | { | |
15730 | cp_token *token; | |
15731 | ||
15732 | token = cp_lexer_peek_nth_token (parser->lexer, n); | |
15733 | if (token->type == CPP_LESS) | |
15734 | return true; | |
15735 | /* Check for the sequence `<::' in the original code. It would be lexed as | |
15736 | `[:', where `[' is a digraph, and there is no whitespace before | |
15737 | `:'. */ | |
15738 | if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) | |
15739 | { | |
15740 | cp_token *token2; | |
15741 | token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); | |
15742 | if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) | |
15743 | return true; | |
15744 | } | |
15745 | return false; | |
15746 | } | |
21526606 | 15747 | |
a723baf1 MM |
15748 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
15749 | or none_type otherwise. */ | |
15750 | ||
15751 | static enum tag_types | |
94edc4ab | 15752 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
15753 | { |
15754 | switch (token->keyword) | |
15755 | { | |
15756 | case RID_CLASS: | |
15757 | return class_type; | |
15758 | case RID_STRUCT: | |
15759 | return record_type; | |
15760 | case RID_UNION: | |
15761 | return union_type; | |
21526606 | 15762 | |
a723baf1 MM |
15763 | default: |
15764 | return none_type; | |
15765 | } | |
15766 | } | |
15767 | ||
15768 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
15769 | ||
15770 | static void | |
15771 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
15772 | { | |
15773 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
2a13a625 | 15774 | pedwarn ("%qs tag used in naming %q#T", |
a723baf1 | 15775 | class_key == union_type ? "union" |
21526606 | 15776 | : class_key == record_type ? "struct" : "class", |
a723baf1 MM |
15777 | type); |
15778 | } | |
21526606 | 15779 | |
cd0be382 | 15780 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
15781 | access than its original declaration [class.access.spec/3]. |
15782 | This applies to nested classes and nested class templates. | |
15783 | [class.mem/1]. */ | |
15784 | ||
2a13a625 GDR |
15785 | static void |
15786 | cp_parser_check_access_in_redeclaration (tree decl) | |
37d407a1 KL |
15787 | { |
15788 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
15789 | return; | |
15790 | ||
15791 | if ((TREE_PRIVATE (decl) | |
15792 | != (current_access_specifier == access_private_node)) | |
15793 | || (TREE_PROTECTED (decl) | |
15794 | != (current_access_specifier == access_protected_node))) | |
2a13a625 | 15795 | error ("%qD redeclared with different access", decl); |
37d407a1 KL |
15796 | } |
15797 | ||
a723baf1 | 15798 | /* Look for the `template' keyword, as a syntactic disambiguator. |
21526606 | 15799 | Return TRUE iff it is present, in which case it will be |
a723baf1 MM |
15800 | consumed. */ |
15801 | ||
15802 | static bool | |
15803 | cp_parser_optional_template_keyword (cp_parser *parser) | |
15804 | { | |
15805 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
15806 | { | |
15807 | /* The `template' keyword can only be used within templates; | |
15808 | outside templates the parser can always figure out what is a | |
15809 | template and what is not. */ | |
15810 | if (!processing_template_decl) | |
15811 | { | |
2a13a625 | 15812 | error ("%<template%> (as a disambiguator) is only allowed " |
a723baf1 MM |
15813 | "within templates"); |
15814 | /* If this part of the token stream is rescanned, the same | |
15815 | error message would be generated. So, we purge the token | |
15816 | from the stream. */ | |
15817 | cp_lexer_purge_token (parser->lexer); | |
15818 | return false; | |
15819 | } | |
15820 | else | |
15821 | { | |
15822 | /* Consume the `template' keyword. */ | |
15823 | cp_lexer_consume_token (parser->lexer); | |
15824 | return true; | |
15825 | } | |
15826 | } | |
15827 | ||
15828 | return false; | |
15829 | } | |
15830 | ||
2050a1bb MM |
15831 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
15832 | set PARSER->SCOPE, and perform other related actions. */ | |
15833 | ||
15834 | static void | |
15835 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
15836 | { | |
15837 | tree value; | |
15838 | tree check; | |
15839 | ||
15840 | /* Get the stored value. */ | |
15841 | value = cp_lexer_consume_token (parser->lexer)->value; | |
15842 | /* Perform any access checks that were deferred. */ | |
15843 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 15844 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
15845 | /* Set the scope from the stored value. */ |
15846 | parser->scope = TREE_VALUE (value); | |
15847 | parser->qualifying_scope = TREE_TYPE (value); | |
15848 | parser->object_scope = NULL_TREE; | |
15849 | } | |
15850 | ||
03fd3f84 | 15851 | /* Consume tokens up through a non-nested END token. */ |
a723baf1 MM |
15852 | |
15853 | static void | |
c162c75e MA |
15854 | cp_parser_cache_group (cp_parser *parser, |
15855 | enum cpp_ttype end, | |
15856 | unsigned depth) | |
a723baf1 MM |
15857 | { |
15858 | while (true) | |
15859 | { | |
15860 | cp_token *token; | |
15861 | ||
15862 | /* Abort a parenthesized expression if we encounter a brace. */ | |
15863 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
15864 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
15865 | return; | |
a723baf1 | 15866 | /* If we've reached the end of the file, stop. */ |
4bfb8bba | 15867 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) |
a723baf1 | 15868 | return; |
4bfb8bba MM |
15869 | /* Consume the next token. */ |
15870 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
15871 | /* See if it starts a new group. */ |
15872 | if (token->type == CPP_OPEN_BRACE) | |
15873 | { | |
c162c75e | 15874 | cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1); |
a723baf1 MM |
15875 | if (depth == 0) |
15876 | return; | |
15877 | } | |
15878 | else if (token->type == CPP_OPEN_PAREN) | |
c162c75e | 15879 | cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); |
a723baf1 MM |
15880 | else if (token->type == end) |
15881 | return; | |
15882 | } | |
15883 | } | |
15884 | ||
15885 | /* Begin parsing tentatively. We always save tokens while parsing | |
15886 | tentatively so that if the tentative parsing fails we can restore the | |
15887 | tokens. */ | |
15888 | ||
15889 | static void | |
94edc4ab | 15890 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
15891 | { |
15892 | /* Enter a new parsing context. */ | |
15893 | parser->context = cp_parser_context_new (parser->context); | |
15894 | /* Begin saving tokens. */ | |
15895 | cp_lexer_save_tokens (parser->lexer); | |
15896 | /* In order to avoid repetitive access control error messages, | |
15897 | access checks are queued up until we are no longer parsing | |
15898 | tentatively. */ | |
8d241e0b | 15899 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
15900 | } |
15901 | ||
15902 | /* Commit to the currently active tentative parse. */ | |
15903 | ||
15904 | static void | |
94edc4ab | 15905 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15906 | { |
15907 | cp_parser_context *context; | |
15908 | cp_lexer *lexer; | |
15909 | ||
15910 | /* Mark all of the levels as committed. */ | |
15911 | lexer = parser->lexer; | |
15912 | for (context = parser->context; context->next; context = context->next) | |
15913 | { | |
15914 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
15915 | break; | |
15916 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
15917 | while (!cp_lexer_saving_tokens (lexer)) | |
15918 | lexer = lexer->next; | |
15919 | cp_lexer_commit_tokens (lexer); | |
15920 | } | |
15921 | } | |
15922 | ||
15923 | /* Abort the currently active tentative parse. All consumed tokens | |
15924 | will be rolled back, and no diagnostics will be issued. */ | |
15925 | ||
15926 | static void | |
94edc4ab | 15927 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15928 | { |
15929 | cp_parser_simulate_error (parser); | |
15930 | /* Now, pretend that we want to see if the construct was | |
15931 | successfully parsed. */ | |
15932 | cp_parser_parse_definitely (parser); | |
15933 | } | |
15934 | ||
34cd5ae7 | 15935 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
15936 | token stream. Otherwise, commit to the tokens we have consumed. |
15937 | Returns true if no error occurred; false otherwise. */ | |
15938 | ||
15939 | static bool | |
94edc4ab | 15940 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
15941 | { |
15942 | bool error_occurred; | |
15943 | cp_parser_context *context; | |
15944 | ||
34cd5ae7 | 15945 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
15946 | destroy that information. */ |
15947 | error_occurred = cp_parser_error_occurred (parser); | |
15948 | /* Remove the topmost context from the stack. */ | |
15949 | context = parser->context; | |
15950 | parser->context = context->next; | |
15951 | /* If no parse errors occurred, commit to the tentative parse. */ | |
15952 | if (!error_occurred) | |
15953 | { | |
15954 | /* Commit to the tokens read tentatively, unless that was | |
15955 | already done. */ | |
15956 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
15957 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
15958 | |
15959 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
15960 | } |
15961 | /* Otherwise, if errors occurred, roll back our state so that things | |
15962 | are just as they were before we began the tentative parse. */ | |
15963 | else | |
cf22909c KL |
15964 | { |
15965 | cp_lexer_rollback_tokens (parser->lexer); | |
15966 | pop_deferring_access_checks (); | |
15967 | } | |
e5976695 MM |
15968 | /* Add the context to the front of the free list. */ |
15969 | context->next = cp_parser_context_free_list; | |
15970 | cp_parser_context_free_list = context; | |
15971 | ||
15972 | return !error_occurred; | |
a723baf1 MM |
15973 | } |
15974 | ||
0b16f8f4 VR |
15975 | /* Returns true if we are parsing tentatively and are not committed to |
15976 | this tentative parse. */ | |
a723baf1 MM |
15977 | |
15978 | static bool | |
0b16f8f4 | 15979 | cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser) |
a723baf1 MM |
15980 | { |
15981 | return (cp_parser_parsing_tentatively (parser) | |
0b16f8f4 | 15982 | && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED); |
a723baf1 MM |
15983 | } |
15984 | ||
4de8668e | 15985 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 | 15986 | tentative parse. */ |
21526606 | 15987 | |
a723baf1 | 15988 | static bool |
94edc4ab | 15989 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
15990 | { |
15991 | return (cp_parser_parsing_tentatively (parser) | |
15992 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
15993 | } | |
15994 | ||
4de8668e | 15995 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
15996 | |
15997 | static bool | |
94edc4ab | 15998 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
15999 | { |
16000 | return parser->allow_gnu_extensions_p; | |
16001 | } | |
16002 | ||
16003 | \f | |
a723baf1 MM |
16004 | /* The parser. */ |
16005 | ||
16006 | static GTY (()) cp_parser *the_parser; | |
16007 | ||
16008 | /* External interface. */ | |
16009 | ||
d1bd0ded | 16010 | /* Parse one entire translation unit. */ |
a723baf1 | 16011 | |
d1bd0ded GK |
16012 | void |
16013 | c_parse_file (void) | |
a723baf1 MM |
16014 | { |
16015 | bool error_occurred; | |
f75fbaf7 ZW |
16016 | static bool already_called = false; |
16017 | ||
16018 | if (already_called) | |
16019 | { | |
16020 | sorry ("inter-module optimizations not implemented for C++"); | |
16021 | return; | |
16022 | } | |
16023 | already_called = true; | |
a723baf1 MM |
16024 | |
16025 | the_parser = cp_parser_new (); | |
78757caa KL |
16026 | push_deferring_access_checks (flag_access_control |
16027 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
16028 | error_occurred = cp_parser_translation_unit (the_parser); |
16029 | the_parser = NULL; | |
a723baf1 MM |
16030 | } |
16031 | ||
a723baf1 MM |
16032 | /* This variable must be provided by every front end. */ |
16033 | ||
16034 | int yydebug; | |
16035 | ||
16036 | #include "gt-cp-parser.h" |