]>
Commit | Line | Data |
---|---|---|
a723baf1 | 1 | /* C++ Parser. |
b0bc6e8e | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
a723baf1 MM |
3 | Written by Mark Mitchell <mark@codesourcery.com>. |
4 | ||
f5adbb8d | 5 | This file is part of GCC. |
a723baf1 | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify it |
a723baf1 MM |
8 | under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, but |
a723baf1 MM |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 18 | along with GCC; see the file COPYING. If not, write to the Free |
a723baf1 MM |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "dyn-string.h" | |
27 | #include "varray.h" | |
28 | #include "cpplib.h" | |
29 | #include "tree.h" | |
30 | #include "cp-tree.h" | |
31 | #include "c-pragma.h" | |
32 | #include "decl.h" | |
33 | #include "flags.h" | |
34 | #include "diagnostic.h" | |
a723baf1 MM |
35 | #include "toplev.h" |
36 | #include "output.h" | |
37 | ||
38 | \f | |
39 | /* The lexer. */ | |
40 | ||
41 | /* Overview | |
42 | -------- | |
43 | ||
44 | A cp_lexer represents a stream of cp_tokens. It allows arbitrary | |
45 | look-ahead. | |
46 | ||
47 | Methodology | |
48 | ----------- | |
49 | ||
50 | We use a circular buffer to store incoming tokens. | |
51 | ||
52 | Some artifacts of the C++ language (such as the | |
53 | expression/declaration ambiguity) require arbitrary look-ahead. | |
54 | The strategy we adopt for dealing with these problems is to attempt | |
55 | to parse one construct (e.g., the declaration) and fall back to the | |
56 | other (e.g., the expression) if that attempt does not succeed. | |
57 | Therefore, we must sometimes store an arbitrary number of tokens. | |
58 | ||
59 | The parser routinely peeks at the next token, and then consumes it | |
60 | later. That also requires a buffer in which to store the tokens. | |
21526606 | 61 | |
a723baf1 MM |
62 | In order to easily permit adding tokens to the end of the buffer, |
63 | while removing them from the beginning of the buffer, we use a | |
64 | circular buffer. */ | |
65 | ||
66 | /* A C++ token. */ | |
67 | ||
68 | typedef struct cp_token GTY (()) | |
69 | { | |
70 | /* The kind of token. */ | |
df2b750f | 71 | ENUM_BITFIELD (cpp_ttype) type : 8; |
a723baf1 MM |
72 | /* If this token is a keyword, this value indicates which keyword. |
73 | Otherwise, this value is RID_MAX. */ | |
df2b750f | 74 | ENUM_BITFIELD (rid) keyword : 8; |
f4abade9 GB |
75 | /* Token flags. */ |
76 | unsigned char flags; | |
522df488 DN |
77 | /* The value associated with this token, if any. */ |
78 | tree value; | |
82a98427 NS |
79 | /* The location at which this token was found. */ |
80 | location_t location; | |
a723baf1 MM |
81 | } cp_token; |
82 | ||
522df488 DN |
83 | /* The number of tokens in a single token block. |
84 | Computed so that cp_token_block fits in a 512B allocation unit. */ | |
a723baf1 | 85 | |
522df488 | 86 | #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token)) |
a723baf1 MM |
87 | |
88 | /* A group of tokens. These groups are chained together to store | |
89 | large numbers of tokens. (For example, a token block is created | |
90 | when the body of an inline member function is first encountered; | |
91 | the tokens are processed later after the class definition is | |
21526606 | 92 | complete.) |
a723baf1 MM |
93 | |
94 | This somewhat ungainly data structure (as opposed to, say, a | |
34cd5ae7 | 95 | variable-length array), is used due to constraints imposed by the |
a723baf1 MM |
96 | current garbage-collection methodology. If it is made more |
97 | flexible, we could perhaps simplify the data structures involved. */ | |
98 | ||
99 | typedef struct cp_token_block GTY (()) | |
100 | { | |
101 | /* The tokens. */ | |
102 | cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS]; | |
103 | /* The number of tokens in this block. */ | |
104 | size_t num_tokens; | |
105 | /* The next token block in the chain. */ | |
106 | struct cp_token_block *next; | |
107 | /* The previous block in the chain. */ | |
108 | struct cp_token_block *prev; | |
109 | } cp_token_block; | |
110 | ||
111 | typedef struct cp_token_cache GTY (()) | |
112 | { | |
113 | /* The first block in the cache. NULL if there are no tokens in the | |
114 | cache. */ | |
115 | cp_token_block *first; | |
116 | /* The last block in the cache. NULL If there are no tokens in the | |
117 | cache. */ | |
118 | cp_token_block *last; | |
119 | } cp_token_cache; | |
120 | ||
9bcb9aae | 121 | /* Prototypes. */ |
a723baf1 | 122 | |
21526606 | 123 | static cp_token_cache *cp_token_cache_new |
a723baf1 MM |
124 | (void); |
125 | static void cp_token_cache_push_token | |
126 | (cp_token_cache *, cp_token *); | |
127 | ||
128 | /* Create a new cp_token_cache. */ | |
129 | ||
130 | static cp_token_cache * | |
bf9d3c27 | 131 | cp_token_cache_new (void) |
a723baf1 | 132 | { |
c68b0a84 | 133 | return ggc_alloc_cleared (sizeof (cp_token_cache)); |
a723baf1 MM |
134 | } |
135 | ||
136 | /* Add *TOKEN to *CACHE. */ | |
137 | ||
138 | static void | |
139 | cp_token_cache_push_token (cp_token_cache *cache, | |
140 | cp_token *token) | |
141 | { | |
142 | cp_token_block *b = cache->last; | |
143 | ||
144 | /* See if we need to allocate a new token block. */ | |
145 | if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS) | |
146 | { | |
c68b0a84 | 147 | b = ggc_alloc_cleared (sizeof (cp_token_block)); |
a723baf1 MM |
148 | b->prev = cache->last; |
149 | if (cache->last) | |
150 | { | |
151 | cache->last->next = b; | |
152 | cache->last = b; | |
153 | } | |
154 | else | |
155 | cache->first = cache->last = b; | |
156 | } | |
157 | /* Add this token to the current token block. */ | |
158 | b->tokens[b->num_tokens++] = *token; | |
159 | } | |
160 | ||
161 | /* The cp_lexer structure represents the C++ lexer. It is responsible | |
162 | for managing the token stream from the preprocessor and supplying | |
163 | it to the parser. */ | |
164 | ||
165 | typedef struct cp_lexer GTY (()) | |
166 | { | |
167 | /* The memory allocated for the buffer. Never NULL. */ | |
168 | cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer; | |
169 | /* A pointer just past the end of the memory allocated for the buffer. */ | |
1431042e | 170 | cp_token * GTY ((skip)) buffer_end; |
a723baf1 | 171 | /* The first valid token in the buffer, or NULL if none. */ |
1431042e | 172 | cp_token * GTY ((skip)) first_token; |
a723baf1 MM |
173 | /* The next available token. If NEXT_TOKEN is NULL, then there are |
174 | no more available tokens. */ | |
1431042e | 175 | cp_token * GTY ((skip)) next_token; |
a723baf1 MM |
176 | /* A pointer just past the last available token. If FIRST_TOKEN is |
177 | NULL, however, there are no available tokens, and then this | |
178 | location is simply the place in which the next token read will be | |
179 | placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full. | |
180 | When the LAST_TOKEN == BUFFER, then the last token is at the | |
181 | highest memory address in the BUFFER. */ | |
1431042e | 182 | cp_token * GTY ((skip)) last_token; |
a723baf1 MM |
183 | |
184 | /* A stack indicating positions at which cp_lexer_save_tokens was | |
185 | called. The top entry is the most recent position at which we | |
186 | began saving tokens. The entries are differences in token | |
187 | position between FIRST_TOKEN and the first saved token. | |
188 | ||
189 | If the stack is non-empty, we are saving tokens. When a token is | |
190 | consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN | |
191 | pointer will not. The token stream will be preserved so that it | |
192 | can be reexamined later. | |
193 | ||
194 | If the stack is empty, then we are not saving tokens. Whenever a | |
195 | token is consumed, the FIRST_TOKEN pointer will be moved, and the | |
196 | consumed token will be gone forever. */ | |
197 | varray_type saved_tokens; | |
198 | ||
199 | /* The STRING_CST tokens encountered while processing the current | |
200 | string literal. */ | |
201 | varray_type string_tokens; | |
202 | ||
203 | /* True if we should obtain more tokens from the preprocessor; false | |
204 | if we are processing a saved token cache. */ | |
205 | bool main_lexer_p; | |
206 | ||
207 | /* True if we should output debugging information. */ | |
208 | bool debugging_p; | |
209 | ||
210 | /* The next lexer in a linked list of lexers. */ | |
211 | struct cp_lexer *next; | |
212 | } cp_lexer; | |
213 | ||
214 | /* Prototypes. */ | |
215 | ||
17211ab5 | 216 | static cp_lexer *cp_lexer_new_main |
94edc4ab | 217 | (void); |
a723baf1 | 218 | static cp_lexer *cp_lexer_new_from_tokens |
94edc4ab | 219 | (struct cp_token_cache *); |
a723baf1 | 220 | static int cp_lexer_saving_tokens |
94edc4ab | 221 | (const cp_lexer *); |
a723baf1 | 222 | static cp_token *cp_lexer_next_token |
94edc4ab | 223 | (cp_lexer *, cp_token *); |
a668c6ad MM |
224 | static cp_token *cp_lexer_prev_token |
225 | (cp_lexer *, cp_token *); | |
21526606 | 226 | static ptrdiff_t cp_lexer_token_difference |
94edc4ab | 227 | (cp_lexer *, cp_token *, cp_token *); |
a723baf1 | 228 | static cp_token *cp_lexer_read_token |
94edc4ab | 229 | (cp_lexer *); |
a723baf1 | 230 | static void cp_lexer_maybe_grow_buffer |
94edc4ab | 231 | (cp_lexer *); |
a723baf1 | 232 | static void cp_lexer_get_preprocessor_token |
94edc4ab | 233 | (cp_lexer *, cp_token *); |
a723baf1 | 234 | static cp_token *cp_lexer_peek_token |
94edc4ab | 235 | (cp_lexer *); |
a723baf1 | 236 | static cp_token *cp_lexer_peek_nth_token |
94edc4ab | 237 | (cp_lexer *, size_t); |
f7b5ecd9 | 238 | static inline bool cp_lexer_next_token_is |
94edc4ab | 239 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 240 | static bool cp_lexer_next_token_is_not |
94edc4ab | 241 | (cp_lexer *, enum cpp_ttype); |
a723baf1 | 242 | static bool cp_lexer_next_token_is_keyword |
94edc4ab | 243 | (cp_lexer *, enum rid); |
21526606 | 244 | static cp_token *cp_lexer_consume_token |
94edc4ab | 245 | (cp_lexer *); |
a723baf1 MM |
246 | static void cp_lexer_purge_token |
247 | (cp_lexer *); | |
248 | static void cp_lexer_purge_tokens_after | |
249 | (cp_lexer *, cp_token *); | |
250 | static void cp_lexer_save_tokens | |
94edc4ab | 251 | (cp_lexer *); |
a723baf1 | 252 | static void cp_lexer_commit_tokens |
94edc4ab | 253 | (cp_lexer *); |
a723baf1 | 254 | static void cp_lexer_rollback_tokens |
94edc4ab | 255 | (cp_lexer *); |
21526606 | 256 | static inline void cp_lexer_set_source_position_from_token |
94edc4ab | 257 | (cp_lexer *, const cp_token *); |
a723baf1 | 258 | static void cp_lexer_print_token |
94edc4ab | 259 | (FILE *, cp_token *); |
21526606 | 260 | static inline bool cp_lexer_debugging_p |
94edc4ab | 261 | (cp_lexer *); |
a723baf1 | 262 | static void cp_lexer_start_debugging |
94edc4ab | 263 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 | 264 | static void cp_lexer_stop_debugging |
94edc4ab | 265 | (cp_lexer *) ATTRIBUTE_UNUSED; |
a723baf1 MM |
266 | |
267 | /* Manifest constants. */ | |
268 | ||
269 | #define CP_TOKEN_BUFFER_SIZE 5 | |
270 | #define CP_SAVED_TOKENS_SIZE 5 | |
271 | ||
272 | /* A token type for keywords, as opposed to ordinary identifiers. */ | |
273 | #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) | |
274 | ||
275 | /* A token type for template-ids. If a template-id is processed while | |
276 | parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token; | |
277 | the value of the CPP_TEMPLATE_ID is whatever was returned by | |
278 | cp_parser_template_id. */ | |
279 | #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1)) | |
280 | ||
281 | /* A token type for nested-name-specifiers. If a | |
282 | nested-name-specifier is processed while parsing tentatively, it is | |
283 | replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the | |
284 | CPP_NESTED_NAME_SPECIFIER is whatever was returned by | |
285 | cp_parser_nested_name_specifier_opt. */ | |
286 | #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1)) | |
287 | ||
288 | /* A token type for tokens that are not tokens at all; these are used | |
289 | to mark the end of a token block. */ | |
290 | #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1) | |
291 | ||
292 | /* Variables. */ | |
293 | ||
294 | /* The stream to which debugging output should be written. */ | |
295 | static FILE *cp_lexer_debug_stream; | |
296 | ||
17211ab5 GK |
297 | /* Create a new main C++ lexer, the lexer that gets tokens from the |
298 | preprocessor. */ | |
a723baf1 MM |
299 | |
300 | static cp_lexer * | |
17211ab5 | 301 | cp_lexer_new_main (void) |
a723baf1 MM |
302 | { |
303 | cp_lexer *lexer; | |
17211ab5 GK |
304 | cp_token first_token; |
305 | ||
306 | /* It's possible that lexing the first token will load a PCH file, | |
307 | which is a GC collection point. So we have to grab the first | |
308 | token before allocating any memory. */ | |
309 | cp_lexer_get_preprocessor_token (NULL, &first_token); | |
18c81520 | 310 | c_common_no_more_pch (); |
a723baf1 MM |
311 | |
312 | /* Allocate the memory. */ | |
c68b0a84 | 313 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
314 | |
315 | /* Create the circular buffer. */ | |
c68b0a84 | 316 | lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)); |
a723baf1 MM |
317 | lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE; |
318 | ||
17211ab5 GK |
319 | /* There is one token in the buffer. */ |
320 | lexer->last_token = lexer->buffer + 1; | |
321 | lexer->first_token = lexer->buffer; | |
322 | lexer->next_token = lexer->buffer; | |
323 | memcpy (lexer->buffer, &first_token, sizeof (cp_token)); | |
a723baf1 MM |
324 | |
325 | /* This lexer obtains more tokens by calling c_lex. */ | |
17211ab5 | 326 | lexer->main_lexer_p = true; |
a723baf1 MM |
327 | |
328 | /* Create the SAVED_TOKENS stack. */ | |
329 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
21526606 | 330 | |
a723baf1 MM |
331 | /* Create the STRINGS array. */ |
332 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
333 | ||
334 | /* Assume we are not debugging. */ | |
335 | lexer->debugging_p = false; | |
336 | ||
337 | return lexer; | |
338 | } | |
339 | ||
340 | /* Create a new lexer whose token stream is primed with the TOKENS. | |
341 | When these tokens are exhausted, no new tokens will be read. */ | |
342 | ||
343 | static cp_lexer * | |
344 | cp_lexer_new_from_tokens (cp_token_cache *tokens) | |
345 | { | |
346 | cp_lexer *lexer; | |
347 | cp_token *token; | |
348 | cp_token_block *block; | |
349 | ptrdiff_t num_tokens; | |
350 | ||
17211ab5 | 351 | /* Allocate the memory. */ |
c68b0a84 | 352 | lexer = ggc_alloc_cleared (sizeof (cp_lexer)); |
a723baf1 MM |
353 | |
354 | /* Create a new buffer, appropriately sized. */ | |
355 | num_tokens = 0; | |
356 | for (block = tokens->first; block != NULL; block = block->next) | |
357 | num_tokens += block->num_tokens; | |
c68b0a84 | 358 | lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token)); |
a723baf1 | 359 | lexer->buffer_end = lexer->buffer + num_tokens; |
21526606 | 360 | |
a723baf1 MM |
361 | /* Install the tokens. */ |
362 | token = lexer->buffer; | |
363 | for (block = tokens->first; block != NULL; block = block->next) | |
364 | { | |
365 | memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token)); | |
366 | token += block->num_tokens; | |
367 | } | |
368 | ||
369 | /* The FIRST_TOKEN is the beginning of the buffer. */ | |
370 | lexer->first_token = lexer->buffer; | |
371 | /* The next available token is also at the beginning of the buffer. */ | |
372 | lexer->next_token = lexer->buffer; | |
373 | /* The buffer is full. */ | |
374 | lexer->last_token = lexer->first_token; | |
375 | ||
17211ab5 GK |
376 | /* This lexer doesn't obtain more tokens. */ |
377 | lexer->main_lexer_p = false; | |
378 | ||
379 | /* Create the SAVED_TOKENS stack. */ | |
380 | VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens"); | |
21526606 | 381 | |
17211ab5 GK |
382 | /* Create the STRINGS array. */ |
383 | VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings"); | |
384 | ||
385 | /* Assume we are not debugging. */ | |
386 | lexer->debugging_p = false; | |
387 | ||
a723baf1 MM |
388 | return lexer; |
389 | } | |
390 | ||
4de8668e | 391 | /* Returns nonzero if debugging information should be output. */ |
a723baf1 | 392 | |
f7b5ecd9 MM |
393 | static inline bool |
394 | cp_lexer_debugging_p (cp_lexer *lexer) | |
a723baf1 | 395 | { |
f7b5ecd9 MM |
396 | return lexer->debugging_p; |
397 | } | |
398 | ||
399 | /* Set the current source position from the information stored in | |
400 | TOKEN. */ | |
401 | ||
402 | static inline void | |
94edc4ab NN |
403 | cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
404 | const cp_token *token) | |
f7b5ecd9 MM |
405 | { |
406 | /* Ideally, the source position information would not be a global | |
407 | variable, but it is. */ | |
408 | ||
409 | /* Update the line number. */ | |
410 | if (token->type != CPP_EOF) | |
82a98427 | 411 | input_location = token->location; |
a723baf1 MM |
412 | } |
413 | ||
414 | /* TOKEN points into the circular token buffer. Return a pointer to | |
415 | the next token in the buffer. */ | |
416 | ||
f7b5ecd9 | 417 | static inline cp_token * |
94edc4ab | 418 | cp_lexer_next_token (cp_lexer* lexer, cp_token* token) |
a723baf1 MM |
419 | { |
420 | token++; | |
421 | if (token == lexer->buffer_end) | |
422 | token = lexer->buffer; | |
423 | return token; | |
424 | } | |
425 | ||
a668c6ad MM |
426 | /* TOKEN points into the circular token buffer. Return a pointer to |
427 | the previous token in the buffer. */ | |
428 | ||
429 | static inline cp_token * | |
430 | cp_lexer_prev_token (cp_lexer* lexer, cp_token* token) | |
431 | { | |
432 | if (token == lexer->buffer) | |
433 | token = lexer->buffer_end; | |
434 | return token - 1; | |
435 | } | |
436 | ||
4de8668e | 437 | /* nonzero if we are presently saving tokens. */ |
f7b5ecd9 MM |
438 | |
439 | static int | |
94edc4ab | 440 | cp_lexer_saving_tokens (const cp_lexer* lexer) |
f7b5ecd9 MM |
441 | { |
442 | return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0; | |
443 | } | |
444 | ||
a723baf1 MM |
445 | /* Return a pointer to the token that is N tokens beyond TOKEN in the |
446 | buffer. */ | |
447 | ||
448 | static cp_token * | |
449 | cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n) | |
450 | { | |
451 | token += n; | |
452 | if (token >= lexer->buffer_end) | |
453 | token = lexer->buffer + (token - lexer->buffer_end); | |
454 | return token; | |
455 | } | |
456 | ||
457 | /* Returns the number of times that START would have to be incremented | |
458 | to reach FINISH. If START and FINISH are the same, returns zero. */ | |
459 | ||
460 | static ptrdiff_t | |
94edc4ab | 461 | cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish) |
a723baf1 MM |
462 | { |
463 | if (finish >= start) | |
464 | return finish - start; | |
465 | else | |
466 | return ((lexer->buffer_end - lexer->buffer) | |
467 | - (start - finish)); | |
468 | } | |
469 | ||
470 | /* Obtain another token from the C preprocessor and add it to the | |
471 | token buffer. Returns the newly read token. */ | |
472 | ||
473 | static cp_token * | |
94edc4ab | 474 | cp_lexer_read_token (cp_lexer* lexer) |
a723baf1 MM |
475 | { |
476 | cp_token *token; | |
477 | ||
478 | /* Make sure there is room in the buffer. */ | |
479 | cp_lexer_maybe_grow_buffer (lexer); | |
480 | ||
481 | /* If there weren't any tokens, then this one will be the first. */ | |
482 | if (!lexer->first_token) | |
483 | lexer->first_token = lexer->last_token; | |
484 | /* Similarly, if there were no available tokens, there is one now. */ | |
485 | if (!lexer->next_token) | |
486 | lexer->next_token = lexer->last_token; | |
487 | ||
488 | /* Figure out where we're going to store the new token. */ | |
489 | token = lexer->last_token; | |
490 | ||
491 | /* Get a new token from the preprocessor. */ | |
492 | cp_lexer_get_preprocessor_token (lexer, token); | |
493 | ||
494 | /* Increment LAST_TOKEN. */ | |
495 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
496 | ||
e6cc3a24 ZW |
497 | /* Strings should have type `const char []'. Right now, we will |
498 | have an ARRAY_TYPE that is constant rather than an array of | |
499 | constant elements. | |
500 | FIXME: Make fix_string_type get this right in the first place. */ | |
501 | if ((token->type == CPP_STRING || token->type == CPP_WSTRING) | |
502 | && flag_const_strings) | |
a723baf1 | 503 | { |
0173bb6f AO |
504 | if (c_lex_string_translate) |
505 | { | |
506 | tree value = token->value; | |
507 | tree type; | |
e6cc3a24 | 508 | |
0173bb6f AO |
509 | /* We might as well go ahead and release the chained |
510 | translated string such that we can reuse its memory. */ | |
511 | if (TREE_CHAIN (value)) | |
512 | value = TREE_CHAIN (token->value); | |
513 | ||
514 | /* Get the current type. It will be an ARRAY_TYPE. */ | |
515 | type = TREE_TYPE (value); | |
516 | /* Use build_cplus_array_type to rebuild the array, thereby | |
517 | getting the right type. */ | |
518 | type = build_cplus_array_type (TREE_TYPE (type), | |
519 | TYPE_DOMAIN (type)); | |
520 | /* Reset the type of the token. */ | |
521 | TREE_TYPE (value) = type; | |
522 | } | |
a723baf1 MM |
523 | } |
524 | ||
525 | return token; | |
526 | } | |
527 | ||
528 | /* If the circular buffer is full, make it bigger. */ | |
529 | ||
530 | static void | |
94edc4ab | 531 | cp_lexer_maybe_grow_buffer (cp_lexer* lexer) |
a723baf1 MM |
532 | { |
533 | /* If the buffer is full, enlarge it. */ | |
534 | if (lexer->last_token == lexer->first_token) | |
535 | { | |
536 | cp_token *new_buffer; | |
537 | cp_token *old_buffer; | |
538 | cp_token *new_first_token; | |
539 | ptrdiff_t buffer_length; | |
540 | size_t num_tokens_to_copy; | |
541 | ||
542 | /* Remember the current buffer pointer. It will become invalid, | |
543 | but we will need to do pointer arithmetic involving this | |
544 | value. */ | |
545 | old_buffer = lexer->buffer; | |
546 | /* Compute the current buffer size. */ | |
547 | buffer_length = lexer->buffer_end - lexer->buffer; | |
548 | /* Allocate a buffer twice as big. */ | |
21526606 | 549 | new_buffer = ggc_realloc (lexer->buffer, |
c68b0a84 | 550 | 2 * buffer_length * sizeof (cp_token)); |
21526606 | 551 | |
a723baf1 MM |
552 | /* Because the buffer is circular, logically consecutive tokens |
553 | are not necessarily placed consecutively in memory. | |
554 | Therefore, we must keep move the tokens that were before | |
555 | FIRST_TOKEN to the second half of the newly allocated | |
556 | buffer. */ | |
557 | num_tokens_to_copy = (lexer->first_token - old_buffer); | |
558 | memcpy (new_buffer + buffer_length, | |
559 | new_buffer, | |
560 | num_tokens_to_copy * sizeof (cp_token)); | |
561 | /* Clear the rest of the buffer. We never look at this storage, | |
562 | but the garbage collector may. */ | |
21526606 | 563 | memset (new_buffer + buffer_length + num_tokens_to_copy, 0, |
a723baf1 MM |
564 | (buffer_length - num_tokens_to_copy) * sizeof (cp_token)); |
565 | ||
566 | /* Now recompute all of the buffer pointers. */ | |
21526606 | 567 | new_first_token |
a723baf1 MM |
568 | = new_buffer + (lexer->first_token - old_buffer); |
569 | if (lexer->next_token != NULL) | |
570 | { | |
571 | ptrdiff_t next_token_delta; | |
572 | ||
573 | if (lexer->next_token > lexer->first_token) | |
574 | next_token_delta = lexer->next_token - lexer->first_token; | |
575 | else | |
21526606 | 576 | next_token_delta = |
a723baf1 MM |
577 | buffer_length - (lexer->first_token - lexer->next_token); |
578 | lexer->next_token = new_first_token + next_token_delta; | |
579 | } | |
580 | lexer->last_token = new_first_token + buffer_length; | |
581 | lexer->buffer = new_buffer; | |
582 | lexer->buffer_end = new_buffer + buffer_length * 2; | |
583 | lexer->first_token = new_first_token; | |
584 | } | |
585 | } | |
586 | ||
587 | /* Store the next token from the preprocessor in *TOKEN. */ | |
588 | ||
21526606 | 589 | static void |
94edc4ab NN |
590 | cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED , |
591 | cp_token *token) | |
a723baf1 MM |
592 | { |
593 | bool done; | |
594 | ||
595 | /* If this not the main lexer, return a terminating CPP_EOF token. */ | |
17211ab5 | 596 | if (lexer != NULL && !lexer->main_lexer_p) |
a723baf1 MM |
597 | { |
598 | token->type = CPP_EOF; | |
82a98427 NS |
599 | token->location.line = 0; |
600 | token->location.file = NULL; | |
a723baf1 MM |
601 | token->value = NULL_TREE; |
602 | token->keyword = RID_MAX; | |
603 | ||
604 | return; | |
605 | } | |
606 | ||
607 | done = false; | |
608 | /* Keep going until we get a token we like. */ | |
609 | while (!done) | |
610 | { | |
611 | /* Get a new token from the preprocessor. */ | |
f4abade9 | 612 | token->type = c_lex_with_flags (&token->value, &token->flags); |
a723baf1 MM |
613 | /* Issue messages about tokens we cannot process. */ |
614 | switch (token->type) | |
615 | { | |
616 | case CPP_ATSIGN: | |
617 | case CPP_HASH: | |
618 | case CPP_PASTE: | |
619 | error ("invalid token"); | |
620 | break; | |
621 | ||
a723baf1 MM |
622 | default: |
623 | /* This is a good token, so we exit the loop. */ | |
624 | done = true; | |
625 | break; | |
626 | } | |
627 | } | |
628 | /* Now we've got our token. */ | |
82a98427 | 629 | token->location = input_location; |
a723baf1 MM |
630 | |
631 | /* Check to see if this token is a keyword. */ | |
21526606 | 632 | if (token->type == CPP_NAME |
a723baf1 MM |
633 | && C_IS_RESERVED_WORD (token->value)) |
634 | { | |
635 | /* Mark this token as a keyword. */ | |
636 | token->type = CPP_KEYWORD; | |
637 | /* Record which keyword. */ | |
638 | token->keyword = C_RID_CODE (token->value); | |
639 | /* Update the value. Some keywords are mapped to particular | |
640 | entities, rather than simply having the value of the | |
641 | corresponding IDENTIFIER_NODE. For example, `__const' is | |
642 | mapped to `const'. */ | |
643 | token->value = ridpointers[token->keyword]; | |
644 | } | |
645 | else | |
646 | token->keyword = RID_MAX; | |
647 | } | |
648 | ||
649 | /* Return a pointer to the next token in the token stream, but do not | |
650 | consume it. */ | |
651 | ||
652 | static cp_token * | |
94edc4ab | 653 | cp_lexer_peek_token (cp_lexer* lexer) |
a723baf1 MM |
654 | { |
655 | cp_token *token; | |
656 | ||
657 | /* If there are no tokens, read one now. */ | |
658 | if (!lexer->next_token) | |
659 | cp_lexer_read_token (lexer); | |
660 | ||
661 | /* Provide debugging output. */ | |
662 | if (cp_lexer_debugging_p (lexer)) | |
663 | { | |
664 | fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: "); | |
665 | cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); | |
666 | fprintf (cp_lexer_debug_stream, "\n"); | |
667 | } | |
668 | ||
669 | token = lexer->next_token; | |
670 | cp_lexer_set_source_position_from_token (lexer, token); | |
671 | return token; | |
672 | } | |
673 | ||
674 | /* Return true if the next token has the indicated TYPE. */ | |
675 | ||
676 | static bool | |
94edc4ab | 677 | cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
678 | { |
679 | cp_token *token; | |
680 | ||
681 | /* Peek at the next token. */ | |
682 | token = cp_lexer_peek_token (lexer); | |
683 | /* Check to see if it has the indicated TYPE. */ | |
684 | return token->type == type; | |
685 | } | |
686 | ||
687 | /* Return true if the next token does not have the indicated TYPE. */ | |
688 | ||
689 | static bool | |
94edc4ab | 690 | cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) |
a723baf1 MM |
691 | { |
692 | return !cp_lexer_next_token_is (lexer, type); | |
693 | } | |
694 | ||
695 | /* Return true if the next token is the indicated KEYWORD. */ | |
696 | ||
697 | static bool | |
94edc4ab | 698 | cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) |
a723baf1 MM |
699 | { |
700 | cp_token *token; | |
701 | ||
702 | /* Peek at the next token. */ | |
703 | token = cp_lexer_peek_token (lexer); | |
704 | /* Check to see if it is the indicated keyword. */ | |
705 | return token->keyword == keyword; | |
706 | } | |
707 | ||
708 | /* Return a pointer to the Nth token in the token stream. If N is 1, | |
709 | then this is precisely equivalent to cp_lexer_peek_token. */ | |
710 | ||
711 | static cp_token * | |
94edc4ab | 712 | cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) |
a723baf1 MM |
713 | { |
714 | cp_token *token; | |
715 | ||
716 | /* N is 1-based, not zero-based. */ | |
717 | my_friendly_assert (n > 0, 20000224); | |
718 | ||
719 | /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */ | |
720 | token = lexer->next_token; | |
721 | /* If there are no tokens in the buffer, get one now. */ | |
722 | if (!token) | |
723 | { | |
724 | cp_lexer_read_token (lexer); | |
725 | token = lexer->next_token; | |
726 | } | |
727 | ||
728 | /* Now, read tokens until we have enough. */ | |
729 | while (--n > 0) | |
730 | { | |
731 | /* Advance to the next token. */ | |
732 | token = cp_lexer_next_token (lexer, token); | |
733 | /* If that's all the tokens we have, read a new one. */ | |
734 | if (token == lexer->last_token) | |
735 | token = cp_lexer_read_token (lexer); | |
736 | } | |
737 | ||
738 | return token; | |
739 | } | |
740 | ||
741 | /* Consume the next token. The pointer returned is valid only until | |
742 | another token is read. Callers should preserve copy the token | |
743 | explicitly if they will need its value for a longer period of | |
744 | time. */ | |
745 | ||
746 | static cp_token * | |
94edc4ab | 747 | cp_lexer_consume_token (cp_lexer* lexer) |
a723baf1 MM |
748 | { |
749 | cp_token *token; | |
750 | ||
751 | /* If there are no tokens, read one now. */ | |
752 | if (!lexer->next_token) | |
753 | cp_lexer_read_token (lexer); | |
754 | ||
755 | /* Remember the token we'll be returning. */ | |
756 | token = lexer->next_token; | |
757 | ||
758 | /* Increment NEXT_TOKEN. */ | |
21526606 | 759 | lexer->next_token = cp_lexer_next_token (lexer, |
a723baf1 MM |
760 | lexer->next_token); |
761 | /* Check to see if we're all out of tokens. */ | |
762 | if (lexer->next_token == lexer->last_token) | |
763 | lexer->next_token = NULL; | |
764 | ||
765 | /* If we're not saving tokens, then move FIRST_TOKEN too. */ | |
766 | if (!cp_lexer_saving_tokens (lexer)) | |
767 | { | |
768 | /* If there are no tokens available, set FIRST_TOKEN to NULL. */ | |
769 | if (!lexer->next_token) | |
770 | lexer->first_token = NULL; | |
771 | else | |
772 | lexer->first_token = lexer->next_token; | |
773 | } | |
774 | ||
775 | /* Provide debugging output. */ | |
776 | if (cp_lexer_debugging_p (lexer)) | |
777 | { | |
778 | fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: "); | |
779 | cp_lexer_print_token (cp_lexer_debug_stream, token); | |
780 | fprintf (cp_lexer_debug_stream, "\n"); | |
781 | } | |
782 | ||
783 | return token; | |
784 | } | |
785 | ||
786 | /* Permanently remove the next token from the token stream. There | |
787 | must be a valid next token already; this token never reads | |
788 | additional tokens from the preprocessor. */ | |
789 | ||
790 | static void | |
791 | cp_lexer_purge_token (cp_lexer *lexer) | |
792 | { | |
793 | cp_token *token; | |
794 | cp_token *next_token; | |
795 | ||
796 | token = lexer->next_token; | |
21526606 | 797 | while (true) |
a723baf1 MM |
798 | { |
799 | next_token = cp_lexer_next_token (lexer, token); | |
800 | if (next_token == lexer->last_token) | |
801 | break; | |
802 | *token = *next_token; | |
803 | token = next_token; | |
804 | } | |
805 | ||
806 | lexer->last_token = token; | |
807 | /* The token purged may have been the only token remaining; if so, | |
808 | clear NEXT_TOKEN. */ | |
809 | if (lexer->next_token == token) | |
810 | lexer->next_token = NULL; | |
811 | } | |
812 | ||
813 | /* Permanently remove all tokens after TOKEN, up to, but not | |
814 | including, the token that will be returned next by | |
815 | cp_lexer_peek_token. */ | |
816 | ||
817 | static void | |
818 | cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token) | |
819 | { | |
820 | cp_token *peek; | |
821 | cp_token *t1; | |
822 | cp_token *t2; | |
823 | ||
824 | if (lexer->next_token) | |
825 | { | |
826 | /* Copy the tokens that have not yet been read to the location | |
827 | immediately following TOKEN. */ | |
828 | t1 = cp_lexer_next_token (lexer, token); | |
829 | t2 = peek = cp_lexer_peek_token (lexer); | |
830 | /* Move tokens into the vacant area between TOKEN and PEEK. */ | |
831 | while (t2 != lexer->last_token) | |
832 | { | |
833 | *t1 = *t2; | |
834 | t1 = cp_lexer_next_token (lexer, t1); | |
835 | t2 = cp_lexer_next_token (lexer, t2); | |
836 | } | |
837 | /* Now, the next available token is right after TOKEN. */ | |
838 | lexer->next_token = cp_lexer_next_token (lexer, token); | |
839 | /* And the last token is wherever we ended up. */ | |
840 | lexer->last_token = t1; | |
841 | } | |
842 | else | |
843 | { | |
844 | /* There are no tokens in the buffer, so there is nothing to | |
845 | copy. The last token in the buffer is TOKEN itself. */ | |
846 | lexer->last_token = cp_lexer_next_token (lexer, token); | |
847 | } | |
848 | } | |
849 | ||
850 | /* Begin saving tokens. All tokens consumed after this point will be | |
851 | preserved. */ | |
852 | ||
853 | static void | |
94edc4ab | 854 | cp_lexer_save_tokens (cp_lexer* lexer) |
a723baf1 MM |
855 | { |
856 | /* Provide debugging output. */ | |
857 | if (cp_lexer_debugging_p (lexer)) | |
858 | fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); | |
859 | ||
860 | /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can | |
861 | restore the tokens if required. */ | |
862 | if (!lexer->next_token) | |
863 | cp_lexer_read_token (lexer); | |
864 | ||
865 | VARRAY_PUSH_INT (lexer->saved_tokens, | |
866 | cp_lexer_token_difference (lexer, | |
867 | lexer->first_token, | |
868 | lexer->next_token)); | |
869 | } | |
870 | ||
871 | /* Commit to the portion of the token stream most recently saved. */ | |
872 | ||
873 | static void | |
94edc4ab | 874 | cp_lexer_commit_tokens (cp_lexer* lexer) |
a723baf1 MM |
875 | { |
876 | /* Provide debugging output. */ | |
877 | if (cp_lexer_debugging_p (lexer)) | |
878 | fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); | |
879 | ||
880 | VARRAY_POP (lexer->saved_tokens); | |
881 | } | |
882 | ||
883 | /* Return all tokens saved since the last call to cp_lexer_save_tokens | |
884 | to the token stream. Stop saving tokens. */ | |
885 | ||
886 | static void | |
94edc4ab | 887 | cp_lexer_rollback_tokens (cp_lexer* lexer) |
a723baf1 MM |
888 | { |
889 | size_t delta; | |
890 | ||
891 | /* Provide debugging output. */ | |
892 | if (cp_lexer_debugging_p (lexer)) | |
893 | fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); | |
894 | ||
895 | /* Find the token that was the NEXT_TOKEN when we started saving | |
896 | tokens. */ | |
897 | delta = VARRAY_TOP_INT(lexer->saved_tokens); | |
898 | /* Make it the next token again now. */ | |
899 | lexer->next_token = cp_lexer_advance_token (lexer, | |
21526606 | 900 | lexer->first_token, |
a723baf1 | 901 | delta); |
15d2cb19 | 902 | /* It might be the case that there were no tokens when we started |
a723baf1 MM |
903 | saving tokens, but that there are some tokens now. */ |
904 | if (!lexer->next_token && lexer->first_token) | |
905 | lexer->next_token = lexer->first_token; | |
906 | ||
907 | /* Stop saving tokens. */ | |
908 | VARRAY_POP (lexer->saved_tokens); | |
909 | } | |
910 | ||
a723baf1 MM |
911 | /* Print a representation of the TOKEN on the STREAM. */ |
912 | ||
913 | static void | |
94edc4ab | 914 | cp_lexer_print_token (FILE * stream, cp_token* token) |
a723baf1 MM |
915 | { |
916 | const char *token_type = NULL; | |
917 | ||
918 | /* Figure out what kind of token this is. */ | |
919 | switch (token->type) | |
920 | { | |
921 | case CPP_EQ: | |
922 | token_type = "EQ"; | |
923 | break; | |
924 | ||
925 | case CPP_COMMA: | |
926 | token_type = "COMMA"; | |
927 | break; | |
928 | ||
929 | case CPP_OPEN_PAREN: | |
930 | token_type = "OPEN_PAREN"; | |
931 | break; | |
932 | ||
933 | case CPP_CLOSE_PAREN: | |
934 | token_type = "CLOSE_PAREN"; | |
935 | break; | |
936 | ||
937 | case CPP_OPEN_BRACE: | |
938 | token_type = "OPEN_BRACE"; | |
939 | break; | |
940 | ||
941 | case CPP_CLOSE_BRACE: | |
942 | token_type = "CLOSE_BRACE"; | |
943 | break; | |
944 | ||
945 | case CPP_SEMICOLON: | |
946 | token_type = "SEMICOLON"; | |
947 | break; | |
948 | ||
949 | case CPP_NAME: | |
950 | token_type = "NAME"; | |
951 | break; | |
952 | ||
953 | case CPP_EOF: | |
954 | token_type = "EOF"; | |
955 | break; | |
956 | ||
957 | case CPP_KEYWORD: | |
958 | token_type = "keyword"; | |
959 | break; | |
960 | ||
961 | /* This is not a token that we know how to handle yet. */ | |
962 | default: | |
963 | break; | |
964 | } | |
965 | ||
966 | /* If we have a name for the token, print it out. Otherwise, we | |
967 | simply give the numeric code. */ | |
968 | if (token_type) | |
969 | fprintf (stream, "%s", token_type); | |
970 | else | |
971 | fprintf (stream, "%d", token->type); | |
972 | /* And, for an identifier, print the identifier name. */ | |
21526606 | 973 | if (token->type == CPP_NAME |
a723baf1 MM |
974 | /* Some keywords have a value that is not an IDENTIFIER_NODE. |
975 | For example, `struct' is mapped to an INTEGER_CST. */ | |
21526606 | 976 | || (token->type == CPP_KEYWORD |
a723baf1 MM |
977 | && TREE_CODE (token->value) == IDENTIFIER_NODE)) |
978 | fprintf (stream, " %s", IDENTIFIER_POINTER (token->value)); | |
979 | } | |
980 | ||
a723baf1 MM |
981 | /* Start emitting debugging information. */ |
982 | ||
983 | static void | |
94edc4ab | 984 | cp_lexer_start_debugging (cp_lexer* lexer) |
a723baf1 MM |
985 | { |
986 | ++lexer->debugging_p; | |
987 | } | |
21526606 | 988 | |
a723baf1 MM |
989 | /* Stop emitting debugging information. */ |
990 | ||
991 | static void | |
94edc4ab | 992 | cp_lexer_stop_debugging (cp_lexer* lexer) |
a723baf1 MM |
993 | { |
994 | --lexer->debugging_p; | |
995 | } | |
996 | ||
997 | \f | |
998 | /* The parser. */ | |
999 | ||
1000 | /* Overview | |
1001 | -------- | |
1002 | ||
1003 | A cp_parser parses the token stream as specified by the C++ | |
1004 | grammar. Its job is purely parsing, not semantic analysis. For | |
1005 | example, the parser breaks the token stream into declarators, | |
1006 | expressions, statements, and other similar syntactic constructs. | |
1007 | It does not check that the types of the expressions on either side | |
1008 | of an assignment-statement are compatible, or that a function is | |
1009 | not declared with a parameter of type `void'. | |
1010 | ||
1011 | The parser invokes routines elsewhere in the compiler to perform | |
1012 | semantic analysis and to build up the abstract syntax tree for the | |
21526606 | 1013 | code processed. |
a723baf1 MM |
1014 | |
1015 | The parser (and the template instantiation code, which is, in a | |
1016 | way, a close relative of parsing) are the only parts of the | |
1017 | compiler that should be calling push_scope and pop_scope, or | |
1018 | related functions. The parser (and template instantiation code) | |
1019 | keeps track of what scope is presently active; everything else | |
1020 | should simply honor that. (The code that generates static | |
1021 | initializers may also need to set the scope, in order to check | |
1022 | access control correctly when emitting the initializers.) | |
1023 | ||
1024 | Methodology | |
1025 | ----------- | |
21526606 | 1026 | |
a723baf1 MM |
1027 | The parser is of the standard recursive-descent variety. Upcoming |
1028 | tokens in the token stream are examined in order to determine which | |
1029 | production to use when parsing a non-terminal. Some C++ constructs | |
1030 | require arbitrary look ahead to disambiguate. For example, it is | |
1031 | impossible, in the general case, to tell whether a statement is an | |
1032 | expression or declaration without scanning the entire statement. | |
1033 | Therefore, the parser is capable of "parsing tentatively." When the | |
1034 | parser is not sure what construct comes next, it enters this mode. | |
1035 | Then, while we attempt to parse the construct, the parser queues up | |
1036 | error messages, rather than issuing them immediately, and saves the | |
1037 | tokens it consumes. If the construct is parsed successfully, the | |
1038 | parser "commits", i.e., it issues any queued error messages and | |
1039 | the tokens that were being preserved are permanently discarded. | |
1040 | If, however, the construct is not parsed successfully, the parser | |
1041 | rolls back its state completely so that it can resume parsing using | |
1042 | a different alternative. | |
1043 | ||
1044 | Future Improvements | |
1045 | ------------------- | |
21526606 | 1046 | |
a723baf1 MM |
1047 | The performance of the parser could probably be improved |
1048 | substantially. Some possible improvements include: | |
1049 | ||
1050 | - The expression parser recurses through the various levels of | |
1051 | precedence as specified in the grammar, rather than using an | |
1052 | operator-precedence technique. Therefore, parsing a simple | |
1053 | identifier requires multiple recursive calls. | |
1054 | ||
1055 | - We could often eliminate the need to parse tentatively by | |
1056 | looking ahead a little bit. In some places, this approach | |
1057 | might not entirely eliminate the need to parse tentatively, but | |
1058 | it might still speed up the average case. */ | |
1059 | ||
1060 | /* Flags that are passed to some parsing functions. These values can | |
1061 | be bitwise-ored together. */ | |
1062 | ||
1063 | typedef enum cp_parser_flags | |
1064 | { | |
1065 | /* No flags. */ | |
1066 | CP_PARSER_FLAGS_NONE = 0x0, | |
1067 | /* The construct is optional. If it is not present, then no error | |
1068 | should be issued. */ | |
1069 | CP_PARSER_FLAGS_OPTIONAL = 0x1, | |
1070 | /* When parsing a type-specifier, do not allow user-defined types. */ | |
1071 | CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2 | |
1072 | } cp_parser_flags; | |
1073 | ||
62b8a44e NS |
1074 | /* The different kinds of declarators we want to parse. */ |
1075 | ||
1076 | typedef enum cp_parser_declarator_kind | |
1077 | { | |
852dcbdd | 1078 | /* We want an abstract declarator. */ |
62b8a44e NS |
1079 | CP_PARSER_DECLARATOR_ABSTRACT, |
1080 | /* We want a named declarator. */ | |
1081 | CP_PARSER_DECLARATOR_NAMED, | |
04c06002 | 1082 | /* We don't mind, but the name must be an unqualified-id. */ |
62b8a44e NS |
1083 | CP_PARSER_DECLARATOR_EITHER |
1084 | } cp_parser_declarator_kind; | |
1085 | ||
a723baf1 MM |
1086 | /* A mapping from a token type to a corresponding tree node type. */ |
1087 | ||
1088 | typedef struct cp_parser_token_tree_map_node | |
1089 | { | |
1090 | /* The token type. */ | |
df2b750f | 1091 | ENUM_BITFIELD (cpp_ttype) token_type : 8; |
a723baf1 | 1092 | /* The corresponding tree code. */ |
df2b750f | 1093 | ENUM_BITFIELD (tree_code) tree_type : 8; |
a723baf1 MM |
1094 | } cp_parser_token_tree_map_node; |
1095 | ||
1096 | /* A complete map consists of several ordinary entries, followed by a | |
1097 | terminator. The terminating entry has a token_type of CPP_EOF. */ | |
1098 | ||
1099 | typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[]; | |
1100 | ||
1101 | /* The status of a tentative parse. */ | |
1102 | ||
1103 | typedef enum cp_parser_status_kind | |
1104 | { | |
1105 | /* No errors have occurred. */ | |
1106 | CP_PARSER_STATUS_KIND_NO_ERROR, | |
1107 | /* An error has occurred. */ | |
1108 | CP_PARSER_STATUS_KIND_ERROR, | |
1109 | /* We are committed to this tentative parse, whether or not an error | |
1110 | has occurred. */ | |
1111 | CP_PARSER_STATUS_KIND_COMMITTED | |
1112 | } cp_parser_status_kind; | |
1113 | ||
1114 | /* Context that is saved and restored when parsing tentatively. */ | |
1115 | ||
1116 | typedef struct cp_parser_context GTY (()) | |
1117 | { | |
1118 | /* If this is a tentative parsing context, the status of the | |
1119 | tentative parse. */ | |
1120 | enum cp_parser_status_kind status; | |
1121 | /* If non-NULL, we have just seen a `x->' or `x.' expression. Names | |
1122 | that are looked up in this context must be looked up both in the | |
1123 | scope given by OBJECT_TYPE (the type of `x' or `*x') and also in | |
1124 | the context of the containing expression. */ | |
1125 | tree object_type; | |
a723baf1 MM |
1126 | /* The next parsing context in the stack. */ |
1127 | struct cp_parser_context *next; | |
1128 | } cp_parser_context; | |
1129 | ||
1130 | /* Prototypes. */ | |
1131 | ||
1132 | /* Constructors and destructors. */ | |
1133 | ||
1134 | static cp_parser_context *cp_parser_context_new | |
94edc4ab | 1135 | (cp_parser_context *); |
a723baf1 | 1136 | |
e5976695 MM |
1137 | /* Class variables. */ |
1138 | ||
1431042e | 1139 | static GTY((deletable)) cp_parser_context* cp_parser_context_free_list; |
e5976695 | 1140 | |
a723baf1 MM |
1141 | /* Constructors and destructors. */ |
1142 | ||
1143 | /* Construct a new context. The context below this one on the stack | |
1144 | is given by NEXT. */ | |
1145 | ||
1146 | static cp_parser_context * | |
94edc4ab | 1147 | cp_parser_context_new (cp_parser_context* next) |
a723baf1 MM |
1148 | { |
1149 | cp_parser_context *context; | |
1150 | ||
1151 | /* Allocate the storage. */ | |
e5976695 MM |
1152 | if (cp_parser_context_free_list != NULL) |
1153 | { | |
1154 | /* Pull the first entry from the free list. */ | |
1155 | context = cp_parser_context_free_list; | |
1156 | cp_parser_context_free_list = context->next; | |
c68b0a84 | 1157 | memset (context, 0, sizeof (*context)); |
e5976695 MM |
1158 | } |
1159 | else | |
c68b0a84 | 1160 | context = ggc_alloc_cleared (sizeof (cp_parser_context)); |
a723baf1 MM |
1161 | /* No errors have occurred yet in this context. */ |
1162 | context->status = CP_PARSER_STATUS_KIND_NO_ERROR; | |
1163 | /* If this is not the bottomost context, copy information that we | |
1164 | need from the previous context. */ | |
1165 | if (next) | |
1166 | { | |
1167 | /* If, in the NEXT context, we are parsing an `x->' or `x.' | |
1168 | expression, then we are parsing one in this context, too. */ | |
1169 | context->object_type = next->object_type; | |
a723baf1 MM |
1170 | /* Thread the stack. */ |
1171 | context->next = next; | |
1172 | } | |
1173 | ||
1174 | return context; | |
1175 | } | |
1176 | ||
1177 | /* The cp_parser structure represents the C++ parser. */ | |
1178 | ||
1179 | typedef struct cp_parser GTY(()) | |
1180 | { | |
1181 | /* The lexer from which we are obtaining tokens. */ | |
1182 | cp_lexer *lexer; | |
1183 | ||
1184 | /* The scope in which names should be looked up. If NULL_TREE, then | |
1185 | we look up names in the scope that is currently open in the | |
1186 | source program. If non-NULL, this is either a TYPE or | |
21526606 | 1187 | NAMESPACE_DECL for the scope in which we should look. |
a723baf1 MM |
1188 | |
1189 | This value is not cleared automatically after a name is looked | |
1190 | up, so we must be careful to clear it before starting a new look | |
1191 | up sequence. (If it is not cleared, then `X::Y' followed by `Z' | |
1192 | will look up `Z' in the scope of `X', rather than the current | |
1193 | scope.) Unfortunately, it is difficult to tell when name lookup | |
1194 | is complete, because we sometimes peek at a token, look it up, | |
1195 | and then decide not to consume it. */ | |
1196 | tree scope; | |
1197 | ||
1198 | /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the | |
1199 | last lookup took place. OBJECT_SCOPE is used if an expression | |
1200 | like "x->y" or "x.y" was used; it gives the type of "*x" or "x", | |
21526606 | 1201 | respectively. QUALIFYING_SCOPE is used for an expression of the |
a723baf1 MM |
1202 | form "X::Y"; it refers to X. */ |
1203 | tree object_scope; | |
1204 | tree qualifying_scope; | |
1205 | ||
1206 | /* A stack of parsing contexts. All but the bottom entry on the | |
1207 | stack will be tentative contexts. | |
1208 | ||
1209 | We parse tentatively in order to determine which construct is in | |
1210 | use in some situations. For example, in order to determine | |
1211 | whether a statement is an expression-statement or a | |
1212 | declaration-statement we parse it tentatively as a | |
1213 | declaration-statement. If that fails, we then reparse the same | |
1214 | token stream as an expression-statement. */ | |
1215 | cp_parser_context *context; | |
1216 | ||
1217 | /* True if we are parsing GNU C++. If this flag is not set, then | |
1218 | GNU extensions are not recognized. */ | |
1219 | bool allow_gnu_extensions_p; | |
1220 | ||
1221 | /* TRUE if the `>' token should be interpreted as the greater-than | |
1222 | operator. FALSE if it is the end of a template-id or | |
1223 | template-parameter-list. */ | |
1224 | bool greater_than_is_operator_p; | |
1225 | ||
1226 | /* TRUE if default arguments are allowed within a parameter list | |
1227 | that starts at this point. FALSE if only a gnu extension makes | |
cd0be382 | 1228 | them permissible. */ |
a723baf1 | 1229 | bool default_arg_ok_p; |
21526606 | 1230 | |
a723baf1 MM |
1231 | /* TRUE if we are parsing an integral constant-expression. See |
1232 | [expr.const] for a precise definition. */ | |
67c03833 | 1233 | bool integral_constant_expression_p; |
a723baf1 | 1234 | |
14d22dd6 MM |
1235 | /* TRUE if we are parsing an integral constant-expression -- but a |
1236 | non-constant expression should be permitted as well. This flag | |
1237 | is used when parsing an array bound so that GNU variable-length | |
1238 | arrays are tolerated. */ | |
67c03833 | 1239 | bool allow_non_integral_constant_expression_p; |
14d22dd6 MM |
1240 | |
1241 | /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has | |
1242 | been seen that makes the expression non-constant. */ | |
67c03833 | 1243 | bool non_integral_constant_expression_p; |
14d22dd6 | 1244 | |
a723baf1 MM |
1245 | /* TRUE if local variable names and `this' are forbidden in the |
1246 | current context. */ | |
1247 | bool local_variables_forbidden_p; | |
1248 | ||
1249 | /* TRUE if the declaration we are parsing is part of a | |
1250 | linkage-specification of the form `extern string-literal | |
1251 | declaration'. */ | |
1252 | bool in_unbraced_linkage_specification_p; | |
1253 | ||
1254 | /* TRUE if we are presently parsing a declarator, after the | |
1255 | direct-declarator. */ | |
1256 | bool in_declarator_p; | |
1257 | ||
4bb8ca28 MM |
1258 | /* TRUE if we are presently parsing a template-argument-list. */ |
1259 | bool in_template_argument_list_p; | |
1260 | ||
0e59b3fb MM |
1261 | /* TRUE if we are presently parsing the body of an |
1262 | iteration-statement. */ | |
1263 | bool in_iteration_statement_p; | |
1264 | ||
1265 | /* TRUE if we are presently parsing the body of a switch | |
1266 | statement. */ | |
1267 | bool in_switch_statement_p; | |
1268 | ||
4f8163b1 MM |
1269 | /* TRUE if we are parsing a type-id in an expression context. In |
1270 | such a situation, both "type (expr)" and "type (type)" are valid | |
1271 | alternatives. */ | |
1272 | bool in_type_id_in_expr_p; | |
1273 | ||
a723baf1 MM |
1274 | /* If non-NULL, then we are parsing a construct where new type |
1275 | definitions are not permitted. The string stored here will be | |
1276 | issued as an error message if a type is defined. */ | |
1277 | const char *type_definition_forbidden_message; | |
1278 | ||
8db1028e NS |
1279 | /* A list of lists. The outer list is a stack, used for member |
1280 | functions of local classes. At each level there are two sub-list, | |
1281 | one on TREE_VALUE and one on TREE_PURPOSE. Each of those | |
1282 | sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their | |
1283 | TREE_VALUE's. The functions are chained in reverse declaration | |
1284 | order. | |
1285 | ||
1286 | The TREE_PURPOSE sublist contains those functions with default | |
1287 | arguments that need post processing, and the TREE_VALUE sublist | |
1288 | contains those functions with definitions that need post | |
1289 | processing. | |
1290 | ||
1291 | These lists can only be processed once the outermost class being | |
9bcb9aae | 1292 | defined is complete. */ |
a723baf1 MM |
1293 | tree unparsed_functions_queues; |
1294 | ||
1295 | /* The number of classes whose definitions are currently in | |
1296 | progress. */ | |
1297 | unsigned num_classes_being_defined; | |
1298 | ||
1299 | /* The number of template parameter lists that apply directly to the | |
1300 | current declaration. */ | |
1301 | unsigned num_template_parameter_lists; | |
1302 | } cp_parser; | |
1303 | ||
04c06002 | 1304 | /* The type of a function that parses some kind of expression. */ |
94edc4ab | 1305 | typedef tree (*cp_parser_expression_fn) (cp_parser *); |
a723baf1 MM |
1306 | |
1307 | /* Prototypes. */ | |
1308 | ||
1309 | /* Constructors and destructors. */ | |
1310 | ||
1311 | static cp_parser *cp_parser_new | |
94edc4ab | 1312 | (void); |
a723baf1 | 1313 | |
21526606 | 1314 | /* Routines to parse various constructs. |
a723baf1 MM |
1315 | |
1316 | Those that return `tree' will return the error_mark_node (rather | |
1317 | than NULL_TREE) if a parse error occurs, unless otherwise noted. | |
1318 | Sometimes, they will return an ordinary node if error-recovery was | |
34cd5ae7 | 1319 | attempted, even though a parse error occurred. So, to check |
a723baf1 MM |
1320 | whether or not a parse error occurred, you should always use |
1321 | cp_parser_error_occurred. If the construct is optional (indicated | |
1322 | either by an `_opt' in the name of the function that does the | |
1323 | parsing or via a FLAGS parameter), then NULL_TREE is returned if | |
1324 | the construct is not present. */ | |
1325 | ||
1326 | /* Lexical conventions [gram.lex] */ | |
1327 | ||
1328 | static tree cp_parser_identifier | |
94edc4ab | 1329 | (cp_parser *); |
a723baf1 MM |
1330 | |
1331 | /* Basic concepts [gram.basic] */ | |
1332 | ||
1333 | static bool cp_parser_translation_unit | |
94edc4ab | 1334 | (cp_parser *); |
a723baf1 MM |
1335 | |
1336 | /* Expressions [gram.expr] */ | |
1337 | ||
1338 | static tree cp_parser_primary_expression | |
b3445994 | 1339 | (cp_parser *, cp_id_kind *, tree *); |
a723baf1 | 1340 | static tree cp_parser_id_expression |
f3c2dfc6 | 1341 | (cp_parser *, bool, bool, bool *, bool); |
a723baf1 | 1342 | static tree cp_parser_unqualified_id |
f3c2dfc6 | 1343 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1344 | static tree cp_parser_nested_name_specifier_opt |
a668c6ad | 1345 | (cp_parser *, bool, bool, bool, bool); |
a723baf1 | 1346 | static tree cp_parser_nested_name_specifier |
a723baf1 | 1347 | (cp_parser *, bool, bool, bool, bool); |
a668c6ad MM |
1348 | static tree cp_parser_class_or_namespace_name |
1349 | (cp_parser *, bool, bool, bool, bool, bool); | |
a723baf1 MM |
1350 | static tree cp_parser_postfix_expression |
1351 | (cp_parser *, bool); | |
7a3ea201 RH |
1352 | static tree cp_parser_postfix_open_square_expression |
1353 | (cp_parser *, tree, bool); | |
1354 | static tree cp_parser_postfix_dot_deref_expression | |
1355 | (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *); | |
7efa3e22 | 1356 | static tree cp_parser_parenthesized_expression_list |
39703eb9 | 1357 | (cp_parser *, bool, bool *); |
a723baf1 | 1358 | static void cp_parser_pseudo_destructor_name |
94edc4ab | 1359 | (cp_parser *, tree *, tree *); |
a723baf1 MM |
1360 | static tree cp_parser_unary_expression |
1361 | (cp_parser *, bool); | |
1362 | static enum tree_code cp_parser_unary_operator | |
94edc4ab | 1363 | (cp_token *); |
a723baf1 | 1364 | static tree cp_parser_new_expression |
94edc4ab | 1365 | (cp_parser *); |
a723baf1 | 1366 | static tree cp_parser_new_placement |
94edc4ab | 1367 | (cp_parser *); |
a723baf1 | 1368 | static tree cp_parser_new_type_id |
94edc4ab | 1369 | (cp_parser *); |
a723baf1 | 1370 | static tree cp_parser_new_declarator_opt |
94edc4ab | 1371 | (cp_parser *); |
a723baf1 | 1372 | static tree cp_parser_direct_new_declarator |
94edc4ab | 1373 | (cp_parser *); |
a723baf1 | 1374 | static tree cp_parser_new_initializer |
94edc4ab | 1375 | (cp_parser *); |
a723baf1 | 1376 | static tree cp_parser_delete_expression |
94edc4ab | 1377 | (cp_parser *); |
21526606 | 1378 | static tree cp_parser_cast_expression |
a723baf1 MM |
1379 | (cp_parser *, bool); |
1380 | static tree cp_parser_pm_expression | |
94edc4ab | 1381 | (cp_parser *); |
a723baf1 | 1382 | static tree cp_parser_multiplicative_expression |
94edc4ab | 1383 | (cp_parser *); |
a723baf1 | 1384 | static tree cp_parser_additive_expression |
94edc4ab | 1385 | (cp_parser *); |
a723baf1 | 1386 | static tree cp_parser_shift_expression |
94edc4ab | 1387 | (cp_parser *); |
a723baf1 | 1388 | static tree cp_parser_relational_expression |
94edc4ab | 1389 | (cp_parser *); |
a723baf1 | 1390 | static tree cp_parser_equality_expression |
94edc4ab | 1391 | (cp_parser *); |
a723baf1 | 1392 | static tree cp_parser_and_expression |
94edc4ab | 1393 | (cp_parser *); |
a723baf1 | 1394 | static tree cp_parser_exclusive_or_expression |
94edc4ab | 1395 | (cp_parser *); |
a723baf1 | 1396 | static tree cp_parser_inclusive_or_expression |
94edc4ab | 1397 | (cp_parser *); |
a723baf1 | 1398 | static tree cp_parser_logical_and_expression |
94edc4ab | 1399 | (cp_parser *); |
21526606 | 1400 | static tree cp_parser_logical_or_expression |
94edc4ab | 1401 | (cp_parser *); |
a723baf1 | 1402 | static tree cp_parser_question_colon_clause |
94edc4ab | 1403 | (cp_parser *, tree); |
a723baf1 | 1404 | static tree cp_parser_assignment_expression |
94edc4ab | 1405 | (cp_parser *); |
a723baf1 | 1406 | static enum tree_code cp_parser_assignment_operator_opt |
94edc4ab | 1407 | (cp_parser *); |
a723baf1 | 1408 | static tree cp_parser_expression |
94edc4ab | 1409 | (cp_parser *); |
a723baf1 | 1410 | static tree cp_parser_constant_expression |
14d22dd6 | 1411 | (cp_parser *, bool, bool *); |
7a3ea201 RH |
1412 | static tree cp_parser_builtin_offsetof |
1413 | (cp_parser *); | |
a723baf1 MM |
1414 | |
1415 | /* Statements [gram.stmt.stmt] */ | |
1416 | ||
1417 | static void cp_parser_statement | |
a5bcc582 | 1418 | (cp_parser *, bool); |
a723baf1 | 1419 | static tree cp_parser_labeled_statement |
a5bcc582 | 1420 | (cp_parser *, bool); |
a723baf1 | 1421 | static tree cp_parser_expression_statement |
a5bcc582 | 1422 | (cp_parser *, bool); |
a723baf1 | 1423 | static tree cp_parser_compound_statement |
a5bcc582 | 1424 | (cp_parser *, bool); |
a723baf1 | 1425 | static void cp_parser_statement_seq_opt |
a5bcc582 | 1426 | (cp_parser *, bool); |
a723baf1 | 1427 | static tree cp_parser_selection_statement |
94edc4ab | 1428 | (cp_parser *); |
a723baf1 | 1429 | static tree cp_parser_condition |
94edc4ab | 1430 | (cp_parser *); |
a723baf1 | 1431 | static tree cp_parser_iteration_statement |
94edc4ab | 1432 | (cp_parser *); |
a723baf1 | 1433 | static void cp_parser_for_init_statement |
94edc4ab | 1434 | (cp_parser *); |
a723baf1 | 1435 | static tree cp_parser_jump_statement |
94edc4ab | 1436 | (cp_parser *); |
a723baf1 | 1437 | static void cp_parser_declaration_statement |
94edc4ab | 1438 | (cp_parser *); |
a723baf1 MM |
1439 | |
1440 | static tree cp_parser_implicitly_scoped_statement | |
94edc4ab | 1441 | (cp_parser *); |
a723baf1 | 1442 | static void cp_parser_already_scoped_statement |
94edc4ab | 1443 | (cp_parser *); |
a723baf1 MM |
1444 | |
1445 | /* Declarations [gram.dcl.dcl] */ | |
1446 | ||
1447 | static void cp_parser_declaration_seq_opt | |
94edc4ab | 1448 | (cp_parser *); |
a723baf1 | 1449 | static void cp_parser_declaration |
94edc4ab | 1450 | (cp_parser *); |
a723baf1 | 1451 | static void cp_parser_block_declaration |
94edc4ab | 1452 | (cp_parser *, bool); |
a723baf1 | 1453 | static void cp_parser_simple_declaration |
94edc4ab | 1454 | (cp_parser *, bool); |
21526606 | 1455 | static tree cp_parser_decl_specifier_seq |
560ad596 | 1456 | (cp_parser *, cp_parser_flags, tree *, int *); |
a723baf1 | 1457 | static tree cp_parser_storage_class_specifier_opt |
94edc4ab | 1458 | (cp_parser *); |
a723baf1 | 1459 | static tree cp_parser_function_specifier_opt |
94edc4ab | 1460 | (cp_parser *); |
a723baf1 | 1461 | static tree cp_parser_type_specifier |
560ad596 | 1462 | (cp_parser *, cp_parser_flags, bool, bool, int *, bool *); |
a723baf1 | 1463 | static tree cp_parser_simple_type_specifier |
4b0d3cbe | 1464 | (cp_parser *, cp_parser_flags, bool); |
a723baf1 | 1465 | static tree cp_parser_type_name |
94edc4ab | 1466 | (cp_parser *); |
a723baf1 | 1467 | static tree cp_parser_elaborated_type_specifier |
94edc4ab | 1468 | (cp_parser *, bool, bool); |
a723baf1 | 1469 | static tree cp_parser_enum_specifier |
94edc4ab | 1470 | (cp_parser *); |
a723baf1 | 1471 | static void cp_parser_enumerator_list |
94edc4ab | 1472 | (cp_parser *, tree); |
21526606 | 1473 | static void cp_parser_enumerator_definition |
94edc4ab | 1474 | (cp_parser *, tree); |
a723baf1 | 1475 | static tree cp_parser_namespace_name |
94edc4ab | 1476 | (cp_parser *); |
a723baf1 | 1477 | static void cp_parser_namespace_definition |
94edc4ab | 1478 | (cp_parser *); |
a723baf1 | 1479 | static void cp_parser_namespace_body |
94edc4ab | 1480 | (cp_parser *); |
a723baf1 | 1481 | static tree cp_parser_qualified_namespace_specifier |
94edc4ab | 1482 | (cp_parser *); |
a723baf1 | 1483 | static void cp_parser_namespace_alias_definition |
94edc4ab | 1484 | (cp_parser *); |
a723baf1 | 1485 | static void cp_parser_using_declaration |
94edc4ab | 1486 | (cp_parser *); |
a723baf1 | 1487 | static void cp_parser_using_directive |
94edc4ab | 1488 | (cp_parser *); |
a723baf1 | 1489 | static void cp_parser_asm_definition |
94edc4ab | 1490 | (cp_parser *); |
a723baf1 | 1491 | static void cp_parser_linkage_specification |
94edc4ab | 1492 | (cp_parser *); |
a723baf1 MM |
1493 | |
1494 | /* Declarators [gram.dcl.decl] */ | |
1495 | ||
1496 | static tree cp_parser_init_declarator | |
560ad596 | 1497 | (cp_parser *, tree, tree, bool, bool, int, bool *); |
a723baf1 | 1498 | static tree cp_parser_declarator |
4bb8ca28 | 1499 | (cp_parser *, cp_parser_declarator_kind, int *, bool *); |
a723baf1 | 1500 | static tree cp_parser_direct_declarator |
7efa3e22 | 1501 | (cp_parser *, cp_parser_declarator_kind, int *); |
a723baf1 | 1502 | static enum tree_code cp_parser_ptr_operator |
94edc4ab | 1503 | (cp_parser *, tree *, tree *); |
a723baf1 | 1504 | static tree cp_parser_cv_qualifier_seq_opt |
94edc4ab | 1505 | (cp_parser *); |
a723baf1 | 1506 | static tree cp_parser_cv_qualifier_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 *); |
a723baf1 | 1512 | static tree cp_parser_type_specifier_seq |
94edc4ab | 1513 | (cp_parser *); |
a723baf1 | 1514 | static tree cp_parser_parameter_declaration_clause |
94edc4ab | 1515 | (cp_parser *); |
a723baf1 | 1516 | static tree cp_parser_parameter_declaration_list |
94edc4ab | 1517 | (cp_parser *); |
a723baf1 | 1518 | static tree 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 | |
a668c6ad | 1535 | (cp_parser *, bool, bool, bool, 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 *); |
a723baf1 | 1564 | static tree 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 |
94edc4ab | 1589 | (cp_parser *); |
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 | |
b0bc6e8e | 1644 | (cp_parser *, tree, 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 | |
94edc4ab | 1650 | (cp_parser *, tree); |
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_binary_expression |
94edc4ab | 1656 | (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn); |
a723baf1 | 1657 | static tree cp_parser_global_scope_opt |
94edc4ab | 1658 | (cp_parser *, bool); |
a723baf1 MM |
1659 | static bool cp_parser_constructor_declarator_p |
1660 | (cp_parser *, bool); | |
1661 | static tree cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab | 1662 | (cp_parser *, tree, tree, tree); |
a723baf1 | 1663 | static tree cp_parser_function_definition_after_declarator |
94edc4ab | 1664 | (cp_parser *, bool); |
a723baf1 | 1665 | static void cp_parser_template_declaration_after_export |
94edc4ab | 1666 | (cp_parser *, bool); |
a723baf1 | 1667 | static tree cp_parser_single_declaration |
94edc4ab | 1668 | (cp_parser *, bool, bool *); |
a723baf1 | 1669 | static tree cp_parser_functional_cast |
94edc4ab | 1670 | (cp_parser *, tree); |
4bb8ca28 MM |
1671 | static tree cp_parser_save_member_function_body |
1672 | (cp_parser *, tree, tree, tree); | |
ec75414f MM |
1673 | static tree cp_parser_enclosed_template_argument_list |
1674 | (cp_parser *); | |
8db1028e NS |
1675 | static void cp_parser_save_default_args |
1676 | (cp_parser *, tree); | |
a723baf1 | 1677 | static void cp_parser_late_parsing_for_member |
94edc4ab | 1678 | (cp_parser *, tree); |
a723baf1 | 1679 | static void cp_parser_late_parsing_default_args |
8218bd34 | 1680 | (cp_parser *, tree); |
a723baf1 | 1681 | static tree cp_parser_sizeof_operand |
94edc4ab | 1682 | (cp_parser *, enum rid); |
a723baf1 | 1683 | static bool cp_parser_declares_only_class_p |
94edc4ab | 1684 | (cp_parser *); |
a723baf1 | 1685 | static bool cp_parser_friend_p |
94edc4ab | 1686 | (tree); |
a723baf1 | 1687 | static cp_token *cp_parser_require |
94edc4ab | 1688 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1689 | static cp_token *cp_parser_require_keyword |
94edc4ab | 1690 | (cp_parser *, enum rid, const char *); |
21526606 | 1691 | static bool cp_parser_token_starts_function_definition_p |
94edc4ab | 1692 | (cp_token *); |
a723baf1 MM |
1693 | static bool cp_parser_next_token_starts_class_definition_p |
1694 | (cp_parser *); | |
d17811fd MM |
1695 | static bool cp_parser_next_token_ends_template_argument_p |
1696 | (cp_parser *); | |
f4abade9 GB |
1697 | static bool cp_parser_nth_token_starts_template_argument_list_p |
1698 | (cp_parser *, size_t); | |
a723baf1 | 1699 | static enum tag_types cp_parser_token_is_class_key |
94edc4ab | 1700 | (cp_token *); |
a723baf1 MM |
1701 | static void cp_parser_check_class_key |
1702 | (enum tag_types, tree type); | |
37d407a1 KL |
1703 | static void cp_parser_check_access_in_redeclaration |
1704 | (tree type); | |
a723baf1 MM |
1705 | static bool cp_parser_optional_template_keyword |
1706 | (cp_parser *); | |
21526606 | 1707 | static void cp_parser_pre_parsed_nested_name_specifier |
2050a1bb | 1708 | (cp_parser *); |
a723baf1 MM |
1709 | static void cp_parser_cache_group |
1710 | (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned); | |
21526606 | 1711 | static void cp_parser_parse_tentatively |
94edc4ab | 1712 | (cp_parser *); |
a723baf1 | 1713 | static void cp_parser_commit_to_tentative_parse |
94edc4ab | 1714 | (cp_parser *); |
a723baf1 | 1715 | static void cp_parser_abort_tentative_parse |
94edc4ab | 1716 | (cp_parser *); |
a723baf1 | 1717 | static bool cp_parser_parse_definitely |
94edc4ab | 1718 | (cp_parser *); |
f7b5ecd9 | 1719 | static inline bool cp_parser_parsing_tentatively |
94edc4ab | 1720 | (cp_parser *); |
a723baf1 | 1721 | static bool cp_parser_committed_to_tentative_parse |
94edc4ab | 1722 | (cp_parser *); |
a723baf1 | 1723 | static void cp_parser_error |
94edc4ab | 1724 | (cp_parser *, const char *); |
4bb8ca28 MM |
1725 | static void cp_parser_name_lookup_error |
1726 | (cp_parser *, tree, tree, const char *); | |
e5976695 | 1727 | static bool cp_parser_simulate_error |
94edc4ab | 1728 | (cp_parser *); |
a723baf1 | 1729 | static void cp_parser_check_type_definition |
94edc4ab | 1730 | (cp_parser *); |
560ad596 MM |
1731 | static void cp_parser_check_for_definition_in_return_type |
1732 | (tree, int); | |
ee43dab5 MM |
1733 | static void cp_parser_check_for_invalid_template_id |
1734 | (cp_parser *, tree); | |
625cbf93 MM |
1735 | static bool cp_parser_non_integral_constant_expression |
1736 | (cp_parser *, const char *); | |
2097b5f2 GB |
1737 | static void cp_parser_diagnose_invalid_type_name |
1738 | (cp_parser *, tree, tree); | |
1739 | static bool cp_parser_parse_and_diagnose_invalid_type_name | |
8fbc5ae7 | 1740 | (cp_parser *); |
7efa3e22 | 1741 | static int cp_parser_skip_to_closing_parenthesis |
a668c6ad | 1742 | (cp_parser *, bool, bool, bool); |
a723baf1 | 1743 | static void cp_parser_skip_to_end_of_statement |
94edc4ab | 1744 | (cp_parser *); |
e0860732 MM |
1745 | static void cp_parser_consume_semicolon_at_end_of_statement |
1746 | (cp_parser *); | |
a723baf1 | 1747 | static void cp_parser_skip_to_end_of_block_or_statement |
94edc4ab | 1748 | (cp_parser *); |
a723baf1 MM |
1749 | static void cp_parser_skip_to_closing_brace |
1750 | (cp_parser *); | |
1751 | static void cp_parser_skip_until_found | |
94edc4ab | 1752 | (cp_parser *, enum cpp_ttype, const char *); |
a723baf1 | 1753 | static bool cp_parser_error_occurred |
94edc4ab | 1754 | (cp_parser *); |
a723baf1 | 1755 | static bool cp_parser_allow_gnu_extensions_p |
94edc4ab | 1756 | (cp_parser *); |
a723baf1 | 1757 | static bool cp_parser_is_string_literal |
94edc4ab | 1758 | (cp_token *); |
21526606 | 1759 | static bool cp_parser_is_keyword |
94edc4ab | 1760 | (cp_token *, enum rid); |
2097b5f2 GB |
1761 | static tree cp_parser_make_typename_type |
1762 | (cp_parser *, tree, tree); | |
a723baf1 | 1763 | |
4de8668e | 1764 | /* Returns nonzero if we are parsing tentatively. */ |
f7b5ecd9 MM |
1765 | |
1766 | static inline bool | |
94edc4ab | 1767 | cp_parser_parsing_tentatively (cp_parser* parser) |
f7b5ecd9 MM |
1768 | { |
1769 | return parser->context->next != NULL; | |
1770 | } | |
1771 | ||
4de8668e | 1772 | /* Returns nonzero if TOKEN is a string literal. */ |
a723baf1 MM |
1773 | |
1774 | static bool | |
94edc4ab | 1775 | cp_parser_is_string_literal (cp_token* token) |
a723baf1 MM |
1776 | { |
1777 | return (token->type == CPP_STRING || token->type == CPP_WSTRING); | |
1778 | } | |
1779 | ||
4de8668e | 1780 | /* Returns nonzero if TOKEN is the indicated KEYWORD. */ |
a723baf1 MM |
1781 | |
1782 | static bool | |
94edc4ab | 1783 | cp_parser_is_keyword (cp_token* token, enum rid keyword) |
a723baf1 MM |
1784 | { |
1785 | return token->keyword == keyword; | |
1786 | } | |
1787 | ||
a723baf1 MM |
1788 | /* Issue the indicated error MESSAGE. */ |
1789 | ||
1790 | static void | |
94edc4ab | 1791 | cp_parser_error (cp_parser* parser, const char* message) |
a723baf1 | 1792 | { |
a723baf1 | 1793 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
e5976695 | 1794 | if (!cp_parser_simulate_error (parser)) |
4bb8ca28 MM |
1795 | { |
1796 | cp_token *token; | |
1797 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 1798 | c_parse_error (message, |
5c832178 MM |
1799 | /* Because c_parser_error does not understand |
1800 | CPP_KEYWORD, keywords are treated like | |
1801 | identifiers. */ | |
21526606 | 1802 | (token->type == CPP_KEYWORD ? CPP_NAME : token->type), |
5c832178 | 1803 | token->value); |
4bb8ca28 MM |
1804 | } |
1805 | } | |
1806 | ||
1807 | /* Issue an error about name-lookup failing. NAME is the | |
1808 | IDENTIFIER_NODE DECL is the result of | |
1809 | the lookup (as returned from cp_parser_lookup_name). DESIRED is | |
1810 | the thing that we hoped to find. */ | |
1811 | ||
1812 | static void | |
1813 | cp_parser_name_lookup_error (cp_parser* parser, | |
1814 | tree name, | |
1815 | tree decl, | |
1816 | const char* desired) | |
1817 | { | |
1818 | /* If name lookup completely failed, tell the user that NAME was not | |
1819 | declared. */ | |
1820 | if (decl == error_mark_node) | |
1821 | { | |
1822 | if (parser->scope && parser->scope != global_namespace) | |
21526606 | 1823 | error ("`%D::%D' has not been declared", |
4bb8ca28 MM |
1824 | parser->scope, name); |
1825 | else if (parser->scope == global_namespace) | |
1826 | error ("`::%D' has not been declared", name); | |
1827 | else | |
1828 | error ("`%D' has not been declared", name); | |
1829 | } | |
1830 | else if (parser->scope && parser->scope != global_namespace) | |
1831 | error ("`%D::%D' %s", parser->scope, name, desired); | |
1832 | else if (parser->scope == global_namespace) | |
1833 | error ("`::%D' %s", name, desired); | |
1834 | else | |
1835 | error ("`%D' %s", name, desired); | |
a723baf1 MM |
1836 | } |
1837 | ||
1838 | /* If we are parsing tentatively, remember that an error has occurred | |
e5976695 | 1839 | during this tentative parse. Returns true if the error was |
77077b39 | 1840 | simulated; false if a message should be issued by the caller. */ |
a723baf1 | 1841 | |
e5976695 | 1842 | static bool |
94edc4ab | 1843 | cp_parser_simulate_error (cp_parser* parser) |
a723baf1 MM |
1844 | { |
1845 | if (cp_parser_parsing_tentatively (parser) | |
1846 | && !cp_parser_committed_to_tentative_parse (parser)) | |
e5976695 MM |
1847 | { |
1848 | parser->context->status = CP_PARSER_STATUS_KIND_ERROR; | |
1849 | return true; | |
1850 | } | |
1851 | return false; | |
a723baf1 MM |
1852 | } |
1853 | ||
1854 | /* This function is called when a type is defined. If type | |
1855 | definitions are forbidden at this point, an error message is | |
1856 | issued. */ | |
1857 | ||
1858 | static void | |
94edc4ab | 1859 | cp_parser_check_type_definition (cp_parser* parser) |
a723baf1 MM |
1860 | { |
1861 | /* If types are forbidden here, issue a message. */ | |
1862 | if (parser->type_definition_forbidden_message) | |
1863 | /* Use `%s' to print the string in case there are any escape | |
1864 | characters in the message. */ | |
1865 | error ("%s", parser->type_definition_forbidden_message); | |
1866 | } | |
1867 | ||
560ad596 MM |
1868 | /* This function is called when a declaration is parsed. If |
1869 | DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM | |
1870 | indicates that a type was defined in the decl-specifiers for DECL, | |
1871 | then an error is issued. */ | |
1872 | ||
1873 | static void | |
21526606 | 1874 | cp_parser_check_for_definition_in_return_type (tree declarator, |
560ad596 MM |
1875 | int declares_class_or_enum) |
1876 | { | |
1877 | /* [dcl.fct] forbids type definitions in return types. | |
1878 | Unfortunately, it's not easy to know whether or not we are | |
1879 | processing a return type until after the fact. */ | |
1880 | while (declarator | |
1881 | && (TREE_CODE (declarator) == INDIRECT_REF | |
1882 | || TREE_CODE (declarator) == ADDR_EXPR)) | |
1883 | declarator = TREE_OPERAND (declarator, 0); | |
1884 | if (declarator | |
21526606 | 1885 | && TREE_CODE (declarator) == CALL_EXPR |
560ad596 MM |
1886 | && declares_class_or_enum & 2) |
1887 | error ("new types may not be defined in a return type"); | |
1888 | } | |
1889 | ||
ee43dab5 MM |
1890 | /* A type-specifier (TYPE) has been parsed which cannot be followed by |
1891 | "<" in any valid C++ program. If the next token is indeed "<", | |
1892 | issue a message warning the user about what appears to be an | |
1893 | invalid attempt to form a template-id. */ | |
1894 | ||
1895 | static void | |
21526606 | 1896 | cp_parser_check_for_invalid_template_id (cp_parser* parser, |
ee43dab5 MM |
1897 | tree type) |
1898 | { | |
1899 | ptrdiff_t start; | |
1900 | cp_token *token; | |
1901 | ||
1902 | if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) | |
1903 | { | |
1904 | if (TYPE_P (type)) | |
1905 | error ("`%T' is not a template", type); | |
1906 | else if (TREE_CODE (type) == IDENTIFIER_NODE) | |
4460cef2 | 1907 | error ("`%E' is not a template", type); |
ee43dab5 MM |
1908 | else |
1909 | error ("invalid template-id"); | |
1910 | /* Remember the location of the invalid "<". */ | |
1911 | if (cp_parser_parsing_tentatively (parser) | |
1912 | && !cp_parser_committed_to_tentative_parse (parser)) | |
1913 | { | |
1914 | token = cp_lexer_peek_token (parser->lexer); | |
1915 | token = cp_lexer_prev_token (parser->lexer, token); | |
1916 | start = cp_lexer_token_difference (parser->lexer, | |
1917 | parser->lexer->first_token, | |
1918 | token); | |
1919 | } | |
1920 | else | |
1921 | start = -1; | |
1922 | /* Consume the "<". */ | |
1923 | cp_lexer_consume_token (parser->lexer); | |
1924 | /* Parse the template arguments. */ | |
1925 | cp_parser_enclosed_template_argument_list (parser); | |
da1d7781 | 1926 | /* Permanently remove the invalid template arguments so that |
ee43dab5 MM |
1927 | this error message is not issued again. */ |
1928 | if (start >= 0) | |
1929 | { | |
1930 | token = cp_lexer_advance_token (parser->lexer, | |
1931 | parser->lexer->first_token, | |
1932 | start); | |
1933 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
1934 | } | |
1935 | } | |
1936 | } | |
1937 | ||
625cbf93 MM |
1938 | /* If parsing an integral constant-expression, issue an error message |
1939 | about the fact that THING appeared and return true. Otherwise, | |
1940 | return false, marking the current expression as non-constant. */ | |
14d22dd6 | 1941 | |
625cbf93 MM |
1942 | static bool |
1943 | cp_parser_non_integral_constant_expression (cp_parser *parser, | |
1944 | const char *thing) | |
14d22dd6 | 1945 | { |
625cbf93 MM |
1946 | if (parser->integral_constant_expression_p) |
1947 | { | |
1948 | if (!parser->allow_non_integral_constant_expression_p) | |
1949 | { | |
1950 | error ("%s cannot appear in a constant-expression", thing); | |
1951 | return true; | |
1952 | } | |
1953 | parser->non_integral_constant_expression_p = true; | |
1954 | } | |
1955 | return false; | |
14d22dd6 MM |
1956 | } |
1957 | ||
2097b5f2 | 1958 | /* Emit a diagnostic for an invalid type name. Consider also if it is |
21526606 | 1959 | qualified or not and the result of a lookup, to provide a better |
2097b5f2 | 1960 | message. */ |
8fbc5ae7 | 1961 | |
2097b5f2 GB |
1962 | static void |
1963 | cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
1964 | { |
1965 | tree decl, old_scope; | |
2097b5f2 GB |
1966 | /* Try to lookup the identifier. */ |
1967 | old_scope = parser->scope; | |
1968 | parser->scope = scope; | |
1969 | decl = cp_parser_lookup_name_simple (parser, id); | |
1970 | parser->scope = old_scope; | |
1971 | /* If the lookup found a template-name, it means that the user forgot | |
1972 | to specify an argument list. Emit an useful error message. */ | |
1973 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
6c0cc713 GB |
1974 | error ("invalid use of template-name `%E' without an argument list", |
1975 | decl); | |
2097b5f2 | 1976 | else if (!parser->scope) |
8fbc5ae7 | 1977 | { |
8fbc5ae7 | 1978 | /* Issue an error message. */ |
2097b5f2 | 1979 | error ("`%E' does not name a type", id); |
8fbc5ae7 MM |
1980 | /* If we're in a template class, it's possible that the user was |
1981 | referring to a type from a base class. For example: | |
1982 | ||
1983 | template <typename T> struct A { typedef T X; }; | |
1984 | template <typename T> struct B : public A<T> { X x; }; | |
1985 | ||
1986 | The user should have said "typename A<T>::X". */ | |
1987 | if (processing_template_decl && current_class_type) | |
1988 | { | |
1989 | tree b; | |
1990 | ||
1991 | for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); | |
1992 | b; | |
1993 | b = TREE_CHAIN (b)) | |
1994 | { | |
1995 | tree base_type = BINFO_TYPE (b); | |
21526606 | 1996 | if (CLASS_TYPE_P (base_type) |
1fb3244a | 1997 | && dependent_type_p (base_type)) |
8fbc5ae7 MM |
1998 | { |
1999 | tree field; | |
2000 | /* Go from a particular instantiation of the | |
2001 | template (which will have an empty TYPE_FIELDs), | |
2002 | to the main version. */ | |
353b4fc0 | 2003 | base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); |
8fbc5ae7 MM |
2004 | for (field = TYPE_FIELDS (base_type); |
2005 | field; | |
2006 | field = TREE_CHAIN (field)) | |
2007 | if (TREE_CODE (field) == TYPE_DECL | |
2097b5f2 | 2008 | && DECL_NAME (field) == id) |
8fbc5ae7 | 2009 | { |
2097b5f2 GB |
2010 | inform ("(perhaps `typename %T::%E' was intended)", |
2011 | BINFO_TYPE (b), id); | |
8fbc5ae7 MM |
2012 | break; |
2013 | } | |
2014 | if (field) | |
2015 | break; | |
2016 | } | |
2017 | } | |
2018 | } | |
8fbc5ae7 | 2019 | } |
2097b5f2 GB |
2020 | /* Here we diagnose qualified-ids where the scope is actually correct, |
2021 | but the identifier does not resolve to a valid type name. */ | |
21526606 | 2022 | else |
2097b5f2 GB |
2023 | { |
2024 | if (TREE_CODE (parser->scope) == NAMESPACE_DECL) | |
21526606 | 2025 | error ("`%E' in namespace `%E' does not name a type", |
2097b5f2 GB |
2026 | id, parser->scope); |
2027 | else if (TYPE_P (parser->scope)) | |
21526606 | 2028 | error ("`%E' in class `%T' does not name a type", |
2097b5f2 GB |
2029 | id, parser->scope); |
2030 | else | |
2031 | abort(); | |
2032 | } | |
2033 | } | |
8fbc5ae7 | 2034 | |
2097b5f2 GB |
2035 | /* Check for a common situation where a type-name should be present, |
2036 | but is not, and issue a sensible error message. Returns true if an | |
2037 | invalid type-name was detected. | |
21526606 | 2038 | |
2097b5f2 | 2039 | The situation handled by this function are variable declarations of the |
21526606 EC |
2040 | form `ID a', where `ID' is an id-expression and `a' is a plain identifier. |
2041 | Usually, `ID' should name a type, but if we got here it means that it | |
2097b5f2 GB |
2042 | does not. We try to emit the best possible error message depending on |
2043 | how exactly the id-expression looks like. | |
2044 | */ | |
2045 | ||
2046 | static bool | |
2047 | cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser) | |
2048 | { | |
2049 | tree id; | |
2050 | ||
2051 | cp_parser_parse_tentatively (parser); | |
21526606 | 2052 | id = cp_parser_id_expression (parser, |
2097b5f2 GB |
2053 | /*template_keyword_p=*/false, |
2054 | /*check_dependency_p=*/true, | |
2055 | /*template_p=*/NULL, | |
2056 | /*declarator_p=*/true); | |
2057 | /* After the id-expression, there should be a plain identifier, | |
2058 | otherwise this is not a simple variable declaration. Also, if | |
2059 | the scope is dependent, we cannot do much. */ | |
2060 | if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 2061 | || (parser->scope && TYPE_P (parser->scope) |
2097b5f2 GB |
2062 | && dependent_type_p (parser->scope))) |
2063 | { | |
2064 | cp_parser_abort_tentative_parse (parser); | |
2065 | return false; | |
2066 | } | |
2067 | if (!cp_parser_parse_definitely (parser)) | |
2068 | return false; | |
2069 | ||
2070 | /* If we got here, this cannot be a valid variable declaration, thus | |
2071 | the cp_parser_id_expression must have resolved to a plain identifier | |
2072 | node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */ | |
2073 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203); | |
2074 | /* Emit a diagnostic for the invalid type. */ | |
2075 | cp_parser_diagnose_invalid_type_name (parser, parser->scope, id); | |
2076 | /* Skip to the end of the declaration; there's no point in | |
2077 | trying to process it. */ | |
2078 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
2079 | return true; | |
8fbc5ae7 MM |
2080 | } |
2081 | ||
21526606 | 2082 | /* Consume tokens up to, and including, the next non-nested closing `)'. |
7efa3e22 NS |
2083 | Returns 1 iff we found a closing `)'. RECOVERING is true, if we |
2084 | are doing error recovery. Returns -1 if OR_COMMA is true and we | |
2085 | found an unnested comma. */ | |
a723baf1 | 2086 | |
7efa3e22 NS |
2087 | static int |
2088 | cp_parser_skip_to_closing_parenthesis (cp_parser *parser, | |
21526606 | 2089 | bool recovering, |
a668c6ad MM |
2090 | bool or_comma, |
2091 | bool consume_paren) | |
a723baf1 | 2092 | { |
7efa3e22 NS |
2093 | unsigned paren_depth = 0; |
2094 | unsigned brace_depth = 0; | |
0173bb6f AO |
2095 | int saved_c_lex_string_translate = c_lex_string_translate; |
2096 | int result; | |
a723baf1 | 2097 | |
7efa3e22 NS |
2098 | if (recovering && !or_comma && cp_parser_parsing_tentatively (parser) |
2099 | && !cp_parser_committed_to_tentative_parse (parser)) | |
2100 | return 0; | |
21526606 | 2101 | |
0173bb6f AO |
2102 | if (! recovering) |
2103 | /* If we're looking ahead, keep both translated and untranslated | |
2104 | strings. */ | |
2105 | c_lex_string_translate = -1; | |
2106 | ||
a723baf1 MM |
2107 | while (true) |
2108 | { | |
2109 | cp_token *token; | |
21526606 | 2110 | |
a723baf1 MM |
2111 | /* If we've run out of tokens, then there is no closing `)'. */ |
2112 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
0173bb6f AO |
2113 | { |
2114 | result = 0; | |
2115 | break; | |
2116 | } | |
a723baf1 | 2117 | |
a668c6ad | 2118 | token = cp_lexer_peek_token (parser->lexer); |
21526606 | 2119 | |
f4f206f4 | 2120 | /* This matches the processing in skip_to_end_of_statement. */ |
a668c6ad | 2121 | if (token->type == CPP_SEMICOLON && !brace_depth) |
0173bb6f AO |
2122 | { |
2123 | result = 0; | |
2124 | break; | |
2125 | } | |
a668c6ad MM |
2126 | if (token->type == CPP_OPEN_BRACE) |
2127 | ++brace_depth; | |
2128 | if (token->type == CPP_CLOSE_BRACE) | |
7efa3e22 | 2129 | { |
a668c6ad | 2130 | if (!brace_depth--) |
0173bb6f AO |
2131 | { |
2132 | result = 0; | |
2133 | break; | |
2134 | } | |
7efa3e22 | 2135 | } |
a668c6ad MM |
2136 | if (recovering && or_comma && token->type == CPP_COMMA |
2137 | && !brace_depth && !paren_depth) | |
0173bb6f AO |
2138 | { |
2139 | result = -1; | |
2140 | break; | |
2141 | } | |
21526606 | 2142 | |
7efa3e22 NS |
2143 | if (!brace_depth) |
2144 | { | |
2145 | /* If it is an `(', we have entered another level of nesting. */ | |
2146 | if (token->type == CPP_OPEN_PAREN) | |
2147 | ++paren_depth; | |
2148 | /* If it is a `)', then we might be done. */ | |
2149 | else if (token->type == CPP_CLOSE_PAREN && !paren_depth--) | |
a668c6ad MM |
2150 | { |
2151 | if (consume_paren) | |
2152 | cp_lexer_consume_token (parser->lexer); | |
0173bb6f AO |
2153 | { |
2154 | result = 1; | |
2155 | break; | |
2156 | } | |
a668c6ad | 2157 | } |
7efa3e22 | 2158 | } |
21526606 | 2159 | |
a668c6ad MM |
2160 | /* Consume the token. */ |
2161 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 2162 | } |
0173bb6f AO |
2163 | |
2164 | c_lex_string_translate = saved_c_lex_string_translate; | |
2165 | return result; | |
a723baf1 MM |
2166 | } |
2167 | ||
2168 | /* Consume tokens until we reach the end of the current statement. | |
2169 | Normally, that will be just before consuming a `;'. However, if a | |
2170 | non-nested `}' comes first, then we stop before consuming that. */ | |
2171 | ||
2172 | static void | |
94edc4ab | 2173 | cp_parser_skip_to_end_of_statement (cp_parser* parser) |
a723baf1 MM |
2174 | { |
2175 | unsigned nesting_depth = 0; | |
2176 | ||
2177 | while (true) | |
2178 | { | |
2179 | cp_token *token; | |
2180 | ||
2181 | /* Peek at the next token. */ | |
2182 | token = cp_lexer_peek_token (parser->lexer); | |
2183 | /* If we've run out of tokens, stop. */ | |
2184 | if (token->type == CPP_EOF) | |
2185 | break; | |
2186 | /* If the next token is a `;', we have reached the end of the | |
2187 | statement. */ | |
2188 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2189 | break; | |
2190 | /* If the next token is a non-nested `}', then we have reached | |
2191 | the end of the current block. */ | |
2192 | if (token->type == CPP_CLOSE_BRACE) | |
2193 | { | |
2194 | /* If this is a non-nested `}', stop before consuming it. | |
2195 | That way, when confronted with something like: | |
2196 | ||
21526606 | 2197 | { 3 + } |
a723baf1 MM |
2198 | |
2199 | we stop before consuming the closing `}', even though we | |
2200 | have not yet reached a `;'. */ | |
2201 | if (nesting_depth == 0) | |
2202 | break; | |
2203 | /* If it is the closing `}' for a block that we have | |
2204 | scanned, stop -- but only after consuming the token. | |
2205 | That way given: | |
2206 | ||
2207 | void f g () { ... } | |
2208 | typedef int I; | |
2209 | ||
2210 | we will stop after the body of the erroneously declared | |
2211 | function, but before consuming the following `typedef' | |
2212 | declaration. */ | |
2213 | if (--nesting_depth == 0) | |
2214 | { | |
2215 | cp_lexer_consume_token (parser->lexer); | |
2216 | break; | |
2217 | } | |
2218 | } | |
2219 | /* If it the next token is a `{', then we are entering a new | |
2220 | block. Consume the entire block. */ | |
2221 | else if (token->type == CPP_OPEN_BRACE) | |
2222 | ++nesting_depth; | |
2223 | /* Consume the token. */ | |
2224 | cp_lexer_consume_token (parser->lexer); | |
2225 | } | |
2226 | } | |
2227 | ||
e0860732 MM |
2228 | /* This function is called at the end of a statement or declaration. |
2229 | If the next token is a semicolon, it is consumed; otherwise, error | |
2230 | recovery is attempted. */ | |
2231 | ||
2232 | static void | |
2233 | cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) | |
2234 | { | |
2235 | /* Look for the trailing `;'. */ | |
2236 | if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
2237 | { | |
2238 | /* If there is additional (erroneous) input, skip to the end of | |
2239 | the statement. */ | |
2240 | cp_parser_skip_to_end_of_statement (parser); | |
2241 | /* If the next token is now a `;', consume it. */ | |
2242 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
2243 | cp_lexer_consume_token (parser->lexer); | |
2244 | } | |
2245 | } | |
2246 | ||
a723baf1 MM |
2247 | /* Skip tokens until we have consumed an entire block, or until we |
2248 | have consumed a non-nested `;'. */ | |
2249 | ||
2250 | static void | |
94edc4ab | 2251 | cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) |
a723baf1 MM |
2252 | { |
2253 | unsigned nesting_depth = 0; | |
2254 | ||
2255 | while (true) | |
2256 | { | |
2257 | cp_token *token; | |
2258 | ||
2259 | /* Peek at the next token. */ | |
2260 | token = cp_lexer_peek_token (parser->lexer); | |
2261 | /* If we've run out of tokens, stop. */ | |
2262 | if (token->type == CPP_EOF) | |
2263 | break; | |
2264 | /* If the next token is a `;', we have reached the end of the | |
2265 | statement. */ | |
2266 | if (token->type == CPP_SEMICOLON && !nesting_depth) | |
2267 | { | |
2268 | /* Consume the `;'. */ | |
2269 | cp_lexer_consume_token (parser->lexer); | |
2270 | break; | |
2271 | } | |
2272 | /* Consume the token. */ | |
2273 | token = cp_lexer_consume_token (parser->lexer); | |
2274 | /* If the next token is a non-nested `}', then we have reached | |
2275 | the end of the current block. */ | |
21526606 | 2276 | if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
2277 | && (nesting_depth == 0 || --nesting_depth == 0)) |
2278 | break; | |
2279 | /* If it the next token is a `{', then we are entering a new | |
2280 | block. Consume the entire block. */ | |
2281 | if (token->type == CPP_OPEN_BRACE) | |
2282 | ++nesting_depth; | |
2283 | } | |
2284 | } | |
2285 | ||
2286 | /* Skip tokens until a non-nested closing curly brace is the next | |
2287 | token. */ | |
2288 | ||
2289 | static void | |
2290 | cp_parser_skip_to_closing_brace (cp_parser *parser) | |
2291 | { | |
2292 | unsigned nesting_depth = 0; | |
2293 | ||
2294 | while (true) | |
2295 | { | |
2296 | cp_token *token; | |
2297 | ||
2298 | /* Peek at the next token. */ | |
2299 | token = cp_lexer_peek_token (parser->lexer); | |
2300 | /* If we've run out of tokens, stop. */ | |
2301 | if (token->type == CPP_EOF) | |
2302 | break; | |
2303 | /* If the next token is a non-nested `}', then we have reached | |
2304 | the end of the current block. */ | |
2305 | if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0) | |
2306 | break; | |
2307 | /* If it the next token is a `{', then we are entering a new | |
2308 | block. Consume the entire block. */ | |
2309 | else if (token->type == CPP_OPEN_BRACE) | |
2310 | ++nesting_depth; | |
2311 | /* Consume the token. */ | |
2312 | cp_lexer_consume_token (parser->lexer); | |
2313 | } | |
2314 | } | |
2315 | ||
2097b5f2 GB |
2316 | /* This is a simple wrapper around make_typename_type. When the id is |
2317 | an unresolved identifier node, we can provide a superior diagnostic | |
2318 | using cp_parser_diagnose_invalid_type_name. */ | |
2319 | ||
2320 | static tree | |
2321 | cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id) | |
6c0cc713 GB |
2322 | { |
2323 | tree result; | |
2324 | if (TREE_CODE (id) == IDENTIFIER_NODE) | |
2325 | { | |
2326 | result = make_typename_type (scope, id, /*complain=*/0); | |
2327 | if (result == error_mark_node) | |
2328 | cp_parser_diagnose_invalid_type_name (parser, scope, id); | |
2329 | return result; | |
2330 | } | |
2331 | return make_typename_type (scope, id, tf_error); | |
2097b5f2 GB |
2332 | } |
2333 | ||
2334 | ||
a723baf1 MM |
2335 | /* Create a new C++ parser. */ |
2336 | ||
2337 | static cp_parser * | |
94edc4ab | 2338 | cp_parser_new (void) |
a723baf1 MM |
2339 | { |
2340 | cp_parser *parser; | |
17211ab5 GK |
2341 | cp_lexer *lexer; |
2342 | ||
2343 | /* cp_lexer_new_main is called before calling ggc_alloc because | |
2344 | cp_lexer_new_main might load a PCH file. */ | |
2345 | lexer = cp_lexer_new_main (); | |
a723baf1 | 2346 | |
c68b0a84 | 2347 | parser = ggc_alloc_cleared (sizeof (cp_parser)); |
17211ab5 | 2348 | parser->lexer = lexer; |
a723baf1 MM |
2349 | parser->context = cp_parser_context_new (NULL); |
2350 | ||
2351 | /* For now, we always accept GNU extensions. */ | |
2352 | parser->allow_gnu_extensions_p = 1; | |
2353 | ||
2354 | /* The `>' token is a greater-than operator, not the end of a | |
2355 | template-id. */ | |
2356 | parser->greater_than_is_operator_p = true; | |
2357 | ||
2358 | parser->default_arg_ok_p = true; | |
21526606 | 2359 | |
a723baf1 | 2360 | /* We are not parsing a constant-expression. */ |
67c03833 JM |
2361 | parser->integral_constant_expression_p = false; |
2362 | parser->allow_non_integral_constant_expression_p = false; | |
2363 | parser->non_integral_constant_expression_p = false; | |
a723baf1 MM |
2364 | |
2365 | /* Local variable names are not forbidden. */ | |
2366 | parser->local_variables_forbidden_p = false; | |
2367 | ||
34cd5ae7 | 2368 | /* We are not processing an `extern "C"' declaration. */ |
a723baf1 MM |
2369 | parser->in_unbraced_linkage_specification_p = false; |
2370 | ||
2371 | /* We are not processing a declarator. */ | |
2372 | parser->in_declarator_p = false; | |
2373 | ||
4bb8ca28 MM |
2374 | /* We are not processing a template-argument-list. */ |
2375 | parser->in_template_argument_list_p = false; | |
2376 | ||
0e59b3fb MM |
2377 | /* We are not in an iteration statement. */ |
2378 | parser->in_iteration_statement_p = false; | |
2379 | ||
2380 | /* We are not in a switch statement. */ | |
2381 | parser->in_switch_statement_p = false; | |
2382 | ||
4f8163b1 MM |
2383 | /* We are not parsing a type-id inside an expression. */ |
2384 | parser->in_type_id_in_expr_p = false; | |
2385 | ||
a723baf1 MM |
2386 | /* The unparsed function queue is empty. */ |
2387 | parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE); | |
2388 | ||
2389 | /* There are no classes being defined. */ | |
2390 | parser->num_classes_being_defined = 0; | |
2391 | ||
2392 | /* No template parameters apply. */ | |
2393 | parser->num_template_parameter_lists = 0; | |
2394 | ||
2395 | return parser; | |
2396 | } | |
2397 | ||
2398 | /* Lexical conventions [gram.lex] */ | |
2399 | ||
2400 | /* Parse an identifier. Returns an IDENTIFIER_NODE representing the | |
2401 | identifier. */ | |
2402 | ||
21526606 | 2403 | static tree |
94edc4ab | 2404 | cp_parser_identifier (cp_parser* parser) |
a723baf1 MM |
2405 | { |
2406 | cp_token *token; | |
2407 | ||
2408 | /* Look for the identifier. */ | |
2409 | token = cp_parser_require (parser, CPP_NAME, "identifier"); | |
2410 | /* Return the value. */ | |
2411 | return token ? token->value : error_mark_node; | |
2412 | } | |
2413 | ||
2414 | /* Basic concepts [gram.basic] */ | |
2415 | ||
2416 | /* Parse a translation-unit. | |
2417 | ||
2418 | translation-unit: | |
21526606 | 2419 | declaration-seq [opt] |
a723baf1 MM |
2420 | |
2421 | Returns TRUE if all went well. */ | |
2422 | ||
2423 | static bool | |
94edc4ab | 2424 | cp_parser_translation_unit (cp_parser* parser) |
a723baf1 MM |
2425 | { |
2426 | while (true) | |
2427 | { | |
2428 | cp_parser_declaration_seq_opt (parser); | |
2429 | ||
2430 | /* If there are no tokens left then all went well. */ | |
2431 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
2432 | break; | |
21526606 | 2433 | |
a723baf1 MM |
2434 | /* Otherwise, issue an error message. */ |
2435 | cp_parser_error (parser, "expected declaration"); | |
2436 | return false; | |
2437 | } | |
2438 | ||
2439 | /* Consume the EOF token. */ | |
2440 | cp_parser_require (parser, CPP_EOF, "end-of-file"); | |
21526606 | 2441 | |
a723baf1 MM |
2442 | /* Finish up. */ |
2443 | finish_translation_unit (); | |
2444 | ||
2445 | /* All went well. */ | |
2446 | return true; | |
2447 | } | |
2448 | ||
2449 | /* Expressions [gram.expr] */ | |
2450 | ||
2451 | /* Parse a primary-expression. | |
2452 | ||
2453 | primary-expression: | |
2454 | literal | |
2455 | this | |
2456 | ( expression ) | |
2457 | id-expression | |
2458 | ||
2459 | GNU Extensions: | |
2460 | ||
2461 | primary-expression: | |
2462 | ( compound-statement ) | |
2463 | __builtin_va_arg ( assignment-expression , type-id ) | |
2464 | ||
2465 | literal: | |
2466 | __null | |
2467 | ||
21526606 | 2468 | Returns a representation of the expression. |
a723baf1 | 2469 | |
21526606 | 2470 | *IDK indicates what kind of id-expression (if any) was present. |
a723baf1 MM |
2471 | |
2472 | *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be | |
2473 | used as the operand of a pointer-to-member. In that case, | |
2474 | *QUALIFYING_CLASS gives the class that is used as the qualifying | |
2475 | class in the pointer-to-member. */ | |
2476 | ||
2477 | static tree | |
21526606 | 2478 | cp_parser_primary_expression (cp_parser *parser, |
b3445994 | 2479 | cp_id_kind *idk, |
a723baf1 MM |
2480 | tree *qualifying_class) |
2481 | { | |
2482 | cp_token *token; | |
2483 | ||
2484 | /* Assume the primary expression is not an id-expression. */ | |
b3445994 | 2485 | *idk = CP_ID_KIND_NONE; |
a723baf1 MM |
2486 | /* And that it cannot be used as pointer-to-member. */ |
2487 | *qualifying_class = NULL_TREE; | |
2488 | ||
2489 | /* Peek at the next token. */ | |
2490 | token = cp_lexer_peek_token (parser->lexer); | |
2491 | switch (token->type) | |
2492 | { | |
2493 | /* literal: | |
2494 | integer-literal | |
2495 | character-literal | |
2496 | floating-literal | |
2497 | string-literal | |
2498 | boolean-literal */ | |
2499 | case CPP_CHAR: | |
2500 | case CPP_WCHAR: | |
a723baf1 MM |
2501 | case CPP_NUMBER: |
2502 | token = cp_lexer_consume_token (parser->lexer); | |
2503 | return token->value; | |
2504 | ||
0173bb6f AO |
2505 | case CPP_STRING: |
2506 | case CPP_WSTRING: | |
2507 | token = cp_lexer_consume_token (parser->lexer); | |
2508 | if (TREE_CHAIN (token->value)) | |
2509 | return TREE_CHAIN (token->value); | |
2510 | else | |
2511 | return token->value; | |
2512 | ||
a723baf1 MM |
2513 | case CPP_OPEN_PAREN: |
2514 | { | |
2515 | tree expr; | |
2516 | bool saved_greater_than_is_operator_p; | |
2517 | ||
2518 | /* Consume the `('. */ | |
2519 | cp_lexer_consume_token (parser->lexer); | |
2520 | /* Within a parenthesized expression, a `>' token is always | |
2521 | the greater-than operator. */ | |
21526606 | 2522 | saved_greater_than_is_operator_p |
a723baf1 MM |
2523 | = parser->greater_than_is_operator_p; |
2524 | parser->greater_than_is_operator_p = true; | |
2525 | /* If we see `( { ' then we are looking at the beginning of | |
2526 | a GNU statement-expression. */ | |
2527 | if (cp_parser_allow_gnu_extensions_p (parser) | |
2528 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
2529 | { | |
2530 | /* Statement-expressions are not allowed by the standard. */ | |
2531 | if (pedantic) | |
21526606 EC |
2532 | pedwarn ("ISO C++ forbids braced-groups within expressions"); |
2533 | ||
a723baf1 MM |
2534 | /* And they're not allowed outside of a function-body; you |
2535 | cannot, for example, write: | |
21526606 | 2536 | |
a723baf1 | 2537 | int i = ({ int j = 3; j + 1; }); |
21526606 | 2538 | |
a723baf1 MM |
2539 | at class or namespace scope. */ |
2540 | if (!at_function_scope_p ()) | |
2541 | error ("statement-expressions are allowed only inside functions"); | |
2542 | /* Start the statement-expression. */ | |
2543 | expr = begin_stmt_expr (); | |
2544 | /* Parse the compound-statement. */ | |
a5bcc582 | 2545 | cp_parser_compound_statement (parser, true); |
a723baf1 | 2546 | /* Finish up. */ |
303b7406 | 2547 | expr = finish_stmt_expr (expr, false); |
a723baf1 MM |
2548 | } |
2549 | else | |
2550 | { | |
2551 | /* Parse the parenthesized expression. */ | |
2552 | expr = cp_parser_expression (parser); | |
2553 | /* Let the front end know that this expression was | |
2554 | enclosed in parentheses. This matters in case, for | |
2555 | example, the expression is of the form `A::B', since | |
2556 | `&A::B' might be a pointer-to-member, but `&(A::B)' is | |
2557 | not. */ | |
2558 | finish_parenthesized_expr (expr); | |
2559 | } | |
2560 | /* The `>' token might be the end of a template-id or | |
2561 | template-parameter-list now. */ | |
21526606 | 2562 | parser->greater_than_is_operator_p |
a723baf1 MM |
2563 | = saved_greater_than_is_operator_p; |
2564 | /* Consume the `)'. */ | |
2565 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
2566 | cp_parser_skip_to_end_of_statement (parser); | |
2567 | ||
2568 | return expr; | |
2569 | } | |
2570 | ||
2571 | case CPP_KEYWORD: | |
2572 | switch (token->keyword) | |
2573 | { | |
2574 | /* These two are the boolean literals. */ | |
2575 | case RID_TRUE: | |
2576 | cp_lexer_consume_token (parser->lexer); | |
2577 | return boolean_true_node; | |
2578 | case RID_FALSE: | |
2579 | cp_lexer_consume_token (parser->lexer); | |
2580 | return boolean_false_node; | |
21526606 | 2581 | |
a723baf1 MM |
2582 | /* The `__null' literal. */ |
2583 | case RID_NULL: | |
2584 | cp_lexer_consume_token (parser->lexer); | |
2585 | return null_node; | |
2586 | ||
2587 | /* Recognize the `this' keyword. */ | |
2588 | case RID_THIS: | |
2589 | cp_lexer_consume_token (parser->lexer); | |
2590 | if (parser->local_variables_forbidden_p) | |
2591 | { | |
2592 | error ("`this' may not be used in this context"); | |
2593 | return error_mark_node; | |
2594 | } | |
14d22dd6 | 2595 | /* Pointers cannot appear in constant-expressions. */ |
625cbf93 MM |
2596 | if (cp_parser_non_integral_constant_expression (parser, |
2597 | "`this'")) | |
2598 | return error_mark_node; | |
a723baf1 MM |
2599 | return finish_this_expr (); |
2600 | ||
2601 | /* The `operator' keyword can be the beginning of an | |
2602 | id-expression. */ | |
2603 | case RID_OPERATOR: | |
2604 | goto id_expression; | |
2605 | ||
2606 | case RID_FUNCTION_NAME: | |
2607 | case RID_PRETTY_FUNCTION_NAME: | |
2608 | case RID_C99_FUNCTION_NAME: | |
2609 | /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and | |
2610 | __func__ are the names of variables -- but they are | |
2611 | treated specially. Therefore, they are handled here, | |
2612 | rather than relying on the generic id-expression logic | |
21526606 | 2613 | below. Grammatically, these names are id-expressions. |
a723baf1 MM |
2614 | |
2615 | Consume the token. */ | |
2616 | token = cp_lexer_consume_token (parser->lexer); | |
2617 | /* Look up the name. */ | |
2618 | return finish_fname (token->value); | |
2619 | ||
2620 | case RID_VA_ARG: | |
2621 | { | |
2622 | tree expression; | |
2623 | tree type; | |
2624 | ||
2625 | /* The `__builtin_va_arg' construct is used to handle | |
2626 | `va_arg'. Consume the `__builtin_va_arg' token. */ | |
2627 | cp_lexer_consume_token (parser->lexer); | |
2628 | /* Look for the opening `('. */ | |
2629 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
2630 | /* Now, parse the assignment-expression. */ | |
2631 | expression = cp_parser_assignment_expression (parser); | |
2632 | /* Look for the `,'. */ | |
2633 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
2634 | /* Parse the type-id. */ | |
2635 | type = cp_parser_type_id (parser); | |
2636 | /* Look for the closing `)'. */ | |
2637 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
14d22dd6 MM |
2638 | /* Using `va_arg' in a constant-expression is not |
2639 | allowed. */ | |
625cbf93 MM |
2640 | if (cp_parser_non_integral_constant_expression (parser, |
2641 | "`va_arg'")) | |
2642 | return error_mark_node; | |
a723baf1 MM |
2643 | return build_x_va_arg (expression, type); |
2644 | } | |
2645 | ||
263ee052 | 2646 | case RID_OFFSETOF: |
7a3ea201 | 2647 | return cp_parser_builtin_offsetof (parser); |
263ee052 | 2648 | |
a723baf1 MM |
2649 | default: |
2650 | cp_parser_error (parser, "expected primary-expression"); | |
2651 | return error_mark_node; | |
2652 | } | |
a723baf1 MM |
2653 | |
2654 | /* An id-expression can start with either an identifier, a | |
2655 | `::' as the beginning of a qualified-id, or the "operator" | |
2656 | keyword. */ | |
2657 | case CPP_NAME: | |
2658 | case CPP_SCOPE: | |
2659 | case CPP_TEMPLATE_ID: | |
2660 | case CPP_NESTED_NAME_SPECIFIER: | |
2661 | { | |
2662 | tree id_expression; | |
2663 | tree decl; | |
b3445994 | 2664 | const char *error_msg; |
a723baf1 MM |
2665 | |
2666 | id_expression: | |
2667 | /* Parse the id-expression. */ | |
21526606 EC |
2668 | id_expression |
2669 | = cp_parser_id_expression (parser, | |
a723baf1 MM |
2670 | /*template_keyword_p=*/false, |
2671 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
2672 | /*template_p=*/NULL, |
2673 | /*declarator_p=*/false); | |
a723baf1 MM |
2674 | if (id_expression == error_mark_node) |
2675 | return error_mark_node; | |
2676 | /* If we have a template-id, then no further lookup is | |
2677 | required. If the template-id was for a template-class, we | |
2678 | will sometimes have a TYPE_DECL at this point. */ | |
2679 | else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR | |
2680 | || TREE_CODE (id_expression) == TYPE_DECL) | |
2681 | decl = id_expression; | |
2682 | /* Look up the name. */ | |
21526606 | 2683 | else |
a723baf1 MM |
2684 | { |
2685 | decl = cp_parser_lookup_name_simple (parser, id_expression); | |
2686 | /* If name lookup gives us a SCOPE_REF, then the | |
2687 | qualifying scope was dependent. Just propagate the | |
2688 | name. */ | |
2689 | if (TREE_CODE (decl) == SCOPE_REF) | |
2690 | { | |
2691 | if (TYPE_P (TREE_OPERAND (decl, 0))) | |
2692 | *qualifying_class = TREE_OPERAND (decl, 0); | |
2693 | return decl; | |
2694 | } | |
2695 | /* Check to see if DECL is a local variable in a context | |
2696 | where that is forbidden. */ | |
2697 | if (parser->local_variables_forbidden_p | |
2698 | && local_variable_p (decl)) | |
2699 | { | |
2700 | /* It might be that we only found DECL because we are | |
2701 | trying to be generous with pre-ISO scoping rules. | |
2702 | For example, consider: | |
2703 | ||
2704 | int i; | |
2705 | void g() { | |
2706 | for (int i = 0; i < 10; ++i) {} | |
2707 | extern void f(int j = i); | |
2708 | } | |
2709 | ||
21526606 | 2710 | Here, name look up will originally find the out |
a723baf1 MM |
2711 | of scope `i'. We need to issue a warning message, |
2712 | but then use the global `i'. */ | |
2713 | decl = check_for_out_of_scope_variable (decl); | |
2714 | if (local_variable_p (decl)) | |
2715 | { | |
2716 | error ("local variable `%D' may not appear in this context", | |
2717 | decl); | |
2718 | return error_mark_node; | |
2719 | } | |
2720 | } | |
c006d942 | 2721 | } |
21526606 EC |
2722 | |
2723 | decl = finish_id_expression (id_expression, decl, parser->scope, | |
b3445994 | 2724 | idk, qualifying_class, |
67c03833 JM |
2725 | parser->integral_constant_expression_p, |
2726 | parser->allow_non_integral_constant_expression_p, | |
2727 | &parser->non_integral_constant_expression_p, | |
b3445994 MM |
2728 | &error_msg); |
2729 | if (error_msg) | |
2730 | cp_parser_error (parser, error_msg); | |
a723baf1 MM |
2731 | return decl; |
2732 | } | |
2733 | ||
2734 | /* Anything else is an error. */ | |
2735 | default: | |
2736 | cp_parser_error (parser, "expected primary-expression"); | |
2737 | return error_mark_node; | |
2738 | } | |
2739 | } | |
2740 | ||
2741 | /* Parse an id-expression. | |
2742 | ||
2743 | id-expression: | |
2744 | unqualified-id | |
2745 | qualified-id | |
2746 | ||
2747 | qualified-id: | |
2748 | :: [opt] nested-name-specifier template [opt] unqualified-id | |
2749 | :: identifier | |
2750 | :: operator-function-id | |
2751 | :: template-id | |
2752 | ||
2753 | Return a representation of the unqualified portion of the | |
2754 | identifier. Sets PARSER->SCOPE to the qualifying scope if there is | |
2755 | a `::' or nested-name-specifier. | |
2756 | ||
2757 | Often, if the id-expression was a qualified-id, the caller will | |
2758 | want to make a SCOPE_REF to represent the qualified-id. This | |
2759 | function does not do this in order to avoid wastefully creating | |
2760 | SCOPE_REFs when they are not required. | |
2761 | ||
a723baf1 MM |
2762 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
2763 | `template' keyword. | |
2764 | ||
2765 | If CHECK_DEPENDENCY_P is false, then names are looked up inside | |
21526606 | 2766 | uninstantiated templates. |
a723baf1 | 2767 | |
15d2cb19 | 2768 | If *TEMPLATE_P is non-NULL, it is set to true iff the |
a723baf1 | 2769 | `template' keyword is used to explicitly indicate that the entity |
21526606 | 2770 | named is a template. |
f3c2dfc6 MM |
2771 | |
2772 | If DECLARATOR_P is true, the id-expression is appearing as part of | |
cd0be382 | 2773 | a declarator, rather than as part of an expression. */ |
a723baf1 MM |
2774 | |
2775 | static tree | |
2776 | cp_parser_id_expression (cp_parser *parser, | |
2777 | bool template_keyword_p, | |
2778 | bool check_dependency_p, | |
f3c2dfc6 MM |
2779 | bool *template_p, |
2780 | bool declarator_p) | |
a723baf1 MM |
2781 | { |
2782 | bool global_scope_p; | |
2783 | bool nested_name_specifier_p; | |
2784 | ||
2785 | /* Assume the `template' keyword was not used. */ | |
2786 | if (template_p) | |
2787 | *template_p = false; | |
2788 | ||
2789 | /* Look for the optional `::' operator. */ | |
21526606 EC |
2790 | global_scope_p |
2791 | = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) | |
a723baf1 MM |
2792 | != NULL_TREE); |
2793 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 2794 | nested_name_specifier_p |
a723baf1 MM |
2795 | = (cp_parser_nested_name_specifier_opt (parser, |
2796 | /*typename_keyword_p=*/false, | |
2797 | check_dependency_p, | |
a668c6ad MM |
2798 | /*type_p=*/false, |
2799 | /*is_declarator=*/false) | |
a723baf1 MM |
2800 | != NULL_TREE); |
2801 | /* If there is a nested-name-specifier, then we are looking at | |
2802 | the first qualified-id production. */ | |
2803 | if (nested_name_specifier_p) | |
2804 | { | |
2805 | tree saved_scope; | |
2806 | tree saved_object_scope; | |
2807 | tree saved_qualifying_scope; | |
2808 | tree unqualified_id; | |
2809 | bool is_template; | |
2810 | ||
2811 | /* See if the next token is the `template' keyword. */ | |
2812 | if (!template_p) | |
2813 | template_p = &is_template; | |
2814 | *template_p = cp_parser_optional_template_keyword (parser); | |
2815 | /* Name lookup we do during the processing of the | |
2816 | unqualified-id might obliterate SCOPE. */ | |
2817 | saved_scope = parser->scope; | |
2818 | saved_object_scope = parser->object_scope; | |
2819 | saved_qualifying_scope = parser->qualifying_scope; | |
2820 | /* Process the final unqualified-id. */ | |
2821 | unqualified_id = cp_parser_unqualified_id (parser, *template_p, | |
f3c2dfc6 MM |
2822 | check_dependency_p, |
2823 | declarator_p); | |
a723baf1 MM |
2824 | /* Restore the SAVED_SCOPE for our caller. */ |
2825 | parser->scope = saved_scope; | |
2826 | parser->object_scope = saved_object_scope; | |
2827 | parser->qualifying_scope = saved_qualifying_scope; | |
2828 | ||
2829 | return unqualified_id; | |
2830 | } | |
2831 | /* Otherwise, if we are in global scope, then we are looking at one | |
2832 | of the other qualified-id productions. */ | |
2833 | else if (global_scope_p) | |
2834 | { | |
2835 | cp_token *token; | |
2836 | tree id; | |
2837 | ||
e5976695 MM |
2838 | /* Peek at the next token. */ |
2839 | token = cp_lexer_peek_token (parser->lexer); | |
2840 | ||
2841 | /* If it's an identifier, and the next token is not a "<", then | |
2842 | we can avoid the template-id case. This is an optimization | |
2843 | for this common case. */ | |
21526606 EC |
2844 | if (token->type == CPP_NAME |
2845 | && !cp_parser_nth_token_starts_template_argument_list_p | |
f4abade9 | 2846 | (parser, 2)) |
e5976695 MM |
2847 | return cp_parser_identifier (parser); |
2848 | ||
a723baf1 MM |
2849 | cp_parser_parse_tentatively (parser); |
2850 | /* Try a template-id. */ | |
21526606 | 2851 | id = cp_parser_template_id (parser, |
a723baf1 | 2852 | /*template_keyword_p=*/false, |
a668c6ad MM |
2853 | /*check_dependency_p=*/true, |
2854 | declarator_p); | |
a723baf1 MM |
2855 | /* If that worked, we're done. */ |
2856 | if (cp_parser_parse_definitely (parser)) | |
2857 | return id; | |
2858 | ||
e5976695 MM |
2859 | /* Peek at the next token. (Changes in the token buffer may |
2860 | have invalidated the pointer obtained above.) */ | |
a723baf1 MM |
2861 | token = cp_lexer_peek_token (parser->lexer); |
2862 | ||
2863 | switch (token->type) | |
2864 | { | |
2865 | case CPP_NAME: | |
2866 | return cp_parser_identifier (parser); | |
2867 | ||
2868 | case CPP_KEYWORD: | |
2869 | if (token->keyword == RID_OPERATOR) | |
2870 | return cp_parser_operator_function_id (parser); | |
2871 | /* Fall through. */ | |
21526606 | 2872 | |
a723baf1 MM |
2873 | default: |
2874 | cp_parser_error (parser, "expected id-expression"); | |
2875 | return error_mark_node; | |
2876 | } | |
2877 | } | |
2878 | else | |
2879 | return cp_parser_unqualified_id (parser, template_keyword_p, | |
f3c2dfc6 MM |
2880 | /*check_dependency_p=*/true, |
2881 | declarator_p); | |
a723baf1 MM |
2882 | } |
2883 | ||
2884 | /* Parse an unqualified-id. | |
2885 | ||
2886 | unqualified-id: | |
2887 | identifier | |
2888 | operator-function-id | |
2889 | conversion-function-id | |
2890 | ~ class-name | |
2891 | template-id | |
2892 | ||
2893 | If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' | |
2894 | keyword, in a construct like `A::template ...'. | |
2895 | ||
2896 | Returns a representation of unqualified-id. For the `identifier' | |
2897 | production, an IDENTIFIER_NODE is returned. For the `~ class-name' | |
2898 | production a BIT_NOT_EXPR is returned; the operand of the | |
2899 | BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the | |
2900 | other productions, see the documentation accompanying the | |
2901 | corresponding parsing functions. If CHECK_DEPENDENCY_P is false, | |
f3c2dfc6 MM |
2902 | names are looked up in uninstantiated templates. If DECLARATOR_P |
2903 | is true, the unqualified-id is appearing as part of a declarator, | |
2904 | rather than as part of an expression. */ | |
a723baf1 MM |
2905 | |
2906 | static tree | |
21526606 | 2907 | cp_parser_unqualified_id (cp_parser* parser, |
94edc4ab | 2908 | bool template_keyword_p, |
f3c2dfc6 MM |
2909 | bool check_dependency_p, |
2910 | bool declarator_p) | |
a723baf1 MM |
2911 | { |
2912 | cp_token *token; | |
2913 | ||
2914 | /* Peek at the next token. */ | |
2915 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 2916 | |
a723baf1 MM |
2917 | switch (token->type) |
2918 | { | |
2919 | case CPP_NAME: | |
2920 | { | |
2921 | tree id; | |
2922 | ||
2923 | /* We don't know yet whether or not this will be a | |
2924 | template-id. */ | |
2925 | cp_parser_parse_tentatively (parser); | |
2926 | /* Try a template-id. */ | |
2927 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2928 | check_dependency_p, |
2929 | declarator_p); | |
a723baf1 MM |
2930 | /* If it worked, we're done. */ |
2931 | if (cp_parser_parse_definitely (parser)) | |
2932 | return id; | |
2933 | /* Otherwise, it's an ordinary identifier. */ | |
2934 | return cp_parser_identifier (parser); | |
2935 | } | |
2936 | ||
2937 | case CPP_TEMPLATE_ID: | |
2938 | return cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
2939 | check_dependency_p, |
2940 | declarator_p); | |
a723baf1 MM |
2941 | |
2942 | case CPP_COMPL: | |
2943 | { | |
2944 | tree type_decl; | |
2945 | tree qualifying_scope; | |
2946 | tree object_scope; | |
2947 | tree scope; | |
2948 | ||
2949 | /* Consume the `~' token. */ | |
2950 | cp_lexer_consume_token (parser->lexer); | |
2951 | /* Parse the class-name. The standard, as written, seems to | |
2952 | say that: | |
2953 | ||
2954 | template <typename T> struct S { ~S (); }; | |
2955 | template <typename T> S<T>::~S() {} | |
2956 | ||
2957 | is invalid, since `~' must be followed by a class-name, but | |
2958 | `S<T>' is dependent, and so not known to be a class. | |
2959 | That's not right; we need to look in uninstantiated | |
2960 | templates. A further complication arises from: | |
2961 | ||
2962 | template <typename T> void f(T t) { | |
2963 | t.T::~T(); | |
21526606 | 2964 | } |
a723baf1 MM |
2965 | |
2966 | Here, it is not possible to look up `T' in the scope of `T' | |
2967 | itself. We must look in both the current scope, and the | |
21526606 | 2968 | scope of the containing complete expression. |
a723baf1 MM |
2969 | |
2970 | Yet another issue is: | |
2971 | ||
2972 | struct S { | |
2973 | int S; | |
2974 | ~S(); | |
2975 | }; | |
2976 | ||
2977 | S::~S() {} | |
2978 | ||
2979 | The standard does not seem to say that the `S' in `~S' | |
2980 | should refer to the type `S' and not the data member | |
2981 | `S::S'. */ | |
2982 | ||
2983 | /* DR 244 says that we look up the name after the "~" in the | |
2984 | same scope as we looked up the qualifying name. That idea | |
2985 | isn't fully worked out; it's more complicated than that. */ | |
2986 | scope = parser->scope; | |
2987 | object_scope = parser->object_scope; | |
2988 | qualifying_scope = parser->qualifying_scope; | |
2989 | ||
2990 | /* If the name is of the form "X::~X" it's OK. */ | |
2991 | if (scope && TYPE_P (scope) | |
2992 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 2993 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 2994 | == CPP_OPEN_PAREN) |
21526606 | 2995 | && (cp_lexer_peek_token (parser->lexer)->value |
a723baf1 MM |
2996 | == TYPE_IDENTIFIER (scope))) |
2997 | { | |
2998 | cp_lexer_consume_token (parser->lexer); | |
2999 | return build_nt (BIT_NOT_EXPR, scope); | |
3000 | } | |
3001 | ||
3002 | /* If there was an explicit qualification (S::~T), first look | |
3003 | in the scope given by the qualification (i.e., S). */ | |
3004 | if (scope) | |
3005 | { | |
3006 | cp_parser_parse_tentatively (parser); | |
21526606 | 3007 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
3008 | /*typename_keyword_p=*/false, |
3009 | /*template_keyword_p=*/false, | |
3010 | /*type_p=*/false, | |
a723baf1 | 3011 | /*check_dependency=*/false, |
a668c6ad MM |
3012 | /*class_head_p=*/false, |
3013 | declarator_p); | |
a723baf1 MM |
3014 | if (cp_parser_parse_definitely (parser)) |
3015 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
3016 | } | |
3017 | /* In "N::S::~S", look in "N" as well. */ | |
3018 | if (scope && qualifying_scope) | |
3019 | { | |
3020 | cp_parser_parse_tentatively (parser); | |
3021 | parser->scope = qualifying_scope; | |
3022 | parser->object_scope = NULL_TREE; | |
3023 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3024 | type_decl |
3025 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3026 | /*typename_keyword_p=*/false, |
3027 | /*template_keyword_p=*/false, | |
3028 | /*type_p=*/false, | |
a723baf1 | 3029 | /*check_dependency=*/false, |
a668c6ad MM |
3030 | /*class_head_p=*/false, |
3031 | declarator_p); | |
a723baf1 MM |
3032 | if (cp_parser_parse_definitely (parser)) |
3033 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
3034 | } | |
3035 | /* In "p->S::~T", look in the scope given by "*p" as well. */ | |
3036 | else if (object_scope) | |
3037 | { | |
3038 | cp_parser_parse_tentatively (parser); | |
3039 | parser->scope = object_scope; | |
3040 | parser->object_scope = NULL_TREE; | |
3041 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3042 | type_decl |
3043 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3044 | /*typename_keyword_p=*/false, |
3045 | /*template_keyword_p=*/false, | |
3046 | /*type_p=*/false, | |
a723baf1 | 3047 | /*check_dependency=*/false, |
a668c6ad MM |
3048 | /*class_head_p=*/false, |
3049 | declarator_p); | |
a723baf1 MM |
3050 | if (cp_parser_parse_definitely (parser)) |
3051 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); | |
3052 | } | |
3053 | /* Look in the surrounding context. */ | |
3054 | parser->scope = NULL_TREE; | |
3055 | parser->object_scope = NULL_TREE; | |
3056 | parser->qualifying_scope = NULL_TREE; | |
21526606 EC |
3057 | type_decl |
3058 | = cp_parser_class_name (parser, | |
a723baf1 MM |
3059 | /*typename_keyword_p=*/false, |
3060 | /*template_keyword_p=*/false, | |
3061 | /*type_p=*/false, | |
a723baf1 | 3062 | /*check_dependency=*/false, |
a668c6ad MM |
3063 | /*class_head_p=*/false, |
3064 | declarator_p); | |
a723baf1 MM |
3065 | /* If an error occurred, assume that the name of the |
3066 | destructor is the same as the name of the qualifying | |
3067 | class. That allows us to keep parsing after running | |
3068 | into ill-formed destructor names. */ | |
3069 | if (type_decl == error_mark_node && scope && TYPE_P (scope)) | |
3070 | return build_nt (BIT_NOT_EXPR, scope); | |
3071 | else if (type_decl == error_mark_node) | |
3072 | return error_mark_node; | |
3073 | ||
f3c2dfc6 MM |
3074 | /* [class.dtor] |
3075 | ||
3076 | A typedef-name that names a class shall not be used as the | |
3077 | identifier in the declarator for a destructor declaration. */ | |
21526606 | 3078 | if (declarator_p |
f3c2dfc6 MM |
3079 | && !DECL_IMPLICIT_TYPEDEF_P (type_decl) |
3080 | && !DECL_SELF_REFERENCE_P (type_decl)) | |
3081 | error ("typedef-name `%D' used as destructor declarator", | |
3082 | type_decl); | |
3083 | ||
a723baf1 MM |
3084 | return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); |
3085 | } | |
3086 | ||
3087 | case CPP_KEYWORD: | |
3088 | if (token->keyword == RID_OPERATOR) | |
3089 | { | |
3090 | tree id; | |
3091 | ||
3092 | /* This could be a template-id, so we try that first. */ | |
3093 | cp_parser_parse_tentatively (parser); | |
3094 | /* Try a template-id. */ | |
3095 | id = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
3096 | /*check_dependency_p=*/true, |
3097 | declarator_p); | |
a723baf1 MM |
3098 | /* If that worked, we're done. */ |
3099 | if (cp_parser_parse_definitely (parser)) | |
3100 | return id; | |
3101 | /* We still don't know whether we're looking at an | |
3102 | operator-function-id or a conversion-function-id. */ | |
3103 | cp_parser_parse_tentatively (parser); | |
3104 | /* Try an operator-function-id. */ | |
3105 | id = cp_parser_operator_function_id (parser); | |
3106 | /* If that didn't work, try a conversion-function-id. */ | |
3107 | if (!cp_parser_parse_definitely (parser)) | |
3108 | id = cp_parser_conversion_function_id (parser); | |
3109 | ||
3110 | return id; | |
3111 | } | |
3112 | /* Fall through. */ | |
3113 | ||
3114 | default: | |
3115 | cp_parser_error (parser, "expected unqualified-id"); | |
3116 | return error_mark_node; | |
3117 | } | |
3118 | } | |
3119 | ||
3120 | /* Parse an (optional) nested-name-specifier. | |
3121 | ||
3122 | nested-name-specifier: | |
3123 | class-or-namespace-name :: nested-name-specifier [opt] | |
3124 | class-or-namespace-name :: template nested-name-specifier [opt] | |
3125 | ||
3126 | PARSER->SCOPE should be set appropriately before this function is | |
3127 | called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in | |
3128 | effect. TYPE_P is TRUE if we non-type bindings should be ignored | |
3129 | in name lookups. | |
3130 | ||
3131 | Sets PARSER->SCOPE to the class (TYPE) or namespace | |
3132 | (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves | |
3133 | it unchanged if there is no nested-name-specifier. Returns the new | |
21526606 | 3134 | scope iff there is a nested-name-specifier, or NULL_TREE otherwise. |
a668c6ad MM |
3135 | |
3136 | If IS_DECLARATION is TRUE, the nested-name-specifier is known to be | |
3137 | part of a declaration and/or decl-specifier. */ | |
a723baf1 MM |
3138 | |
3139 | static tree | |
21526606 EC |
3140 | cp_parser_nested_name_specifier_opt (cp_parser *parser, |
3141 | bool typename_keyword_p, | |
a723baf1 | 3142 | bool check_dependency_p, |
a668c6ad MM |
3143 | bool type_p, |
3144 | bool is_declaration) | |
a723baf1 MM |
3145 | { |
3146 | bool success = false; | |
3147 | tree access_check = NULL_TREE; | |
3148 | ptrdiff_t start; | |
2050a1bb | 3149 | cp_token* token; |
a723baf1 MM |
3150 | |
3151 | /* If the next token corresponds to a nested name specifier, there | |
2050a1bb | 3152 | is no need to reparse it. However, if CHECK_DEPENDENCY_P is |
21526606 | 3153 | false, it may have been true before, in which case something |
2050a1bb MM |
3154 | like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier |
3155 | of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when | |
3156 | CHECK_DEPENDENCY_P is false, we have to fall through into the | |
3157 | main loop. */ | |
3158 | if (check_dependency_p | |
3159 | && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER)) | |
3160 | { | |
3161 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
a723baf1 MM |
3162 | return parser->scope; |
3163 | } | |
3164 | ||
3165 | /* Remember where the nested-name-specifier starts. */ | |
3166 | if (cp_parser_parsing_tentatively (parser) | |
3167 | && !cp_parser_committed_to_tentative_parse (parser)) | |
3168 | { | |
2050a1bb | 3169 | token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
3170 | start = cp_lexer_token_difference (parser->lexer, |
3171 | parser->lexer->first_token, | |
2050a1bb | 3172 | token); |
a723baf1 MM |
3173 | } |
3174 | else | |
3175 | start = -1; | |
3176 | ||
8d241e0b | 3177 | push_deferring_access_checks (dk_deferred); |
cf22909c | 3178 | |
a723baf1 MM |
3179 | while (true) |
3180 | { | |
3181 | tree new_scope; | |
3182 | tree old_scope; | |
3183 | tree saved_qualifying_scope; | |
a723baf1 MM |
3184 | bool template_keyword_p; |
3185 | ||
2050a1bb MM |
3186 | /* Spot cases that cannot be the beginning of a |
3187 | nested-name-specifier. */ | |
3188 | token = cp_lexer_peek_token (parser->lexer); | |
3189 | ||
3190 | /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process | |
3191 | the already parsed nested-name-specifier. */ | |
3192 | if (token->type == CPP_NESTED_NAME_SPECIFIER) | |
3193 | { | |
3194 | /* Grab the nested-name-specifier and continue the loop. */ | |
3195 | cp_parser_pre_parsed_nested_name_specifier (parser); | |
3196 | success = true; | |
3197 | continue; | |
3198 | } | |
3199 | ||
a723baf1 MM |
3200 | /* Spot cases that cannot be the beginning of a |
3201 | nested-name-specifier. On the second and subsequent times | |
3202 | through the loop, we look for the `template' keyword. */ | |
f7b5ecd9 | 3203 | if (success && token->keyword == RID_TEMPLATE) |
a723baf1 MM |
3204 | ; |
3205 | /* A template-id can start a nested-name-specifier. */ | |
f7b5ecd9 | 3206 | else if (token->type == CPP_TEMPLATE_ID) |
a723baf1 MM |
3207 | ; |
3208 | else | |
3209 | { | |
3210 | /* If the next token is not an identifier, then it is | |
3211 | definitely not a class-or-namespace-name. */ | |
f7b5ecd9 | 3212 | if (token->type != CPP_NAME) |
a723baf1 MM |
3213 | break; |
3214 | /* If the following token is neither a `<' (to begin a | |
3215 | template-id), nor a `::', then we are not looking at a | |
3216 | nested-name-specifier. */ | |
3217 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
f4abade9 GB |
3218 | if (token->type != CPP_SCOPE |
3219 | && !cp_parser_nth_token_starts_template_argument_list_p | |
3220 | (parser, 2)) | |
a723baf1 MM |
3221 | break; |
3222 | } | |
3223 | ||
3224 | /* The nested-name-specifier is optional, so we parse | |
3225 | tentatively. */ | |
3226 | cp_parser_parse_tentatively (parser); | |
3227 | ||
3228 | /* Look for the optional `template' keyword, if this isn't the | |
3229 | first time through the loop. */ | |
3230 | if (success) | |
3231 | template_keyword_p = cp_parser_optional_template_keyword (parser); | |
3232 | else | |
3233 | template_keyword_p = false; | |
3234 | ||
3235 | /* Save the old scope since the name lookup we are about to do | |
3236 | might destroy it. */ | |
3237 | old_scope = parser->scope; | |
3238 | saved_qualifying_scope = parser->qualifying_scope; | |
3239 | /* Parse the qualifying entity. */ | |
21526606 | 3240 | new_scope |
a723baf1 MM |
3241 | = cp_parser_class_or_namespace_name (parser, |
3242 | typename_keyword_p, | |
3243 | template_keyword_p, | |
3244 | check_dependency_p, | |
a668c6ad MM |
3245 | type_p, |
3246 | is_declaration); | |
a723baf1 MM |
3247 | /* Look for the `::' token. */ |
3248 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
3249 | ||
3250 | /* If we found what we wanted, we keep going; otherwise, we're | |
3251 | done. */ | |
3252 | if (!cp_parser_parse_definitely (parser)) | |
3253 | { | |
3254 | bool error_p = false; | |
3255 | ||
3256 | /* Restore the OLD_SCOPE since it was valid before the | |
3257 | failed attempt at finding the last | |
3258 | class-or-namespace-name. */ | |
3259 | parser->scope = old_scope; | |
3260 | parser->qualifying_scope = saved_qualifying_scope; | |
3261 | /* If the next token is an identifier, and the one after | |
3262 | that is a `::', then any valid interpretation would have | |
3263 | found a class-or-namespace-name. */ | |
3264 | while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
21526606 | 3265 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 | 3266 | == CPP_SCOPE) |
21526606 | 3267 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
3268 | != CPP_COMPL)) |
3269 | { | |
3270 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 3271 | if (!error_p) |
a723baf1 MM |
3272 | { |
3273 | tree decl; | |
3274 | ||
3275 | decl = cp_parser_lookup_name_simple (parser, token->value); | |
3276 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
3277 | error ("`%D' used without template parameters", | |
3278 | decl); | |
a723baf1 | 3279 | else |
21526606 EC |
3280 | cp_parser_name_lookup_error |
3281 | (parser, token->value, decl, | |
4bb8ca28 | 3282 | "is not a class or namespace"); |
a723baf1 MM |
3283 | parser->scope = NULL_TREE; |
3284 | error_p = true; | |
eea9800f MM |
3285 | /* Treat this as a successful nested-name-specifier |
3286 | due to: | |
3287 | ||
3288 | [basic.lookup.qual] | |
3289 | ||
3290 | If the name found is not a class-name (clause | |
3291 | _class_) or namespace-name (_namespace.def_), the | |
3292 | program is ill-formed. */ | |
3293 | success = true; | |
a723baf1 MM |
3294 | } |
3295 | cp_lexer_consume_token (parser->lexer); | |
3296 | } | |
3297 | break; | |
3298 | } | |
3299 | ||
3300 | /* We've found one valid nested-name-specifier. */ | |
3301 | success = true; | |
3302 | /* Make sure we look in the right scope the next time through | |
3303 | the loop. */ | |
21526606 | 3304 | parser->scope = (TREE_CODE (new_scope) == TYPE_DECL |
a723baf1 MM |
3305 | ? TREE_TYPE (new_scope) |
3306 | : new_scope); | |
3307 | /* If it is a class scope, try to complete it; we are about to | |
3308 | be looking up names inside the class. */ | |
8fbc5ae7 MM |
3309 | if (TYPE_P (parser->scope) |
3310 | /* Since checking types for dependency can be expensive, | |
3311 | avoid doing it if the type is already complete. */ | |
3312 | && !COMPLETE_TYPE_P (parser->scope) | |
3313 | /* Do not try to complete dependent types. */ | |
1fb3244a | 3314 | && !dependent_type_p (parser->scope)) |
a723baf1 MM |
3315 | complete_type (parser->scope); |
3316 | } | |
3317 | ||
cf22909c KL |
3318 | /* Retrieve any deferred checks. Do not pop this access checks yet |
3319 | so the memory will not be reclaimed during token replacing below. */ | |
3320 | access_check = get_deferred_access_checks (); | |
3321 | ||
a723baf1 MM |
3322 | /* If parsing tentatively, replace the sequence of tokens that makes |
3323 | up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER | |
3324 | token. That way, should we re-parse the token stream, we will | |
3325 | not have to repeat the effort required to do the parse, nor will | |
3326 | we issue duplicate error messages. */ | |
3327 | if (success && start >= 0) | |
3328 | { | |
a723baf1 MM |
3329 | /* Find the token that corresponds to the start of the |
3330 | template-id. */ | |
21526606 | 3331 | token = cp_lexer_advance_token (parser->lexer, |
a723baf1 MM |
3332 | parser->lexer->first_token, |
3333 | start); | |
3334 | ||
a723baf1 MM |
3335 | /* Reset the contents of the START token. */ |
3336 | token->type = CPP_NESTED_NAME_SPECIFIER; | |
3337 | token->value = build_tree_list (access_check, parser->scope); | |
3338 | TREE_TYPE (token->value) = parser->qualifying_scope; | |
3339 | token->keyword = RID_MAX; | |
3340 | /* Purge all subsequent tokens. */ | |
3341 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
3342 | } | |
3343 | ||
cf22909c | 3344 | pop_deferring_access_checks (); |
a723baf1 MM |
3345 | return success ? parser->scope : NULL_TREE; |
3346 | } | |
3347 | ||
3348 | /* Parse a nested-name-specifier. See | |
3349 | cp_parser_nested_name_specifier_opt for details. This function | |
3350 | behaves identically, except that it will an issue an error if no | |
3351 | nested-name-specifier is present, and it will return | |
3352 | ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier | |
3353 | is present. */ | |
3354 | ||
3355 | static tree | |
21526606 EC |
3356 | cp_parser_nested_name_specifier (cp_parser *parser, |
3357 | bool typename_keyword_p, | |
a723baf1 | 3358 | bool check_dependency_p, |
a668c6ad MM |
3359 | bool type_p, |
3360 | bool is_declaration) | |
a723baf1 MM |
3361 | { |
3362 | tree scope; | |
3363 | ||
3364 | /* Look for the nested-name-specifier. */ | |
3365 | scope = cp_parser_nested_name_specifier_opt (parser, | |
3366 | typename_keyword_p, | |
3367 | check_dependency_p, | |
a668c6ad MM |
3368 | type_p, |
3369 | is_declaration); | |
a723baf1 MM |
3370 | /* If it was not present, issue an error message. */ |
3371 | if (!scope) | |
3372 | { | |
3373 | cp_parser_error (parser, "expected nested-name-specifier"); | |
eb5abb39 | 3374 | parser->scope = NULL_TREE; |
a723baf1 MM |
3375 | return error_mark_node; |
3376 | } | |
3377 | ||
3378 | return scope; | |
3379 | } | |
3380 | ||
3381 | /* Parse a class-or-namespace-name. | |
3382 | ||
3383 | class-or-namespace-name: | |
3384 | class-name | |
3385 | namespace-name | |
3386 | ||
3387 | TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. | |
3388 | TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. | |
3389 | CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. | |
3390 | TYPE_P is TRUE iff the next name should be taken as a class-name, | |
3391 | even the same name is declared to be another entity in the same | |
3392 | scope. | |
3393 | ||
3394 | Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) | |
eea9800f MM |
3395 | specified by the class-or-namespace-name. If neither is found the |
3396 | ERROR_MARK_NODE is returned. */ | |
a723baf1 MM |
3397 | |
3398 | static tree | |
21526606 | 3399 | cp_parser_class_or_namespace_name (cp_parser *parser, |
a723baf1 MM |
3400 | bool typename_keyword_p, |
3401 | bool template_keyword_p, | |
3402 | bool check_dependency_p, | |
a668c6ad MM |
3403 | bool type_p, |
3404 | bool is_declaration) | |
a723baf1 MM |
3405 | { |
3406 | tree saved_scope; | |
3407 | tree saved_qualifying_scope; | |
3408 | tree saved_object_scope; | |
3409 | tree scope; | |
eea9800f | 3410 | bool only_class_p; |
a723baf1 | 3411 | |
a723baf1 MM |
3412 | /* Before we try to parse the class-name, we must save away the |
3413 | current PARSER->SCOPE since cp_parser_class_name will destroy | |
3414 | it. */ | |
3415 | saved_scope = parser->scope; | |
3416 | saved_qualifying_scope = parser->qualifying_scope; | |
3417 | saved_object_scope = parser->object_scope; | |
eea9800f MM |
3418 | /* Try for a class-name first. If the SAVED_SCOPE is a type, then |
3419 | there is no need to look for a namespace-name. */ | |
bbaab916 | 3420 | only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope)); |
eea9800f MM |
3421 | if (!only_class_p) |
3422 | cp_parser_parse_tentatively (parser); | |
21526606 | 3423 | scope = cp_parser_class_name (parser, |
a723baf1 MM |
3424 | typename_keyword_p, |
3425 | template_keyword_p, | |
3426 | type_p, | |
a723baf1 | 3427 | check_dependency_p, |
a668c6ad MM |
3428 | /*class_head_p=*/false, |
3429 | is_declaration); | |
a723baf1 | 3430 | /* If that didn't work, try for a namespace-name. */ |
eea9800f | 3431 | if (!only_class_p && !cp_parser_parse_definitely (parser)) |
a723baf1 MM |
3432 | { |
3433 | /* Restore the saved scope. */ | |
3434 | parser->scope = saved_scope; | |
3435 | parser->qualifying_scope = saved_qualifying_scope; | |
3436 | parser->object_scope = saved_object_scope; | |
eea9800f MM |
3437 | /* If we are not looking at an identifier followed by the scope |
3438 | resolution operator, then this is not part of a | |
3439 | nested-name-specifier. (Note that this function is only used | |
3440 | to parse the components of a nested-name-specifier.) */ | |
3441 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) | |
3442 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) | |
3443 | return error_mark_node; | |
a723baf1 MM |
3444 | scope = cp_parser_namespace_name (parser); |
3445 | } | |
3446 | ||
3447 | return scope; | |
3448 | } | |
3449 | ||
3450 | /* Parse a postfix-expression. | |
3451 | ||
3452 | postfix-expression: | |
3453 | primary-expression | |
3454 | postfix-expression [ expression ] | |
3455 | postfix-expression ( expression-list [opt] ) | |
3456 | simple-type-specifier ( expression-list [opt] ) | |
21526606 | 3457 | typename :: [opt] nested-name-specifier identifier |
a723baf1 MM |
3458 | ( expression-list [opt] ) |
3459 | typename :: [opt] nested-name-specifier template [opt] template-id | |
3460 | ( expression-list [opt] ) | |
3461 | postfix-expression . template [opt] id-expression | |
3462 | postfix-expression -> template [opt] id-expression | |
3463 | postfix-expression . pseudo-destructor-name | |
3464 | postfix-expression -> pseudo-destructor-name | |
3465 | postfix-expression ++ | |
3466 | postfix-expression -- | |
3467 | dynamic_cast < type-id > ( expression ) | |
3468 | static_cast < type-id > ( expression ) | |
3469 | reinterpret_cast < type-id > ( expression ) | |
3470 | const_cast < type-id > ( expression ) | |
3471 | typeid ( expression ) | |
3472 | typeid ( type-id ) | |
3473 | ||
3474 | GNU Extension: | |
21526606 | 3475 | |
a723baf1 MM |
3476 | postfix-expression: |
3477 | ( type-id ) { initializer-list , [opt] } | |
3478 | ||
3479 | This extension is a GNU version of the C99 compound-literal | |
3480 | construct. (The C99 grammar uses `type-name' instead of `type-id', | |
3481 | but they are essentially the same concept.) | |
3482 | ||
3483 | If ADDRESS_P is true, the postfix expression is the operand of the | |
3484 | `&' operator. | |
3485 | ||
3486 | Returns a representation of the expression. */ | |
3487 | ||
3488 | static tree | |
3489 | cp_parser_postfix_expression (cp_parser *parser, bool address_p) | |
3490 | { | |
3491 | cp_token *token; | |
3492 | enum rid keyword; | |
b3445994 | 3493 | cp_id_kind idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3494 | tree postfix_expression = NULL_TREE; |
3495 | /* Non-NULL only if the current postfix-expression can be used to | |
3496 | form a pointer-to-member. In that case, QUALIFYING_CLASS is the | |
3497 | class used to qualify the member. */ | |
3498 | tree qualifying_class = NULL_TREE; | |
a723baf1 MM |
3499 | |
3500 | /* Peek at the next token. */ | |
3501 | token = cp_lexer_peek_token (parser->lexer); | |
3502 | /* Some of the productions are determined by keywords. */ | |
3503 | keyword = token->keyword; | |
3504 | switch (keyword) | |
3505 | { | |
3506 | case RID_DYNCAST: | |
3507 | case RID_STATCAST: | |
3508 | case RID_REINTCAST: | |
3509 | case RID_CONSTCAST: | |
3510 | { | |
3511 | tree type; | |
3512 | tree expression; | |
3513 | const char *saved_message; | |
3514 | ||
3515 | /* All of these can be handled in the same way from the point | |
3516 | of view of parsing. Begin by consuming the token | |
3517 | identifying the cast. */ | |
3518 | cp_lexer_consume_token (parser->lexer); | |
21526606 | 3519 | |
a723baf1 MM |
3520 | /* New types cannot be defined in the cast. */ |
3521 | saved_message = parser->type_definition_forbidden_message; | |
3522 | parser->type_definition_forbidden_message | |
3523 | = "types may not be defined in casts"; | |
3524 | ||
3525 | /* Look for the opening `<'. */ | |
3526 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
3527 | /* Parse the type to which we are casting. */ | |
3528 | type = cp_parser_type_id (parser); | |
3529 | /* Look for the closing `>'. */ | |
3530 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
3531 | /* Restore the old message. */ | |
3532 | parser->type_definition_forbidden_message = saved_message; | |
3533 | ||
3534 | /* And the expression which is being cast. */ | |
3535 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3536 | expression = cp_parser_expression (parser); | |
3537 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3538 | ||
14d22dd6 MM |
3539 | /* Only type conversions to integral or enumeration types |
3540 | can be used in constant-expressions. */ | |
67c03833 | 3541 | if (parser->integral_constant_expression_p |
14d22dd6 | 3542 | && !dependent_type_p (type) |
263ee052 | 3543 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
625cbf93 MM |
3544 | && (cp_parser_non_integral_constant_expression |
3545 | (parser, | |
3546 | "a cast to a type other than an integral or " | |
3547 | "enumeration type"))) | |
3548 | return error_mark_node; | |
14d22dd6 | 3549 | |
a723baf1 MM |
3550 | switch (keyword) |
3551 | { | |
3552 | case RID_DYNCAST: | |
3553 | postfix_expression | |
3554 | = build_dynamic_cast (type, expression); | |
3555 | break; | |
3556 | case RID_STATCAST: | |
3557 | postfix_expression | |
3558 | = build_static_cast (type, expression); | |
3559 | break; | |
3560 | case RID_REINTCAST: | |
3561 | postfix_expression | |
3562 | = build_reinterpret_cast (type, expression); | |
3563 | break; | |
3564 | case RID_CONSTCAST: | |
3565 | postfix_expression | |
3566 | = build_const_cast (type, expression); | |
3567 | break; | |
3568 | default: | |
3569 | abort (); | |
3570 | } | |
3571 | } | |
3572 | break; | |
3573 | ||
3574 | case RID_TYPEID: | |
3575 | { | |
3576 | tree type; | |
3577 | const char *saved_message; | |
4f8163b1 | 3578 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3579 | |
3580 | /* Consume the `typeid' token. */ | |
3581 | cp_lexer_consume_token (parser->lexer); | |
3582 | /* Look for the `(' token. */ | |
3583 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
3584 | /* Types cannot be defined in a `typeid' expression. */ | |
3585 | saved_message = parser->type_definition_forbidden_message; | |
3586 | parser->type_definition_forbidden_message | |
3587 | = "types may not be defined in a `typeid\' expression"; | |
3588 | /* We can't be sure yet whether we're looking at a type-id or an | |
3589 | expression. */ | |
3590 | cp_parser_parse_tentatively (parser); | |
3591 | /* Try a type-id first. */ | |
4f8163b1 MM |
3592 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3593 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 3594 | type = cp_parser_type_id (parser); |
4f8163b1 | 3595 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
3596 | /* Look for the `)' token. Otherwise, we can't be sure that |
3597 | we're not looking at an expression: consider `typeid (int | |
3598 | (3))', for example. */ | |
3599 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3600 | /* If all went well, simply lookup the type-id. */ | |
3601 | if (cp_parser_parse_definitely (parser)) | |
3602 | postfix_expression = get_typeid (type); | |
3603 | /* Otherwise, fall back to the expression variant. */ | |
3604 | else | |
3605 | { | |
3606 | tree expression; | |
3607 | ||
3608 | /* Look for an expression. */ | |
3609 | expression = cp_parser_expression (parser); | |
3610 | /* Compute its typeid. */ | |
3611 | postfix_expression = build_typeid (expression); | |
3612 | /* Look for the `)' token. */ | |
3613 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3614 | } | |
4424e0da GB |
3615 | /* `typeid' may not appear in an integral constant expression. */ |
3616 | if (cp_parser_non_integral_constant_expression(parser, | |
3617 | "`typeid' operator")) | |
3618 | return error_mark_node; | |
a723baf1 MM |
3619 | /* Restore the saved message. */ |
3620 | parser->type_definition_forbidden_message = saved_message; | |
3621 | } | |
3622 | break; | |
21526606 | 3623 | |
a723baf1 MM |
3624 | case RID_TYPENAME: |
3625 | { | |
3626 | bool template_p = false; | |
3627 | tree id; | |
3628 | tree type; | |
3629 | ||
3630 | /* Consume the `typename' token. */ | |
3631 | cp_lexer_consume_token (parser->lexer); | |
3632 | /* Look for the optional `::' operator. */ | |
21526606 | 3633 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
3634 | /*current_scope_valid_p=*/false); |
3635 | /* Look for the nested-name-specifier. */ | |
3636 | cp_parser_nested_name_specifier (parser, | |
3637 | /*typename_keyword_p=*/true, | |
3638 | /*check_dependency_p=*/true, | |
a668c6ad MM |
3639 | /*type_p=*/true, |
3640 | /*is_declaration=*/true); | |
a723baf1 MM |
3641 | /* Look for the optional `template' keyword. */ |
3642 | template_p = cp_parser_optional_template_keyword (parser); | |
3643 | /* We don't know whether we're looking at a template-id or an | |
3644 | identifier. */ | |
3645 | cp_parser_parse_tentatively (parser); | |
3646 | /* Try a template-id. */ | |
3647 | id = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
3648 | /*check_dependency_p=*/true, |
3649 | /*is_declaration=*/true); | |
a723baf1 MM |
3650 | /* If that didn't work, try an identifier. */ |
3651 | if (!cp_parser_parse_definitely (parser)) | |
3652 | id = cp_parser_identifier (parser); | |
26bcf8fc MM |
3653 | /* If we look up a template-id in a non-dependent qualifying |
3654 | scope, there's no need to create a dependent type. */ | |
3655 | if (TREE_CODE (id) == TYPE_DECL | |
3656 | && !dependent_type_p (parser->scope)) | |
3657 | type = TREE_TYPE (id); | |
a723baf1 MM |
3658 | /* Create a TYPENAME_TYPE to represent the type to which the |
3659 | functional cast is being performed. */ | |
26bcf8fc MM |
3660 | else |
3661 | type = make_typename_type (parser->scope, id, | |
3662 | /*complain=*/1); | |
a723baf1 MM |
3663 | |
3664 | postfix_expression = cp_parser_functional_cast (parser, type); | |
3665 | } | |
3666 | break; | |
3667 | ||
3668 | default: | |
3669 | { | |
3670 | tree type; | |
3671 | ||
3672 | /* If the next thing is a simple-type-specifier, we may be | |
3673 | looking at a functional cast. We could also be looking at | |
3674 | an id-expression. So, we try the functional cast, and if | |
3675 | that doesn't work we fall back to the primary-expression. */ | |
3676 | cp_parser_parse_tentatively (parser); | |
3677 | /* Look for the simple-type-specifier. */ | |
21526606 | 3678 | type = cp_parser_simple_type_specifier (parser, |
4b0d3cbe MM |
3679 | CP_PARSER_FLAGS_NONE, |
3680 | /*identifier_p=*/false); | |
a723baf1 MM |
3681 | /* Parse the cast itself. */ |
3682 | if (!cp_parser_error_occurred (parser)) | |
21526606 | 3683 | postfix_expression |
a723baf1 MM |
3684 | = cp_parser_functional_cast (parser, type); |
3685 | /* If that worked, we're done. */ | |
3686 | if (cp_parser_parse_definitely (parser)) | |
3687 | break; | |
3688 | ||
3689 | /* If the functional-cast didn't work out, try a | |
3690 | compound-literal. */ | |
14d22dd6 MM |
3691 | if (cp_parser_allow_gnu_extensions_p (parser) |
3692 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
a723baf1 MM |
3693 | { |
3694 | tree initializer_list = NULL_TREE; | |
4f8163b1 | 3695 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
3696 | |
3697 | cp_parser_parse_tentatively (parser); | |
14d22dd6 MM |
3698 | /* Consume the `('. */ |
3699 | cp_lexer_consume_token (parser->lexer); | |
3700 | /* Parse the type. */ | |
4f8163b1 MM |
3701 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
3702 | parser->in_type_id_in_expr_p = true; | |
14d22dd6 | 3703 | type = cp_parser_type_id (parser); |
4f8163b1 | 3704 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
14d22dd6 MM |
3705 | /* Look for the `)'. */ |
3706 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
3707 | /* Look for the `{'. */ | |
3708 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
3709 | /* If things aren't going well, there's no need to | |
3710 | keep going. */ | |
3711 | if (!cp_parser_error_occurred (parser)) | |
a723baf1 | 3712 | { |
39703eb9 | 3713 | bool non_constant_p; |
14d22dd6 | 3714 | /* Parse the initializer-list. */ |
21526606 | 3715 | initializer_list |
39703eb9 | 3716 | = cp_parser_initializer_list (parser, &non_constant_p); |
14d22dd6 MM |
3717 | /* Allow a trailing `,'. */ |
3718 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
3719 | cp_lexer_consume_token (parser->lexer); | |
3720 | /* Look for the final `}'. */ | |
3721 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
a723baf1 MM |
3722 | } |
3723 | /* If that worked, we're definitely looking at a | |
3724 | compound-literal expression. */ | |
3725 | if (cp_parser_parse_definitely (parser)) | |
3726 | { | |
3727 | /* Warn the user that a compound literal is not | |
3728 | allowed in standard C++. */ | |
3729 | if (pedantic) | |
3730 | pedwarn ("ISO C++ forbids compound-literals"); | |
3731 | /* Form the representation of the compound-literal. */ | |
21526606 | 3732 | postfix_expression |
a723baf1 MM |
3733 | = finish_compound_literal (type, initializer_list); |
3734 | break; | |
3735 | } | |
3736 | } | |
3737 | ||
3738 | /* It must be a primary-expression. */ | |
21526606 | 3739 | postfix_expression = cp_parser_primary_expression (parser, |
a723baf1 MM |
3740 | &idk, |
3741 | &qualifying_class); | |
3742 | } | |
3743 | break; | |
3744 | } | |
3745 | ||
ee76b931 MM |
3746 | /* If we were avoiding committing to the processing of a |
3747 | qualified-id until we knew whether or not we had a | |
3748 | pointer-to-member, we now know. */ | |
089d6ea7 | 3749 | if (qualifying_class) |
a723baf1 | 3750 | { |
ee76b931 | 3751 | bool done; |
a723baf1 | 3752 | |
ee76b931 MM |
3753 | /* Peek at the next token. */ |
3754 | token = cp_lexer_peek_token (parser->lexer); | |
3755 | done = (token->type != CPP_OPEN_SQUARE | |
3756 | && token->type != CPP_OPEN_PAREN | |
3757 | && token->type != CPP_DOT | |
3758 | && token->type != CPP_DEREF | |
3759 | && token->type != CPP_PLUS_PLUS | |
3760 | && token->type != CPP_MINUS_MINUS); | |
3761 | ||
3762 | postfix_expression = finish_qualified_id_expr (qualifying_class, | |
3763 | postfix_expression, | |
3764 | done, | |
3765 | address_p); | |
3766 | if (done) | |
3767 | return postfix_expression; | |
a723baf1 MM |
3768 | } |
3769 | ||
a723baf1 MM |
3770 | /* Keep looping until the postfix-expression is complete. */ |
3771 | while (true) | |
3772 | { | |
10b1d5e7 MM |
3773 | if (idk == CP_ID_KIND_UNQUALIFIED |
3774 | && TREE_CODE (postfix_expression) == IDENTIFIER_NODE | |
a723baf1 | 3775 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) |
b3445994 | 3776 | /* It is not a Koenig lookup function call. */ |
21526606 | 3777 | postfix_expression |
b3445994 | 3778 | = unqualified_name_lookup_error (postfix_expression); |
21526606 | 3779 | |
a723baf1 MM |
3780 | /* Peek at the next token. */ |
3781 | token = cp_lexer_peek_token (parser->lexer); | |
3782 | ||
3783 | switch (token->type) | |
3784 | { | |
3785 | case CPP_OPEN_SQUARE: | |
7a3ea201 RH |
3786 | postfix_expression |
3787 | = cp_parser_postfix_open_square_expression (parser, | |
3788 | postfix_expression, | |
3789 | false); | |
3790 | idk = CP_ID_KIND_NONE; | |
a723baf1 MM |
3791 | break; |
3792 | ||
3793 | case CPP_OPEN_PAREN: | |
3794 | /* postfix-expression ( expression-list [opt] ) */ | |
3795 | { | |
6d80c4b9 | 3796 | bool koenig_p; |
21526606 | 3797 | tree args = (cp_parser_parenthesized_expression_list |
39703eb9 | 3798 | (parser, false, /*non_constant_p=*/NULL)); |
a723baf1 | 3799 | |
7efa3e22 NS |
3800 | if (args == error_mark_node) |
3801 | { | |
3802 | postfix_expression = error_mark_node; | |
3803 | break; | |
3804 | } | |
21526606 | 3805 | |
14d22dd6 MM |
3806 | /* Function calls are not permitted in |
3807 | constant-expressions. */ | |
625cbf93 MM |
3808 | if (cp_parser_non_integral_constant_expression (parser, |
3809 | "a function call")) | |
14d22dd6 | 3810 | { |
625cbf93 MM |
3811 | postfix_expression = error_mark_node; |
3812 | break; | |
14d22dd6 | 3813 | } |
a723baf1 | 3814 | |
6d80c4b9 | 3815 | koenig_p = false; |
399dedb9 NS |
3816 | if (idk == CP_ID_KIND_UNQUALIFIED) |
3817 | { | |
676e33ca MM |
3818 | /* We do not perform argument-dependent lookup if |
3819 | normal lookup finds a non-function, in accordance | |
3820 | with the expected resolution of DR 218. */ | |
399dedb9 NS |
3821 | if (args |
3822 | && (is_overloaded_fn (postfix_expression) | |
399dedb9 | 3823 | || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
3824 | { |
3825 | koenig_p = true; | |
21526606 | 3826 | postfix_expression |
6d80c4b9 MM |
3827 | = perform_koenig_lookup (postfix_expression, args); |
3828 | } | |
399dedb9 NS |
3829 | else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) |
3830 | postfix_expression | |
3831 | = unqualified_fn_lookup_error (postfix_expression); | |
3832 | } | |
21526606 | 3833 | |
d17811fd | 3834 | if (TREE_CODE (postfix_expression) == COMPONENT_REF) |
a723baf1 | 3835 | { |
d17811fd MM |
3836 | tree instance = TREE_OPERAND (postfix_expression, 0); |
3837 | tree fn = TREE_OPERAND (postfix_expression, 1); | |
3838 | ||
3839 | if (processing_template_decl | |
3840 | && (type_dependent_expression_p (instance) | |
3841 | || (!BASELINK_P (fn) | |
3842 | && TREE_CODE (fn) != FIELD_DECL) | |
584672ee | 3843 | || type_dependent_expression_p (fn) |
d17811fd MM |
3844 | || any_type_dependent_arguments_p (args))) |
3845 | { | |
3846 | postfix_expression | |
6de9cd9a DN |
3847 | = build_min_nt (CALL_EXPR, postfix_expression, |
3848 | args, NULL_TREE); | |
d17811fd MM |
3849 | break; |
3850 | } | |
9f880ef9 MM |
3851 | |
3852 | if (BASELINK_P (fn)) | |
3853 | postfix_expression | |
21526606 EC |
3854 | = (build_new_method_call |
3855 | (instance, fn, args, NULL_TREE, | |
3856 | (idk == CP_ID_KIND_QUALIFIED | |
9f880ef9 MM |
3857 | ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL))); |
3858 | else | |
3859 | postfix_expression | |
3860 | = finish_call_expr (postfix_expression, args, | |
3861 | /*disallow_virtual=*/false, | |
3862 | /*koenig_p=*/false); | |
a723baf1 | 3863 | } |
d17811fd MM |
3864 | else if (TREE_CODE (postfix_expression) == OFFSET_REF |
3865 | || TREE_CODE (postfix_expression) == MEMBER_REF | |
3866 | || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) | |
a723baf1 MM |
3867 | postfix_expression = (build_offset_ref_call_from_tree |
3868 | (postfix_expression, args)); | |
b3445994 | 3869 | else if (idk == CP_ID_KIND_QUALIFIED) |
2050a1bb MM |
3870 | /* A call to a static class member, or a namespace-scope |
3871 | function. */ | |
3872 | postfix_expression | |
3873 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3874 | /*disallow_virtual=*/true, |
3875 | koenig_p); | |
a723baf1 | 3876 | else |
2050a1bb | 3877 | /* All other function calls. */ |
21526606 EC |
3878 | postfix_expression |
3879 | = finish_call_expr (postfix_expression, args, | |
6d80c4b9 MM |
3880 | /*disallow_virtual=*/false, |
3881 | koenig_p); | |
a723baf1 MM |
3882 | |
3883 | /* The POSTFIX_EXPRESSION is certainly no longer an id. */ | |
b3445994 | 3884 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3885 | } |
3886 | break; | |
21526606 | 3887 | |
a723baf1 MM |
3888 | case CPP_DOT: |
3889 | case CPP_DEREF: | |
21526606 EC |
3890 | /* postfix-expression . template [opt] id-expression |
3891 | postfix-expression . pseudo-destructor-name | |
a723baf1 MM |
3892 | postfix-expression -> template [opt] id-expression |
3893 | postfix-expression -> pseudo-destructor-name */ | |
7a3ea201 RH |
3894 | |
3895 | /* Consume the `.' or `->' operator. */ | |
3896 | cp_lexer_consume_token (parser->lexer); | |
a723baf1 | 3897 | |
7a3ea201 RH |
3898 | postfix_expression |
3899 | = cp_parser_postfix_dot_deref_expression (parser, token->type, | |
3900 | postfix_expression, | |
3901 | false, &idk); | |
a723baf1 MM |
3902 | break; |
3903 | ||
3904 | case CPP_PLUS_PLUS: | |
3905 | /* postfix-expression ++ */ | |
3906 | /* Consume the `++' token. */ | |
3907 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 3908 | /* Generate a representation for the complete expression. */ |
21526606 EC |
3909 | postfix_expression |
3910 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 3911 | POSTINCREMENT_EXPR); |
14d22dd6 | 3912 | /* Increments may not appear in constant-expressions. */ |
625cbf93 MM |
3913 | if (cp_parser_non_integral_constant_expression (parser, |
3914 | "an increment")) | |
3915 | postfix_expression = error_mark_node; | |
b3445994 | 3916 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3917 | break; |
3918 | ||
3919 | case CPP_MINUS_MINUS: | |
3920 | /* postfix-expression -- */ | |
3921 | /* Consume the `--' token. */ | |
3922 | cp_lexer_consume_token (parser->lexer); | |
a5ac3982 | 3923 | /* Generate a representation for the complete expression. */ |
21526606 EC |
3924 | postfix_expression |
3925 | = finish_increment_expr (postfix_expression, | |
a5ac3982 | 3926 | POSTDECREMENT_EXPR); |
14d22dd6 | 3927 | /* Decrements may not appear in constant-expressions. */ |
625cbf93 MM |
3928 | if (cp_parser_non_integral_constant_expression (parser, |
3929 | "a decrement")) | |
3930 | postfix_expression = error_mark_node; | |
b3445994 | 3931 | idk = CP_ID_KIND_NONE; |
a723baf1 MM |
3932 | break; |
3933 | ||
3934 | default: | |
3935 | return postfix_expression; | |
3936 | } | |
3937 | } | |
3938 | ||
3939 | /* We should never get here. */ | |
3940 | abort (); | |
3941 | return error_mark_node; | |
3942 | } | |
3943 | ||
7a3ea201 RH |
3944 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked |
3945 | by cp_parser_builtin_offsetof. We're looking for | |
3946 | ||
3947 | postfix-expression [ expression ] | |
3948 | ||
3949 | FOR_OFFSETOF is set if we're being called in that context, which | |
3950 | changes how we deal with integer constant expressions. */ | |
3951 | ||
3952 | static tree | |
3953 | cp_parser_postfix_open_square_expression (cp_parser *parser, | |
3954 | tree postfix_expression, | |
3955 | bool for_offsetof) | |
3956 | { | |
3957 | tree index; | |
3958 | ||
3959 | /* Consume the `[' token. */ | |
3960 | cp_lexer_consume_token (parser->lexer); | |
3961 | ||
3962 | /* Parse the index expression. */ | |
3963 | /* ??? For offsetof, there is a question of what to allow here. If | |
3964 | offsetof is not being used in an integral constant expression context, | |
3965 | then we *could* get the right answer by computing the value at runtime. | |
3966 | If we are in an integral constant expression context, then we might | |
3967 | could accept any constant expression; hard to say without analysis. | |
3968 | Rather than open the barn door too wide right away, allow only integer | |
3969 | constant expresions here. */ | |
3970 | if (for_offsetof) | |
3971 | index = cp_parser_constant_expression (parser, false, NULL); | |
3972 | else | |
3973 | index = cp_parser_expression (parser); | |
3974 | ||
3975 | /* Look for the closing `]'. */ | |
3976 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
3977 | ||
3978 | /* Build the ARRAY_REF. */ | |
3979 | postfix_expression = grok_array_decl (postfix_expression, index); | |
3980 | ||
3981 | /* When not doing offsetof, array references are not permitted in | |
3982 | constant-expressions. */ | |
3983 | if (!for_offsetof | |
3984 | && (cp_parser_non_integral_constant_expression | |
3985 | (parser, "an array reference"))) | |
3986 | postfix_expression = error_mark_node; | |
3987 | ||
3988 | return postfix_expression; | |
3989 | } | |
3990 | ||
3991 | /* A subroutine of cp_parser_postfix_expression that also gets hijacked | |
3992 | by cp_parser_builtin_offsetof. We're looking for | |
3993 | ||
3994 | postfix-expression . template [opt] id-expression | |
3995 | postfix-expression . pseudo-destructor-name | |
3996 | postfix-expression -> template [opt] id-expression | |
3997 | postfix-expression -> pseudo-destructor-name | |
3998 | ||
3999 | FOR_OFFSETOF is set if we're being called in that context. That sorta | |
4000 | limits what of the above we'll actually accept, but nevermind. | |
4001 | TOKEN_TYPE is the "." or "->" token, which will already have been | |
4002 | removed from the stream. */ | |
4003 | ||
4004 | static tree | |
4005 | cp_parser_postfix_dot_deref_expression (cp_parser *parser, | |
4006 | enum cpp_ttype token_type, | |
4007 | tree postfix_expression, | |
4008 | bool for_offsetof, cp_id_kind *idk) | |
4009 | { | |
4010 | tree name; | |
4011 | bool dependent_p; | |
4012 | bool template_p; | |
4013 | tree scope = NULL_TREE; | |
4014 | ||
4015 | /* If this is a `->' operator, dereference the pointer. */ | |
4016 | if (token_type == CPP_DEREF) | |
4017 | postfix_expression = build_x_arrow (postfix_expression); | |
4018 | /* Check to see whether or not the expression is type-dependent. */ | |
4019 | dependent_p = type_dependent_expression_p (postfix_expression); | |
4020 | /* The identifier following the `->' or `.' is not qualified. */ | |
4021 | parser->scope = NULL_TREE; | |
4022 | parser->qualifying_scope = NULL_TREE; | |
4023 | parser->object_scope = NULL_TREE; | |
4024 | *idk = CP_ID_KIND_NONE; | |
4025 | /* Enter the scope corresponding to the type of the object | |
4026 | given by the POSTFIX_EXPRESSION. */ | |
4027 | if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE) | |
4028 | { | |
4029 | scope = TREE_TYPE (postfix_expression); | |
4030 | /* According to the standard, no expression should ever have | |
4031 | reference type. Unfortunately, we do not currently match | |
4032 | the standard in this respect in that our internal representation | |
4033 | of an expression may have reference type even when the standard | |
4034 | says it does not. Therefore, we have to manually obtain the | |
4035 | underlying type here. */ | |
4036 | scope = non_reference (scope); | |
4037 | /* The type of the POSTFIX_EXPRESSION must be complete. */ | |
4038 | scope = complete_type_or_else (scope, NULL_TREE); | |
4039 | /* Let the name lookup machinery know that we are processing a | |
4040 | class member access expression. */ | |
4041 | parser->context->object_type = scope; | |
4042 | /* If something went wrong, we want to be able to discern that case, | |
4043 | as opposed to the case where there was no SCOPE due to the type | |
4044 | of expression being dependent. */ | |
4045 | if (!scope) | |
4046 | scope = error_mark_node; | |
4047 | /* If the SCOPE was erroneous, make the various semantic analysis | |
4048 | functions exit quickly -- and without issuing additional error | |
4049 | messages. */ | |
4050 | if (scope == error_mark_node) | |
4051 | postfix_expression = error_mark_node; | |
4052 | } | |
4053 | ||
4054 | /* If the SCOPE is not a scalar type, we are looking at an | |
4055 | ordinary class member access expression, rather than a | |
4056 | pseudo-destructor-name. */ | |
4057 | if (!scope || !SCALAR_TYPE_P (scope)) | |
4058 | { | |
4059 | template_p = cp_parser_optional_template_keyword (parser); | |
4060 | /* Parse the id-expression. */ | |
4061 | name = cp_parser_id_expression (parser, template_p, | |
4062 | /*check_dependency_p=*/true, | |
4063 | /*template_p=*/NULL, | |
4064 | /*declarator_p=*/false); | |
4065 | /* In general, build a SCOPE_REF if the member name is qualified. | |
4066 | However, if the name was not dependent and has already been | |
4067 | resolved; there is no need to build the SCOPE_REF. For example; | |
4068 | ||
4069 | struct X { void f(); }; | |
4070 | template <typename T> void f(T* t) { t->X::f(); } | |
4071 | ||
4072 | Even though "t" is dependent, "X::f" is not and has been resolved | |
4073 | to a BASELINK; there is no need to include scope information. */ | |
4074 | ||
4075 | /* But we do need to remember that there was an explicit scope for | |
4076 | virtual function calls. */ | |
4077 | if (parser->scope) | |
4078 | *idk = CP_ID_KIND_QUALIFIED; | |
4079 | ||
4080 | if (name != error_mark_node && !BASELINK_P (name) && parser->scope) | |
4081 | { | |
4082 | name = build_nt (SCOPE_REF, parser->scope, name); | |
4083 | parser->scope = NULL_TREE; | |
4084 | parser->qualifying_scope = NULL_TREE; | |
4085 | parser->object_scope = NULL_TREE; | |
4086 | } | |
4087 | if (scope && name && BASELINK_P (name)) | |
4088 | adjust_result_of_qualified_name_lookup | |
4089 | (name, BINFO_TYPE (BASELINK_BINFO (name)), scope); | |
4090 | postfix_expression | |
4091 | = finish_class_member_access_expr (postfix_expression, name); | |
4092 | } | |
4093 | /* Otherwise, try the pseudo-destructor-name production. */ | |
4094 | else | |
4095 | { | |
4096 | tree s = NULL_TREE; | |
4097 | tree type; | |
4098 | ||
4099 | /* Parse the pseudo-destructor-name. */ | |
4100 | cp_parser_pseudo_destructor_name (parser, &s, &type); | |
4101 | /* Form the call. */ | |
4102 | postfix_expression | |
4103 | = finish_pseudo_destructor_expr (postfix_expression, | |
4104 | s, TREE_TYPE (type)); | |
4105 | } | |
4106 | ||
4107 | /* We no longer need to look up names in the scope of the object on | |
4108 | the left-hand side of the `.' or `->' operator. */ | |
4109 | parser->context->object_type = NULL_TREE; | |
4110 | ||
4111 | /* Outside of offsetof, these operators may not appear in | |
4112 | constant-expressions. */ | |
4113 | if (!for_offsetof | |
4114 | && (cp_parser_non_integral_constant_expression | |
4115 | (parser, token_type == CPP_DEREF ? "'->'" : "`.'"))) | |
4116 | postfix_expression = error_mark_node; | |
4117 | ||
4118 | return postfix_expression; | |
4119 | } | |
4120 | ||
7efa3e22 | 4121 | /* Parse a parenthesized expression-list. |
a723baf1 MM |
4122 | |
4123 | expression-list: | |
4124 | assignment-expression | |
4125 | expression-list, assignment-expression | |
4126 | ||
7efa3e22 NS |
4127 | attribute-list: |
4128 | expression-list | |
4129 | identifier | |
4130 | identifier, expression-list | |
4131 | ||
a723baf1 MM |
4132 | Returns a TREE_LIST. The TREE_VALUE of each node is a |
4133 | representation of an assignment-expression. Note that a TREE_LIST | |
7efa3e22 NS |
4134 | is returned even if there is only a single expression in the list. |
4135 | error_mark_node is returned if the ( and or ) are | |
4136 | missing. NULL_TREE is returned on no expressions. The parentheses | |
4137 | are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute | |
39703eb9 MM |
4138 | list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P |
4139 | indicates whether or not all of the expressions in the list were | |
4140 | constant. */ | |
a723baf1 MM |
4141 | |
4142 | static tree | |
21526606 | 4143 | cp_parser_parenthesized_expression_list (cp_parser* parser, |
39703eb9 MM |
4144 | bool is_attribute_list, |
4145 | bool *non_constant_p) | |
a723baf1 MM |
4146 | { |
4147 | tree expression_list = NULL_TREE; | |
7efa3e22 | 4148 | tree identifier = NULL_TREE; |
39703eb9 MM |
4149 | |
4150 | /* Assume all the expressions will be constant. */ | |
4151 | if (non_constant_p) | |
4152 | *non_constant_p = false; | |
4153 | ||
7efa3e22 NS |
4154 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
4155 | return error_mark_node; | |
21526606 | 4156 | |
a723baf1 | 4157 | /* Consume expressions until there are no more. */ |
7efa3e22 NS |
4158 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) |
4159 | while (true) | |
4160 | { | |
4161 | tree expr; | |
21526606 | 4162 | |
7efa3e22 NS |
4163 | /* At the beginning of attribute lists, check to see if the |
4164 | next token is an identifier. */ | |
4165 | if (is_attribute_list | |
4166 | && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) | |
4167 | { | |
4168 | cp_token *token; | |
21526606 | 4169 | |
7efa3e22 NS |
4170 | /* Consume the identifier. */ |
4171 | token = cp_lexer_consume_token (parser->lexer); | |
4172 | /* Save the identifier. */ | |
4173 | identifier = token->value; | |
4174 | } | |
4175 | else | |
4176 | { | |
4177 | /* Parse the next assignment-expression. */ | |
39703eb9 MM |
4178 | if (non_constant_p) |
4179 | { | |
4180 | bool expr_non_constant_p; | |
21526606 | 4181 | expr = (cp_parser_constant_expression |
39703eb9 MM |
4182 | (parser, /*allow_non_constant_p=*/true, |
4183 | &expr_non_constant_p)); | |
4184 | if (expr_non_constant_p) | |
4185 | *non_constant_p = true; | |
4186 | } | |
4187 | else | |
4188 | expr = cp_parser_assignment_expression (parser); | |
a723baf1 | 4189 | |
7efa3e22 NS |
4190 | /* Add it to the list. We add error_mark_node |
4191 | expressions to the list, so that we can still tell if | |
4192 | the correct form for a parenthesized expression-list | |
4193 | is found. That gives better errors. */ | |
4194 | expression_list = tree_cons (NULL_TREE, expr, expression_list); | |
a723baf1 | 4195 | |
7efa3e22 NS |
4196 | if (expr == error_mark_node) |
4197 | goto skip_comma; | |
4198 | } | |
a723baf1 | 4199 | |
7efa3e22 NS |
4200 | /* After the first item, attribute lists look the same as |
4201 | expression lists. */ | |
4202 | is_attribute_list = false; | |
21526606 | 4203 | |
7efa3e22 NS |
4204 | get_comma:; |
4205 | /* If the next token isn't a `,', then we are done. */ | |
4206 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
4207 | break; | |
4208 | ||
4209 | /* Otherwise, consume the `,' and keep going. */ | |
4210 | cp_lexer_consume_token (parser->lexer); | |
4211 | } | |
21526606 | 4212 | |
7efa3e22 NS |
4213 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) |
4214 | { | |
4215 | int ending; | |
21526606 | 4216 | |
7efa3e22 NS |
4217 | skip_comma:; |
4218 | /* We try and resync to an unnested comma, as that will give the | |
4219 | user better diagnostics. */ | |
21526606 EC |
4220 | ending = cp_parser_skip_to_closing_parenthesis (parser, |
4221 | /*recovering=*/true, | |
4bb8ca28 | 4222 | /*or_comma=*/true, |
a668c6ad | 4223 | /*consume_paren=*/true); |
7efa3e22 NS |
4224 | if (ending < 0) |
4225 | goto get_comma; | |
4226 | if (!ending) | |
4227 | return error_mark_node; | |
a723baf1 MM |
4228 | } |
4229 | ||
4230 | /* We built up the list in reverse order so we must reverse it now. */ | |
7efa3e22 NS |
4231 | expression_list = nreverse (expression_list); |
4232 | if (identifier) | |
4233 | expression_list = tree_cons (NULL_TREE, identifier, expression_list); | |
21526606 | 4234 | |
7efa3e22 | 4235 | return expression_list; |
a723baf1 MM |
4236 | } |
4237 | ||
4238 | /* Parse a pseudo-destructor-name. | |
4239 | ||
4240 | pseudo-destructor-name: | |
4241 | :: [opt] nested-name-specifier [opt] type-name :: ~ type-name | |
4242 | :: [opt] nested-name-specifier template template-id :: ~ type-name | |
4243 | :: [opt] nested-name-specifier [opt] ~ type-name | |
4244 | ||
4245 | If either of the first two productions is used, sets *SCOPE to the | |
4246 | TYPE specified before the final `::'. Otherwise, *SCOPE is set to | |
4247 | NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, | |
d6e57462 | 4248 | or ERROR_MARK_NODE if the parse fails. */ |
a723baf1 MM |
4249 | |
4250 | static void | |
21526606 EC |
4251 | cp_parser_pseudo_destructor_name (cp_parser* parser, |
4252 | tree* scope, | |
94edc4ab | 4253 | tree* type) |
a723baf1 MM |
4254 | { |
4255 | bool nested_name_specifier_p; | |
4256 | ||
4257 | /* Look for the optional `::' operator. */ | |
4258 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); | |
4259 | /* Look for the optional nested-name-specifier. */ | |
21526606 | 4260 | nested_name_specifier_p |
a723baf1 MM |
4261 | = (cp_parser_nested_name_specifier_opt (parser, |
4262 | /*typename_keyword_p=*/false, | |
4263 | /*check_dependency_p=*/true, | |
a668c6ad | 4264 | /*type_p=*/false, |
21526606 | 4265 | /*is_declaration=*/true) |
a723baf1 MM |
4266 | != NULL_TREE); |
4267 | /* Now, if we saw a nested-name-specifier, we might be doing the | |
4268 | second production. */ | |
21526606 | 4269 | if (nested_name_specifier_p |
a723baf1 MM |
4270 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) |
4271 | { | |
4272 | /* Consume the `template' keyword. */ | |
4273 | cp_lexer_consume_token (parser->lexer); | |
4274 | /* Parse the template-id. */ | |
21526606 | 4275 | cp_parser_template_id (parser, |
a723baf1 | 4276 | /*template_keyword_p=*/true, |
a668c6ad MM |
4277 | /*check_dependency_p=*/false, |
4278 | /*is_declaration=*/true); | |
a723baf1 MM |
4279 | /* Look for the `::' token. */ |
4280 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4281 | } | |
4282 | /* If the next token is not a `~', then there might be some | |
9bcb9aae | 4283 | additional qualification. */ |
a723baf1 MM |
4284 | else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) |
4285 | { | |
4286 | /* Look for the type-name. */ | |
4287 | *scope = TREE_TYPE (cp_parser_type_name (parser)); | |
d6e57462 ILT |
4288 | |
4289 | /* If we didn't get an aggregate type, or we don't have ::~, | |
4290 | then something has gone wrong. Since the only caller of this | |
4291 | function is looking for something after `.' or `->' after a | |
4292 | scalar type, most likely the program is trying to get a | |
4293 | member of a non-aggregate type. */ | |
4294 | if (*scope == error_mark_node | |
4295 | || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE) | |
4296 | || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL) | |
4297 | { | |
4298 | cp_parser_error (parser, "request for member of non-aggregate type"); | |
4299 | *type = error_mark_node; | |
4300 | return; | |
4301 | } | |
4302 | ||
a723baf1 MM |
4303 | /* Look for the `::' token. */ |
4304 | cp_parser_require (parser, CPP_SCOPE, "`::'"); | |
4305 | } | |
4306 | else | |
4307 | *scope = NULL_TREE; | |
4308 | ||
4309 | /* Look for the `~'. */ | |
4310 | cp_parser_require (parser, CPP_COMPL, "`~'"); | |
4311 | /* Look for the type-name again. We are not responsible for | |
4312 | checking that it matches the first type-name. */ | |
4313 | *type = cp_parser_type_name (parser); | |
4314 | } | |
4315 | ||
4316 | /* Parse a unary-expression. | |
4317 | ||
4318 | unary-expression: | |
4319 | postfix-expression | |
4320 | ++ cast-expression | |
4321 | -- cast-expression | |
4322 | unary-operator cast-expression | |
4323 | sizeof unary-expression | |
4324 | sizeof ( type-id ) | |
4325 | new-expression | |
4326 | delete-expression | |
4327 | ||
4328 | GNU Extensions: | |
4329 | ||
4330 | unary-expression: | |
4331 | __extension__ cast-expression | |
4332 | __alignof__ unary-expression | |
4333 | __alignof__ ( type-id ) | |
4334 | __real__ cast-expression | |
4335 | __imag__ cast-expression | |
4336 | && identifier | |
4337 | ||
4338 | ADDRESS_P is true iff the unary-expression is appearing as the | |
4339 | operand of the `&' operator. | |
4340 | ||
34cd5ae7 | 4341 | Returns a representation of the expression. */ |
a723baf1 MM |
4342 | |
4343 | static tree | |
4344 | cp_parser_unary_expression (cp_parser *parser, bool address_p) | |
4345 | { | |
4346 | cp_token *token; | |
4347 | enum tree_code unary_operator; | |
4348 | ||
4349 | /* Peek at the next token. */ | |
4350 | token = cp_lexer_peek_token (parser->lexer); | |
4351 | /* Some keywords give away the kind of expression. */ | |
4352 | if (token->type == CPP_KEYWORD) | |
4353 | { | |
4354 | enum rid keyword = token->keyword; | |
4355 | ||
4356 | switch (keyword) | |
4357 | { | |
4358 | case RID_ALIGNOF: | |
a723baf1 MM |
4359 | case RID_SIZEOF: |
4360 | { | |
4361 | tree operand; | |
7a18b933 | 4362 | enum tree_code op; |
21526606 | 4363 | |
7a18b933 NS |
4364 | op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; |
4365 | /* Consume the token. */ | |
a723baf1 MM |
4366 | cp_lexer_consume_token (parser->lexer); |
4367 | /* Parse the operand. */ | |
4368 | operand = cp_parser_sizeof_operand (parser, keyword); | |
4369 | ||
7a18b933 NS |
4370 | if (TYPE_P (operand)) |
4371 | return cxx_sizeof_or_alignof_type (operand, op, true); | |
a723baf1 | 4372 | else |
7a18b933 | 4373 | return cxx_sizeof_or_alignof_expr (operand, op); |
a723baf1 MM |
4374 | } |
4375 | ||
4376 | case RID_NEW: | |
4377 | return cp_parser_new_expression (parser); | |
4378 | ||
4379 | case RID_DELETE: | |
4380 | return cp_parser_delete_expression (parser); | |
21526606 | 4381 | |
a723baf1 MM |
4382 | case RID_EXTENSION: |
4383 | { | |
4384 | /* The saved value of the PEDANTIC flag. */ | |
4385 | int saved_pedantic; | |
4386 | tree expr; | |
4387 | ||
4388 | /* Save away the PEDANTIC flag. */ | |
4389 | cp_parser_extension_opt (parser, &saved_pedantic); | |
4390 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4391 | expr = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4392 | /* Restore the PEDANTIC flag. */ |
4393 | pedantic = saved_pedantic; | |
4394 | ||
4395 | return expr; | |
4396 | } | |
4397 | ||
4398 | case RID_REALPART: | |
4399 | case RID_IMAGPART: | |
4400 | { | |
4401 | tree expression; | |
4402 | ||
4403 | /* Consume the `__real__' or `__imag__' token. */ | |
4404 | cp_lexer_consume_token (parser->lexer); | |
4405 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4406 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 MM |
4407 | /* Create the complete representation. */ |
4408 | return build_x_unary_op ((keyword == RID_REALPART | |
4409 | ? REALPART_EXPR : IMAGPART_EXPR), | |
4410 | expression); | |
4411 | } | |
4412 | break; | |
4413 | ||
4414 | default: | |
4415 | break; | |
4416 | } | |
4417 | } | |
4418 | ||
4419 | /* Look for the `:: new' and `:: delete', which also signal the | |
4420 | beginning of a new-expression, or delete-expression, | |
4421 | respectively. If the next token is `::', then it might be one of | |
4422 | these. */ | |
4423 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
4424 | { | |
4425 | enum rid keyword; | |
4426 | ||
4427 | /* See if the token after the `::' is one of the keywords in | |
4428 | which we're interested. */ | |
4429 | keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; | |
4430 | /* If it's `new', we have a new-expression. */ | |
4431 | if (keyword == RID_NEW) | |
4432 | return cp_parser_new_expression (parser); | |
4433 | /* Similarly, for `delete'. */ | |
4434 | else if (keyword == RID_DELETE) | |
4435 | return cp_parser_delete_expression (parser); | |
4436 | } | |
4437 | ||
4438 | /* Look for a unary operator. */ | |
4439 | unary_operator = cp_parser_unary_operator (token); | |
4440 | /* The `++' and `--' operators can be handled similarly, even though | |
4441 | they are not technically unary-operators in the grammar. */ | |
4442 | if (unary_operator == ERROR_MARK) | |
4443 | { | |
4444 | if (token->type == CPP_PLUS_PLUS) | |
4445 | unary_operator = PREINCREMENT_EXPR; | |
4446 | else if (token->type == CPP_MINUS_MINUS) | |
4447 | unary_operator = PREDECREMENT_EXPR; | |
4448 | /* Handle the GNU address-of-label extension. */ | |
4449 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
4450 | && token->type == CPP_AND_AND) | |
4451 | { | |
4452 | tree identifier; | |
4453 | ||
4454 | /* Consume the '&&' token. */ | |
4455 | cp_lexer_consume_token (parser->lexer); | |
4456 | /* Look for the identifier. */ | |
4457 | identifier = cp_parser_identifier (parser); | |
4458 | /* Create an expression representing the address. */ | |
4459 | return finish_label_address_expr (identifier); | |
4460 | } | |
4461 | } | |
4462 | if (unary_operator != ERROR_MARK) | |
4463 | { | |
4464 | tree cast_expression; | |
a5ac3982 MM |
4465 | tree expression = error_mark_node; |
4466 | const char *non_constant_p = NULL; | |
a723baf1 MM |
4467 | |
4468 | /* Consume the operator token. */ | |
4469 | token = cp_lexer_consume_token (parser->lexer); | |
4470 | /* Parse the cast-expression. */ | |
21526606 | 4471 | cast_expression |
a723baf1 MM |
4472 | = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR); |
4473 | /* Now, build an appropriate representation. */ | |
4474 | switch (unary_operator) | |
4475 | { | |
4476 | case INDIRECT_REF: | |
a5ac3982 MM |
4477 | non_constant_p = "`*'"; |
4478 | expression = build_x_indirect_ref (cast_expression, "unary *"); | |
4479 | break; | |
4480 | ||
a723baf1 | 4481 | case ADDR_EXPR: |
7a3ea201 | 4482 | non_constant_p = "`&'"; |
a5ac3982 | 4483 | /* Fall through. */ |
d17811fd | 4484 | case BIT_NOT_EXPR: |
a5ac3982 MM |
4485 | expression = build_x_unary_op (unary_operator, cast_expression); |
4486 | break; | |
4487 | ||
14d22dd6 MM |
4488 | case PREINCREMENT_EXPR: |
4489 | case PREDECREMENT_EXPR: | |
a5ac3982 MM |
4490 | non_constant_p = (unary_operator == PREINCREMENT_EXPR |
4491 | ? "`++'" : "`--'"); | |
14d22dd6 | 4492 | /* Fall through. */ |
a723baf1 MM |
4493 | case CONVERT_EXPR: |
4494 | case NEGATE_EXPR: | |
4495 | case TRUTH_NOT_EXPR: | |
a5ac3982 MM |
4496 | expression = finish_unary_op_expr (unary_operator, cast_expression); |
4497 | break; | |
a723baf1 | 4498 | |
a723baf1 MM |
4499 | default: |
4500 | abort (); | |
a723baf1 | 4501 | } |
a5ac3982 | 4502 | |
625cbf93 MM |
4503 | if (non_constant_p |
4504 | && cp_parser_non_integral_constant_expression (parser, | |
4505 | non_constant_p)) | |
4506 | expression = error_mark_node; | |
a5ac3982 MM |
4507 | |
4508 | return expression; | |
a723baf1 MM |
4509 | } |
4510 | ||
4511 | return cp_parser_postfix_expression (parser, address_p); | |
4512 | } | |
4513 | ||
4514 | /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a | |
4515 | unary-operator, the corresponding tree code is returned. */ | |
4516 | ||
4517 | static enum tree_code | |
94edc4ab | 4518 | cp_parser_unary_operator (cp_token* token) |
a723baf1 MM |
4519 | { |
4520 | switch (token->type) | |
4521 | { | |
4522 | case CPP_MULT: | |
4523 | return INDIRECT_REF; | |
4524 | ||
4525 | case CPP_AND: | |
4526 | return ADDR_EXPR; | |
4527 | ||
4528 | case CPP_PLUS: | |
4529 | return CONVERT_EXPR; | |
4530 | ||
4531 | case CPP_MINUS: | |
4532 | return NEGATE_EXPR; | |
4533 | ||
4534 | case CPP_NOT: | |
4535 | return TRUTH_NOT_EXPR; | |
21526606 | 4536 | |
a723baf1 MM |
4537 | case CPP_COMPL: |
4538 | return BIT_NOT_EXPR; | |
4539 | ||
4540 | default: | |
4541 | return ERROR_MARK; | |
4542 | } | |
4543 | } | |
4544 | ||
4545 | /* Parse a new-expression. | |
4546 | ||
ca099ac8 | 4547 | new-expression: |
a723baf1 MM |
4548 | :: [opt] new new-placement [opt] new-type-id new-initializer [opt] |
4549 | :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] | |
4550 | ||
4551 | Returns a representation of the expression. */ | |
4552 | ||
4553 | static tree | |
94edc4ab | 4554 | cp_parser_new_expression (cp_parser* parser) |
a723baf1 MM |
4555 | { |
4556 | bool global_scope_p; | |
4557 | tree placement; | |
4558 | tree type; | |
4559 | tree initializer; | |
4560 | ||
4561 | /* Look for the optional `::' operator. */ | |
21526606 | 4562 | global_scope_p |
a723baf1 MM |
4563 | = (cp_parser_global_scope_opt (parser, |
4564 | /*current_scope_valid_p=*/false) | |
4565 | != NULL_TREE); | |
4566 | /* Look for the `new' operator. */ | |
4567 | cp_parser_require_keyword (parser, RID_NEW, "`new'"); | |
4568 | /* There's no easy way to tell a new-placement from the | |
4569 | `( type-id )' construct. */ | |
4570 | cp_parser_parse_tentatively (parser); | |
4571 | /* Look for a new-placement. */ | |
4572 | placement = cp_parser_new_placement (parser); | |
4573 | /* If that didn't work out, there's no new-placement. */ | |
4574 | if (!cp_parser_parse_definitely (parser)) | |
4575 | placement = NULL_TREE; | |
4576 | ||
4577 | /* If the next token is a `(', then we have a parenthesized | |
4578 | type-id. */ | |
4579 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4580 | { | |
4581 | /* Consume the `('. */ | |
4582 | cp_lexer_consume_token (parser->lexer); | |
4583 | /* Parse the type-id. */ | |
4584 | type = cp_parser_type_id (parser); | |
4585 | /* Look for the closing `)'. */ | |
4586 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
063e900f GB |
4587 | /* There should not be a direct-new-declarator in this production, |
4588 | but GCC used to allowed this, so we check and emit a sensible error | |
4589 | message for this case. */ | |
4590 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
0da99d4e GB |
4591 | { |
4592 | error ("array bound forbidden after parenthesized type-id"); | |
4593 | inform ("try removing the parentheses around the type-id"); | |
063e900f GB |
4594 | cp_parser_direct_new_declarator (parser); |
4595 | } | |
a723baf1 MM |
4596 | } |
4597 | /* Otherwise, there must be a new-type-id. */ | |
4598 | else | |
4599 | type = cp_parser_new_type_id (parser); | |
4600 | ||
4601 | /* If the next token is a `(', then we have a new-initializer. */ | |
4602 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4603 | initializer = cp_parser_new_initializer (parser); | |
4604 | else | |
4605 | initializer = NULL_TREE; | |
4606 | ||
625cbf93 MM |
4607 | /* A new-expression may not appear in an integral constant |
4608 | expression. */ | |
4609 | if (cp_parser_non_integral_constant_expression (parser, "`new'")) | |
4610 | return error_mark_node; | |
4611 | ||
a723baf1 MM |
4612 | /* Create a representation of the new-expression. */ |
4613 | return build_new (placement, type, initializer, global_scope_p); | |
4614 | } | |
4615 | ||
4616 | /* Parse a new-placement. | |
4617 | ||
4618 | new-placement: | |
4619 | ( expression-list ) | |
4620 | ||
4621 | Returns the same representation as for an expression-list. */ | |
4622 | ||
4623 | static tree | |
94edc4ab | 4624 | cp_parser_new_placement (cp_parser* parser) |
a723baf1 MM |
4625 | { |
4626 | tree expression_list; | |
4627 | ||
a723baf1 | 4628 | /* Parse the expression-list. */ |
21526606 | 4629 | expression_list = (cp_parser_parenthesized_expression_list |
39703eb9 | 4630 | (parser, false, /*non_constant_p=*/NULL)); |
a723baf1 MM |
4631 | |
4632 | return expression_list; | |
4633 | } | |
4634 | ||
4635 | /* Parse a new-type-id. | |
4636 | ||
4637 | new-type-id: | |
4638 | type-specifier-seq new-declarator [opt] | |
4639 | ||
4640 | Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq, | |
4641 | and whose TREE_VALUE is the new-declarator. */ | |
4642 | ||
4643 | static tree | |
94edc4ab | 4644 | cp_parser_new_type_id (cp_parser* parser) |
a723baf1 MM |
4645 | { |
4646 | tree type_specifier_seq; | |
4647 | tree declarator; | |
4648 | const char *saved_message; | |
4649 | ||
4650 | /* The type-specifier sequence must not contain type definitions. | |
4651 | (It cannot contain declarations of new types either, but if they | |
4652 | are not definitions we will catch that because they are not | |
4653 | complete.) */ | |
4654 | saved_message = parser->type_definition_forbidden_message; | |
4655 | parser->type_definition_forbidden_message | |
4656 | = "types may not be defined in a new-type-id"; | |
4657 | /* Parse the type-specifier-seq. */ | |
4658 | type_specifier_seq = cp_parser_type_specifier_seq (parser); | |
4659 | /* Restore the old message. */ | |
4660 | parser->type_definition_forbidden_message = saved_message; | |
4661 | /* Parse the new-declarator. */ | |
4662 | declarator = cp_parser_new_declarator_opt (parser); | |
4663 | ||
4664 | return build_tree_list (type_specifier_seq, declarator); | |
4665 | } | |
4666 | ||
4667 | /* Parse an (optional) new-declarator. | |
4668 | ||
4669 | new-declarator: | |
4670 | ptr-operator new-declarator [opt] | |
4671 | direct-new-declarator | |
4672 | ||
4673 | Returns a representation of the declarator. See | |
4674 | cp_parser_declarator for the representations used. */ | |
4675 | ||
4676 | static tree | |
94edc4ab | 4677 | cp_parser_new_declarator_opt (cp_parser* parser) |
a723baf1 MM |
4678 | { |
4679 | enum tree_code code; | |
4680 | tree type; | |
4681 | tree cv_qualifier_seq; | |
4682 | ||
4683 | /* We don't know if there's a ptr-operator next, or not. */ | |
4684 | cp_parser_parse_tentatively (parser); | |
4685 | /* Look for a ptr-operator. */ | |
4686 | code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq); | |
4687 | /* If that worked, look for more new-declarators. */ | |
4688 | if (cp_parser_parse_definitely (parser)) | |
4689 | { | |
4690 | tree declarator; | |
4691 | ||
4692 | /* Parse another optional declarator. */ | |
4693 | declarator = cp_parser_new_declarator_opt (parser); | |
4694 | ||
4695 | /* Create the representation of the declarator. */ | |
4696 | if (code == INDIRECT_REF) | |
4697 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
4698 | declarator); | |
4699 | else | |
4700 | declarator = make_reference_declarator (cv_qualifier_seq, | |
4701 | declarator); | |
4702 | ||
4703 | /* Handle the pointer-to-member case. */ | |
4704 | if (type) | |
4705 | declarator = build_nt (SCOPE_REF, type, declarator); | |
4706 | ||
4707 | return declarator; | |
4708 | } | |
4709 | ||
4710 | /* If the next token is a `[', there is a direct-new-declarator. */ | |
4711 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4712 | return cp_parser_direct_new_declarator (parser); | |
4713 | ||
4714 | return NULL_TREE; | |
4715 | } | |
4716 | ||
4717 | /* Parse a direct-new-declarator. | |
4718 | ||
4719 | direct-new-declarator: | |
4720 | [ expression ] | |
21526606 | 4721 | direct-new-declarator [constant-expression] |
a723baf1 MM |
4722 | |
4723 | Returns an ARRAY_REF, following the same conventions as are | |
4724 | documented for cp_parser_direct_declarator. */ | |
4725 | ||
4726 | static tree | |
94edc4ab | 4727 | cp_parser_direct_new_declarator (cp_parser* parser) |
a723baf1 MM |
4728 | { |
4729 | tree declarator = NULL_TREE; | |
4730 | ||
4731 | while (true) | |
4732 | { | |
4733 | tree expression; | |
4734 | ||
4735 | /* Look for the opening `['. */ | |
4736 | cp_parser_require (parser, CPP_OPEN_SQUARE, "`['"); | |
4737 | /* The first expression is not required to be constant. */ | |
4738 | if (!declarator) | |
4739 | { | |
4740 | expression = cp_parser_expression (parser); | |
4741 | /* The standard requires that the expression have integral | |
4742 | type. DR 74 adds enumeration types. We believe that the | |
4743 | real intent is that these expressions be handled like the | |
4744 | expression in a `switch' condition, which also allows | |
4745 | classes with a single conversion to integral or | |
4746 | enumeration type. */ | |
4747 | if (!processing_template_decl) | |
4748 | { | |
21526606 | 4749 | expression |
a723baf1 MM |
4750 | = build_expr_type_conversion (WANT_INT | WANT_ENUM, |
4751 | expression, | |
b746c5dc | 4752 | /*complain=*/true); |
a723baf1 MM |
4753 | if (!expression) |
4754 | { | |
4755 | error ("expression in new-declarator must have integral or enumeration type"); | |
4756 | expression = error_mark_node; | |
4757 | } | |
4758 | } | |
4759 | } | |
4760 | /* But all the other expressions must be. */ | |
4761 | else | |
21526606 EC |
4762 | expression |
4763 | = cp_parser_constant_expression (parser, | |
14d22dd6 MM |
4764 | /*allow_non_constant=*/false, |
4765 | NULL); | |
a723baf1 MM |
4766 | /* Look for the closing `]'. */ |
4767 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4768 | ||
4769 | /* Add this bound to the declarator. */ | |
4770 | declarator = build_nt (ARRAY_REF, declarator, expression); | |
4771 | ||
4772 | /* If the next token is not a `[', then there are no more | |
4773 | bounds. */ | |
4774 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) | |
4775 | break; | |
4776 | } | |
4777 | ||
4778 | return declarator; | |
4779 | } | |
4780 | ||
4781 | /* Parse a new-initializer. | |
4782 | ||
4783 | new-initializer: | |
4784 | ( expression-list [opt] ) | |
4785 | ||
34cd5ae7 | 4786 | Returns a representation of the expression-list. If there is no |
a723baf1 MM |
4787 | expression-list, VOID_ZERO_NODE is returned. */ |
4788 | ||
4789 | static tree | |
94edc4ab | 4790 | cp_parser_new_initializer (cp_parser* parser) |
a723baf1 MM |
4791 | { |
4792 | tree expression_list; | |
4793 | ||
21526606 | 4794 | expression_list = (cp_parser_parenthesized_expression_list |
39703eb9 | 4795 | (parser, false, /*non_constant_p=*/NULL)); |
7efa3e22 | 4796 | if (!expression_list) |
a723baf1 | 4797 | expression_list = void_zero_node; |
a723baf1 MM |
4798 | |
4799 | return expression_list; | |
4800 | } | |
4801 | ||
4802 | /* Parse a delete-expression. | |
4803 | ||
4804 | delete-expression: | |
4805 | :: [opt] delete cast-expression | |
4806 | :: [opt] delete [ ] cast-expression | |
4807 | ||
4808 | Returns a representation of the expression. */ | |
4809 | ||
4810 | static tree | |
94edc4ab | 4811 | cp_parser_delete_expression (cp_parser* parser) |
a723baf1 MM |
4812 | { |
4813 | bool global_scope_p; | |
4814 | bool array_p; | |
4815 | tree expression; | |
4816 | ||
4817 | /* Look for the optional `::' operator. */ | |
4818 | global_scope_p | |
4819 | = (cp_parser_global_scope_opt (parser, | |
4820 | /*current_scope_valid_p=*/false) | |
4821 | != NULL_TREE); | |
4822 | /* Look for the `delete' keyword. */ | |
4823 | cp_parser_require_keyword (parser, RID_DELETE, "`delete'"); | |
4824 | /* See if the array syntax is in use. */ | |
4825 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) | |
4826 | { | |
4827 | /* Consume the `[' token. */ | |
4828 | cp_lexer_consume_token (parser->lexer); | |
4829 | /* Look for the `]' token. */ | |
4830 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
4831 | /* Remember that this is the `[]' construct. */ | |
4832 | array_p = true; | |
4833 | } | |
4834 | else | |
4835 | array_p = false; | |
4836 | ||
4837 | /* Parse the cast-expression. */ | |
d6b4ea85 | 4838 | expression = cp_parser_simple_cast_expression (parser); |
a723baf1 | 4839 | |
625cbf93 MM |
4840 | /* A delete-expression may not appear in an integral constant |
4841 | expression. */ | |
4842 | if (cp_parser_non_integral_constant_expression (parser, "`delete'")) | |
4843 | return error_mark_node; | |
4844 | ||
a723baf1 MM |
4845 | return delete_sanity (expression, NULL_TREE, array_p, global_scope_p); |
4846 | } | |
4847 | ||
4848 | /* Parse a cast-expression. | |
4849 | ||
4850 | cast-expression: | |
4851 | unary-expression | |
4852 | ( type-id ) cast-expression | |
4853 | ||
4854 | Returns a representation of the expression. */ | |
4855 | ||
4856 | static tree | |
4857 | cp_parser_cast_expression (cp_parser *parser, bool address_p) | |
4858 | { | |
4859 | /* If it's a `(', then we might be looking at a cast. */ | |
4860 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
4861 | { | |
4862 | tree type = NULL_TREE; | |
4863 | tree expr = NULL_TREE; | |
4864 | bool compound_literal_p; | |
4865 | const char *saved_message; | |
4866 | ||
4867 | /* There's no way to know yet whether or not this is a cast. | |
4868 | For example, `(int (3))' is a unary-expression, while `(int) | |
4869 | 3' is a cast. So, we resort to parsing tentatively. */ | |
4870 | cp_parser_parse_tentatively (parser); | |
4871 | /* Types may not be defined in a cast. */ | |
4872 | saved_message = parser->type_definition_forbidden_message; | |
4873 | parser->type_definition_forbidden_message | |
4874 | = "types may not be defined in casts"; | |
4875 | /* Consume the `('. */ | |
4876 | cp_lexer_consume_token (parser->lexer); | |
4877 | /* A very tricky bit is that `(struct S) { 3 }' is a | |
4878 | compound-literal (which we permit in C++ as an extension). | |
4879 | But, that construct is not a cast-expression -- it is a | |
4880 | postfix-expression. (The reason is that `(struct S) { 3 }.i' | |
4881 | is legal; if the compound-literal were a cast-expression, | |
4882 | you'd need an extra set of parentheses.) But, if we parse | |
4883 | the type-id, and it happens to be a class-specifier, then we | |
4884 | will commit to the parse at that point, because we cannot | |
4885 | undo the action that is done when creating a new class. So, | |
21526606 | 4886 | then we cannot back up and do a postfix-expression. |
a723baf1 MM |
4887 | |
4888 | Therefore, we scan ahead to the closing `)', and check to see | |
4889 | if the token after the `)' is a `{'. If so, we are not | |
21526606 | 4890 | looking at a cast-expression. |
a723baf1 MM |
4891 | |
4892 | Save tokens so that we can put them back. */ | |
4893 | cp_lexer_save_tokens (parser->lexer); | |
4894 | /* Skip tokens until the next token is a closing parenthesis. | |
4895 | If we find the closing `)', and the next token is a `{', then | |
4896 | we are looking at a compound-literal. */ | |
21526606 | 4897 | compound_literal_p |
a668c6ad MM |
4898 | = (cp_parser_skip_to_closing_parenthesis (parser, false, false, |
4899 | /*consume_paren=*/true) | |
a723baf1 MM |
4900 | && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); |
4901 | /* Roll back the tokens we skipped. */ | |
4902 | cp_lexer_rollback_tokens (parser->lexer); | |
4903 | /* If we were looking at a compound-literal, simulate an error | |
4904 | so that the call to cp_parser_parse_definitely below will | |
4905 | fail. */ | |
4906 | if (compound_literal_p) | |
4907 | cp_parser_simulate_error (parser); | |
4908 | else | |
4909 | { | |
4f8163b1 MM |
4910 | bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
4911 | parser->in_type_id_in_expr_p = true; | |
a723baf1 MM |
4912 | /* Look for the type-id. */ |
4913 | type = cp_parser_type_id (parser); | |
4914 | /* Look for the closing `)'. */ | |
4915 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
4f8163b1 | 4916 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
4917 | } |
4918 | ||
4919 | /* Restore the saved message. */ | |
4920 | parser->type_definition_forbidden_message = saved_message; | |
4921 | ||
bbaab916 NS |
4922 | /* If ok so far, parse the dependent expression. We cannot be |
4923 | sure it is a cast. Consider `(T ())'. It is a parenthesized | |
4924 | ctor of T, but looks like a cast to function returning T | |
4925 | without a dependent expression. */ | |
4926 | if (!cp_parser_error_occurred (parser)) | |
d6b4ea85 | 4927 | expr = cp_parser_simple_cast_expression (parser); |
bbaab916 | 4928 | |
a723baf1 MM |
4929 | if (cp_parser_parse_definitely (parser)) |
4930 | { | |
a723baf1 | 4931 | /* Warn about old-style casts, if so requested. */ |
21526606 EC |
4932 | if (warn_old_style_cast |
4933 | && !in_system_header | |
4934 | && !VOID_TYPE_P (type) | |
a723baf1 MM |
4935 | && current_lang_name != lang_name_c) |
4936 | warning ("use of old-style cast"); | |
14d22dd6 MM |
4937 | |
4938 | /* Only type conversions to integral or enumeration types | |
4939 | can be used in constant-expressions. */ | |
67c03833 | 4940 | if (parser->integral_constant_expression_p |
14d22dd6 | 4941 | && !dependent_type_p (type) |
625cbf93 MM |
4942 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
4943 | && (cp_parser_non_integral_constant_expression | |
4944 | (parser, | |
4945 | "a cast to a type other than an integral or " | |
4946 | "enumeration type"))) | |
4947 | return error_mark_node; | |
4948 | ||
a723baf1 MM |
4949 | /* Perform the cast. */ |
4950 | expr = build_c_cast (type, expr); | |
bbaab916 | 4951 | return expr; |
a723baf1 | 4952 | } |
a723baf1 MM |
4953 | } |
4954 | ||
4955 | /* If we get here, then it's not a cast, so it must be a | |
4956 | unary-expression. */ | |
4957 | return cp_parser_unary_expression (parser, address_p); | |
4958 | } | |
4959 | ||
4960 | /* Parse a pm-expression. | |
4961 | ||
4962 | pm-expression: | |
4963 | cast-expression | |
4964 | pm-expression .* cast-expression | |
4965 | pm-expression ->* cast-expression | |
4966 | ||
4967 | Returns a representation of the expression. */ | |
4968 | ||
4969 | static tree | |
94edc4ab | 4970 | cp_parser_pm_expression (cp_parser* parser) |
a723baf1 | 4971 | { |
d6b4ea85 MM |
4972 | static const cp_parser_token_tree_map map = { |
4973 | { CPP_DEREF_STAR, MEMBER_REF }, | |
4974 | { CPP_DOT_STAR, DOTSTAR_EXPR }, | |
4975 | { CPP_EOF, ERROR_MARK } | |
4976 | }; | |
a723baf1 | 4977 | |
21526606 | 4978 | return cp_parser_binary_expression (parser, map, |
d6b4ea85 | 4979 | cp_parser_simple_cast_expression); |
a723baf1 MM |
4980 | } |
4981 | ||
4982 | /* Parse a multiplicative-expression. | |
4983 | ||
77077b39 | 4984 | multiplicative-expression: |
a723baf1 MM |
4985 | pm-expression |
4986 | multiplicative-expression * pm-expression | |
4987 | multiplicative-expression / pm-expression | |
4988 | multiplicative-expression % pm-expression | |
4989 | ||
4990 | Returns a representation of the expression. */ | |
4991 | ||
4992 | static tree | |
94edc4ab | 4993 | cp_parser_multiplicative_expression (cp_parser* parser) |
a723baf1 | 4994 | { |
39b1af70 | 4995 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
4996 | { CPP_MULT, MULT_EXPR }, |
4997 | { CPP_DIV, TRUNC_DIV_EXPR }, | |
4998 | { CPP_MOD, TRUNC_MOD_EXPR }, | |
4999 | { CPP_EOF, ERROR_MARK } | |
5000 | }; | |
5001 | ||
5002 | return cp_parser_binary_expression (parser, | |
5003 | map, | |
5004 | cp_parser_pm_expression); | |
5005 | } | |
5006 | ||
5007 | /* Parse an additive-expression. | |
5008 | ||
5009 | additive-expression: | |
5010 | multiplicative-expression | |
5011 | additive-expression + multiplicative-expression | |
5012 | additive-expression - multiplicative-expression | |
5013 | ||
5014 | Returns a representation of the expression. */ | |
5015 | ||
5016 | static tree | |
94edc4ab | 5017 | cp_parser_additive_expression (cp_parser* parser) |
a723baf1 | 5018 | { |
39b1af70 | 5019 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5020 | { CPP_PLUS, PLUS_EXPR }, |
5021 | { CPP_MINUS, MINUS_EXPR }, | |
5022 | { CPP_EOF, ERROR_MARK } | |
5023 | }; | |
5024 | ||
5025 | return cp_parser_binary_expression (parser, | |
5026 | map, | |
5027 | cp_parser_multiplicative_expression); | |
5028 | } | |
5029 | ||
5030 | /* Parse a shift-expression. | |
5031 | ||
5032 | shift-expression: | |
5033 | additive-expression | |
5034 | shift-expression << additive-expression | |
5035 | shift-expression >> additive-expression | |
5036 | ||
5037 | Returns a representation of the expression. */ | |
5038 | ||
5039 | static tree | |
94edc4ab | 5040 | cp_parser_shift_expression (cp_parser* parser) |
a723baf1 | 5041 | { |
39b1af70 | 5042 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5043 | { CPP_LSHIFT, LSHIFT_EXPR }, |
5044 | { CPP_RSHIFT, RSHIFT_EXPR }, | |
5045 | { CPP_EOF, ERROR_MARK } | |
5046 | }; | |
5047 | ||
5048 | return cp_parser_binary_expression (parser, | |
5049 | map, | |
5050 | cp_parser_additive_expression); | |
5051 | } | |
5052 | ||
5053 | /* Parse a relational-expression. | |
5054 | ||
5055 | relational-expression: | |
5056 | shift-expression | |
5057 | relational-expression < shift-expression | |
5058 | relational-expression > shift-expression | |
5059 | relational-expression <= shift-expression | |
5060 | relational-expression >= shift-expression | |
5061 | ||
5062 | GNU Extension: | |
5063 | ||
5064 | relational-expression: | |
5065 | relational-expression <? shift-expression | |
5066 | relational-expression >? shift-expression | |
5067 | ||
5068 | Returns a representation of the expression. */ | |
5069 | ||
5070 | static tree | |
94edc4ab | 5071 | cp_parser_relational_expression (cp_parser* parser) |
a723baf1 | 5072 | { |
39b1af70 | 5073 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5074 | { CPP_LESS, LT_EXPR }, |
5075 | { CPP_GREATER, GT_EXPR }, | |
5076 | { CPP_LESS_EQ, LE_EXPR }, | |
5077 | { CPP_GREATER_EQ, GE_EXPR }, | |
5078 | { CPP_MIN, MIN_EXPR }, | |
5079 | { CPP_MAX, MAX_EXPR }, | |
5080 | { CPP_EOF, ERROR_MARK } | |
5081 | }; | |
5082 | ||
5083 | return cp_parser_binary_expression (parser, | |
5084 | map, | |
5085 | cp_parser_shift_expression); | |
5086 | } | |
5087 | ||
5088 | /* Parse an equality-expression. | |
5089 | ||
5090 | equality-expression: | |
5091 | relational-expression | |
5092 | equality-expression == relational-expression | |
5093 | equality-expression != relational-expression | |
5094 | ||
5095 | Returns a representation of the expression. */ | |
5096 | ||
5097 | static tree | |
94edc4ab | 5098 | cp_parser_equality_expression (cp_parser* parser) |
a723baf1 | 5099 | { |
39b1af70 | 5100 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5101 | { CPP_EQ_EQ, EQ_EXPR }, |
5102 | { CPP_NOT_EQ, NE_EXPR }, | |
5103 | { CPP_EOF, ERROR_MARK } | |
5104 | }; | |
5105 | ||
5106 | return cp_parser_binary_expression (parser, | |
5107 | map, | |
5108 | cp_parser_relational_expression); | |
5109 | } | |
5110 | ||
5111 | /* Parse an and-expression. | |
5112 | ||
5113 | and-expression: | |
5114 | equality-expression | |
5115 | and-expression & equality-expression | |
5116 | ||
5117 | Returns a representation of the expression. */ | |
5118 | ||
5119 | static tree | |
94edc4ab | 5120 | cp_parser_and_expression (cp_parser* parser) |
a723baf1 | 5121 | { |
39b1af70 | 5122 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5123 | { CPP_AND, BIT_AND_EXPR }, |
5124 | { CPP_EOF, ERROR_MARK } | |
5125 | }; | |
5126 | ||
5127 | return cp_parser_binary_expression (parser, | |
5128 | map, | |
5129 | cp_parser_equality_expression); | |
5130 | } | |
5131 | ||
5132 | /* Parse an exclusive-or-expression. | |
5133 | ||
5134 | exclusive-or-expression: | |
5135 | and-expression | |
5136 | exclusive-or-expression ^ and-expression | |
5137 | ||
5138 | Returns a representation of the expression. */ | |
5139 | ||
5140 | static tree | |
94edc4ab | 5141 | cp_parser_exclusive_or_expression (cp_parser* parser) |
a723baf1 | 5142 | { |
39b1af70 | 5143 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5144 | { CPP_XOR, BIT_XOR_EXPR }, |
5145 | { CPP_EOF, ERROR_MARK } | |
5146 | }; | |
5147 | ||
5148 | return cp_parser_binary_expression (parser, | |
5149 | map, | |
5150 | cp_parser_and_expression); | |
5151 | } | |
5152 | ||
5153 | ||
5154 | /* Parse an inclusive-or-expression. | |
5155 | ||
5156 | inclusive-or-expression: | |
5157 | exclusive-or-expression | |
5158 | inclusive-or-expression | exclusive-or-expression | |
5159 | ||
5160 | Returns a representation of the expression. */ | |
5161 | ||
5162 | static tree | |
94edc4ab | 5163 | cp_parser_inclusive_or_expression (cp_parser* parser) |
a723baf1 | 5164 | { |
39b1af70 | 5165 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5166 | { CPP_OR, BIT_IOR_EXPR }, |
5167 | { CPP_EOF, ERROR_MARK } | |
5168 | }; | |
5169 | ||
5170 | return cp_parser_binary_expression (parser, | |
5171 | map, | |
5172 | cp_parser_exclusive_or_expression); | |
5173 | } | |
5174 | ||
5175 | /* Parse a logical-and-expression. | |
5176 | ||
5177 | logical-and-expression: | |
5178 | inclusive-or-expression | |
5179 | logical-and-expression && inclusive-or-expression | |
5180 | ||
5181 | Returns a representation of the expression. */ | |
5182 | ||
5183 | static tree | |
94edc4ab | 5184 | cp_parser_logical_and_expression (cp_parser* parser) |
a723baf1 | 5185 | { |
39b1af70 | 5186 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5187 | { CPP_AND_AND, TRUTH_ANDIF_EXPR }, |
5188 | { CPP_EOF, ERROR_MARK } | |
5189 | }; | |
5190 | ||
5191 | return cp_parser_binary_expression (parser, | |
5192 | map, | |
5193 | cp_parser_inclusive_or_expression); | |
5194 | } | |
5195 | ||
5196 | /* Parse a logical-or-expression. | |
5197 | ||
5198 | logical-or-expression: | |
34cd5ae7 | 5199 | logical-and-expression |
a723baf1 MM |
5200 | logical-or-expression || logical-and-expression |
5201 | ||
5202 | Returns a representation of the expression. */ | |
5203 | ||
5204 | static tree | |
94edc4ab | 5205 | cp_parser_logical_or_expression (cp_parser* parser) |
a723baf1 | 5206 | { |
39b1af70 | 5207 | static const cp_parser_token_tree_map map = { |
a723baf1 MM |
5208 | { CPP_OR_OR, TRUTH_ORIF_EXPR }, |
5209 | { CPP_EOF, ERROR_MARK } | |
5210 | }; | |
5211 | ||
5212 | return cp_parser_binary_expression (parser, | |
5213 | map, | |
5214 | cp_parser_logical_and_expression); | |
5215 | } | |
5216 | ||
a723baf1 MM |
5217 | /* Parse the `? expression : assignment-expression' part of a |
5218 | conditional-expression. The LOGICAL_OR_EXPR is the | |
5219 | logical-or-expression that started the conditional-expression. | |
5220 | Returns a representation of the entire conditional-expression. | |
5221 | ||
39703eb9 | 5222 | This routine is used by cp_parser_assignment_expression. |
a723baf1 MM |
5223 | |
5224 | ? expression : assignment-expression | |
21526606 | 5225 | |
a723baf1 | 5226 | GNU Extensions: |
21526606 | 5227 | |
a723baf1 MM |
5228 | ? : assignment-expression */ |
5229 | ||
5230 | static tree | |
94edc4ab | 5231 | cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) |
a723baf1 MM |
5232 | { |
5233 | tree expr; | |
5234 | tree assignment_expr; | |
5235 | ||
5236 | /* Consume the `?' token. */ | |
5237 | cp_lexer_consume_token (parser->lexer); | |
5238 | if (cp_parser_allow_gnu_extensions_p (parser) | |
5239 | && cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
5240 | /* Implicit true clause. */ | |
5241 | expr = NULL_TREE; | |
5242 | else | |
5243 | /* Parse the expression. */ | |
5244 | expr = cp_parser_expression (parser); | |
21526606 | 5245 | |
a723baf1 MM |
5246 | /* The next token should be a `:'. */ |
5247 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5248 | /* Parse the assignment-expression. */ | |
5249 | assignment_expr = cp_parser_assignment_expression (parser); | |
5250 | ||
5251 | /* Build the conditional-expression. */ | |
5252 | return build_x_conditional_expr (logical_or_expr, | |
5253 | expr, | |
5254 | assignment_expr); | |
5255 | } | |
5256 | ||
5257 | /* Parse an assignment-expression. | |
5258 | ||
5259 | assignment-expression: | |
5260 | conditional-expression | |
5261 | logical-or-expression assignment-operator assignment_expression | |
5262 | throw-expression | |
5263 | ||
5264 | Returns a representation for the expression. */ | |
5265 | ||
5266 | static tree | |
94edc4ab | 5267 | cp_parser_assignment_expression (cp_parser* parser) |
a723baf1 MM |
5268 | { |
5269 | tree expr; | |
5270 | ||
5271 | /* If the next token is the `throw' keyword, then we're looking at | |
5272 | a throw-expression. */ | |
5273 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) | |
5274 | expr = cp_parser_throw_expression (parser); | |
5275 | /* Otherwise, it must be that we are looking at a | |
5276 | logical-or-expression. */ | |
5277 | else | |
5278 | { | |
5279 | /* Parse the logical-or-expression. */ | |
5280 | expr = cp_parser_logical_or_expression (parser); | |
5281 | /* If the next token is a `?' then we're actually looking at a | |
5282 | conditional-expression. */ | |
5283 | if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) | |
5284 | return cp_parser_question_colon_clause (parser, expr); | |
21526606 | 5285 | else |
a723baf1 MM |
5286 | { |
5287 | enum tree_code assignment_operator; | |
5288 | ||
5289 | /* If it's an assignment-operator, we're using the second | |
5290 | production. */ | |
21526606 | 5291 | assignment_operator |
a723baf1 MM |
5292 | = cp_parser_assignment_operator_opt (parser); |
5293 | if (assignment_operator != ERROR_MARK) | |
5294 | { | |
5295 | tree rhs; | |
5296 | ||
5297 | /* Parse the right-hand side of the assignment. */ | |
5298 | rhs = cp_parser_assignment_expression (parser); | |
14d22dd6 MM |
5299 | /* An assignment may not appear in a |
5300 | constant-expression. */ | |
625cbf93 MM |
5301 | if (cp_parser_non_integral_constant_expression (parser, |
5302 | "an assignment")) | |
5303 | return error_mark_node; | |
34cd5ae7 | 5304 | /* Build the assignment expression. */ |
21526606 EC |
5305 | expr = build_x_modify_expr (expr, |
5306 | assignment_operator, | |
a723baf1 MM |
5307 | rhs); |
5308 | } | |
5309 | } | |
5310 | } | |
5311 | ||
5312 | return expr; | |
5313 | } | |
5314 | ||
5315 | /* Parse an (optional) assignment-operator. | |
5316 | ||
21526606 EC |
5317 | assignment-operator: one of |
5318 | = *= /= %= += -= >>= <<= &= ^= |= | |
a723baf1 MM |
5319 | |
5320 | GNU Extension: | |
21526606 | 5321 | |
a723baf1 MM |
5322 | assignment-operator: one of |
5323 | <?= >?= | |
5324 | ||
5325 | If the next token is an assignment operator, the corresponding tree | |
5326 | code is returned, and the token is consumed. For example, for | |
5327 | `+=', PLUS_EXPR is returned. For `=' itself, the code returned is | |
5328 | NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', | |
5329 | TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment | |
5330 | operator, ERROR_MARK is returned. */ | |
5331 | ||
5332 | static enum tree_code | |
94edc4ab | 5333 | cp_parser_assignment_operator_opt (cp_parser* parser) |
a723baf1 MM |
5334 | { |
5335 | enum tree_code op; | |
5336 | cp_token *token; | |
5337 | ||
5338 | /* Peek at the next toen. */ | |
5339 | token = cp_lexer_peek_token (parser->lexer); | |
5340 | ||
5341 | switch (token->type) | |
5342 | { | |
5343 | case CPP_EQ: | |
5344 | op = NOP_EXPR; | |
5345 | break; | |
5346 | ||
5347 | case CPP_MULT_EQ: | |
5348 | op = MULT_EXPR; | |
5349 | break; | |
5350 | ||
5351 | case CPP_DIV_EQ: | |
5352 | op = TRUNC_DIV_EXPR; | |
5353 | break; | |
5354 | ||
5355 | case CPP_MOD_EQ: | |
5356 | op = TRUNC_MOD_EXPR; | |
5357 | break; | |
5358 | ||
5359 | case CPP_PLUS_EQ: | |
5360 | op = PLUS_EXPR; | |
5361 | break; | |
5362 | ||
5363 | case CPP_MINUS_EQ: | |
5364 | op = MINUS_EXPR; | |
5365 | break; | |
5366 | ||
5367 | case CPP_RSHIFT_EQ: | |
5368 | op = RSHIFT_EXPR; | |
5369 | break; | |
5370 | ||
5371 | case CPP_LSHIFT_EQ: | |
5372 | op = LSHIFT_EXPR; | |
5373 | break; | |
5374 | ||
5375 | case CPP_AND_EQ: | |
5376 | op = BIT_AND_EXPR; | |
5377 | break; | |
5378 | ||
5379 | case CPP_XOR_EQ: | |
5380 | op = BIT_XOR_EXPR; | |
5381 | break; | |
5382 | ||
5383 | case CPP_OR_EQ: | |
5384 | op = BIT_IOR_EXPR; | |
5385 | break; | |
5386 | ||
5387 | case CPP_MIN_EQ: | |
5388 | op = MIN_EXPR; | |
5389 | break; | |
5390 | ||
5391 | case CPP_MAX_EQ: | |
5392 | op = MAX_EXPR; | |
5393 | break; | |
5394 | ||
21526606 | 5395 | default: |
a723baf1 MM |
5396 | /* Nothing else is an assignment operator. */ |
5397 | op = ERROR_MARK; | |
5398 | } | |
5399 | ||
5400 | /* If it was an assignment operator, consume it. */ | |
5401 | if (op != ERROR_MARK) | |
5402 | cp_lexer_consume_token (parser->lexer); | |
5403 | ||
5404 | return op; | |
5405 | } | |
5406 | ||
5407 | /* Parse an expression. | |
5408 | ||
5409 | expression: | |
5410 | assignment-expression | |
5411 | expression , assignment-expression | |
5412 | ||
5413 | Returns a representation of the expression. */ | |
5414 | ||
5415 | static tree | |
94edc4ab | 5416 | cp_parser_expression (cp_parser* parser) |
a723baf1 MM |
5417 | { |
5418 | tree expression = NULL_TREE; | |
a723baf1 MM |
5419 | |
5420 | while (true) | |
5421 | { | |
5422 | tree assignment_expression; | |
5423 | ||
5424 | /* Parse the next assignment-expression. */ | |
21526606 | 5425 | assignment_expression |
a723baf1 MM |
5426 | = cp_parser_assignment_expression (parser); |
5427 | /* If this is the first assignment-expression, we can just | |
5428 | save it away. */ | |
5429 | if (!expression) | |
5430 | expression = assignment_expression; | |
a723baf1 | 5431 | else |
d17811fd MM |
5432 | expression = build_x_compound_expr (expression, |
5433 | assignment_expression); | |
a723baf1 MM |
5434 | /* If the next token is not a comma, then we are done with the |
5435 | expression. */ | |
5436 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
5437 | break; | |
5438 | /* Consume the `,'. */ | |
5439 | cp_lexer_consume_token (parser->lexer); | |
14d22dd6 | 5440 | /* A comma operator cannot appear in a constant-expression. */ |
625cbf93 MM |
5441 | if (cp_parser_non_integral_constant_expression (parser, |
5442 | "a comma operator")) | |
5443 | expression = error_mark_node; | |
14d22dd6 | 5444 | } |
a723baf1 MM |
5445 | |
5446 | return expression; | |
5447 | } | |
5448 | ||
21526606 | 5449 | /* Parse a constant-expression. |
a723baf1 MM |
5450 | |
5451 | constant-expression: | |
21526606 | 5452 | conditional-expression |
14d22dd6 MM |
5453 | |
5454 | If ALLOW_NON_CONSTANT_P a non-constant expression is silently | |
d17811fd MM |
5455 | accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not |
5456 | constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P | |
5457 | is false, NON_CONSTANT_P should be NULL. */ | |
a723baf1 MM |
5458 | |
5459 | static tree | |
21526606 | 5460 | cp_parser_constant_expression (cp_parser* parser, |
14d22dd6 MM |
5461 | bool allow_non_constant_p, |
5462 | bool *non_constant_p) | |
a723baf1 | 5463 | { |
67c03833 JM |
5464 | bool saved_integral_constant_expression_p; |
5465 | bool saved_allow_non_integral_constant_expression_p; | |
5466 | bool saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5467 | tree expression; |
5468 | ||
5469 | /* It might seem that we could simply parse the | |
5470 | conditional-expression, and then check to see if it were | |
5471 | TREE_CONSTANT. However, an expression that is TREE_CONSTANT is | |
5472 | one that the compiler can figure out is constant, possibly after | |
5473 | doing some simplifications or optimizations. The standard has a | |
5474 | precise definition of constant-expression, and we must honor | |
5475 | that, even though it is somewhat more restrictive. | |
5476 | ||
5477 | For example: | |
5478 | ||
5479 | int i[(2, 3)]; | |
5480 | ||
5481 | is not a legal declaration, because `(2, 3)' is not a | |
5482 | constant-expression. The `,' operator is forbidden in a | |
5483 | constant-expression. However, GCC's constant-folding machinery | |
5484 | will fold this operation to an INTEGER_CST for `3'. */ | |
5485 | ||
14d22dd6 | 5486 | /* Save the old settings. */ |
67c03833 | 5487 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
21526606 | 5488 | saved_allow_non_integral_constant_expression_p |
67c03833 JM |
5489 | = parser->allow_non_integral_constant_expression_p; |
5490 | saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; | |
a723baf1 | 5491 | /* We are now parsing a constant-expression. */ |
67c03833 JM |
5492 | parser->integral_constant_expression_p = true; |
5493 | parser->allow_non_integral_constant_expression_p = allow_non_constant_p; | |
5494 | parser->non_integral_constant_expression_p = false; | |
39703eb9 MM |
5495 | /* Although the grammar says "conditional-expression", we parse an |
5496 | "assignment-expression", which also permits "throw-expression" | |
5497 | and the use of assignment operators. In the case that | |
5498 | ALLOW_NON_CONSTANT_P is false, we get better errors than we would | |
5499 | otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is | |
5500 | actually essential that we look for an assignment-expression. | |
5501 | For example, cp_parser_initializer_clauses uses this function to | |
5502 | determine whether a particular assignment-expression is in fact | |
5503 | constant. */ | |
5504 | expression = cp_parser_assignment_expression (parser); | |
14d22dd6 | 5505 | /* Restore the old settings. */ |
67c03833 | 5506 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
21526606 | 5507 | parser->allow_non_integral_constant_expression_p |
67c03833 | 5508 | = saved_allow_non_integral_constant_expression_p; |
14d22dd6 | 5509 | if (allow_non_constant_p) |
67c03833 JM |
5510 | *non_constant_p = parser->non_integral_constant_expression_p; |
5511 | parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p; | |
a723baf1 MM |
5512 | |
5513 | return expression; | |
5514 | } | |
5515 | ||
7a3ea201 RH |
5516 | /* Parse __builtin_offsetof. |
5517 | ||
5518 | offsetof-expression: | |
5519 | "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" | |
5520 | ||
5521 | offsetof-member-designator: | |
5522 | id-expression | |
5523 | | offsetof-member-designator "." id-expression | |
5524 | | offsetof-member-designator "[" expression "]" | |
5525 | */ | |
5526 | ||
5527 | static tree | |
5528 | cp_parser_builtin_offsetof (cp_parser *parser) | |
5529 | { | |
5530 | int save_ice_p, save_non_ice_p; | |
5531 | tree type, expr; | |
5532 | cp_id_kind dummy; | |
5533 | ||
5534 | /* We're about to accept non-integral-constant things, but will | |
5535 | definitely yield an integral constant expression. Save and | |
5536 | restore these values around our local parsing. */ | |
5537 | save_ice_p = parser->integral_constant_expression_p; | |
5538 | save_non_ice_p = parser->non_integral_constant_expression_p; | |
5539 | ||
5540 | /* Consume the "__builtin_offsetof" token. */ | |
5541 | cp_lexer_consume_token (parser->lexer); | |
5542 | /* Consume the opening `('. */ | |
5543 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
5544 | /* Parse the type-id. */ | |
5545 | type = cp_parser_type_id (parser); | |
5546 | /* Look for the `,'. */ | |
5547 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
5548 | ||
5549 | /* Build the (type *)null that begins the traditional offsetof macro. */ | |
5550 | expr = build_static_cast (build_pointer_type (type), null_pointer_node); | |
5551 | ||
5552 | /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ | |
5553 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, | |
5554 | true, &dummy); | |
5555 | while (true) | |
5556 | { | |
5557 | cp_token *token = cp_lexer_peek_token (parser->lexer); | |
5558 | switch (token->type) | |
5559 | { | |
5560 | case CPP_OPEN_SQUARE: | |
5561 | /* offsetof-member-designator "[" expression "]" */ | |
5562 | expr = cp_parser_postfix_open_square_expression (parser, expr, true); | |
5563 | break; | |
5564 | ||
5565 | case CPP_DOT: | |
5566 | /* offsetof-member-designator "." identifier */ | |
5567 | cp_lexer_consume_token (parser->lexer); | |
5568 | expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr, | |
5569 | true, &dummy); | |
5570 | break; | |
5571 | ||
5572 | case CPP_CLOSE_PAREN: | |
5573 | /* Consume the ")" token. */ | |
5574 | cp_lexer_consume_token (parser->lexer); | |
5575 | goto success; | |
5576 | ||
5577 | default: | |
5578 | /* Error. We know the following require will fail, but | |
5579 | that gives the proper error message. */ | |
5580 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
5581 | cp_parser_skip_to_closing_parenthesis (parser, true, false, true); | |
5582 | expr = error_mark_node; | |
5583 | goto failure; | |
5584 | } | |
5585 | } | |
5586 | ||
5587 | success: | |
5588 | /* We've finished the parsing, now finish with the semantics. At present | |
5589 | we're just mirroring the traditional macro implementation. Better | |
5590 | would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic | |
5591 | here rather than in build_x_unary_op. */ | |
5592 | expr = build_reinterpret_cast (build_reference_type (char_type_node), expr); | |
5593 | expr = build_x_unary_op (ADDR_EXPR, expr); | |
5594 | expr = build_reinterpret_cast (size_type_node, expr); | |
5595 | ||
5596 | failure: | |
5597 | parser->integral_constant_expression_p = save_ice_p; | |
5598 | parser->non_integral_constant_expression_p = save_non_ice_p; | |
5599 | ||
5600 | return expr; | |
5601 | } | |
5602 | ||
a723baf1 MM |
5603 | /* Statements [gram.stmt.stmt] */ |
5604 | ||
21526606 | 5605 | /* Parse a statement. |
a723baf1 MM |
5606 | |
5607 | statement: | |
5608 | labeled-statement | |
5609 | expression-statement | |
5610 | compound-statement | |
5611 | selection-statement | |
5612 | iteration-statement | |
5613 | jump-statement | |
5614 | declaration-statement | |
5615 | try-block */ | |
5616 | ||
5617 | static void | |
a5bcc582 | 5618 | cp_parser_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5619 | { |
5620 | tree statement; | |
5621 | cp_token *token; | |
6de9cd9a | 5622 | location_t statement_locus; |
a723baf1 MM |
5623 | |
5624 | /* There is no statement yet. */ | |
5625 | statement = NULL_TREE; | |
5626 | /* Peek at the next token. */ | |
5627 | token = cp_lexer_peek_token (parser->lexer); | |
6de9cd9a DN |
5628 | /* Remember the location of the first token in the statement. */ |
5629 | statement_locus = token->location; | |
a723baf1 MM |
5630 | /* If this is a keyword, then that will often determine what kind of |
5631 | statement we have. */ | |
5632 | if (token->type == CPP_KEYWORD) | |
5633 | { | |
5634 | enum rid keyword = token->keyword; | |
5635 | ||
5636 | switch (keyword) | |
5637 | { | |
5638 | case RID_CASE: | |
5639 | case RID_DEFAULT: | |
a5bcc582 NS |
5640 | statement = cp_parser_labeled_statement (parser, |
5641 | in_statement_expr_p); | |
a723baf1 MM |
5642 | break; |
5643 | ||
5644 | case RID_IF: | |
5645 | case RID_SWITCH: | |
5646 | statement = cp_parser_selection_statement (parser); | |
5647 | break; | |
5648 | ||
5649 | case RID_WHILE: | |
5650 | case RID_DO: | |
5651 | case RID_FOR: | |
5652 | statement = cp_parser_iteration_statement (parser); | |
5653 | break; | |
5654 | ||
5655 | case RID_BREAK: | |
5656 | case RID_CONTINUE: | |
5657 | case RID_RETURN: | |
5658 | case RID_GOTO: | |
5659 | statement = cp_parser_jump_statement (parser); | |
5660 | break; | |
5661 | ||
5662 | case RID_TRY: | |
5663 | statement = cp_parser_try_block (parser); | |
5664 | break; | |
5665 | ||
5666 | default: | |
5667 | /* It might be a keyword like `int' that can start a | |
5668 | declaration-statement. */ | |
5669 | break; | |
5670 | } | |
5671 | } | |
5672 | else if (token->type == CPP_NAME) | |
5673 | { | |
5674 | /* If the next token is a `:', then we are looking at a | |
5675 | labeled-statement. */ | |
5676 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
5677 | if (token->type == CPP_COLON) | |
a5bcc582 | 5678 | statement = cp_parser_labeled_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5679 | } |
5680 | /* Anything that starts with a `{' must be a compound-statement. */ | |
5681 | else if (token->type == CPP_OPEN_BRACE) | |
a5bcc582 | 5682 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
5683 | |
5684 | /* Everything else must be a declaration-statement or an | |
21526606 | 5685 | expression-statement. Try for the declaration-statement |
a723baf1 MM |
5686 | first, unless we are looking at a `;', in which case we know that |
5687 | we have an expression-statement. */ | |
5688 | if (!statement) | |
5689 | { | |
5690 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
5691 | { | |
5692 | cp_parser_parse_tentatively (parser); | |
5693 | /* Try to parse the declaration-statement. */ | |
5694 | cp_parser_declaration_statement (parser); | |
5695 | /* If that worked, we're done. */ | |
5696 | if (cp_parser_parse_definitely (parser)) | |
5697 | return; | |
5698 | } | |
5699 | /* Look for an expression-statement instead. */ | |
a5bcc582 | 5700 | statement = cp_parser_expression_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5701 | } |
5702 | ||
5703 | /* Set the line number for the statement. */ | |
009ed910 | 5704 | if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) |
6de9cd9a DN |
5705 | { |
5706 | SET_EXPR_LOCUS (statement, NULL); | |
5707 | annotate_with_locus (statement, statement_locus); | |
5708 | } | |
a723baf1 MM |
5709 | } |
5710 | ||
5711 | /* Parse a labeled-statement. | |
5712 | ||
5713 | labeled-statement: | |
5714 | identifier : statement | |
5715 | case constant-expression : statement | |
98ce043b MM |
5716 | default : statement |
5717 | ||
5718 | GNU Extension: | |
21526606 | 5719 | |
98ce043b MM |
5720 | labeled-statement: |
5721 | case constant-expression ... constant-expression : statement | |
a723baf1 MM |
5722 | |
5723 | Returns the new CASE_LABEL, for a `case' or `default' label. For | |
5724 | an ordinary label, returns a LABEL_STMT. */ | |
5725 | ||
5726 | static tree | |
a5bcc582 | 5727 | cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5728 | { |
5729 | cp_token *token; | |
0e59b3fb | 5730 | tree statement = error_mark_node; |
a723baf1 MM |
5731 | |
5732 | /* The next token should be an identifier. */ | |
5733 | token = cp_lexer_peek_token (parser->lexer); | |
5734 | if (token->type != CPP_NAME | |
5735 | && token->type != CPP_KEYWORD) | |
5736 | { | |
5737 | cp_parser_error (parser, "expected labeled-statement"); | |
5738 | return error_mark_node; | |
5739 | } | |
5740 | ||
5741 | switch (token->keyword) | |
5742 | { | |
5743 | case RID_CASE: | |
5744 | { | |
98ce043b MM |
5745 | tree expr, expr_hi; |
5746 | cp_token *ellipsis; | |
a723baf1 MM |
5747 | |
5748 | /* Consume the `case' token. */ | |
5749 | cp_lexer_consume_token (parser->lexer); | |
5750 | /* Parse the constant-expression. */ | |
21526606 | 5751 | expr = cp_parser_constant_expression (parser, |
d17811fd | 5752 | /*allow_non_constant_p=*/false, |
14d22dd6 | 5753 | NULL); |
98ce043b MM |
5754 | |
5755 | ellipsis = cp_lexer_peek_token (parser->lexer); | |
5756 | if (ellipsis->type == CPP_ELLIPSIS) | |
5757 | { | |
5758 | /* Consume the `...' token. */ | |
5759 | cp_lexer_consume_token (parser->lexer); | |
5760 | expr_hi = | |
5761 | cp_parser_constant_expression (parser, | |
5762 | /*allow_non_constant_p=*/false, | |
5763 | NULL); | |
5764 | /* We don't need to emit warnings here, as the common code | |
5765 | will do this for us. */ | |
5766 | } | |
5767 | else | |
5768 | expr_hi = NULL_TREE; | |
5769 | ||
0e59b3fb MM |
5770 | if (!parser->in_switch_statement_p) |
5771 | error ("case label `%E' not within a switch statement", expr); | |
5772 | else | |
98ce043b | 5773 | statement = finish_case_label (expr, expr_hi); |
a723baf1 MM |
5774 | } |
5775 | break; | |
5776 | ||
5777 | case RID_DEFAULT: | |
5778 | /* Consume the `default' token. */ | |
5779 | cp_lexer_consume_token (parser->lexer); | |
0e59b3fb MM |
5780 | if (!parser->in_switch_statement_p) |
5781 | error ("case label not within a switch statement"); | |
5782 | else | |
5783 | statement = finish_case_label (NULL_TREE, NULL_TREE); | |
a723baf1 MM |
5784 | break; |
5785 | ||
5786 | default: | |
5787 | /* Anything else must be an ordinary label. */ | |
5788 | statement = finish_label_stmt (cp_parser_identifier (parser)); | |
5789 | break; | |
5790 | } | |
5791 | ||
5792 | /* Require the `:' token. */ | |
5793 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
5794 | /* Parse the labeled statement. */ | |
a5bcc582 | 5795 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5796 | |
5797 | /* Return the label, in the case of a `case' or `default' label. */ | |
5798 | return statement; | |
5799 | } | |
5800 | ||
5801 | /* Parse an expression-statement. | |
5802 | ||
5803 | expression-statement: | |
5804 | expression [opt] ; | |
5805 | ||
5806 | Returns the new EXPR_STMT -- or NULL_TREE if the expression | |
a5bcc582 NS |
5807 | statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P |
5808 | indicates whether this expression-statement is part of an | |
5809 | expression statement. */ | |
a723baf1 MM |
5810 | |
5811 | static tree | |
a5bcc582 | 5812 | cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 | 5813 | { |
a5bcc582 | 5814 | tree statement = NULL_TREE; |
a723baf1 | 5815 | |
a5bcc582 | 5816 | /* If the next token is a ';', then there is no expression |
04c06002 | 5817 | statement. */ |
a723baf1 | 5818 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) |
a5bcc582 | 5819 | statement = cp_parser_expression (parser); |
21526606 | 5820 | |
a723baf1 | 5821 | /* Consume the final `;'. */ |
e0860732 | 5822 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 | 5823 | |
a5bcc582 NS |
5824 | if (in_statement_expr_p |
5825 | && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
5826 | { | |
5827 | /* This is the final expression statement of a statement | |
5828 | expression. */ | |
5829 | statement = finish_stmt_expr_expr (statement); | |
5830 | } | |
5831 | else if (statement) | |
5832 | statement = finish_expr_stmt (statement); | |
5833 | else | |
5834 | finish_stmt (); | |
21526606 | 5835 | |
a723baf1 MM |
5836 | return statement; |
5837 | } | |
5838 | ||
5839 | /* Parse a compound-statement. | |
5840 | ||
5841 | compound-statement: | |
5842 | { statement-seq [opt] } | |
21526606 | 5843 | |
a723baf1 MM |
5844 | Returns a COMPOUND_STMT representing the statement. */ |
5845 | ||
5846 | static tree | |
a5bcc582 | 5847 | cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p) |
a723baf1 MM |
5848 | { |
5849 | tree compound_stmt; | |
5850 | ||
5851 | /* Consume the `{'. */ | |
5852 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
5853 | return error_mark_node; | |
5854 | /* Begin the compound-statement. */ | |
7a3397c7 | 5855 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 5856 | /* Parse an (optional) statement-seq. */ |
a5bcc582 | 5857 | cp_parser_statement_seq_opt (parser, in_statement_expr_p); |
a723baf1 | 5858 | /* Finish the compound-statement. */ |
7a3397c7 | 5859 | finish_compound_stmt (compound_stmt); |
a723baf1 MM |
5860 | /* Consume the `}'. */ |
5861 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
5862 | ||
5863 | return compound_stmt; | |
5864 | } | |
5865 | ||
5866 | /* Parse an (optional) statement-seq. | |
5867 | ||
5868 | statement-seq: | |
5869 | statement | |
5870 | statement-seq [opt] statement */ | |
5871 | ||
5872 | static void | |
a5bcc582 | 5873 | cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p) |
a723baf1 MM |
5874 | { |
5875 | /* Scan statements until there aren't any more. */ | |
5876 | while (true) | |
5877 | { | |
5878 | /* If we're looking at a `}', then we've run out of statements. */ | |
5879 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE) | |
5880 | || cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
5881 | break; | |
5882 | ||
5883 | /* Parse the statement. */ | |
a5bcc582 | 5884 | cp_parser_statement (parser, in_statement_expr_p); |
a723baf1 MM |
5885 | } |
5886 | } | |
5887 | ||
5888 | /* Parse a selection-statement. | |
5889 | ||
5890 | selection-statement: | |
5891 | if ( condition ) statement | |
5892 | if ( condition ) statement else statement | |
21526606 | 5893 | switch ( condition ) statement |
a723baf1 MM |
5894 | |
5895 | Returns the new IF_STMT or SWITCH_STMT. */ | |
5896 | ||
5897 | static tree | |
94edc4ab | 5898 | cp_parser_selection_statement (cp_parser* parser) |
a723baf1 MM |
5899 | { |
5900 | cp_token *token; | |
5901 | enum rid keyword; | |
5902 | ||
5903 | /* Peek at the next token. */ | |
5904 | token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement"); | |
5905 | ||
5906 | /* See what kind of keyword it is. */ | |
5907 | keyword = token->keyword; | |
5908 | switch (keyword) | |
5909 | { | |
5910 | case RID_IF: | |
5911 | case RID_SWITCH: | |
5912 | { | |
5913 | tree statement; | |
5914 | tree condition; | |
5915 | ||
5916 | /* Look for the `('. */ | |
5917 | if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) | |
5918 | { | |
5919 | cp_parser_skip_to_end_of_statement (parser); | |
5920 | return error_mark_node; | |
5921 | } | |
5922 | ||
5923 | /* Begin the selection-statement. */ | |
5924 | if (keyword == RID_IF) | |
5925 | statement = begin_if_stmt (); | |
5926 | else | |
5927 | statement = begin_switch_stmt (); | |
5928 | ||
5929 | /* Parse the condition. */ | |
5930 | condition = cp_parser_condition (parser); | |
5931 | /* Look for the `)'. */ | |
5932 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
5933 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
5934 | /*consume_paren=*/true); | |
a723baf1 MM |
5935 | |
5936 | if (keyword == RID_IF) | |
5937 | { | |
5938 | tree then_stmt; | |
5939 | ||
5940 | /* Add the condition. */ | |
5941 | finish_if_stmt_cond (condition, statement); | |
5942 | ||
5943 | /* Parse the then-clause. */ | |
5944 | then_stmt = cp_parser_implicitly_scoped_statement (parser); | |
5945 | finish_then_clause (statement); | |
5946 | ||
5947 | /* If the next token is `else', parse the else-clause. */ | |
5948 | if (cp_lexer_next_token_is_keyword (parser->lexer, | |
5949 | RID_ELSE)) | |
5950 | { | |
5951 | tree else_stmt; | |
5952 | ||
5953 | /* Consume the `else' keyword. */ | |
5954 | cp_lexer_consume_token (parser->lexer); | |
5955 | /* Parse the else-clause. */ | |
21526606 | 5956 | else_stmt |
a723baf1 MM |
5957 | = cp_parser_implicitly_scoped_statement (parser); |
5958 | finish_else_clause (statement); | |
5959 | } | |
5960 | ||
5961 | /* Now we're all done with the if-statement. */ | |
5962 | finish_if_stmt (); | |
5963 | } | |
5964 | else | |
5965 | { | |
5966 | tree body; | |
0e59b3fb | 5967 | bool in_switch_statement_p; |
a723baf1 MM |
5968 | |
5969 | /* Add the condition. */ | |
5970 | finish_switch_cond (condition, statement); | |
5971 | ||
5972 | /* Parse the body of the switch-statement. */ | |
0e59b3fb MM |
5973 | in_switch_statement_p = parser->in_switch_statement_p; |
5974 | parser->in_switch_statement_p = true; | |
a723baf1 | 5975 | body = cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 5976 | parser->in_switch_statement_p = in_switch_statement_p; |
a723baf1 MM |
5977 | |
5978 | /* Now we're all done with the switch-statement. */ | |
5979 | finish_switch_stmt (statement); | |
5980 | } | |
5981 | ||
5982 | return statement; | |
5983 | } | |
5984 | break; | |
5985 | ||
5986 | default: | |
5987 | cp_parser_error (parser, "expected selection-statement"); | |
5988 | return error_mark_node; | |
5989 | } | |
5990 | } | |
5991 | ||
21526606 | 5992 | /* Parse a condition. |
a723baf1 MM |
5993 | |
5994 | condition: | |
5995 | expression | |
21526606 | 5996 | type-specifier-seq declarator = assignment-expression |
a723baf1 MM |
5997 | |
5998 | GNU Extension: | |
21526606 | 5999 | |
a723baf1 | 6000 | condition: |
21526606 | 6001 | type-specifier-seq declarator asm-specification [opt] |
a723baf1 | 6002 | attributes [opt] = assignment-expression |
21526606 | 6003 | |
a723baf1 MM |
6004 | Returns the expression that should be tested. */ |
6005 | ||
6006 | static tree | |
94edc4ab | 6007 | cp_parser_condition (cp_parser* parser) |
a723baf1 MM |
6008 | { |
6009 | tree type_specifiers; | |
6010 | const char *saved_message; | |
6011 | ||
6012 | /* Try the declaration first. */ | |
6013 | cp_parser_parse_tentatively (parser); | |
6014 | /* New types are not allowed in the type-specifier-seq for a | |
6015 | condition. */ | |
6016 | saved_message = parser->type_definition_forbidden_message; | |
6017 | parser->type_definition_forbidden_message | |
6018 | = "types may not be defined in conditions"; | |
6019 | /* Parse the type-specifier-seq. */ | |
6020 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
6021 | /* Restore the saved message. */ | |
6022 | parser->type_definition_forbidden_message = saved_message; | |
6023 | /* If all is well, we might be looking at a declaration. */ | |
6024 | if (!cp_parser_error_occurred (parser)) | |
6025 | { | |
6026 | tree decl; | |
6027 | tree asm_specification; | |
6028 | tree attributes; | |
6029 | tree declarator; | |
6030 | tree initializer = NULL_TREE; | |
21526606 | 6031 | |
a723baf1 | 6032 | /* Parse the declarator. */ |
62b8a44e | 6033 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
6034 | /*ctor_dtor_or_conv_p=*/NULL, |
6035 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
6036 | /* Parse the attributes. */ |
6037 | attributes = cp_parser_attributes_opt (parser); | |
6038 | /* Parse the asm-specification. */ | |
6039 | asm_specification = cp_parser_asm_specification_opt (parser); | |
6040 | /* If the next token is not an `=', then we might still be | |
6041 | looking at an expression. For example: | |
21526606 | 6042 | |
a723baf1 | 6043 | if (A(a).x) |
21526606 | 6044 | |
a723baf1 MM |
6045 | looks like a decl-specifier-seq and a declarator -- but then |
6046 | there is no `=', so this is an expression. */ | |
6047 | cp_parser_require (parser, CPP_EQ, "`='"); | |
6048 | /* If we did see an `=', then we are looking at a declaration | |
6049 | for sure. */ | |
6050 | if (cp_parser_parse_definitely (parser)) | |
6051 | { | |
6052 | /* Create the declaration. */ | |
21526606 | 6053 | decl = start_decl (declarator, type_specifiers, |
a723baf1 MM |
6054 | /*initialized_p=*/true, |
6055 | attributes, /*prefix_attributes=*/NULL_TREE); | |
6056 | /* Parse the assignment-expression. */ | |
6057 | initializer = cp_parser_assignment_expression (parser); | |
21526606 | 6058 | |
a723baf1 | 6059 | /* Process the initializer. */ |
21526606 EC |
6060 | cp_finish_decl (decl, |
6061 | initializer, | |
6062 | asm_specification, | |
a723baf1 | 6063 | LOOKUP_ONLYCONVERTING); |
21526606 | 6064 | |
a723baf1 MM |
6065 | return convert_from_reference (decl); |
6066 | } | |
6067 | } | |
6068 | /* If we didn't even get past the declarator successfully, we are | |
6069 | definitely not looking at a declaration. */ | |
6070 | else | |
6071 | cp_parser_abort_tentative_parse (parser); | |
6072 | ||
6073 | /* Otherwise, we are looking at an expression. */ | |
6074 | return cp_parser_expression (parser); | |
6075 | } | |
6076 | ||
6077 | /* Parse an iteration-statement. | |
6078 | ||
6079 | iteration-statement: | |
6080 | while ( condition ) statement | |
6081 | do statement while ( expression ) ; | |
6082 | for ( for-init-statement condition [opt] ; expression [opt] ) | |
6083 | statement | |
6084 | ||
6085 | Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */ | |
6086 | ||
6087 | static tree | |
94edc4ab | 6088 | cp_parser_iteration_statement (cp_parser* parser) |
a723baf1 MM |
6089 | { |
6090 | cp_token *token; | |
6091 | enum rid keyword; | |
6092 | tree statement; | |
0e59b3fb MM |
6093 | bool in_iteration_statement_p; |
6094 | ||
a723baf1 MM |
6095 | |
6096 | /* Peek at the next token. */ | |
6097 | token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement"); | |
6098 | if (!token) | |
6099 | return error_mark_node; | |
6100 | ||
0e59b3fb | 6101 | /* Remember whether or not we are already within an iteration |
21526606 | 6102 | statement. */ |
0e59b3fb MM |
6103 | in_iteration_statement_p = parser->in_iteration_statement_p; |
6104 | ||
a723baf1 MM |
6105 | /* See what kind of keyword it is. */ |
6106 | keyword = token->keyword; | |
6107 | switch (keyword) | |
6108 | { | |
6109 | case RID_WHILE: | |
6110 | { | |
6111 | tree condition; | |
6112 | ||
6113 | /* Begin the while-statement. */ | |
6114 | statement = begin_while_stmt (); | |
6115 | /* Look for the `('. */ | |
6116 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6117 | /* Parse the condition. */ | |
6118 | condition = cp_parser_condition (parser); | |
6119 | finish_while_stmt_cond (condition, statement); | |
6120 | /* Look for the `)'. */ | |
6121 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6122 | /* Parse the dependent statement. */ | |
0e59b3fb | 6123 | parser->in_iteration_statement_p = true; |
a723baf1 | 6124 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6125 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6126 | /* We're done with the while-statement. */ |
6127 | finish_while_stmt (statement); | |
6128 | } | |
6129 | break; | |
6130 | ||
6131 | case RID_DO: | |
6132 | { | |
6133 | tree expression; | |
6134 | ||
6135 | /* Begin the do-statement. */ | |
6136 | statement = begin_do_stmt (); | |
6137 | /* Parse the body of the do-statement. */ | |
0e59b3fb | 6138 | parser->in_iteration_statement_p = true; |
a723baf1 | 6139 | cp_parser_implicitly_scoped_statement (parser); |
0e59b3fb | 6140 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6141 | finish_do_body (statement); |
6142 | /* Look for the `while' keyword. */ | |
6143 | cp_parser_require_keyword (parser, RID_WHILE, "`while'"); | |
6144 | /* Look for the `('. */ | |
6145 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6146 | /* Parse the expression. */ | |
6147 | expression = cp_parser_expression (parser); | |
6148 | /* We're done with the do-statement. */ | |
6149 | finish_do_stmt (expression, statement); | |
6150 | /* Look for the `)'. */ | |
6151 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
6152 | /* Look for the `;'. */ | |
6153 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6154 | } | |
6155 | break; | |
6156 | ||
6157 | case RID_FOR: | |
6158 | { | |
6159 | tree condition = NULL_TREE; | |
6160 | tree expression = NULL_TREE; | |
6161 | ||
6162 | /* Begin the for-statement. */ | |
6163 | statement = begin_for_stmt (); | |
6164 | /* Look for the `('. */ | |
6165 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
6166 | /* Parse the initialization. */ | |
6167 | cp_parser_for_init_statement (parser); | |
6168 | finish_for_init_stmt (statement); | |
6169 | ||
6170 | /* If there's a condition, process it. */ | |
6171 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6172 | condition = cp_parser_condition (parser); | |
6173 | finish_for_cond (condition, statement); | |
6174 | /* Look for the `;'. */ | |
6175 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6176 | ||
6177 | /* If there's an expression, process it. */ | |
6178 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) | |
6179 | expression = cp_parser_expression (parser); | |
6180 | finish_for_expr (expression, statement); | |
6181 | /* Look for the `)'. */ | |
d5a10cf0 MM |
6182 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); |
6183 | ||
a723baf1 | 6184 | /* Parse the body of the for-statement. */ |
0e59b3fb | 6185 | parser->in_iteration_statement_p = true; |
a723baf1 | 6186 | cp_parser_already_scoped_statement (parser); |
0e59b3fb | 6187 | parser->in_iteration_statement_p = in_iteration_statement_p; |
a723baf1 MM |
6188 | |
6189 | /* We're done with the for-statement. */ | |
6190 | finish_for_stmt (statement); | |
6191 | } | |
6192 | break; | |
6193 | ||
6194 | default: | |
6195 | cp_parser_error (parser, "expected iteration-statement"); | |
6196 | statement = error_mark_node; | |
6197 | break; | |
6198 | } | |
6199 | ||
6200 | return statement; | |
6201 | } | |
6202 | ||
6203 | /* Parse a for-init-statement. | |
6204 | ||
6205 | for-init-statement: | |
6206 | expression-statement | |
6207 | simple-declaration */ | |
6208 | ||
6209 | static void | |
94edc4ab | 6210 | cp_parser_for_init_statement (cp_parser* parser) |
a723baf1 MM |
6211 | { |
6212 | /* If the next token is a `;', then we have an empty | |
34cd5ae7 | 6213 | expression-statement. Grammatically, this is also a |
a723baf1 MM |
6214 | simple-declaration, but an invalid one, because it does not |
6215 | declare anything. Therefore, if we did not handle this case | |
6216 | specially, we would issue an error message about an invalid | |
6217 | declaration. */ | |
6218 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6219 | { | |
6220 | /* We're going to speculatively look for a declaration, falling back | |
6221 | to an expression, if necessary. */ | |
6222 | cp_parser_parse_tentatively (parser); | |
6223 | /* Parse the declaration. */ | |
6224 | cp_parser_simple_declaration (parser, | |
6225 | /*function_definition_allowed_p=*/false); | |
6226 | /* If the tentative parse failed, then we shall need to look for an | |
6227 | expression-statement. */ | |
6228 | if (cp_parser_parse_definitely (parser)) | |
6229 | return; | |
6230 | } | |
6231 | ||
a5bcc582 | 6232 | cp_parser_expression_statement (parser, false); |
a723baf1 MM |
6233 | } |
6234 | ||
6235 | /* Parse a jump-statement. | |
6236 | ||
6237 | jump-statement: | |
6238 | break ; | |
6239 | continue ; | |
6240 | return expression [opt] ; | |
21526606 | 6241 | goto identifier ; |
a723baf1 MM |
6242 | |
6243 | GNU extension: | |
6244 | ||
6245 | jump-statement: | |
6246 | goto * expression ; | |
6247 | ||
6248 | Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or | |
6249 | GOTO_STMT. */ | |
6250 | ||
6251 | static tree | |
94edc4ab | 6252 | cp_parser_jump_statement (cp_parser* parser) |
a723baf1 MM |
6253 | { |
6254 | tree statement = error_mark_node; | |
6255 | cp_token *token; | |
6256 | enum rid keyword; | |
6257 | ||
6258 | /* Peek at the next token. */ | |
6259 | token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement"); | |
6260 | if (!token) | |
6261 | return error_mark_node; | |
6262 | ||
6263 | /* See what kind of keyword it is. */ | |
6264 | keyword = token->keyword; | |
6265 | switch (keyword) | |
6266 | { | |
6267 | case RID_BREAK: | |
0e59b3fb MM |
6268 | if (!parser->in_switch_statement_p |
6269 | && !parser->in_iteration_statement_p) | |
6270 | { | |
6271 | error ("break statement not within loop or switch"); | |
6272 | statement = error_mark_node; | |
6273 | } | |
6274 | else | |
6275 | statement = finish_break_stmt (); | |
a723baf1 MM |
6276 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
6277 | break; | |
6278 | ||
6279 | case RID_CONTINUE: | |
0e59b3fb MM |
6280 | if (!parser->in_iteration_statement_p) |
6281 | { | |
6282 | error ("continue statement not within a loop"); | |
6283 | statement = error_mark_node; | |
6284 | } | |
6285 | else | |
6286 | statement = finish_continue_stmt (); | |
a723baf1 MM |
6287 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
6288 | break; | |
6289 | ||
6290 | case RID_RETURN: | |
6291 | { | |
6292 | tree expr; | |
6293 | ||
21526606 | 6294 | /* If the next token is a `;', then there is no |
a723baf1 MM |
6295 | expression. */ |
6296 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
6297 | expr = cp_parser_expression (parser); | |
6298 | else | |
6299 | expr = NULL_TREE; | |
6300 | /* Build the return-statement. */ | |
6301 | statement = finish_return_stmt (expr); | |
6302 | /* Look for the final `;'. */ | |
6303 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6304 | } | |
6305 | break; | |
6306 | ||
6307 | case RID_GOTO: | |
6308 | /* Create the goto-statement. */ | |
6309 | if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) | |
6310 | { | |
6311 | /* Issue a warning about this use of a GNU extension. */ | |
6312 | if (pedantic) | |
6313 | pedwarn ("ISO C++ forbids computed gotos"); | |
6314 | /* Consume the '*' token. */ | |
6315 | cp_lexer_consume_token (parser->lexer); | |
6316 | /* Parse the dependent expression. */ | |
6317 | finish_goto_stmt (cp_parser_expression (parser)); | |
6318 | } | |
6319 | else | |
6320 | finish_goto_stmt (cp_parser_identifier (parser)); | |
6321 | /* Look for the final `;'. */ | |
6322 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6323 | break; | |
6324 | ||
6325 | default: | |
6326 | cp_parser_error (parser, "expected jump-statement"); | |
6327 | break; | |
6328 | } | |
6329 | ||
6330 | return statement; | |
6331 | } | |
6332 | ||
6333 | /* Parse a declaration-statement. | |
6334 | ||
6335 | declaration-statement: | |
6336 | block-declaration */ | |
6337 | ||
6338 | static void | |
94edc4ab | 6339 | cp_parser_declaration_statement (cp_parser* parser) |
a723baf1 MM |
6340 | { |
6341 | /* Parse the block-declaration. */ | |
6342 | cp_parser_block_declaration (parser, /*statement_p=*/true); | |
6343 | ||
6344 | /* Finish off the statement. */ | |
6345 | finish_stmt (); | |
6346 | } | |
6347 | ||
6348 | /* Some dependent statements (like `if (cond) statement'), are | |
6349 | implicitly in their own scope. In other words, if the statement is | |
6350 | a single statement (as opposed to a compound-statement), it is | |
6351 | none-the-less treated as if it were enclosed in braces. Any | |
6352 | declarations appearing in the dependent statement are out of scope | |
6353 | after control passes that point. This function parses a statement, | |
6354 | but ensures that is in its own scope, even if it is not a | |
21526606 | 6355 | compound-statement. |
a723baf1 MM |
6356 | |
6357 | Returns the new statement. */ | |
6358 | ||
6359 | static tree | |
94edc4ab | 6360 | cp_parser_implicitly_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6361 | { |
6362 | tree statement; | |
6363 | ||
6364 | /* If the token is not a `{', then we must take special action. */ | |
6365 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
6366 | { | |
6367 | /* Create a compound-statement. */ | |
7a3397c7 | 6368 | statement = begin_compound_stmt (/*has_no_scope=*/false); |
a723baf1 | 6369 | /* Parse the dependent-statement. */ |
a5bcc582 | 6370 | cp_parser_statement (parser, false); |
a723baf1 | 6371 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6372 | finish_compound_stmt (statement); |
a723baf1 MM |
6373 | } |
6374 | /* Otherwise, we simply parse the statement directly. */ | |
6375 | else | |
a5bcc582 | 6376 | statement = cp_parser_compound_statement (parser, false); |
a723baf1 MM |
6377 | |
6378 | /* Return the statement. */ | |
6379 | return statement; | |
6380 | } | |
6381 | ||
6382 | /* For some dependent statements (like `while (cond) statement'), we | |
6383 | have already created a scope. Therefore, even if the dependent | |
6384 | statement is a compound-statement, we do not want to create another | |
6385 | scope. */ | |
6386 | ||
6387 | static void | |
94edc4ab | 6388 | cp_parser_already_scoped_statement (cp_parser* parser) |
a723baf1 MM |
6389 | { |
6390 | /* If the token is not a `{', then we must take special action. */ | |
6391 | if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE)) | |
6392 | { | |
6393 | tree statement; | |
6394 | ||
6395 | /* Create a compound-statement. */ | |
7a3397c7 | 6396 | statement = begin_compound_stmt (/*has_no_scope=*/true); |
a723baf1 | 6397 | /* Parse the dependent-statement. */ |
a5bcc582 | 6398 | cp_parser_statement (parser, false); |
a723baf1 | 6399 | /* Finish the dummy compound-statement. */ |
7a3397c7 | 6400 | finish_compound_stmt (statement); |
a723baf1 MM |
6401 | } |
6402 | /* Otherwise, we simply parse the statement directly. */ | |
6403 | else | |
a5bcc582 | 6404 | cp_parser_statement (parser, false); |
a723baf1 MM |
6405 | } |
6406 | ||
6407 | /* Declarations [gram.dcl.dcl] */ | |
6408 | ||
6409 | /* Parse an optional declaration-sequence. | |
6410 | ||
6411 | declaration-seq: | |
6412 | declaration | |
6413 | declaration-seq declaration */ | |
6414 | ||
6415 | static void | |
94edc4ab | 6416 | cp_parser_declaration_seq_opt (cp_parser* parser) |
a723baf1 MM |
6417 | { |
6418 | while (true) | |
6419 | { | |
6420 | cp_token *token; | |
6421 | ||
6422 | token = cp_lexer_peek_token (parser->lexer); | |
6423 | ||
6424 | if (token->type == CPP_CLOSE_BRACE | |
6425 | || token->type == CPP_EOF) | |
6426 | break; | |
6427 | ||
21526606 | 6428 | if (token->type == CPP_SEMICOLON) |
a723baf1 MM |
6429 | { |
6430 | /* A declaration consisting of a single semicolon is | |
6431 | invalid. Allow it unless we're being pedantic. */ | |
499b568f | 6432 | if (pedantic && !in_system_header) |
a723baf1 MM |
6433 | pedwarn ("extra `;'"); |
6434 | cp_lexer_consume_token (parser->lexer); | |
6435 | continue; | |
6436 | } | |
6437 | ||
c838d82f | 6438 | /* The C lexer modifies PENDING_LANG_CHANGE when it wants the |
34cd5ae7 | 6439 | parser to enter or exit implicit `extern "C"' blocks. */ |
c838d82f MM |
6440 | while (pending_lang_change > 0) |
6441 | { | |
6442 | push_lang_context (lang_name_c); | |
6443 | --pending_lang_change; | |
6444 | } | |
6445 | while (pending_lang_change < 0) | |
6446 | { | |
6447 | pop_lang_context (); | |
6448 | ++pending_lang_change; | |
6449 | } | |
6450 | ||
6451 | /* Parse the declaration itself. */ | |
a723baf1 MM |
6452 | cp_parser_declaration (parser); |
6453 | } | |
6454 | } | |
6455 | ||
6456 | /* Parse a declaration. | |
6457 | ||
6458 | declaration: | |
6459 | block-declaration | |
6460 | function-definition | |
6461 | template-declaration | |
6462 | explicit-instantiation | |
6463 | explicit-specialization | |
6464 | linkage-specification | |
21526606 | 6465 | namespace-definition |
1092805d MM |
6466 | |
6467 | GNU extension: | |
6468 | ||
6469 | declaration: | |
6470 | __extension__ declaration */ | |
a723baf1 MM |
6471 | |
6472 | static void | |
94edc4ab | 6473 | cp_parser_declaration (cp_parser* parser) |
a723baf1 MM |
6474 | { |
6475 | cp_token token1; | |
6476 | cp_token token2; | |
1092805d MM |
6477 | int saved_pedantic; |
6478 | ||
21526606 EC |
6479 | /* Set this here since we can be called after |
6480 | pushing the linkage specification. */ | |
0173bb6f | 6481 | c_lex_string_translate = 1; |
21526606 | 6482 | |
1092805d MM |
6483 | /* Check for the `__extension__' keyword. */ |
6484 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6485 | { | |
6486 | /* Parse the qualified declaration. */ | |
6487 | cp_parser_declaration (parser); | |
6488 | /* Restore the PEDANTIC flag. */ | |
6489 | pedantic = saved_pedantic; | |
6490 | ||
6491 | return; | |
6492 | } | |
a723baf1 MM |
6493 | |
6494 | /* Try to figure out what kind of declaration is present. */ | |
6495 | token1 = *cp_lexer_peek_token (parser->lexer); | |
21526606 EC |
6496 | |
6497 | /* Don't translate the CPP_STRING in extern "C". */ | |
6498 | if (token1.keyword == RID_EXTERN) | |
0173bb6f | 6499 | c_lex_string_translate = 0; |
21526606 | 6500 | |
a723baf1 MM |
6501 | if (token1.type != CPP_EOF) |
6502 | token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); | |
6503 | ||
0173bb6f | 6504 | c_lex_string_translate = 1; |
77a705e4 | 6505 | |
a723baf1 MM |
6506 | /* If the next token is `extern' and the following token is a string |
6507 | literal, then we have a linkage specification. */ | |
6508 | if (token1.keyword == RID_EXTERN | |
6509 | && cp_parser_is_string_literal (&token2)) | |
6510 | cp_parser_linkage_specification (parser); | |
6511 | /* If the next token is `template', then we have either a template | |
6512 | declaration, an explicit instantiation, or an explicit | |
6513 | specialization. */ | |
6514 | else if (token1.keyword == RID_TEMPLATE) | |
6515 | { | |
6516 | /* `template <>' indicates a template specialization. */ | |
6517 | if (token2.type == CPP_LESS | |
6518 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) | |
6519 | cp_parser_explicit_specialization (parser); | |
6520 | /* `template <' indicates a template declaration. */ | |
6521 | else if (token2.type == CPP_LESS) | |
6522 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6523 | /* Anything else must be an explicit instantiation. */ | |
6524 | else | |
6525 | cp_parser_explicit_instantiation (parser); | |
6526 | } | |
6527 | /* If the next token is `export', then we have a template | |
6528 | declaration. */ | |
6529 | else if (token1.keyword == RID_EXPORT) | |
6530 | cp_parser_template_declaration (parser, /*member_p=*/false); | |
6531 | /* If the next token is `extern', 'static' or 'inline' and the one | |
6532 | after that is `template', we have a GNU extended explicit | |
6533 | instantiation directive. */ | |
6534 | else if (cp_parser_allow_gnu_extensions_p (parser) | |
6535 | && (token1.keyword == RID_EXTERN | |
6536 | || token1.keyword == RID_STATIC | |
6537 | || token1.keyword == RID_INLINE) | |
6538 | && token2.keyword == RID_TEMPLATE) | |
6539 | cp_parser_explicit_instantiation (parser); | |
6540 | /* If the next token is `namespace', check for a named or unnamed | |
6541 | namespace definition. */ | |
6542 | else if (token1.keyword == RID_NAMESPACE | |
6543 | && (/* A named namespace definition. */ | |
6544 | (token2.type == CPP_NAME | |
21526606 | 6545 | && (cp_lexer_peek_nth_token (parser->lexer, 3)->type |
a723baf1 MM |
6546 | == CPP_OPEN_BRACE)) |
6547 | /* An unnamed namespace definition. */ | |
6548 | || token2.type == CPP_OPEN_BRACE)) | |
6549 | cp_parser_namespace_definition (parser); | |
6550 | /* We must have either a block declaration or a function | |
6551 | definition. */ | |
6552 | else | |
6553 | /* Try to parse a block-declaration, or a function-definition. */ | |
6554 | cp_parser_block_declaration (parser, /*statement_p=*/false); | |
6555 | } | |
6556 | ||
21526606 | 6557 | /* Parse a block-declaration. |
a723baf1 MM |
6558 | |
6559 | block-declaration: | |
6560 | simple-declaration | |
6561 | asm-definition | |
6562 | namespace-alias-definition | |
6563 | using-declaration | |
21526606 | 6564 | using-directive |
a723baf1 MM |
6565 | |
6566 | GNU Extension: | |
6567 | ||
6568 | block-declaration: | |
21526606 | 6569 | __extension__ block-declaration |
a723baf1 MM |
6570 | label-declaration |
6571 | ||
34cd5ae7 | 6572 | If STATEMENT_P is TRUE, then this block-declaration is occurring as |
a723baf1 MM |
6573 | part of a declaration-statement. */ |
6574 | ||
6575 | static void | |
21526606 | 6576 | cp_parser_block_declaration (cp_parser *parser, |
a723baf1 MM |
6577 | bool statement_p) |
6578 | { | |
6579 | cp_token *token1; | |
6580 | int saved_pedantic; | |
6581 | ||
6582 | /* Check for the `__extension__' keyword. */ | |
6583 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
6584 | { | |
6585 | /* Parse the qualified declaration. */ | |
6586 | cp_parser_block_declaration (parser, statement_p); | |
6587 | /* Restore the PEDANTIC flag. */ | |
6588 | pedantic = saved_pedantic; | |
6589 | ||
6590 | return; | |
6591 | } | |
6592 | ||
6593 | /* Peek at the next token to figure out which kind of declaration is | |
6594 | present. */ | |
6595 | token1 = cp_lexer_peek_token (parser->lexer); | |
6596 | ||
6597 | /* If the next keyword is `asm', we have an asm-definition. */ | |
6598 | if (token1->keyword == RID_ASM) | |
6599 | { | |
6600 | if (statement_p) | |
6601 | cp_parser_commit_to_tentative_parse (parser); | |
6602 | cp_parser_asm_definition (parser); | |
6603 | } | |
6604 | /* If the next keyword is `namespace', we have a | |
6605 | namespace-alias-definition. */ | |
6606 | else if (token1->keyword == RID_NAMESPACE) | |
6607 | cp_parser_namespace_alias_definition (parser); | |
6608 | /* If the next keyword is `using', we have either a | |
6609 | using-declaration or a using-directive. */ | |
6610 | else if (token1->keyword == RID_USING) | |
6611 | { | |
6612 | cp_token *token2; | |
6613 | ||
6614 | if (statement_p) | |
6615 | cp_parser_commit_to_tentative_parse (parser); | |
6616 | /* If the token after `using' is `namespace', then we have a | |
6617 | using-directive. */ | |
6618 | token2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
6619 | if (token2->keyword == RID_NAMESPACE) | |
6620 | cp_parser_using_directive (parser); | |
6621 | /* Otherwise, it's a using-declaration. */ | |
6622 | else | |
6623 | cp_parser_using_declaration (parser); | |
6624 | } | |
6625 | /* If the next keyword is `__label__' we have a label declaration. */ | |
6626 | else if (token1->keyword == RID_LABEL) | |
6627 | { | |
6628 | if (statement_p) | |
6629 | cp_parser_commit_to_tentative_parse (parser); | |
6630 | cp_parser_label_declaration (parser); | |
6631 | } | |
6632 | /* Anything else must be a simple-declaration. */ | |
6633 | else | |
6634 | cp_parser_simple_declaration (parser, !statement_p); | |
6635 | } | |
6636 | ||
6637 | /* Parse a simple-declaration. | |
6638 | ||
6639 | simple-declaration: | |
21526606 | 6640 | decl-specifier-seq [opt] init-declarator-list [opt] ; |
a723baf1 MM |
6641 | |
6642 | init-declarator-list: | |
6643 | init-declarator | |
21526606 | 6644 | init-declarator-list , init-declarator |
a723baf1 | 6645 | |
34cd5ae7 | 6646 | If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a |
9bcb9aae | 6647 | function-definition as a simple-declaration. */ |
a723baf1 MM |
6648 | |
6649 | static void | |
21526606 | 6650 | cp_parser_simple_declaration (cp_parser* parser, |
94edc4ab | 6651 | bool function_definition_allowed_p) |
a723baf1 MM |
6652 | { |
6653 | tree decl_specifiers; | |
6654 | tree attributes; | |
560ad596 | 6655 | int declares_class_or_enum; |
a723baf1 MM |
6656 | bool saw_declarator; |
6657 | ||
6658 | /* Defer access checks until we know what is being declared; the | |
6659 | checks for names appearing in the decl-specifier-seq should be | |
6660 | done as if we were in the scope of the thing being declared. */ | |
8d241e0b | 6661 | push_deferring_access_checks (dk_deferred); |
cf22909c | 6662 | |
a723baf1 MM |
6663 | /* Parse the decl-specifier-seq. We have to keep track of whether |
6664 | or not the decl-specifier-seq declares a named class or | |
6665 | enumeration type, since that is the only case in which the | |
21526606 | 6666 | init-declarator-list is allowed to be empty. |
a723baf1 MM |
6667 | |
6668 | [dcl.dcl] | |
6669 | ||
6670 | In a simple-declaration, the optional init-declarator-list can be | |
6671 | omitted only when declaring a class or enumeration, that is when | |
6672 | the decl-specifier-seq contains either a class-specifier, an | |
6673 | elaborated-type-specifier, or an enum-specifier. */ | |
6674 | decl_specifiers | |
21526606 | 6675 | = cp_parser_decl_specifier_seq (parser, |
a723baf1 MM |
6676 | CP_PARSER_FLAGS_OPTIONAL, |
6677 | &attributes, | |
6678 | &declares_class_or_enum); | |
6679 | /* We no longer need to defer access checks. */ | |
cf22909c | 6680 | stop_deferring_access_checks (); |
24c0ef37 | 6681 | |
39703eb9 MM |
6682 | /* In a block scope, a valid declaration must always have a |
6683 | decl-specifier-seq. By not trying to parse declarators, we can | |
6684 | resolve the declaration/expression ambiguity more quickly. */ | |
6685 | if (!function_definition_allowed_p && !decl_specifiers) | |
6686 | { | |
6687 | cp_parser_error (parser, "expected declaration"); | |
6688 | goto done; | |
6689 | } | |
6690 | ||
8fbc5ae7 MM |
6691 | /* If the next two tokens are both identifiers, the code is |
6692 | erroneous. The usual cause of this situation is code like: | |
6693 | ||
6694 | T t; | |
6695 | ||
6696 | where "T" should name a type -- but does not. */ | |
2097b5f2 | 6697 | if (cp_parser_parse_and_diagnose_invalid_type_name (parser)) |
8fbc5ae7 | 6698 | { |
8d241e0b | 6699 | /* If parsing tentatively, we should commit; we really are |
8fbc5ae7 MM |
6700 | looking at a declaration. */ |
6701 | cp_parser_commit_to_tentative_parse (parser); | |
6702 | /* Give up. */ | |
39703eb9 | 6703 | goto done; |
8fbc5ae7 MM |
6704 | } |
6705 | ||
a723baf1 MM |
6706 | /* Keep going until we hit the `;' at the end of the simple |
6707 | declaration. */ | |
6708 | saw_declarator = false; | |
21526606 | 6709 | while (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
6710 | CPP_SEMICOLON)) |
6711 | { | |
6712 | cp_token *token; | |
6713 | bool function_definition_p; | |
560ad596 | 6714 | tree decl; |
a723baf1 MM |
6715 | |
6716 | saw_declarator = true; | |
6717 | /* Parse the init-declarator. */ | |
560ad596 MM |
6718 | decl = cp_parser_init_declarator (parser, decl_specifiers, attributes, |
6719 | function_definition_allowed_p, | |
6720 | /*member_p=*/false, | |
6721 | declares_class_or_enum, | |
6722 | &function_definition_p); | |
1fb3244a MM |
6723 | /* If an error occurred while parsing tentatively, exit quickly. |
6724 | (That usually happens when in the body of a function; each | |
6725 | statement is treated as a declaration-statement until proven | |
6726 | otherwise.) */ | |
6727 | if (cp_parser_error_occurred (parser)) | |
39703eb9 | 6728 | goto done; |
a723baf1 MM |
6729 | /* Handle function definitions specially. */ |
6730 | if (function_definition_p) | |
6731 | { | |
6732 | /* If the next token is a `,', then we are probably | |
6733 | processing something like: | |
6734 | ||
6735 | void f() {}, *p; | |
6736 | ||
6737 | which is erroneous. */ | |
6738 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
6739 | error ("mixing declarations and function-definitions is forbidden"); | |
6740 | /* Otherwise, we're done with the list of declarators. */ | |
6741 | else | |
24c0ef37 | 6742 | { |
cf22909c | 6743 | pop_deferring_access_checks (); |
24c0ef37 GS |
6744 | return; |
6745 | } | |
a723baf1 MM |
6746 | } |
6747 | /* The next token should be either a `,' or a `;'. */ | |
6748 | token = cp_lexer_peek_token (parser->lexer); | |
6749 | /* If it's a `,', there are more declarators to come. */ | |
6750 | if (token->type == CPP_COMMA) | |
6751 | cp_lexer_consume_token (parser->lexer); | |
6752 | /* If it's a `;', we are done. */ | |
6753 | else if (token->type == CPP_SEMICOLON) | |
6754 | break; | |
6755 | /* Anything else is an error. */ | |
6756 | else | |
6757 | { | |
6758 | cp_parser_error (parser, "expected `,' or `;'"); | |
6759 | /* Skip tokens until we reach the end of the statement. */ | |
6760 | cp_parser_skip_to_end_of_statement (parser); | |
5a98fa7b MM |
6761 | /* If the next token is now a `;', consume it. */ |
6762 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
6763 | cp_lexer_consume_token (parser->lexer); | |
39703eb9 | 6764 | goto done; |
a723baf1 MM |
6765 | } |
6766 | /* After the first time around, a function-definition is not | |
6767 | allowed -- even if it was OK at first. For example: | |
6768 | ||
6769 | int i, f() {} | |
6770 | ||
6771 | is not valid. */ | |
6772 | function_definition_allowed_p = false; | |
6773 | } | |
6774 | ||
6775 | /* Issue an error message if no declarators are present, and the | |
6776 | decl-specifier-seq does not itself declare a class or | |
6777 | enumeration. */ | |
6778 | if (!saw_declarator) | |
6779 | { | |
6780 | if (cp_parser_declares_only_class_p (parser)) | |
6781 | shadow_tag (decl_specifiers); | |
6782 | /* Perform any deferred access checks. */ | |
cf22909c | 6783 | perform_deferred_access_checks (); |
a723baf1 MM |
6784 | } |
6785 | ||
6786 | /* Consume the `;'. */ | |
6787 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
6788 | ||
39703eb9 MM |
6789 | done: |
6790 | pop_deferring_access_checks (); | |
a723baf1 MM |
6791 | } |
6792 | ||
6793 | /* Parse a decl-specifier-seq. | |
6794 | ||
6795 | decl-specifier-seq: | |
6796 | decl-specifier-seq [opt] decl-specifier | |
6797 | ||
6798 | decl-specifier: | |
6799 | storage-class-specifier | |
6800 | type-specifier | |
6801 | function-specifier | |
6802 | friend | |
21526606 | 6803 | typedef |
a723baf1 MM |
6804 | |
6805 | GNU Extension: | |
6806 | ||
15077df5 MM |
6807 | decl-specifier: |
6808 | attributes | |
a723baf1 MM |
6809 | |
6810 | Returns a TREE_LIST, giving the decl-specifiers in the order they | |
6811 | appear in the source code. The TREE_VALUE of each node is the | |
6812 | decl-specifier. For a keyword (such as `auto' or `friend'), the | |
34cd5ae7 | 6813 | TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the |
21526606 | 6814 | representation of a type-specifier, see cp_parser_type_specifier. |
a723baf1 MM |
6815 | |
6816 | If there are attributes, they will be stored in *ATTRIBUTES, | |
21526606 | 6817 | represented as described above cp_parser_attributes. |
a723baf1 MM |
6818 | |
6819 | If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier | |
6820 | appears, and the entity that will be a friend is not going to be a | |
6821 | class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that | |
6822 | even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which | |
21526606 | 6823 | friendship is granted might not be a class. |
560ad596 MM |
6824 | |
6825 | *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following | |
543ca912 | 6826 | flags: |
560ad596 MM |
6827 | |
6828 | 1: one of the decl-specifiers is an elaborated-type-specifier | |
543ca912 | 6829 | (i.e., a type declaration) |
560ad596 | 6830 | 2: one of the decl-specifiers is an enum-specifier or a |
543ca912 | 6831 | class-specifier (i.e., a type definition) |
560ad596 MM |
6832 | |
6833 | */ | |
a723baf1 MM |
6834 | |
6835 | static tree | |
21526606 EC |
6836 | cp_parser_decl_specifier_seq (cp_parser* parser, |
6837 | cp_parser_flags flags, | |
94edc4ab | 6838 | tree* attributes, |
560ad596 | 6839 | int* declares_class_or_enum) |
a723baf1 MM |
6840 | { |
6841 | tree decl_specs = NULL_TREE; | |
6842 | bool friend_p = false; | |
f2ce60b8 | 6843 | bool constructor_possible_p = !parser->in_declarator_p; |
21526606 | 6844 | |
a723baf1 | 6845 | /* Assume no class or enumeration type is declared. */ |
560ad596 | 6846 | *declares_class_or_enum = 0; |
a723baf1 MM |
6847 | |
6848 | /* Assume there are no attributes. */ | |
6849 | *attributes = NULL_TREE; | |
6850 | ||
6851 | /* Keep reading specifiers until there are no more to read. */ | |
6852 | while (true) | |
6853 | { | |
6854 | tree decl_spec = NULL_TREE; | |
6855 | bool constructor_p; | |
6856 | cp_token *token; | |
6857 | ||
6858 | /* Peek at the next token. */ | |
6859 | token = cp_lexer_peek_token (parser->lexer); | |
6860 | /* Handle attributes. */ | |
6861 | if (token->keyword == RID_ATTRIBUTE) | |
6862 | { | |
6863 | /* Parse the attributes. */ | |
6864 | decl_spec = cp_parser_attributes_opt (parser); | |
6865 | /* Add them to the list. */ | |
6866 | *attributes = chainon (*attributes, decl_spec); | |
6867 | continue; | |
6868 | } | |
6869 | /* If the next token is an appropriate keyword, we can simply | |
6870 | add it to the list. */ | |
6871 | switch (token->keyword) | |
6872 | { | |
6873 | case RID_FRIEND: | |
6874 | /* decl-specifier: | |
6875 | friend */ | |
1918facf SB |
6876 | if (friend_p) |
6877 | error ("duplicate `friend'"); | |
6878 | else | |
6879 | friend_p = true; | |
a723baf1 MM |
6880 | /* The representation of the specifier is simply the |
6881 | appropriate TREE_IDENTIFIER node. */ | |
6882 | decl_spec = token->value; | |
6883 | /* Consume the token. */ | |
6884 | cp_lexer_consume_token (parser->lexer); | |
6885 | break; | |
6886 | ||
6887 | /* function-specifier: | |
6888 | inline | |
6889 | virtual | |
6890 | explicit */ | |
6891 | case RID_INLINE: | |
6892 | case RID_VIRTUAL: | |
6893 | case RID_EXPLICIT: | |
6894 | decl_spec = cp_parser_function_specifier_opt (parser); | |
6895 | break; | |
21526606 | 6896 | |
a723baf1 MM |
6897 | /* decl-specifier: |
6898 | typedef */ | |
6899 | case RID_TYPEDEF: | |
6900 | /* The representation of the specifier is simply the | |
6901 | appropriate TREE_IDENTIFIER node. */ | |
6902 | decl_spec = token->value; | |
6903 | /* Consume the token. */ | |
6904 | cp_lexer_consume_token (parser->lexer); | |
2050a1bb MM |
6905 | /* A constructor declarator cannot appear in a typedef. */ |
6906 | constructor_possible_p = false; | |
c006d942 MM |
6907 | /* The "typedef" keyword can only occur in a declaration; we |
6908 | may as well commit at this point. */ | |
6909 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
6910 | break; |
6911 | ||
6912 | /* storage-class-specifier: | |
6913 | auto | |
6914 | register | |
6915 | static | |
6916 | extern | |
21526606 | 6917 | mutable |
a723baf1 MM |
6918 | |
6919 | GNU Extension: | |
6920 | thread */ | |
6921 | case RID_AUTO: | |
6922 | case RID_REGISTER: | |
6923 | case RID_STATIC: | |
6924 | case RID_EXTERN: | |
6925 | case RID_MUTABLE: | |
6926 | case RID_THREAD: | |
6927 | decl_spec = cp_parser_storage_class_specifier_opt (parser); | |
6928 | break; | |
21526606 | 6929 | |
a723baf1 MM |
6930 | default: |
6931 | break; | |
6932 | } | |
6933 | ||
6934 | /* Constructors are a special case. The `S' in `S()' is not a | |
6935 | decl-specifier; it is the beginning of the declarator. */ | |
21526606 | 6936 | constructor_p = (!decl_spec |
2050a1bb | 6937 | && constructor_possible_p |
a723baf1 MM |
6938 | && cp_parser_constructor_declarator_p (parser, |
6939 | friend_p)); | |
6940 | ||
6941 | /* If we don't have a DECL_SPEC yet, then we must be looking at | |
6942 | a type-specifier. */ | |
6943 | if (!decl_spec && !constructor_p) | |
6944 | { | |
560ad596 | 6945 | int decl_spec_declares_class_or_enum; |
a723baf1 MM |
6946 | bool is_cv_qualifier; |
6947 | ||
6948 | decl_spec | |
6949 | = cp_parser_type_specifier (parser, flags, | |
6950 | friend_p, | |
6951 | /*is_declaration=*/true, | |
6952 | &decl_spec_declares_class_or_enum, | |
6953 | &is_cv_qualifier); | |
6954 | ||
6955 | *declares_class_or_enum |= decl_spec_declares_class_or_enum; | |
6956 | ||
6957 | /* If this type-specifier referenced a user-defined type | |
6958 | (a typedef, class-name, etc.), then we can't allow any | |
6959 | more such type-specifiers henceforth. | |
6960 | ||
6961 | [dcl.spec] | |
6962 | ||
6963 | The longest sequence of decl-specifiers that could | |
6964 | possibly be a type name is taken as the | |
6965 | decl-specifier-seq of a declaration. The sequence shall | |
6966 | be self-consistent as described below. | |
6967 | ||
6968 | [dcl.type] | |
6969 | ||
6970 | As a general rule, at most one type-specifier is allowed | |
6971 | in the complete decl-specifier-seq of a declaration. The | |
6972 | only exceptions are the following: | |
6973 | ||
6974 | -- const or volatile can be combined with any other | |
21526606 | 6975 | type-specifier. |
a723baf1 MM |
6976 | |
6977 | -- signed or unsigned can be combined with char, long, | |
6978 | short, or int. | |
6979 | ||
6980 | -- .. | |
6981 | ||
6982 | Example: | |
6983 | ||
6984 | typedef char* Pc; | |
6985 | void g (const int Pc); | |
6986 | ||
6987 | Here, Pc is *not* part of the decl-specifier seq; it's | |
6988 | the declarator. Therefore, once we see a type-specifier | |
6989 | (other than a cv-qualifier), we forbid any additional | |
6990 | user-defined types. We *do* still allow things like `int | |
6991 | int' to be considered a decl-specifier-seq, and issue the | |
6992 | error message later. */ | |
6993 | if (decl_spec && !is_cv_qualifier) | |
6994 | flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; | |
2050a1bb MM |
6995 | /* A constructor declarator cannot follow a type-specifier. */ |
6996 | if (decl_spec) | |
6997 | constructor_possible_p = false; | |
a723baf1 MM |
6998 | } |
6999 | ||
7000 | /* If we still do not have a DECL_SPEC, then there are no more | |
7001 | decl-specifiers. */ | |
7002 | if (!decl_spec) | |
7003 | { | |
7004 | /* Issue an error message, unless the entire construct was | |
7005 | optional. */ | |
7006 | if (!(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
7007 | { | |
7008 | cp_parser_error (parser, "expected decl specifier"); | |
7009 | return error_mark_node; | |
7010 | } | |
7011 | ||
7012 | break; | |
7013 | } | |
7014 | ||
7015 | /* Add the DECL_SPEC to the list of specifiers. */ | |
e90c7b84 ILT |
7016 | if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node) |
7017 | decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs); | |
a723baf1 MM |
7018 | |
7019 | /* After we see one decl-specifier, further decl-specifiers are | |
7020 | always optional. */ | |
7021 | flags |= CP_PARSER_FLAGS_OPTIONAL; | |
7022 | } | |
7023 | ||
0426c4ca SB |
7024 | /* Don't allow a friend specifier with a class definition. */ |
7025 | if (friend_p && (*declares_class_or_enum & 2)) | |
7026 | error ("class definition may not be declared a friend"); | |
7027 | ||
a723baf1 MM |
7028 | /* We have built up the DECL_SPECS in reverse order. Return them in |
7029 | the correct order. */ | |
7030 | return nreverse (decl_specs); | |
7031 | } | |
7032 | ||
21526606 | 7033 | /* Parse an (optional) storage-class-specifier. |
a723baf1 MM |
7034 | |
7035 | storage-class-specifier: | |
7036 | auto | |
7037 | register | |
7038 | static | |
7039 | extern | |
21526606 | 7040 | mutable |
a723baf1 MM |
7041 | |
7042 | GNU Extension: | |
7043 | ||
7044 | storage-class-specifier: | |
7045 | thread | |
7046 | ||
7047 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
21526606 | 7048 | |
a723baf1 | 7049 | static tree |
94edc4ab | 7050 | cp_parser_storage_class_specifier_opt (cp_parser* parser) |
a723baf1 MM |
7051 | { |
7052 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7053 | { | |
7054 | case RID_AUTO: | |
7055 | case RID_REGISTER: | |
7056 | case RID_STATIC: | |
7057 | case RID_EXTERN: | |
7058 | case RID_MUTABLE: | |
7059 | case RID_THREAD: | |
7060 | /* Consume the token. */ | |
7061 | return cp_lexer_consume_token (parser->lexer)->value; | |
7062 | ||
7063 | default: | |
7064 | return NULL_TREE; | |
7065 | } | |
7066 | } | |
7067 | ||
21526606 | 7068 | /* Parse an (optional) function-specifier. |
a723baf1 MM |
7069 | |
7070 | function-specifier: | |
7071 | inline | |
7072 | virtual | |
7073 | explicit | |
7074 | ||
7075 | Returns an IDENTIFIER_NODE corresponding to the keyword used. */ | |
21526606 | 7076 | |
a723baf1 | 7077 | static tree |
94edc4ab | 7078 | cp_parser_function_specifier_opt (cp_parser* parser) |
a723baf1 MM |
7079 | { |
7080 | switch (cp_lexer_peek_token (parser->lexer)->keyword) | |
7081 | { | |
7082 | case RID_INLINE: | |
7083 | case RID_VIRTUAL: | |
7084 | case RID_EXPLICIT: | |
7085 | /* Consume the token. */ | |
7086 | return cp_lexer_consume_token (parser->lexer)->value; | |
7087 | ||
7088 | default: | |
7089 | return NULL_TREE; | |
7090 | } | |
7091 | } | |
7092 | ||
7093 | /* Parse a linkage-specification. | |
7094 | ||
7095 | linkage-specification: | |
7096 | extern string-literal { declaration-seq [opt] } | |
7097 | extern string-literal declaration */ | |
7098 | ||
7099 | static void | |
94edc4ab | 7100 | cp_parser_linkage_specification (cp_parser* parser) |
a723baf1 MM |
7101 | { |
7102 | cp_token *token; | |
7103 | tree linkage; | |
7104 | ||
7105 | /* Look for the `extern' keyword. */ | |
7106 | cp_parser_require_keyword (parser, RID_EXTERN, "`extern'"); | |
7107 | ||
7108 | /* Peek at the next token. */ | |
7109 | token = cp_lexer_peek_token (parser->lexer); | |
7110 | /* If it's not a string-literal, then there's a problem. */ | |
7111 | if (!cp_parser_is_string_literal (token)) | |
7112 | { | |
7113 | cp_parser_error (parser, "expected language-name"); | |
7114 | return; | |
7115 | } | |
7116 | /* Consume the token. */ | |
7117 | cp_lexer_consume_token (parser->lexer); | |
7118 | ||
7119 | /* Transform the literal into an identifier. If the literal is a | |
7120 | wide-character string, or contains embedded NULs, then we can't | |
7121 | handle it as the user wants. */ | |
7122 | if (token->type == CPP_WSTRING | |
7123 | || (strlen (TREE_STRING_POINTER (token->value)) | |
7124 | != (size_t) (TREE_STRING_LENGTH (token->value) - 1))) | |
7125 | { | |
7126 | cp_parser_error (parser, "invalid linkage-specification"); | |
7127 | /* Assume C++ linkage. */ | |
7128 | linkage = get_identifier ("c++"); | |
7129 | } | |
0173bb6f AO |
7130 | /* If the string is chained to another string, take the latter, |
7131 | that's the untranslated string. */ | |
7132 | else if (TREE_CHAIN (token->value)) | |
7133 | linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value))); | |
a723baf1 MM |
7134 | /* If it's a simple string constant, things are easier. */ |
7135 | else | |
7136 | linkage = get_identifier (TREE_STRING_POINTER (token->value)); | |
7137 | ||
7138 | /* We're now using the new linkage. */ | |
7139 | push_lang_context (linkage); | |
7140 | ||
7141 | /* If the next token is a `{', then we're using the first | |
7142 | production. */ | |
7143 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
7144 | { | |
7145 | /* Consume the `{' token. */ | |
7146 | cp_lexer_consume_token (parser->lexer); | |
7147 | /* Parse the declarations. */ | |
7148 | cp_parser_declaration_seq_opt (parser); | |
7149 | /* Look for the closing `}'. */ | |
7150 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
7151 | } | |
7152 | /* Otherwise, there's just one declaration. */ | |
7153 | else | |
7154 | { | |
7155 | bool saved_in_unbraced_linkage_specification_p; | |
7156 | ||
21526606 | 7157 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
7158 | = parser->in_unbraced_linkage_specification_p; |
7159 | parser->in_unbraced_linkage_specification_p = true; | |
7160 | have_extern_spec = true; | |
7161 | cp_parser_declaration (parser); | |
7162 | have_extern_spec = false; | |
21526606 | 7163 | parser->in_unbraced_linkage_specification_p |
a723baf1 MM |
7164 | = saved_in_unbraced_linkage_specification_p; |
7165 | } | |
7166 | ||
7167 | /* We're done with the linkage-specification. */ | |
7168 | pop_lang_context (); | |
7169 | } | |
7170 | ||
7171 | /* Special member functions [gram.special] */ | |
7172 | ||
7173 | /* Parse a conversion-function-id. | |
7174 | ||
7175 | conversion-function-id: | |
21526606 | 7176 | operator conversion-type-id |
a723baf1 MM |
7177 | |
7178 | Returns an IDENTIFIER_NODE representing the operator. */ | |
7179 | ||
21526606 | 7180 | static tree |
94edc4ab | 7181 | cp_parser_conversion_function_id (cp_parser* parser) |
a723baf1 MM |
7182 | { |
7183 | tree type; | |
7184 | tree saved_scope; | |
7185 | tree saved_qualifying_scope; | |
7186 | tree saved_object_scope; | |
91b004e5 | 7187 | bool pop_p = false; |
a723baf1 MM |
7188 | |
7189 | /* Look for the `operator' token. */ | |
7190 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7191 | return error_mark_node; | |
7192 | /* When we parse the conversion-type-id, the current scope will be | |
7193 | reset. However, we need that information in able to look up the | |
7194 | conversion function later, so we save it here. */ | |
7195 | saved_scope = parser->scope; | |
7196 | saved_qualifying_scope = parser->qualifying_scope; | |
7197 | saved_object_scope = parser->object_scope; | |
7198 | /* We must enter the scope of the class so that the names of | |
7199 | entities declared within the class are available in the | |
7200 | conversion-type-id. For example, consider: | |
7201 | ||
21526606 | 7202 | struct S { |
a723baf1 MM |
7203 | typedef int I; |
7204 | operator I(); | |
7205 | }; | |
7206 | ||
7207 | S::operator I() { ... } | |
7208 | ||
7209 | In order to see that `I' is a type-name in the definition, we | |
7210 | must be in the scope of `S'. */ | |
7211 | if (saved_scope) | |
91b004e5 | 7212 | pop_p = push_scope (saved_scope); |
a723baf1 MM |
7213 | /* Parse the conversion-type-id. */ |
7214 | type = cp_parser_conversion_type_id (parser); | |
7215 | /* Leave the scope of the class, if any. */ | |
91b004e5 | 7216 | if (pop_p) |
a723baf1 MM |
7217 | pop_scope (saved_scope); |
7218 | /* Restore the saved scope. */ | |
7219 | parser->scope = saved_scope; | |
7220 | parser->qualifying_scope = saved_qualifying_scope; | |
7221 | parser->object_scope = saved_object_scope; | |
7222 | /* If the TYPE is invalid, indicate failure. */ | |
7223 | if (type == error_mark_node) | |
7224 | return error_mark_node; | |
7225 | return mangle_conv_op_name_for_type (type); | |
7226 | } | |
7227 | ||
7228 | /* Parse a conversion-type-id: | |
7229 | ||
7230 | conversion-type-id: | |
7231 | type-specifier-seq conversion-declarator [opt] | |
7232 | ||
7233 | Returns the TYPE specified. */ | |
7234 | ||
7235 | static tree | |
94edc4ab | 7236 | cp_parser_conversion_type_id (cp_parser* parser) |
a723baf1 MM |
7237 | { |
7238 | tree attributes; | |
7239 | tree type_specifiers; | |
7240 | tree declarator; | |
7241 | ||
7242 | /* Parse the attributes. */ | |
7243 | attributes = cp_parser_attributes_opt (parser); | |
7244 | /* Parse the type-specifiers. */ | |
7245 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
7246 | /* If that didn't work, stop. */ | |
7247 | if (type_specifiers == error_mark_node) | |
7248 | return error_mark_node; | |
7249 | /* Parse the conversion-declarator. */ | |
7250 | declarator = cp_parser_conversion_declarator_opt (parser); | |
7251 | ||
7252 | return grokdeclarator (declarator, type_specifiers, TYPENAME, | |
7253 | /*initialized=*/0, &attributes); | |
7254 | } | |
7255 | ||
7256 | /* Parse an (optional) conversion-declarator. | |
7257 | ||
7258 | conversion-declarator: | |
21526606 | 7259 | ptr-operator conversion-declarator [opt] |
a723baf1 MM |
7260 | |
7261 | Returns a representation of the declarator. See | |
7262 | cp_parser_declarator for details. */ | |
7263 | ||
7264 | static tree | |
94edc4ab | 7265 | cp_parser_conversion_declarator_opt (cp_parser* parser) |
a723baf1 MM |
7266 | { |
7267 | enum tree_code code; | |
7268 | tree class_type; | |
7269 | tree cv_qualifier_seq; | |
7270 | ||
7271 | /* We don't know if there's a ptr-operator next, or not. */ | |
7272 | cp_parser_parse_tentatively (parser); | |
7273 | /* Try the ptr-operator. */ | |
21526606 | 7274 | code = cp_parser_ptr_operator (parser, &class_type, |
a723baf1 MM |
7275 | &cv_qualifier_seq); |
7276 | /* If it worked, look for more conversion-declarators. */ | |
7277 | if (cp_parser_parse_definitely (parser)) | |
7278 | { | |
7279 | tree declarator; | |
7280 | ||
7281 | /* Parse another optional declarator. */ | |
7282 | declarator = cp_parser_conversion_declarator_opt (parser); | |
7283 | ||
7284 | /* Create the representation of the declarator. */ | |
7285 | if (code == INDIRECT_REF) | |
7286 | declarator = make_pointer_declarator (cv_qualifier_seq, | |
7287 | declarator); | |
7288 | else | |
7289 | declarator = make_reference_declarator (cv_qualifier_seq, | |
7290 | declarator); | |
7291 | ||
7292 | /* Handle the pointer-to-member case. */ | |
7293 | if (class_type) | |
7294 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
7295 | ||
7296 | return declarator; | |
7297 | } | |
7298 | ||
7299 | return NULL_TREE; | |
7300 | } | |
7301 | ||
7302 | /* Parse an (optional) ctor-initializer. | |
7303 | ||
7304 | ctor-initializer: | |
21526606 | 7305 | : mem-initializer-list |
a723baf1 MM |
7306 | |
7307 | Returns TRUE iff the ctor-initializer was actually present. */ | |
7308 | ||
7309 | static bool | |
94edc4ab | 7310 | cp_parser_ctor_initializer_opt (cp_parser* parser) |
a723baf1 MM |
7311 | { |
7312 | /* If the next token is not a `:', then there is no | |
7313 | ctor-initializer. */ | |
7314 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) | |
7315 | { | |
7316 | /* Do default initialization of any bases and members. */ | |
7317 | if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
7318 | finish_mem_initializers (NULL_TREE); | |
7319 | ||
7320 | return false; | |
7321 | } | |
7322 | ||
7323 | /* Consume the `:' token. */ | |
7324 | cp_lexer_consume_token (parser->lexer); | |
7325 | /* And the mem-initializer-list. */ | |
7326 | cp_parser_mem_initializer_list (parser); | |
7327 | ||
7328 | return true; | |
7329 | } | |
7330 | ||
7331 | /* Parse a mem-initializer-list. | |
7332 | ||
7333 | mem-initializer-list: | |
7334 | mem-initializer | |
7335 | mem-initializer , mem-initializer-list */ | |
7336 | ||
7337 | static void | |
94edc4ab | 7338 | cp_parser_mem_initializer_list (cp_parser* parser) |
a723baf1 MM |
7339 | { |
7340 | tree mem_initializer_list = NULL_TREE; | |
7341 | ||
7342 | /* Let the semantic analysis code know that we are starting the | |
7343 | mem-initializer-list. */ | |
0e136342 MM |
7344 | if (!DECL_CONSTRUCTOR_P (current_function_decl)) |
7345 | error ("only constructors take base initializers"); | |
a723baf1 MM |
7346 | |
7347 | /* Loop through the list. */ | |
7348 | while (true) | |
7349 | { | |
7350 | tree mem_initializer; | |
7351 | ||
7352 | /* Parse the mem-initializer. */ | |
7353 | mem_initializer = cp_parser_mem_initializer (parser); | |
7354 | /* Add it to the list, unless it was erroneous. */ | |
7355 | if (mem_initializer) | |
7356 | { | |
7357 | TREE_CHAIN (mem_initializer) = mem_initializer_list; | |
7358 | mem_initializer_list = mem_initializer; | |
7359 | } | |
7360 | /* If the next token is not a `,', we're done. */ | |
7361 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7362 | break; | |
7363 | /* Consume the `,' token. */ | |
7364 | cp_lexer_consume_token (parser->lexer); | |
7365 | } | |
7366 | ||
7367 | /* Perform semantic analysis. */ | |
0e136342 MM |
7368 | if (DECL_CONSTRUCTOR_P (current_function_decl)) |
7369 | finish_mem_initializers (mem_initializer_list); | |
a723baf1 MM |
7370 | } |
7371 | ||
7372 | /* Parse a mem-initializer. | |
7373 | ||
7374 | mem-initializer: | |
21526606 | 7375 | mem-initializer-id ( expression-list [opt] ) |
a723baf1 MM |
7376 | |
7377 | GNU extension: | |
21526606 | 7378 | |
a723baf1 | 7379 | mem-initializer: |
34cd5ae7 | 7380 | ( expression-list [opt] ) |
a723baf1 MM |
7381 | |
7382 | Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base | |
7383 | class) or FIELD_DECL (for a non-static data member) to initialize; | |
7384 | the TREE_VALUE is the expression-list. */ | |
7385 | ||
7386 | static tree | |
94edc4ab | 7387 | cp_parser_mem_initializer (cp_parser* parser) |
a723baf1 MM |
7388 | { |
7389 | tree mem_initializer_id; | |
7390 | tree expression_list; | |
1f5a253a | 7391 | tree member; |
21526606 | 7392 | |
a723baf1 MM |
7393 | /* Find out what is being initialized. */ |
7394 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
7395 | { | |
7396 | pedwarn ("anachronistic old-style base class initializer"); | |
7397 | mem_initializer_id = NULL_TREE; | |
7398 | } | |
7399 | else | |
7400 | mem_initializer_id = cp_parser_mem_initializer_id (parser); | |
1f5a253a NS |
7401 | member = expand_member_init (mem_initializer_id); |
7402 | if (member && !DECL_P (member)) | |
7403 | in_base_initializer = 1; | |
7efa3e22 | 7404 | |
21526606 | 7405 | expression_list |
39703eb9 MM |
7406 | = cp_parser_parenthesized_expression_list (parser, false, |
7407 | /*non_constant_p=*/NULL); | |
7efa3e22 | 7408 | if (!expression_list) |
a723baf1 | 7409 | expression_list = void_type_node; |
a723baf1 | 7410 | |
1f5a253a | 7411 | in_base_initializer = 0; |
21526606 | 7412 | |
1f5a253a | 7413 | return member ? build_tree_list (member, expression_list) : NULL_TREE; |
a723baf1 MM |
7414 | } |
7415 | ||
7416 | /* Parse a mem-initializer-id. | |
7417 | ||
7418 | mem-initializer-id: | |
7419 | :: [opt] nested-name-specifier [opt] class-name | |
21526606 | 7420 | identifier |
a723baf1 MM |
7421 | |
7422 | Returns a TYPE indicating the class to be initializer for the first | |
7423 | production. Returns an IDENTIFIER_NODE indicating the data member | |
7424 | to be initialized for the second production. */ | |
7425 | ||
7426 | static tree | |
94edc4ab | 7427 | cp_parser_mem_initializer_id (cp_parser* parser) |
a723baf1 MM |
7428 | { |
7429 | bool global_scope_p; | |
7430 | bool nested_name_specifier_p; | |
8a83a693 | 7431 | bool template_p = false; |
a723baf1 MM |
7432 | tree id; |
7433 | ||
8a83a693 GB |
7434 | /* `typename' is not allowed in this context ([temp.res]). */ |
7435 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
7436 | { | |
7437 | error ("keyword `typename' not allowed in this context (a qualified " | |
7438 | "member initializer is implicitly a type)"); | |
7439 | cp_lexer_consume_token (parser->lexer); | |
7440 | } | |
a723baf1 | 7441 | /* Look for the optional `::' operator. */ |
21526606 EC |
7442 | global_scope_p |
7443 | = (cp_parser_global_scope_opt (parser, | |
7444 | /*current_scope_valid_p=*/false) | |
a723baf1 MM |
7445 | != NULL_TREE); |
7446 | /* Look for the optional nested-name-specifier. The simplest way to | |
7447 | implement: | |
7448 | ||
7449 | [temp.res] | |
7450 | ||
7451 | The keyword `typename' is not permitted in a base-specifier or | |
7452 | mem-initializer; in these contexts a qualified name that | |
7453 | depends on a template-parameter is implicitly assumed to be a | |
7454 | type name. | |
7455 | ||
7456 | is to assume that we have seen the `typename' keyword at this | |
7457 | point. */ | |
21526606 | 7458 | nested_name_specifier_p |
a723baf1 MM |
7459 | = (cp_parser_nested_name_specifier_opt (parser, |
7460 | /*typename_keyword_p=*/true, | |
7461 | /*check_dependency_p=*/true, | |
a668c6ad MM |
7462 | /*type_p=*/true, |
7463 | /*is_declaration=*/true) | |
a723baf1 | 7464 | != NULL_TREE); |
8a83a693 GB |
7465 | if (nested_name_specifier_p) |
7466 | template_p = cp_parser_optional_template_keyword (parser); | |
a723baf1 MM |
7467 | /* If there is a `::' operator or a nested-name-specifier, then we |
7468 | are definitely looking for a class-name. */ | |
7469 | if (global_scope_p || nested_name_specifier_p) | |
7470 | return cp_parser_class_name (parser, | |
7471 | /*typename_keyword_p=*/true, | |
8a83a693 | 7472 | /*template_keyword_p=*/template_p, |
a723baf1 | 7473 | /*type_p=*/false, |
a723baf1 | 7474 | /*check_dependency_p=*/true, |
a668c6ad MM |
7475 | /*class_head_p=*/false, |
7476 | /*is_declaration=*/true); | |
a723baf1 MM |
7477 | /* Otherwise, we could also be looking for an ordinary identifier. */ |
7478 | cp_parser_parse_tentatively (parser); | |
7479 | /* Try a class-name. */ | |
21526606 | 7480 | id = cp_parser_class_name (parser, |
a723baf1 MM |
7481 | /*typename_keyword_p=*/true, |
7482 | /*template_keyword_p=*/false, | |
7483 | /*type_p=*/false, | |
a723baf1 | 7484 | /*check_dependency_p=*/true, |
a668c6ad MM |
7485 | /*class_head_p=*/false, |
7486 | /*is_declaration=*/true); | |
a723baf1 MM |
7487 | /* If we found one, we're done. */ |
7488 | if (cp_parser_parse_definitely (parser)) | |
7489 | return id; | |
7490 | /* Otherwise, look for an ordinary identifier. */ | |
7491 | return cp_parser_identifier (parser); | |
7492 | } | |
7493 | ||
7494 | /* Overloading [gram.over] */ | |
7495 | ||
7496 | /* Parse an operator-function-id. | |
7497 | ||
7498 | operator-function-id: | |
21526606 | 7499 | operator operator |
a723baf1 MM |
7500 | |
7501 | Returns an IDENTIFIER_NODE for the operator which is a | |
7502 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
7503 | ||
21526606 | 7504 | static tree |
94edc4ab | 7505 | cp_parser_operator_function_id (cp_parser* parser) |
a723baf1 MM |
7506 | { |
7507 | /* Look for the `operator' keyword. */ | |
7508 | if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'")) | |
7509 | return error_mark_node; | |
7510 | /* And then the name of the operator itself. */ | |
7511 | return cp_parser_operator (parser); | |
7512 | } | |
7513 | ||
7514 | /* Parse an operator. | |
7515 | ||
7516 | operator: | |
7517 | new delete new[] delete[] + - * / % ^ & | ~ ! = < > | |
7518 | += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && | |
7519 | || ++ -- , ->* -> () [] | |
7520 | ||
7521 | GNU Extensions: | |
21526606 | 7522 | |
a723baf1 MM |
7523 | operator: |
7524 | <? >? <?= >?= | |
7525 | ||
7526 | Returns an IDENTIFIER_NODE for the operator which is a | |
7527 | human-readable spelling of the identifier, e.g., `operator +'. */ | |
21526606 | 7528 | |
a723baf1 | 7529 | static tree |
94edc4ab | 7530 | cp_parser_operator (cp_parser* parser) |
a723baf1 MM |
7531 | { |
7532 | tree id = NULL_TREE; | |
7533 | cp_token *token; | |
7534 | ||
7535 | /* Peek at the next token. */ | |
7536 | token = cp_lexer_peek_token (parser->lexer); | |
7537 | /* Figure out which operator we have. */ | |
7538 | switch (token->type) | |
7539 | { | |
7540 | case CPP_KEYWORD: | |
7541 | { | |
7542 | enum tree_code op; | |
7543 | ||
7544 | /* The keyword should be either `new' or `delete'. */ | |
7545 | if (token->keyword == RID_NEW) | |
7546 | op = NEW_EXPR; | |
7547 | else if (token->keyword == RID_DELETE) | |
7548 | op = DELETE_EXPR; | |
7549 | else | |
7550 | break; | |
7551 | ||
7552 | /* Consume the `new' or `delete' token. */ | |
7553 | cp_lexer_consume_token (parser->lexer); | |
7554 | ||
7555 | /* Peek at the next token. */ | |
7556 | token = cp_lexer_peek_token (parser->lexer); | |
7557 | /* If it's a `[' token then this is the array variant of the | |
7558 | operator. */ | |
7559 | if (token->type == CPP_OPEN_SQUARE) | |
7560 | { | |
7561 | /* Consume the `[' token. */ | |
7562 | cp_lexer_consume_token (parser->lexer); | |
7563 | /* Look for the `]' token. */ | |
7564 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
21526606 | 7565 | id = ansi_opname (op == NEW_EXPR |
a723baf1 MM |
7566 | ? VEC_NEW_EXPR : VEC_DELETE_EXPR); |
7567 | } | |
7568 | /* Otherwise, we have the non-array variant. */ | |
7569 | else | |
7570 | id = ansi_opname (op); | |
7571 | ||
7572 | return id; | |
7573 | } | |
7574 | ||
7575 | case CPP_PLUS: | |
7576 | id = ansi_opname (PLUS_EXPR); | |
7577 | break; | |
7578 | ||
7579 | case CPP_MINUS: | |
7580 | id = ansi_opname (MINUS_EXPR); | |
7581 | break; | |
7582 | ||
7583 | case CPP_MULT: | |
7584 | id = ansi_opname (MULT_EXPR); | |
7585 | break; | |
7586 | ||
7587 | case CPP_DIV: | |
7588 | id = ansi_opname (TRUNC_DIV_EXPR); | |
7589 | break; | |
7590 | ||
7591 | case CPP_MOD: | |
7592 | id = ansi_opname (TRUNC_MOD_EXPR); | |
7593 | break; | |
7594 | ||
7595 | case CPP_XOR: | |
7596 | id = ansi_opname (BIT_XOR_EXPR); | |
7597 | break; | |
7598 | ||
7599 | case CPP_AND: | |
7600 | id = ansi_opname (BIT_AND_EXPR); | |
7601 | break; | |
7602 | ||
7603 | case CPP_OR: | |
7604 | id = ansi_opname (BIT_IOR_EXPR); | |
7605 | break; | |
7606 | ||
7607 | case CPP_COMPL: | |
7608 | id = ansi_opname (BIT_NOT_EXPR); | |
7609 | break; | |
21526606 | 7610 | |
a723baf1 MM |
7611 | case CPP_NOT: |
7612 | id = ansi_opname (TRUTH_NOT_EXPR); | |
7613 | break; | |
7614 | ||
7615 | case CPP_EQ: | |
7616 | id = ansi_assopname (NOP_EXPR); | |
7617 | break; | |
7618 | ||
7619 | case CPP_LESS: | |
7620 | id = ansi_opname (LT_EXPR); | |
7621 | break; | |
7622 | ||
7623 | case CPP_GREATER: | |
7624 | id = ansi_opname (GT_EXPR); | |
7625 | break; | |
7626 | ||
7627 | case CPP_PLUS_EQ: | |
7628 | id = ansi_assopname (PLUS_EXPR); | |
7629 | break; | |
7630 | ||
7631 | case CPP_MINUS_EQ: | |
7632 | id = ansi_assopname (MINUS_EXPR); | |
7633 | break; | |
7634 | ||
7635 | case CPP_MULT_EQ: | |
7636 | id = ansi_assopname (MULT_EXPR); | |
7637 | break; | |
7638 | ||
7639 | case CPP_DIV_EQ: | |
7640 | id = ansi_assopname (TRUNC_DIV_EXPR); | |
7641 | break; | |
7642 | ||
7643 | case CPP_MOD_EQ: | |
7644 | id = ansi_assopname (TRUNC_MOD_EXPR); | |
7645 | break; | |
7646 | ||
7647 | case CPP_XOR_EQ: | |
7648 | id = ansi_assopname (BIT_XOR_EXPR); | |
7649 | break; | |
7650 | ||
7651 | case CPP_AND_EQ: | |
7652 | id = ansi_assopname (BIT_AND_EXPR); | |
7653 | break; | |
7654 | ||
7655 | case CPP_OR_EQ: | |
7656 | id = ansi_assopname (BIT_IOR_EXPR); | |
7657 | break; | |
7658 | ||
7659 | case CPP_LSHIFT: | |
7660 | id = ansi_opname (LSHIFT_EXPR); | |
7661 | break; | |
7662 | ||
7663 | case CPP_RSHIFT: | |
7664 | id = ansi_opname (RSHIFT_EXPR); | |
7665 | break; | |
7666 | ||
7667 | case CPP_LSHIFT_EQ: | |
7668 | id = ansi_assopname (LSHIFT_EXPR); | |
7669 | break; | |
7670 | ||
7671 | case CPP_RSHIFT_EQ: | |
7672 | id = ansi_assopname (RSHIFT_EXPR); | |
7673 | break; | |
7674 | ||
7675 | case CPP_EQ_EQ: | |
7676 | id = ansi_opname (EQ_EXPR); | |
7677 | break; | |
7678 | ||
7679 | case CPP_NOT_EQ: | |
7680 | id = ansi_opname (NE_EXPR); | |
7681 | break; | |
7682 | ||
7683 | case CPP_LESS_EQ: | |
7684 | id = ansi_opname (LE_EXPR); | |
7685 | break; | |
7686 | ||
7687 | case CPP_GREATER_EQ: | |
7688 | id = ansi_opname (GE_EXPR); | |
7689 | break; | |
7690 | ||
7691 | case CPP_AND_AND: | |
7692 | id = ansi_opname (TRUTH_ANDIF_EXPR); | |
7693 | break; | |
7694 | ||
7695 | case CPP_OR_OR: | |
7696 | id = ansi_opname (TRUTH_ORIF_EXPR); | |
7697 | break; | |
21526606 | 7698 | |
a723baf1 MM |
7699 | case CPP_PLUS_PLUS: |
7700 | id = ansi_opname (POSTINCREMENT_EXPR); | |
7701 | break; | |
7702 | ||
7703 | case CPP_MINUS_MINUS: | |
7704 | id = ansi_opname (PREDECREMENT_EXPR); | |
7705 | break; | |
7706 | ||
7707 | case CPP_COMMA: | |
7708 | id = ansi_opname (COMPOUND_EXPR); | |
7709 | break; | |
7710 | ||
7711 | case CPP_DEREF_STAR: | |
7712 | id = ansi_opname (MEMBER_REF); | |
7713 | break; | |
7714 | ||
7715 | case CPP_DEREF: | |
7716 | id = ansi_opname (COMPONENT_REF); | |
7717 | break; | |
7718 | ||
7719 | case CPP_OPEN_PAREN: | |
7720 | /* Consume the `('. */ | |
7721 | cp_lexer_consume_token (parser->lexer); | |
7722 | /* Look for the matching `)'. */ | |
7723 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
7724 | return ansi_opname (CALL_EXPR); | |
7725 | ||
7726 | case CPP_OPEN_SQUARE: | |
7727 | /* Consume the `['. */ | |
7728 | cp_lexer_consume_token (parser->lexer); | |
7729 | /* Look for the matching `]'. */ | |
7730 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
7731 | return ansi_opname (ARRAY_REF); | |
7732 | ||
7733 | /* Extensions. */ | |
7734 | case CPP_MIN: | |
7735 | id = ansi_opname (MIN_EXPR); | |
7736 | break; | |
7737 | ||
7738 | case CPP_MAX: | |
7739 | id = ansi_opname (MAX_EXPR); | |
7740 | break; | |
7741 | ||
7742 | case CPP_MIN_EQ: | |
7743 | id = ansi_assopname (MIN_EXPR); | |
7744 | break; | |
7745 | ||
7746 | case CPP_MAX_EQ: | |
7747 | id = ansi_assopname (MAX_EXPR); | |
7748 | break; | |
7749 | ||
7750 | default: | |
7751 | /* Anything else is an error. */ | |
7752 | break; | |
7753 | } | |
7754 | ||
7755 | /* If we have selected an identifier, we need to consume the | |
7756 | operator token. */ | |
7757 | if (id) | |
7758 | cp_lexer_consume_token (parser->lexer); | |
7759 | /* Otherwise, no valid operator name was present. */ | |
7760 | else | |
7761 | { | |
7762 | cp_parser_error (parser, "expected operator"); | |
7763 | id = error_mark_node; | |
7764 | } | |
7765 | ||
7766 | return id; | |
7767 | } | |
7768 | ||
7769 | /* Parse a template-declaration. | |
7770 | ||
7771 | template-declaration: | |
21526606 | 7772 | export [opt] template < template-parameter-list > declaration |
a723baf1 MM |
7773 | |
7774 | If MEMBER_P is TRUE, this template-declaration occurs within a | |
21526606 | 7775 | class-specifier. |
a723baf1 MM |
7776 | |
7777 | The grammar rule given by the standard isn't correct. What | |
7778 | is really meant is: | |
7779 | ||
7780 | template-declaration: | |
21526606 | 7781 | export [opt] template-parameter-list-seq |
a723baf1 | 7782 | decl-specifier-seq [opt] init-declarator [opt] ; |
21526606 | 7783 | export [opt] template-parameter-list-seq |
a723baf1 MM |
7784 | function-definition |
7785 | ||
7786 | template-parameter-list-seq: | |
7787 | template-parameter-list-seq [opt] | |
7788 | template < template-parameter-list > */ | |
7789 | ||
7790 | static void | |
94edc4ab | 7791 | cp_parser_template_declaration (cp_parser* parser, bool member_p) |
a723baf1 MM |
7792 | { |
7793 | /* Check for `export'. */ | |
7794 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) | |
7795 | { | |
7796 | /* Consume the `export' token. */ | |
7797 | cp_lexer_consume_token (parser->lexer); | |
7798 | /* Warn that we do not support `export'. */ | |
7799 | warning ("keyword `export' not implemented, and will be ignored"); | |
7800 | } | |
7801 | ||
7802 | cp_parser_template_declaration_after_export (parser, member_p); | |
7803 | } | |
7804 | ||
7805 | /* Parse a template-parameter-list. | |
7806 | ||
7807 | template-parameter-list: | |
7808 | template-parameter | |
7809 | template-parameter-list , template-parameter | |
7810 | ||
7811 | Returns a TREE_LIST. Each node represents a template parameter. | |
7812 | The nodes are connected via their TREE_CHAINs. */ | |
7813 | ||
7814 | static tree | |
94edc4ab | 7815 | cp_parser_template_parameter_list (cp_parser* parser) |
a723baf1 MM |
7816 | { |
7817 | tree parameter_list = NULL_TREE; | |
7818 | ||
7819 | while (true) | |
7820 | { | |
7821 | tree parameter; | |
7822 | cp_token *token; | |
7823 | ||
7824 | /* Parse the template-parameter. */ | |
7825 | parameter = cp_parser_template_parameter (parser); | |
7826 | /* Add it to the list. */ | |
7827 | parameter_list = process_template_parm (parameter_list, | |
7828 | parameter); | |
7829 | ||
7830 | /* Peek at the next token. */ | |
7831 | token = cp_lexer_peek_token (parser->lexer); | |
7832 | /* If it's not a `,', we're done. */ | |
7833 | if (token->type != CPP_COMMA) | |
7834 | break; | |
7835 | /* Otherwise, consume the `,' token. */ | |
7836 | cp_lexer_consume_token (parser->lexer); | |
7837 | } | |
7838 | ||
7839 | return parameter_list; | |
7840 | } | |
7841 | ||
7842 | /* Parse a template-parameter. | |
7843 | ||
7844 | template-parameter: | |
7845 | type-parameter | |
7846 | parameter-declaration | |
7847 | ||
7848 | Returns a TREE_LIST. The TREE_VALUE represents the parameter. The | |
7849 | TREE_PURPOSE is the default value, if any. */ | |
7850 | ||
7851 | static tree | |
94edc4ab | 7852 | cp_parser_template_parameter (cp_parser* parser) |
a723baf1 MM |
7853 | { |
7854 | cp_token *token; | |
7855 | ||
7856 | /* Peek at the next token. */ | |
7857 | token = cp_lexer_peek_token (parser->lexer); | |
7858 | /* If it is `class' or `template', we have a type-parameter. */ | |
7859 | if (token->keyword == RID_TEMPLATE) | |
7860 | return cp_parser_type_parameter (parser); | |
7861 | /* If it is `class' or `typename' we do not know yet whether it is a | |
7862 | type parameter or a non-type parameter. Consider: | |
7863 | ||
7864 | template <typename T, typename T::X X> ... | |
7865 | ||
7866 | or: | |
21526606 | 7867 | |
a723baf1 MM |
7868 | template <class C, class D*> ... |
7869 | ||
7870 | Here, the first parameter is a type parameter, and the second is | |
7871 | a non-type parameter. We can tell by looking at the token after | |
7872 | the identifier -- if it is a `,', `=', or `>' then we have a type | |
7873 | parameter. */ | |
7874 | if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) | |
7875 | { | |
7876 | /* Peek at the token after `class' or `typename'. */ | |
7877 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
7878 | /* If it's an identifier, skip it. */ | |
7879 | if (token->type == CPP_NAME) | |
7880 | token = cp_lexer_peek_nth_token (parser->lexer, 3); | |
7881 | /* Now, see if the token looks like the end of a template | |
7882 | parameter. */ | |
21526606 | 7883 | if (token->type == CPP_COMMA |
a723baf1 MM |
7884 | || token->type == CPP_EQ |
7885 | || token->type == CPP_GREATER) | |
7886 | return cp_parser_type_parameter (parser); | |
7887 | } | |
7888 | ||
21526606 | 7889 | /* Otherwise, it is a non-type parameter. |
a723baf1 MM |
7890 | |
7891 | [temp.param] | |
7892 | ||
7893 | When parsing a default template-argument for a non-type | |
7894 | template-parameter, the first non-nested `>' is taken as the end | |
7895 | of the template parameter-list rather than a greater-than | |
7896 | operator. */ | |
21526606 | 7897 | return |
4bb8ca28 MM |
7898 | cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, |
7899 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
7900 | } |
7901 | ||
7902 | /* Parse a type-parameter. | |
7903 | ||
7904 | type-parameter: | |
7905 | class identifier [opt] | |
7906 | class identifier [opt] = type-id | |
7907 | typename identifier [opt] | |
7908 | typename identifier [opt] = type-id | |
7909 | template < template-parameter-list > class identifier [opt] | |
21526606 EC |
7910 | template < template-parameter-list > class identifier [opt] |
7911 | = id-expression | |
a723baf1 MM |
7912 | |
7913 | Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The | |
7914 | TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is | |
7915 | the declaration of the parameter. */ | |
7916 | ||
7917 | static tree | |
94edc4ab | 7918 | cp_parser_type_parameter (cp_parser* parser) |
a723baf1 MM |
7919 | { |
7920 | cp_token *token; | |
7921 | tree parameter; | |
7922 | ||
7923 | /* Look for a keyword to tell us what kind of parameter this is. */ | |
21526606 | 7924 | token = cp_parser_require (parser, CPP_KEYWORD, |
8a6393df | 7925 | "`class', `typename', or `template'"); |
a723baf1 MM |
7926 | if (!token) |
7927 | return error_mark_node; | |
7928 | ||
7929 | switch (token->keyword) | |
7930 | { | |
7931 | case RID_CLASS: | |
7932 | case RID_TYPENAME: | |
7933 | { | |
7934 | tree identifier; | |
7935 | tree default_argument; | |
7936 | ||
7937 | /* If the next token is an identifier, then it names the | |
7938 | parameter. */ | |
7939 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
7940 | identifier = cp_parser_identifier (parser); | |
7941 | else | |
7942 | identifier = NULL_TREE; | |
7943 | ||
7944 | /* Create the parameter. */ | |
7945 | parameter = finish_template_type_parm (class_type_node, identifier); | |
7946 | ||
7947 | /* If the next token is an `=', we have a default argument. */ | |
7948 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7949 | { | |
7950 | /* Consume the `=' token. */ | |
7951 | cp_lexer_consume_token (parser->lexer); | |
34cd5ae7 | 7952 | /* Parse the default-argument. */ |
a723baf1 MM |
7953 | default_argument = cp_parser_type_id (parser); |
7954 | } | |
7955 | else | |
7956 | default_argument = NULL_TREE; | |
7957 | ||
7958 | /* Create the combined representation of the parameter and the | |
7959 | default argument. */ | |
c67d36d0 | 7960 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
7961 | } |
7962 | break; | |
7963 | ||
7964 | case RID_TEMPLATE: | |
7965 | { | |
7966 | tree parameter_list; | |
7967 | tree identifier; | |
7968 | tree default_argument; | |
7969 | ||
7970 | /* Look for the `<'. */ | |
7971 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
7972 | /* Parse the template-parameter-list. */ | |
7973 | begin_template_parm_list (); | |
21526606 | 7974 | parameter_list |
a723baf1 MM |
7975 | = cp_parser_template_parameter_list (parser); |
7976 | parameter_list = end_template_parm_list (parameter_list); | |
7977 | /* Look for the `>'. */ | |
7978 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
7979 | /* Look for the `class' keyword. */ | |
7980 | cp_parser_require_keyword (parser, RID_CLASS, "`class'"); | |
7981 | /* If the next token is an `=', then there is a | |
7982 | default-argument. If the next token is a `>', we are at | |
7983 | the end of the parameter-list. If the next token is a `,', | |
7984 | then we are at the end of this parameter. */ | |
7985 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) | |
7986 | && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) | |
7987 | && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
7988 | identifier = cp_parser_identifier (parser); | |
7989 | else | |
7990 | identifier = NULL_TREE; | |
7991 | /* Create the template parameter. */ | |
7992 | parameter = finish_template_template_parm (class_type_node, | |
7993 | identifier); | |
21526606 | 7994 | |
a723baf1 MM |
7995 | /* If the next token is an `=', then there is a |
7996 | default-argument. */ | |
7997 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
7998 | { | |
b0bc6e8e KL |
7999 | bool is_template; |
8000 | ||
a723baf1 MM |
8001 | /* Consume the `='. */ |
8002 | cp_lexer_consume_token (parser->lexer); | |
8003 | /* Parse the id-expression. */ | |
21526606 | 8004 | default_argument |
a723baf1 MM |
8005 | = cp_parser_id_expression (parser, |
8006 | /*template_keyword_p=*/false, | |
8007 | /*check_dependency_p=*/true, | |
b0bc6e8e | 8008 | /*template_p=*/&is_template, |
f3c2dfc6 | 8009 | /*declarator_p=*/false); |
a3a503a5 GB |
8010 | if (TREE_CODE (default_argument) == TYPE_DECL) |
8011 | /* If the id-expression was a template-id that refers to | |
8012 | a template-class, we already have the declaration here, | |
8013 | so no further lookup is needed. */ | |
8014 | ; | |
8015 | else | |
8016 | /* Look up the name. */ | |
21526606 | 8017 | default_argument |
a3a503a5 GB |
8018 | = cp_parser_lookup_name (parser, default_argument, |
8019 | /*is_type=*/false, | |
8020 | /*is_template=*/is_template, | |
8021 | /*is_namespace=*/false, | |
8022 | /*check_dependency=*/true); | |
a723baf1 MM |
8023 | /* See if the default argument is valid. */ |
8024 | default_argument | |
8025 | = check_template_template_default_arg (default_argument); | |
8026 | } | |
8027 | else | |
8028 | default_argument = NULL_TREE; | |
8029 | ||
8030 | /* Create the combined representation of the parameter and the | |
8031 | default argument. */ | |
c67d36d0 | 8032 | parameter = build_tree_list (default_argument, parameter); |
a723baf1 MM |
8033 | } |
8034 | break; | |
8035 | ||
8036 | default: | |
8037 | /* Anything else is an error. */ | |
8038 | cp_parser_error (parser, | |
8039 | "expected `class', `typename', or `template'"); | |
8040 | parameter = error_mark_node; | |
8041 | } | |
21526606 | 8042 | |
a723baf1 MM |
8043 | return parameter; |
8044 | } | |
8045 | ||
8046 | /* Parse a template-id. | |
8047 | ||
8048 | template-id: | |
8049 | template-name < template-argument-list [opt] > | |
8050 | ||
8051 | If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the | |
8052 | `template' keyword. In this case, a TEMPLATE_ID_EXPR will be | |
8053 | returned. Otherwise, if the template-name names a function, or set | |
8054 | of functions, returns a TEMPLATE_ID_EXPR. If the template-name | |
21526606 | 8055 | names a class, returns a TYPE_DECL for the specialization. |
a723baf1 MM |
8056 | |
8057 | If CHECK_DEPENDENCY_P is FALSE, names are looked up in | |
8058 | uninstantiated templates. */ | |
8059 | ||
8060 | static tree | |
21526606 EC |
8061 | cp_parser_template_id (cp_parser *parser, |
8062 | bool template_keyword_p, | |
a668c6ad MM |
8063 | bool check_dependency_p, |
8064 | bool is_declaration) | |
a723baf1 MM |
8065 | { |
8066 | tree template; | |
8067 | tree arguments; | |
a723baf1 | 8068 | tree template_id; |
a723baf1 MM |
8069 | ptrdiff_t start_of_id; |
8070 | tree access_check = NULL_TREE; | |
f4abade9 | 8071 | cp_token *next_token, *next_token_2; |
a668c6ad | 8072 | bool is_identifier; |
a723baf1 MM |
8073 | |
8074 | /* If the next token corresponds to a template-id, there is no need | |
8075 | to reparse it. */ | |
2050a1bb MM |
8076 | next_token = cp_lexer_peek_token (parser->lexer); |
8077 | if (next_token->type == CPP_TEMPLATE_ID) | |
a723baf1 MM |
8078 | { |
8079 | tree value; | |
8080 | tree check; | |
8081 | ||
8082 | /* Get the stored value. */ | |
8083 | value = cp_lexer_consume_token (parser->lexer)->value; | |
8084 | /* Perform any access checks that were deferred. */ | |
8085 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c KL |
8086 | perform_or_defer_access_check (TREE_PURPOSE (check), |
8087 | TREE_VALUE (check)); | |
a723baf1 MM |
8088 | /* Return the stored value. */ |
8089 | return TREE_VALUE (value); | |
8090 | } | |
8091 | ||
2050a1bb MM |
8092 | /* Avoid performing name lookup if there is no possibility of |
8093 | finding a template-id. */ | |
8094 | if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) | |
8095 | || (next_token->type == CPP_NAME | |
21526606 | 8096 | && !cp_parser_nth_token_starts_template_argument_list_p |
f4abade9 | 8097 | (parser, 2))) |
2050a1bb MM |
8098 | { |
8099 | cp_parser_error (parser, "expected template-id"); | |
8100 | return error_mark_node; | |
8101 | } | |
8102 | ||
a723baf1 MM |
8103 | /* Remember where the template-id starts. */ |
8104 | if (cp_parser_parsing_tentatively (parser) | |
8105 | && !cp_parser_committed_to_tentative_parse (parser)) | |
8106 | { | |
2050a1bb | 8107 | next_token = cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
8108 | start_of_id = cp_lexer_token_difference (parser->lexer, |
8109 | parser->lexer->first_token, | |
8110 | next_token); | |
a723baf1 MM |
8111 | } |
8112 | else | |
8113 | start_of_id = -1; | |
8114 | ||
8d241e0b | 8115 | push_deferring_access_checks (dk_deferred); |
cf22909c | 8116 | |
a723baf1 | 8117 | /* Parse the template-name. */ |
a668c6ad | 8118 | is_identifier = false; |
a723baf1 | 8119 | template = cp_parser_template_name (parser, template_keyword_p, |
a668c6ad MM |
8120 | check_dependency_p, |
8121 | is_declaration, | |
8122 | &is_identifier); | |
8123 | if (template == error_mark_node || is_identifier) | |
cf22909c KL |
8124 | { |
8125 | pop_deferring_access_checks (); | |
a668c6ad | 8126 | return template; |
cf22909c | 8127 | } |
a723baf1 | 8128 | |
21526606 | 8129 | /* If we find the sequence `[:' after a template-name, it's probably |
f4abade9 GB |
8130 | a digraph-typo for `< ::'. Substitute the tokens and check if we can |
8131 | parse correctly the argument list. */ | |
8132 | next_token = cp_lexer_peek_nth_token (parser->lexer, 1); | |
8133 | next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); | |
21526606 | 8134 | if (next_token->type == CPP_OPEN_SQUARE |
f4abade9 | 8135 | && next_token->flags & DIGRAPH |
21526606 | 8136 | && next_token_2->type == CPP_COLON |
f4abade9 | 8137 | && !(next_token_2->flags & PREV_WHITE)) |
cf22909c | 8138 | { |
f4abade9 GB |
8139 | cp_parser_parse_tentatively (parser); |
8140 | /* Change `:' into `::'. */ | |
8141 | next_token_2->type = CPP_SCOPE; | |
8142 | /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is | |
8143 | CPP_LESS. */ | |
8144 | cp_lexer_consume_token (parser->lexer); | |
8145 | /* Parse the arguments. */ | |
8146 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
8147 | if (!cp_parser_parse_definitely (parser)) | |
8148 | { | |
8149 | /* If we couldn't parse an argument list, then we revert our changes | |
8150 | and return simply an error. Maybe this is not a template-id | |
8151 | after all. */ | |
8152 | next_token_2->type = CPP_COLON; | |
8153 | cp_parser_error (parser, "expected `<'"); | |
8154 | pop_deferring_access_checks (); | |
8155 | return error_mark_node; | |
8156 | } | |
8157 | /* Otherwise, emit an error about the invalid digraph, but continue | |
8158 | parsing because we got our argument list. */ | |
8159 | pedwarn ("`<::' cannot begin a template-argument list"); | |
8160 | inform ("`<:' is an alternate spelling for `['. Insert whitespace " | |
8161 | "between `<' and `::'"); | |
8162 | if (!flag_permissive) | |
8163 | { | |
8164 | static bool hint; | |
8165 | if (!hint) | |
8166 | { | |
8167 | inform ("(if you use `-fpermissive' G++ will accept your code)"); | |
8168 | hint = true; | |
8169 | } | |
8170 | } | |
8171 | } | |
8172 | else | |
8173 | { | |
8174 | /* Look for the `<' that starts the template-argument-list. */ | |
8175 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
8176 | { | |
8177 | pop_deferring_access_checks (); | |
8178 | return error_mark_node; | |
8179 | } | |
8180 | /* Parse the arguments. */ | |
8181 | arguments = cp_parser_enclosed_template_argument_list (parser); | |
cf22909c | 8182 | } |
a723baf1 MM |
8183 | |
8184 | /* Build a representation of the specialization. */ | |
8185 | if (TREE_CODE (template) == IDENTIFIER_NODE) | |
8186 | template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments); | |
8187 | else if (DECL_CLASS_TEMPLATE_P (template) | |
8188 | || DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
21526606 EC |
8189 | template_id |
8190 | = finish_template_type (template, arguments, | |
8191 | cp_lexer_next_token_is (parser->lexer, | |
a723baf1 MM |
8192 | CPP_SCOPE)); |
8193 | else | |
8194 | { | |
8195 | /* If it's not a class-template or a template-template, it should be | |
8196 | a function-template. */ | |
8197 | my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template) | |
8198 | || TREE_CODE (template) == OVERLOAD | |
8199 | || BASELINK_P (template)), | |
8200 | 20010716); | |
21526606 | 8201 | |
a723baf1 MM |
8202 | template_id = lookup_template_function (template, arguments); |
8203 | } | |
21526606 | 8204 | |
cf22909c KL |
8205 | /* Retrieve any deferred checks. Do not pop this access checks yet |
8206 | so the memory will not be reclaimed during token replacing below. */ | |
8207 | access_check = get_deferred_access_checks (); | |
8208 | ||
a723baf1 MM |
8209 | /* If parsing tentatively, replace the sequence of tokens that makes |
8210 | up the template-id with a CPP_TEMPLATE_ID token. That way, | |
8211 | should we re-parse the token stream, we will not have to repeat | |
8212 | the effort required to do the parse, nor will we issue duplicate | |
8213 | error messages about problems during instantiation of the | |
8214 | template. */ | |
8215 | if (start_of_id >= 0) | |
8216 | { | |
8217 | cp_token *token; | |
a723baf1 MM |
8218 | |
8219 | /* Find the token that corresponds to the start of the | |
8220 | template-id. */ | |
21526606 | 8221 | token = cp_lexer_advance_token (parser->lexer, |
a723baf1 MM |
8222 | parser->lexer->first_token, |
8223 | start_of_id); | |
8224 | ||
a723baf1 MM |
8225 | /* Reset the contents of the START_OF_ID token. */ |
8226 | token->type = CPP_TEMPLATE_ID; | |
8227 | token->value = build_tree_list (access_check, template_id); | |
8228 | token->keyword = RID_MAX; | |
8229 | /* Purge all subsequent tokens. */ | |
8230 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
8231 | } | |
8232 | ||
cf22909c | 8233 | pop_deferring_access_checks (); |
a723baf1 MM |
8234 | return template_id; |
8235 | } | |
8236 | ||
8237 | /* Parse a template-name. | |
8238 | ||
8239 | template-name: | |
8240 | identifier | |
21526606 | 8241 | |
a723baf1 MM |
8242 | The standard should actually say: |
8243 | ||
8244 | template-name: | |
8245 | identifier | |
8246 | operator-function-id | |
a723baf1 MM |
8247 | |
8248 | A defect report has been filed about this issue. | |
8249 | ||
0d956474 GB |
8250 | A conversion-function-id cannot be a template name because they cannot |
8251 | be part of a template-id. In fact, looking at this code: | |
8252 | ||
8253 | a.operator K<int>() | |
8254 | ||
8255 | the conversion-function-id is "operator K<int>", and K<int> is a type-id. | |
21526606 | 8256 | It is impossible to call a templated conversion-function-id with an |
0d956474 GB |
8257 | explicit argument list, since the only allowed template parameter is |
8258 | the type to which it is converting. | |
8259 | ||
a723baf1 MM |
8260 | If TEMPLATE_KEYWORD_P is true, then we have just seen the |
8261 | `template' keyword, in a construction like: | |
8262 | ||
8263 | T::template f<3>() | |
8264 | ||
8265 | In that case `f' is taken to be a template-name, even though there | |
8266 | is no way of knowing for sure. | |
8267 | ||
8268 | Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the | |
8269 | name refers to a set of overloaded functions, at least one of which | |
8270 | is a template, or an IDENTIFIER_NODE with the name of the template, | |
8271 | if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, | |
8272 | names are looked up inside uninstantiated templates. */ | |
8273 | ||
8274 | static tree | |
21526606 EC |
8275 | cp_parser_template_name (cp_parser* parser, |
8276 | bool template_keyword_p, | |
a668c6ad MM |
8277 | bool check_dependency_p, |
8278 | bool is_declaration, | |
8279 | bool *is_identifier) | |
a723baf1 MM |
8280 | { |
8281 | tree identifier; | |
8282 | tree decl; | |
8283 | tree fns; | |
8284 | ||
8285 | /* If the next token is `operator', then we have either an | |
8286 | operator-function-id or a conversion-function-id. */ | |
8287 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) | |
8288 | { | |
8289 | /* We don't know whether we're looking at an | |
8290 | operator-function-id or a conversion-function-id. */ | |
8291 | cp_parser_parse_tentatively (parser); | |
8292 | /* Try an operator-function-id. */ | |
8293 | identifier = cp_parser_operator_function_id (parser); | |
8294 | /* If that didn't work, try a conversion-function-id. */ | |
8295 | if (!cp_parser_parse_definitely (parser)) | |
0d956474 GB |
8296 | { |
8297 | cp_parser_error (parser, "expected template-name"); | |
8298 | return error_mark_node; | |
8299 | } | |
a723baf1 MM |
8300 | } |
8301 | /* Look for the identifier. */ | |
8302 | else | |
8303 | identifier = cp_parser_identifier (parser); | |
21526606 | 8304 | |
a723baf1 MM |
8305 | /* If we didn't find an identifier, we don't have a template-id. */ |
8306 | if (identifier == error_mark_node) | |
8307 | return error_mark_node; | |
8308 | ||
8309 | /* If the name immediately followed the `template' keyword, then it | |
8310 | is a template-name. However, if the next token is not `<', then | |
8311 | we do not treat it as a template-name, since it is not being used | |
8312 | as part of a template-id. This enables us to handle constructs | |
8313 | like: | |
8314 | ||
8315 | template <typename T> struct S { S(); }; | |
8316 | template <typename T> S<T>::S(); | |
8317 | ||
8318 | correctly. We would treat `S' as a template -- if it were `S<T>' | |
8319 | -- but we do not if there is no `<'. */ | |
a668c6ad MM |
8320 | |
8321 | if (processing_template_decl | |
f4abade9 | 8322 | && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) |
a668c6ad MM |
8323 | { |
8324 | /* In a declaration, in a dependent context, we pretend that the | |
8325 | "template" keyword was present in order to improve error | |
8326 | recovery. For example, given: | |
21526606 | 8327 | |
a668c6ad | 8328 | template <typename T> void f(T::X<int>); |
21526606 | 8329 | |
a668c6ad | 8330 | we want to treat "X<int>" as a template-id. */ |
21526606 EC |
8331 | if (is_declaration |
8332 | && !template_keyword_p | |
a668c6ad | 8333 | && parser->scope && TYPE_P (parser->scope) |
4e0f4df5 GB |
8334 | && dependent_type_p (parser->scope) |
8335 | /* Do not do this for dtors (or ctors), since they never | |
8336 | need the template keyword before their name. */ | |
8337 | && !constructor_name_p (identifier, parser->scope)) | |
a668c6ad MM |
8338 | { |
8339 | ptrdiff_t start; | |
8340 | cp_token* token; | |
8341 | /* Explain what went wrong. */ | |
8342 | error ("non-template `%D' used as template", identifier); | |
4e0f4df5 GB |
8343 | inform ("use `%T::template %D' to indicate that it is a template", |
8344 | parser->scope, identifier); | |
a668c6ad MM |
8345 | /* If parsing tentatively, find the location of the "<" |
8346 | token. */ | |
8347 | if (cp_parser_parsing_tentatively (parser) | |
8348 | && !cp_parser_committed_to_tentative_parse (parser)) | |
8349 | { | |
8350 | cp_parser_simulate_error (parser); | |
8351 | token = cp_lexer_peek_token (parser->lexer); | |
8352 | token = cp_lexer_prev_token (parser->lexer, token); | |
8353 | start = cp_lexer_token_difference (parser->lexer, | |
8354 | parser->lexer->first_token, | |
8355 | token); | |
8356 | } | |
8357 | else | |
8358 | start = -1; | |
8359 | /* Parse the template arguments so that we can issue error | |
8360 | messages about them. */ | |
8361 | cp_lexer_consume_token (parser->lexer); | |
8362 | cp_parser_enclosed_template_argument_list (parser); | |
8363 | /* Skip tokens until we find a good place from which to | |
8364 | continue parsing. */ | |
8365 | cp_parser_skip_to_closing_parenthesis (parser, | |
8366 | /*recovering=*/true, | |
8367 | /*or_comma=*/true, | |
8368 | /*consume_paren=*/false); | |
8369 | /* If parsing tentatively, permanently remove the | |
8370 | template argument list. That will prevent duplicate | |
8371 | error messages from being issued about the missing | |
8372 | "template" keyword. */ | |
8373 | if (start >= 0) | |
8374 | { | |
8375 | token = cp_lexer_advance_token (parser->lexer, | |
8376 | parser->lexer->first_token, | |
8377 | start); | |
8378 | cp_lexer_purge_tokens_after (parser->lexer, token); | |
8379 | } | |
8380 | if (is_identifier) | |
8381 | *is_identifier = true; | |
8382 | return identifier; | |
8383 | } | |
9d363a56 MM |
8384 | |
8385 | /* If the "template" keyword is present, then there is generally | |
8386 | no point in doing name-lookup, so we just return IDENTIFIER. | |
8387 | But, if the qualifying scope is non-dependent then we can | |
8388 | (and must) do name-lookup normally. */ | |
8389 | if (template_keyword_p | |
8390 | && (!parser->scope | |
8391 | || (TYPE_P (parser->scope) | |
8392 | && dependent_type_p (parser->scope)))) | |
a668c6ad MM |
8393 | return identifier; |
8394 | } | |
a723baf1 MM |
8395 | |
8396 | /* Look up the name. */ | |
8397 | decl = cp_parser_lookup_name (parser, identifier, | |
a723baf1 | 8398 | /*is_type=*/false, |
b0bc6e8e | 8399 | /*is_template=*/false, |
eea9800f | 8400 | /*is_namespace=*/false, |
a723baf1 MM |
8401 | check_dependency_p); |
8402 | decl = maybe_get_template_decl_from_type_decl (decl); | |
8403 | ||
8404 | /* If DECL is a template, then the name was a template-name. */ | |
8405 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8406 | ; | |
21526606 | 8407 | else |
a723baf1 MM |
8408 | { |
8409 | /* The standard does not explicitly indicate whether a name that | |
8410 | names a set of overloaded declarations, some of which are | |
8411 | templates, is a template-name. However, such a name should | |
8412 | be a template-name; otherwise, there is no way to form a | |
8413 | template-id for the overloaded templates. */ | |
8414 | fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; | |
8415 | if (TREE_CODE (fns) == OVERLOAD) | |
8416 | { | |
8417 | tree fn; | |
21526606 | 8418 | |
a723baf1 MM |
8419 | for (fn = fns; fn; fn = OVL_NEXT (fn)) |
8420 | if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) | |
8421 | break; | |
8422 | } | |
8423 | else | |
8424 | { | |
8425 | /* Otherwise, the name does not name a template. */ | |
8426 | cp_parser_error (parser, "expected template-name"); | |
8427 | return error_mark_node; | |
8428 | } | |
8429 | } | |
8430 | ||
8431 | /* If DECL is dependent, and refers to a function, then just return | |
8432 | its name; we will look it up again during template instantiation. */ | |
8433 | if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) | |
8434 | { | |
8435 | tree scope = CP_DECL_CONTEXT (get_first_fn (decl)); | |
1fb3244a | 8436 | if (TYPE_P (scope) && dependent_type_p (scope)) |
a723baf1 MM |
8437 | return identifier; |
8438 | } | |
8439 | ||
8440 | return decl; | |
8441 | } | |
8442 | ||
8443 | /* Parse a template-argument-list. | |
8444 | ||
8445 | template-argument-list: | |
8446 | template-argument | |
8447 | template-argument-list , template-argument | |
8448 | ||
04c06002 | 8449 | Returns a TREE_VEC containing the arguments. */ |
a723baf1 MM |
8450 | |
8451 | static tree | |
94edc4ab | 8452 | cp_parser_template_argument_list (cp_parser* parser) |
a723baf1 | 8453 | { |
bf12d54d NS |
8454 | tree fixed_args[10]; |
8455 | unsigned n_args = 0; | |
8456 | unsigned alloced = 10; | |
8457 | tree *arg_ary = fixed_args; | |
8458 | tree vec; | |
4bb8ca28 | 8459 | bool saved_in_template_argument_list_p; |
a723baf1 | 8460 | |
4bb8ca28 MM |
8461 | saved_in_template_argument_list_p = parser->in_template_argument_list_p; |
8462 | parser->in_template_argument_list_p = true; | |
bf12d54d | 8463 | do |
a723baf1 MM |
8464 | { |
8465 | tree argument; | |
8466 | ||
bf12d54d | 8467 | if (n_args) |
04c06002 | 8468 | /* Consume the comma. */ |
bf12d54d | 8469 | cp_lexer_consume_token (parser->lexer); |
21526606 | 8470 | |
a723baf1 MM |
8471 | /* Parse the template-argument. */ |
8472 | argument = cp_parser_template_argument (parser); | |
bf12d54d NS |
8473 | if (n_args == alloced) |
8474 | { | |
8475 | alloced *= 2; | |
21526606 | 8476 | |
bf12d54d NS |
8477 | if (arg_ary == fixed_args) |
8478 | { | |
8479 | arg_ary = xmalloc (sizeof (tree) * alloced); | |
8480 | memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); | |
8481 | } | |
8482 | else | |
8483 | arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced); | |
8484 | } | |
8485 | arg_ary[n_args++] = argument; | |
a723baf1 | 8486 | } |
bf12d54d NS |
8487 | while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); |
8488 | ||
8489 | vec = make_tree_vec (n_args); | |
a723baf1 | 8490 | |
bf12d54d NS |
8491 | while (n_args--) |
8492 | TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; | |
21526606 | 8493 | |
bf12d54d NS |
8494 | if (arg_ary != fixed_args) |
8495 | free (arg_ary); | |
4bb8ca28 | 8496 | parser->in_template_argument_list_p = saved_in_template_argument_list_p; |
bf12d54d | 8497 | return vec; |
a723baf1 MM |
8498 | } |
8499 | ||
8500 | /* Parse a template-argument. | |
8501 | ||
8502 | template-argument: | |
8503 | assignment-expression | |
8504 | type-id | |
8505 | id-expression | |
8506 | ||
8507 | The representation is that of an assignment-expression, type-id, or | |
8508 | id-expression -- except that the qualified id-expression is | |
8509 | evaluated, so that the value returned is either a DECL or an | |
21526606 | 8510 | OVERLOAD. |
d17811fd MM |
8511 | |
8512 | Although the standard says "assignment-expression", it forbids | |
8513 | throw-expressions or assignments in the template argument. | |
8514 | Therefore, we use "conditional-expression" instead. */ | |
a723baf1 MM |
8515 | |
8516 | static tree | |
94edc4ab | 8517 | cp_parser_template_argument (cp_parser* parser) |
a723baf1 MM |
8518 | { |
8519 | tree argument; | |
8520 | bool template_p; | |
d17811fd | 8521 | bool address_p; |
4d5297fa | 8522 | bool maybe_type_id = false; |
d17811fd | 8523 | cp_token *token; |
b3445994 | 8524 | cp_id_kind idk; |
d17811fd | 8525 | tree qualifying_class; |
a723baf1 MM |
8526 | |
8527 | /* There's really no way to know what we're looking at, so we just | |
21526606 | 8528 | try each alternative in order. |
a723baf1 MM |
8529 | |
8530 | [temp.arg] | |
8531 | ||
8532 | In a template-argument, an ambiguity between a type-id and an | |
8533 | expression is resolved to a type-id, regardless of the form of | |
21526606 | 8534 | the corresponding template-parameter. |
a723baf1 MM |
8535 | |
8536 | Therefore, we try a type-id first. */ | |
8537 | cp_parser_parse_tentatively (parser); | |
a723baf1 | 8538 | argument = cp_parser_type_id (parser); |
4d5297fa | 8539 | /* If there was no error parsing the type-id but the next token is a '>>', |
21526606 | 8540 | we probably found a typo for '> >'. But there are type-id which are |
4d5297fa GB |
8541 | also valid expressions. For instance: |
8542 | ||
8543 | struct X { int operator >> (int); }; | |
8544 | template <int V> struct Foo {}; | |
8545 | Foo<X () >> 5> r; | |
8546 | ||
8547 | Here 'X()' is a valid type-id of a function type, but the user just | |
8548 | wanted to write the expression "X() >> 5". Thus, we remember that we | |
8549 | found a valid type-id, but we still try to parse the argument as an | |
8550 | expression to see what happens. */ | |
8551 | if (!cp_parser_error_occurred (parser) | |
8552 | && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
8553 | { | |
8554 | maybe_type_id = true; | |
8555 | cp_parser_abort_tentative_parse (parser); | |
8556 | } | |
8557 | else | |
8558 | { | |
8559 | /* If the next token isn't a `,' or a `>', then this argument wasn't | |
8560 | really finished. This means that the argument is not a valid | |
8561 | type-id. */ | |
8562 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8563 | cp_parser_error (parser, "expected template-argument"); | |
8564 | /* If that worked, we're done. */ | |
8565 | if (cp_parser_parse_definitely (parser)) | |
8566 | return argument; | |
8567 | } | |
a723baf1 MM |
8568 | /* We're still not sure what the argument will be. */ |
8569 | cp_parser_parse_tentatively (parser); | |
8570 | /* Try a template. */ | |
21526606 | 8571 | argument = cp_parser_id_expression (parser, |
a723baf1 MM |
8572 | /*template_keyword_p=*/false, |
8573 | /*check_dependency_p=*/true, | |
f3c2dfc6 MM |
8574 | &template_p, |
8575 | /*declarator_p=*/false); | |
a723baf1 MM |
8576 | /* If the next token isn't a `,' or a `>', then this argument wasn't |
8577 | really finished. */ | |
d17811fd | 8578 | if (!cp_parser_next_token_ends_template_argument_p (parser)) |
a723baf1 MM |
8579 | cp_parser_error (parser, "expected template-argument"); |
8580 | if (!cp_parser_error_occurred (parser)) | |
8581 | { | |
f746161e MM |
8582 | /* Figure out what is being referred to. If the id-expression |
8583 | was for a class template specialization, then we will have a | |
8584 | TYPE_DECL at this point. There is no need to do name lookup | |
8585 | at this point in that case. */ | |
8586 | if (TREE_CODE (argument) != TYPE_DECL) | |
8587 | argument = cp_parser_lookup_name (parser, argument, | |
8588 | /*is_type=*/false, | |
8589 | /*is_template=*/template_p, | |
8590 | /*is_namespace=*/false, | |
8591 | /*check_dependency=*/true); | |
5b4acce1 KL |
8592 | if (TREE_CODE (argument) != TEMPLATE_DECL |
8593 | && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) | |
a723baf1 MM |
8594 | cp_parser_error (parser, "expected template-name"); |
8595 | } | |
8596 | if (cp_parser_parse_definitely (parser)) | |
8597 | return argument; | |
d17811fd MM |
8598 | /* It must be a non-type argument. There permitted cases are given |
8599 | in [temp.arg.nontype]: | |
8600 | ||
8601 | -- an integral constant-expression of integral or enumeration | |
8602 | type; or | |
8603 | ||
8604 | -- the name of a non-type template-parameter; or | |
8605 | ||
8606 | -- the name of an object or function with external linkage... | |
8607 | ||
8608 | -- the address of an object or function with external linkage... | |
8609 | ||
04c06002 | 8610 | -- a pointer to member... */ |
d17811fd MM |
8611 | /* Look for a non-type template parameter. */ |
8612 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
8613 | { | |
8614 | cp_parser_parse_tentatively (parser); | |
8615 | argument = cp_parser_primary_expression (parser, | |
8616 | &idk, | |
8617 | &qualifying_class); | |
8618 | if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX | |
8619 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8620 | cp_parser_simulate_error (parser); | |
8621 | if (cp_parser_parse_definitely (parser)) | |
8622 | return argument; | |
8623 | } | |
8624 | /* If the next token is "&", the argument must be the address of an | |
8625 | object or function with external linkage. */ | |
8626 | address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); | |
8627 | if (address_p) | |
8628 | cp_lexer_consume_token (parser->lexer); | |
8629 | /* See if we might have an id-expression. */ | |
8630 | token = cp_lexer_peek_token (parser->lexer); | |
8631 | if (token->type == CPP_NAME | |
8632 | || token->keyword == RID_OPERATOR | |
8633 | || token->type == CPP_SCOPE | |
8634 | || token->type == CPP_TEMPLATE_ID | |
8635 | || token->type == CPP_NESTED_NAME_SPECIFIER) | |
8636 | { | |
8637 | cp_parser_parse_tentatively (parser); | |
8638 | argument = cp_parser_primary_expression (parser, | |
8639 | &idk, | |
8640 | &qualifying_class); | |
8641 | if (cp_parser_error_occurred (parser) | |
8642 | || !cp_parser_next_token_ends_template_argument_p (parser)) | |
8643 | cp_parser_abort_tentative_parse (parser); | |
8644 | else | |
8645 | { | |
8646 | if (qualifying_class) | |
8647 | argument = finish_qualified_id_expr (qualifying_class, | |
8648 | argument, | |
8649 | /*done=*/true, | |
8650 | address_p); | |
8651 | if (TREE_CODE (argument) == VAR_DECL) | |
8652 | { | |
8653 | /* A variable without external linkage might still be a | |
8654 | valid constant-expression, so no error is issued here | |
8655 | if the external-linkage check fails. */ | |
8656 | if (!DECL_EXTERNAL_LINKAGE_P (argument)) | |
8657 | cp_parser_simulate_error (parser); | |
8658 | } | |
8659 | else if (is_overloaded_fn (argument)) | |
8660 | /* All overloaded functions are allowed; if the external | |
8661 | linkage test does not pass, an error will be issued | |
8662 | later. */ | |
8663 | ; | |
8664 | else if (address_p | |
21526606 | 8665 | && (TREE_CODE (argument) == OFFSET_REF |
d17811fd MM |
8666 | || TREE_CODE (argument) == SCOPE_REF)) |
8667 | /* A pointer-to-member. */ | |
8668 | ; | |
8669 | else | |
8670 | cp_parser_simulate_error (parser); | |
8671 | ||
8672 | if (cp_parser_parse_definitely (parser)) | |
8673 | { | |
8674 | if (address_p) | |
8675 | argument = build_x_unary_op (ADDR_EXPR, argument); | |
8676 | return argument; | |
8677 | } | |
8678 | } | |
8679 | } | |
8680 | /* If the argument started with "&", there are no other valid | |
8681 | alternatives at this point. */ | |
8682 | if (address_p) | |
8683 | { | |
8684 | cp_parser_error (parser, "invalid non-type template argument"); | |
8685 | return error_mark_node; | |
8686 | } | |
4d5297fa | 8687 | /* If the argument wasn't successfully parsed as a type-id followed |
21526606 | 8688 | by '>>', the argument can only be a constant expression now. |
4d5297fa GB |
8689 | Otherwise, we try parsing the constant-expression tentatively, |
8690 | because the argument could really be a type-id. */ | |
8691 | if (maybe_type_id) | |
8692 | cp_parser_parse_tentatively (parser); | |
21526606 | 8693 | argument = cp_parser_constant_expression (parser, |
d17811fd MM |
8694 | /*allow_non_constant_p=*/false, |
8695 | /*non_constant_p=*/NULL); | |
9baa27a9 | 8696 | argument = fold_non_dependent_expr (argument); |
4d5297fa GB |
8697 | if (!maybe_type_id) |
8698 | return argument; | |
8699 | if (!cp_parser_next_token_ends_template_argument_p (parser)) | |
8700 | cp_parser_error (parser, "expected template-argument"); | |
8701 | if (cp_parser_parse_definitely (parser)) | |
8702 | return argument; | |
8703 | /* We did our best to parse the argument as a non type-id, but that | |
8704 | was the only alternative that matched (albeit with a '>' after | |
21526606 | 8705 | it). We can assume it's just a typo from the user, and a |
4d5297fa GB |
8706 | diagnostic will then be issued. */ |
8707 | return cp_parser_type_id (parser); | |
a723baf1 MM |
8708 | } |
8709 | ||
8710 | /* Parse an explicit-instantiation. | |
8711 | ||
8712 | explicit-instantiation: | |
21526606 | 8713 | template declaration |
a723baf1 MM |
8714 | |
8715 | Although the standard says `declaration', what it really means is: | |
8716 | ||
8717 | explicit-instantiation: | |
21526606 | 8718 | template decl-specifier-seq [opt] declarator [opt] ; |
a723baf1 MM |
8719 | |
8720 | Things like `template int S<int>::i = 5, int S<double>::j;' are not | |
8721 | supposed to be allowed. A defect report has been filed about this | |
21526606 | 8722 | issue. |
a723baf1 MM |
8723 | |
8724 | GNU Extension: | |
21526606 | 8725 | |
a723baf1 | 8726 | explicit-instantiation: |
21526606 | 8727 | storage-class-specifier template |
a723baf1 | 8728 | decl-specifier-seq [opt] declarator [opt] ; |
21526606 | 8729 | function-specifier template |
a723baf1 MM |
8730 | decl-specifier-seq [opt] declarator [opt] ; */ |
8731 | ||
8732 | static void | |
94edc4ab | 8733 | cp_parser_explicit_instantiation (cp_parser* parser) |
a723baf1 | 8734 | { |
560ad596 | 8735 | int declares_class_or_enum; |
a723baf1 MM |
8736 | tree decl_specifiers; |
8737 | tree attributes; | |
8738 | tree extension_specifier = NULL_TREE; | |
8739 | ||
8740 | /* Look for an (optional) storage-class-specifier or | |
8741 | function-specifier. */ | |
8742 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
8743 | { | |
21526606 | 8744 | extension_specifier |
a723baf1 MM |
8745 | = cp_parser_storage_class_specifier_opt (parser); |
8746 | if (!extension_specifier) | |
8747 | extension_specifier = cp_parser_function_specifier_opt (parser); | |
8748 | } | |
8749 | ||
8750 | /* Look for the `template' keyword. */ | |
8751 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8752 | /* Let the front end know that we are processing an explicit | |
8753 | instantiation. */ | |
8754 | begin_explicit_instantiation (); | |
8755 | /* [temp.explicit] says that we are supposed to ignore access | |
8756 | control while processing explicit instantiation directives. */ | |
78757caa | 8757 | push_deferring_access_checks (dk_no_check); |
a723baf1 | 8758 | /* Parse a decl-specifier-seq. */ |
21526606 | 8759 | decl_specifiers |
a723baf1 MM |
8760 | = cp_parser_decl_specifier_seq (parser, |
8761 | CP_PARSER_FLAGS_OPTIONAL, | |
8762 | &attributes, | |
8763 | &declares_class_or_enum); | |
8764 | /* If there was exactly one decl-specifier, and it declared a class, | |
8765 | and there's no declarator, then we have an explicit type | |
8766 | instantiation. */ | |
8767 | if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) | |
8768 | { | |
8769 | tree type; | |
8770 | ||
8771 | type = check_tag_decl (decl_specifiers); | |
b7fc8b57 KL |
8772 | /* Turn access control back on for names used during |
8773 | template instantiation. */ | |
8774 | pop_deferring_access_checks (); | |
a723baf1 MM |
8775 | if (type) |
8776 | do_type_instantiation (type, extension_specifier, /*complain=*/1); | |
8777 | } | |
8778 | else | |
8779 | { | |
8780 | tree declarator; | |
8781 | tree decl; | |
8782 | ||
8783 | /* Parse the declarator. */ | |
21526606 | 8784 | declarator |
62b8a44e | 8785 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
8786 | /*ctor_dtor_or_conv_p=*/NULL, |
8787 | /*parenthesized_p=*/NULL); | |
21526606 | 8788 | cp_parser_check_for_definition_in_return_type (declarator, |
560ad596 | 8789 | declares_class_or_enum); |
216bb6e1 MM |
8790 | if (declarator != error_mark_node) |
8791 | { | |
21526606 | 8792 | decl = grokdeclarator (declarator, decl_specifiers, |
216bb6e1 MM |
8793 | NORMAL, 0, NULL); |
8794 | /* Turn access control back on for names used during | |
8795 | template instantiation. */ | |
8796 | pop_deferring_access_checks (); | |
8797 | /* Do the explicit instantiation. */ | |
8798 | do_decl_instantiation (decl, extension_specifier); | |
8799 | } | |
8800 | else | |
8801 | { | |
8802 | pop_deferring_access_checks (); | |
8803 | /* Skip the body of the explicit instantiation. */ | |
8804 | cp_parser_skip_to_end_of_statement (parser); | |
8805 | } | |
a723baf1 MM |
8806 | } |
8807 | /* We're done with the instantiation. */ | |
8808 | end_explicit_instantiation (); | |
a723baf1 | 8809 | |
e0860732 | 8810 | cp_parser_consume_semicolon_at_end_of_statement (parser); |
a723baf1 MM |
8811 | } |
8812 | ||
8813 | /* Parse an explicit-specialization. | |
8814 | ||
8815 | explicit-specialization: | |
21526606 | 8816 | template < > declaration |
a723baf1 MM |
8817 | |
8818 | Although the standard says `declaration', what it really means is: | |
8819 | ||
8820 | explicit-specialization: | |
8821 | template <> decl-specifier [opt] init-declarator [opt] ; | |
21526606 | 8822 | template <> function-definition |
a723baf1 MM |
8823 | template <> explicit-specialization |
8824 | template <> template-declaration */ | |
8825 | ||
8826 | static void | |
94edc4ab | 8827 | cp_parser_explicit_specialization (cp_parser* parser) |
a723baf1 MM |
8828 | { |
8829 | /* Look for the `template' keyword. */ | |
8830 | cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"); | |
8831 | /* Look for the `<'. */ | |
8832 | cp_parser_require (parser, CPP_LESS, "`<'"); | |
8833 | /* Look for the `>'. */ | |
8834 | cp_parser_require (parser, CPP_GREATER, "`>'"); | |
8835 | /* We have processed another parameter list. */ | |
8836 | ++parser->num_template_parameter_lists; | |
8837 | /* Let the front end know that we are beginning a specialization. */ | |
8838 | begin_specialization (); | |
8839 | ||
8840 | /* If the next keyword is `template', we need to figure out whether | |
8841 | or not we're looking a template-declaration. */ | |
8842 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
8843 | { | |
8844 | if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS | |
8845 | && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) | |
8846 | cp_parser_template_declaration_after_export (parser, | |
8847 | /*member_p=*/false); | |
8848 | else | |
8849 | cp_parser_explicit_specialization (parser); | |
8850 | } | |
8851 | else | |
8852 | /* Parse the dependent declaration. */ | |
21526606 | 8853 | cp_parser_single_declaration (parser, |
a723baf1 MM |
8854 | /*member_p=*/false, |
8855 | /*friend_p=*/NULL); | |
8856 | ||
8857 | /* We're done with the specialization. */ | |
8858 | end_specialization (); | |
8859 | /* We're done with this parameter list. */ | |
8860 | --parser->num_template_parameter_lists; | |
8861 | } | |
8862 | ||
8863 | /* Parse a type-specifier. | |
8864 | ||
8865 | type-specifier: | |
8866 | simple-type-specifier | |
8867 | class-specifier | |
8868 | enum-specifier | |
8869 | elaborated-type-specifier | |
8870 | cv-qualifier | |
8871 | ||
8872 | GNU Extension: | |
8873 | ||
8874 | type-specifier: | |
8875 | __complex__ | |
8876 | ||
8877 | Returns a representation of the type-specifier. If the | |
8878 | type-specifier is a keyword (like `int' or `const', or | |
34cd5ae7 | 8879 | `__complex__') then the corresponding IDENTIFIER_NODE is returned. |
a723baf1 MM |
8880 | For a class-specifier, enum-specifier, or elaborated-type-specifier |
8881 | a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. | |
8882 | ||
8883 | If IS_FRIEND is TRUE then this type-specifier is being declared a | |
8884 | `friend'. If IS_DECLARATION is TRUE, then this type-specifier is | |
8885 | appearing in a decl-specifier-seq. | |
8886 | ||
8887 | If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a | |
8888 | class-specifier, enum-specifier, or elaborated-type-specifier, then | |
83a00410 | 8889 | *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 |
560ad596 MM |
8890 | if a type is declared; 2 if it is defined. Otherwise, it is set to |
8891 | zero. | |
a723baf1 MM |
8892 | |
8893 | If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a | |
8894 | cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it | |
8895 | is set to FALSE. */ | |
8896 | ||
8897 | static tree | |
21526606 EC |
8898 | cp_parser_type_specifier (cp_parser* parser, |
8899 | cp_parser_flags flags, | |
94edc4ab NN |
8900 | bool is_friend, |
8901 | bool is_declaration, | |
560ad596 | 8902 | int* declares_class_or_enum, |
94edc4ab | 8903 | bool* is_cv_qualifier) |
a723baf1 MM |
8904 | { |
8905 | tree type_spec = NULL_TREE; | |
8906 | cp_token *token; | |
8907 | enum rid keyword; | |
8908 | ||
8909 | /* Assume this type-specifier does not declare a new type. */ | |
8910 | if (declares_class_or_enum) | |
543ca912 | 8911 | *declares_class_or_enum = 0; |
a723baf1 MM |
8912 | /* And that it does not specify a cv-qualifier. */ |
8913 | if (is_cv_qualifier) | |
8914 | *is_cv_qualifier = false; | |
8915 | /* Peek at the next token. */ | |
8916 | token = cp_lexer_peek_token (parser->lexer); | |
8917 | ||
8918 | /* If we're looking at a keyword, we can use that to guide the | |
8919 | production we choose. */ | |
8920 | keyword = token->keyword; | |
8921 | switch (keyword) | |
8922 | { | |
8923 | /* Any of these indicate either a class-specifier, or an | |
8924 | elaborated-type-specifier. */ | |
8925 | case RID_CLASS: | |
8926 | case RID_STRUCT: | |
8927 | case RID_UNION: | |
8928 | case RID_ENUM: | |
8929 | /* Parse tentatively so that we can back up if we don't find a | |
8930 | class-specifier or enum-specifier. */ | |
8931 | cp_parser_parse_tentatively (parser); | |
8932 | /* Look for the class-specifier or enum-specifier. */ | |
8933 | if (keyword == RID_ENUM) | |
8934 | type_spec = cp_parser_enum_specifier (parser); | |
8935 | else | |
8936 | type_spec = cp_parser_class_specifier (parser); | |
8937 | ||
8938 | /* If that worked, we're done. */ | |
8939 | if (cp_parser_parse_definitely (parser)) | |
8940 | { | |
8941 | if (declares_class_or_enum) | |
560ad596 | 8942 | *declares_class_or_enum = 2; |
a723baf1 MM |
8943 | return type_spec; |
8944 | } | |
8945 | ||
8946 | /* Fall through. */ | |
8947 | ||
8948 | case RID_TYPENAME: | |
8949 | /* Look for an elaborated-type-specifier. */ | |
8950 | type_spec = cp_parser_elaborated_type_specifier (parser, | |
8951 | is_friend, | |
8952 | is_declaration); | |
8953 | /* We're declaring a class or enum -- unless we're using | |
8954 | `typename'. */ | |
8955 | if (declares_class_or_enum && keyword != RID_TYPENAME) | |
560ad596 | 8956 | *declares_class_or_enum = 1; |
a723baf1 MM |
8957 | return type_spec; |
8958 | ||
8959 | case RID_CONST: | |
8960 | case RID_VOLATILE: | |
8961 | case RID_RESTRICT: | |
8962 | type_spec = cp_parser_cv_qualifier_opt (parser); | |
8963 | /* Even though we call a routine that looks for an optional | |
8964 | qualifier, we know that there should be one. */ | |
8965 | my_friendly_assert (type_spec != NULL, 20000328); | |
8966 | /* This type-specifier was a cv-qualified. */ | |
8967 | if (is_cv_qualifier) | |
8968 | *is_cv_qualifier = true; | |
8969 | ||
8970 | return type_spec; | |
8971 | ||
8972 | case RID_COMPLEX: | |
8973 | /* The `__complex__' keyword is a GNU extension. */ | |
8974 | return cp_lexer_consume_token (parser->lexer)->value; | |
8975 | ||
8976 | default: | |
8977 | break; | |
8978 | } | |
8979 | ||
8980 | /* If we do not already have a type-specifier, assume we are looking | |
8981 | at a simple-type-specifier. */ | |
21526606 | 8982 | type_spec = cp_parser_simple_type_specifier (parser, flags, |
4b0d3cbe | 8983 | /*identifier_p=*/true); |
a723baf1 MM |
8984 | |
8985 | /* If we didn't find a type-specifier, and a type-specifier was not | |
8986 | optional in this context, issue an error message. */ | |
8987 | if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
8988 | { | |
8989 | cp_parser_error (parser, "expected type specifier"); | |
8990 | return error_mark_node; | |
8991 | } | |
8992 | ||
8993 | return type_spec; | |
8994 | } | |
8995 | ||
8996 | /* Parse a simple-type-specifier. | |
8997 | ||
8998 | simple-type-specifier: | |
8999 | :: [opt] nested-name-specifier [opt] type-name | |
9000 | :: [opt] nested-name-specifier template template-id | |
9001 | char | |
9002 | wchar_t | |
9003 | bool | |
9004 | short | |
9005 | int | |
9006 | long | |
9007 | signed | |
9008 | unsigned | |
9009 | float | |
9010 | double | |
21526606 | 9011 | void |
a723baf1 MM |
9012 | |
9013 | GNU Extension: | |
9014 | ||
9015 | simple-type-specifier: | |
9016 | __typeof__ unary-expression | |
9017 | __typeof__ ( type-id ) | |
9018 | ||
9019 | For the various keywords, the value returned is simply the | |
4b0d3cbe MM |
9020 | TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true. |
9021 | For the first two productions, and if IDENTIFIER_P is false, the | |
9022 | value returned is the indicated TYPE_DECL. */ | |
a723baf1 MM |
9023 | |
9024 | static tree | |
4b0d3cbe MM |
9025 | cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags, |
9026 | bool identifier_p) | |
a723baf1 MM |
9027 | { |
9028 | tree type = NULL_TREE; | |
9029 | cp_token *token; | |
9030 | ||
9031 | /* Peek at the next token. */ | |
9032 | token = cp_lexer_peek_token (parser->lexer); | |
9033 | ||
9034 | /* If we're looking at a keyword, things are easy. */ | |
9035 | switch (token->keyword) | |
9036 | { | |
9037 | case RID_CHAR: | |
4b0d3cbe MM |
9038 | type = char_type_node; |
9039 | break; | |
a723baf1 | 9040 | case RID_WCHAR: |
4b0d3cbe MM |
9041 | type = wchar_type_node; |
9042 | break; | |
a723baf1 | 9043 | case RID_BOOL: |
4b0d3cbe MM |
9044 | type = boolean_type_node; |
9045 | break; | |
a723baf1 | 9046 | case RID_SHORT: |
4b0d3cbe MM |
9047 | type = short_integer_type_node; |
9048 | break; | |
a723baf1 | 9049 | case RID_INT: |
4b0d3cbe MM |
9050 | type = integer_type_node; |
9051 | break; | |
a723baf1 | 9052 | case RID_LONG: |
4b0d3cbe MM |
9053 | type = long_integer_type_node; |
9054 | break; | |
a723baf1 | 9055 | case RID_SIGNED: |
4b0d3cbe MM |
9056 | type = integer_type_node; |
9057 | break; | |
a723baf1 | 9058 | case RID_UNSIGNED: |
4b0d3cbe MM |
9059 | type = unsigned_type_node; |
9060 | break; | |
a723baf1 | 9061 | case RID_FLOAT: |
4b0d3cbe MM |
9062 | type = float_type_node; |
9063 | break; | |
a723baf1 | 9064 | case RID_DOUBLE: |
4b0d3cbe MM |
9065 | type = double_type_node; |
9066 | break; | |
a723baf1 | 9067 | case RID_VOID: |
4b0d3cbe MM |
9068 | type = void_type_node; |
9069 | break; | |
a723baf1 MM |
9070 | |
9071 | case RID_TYPEOF: | |
9072 | { | |
9073 | tree operand; | |
9074 | ||
9075 | /* Consume the `typeof' token. */ | |
9076 | cp_lexer_consume_token (parser->lexer); | |
04c06002 | 9077 | /* Parse the operand to `typeof'. */ |
a723baf1 MM |
9078 | operand = cp_parser_sizeof_operand (parser, RID_TYPEOF); |
9079 | /* If it is not already a TYPE, take its type. */ | |
9080 | if (!TYPE_P (operand)) | |
9081 | operand = finish_typeof (operand); | |
9082 | ||
9083 | return operand; | |
9084 | } | |
9085 | ||
9086 | default: | |
9087 | break; | |
9088 | } | |
9089 | ||
4b0d3cbe MM |
9090 | /* If the type-specifier was for a built-in type, we're done. */ |
9091 | if (type) | |
9092 | { | |
9093 | tree id; | |
9094 | ||
9095 | /* Consume the token. */ | |
9096 | id = cp_lexer_consume_token (parser->lexer)->value; | |
0d956474 GB |
9097 | |
9098 | /* There is no valid C++ program where a non-template type is | |
9099 | followed by a "<". That usually indicates that the user thought | |
9100 | that the type was a template. */ | |
9101 | cp_parser_check_for_invalid_template_id (parser, type); | |
9102 | ||
4b0d3cbe MM |
9103 | return identifier_p ? id : TYPE_NAME (type); |
9104 | } | |
9105 | ||
a723baf1 | 9106 | /* The type-specifier must be a user-defined type. */ |
21526606 | 9107 | if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) |
a723baf1 | 9108 | { |
0c1a1ecd MM |
9109 | bool qualified_p; |
9110 | ||
a723baf1 MM |
9111 | /* Don't gobble tokens or issue error messages if this is an |
9112 | optional type-specifier. */ | |
9113 | if (flags & CP_PARSER_FLAGS_OPTIONAL) | |
9114 | cp_parser_parse_tentatively (parser); | |
9115 | ||
9116 | /* Look for the optional `::' operator. */ | |
9117 | cp_parser_global_scope_opt (parser, | |
9118 | /*current_scope_valid_p=*/false); | |
9119 | /* Look for the nested-name specifier. */ | |
0c1a1ecd MM |
9120 | qualified_p |
9121 | = (cp_parser_nested_name_specifier_opt (parser, | |
9122 | /*typename_keyword_p=*/false, | |
9123 | /*check_dependency_p=*/true, | |
9124 | /*type_p=*/false, | |
6661a85f EB |
9125 | /*is_declaration=*/false) |
9126 | != NULL_TREE); | |
a723baf1 MM |
9127 | /* If we have seen a nested-name-specifier, and the next token |
9128 | is `template', then we are using the template-id production. */ | |
21526606 | 9129 | if (parser->scope |
a723baf1 MM |
9130 | && cp_parser_optional_template_keyword (parser)) |
9131 | { | |
9132 | /* Look for the template-id. */ | |
21526606 | 9133 | type = cp_parser_template_id (parser, |
a723baf1 | 9134 | /*template_keyword_p=*/true, |
a668c6ad MM |
9135 | /*check_dependency_p=*/true, |
9136 | /*is_declaration=*/false); | |
a723baf1 MM |
9137 | /* If the template-id did not name a type, we are out of |
9138 | luck. */ | |
9139 | if (TREE_CODE (type) != TYPE_DECL) | |
9140 | { | |
9141 | cp_parser_error (parser, "expected template-id for type"); | |
9142 | type = NULL_TREE; | |
9143 | } | |
9144 | } | |
9145 | /* Otherwise, look for a type-name. */ | |
9146 | else | |
4bb8ca28 | 9147 | type = cp_parser_type_name (parser); |
0c1a1ecd MM |
9148 | /* Keep track of all name-lookups performed in class scopes. */ |
9149 | if (type | |
9150 | && !qualified_p | |
9151 | && TREE_CODE (type) == TYPE_DECL | |
9152 | && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) | |
9153 | maybe_note_name_used_in_class (DECL_NAME (type), type); | |
a723baf1 | 9154 | /* If it didn't work out, we don't have a TYPE. */ |
21526606 | 9155 | if ((flags & CP_PARSER_FLAGS_OPTIONAL) |
a723baf1 MM |
9156 | && !cp_parser_parse_definitely (parser)) |
9157 | type = NULL_TREE; | |
9158 | } | |
9159 | ||
9160 | /* If we didn't get a type-name, issue an error message. */ | |
9161 | if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) | |
9162 | { | |
9163 | cp_parser_error (parser, "expected type-name"); | |
9164 | return error_mark_node; | |
9165 | } | |
9166 | ||
a668c6ad MM |
9167 | /* There is no valid C++ program where a non-template type is |
9168 | followed by a "<". That usually indicates that the user thought | |
9169 | that the type was a template. */ | |
4bb8ca28 | 9170 | if (type && type != error_mark_node) |
ee43dab5 | 9171 | cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type)); |
ec75414f | 9172 | |
a723baf1 MM |
9173 | return type; |
9174 | } | |
9175 | ||
9176 | /* Parse a type-name. | |
9177 | ||
9178 | type-name: | |
9179 | class-name | |
9180 | enum-name | |
21526606 | 9181 | typedef-name |
a723baf1 MM |
9182 | |
9183 | enum-name: | |
9184 | identifier | |
9185 | ||
9186 | typedef-name: | |
21526606 | 9187 | identifier |
a723baf1 MM |
9188 | |
9189 | Returns a TYPE_DECL for the the type. */ | |
9190 | ||
9191 | static tree | |
94edc4ab | 9192 | cp_parser_type_name (cp_parser* parser) |
a723baf1 MM |
9193 | { |
9194 | tree type_decl; | |
9195 | tree identifier; | |
9196 | ||
9197 | /* We can't know yet whether it is a class-name or not. */ | |
9198 | cp_parser_parse_tentatively (parser); | |
9199 | /* Try a class-name. */ | |
21526606 | 9200 | type_decl = cp_parser_class_name (parser, |
a723baf1 MM |
9201 | /*typename_keyword_p=*/false, |
9202 | /*template_keyword_p=*/false, | |
9203 | /*type_p=*/false, | |
a723baf1 | 9204 | /*check_dependency_p=*/true, |
a668c6ad MM |
9205 | /*class_head_p=*/false, |
9206 | /*is_declaration=*/false); | |
a723baf1 MM |
9207 | /* If it's not a class-name, keep looking. */ |
9208 | if (!cp_parser_parse_definitely (parser)) | |
9209 | { | |
9210 | /* It must be a typedef-name or an enum-name. */ | |
9211 | identifier = cp_parser_identifier (parser); | |
9212 | if (identifier == error_mark_node) | |
9213 | return error_mark_node; | |
21526606 | 9214 | |
a723baf1 MM |
9215 | /* Look up the type-name. */ |
9216 | type_decl = cp_parser_lookup_name_simple (parser, identifier); | |
9217 | /* Issue an error if we did not find a type-name. */ | |
9218 | if (TREE_CODE (type_decl) != TYPE_DECL) | |
9219 | { | |
4bb8ca28 | 9220 | if (!cp_parser_simulate_error (parser)) |
21526606 | 9221 | cp_parser_name_lookup_error (parser, identifier, type_decl, |
4bb8ca28 | 9222 | "is not a type"); |
a723baf1 MM |
9223 | type_decl = error_mark_node; |
9224 | } | |
9225 | /* Remember that the name was used in the definition of the | |
9226 | current class so that we can check later to see if the | |
9227 | meaning would have been different after the class was | |
9228 | entirely defined. */ | |
9229 | else if (type_decl != error_mark_node | |
9230 | && !parser->scope) | |
9231 | maybe_note_name_used_in_class (identifier, type_decl); | |
9232 | } | |
21526606 | 9233 | |
a723baf1 MM |
9234 | return type_decl; |
9235 | } | |
9236 | ||
9237 | ||
9238 | /* Parse an elaborated-type-specifier. Note that the grammar given | |
9239 | here incorporates the resolution to DR68. | |
9240 | ||
9241 | elaborated-type-specifier: | |
9242 | class-key :: [opt] nested-name-specifier [opt] identifier | |
9243 | class-key :: [opt] nested-name-specifier [opt] template [opt] template-id | |
9244 | enum :: [opt] nested-name-specifier [opt] identifier | |
9245 | typename :: [opt] nested-name-specifier identifier | |
21526606 EC |
9246 | typename :: [opt] nested-name-specifier template [opt] |
9247 | template-id | |
a723baf1 | 9248 | |
360d1b99 MM |
9249 | GNU extension: |
9250 | ||
9251 | elaborated-type-specifier: | |
9252 | class-key attributes :: [opt] nested-name-specifier [opt] identifier | |
21526606 | 9253 | class-key attributes :: [opt] nested-name-specifier [opt] |
360d1b99 MM |
9254 | template [opt] template-id |
9255 | enum attributes :: [opt] nested-name-specifier [opt] identifier | |
9256 | ||
a723baf1 MM |
9257 | If IS_FRIEND is TRUE, then this elaborated-type-specifier is being |
9258 | declared `friend'. If IS_DECLARATION is TRUE, then this | |
9259 | elaborated-type-specifier appears in a decl-specifiers-seq, i.e., | |
9260 | something is being declared. | |
9261 | ||
9262 | Returns the TYPE specified. */ | |
9263 | ||
9264 | static tree | |
21526606 EC |
9265 | cp_parser_elaborated_type_specifier (cp_parser* parser, |
9266 | bool is_friend, | |
94edc4ab | 9267 | bool is_declaration) |
a723baf1 MM |
9268 | { |
9269 | enum tag_types tag_type; | |
9270 | tree identifier; | |
9271 | tree type = NULL_TREE; | |
360d1b99 | 9272 | tree attributes = NULL_TREE; |
a723baf1 MM |
9273 | |
9274 | /* See if we're looking at the `enum' keyword. */ | |
9275 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) | |
9276 | { | |
9277 | /* Consume the `enum' token. */ | |
9278 | cp_lexer_consume_token (parser->lexer); | |
9279 | /* Remember that it's an enumeration type. */ | |
9280 | tag_type = enum_type; | |
360d1b99 MM |
9281 | /* Parse the attributes. */ |
9282 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9283 | } |
9284 | /* Or, it might be `typename'. */ | |
9285 | else if (cp_lexer_next_token_is_keyword (parser->lexer, | |
9286 | RID_TYPENAME)) | |
9287 | { | |
9288 | /* Consume the `typename' token. */ | |
9289 | cp_lexer_consume_token (parser->lexer); | |
9290 | /* Remember that it's a `typename' type. */ | |
9291 | tag_type = typename_type; | |
9292 | /* The `typename' keyword is only allowed in templates. */ | |
9293 | if (!processing_template_decl) | |
9294 | pedwarn ("using `typename' outside of template"); | |
9295 | } | |
9296 | /* Otherwise it must be a class-key. */ | |
9297 | else | |
9298 | { | |
9299 | tag_type = cp_parser_class_key (parser); | |
9300 | if (tag_type == none_type) | |
9301 | return error_mark_node; | |
360d1b99 MM |
9302 | /* Parse the attributes. */ |
9303 | attributes = cp_parser_attributes_opt (parser); | |
a723baf1 MM |
9304 | } |
9305 | ||
9306 | /* Look for the `::' operator. */ | |
21526606 | 9307 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
9308 | /*current_scope_valid_p=*/false); |
9309 | /* Look for the nested-name-specifier. */ | |
9310 | if (tag_type == typename_type) | |
8fa1ad0e MM |
9311 | { |
9312 | if (cp_parser_nested_name_specifier (parser, | |
9313 | /*typename_keyword_p=*/true, | |
9314 | /*check_dependency_p=*/true, | |
a668c6ad | 9315 | /*type_p=*/true, |
21526606 | 9316 | is_declaration) |
8fa1ad0e MM |
9317 | == error_mark_node) |
9318 | return error_mark_node; | |
9319 | } | |
a723baf1 MM |
9320 | else |
9321 | /* Even though `typename' is not present, the proposed resolution | |
9322 | to Core Issue 180 says that in `class A<T>::B', `B' should be | |
9323 | considered a type-name, even if `A<T>' is dependent. */ | |
9324 | cp_parser_nested_name_specifier_opt (parser, | |
9325 | /*typename_keyword_p=*/true, | |
9326 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9327 | /*type_p=*/true, |
9328 | is_declaration); | |
a723baf1 MM |
9329 | /* For everything but enumeration types, consider a template-id. */ |
9330 | if (tag_type != enum_type) | |
9331 | { | |
9332 | bool template_p = false; | |
9333 | tree decl; | |
9334 | ||
9335 | /* Allow the `template' keyword. */ | |
9336 | template_p = cp_parser_optional_template_keyword (parser); | |
9337 | /* If we didn't see `template', we don't know if there's a | |
9338 | template-id or not. */ | |
9339 | if (!template_p) | |
9340 | cp_parser_parse_tentatively (parser); | |
9341 | /* Parse the template-id. */ | |
9342 | decl = cp_parser_template_id (parser, template_p, | |
a668c6ad MM |
9343 | /*check_dependency_p=*/true, |
9344 | is_declaration); | |
a723baf1 MM |
9345 | /* If we didn't find a template-id, look for an ordinary |
9346 | identifier. */ | |
9347 | if (!template_p && !cp_parser_parse_definitely (parser)) | |
9348 | ; | |
9349 | /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is | |
9350 | in effect, then we must assume that, upon instantiation, the | |
9351 | template will correspond to a class. */ | |
9352 | else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR | |
9353 | && tag_type == typename_type) | |
9354 | type = make_typename_type (parser->scope, decl, | |
9355 | /*complain=*/1); | |
21526606 | 9356 | else |
a723baf1 MM |
9357 | type = TREE_TYPE (decl); |
9358 | } | |
9359 | ||
9360 | /* For an enumeration type, consider only a plain identifier. */ | |
9361 | if (!type) | |
9362 | { | |
9363 | identifier = cp_parser_identifier (parser); | |
9364 | ||
9365 | if (identifier == error_mark_node) | |
eb5abb39 NS |
9366 | { |
9367 | parser->scope = NULL_TREE; | |
9368 | return error_mark_node; | |
9369 | } | |
a723baf1 MM |
9370 | |
9371 | /* For a `typename', we needn't call xref_tag. */ | |
9372 | if (tag_type == typename_type) | |
21526606 | 9373 | return cp_parser_make_typename_type (parser, parser->scope, |
2097b5f2 | 9374 | identifier); |
a723baf1 MM |
9375 | /* Look up a qualified name in the usual way. */ |
9376 | if (parser->scope) | |
9377 | { | |
9378 | tree decl; | |
9379 | ||
9380 | /* In an elaborated-type-specifier, names are assumed to name | |
9381 | types, so we set IS_TYPE to TRUE when calling | |
9382 | cp_parser_lookup_name. */ | |
21526606 | 9383 | decl = cp_parser_lookup_name (parser, identifier, |
a723baf1 | 9384 | /*is_type=*/true, |
b0bc6e8e | 9385 | /*is_template=*/false, |
eea9800f | 9386 | /*is_namespace=*/false, |
a723baf1 | 9387 | /*check_dependency=*/true); |
710b73e6 KL |
9388 | |
9389 | /* If we are parsing friend declaration, DECL may be a | |
9390 | TEMPLATE_DECL tree node here. However, we need to check | |
9391 | whether this TEMPLATE_DECL results in valid code. Consider | |
9392 | the following example: | |
9393 | ||
9394 | namespace N { | |
9395 | template <class T> class C {}; | |
9396 | } | |
9397 | class X { | |
9398 | template <class T> friend class N::C; // #1, valid code | |
9399 | }; | |
9400 | template <class T> class Y { | |
9401 | friend class N::C; // #2, invalid code | |
9402 | }; | |
9403 | ||
9404 | For both case #1 and #2, we arrive at a TEMPLATE_DECL after | |
9405 | name lookup of `N::C'. We see that friend declaration must | |
9406 | be template for the code to be valid. Note that | |
9407 | processing_template_decl does not work here since it is | |
9408 | always 1 for the above two cases. */ | |
9409 | ||
21526606 | 9410 | decl = (cp_parser_maybe_treat_template_as_class |
710b73e6 KL |
9411 | (decl, /*tag_name_p=*/is_friend |
9412 | && parser->num_template_parameter_lists)); | |
a723baf1 MM |
9413 | |
9414 | if (TREE_CODE (decl) != TYPE_DECL) | |
9415 | { | |
9416 | error ("expected type-name"); | |
9417 | return error_mark_node; | |
9418 | } | |
560ad596 MM |
9419 | |
9420 | if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) | |
21526606 | 9421 | check_elaborated_type_specifier |
4b0d3cbe | 9422 | (tag_type, decl, |
560ad596 MM |
9423 | (parser->num_template_parameter_lists |
9424 | || DECL_SELF_REFERENCE_P (decl))); | |
a723baf1 MM |
9425 | |
9426 | type = TREE_TYPE (decl); | |
9427 | } | |
21526606 | 9428 | else |
a723baf1 MM |
9429 | { |
9430 | /* An elaborated-type-specifier sometimes introduces a new type and | |
9431 | sometimes names an existing type. Normally, the rule is that it | |
9432 | introduces a new type only if there is not an existing type of | |
9433 | the same name already in scope. For example, given: | |
9434 | ||
9435 | struct S {}; | |
9436 | void f() { struct S s; } | |
9437 | ||
9438 | the `struct S' in the body of `f' is the same `struct S' as in | |
9439 | the global scope; the existing definition is used. However, if | |
21526606 | 9440 | there were no global declaration, this would introduce a new |
a723baf1 MM |
9441 | local class named `S'. |
9442 | ||
9443 | An exception to this rule applies to the following code: | |
9444 | ||
9445 | namespace N { struct S; } | |
9446 | ||
9447 | Here, the elaborated-type-specifier names a new type | |
9448 | unconditionally; even if there is already an `S' in the | |
9449 | containing scope this declaration names a new type. | |
9450 | This exception only applies if the elaborated-type-specifier | |
9451 | forms the complete declaration: | |
9452 | ||
21526606 | 9453 | [class.name] |
a723baf1 MM |
9454 | |
9455 | A declaration consisting solely of `class-key identifier ;' is | |
9456 | either a redeclaration of the name in the current scope or a | |
9457 | forward declaration of the identifier as a class name. It | |
9458 | introduces the name into the current scope. | |
9459 | ||
9460 | We are in this situation precisely when the next token is a `;'. | |
9461 | ||
9462 | An exception to the exception is that a `friend' declaration does | |
9463 | *not* name a new type; i.e., given: | |
9464 | ||
9465 | struct S { friend struct T; }; | |
9466 | ||
21526606 | 9467 | `T' is not a new type in the scope of `S'. |
a723baf1 MM |
9468 | |
9469 | Also, `new struct S' or `sizeof (struct S)' never results in the | |
9470 | definition of a new type; a new type can only be declared in a | |
9bcb9aae | 9471 | declaration context. */ |
a723baf1 | 9472 | |
e0fed25b DS |
9473 | /* Warn about attributes. They are ignored. */ |
9474 | if (attributes) | |
9475 | warning ("type attributes are honored only at type definition"); | |
9476 | ||
21526606 | 9477 | type = xref_tag (tag_type, identifier, |
21526606 | 9478 | (is_friend |
a723baf1 | 9479 | || !is_declaration |
21526606 | 9480 | || cp_lexer_next_token_is_not (parser->lexer, |
cbd63935 KL |
9481 | CPP_SEMICOLON)), |
9482 | parser->num_template_parameter_lists); | |
a723baf1 MM |
9483 | } |
9484 | } | |
9485 | if (tag_type != enum_type) | |
9486 | cp_parser_check_class_key (tag_type, type); | |
ee43dab5 MM |
9487 | |
9488 | /* A "<" cannot follow an elaborated type specifier. If that | |
9489 | happens, the user was probably trying to form a template-id. */ | |
9490 | cp_parser_check_for_invalid_template_id (parser, type); | |
9491 | ||
a723baf1 MM |
9492 | return type; |
9493 | } | |
9494 | ||
9495 | /* Parse an enum-specifier. | |
9496 | ||
9497 | enum-specifier: | |
9498 | enum identifier [opt] { enumerator-list [opt] } | |
9499 | ||
9500 | Returns an ENUM_TYPE representing the enumeration. */ | |
9501 | ||
9502 | static tree | |
94edc4ab | 9503 | cp_parser_enum_specifier (cp_parser* parser) |
a723baf1 MM |
9504 | { |
9505 | cp_token *token; | |
9506 | tree identifier = NULL_TREE; | |
9507 | tree type; | |
9508 | ||
9509 | /* Look for the `enum' keyword. */ | |
9510 | if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'")) | |
9511 | return error_mark_node; | |
9512 | /* Peek at the next token. */ | |
9513 | token = cp_lexer_peek_token (parser->lexer); | |
9514 | ||
9515 | /* See if it is an identifier. */ | |
9516 | if (token->type == CPP_NAME) | |
9517 | identifier = cp_parser_identifier (parser); | |
9518 | ||
9519 | /* Look for the `{'. */ | |
9520 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
9521 | return error_mark_node; | |
9522 | ||
9523 | /* At this point, we're going ahead with the enum-specifier, even | |
9524 | if some other problem occurs. */ | |
9525 | cp_parser_commit_to_tentative_parse (parser); | |
9526 | ||
9527 | /* Issue an error message if type-definitions are forbidden here. */ | |
9528 | cp_parser_check_type_definition (parser); | |
9529 | ||
9530 | /* Create the new type. */ | |
9531 | type = start_enum (identifier ? identifier : make_anon_name ()); | |
9532 | ||
9533 | /* Peek at the next token. */ | |
9534 | token = cp_lexer_peek_token (parser->lexer); | |
9535 | /* If it's not a `}', then there are some enumerators. */ | |
9536 | if (token->type != CPP_CLOSE_BRACE) | |
9537 | cp_parser_enumerator_list (parser, type); | |
9538 | /* Look for the `}'. */ | |
9539 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9540 | ||
9541 | /* Finish up the enumeration. */ | |
9542 | finish_enum (type); | |
9543 | ||
9544 | return type; | |
9545 | } | |
9546 | ||
9547 | /* Parse an enumerator-list. The enumerators all have the indicated | |
21526606 | 9548 | TYPE. |
a723baf1 MM |
9549 | |
9550 | enumerator-list: | |
9551 | enumerator-definition | |
9552 | enumerator-list , enumerator-definition */ | |
9553 | ||
9554 | static void | |
94edc4ab | 9555 | cp_parser_enumerator_list (cp_parser* parser, tree type) |
a723baf1 MM |
9556 | { |
9557 | while (true) | |
9558 | { | |
9559 | cp_token *token; | |
9560 | ||
9561 | /* Parse an enumerator-definition. */ | |
9562 | cp_parser_enumerator_definition (parser, type); | |
9563 | /* Peek at the next token. */ | |
9564 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 9565 | /* If it's not a `,', then we've reached the end of the |
a723baf1 MM |
9566 | list. */ |
9567 | if (token->type != CPP_COMMA) | |
9568 | break; | |
9569 | /* Otherwise, consume the `,' and keep going. */ | |
9570 | cp_lexer_consume_token (parser->lexer); | |
9571 | /* If the next token is a `}', there is a trailing comma. */ | |
9572 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) | |
9573 | { | |
9574 | if (pedantic && !in_system_header) | |
9575 | pedwarn ("comma at end of enumerator list"); | |
9576 | break; | |
9577 | } | |
9578 | } | |
9579 | } | |
9580 | ||
9581 | /* Parse an enumerator-definition. The enumerator has the indicated | |
9582 | TYPE. | |
9583 | ||
9584 | enumerator-definition: | |
9585 | enumerator | |
9586 | enumerator = constant-expression | |
21526606 | 9587 | |
a723baf1 MM |
9588 | enumerator: |
9589 | identifier */ | |
9590 | ||
9591 | static void | |
94edc4ab | 9592 | cp_parser_enumerator_definition (cp_parser* parser, tree type) |
a723baf1 MM |
9593 | { |
9594 | cp_token *token; | |
9595 | tree identifier; | |
9596 | tree value; | |
9597 | ||
9598 | /* Look for the identifier. */ | |
9599 | identifier = cp_parser_identifier (parser); | |
9600 | if (identifier == error_mark_node) | |
9601 | return; | |
21526606 | 9602 | |
a723baf1 MM |
9603 | /* Peek at the next token. */ |
9604 | token = cp_lexer_peek_token (parser->lexer); | |
9605 | /* If it's an `=', then there's an explicit value. */ | |
9606 | if (token->type == CPP_EQ) | |
9607 | { | |
9608 | /* Consume the `=' token. */ | |
9609 | cp_lexer_consume_token (parser->lexer); | |
9610 | /* Parse the value. */ | |
21526606 | 9611 | value = cp_parser_constant_expression (parser, |
d17811fd | 9612 | /*allow_non_constant_p=*/false, |
14d22dd6 | 9613 | NULL); |
a723baf1 MM |
9614 | } |
9615 | else | |
9616 | value = NULL_TREE; | |
9617 | ||
9618 | /* Create the enumerator. */ | |
9619 | build_enumerator (identifier, value, type); | |
9620 | } | |
9621 | ||
9622 | /* Parse a namespace-name. | |
9623 | ||
9624 | namespace-name: | |
9625 | original-namespace-name | |
9626 | namespace-alias | |
9627 | ||
9628 | Returns the NAMESPACE_DECL for the namespace. */ | |
9629 | ||
9630 | static tree | |
94edc4ab | 9631 | cp_parser_namespace_name (cp_parser* parser) |
a723baf1 MM |
9632 | { |
9633 | tree identifier; | |
9634 | tree namespace_decl; | |
9635 | ||
9636 | /* Get the name of the namespace. */ | |
9637 | identifier = cp_parser_identifier (parser); | |
9638 | if (identifier == error_mark_node) | |
9639 | return error_mark_node; | |
9640 | ||
eea9800f MM |
9641 | /* Look up the identifier in the currently active scope. Look only |
9642 | for namespaces, due to: | |
9643 | ||
9644 | [basic.lookup.udir] | |
9645 | ||
9646 | When looking up a namespace-name in a using-directive or alias | |
21526606 | 9647 | definition, only namespace names are considered. |
eea9800f MM |
9648 | |
9649 | And: | |
9650 | ||
9651 | [basic.lookup.qual] | |
9652 | ||
9653 | During the lookup of a name preceding the :: scope resolution | |
21526606 | 9654 | operator, object, function, and enumerator names are ignored. |
eea9800f MM |
9655 | |
9656 | (Note that cp_parser_class_or_namespace_name only calls this | |
9657 | function if the token after the name is the scope resolution | |
9658 | operator.) */ | |
9659 | namespace_decl = cp_parser_lookup_name (parser, identifier, | |
eea9800f | 9660 | /*is_type=*/false, |
b0bc6e8e | 9661 | /*is_template=*/false, |
eea9800f MM |
9662 | /*is_namespace=*/true, |
9663 | /*check_dependency=*/true); | |
a723baf1 MM |
9664 | /* If it's not a namespace, issue an error. */ |
9665 | if (namespace_decl == error_mark_node | |
9666 | || TREE_CODE (namespace_decl) != NAMESPACE_DECL) | |
9667 | { | |
9668 | cp_parser_error (parser, "expected namespace-name"); | |
9669 | namespace_decl = error_mark_node; | |
9670 | } | |
21526606 | 9671 | |
a723baf1 MM |
9672 | return namespace_decl; |
9673 | } | |
9674 | ||
9675 | /* Parse a namespace-definition. | |
9676 | ||
9677 | namespace-definition: | |
9678 | named-namespace-definition | |
21526606 | 9679 | unnamed-namespace-definition |
a723baf1 MM |
9680 | |
9681 | named-namespace-definition: | |
9682 | original-namespace-definition | |
9683 | extension-namespace-definition | |
9684 | ||
9685 | original-namespace-definition: | |
9686 | namespace identifier { namespace-body } | |
21526606 | 9687 | |
a723baf1 MM |
9688 | extension-namespace-definition: |
9689 | namespace original-namespace-name { namespace-body } | |
21526606 | 9690 | |
a723baf1 MM |
9691 | unnamed-namespace-definition: |
9692 | namespace { namespace-body } */ | |
9693 | ||
9694 | static void | |
94edc4ab | 9695 | cp_parser_namespace_definition (cp_parser* parser) |
a723baf1 MM |
9696 | { |
9697 | tree identifier; | |
9698 | ||
9699 | /* Look for the `namespace' keyword. */ | |
9700 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9701 | ||
9702 | /* Get the name of the namespace. We do not attempt to distinguish | |
9703 | between an original-namespace-definition and an | |
9704 | extension-namespace-definition at this point. The semantic | |
9705 | analysis routines are responsible for that. */ | |
9706 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
9707 | identifier = cp_parser_identifier (parser); | |
9708 | else | |
9709 | identifier = NULL_TREE; | |
9710 | ||
9711 | /* Look for the `{' to start the namespace. */ | |
9712 | cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"); | |
9713 | /* Start the namespace. */ | |
9714 | push_namespace (identifier); | |
9715 | /* Parse the body of the namespace. */ | |
9716 | cp_parser_namespace_body (parser); | |
9717 | /* Finish the namespace. */ | |
9718 | pop_namespace (); | |
9719 | /* Look for the final `}'. */ | |
9720 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
9721 | } | |
9722 | ||
9723 | /* Parse a namespace-body. | |
9724 | ||
9725 | namespace-body: | |
9726 | declaration-seq [opt] */ | |
9727 | ||
9728 | static void | |
94edc4ab | 9729 | cp_parser_namespace_body (cp_parser* parser) |
a723baf1 MM |
9730 | { |
9731 | cp_parser_declaration_seq_opt (parser); | |
9732 | } | |
9733 | ||
9734 | /* Parse a namespace-alias-definition. | |
9735 | ||
9736 | namespace-alias-definition: | |
9737 | namespace identifier = qualified-namespace-specifier ; */ | |
9738 | ||
9739 | static void | |
94edc4ab | 9740 | cp_parser_namespace_alias_definition (cp_parser* parser) |
a723baf1 MM |
9741 | { |
9742 | tree identifier; | |
9743 | tree namespace_specifier; | |
9744 | ||
9745 | /* Look for the `namespace' keyword. */ | |
9746 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9747 | /* Look for the identifier. */ | |
9748 | identifier = cp_parser_identifier (parser); | |
9749 | if (identifier == error_mark_node) | |
9750 | return; | |
9751 | /* Look for the `=' token. */ | |
9752 | cp_parser_require (parser, CPP_EQ, "`='"); | |
9753 | /* Look for the qualified-namespace-specifier. */ | |
21526606 | 9754 | namespace_specifier |
a723baf1 MM |
9755 | = cp_parser_qualified_namespace_specifier (parser); |
9756 | /* Look for the `;' token. */ | |
9757 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9758 | ||
9759 | /* Register the alias in the symbol table. */ | |
9760 | do_namespace_alias (identifier, namespace_specifier); | |
9761 | } | |
9762 | ||
9763 | /* Parse a qualified-namespace-specifier. | |
9764 | ||
9765 | qualified-namespace-specifier: | |
9766 | :: [opt] nested-name-specifier [opt] namespace-name | |
9767 | ||
9768 | Returns a NAMESPACE_DECL corresponding to the specified | |
9769 | namespace. */ | |
9770 | ||
9771 | static tree | |
94edc4ab | 9772 | cp_parser_qualified_namespace_specifier (cp_parser* parser) |
a723baf1 MM |
9773 | { |
9774 | /* Look for the optional `::'. */ | |
21526606 | 9775 | cp_parser_global_scope_opt (parser, |
a723baf1 MM |
9776 | /*current_scope_valid_p=*/false); |
9777 | ||
9778 | /* Look for the optional nested-name-specifier. */ | |
9779 | cp_parser_nested_name_specifier_opt (parser, | |
9780 | /*typename_keyword_p=*/false, | |
9781 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9782 | /*type_p=*/false, |
9783 | /*is_declaration=*/true); | |
a723baf1 MM |
9784 | |
9785 | return cp_parser_namespace_name (parser); | |
9786 | } | |
9787 | ||
9788 | /* Parse a using-declaration. | |
9789 | ||
9790 | using-declaration: | |
9791 | using typename [opt] :: [opt] nested-name-specifier unqualified-id ; | |
9792 | using :: unqualified-id ; */ | |
9793 | ||
9794 | static void | |
94edc4ab | 9795 | cp_parser_using_declaration (cp_parser* parser) |
a723baf1 MM |
9796 | { |
9797 | cp_token *token; | |
9798 | bool typename_p = false; | |
9799 | bool global_scope_p; | |
9800 | tree decl; | |
9801 | tree identifier; | |
9802 | tree scope; | |
ed5f054f | 9803 | tree qscope; |
a723baf1 MM |
9804 | |
9805 | /* Look for the `using' keyword. */ | |
9806 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
21526606 | 9807 | |
a723baf1 MM |
9808 | /* Peek at the next token. */ |
9809 | token = cp_lexer_peek_token (parser->lexer); | |
9810 | /* See if it's `typename'. */ | |
9811 | if (token->keyword == RID_TYPENAME) | |
9812 | { | |
9813 | /* Remember that we've seen it. */ | |
9814 | typename_p = true; | |
9815 | /* Consume the `typename' token. */ | |
9816 | cp_lexer_consume_token (parser->lexer); | |
9817 | } | |
9818 | ||
9819 | /* Look for the optional global scope qualification. */ | |
21526606 | 9820 | global_scope_p |
a723baf1 | 9821 | = (cp_parser_global_scope_opt (parser, |
21526606 | 9822 | /*current_scope_valid_p=*/false) |
a723baf1 MM |
9823 | != NULL_TREE); |
9824 | ||
9825 | /* If we saw `typename', or didn't see `::', then there must be a | |
9826 | nested-name-specifier present. */ | |
9827 | if (typename_p || !global_scope_p) | |
21526606 | 9828 | qscope = cp_parser_nested_name_specifier (parser, typename_p, |
ed5f054f AO |
9829 | /*check_dependency_p=*/true, |
9830 | /*type_p=*/false, | |
9831 | /*is_declaration=*/true); | |
a723baf1 MM |
9832 | /* Otherwise, we could be in either of the two productions. In that |
9833 | case, treat the nested-name-specifier as optional. */ | |
9834 | else | |
ed5f054f AO |
9835 | qscope = cp_parser_nested_name_specifier_opt (parser, |
9836 | /*typename_keyword_p=*/false, | |
9837 | /*check_dependency_p=*/true, | |
9838 | /*type_p=*/false, | |
9839 | /*is_declaration=*/true); | |
9840 | if (!qscope) | |
9841 | qscope = global_namespace; | |
a723baf1 MM |
9842 | |
9843 | /* Parse the unqualified-id. */ | |
21526606 | 9844 | identifier = cp_parser_unqualified_id (parser, |
a723baf1 | 9845 | /*template_keyword_p=*/false, |
f3c2dfc6 MM |
9846 | /*check_dependency_p=*/true, |
9847 | /*declarator_p=*/true); | |
a723baf1 MM |
9848 | |
9849 | /* The function we call to handle a using-declaration is different | |
9850 | depending on what scope we are in. */ | |
f3c2dfc6 MM |
9851 | if (identifier == error_mark_node) |
9852 | ; | |
9853 | else if (TREE_CODE (identifier) != IDENTIFIER_NODE | |
9854 | && TREE_CODE (identifier) != BIT_NOT_EXPR) | |
9855 | /* [namespace.udecl] | |
9856 | ||
9857 | A using declaration shall not name a template-id. */ | |
9858 | error ("a template-id may not appear in a using-declaration"); | |
a723baf1 MM |
9859 | else |
9860 | { | |
f3c2dfc6 MM |
9861 | scope = current_scope (); |
9862 | if (scope && TYPE_P (scope)) | |
4eb6d609 | 9863 | { |
f3c2dfc6 MM |
9864 | /* Create the USING_DECL. */ |
9865 | decl = do_class_using_decl (build_nt (SCOPE_REF, | |
9866 | parser->scope, | |
9867 | identifier)); | |
9868 | /* Add it to the list of members in this class. */ | |
9869 | finish_member_declaration (decl); | |
4eb6d609 | 9870 | } |
a723baf1 | 9871 | else |
f3c2dfc6 MM |
9872 | { |
9873 | decl = cp_parser_lookup_name_simple (parser, identifier); | |
9874 | if (decl == error_mark_node) | |
4bb8ca28 | 9875 | cp_parser_name_lookup_error (parser, identifier, decl, NULL); |
f3c2dfc6 | 9876 | else if (scope) |
ed5f054f | 9877 | do_local_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 9878 | else |
ed5f054f | 9879 | do_toplevel_using_decl (decl, qscope, identifier); |
f3c2dfc6 | 9880 | } |
a723baf1 MM |
9881 | } |
9882 | ||
9883 | /* Look for the final `;'. */ | |
9884 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9885 | } | |
9886 | ||
21526606 EC |
9887 | /* Parse a using-directive. |
9888 | ||
a723baf1 MM |
9889 | using-directive: |
9890 | using namespace :: [opt] nested-name-specifier [opt] | |
9891 | namespace-name ; */ | |
9892 | ||
9893 | static void | |
94edc4ab | 9894 | cp_parser_using_directive (cp_parser* parser) |
a723baf1 MM |
9895 | { |
9896 | tree namespace_decl; | |
86098eb8 | 9897 | tree attribs; |
a723baf1 MM |
9898 | |
9899 | /* Look for the `using' keyword. */ | |
9900 | cp_parser_require_keyword (parser, RID_USING, "`using'"); | |
9901 | /* And the `namespace' keyword. */ | |
9902 | cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'"); | |
9903 | /* Look for the optional `::' operator. */ | |
9904 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
34cd5ae7 | 9905 | /* And the optional nested-name-specifier. */ |
a723baf1 MM |
9906 | cp_parser_nested_name_specifier_opt (parser, |
9907 | /*typename_keyword_p=*/false, | |
9908 | /*check_dependency_p=*/true, | |
a668c6ad MM |
9909 | /*type_p=*/false, |
9910 | /*is_declaration=*/true); | |
a723baf1 MM |
9911 | /* Get the namespace being used. */ |
9912 | namespace_decl = cp_parser_namespace_name (parser); | |
86098eb8 JM |
9913 | /* And any specified attributes. */ |
9914 | attribs = cp_parser_attributes_opt (parser); | |
a723baf1 | 9915 | /* Update the symbol table. */ |
86098eb8 | 9916 | parse_using_directive (namespace_decl, attribs); |
a723baf1 MM |
9917 | /* Look for the final `;'. */ |
9918 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
9919 | } | |
9920 | ||
9921 | /* Parse an asm-definition. | |
9922 | ||
9923 | asm-definition: | |
21526606 | 9924 | asm ( string-literal ) ; |
a723baf1 MM |
9925 | |
9926 | GNU Extension: | |
9927 | ||
9928 | asm-definition: | |
9929 | asm volatile [opt] ( string-literal ) ; | |
9930 | asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; | |
9931 | asm volatile [opt] ( string-literal : asm-operand-list [opt] | |
9932 | : asm-operand-list [opt] ) ; | |
21526606 EC |
9933 | asm volatile [opt] ( string-literal : asm-operand-list [opt] |
9934 | : asm-operand-list [opt] | |
a723baf1 MM |
9935 | : asm-operand-list [opt] ) ; */ |
9936 | ||
9937 | static void | |
94edc4ab | 9938 | cp_parser_asm_definition (cp_parser* parser) |
a723baf1 MM |
9939 | { |
9940 | cp_token *token; | |
9941 | tree string; | |
9942 | tree outputs = NULL_TREE; | |
9943 | tree inputs = NULL_TREE; | |
9944 | tree clobbers = NULL_TREE; | |
9945 | tree asm_stmt; | |
9946 | bool volatile_p = false; | |
9947 | bool extended_p = false; | |
9948 | ||
9949 | /* Look for the `asm' keyword. */ | |
9950 | cp_parser_require_keyword (parser, RID_ASM, "`asm'"); | |
9951 | /* See if the next token is `volatile'. */ | |
9952 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9953 | && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) | |
9954 | { | |
9955 | /* Remember that we saw the `volatile' keyword. */ | |
9956 | volatile_p = true; | |
9957 | /* Consume the token. */ | |
9958 | cp_lexer_consume_token (parser->lexer); | |
9959 | } | |
9960 | /* Look for the opening `('. */ | |
9961 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
9962 | /* Look for the string. */ | |
0173bb6f | 9963 | c_lex_string_translate = 0; |
a723baf1 MM |
9964 | token = cp_parser_require (parser, CPP_STRING, "asm body"); |
9965 | if (!token) | |
21526606 | 9966 | goto finish; |
a723baf1 MM |
9967 | string = token->value; |
9968 | /* If we're allowing GNU extensions, check for the extended assembly | |
21526606 | 9969 | syntax. Unfortunately, the `:' tokens need not be separated by |
a723baf1 MM |
9970 | a space in C, and so, for compatibility, we tolerate that here |
9971 | too. Doing that means that we have to treat the `::' operator as | |
9972 | two `:' tokens. */ | |
9973 | if (cp_parser_allow_gnu_extensions_p (parser) | |
9974 | && at_function_scope_p () | |
9975 | && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) | |
9976 | || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) | |
9977 | { | |
9978 | bool inputs_p = false; | |
9979 | bool clobbers_p = false; | |
9980 | ||
9981 | /* The extended syntax was used. */ | |
9982 | extended_p = true; | |
9983 | ||
9984 | /* Look for outputs. */ | |
9985 | if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
9986 | { | |
9987 | /* Consume the `:'. */ | |
9988 | cp_lexer_consume_token (parser->lexer); | |
9989 | /* Parse the output-operands. */ | |
21526606 | 9990 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
9991 | CPP_COLON) |
9992 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
9993 | CPP_SCOPE) |
9994 | && cp_lexer_next_token_is_not (parser->lexer, | |
9995 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
9996 | outputs = cp_parser_asm_operand_list (parser); |
9997 | } | |
9998 | /* If the next token is `::', there are no outputs, and the | |
9999 | next token is the beginning of the inputs. */ | |
10000 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
10001 | { | |
10002 | /* Consume the `::' token. */ | |
10003 | cp_lexer_consume_token (parser->lexer); | |
10004 | /* The inputs are coming next. */ | |
10005 | inputs_p = true; | |
10006 | } | |
10007 | ||
10008 | /* Look for inputs. */ | |
10009 | if (inputs_p | |
10010 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) | |
10011 | { | |
10012 | if (!inputs_p) | |
10013 | /* Consume the `:'. */ | |
10014 | cp_lexer_consume_token (parser->lexer); | |
10015 | /* Parse the output-operands. */ | |
21526606 | 10016 | if (cp_lexer_next_token_is_not (parser->lexer, |
a723baf1 MM |
10017 | CPP_COLON) |
10018 | && cp_lexer_next_token_is_not (parser->lexer, | |
8caf4c38 MM |
10019 | CPP_SCOPE) |
10020 | && cp_lexer_next_token_is_not (parser->lexer, | |
10021 | CPP_CLOSE_PAREN)) | |
a723baf1 MM |
10022 | inputs = cp_parser_asm_operand_list (parser); |
10023 | } | |
10024 | else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
10025 | /* The clobbers are coming next. */ | |
10026 | clobbers_p = true; | |
10027 | ||
10028 | /* Look for clobbers. */ | |
21526606 | 10029 | if (clobbers_p |
a723baf1 MM |
10030 | || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) |
10031 | { | |
10032 | if (!clobbers_p) | |
10033 | /* Consume the `:'. */ | |
10034 | cp_lexer_consume_token (parser->lexer); | |
10035 | /* Parse the clobbers. */ | |
8caf4c38 MM |
10036 | if (cp_lexer_next_token_is_not (parser->lexer, |
10037 | CPP_CLOSE_PAREN)) | |
10038 | clobbers = cp_parser_asm_clobber_list (parser); | |
a723baf1 MM |
10039 | } |
10040 | } | |
10041 | /* Look for the closing `)'. */ | |
10042 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
a668c6ad MM |
10043 | cp_parser_skip_to_closing_parenthesis (parser, true, false, |
10044 | /*consume_paren=*/true); | |
a723baf1 MM |
10045 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
10046 | ||
10047 | /* Create the ASM_STMT. */ | |
10048 | if (at_function_scope_p ()) | |
10049 | { | |
6de9cd9a DN |
10050 | asm_stmt = finish_asm_stmt (volatile_p, string, outputs, |
10051 | inputs, clobbers); | |
a723baf1 MM |
10052 | /* If the extended syntax was not used, mark the ASM_STMT. */ |
10053 | if (!extended_p) | |
10054 | ASM_INPUT_P (asm_stmt) = 1; | |
10055 | } | |
10056 | else | |
10057 | assemble_asm (string); | |
21526606 EC |
10058 | |
10059 | finish: | |
0173bb6f | 10060 | c_lex_string_translate = 1; |
a723baf1 MM |
10061 | } |
10062 | ||
10063 | /* Declarators [gram.dcl.decl] */ | |
10064 | ||
10065 | /* Parse an init-declarator. | |
10066 | ||
10067 | init-declarator: | |
10068 | declarator initializer [opt] | |
10069 | ||
10070 | GNU Extension: | |
10071 | ||
10072 | init-declarator: | |
10073 | declarator asm-specification [opt] attributes [opt] initializer [opt] | |
10074 | ||
4bb8ca28 MM |
10075 | function-definition: |
10076 | decl-specifier-seq [opt] declarator ctor-initializer [opt] | |
21526606 EC |
10077 | function-body |
10078 | decl-specifier-seq [opt] declarator function-try-block | |
4bb8ca28 MM |
10079 | |
10080 | GNU Extension: | |
10081 | ||
10082 | function-definition: | |
21526606 | 10083 | __extension__ function-definition |
4bb8ca28 | 10084 | |
a723baf1 | 10085 | The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator. |
c8e4f0e9 | 10086 | Returns a representation of the entity declared. If MEMBER_P is TRUE, |
cf22909c KL |
10087 | then this declarator appears in a class scope. The new DECL created |
10088 | by this declarator is returned. | |
a723baf1 MM |
10089 | |
10090 | If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and | |
10091 | for a function-definition here as well. If the declarator is a | |
10092 | declarator for a function-definition, *FUNCTION_DEFINITION_P will | |
10093 | be TRUE upon return. By that point, the function-definition will | |
10094 | have been completely parsed. | |
10095 | ||
10096 | FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P | |
10097 | is FALSE. */ | |
10098 | ||
10099 | static tree | |
21526606 EC |
10100 | cp_parser_init_declarator (cp_parser* parser, |
10101 | tree decl_specifiers, | |
94edc4ab NN |
10102 | tree prefix_attributes, |
10103 | bool function_definition_allowed_p, | |
10104 | bool member_p, | |
560ad596 | 10105 | int declares_class_or_enum, |
94edc4ab | 10106 | bool* function_definition_p) |
a723baf1 MM |
10107 | { |
10108 | cp_token *token; | |
10109 | tree declarator; | |
10110 | tree attributes; | |
10111 | tree asm_specification; | |
10112 | tree initializer; | |
10113 | tree decl = NULL_TREE; | |
10114 | tree scope; | |
a723baf1 MM |
10115 | bool is_initialized; |
10116 | bool is_parenthesized_init; | |
39703eb9 | 10117 | bool is_non_constant_init; |
7efa3e22 | 10118 | int ctor_dtor_or_conv_p; |
a723baf1 | 10119 | bool friend_p; |
91b004e5 | 10120 | bool pop_p = false; |
a723baf1 MM |
10121 | |
10122 | /* Assume that this is not the declarator for a function | |
10123 | definition. */ | |
10124 | if (function_definition_p) | |
10125 | *function_definition_p = false; | |
10126 | ||
10127 | /* Defer access checks while parsing the declarator; we cannot know | |
21526606 | 10128 | what names are accessible until we know what is being |
a723baf1 | 10129 | declared. */ |
cf22909c KL |
10130 | resume_deferring_access_checks (); |
10131 | ||
a723baf1 | 10132 | /* Parse the declarator. */ |
21526606 | 10133 | declarator |
62b8a44e | 10134 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
10135 | &ctor_dtor_or_conv_p, |
10136 | /*parenthesized_p=*/NULL); | |
a723baf1 | 10137 | /* Gather up the deferred checks. */ |
cf22909c | 10138 | stop_deferring_access_checks (); |
24c0ef37 | 10139 | |
a723baf1 MM |
10140 | /* If the DECLARATOR was erroneous, there's no need to go |
10141 | further. */ | |
10142 | if (declarator == error_mark_node) | |
cf22909c | 10143 | return error_mark_node; |
a723baf1 | 10144 | |
560ad596 MM |
10145 | cp_parser_check_for_definition_in_return_type (declarator, |
10146 | declares_class_or_enum); | |
10147 | ||
a723baf1 MM |
10148 | /* Figure out what scope the entity declared by the DECLARATOR is |
10149 | located in. `grokdeclarator' sometimes changes the scope, so | |
10150 | we compute it now. */ | |
10151 | scope = get_scope_of_declarator (declarator); | |
10152 | ||
10153 | /* If we're allowing GNU extensions, look for an asm-specification | |
10154 | and attributes. */ | |
10155 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10156 | { | |
10157 | /* Look for an asm-specification. */ | |
10158 | asm_specification = cp_parser_asm_specification_opt (parser); | |
10159 | /* And attributes. */ | |
10160 | attributes = cp_parser_attributes_opt (parser); | |
10161 | } | |
10162 | else | |
10163 | { | |
10164 | asm_specification = NULL_TREE; | |
10165 | attributes = NULL_TREE; | |
10166 | } | |
10167 | ||
10168 | /* Peek at the next token. */ | |
10169 | token = cp_lexer_peek_token (parser->lexer); | |
10170 | /* Check to see if the token indicates the start of a | |
10171 | function-definition. */ | |
10172 | if (cp_parser_token_starts_function_definition_p (token)) | |
10173 | { | |
10174 | if (!function_definition_allowed_p) | |
10175 | { | |
10176 | /* If a function-definition should not appear here, issue an | |
10177 | error message. */ | |
10178 | cp_parser_error (parser, | |
10179 | "a function-definition is not allowed here"); | |
10180 | return error_mark_node; | |
10181 | } | |
10182 | else | |
10183 | { | |
a723baf1 MM |
10184 | /* Neither attributes nor an asm-specification are allowed |
10185 | on a function-definition. */ | |
10186 | if (asm_specification) | |
10187 | error ("an asm-specification is not allowed on a function-definition"); | |
10188 | if (attributes) | |
10189 | error ("attributes are not allowed on a function-definition"); | |
10190 | /* This is a function-definition. */ | |
10191 | *function_definition_p = true; | |
10192 | ||
a723baf1 | 10193 | /* Parse the function definition. */ |
4bb8ca28 MM |
10194 | if (member_p) |
10195 | decl = cp_parser_save_member_function_body (parser, | |
10196 | decl_specifiers, | |
10197 | declarator, | |
10198 | prefix_attributes); | |
10199 | else | |
21526606 | 10200 | decl |
4bb8ca28 MM |
10201 | = (cp_parser_function_definition_from_specifiers_and_declarator |
10202 | (parser, decl_specifiers, prefix_attributes, declarator)); | |
24c0ef37 | 10203 | |
a723baf1 MM |
10204 | return decl; |
10205 | } | |
10206 | } | |
10207 | ||
10208 | /* [dcl.dcl] | |
10209 | ||
10210 | Only in function declarations for constructors, destructors, and | |
21526606 | 10211 | type conversions can the decl-specifier-seq be omitted. |
a723baf1 MM |
10212 | |
10213 | We explicitly postpone this check past the point where we handle | |
10214 | function-definitions because we tolerate function-definitions | |
10215 | that are missing their return types in some modes. */ | |
7efa3e22 | 10216 | if (!decl_specifiers && ctor_dtor_or_conv_p <= 0) |
a723baf1 | 10217 | { |
21526606 | 10218 | cp_parser_error (parser, |
a723baf1 MM |
10219 | "expected constructor, destructor, or type conversion"); |
10220 | return error_mark_node; | |
10221 | } | |
10222 | ||
10223 | /* An `=' or an `(' indicates an initializer. */ | |
21526606 | 10224 | is_initialized = (token->type == CPP_EQ |
a723baf1 MM |
10225 | || token->type == CPP_OPEN_PAREN); |
10226 | /* If the init-declarator isn't initialized and isn't followed by a | |
10227 | `,' or `;', it's not a valid init-declarator. */ | |
21526606 | 10228 | if (!is_initialized |
a723baf1 MM |
10229 | && token->type != CPP_COMMA |
10230 | && token->type != CPP_SEMICOLON) | |
10231 | { | |
10232 | cp_parser_error (parser, "expected init-declarator"); | |
10233 | return error_mark_node; | |
10234 | } | |
10235 | ||
10236 | /* Because start_decl has side-effects, we should only call it if we | |
10237 | know we're going ahead. By this point, we know that we cannot | |
10238 | possibly be looking at any other construct. */ | |
10239 | cp_parser_commit_to_tentative_parse (parser); | |
10240 | ||
e90c7b84 ILT |
10241 | /* If the decl specifiers were bad, issue an error now that we're |
10242 | sure this was intended to be a declarator. Then continue | |
10243 | declaring the variable(s), as int, to try to cut down on further | |
10244 | errors. */ | |
10245 | if (decl_specifiers != NULL | |
10246 | && TREE_VALUE (decl_specifiers) == error_mark_node) | |
10247 | { | |
10248 | cp_parser_error (parser, "invalid type in declaration"); | |
10249 | TREE_VALUE (decl_specifiers) = integer_type_node; | |
10250 | } | |
10251 | ||
a723baf1 MM |
10252 | /* Check to see whether or not this declaration is a friend. */ |
10253 | friend_p = cp_parser_friend_p (decl_specifiers); | |
10254 | ||
10255 | /* Check that the number of template-parameter-lists is OK. */ | |
ee3071ef | 10256 | if (!cp_parser_check_declarator_template_parameters (parser, declarator)) |
cf22909c | 10257 | return error_mark_node; |
a723baf1 MM |
10258 | |
10259 | /* Enter the newly declared entry in the symbol table. If we're | |
10260 | processing a declaration in a class-specifier, we wait until | |
10261 | after processing the initializer. */ | |
10262 | if (!member_p) | |
10263 | { | |
10264 | if (parser->in_unbraced_linkage_specification_p) | |
10265 | { | |
10266 | decl_specifiers = tree_cons (error_mark_node, | |
10267 | get_identifier ("extern"), | |
10268 | decl_specifiers); | |
10269 | have_extern_spec = false; | |
10270 | } | |
ee3071ef NS |
10271 | decl = start_decl (declarator, decl_specifiers, |
10272 | is_initialized, attributes, prefix_attributes); | |
a723baf1 MM |
10273 | } |
10274 | ||
10275 | /* Enter the SCOPE. That way unqualified names appearing in the | |
10276 | initializer will be looked up in SCOPE. */ | |
10277 | if (scope) | |
91b004e5 | 10278 | pop_p = push_scope (scope); |
a723baf1 MM |
10279 | |
10280 | /* Perform deferred access control checks, now that we know in which | |
10281 | SCOPE the declared entity resides. */ | |
21526606 | 10282 | if (!member_p && decl) |
a723baf1 MM |
10283 | { |
10284 | tree saved_current_function_decl = NULL_TREE; | |
10285 | ||
10286 | /* If the entity being declared is a function, pretend that we | |
10287 | are in its scope. If it is a `friend', it may have access to | |
9bcb9aae | 10288 | things that would not otherwise be accessible. */ |
a723baf1 MM |
10289 | if (TREE_CODE (decl) == FUNCTION_DECL) |
10290 | { | |
10291 | saved_current_function_decl = current_function_decl; | |
10292 | current_function_decl = decl; | |
10293 | } | |
21526606 | 10294 | |
cf22909c KL |
10295 | /* Perform the access control checks for the declarator and the |
10296 | the decl-specifiers. */ | |
10297 | perform_deferred_access_checks (); | |
a723baf1 MM |
10298 | |
10299 | /* Restore the saved value. */ | |
10300 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10301 | current_function_decl = saved_current_function_decl; | |
10302 | } | |
10303 | ||
10304 | /* Parse the initializer. */ | |
10305 | if (is_initialized) | |
21526606 | 10306 | initializer = cp_parser_initializer (parser, |
39703eb9 MM |
10307 | &is_parenthesized_init, |
10308 | &is_non_constant_init); | |
a723baf1 MM |
10309 | else |
10310 | { | |
10311 | initializer = NULL_TREE; | |
10312 | is_parenthesized_init = false; | |
39703eb9 | 10313 | is_non_constant_init = true; |
a723baf1 MM |
10314 | } |
10315 | ||
10316 | /* The old parser allows attributes to appear after a parenthesized | |
10317 | initializer. Mark Mitchell proposed removing this functionality | |
10318 | on the GCC mailing lists on 2002-08-13. This parser accepts the | |
10319 | attributes -- but ignores them. */ | |
10320 | if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init) | |
10321 | if (cp_parser_attributes_opt (parser)) | |
10322 | warning ("attributes after parenthesized initializer ignored"); | |
10323 | ||
10324 | /* Leave the SCOPE, now that we have processed the initializer. It | |
10325 | is important to do this before calling cp_finish_decl because it | |
10326 | makes decisions about whether to create DECL_STMTs or not based | |
10327 | on the current scope. */ | |
91b004e5 | 10328 | if (pop_p) |
a723baf1 MM |
10329 | pop_scope (scope); |
10330 | ||
10331 | /* For an in-class declaration, use `grokfield' to create the | |
10332 | declaration. */ | |
10333 | if (member_p) | |
8db1028e NS |
10334 | { |
10335 | decl = grokfield (declarator, decl_specifiers, | |
10336 | initializer, /*asmspec=*/NULL_TREE, | |
a723baf1 | 10337 | /*attributes=*/NULL_TREE); |
8db1028e NS |
10338 | if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
10339 | cp_parser_save_default_args (parser, decl); | |
10340 | } | |
21526606 | 10341 | |
a723baf1 MM |
10342 | /* Finish processing the declaration. But, skip friend |
10343 | declarations. */ | |
10344 | if (!friend_p && decl) | |
21526606 EC |
10345 | cp_finish_decl (decl, |
10346 | initializer, | |
a723baf1 MM |
10347 | asm_specification, |
10348 | /* If the initializer is in parentheses, then this is | |
10349 | a direct-initialization, which means that an | |
10350 | `explicit' constructor is OK. Otherwise, an | |
10351 | `explicit' constructor cannot be used. */ | |
10352 | ((is_parenthesized_init || !is_initialized) | |
10353 | ? 0 : LOOKUP_ONLYCONVERTING)); | |
10354 | ||
39703eb9 MM |
10355 | /* Remember whether or not variables were initialized by |
10356 | constant-expressions. */ | |
21526606 | 10357 | if (decl && TREE_CODE (decl) == VAR_DECL |
39703eb9 MM |
10358 | && is_initialized && !is_non_constant_init) |
10359 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; | |
10360 | ||
a723baf1 MM |
10361 | return decl; |
10362 | } | |
10363 | ||
10364 | /* Parse a declarator. | |
21526606 | 10365 | |
a723baf1 MM |
10366 | declarator: |
10367 | direct-declarator | |
21526606 | 10368 | ptr-operator declarator |
a723baf1 MM |
10369 | |
10370 | abstract-declarator: | |
10371 | ptr-operator abstract-declarator [opt] | |
10372 | direct-abstract-declarator | |
10373 | ||
10374 | GNU Extensions: | |
10375 | ||
10376 | declarator: | |
10377 | attributes [opt] direct-declarator | |
21526606 | 10378 | attributes [opt] ptr-operator declarator |
a723baf1 MM |
10379 | |
10380 | abstract-declarator: | |
10381 | attributes [opt] ptr-operator abstract-declarator [opt] | |
10382 | attributes [opt] direct-abstract-declarator | |
21526606 | 10383 | |
a723baf1 MM |
10384 | Returns a representation of the declarator. If the declarator has |
10385 | the form `* declarator', then an INDIRECT_REF is returned, whose | |
34cd5ae7 | 10386 | only operand is the sub-declarator. Analogously, `& declarator' is |
a723baf1 MM |
10387 | represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is |
10388 | used. The first operand is the TYPE for `X'. The second operand | |
10389 | is an INDIRECT_REF whose operand is the sub-declarator. | |
10390 | ||
34cd5ae7 | 10391 | Otherwise, the representation is as for a direct-declarator. |
a723baf1 MM |
10392 | |
10393 | (It would be better to define a structure type to represent | |
10394 | declarators, rather than abusing `tree' nodes to represent | |
10395 | declarators. That would be much clearer and save some memory. | |
10396 | There is no reason for declarators to be garbage-collected, for | |
10397 | example; they are created during parser and no longer needed after | |
10398 | `grokdeclarator' has been called.) | |
10399 | ||
10400 | For a ptr-operator that has the optional cv-qualifier-seq, | |
10401 | cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF | |
10402 | node. | |
10403 | ||
7efa3e22 NS |
10404 | If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to |
10405 | detect constructor, destructor or conversion operators. It is set | |
10406 | to -1 if the declarator is a name, and +1 if it is a | |
10407 | function. Otherwise it is set to zero. Usually you just want to | |
10408 | test for >0, but internally the negative value is used. | |
21526606 | 10409 | |
a723baf1 MM |
10410 | (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have |
10411 | a decl-specifier-seq unless it declares a constructor, destructor, | |
10412 | or conversion. It might seem that we could check this condition in | |
10413 | semantic analysis, rather than parsing, but that makes it difficult | |
10414 | to handle something like `f()'. We want to notice that there are | |
10415 | no decl-specifiers, and therefore realize that this is an | |
21526606 EC |
10416 | expression, not a declaration.) |
10417 | ||
4bb8ca28 MM |
10418 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff |
10419 | the declarator is a direct-declarator of the form "(...)". */ | |
a723baf1 MM |
10420 | |
10421 | static tree | |
21526606 EC |
10422 | cp_parser_declarator (cp_parser* parser, |
10423 | cp_parser_declarator_kind dcl_kind, | |
4bb8ca28 MM |
10424 | int* ctor_dtor_or_conv_p, |
10425 | bool* parenthesized_p) | |
a723baf1 MM |
10426 | { |
10427 | cp_token *token; | |
10428 | tree declarator; | |
10429 | enum tree_code code; | |
10430 | tree cv_qualifier_seq; | |
10431 | tree class_type; | |
10432 | tree attributes = NULL_TREE; | |
10433 | ||
10434 | /* Assume this is not a constructor, destructor, or type-conversion | |
10435 | operator. */ | |
10436 | if (ctor_dtor_or_conv_p) | |
7efa3e22 | 10437 | *ctor_dtor_or_conv_p = 0; |
a723baf1 MM |
10438 | |
10439 | if (cp_parser_allow_gnu_extensions_p (parser)) | |
10440 | attributes = cp_parser_attributes_opt (parser); | |
21526606 | 10441 | |
a723baf1 MM |
10442 | /* Peek at the next token. */ |
10443 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 10444 | |
a723baf1 MM |
10445 | /* Check for the ptr-operator production. */ |
10446 | cp_parser_parse_tentatively (parser); | |
10447 | /* Parse the ptr-operator. */ | |
21526606 EC |
10448 | code = cp_parser_ptr_operator (parser, |
10449 | &class_type, | |
a723baf1 MM |
10450 | &cv_qualifier_seq); |
10451 | /* If that worked, then we have a ptr-operator. */ | |
10452 | if (cp_parser_parse_definitely (parser)) | |
10453 | { | |
4bb8ca28 MM |
10454 | /* If a ptr-operator was found, then this declarator was not |
10455 | parenthesized. */ | |
10456 | if (parenthesized_p) | |
10457 | *parenthesized_p = true; | |
a723baf1 MM |
10458 | /* The dependent declarator is optional if we are parsing an |
10459 | abstract-declarator. */ | |
62b8a44e | 10460 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
a723baf1 MM |
10461 | cp_parser_parse_tentatively (parser); |
10462 | ||
10463 | /* Parse the dependent declarator. */ | |
62b8a44e | 10464 | declarator = cp_parser_declarator (parser, dcl_kind, |
4bb8ca28 MM |
10465 | /*ctor_dtor_or_conv_p=*/NULL, |
10466 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
10467 | |
10468 | /* If we are parsing an abstract-declarator, we must handle the | |
10469 | case where the dependent declarator is absent. */ | |
62b8a44e NS |
10470 | if (dcl_kind != CP_PARSER_DECLARATOR_NAMED |
10471 | && !cp_parser_parse_definitely (parser)) | |
a723baf1 | 10472 | declarator = NULL_TREE; |
21526606 | 10473 | |
a723baf1 MM |
10474 | /* Build the representation of the ptr-operator. */ |
10475 | if (code == INDIRECT_REF) | |
21526606 | 10476 | declarator = make_pointer_declarator (cv_qualifier_seq, |
a723baf1 MM |
10477 | declarator); |
10478 | else | |
10479 | declarator = make_reference_declarator (cv_qualifier_seq, | |
10480 | declarator); | |
10481 | /* Handle the pointer-to-member case. */ | |
10482 | if (class_type) | |
10483 | declarator = build_nt (SCOPE_REF, class_type, declarator); | |
10484 | } | |
10485 | /* Everything else is a direct-declarator. */ | |
10486 | else | |
4bb8ca28 MM |
10487 | { |
10488 | if (parenthesized_p) | |
10489 | *parenthesized_p = cp_lexer_next_token_is (parser->lexer, | |
10490 | CPP_OPEN_PAREN); | |
10491 | declarator = cp_parser_direct_declarator (parser, dcl_kind, | |
10492 | ctor_dtor_or_conv_p); | |
10493 | } | |
a723baf1 MM |
10494 | |
10495 | if (attributes && declarator != error_mark_node) | |
10496 | declarator = tree_cons (attributes, declarator, NULL_TREE); | |
21526606 | 10497 | |
a723baf1 MM |
10498 | return declarator; |
10499 | } | |
10500 | ||
10501 | /* Parse a direct-declarator or direct-abstract-declarator. | |
10502 | ||
10503 | direct-declarator: | |
10504 | declarator-id | |
10505 | direct-declarator ( parameter-declaration-clause ) | |
21526606 | 10506 | cv-qualifier-seq [opt] |
a723baf1 MM |
10507 | exception-specification [opt] |
10508 | direct-declarator [ constant-expression [opt] ] | |
21526606 | 10509 | ( declarator ) |
a723baf1 MM |
10510 | |
10511 | direct-abstract-declarator: | |
10512 | direct-abstract-declarator [opt] | |
21526606 | 10513 | ( parameter-declaration-clause ) |
a723baf1 MM |
10514 | cv-qualifier-seq [opt] |
10515 | exception-specification [opt] | |
10516 | direct-abstract-declarator [opt] [ constant-expression [opt] ] | |
10517 | ( abstract-declarator ) | |
10518 | ||
62b8a44e NS |
10519 | Returns a representation of the declarator. DCL_KIND is |
10520 | CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a | |
10521 | direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if | |
10522 | we are parsing a direct-declarator. It is | |
10523 | CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case | |
10524 | of ambiguity we prefer an abstract declarator, as per | |
10525 | [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for | |
a723baf1 MM |
10526 | cp_parser_declarator. |
10527 | ||
10528 | For the declarator-id production, the representation is as for an | |
10529 | id-expression, except that a qualified name is represented as a | |
10530 | SCOPE_REF. A function-declarator is represented as a CALL_EXPR; | |
10531 | see the documentation of the FUNCTION_DECLARATOR_* macros for | |
10532 | information about how to find the various declarator components. | |
10533 | An array-declarator is represented as an ARRAY_REF. The | |
10534 | direct-declarator is the first operand; the constant-expression | |
10535 | indicating the size of the array is the second operand. */ | |
10536 | ||
10537 | static tree | |
94edc4ab NN |
10538 | cp_parser_direct_declarator (cp_parser* parser, |
10539 | cp_parser_declarator_kind dcl_kind, | |
7efa3e22 | 10540 | int* ctor_dtor_or_conv_p) |
a723baf1 MM |
10541 | { |
10542 | cp_token *token; | |
62b8a44e | 10543 | tree declarator = NULL_TREE; |
a723baf1 MM |
10544 | tree scope = NULL_TREE; |
10545 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
10546 | bool saved_in_declarator_p = parser->in_declarator_p; | |
62b8a44e | 10547 | bool first = true; |
91b004e5 | 10548 | bool pop_p = false; |
21526606 | 10549 | |
62b8a44e | 10550 | while (true) |
a723baf1 | 10551 | { |
62b8a44e NS |
10552 | /* Peek at the next token. */ |
10553 | token = cp_lexer_peek_token (parser->lexer); | |
10554 | if (token->type == CPP_OPEN_PAREN) | |
a723baf1 | 10555 | { |
62b8a44e NS |
10556 | /* This is either a parameter-declaration-clause, or a |
10557 | parenthesized declarator. When we know we are parsing a | |
34cd5ae7 | 10558 | named declarator, it must be a parenthesized declarator |
62b8a44e NS |
10559 | if FIRST is true. For instance, `(int)' is a |
10560 | parameter-declaration-clause, with an omitted | |
10561 | direct-abstract-declarator. But `((*))', is a | |
10562 | parenthesized abstract declarator. Finally, when T is a | |
10563 | template parameter `(T)' is a | |
34cd5ae7 | 10564 | parameter-declaration-clause, and not a parenthesized |
62b8a44e | 10565 | named declarator. |
21526606 | 10566 | |
62b8a44e NS |
10567 | We first try and parse a parameter-declaration-clause, |
10568 | and then try a nested declarator (if FIRST is true). | |
a723baf1 | 10569 | |
62b8a44e NS |
10570 | It is not an error for it not to be a |
10571 | parameter-declaration-clause, even when FIRST is | |
10572 | false. Consider, | |
10573 | ||
10574 | int i (int); | |
10575 | int i (3); | |
10576 | ||
10577 | The first is the declaration of a function while the | |
10578 | second is a the definition of a variable, including its | |
10579 | initializer. | |
10580 | ||
10581 | Having seen only the parenthesis, we cannot know which of | |
10582 | these two alternatives should be selected. Even more | |
10583 | complex are examples like: | |
10584 | ||
10585 | int i (int (a)); | |
10586 | int i (int (3)); | |
10587 | ||
10588 | The former is a function-declaration; the latter is a | |
21526606 | 10589 | variable initialization. |
62b8a44e | 10590 | |
34cd5ae7 | 10591 | Thus again, we try a parameter-declaration-clause, and if |
62b8a44e NS |
10592 | that fails, we back out and return. */ |
10593 | ||
10594 | if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) | |
a723baf1 | 10595 | { |
62b8a44e | 10596 | tree params; |
4047b164 | 10597 | unsigned saved_num_template_parameter_lists; |
21526606 | 10598 | |
62b8a44e | 10599 | cp_parser_parse_tentatively (parser); |
a723baf1 | 10600 | |
62b8a44e NS |
10601 | /* Consume the `('. */ |
10602 | cp_lexer_consume_token (parser->lexer); | |
10603 | if (first) | |
10604 | { | |
10605 | /* If this is going to be an abstract declarator, we're | |
10606 | in a declarator and we can't have default args. */ | |
10607 | parser->default_arg_ok_p = false; | |
10608 | parser->in_declarator_p = true; | |
10609 | } | |
21526606 | 10610 | |
4047b164 KL |
10611 | /* Inside the function parameter list, surrounding |
10612 | template-parameter-lists do not apply. */ | |
10613 | saved_num_template_parameter_lists | |
10614 | = parser->num_template_parameter_lists; | |
10615 | parser->num_template_parameter_lists = 0; | |
10616 | ||
62b8a44e NS |
10617 | /* Parse the parameter-declaration-clause. */ |
10618 | params = cp_parser_parameter_declaration_clause (parser); | |
10619 | ||
4047b164 KL |
10620 | parser->num_template_parameter_lists |
10621 | = saved_num_template_parameter_lists; | |
10622 | ||
62b8a44e | 10623 | /* If all went well, parse the cv-qualifier-seq and the |
34cd5ae7 | 10624 | exception-specification. */ |
62b8a44e NS |
10625 | if (cp_parser_parse_definitely (parser)) |
10626 | { | |
10627 | tree cv_qualifiers; | |
10628 | tree exception_specification; | |
7efa3e22 NS |
10629 | |
10630 | if (ctor_dtor_or_conv_p) | |
10631 | *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; | |
62b8a44e NS |
10632 | first = false; |
10633 | /* Consume the `)'. */ | |
10634 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
10635 | ||
10636 | /* Parse the cv-qualifier-seq. */ | |
10637 | cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser); | |
10638 | /* And the exception-specification. */ | |
21526606 | 10639 | exception_specification |
62b8a44e NS |
10640 | = cp_parser_exception_specification_opt (parser); |
10641 | ||
10642 | /* Create the function-declarator. */ | |
10643 | declarator = make_call_declarator (declarator, | |
10644 | params, | |
10645 | cv_qualifiers, | |
10646 | exception_specification); | |
10647 | /* Any subsequent parameter lists are to do with | |
10648 | return type, so are not those of the declared | |
10649 | function. */ | |
10650 | parser->default_arg_ok_p = false; | |
21526606 | 10651 | |
62b8a44e NS |
10652 | /* Repeat the main loop. */ |
10653 | continue; | |
10654 | } | |
10655 | } | |
21526606 | 10656 | |
62b8a44e NS |
10657 | /* If this is the first, we can try a parenthesized |
10658 | declarator. */ | |
10659 | if (first) | |
a723baf1 | 10660 | { |
a7324e75 MM |
10661 | bool saved_in_type_id_in_expr_p; |
10662 | ||
a723baf1 | 10663 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
62b8a44e | 10664 | parser->in_declarator_p = saved_in_declarator_p; |
21526606 | 10665 | |
62b8a44e NS |
10666 | /* Consume the `('. */ |
10667 | cp_lexer_consume_token (parser->lexer); | |
10668 | /* Parse the nested declarator. */ | |
a7324e75 MM |
10669 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
10670 | parser->in_type_id_in_expr_p = true; | |
21526606 | 10671 | declarator |
4bb8ca28 MM |
10672 | = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, |
10673 | /*parenthesized_p=*/NULL); | |
a7324e75 | 10674 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
62b8a44e NS |
10675 | first = false; |
10676 | /* Expect a `)'. */ | |
10677 | if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'")) | |
10678 | declarator = error_mark_node; | |
10679 | if (declarator == error_mark_node) | |
10680 | break; | |
21526606 | 10681 | |
62b8a44e | 10682 | goto handle_declarator; |
a723baf1 | 10683 | } |
9bcb9aae | 10684 | /* Otherwise, we must be done. */ |
62b8a44e NS |
10685 | else |
10686 | break; | |
a723baf1 | 10687 | } |
62b8a44e NS |
10688 | else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) |
10689 | && token->type == CPP_OPEN_SQUARE) | |
a723baf1 | 10690 | { |
62b8a44e | 10691 | /* Parse an array-declarator. */ |
a723baf1 MM |
10692 | tree bounds; |
10693 | ||
7efa3e22 NS |
10694 | if (ctor_dtor_or_conv_p) |
10695 | *ctor_dtor_or_conv_p = 0; | |
21526606 | 10696 | |
62b8a44e NS |
10697 | first = false; |
10698 | parser->default_arg_ok_p = false; | |
10699 | parser->in_declarator_p = true; | |
a723baf1 MM |
10700 | /* Consume the `['. */ |
10701 | cp_lexer_consume_token (parser->lexer); | |
10702 | /* Peek at the next token. */ | |
10703 | token = cp_lexer_peek_token (parser->lexer); | |
10704 | /* If the next token is `]', then there is no | |
10705 | constant-expression. */ | |
10706 | if (token->type != CPP_CLOSE_SQUARE) | |
14d22dd6 MM |
10707 | { |
10708 | bool non_constant_p; | |
10709 | ||
21526606 | 10710 | bounds |
14d22dd6 MM |
10711 | = cp_parser_constant_expression (parser, |
10712 | /*allow_non_constant=*/true, | |
10713 | &non_constant_p); | |
d17811fd | 10714 | if (!non_constant_p) |
9baa27a9 | 10715 | bounds = fold_non_dependent_expr (bounds); |
14d22dd6 | 10716 | } |
a723baf1 MM |
10717 | else |
10718 | bounds = NULL_TREE; | |
10719 | /* Look for the closing `]'. */ | |
62b8a44e NS |
10720 | if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'")) |
10721 | { | |
10722 | declarator = error_mark_node; | |
10723 | break; | |
10724 | } | |
a723baf1 MM |
10725 | |
10726 | declarator = build_nt (ARRAY_REF, declarator, bounds); | |
10727 | } | |
62b8a44e | 10728 | else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) |
a723baf1 | 10729 | { |
a668c6ad | 10730 | /* Parse a declarator-id */ |
62b8a44e NS |
10731 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10732 | cp_parser_parse_tentatively (parser); | |
10733 | declarator = cp_parser_declarator_id (parser); | |
712becab NS |
10734 | if (dcl_kind == CP_PARSER_DECLARATOR_EITHER) |
10735 | { | |
10736 | if (!cp_parser_parse_definitely (parser)) | |
10737 | declarator = error_mark_node; | |
10738 | else if (TREE_CODE (declarator) != IDENTIFIER_NODE) | |
10739 | { | |
10740 | cp_parser_error (parser, "expected unqualified-id"); | |
10741 | declarator = error_mark_node; | |
10742 | } | |
10743 | } | |
21526606 | 10744 | |
62b8a44e NS |
10745 | if (declarator == error_mark_node) |
10746 | break; | |
21526606 | 10747 | |
d9a50301 KL |
10748 | if (TREE_CODE (declarator) == SCOPE_REF |
10749 | && !current_scope ()) | |
62b8a44e NS |
10750 | { |
10751 | tree scope = TREE_OPERAND (declarator, 0); | |
712becab | 10752 | |
62b8a44e NS |
10753 | /* In the declaration of a member of a template class |
10754 | outside of the class itself, the SCOPE will sometimes | |
10755 | be a TYPENAME_TYPE. For example, given: | |
21526606 | 10756 | |
62b8a44e NS |
10757 | template <typename T> |
10758 | int S<T>::R::i = 3; | |
21526606 | 10759 | |
62b8a44e NS |
10760 | the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In |
10761 | this context, we must resolve S<T>::R to an ordinary | |
10762 | type, rather than a typename type. | |
21526606 | 10763 | |
62b8a44e NS |
10764 | The reason we normally avoid resolving TYPENAME_TYPEs |
10765 | is that a specialization of `S' might render | |
10766 | `S<T>::R' not a type. However, if `S' is | |
10767 | specialized, then this `i' will not be used, so there | |
10768 | is no harm in resolving the types here. */ | |
10769 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
10770 | { | |
14d22dd6 MM |
10771 | tree type; |
10772 | ||
62b8a44e | 10773 | /* Resolve the TYPENAME_TYPE. */ |
14d22dd6 MM |
10774 | type = resolve_typename_type (scope, |
10775 | /*only_current_p=*/false); | |
62b8a44e | 10776 | /* If that failed, the declarator is invalid. */ |
109e0040 MM |
10777 | if (type == error_mark_node) |
10778 | error ("`%T::%D' is not a type", | |
10779 | TYPE_CONTEXT (scope), | |
10780 | TYPE_IDENTIFIER (scope)); | |
62b8a44e | 10781 | /* Build a new DECLARATOR. */ |
21526606 | 10782 | declarator = build_nt (SCOPE_REF, |
afeebbc0 | 10783 | type, |
62b8a44e NS |
10784 | TREE_OPERAND (declarator, 1)); |
10785 | } | |
10786 | } | |
21526606 EC |
10787 | |
10788 | /* Check to see whether the declarator-id names a constructor, | |
62b8a44e | 10789 | destructor, or conversion. */ |
21526606 EC |
10790 | if (declarator && ctor_dtor_or_conv_p |
10791 | && ((TREE_CODE (declarator) == SCOPE_REF | |
62b8a44e NS |
10792 | && CLASS_TYPE_P (TREE_OPERAND (declarator, 0))) |
10793 | || (TREE_CODE (declarator) != SCOPE_REF | |
10794 | && at_class_scope_p ()))) | |
a723baf1 | 10795 | { |
62b8a44e NS |
10796 | tree unqualified_name; |
10797 | tree class_type; | |
10798 | ||
10799 | /* Get the unqualified part of the name. */ | |
10800 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10801 | { | |
10802 | class_type = TREE_OPERAND (declarator, 0); | |
10803 | unqualified_name = TREE_OPERAND (declarator, 1); | |
10804 | } | |
10805 | else | |
10806 | { | |
10807 | class_type = current_class_type; | |
10808 | unqualified_name = declarator; | |
10809 | } | |
10810 | ||
10811 | /* See if it names ctor, dtor or conv. */ | |
10812 | if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR | |
10813 | || IDENTIFIER_TYPENAME_P (unqualified_name) | |
ab73670a MM |
10814 | || constructor_name_p (unqualified_name, class_type) |
10815 | || (TREE_CODE (unqualified_name) == TYPE_DECL | |
10816 | && same_type_p (TREE_TYPE (unqualified_name), | |
10817 | class_type))) | |
7efa3e22 | 10818 | *ctor_dtor_or_conv_p = -1; |
9221325f GB |
10819 | if (TREE_CODE (declarator) == SCOPE_REF |
10820 | && TREE_CODE (unqualified_name) == TYPE_DECL | |
10821 | && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name))) | |
10822 | { | |
10823 | error ("invalid use of constructor as a template"); | |
10824 | inform ("use `%T::%D' instead of `%T::%T' to name the " | |
10825 | "constructor in a qualified name", class_type, | |
10826 | DECL_NAME (TYPE_TI_TEMPLATE (class_type)), | |
10827 | class_type, class_type); | |
10828 | } | |
a723baf1 | 10829 | } |
62b8a44e NS |
10830 | |
10831 | handle_declarator:; | |
10832 | scope = get_scope_of_declarator (declarator); | |
10833 | if (scope) | |
91b004e5 MM |
10834 | /* Any names that appear after the declarator-id for a |
10835 | member are looked up in the containing scope. */ | |
10836 | pop_p = push_scope (scope); | |
62b8a44e NS |
10837 | parser->in_declarator_p = true; |
10838 | if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) | |
10839 | || (declarator | |
10840 | && (TREE_CODE (declarator) == SCOPE_REF | |
10841 | || TREE_CODE (declarator) == IDENTIFIER_NODE))) | |
10842 | /* Default args are only allowed on function | |
10843 | declarations. */ | |
10844 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
a723baf1 | 10845 | else |
62b8a44e NS |
10846 | parser->default_arg_ok_p = false; |
10847 | ||
10848 | first = false; | |
a723baf1 | 10849 | } |
62b8a44e | 10850 | /* We're done. */ |
a723baf1 MM |
10851 | else |
10852 | break; | |
a723baf1 MM |
10853 | } |
10854 | ||
10855 | /* For an abstract declarator, we might wind up with nothing at this | |
10856 | point. That's an error; the declarator is not optional. */ | |
10857 | if (!declarator) | |
10858 | cp_parser_error (parser, "expected declarator"); | |
10859 | ||
10860 | /* If we entered a scope, we must exit it now. */ | |
91b004e5 | 10861 | if (pop_p) |
a723baf1 MM |
10862 | pop_scope (scope); |
10863 | ||
10864 | parser->default_arg_ok_p = saved_default_arg_ok_p; | |
10865 | parser->in_declarator_p = saved_in_declarator_p; | |
21526606 | 10866 | |
a723baf1 MM |
10867 | return declarator; |
10868 | } | |
10869 | ||
21526606 | 10870 | /* Parse a ptr-operator. |
a723baf1 MM |
10871 | |
10872 | ptr-operator: | |
10873 | * cv-qualifier-seq [opt] | |
10874 | & | |
10875 | :: [opt] nested-name-specifier * cv-qualifier-seq [opt] | |
10876 | ||
10877 | GNU Extension: | |
10878 | ||
10879 | ptr-operator: | |
10880 | & cv-qualifier-seq [opt] | |
10881 | ||
10882 | Returns INDIRECT_REF if a pointer, or pointer-to-member, was | |
10883 | used. Returns ADDR_EXPR if a reference was used. In the | |
21526606 | 10884 | case of a pointer-to-member, *TYPE is filled in with the |
a723baf1 MM |
10885 | TYPE containing the member. *CV_QUALIFIER_SEQ is filled in |
10886 | with the cv-qualifier-seq, or NULL_TREE, if there are no | |
10887 | cv-qualifiers. Returns ERROR_MARK if an error occurred. */ | |
21526606 | 10888 | |
a723baf1 | 10889 | static enum tree_code |
21526606 EC |
10890 | cp_parser_ptr_operator (cp_parser* parser, |
10891 | tree* type, | |
94edc4ab | 10892 | tree* cv_qualifier_seq) |
a723baf1 MM |
10893 | { |
10894 | enum tree_code code = ERROR_MARK; | |
10895 | cp_token *token; | |
10896 | ||
10897 | /* Assume that it's not a pointer-to-member. */ | |
10898 | *type = NULL_TREE; | |
10899 | /* And that there are no cv-qualifiers. */ | |
10900 | *cv_qualifier_seq = NULL_TREE; | |
10901 | ||
10902 | /* Peek at the next token. */ | |
10903 | token = cp_lexer_peek_token (parser->lexer); | |
10904 | /* If it's a `*' or `&' we have a pointer or reference. */ | |
10905 | if (token->type == CPP_MULT || token->type == CPP_AND) | |
10906 | { | |
10907 | /* Remember which ptr-operator we were processing. */ | |
10908 | code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF); | |
10909 | ||
10910 | /* Consume the `*' or `&'. */ | |
10911 | cp_lexer_consume_token (parser->lexer); | |
10912 | ||
10913 | /* A `*' can be followed by a cv-qualifier-seq, and so can a | |
10914 | `&', if we are allowing GNU extensions. (The only qualifier | |
10915 | that can legally appear after `&' is `restrict', but that is | |
10916 | enforced during semantic analysis. */ | |
21526606 | 10917 | if (code == INDIRECT_REF |
a723baf1 MM |
10918 | || cp_parser_allow_gnu_extensions_p (parser)) |
10919 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10920 | } | |
10921 | else | |
10922 | { | |
10923 | /* Try the pointer-to-member case. */ | |
10924 | cp_parser_parse_tentatively (parser); | |
10925 | /* Look for the optional `::' operator. */ | |
10926 | cp_parser_global_scope_opt (parser, | |
10927 | /*current_scope_valid_p=*/false); | |
10928 | /* Look for the nested-name specifier. */ | |
10929 | cp_parser_nested_name_specifier (parser, | |
10930 | /*typename_keyword_p=*/false, | |
10931 | /*check_dependency_p=*/true, | |
a668c6ad MM |
10932 | /*type_p=*/false, |
10933 | /*is_declaration=*/false); | |
a723baf1 MM |
10934 | /* If we found it, and the next token is a `*', then we are |
10935 | indeed looking at a pointer-to-member operator. */ | |
10936 | if (!cp_parser_error_occurred (parser) | |
10937 | && cp_parser_require (parser, CPP_MULT, "`*'")) | |
10938 | { | |
10939 | /* The type of which the member is a member is given by the | |
10940 | current SCOPE. */ | |
10941 | *type = parser->scope; | |
10942 | /* The next name will not be qualified. */ | |
10943 | parser->scope = NULL_TREE; | |
10944 | parser->qualifying_scope = NULL_TREE; | |
10945 | parser->object_scope = NULL_TREE; | |
10946 | /* Indicate that the `*' operator was used. */ | |
10947 | code = INDIRECT_REF; | |
10948 | /* Look for the optional cv-qualifier-seq. */ | |
10949 | *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser); | |
10950 | } | |
10951 | /* If that didn't work we don't have a ptr-operator. */ | |
10952 | if (!cp_parser_parse_definitely (parser)) | |
10953 | cp_parser_error (parser, "expected ptr-operator"); | |
10954 | } | |
10955 | ||
10956 | return code; | |
10957 | } | |
10958 | ||
10959 | /* Parse an (optional) cv-qualifier-seq. | |
10960 | ||
10961 | cv-qualifier-seq: | |
21526606 | 10962 | cv-qualifier cv-qualifier-seq [opt] |
a723baf1 MM |
10963 | |
10964 | Returns a TREE_LIST. The TREE_VALUE of each node is the | |
10965 | representation of a cv-qualifier. */ | |
10966 | ||
10967 | static tree | |
94edc4ab | 10968 | cp_parser_cv_qualifier_seq_opt (cp_parser* parser) |
a723baf1 MM |
10969 | { |
10970 | tree cv_qualifiers = NULL_TREE; | |
21526606 | 10971 | |
a723baf1 MM |
10972 | while (true) |
10973 | { | |
10974 | tree cv_qualifier; | |
10975 | ||
10976 | /* Look for the next cv-qualifier. */ | |
10977 | cv_qualifier = cp_parser_cv_qualifier_opt (parser); | |
10978 | /* If we didn't find one, we're done. */ | |
10979 | if (!cv_qualifier) | |
10980 | break; | |
10981 | ||
10982 | /* Add this cv-qualifier to the list. */ | |
21526606 | 10983 | cv_qualifiers |
a723baf1 MM |
10984 | = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers); |
10985 | } | |
10986 | ||
10987 | /* We built up the list in reverse order. */ | |
10988 | return nreverse (cv_qualifiers); | |
10989 | } | |
10990 | ||
10991 | /* Parse an (optional) cv-qualifier. | |
10992 | ||
10993 | cv-qualifier: | |
10994 | const | |
21526606 | 10995 | volatile |
a723baf1 MM |
10996 | |
10997 | GNU Extension: | |
10998 | ||
10999 | cv-qualifier: | |
11000 | __restrict__ */ | |
11001 | ||
11002 | static tree | |
94edc4ab | 11003 | cp_parser_cv_qualifier_opt (cp_parser* parser) |
a723baf1 MM |
11004 | { |
11005 | cp_token *token; | |
11006 | tree cv_qualifier = NULL_TREE; | |
11007 | ||
11008 | /* Peek at the next token. */ | |
11009 | token = cp_lexer_peek_token (parser->lexer); | |
11010 | /* See if it's a cv-qualifier. */ | |
11011 | switch (token->keyword) | |
11012 | { | |
11013 | case RID_CONST: | |
11014 | case RID_VOLATILE: | |
11015 | case RID_RESTRICT: | |
11016 | /* Save the value of the token. */ | |
11017 | cv_qualifier = token->value; | |
11018 | /* Consume the token. */ | |
11019 | cp_lexer_consume_token (parser->lexer); | |
11020 | break; | |
11021 | ||
11022 | default: | |
11023 | break; | |
11024 | } | |
11025 | ||
11026 | return cv_qualifier; | |
11027 | } | |
11028 | ||
11029 | /* Parse a declarator-id. | |
11030 | ||
11031 | declarator-id: | |
11032 | id-expression | |
21526606 | 11033 | :: [opt] nested-name-specifier [opt] type-name |
a723baf1 MM |
11034 | |
11035 | In the `id-expression' case, the value returned is as for | |
11036 | cp_parser_id_expression if the id-expression was an unqualified-id. | |
11037 | If the id-expression was a qualified-id, then a SCOPE_REF is | |
11038 | returned. The first operand is the scope (either a NAMESPACE_DECL | |
11039 | or TREE_TYPE), but the second is still just a representation of an | |
11040 | unqualified-id. */ | |
11041 | ||
11042 | static tree | |
94edc4ab | 11043 | cp_parser_declarator_id (cp_parser* parser) |
a723baf1 MM |
11044 | { |
11045 | tree id_expression; | |
11046 | ||
11047 | /* The expression must be an id-expression. Assume that qualified | |
11048 | names are the names of types so that: | |
11049 | ||
11050 | template <class T> | |
11051 | int S<T>::R::i = 3; | |
11052 | ||
11053 | will work; we must treat `S<T>::R' as the name of a type. | |
11054 | Similarly, assume that qualified names are templates, where | |
11055 | required, so that: | |
11056 | ||
11057 | template <class T> | |
11058 | int S<T>::R<T>::i = 3; | |
11059 | ||
11060 | will work, too. */ | |
11061 | id_expression = cp_parser_id_expression (parser, | |
11062 | /*template_keyword_p=*/false, | |
11063 | /*check_dependency_p=*/false, | |
f3c2dfc6 MM |
11064 | /*template_p=*/NULL, |
11065 | /*declarator_p=*/true); | |
21526606 | 11066 | /* If the name was qualified, create a SCOPE_REF to represent |
a723baf1 MM |
11067 | that. */ |
11068 | if (parser->scope) | |
ec20aa6c MM |
11069 | { |
11070 | id_expression = build_nt (SCOPE_REF, parser->scope, id_expression); | |
11071 | parser->scope = NULL_TREE; | |
11072 | } | |
a723baf1 MM |
11073 | |
11074 | return id_expression; | |
11075 | } | |
11076 | ||
11077 | /* Parse a type-id. | |
11078 | ||
11079 | type-id: | |
11080 | type-specifier-seq abstract-declarator [opt] | |
11081 | ||
11082 | Returns the TYPE specified. */ | |
11083 | ||
11084 | static tree | |
94edc4ab | 11085 | cp_parser_type_id (cp_parser* parser) |
a723baf1 MM |
11086 | { |
11087 | tree type_specifier_seq; | |
11088 | tree abstract_declarator; | |
11089 | ||
11090 | /* Parse the type-specifier-seq. */ | |
21526606 | 11091 | type_specifier_seq |
a723baf1 MM |
11092 | = cp_parser_type_specifier_seq (parser); |
11093 | if (type_specifier_seq == error_mark_node) | |
11094 | return error_mark_node; | |
11095 | ||
11096 | /* There might or might not be an abstract declarator. */ | |
11097 | cp_parser_parse_tentatively (parser); | |
11098 | /* Look for the declarator. */ | |
21526606 | 11099 | abstract_declarator |
4bb8ca28 MM |
11100 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, |
11101 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
11102 | /* Check to see if there really was a declarator. */ |
11103 | if (!cp_parser_parse_definitely (parser)) | |
11104 | abstract_declarator = NULL_TREE; | |
11105 | ||
11106 | return groktypename (build_tree_list (type_specifier_seq, | |
11107 | abstract_declarator)); | |
11108 | } | |
11109 | ||
11110 | /* Parse a type-specifier-seq. | |
11111 | ||
11112 | type-specifier-seq: | |
11113 | type-specifier type-specifier-seq [opt] | |
11114 | ||
11115 | GNU extension: | |
11116 | ||
11117 | type-specifier-seq: | |
11118 | attributes type-specifier-seq [opt] | |
11119 | ||
11120 | Returns a TREE_LIST. Either the TREE_VALUE of each node is a | |
11121 | type-specifier, or the TREE_PURPOSE is a list of attributes. */ | |
11122 | ||
11123 | static tree | |
94edc4ab | 11124 | cp_parser_type_specifier_seq (cp_parser* parser) |
a723baf1 MM |
11125 | { |
11126 | bool seen_type_specifier = false; | |
11127 | tree type_specifier_seq = NULL_TREE; | |
11128 | ||
11129 | /* Parse the type-specifiers and attributes. */ | |
11130 | while (true) | |
11131 | { | |
11132 | tree type_specifier; | |
11133 | ||
11134 | /* Check for attributes first. */ | |
11135 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) | |
11136 | { | |
11137 | type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser), | |
11138 | NULL_TREE, | |
11139 | type_specifier_seq); | |
11140 | continue; | |
11141 | } | |
11142 | ||
11143 | /* After the first type-specifier, others are optional. */ | |
11144 | if (seen_type_specifier) | |
11145 | cp_parser_parse_tentatively (parser); | |
11146 | /* Look for the type-specifier. */ | |
21526606 | 11147 | type_specifier = cp_parser_type_specifier (parser, |
a723baf1 MM |
11148 | CP_PARSER_FLAGS_NONE, |
11149 | /*is_friend=*/false, | |
11150 | /*is_declaration=*/false, | |
11151 | NULL, | |
11152 | NULL); | |
11153 | /* If the first type-specifier could not be found, this is not a | |
11154 | type-specifier-seq at all. */ | |
11155 | if (!seen_type_specifier && type_specifier == error_mark_node) | |
11156 | return error_mark_node; | |
11157 | /* If subsequent type-specifiers could not be found, the | |
11158 | type-specifier-seq is complete. */ | |
11159 | else if (seen_type_specifier && !cp_parser_parse_definitely (parser)) | |
11160 | break; | |
11161 | ||
11162 | /* Add the new type-specifier to the list. */ | |
21526606 | 11163 | type_specifier_seq |
a723baf1 MM |
11164 | = tree_cons (NULL_TREE, type_specifier, type_specifier_seq); |
11165 | seen_type_specifier = true; | |
11166 | } | |
11167 | ||
11168 | /* We built up the list in reverse order. */ | |
11169 | return nreverse (type_specifier_seq); | |
11170 | } | |
11171 | ||
11172 | /* Parse a parameter-declaration-clause. | |
11173 | ||
11174 | parameter-declaration-clause: | |
11175 | parameter-declaration-list [opt] ... [opt] | |
11176 | parameter-declaration-list , ... | |
11177 | ||
11178 | Returns a representation for the parameter declarations. Each node | |
11179 | is a TREE_LIST. (See cp_parser_parameter_declaration for the exact | |
11180 | representation.) If the parameter-declaration-clause ends with an | |
11181 | ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the | |
11182 | list. A return value of NULL_TREE indicates a | |
11183 | parameter-declaration-clause consisting only of an ellipsis. */ | |
11184 | ||
11185 | static tree | |
94edc4ab | 11186 | cp_parser_parameter_declaration_clause (cp_parser* parser) |
a723baf1 MM |
11187 | { |
11188 | tree parameters; | |
11189 | cp_token *token; | |
11190 | bool ellipsis_p; | |
11191 | ||
11192 | /* Peek at the next token. */ | |
11193 | token = cp_lexer_peek_token (parser->lexer); | |
11194 | /* Check for trivial parameter-declaration-clauses. */ | |
11195 | if (token->type == CPP_ELLIPSIS) | |
11196 | { | |
11197 | /* Consume the `...' token. */ | |
11198 | cp_lexer_consume_token (parser->lexer); | |
11199 | return NULL_TREE; | |
11200 | } | |
11201 | else if (token->type == CPP_CLOSE_PAREN) | |
11202 | /* There are no parameters. */ | |
c73aecdf DE |
11203 | { |
11204 | #ifndef NO_IMPLICIT_EXTERN_C | |
11205 | if (in_system_header && current_class_type == NULL | |
11206 | && current_lang_name == lang_name_c) | |
11207 | return NULL_TREE; | |
11208 | else | |
11209 | #endif | |
11210 | return void_list_node; | |
11211 | } | |
a723baf1 MM |
11212 | /* Check for `(void)', too, which is a special case. */ |
11213 | else if (token->keyword == RID_VOID | |
21526606 | 11214 | && (cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
11215 | == CPP_CLOSE_PAREN)) |
11216 | { | |
11217 | /* Consume the `void' token. */ | |
11218 | cp_lexer_consume_token (parser->lexer); | |
11219 | /* There are no parameters. */ | |
11220 | return void_list_node; | |
11221 | } | |
21526606 | 11222 | |
a723baf1 MM |
11223 | /* Parse the parameter-declaration-list. */ |
11224 | parameters = cp_parser_parameter_declaration_list (parser); | |
11225 | /* If a parse error occurred while parsing the | |
11226 | parameter-declaration-list, then the entire | |
11227 | parameter-declaration-clause is erroneous. */ | |
11228 | if (parameters == error_mark_node) | |
11229 | return error_mark_node; | |
11230 | ||
11231 | /* Peek at the next token. */ | |
11232 | token = cp_lexer_peek_token (parser->lexer); | |
11233 | /* If it's a `,', the clause should terminate with an ellipsis. */ | |
11234 | if (token->type == CPP_COMMA) | |
11235 | { | |
11236 | /* Consume the `,'. */ | |
11237 | cp_lexer_consume_token (parser->lexer); | |
11238 | /* Expect an ellipsis. */ | |
21526606 | 11239 | ellipsis_p |
a723baf1 MM |
11240 | = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL); |
11241 | } | |
21526606 | 11242 | /* It might also be `...' if the optional trailing `,' was |
a723baf1 MM |
11243 | omitted. */ |
11244 | else if (token->type == CPP_ELLIPSIS) | |
11245 | { | |
11246 | /* Consume the `...' token. */ | |
11247 | cp_lexer_consume_token (parser->lexer); | |
11248 | /* And remember that we saw it. */ | |
11249 | ellipsis_p = true; | |
11250 | } | |
11251 | else | |
11252 | ellipsis_p = false; | |
11253 | ||
11254 | /* Finish the parameter list. */ | |
11255 | return finish_parmlist (parameters, ellipsis_p); | |
11256 | } | |
11257 | ||
11258 | /* Parse a parameter-declaration-list. | |
11259 | ||
11260 | parameter-declaration-list: | |
11261 | parameter-declaration | |
11262 | parameter-declaration-list , parameter-declaration | |
11263 | ||
11264 | Returns a representation of the parameter-declaration-list, as for | |
11265 | cp_parser_parameter_declaration_clause. However, the | |
11266 | `void_list_node' is never appended to the list. */ | |
11267 | ||
11268 | static tree | |
94edc4ab | 11269 | cp_parser_parameter_declaration_list (cp_parser* parser) |
a723baf1 MM |
11270 | { |
11271 | tree parameters = NULL_TREE; | |
11272 | ||
11273 | /* Look for more parameters. */ | |
11274 | while (true) | |
11275 | { | |
11276 | tree parameter; | |
4bb8ca28 | 11277 | bool parenthesized_p; |
a723baf1 | 11278 | /* Parse the parameter. */ |
21526606 EC |
11279 | parameter |
11280 | = cp_parser_parameter_declaration (parser, | |
4bb8ca28 MM |
11281 | /*template_parm_p=*/false, |
11282 | &parenthesized_p); | |
ec194454 | 11283 | |
34cd5ae7 | 11284 | /* If a parse error occurred parsing the parameter declaration, |
a723baf1 MM |
11285 | then the entire parameter-declaration-list is erroneous. */ |
11286 | if (parameter == error_mark_node) | |
11287 | { | |
11288 | parameters = error_mark_node; | |
11289 | break; | |
11290 | } | |
11291 | /* Add the new parameter to the list. */ | |
11292 | TREE_CHAIN (parameter) = parameters; | |
11293 | parameters = parameter; | |
11294 | ||
11295 | /* Peek at the next token. */ | |
11296 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) | |
11297 | || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
11298 | /* The parameter-declaration-list is complete. */ | |
11299 | break; | |
11300 | else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11301 | { | |
11302 | cp_token *token; | |
11303 | ||
11304 | /* Peek at the next token. */ | |
11305 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11306 | /* If it's an ellipsis, then the list is complete. */ | |
11307 | if (token->type == CPP_ELLIPSIS) | |
11308 | break; | |
11309 | /* Otherwise, there must be more parameters. Consume the | |
11310 | `,'. */ | |
11311 | cp_lexer_consume_token (parser->lexer); | |
4bb8ca28 MM |
11312 | /* When parsing something like: |
11313 | ||
11314 | int i(float f, double d) | |
21526606 | 11315 | |
4bb8ca28 MM |
11316 | we can tell after seeing the declaration for "f" that we |
11317 | are not looking at an initialization of a variable "i", | |
21526606 | 11318 | but rather at the declaration of a function "i". |
4bb8ca28 MM |
11319 | |
11320 | Due to the fact that the parsing of template arguments | |
11321 | (as specified to a template-id) requires backtracking we | |
11322 | cannot use this technique when inside a template argument | |
11323 | list. */ | |
11324 | if (!parser->in_template_argument_list_p | |
4d5fe289 | 11325 | && !parser->in_type_id_in_expr_p |
4bb8ca28 MM |
11326 | && cp_parser_parsing_tentatively (parser) |
11327 | && !cp_parser_committed_to_tentative_parse (parser) | |
11328 | /* However, a parameter-declaration of the form | |
11329 | "foat(f)" (which is a valid declaration of a | |
11330 | parameter "f") can also be interpreted as an | |
11331 | expression (the conversion of "f" to "float"). */ | |
11332 | && !parenthesized_p) | |
11333 | cp_parser_commit_to_tentative_parse (parser); | |
a723baf1 MM |
11334 | } |
11335 | else | |
11336 | { | |
11337 | cp_parser_error (parser, "expected `,' or `...'"); | |
4bb8ca28 MM |
11338 | if (!cp_parser_parsing_tentatively (parser) |
11339 | || cp_parser_committed_to_tentative_parse (parser)) | |
21526606 | 11340 | cp_parser_skip_to_closing_parenthesis (parser, |
4bb8ca28 | 11341 | /*recovering=*/true, |
5c832178 | 11342 | /*or_comma=*/false, |
4bb8ca28 | 11343 | /*consume_paren=*/false); |
a723baf1 MM |
11344 | break; |
11345 | } | |
11346 | } | |
11347 | ||
11348 | /* We built up the list in reverse order; straighten it out now. */ | |
11349 | return nreverse (parameters); | |
11350 | } | |
11351 | ||
11352 | /* Parse a parameter declaration. | |
11353 | ||
11354 | parameter-declaration: | |
11355 | decl-specifier-seq declarator | |
11356 | decl-specifier-seq declarator = assignment-expression | |
11357 | decl-specifier-seq abstract-declarator [opt] | |
11358 | decl-specifier-seq abstract-declarator [opt] = assignment-expression | |
11359 | ||
ec194454 MM |
11360 | If TEMPLATE_PARM_P is TRUE, then this parameter-declaration |
11361 | declares a template parameter. (In that case, a non-nested `>' | |
11362 | token encountered during the parsing of the assignment-expression | |
11363 | is not interpreted as a greater-than operator.) | |
a723baf1 MM |
11364 | |
11365 | Returns a TREE_LIST representing the parameter-declaration. The | |
4bb8ca28 MM |
11366 | TREE_PURPOSE is the default argument expression, or NULL_TREE if |
11367 | there is no default argument. The TREE_VALUE is a representation | |
11368 | of the decl-specifier-seq and declarator. In particular, the | |
11369 | TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the | |
11370 | decl-specifier-seq and whose TREE_VALUE represents the declarator. | |
11371 | If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff | |
11372 | the declarator is of the form "(p)". */ | |
a723baf1 MM |
11373 | |
11374 | static tree | |
21526606 | 11375 | cp_parser_parameter_declaration (cp_parser *parser, |
4bb8ca28 MM |
11376 | bool template_parm_p, |
11377 | bool *parenthesized_p) | |
a723baf1 | 11378 | { |
560ad596 | 11379 | int declares_class_or_enum; |
ec194454 | 11380 | bool greater_than_is_operator_p; |
a723baf1 MM |
11381 | tree decl_specifiers; |
11382 | tree attributes; | |
11383 | tree declarator; | |
11384 | tree default_argument; | |
11385 | tree parameter; | |
11386 | cp_token *token; | |
11387 | const char *saved_message; | |
11388 | ||
ec194454 MM |
11389 | /* In a template parameter, `>' is not an operator. |
11390 | ||
11391 | [temp.param] | |
11392 | ||
11393 | When parsing a default template-argument for a non-type | |
11394 | template-parameter, the first non-nested `>' is taken as the end | |
11395 | of the template parameter-list rather than a greater-than | |
11396 | operator. */ | |
11397 | greater_than_is_operator_p = !template_parm_p; | |
11398 | ||
a723baf1 MM |
11399 | /* Type definitions may not appear in parameter types. */ |
11400 | saved_message = parser->type_definition_forbidden_message; | |
21526606 | 11401 | parser->type_definition_forbidden_message |
a723baf1 MM |
11402 | = "types may not be defined in parameter types"; |
11403 | ||
11404 | /* Parse the declaration-specifiers. */ | |
21526606 | 11405 | decl_specifiers |
a723baf1 MM |
11406 | = cp_parser_decl_specifier_seq (parser, |
11407 | CP_PARSER_FLAGS_NONE, | |
11408 | &attributes, | |
11409 | &declares_class_or_enum); | |
11410 | /* If an error occurred, there's no reason to attempt to parse the | |
11411 | rest of the declaration. */ | |
11412 | if (cp_parser_error_occurred (parser)) | |
11413 | { | |
11414 | parser->type_definition_forbidden_message = saved_message; | |
11415 | return error_mark_node; | |
11416 | } | |
11417 | ||
11418 | /* Peek at the next token. */ | |
11419 | token = cp_lexer_peek_token (parser->lexer); | |
11420 | /* If the next token is a `)', `,', `=', `>', or `...', then there | |
11421 | is no declarator. */ | |
21526606 | 11422 | if (token->type == CPP_CLOSE_PAREN |
a723baf1 MM |
11423 | || token->type == CPP_COMMA |
11424 | || token->type == CPP_EQ | |
11425 | || token->type == CPP_ELLIPSIS | |
11426 | || token->type == CPP_GREATER) | |
4bb8ca28 MM |
11427 | { |
11428 | declarator = NULL_TREE; | |
11429 | if (parenthesized_p) | |
11430 | *parenthesized_p = false; | |
11431 | } | |
a723baf1 MM |
11432 | /* Otherwise, there should be a declarator. */ |
11433 | else | |
11434 | { | |
11435 | bool saved_default_arg_ok_p = parser->default_arg_ok_p; | |
11436 | parser->default_arg_ok_p = false; | |
21526606 | 11437 | |
5c832178 MM |
11438 | /* After seeing a decl-specifier-seq, if the next token is not a |
11439 | "(", there is no possibility that the code is a valid | |
4f8163b1 MM |
11440 | expression. Therefore, if parsing tentatively, we commit at |
11441 | this point. */ | |
5c832178 | 11442 | if (!parser->in_template_argument_list_p |
643aee72 | 11443 | /* In an expression context, having seen: |
4f8163b1 | 11444 | |
a7324e75 | 11445 | (int((char ... |
4f8163b1 MM |
11446 | |
11447 | we cannot be sure whether we are looking at a | |
a7324e75 MM |
11448 | function-type (taking a "char" as a parameter) or a cast |
11449 | of some object of type "char" to "int". */ | |
4f8163b1 | 11450 | && !parser->in_type_id_in_expr_p |
5c832178 MM |
11451 | && cp_parser_parsing_tentatively (parser) |
11452 | && !cp_parser_committed_to_tentative_parse (parser) | |
11453 | && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) | |
11454 | cp_parser_commit_to_tentative_parse (parser); | |
11455 | /* Parse the declarator. */ | |
a723baf1 | 11456 | declarator = cp_parser_declarator (parser, |
62b8a44e | 11457 | CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
11458 | /*ctor_dtor_or_conv_p=*/NULL, |
11459 | parenthesized_p); | |
a723baf1 | 11460 | parser->default_arg_ok_p = saved_default_arg_ok_p; |
4971227d MM |
11461 | /* After the declarator, allow more attributes. */ |
11462 | attributes = chainon (attributes, cp_parser_attributes_opt (parser)); | |
a723baf1 MM |
11463 | } |
11464 | ||
62b8a44e | 11465 | /* The restriction on defining new types applies only to the type |
a723baf1 MM |
11466 | of the parameter, not to the default argument. */ |
11467 | parser->type_definition_forbidden_message = saved_message; | |
11468 | ||
11469 | /* If the next token is `=', then process a default argument. */ | |
11470 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
11471 | { | |
11472 | bool saved_greater_than_is_operator_p; | |
11473 | /* Consume the `='. */ | |
11474 | cp_lexer_consume_token (parser->lexer); | |
11475 | ||
11476 | /* If we are defining a class, then the tokens that make up the | |
11477 | default argument must be saved and processed later. */ | |
21526606 | 11478 | if (!template_parm_p && at_class_scope_p () |
ec194454 | 11479 | && TYPE_BEING_DEFINED (current_class_type)) |
a723baf1 MM |
11480 | { |
11481 | unsigned depth = 0; | |
11482 | ||
11483 | /* Create a DEFAULT_ARG to represented the unparsed default | |
11484 | argument. */ | |
11485 | default_argument = make_node (DEFAULT_ARG); | |
11486 | DEFARG_TOKENS (default_argument) = cp_token_cache_new (); | |
11487 | ||
11488 | /* Add tokens until we have processed the entire default | |
11489 | argument. */ | |
11490 | while (true) | |
11491 | { | |
11492 | bool done = false; | |
11493 | cp_token *token; | |
11494 | ||
11495 | /* Peek at the next token. */ | |
11496 | token = cp_lexer_peek_token (parser->lexer); | |
11497 | /* What we do depends on what token we have. */ | |
11498 | switch (token->type) | |
11499 | { | |
11500 | /* In valid code, a default argument must be | |
11501 | immediately followed by a `,' `)', or `...'. */ | |
11502 | case CPP_COMMA: | |
11503 | case CPP_CLOSE_PAREN: | |
11504 | case CPP_ELLIPSIS: | |
11505 | /* If we run into a non-nested `;', `}', or `]', | |
11506 | then the code is invalid -- but the default | |
11507 | argument is certainly over. */ | |
11508 | case CPP_SEMICOLON: | |
11509 | case CPP_CLOSE_BRACE: | |
11510 | case CPP_CLOSE_SQUARE: | |
11511 | if (depth == 0) | |
11512 | done = true; | |
11513 | /* Update DEPTH, if necessary. */ | |
11514 | else if (token->type == CPP_CLOSE_PAREN | |
11515 | || token->type == CPP_CLOSE_BRACE | |
11516 | || token->type == CPP_CLOSE_SQUARE) | |
11517 | --depth; | |
11518 | break; | |
11519 | ||
11520 | case CPP_OPEN_PAREN: | |
11521 | case CPP_OPEN_SQUARE: | |
11522 | case CPP_OPEN_BRACE: | |
11523 | ++depth; | |
11524 | break; | |
11525 | ||
11526 | case CPP_GREATER: | |
11527 | /* If we see a non-nested `>', and `>' is not an | |
11528 | operator, then it marks the end of the default | |
11529 | argument. */ | |
11530 | if (!depth && !greater_than_is_operator_p) | |
11531 | done = true; | |
11532 | break; | |
11533 | ||
11534 | /* If we run out of tokens, issue an error message. */ | |
11535 | case CPP_EOF: | |
11536 | error ("file ends in default argument"); | |
11537 | done = true; | |
11538 | break; | |
11539 | ||
11540 | case CPP_NAME: | |
11541 | case CPP_SCOPE: | |
11542 | /* In these cases, we should look for template-ids. | |
21526606 | 11543 | For example, if the default argument is |
a723baf1 MM |
11544 | `X<int, double>()', we need to do name lookup to |
11545 | figure out whether or not `X' is a template; if | |
34cd5ae7 | 11546 | so, the `,' does not end the default argument. |
a723baf1 MM |
11547 | |
11548 | That is not yet done. */ | |
11549 | break; | |
11550 | ||
11551 | default: | |
11552 | break; | |
11553 | } | |
11554 | ||
11555 | /* If we've reached the end, stop. */ | |
11556 | if (done) | |
11557 | break; | |
21526606 | 11558 | |
a723baf1 MM |
11559 | /* Add the token to the token block. */ |
11560 | token = cp_lexer_consume_token (parser->lexer); | |
11561 | cp_token_cache_push_token (DEFARG_TOKENS (default_argument), | |
11562 | token); | |
11563 | } | |
11564 | } | |
11565 | /* Outside of a class definition, we can just parse the | |
11566 | assignment-expression. */ | |
11567 | else | |
11568 | { | |
11569 | bool saved_local_variables_forbidden_p; | |
11570 | ||
11571 | /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is | |
11572 | set correctly. */ | |
21526606 | 11573 | saved_greater_than_is_operator_p |
a723baf1 MM |
11574 | = parser->greater_than_is_operator_p; |
11575 | parser->greater_than_is_operator_p = greater_than_is_operator_p; | |
11576 | /* Local variable names (and the `this' keyword) may not | |
11577 | appear in a default argument. */ | |
21526606 | 11578 | saved_local_variables_forbidden_p |
a723baf1 MM |
11579 | = parser->local_variables_forbidden_p; |
11580 | parser->local_variables_forbidden_p = true; | |
11581 | /* Parse the assignment-expression. */ | |
11582 | default_argument = cp_parser_assignment_expression (parser); | |
11583 | /* Restore saved state. */ | |
21526606 | 11584 | parser->greater_than_is_operator_p |
a723baf1 | 11585 | = saved_greater_than_is_operator_p; |
21526606 EC |
11586 | parser->local_variables_forbidden_p |
11587 | = saved_local_variables_forbidden_p; | |
a723baf1 MM |
11588 | } |
11589 | if (!parser->default_arg_ok_p) | |
11590 | { | |
c67d36d0 NS |
11591 | if (!flag_pedantic_errors) |
11592 | warning ("deprecated use of default argument for parameter of non-function"); | |
11593 | else | |
11594 | { | |
11595 | error ("default arguments are only permitted for function parameters"); | |
11596 | default_argument = NULL_TREE; | |
11597 | } | |
a723baf1 MM |
11598 | } |
11599 | } | |
11600 | else | |
11601 | default_argument = NULL_TREE; | |
21526606 | 11602 | |
a723baf1 MM |
11603 | /* Create the representation of the parameter. */ |
11604 | if (attributes) | |
11605 | decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers); | |
21526606 | 11606 | parameter = build_tree_list (default_argument, |
a723baf1 MM |
11607 | build_tree_list (decl_specifiers, |
11608 | declarator)); | |
11609 | ||
11610 | return parameter; | |
11611 | } | |
11612 | ||
a723baf1 MM |
11613 | /* Parse a function-body. |
11614 | ||
11615 | function-body: | |
11616 | compound_statement */ | |
11617 | ||
11618 | static void | |
11619 | cp_parser_function_body (cp_parser *parser) | |
11620 | { | |
a5bcc582 | 11621 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
11622 | } |
11623 | ||
11624 | /* Parse a ctor-initializer-opt followed by a function-body. Return | |
11625 | true if a ctor-initializer was present. */ | |
11626 | ||
11627 | static bool | |
11628 | cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) | |
11629 | { | |
11630 | tree body; | |
11631 | bool ctor_initializer_p; | |
11632 | ||
11633 | /* Begin the function body. */ | |
11634 | body = begin_function_body (); | |
11635 | /* Parse the optional ctor-initializer. */ | |
11636 | ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); | |
11637 | /* Parse the function-body. */ | |
11638 | cp_parser_function_body (parser); | |
11639 | /* Finish the function body. */ | |
11640 | finish_function_body (body); | |
11641 | ||
11642 | return ctor_initializer_p; | |
11643 | } | |
11644 | ||
11645 | /* Parse an initializer. | |
11646 | ||
11647 | initializer: | |
11648 | = initializer-clause | |
21526606 | 11649 | ( expression-list ) |
a723baf1 MM |
11650 | |
11651 | Returns a expression representing the initializer. If no | |
21526606 | 11652 | initializer is present, NULL_TREE is returned. |
a723baf1 MM |
11653 | |
11654 | *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )' | |
11655 | production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is | |
39703eb9 MM |
11656 | set to FALSE if there is no initializer present. If there is an |
11657 | initializer, and it is not a constant-expression, *NON_CONSTANT_P | |
11658 | is set to true; otherwise it is set to false. */ | |
a723baf1 MM |
11659 | |
11660 | static tree | |
39703eb9 MM |
11661 | cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init, |
11662 | bool* non_constant_p) | |
a723baf1 MM |
11663 | { |
11664 | cp_token *token; | |
11665 | tree init; | |
11666 | ||
11667 | /* Peek at the next token. */ | |
11668 | token = cp_lexer_peek_token (parser->lexer); | |
11669 | ||
11670 | /* Let our caller know whether or not this initializer was | |
11671 | parenthesized. */ | |
11672 | *is_parenthesized_init = (token->type == CPP_OPEN_PAREN); | |
39703eb9 MM |
11673 | /* Assume that the initializer is constant. */ |
11674 | *non_constant_p = false; | |
a723baf1 MM |
11675 | |
11676 | if (token->type == CPP_EQ) | |
11677 | { | |
11678 | /* Consume the `='. */ | |
11679 | cp_lexer_consume_token (parser->lexer); | |
11680 | /* Parse the initializer-clause. */ | |
39703eb9 | 11681 | init = cp_parser_initializer_clause (parser, non_constant_p); |
a723baf1 MM |
11682 | } |
11683 | else if (token->type == CPP_OPEN_PAREN) | |
39703eb9 MM |
11684 | init = cp_parser_parenthesized_expression_list (parser, false, |
11685 | non_constant_p); | |
a723baf1 MM |
11686 | else |
11687 | { | |
11688 | /* Anything else is an error. */ | |
11689 | cp_parser_error (parser, "expected initializer"); | |
11690 | init = error_mark_node; | |
11691 | } | |
11692 | ||
11693 | return init; | |
11694 | } | |
11695 | ||
21526606 | 11696 | /* Parse an initializer-clause. |
a723baf1 MM |
11697 | |
11698 | initializer-clause: | |
11699 | assignment-expression | |
11700 | { initializer-list , [opt] } | |
11701 | { } | |
11702 | ||
21526606 | 11703 | Returns an expression representing the initializer. |
a723baf1 MM |
11704 | |
11705 | If the `assignment-expression' production is used the value | |
21526606 | 11706 | returned is simply a representation for the expression. |
a723baf1 MM |
11707 | |
11708 | Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be | |
11709 | the elements of the initializer-list (or NULL_TREE, if the last | |
11710 | production is used). The TREE_TYPE for the CONSTRUCTOR will be | |
11711 | NULL_TREE. There is no way to detect whether or not the optional | |
39703eb9 MM |
11712 | trailing `,' was provided. NON_CONSTANT_P is as for |
11713 | cp_parser_initializer. */ | |
a723baf1 MM |
11714 | |
11715 | static tree | |
39703eb9 | 11716 | cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11717 | { |
11718 | tree initializer; | |
11719 | ||
11720 | /* If it is not a `{', then we are looking at an | |
11721 | assignment-expression. */ | |
11722 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) | |
0da99d4e GB |
11723 | { |
11724 | initializer | |
11725 | = cp_parser_constant_expression (parser, | |
11726 | /*allow_non_constant_p=*/true, | |
11727 | non_constant_p); | |
11728 | if (!*non_constant_p) | |
11729 | initializer = fold_non_dependent_expr (initializer); | |
11730 | } | |
a723baf1 MM |
11731 | else |
11732 | { | |
11733 | /* Consume the `{' token. */ | |
11734 | cp_lexer_consume_token (parser->lexer); | |
11735 | /* Create a CONSTRUCTOR to represent the braced-initializer. */ | |
11736 | initializer = make_node (CONSTRUCTOR); | |
a723baf1 MM |
11737 | /* If it's not a `}', then there is a non-trivial initializer. */ |
11738 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) | |
11739 | { | |
11740 | /* Parse the initializer list. */ | |
11741 | CONSTRUCTOR_ELTS (initializer) | |
39703eb9 | 11742 | = cp_parser_initializer_list (parser, non_constant_p); |
a723baf1 MM |
11743 | /* A trailing `,' token is allowed. */ |
11744 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
11745 | cp_lexer_consume_token (parser->lexer); | |
11746 | } | |
a723baf1 MM |
11747 | /* Now, there should be a trailing `}'. */ |
11748 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
11749 | } | |
11750 | ||
11751 | return initializer; | |
11752 | } | |
11753 | ||
11754 | /* Parse an initializer-list. | |
11755 | ||
11756 | initializer-list: | |
11757 | initializer-clause | |
11758 | initializer-list , initializer-clause | |
11759 | ||
11760 | GNU Extension: | |
21526606 | 11761 | |
a723baf1 MM |
11762 | initializer-list: |
11763 | identifier : initializer-clause | |
11764 | initializer-list, identifier : initializer-clause | |
11765 | ||
11766 | Returns a TREE_LIST. The TREE_VALUE of each node is an expression | |
11767 | for the initializer. If the TREE_PURPOSE is non-NULL, it is the | |
39703eb9 MM |
11768 | IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is |
11769 | as for cp_parser_initializer. */ | |
a723baf1 MM |
11770 | |
11771 | static tree | |
39703eb9 | 11772 | cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) |
a723baf1 MM |
11773 | { |
11774 | tree initializers = NULL_TREE; | |
11775 | ||
39703eb9 MM |
11776 | /* Assume all of the expressions are constant. */ |
11777 | *non_constant_p = false; | |
11778 | ||
a723baf1 MM |
11779 | /* Parse the rest of the list. */ |
11780 | while (true) | |
11781 | { | |
11782 | cp_token *token; | |
11783 | tree identifier; | |
11784 | tree initializer; | |
39703eb9 MM |
11785 | bool clause_non_constant_p; |
11786 | ||
a723baf1 MM |
11787 | /* If the next token is an identifier and the following one is a |
11788 | colon, we are looking at the GNU designated-initializer | |
11789 | syntax. */ | |
11790 | if (cp_parser_allow_gnu_extensions_p (parser) | |
11791 | && cp_lexer_next_token_is (parser->lexer, CPP_NAME) | |
11792 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) | |
11793 | { | |
11794 | /* Consume the identifier. */ | |
11795 | identifier = cp_lexer_consume_token (parser->lexer)->value; | |
11796 | /* Consume the `:'. */ | |
11797 | cp_lexer_consume_token (parser->lexer); | |
11798 | } | |
11799 | else | |
11800 | identifier = NULL_TREE; | |
11801 | ||
11802 | /* Parse the initializer. */ | |
21526606 | 11803 | initializer = cp_parser_initializer_clause (parser, |
39703eb9 MM |
11804 | &clause_non_constant_p); |
11805 | /* If any clause is non-constant, so is the entire initializer. */ | |
11806 | if (clause_non_constant_p) | |
11807 | *non_constant_p = true; | |
a723baf1 MM |
11808 | /* Add it to the list. */ |
11809 | initializers = tree_cons (identifier, initializer, initializers); | |
11810 | ||
11811 | /* If the next token is not a comma, we have reached the end of | |
11812 | the list. */ | |
11813 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
11814 | break; | |
11815 | ||
11816 | /* Peek at the next token. */ | |
11817 | token = cp_lexer_peek_nth_token (parser->lexer, 2); | |
11818 | /* If the next token is a `}', then we're still done. An | |
11819 | initializer-clause can have a trailing `,' after the | |
11820 | initializer-list and before the closing `}'. */ | |
11821 | if (token->type == CPP_CLOSE_BRACE) | |
11822 | break; | |
11823 | ||
11824 | /* Consume the `,' token. */ | |
11825 | cp_lexer_consume_token (parser->lexer); | |
11826 | } | |
11827 | ||
11828 | /* The initializers were built up in reverse order, so we need to | |
11829 | reverse them now. */ | |
11830 | return nreverse (initializers); | |
11831 | } | |
11832 | ||
11833 | /* Classes [gram.class] */ | |
11834 | ||
11835 | /* Parse a class-name. | |
11836 | ||
11837 | class-name: | |
11838 | identifier | |
11839 | template-id | |
11840 | ||
11841 | TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used | |
11842 | to indicate that names looked up in dependent types should be | |
11843 | assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' | |
11844 | keyword has been used to indicate that the name that appears next | |
11845 | is a template. TYPE_P is true iff the next name should be treated | |
11846 | as class-name, even if it is declared to be some other kind of name | |
8d241e0b KL |
11847 | as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in |
11848 | dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class | |
11849 | being defined in a class-head. | |
a723baf1 MM |
11850 | |
11851 | Returns the TYPE_DECL representing the class. */ | |
11852 | ||
11853 | static tree | |
21526606 EC |
11854 | cp_parser_class_name (cp_parser *parser, |
11855 | bool typename_keyword_p, | |
11856 | bool template_keyword_p, | |
a723baf1 | 11857 | bool type_p, |
a723baf1 | 11858 | bool check_dependency_p, |
a668c6ad MM |
11859 | bool class_head_p, |
11860 | bool is_declaration) | |
a723baf1 MM |
11861 | { |
11862 | tree decl; | |
11863 | tree scope; | |
11864 | bool typename_p; | |
e5976695 MM |
11865 | cp_token *token; |
11866 | ||
11867 | /* All class-names start with an identifier. */ | |
11868 | token = cp_lexer_peek_token (parser->lexer); | |
11869 | if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) | |
11870 | { | |
11871 | cp_parser_error (parser, "expected class-name"); | |
11872 | return error_mark_node; | |
11873 | } | |
21526606 | 11874 | |
a723baf1 MM |
11875 | /* PARSER->SCOPE can be cleared when parsing the template-arguments |
11876 | to a template-id, so we save it here. */ | |
11877 | scope = parser->scope; | |
3adee96c KL |
11878 | if (scope == error_mark_node) |
11879 | return error_mark_node; | |
21526606 | 11880 | |
a723baf1 MM |
11881 | /* Any name names a type if we're following the `typename' keyword |
11882 | in a qualified name where the enclosing scope is type-dependent. */ | |
11883 | typename_p = (typename_keyword_p && scope && TYPE_P (scope) | |
1fb3244a | 11884 | && dependent_type_p (scope)); |
e5976695 MM |
11885 | /* Handle the common case (an identifier, but not a template-id) |
11886 | efficiently. */ | |
21526606 | 11887 | if (token->type == CPP_NAME |
f4abade9 | 11888 | && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) |
a723baf1 | 11889 | { |
a723baf1 MM |
11890 | tree identifier; |
11891 | ||
11892 | /* Look for the identifier. */ | |
11893 | identifier = cp_parser_identifier (parser); | |
11894 | /* If the next token isn't an identifier, we are certainly not | |
11895 | looking at a class-name. */ | |
11896 | if (identifier == error_mark_node) | |
11897 | decl = error_mark_node; | |
11898 | /* If we know this is a type-name, there's no need to look it | |
11899 | up. */ | |
11900 | else if (typename_p) | |
11901 | decl = identifier; | |
11902 | else | |
11903 | { | |
11904 | /* If the next token is a `::', then the name must be a type | |
11905 | name. | |
11906 | ||
11907 | [basic.lookup.qual] | |
11908 | ||
11909 | During the lookup for a name preceding the :: scope | |
11910 | resolution operator, object, function, and enumerator | |
11911 | names are ignored. */ | |
11912 | if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11913 | type_p = true; | |
11914 | /* Look up the name. */ | |
21526606 | 11915 | decl = cp_parser_lookup_name (parser, identifier, |
a723baf1 | 11916 | type_p, |
b0bc6e8e | 11917 | /*is_template=*/false, |
eea9800f | 11918 | /*is_namespace=*/false, |
a723baf1 MM |
11919 | check_dependency_p); |
11920 | } | |
11921 | } | |
e5976695 MM |
11922 | else |
11923 | { | |
11924 | /* Try a template-id. */ | |
11925 | decl = cp_parser_template_id (parser, template_keyword_p, | |
a668c6ad MM |
11926 | check_dependency_p, |
11927 | is_declaration); | |
e5976695 MM |
11928 | if (decl == error_mark_node) |
11929 | return error_mark_node; | |
11930 | } | |
a723baf1 MM |
11931 | |
11932 | decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); | |
11933 | ||
11934 | /* If this is a typename, create a TYPENAME_TYPE. */ | |
11935 | if (typename_p && decl != error_mark_node) | |
4bfb8bba MM |
11936 | { |
11937 | decl = make_typename_type (scope, decl, /*complain=*/1); | |
11938 | if (decl != error_mark_node) | |
11939 | decl = TYPE_NAME (decl); | |
11940 | } | |
a723baf1 MM |
11941 | |
11942 | /* Check to see that it is really the name of a class. */ | |
21526606 | 11943 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR |
a723baf1 MM |
11944 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE |
11945 | && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) | |
11946 | /* Situations like this: | |
11947 | ||
11948 | template <typename T> struct A { | |
21526606 | 11949 | typename T::template X<int>::I i; |
a723baf1 MM |
11950 | }; |
11951 | ||
11952 | are problematic. Is `T::template X<int>' a class-name? The | |
11953 | standard does not seem to be definitive, but there is no other | |
11954 | valid interpretation of the following `::'. Therefore, those | |
11955 | names are considered class-names. */ | |
78757caa | 11956 | decl = TYPE_NAME (make_typename_type (scope, decl, tf_error)); |
a723baf1 MM |
11957 | else if (decl == error_mark_node |
11958 | || TREE_CODE (decl) != TYPE_DECL | |
11959 | || !IS_AGGR_TYPE (TREE_TYPE (decl))) | |
11960 | { | |
11961 | cp_parser_error (parser, "expected class-name"); | |
11962 | return error_mark_node; | |
11963 | } | |
11964 | ||
11965 | return decl; | |
11966 | } | |
11967 | ||
11968 | /* Parse a class-specifier. | |
11969 | ||
11970 | class-specifier: | |
11971 | class-head { member-specification [opt] } | |
11972 | ||
11973 | Returns the TREE_TYPE representing the class. */ | |
11974 | ||
11975 | static tree | |
94edc4ab | 11976 | cp_parser_class_specifier (cp_parser* parser) |
a723baf1 MM |
11977 | { |
11978 | cp_token *token; | |
11979 | tree type; | |
6de9cd9a | 11980 | tree attributes = NULL_TREE; |
a723baf1 MM |
11981 | int has_trailing_semicolon; |
11982 | bool nested_name_specifier_p; | |
a723baf1 | 11983 | unsigned saved_num_template_parameter_lists; |
91b004e5 | 11984 | bool pop_p = false; |
a723baf1 | 11985 | |
8d241e0b | 11986 | push_deferring_access_checks (dk_no_deferred); |
cf22909c | 11987 | |
a723baf1 MM |
11988 | /* Parse the class-head. */ |
11989 | type = cp_parser_class_head (parser, | |
38b305d0 JM |
11990 | &nested_name_specifier_p, |
11991 | &attributes); | |
a723baf1 MM |
11992 | /* If the class-head was a semantic disaster, skip the entire body |
11993 | of the class. */ | |
11994 | if (!type) | |
11995 | { | |
11996 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
cf22909c | 11997 | pop_deferring_access_checks (); |
a723baf1 MM |
11998 | return error_mark_node; |
11999 | } | |
cf22909c | 12000 | |
a723baf1 MM |
12001 | /* Look for the `{'. */ |
12002 | if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'")) | |
cf22909c KL |
12003 | { |
12004 | pop_deferring_access_checks (); | |
12005 | return error_mark_node; | |
12006 | } | |
12007 | ||
a723baf1 MM |
12008 | /* Issue an error message if type-definitions are forbidden here. */ |
12009 | cp_parser_check_type_definition (parser); | |
12010 | /* Remember that we are defining one more class. */ | |
12011 | ++parser->num_classes_being_defined; | |
12012 | /* Inside the class, surrounding template-parameter-lists do not | |
12013 | apply. */ | |
21526606 EC |
12014 | saved_num_template_parameter_lists |
12015 | = parser->num_template_parameter_lists; | |
a723baf1 | 12016 | parser->num_template_parameter_lists = 0; |
78757caa | 12017 | |
a723baf1 | 12018 | /* Start the class. */ |
eeb23c11 | 12019 | if (nested_name_specifier_p) |
91b004e5 | 12020 | pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); |
a723baf1 MM |
12021 | type = begin_class_definition (type); |
12022 | if (type == error_mark_node) | |
9bcb9aae | 12023 | /* If the type is erroneous, skip the entire body of the class. */ |
a723baf1 MM |
12024 | cp_parser_skip_to_closing_brace (parser); |
12025 | else | |
12026 | /* Parse the member-specification. */ | |
12027 | cp_parser_member_specification_opt (parser); | |
12028 | /* Look for the trailing `}'. */ | |
12029 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
12030 | /* We get better error messages by noticing a common problem: a | |
12031 | missing trailing `;'. */ | |
12032 | token = cp_lexer_peek_token (parser->lexer); | |
12033 | has_trailing_semicolon = (token->type == CPP_SEMICOLON); | |
38b305d0 | 12034 | /* Look for trailing attributes to apply to this class. */ |
a723baf1 | 12035 | if (cp_parser_allow_gnu_extensions_p (parser)) |
560ad596 | 12036 | { |
38b305d0 JM |
12037 | tree sub_attr = cp_parser_attributes_opt (parser); |
12038 | attributes = chainon (attributes, sub_attr); | |
560ad596 | 12039 | } |
38b305d0 JM |
12040 | if (type != error_mark_node) |
12041 | type = finish_struct (type, attributes); | |
91b004e5 | 12042 | if (pop_p) |
560ad596 | 12043 | pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type))); |
a723baf1 MM |
12044 | /* If this class is not itself within the scope of another class, |
12045 | then we need to parse the bodies of all of the queued function | |
12046 | definitions. Note that the queued functions defined in a class | |
12047 | are not always processed immediately following the | |
12048 | class-specifier for that class. Consider: | |
12049 | ||
12050 | struct A { | |
12051 | struct B { void f() { sizeof (A); } }; | |
12052 | }; | |
12053 | ||
12054 | If `f' were processed before the processing of `A' were | |
12055 | completed, there would be no way to compute the size of `A'. | |
12056 | Note that the nesting we are interested in here is lexical -- | |
12057 | not the semantic nesting given by TYPE_CONTEXT. In particular, | |
12058 | for: | |
12059 | ||
12060 | struct A { struct B; }; | |
12061 | struct A::B { void f() { } }; | |
12062 | ||
12063 | there is no need to delay the parsing of `A::B::f'. */ | |
21526606 | 12064 | if (--parser->num_classes_being_defined == 0) |
a723baf1 | 12065 | { |
8218bd34 MM |
12066 | tree queue_entry; |
12067 | tree fn; | |
a723baf1 | 12068 | |
8218bd34 MM |
12069 | /* In a first pass, parse default arguments to the functions. |
12070 | Then, in a second pass, parse the bodies of the functions. | |
12071 | This two-phased approach handles cases like: | |
21526606 EC |
12072 | |
12073 | struct S { | |
12074 | void f() { g(); } | |
8218bd34 MM |
12075 | void g(int i = 3); |
12076 | }; | |
12077 | ||
12078 | */ | |
8db1028e NS |
12079 | for (TREE_PURPOSE (parser->unparsed_functions_queues) |
12080 | = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12081 | (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues)); | |
12082 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
12083 | = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues))) | |
8218bd34 MM |
12084 | { |
12085 | fn = TREE_VALUE (queue_entry); | |
8218bd34 MM |
12086 | /* Make sure that any template parameters are in scope. */ |
12087 | maybe_begin_member_template_processing (fn); | |
12088 | /* If there are default arguments that have not yet been processed, | |
12089 | take care of them now. */ | |
12090 | cp_parser_late_parsing_default_args (parser, fn); | |
12091 | /* Remove any template parameters from the symbol table. */ | |
12092 | maybe_end_member_template_processing (); | |
12093 | } | |
12094 | /* Now parse the body of the functions. */ | |
8db1028e NS |
12095 | for (TREE_VALUE (parser->unparsed_functions_queues) |
12096 | = nreverse (TREE_VALUE (parser->unparsed_functions_queues)); | |
12097 | (queue_entry = TREE_VALUE (parser->unparsed_functions_queues)); | |
12098 | TREE_VALUE (parser->unparsed_functions_queues) | |
12099 | = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues))) | |
a723baf1 | 12100 | { |
a723baf1 | 12101 | /* Figure out which function we need to process. */ |
a723baf1 MM |
12102 | fn = TREE_VALUE (queue_entry); |
12103 | ||
4543ee47 ZD |
12104 | /* A hack to prevent garbage collection. */ |
12105 | function_depth++; | |
12106 | ||
a723baf1 MM |
12107 | /* Parse the function. */ |
12108 | cp_parser_late_parsing_for_member (parser, fn); | |
4543ee47 | 12109 | function_depth--; |
a723baf1 MM |
12110 | } |
12111 | ||
a723baf1 MM |
12112 | } |
12113 | ||
12114 | /* Put back any saved access checks. */ | |
cf22909c | 12115 | pop_deferring_access_checks (); |
a723baf1 MM |
12116 | |
12117 | /* Restore the count of active template-parameter-lists. */ | |
12118 | parser->num_template_parameter_lists | |
12119 | = saved_num_template_parameter_lists; | |
12120 | ||
12121 | return type; | |
12122 | } | |
12123 | ||
12124 | /* Parse a class-head. | |
12125 | ||
12126 | class-head: | |
12127 | class-key identifier [opt] base-clause [opt] | |
12128 | class-key nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12129 | class-key nested-name-specifier [opt] template-id |
12130 | base-clause [opt] | |
a723baf1 MM |
12131 | |
12132 | GNU Extensions: | |
12133 | class-key attributes identifier [opt] base-clause [opt] | |
12134 | class-key attributes nested-name-specifier identifier base-clause [opt] | |
21526606 EC |
12135 | class-key attributes nested-name-specifier [opt] template-id |
12136 | base-clause [opt] | |
a723baf1 MM |
12137 | |
12138 | Returns the TYPE of the indicated class. Sets | |
12139 | *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions | |
12140 | involving a nested-name-specifier was used, and FALSE otherwise. | |
a723baf1 MM |
12141 | |
12142 | Returns NULL_TREE if the class-head is syntactically valid, but | |
12143 | semantically invalid in a way that means we should skip the entire | |
12144 | body of the class. */ | |
12145 | ||
12146 | static tree | |
21526606 | 12147 | cp_parser_class_head (cp_parser* parser, |
38b305d0 JM |
12148 | bool* nested_name_specifier_p, |
12149 | tree *attributes_p) | |
a723baf1 MM |
12150 | { |
12151 | cp_token *token; | |
12152 | tree nested_name_specifier; | |
12153 | enum tag_types class_key; | |
12154 | tree id = NULL_TREE; | |
12155 | tree type = NULL_TREE; | |
12156 | tree attributes; | |
12157 | bool template_id_p = false; | |
12158 | bool qualified_p = false; | |
12159 | bool invalid_nested_name_p = false; | |
afb0918a | 12160 | bool invalid_explicit_specialization_p = false; |
91b004e5 | 12161 | bool pop_p = false; |
a723baf1 MM |
12162 | unsigned num_templates; |
12163 | ||
12164 | /* Assume no nested-name-specifier will be present. */ | |
12165 | *nested_name_specifier_p = false; | |
12166 | /* Assume no template parameter lists will be used in defining the | |
12167 | type. */ | |
12168 | num_templates = 0; | |
12169 | ||
12170 | /* Look for the class-key. */ | |
12171 | class_key = cp_parser_class_key (parser); | |
12172 | if (class_key == none_type) | |
12173 | return error_mark_node; | |
12174 | ||
12175 | /* Parse the attributes. */ | |
12176 | attributes = cp_parser_attributes_opt (parser); | |
12177 | ||
12178 | /* If the next token is `::', that is invalid -- but sometimes | |
12179 | people do try to write: | |
12180 | ||
21526606 | 12181 | struct ::S {}; |
a723baf1 MM |
12182 | |
12183 | Handle this gracefully by accepting the extra qualifier, and then | |
12184 | issuing an error about it later if this really is a | |
2050a1bb | 12185 | class-head. If it turns out just to be an elaborated type |
a723baf1 MM |
12186 | specifier, remain silent. */ |
12187 | if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) | |
12188 | qualified_p = true; | |
12189 | ||
8d241e0b KL |
12190 | push_deferring_access_checks (dk_no_check); |
12191 | ||
a723baf1 MM |
12192 | /* Determine the name of the class. Begin by looking for an |
12193 | optional nested-name-specifier. */ | |
21526606 | 12194 | nested_name_specifier |
a723baf1 MM |
12195 | = cp_parser_nested_name_specifier_opt (parser, |
12196 | /*typename_keyword_p=*/false, | |
66d418e6 | 12197 | /*check_dependency_p=*/false, |
a668c6ad MM |
12198 | /*type_p=*/false, |
12199 | /*is_declaration=*/false); | |
a723baf1 MM |
12200 | /* If there was a nested-name-specifier, then there *must* be an |
12201 | identifier. */ | |
12202 | if (nested_name_specifier) | |
12203 | { | |
12204 | /* Although the grammar says `identifier', it really means | |
12205 | `class-name' or `template-name'. You are only allowed to | |
12206 | define a class that has already been declared with this | |
21526606 | 12207 | syntax. |
a723baf1 MM |
12208 | |
12209 | The proposed resolution for Core Issue 180 says that whever | |
12210 | you see `class T::X' you should treat `X' as a type-name. | |
21526606 | 12211 | |
a723baf1 | 12212 | It is OK to define an inaccessible class; for example: |
21526606 | 12213 | |
a723baf1 MM |
12214 | class A { class B; }; |
12215 | class A::B {}; | |
21526606 | 12216 | |
a723baf1 MM |
12217 | We do not know if we will see a class-name, or a |
12218 | template-name. We look for a class-name first, in case the | |
12219 | class-name is a template-id; if we looked for the | |
12220 | template-name first we would stop after the template-name. */ | |
12221 | cp_parser_parse_tentatively (parser); | |
12222 | type = cp_parser_class_name (parser, | |
12223 | /*typename_keyword_p=*/false, | |
12224 | /*template_keyword_p=*/false, | |
12225 | /*type_p=*/true, | |
a723baf1 | 12226 | /*check_dependency_p=*/false, |
a668c6ad MM |
12227 | /*class_head_p=*/true, |
12228 | /*is_declaration=*/false); | |
a723baf1 MM |
12229 | /* If that didn't work, ignore the nested-name-specifier. */ |
12230 | if (!cp_parser_parse_definitely (parser)) | |
12231 | { | |
12232 | invalid_nested_name_p = true; | |
12233 | id = cp_parser_identifier (parser); | |
12234 | if (id == error_mark_node) | |
12235 | id = NULL_TREE; | |
12236 | } | |
12237 | /* If we could not find a corresponding TYPE, treat this | |
12238 | declaration like an unqualified declaration. */ | |
12239 | if (type == error_mark_node) | |
12240 | nested_name_specifier = NULL_TREE; | |
12241 | /* Otherwise, count the number of templates used in TYPE and its | |
12242 | containing scopes. */ | |
21526606 | 12243 | else |
a723baf1 MM |
12244 | { |
12245 | tree scope; | |
12246 | ||
21526606 | 12247 | for (scope = TREE_TYPE (type); |
a723baf1 | 12248 | scope && TREE_CODE (scope) != NAMESPACE_DECL; |
21526606 | 12249 | scope = (TYPE_P (scope) |
a723baf1 | 12250 | ? TYPE_CONTEXT (scope) |
21526606 EC |
12251 | : DECL_CONTEXT (scope))) |
12252 | if (TYPE_P (scope) | |
a723baf1 MM |
12253 | && CLASS_TYPE_P (scope) |
12254 | && CLASSTYPE_TEMPLATE_INFO (scope) | |
2050a1bb MM |
12255 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) |
12256 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) | |
a723baf1 MM |
12257 | ++num_templates; |
12258 | } | |
12259 | } | |
12260 | /* Otherwise, the identifier is optional. */ | |
12261 | else | |
12262 | { | |
12263 | /* We don't know whether what comes next is a template-id, | |
12264 | an identifier, or nothing at all. */ | |
12265 | cp_parser_parse_tentatively (parser); | |
12266 | /* Check for a template-id. */ | |
21526606 | 12267 | id = cp_parser_template_id (parser, |
a723baf1 | 12268 | /*template_keyword_p=*/false, |
a668c6ad MM |
12269 | /*check_dependency_p=*/true, |
12270 | /*is_declaration=*/true); | |
a723baf1 MM |
12271 | /* If that didn't work, it could still be an identifier. */ |
12272 | if (!cp_parser_parse_definitely (parser)) | |
12273 | { | |
12274 | if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) | |
12275 | id = cp_parser_identifier (parser); | |
12276 | else | |
12277 | id = NULL_TREE; | |
12278 | } | |
12279 | else | |
12280 | { | |
12281 | template_id_p = true; | |
12282 | ++num_templates; | |
12283 | } | |
12284 | } | |
12285 | ||
8d241e0b KL |
12286 | pop_deferring_access_checks (); |
12287 | ||
15077df5 MM |
12288 | if (id) |
12289 | cp_parser_check_for_invalid_template_id (parser, id); | |
ee43dab5 | 12290 | |
a723baf1 MM |
12291 | /* If it's not a `:' or a `{' then we can't really be looking at a |
12292 | class-head, since a class-head only appears as part of a | |
12293 | class-specifier. We have to detect this situation before calling | |
12294 | xref_tag, since that has irreversible side-effects. */ | |
12295 | if (!cp_parser_next_token_starts_class_definition_p (parser)) | |
12296 | { | |
12297 | cp_parser_error (parser, "expected `{' or `:'"); | |
12298 | return error_mark_node; | |
12299 | } | |
12300 | ||
12301 | /* At this point, we're going ahead with the class-specifier, even | |
12302 | if some other problem occurs. */ | |
12303 | cp_parser_commit_to_tentative_parse (parser); | |
12304 | /* Issue the error about the overly-qualified name now. */ | |
12305 | if (qualified_p) | |
12306 | cp_parser_error (parser, | |
12307 | "global qualification of class name is invalid"); | |
12308 | else if (invalid_nested_name_p) | |
12309 | cp_parser_error (parser, | |
12310 | "qualified name does not name a class"); | |
88081599 MM |
12311 | else if (nested_name_specifier) |
12312 | { | |
12313 | tree scope; | |
12314 | /* Figure out in what scope the declaration is being placed. */ | |
12315 | scope = current_scope (); | |
12316 | if (!scope) | |
12317 | scope = current_namespace; | |
12318 | /* If that scope does not contain the scope in which the | |
12319 | class was originally declared, the program is invalid. */ | |
12320 | if (scope && !is_ancestor (scope, nested_name_specifier)) | |
12321 | { | |
12322 | error ("declaration of `%D' in `%D' which does not " | |
12323 | "enclose `%D'", type, scope, nested_name_specifier); | |
12324 | type = NULL_TREE; | |
12325 | goto done; | |
12326 | } | |
12327 | /* [dcl.meaning] | |
12328 | ||
12329 | A declarator-id shall not be qualified exception of the | |
12330 | definition of a ... nested class outside of its class | |
12331 | ... [or] a the definition or explicit instantiation of a | |
12332 | class member of a namespace outside of its namespace. */ | |
12333 | if (scope == nested_name_specifier) | |
12334 | { | |
12335 | pedwarn ("extra qualification ignored"); | |
12336 | nested_name_specifier = NULL_TREE; | |
12337 | num_templates = 0; | |
12338 | } | |
12339 | } | |
afb0918a MM |
12340 | /* An explicit-specialization must be preceded by "template <>". If |
12341 | it is not, try to recover gracefully. */ | |
21526606 | 12342 | if (at_namespace_scope_p () |
afb0918a | 12343 | && parser->num_template_parameter_lists == 0 |
eeb23c11 | 12344 | && template_id_p) |
afb0918a MM |
12345 | { |
12346 | error ("an explicit specialization must be preceded by 'template <>'"); | |
12347 | invalid_explicit_specialization_p = true; | |
12348 | /* Take the same action that would have been taken by | |
12349 | cp_parser_explicit_specialization. */ | |
12350 | ++parser->num_template_parameter_lists; | |
12351 | begin_specialization (); | |
12352 | } | |
12353 | /* There must be no "return" statements between this point and the | |
12354 | end of this function; set "type "to the correct return value and | |
12355 | use "goto done;" to return. */ | |
a723baf1 MM |
12356 | /* Make sure that the right number of template parameters were |
12357 | present. */ | |
12358 | if (!cp_parser_check_template_parameters (parser, num_templates)) | |
afb0918a MM |
12359 | { |
12360 | /* If something went wrong, there is no point in even trying to | |
12361 | process the class-definition. */ | |
12362 | type = NULL_TREE; | |
12363 | goto done; | |
12364 | } | |
a723baf1 | 12365 | |
a723baf1 MM |
12366 | /* Look up the type. */ |
12367 | if (template_id_p) | |
12368 | { | |
12369 | type = TREE_TYPE (id); | |
12370 | maybe_process_partial_specialization (type); | |
12371 | } | |
12372 | else if (!nested_name_specifier) | |
12373 | { | |
12374 | /* If the class was unnamed, create a dummy name. */ | |
12375 | if (!id) | |
12376 | id = make_anon_name (); | |
38b305d0 | 12377 | type = xref_tag (class_key, id, /*globalize=*/false, |
cbd63935 | 12378 | parser->num_template_parameter_lists); |
a723baf1 MM |
12379 | } |
12380 | else | |
12381 | { | |
a723baf1 | 12382 | tree class_type; |
91b004e5 | 12383 | bool pop_p = false; |
a723baf1 MM |
12384 | |
12385 | /* Given: | |
12386 | ||
12387 | template <typename T> struct S { struct T }; | |
14d22dd6 | 12388 | template <typename T> struct S<T>::T { }; |
a723baf1 MM |
12389 | |
12390 | we will get a TYPENAME_TYPE when processing the definition of | |
12391 | `S::T'. We need to resolve it to the actual type before we | |
12392 | try to define it. */ | |
12393 | if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) | |
12394 | { | |
14d22dd6 MM |
12395 | class_type = resolve_typename_type (TREE_TYPE (type), |
12396 | /*only_current_p=*/false); | |
12397 | if (class_type != error_mark_node) | |
12398 | type = TYPE_NAME (class_type); | |
12399 | else | |
12400 | { | |
12401 | cp_parser_error (parser, "could not resolve typename type"); | |
12402 | type = error_mark_node; | |
12403 | } | |
a723baf1 MM |
12404 | } |
12405 | ||
560ad596 MM |
12406 | maybe_process_partial_specialization (TREE_TYPE (type)); |
12407 | class_type = current_class_type; | |
12408 | /* Enter the scope indicated by the nested-name-specifier. */ | |
12409 | if (nested_name_specifier) | |
91b004e5 | 12410 | pop_p = push_scope (nested_name_specifier); |
560ad596 MM |
12411 | /* Get the canonical version of this type. */ |
12412 | type = TYPE_MAIN_DECL (TREE_TYPE (type)); | |
12413 | if (PROCESSING_REAL_TEMPLATE_DECL_P () | |
12414 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) | |
12415 | type = push_template_decl (type); | |
12416 | type = TREE_TYPE (type); | |
12417 | if (nested_name_specifier) | |
eeb23c11 MM |
12418 | { |
12419 | *nested_name_specifier_p = true; | |
91b004e5 MM |
12420 | if (pop_p) |
12421 | pop_scope (nested_name_specifier); | |
eeb23c11 | 12422 | } |
a723baf1 MM |
12423 | } |
12424 | /* Indicate whether this class was declared as a `class' or as a | |
12425 | `struct'. */ | |
12426 | if (TREE_CODE (type) == RECORD_TYPE) | |
12427 | CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); | |
12428 | cp_parser_check_class_key (class_key, type); | |
12429 | ||
12430 | /* Enter the scope containing the class; the names of base classes | |
12431 | should be looked up in that context. For example, given: | |
12432 | ||
12433 | struct A { struct B {}; struct C; }; | |
12434 | struct A::C : B {}; | |
12435 | ||
12436 | is valid. */ | |
12437 | if (nested_name_specifier) | |
91b004e5 | 12438 | pop_p = push_scope (nested_name_specifier); |
a723baf1 MM |
12439 | /* Now, look for the base-clause. */ |
12440 | token = cp_lexer_peek_token (parser->lexer); | |
12441 | if (token->type == CPP_COLON) | |
12442 | { | |
12443 | tree bases; | |
12444 | ||
12445 | /* Get the list of base-classes. */ | |
12446 | bases = cp_parser_base_clause (parser); | |
12447 | /* Process them. */ | |
12448 | xref_basetypes (type, bases); | |
12449 | } | |
12450 | /* Leave the scope given by the nested-name-specifier. We will | |
12451 | enter the class scope itself while processing the members. */ | |
91b004e5 | 12452 | if (pop_p) |
a723baf1 MM |
12453 | pop_scope (nested_name_specifier); |
12454 | ||
afb0918a MM |
12455 | done: |
12456 | if (invalid_explicit_specialization_p) | |
12457 | { | |
12458 | end_specialization (); | |
12459 | --parser->num_template_parameter_lists; | |
12460 | } | |
38b305d0 | 12461 | *attributes_p = attributes; |
a723baf1 MM |
12462 | return type; |
12463 | } | |
12464 | ||
12465 | /* Parse a class-key. | |
12466 | ||
12467 | class-key: | |
12468 | class | |
12469 | struct | |
12470 | union | |
12471 | ||
12472 | Returns the kind of class-key specified, or none_type to indicate | |
12473 | error. */ | |
12474 | ||
12475 | static enum tag_types | |
94edc4ab | 12476 | cp_parser_class_key (cp_parser* parser) |
a723baf1 MM |
12477 | { |
12478 | cp_token *token; | |
12479 | enum tag_types tag_type; | |
12480 | ||
12481 | /* Look for the class-key. */ | |
12482 | token = cp_parser_require (parser, CPP_KEYWORD, "class-key"); | |
12483 | if (!token) | |
12484 | return none_type; | |
12485 | ||
12486 | /* Check to see if the TOKEN is a class-key. */ | |
12487 | tag_type = cp_parser_token_is_class_key (token); | |
12488 | if (!tag_type) | |
12489 | cp_parser_error (parser, "expected class-key"); | |
12490 | return tag_type; | |
12491 | } | |
12492 | ||
12493 | /* Parse an (optional) member-specification. | |
12494 | ||
12495 | member-specification: | |
12496 | member-declaration member-specification [opt] | |
12497 | access-specifier : member-specification [opt] */ | |
12498 | ||
12499 | static void | |
94edc4ab | 12500 | cp_parser_member_specification_opt (cp_parser* parser) |
a723baf1 MM |
12501 | { |
12502 | while (true) | |
12503 | { | |
12504 | cp_token *token; | |
12505 | enum rid keyword; | |
12506 | ||
12507 | /* Peek at the next token. */ | |
12508 | token = cp_lexer_peek_token (parser->lexer); | |
12509 | /* If it's a `}', or EOF then we've seen all the members. */ | |
12510 | if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF) | |
12511 | break; | |
12512 | ||
12513 | /* See if this token is a keyword. */ | |
12514 | keyword = token->keyword; | |
12515 | switch (keyword) | |
12516 | { | |
12517 | case RID_PUBLIC: | |
12518 | case RID_PROTECTED: | |
12519 | case RID_PRIVATE: | |
12520 | /* Consume the access-specifier. */ | |
12521 | cp_lexer_consume_token (parser->lexer); | |
12522 | /* Remember which access-specifier is active. */ | |
12523 | current_access_specifier = token->value; | |
12524 | /* Look for the `:'. */ | |
12525 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
12526 | break; | |
12527 | ||
12528 | default: | |
12529 | /* Otherwise, the next construction must be a | |
12530 | member-declaration. */ | |
12531 | cp_parser_member_declaration (parser); | |
a723baf1 MM |
12532 | } |
12533 | } | |
12534 | } | |
12535 | ||
21526606 | 12536 | /* Parse a member-declaration. |
a723baf1 MM |
12537 | |
12538 | member-declaration: | |
12539 | decl-specifier-seq [opt] member-declarator-list [opt] ; | |
12540 | function-definition ; [opt] | |
12541 | :: [opt] nested-name-specifier template [opt] unqualified-id ; | |
12542 | using-declaration | |
21526606 | 12543 | template-declaration |
a723baf1 MM |
12544 | |
12545 | member-declarator-list: | |
12546 | member-declarator | |
12547 | member-declarator-list , member-declarator | |
12548 | ||
12549 | member-declarator: | |
21526606 | 12550 | declarator pure-specifier [opt] |
a723baf1 | 12551 | declarator constant-initializer [opt] |
21526606 | 12552 | identifier [opt] : constant-expression |
a723baf1 MM |
12553 | |
12554 | GNU Extensions: | |
12555 | ||
12556 | member-declaration: | |
12557 | __extension__ member-declaration | |
12558 | ||
12559 | member-declarator: | |
12560 | declarator attributes [opt] pure-specifier [opt] | |
12561 | declarator attributes [opt] constant-initializer [opt] | |
12562 | identifier [opt] attributes [opt] : constant-expression */ | |
12563 | ||
12564 | static void | |
94edc4ab | 12565 | cp_parser_member_declaration (cp_parser* parser) |
a723baf1 MM |
12566 | { |
12567 | tree decl_specifiers; | |
12568 | tree prefix_attributes; | |
12569 | tree decl; | |
560ad596 | 12570 | int declares_class_or_enum; |
a723baf1 MM |
12571 | bool friend_p; |
12572 | cp_token *token; | |
12573 | int saved_pedantic; | |
12574 | ||
12575 | /* Check for the `__extension__' keyword. */ | |
12576 | if (cp_parser_extension_opt (parser, &saved_pedantic)) | |
12577 | { | |
12578 | /* Recurse. */ | |
12579 | cp_parser_member_declaration (parser); | |
12580 | /* Restore the old value of the PEDANTIC flag. */ | |
12581 | pedantic = saved_pedantic; | |
12582 | ||
12583 | return; | |
12584 | } | |
12585 | ||
12586 | /* Check for a template-declaration. */ | |
12587 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
12588 | { | |
12589 | /* Parse the template-declaration. */ | |
12590 | cp_parser_template_declaration (parser, /*member_p=*/true); | |
12591 | ||
12592 | return; | |
12593 | } | |
12594 | ||
12595 | /* Check for a using-declaration. */ | |
12596 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) | |
12597 | { | |
12598 | /* Parse the using-declaration. */ | |
12599 | cp_parser_using_declaration (parser); | |
12600 | ||
12601 | return; | |
12602 | } | |
21526606 | 12603 | |
a723baf1 | 12604 | /* Parse the decl-specifier-seq. */ |
21526606 | 12605 | decl_specifiers |
a723baf1 MM |
12606 | = cp_parser_decl_specifier_seq (parser, |
12607 | CP_PARSER_FLAGS_OPTIONAL, | |
12608 | &prefix_attributes, | |
12609 | &declares_class_or_enum); | |
8fbc5ae7 | 12610 | /* Check for an invalid type-name. */ |
2097b5f2 | 12611 | if (cp_parser_parse_and_diagnose_invalid_type_name (parser)) |
8fbc5ae7 | 12612 | return; |
a723baf1 MM |
12613 | /* If there is no declarator, then the decl-specifier-seq should |
12614 | specify a type. */ | |
12615 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
12616 | { | |
12617 | /* If there was no decl-specifier-seq, and the next token is a | |
12618 | `;', then we have something like: | |
12619 | ||
12620 | struct S { ; }; | |
12621 | ||
12622 | [class.mem] | |
12623 | ||
12624 | Each member-declaration shall declare at least one member | |
12625 | name of the class. */ | |
12626 | if (!decl_specifiers) | |
12627 | { | |
12628 | if (pedantic) | |
12629 | pedwarn ("extra semicolon"); | |
12630 | } | |
21526606 | 12631 | else |
a723baf1 MM |
12632 | { |
12633 | tree type; | |
21526606 | 12634 | |
a723baf1 MM |
12635 | /* See if this declaration is a friend. */ |
12636 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12637 | /* If there were decl-specifiers, check to see if there was | |
12638 | a class-declaration. */ | |
12639 | type = check_tag_decl (decl_specifiers); | |
12640 | /* Nested classes have already been added to the class, but | |
12641 | a `friend' needs to be explicitly registered. */ | |
12642 | if (friend_p) | |
12643 | { | |
12644 | /* If the `friend' keyword was present, the friend must | |
12645 | be introduced with a class-key. */ | |
12646 | if (!declares_class_or_enum) | |
12647 | error ("a class-key must be used when declaring a friend"); | |
12648 | /* In this case: | |
12649 | ||
21526606 EC |
12650 | template <typename T> struct A { |
12651 | friend struct A<T>::B; | |
a723baf1 | 12652 | }; |
21526606 | 12653 | |
a723baf1 MM |
12654 | A<T>::B will be represented by a TYPENAME_TYPE, and |
12655 | therefore not recognized by check_tag_decl. */ | |
12656 | if (!type) | |
12657 | { | |
12658 | tree specifier; | |
12659 | ||
21526606 | 12660 | for (specifier = decl_specifiers; |
a723baf1 MM |
12661 | specifier; |
12662 | specifier = TREE_CHAIN (specifier)) | |
12663 | { | |
12664 | tree s = TREE_VALUE (specifier); | |
12665 | ||
c003e212 GDR |
12666 | if (TREE_CODE (s) == IDENTIFIER_NODE) |
12667 | get_global_value_if_present (s, &type); | |
a723baf1 MM |
12668 | if (TREE_CODE (s) == TYPE_DECL) |
12669 | s = TREE_TYPE (s); | |
12670 | if (TYPE_P (s)) | |
12671 | { | |
12672 | type = s; | |
12673 | break; | |
12674 | } | |
12675 | } | |
12676 | } | |
fdd09134 | 12677 | if (!type || !TYPE_P (type)) |
a723baf1 MM |
12678 | error ("friend declaration does not name a class or " |
12679 | "function"); | |
12680 | else | |
19db77ce KL |
12681 | make_friend_class (current_class_type, type, |
12682 | /*complain=*/true); | |
a723baf1 MM |
12683 | } |
12684 | /* If there is no TYPE, an error message will already have | |
12685 | been issued. */ | |
12686 | else if (!type) | |
12687 | ; | |
12688 | /* An anonymous aggregate has to be handled specially; such | |
12689 | a declaration really declares a data member (with a | |
12690 | particular type), as opposed to a nested class. */ | |
12691 | else if (ANON_AGGR_TYPE_P (type)) | |
12692 | { | |
12693 | /* Remove constructors and such from TYPE, now that we | |
34cd5ae7 | 12694 | know it is an anonymous aggregate. */ |
a723baf1 MM |
12695 | fixup_anonymous_aggr (type); |
12696 | /* And make the corresponding data member. */ | |
12697 | decl = build_decl (FIELD_DECL, NULL_TREE, type); | |
12698 | /* Add it to the class. */ | |
12699 | finish_member_declaration (decl); | |
12700 | } | |
37d407a1 KL |
12701 | else |
12702 | cp_parser_check_access_in_redeclaration (TYPE_NAME (type)); | |
a723baf1 MM |
12703 | } |
12704 | } | |
12705 | else | |
12706 | { | |
12707 | /* See if these declarations will be friends. */ | |
12708 | friend_p = cp_parser_friend_p (decl_specifiers); | |
12709 | ||
21526606 | 12710 | /* Keep going until we hit the `;' at the end of the |
a723baf1 MM |
12711 | declaration. */ |
12712 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) | |
12713 | { | |
12714 | tree attributes = NULL_TREE; | |
12715 | tree first_attribute; | |
12716 | ||
12717 | /* Peek at the next token. */ | |
12718 | token = cp_lexer_peek_token (parser->lexer); | |
12719 | ||
12720 | /* Check for a bitfield declaration. */ | |
12721 | if (token->type == CPP_COLON | |
12722 | || (token->type == CPP_NAME | |
21526606 | 12723 | && cp_lexer_peek_nth_token (parser->lexer, 2)->type |
a723baf1 MM |
12724 | == CPP_COLON)) |
12725 | { | |
12726 | tree identifier; | |
12727 | tree width; | |
12728 | ||
12729 | /* Get the name of the bitfield. Note that we cannot just | |
12730 | check TOKEN here because it may have been invalidated by | |
12731 | the call to cp_lexer_peek_nth_token above. */ | |
12732 | if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) | |
12733 | identifier = cp_parser_identifier (parser); | |
12734 | else | |
12735 | identifier = NULL_TREE; | |
12736 | ||
12737 | /* Consume the `:' token. */ | |
12738 | cp_lexer_consume_token (parser->lexer); | |
12739 | /* Get the width of the bitfield. */ | |
21526606 | 12740 | width |
14d22dd6 MM |
12741 | = cp_parser_constant_expression (parser, |
12742 | /*allow_non_constant=*/false, | |
12743 | NULL); | |
a723baf1 MM |
12744 | |
12745 | /* Look for attributes that apply to the bitfield. */ | |
12746 | attributes = cp_parser_attributes_opt (parser); | |
12747 | /* Remember which attributes are prefix attributes and | |
12748 | which are not. */ | |
12749 | first_attribute = attributes; | |
12750 | /* Combine the attributes. */ | |
12751 | attributes = chainon (prefix_attributes, attributes); | |
12752 | ||
12753 | /* Create the bitfield declaration. */ | |
21526606 | 12754 | decl = grokbitfield (identifier, |
a723baf1 MM |
12755 | decl_specifiers, |
12756 | width); | |
12757 | /* Apply the attributes. */ | |
12758 | cplus_decl_attributes (&decl, attributes, /*flags=*/0); | |
12759 | } | |
12760 | else | |
12761 | { | |
12762 | tree declarator; | |
12763 | tree initializer; | |
12764 | tree asm_specification; | |
7efa3e22 | 12765 | int ctor_dtor_or_conv_p; |
a723baf1 MM |
12766 | |
12767 | /* Parse the declarator. */ | |
21526606 | 12768 | declarator |
62b8a44e | 12769 | = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, |
4bb8ca28 MM |
12770 | &ctor_dtor_or_conv_p, |
12771 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
12772 | |
12773 | /* If something went wrong parsing the declarator, make sure | |
12774 | that we at least consume some tokens. */ | |
12775 | if (declarator == error_mark_node) | |
12776 | { | |
12777 | /* Skip to the end of the statement. */ | |
12778 | cp_parser_skip_to_end_of_statement (parser); | |
4bb8ca28 MM |
12779 | /* If the next token is not a semicolon, that is |
12780 | probably because we just skipped over the body of | |
12781 | a function. So, we consume a semicolon if | |
12782 | present, but do not issue an error message if it | |
12783 | is not present. */ | |
12784 | if (cp_lexer_next_token_is (parser->lexer, | |
12785 | CPP_SEMICOLON)) | |
12786 | cp_lexer_consume_token (parser->lexer); | |
12787 | return; | |
a723baf1 MM |
12788 | } |
12789 | ||
21526606 | 12790 | cp_parser_check_for_definition_in_return_type |
560ad596 MM |
12791 | (declarator, declares_class_or_enum); |
12792 | ||
a723baf1 MM |
12793 | /* Look for an asm-specification. */ |
12794 | asm_specification = cp_parser_asm_specification_opt (parser); | |
12795 | /* Look for attributes that apply to the declaration. */ | |
12796 | attributes = cp_parser_attributes_opt (parser); | |
12797 | /* Remember which attributes are prefix attributes and | |
12798 | which are not. */ | |
12799 | first_attribute = attributes; | |
12800 | /* Combine the attributes. */ | |
12801 | attributes = chainon (prefix_attributes, attributes); | |
12802 | ||
12803 | /* If it's an `=', then we have a constant-initializer or a | |
12804 | pure-specifier. It is not correct to parse the | |
12805 | initializer before registering the member declaration | |
12806 | since the member declaration should be in scope while | |
12807 | its initializer is processed. However, the rest of the | |
12808 | front end does not yet provide an interface that allows | |
12809 | us to handle this correctly. */ | |
12810 | if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) | |
12811 | { | |
12812 | /* In [class.mem]: | |
12813 | ||
12814 | A pure-specifier shall be used only in the declaration of | |
21526606 | 12815 | a virtual function. |
a723baf1 MM |
12816 | |
12817 | A member-declarator can contain a constant-initializer | |
12818 | only if it declares a static member of integral or | |
21526606 | 12819 | enumeration type. |
a723baf1 MM |
12820 | |
12821 | Therefore, if the DECLARATOR is for a function, we look | |
12822 | for a pure-specifier; otherwise, we look for a | |
12823 | constant-initializer. When we call `grokfield', it will | |
12824 | perform more stringent semantics checks. */ | |
12825 | if (TREE_CODE (declarator) == CALL_EXPR) | |
12826 | initializer = cp_parser_pure_specifier (parser); | |
12827 | else | |
4bb8ca28 MM |
12828 | /* Parse the initializer. */ |
12829 | initializer = cp_parser_constant_initializer (parser); | |
a723baf1 MM |
12830 | } |
12831 | /* Otherwise, there is no initializer. */ | |
12832 | else | |
12833 | initializer = NULL_TREE; | |
12834 | ||
12835 | /* See if we are probably looking at a function | |
12836 | definition. We are certainly not looking at at a | |
12837 | member-declarator. Calling `grokfield' has | |
12838 | side-effects, so we must not do it unless we are sure | |
12839 | that we are looking at a member-declarator. */ | |
21526606 | 12840 | if (cp_parser_token_starts_function_definition_p |
a723baf1 | 12841 | (cp_lexer_peek_token (parser->lexer))) |
4bb8ca28 MM |
12842 | { |
12843 | /* The grammar does not allow a pure-specifier to be | |
12844 | used when a member function is defined. (It is | |
12845 | possible that this fact is an oversight in the | |
12846 | standard, since a pure function may be defined | |
12847 | outside of the class-specifier. */ | |
12848 | if (initializer) | |
12849 | error ("pure-specifier on function-definition"); | |
12850 | decl = cp_parser_save_member_function_body (parser, | |
12851 | decl_specifiers, | |
12852 | declarator, | |
12853 | attributes); | |
12854 | /* If the member was not a friend, declare it here. */ | |
12855 | if (!friend_p) | |
12856 | finish_member_declaration (decl); | |
12857 | /* Peek at the next token. */ | |
12858 | token = cp_lexer_peek_token (parser->lexer); | |
12859 | /* If the next token is a semicolon, consume it. */ | |
12860 | if (token->type == CPP_SEMICOLON) | |
12861 | cp_lexer_consume_token (parser->lexer); | |
12862 | return; | |
12863 | } | |
a723baf1 | 12864 | else |
39703eb9 MM |
12865 | { |
12866 | /* Create the declaration. */ | |
21526606 | 12867 | decl = grokfield (declarator, decl_specifiers, |
ee3071ef | 12868 | initializer, asm_specification, |
39703eb9 MM |
12869 | attributes); |
12870 | /* Any initialization must have been from a | |
12871 | constant-expression. */ | |
12872 | if (decl && TREE_CODE (decl) == VAR_DECL && initializer) | |
12873 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; | |
12874 | } | |
a723baf1 MM |
12875 | } |
12876 | ||
12877 | /* Reset PREFIX_ATTRIBUTES. */ | |
12878 | while (attributes && TREE_CHAIN (attributes) != first_attribute) | |
12879 | attributes = TREE_CHAIN (attributes); | |
12880 | if (attributes) | |
12881 | TREE_CHAIN (attributes) = NULL_TREE; | |
12882 | ||
12883 | /* If there is any qualification still in effect, clear it | |
12884 | now; we will be starting fresh with the next declarator. */ | |
12885 | parser->scope = NULL_TREE; | |
12886 | parser->qualifying_scope = NULL_TREE; | |
12887 | parser->object_scope = NULL_TREE; | |
12888 | /* If it's a `,', then there are more declarators. */ | |
12889 | if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) | |
12890 | cp_lexer_consume_token (parser->lexer); | |
12891 | /* If the next token isn't a `;', then we have a parse error. */ | |
12892 | else if (cp_lexer_next_token_is_not (parser->lexer, | |
12893 | CPP_SEMICOLON)) | |
12894 | { | |
12895 | cp_parser_error (parser, "expected `;'"); | |
04c06002 | 12896 | /* Skip tokens until we find a `;'. */ |
a723baf1 MM |
12897 | cp_parser_skip_to_end_of_statement (parser); |
12898 | ||
12899 | break; | |
12900 | } | |
12901 | ||
12902 | if (decl) | |
12903 | { | |
12904 | /* Add DECL to the list of members. */ | |
12905 | if (!friend_p) | |
12906 | finish_member_declaration (decl); | |
12907 | ||
a723baf1 | 12908 | if (TREE_CODE (decl) == FUNCTION_DECL) |
8db1028e | 12909 | cp_parser_save_default_args (parser, decl); |
a723baf1 MM |
12910 | } |
12911 | } | |
12912 | } | |
12913 | ||
4bb8ca28 | 12914 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); |
a723baf1 MM |
12915 | } |
12916 | ||
12917 | /* Parse a pure-specifier. | |
12918 | ||
12919 | pure-specifier: | |
12920 | = 0 | |
12921 | ||
12922 | Returns INTEGER_ZERO_NODE if a pure specifier is found. | |
cd0be382 | 12923 | Otherwise, ERROR_MARK_NODE is returned. */ |
a723baf1 MM |
12924 | |
12925 | static tree | |
94edc4ab | 12926 | cp_parser_pure_specifier (cp_parser* parser) |
a723baf1 MM |
12927 | { |
12928 | cp_token *token; | |
12929 | ||
12930 | /* Look for the `=' token. */ | |
12931 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12932 | return error_mark_node; | |
12933 | /* Look for the `0' token. */ | |
12934 | token = cp_parser_require (parser, CPP_NUMBER, "`0'"); | |
12935 | /* Unfortunately, this will accept `0L' and `0x00' as well. We need | |
12936 | to get information from the lexer about how the number was | |
12937 | spelled in order to fix this problem. */ | |
12938 | if (!token || !integer_zerop (token->value)) | |
12939 | return error_mark_node; | |
12940 | ||
12941 | return integer_zero_node; | |
12942 | } | |
12943 | ||
12944 | /* Parse a constant-initializer. | |
12945 | ||
12946 | constant-initializer: | |
12947 | = constant-expression | |
12948 | ||
12949 | Returns a representation of the constant-expression. */ | |
12950 | ||
12951 | static tree | |
94edc4ab | 12952 | cp_parser_constant_initializer (cp_parser* parser) |
a723baf1 MM |
12953 | { |
12954 | /* Look for the `=' token. */ | |
12955 | if (!cp_parser_require (parser, CPP_EQ, "`='")) | |
12956 | return error_mark_node; | |
12957 | ||
12958 | /* It is invalid to write: | |
12959 | ||
12960 | struct S { static const int i = { 7 }; }; | |
12961 | ||
12962 | */ | |
12963 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) | |
12964 | { | |
12965 | cp_parser_error (parser, | |
12966 | "a brace-enclosed initializer is not allowed here"); | |
12967 | /* Consume the opening brace. */ | |
12968 | cp_lexer_consume_token (parser->lexer); | |
12969 | /* Skip the initializer. */ | |
12970 | cp_parser_skip_to_closing_brace (parser); | |
12971 | /* Look for the trailing `}'. */ | |
12972 | cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'"); | |
21526606 | 12973 | |
a723baf1 MM |
12974 | return error_mark_node; |
12975 | } | |
12976 | ||
21526606 | 12977 | return cp_parser_constant_expression (parser, |
14d22dd6 MM |
12978 | /*allow_non_constant=*/false, |
12979 | NULL); | |
a723baf1 MM |
12980 | } |
12981 | ||
12982 | /* Derived classes [gram.class.derived] */ | |
12983 | ||
12984 | /* Parse a base-clause. | |
12985 | ||
12986 | base-clause: | |
21526606 | 12987 | : base-specifier-list |
a723baf1 MM |
12988 | |
12989 | base-specifier-list: | |
12990 | base-specifier | |
12991 | base-specifier-list , base-specifier | |
12992 | ||
12993 | Returns a TREE_LIST representing the base-classes, in the order in | |
12994 | which they were declared. The representation of each node is as | |
21526606 | 12995 | described by cp_parser_base_specifier. |
a723baf1 MM |
12996 | |
12997 | In the case that no bases are specified, this function will return | |
12998 | NULL_TREE, not ERROR_MARK_NODE. */ | |
12999 | ||
13000 | static tree | |
94edc4ab | 13001 | cp_parser_base_clause (cp_parser* parser) |
a723baf1 MM |
13002 | { |
13003 | tree bases = NULL_TREE; | |
13004 | ||
13005 | /* Look for the `:' that begins the list. */ | |
13006 | cp_parser_require (parser, CPP_COLON, "`:'"); | |
13007 | ||
13008 | /* Scan the base-specifier-list. */ | |
13009 | while (true) | |
13010 | { | |
13011 | cp_token *token; | |
13012 | tree base; | |
13013 | ||
13014 | /* Look for the base-specifier. */ | |
13015 | base = cp_parser_base_specifier (parser); | |
13016 | /* Add BASE to the front of the list. */ | |
13017 | if (base != error_mark_node) | |
13018 | { | |
13019 | TREE_CHAIN (base) = bases; | |
13020 | bases = base; | |
13021 | } | |
13022 | /* Peek at the next token. */ | |
13023 | token = cp_lexer_peek_token (parser->lexer); | |
13024 | /* If it's not a comma, then the list is complete. */ | |
13025 | if (token->type != CPP_COMMA) | |
13026 | break; | |
13027 | /* Consume the `,'. */ | |
13028 | cp_lexer_consume_token (parser->lexer); | |
13029 | } | |
13030 | ||
13031 | /* PARSER->SCOPE may still be non-NULL at this point, if the last | |
13032 | base class had a qualified name. However, the next name that | |
13033 | appears is certainly not qualified. */ | |
13034 | parser->scope = NULL_TREE; | |
13035 | parser->qualifying_scope = NULL_TREE; | |
13036 | parser->object_scope = NULL_TREE; | |
13037 | ||
13038 | return nreverse (bases); | |
13039 | } | |
13040 | ||
13041 | /* Parse a base-specifier. | |
13042 | ||
13043 | base-specifier: | |
13044 | :: [opt] nested-name-specifier [opt] class-name | |
13045 | virtual access-specifier [opt] :: [opt] nested-name-specifier | |
13046 | [opt] class-name | |
13047 | access-specifier virtual [opt] :: [opt] nested-name-specifier | |
13048 | [opt] class-name | |
13049 | ||
13050 | Returns a TREE_LIST. The TREE_PURPOSE will be one of | |
13051 | ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to | |
13052 | indicate the specifiers provided. The TREE_VALUE will be a TYPE | |
13053 | (or the ERROR_MARK_NODE) indicating the type that was specified. */ | |
21526606 | 13054 | |
a723baf1 | 13055 | static tree |
94edc4ab | 13056 | cp_parser_base_specifier (cp_parser* parser) |
a723baf1 MM |
13057 | { |
13058 | cp_token *token; | |
13059 | bool done = false; | |
13060 | bool virtual_p = false; | |
13061 | bool duplicate_virtual_error_issued_p = false; | |
13062 | bool duplicate_access_error_issued_p = false; | |
bbaab916 | 13063 | bool class_scope_p, template_p; |
dbbf88d1 | 13064 | tree access = access_default_node; |
a723baf1 MM |
13065 | tree type; |
13066 | ||
13067 | /* Process the optional `virtual' and `access-specifier'. */ | |
13068 | while (!done) | |
13069 | { | |
13070 | /* Peek at the next token. */ | |
13071 | token = cp_lexer_peek_token (parser->lexer); | |
13072 | /* Process `virtual'. */ | |
13073 | switch (token->keyword) | |
13074 | { | |
13075 | case RID_VIRTUAL: | |
13076 | /* If `virtual' appears more than once, issue an error. */ | |
13077 | if (virtual_p && !duplicate_virtual_error_issued_p) | |
13078 | { | |
13079 | cp_parser_error (parser, | |
13080 | "`virtual' specified more than once in base-specified"); | |
13081 | duplicate_virtual_error_issued_p = true; | |
13082 | } | |
13083 | ||
13084 | virtual_p = true; | |
13085 | ||
13086 | /* Consume the `virtual' token. */ | |
13087 | cp_lexer_consume_token (parser->lexer); | |
13088 | ||
13089 | break; | |
13090 | ||
13091 | case RID_PUBLIC: | |
13092 | case RID_PROTECTED: | |
13093 | case RID_PRIVATE: | |
13094 | /* If more than one access specifier appears, issue an | |
13095 | error. */ | |
dbbf88d1 NS |
13096 | if (access != access_default_node |
13097 | && !duplicate_access_error_issued_p) | |
a723baf1 MM |
13098 | { |
13099 | cp_parser_error (parser, | |
13100 | "more than one access specifier in base-specified"); | |
13101 | duplicate_access_error_issued_p = true; | |
13102 | } | |
13103 | ||
dbbf88d1 | 13104 | access = ridpointers[(int) token->keyword]; |
a723baf1 MM |
13105 | |
13106 | /* Consume the access-specifier. */ | |
13107 | cp_lexer_consume_token (parser->lexer); | |
13108 | ||
13109 | break; | |
13110 | ||
13111 | default: | |
13112 | done = true; | |
13113 | break; | |
13114 | } | |
13115 | } | |
852dcbdd | 13116 | /* It is not uncommon to see programs mechanically, erroneously, use |
a3a503a5 | 13117 | the 'typename' keyword to denote (dependent) qualified types |
1ed53ef3 GB |
13118 | as base classes. */ |
13119 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) | |
13120 | { | |
13121 | if (!processing_template_decl) | |
13122 | error ("keyword `typename' not allowed outside of templates"); | |
13123 | else | |
13124 | error ("keyword `typename' not allowed in this context " | |
13125 | "(the base class is implicitly a type)"); | |
13126 | cp_lexer_consume_token (parser->lexer); | |
13127 | } | |
a723baf1 | 13128 | |
a723baf1 MM |
13129 | /* Look for the optional `::' operator. */ |
13130 | cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); | |
13131 | /* Look for the nested-name-specifier. The simplest way to | |
13132 | implement: | |
13133 | ||
13134 | [temp.res] | |
13135 | ||
13136 | The keyword `typename' is not permitted in a base-specifier or | |
13137 | mem-initializer; in these contexts a qualified name that | |
13138 | depends on a template-parameter is implicitly assumed to be a | |
13139 | type name. | |
13140 | ||
13141 | is to pretend that we have seen the `typename' keyword at this | |
21526606 | 13142 | point. */ |
a723baf1 MM |
13143 | cp_parser_nested_name_specifier_opt (parser, |
13144 | /*typename_keyword_p=*/true, | |
13145 | /*check_dependency_p=*/true, | |
a668c6ad MM |
13146 | /*type_p=*/true, |
13147 | /*is_declaration=*/true); | |
a723baf1 MM |
13148 | /* If the base class is given by a qualified name, assume that names |
13149 | we see are type names or templates, as appropriate. */ | |
13150 | class_scope_p = (parser->scope && TYPE_P (parser->scope)); | |
bbaab916 | 13151 | template_p = class_scope_p && cp_parser_optional_template_keyword (parser); |
21526606 | 13152 | |
a723baf1 | 13153 | /* Finally, look for the class-name. */ |
21526606 | 13154 | type = cp_parser_class_name (parser, |
a723baf1 | 13155 | class_scope_p, |
bbaab916 | 13156 | template_p, |
a723baf1 | 13157 | /*type_p=*/true, |
a723baf1 | 13158 | /*check_dependency_p=*/true, |
a668c6ad MM |
13159 | /*class_head_p=*/false, |
13160 | /*is_declaration=*/true); | |
a723baf1 MM |
13161 | |
13162 | if (type == error_mark_node) | |
13163 | return error_mark_node; | |
13164 | ||
dbbf88d1 | 13165 | return finish_base_specifier (TREE_TYPE (type), access, virtual_p); |
a723baf1 MM |
13166 | } |
13167 | ||
13168 | /* Exception handling [gram.exception] */ | |
13169 | ||
13170 | /* Parse an (optional) exception-specification. | |
13171 | ||
13172 | exception-specification: | |
13173 | throw ( type-id-list [opt] ) | |
13174 | ||
13175 | Returns a TREE_LIST representing the exception-specification. The | |
13176 | TREE_VALUE of each node is a type. */ | |
13177 | ||
13178 | static tree | |
94edc4ab | 13179 | cp_parser_exception_specification_opt (cp_parser* parser) |
a723baf1 MM |
13180 | { |
13181 | cp_token *token; | |
13182 | tree type_id_list; | |
13183 | ||
13184 | /* Peek at the next token. */ | |
13185 | token = cp_lexer_peek_token (parser->lexer); | |
13186 | /* If it's not `throw', then there's no exception-specification. */ | |
13187 | if (!cp_parser_is_keyword (token, RID_THROW)) | |
13188 | return NULL_TREE; | |
13189 | ||
13190 | /* Consume the `throw'. */ | |
13191 | cp_lexer_consume_token (parser->lexer); | |
13192 | ||
13193 | /* Look for the `('. */ | |
13194 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13195 | ||
13196 | /* Peek at the next token. */ | |
13197 | token = cp_lexer_peek_token (parser->lexer); | |
13198 | /* If it's not a `)', then there is a type-id-list. */ | |
13199 | if (token->type != CPP_CLOSE_PAREN) | |
13200 | { | |
13201 | const char *saved_message; | |
13202 | ||
13203 | /* Types may not be defined in an exception-specification. */ | |
13204 | saved_message = parser->type_definition_forbidden_message; | |
13205 | parser->type_definition_forbidden_message | |
13206 | = "types may not be defined in an exception-specification"; | |
13207 | /* Parse the type-id-list. */ | |
13208 | type_id_list = cp_parser_type_id_list (parser); | |
13209 | /* Restore the saved message. */ | |
13210 | parser->type_definition_forbidden_message = saved_message; | |
13211 | } | |
13212 | else | |
13213 | type_id_list = empty_except_spec; | |
13214 | ||
13215 | /* Look for the `)'. */ | |
13216 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13217 | ||
13218 | return type_id_list; | |
13219 | } | |
13220 | ||
13221 | /* Parse an (optional) type-id-list. | |
13222 | ||
13223 | type-id-list: | |
13224 | type-id | |
13225 | type-id-list , type-id | |
13226 | ||
13227 | Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, | |
13228 | in the order that the types were presented. */ | |
13229 | ||
13230 | static tree | |
94edc4ab | 13231 | cp_parser_type_id_list (cp_parser* parser) |
a723baf1 MM |
13232 | { |
13233 | tree types = NULL_TREE; | |
13234 | ||
13235 | while (true) | |
13236 | { | |
13237 | cp_token *token; | |
13238 | tree type; | |
13239 | ||
13240 | /* Get the next type-id. */ | |
13241 | type = cp_parser_type_id (parser); | |
13242 | /* Add it to the list. */ | |
13243 | types = add_exception_specifier (types, type, /*complain=*/1); | |
13244 | /* Peek at the next token. */ | |
13245 | token = cp_lexer_peek_token (parser->lexer); | |
13246 | /* If it is not a `,', we are done. */ | |
13247 | if (token->type != CPP_COMMA) | |
13248 | break; | |
13249 | /* Consume the `,'. */ | |
13250 | cp_lexer_consume_token (parser->lexer); | |
13251 | } | |
13252 | ||
13253 | return nreverse (types); | |
13254 | } | |
13255 | ||
13256 | /* Parse a try-block. | |
13257 | ||
13258 | try-block: | |
13259 | try compound-statement handler-seq */ | |
13260 | ||
13261 | static tree | |
94edc4ab | 13262 | cp_parser_try_block (cp_parser* parser) |
a723baf1 MM |
13263 | { |
13264 | tree try_block; | |
13265 | ||
13266 | cp_parser_require_keyword (parser, RID_TRY, "`try'"); | |
13267 | try_block = begin_try_block (); | |
a5bcc582 | 13268 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
13269 | finish_try_block (try_block); |
13270 | cp_parser_handler_seq (parser); | |
13271 | finish_handler_sequence (try_block); | |
13272 | ||
13273 | return try_block; | |
13274 | } | |
13275 | ||
13276 | /* Parse a function-try-block. | |
13277 | ||
13278 | function-try-block: | |
13279 | try ctor-initializer [opt] function-body handler-seq */ | |
13280 | ||
13281 | static bool | |
94edc4ab | 13282 | cp_parser_function_try_block (cp_parser* parser) |
a723baf1 MM |
13283 | { |
13284 | tree try_block; | |
13285 | bool ctor_initializer_p; | |
13286 | ||
13287 | /* Look for the `try' keyword. */ | |
13288 | if (!cp_parser_require_keyword (parser, RID_TRY, "`try'")) | |
13289 | return false; | |
13290 | /* Let the rest of the front-end know where we are. */ | |
13291 | try_block = begin_function_try_block (); | |
13292 | /* Parse the function-body. */ | |
21526606 | 13293 | ctor_initializer_p |
a723baf1 MM |
13294 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
13295 | /* We're done with the `try' part. */ | |
13296 | finish_function_try_block (try_block); | |
13297 | /* Parse the handlers. */ | |
13298 | cp_parser_handler_seq (parser); | |
13299 | /* We're done with the handlers. */ | |
13300 | finish_function_handler_sequence (try_block); | |
13301 | ||
13302 | return ctor_initializer_p; | |
13303 | } | |
13304 | ||
13305 | /* Parse a handler-seq. | |
13306 | ||
13307 | handler-seq: | |
13308 | handler handler-seq [opt] */ | |
13309 | ||
13310 | static void | |
94edc4ab | 13311 | cp_parser_handler_seq (cp_parser* parser) |
a723baf1 MM |
13312 | { |
13313 | while (true) | |
13314 | { | |
13315 | cp_token *token; | |
13316 | ||
13317 | /* Parse the handler. */ | |
13318 | cp_parser_handler (parser); | |
13319 | /* Peek at the next token. */ | |
13320 | token = cp_lexer_peek_token (parser->lexer); | |
13321 | /* If it's not `catch' then there are no more handlers. */ | |
13322 | if (!cp_parser_is_keyword (token, RID_CATCH)) | |
13323 | break; | |
13324 | } | |
13325 | } | |
13326 | ||
13327 | /* Parse a handler. | |
13328 | ||
13329 | handler: | |
13330 | catch ( exception-declaration ) compound-statement */ | |
13331 | ||
13332 | static void | |
94edc4ab | 13333 | cp_parser_handler (cp_parser* parser) |
a723baf1 MM |
13334 | { |
13335 | tree handler; | |
13336 | tree declaration; | |
13337 | ||
13338 | cp_parser_require_keyword (parser, RID_CATCH, "`catch'"); | |
13339 | handler = begin_handler (); | |
13340 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13341 | declaration = cp_parser_exception_declaration (parser); | |
13342 | finish_handler_parms (declaration, handler); | |
13343 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
a5bcc582 | 13344 | cp_parser_compound_statement (parser, false); |
a723baf1 MM |
13345 | finish_handler (handler); |
13346 | } | |
13347 | ||
13348 | /* Parse an exception-declaration. | |
13349 | ||
13350 | exception-declaration: | |
13351 | type-specifier-seq declarator | |
13352 | type-specifier-seq abstract-declarator | |
13353 | type-specifier-seq | |
21526606 | 13354 | ... |
a723baf1 MM |
13355 | |
13356 | Returns a VAR_DECL for the declaration, or NULL_TREE if the | |
13357 | ellipsis variant is used. */ | |
13358 | ||
13359 | static tree | |
94edc4ab | 13360 | cp_parser_exception_declaration (cp_parser* parser) |
a723baf1 MM |
13361 | { |
13362 | tree type_specifiers; | |
13363 | tree declarator; | |
13364 | const char *saved_message; | |
13365 | ||
13366 | /* If it's an ellipsis, it's easy to handle. */ | |
13367 | if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) | |
13368 | { | |
13369 | /* Consume the `...' token. */ | |
13370 | cp_lexer_consume_token (parser->lexer); | |
13371 | return NULL_TREE; | |
13372 | } | |
13373 | ||
13374 | /* Types may not be defined in exception-declarations. */ | |
13375 | saved_message = parser->type_definition_forbidden_message; | |
13376 | parser->type_definition_forbidden_message | |
13377 | = "types may not be defined in exception-declarations"; | |
13378 | ||
13379 | /* Parse the type-specifier-seq. */ | |
13380 | type_specifiers = cp_parser_type_specifier_seq (parser); | |
13381 | /* If it's a `)', then there is no declarator. */ | |
13382 | if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) | |
13383 | declarator = NULL_TREE; | |
13384 | else | |
62b8a44e | 13385 | declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, |
4bb8ca28 MM |
13386 | /*ctor_dtor_or_conv_p=*/NULL, |
13387 | /*parenthesized_p=*/NULL); | |
a723baf1 MM |
13388 | |
13389 | /* Restore the saved message. */ | |
13390 | parser->type_definition_forbidden_message = saved_message; | |
13391 | ||
13392 | return start_handler_parms (type_specifiers, declarator); | |
13393 | } | |
13394 | ||
21526606 | 13395 | /* Parse a throw-expression. |
a723baf1 MM |
13396 | |
13397 | throw-expression: | |
34cd5ae7 | 13398 | throw assignment-expression [opt] |
a723baf1 MM |
13399 | |
13400 | Returns a THROW_EXPR representing the throw-expression. */ | |
13401 | ||
13402 | static tree | |
94edc4ab | 13403 | cp_parser_throw_expression (cp_parser* parser) |
a723baf1 MM |
13404 | { |
13405 | tree expression; | |
89f1a6ec | 13406 | cp_token* token; |
a723baf1 MM |
13407 | |
13408 | cp_parser_require_keyword (parser, RID_THROW, "`throw'"); | |
89f1a6ec MM |
13409 | token = cp_lexer_peek_token (parser->lexer); |
13410 | /* Figure out whether or not there is an assignment-expression | |
13411 | following the "throw" keyword. */ | |
13412 | if (token->type == CPP_COMMA | |
13413 | || token->type == CPP_SEMICOLON | |
13414 | || token->type == CPP_CLOSE_PAREN | |
13415 | || token->type == CPP_CLOSE_SQUARE | |
13416 | || token->type == CPP_CLOSE_BRACE | |
13417 | || token->type == CPP_COLON) | |
a723baf1 | 13418 | expression = NULL_TREE; |
89f1a6ec MM |
13419 | else |
13420 | expression = cp_parser_assignment_expression (parser); | |
a723baf1 MM |
13421 | |
13422 | return build_throw (expression); | |
13423 | } | |
13424 | ||
13425 | /* GNU Extensions */ | |
13426 | ||
13427 | /* Parse an (optional) asm-specification. | |
13428 | ||
13429 | asm-specification: | |
13430 | asm ( string-literal ) | |
13431 | ||
13432 | If the asm-specification is present, returns a STRING_CST | |
13433 | corresponding to the string-literal. Otherwise, returns | |
13434 | NULL_TREE. */ | |
13435 | ||
13436 | static tree | |
94edc4ab | 13437 | cp_parser_asm_specification_opt (cp_parser* parser) |
a723baf1 MM |
13438 | { |
13439 | cp_token *token; | |
13440 | tree asm_specification; | |
13441 | ||
13442 | /* Peek at the next token. */ | |
13443 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 13444 | /* If the next token isn't the `asm' keyword, then there's no |
a723baf1 MM |
13445 | asm-specification. */ |
13446 | if (!cp_parser_is_keyword (token, RID_ASM)) | |
13447 | return NULL_TREE; | |
13448 | ||
13449 | /* Consume the `asm' token. */ | |
13450 | cp_lexer_consume_token (parser->lexer); | |
13451 | /* Look for the `('. */ | |
13452 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13453 | ||
13454 | /* Look for the string-literal. */ | |
13455 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13456 | if (token) | |
13457 | asm_specification = token->value; | |
13458 | else | |
13459 | asm_specification = NULL_TREE; | |
13460 | ||
13461 | /* Look for the `)'. */ | |
13462 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`('"); | |
13463 | ||
13464 | return asm_specification; | |
13465 | } | |
13466 | ||
21526606 | 13467 | /* Parse an asm-operand-list. |
a723baf1 MM |
13468 | |
13469 | asm-operand-list: | |
13470 | asm-operand | |
13471 | asm-operand-list , asm-operand | |
21526606 | 13472 | |
a723baf1 | 13473 | asm-operand: |
21526606 | 13474 | string-literal ( expression ) |
a723baf1 MM |
13475 | [ string-literal ] string-literal ( expression ) |
13476 | ||
13477 | Returns a TREE_LIST representing the operands. The TREE_VALUE of | |
13478 | each node is the expression. The TREE_PURPOSE is itself a | |
13479 | TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed | |
13480 | string-literal (or NULL_TREE if not present) and whose TREE_VALUE | |
13481 | is a STRING_CST for the string literal before the parenthesis. */ | |
13482 | ||
13483 | static tree | |
94edc4ab | 13484 | cp_parser_asm_operand_list (cp_parser* parser) |
a723baf1 MM |
13485 | { |
13486 | tree asm_operands = NULL_TREE; | |
13487 | ||
13488 | while (true) | |
13489 | { | |
13490 | tree string_literal; | |
13491 | tree expression; | |
13492 | tree name; | |
13493 | cp_token *token; | |
21526606 | 13494 | |
21526606 | 13495 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) |
a723baf1 MM |
13496 | { |
13497 | /* Consume the `[' token. */ | |
13498 | cp_lexer_consume_token (parser->lexer); | |
13499 | /* Read the operand name. */ | |
13500 | name = cp_parser_identifier (parser); | |
21526606 | 13501 | if (name != error_mark_node) |
a723baf1 MM |
13502 | name = build_string (IDENTIFIER_LENGTH (name), |
13503 | IDENTIFIER_POINTER (name)); | |
13504 | /* Look for the closing `]'. */ | |
13505 | cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"); | |
13506 | } | |
13507 | else | |
13508 | name = NULL_TREE; | |
13509 | /* Look for the string-literal. */ | |
13510 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13511 | string_literal = token ? token->value : error_mark_node; | |
0173bb6f | 13512 | c_lex_string_translate = 1; |
a723baf1 MM |
13513 | /* Look for the `('. */ |
13514 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13515 | /* Parse the expression. */ | |
13516 | expression = cp_parser_expression (parser); | |
13517 | /* Look for the `)'. */ | |
13518 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
0173bb6f | 13519 | c_lex_string_translate = 0; |
a723baf1 MM |
13520 | /* Add this operand to the list. */ |
13521 | asm_operands = tree_cons (build_tree_list (name, string_literal), | |
21526606 | 13522 | expression, |
a723baf1 | 13523 | asm_operands); |
21526606 | 13524 | /* If the next token is not a `,', there are no more |
a723baf1 MM |
13525 | operands. */ |
13526 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13527 | break; | |
13528 | /* Consume the `,'. */ | |
13529 | cp_lexer_consume_token (parser->lexer); | |
13530 | } | |
13531 | ||
13532 | return nreverse (asm_operands); | |
13533 | } | |
13534 | ||
21526606 | 13535 | /* Parse an asm-clobber-list. |
a723baf1 MM |
13536 | |
13537 | asm-clobber-list: | |
13538 | string-literal | |
21526606 | 13539 | asm-clobber-list , string-literal |
a723baf1 MM |
13540 | |
13541 | Returns a TREE_LIST, indicating the clobbers in the order that they | |
13542 | appeared. The TREE_VALUE of each node is a STRING_CST. */ | |
13543 | ||
13544 | static tree | |
94edc4ab | 13545 | cp_parser_asm_clobber_list (cp_parser* parser) |
a723baf1 MM |
13546 | { |
13547 | tree clobbers = NULL_TREE; | |
13548 | ||
13549 | while (true) | |
13550 | { | |
13551 | cp_token *token; | |
13552 | tree string_literal; | |
13553 | ||
13554 | /* Look for the string literal. */ | |
13555 | token = cp_parser_require (parser, CPP_STRING, "string-literal"); | |
13556 | string_literal = token ? token->value : error_mark_node; | |
13557 | /* Add it to the list. */ | |
13558 | clobbers = tree_cons (NULL_TREE, string_literal, clobbers); | |
21526606 | 13559 | /* If the next token is not a `,', then the list is |
a723baf1 MM |
13560 | complete. */ |
13561 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) | |
13562 | break; | |
13563 | /* Consume the `,' token. */ | |
13564 | cp_lexer_consume_token (parser->lexer); | |
13565 | } | |
13566 | ||
13567 | return clobbers; | |
13568 | } | |
13569 | ||
13570 | /* Parse an (optional) series of attributes. | |
13571 | ||
13572 | attributes: | |
13573 | attributes attribute | |
13574 | ||
13575 | attribute: | |
21526606 | 13576 | __attribute__ (( attribute-list [opt] )) |
a723baf1 MM |
13577 | |
13578 | The return value is as for cp_parser_attribute_list. */ | |
21526606 | 13579 | |
a723baf1 | 13580 | static tree |
94edc4ab | 13581 | cp_parser_attributes_opt (cp_parser* parser) |
a723baf1 MM |
13582 | { |
13583 | tree attributes = NULL_TREE; | |
13584 | ||
13585 | while (true) | |
13586 | { | |
13587 | cp_token *token; | |
13588 | tree attribute_list; | |
13589 | ||
13590 | /* Peek at the next token. */ | |
13591 | token = cp_lexer_peek_token (parser->lexer); | |
13592 | /* If it's not `__attribute__', then we're done. */ | |
13593 | if (token->keyword != RID_ATTRIBUTE) | |
13594 | break; | |
13595 | ||
13596 | /* Consume the `__attribute__' keyword. */ | |
13597 | cp_lexer_consume_token (parser->lexer); | |
13598 | /* Look for the two `(' tokens. */ | |
13599 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13600 | cp_parser_require (parser, CPP_OPEN_PAREN, "`('"); | |
13601 | ||
13602 | /* Peek at the next token. */ | |
13603 | token = cp_lexer_peek_token (parser->lexer); | |
13604 | if (token->type != CPP_CLOSE_PAREN) | |
13605 | /* Parse the attribute-list. */ | |
13606 | attribute_list = cp_parser_attribute_list (parser); | |
13607 | else | |
13608 | /* If the next token is a `)', then there is no attribute | |
13609 | list. */ | |
13610 | attribute_list = NULL; | |
13611 | ||
13612 | /* Look for the two `)' tokens. */ | |
13613 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13614 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
13615 | ||
13616 | /* Add these new attributes to the list. */ | |
13617 | attributes = chainon (attributes, attribute_list); | |
13618 | } | |
13619 | ||
13620 | return attributes; | |
13621 | } | |
13622 | ||
21526606 | 13623 | /* Parse an attribute-list. |
a723baf1 | 13624 | |
21526606 EC |
13625 | attribute-list: |
13626 | attribute | |
a723baf1 MM |
13627 | attribute-list , attribute |
13628 | ||
13629 | attribute: | |
21526606 | 13630 | identifier |
a723baf1 MM |
13631 | identifier ( identifier ) |
13632 | identifier ( identifier , expression-list ) | |
21526606 | 13633 | identifier ( expression-list ) |
a723baf1 MM |
13634 | |
13635 | Returns a TREE_LIST. Each node corresponds to an attribute. THe | |
13636 | TREE_PURPOSE of each node is the identifier indicating which | |
13637 | attribute is in use. The TREE_VALUE represents the arguments, if | |
13638 | any. */ | |
13639 | ||
13640 | static tree | |
94edc4ab | 13641 | cp_parser_attribute_list (cp_parser* parser) |
a723baf1 MM |
13642 | { |
13643 | tree attribute_list = NULL_TREE; | |
13644 | ||
0173bb6f | 13645 | c_lex_string_translate = 0; |
a723baf1 MM |
13646 | while (true) |
13647 | { | |
13648 | cp_token *token; | |
13649 | tree identifier; | |
13650 | tree attribute; | |
13651 | ||
13652 | /* Look for the identifier. We also allow keywords here; for | |
13653 | example `__attribute__ ((const))' is legal. */ | |
13654 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 13655 | if (token->type != CPP_NAME |
a723baf1 MM |
13656 | && token->type != CPP_KEYWORD) |
13657 | return error_mark_node; | |
13658 | /* Consume the token. */ | |
13659 | token = cp_lexer_consume_token (parser->lexer); | |
21526606 | 13660 | |
a723baf1 MM |
13661 | /* Save away the identifier that indicates which attribute this is. */ |
13662 | identifier = token->value; | |
13663 | attribute = build_tree_list (identifier, NULL_TREE); | |
13664 | ||
13665 | /* Peek at the next token. */ | |
13666 | token = cp_lexer_peek_token (parser->lexer); | |
13667 | /* If it's an `(', then parse the attribute arguments. */ | |
13668 | if (token->type == CPP_OPEN_PAREN) | |
13669 | { | |
13670 | tree arguments; | |
a723baf1 | 13671 | |
21526606 | 13672 | arguments = (cp_parser_parenthesized_expression_list |
39703eb9 | 13673 | (parser, true, /*non_constant_p=*/NULL)); |
a723baf1 MM |
13674 | /* Save the identifier and arguments away. */ |
13675 | TREE_VALUE (attribute) = arguments; | |
a723baf1 MM |
13676 | } |
13677 | ||
13678 | /* Add this attribute to the list. */ | |
13679 | TREE_CHAIN (attribute) = attribute_list; | |
13680 | attribute_list = attribute; | |
13681 | ||
13682 | /* Now, look for more attributes. */ | |
13683 | token = cp_lexer_peek_token (parser->lexer); | |
13684 | /* If the next token isn't a `,', we're done. */ | |
13685 | if (token->type != CPP_COMMA) | |
13686 | break; | |
13687 | ||
cd0be382 | 13688 | /* Consume the comma and keep going. */ |
a723baf1 MM |
13689 | cp_lexer_consume_token (parser->lexer); |
13690 | } | |
0173bb6f | 13691 | c_lex_string_translate = 1; |
a723baf1 MM |
13692 | |
13693 | /* We built up the list in reverse order. */ | |
13694 | return nreverse (attribute_list); | |
13695 | } | |
13696 | ||
13697 | /* Parse an optional `__extension__' keyword. Returns TRUE if it is | |
13698 | present, and FALSE otherwise. *SAVED_PEDANTIC is set to the | |
13699 | current value of the PEDANTIC flag, regardless of whether or not | |
13700 | the `__extension__' keyword is present. The caller is responsible | |
13701 | for restoring the value of the PEDANTIC flag. */ | |
13702 | ||
13703 | static bool | |
94edc4ab | 13704 | cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) |
a723baf1 MM |
13705 | { |
13706 | /* Save the old value of the PEDANTIC flag. */ | |
13707 | *saved_pedantic = pedantic; | |
13708 | ||
13709 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) | |
13710 | { | |
13711 | /* Consume the `__extension__' token. */ | |
13712 | cp_lexer_consume_token (parser->lexer); | |
13713 | /* We're not being pedantic while the `__extension__' keyword is | |
13714 | in effect. */ | |
13715 | pedantic = 0; | |
13716 | ||
13717 | return true; | |
13718 | } | |
13719 | ||
13720 | return false; | |
13721 | } | |
13722 | ||
13723 | /* Parse a label declaration. | |
13724 | ||
13725 | label-declaration: | |
13726 | __label__ label-declarator-seq ; | |
13727 | ||
13728 | label-declarator-seq: | |
13729 | identifier , label-declarator-seq | |
13730 | identifier */ | |
13731 | ||
13732 | static void | |
94edc4ab | 13733 | cp_parser_label_declaration (cp_parser* parser) |
a723baf1 MM |
13734 | { |
13735 | /* Look for the `__label__' keyword. */ | |
13736 | cp_parser_require_keyword (parser, RID_LABEL, "`__label__'"); | |
13737 | ||
13738 | while (true) | |
13739 | { | |
13740 | tree identifier; | |
13741 | ||
13742 | /* Look for an identifier. */ | |
13743 | identifier = cp_parser_identifier (parser); | |
13744 | /* Declare it as a lobel. */ | |
13745 | finish_label_decl (identifier); | |
13746 | /* If the next token is a `;', stop. */ | |
13747 | if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
13748 | break; | |
13749 | /* Look for the `,' separating the label declarations. */ | |
13750 | cp_parser_require (parser, CPP_COMMA, "`,'"); | |
13751 | } | |
13752 | ||
13753 | /* Look for the final `;'. */ | |
13754 | cp_parser_require (parser, CPP_SEMICOLON, "`;'"); | |
13755 | } | |
13756 | ||
13757 | /* Support Functions */ | |
13758 | ||
13759 | /* Looks up NAME in the current scope, as given by PARSER->SCOPE. | |
13760 | NAME should have one of the representations used for an | |
13761 | id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE | |
13762 | is returned. If PARSER->SCOPE is a dependent type, then a | |
13763 | SCOPE_REF is returned. | |
13764 | ||
13765 | If NAME is a TEMPLATE_ID_EXPR, then it will be immediately | |
13766 | returned; the name was already resolved when the TEMPLATE_ID_EXPR | |
13767 | was formed. Abstractly, such entities should not be passed to this | |
13768 | function, because they do not need to be looked up, but it is | |
13769 | simpler to check for this special case here, rather than at the | |
13770 | call-sites. | |
13771 | ||
13772 | In cases not explicitly covered above, this function returns a | |
13773 | DECL, OVERLOAD, or baselink representing the result of the lookup. | |
13774 | If there was no entity with the indicated NAME, the ERROR_MARK_NODE | |
13775 | is returned. | |
13776 | ||
a723baf1 MM |
13777 | If IS_TYPE is TRUE, bindings that do not refer to types are |
13778 | ignored. | |
13779 | ||
b0bc6e8e KL |
13780 | If IS_TEMPLATE is TRUE, bindings that do not refer to templates are |
13781 | ignored. | |
13782 | ||
eea9800f MM |
13783 | If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces |
13784 | are ignored. | |
13785 | ||
a723baf1 MM |
13786 | If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent |
13787 | types. */ | |
13788 | ||
13789 | static tree | |
21526606 | 13790 | cp_parser_lookup_name (cp_parser *parser, tree name, |
b0bc6e8e KL |
13791 | bool is_type, bool is_template, bool is_namespace, |
13792 | bool check_dependency) | |
a723baf1 MM |
13793 | { |
13794 | tree decl; | |
13795 | tree object_type = parser->context->object_type; | |
13796 | ||
13797 | /* Now that we have looked up the name, the OBJECT_TYPE (if any) is | |
13798 | no longer valid. Note that if we are parsing tentatively, and | |
13799 | the parse fails, OBJECT_TYPE will be automatically restored. */ | |
13800 | parser->context->object_type = NULL_TREE; | |
13801 | ||
13802 | if (name == error_mark_node) | |
13803 | return error_mark_node; | |
13804 | ||
13805 | /* A template-id has already been resolved; there is no lookup to | |
13806 | do. */ | |
13807 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
13808 | return name; | |
13809 | if (BASELINK_P (name)) | |
13810 | { | |
13811 | my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name)) | |
13812 | == TEMPLATE_ID_EXPR), | |
13813 | 20020909); | |
13814 | return name; | |
13815 | } | |
13816 | ||
13817 | /* A BIT_NOT_EXPR is used to represent a destructor. By this point, | |
13818 | it should already have been checked to make sure that the name | |
13819 | used matches the type being destroyed. */ | |
13820 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
13821 | { | |
13822 | tree type; | |
13823 | ||
13824 | /* Figure out to which type this destructor applies. */ | |
13825 | if (parser->scope) | |
13826 | type = parser->scope; | |
13827 | else if (object_type) | |
13828 | type = object_type; | |
13829 | else | |
13830 | type = current_class_type; | |
13831 | /* If that's not a class type, there is no destructor. */ | |
13832 | if (!type || !CLASS_TYPE_P (type)) | |
13833 | return error_mark_node; | |
fd6e3cce GB |
13834 | if (!CLASSTYPE_DESTRUCTORS (type)) |
13835 | return error_mark_node; | |
a723baf1 MM |
13836 | /* If it was a class type, return the destructor. */ |
13837 | return CLASSTYPE_DESTRUCTORS (type); | |
13838 | } | |
13839 | ||
13840 | /* By this point, the NAME should be an ordinary identifier. If | |
13841 | the id-expression was a qualified name, the qualifying scope is | |
13842 | stored in PARSER->SCOPE at this point. */ | |
13843 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, | |
13844 | 20000619); | |
21526606 | 13845 | |
a723baf1 MM |
13846 | /* Perform the lookup. */ |
13847 | if (parser->scope) | |
21526606 | 13848 | { |
1fb3244a | 13849 | bool dependent_p; |
a723baf1 MM |
13850 | |
13851 | if (parser->scope == error_mark_node) | |
13852 | return error_mark_node; | |
13853 | ||
13854 | /* If the SCOPE is dependent, the lookup must be deferred until | |
13855 | the template is instantiated -- unless we are explicitly | |
13856 | looking up names in uninstantiated templates. Even then, we | |
13857 | cannot look up the name if the scope is not a class type; it | |
13858 | might, for example, be a template type parameter. */ | |
1fb3244a MM |
13859 | dependent_p = (TYPE_P (parser->scope) |
13860 | && !(parser->in_declarator_p | |
13861 | && currently_open_class (parser->scope)) | |
13862 | && dependent_type_p (parser->scope)); | |
a723baf1 | 13863 | if ((check_dependency || !CLASS_TYPE_P (parser->scope)) |
1fb3244a | 13864 | && dependent_p) |
a723baf1 | 13865 | { |
b0bc6e8e | 13866 | if (is_type) |
a723baf1 MM |
13867 | /* The resolution to Core Issue 180 says that `struct A::B' |
13868 | should be considered a type-name, even if `A' is | |
13869 | dependent. */ | |
13870 | decl = TYPE_NAME (make_typename_type (parser->scope, | |
13871 | name, | |
13872 | /*complain=*/1)); | |
b0bc6e8e | 13873 | else if (is_template) |
5b4acce1 KL |
13874 | decl = make_unbound_class_template (parser->scope, |
13875 | name, | |
13876 | /*complain=*/1); | |
b0bc6e8e KL |
13877 | else |
13878 | decl = build_nt (SCOPE_REF, parser->scope, name); | |
a723baf1 MM |
13879 | } |
13880 | else | |
13881 | { | |
91b004e5 MM |
13882 | bool pop_p = false; |
13883 | ||
a723baf1 MM |
13884 | /* If PARSER->SCOPE is a dependent type, then it must be a |
13885 | class type, and we must not be checking dependencies; | |
13886 | otherwise, we would have processed this lookup above. So | |
13887 | that PARSER->SCOPE is not considered a dependent base by | |
13888 | lookup_member, we must enter the scope here. */ | |
1fb3244a | 13889 | if (dependent_p) |
91b004e5 | 13890 | pop_p = push_scope (parser->scope); |
a723baf1 MM |
13891 | /* If the PARSER->SCOPE is a a template specialization, it |
13892 | may be instantiated during name lookup. In that case, | |
13893 | errors may be issued. Even if we rollback the current | |
13894 | tentative parse, those errors are valid. */ | |
5e08432e MM |
13895 | decl = lookup_qualified_name (parser->scope, name, is_type, |
13896 | /*complain=*/true); | |
91b004e5 | 13897 | if (pop_p) |
a723baf1 MM |
13898 | pop_scope (parser->scope); |
13899 | } | |
13900 | parser->qualifying_scope = parser->scope; | |
13901 | parser->object_scope = NULL_TREE; | |
13902 | } | |
13903 | else if (object_type) | |
13904 | { | |
13905 | tree object_decl = NULL_TREE; | |
13906 | /* Look up the name in the scope of the OBJECT_TYPE, unless the | |
13907 | OBJECT_TYPE is not a class. */ | |
13908 | if (CLASS_TYPE_P (object_type)) | |
13909 | /* If the OBJECT_TYPE is a template specialization, it may | |
13910 | be instantiated during name lookup. In that case, errors | |
13911 | may be issued. Even if we rollback the current tentative | |
13912 | parse, those errors are valid. */ | |
13913 | object_decl = lookup_member (object_type, | |
13914 | name, | |
13915 | /*protect=*/0, is_type); | |
13916 | /* Look it up in the enclosing context, too. */ | |
21526606 | 13917 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, |
eea9800f | 13918 | is_namespace, |
a723baf1 MM |
13919 | /*flags=*/0); |
13920 | parser->object_scope = object_type; | |
13921 | parser->qualifying_scope = NULL_TREE; | |
13922 | if (object_decl) | |
13923 | decl = object_decl; | |
13924 | } | |
13925 | else | |
13926 | { | |
21526606 | 13927 | decl = lookup_name_real (name, is_type, /*nonclass=*/0, |
eea9800f | 13928 | is_namespace, |
a723baf1 MM |
13929 | /*flags=*/0); |
13930 | parser->qualifying_scope = NULL_TREE; | |
13931 | parser->object_scope = NULL_TREE; | |
13932 | } | |
13933 | ||
13934 | /* If the lookup failed, let our caller know. */ | |
21526606 | 13935 | if (!decl |
a723baf1 | 13936 | || decl == error_mark_node |
21526606 | 13937 | || (TREE_CODE (decl) == FUNCTION_DECL |
a723baf1 MM |
13938 | && DECL_ANTICIPATED (decl))) |
13939 | return error_mark_node; | |
13940 | ||
13941 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
13942 | if (TREE_CODE (decl) == TREE_LIST) | |
13943 | { | |
13944 | /* The error message we have to print is too complicated for | |
13945 | cp_parser_error, so we incorporate its actions directly. */ | |
e5976695 | 13946 | if (!cp_parser_simulate_error (parser)) |
a723baf1 MM |
13947 | { |
13948 | error ("reference to `%D' is ambiguous", name); | |
13949 | print_candidates (decl); | |
13950 | } | |
13951 | return error_mark_node; | |
13952 | } | |
13953 | ||
21526606 | 13954 | my_friendly_assert (DECL_P (decl) |
a723baf1 MM |
13955 | || TREE_CODE (decl) == OVERLOAD |
13956 | || TREE_CODE (decl) == SCOPE_REF | |
5b4acce1 | 13957 | || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE |
a723baf1 MM |
13958 | || BASELINK_P (decl), |
13959 | 20000619); | |
13960 | ||
13961 | /* If we have resolved the name of a member declaration, check to | |
13962 | see if the declaration is accessible. When the name resolves to | |
34cd5ae7 | 13963 | set of overloaded functions, accessibility is checked when |
21526606 | 13964 | overload resolution is done. |
a723baf1 MM |
13965 | |
13966 | During an explicit instantiation, access is not checked at all, | |
13967 | as per [temp.explicit]. */ | |
8d241e0b | 13968 | if (DECL_P (decl)) |
ee76b931 | 13969 | check_accessibility_of_qualified_id (decl, object_type, parser->scope); |
a723baf1 MM |
13970 | |
13971 | return decl; | |
13972 | } | |
13973 | ||
13974 | /* Like cp_parser_lookup_name, but for use in the typical case where | |
b0bc6e8e KL |
13975 | CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, |
13976 | IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ | |
a723baf1 MM |
13977 | |
13978 | static tree | |
94edc4ab | 13979 | cp_parser_lookup_name_simple (cp_parser* parser, tree name) |
a723baf1 | 13980 | { |
21526606 | 13981 | return cp_parser_lookup_name (parser, name, |
eea9800f | 13982 | /*is_type=*/false, |
b0bc6e8e | 13983 | /*is_template=*/false, |
eea9800f | 13984 | /*is_namespace=*/false, |
a723baf1 MM |
13985 | /*check_dependency=*/true); |
13986 | } | |
13987 | ||
a723baf1 MM |
13988 | /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in |
13989 | the current context, return the TYPE_DECL. If TAG_NAME_P is | |
13990 | true, the DECL indicates the class being defined in a class-head, | |
13991 | or declared in an elaborated-type-specifier. | |
13992 | ||
13993 | Otherwise, return DECL. */ | |
13994 | ||
13995 | static tree | |
13996 | cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) | |
13997 | { | |
710b73e6 KL |
13998 | /* If the TEMPLATE_DECL is being declared as part of a class-head, |
13999 | the translation from TEMPLATE_DECL to TYPE_DECL occurs: | |
a723baf1 | 14000 | |
21526606 | 14001 | struct A { |
a723baf1 MM |
14002 | template <typename T> struct B; |
14003 | }; | |
14004 | ||
21526606 EC |
14005 | template <typename T> struct A::B {}; |
14006 | ||
a723baf1 MM |
14007 | Similarly, in a elaborated-type-specifier: |
14008 | ||
14009 | namespace N { struct X{}; } | |
14010 | ||
14011 | struct A { | |
14012 | template <typename T> friend struct N::X; | |
14013 | }; | |
14014 | ||
710b73e6 KL |
14015 | However, if the DECL refers to a class type, and we are in |
14016 | the scope of the class, then the name lookup automatically | |
14017 | finds the TYPE_DECL created by build_self_reference rather | |
14018 | than a TEMPLATE_DECL. For example, in: | |
14019 | ||
14020 | template <class T> struct S { | |
14021 | S s; | |
14022 | }; | |
14023 | ||
14024 | there is no need to handle such case. */ | |
14025 | ||
14026 | if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) | |
a723baf1 MM |
14027 | return DECL_TEMPLATE_RESULT (decl); |
14028 | ||
14029 | return decl; | |
14030 | } | |
14031 | ||
14032 | /* If too many, or too few, template-parameter lists apply to the | |
14033 | declarator, issue an error message. Returns TRUE if all went well, | |
14034 | and FALSE otherwise. */ | |
14035 | ||
14036 | static bool | |
21526606 | 14037 | cp_parser_check_declarator_template_parameters (cp_parser* parser, |
94edc4ab | 14038 | tree declarator) |
a723baf1 MM |
14039 | { |
14040 | unsigned num_templates; | |
14041 | ||
14042 | /* We haven't seen any classes that involve template parameters yet. */ | |
14043 | num_templates = 0; | |
14044 | ||
14045 | switch (TREE_CODE (declarator)) | |
14046 | { | |
14047 | case CALL_EXPR: | |
14048 | case ARRAY_REF: | |
14049 | case INDIRECT_REF: | |
14050 | case ADDR_EXPR: | |
14051 | { | |
14052 | tree main_declarator = TREE_OPERAND (declarator, 0); | |
14053 | return | |
21526606 | 14054 | cp_parser_check_declarator_template_parameters (parser, |
a723baf1 MM |
14055 | main_declarator); |
14056 | } | |
14057 | ||
14058 | case SCOPE_REF: | |
14059 | { | |
14060 | tree scope; | |
14061 | tree member; | |
14062 | ||
14063 | scope = TREE_OPERAND (declarator, 0); | |
14064 | member = TREE_OPERAND (declarator, 1); | |
14065 | ||
14066 | /* If this is a pointer-to-member, then we are not interested | |
14067 | in the SCOPE, because it does not qualify the thing that is | |
14068 | being declared. */ | |
14069 | if (TREE_CODE (member) == INDIRECT_REF) | |
14070 | return (cp_parser_check_declarator_template_parameters | |
14071 | (parser, member)); | |
14072 | ||
14073 | while (scope && CLASS_TYPE_P (scope)) | |
14074 | { | |
14075 | /* You're supposed to have one `template <...>' | |
14076 | for every template class, but you don't need one | |
14077 | for a full specialization. For example: | |
21526606 | 14078 | |
a723baf1 MM |
14079 | template <class T> struct S{}; |
14080 | template <> struct S<int> { void f(); }; | |
14081 | void S<int>::f () {} | |
21526606 | 14082 | |
a723baf1 MM |
14083 | is correct; there shouldn't be a `template <>' for |
14084 | the definition of `S<int>::f'. */ | |
14085 | if (CLASSTYPE_TEMPLATE_INFO (scope) | |
14086 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope) | |
14087 | || uses_template_parms (CLASSTYPE_TI_ARGS (scope))) | |
14088 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) | |
14089 | ++num_templates; | |
14090 | ||
14091 | scope = TYPE_CONTEXT (scope); | |
14092 | } | |
14093 | } | |
14094 | ||
14095 | /* Fall through. */ | |
14096 | ||
14097 | default: | |
14098 | /* If the DECLARATOR has the form `X<y>' then it uses one | |
14099 | additional level of template parameters. */ | |
14100 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
14101 | ++num_templates; | |
14102 | ||
21526606 | 14103 | return cp_parser_check_template_parameters (parser, |
a723baf1 MM |
14104 | num_templates); |
14105 | } | |
14106 | } | |
14107 | ||
14108 | /* NUM_TEMPLATES were used in the current declaration. If that is | |
14109 | invalid, return FALSE and issue an error messages. Otherwise, | |
14110 | return TRUE. */ | |
14111 | ||
14112 | static bool | |
94edc4ab NN |
14113 | cp_parser_check_template_parameters (cp_parser* parser, |
14114 | unsigned num_templates) | |
a723baf1 MM |
14115 | { |
14116 | /* If there are more template classes than parameter lists, we have | |
14117 | something like: | |
21526606 | 14118 | |
a723baf1 MM |
14119 | template <class T> void S<T>::R<T>::f (); */ |
14120 | if (parser->num_template_parameter_lists < num_templates) | |
14121 | { | |
14122 | error ("too few template-parameter-lists"); | |
14123 | return false; | |
14124 | } | |
14125 | /* If there are the same number of template classes and parameter | |
14126 | lists, that's OK. */ | |
14127 | if (parser->num_template_parameter_lists == num_templates) | |
14128 | return true; | |
14129 | /* If there are more, but only one more, then we are referring to a | |
14130 | member template. That's OK too. */ | |
14131 | if (parser->num_template_parameter_lists == num_templates + 1) | |
14132 | return true; | |
14133 | /* Otherwise, there are too many template parameter lists. We have | |
14134 | something like: | |
14135 | ||
14136 | template <class T> template <class U> void S::f(); */ | |
14137 | error ("too many template-parameter-lists"); | |
14138 | return false; | |
14139 | } | |
14140 | ||
14141 | /* Parse a binary-expression of the general form: | |
14142 | ||
14143 | binary-expression: | |
14144 | <expr> | |
14145 | binary-expression <token> <expr> | |
14146 | ||
14147 | The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used | |
14148 | to parser the <expr>s. If the first production is used, then the | |
14149 | value returned by FN is returned directly. Otherwise, a node with | |
14150 | the indicated EXPR_TYPE is returned, with operands corresponding to | |
14151 | the two sub-expressions. */ | |
14152 | ||
14153 | static tree | |
21526606 EC |
14154 | cp_parser_binary_expression (cp_parser* parser, |
14155 | const cp_parser_token_tree_map token_tree_map, | |
94edc4ab | 14156 | cp_parser_expression_fn fn) |
a723baf1 MM |
14157 | { |
14158 | tree lhs; | |
14159 | ||
14160 | /* Parse the first expression. */ | |
14161 | lhs = (*fn) (parser); | |
14162 | /* Now, look for more expressions. */ | |
14163 | while (true) | |
14164 | { | |
14165 | cp_token *token; | |
39b1af70 | 14166 | const cp_parser_token_tree_map_node *map_node; |
a723baf1 MM |
14167 | tree rhs; |
14168 | ||
14169 | /* Peek at the next token. */ | |
14170 | token = cp_lexer_peek_token (parser->lexer); | |
14171 | /* If the token is `>', and that's not an operator at the | |
14172 | moment, then we're done. */ | |
14173 | if (token->type == CPP_GREATER | |
14174 | && !parser->greater_than_is_operator_p) | |
14175 | break; | |
34cd5ae7 | 14176 | /* If we find one of the tokens we want, build the corresponding |
a723baf1 | 14177 | tree representation. */ |
21526606 | 14178 | for (map_node = token_tree_map; |
a723baf1 MM |
14179 | map_node->token_type != CPP_EOF; |
14180 | ++map_node) | |
14181 | if (map_node->token_type == token->type) | |
14182 | { | |
ec835fb2 MM |
14183 | /* Assume that an overloaded operator will not be used. */ |
14184 | bool overloaded_p = false; | |
14185 | ||
a723baf1 MM |
14186 | /* Consume the operator token. */ |
14187 | cp_lexer_consume_token (parser->lexer); | |
14188 | /* Parse the right-hand side of the expression. */ | |
14189 | rhs = (*fn) (parser); | |
14190 | /* Build the binary tree node. */ | |
ec835fb2 MM |
14191 | lhs = build_x_binary_op (map_node->tree_type, lhs, rhs, |
14192 | &overloaded_p); | |
14193 | /* If the binary operator required the use of an | |
14194 | overloaded operator, then this expression cannot be an | |
14195 | integral constant-expression. An overloaded operator | |
14196 | can be used even if both operands are otherwise | |
14197 | permissible in an integral constant-expression if at | |
14198 | least one of the operands is of enumeration type. */ | |
14199 | if (overloaded_p | |
14200 | && (cp_parser_non_integral_constant_expression | |
14201 | (parser, "calls to overloaded operators"))) | |
14202 | lhs = error_mark_node; | |
a723baf1 MM |
14203 | break; |
14204 | } | |
14205 | ||
14206 | /* If the token wasn't one of the ones we want, we're done. */ | |
14207 | if (map_node->token_type == CPP_EOF) | |
14208 | break; | |
14209 | } | |
14210 | ||
14211 | return lhs; | |
14212 | } | |
14213 | ||
14214 | /* Parse an optional `::' token indicating that the following name is | |
14215 | from the global namespace. If so, PARSER->SCOPE is set to the | |
14216 | GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, | |
14217 | unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. | |
14218 | Returns the new value of PARSER->SCOPE, if the `::' token is | |
14219 | present, and NULL_TREE otherwise. */ | |
14220 | ||
14221 | static tree | |
94edc4ab | 14222 | cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) |
a723baf1 MM |
14223 | { |
14224 | cp_token *token; | |
14225 | ||
14226 | /* Peek at the next token. */ | |
14227 | token = cp_lexer_peek_token (parser->lexer); | |
14228 | /* If we're looking at a `::' token then we're starting from the | |
14229 | global namespace, not our current location. */ | |
14230 | if (token->type == CPP_SCOPE) | |
14231 | { | |
14232 | /* Consume the `::' token. */ | |
14233 | cp_lexer_consume_token (parser->lexer); | |
14234 | /* Set the SCOPE so that we know where to start the lookup. */ | |
14235 | parser->scope = global_namespace; | |
14236 | parser->qualifying_scope = global_namespace; | |
14237 | parser->object_scope = NULL_TREE; | |
14238 | ||
14239 | return parser->scope; | |
14240 | } | |
14241 | else if (!current_scope_valid_p) | |
14242 | { | |
14243 | parser->scope = NULL_TREE; | |
14244 | parser->qualifying_scope = NULL_TREE; | |
14245 | parser->object_scope = NULL_TREE; | |
14246 | } | |
14247 | ||
14248 | return NULL_TREE; | |
14249 | } | |
14250 | ||
14251 | /* Returns TRUE if the upcoming token sequence is the start of a | |
14252 | constructor declarator. If FRIEND_P is true, the declarator is | |
14253 | preceded by the `friend' specifier. */ | |
14254 | ||
14255 | static bool | |
14256 | cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) | |
14257 | { | |
14258 | bool constructor_p; | |
14259 | tree type_decl = NULL_TREE; | |
14260 | bool nested_name_p; | |
2050a1bb MM |
14261 | cp_token *next_token; |
14262 | ||
14263 | /* The common case is that this is not a constructor declarator, so | |
8fbc5ae7 MM |
14264 | try to avoid doing lots of work if at all possible. It's not |
14265 | valid declare a constructor at function scope. */ | |
14266 | if (at_function_scope_p ()) | |
14267 | return false; | |
14268 | /* And only certain tokens can begin a constructor declarator. */ | |
2050a1bb MM |
14269 | next_token = cp_lexer_peek_token (parser->lexer); |
14270 | if (next_token->type != CPP_NAME | |
14271 | && next_token->type != CPP_SCOPE | |
14272 | && next_token->type != CPP_NESTED_NAME_SPECIFIER | |
14273 | && next_token->type != CPP_TEMPLATE_ID) | |
14274 | return false; | |
a723baf1 MM |
14275 | |
14276 | /* Parse tentatively; we are going to roll back all of the tokens | |
14277 | consumed here. */ | |
14278 | cp_parser_parse_tentatively (parser); | |
14279 | /* Assume that we are looking at a constructor declarator. */ | |
14280 | constructor_p = true; | |
8d241e0b | 14281 | |
a723baf1 MM |
14282 | /* Look for the optional `::' operator. */ |
14283 | cp_parser_global_scope_opt (parser, | |
14284 | /*current_scope_valid_p=*/false); | |
14285 | /* Look for the nested-name-specifier. */ | |
21526606 | 14286 | nested_name_p |
a723baf1 MM |
14287 | = (cp_parser_nested_name_specifier_opt (parser, |
14288 | /*typename_keyword_p=*/false, | |
14289 | /*check_dependency_p=*/false, | |
a668c6ad MM |
14290 | /*type_p=*/false, |
14291 | /*is_declaration=*/false) | |
a723baf1 MM |
14292 | != NULL_TREE); |
14293 | /* Outside of a class-specifier, there must be a | |
14294 | nested-name-specifier. */ | |
21526606 | 14295 | if (!nested_name_p && |
a723baf1 MM |
14296 | (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) |
14297 | || friend_p)) | |
14298 | constructor_p = false; | |
14299 | /* If we still think that this might be a constructor-declarator, | |
14300 | look for a class-name. */ | |
14301 | if (constructor_p) | |
14302 | { | |
14303 | /* If we have: | |
14304 | ||
8fbc5ae7 | 14305 | template <typename T> struct S { S(); }; |
a723baf1 MM |
14306 | template <typename T> S<T>::S (); |
14307 | ||
14308 | we must recognize that the nested `S' names a class. | |
14309 | Similarly, for: | |
14310 | ||
14311 | template <typename T> S<T>::S<T> (); | |
14312 | ||
14313 | we must recognize that the nested `S' names a template. */ | |
14314 | type_decl = cp_parser_class_name (parser, | |
14315 | /*typename_keyword_p=*/false, | |
14316 | /*template_keyword_p=*/false, | |
14317 | /*type_p=*/false, | |
a723baf1 | 14318 | /*check_dependency_p=*/false, |
a668c6ad MM |
14319 | /*class_head_p=*/false, |
14320 | /*is_declaration=*/false); | |
a723baf1 MM |
14321 | /* If there was no class-name, then this is not a constructor. */ |
14322 | constructor_p = !cp_parser_error_occurred (parser); | |
14323 | } | |
8d241e0b | 14324 | |
a723baf1 MM |
14325 | /* If we're still considering a constructor, we have to see a `(', |
14326 | to begin the parameter-declaration-clause, followed by either a | |
14327 | `)', an `...', or a decl-specifier. We need to check for a | |
14328 | type-specifier to avoid being fooled into thinking that: | |
14329 | ||
14330 | S::S (f) (int); | |
14331 | ||
14332 | is a constructor. (It is actually a function named `f' that | |
14333 | takes one parameter (of type `int') and returns a value of type | |
14334 | `S::S'. */ | |
21526606 | 14335 | if (constructor_p |
a723baf1 MM |
14336 | && cp_parser_require (parser, CPP_OPEN_PAREN, "`('")) |
14337 | { | |
14338 | if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) | |
14339 | && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) | |
15077df5 MM |
14340 | /* A parameter declaration begins with a decl-specifier, |
14341 | which is either the "attribute" keyword, a storage class | |
14342 | specifier, or (usually) a type-specifier. */ | |
14343 | && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE) | |
a723baf1 MM |
14344 | && !cp_parser_storage_class_specifier_opt (parser)) |
14345 | { | |
5dae1114 | 14346 | tree type; |
91b004e5 | 14347 | bool pop_p = false; |
4047b164 | 14348 | unsigned saved_num_template_parameter_lists; |
5dae1114 MM |
14349 | |
14350 | /* Names appearing in the type-specifier should be looked up | |
14351 | in the scope of the class. */ | |
14352 | if (current_class_type) | |
14353 | type = NULL_TREE; | |
a723baf1 MM |
14354 | else |
14355 | { | |
5dae1114 MM |
14356 | type = TREE_TYPE (type_decl); |
14357 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
14d22dd6 | 14358 | { |
21526606 | 14359 | type = resolve_typename_type (type, |
14d22dd6 MM |
14360 | /*only_current_p=*/false); |
14361 | if (type == error_mark_node) | |
14362 | { | |
14363 | cp_parser_abort_tentative_parse (parser); | |
14364 | return false; | |
14365 | } | |
14366 | } | |
91b004e5 | 14367 | pop_p = push_scope (type); |
a723baf1 | 14368 | } |
4047b164 KL |
14369 | |
14370 | /* Inside the constructor parameter list, surrounding | |
14371 | template-parameter-lists do not apply. */ | |
14372 | saved_num_template_parameter_lists | |
14373 | = parser->num_template_parameter_lists; | |
14374 | parser->num_template_parameter_lists = 0; | |
14375 | ||
5dae1114 MM |
14376 | /* Look for the type-specifier. */ |
14377 | cp_parser_type_specifier (parser, | |
14378 | CP_PARSER_FLAGS_NONE, | |
14379 | /*is_friend=*/false, | |
14380 | /*is_declarator=*/true, | |
14381 | /*declares_class_or_enum=*/NULL, | |
14382 | /*is_cv_qualifier=*/NULL); | |
4047b164 KL |
14383 | |
14384 | parser->num_template_parameter_lists | |
14385 | = saved_num_template_parameter_lists; | |
14386 | ||
5dae1114 | 14387 | /* Leave the scope of the class. */ |
91b004e5 | 14388 | if (pop_p) |
5dae1114 MM |
14389 | pop_scope (type); |
14390 | ||
14391 | constructor_p = !cp_parser_error_occurred (parser); | |
a723baf1 MM |
14392 | } |
14393 | } | |
14394 | else | |
14395 | constructor_p = false; | |
14396 | /* We did not really want to consume any tokens. */ | |
14397 | cp_parser_abort_tentative_parse (parser); | |
14398 | ||
14399 | return constructor_p; | |
14400 | } | |
14401 | ||
14402 | /* Parse the definition of the function given by the DECL_SPECIFIERS, | |
cf22909c | 14403 | ATTRIBUTES, and DECLARATOR. The access checks have been deferred; |
a723baf1 MM |
14404 | they must be performed once we are in the scope of the function. |
14405 | ||
14406 | Returns the function defined. */ | |
14407 | ||
14408 | static tree | |
14409 | cp_parser_function_definition_from_specifiers_and_declarator | |
94edc4ab NN |
14410 | (cp_parser* parser, |
14411 | tree decl_specifiers, | |
14412 | tree attributes, | |
14413 | tree declarator) | |
a723baf1 MM |
14414 | { |
14415 | tree fn; | |
14416 | bool success_p; | |
14417 | ||
14418 | /* Begin the function-definition. */ | |
21526606 EC |
14419 | success_p = begin_function_definition (decl_specifiers, |
14420 | attributes, | |
a723baf1 MM |
14421 | declarator); |
14422 | ||
14423 | /* If there were names looked up in the decl-specifier-seq that we | |
14424 | did not check, check them now. We must wait until we are in the | |
14425 | scope of the function to perform the checks, since the function | |
14426 | might be a friend. */ | |
cf22909c | 14427 | perform_deferred_access_checks (); |
a723baf1 MM |
14428 | |
14429 | if (!success_p) | |
14430 | { | |
14431 | /* If begin_function_definition didn't like the definition, skip | |
14432 | the entire function. */ | |
14433 | error ("invalid function declaration"); | |
14434 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14435 | fn = error_mark_node; | |
14436 | } | |
14437 | else | |
14438 | fn = cp_parser_function_definition_after_declarator (parser, | |
14439 | /*inline_p=*/false); | |
14440 | ||
14441 | return fn; | |
14442 | } | |
14443 | ||
14444 | /* Parse the part of a function-definition that follows the | |
14445 | declarator. INLINE_P is TRUE iff this function is an inline | |
14446 | function defined with a class-specifier. | |
14447 | ||
14448 | Returns the function defined. */ | |
14449 | ||
21526606 EC |
14450 | static tree |
14451 | cp_parser_function_definition_after_declarator (cp_parser* parser, | |
94edc4ab | 14452 | bool inline_p) |
a723baf1 MM |
14453 | { |
14454 | tree fn; | |
14455 | bool ctor_initializer_p = false; | |
14456 | bool saved_in_unbraced_linkage_specification_p; | |
14457 | unsigned saved_num_template_parameter_lists; | |
14458 | ||
14459 | /* If the next token is `return', then the code may be trying to | |
14460 | make use of the "named return value" extension that G++ used to | |
14461 | support. */ | |
14462 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) | |
14463 | { | |
14464 | /* Consume the `return' keyword. */ | |
14465 | cp_lexer_consume_token (parser->lexer); | |
14466 | /* Look for the identifier that indicates what value is to be | |
14467 | returned. */ | |
14468 | cp_parser_identifier (parser); | |
14469 | /* Issue an error message. */ | |
14470 | error ("named return values are no longer supported"); | |
14471 | /* Skip tokens until we reach the start of the function body. */ | |
21eb631b MM |
14472 | while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) |
14473 | && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF)) | |
a723baf1 MM |
14474 | cp_lexer_consume_token (parser->lexer); |
14475 | } | |
14476 | /* The `extern' in `extern "C" void f () { ... }' does not apply to | |
14477 | anything declared inside `f'. */ | |
21526606 | 14478 | saved_in_unbraced_linkage_specification_p |
a723baf1 MM |
14479 | = parser->in_unbraced_linkage_specification_p; |
14480 | parser->in_unbraced_linkage_specification_p = false; | |
14481 | /* Inside the function, surrounding template-parameter-lists do not | |
14482 | apply. */ | |
21526606 EC |
14483 | saved_num_template_parameter_lists |
14484 | = parser->num_template_parameter_lists; | |
a723baf1 MM |
14485 | parser->num_template_parameter_lists = 0; |
14486 | /* If the next token is `try', then we are looking at a | |
14487 | function-try-block. */ | |
14488 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) | |
14489 | ctor_initializer_p = cp_parser_function_try_block (parser); | |
14490 | /* A function-try-block includes the function-body, so we only do | |
14491 | this next part if we're not processing a function-try-block. */ | |
14492 | else | |
21526606 | 14493 | ctor_initializer_p |
a723baf1 MM |
14494 | = cp_parser_ctor_initializer_opt_and_function_body (parser); |
14495 | ||
14496 | /* Finish the function. */ | |
21526606 | 14497 | fn = finish_function ((ctor_initializer_p ? 1 : 0) | |
a723baf1 MM |
14498 | (inline_p ? 2 : 0)); |
14499 | /* Generate code for it, if necessary. */ | |
8cd2462c | 14500 | expand_or_defer_fn (fn); |
a723baf1 | 14501 | /* Restore the saved values. */ |
21526606 | 14502 | parser->in_unbraced_linkage_specification_p |
a723baf1 | 14503 | = saved_in_unbraced_linkage_specification_p; |
21526606 | 14504 | parser->num_template_parameter_lists |
a723baf1 MM |
14505 | = saved_num_template_parameter_lists; |
14506 | ||
14507 | return fn; | |
14508 | } | |
14509 | ||
14510 | /* Parse a template-declaration, assuming that the `export' (and | |
14511 | `extern') keywords, if present, has already been scanned. MEMBER_P | |
14512 | is as for cp_parser_template_declaration. */ | |
14513 | ||
14514 | static void | |
94edc4ab | 14515 | cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) |
a723baf1 MM |
14516 | { |
14517 | tree decl = NULL_TREE; | |
14518 | tree parameter_list; | |
14519 | bool friend_p = false; | |
14520 | ||
14521 | /* Look for the `template' keyword. */ | |
14522 | if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'")) | |
14523 | return; | |
21526606 | 14524 | |
a723baf1 MM |
14525 | /* And the `<'. */ |
14526 | if (!cp_parser_require (parser, CPP_LESS, "`<'")) | |
14527 | return; | |
21526606 | 14528 | |
a723baf1 MM |
14529 | /* If the next token is `>', then we have an invalid |
14530 | specialization. Rather than complain about an invalid template | |
14531 | parameter, issue an error message here. */ | |
14532 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14533 | { | |
14534 | cp_parser_error (parser, "invalid explicit specialization"); | |
2f9afd51 | 14535 | begin_specialization (); |
a723baf1 MM |
14536 | parameter_list = NULL_TREE; |
14537 | } | |
14538 | else | |
2f9afd51 KL |
14539 | { |
14540 | /* Parse the template parameters. */ | |
14541 | begin_template_parm_list (); | |
14542 | parameter_list = cp_parser_template_parameter_list (parser); | |
14543 | parameter_list = end_template_parm_list (parameter_list); | |
14544 | } | |
14545 | ||
a723baf1 MM |
14546 | /* Look for the `>'. */ |
14547 | cp_parser_skip_until_found (parser, CPP_GREATER, "`>'"); | |
14548 | /* We just processed one more parameter list. */ | |
14549 | ++parser->num_template_parameter_lists; | |
14550 | /* If the next token is `template', there are more template | |
14551 | parameters. */ | |
21526606 | 14552 | if (cp_lexer_next_token_is_keyword (parser->lexer, |
a723baf1 MM |
14553 | RID_TEMPLATE)) |
14554 | cp_parser_template_declaration_after_export (parser, member_p); | |
14555 | else | |
14556 | { | |
14557 | decl = cp_parser_single_declaration (parser, | |
14558 | member_p, | |
14559 | &friend_p); | |
14560 | ||
14561 | /* If this is a member template declaration, let the front | |
14562 | end know. */ | |
14563 | if (member_p && !friend_p && decl) | |
37d407a1 KL |
14564 | { |
14565 | if (TREE_CODE (decl) == TYPE_DECL) | |
14566 | cp_parser_check_access_in_redeclaration (decl); | |
14567 | ||
14568 | decl = finish_member_template_decl (decl); | |
14569 | } | |
a723baf1 | 14570 | else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) |
19db77ce KL |
14571 | make_friend_class (current_class_type, TREE_TYPE (decl), |
14572 | /*complain=*/true); | |
a723baf1 MM |
14573 | } |
14574 | /* We are done with the current parameter list. */ | |
14575 | --parser->num_template_parameter_lists; | |
14576 | ||
14577 | /* Finish up. */ | |
14578 | finish_template_decl (parameter_list); | |
14579 | ||
14580 | /* Register member declarations. */ | |
14581 | if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) | |
14582 | finish_member_declaration (decl); | |
14583 | ||
14584 | /* If DECL is a function template, we must return to parse it later. | |
14585 | (Even though there is no definition, there might be default | |
14586 | arguments that need handling.) */ | |
21526606 | 14587 | if (member_p && decl |
a723baf1 MM |
14588 | && (TREE_CODE (decl) == FUNCTION_DECL |
14589 | || DECL_FUNCTION_TEMPLATE_P (decl))) | |
14590 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 14591 | = tree_cons (NULL_TREE, decl, |
a723baf1 MM |
14592 | TREE_VALUE (parser->unparsed_functions_queues)); |
14593 | } | |
14594 | ||
14595 | /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or | |
14596 | `function-definition' sequence. MEMBER_P is true, this declaration | |
14597 | appears in a class scope. | |
14598 | ||
14599 | Returns the DECL for the declared entity. If FRIEND_P is non-NULL, | |
14600 | *FRIEND_P is set to TRUE iff the declaration is a friend. */ | |
14601 | ||
14602 | static tree | |
21526606 | 14603 | cp_parser_single_declaration (cp_parser* parser, |
94edc4ab NN |
14604 | bool member_p, |
14605 | bool* friend_p) | |
a723baf1 | 14606 | { |
560ad596 | 14607 | int declares_class_or_enum; |
a723baf1 MM |
14608 | tree decl = NULL_TREE; |
14609 | tree decl_specifiers; | |
14610 | tree attributes; | |
4bb8ca28 | 14611 | bool function_definition_p = false; |
a723baf1 | 14612 | |
a723baf1 | 14613 | /* Defer access checks until we know what is being declared. */ |
8d241e0b | 14614 | push_deferring_access_checks (dk_deferred); |
cf22909c | 14615 | |
a723baf1 MM |
14616 | /* Try the `decl-specifier-seq [opt] init-declarator [opt]' |
14617 | alternative. */ | |
21526606 | 14618 | decl_specifiers |
a723baf1 MM |
14619 | = cp_parser_decl_specifier_seq (parser, |
14620 | CP_PARSER_FLAGS_OPTIONAL, | |
14621 | &attributes, | |
14622 | &declares_class_or_enum); | |
4bb8ca28 MM |
14623 | if (friend_p) |
14624 | *friend_p = cp_parser_friend_p (decl_specifiers); | |
a723baf1 MM |
14625 | /* Gather up the access checks that occurred the |
14626 | decl-specifier-seq. */ | |
cf22909c KL |
14627 | stop_deferring_access_checks (); |
14628 | ||
a723baf1 MM |
14629 | /* Check for the declaration of a template class. */ |
14630 | if (declares_class_or_enum) | |
14631 | { | |
14632 | if (cp_parser_declares_only_class_p (parser)) | |
14633 | { | |
14634 | decl = shadow_tag (decl_specifiers); | |
14635 | if (decl) | |
14636 | decl = TYPE_NAME (decl); | |
14637 | else | |
14638 | decl = error_mark_node; | |
14639 | } | |
14640 | } | |
14641 | else | |
14642 | decl = NULL_TREE; | |
14643 | /* If it's not a template class, try for a template function. If | |
14644 | the next token is a `;', then this declaration does not declare | |
14645 | anything. But, if there were errors in the decl-specifiers, then | |
14646 | the error might well have come from an attempted class-specifier. | |
14647 | In that case, there's no need to warn about a missing declarator. */ | |
14648 | if (!decl | |
14649 | && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) | |
14650 | || !value_member (error_mark_node, decl_specifiers))) | |
21526606 | 14651 | decl = cp_parser_init_declarator (parser, |
a723baf1 MM |
14652 | decl_specifiers, |
14653 | attributes, | |
4bb8ca28 | 14654 | /*function_definition_allowed_p=*/true, |
a723baf1 | 14655 | member_p, |
560ad596 | 14656 | declares_class_or_enum, |
4bb8ca28 | 14657 | &function_definition_p); |
cf22909c KL |
14658 | |
14659 | pop_deferring_access_checks (); | |
14660 | ||
a723baf1 MM |
14661 | /* Clear any current qualification; whatever comes next is the start |
14662 | of something new. */ | |
14663 | parser->scope = NULL_TREE; | |
14664 | parser->qualifying_scope = NULL_TREE; | |
14665 | parser->object_scope = NULL_TREE; | |
14666 | /* Look for a trailing `;' after the declaration. */ | |
4bb8ca28 MM |
14667 | if (!function_definition_p |
14668 | && !cp_parser_require (parser, CPP_SEMICOLON, "`;'")) | |
a723baf1 | 14669 | cp_parser_skip_to_end_of_block_or_statement (parser); |
a723baf1 MM |
14670 | |
14671 | return decl; | |
14672 | } | |
14673 | ||
d6b4ea85 MM |
14674 | /* Parse a cast-expression that is not the operand of a unary "&". */ |
14675 | ||
14676 | static tree | |
14677 | cp_parser_simple_cast_expression (cp_parser *parser) | |
14678 | { | |
14679 | return cp_parser_cast_expression (parser, /*address_p=*/false); | |
14680 | } | |
14681 | ||
a723baf1 MM |
14682 | /* Parse a functional cast to TYPE. Returns an expression |
14683 | representing the cast. */ | |
14684 | ||
14685 | static tree | |
94edc4ab | 14686 | cp_parser_functional_cast (cp_parser* parser, tree type) |
a723baf1 MM |
14687 | { |
14688 | tree expression_list; | |
d36d5600 | 14689 | tree cast; |
a723baf1 | 14690 | |
21526606 | 14691 | expression_list |
39703eb9 MM |
14692 | = cp_parser_parenthesized_expression_list (parser, false, |
14693 | /*non_constant_p=*/NULL); | |
a723baf1 | 14694 | |
d36d5600 GB |
14695 | cast = build_functional_cast (type, expression_list); |
14696 | /* [expr.const]/1: In an integral constant expression "only type | |
14697 | conversions to integral or enumeration type can be used". */ | |
14698 | if (cast != error_mark_node && !type_dependent_expression_p (type) | |
14699 | && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type))) | |
14700 | { | |
14701 | if (cp_parser_non_integral_constant_expression | |
14702 | (parser, "a call to a constructor")) | |
14703 | return error_mark_node; | |
14704 | } | |
14705 | return cast; | |
a723baf1 MM |
14706 | } |
14707 | ||
4bb8ca28 MM |
14708 | /* Save the tokens that make up the body of a member function defined |
14709 | in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have | |
14710 | already been parsed. The ATTRIBUTES are any GNU "__attribute__" | |
14711 | specifiers applied to the declaration. Returns the FUNCTION_DECL | |
14712 | for the member function. */ | |
14713 | ||
7ce27103 | 14714 | static tree |
4bb8ca28 MM |
14715 | cp_parser_save_member_function_body (cp_parser* parser, |
14716 | tree decl_specifiers, | |
14717 | tree declarator, | |
14718 | tree attributes) | |
14719 | { | |
14720 | cp_token_cache *cache; | |
14721 | tree fn; | |
14722 | ||
14723 | /* Create the function-declaration. */ | |
14724 | fn = start_method (decl_specifiers, declarator, attributes); | |
14725 | /* If something went badly wrong, bail out now. */ | |
14726 | if (fn == error_mark_node) | |
14727 | { | |
14728 | /* If there's a function-body, skip it. */ | |
21526606 | 14729 | if (cp_parser_token_starts_function_definition_p |
4bb8ca28 MM |
14730 | (cp_lexer_peek_token (parser->lexer))) |
14731 | cp_parser_skip_to_end_of_block_or_statement (parser); | |
14732 | return error_mark_node; | |
14733 | } | |
14734 | ||
14735 | /* Remember it, if there default args to post process. */ | |
14736 | cp_parser_save_default_args (parser, fn); | |
14737 | ||
14738 | /* Create a token cache. */ | |
14739 | cache = cp_token_cache_new (); | |
21526606 | 14740 | /* Save away the tokens that make up the body of the |
4bb8ca28 MM |
14741 | function. */ |
14742 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14743 | /* Handle function try blocks. */ | |
14744 | while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) | |
14745 | cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0); | |
14746 | ||
14747 | /* Save away the inline definition; we will process it when the | |
14748 | class is complete. */ | |
14749 | DECL_PENDING_INLINE_INFO (fn) = cache; | |
14750 | DECL_PENDING_INLINE_P (fn) = 1; | |
14751 | ||
14752 | /* We need to know that this was defined in the class, so that | |
14753 | friend templates are handled correctly. */ | |
14754 | DECL_INITIALIZED_IN_CLASS_P (fn) = 1; | |
14755 | ||
14756 | /* We're done with the inline definition. */ | |
14757 | finish_method (fn); | |
14758 | ||
14759 | /* Add FN to the queue of functions to be parsed later. */ | |
14760 | TREE_VALUE (parser->unparsed_functions_queues) | |
21526606 | 14761 | = tree_cons (NULL_TREE, fn, |
4bb8ca28 MM |
14762 | TREE_VALUE (parser->unparsed_functions_queues)); |
14763 | ||
14764 | return fn; | |
14765 | } | |
14766 | ||
ec75414f MM |
14767 | /* Parse a template-argument-list, as well as the trailing ">" (but |
14768 | not the opening ">"). See cp_parser_template_argument_list for the | |
14769 | return value. */ | |
14770 | ||
14771 | static tree | |
14772 | cp_parser_enclosed_template_argument_list (cp_parser* parser) | |
14773 | { | |
14774 | tree arguments; | |
14775 | tree saved_scope; | |
14776 | tree saved_qualifying_scope; | |
14777 | tree saved_object_scope; | |
14778 | bool saved_greater_than_is_operator_p; | |
14779 | ||
14780 | /* [temp.names] | |
14781 | ||
14782 | When parsing a template-id, the first non-nested `>' is taken as | |
14783 | the end of the template-argument-list rather than a greater-than | |
14784 | operator. */ | |
21526606 | 14785 | saved_greater_than_is_operator_p |
ec75414f MM |
14786 | = parser->greater_than_is_operator_p; |
14787 | parser->greater_than_is_operator_p = false; | |
14788 | /* Parsing the argument list may modify SCOPE, so we save it | |
14789 | here. */ | |
14790 | saved_scope = parser->scope; | |
14791 | saved_qualifying_scope = parser->qualifying_scope; | |
14792 | saved_object_scope = parser->object_scope; | |
14793 | /* Parse the template-argument-list itself. */ | |
14794 | if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) | |
14795 | arguments = NULL_TREE; | |
14796 | else | |
14797 | arguments = cp_parser_template_argument_list (parser); | |
4d5297fa GB |
14798 | /* Look for the `>' that ends the template-argument-list. If we find |
14799 | a '>>' instead, it's probably just a typo. */ | |
14800 | if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) | |
14801 | { | |
14802 | if (!saved_greater_than_is_operator_p) | |
14803 | { | |
14804 | /* If we're in a nested template argument list, the '>>' has to be | |
14805 | a typo for '> >'. We emit the error message, but we continue | |
14806 | parsing and we push a '>' as next token, so that the argument | |
14807 | list will be parsed correctly.. */ | |
14808 | cp_token* token; | |
14809 | error ("`>>' should be `> >' within a nested template argument list"); | |
14810 | token = cp_lexer_peek_token (parser->lexer); | |
14811 | token->type = CPP_GREATER; | |
14812 | } | |
14813 | else | |
14814 | { | |
14815 | /* If this is not a nested template argument list, the '>>' is | |
14816 | a typo for '>'. Emit an error message and continue. */ | |
14817 | error ("spurious `>>', use `>' to terminate a template argument list"); | |
14818 | cp_lexer_consume_token (parser->lexer); | |
14819 | } | |
14820 | } | |
6c0cc713 GB |
14821 | else if (!cp_parser_require (parser, CPP_GREATER, "`>'")) |
14822 | error ("missing `>' to terminate the template argument list"); | |
ec75414f | 14823 | /* The `>' token might be a greater-than operator again now. */ |
21526606 | 14824 | parser->greater_than_is_operator_p |
ec75414f MM |
14825 | = saved_greater_than_is_operator_p; |
14826 | /* Restore the SAVED_SCOPE. */ | |
14827 | parser->scope = saved_scope; | |
14828 | parser->qualifying_scope = saved_qualifying_scope; | |
14829 | parser->object_scope = saved_object_scope; | |
14830 | ||
14831 | return arguments; | |
14832 | } | |
14833 | ||
a723baf1 MM |
14834 | /* MEMBER_FUNCTION is a member function, or a friend. If default |
14835 | arguments, or the body of the function have not yet been parsed, | |
14836 | parse them now. */ | |
14837 | ||
14838 | static void | |
94edc4ab | 14839 | cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) |
a723baf1 MM |
14840 | { |
14841 | cp_lexer *saved_lexer; | |
14842 | ||
14843 | /* If this member is a template, get the underlying | |
14844 | FUNCTION_DECL. */ | |
14845 | if (DECL_FUNCTION_TEMPLATE_P (member_function)) | |
14846 | member_function = DECL_TEMPLATE_RESULT (member_function); | |
14847 | ||
14848 | /* There should not be any class definitions in progress at this | |
14849 | point; the bodies of members are only parsed outside of all class | |
14850 | definitions. */ | |
14851 | my_friendly_assert (parser->num_classes_being_defined == 0, 20010816); | |
14852 | /* While we're parsing the member functions we might encounter more | |
14853 | classes. We want to handle them right away, but we don't want | |
14854 | them getting mixed up with functions that are currently in the | |
14855 | queue. */ | |
14856 | parser->unparsed_functions_queues | |
14857 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14858 | ||
14859 | /* Make sure that any template parameters are in scope. */ | |
14860 | maybe_begin_member_template_processing (member_function); | |
14861 | ||
a723baf1 MM |
14862 | /* If the body of the function has not yet been parsed, parse it |
14863 | now. */ | |
14864 | if (DECL_PENDING_INLINE_P (member_function)) | |
14865 | { | |
14866 | tree function_scope; | |
14867 | cp_token_cache *tokens; | |
14868 | ||
14869 | /* The function is no longer pending; we are processing it. */ | |
14870 | tokens = DECL_PENDING_INLINE_INFO (member_function); | |
14871 | DECL_PENDING_INLINE_INFO (member_function) = NULL; | |
14872 | DECL_PENDING_INLINE_P (member_function) = 0; | |
14873 | /* If this was an inline function in a local class, enter the scope | |
14874 | of the containing function. */ | |
14875 | function_scope = decl_function_context (member_function); | |
14876 | if (function_scope) | |
14877 | push_function_context_to (function_scope); | |
21526606 | 14878 | |
a723baf1 MM |
14879 | /* Save away the current lexer. */ |
14880 | saved_lexer = parser->lexer; | |
14881 | /* Make a new lexer to feed us the tokens saved for this function. */ | |
14882 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14883 | parser->lexer->next = saved_lexer; | |
21526606 | 14884 | |
a723baf1 MM |
14885 | /* Set the current source position to be the location of the first |
14886 | token in the saved inline body. */ | |
3466b292 | 14887 | cp_lexer_peek_token (parser->lexer); |
21526606 | 14888 | |
a723baf1 MM |
14889 | /* Let the front end know that we going to be defining this |
14890 | function. */ | |
14891 | start_function (NULL_TREE, member_function, NULL_TREE, | |
14892 | SF_PRE_PARSED | SF_INCLASS_INLINE); | |
21526606 | 14893 | |
a723baf1 MM |
14894 | /* Now, parse the body of the function. */ |
14895 | cp_parser_function_definition_after_declarator (parser, | |
14896 | /*inline_p=*/true); | |
21526606 | 14897 | |
a723baf1 MM |
14898 | /* Leave the scope of the containing function. */ |
14899 | if (function_scope) | |
14900 | pop_function_context_from (function_scope); | |
14901 | /* Restore the lexer. */ | |
14902 | parser->lexer = saved_lexer; | |
14903 | } | |
14904 | ||
14905 | /* Remove any template parameters from the symbol table. */ | |
14906 | maybe_end_member_template_processing (); | |
14907 | ||
14908 | /* Restore the queue. */ | |
21526606 | 14909 | parser->unparsed_functions_queues |
a723baf1 MM |
14910 | = TREE_CHAIN (parser->unparsed_functions_queues); |
14911 | } | |
14912 | ||
cd0be382 | 14913 | /* If DECL contains any default args, remember it on the unparsed |
8db1028e NS |
14914 | functions queue. */ |
14915 | ||
14916 | static void | |
14917 | cp_parser_save_default_args (cp_parser* parser, tree decl) | |
14918 | { | |
14919 | tree probe; | |
14920 | ||
14921 | for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
14922 | probe; | |
14923 | probe = TREE_CHAIN (probe)) | |
14924 | if (TREE_PURPOSE (probe)) | |
14925 | { | |
14926 | TREE_PURPOSE (parser->unparsed_functions_queues) | |
21526606 | 14927 | = tree_cons (NULL_TREE, decl, |
8db1028e NS |
14928 | TREE_PURPOSE (parser->unparsed_functions_queues)); |
14929 | break; | |
14930 | } | |
14931 | return; | |
14932 | } | |
14933 | ||
8218bd34 MM |
14934 | /* FN is a FUNCTION_DECL which may contains a parameter with an |
14935 | unparsed DEFAULT_ARG. Parse the default args now. */ | |
a723baf1 MM |
14936 | |
14937 | static void | |
8218bd34 | 14938 | cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) |
a723baf1 MM |
14939 | { |
14940 | cp_lexer *saved_lexer; | |
14941 | cp_token_cache *tokens; | |
14942 | bool saved_local_variables_forbidden_p; | |
14943 | tree parameters; | |
8218bd34 | 14944 | |
b92bc2a0 NS |
14945 | /* While we're parsing the default args, we might (due to the |
14946 | statement expression extension) encounter more classes. We want | |
14947 | to handle them right away, but we don't want them getting mixed | |
14948 | up with default args that are currently in the queue. */ | |
14949 | parser->unparsed_functions_queues | |
14950 | = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues); | |
14951 | ||
8218bd34 | 14952 | for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
a723baf1 MM |
14953 | parameters; |
14954 | parameters = TREE_CHAIN (parameters)) | |
14955 | { | |
14956 | if (!TREE_PURPOSE (parameters) | |
14957 | || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG) | |
14958 | continue; | |
21526606 | 14959 | |
a723baf1 MM |
14960 | /* Save away the current lexer. */ |
14961 | saved_lexer = parser->lexer; | |
14962 | /* Create a new one, using the tokens we have saved. */ | |
14963 | tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters)); | |
14964 | parser->lexer = cp_lexer_new_from_tokens (tokens); | |
14965 | ||
14966 | /* Set the current source position to be the location of the | |
14967 | first token in the default argument. */ | |
3466b292 | 14968 | cp_lexer_peek_token (parser->lexer); |
a723baf1 MM |
14969 | |
14970 | /* Local variable names (and the `this' keyword) may not appear | |
14971 | in a default argument. */ | |
14972 | saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; | |
14973 | parser->local_variables_forbidden_p = true; | |
14974 | /* Parse the assignment-expression. */ | |
f128e1f3 | 14975 | if (DECL_CLASS_SCOPE_P (fn)) |
14d22dd6 | 14976 | push_nested_class (DECL_CONTEXT (fn)); |
a723baf1 | 14977 | TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser); |
f128e1f3 | 14978 | if (DECL_CLASS_SCOPE_P (fn)) |
e5976695 | 14979 | pop_nested_class (); |
a723baf1 | 14980 | |
676e33ca MM |
14981 | /* If the token stream has not been completely used up, then |
14982 | there was extra junk after the end of the default | |
14983 | argument. */ | |
14984 | if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) | |
14985 | cp_parser_error (parser, "expected `,'"); | |
14986 | ||
a723baf1 MM |
14987 | /* Restore saved state. */ |
14988 | parser->lexer = saved_lexer; | |
14989 | parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; | |
14990 | } | |
b92bc2a0 NS |
14991 | |
14992 | /* Restore the queue. */ | |
21526606 | 14993 | parser->unparsed_functions_queues |
b92bc2a0 | 14994 | = TREE_CHAIN (parser->unparsed_functions_queues); |
a723baf1 MM |
14995 | } |
14996 | ||
14997 | /* Parse the operand of `sizeof' (or a similar operator). Returns | |
14998 | either a TYPE or an expression, depending on the form of the | |
14999 | input. The KEYWORD indicates which kind of expression we have | |
15000 | encountered. */ | |
15001 | ||
15002 | static tree | |
94edc4ab | 15003 | cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) |
a723baf1 MM |
15004 | { |
15005 | static const char *format; | |
15006 | tree expr = NULL_TREE; | |
15007 | const char *saved_message; | |
67c03833 | 15008 | bool saved_integral_constant_expression_p; |
a723baf1 MM |
15009 | |
15010 | /* Initialize FORMAT the first time we get here. */ | |
15011 | if (!format) | |
15012 | format = "types may not be defined in `%s' expressions"; | |
15013 | ||
15014 | /* Types cannot be defined in a `sizeof' expression. Save away the | |
15015 | old message. */ | |
15016 | saved_message = parser->type_definition_forbidden_message; | |
15017 | /* And create the new one. */ | |
21526606 EC |
15018 | parser->type_definition_forbidden_message |
15019 | = xmalloc (strlen (format) | |
c68b0a84 KG |
15020 | + strlen (IDENTIFIER_POINTER (ridpointers[keyword])) |
15021 | + 1 /* `\0' */); | |
a723baf1 MM |
15022 | sprintf ((char *) parser->type_definition_forbidden_message, |
15023 | format, IDENTIFIER_POINTER (ridpointers[keyword])); | |
15024 | ||
15025 | /* The restrictions on constant-expressions do not apply inside | |
15026 | sizeof expressions. */ | |
67c03833 JM |
15027 | saved_integral_constant_expression_p = parser->integral_constant_expression_p; |
15028 | parser->integral_constant_expression_p = false; | |
a723baf1 | 15029 | |
3beb3abf MM |
15030 | /* Do not actually evaluate the expression. */ |
15031 | ++skip_evaluation; | |
a723baf1 MM |
15032 | /* If it's a `(', then we might be looking at the type-id |
15033 | construction. */ | |
15034 | if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) | |
15035 | { | |
15036 | tree type; | |
4f8163b1 | 15037 | bool saved_in_type_id_in_expr_p; |
a723baf1 MM |
15038 | |
15039 | /* We can't be sure yet whether we're looking at a type-id or an | |
15040 | expression. */ | |
15041 | cp_parser_parse_tentatively (parser); | |
15042 | /* Consume the `('. */ | |
15043 | cp_lexer_consume_token (parser->lexer); | |
15044 | /* Parse the type-id. */ | |
4f8163b1 MM |
15045 | saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; |
15046 | parser->in_type_id_in_expr_p = true; | |
a723baf1 | 15047 | type = cp_parser_type_id (parser); |
4f8163b1 | 15048 | parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; |
a723baf1 MM |
15049 | /* Now, look for the trailing `)'. */ |
15050 | cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"); | |
15051 | /* If all went well, then we're done. */ | |
15052 | if (cp_parser_parse_definitely (parser)) | |
15053 | { | |
15054 | /* Build a list of decl-specifiers; right now, we have only | |
15055 | a single type-specifier. */ | |
15056 | type = build_tree_list (NULL_TREE, | |
15057 | type); | |
15058 | ||
15059 | /* Call grokdeclarator to figure out what type this is. */ | |
15060 | expr = grokdeclarator (NULL_TREE, | |
15061 | type, | |
15062 | TYPENAME, | |
15063 | /*initialized=*/0, | |
15064 | /*attrlist=*/NULL); | |
15065 | } | |
15066 | } | |
15067 | ||
15068 | /* If the type-id production did not work out, then we must be | |
15069 | looking at the unary-expression production. */ | |
15070 | if (!expr) | |
15071 | expr = cp_parser_unary_expression (parser, /*address_p=*/false); | |
3beb3abf MM |
15072 | /* Go back to evaluating expressions. */ |
15073 | --skip_evaluation; | |
a723baf1 MM |
15074 | |
15075 | /* Free the message we created. */ | |
15076 | free ((char *) parser->type_definition_forbidden_message); | |
15077 | /* And restore the old one. */ | |
15078 | parser->type_definition_forbidden_message = saved_message; | |
67c03833 | 15079 | parser->integral_constant_expression_p = saved_integral_constant_expression_p; |
a723baf1 MM |
15080 | |
15081 | return expr; | |
15082 | } | |
15083 | ||
15084 | /* If the current declaration has no declarator, return true. */ | |
15085 | ||
15086 | static bool | |
15087 | cp_parser_declares_only_class_p (cp_parser *parser) | |
15088 | { | |
21526606 | 15089 | /* If the next token is a `;' or a `,' then there is no |
a723baf1 MM |
15090 | declarator. */ |
15091 | return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) | |
15092 | || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); | |
15093 | } | |
15094 | ||
15095 | /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. | |
15096 | Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ | |
15097 | ||
15098 | static bool | |
94edc4ab | 15099 | cp_parser_friend_p (tree decl_specifiers) |
a723baf1 MM |
15100 | { |
15101 | while (decl_specifiers) | |
15102 | { | |
15103 | /* See if this decl-specifier is `friend'. */ | |
15104 | if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE | |
15105 | && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND) | |
15106 | return true; | |
15107 | ||
15108 | /* Go on to the next decl-specifier. */ | |
15109 | decl_specifiers = TREE_CHAIN (decl_specifiers); | |
15110 | } | |
15111 | ||
15112 | return false; | |
15113 | } | |
15114 | ||
15115 | /* If the next token is of the indicated TYPE, consume it. Otherwise, | |
15116 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15117 | |
a723baf1 MM |
15118 | Returns the token consumed, if the token had the appropriate type. |
15119 | Otherwise, returns NULL. */ | |
15120 | ||
15121 | static cp_token * | |
94edc4ab NN |
15122 | cp_parser_require (cp_parser* parser, |
15123 | enum cpp_ttype type, | |
15124 | const char* token_desc) | |
a723baf1 MM |
15125 | { |
15126 | if (cp_lexer_next_token_is (parser->lexer, type)) | |
15127 | return cp_lexer_consume_token (parser->lexer); | |
15128 | else | |
15129 | { | |
e5976695 MM |
15130 | /* Output the MESSAGE -- unless we're parsing tentatively. */ |
15131 | if (!cp_parser_simulate_error (parser)) | |
216bb6e1 MM |
15132 | { |
15133 | char *message = concat ("expected ", token_desc, NULL); | |
15134 | cp_parser_error (parser, message); | |
15135 | free (message); | |
15136 | } | |
a723baf1 MM |
15137 | return NULL; |
15138 | } | |
15139 | } | |
15140 | ||
15141 | /* Like cp_parser_require, except that tokens will be skipped until | |
15142 | the desired token is found. An error message is still produced if | |
15143 | the next token is not as expected. */ | |
15144 | ||
15145 | static void | |
21526606 EC |
15146 | cp_parser_skip_until_found (cp_parser* parser, |
15147 | enum cpp_ttype type, | |
94edc4ab | 15148 | const char* token_desc) |
a723baf1 MM |
15149 | { |
15150 | cp_token *token; | |
15151 | unsigned nesting_depth = 0; | |
15152 | ||
15153 | if (cp_parser_require (parser, type, token_desc)) | |
15154 | return; | |
15155 | ||
15156 | /* Skip tokens until the desired token is found. */ | |
15157 | while (true) | |
15158 | { | |
15159 | /* Peek at the next token. */ | |
15160 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 15161 | /* If we've reached the token we want, consume it and |
a723baf1 MM |
15162 | stop. */ |
15163 | if (token->type == type && !nesting_depth) | |
15164 | { | |
15165 | cp_lexer_consume_token (parser->lexer); | |
15166 | return; | |
15167 | } | |
15168 | /* If we've run out of tokens, stop. */ | |
15169 | if (token->type == CPP_EOF) | |
15170 | return; | |
21526606 | 15171 | if (token->type == CPP_OPEN_BRACE |
a723baf1 MM |
15172 | || token->type == CPP_OPEN_PAREN |
15173 | || token->type == CPP_OPEN_SQUARE) | |
15174 | ++nesting_depth; | |
21526606 | 15175 | else if (token->type == CPP_CLOSE_BRACE |
a723baf1 MM |
15176 | || token->type == CPP_CLOSE_PAREN |
15177 | || token->type == CPP_CLOSE_SQUARE) | |
15178 | { | |
15179 | if (nesting_depth-- == 0) | |
15180 | return; | |
15181 | } | |
15182 | /* Consume this token. */ | |
15183 | cp_lexer_consume_token (parser->lexer); | |
15184 | } | |
15185 | } | |
15186 | ||
15187 | /* If the next token is the indicated keyword, consume it. Otherwise, | |
15188 | issue an error message indicating that TOKEN_DESC was expected. | |
21526606 | 15189 | |
a723baf1 MM |
15190 | Returns the token consumed, if the token had the appropriate type. |
15191 | Otherwise, returns NULL. */ | |
15192 | ||
15193 | static cp_token * | |
94edc4ab NN |
15194 | cp_parser_require_keyword (cp_parser* parser, |
15195 | enum rid keyword, | |
15196 | const char* token_desc) | |
a723baf1 MM |
15197 | { |
15198 | cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); | |
15199 | ||
15200 | if (token && token->keyword != keyword) | |
15201 | { | |
15202 | dyn_string_t error_msg; | |
15203 | ||
15204 | /* Format the error message. */ | |
15205 | error_msg = dyn_string_new (0); | |
15206 | dyn_string_append_cstr (error_msg, "expected "); | |
15207 | dyn_string_append_cstr (error_msg, token_desc); | |
15208 | cp_parser_error (parser, error_msg->s); | |
15209 | dyn_string_delete (error_msg); | |
15210 | return NULL; | |
15211 | } | |
15212 | ||
15213 | return token; | |
15214 | } | |
15215 | ||
15216 | /* Returns TRUE iff TOKEN is a token that can begin the body of a | |
15217 | function-definition. */ | |
15218 | ||
21526606 | 15219 | static bool |
94edc4ab | 15220 | cp_parser_token_starts_function_definition_p (cp_token* token) |
a723baf1 MM |
15221 | { |
15222 | return (/* An ordinary function-body begins with an `{'. */ | |
15223 | token->type == CPP_OPEN_BRACE | |
15224 | /* A ctor-initializer begins with a `:'. */ | |
15225 | || token->type == CPP_COLON | |
15226 | /* A function-try-block begins with `try'. */ | |
15227 | || token->keyword == RID_TRY | |
15228 | /* The named return value extension begins with `return'. */ | |
15229 | || token->keyword == RID_RETURN); | |
15230 | } | |
15231 | ||
15232 | /* Returns TRUE iff the next token is the ":" or "{" beginning a class | |
15233 | definition. */ | |
15234 | ||
15235 | static bool | |
15236 | cp_parser_next_token_starts_class_definition_p (cp_parser *parser) | |
15237 | { | |
15238 | cp_token *token; | |
15239 | ||
15240 | token = cp_lexer_peek_token (parser->lexer); | |
15241 | return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); | |
15242 | } | |
15243 | ||
d17811fd | 15244 | /* Returns TRUE iff the next token is the "," or ">" ending a |
4d5297fa GB |
15245 | template-argument. ">>" is also accepted (after the full |
15246 | argument was parsed) because it's probably a typo for "> >", | |
15247 | and there is a specific diagnostic for this. */ | |
d17811fd MM |
15248 | |
15249 | static bool | |
15250 | cp_parser_next_token_ends_template_argument_p (cp_parser *parser) | |
15251 | { | |
15252 | cp_token *token; | |
15253 | ||
15254 | token = cp_lexer_peek_token (parser->lexer); | |
21526606 | 15255 | return (token->type == CPP_COMMA || token->type == CPP_GREATER |
4d5297fa | 15256 | || token->type == CPP_RSHIFT); |
d17811fd | 15257 | } |
f4abade9 GB |
15258 | |
15259 | /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the | |
15260 | (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ | |
15261 | ||
15262 | static bool | |
21526606 | 15263 | cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, |
f4abade9 GB |
15264 | size_t n) |
15265 | { | |
15266 | cp_token *token; | |
15267 | ||
15268 | token = cp_lexer_peek_nth_token (parser->lexer, n); | |
15269 | if (token->type == CPP_LESS) | |
15270 | return true; | |
15271 | /* Check for the sequence `<::' in the original code. It would be lexed as | |
15272 | `[:', where `[' is a digraph, and there is no whitespace before | |
15273 | `:'. */ | |
15274 | if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) | |
15275 | { | |
15276 | cp_token *token2; | |
15277 | token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); | |
15278 | if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) | |
15279 | return true; | |
15280 | } | |
15281 | return false; | |
15282 | } | |
21526606 | 15283 | |
a723baf1 MM |
15284 | /* Returns the kind of tag indicated by TOKEN, if it is a class-key, |
15285 | or none_type otherwise. */ | |
15286 | ||
15287 | static enum tag_types | |
94edc4ab | 15288 | cp_parser_token_is_class_key (cp_token* token) |
a723baf1 MM |
15289 | { |
15290 | switch (token->keyword) | |
15291 | { | |
15292 | case RID_CLASS: | |
15293 | return class_type; | |
15294 | case RID_STRUCT: | |
15295 | return record_type; | |
15296 | case RID_UNION: | |
15297 | return union_type; | |
21526606 | 15298 | |
a723baf1 MM |
15299 | default: |
15300 | return none_type; | |
15301 | } | |
15302 | } | |
15303 | ||
15304 | /* Issue an error message if the CLASS_KEY does not match the TYPE. */ | |
15305 | ||
15306 | static void | |
15307 | cp_parser_check_class_key (enum tag_types class_key, tree type) | |
15308 | { | |
15309 | if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) | |
15310 | pedwarn ("`%s' tag used in naming `%#T'", | |
15311 | class_key == union_type ? "union" | |
21526606 | 15312 | : class_key == record_type ? "struct" : "class", |
a723baf1 MM |
15313 | type); |
15314 | } | |
21526606 | 15315 | |
cd0be382 | 15316 | /* Issue an error message if DECL is redeclared with different |
37d407a1 KL |
15317 | access than its original declaration [class.access.spec/3]. |
15318 | This applies to nested classes and nested class templates. | |
15319 | [class.mem/1]. */ | |
15320 | ||
15321 | static void cp_parser_check_access_in_redeclaration (tree decl) | |
15322 | { | |
15323 | if (!CLASS_TYPE_P (TREE_TYPE (decl))) | |
15324 | return; | |
15325 | ||
15326 | if ((TREE_PRIVATE (decl) | |
15327 | != (current_access_specifier == access_private_node)) | |
15328 | || (TREE_PROTECTED (decl) | |
15329 | != (current_access_specifier == access_protected_node))) | |
15330 | error ("%D redeclared with different access", decl); | |
15331 | } | |
15332 | ||
a723baf1 | 15333 | /* Look for the `template' keyword, as a syntactic disambiguator. |
21526606 | 15334 | Return TRUE iff it is present, in which case it will be |
a723baf1 MM |
15335 | consumed. */ |
15336 | ||
15337 | static bool | |
15338 | cp_parser_optional_template_keyword (cp_parser *parser) | |
15339 | { | |
15340 | if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) | |
15341 | { | |
15342 | /* The `template' keyword can only be used within templates; | |
15343 | outside templates the parser can always figure out what is a | |
15344 | template and what is not. */ | |
15345 | if (!processing_template_decl) | |
15346 | { | |
15347 | error ("`template' (as a disambiguator) is only allowed " | |
15348 | "within templates"); | |
15349 | /* If this part of the token stream is rescanned, the same | |
15350 | error message would be generated. So, we purge the token | |
15351 | from the stream. */ | |
15352 | cp_lexer_purge_token (parser->lexer); | |
15353 | return false; | |
15354 | } | |
15355 | else | |
15356 | { | |
15357 | /* Consume the `template' keyword. */ | |
15358 | cp_lexer_consume_token (parser->lexer); | |
15359 | return true; | |
15360 | } | |
15361 | } | |
15362 | ||
15363 | return false; | |
15364 | } | |
15365 | ||
2050a1bb MM |
15366 | /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, |
15367 | set PARSER->SCOPE, and perform other related actions. */ | |
15368 | ||
15369 | static void | |
15370 | cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) | |
15371 | { | |
15372 | tree value; | |
15373 | tree check; | |
15374 | ||
15375 | /* Get the stored value. */ | |
15376 | value = cp_lexer_consume_token (parser->lexer)->value; | |
15377 | /* Perform any access checks that were deferred. */ | |
15378 | for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check)) | |
cf22909c | 15379 | perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check)); |
2050a1bb MM |
15380 | /* Set the scope from the stored value. */ |
15381 | parser->scope = TREE_VALUE (value); | |
15382 | parser->qualifying_scope = TREE_TYPE (value); | |
15383 | parser->object_scope = NULL_TREE; | |
15384 | } | |
15385 | ||
852dcbdd | 15386 | /* Add tokens to CACHE until a non-nested END token appears. */ |
a723baf1 MM |
15387 | |
15388 | static void | |
0173bb6f AO |
15389 | cp_parser_cache_group_1 (cp_parser *parser, |
15390 | cp_token_cache *cache, | |
15391 | enum cpp_ttype end, | |
15392 | unsigned depth) | |
a723baf1 MM |
15393 | { |
15394 | while (true) | |
15395 | { | |
15396 | cp_token *token; | |
15397 | ||
15398 | /* Abort a parenthesized expression if we encounter a brace. */ | |
15399 | if ((end == CPP_CLOSE_PAREN || depth == 0) | |
15400 | && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) | |
15401 | return; | |
a723baf1 | 15402 | /* If we've reached the end of the file, stop. */ |
4bfb8bba | 15403 | if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) |
a723baf1 | 15404 | return; |
4bfb8bba MM |
15405 | /* Consume the next token. */ |
15406 | token = cp_lexer_consume_token (parser->lexer); | |
a723baf1 MM |
15407 | /* Add this token to the tokens we are saving. */ |
15408 | cp_token_cache_push_token (cache, token); | |
15409 | /* See if it starts a new group. */ | |
15410 | if (token->type == CPP_OPEN_BRACE) | |
15411 | { | |
0173bb6f | 15412 | cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1); |
a723baf1 MM |
15413 | if (depth == 0) |
15414 | return; | |
15415 | } | |
15416 | else if (token->type == CPP_OPEN_PAREN) | |
0173bb6f | 15417 | cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1); |
a723baf1 MM |
15418 | else if (token->type == end) |
15419 | return; | |
15420 | } | |
15421 | } | |
15422 | ||
0173bb6f AO |
15423 | /* Convenient interface for cp_parser_cache_group_1 that makes sure we |
15424 | preserve string tokens in both translated and untranslated | |
15425 | forms. */ | |
15426 | ||
15427 | static void | |
15428 | cp_parser_cache_group (cp_parser *parser, | |
15429 | cp_token_cache *cache, | |
15430 | enum cpp_ttype end, | |
15431 | unsigned depth) | |
15432 | { | |
15433 | int saved_c_lex_string_translate; | |
15434 | ||
15435 | saved_c_lex_string_translate = c_lex_string_translate; | |
15436 | c_lex_string_translate = -1; | |
15437 | ||
15438 | cp_parser_cache_group_1 (parser, cache, end, depth); | |
15439 | ||
15440 | c_lex_string_translate = saved_c_lex_string_translate; | |
15441 | } | |
15442 | ||
15443 | ||
a723baf1 MM |
15444 | /* Begin parsing tentatively. We always save tokens while parsing |
15445 | tentatively so that if the tentative parsing fails we can restore the | |
15446 | tokens. */ | |
15447 | ||
15448 | static void | |
94edc4ab | 15449 | cp_parser_parse_tentatively (cp_parser* parser) |
a723baf1 MM |
15450 | { |
15451 | /* Enter a new parsing context. */ | |
15452 | parser->context = cp_parser_context_new (parser->context); | |
15453 | /* Begin saving tokens. */ | |
15454 | cp_lexer_save_tokens (parser->lexer); | |
15455 | /* In order to avoid repetitive access control error messages, | |
15456 | access checks are queued up until we are no longer parsing | |
15457 | tentatively. */ | |
8d241e0b | 15458 | push_deferring_access_checks (dk_deferred); |
a723baf1 MM |
15459 | } |
15460 | ||
15461 | /* Commit to the currently active tentative parse. */ | |
15462 | ||
15463 | static void | |
94edc4ab | 15464 | cp_parser_commit_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15465 | { |
15466 | cp_parser_context *context; | |
15467 | cp_lexer *lexer; | |
15468 | ||
15469 | /* Mark all of the levels as committed. */ | |
15470 | lexer = parser->lexer; | |
15471 | for (context = parser->context; context->next; context = context->next) | |
15472 | { | |
15473 | if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) | |
15474 | break; | |
15475 | context->status = CP_PARSER_STATUS_KIND_COMMITTED; | |
15476 | while (!cp_lexer_saving_tokens (lexer)) | |
15477 | lexer = lexer->next; | |
15478 | cp_lexer_commit_tokens (lexer); | |
15479 | } | |
15480 | } | |
15481 | ||
15482 | /* Abort the currently active tentative parse. All consumed tokens | |
15483 | will be rolled back, and no diagnostics will be issued. */ | |
15484 | ||
15485 | static void | |
94edc4ab | 15486 | cp_parser_abort_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15487 | { |
15488 | cp_parser_simulate_error (parser); | |
15489 | /* Now, pretend that we want to see if the construct was | |
15490 | successfully parsed. */ | |
15491 | cp_parser_parse_definitely (parser); | |
15492 | } | |
15493 | ||
34cd5ae7 | 15494 | /* Stop parsing tentatively. If a parse error has occurred, restore the |
a723baf1 MM |
15495 | token stream. Otherwise, commit to the tokens we have consumed. |
15496 | Returns true if no error occurred; false otherwise. */ | |
15497 | ||
15498 | static bool | |
94edc4ab | 15499 | cp_parser_parse_definitely (cp_parser* parser) |
a723baf1 MM |
15500 | { |
15501 | bool error_occurred; | |
15502 | cp_parser_context *context; | |
15503 | ||
34cd5ae7 | 15504 | /* Remember whether or not an error occurred, since we are about to |
a723baf1 MM |
15505 | destroy that information. */ |
15506 | error_occurred = cp_parser_error_occurred (parser); | |
15507 | /* Remove the topmost context from the stack. */ | |
15508 | context = parser->context; | |
15509 | parser->context = context->next; | |
15510 | /* If no parse errors occurred, commit to the tentative parse. */ | |
15511 | if (!error_occurred) | |
15512 | { | |
15513 | /* Commit to the tokens read tentatively, unless that was | |
15514 | already done. */ | |
15515 | if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) | |
15516 | cp_lexer_commit_tokens (parser->lexer); | |
cf22909c KL |
15517 | |
15518 | pop_to_parent_deferring_access_checks (); | |
a723baf1 MM |
15519 | } |
15520 | /* Otherwise, if errors occurred, roll back our state so that things | |
15521 | are just as they were before we began the tentative parse. */ | |
15522 | else | |
cf22909c KL |
15523 | { |
15524 | cp_lexer_rollback_tokens (parser->lexer); | |
15525 | pop_deferring_access_checks (); | |
15526 | } | |
e5976695 MM |
15527 | /* Add the context to the front of the free list. */ |
15528 | context->next = cp_parser_context_free_list; | |
15529 | cp_parser_context_free_list = context; | |
15530 | ||
15531 | return !error_occurred; | |
a723baf1 MM |
15532 | } |
15533 | ||
a723baf1 MM |
15534 | /* Returns true if we are parsing tentatively -- but have decided that |
15535 | we will stick with this tentative parse, even if errors occur. */ | |
15536 | ||
15537 | static bool | |
94edc4ab | 15538 | cp_parser_committed_to_tentative_parse (cp_parser* parser) |
a723baf1 MM |
15539 | { |
15540 | return (cp_parser_parsing_tentatively (parser) | |
15541 | && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED); | |
15542 | } | |
15543 | ||
4de8668e | 15544 | /* Returns nonzero iff an error has occurred during the most recent |
a723baf1 | 15545 | tentative parse. */ |
21526606 | 15546 | |
a723baf1 | 15547 | static bool |
94edc4ab | 15548 | cp_parser_error_occurred (cp_parser* parser) |
a723baf1 MM |
15549 | { |
15550 | return (cp_parser_parsing_tentatively (parser) | |
15551 | && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); | |
15552 | } | |
15553 | ||
4de8668e | 15554 | /* Returns nonzero if GNU extensions are allowed. */ |
a723baf1 MM |
15555 | |
15556 | static bool | |
94edc4ab | 15557 | cp_parser_allow_gnu_extensions_p (cp_parser* parser) |
a723baf1 MM |
15558 | { |
15559 | return parser->allow_gnu_extensions_p; | |
15560 | } | |
15561 | ||
15562 | \f | |
a723baf1 MM |
15563 | /* The parser. */ |
15564 | ||
15565 | static GTY (()) cp_parser *the_parser; | |
15566 | ||
15567 | /* External interface. */ | |
15568 | ||
d1bd0ded | 15569 | /* Parse one entire translation unit. */ |
a723baf1 | 15570 | |
d1bd0ded GK |
15571 | void |
15572 | c_parse_file (void) | |
a723baf1 MM |
15573 | { |
15574 | bool error_occurred; | |
f75fbaf7 ZW |
15575 | static bool already_called = false; |
15576 | ||
15577 | if (already_called) | |
15578 | { | |
15579 | sorry ("inter-module optimizations not implemented for C++"); | |
15580 | return; | |
15581 | } | |
15582 | already_called = true; | |
a723baf1 MM |
15583 | |
15584 | the_parser = cp_parser_new (); | |
78757caa KL |
15585 | push_deferring_access_checks (flag_access_control |
15586 | ? dk_no_deferred : dk_no_check); | |
a723baf1 MM |
15587 | error_occurred = cp_parser_translation_unit (the_parser); |
15588 | the_parser = NULL; | |
a723baf1 MM |
15589 | } |
15590 | ||
a723baf1 MM |
15591 | /* This variable must be provided by every front end. */ |
15592 | ||
15593 | int yydebug; | |
15594 | ||
15595 | #include "gt-cp-parser.h" |