]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
darwin.c (machopic_function_base_name): Remove current_name and getting the name...
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
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
68typedef 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
99typedef 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
111typedef 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 123static cp_token_cache *cp_token_cache_new
a723baf1
MM
124 (void);
125static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
127
128/* Create a new cp_token_cache. */
129
130static cp_token_cache *
bf9d3c27 131cp_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
138static void
139cp_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
165typedef 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. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
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. */
182 cp_token * GTY ((skip (""))) last_token;
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 216static cp_lexer *cp_lexer_new_main
94edc4ab 217 (void);
a723baf1 218static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 219 (struct cp_token_cache *);
a723baf1 220static int cp_lexer_saving_tokens
94edc4ab 221 (const cp_lexer *);
a723baf1 222static cp_token *cp_lexer_next_token
94edc4ab 223 (cp_lexer *, cp_token *);
a668c6ad
MM
224static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
21526606 226static ptrdiff_t cp_lexer_token_difference
94edc4ab 227 (cp_lexer *, cp_token *, cp_token *);
a723baf1 228static cp_token *cp_lexer_read_token
94edc4ab 229 (cp_lexer *);
a723baf1 230static void cp_lexer_maybe_grow_buffer
94edc4ab 231 (cp_lexer *);
a723baf1 232static void cp_lexer_get_preprocessor_token
94edc4ab 233 (cp_lexer *, cp_token *);
a723baf1 234static cp_token *cp_lexer_peek_token
94edc4ab 235 (cp_lexer *);
a723baf1 236static cp_token *cp_lexer_peek_nth_token
94edc4ab 237 (cp_lexer *, size_t);
f7b5ecd9 238static inline bool cp_lexer_next_token_is
94edc4ab 239 (cp_lexer *, enum cpp_ttype);
a723baf1 240static bool cp_lexer_next_token_is_not
94edc4ab 241 (cp_lexer *, enum cpp_ttype);
a723baf1 242static bool cp_lexer_next_token_is_keyword
94edc4ab 243 (cp_lexer *, enum rid);
21526606 244static cp_token *cp_lexer_consume_token
94edc4ab 245 (cp_lexer *);
a723baf1
MM
246static void cp_lexer_purge_token
247 (cp_lexer *);
248static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250static void cp_lexer_save_tokens
94edc4ab 251 (cp_lexer *);
a723baf1 252static void cp_lexer_commit_tokens
94edc4ab 253 (cp_lexer *);
a723baf1 254static void cp_lexer_rollback_tokens
94edc4ab 255 (cp_lexer *);
21526606 256static inline void cp_lexer_set_source_position_from_token
94edc4ab 257 (cp_lexer *, const cp_token *);
a723baf1 258static void cp_lexer_print_token
94edc4ab 259 (FILE *, cp_token *);
21526606 260static inline bool cp_lexer_debugging_p
94edc4ab 261 (cp_lexer *);
a723baf1 262static void cp_lexer_start_debugging
94edc4ab 263 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 264static 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. */
295static 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
300static cp_lexer *
17211ab5 301cp_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
343static cp_lexer *
344cp_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
393static inline bool
394cp_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
402static inline void
94edc4ab
NN
403cp_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 417static inline cp_token *
94edc4ab 418cp_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
429static inline cp_token *
430cp_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
439static int
94edc4ab 440cp_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
448static cp_token *
449cp_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
460static ptrdiff_t
94edc4ab 461cp_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
473static cp_token *
94edc4ab 474cp_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 {
e6cc3a24
ZW
504 tree type;
505
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
a723baf1
MM
513 }
514
515 return token;
516}
517
518/* If the circular buffer is full, make it bigger. */
519
520static void
94edc4ab 521cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
522{
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
525 {
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
531
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
534 value. */
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
21526606 539 new_buffer = ggc_realloc (lexer->buffer,
c68b0a84 540 2 * buffer_length * sizeof (cp_token));
21526606 541
a723baf1
MM
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
546 buffer. */
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
549 new_buffer,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
21526606 553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
a723baf1
MM
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556 /* Now recompute all of the buffer pointers. */
21526606 557 new_first_token
a723baf1
MM
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
560 {
561 ptrdiff_t next_token_delta;
562
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
565 else
21526606 566 next_token_delta =
a723baf1
MM
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
569 }
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
574 }
575}
576
577/* Store the next token from the preprocessor in *TOKEN. */
578
21526606 579static void
94edc4ab
NN
580cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581 cp_token *token)
a723baf1
MM
582{
583 bool done;
584
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 586 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
587 {
588 token->type = CPP_EOF;
82a98427
NS
589 token->location.line = 0;
590 token->location.file = NULL;
a723baf1
MM
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
593
594 return;
595 }
596
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
600 {
601 /* Get a new token from the preprocessor. */
f4abade9 602 token->type = c_lex_with_flags (&token->value, &token->flags);
a723baf1
MM
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
605 {
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
611
a723baf1
MM
612 default:
613 /* This is a good token, so we exit the loop. */
614 done = true;
615 break;
616 }
617 }
618 /* Now we've got our token. */
82a98427 619 token->location = input_location;
a723baf1
MM
620
621 /* Check to see if this token is a keyword. */
21526606 622 if (token->type == CPP_NAME
a723baf1
MM
623 && C_IS_RESERVED_WORD (token->value))
624 {
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
634 }
635 else
636 token->keyword = RID_MAX;
637}
638
639/* Return a pointer to the next token in the token stream, but do not
640 consume it. */
641
642static cp_token *
94edc4ab 643cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
644{
645 cp_token *token;
646
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
650
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
653 {
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
657 }
658
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
661 return token;
662}
663
664/* Return true if the next token has the indicated TYPE. */
665
666static bool
94edc4ab 667cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
668{
669 cp_token *token;
670
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
675}
676
677/* Return true if the next token does not have the indicated TYPE. */
678
679static bool
94edc4ab 680cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
681{
682 return !cp_lexer_next_token_is (lexer, type);
683}
684
685/* Return true if the next token is the indicated KEYWORD. */
686
687static bool
94edc4ab 688cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
689{
690 cp_token *token;
691
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
696}
697
698/* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
700
701static cp_token *
94edc4ab 702cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
703{
704 cp_token *token;
705
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
708
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
712 if (!token)
713 {
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
716 }
717
718 /* Now, read tokens until we have enough. */
719 while (--n > 0)
720 {
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
726 }
727
728 return token;
729}
730
731/* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
734 time. */
735
736static cp_token *
94edc4ab 737cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
738{
739 cp_token *token;
740
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
744
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
747
748 /* Increment NEXT_TOKEN. */
21526606 749 lexer->next_token = cp_lexer_next_token (lexer,
a723baf1
MM
750 lexer->next_token);
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
754
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
757 {
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
761 else
762 lexer->first_token = lexer->next_token;
763 }
764
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
767 {
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
771 }
772
773 return token;
774}
775
776/* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
779
780static void
781cp_lexer_purge_token (cp_lexer *lexer)
782{
783 cp_token *token;
784 cp_token *next_token;
785
786 token = lexer->next_token;
21526606 787 while (true)
a723baf1
MM
788 {
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
791 break;
792 *token = *next_token;
793 token = next_token;
794 }
795
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
798 clear NEXT_TOKEN. */
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
801}
802
803/* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
806
807static void
808cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809{
810 cp_token *peek;
811 cp_token *t1;
812 cp_token *t2;
813
814 if (lexer->next_token)
815 {
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
822 {
823 *t1 = *t2;
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
826 }
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
831 }
832 else
833 {
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
837 }
838}
839
840/* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
842
843static void
94edc4ab 844cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
845{
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
854
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
857 lexer->first_token,
858 lexer->next_token));
859}
860
861/* Commit to the portion of the token stream most recently saved. */
862
863static void
94edc4ab 864cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
865{
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870 VARRAY_POP (lexer->saved_tokens);
871}
872
873/* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
875
876static void
94edc4ab 877cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
878{
879 size_t delta;
880
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885 /* Find the token that was the NEXT_TOKEN when we started saving
886 tokens. */
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
21526606 890 lexer->first_token,
a723baf1 891 delta);
15d2cb19 892 /* It might be the case that there were no tokens when we started
a723baf1
MM
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
896
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
899}
900
a723baf1
MM
901/* Print a representation of the TOKEN on the STREAM. */
902
903static void
94edc4ab 904cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
905{
906 const char *token_type = NULL;
907
908 /* Figure out what kind of token this is. */
909 switch (token->type)
910 {
911 case CPP_EQ:
912 token_type = "EQ";
913 break;
914
915 case CPP_COMMA:
916 token_type = "COMMA";
917 break;
918
919 case CPP_OPEN_PAREN:
920 token_type = "OPEN_PAREN";
921 break;
922
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
925 break;
926
927 case CPP_OPEN_BRACE:
928 token_type = "OPEN_BRACE";
929 break;
930
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
933 break;
934
935 case CPP_SEMICOLON:
936 token_type = "SEMICOLON";
937 break;
938
939 case CPP_NAME:
940 token_type = "NAME";
941 break;
942
943 case CPP_EOF:
944 token_type = "EOF";
945 break;
946
947 case CPP_KEYWORD:
948 token_type = "keyword";
949 break;
950
951 /* This is not a token that we know how to handle yet. */
952 default:
953 break;
954 }
955
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
958 if (token_type)
959 fprintf (stream, "%s", token_type);
960 else
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
21526606 963 if (token->type == CPP_NAME
a723baf1
MM
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
21526606 966 || (token->type == CPP_KEYWORD
a723baf1
MM
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969}
970
a723baf1
MM
971/* Start emitting debugging information. */
972
973static void
94edc4ab 974cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
975{
976 ++lexer->debugging_p;
977}
21526606 978
a723baf1
MM
979/* Stop emitting debugging information. */
980
981static void
94edc4ab 982cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
983{
984 --lexer->debugging_p;
985}
986
987\f
988/* The parser. */
989
990/* Overview
991 --------
992
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1000
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
21526606 1003 code processed.
a723baf1
MM
1004
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1013
1014 Methodology
1015 -----------
21526606 1016
a723baf1
MM
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1033
1034 Future Improvements
1035 -------------------
21526606 1036
a723baf1
MM
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1039
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1044
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1049
1050/* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1052
1053typedef enum cp_parser_flags
1054{
1055 /* No flags. */
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062} cp_parser_flags;
1063
62b8a44e
NS
1064/* The different kinds of declarators we want to parse. */
1065
1066typedef enum cp_parser_declarator_kind
1067{
852dcbdd 1068 /* We want an abstract declarator. */
62b8a44e
NS
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
04c06002 1072 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1073 CP_PARSER_DECLARATOR_EITHER
1074} cp_parser_declarator_kind;
1075
a723baf1
MM
1076/* A mapping from a token type to a corresponding tree node type. */
1077
1078typedef struct cp_parser_token_tree_map_node
1079{
1080 /* The token type. */
df2b750f 1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
a723baf1 1082 /* The corresponding tree code. */
df2b750f 1083 ENUM_BITFIELD (tree_code) tree_type : 8;
a723baf1
MM
1084} cp_parser_token_tree_map_node;
1085
1086/* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1088
1089typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091/* The status of a tentative parse. */
1092
1093typedef enum cp_parser_status_kind
1094{
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1100 has occurred. */
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102} cp_parser_status_kind;
1103
1104/* Context that is saved and restored when parsing tentatively. */
1105
1106typedef struct cp_parser_context GTY (())
1107{
1108 /* If this is a tentative parsing context, the status of the
1109 tentative parse. */
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1115 tree object_type;
a723baf1
MM
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118} cp_parser_context;
1119
1120/* Prototypes. */
1121
1122/* Constructors and destructors. */
1123
1124static cp_parser_context *cp_parser_context_new
94edc4ab 1125 (cp_parser_context *);
a723baf1 1126
e5976695
MM
1127/* Class variables. */
1128
92bc1323 1129static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1130
a723baf1
MM
1131/* Constructors and destructors. */
1132
1133/* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1135
1136static cp_parser_context *
94edc4ab 1137cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1138{
1139 cp_parser_context *context;
1140
1141 /* Allocate the storage. */
e5976695
MM
1142 if (cp_parser_context_free_list != NULL)
1143 {
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
c68b0a84 1147 memset (context, 0, sizeof (*context));
e5976695
MM
1148 }
1149 else
c68b0a84 1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1155 if (next)
1156 {
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
a723baf1
MM
1160 /* Thread the stack. */
1161 context->next = next;
1162 }
1163
1164 return context;
1165}
1166
1167/* The cp_parser structure represents the C++ parser. */
1168
1169typedef struct cp_parser GTY(())
1170{
1171 /* The lexer from which we are obtaining tokens. */
1172 cp_lexer *lexer;
1173
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
21526606 1177 NAMESPACE_DECL for the scope in which we should look.
a723baf1
MM
1178
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1186 tree scope;
1187
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1191 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1192 form "X::Y"; it refers to X. */
1193 tree object_scope;
1194 tree qualifying_scope;
1195
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1198
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1206
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1210
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1215
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1218 them permissible. */
a723baf1 1219 bool default_arg_ok_p;
21526606 1220
a723baf1
MM
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
67c03833 1223 bool integral_constant_expression_p;
a723baf1 1224
14d22dd6
MM
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
67c03833 1229 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1230
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
67c03833 1233 bool non_integral_constant_expression_p;
14d22dd6 1234
263ee052
MM
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1236 bool in_offsetof_p;
1237
a723baf1
MM
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1241
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1246
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1250
4bb8ca28
MM
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1253
0e59b3fb
MM
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1257
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1261
4f8163b1
MM
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1266
a723baf1
MM
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1271
8db1028e
NS
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1277 order.
1278
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1282 processing.
1283
1284 These lists can only be processed once the outermost class being
9bcb9aae 1285 defined is complete. */
a723baf1
MM
1286 tree unparsed_functions_queues;
1287
1288 /* The number of classes whose definitions are currently in
1289 progress. */
1290 unsigned num_classes_being_defined;
1291
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1295} cp_parser;
1296
04c06002 1297/* The type of a function that parses some kind of expression. */
94edc4ab 1298typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1299
1300/* Prototypes. */
1301
1302/* Constructors and destructors. */
1303
1304static cp_parser *cp_parser_new
94edc4ab 1305 (void);
a723baf1 1306
21526606 1307/* Routines to parse various constructs.
a723baf1
MM
1308
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1312 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1318
1319/* Lexical conventions [gram.lex] */
1320
1321static tree cp_parser_identifier
94edc4ab 1322 (cp_parser *);
a723baf1
MM
1323
1324/* Basic concepts [gram.basic] */
1325
1326static bool cp_parser_translation_unit
94edc4ab 1327 (cp_parser *);
a723baf1
MM
1328
1329/* Expressions [gram.expr] */
1330
1331static tree cp_parser_primary_expression
b3445994 1332 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1333static tree cp_parser_id_expression
f3c2dfc6 1334 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1335static tree cp_parser_unqualified_id
f3c2dfc6 1336 (cp_parser *, bool, bool, bool);
a723baf1 1337static tree cp_parser_nested_name_specifier_opt
a668c6ad 1338 (cp_parser *, bool, bool, bool, bool);
a723baf1 1339static tree cp_parser_nested_name_specifier
a723baf1 1340 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1341static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1343static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
7efa3e22 1345static tree cp_parser_parenthesized_expression_list
39703eb9 1346 (cp_parser *, bool, bool *);
a723baf1 1347static void cp_parser_pseudo_destructor_name
94edc4ab 1348 (cp_parser *, tree *, tree *);
a723baf1
MM
1349static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351static enum tree_code cp_parser_unary_operator
94edc4ab 1352 (cp_token *);
a723baf1 1353static tree cp_parser_new_expression
94edc4ab 1354 (cp_parser *);
a723baf1 1355static tree cp_parser_new_placement
94edc4ab 1356 (cp_parser *);
a723baf1 1357static tree cp_parser_new_type_id
94edc4ab 1358 (cp_parser *);
a723baf1 1359static tree cp_parser_new_declarator_opt
94edc4ab 1360 (cp_parser *);
a723baf1 1361static tree cp_parser_direct_new_declarator
94edc4ab 1362 (cp_parser *);
a723baf1 1363static tree cp_parser_new_initializer
94edc4ab 1364 (cp_parser *);
a723baf1 1365static tree cp_parser_delete_expression
94edc4ab 1366 (cp_parser *);
21526606 1367static tree cp_parser_cast_expression
a723baf1
MM
1368 (cp_parser *, bool);
1369static tree cp_parser_pm_expression
94edc4ab 1370 (cp_parser *);
a723baf1 1371static tree cp_parser_multiplicative_expression
94edc4ab 1372 (cp_parser *);
a723baf1 1373static tree cp_parser_additive_expression
94edc4ab 1374 (cp_parser *);
a723baf1 1375static tree cp_parser_shift_expression
94edc4ab 1376 (cp_parser *);
a723baf1 1377static tree cp_parser_relational_expression
94edc4ab 1378 (cp_parser *);
a723baf1 1379static tree cp_parser_equality_expression
94edc4ab 1380 (cp_parser *);
a723baf1 1381static tree cp_parser_and_expression
94edc4ab 1382 (cp_parser *);
a723baf1 1383static tree cp_parser_exclusive_or_expression
94edc4ab 1384 (cp_parser *);
a723baf1 1385static tree cp_parser_inclusive_or_expression
94edc4ab 1386 (cp_parser *);
a723baf1 1387static tree cp_parser_logical_and_expression
94edc4ab 1388 (cp_parser *);
21526606 1389static tree cp_parser_logical_or_expression
94edc4ab 1390 (cp_parser *);
a723baf1 1391static tree cp_parser_question_colon_clause
94edc4ab 1392 (cp_parser *, tree);
a723baf1 1393static tree cp_parser_assignment_expression
94edc4ab 1394 (cp_parser *);
a723baf1 1395static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1396 (cp_parser *);
a723baf1 1397static tree cp_parser_expression
94edc4ab 1398 (cp_parser *);
a723baf1 1399static tree cp_parser_constant_expression
14d22dd6 1400 (cp_parser *, bool, bool *);
a723baf1
MM
1401
1402/* Statements [gram.stmt.stmt] */
1403
1404static void cp_parser_statement
a5bcc582 1405 (cp_parser *, bool);
a723baf1 1406static tree cp_parser_labeled_statement
a5bcc582 1407 (cp_parser *, bool);
a723baf1 1408static tree cp_parser_expression_statement
a5bcc582 1409 (cp_parser *, bool);
a723baf1 1410static tree cp_parser_compound_statement
a5bcc582 1411 (cp_parser *, bool);
a723baf1 1412static void cp_parser_statement_seq_opt
a5bcc582 1413 (cp_parser *, bool);
a723baf1 1414static tree cp_parser_selection_statement
94edc4ab 1415 (cp_parser *);
a723baf1 1416static tree cp_parser_condition
94edc4ab 1417 (cp_parser *);
a723baf1 1418static tree cp_parser_iteration_statement
94edc4ab 1419 (cp_parser *);
a723baf1 1420static void cp_parser_for_init_statement
94edc4ab 1421 (cp_parser *);
a723baf1 1422static tree cp_parser_jump_statement
94edc4ab 1423 (cp_parser *);
a723baf1 1424static void cp_parser_declaration_statement
94edc4ab 1425 (cp_parser *);
a723baf1
MM
1426
1427static tree cp_parser_implicitly_scoped_statement
94edc4ab 1428 (cp_parser *);
a723baf1 1429static void cp_parser_already_scoped_statement
94edc4ab 1430 (cp_parser *);
a723baf1
MM
1431
1432/* Declarations [gram.dcl.dcl] */
1433
1434static void cp_parser_declaration_seq_opt
94edc4ab 1435 (cp_parser *);
a723baf1 1436static void cp_parser_declaration
94edc4ab 1437 (cp_parser *);
a723baf1 1438static void cp_parser_block_declaration
94edc4ab 1439 (cp_parser *, bool);
a723baf1 1440static void cp_parser_simple_declaration
94edc4ab 1441 (cp_parser *, bool);
21526606 1442static tree cp_parser_decl_specifier_seq
560ad596 1443 (cp_parser *, cp_parser_flags, tree *, int *);
a723baf1 1444static tree cp_parser_storage_class_specifier_opt
94edc4ab 1445 (cp_parser *);
a723baf1 1446static tree cp_parser_function_specifier_opt
94edc4ab 1447 (cp_parser *);
a723baf1 1448static tree cp_parser_type_specifier
560ad596 1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
a723baf1 1450static tree cp_parser_simple_type_specifier
4b0d3cbe 1451 (cp_parser *, cp_parser_flags, bool);
a723baf1 1452static tree cp_parser_type_name
94edc4ab 1453 (cp_parser *);
a723baf1 1454static tree cp_parser_elaborated_type_specifier
94edc4ab 1455 (cp_parser *, bool, bool);
a723baf1 1456static tree cp_parser_enum_specifier
94edc4ab 1457 (cp_parser *);
a723baf1 1458static void cp_parser_enumerator_list
94edc4ab 1459 (cp_parser *, tree);
21526606 1460static void cp_parser_enumerator_definition
94edc4ab 1461 (cp_parser *, tree);
a723baf1 1462static tree cp_parser_namespace_name
94edc4ab 1463 (cp_parser *);
a723baf1 1464static void cp_parser_namespace_definition
94edc4ab 1465 (cp_parser *);
a723baf1 1466static void cp_parser_namespace_body
94edc4ab 1467 (cp_parser *);
a723baf1 1468static tree cp_parser_qualified_namespace_specifier
94edc4ab 1469 (cp_parser *);
a723baf1 1470static void cp_parser_namespace_alias_definition
94edc4ab 1471 (cp_parser *);
a723baf1 1472static void cp_parser_using_declaration
94edc4ab 1473 (cp_parser *);
a723baf1 1474static void cp_parser_using_directive
94edc4ab 1475 (cp_parser *);
a723baf1 1476static void cp_parser_asm_definition
94edc4ab 1477 (cp_parser *);
a723baf1 1478static void cp_parser_linkage_specification
94edc4ab 1479 (cp_parser *);
a723baf1
MM
1480
1481/* Declarators [gram.dcl.decl] */
1482
1483static tree cp_parser_init_declarator
560ad596 1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
a723baf1 1485static tree cp_parser_declarator
4bb8ca28 1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
a723baf1 1487static tree cp_parser_direct_declarator
7efa3e22 1488 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1489static enum tree_code cp_parser_ptr_operator
94edc4ab 1490 (cp_parser *, tree *, tree *);
a723baf1 1491static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1492 (cp_parser *);
a723baf1 1493static tree cp_parser_cv_qualifier_opt
94edc4ab 1494 (cp_parser *);
a723baf1 1495static tree cp_parser_declarator_id
94edc4ab 1496 (cp_parser *);
a723baf1 1497static tree cp_parser_type_id
94edc4ab 1498 (cp_parser *);
a723baf1 1499static tree cp_parser_type_specifier_seq
94edc4ab 1500 (cp_parser *);
a723baf1 1501static tree cp_parser_parameter_declaration_clause
94edc4ab 1502 (cp_parser *);
a723baf1 1503static tree cp_parser_parameter_declaration_list
94edc4ab 1504 (cp_parser *);
a723baf1 1505static tree cp_parser_parameter_declaration
4bb8ca28 1506 (cp_parser *, bool, bool *);
a723baf1
MM
1507static void cp_parser_function_body
1508 (cp_parser *);
1509static tree cp_parser_initializer
39703eb9 1510 (cp_parser *, bool *, bool *);
a723baf1 1511static tree cp_parser_initializer_clause
39703eb9 1512 (cp_parser *, bool *);
a723baf1 1513static tree cp_parser_initializer_list
39703eb9 1514 (cp_parser *, bool *);
a723baf1
MM
1515
1516static bool cp_parser_ctor_initializer_opt_and_function_body
1517 (cp_parser *);
1518
1519/* Classes [gram.class] */
1520
1521static tree cp_parser_class_name
a668c6ad 1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
a723baf1 1523static tree cp_parser_class_specifier
94edc4ab 1524 (cp_parser *);
a723baf1 1525static tree cp_parser_class_head
38b305d0 1526 (cp_parser *, bool *, tree *);
a723baf1 1527static enum tag_types cp_parser_class_key
94edc4ab 1528 (cp_parser *);
a723baf1 1529static void cp_parser_member_specification_opt
94edc4ab 1530 (cp_parser *);
a723baf1 1531static void cp_parser_member_declaration
94edc4ab 1532 (cp_parser *);
a723baf1 1533static tree cp_parser_pure_specifier
94edc4ab 1534 (cp_parser *);
a723baf1 1535static tree cp_parser_constant_initializer
94edc4ab 1536 (cp_parser *);
a723baf1
MM
1537
1538/* Derived classes [gram.class.derived] */
1539
1540static tree cp_parser_base_clause
94edc4ab 1541 (cp_parser *);
a723baf1 1542static tree cp_parser_base_specifier
94edc4ab 1543 (cp_parser *);
a723baf1
MM
1544
1545/* Special member functions [gram.special] */
1546
1547static tree cp_parser_conversion_function_id
94edc4ab 1548 (cp_parser *);
a723baf1 1549static tree cp_parser_conversion_type_id
94edc4ab 1550 (cp_parser *);
a723baf1 1551static tree cp_parser_conversion_declarator_opt
94edc4ab 1552 (cp_parser *);
a723baf1 1553static bool cp_parser_ctor_initializer_opt
94edc4ab 1554 (cp_parser *);
a723baf1 1555static void cp_parser_mem_initializer_list
94edc4ab 1556 (cp_parser *);
a723baf1 1557static tree cp_parser_mem_initializer
94edc4ab 1558 (cp_parser *);
a723baf1 1559static tree cp_parser_mem_initializer_id
94edc4ab 1560 (cp_parser *);
a723baf1
MM
1561
1562/* Overloading [gram.over] */
1563
1564static tree cp_parser_operator_function_id
94edc4ab 1565 (cp_parser *);
a723baf1 1566static tree cp_parser_operator
94edc4ab 1567 (cp_parser *);
a723baf1
MM
1568
1569/* Templates [gram.temp] */
1570
1571static void cp_parser_template_declaration
94edc4ab 1572 (cp_parser *, bool);
a723baf1 1573static tree cp_parser_template_parameter_list
94edc4ab 1574 (cp_parser *);
a723baf1 1575static tree cp_parser_template_parameter
94edc4ab 1576 (cp_parser *);
a723baf1 1577static tree cp_parser_type_parameter
94edc4ab 1578 (cp_parser *);
a723baf1 1579static tree cp_parser_template_id
a668c6ad 1580 (cp_parser *, bool, bool, bool);
a723baf1 1581static tree cp_parser_template_name
a668c6ad 1582 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1583static tree cp_parser_template_argument_list
94edc4ab 1584 (cp_parser *);
a723baf1 1585static tree cp_parser_template_argument
94edc4ab 1586 (cp_parser *);
a723baf1 1587static void cp_parser_explicit_instantiation
94edc4ab 1588 (cp_parser *);
a723baf1 1589static void cp_parser_explicit_specialization
94edc4ab 1590 (cp_parser *);
a723baf1
MM
1591
1592/* Exception handling [gram.exception] */
1593
21526606 1594static tree cp_parser_try_block
94edc4ab 1595 (cp_parser *);
a723baf1 1596static bool cp_parser_function_try_block
94edc4ab 1597 (cp_parser *);
a723baf1 1598static void cp_parser_handler_seq
94edc4ab 1599 (cp_parser *);
a723baf1 1600static void cp_parser_handler
94edc4ab 1601 (cp_parser *);
a723baf1 1602static tree cp_parser_exception_declaration
94edc4ab 1603 (cp_parser *);
a723baf1 1604static tree cp_parser_throw_expression
94edc4ab 1605 (cp_parser *);
a723baf1 1606static tree cp_parser_exception_specification_opt
94edc4ab 1607 (cp_parser *);
a723baf1 1608static tree cp_parser_type_id_list
94edc4ab 1609 (cp_parser *);
a723baf1
MM
1610
1611/* GNU Extensions */
1612
1613static tree cp_parser_asm_specification_opt
94edc4ab 1614 (cp_parser *);
a723baf1 1615static tree cp_parser_asm_operand_list
94edc4ab 1616 (cp_parser *);
a723baf1 1617static tree cp_parser_asm_clobber_list
94edc4ab 1618 (cp_parser *);
a723baf1 1619static tree cp_parser_attributes_opt
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_attribute_list
94edc4ab 1622 (cp_parser *);
a723baf1 1623static bool cp_parser_extension_opt
94edc4ab 1624 (cp_parser *, int *);
a723baf1 1625static void cp_parser_label_declaration
94edc4ab 1626 (cp_parser *);
a723baf1
MM
1627
1628/* Utility Routines */
1629
1630static tree cp_parser_lookup_name
b0bc6e8e 1631 (cp_parser *, tree, bool, bool, bool, bool);
a723baf1 1632static tree cp_parser_lookup_name_simple
94edc4ab 1633 (cp_parser *, tree);
a723baf1
MM
1634static tree cp_parser_maybe_treat_template_as_class
1635 (tree, bool);
1636static bool cp_parser_check_declarator_template_parameters
94edc4ab 1637 (cp_parser *, tree);
a723baf1 1638static bool cp_parser_check_template_parameters
94edc4ab 1639 (cp_parser *, unsigned);
d6b4ea85
MM
1640static tree cp_parser_simple_cast_expression
1641 (cp_parser *);
a723baf1 1642static tree cp_parser_binary_expression
94edc4ab 1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1644static tree cp_parser_global_scope_opt
94edc4ab 1645 (cp_parser *, bool);
a723baf1
MM
1646static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1649 (cp_parser *, tree, tree, tree);
a723baf1 1650static tree cp_parser_function_definition_after_declarator
94edc4ab 1651 (cp_parser *, bool);
a723baf1 1652static void cp_parser_template_declaration_after_export
94edc4ab 1653 (cp_parser *, bool);
a723baf1 1654static tree cp_parser_single_declaration
94edc4ab 1655 (cp_parser *, bool, bool *);
a723baf1 1656static tree cp_parser_functional_cast
94edc4ab 1657 (cp_parser *, tree);
4bb8ca28
MM
1658static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
ec75414f
MM
1660static tree cp_parser_enclosed_template_argument_list
1661 (cp_parser *);
8db1028e
NS
1662static void cp_parser_save_default_args
1663 (cp_parser *, tree);
a723baf1 1664static void cp_parser_late_parsing_for_member
94edc4ab 1665 (cp_parser *, tree);
a723baf1 1666static void cp_parser_late_parsing_default_args
8218bd34 1667 (cp_parser *, tree);
a723baf1 1668static tree cp_parser_sizeof_operand
94edc4ab 1669 (cp_parser *, enum rid);
a723baf1 1670static bool cp_parser_declares_only_class_p
94edc4ab 1671 (cp_parser *);
a723baf1 1672static bool cp_parser_friend_p
94edc4ab 1673 (tree);
a723baf1 1674static cp_token *cp_parser_require
94edc4ab 1675 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1676static cp_token *cp_parser_require_keyword
94edc4ab 1677 (cp_parser *, enum rid, const char *);
21526606 1678static bool cp_parser_token_starts_function_definition_p
94edc4ab 1679 (cp_token *);
a723baf1
MM
1680static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
d17811fd
MM
1682static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
f4abade9
GB
1684static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
a723baf1 1686static enum tag_types cp_parser_token_is_class_key
94edc4ab 1687 (cp_token *);
a723baf1
MM
1688static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
37d407a1
KL
1690static void cp_parser_check_access_in_redeclaration
1691 (tree type);
a723baf1
MM
1692static bool cp_parser_optional_template_keyword
1693 (cp_parser *);
21526606 1694static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1695 (cp_parser *);
a723baf1
MM
1696static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
21526606 1698static void cp_parser_parse_tentatively
94edc4ab 1699 (cp_parser *);
a723baf1 1700static void cp_parser_commit_to_tentative_parse
94edc4ab 1701 (cp_parser *);
a723baf1 1702static void cp_parser_abort_tentative_parse
94edc4ab 1703 (cp_parser *);
a723baf1 1704static bool cp_parser_parse_definitely
94edc4ab 1705 (cp_parser *);
f7b5ecd9 1706static inline bool cp_parser_parsing_tentatively
94edc4ab 1707 (cp_parser *);
a723baf1 1708static bool cp_parser_committed_to_tentative_parse
94edc4ab 1709 (cp_parser *);
a723baf1 1710static void cp_parser_error
94edc4ab 1711 (cp_parser *, const char *);
4bb8ca28
MM
1712static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
e5976695 1714static bool cp_parser_simulate_error
94edc4ab 1715 (cp_parser *);
a723baf1 1716static void cp_parser_check_type_definition
94edc4ab 1717 (cp_parser *);
560ad596
MM
1718static void cp_parser_check_for_definition_in_return_type
1719 (tree, int);
ee43dab5
MM
1720static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
625cbf93
MM
1722static bool cp_parser_non_integral_constant_expression
1723 (cp_parser *, const char *);
2097b5f2
GB
1724static void cp_parser_diagnose_invalid_type_name
1725 (cp_parser *, tree, tree);
1726static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1727 (cp_parser *);
7efa3e22 1728static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1729 (cp_parser *, bool, bool, bool);
a723baf1 1730static void cp_parser_skip_to_end_of_statement
94edc4ab 1731 (cp_parser *);
e0860732
MM
1732static void cp_parser_consume_semicolon_at_end_of_statement
1733 (cp_parser *);
a723baf1 1734static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1735 (cp_parser *);
a723baf1
MM
1736static void cp_parser_skip_to_closing_brace
1737 (cp_parser *);
1738static void cp_parser_skip_until_found
94edc4ab 1739 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1740static bool cp_parser_error_occurred
94edc4ab 1741 (cp_parser *);
a723baf1 1742static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1743 (cp_parser *);
a723baf1 1744static bool cp_parser_is_string_literal
94edc4ab 1745 (cp_token *);
21526606 1746static bool cp_parser_is_keyword
94edc4ab 1747 (cp_token *, enum rid);
2097b5f2
GB
1748static tree cp_parser_make_typename_type
1749 (cp_parser *, tree, tree);
a723baf1 1750
4de8668e 1751/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1752
1753static inline bool
94edc4ab 1754cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1755{
1756 return parser->context->next != NULL;
1757}
1758
4de8668e 1759/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1760
1761static bool
94edc4ab 1762cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1763{
1764 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1765}
1766
4de8668e 1767/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1768
1769static bool
94edc4ab 1770cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1771{
1772 return token->keyword == keyword;
1773}
1774
a723baf1
MM
1775/* Issue the indicated error MESSAGE. */
1776
1777static void
94edc4ab 1778cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1779{
a723baf1 1780 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1781 if (!cp_parser_simulate_error (parser))
4bb8ca28
MM
1782 {
1783 cp_token *token;
1784 token = cp_lexer_peek_token (parser->lexer);
21526606 1785 c_parse_error (message,
5c832178
MM
1786 /* Because c_parser_error does not understand
1787 CPP_KEYWORD, keywords are treated like
1788 identifiers. */
21526606 1789 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1790 token->value);
4bb8ca28
MM
1791 }
1792}
1793
1794/* Issue an error about name-lookup failing. NAME is the
1795 IDENTIFIER_NODE DECL is the result of
1796 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1797 the thing that we hoped to find. */
1798
1799static void
1800cp_parser_name_lookup_error (cp_parser* parser,
1801 tree name,
1802 tree decl,
1803 const char* desired)
1804{
1805 /* If name lookup completely failed, tell the user that NAME was not
1806 declared. */
1807 if (decl == error_mark_node)
1808 {
1809 if (parser->scope && parser->scope != global_namespace)
21526606 1810 error ("`%D::%D' has not been declared",
4bb8ca28
MM
1811 parser->scope, name);
1812 else if (parser->scope == global_namespace)
1813 error ("`::%D' has not been declared", name);
1814 else
1815 error ("`%D' has not been declared", name);
1816 }
1817 else if (parser->scope && parser->scope != global_namespace)
1818 error ("`%D::%D' %s", parser->scope, name, desired);
1819 else if (parser->scope == global_namespace)
1820 error ("`::%D' %s", name, desired);
1821 else
1822 error ("`%D' %s", name, desired);
a723baf1
MM
1823}
1824
1825/* If we are parsing tentatively, remember that an error has occurred
e5976695 1826 during this tentative parse. Returns true if the error was
77077b39 1827 simulated; false if a message should be issued by the caller. */
a723baf1 1828
e5976695 1829static bool
94edc4ab 1830cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1831{
1832 if (cp_parser_parsing_tentatively (parser)
1833 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1834 {
1835 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1836 return true;
1837 }
1838 return false;
a723baf1
MM
1839}
1840
1841/* This function is called when a type is defined. If type
1842 definitions are forbidden at this point, an error message is
1843 issued. */
1844
1845static void
94edc4ab 1846cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1847{
1848 /* If types are forbidden here, issue a message. */
1849 if (parser->type_definition_forbidden_message)
1850 /* Use `%s' to print the string in case there are any escape
1851 characters in the message. */
1852 error ("%s", parser->type_definition_forbidden_message);
1853}
1854
560ad596
MM
1855/* This function is called when a declaration is parsed. If
1856 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1857 indicates that a type was defined in the decl-specifiers for DECL,
1858 then an error is issued. */
1859
1860static void
21526606 1861cp_parser_check_for_definition_in_return_type (tree declarator,
560ad596
MM
1862 int declares_class_or_enum)
1863{
1864 /* [dcl.fct] forbids type definitions in return types.
1865 Unfortunately, it's not easy to know whether or not we are
1866 processing a return type until after the fact. */
1867 while (declarator
1868 && (TREE_CODE (declarator) == INDIRECT_REF
1869 || TREE_CODE (declarator) == ADDR_EXPR))
1870 declarator = TREE_OPERAND (declarator, 0);
1871 if (declarator
21526606 1872 && TREE_CODE (declarator) == CALL_EXPR
560ad596
MM
1873 && declares_class_or_enum & 2)
1874 error ("new types may not be defined in a return type");
1875}
1876
ee43dab5
MM
1877/* A type-specifier (TYPE) has been parsed which cannot be followed by
1878 "<" in any valid C++ program. If the next token is indeed "<",
1879 issue a message warning the user about what appears to be an
1880 invalid attempt to form a template-id. */
1881
1882static void
21526606 1883cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
1884 tree type)
1885{
1886 ptrdiff_t start;
1887 cp_token *token;
1888
1889 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1890 {
1891 if (TYPE_P (type))
1892 error ("`%T' is not a template", type);
1893 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1894 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1895 else
1896 error ("invalid template-id");
1897 /* Remember the location of the invalid "<". */
1898 if (cp_parser_parsing_tentatively (parser)
1899 && !cp_parser_committed_to_tentative_parse (parser))
1900 {
1901 token = cp_lexer_peek_token (parser->lexer);
1902 token = cp_lexer_prev_token (parser->lexer, token);
1903 start = cp_lexer_token_difference (parser->lexer,
1904 parser->lexer->first_token,
1905 token);
1906 }
1907 else
1908 start = -1;
1909 /* Consume the "<". */
1910 cp_lexer_consume_token (parser->lexer);
1911 /* Parse the template arguments. */
1912 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1913 /* Permanently remove the invalid template arguments so that
ee43dab5
MM
1914 this error message is not issued again. */
1915 if (start >= 0)
1916 {
1917 token = cp_lexer_advance_token (parser->lexer,
1918 parser->lexer->first_token,
1919 start);
1920 cp_lexer_purge_tokens_after (parser->lexer, token);
1921 }
1922 }
1923}
1924
625cbf93
MM
1925/* If parsing an integral constant-expression, issue an error message
1926 about the fact that THING appeared and return true. Otherwise,
1927 return false, marking the current expression as non-constant. */
14d22dd6 1928
625cbf93
MM
1929static bool
1930cp_parser_non_integral_constant_expression (cp_parser *parser,
1931 const char *thing)
14d22dd6 1932{
625cbf93
MM
1933 if (parser->integral_constant_expression_p)
1934 {
1935 if (!parser->allow_non_integral_constant_expression_p)
1936 {
1937 error ("%s cannot appear in a constant-expression", thing);
1938 return true;
1939 }
1940 parser->non_integral_constant_expression_p = true;
1941 }
1942 return false;
14d22dd6
MM
1943}
1944
2097b5f2 1945/* Emit a diagnostic for an invalid type name. Consider also if it is
21526606 1946 qualified or not and the result of a lookup, to provide a better
2097b5f2 1947 message. */
8fbc5ae7 1948
2097b5f2
GB
1949static void
1950cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
1951{
1952 tree decl, old_scope;
2097b5f2
GB
1953 /* Try to lookup the identifier. */
1954 old_scope = parser->scope;
1955 parser->scope = scope;
1956 decl = cp_parser_lookup_name_simple (parser, id);
1957 parser->scope = old_scope;
1958 /* If the lookup found a template-name, it means that the user forgot
1959 to specify an argument list. Emit an useful error message. */
1960 if (TREE_CODE (decl) == TEMPLATE_DECL)
6c0cc713
GB
1961 error ("invalid use of template-name `%E' without an argument list",
1962 decl);
2097b5f2 1963 else if (!parser->scope)
8fbc5ae7 1964 {
8fbc5ae7 1965 /* Issue an error message. */
2097b5f2 1966 error ("`%E' does not name a type", id);
8fbc5ae7
MM
1967 /* If we're in a template class, it's possible that the user was
1968 referring to a type from a base class. For example:
1969
1970 template <typename T> struct A { typedef T X; };
1971 template <typename T> struct B : public A<T> { X x; };
1972
1973 The user should have said "typename A<T>::X". */
1974 if (processing_template_decl && current_class_type)
1975 {
1976 tree b;
1977
1978 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1979 b;
1980 b = TREE_CHAIN (b))
1981 {
1982 tree base_type = BINFO_TYPE (b);
21526606 1983 if (CLASS_TYPE_P (base_type)
1fb3244a 1984 && dependent_type_p (base_type))
8fbc5ae7
MM
1985 {
1986 tree field;
1987 /* Go from a particular instantiation of the
1988 template (which will have an empty TYPE_FIELDs),
1989 to the main version. */
353b4fc0 1990 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1991 for (field = TYPE_FIELDS (base_type);
1992 field;
1993 field = TREE_CHAIN (field))
1994 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 1995 && DECL_NAME (field) == id)
8fbc5ae7 1996 {
2097b5f2
GB
1997 inform ("(perhaps `typename %T::%E' was intended)",
1998 BINFO_TYPE (b), id);
8fbc5ae7
MM
1999 break;
2000 }
2001 if (field)
2002 break;
2003 }
2004 }
2005 }
8fbc5ae7 2006 }
2097b5f2
GB
2007 /* Here we diagnose qualified-ids where the scope is actually correct,
2008 but the identifier does not resolve to a valid type name. */
21526606 2009 else
2097b5f2
GB
2010 {
2011 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21526606 2012 error ("`%E' in namespace `%E' does not name a type",
2097b5f2
GB
2013 id, parser->scope);
2014 else if (TYPE_P (parser->scope))
21526606 2015 error ("`%E' in class `%T' does not name a type",
2097b5f2
GB
2016 id, parser->scope);
2017 else
2018 abort();
2019 }
2020}
8fbc5ae7 2021
2097b5f2
GB
2022/* Check for a common situation where a type-name should be present,
2023 but is not, and issue a sensible error message. Returns true if an
2024 invalid type-name was detected.
21526606 2025
2097b5f2 2026 The situation handled by this function are variable declarations of the
21526606
EC
2027 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2028 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2029 does not. We try to emit the best possible error message depending on
2030 how exactly the id-expression looks like.
2031*/
2032
2033static bool
2034cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2035{
2036 tree id;
2037
2038 cp_parser_parse_tentatively (parser);
21526606 2039 id = cp_parser_id_expression (parser,
2097b5f2
GB
2040 /*template_keyword_p=*/false,
2041 /*check_dependency_p=*/true,
2042 /*template_p=*/NULL,
2043 /*declarator_p=*/true);
2044 /* After the id-expression, there should be a plain identifier,
2045 otherwise this is not a simple variable declaration. Also, if
2046 the scope is dependent, we cannot do much. */
2047 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2048 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2049 && dependent_type_p (parser->scope)))
2050 {
2051 cp_parser_abort_tentative_parse (parser);
2052 return false;
2053 }
2054 if (!cp_parser_parse_definitely (parser))
2055 return false;
2056
2057 /* If we got here, this cannot be a valid variable declaration, thus
2058 the cp_parser_id_expression must have resolved to a plain identifier
2059 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2060 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2061 /* Emit a diagnostic for the invalid type. */
2062 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2063 /* Skip to the end of the declaration; there's no point in
2064 trying to process it. */
2065 cp_parser_skip_to_end_of_block_or_statement (parser);
2066 return true;
8fbc5ae7
MM
2067}
2068
21526606 2069/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2070 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2071 are doing error recovery. Returns -1 if OR_COMMA is true and we
2072 found an unnested comma. */
a723baf1 2073
7efa3e22
NS
2074static int
2075cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2076 bool recovering,
a668c6ad
MM
2077 bool or_comma,
2078 bool consume_paren)
a723baf1 2079{
7efa3e22
NS
2080 unsigned paren_depth = 0;
2081 unsigned brace_depth = 0;
a723baf1 2082
7efa3e22
NS
2083 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2084 && !cp_parser_committed_to_tentative_parse (parser))
2085 return 0;
21526606 2086
a723baf1
MM
2087 while (true)
2088 {
2089 cp_token *token;
21526606 2090
a723baf1
MM
2091 /* If we've run out of tokens, then there is no closing `)'. */
2092 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 2093 return 0;
a723baf1 2094
a668c6ad 2095 token = cp_lexer_peek_token (parser->lexer);
21526606 2096
f4f206f4 2097 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad
MM
2098 if (token->type == CPP_SEMICOLON && !brace_depth)
2099 return 0;
2100 if (token->type == CPP_OPEN_BRACE)
2101 ++brace_depth;
2102 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2103 {
a668c6ad 2104 if (!brace_depth--)
7efa3e22 2105 return 0;
7efa3e22 2106 }
a668c6ad
MM
2107 if (recovering && or_comma && token->type == CPP_COMMA
2108 && !brace_depth && !paren_depth)
2109 return -1;
21526606 2110
7efa3e22
NS
2111 if (!brace_depth)
2112 {
2113 /* If it is an `(', we have entered another level of nesting. */
2114 if (token->type == CPP_OPEN_PAREN)
2115 ++paren_depth;
2116 /* If it is a `)', then we might be done. */
2117 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2118 {
2119 if (consume_paren)
2120 cp_lexer_consume_token (parser->lexer);
2121 return 1;
2122 }
7efa3e22 2123 }
21526606 2124
a668c6ad
MM
2125 /* Consume the token. */
2126 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2127 }
2128}
2129
2130/* Consume tokens until we reach the end of the current statement.
2131 Normally, that will be just before consuming a `;'. However, if a
2132 non-nested `}' comes first, then we stop before consuming that. */
2133
2134static void
94edc4ab 2135cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2136{
2137 unsigned nesting_depth = 0;
2138
2139 while (true)
2140 {
2141 cp_token *token;
2142
2143 /* Peek at the next token. */
2144 token = cp_lexer_peek_token (parser->lexer);
2145 /* If we've run out of tokens, stop. */
2146 if (token->type == CPP_EOF)
2147 break;
2148 /* If the next token is a `;', we have reached the end of the
2149 statement. */
2150 if (token->type == CPP_SEMICOLON && !nesting_depth)
2151 break;
2152 /* If the next token is a non-nested `}', then we have reached
2153 the end of the current block. */
2154 if (token->type == CPP_CLOSE_BRACE)
2155 {
2156 /* If this is a non-nested `}', stop before consuming it.
2157 That way, when confronted with something like:
2158
21526606 2159 { 3 + }
a723baf1
MM
2160
2161 we stop before consuming the closing `}', even though we
2162 have not yet reached a `;'. */
2163 if (nesting_depth == 0)
2164 break;
2165 /* If it is the closing `}' for a block that we have
2166 scanned, stop -- but only after consuming the token.
2167 That way given:
2168
2169 void f g () { ... }
2170 typedef int I;
2171
2172 we will stop after the body of the erroneously declared
2173 function, but before consuming the following `typedef'
2174 declaration. */
2175 if (--nesting_depth == 0)
2176 {
2177 cp_lexer_consume_token (parser->lexer);
2178 break;
2179 }
2180 }
2181 /* If it the next token is a `{', then we are entering a new
2182 block. Consume the entire block. */
2183 else if (token->type == CPP_OPEN_BRACE)
2184 ++nesting_depth;
2185 /* Consume the token. */
2186 cp_lexer_consume_token (parser->lexer);
2187 }
2188}
2189
e0860732
MM
2190/* This function is called at the end of a statement or declaration.
2191 If the next token is a semicolon, it is consumed; otherwise, error
2192 recovery is attempted. */
2193
2194static void
2195cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2196{
2197 /* Look for the trailing `;'. */
2198 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2199 {
2200 /* If there is additional (erroneous) input, skip to the end of
2201 the statement. */
2202 cp_parser_skip_to_end_of_statement (parser);
2203 /* If the next token is now a `;', consume it. */
2204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2205 cp_lexer_consume_token (parser->lexer);
2206 }
2207}
2208
a723baf1
MM
2209/* Skip tokens until we have consumed an entire block, or until we
2210 have consumed a non-nested `;'. */
2211
2212static void
94edc4ab 2213cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2214{
2215 unsigned nesting_depth = 0;
2216
2217 while (true)
2218 {
2219 cp_token *token;
2220
2221 /* Peek at the next token. */
2222 token = cp_lexer_peek_token (parser->lexer);
2223 /* If we've run out of tokens, stop. */
2224 if (token->type == CPP_EOF)
2225 break;
2226 /* If the next token is a `;', we have reached the end of the
2227 statement. */
2228 if (token->type == CPP_SEMICOLON && !nesting_depth)
2229 {
2230 /* Consume the `;'. */
2231 cp_lexer_consume_token (parser->lexer);
2232 break;
2233 }
2234 /* Consume the token. */
2235 token = cp_lexer_consume_token (parser->lexer);
2236 /* If the next token is a non-nested `}', then we have reached
2237 the end of the current block. */
21526606 2238 if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
2239 && (nesting_depth == 0 || --nesting_depth == 0))
2240 break;
2241 /* If it the next token is a `{', then we are entering a new
2242 block. Consume the entire block. */
2243 if (token->type == CPP_OPEN_BRACE)
2244 ++nesting_depth;
2245 }
2246}
2247
2248/* Skip tokens until a non-nested closing curly brace is the next
2249 token. */
2250
2251static void
2252cp_parser_skip_to_closing_brace (cp_parser *parser)
2253{
2254 unsigned nesting_depth = 0;
2255
2256 while (true)
2257 {
2258 cp_token *token;
2259
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2264 break;
2265 /* If the next token is a non-nested `}', then we have reached
2266 the end of the current block. */
2267 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2268 break;
2269 /* If it the next token is a `{', then we are entering a new
2270 block. Consume the entire block. */
2271 else if (token->type == CPP_OPEN_BRACE)
2272 ++nesting_depth;
2273 /* Consume the token. */
2274 cp_lexer_consume_token (parser->lexer);
2275 }
2276}
2277
2097b5f2
GB
2278/* This is a simple wrapper around make_typename_type. When the id is
2279 an unresolved identifier node, we can provide a superior diagnostic
2280 using cp_parser_diagnose_invalid_type_name. */
2281
2282static tree
2283cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2284{
2285 tree result;
2286 if (TREE_CODE (id) == IDENTIFIER_NODE)
2287 {
2288 result = make_typename_type (scope, id, /*complain=*/0);
2289 if (result == error_mark_node)
2290 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2291 return result;
2292 }
2293 return make_typename_type (scope, id, tf_error);
2097b5f2
GB
2294}
2295
2296
a723baf1
MM
2297/* Create a new C++ parser. */
2298
2299static cp_parser *
94edc4ab 2300cp_parser_new (void)
a723baf1
MM
2301{
2302 cp_parser *parser;
17211ab5
GK
2303 cp_lexer *lexer;
2304
2305 /* cp_lexer_new_main is called before calling ggc_alloc because
2306 cp_lexer_new_main might load a PCH file. */
2307 lexer = cp_lexer_new_main ();
a723baf1 2308
c68b0a84 2309 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2310 parser->lexer = lexer;
a723baf1
MM
2311 parser->context = cp_parser_context_new (NULL);
2312
2313 /* For now, we always accept GNU extensions. */
2314 parser->allow_gnu_extensions_p = 1;
2315
2316 /* The `>' token is a greater-than operator, not the end of a
2317 template-id. */
2318 parser->greater_than_is_operator_p = true;
2319
2320 parser->default_arg_ok_p = true;
21526606 2321
a723baf1 2322 /* We are not parsing a constant-expression. */
67c03833
JM
2323 parser->integral_constant_expression_p = false;
2324 parser->allow_non_integral_constant_expression_p = false;
2325 parser->non_integral_constant_expression_p = false;
a723baf1 2326
263ee052
MM
2327 /* We are not parsing offsetof. */
2328 parser->in_offsetof_p = false;
2329
a723baf1
MM
2330 /* Local variable names are not forbidden. */
2331 parser->local_variables_forbidden_p = false;
2332
34cd5ae7 2333 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2334 parser->in_unbraced_linkage_specification_p = false;
2335
2336 /* We are not processing a declarator. */
2337 parser->in_declarator_p = false;
2338
4bb8ca28
MM
2339 /* We are not processing a template-argument-list. */
2340 parser->in_template_argument_list_p = false;
2341
0e59b3fb
MM
2342 /* We are not in an iteration statement. */
2343 parser->in_iteration_statement_p = false;
2344
2345 /* We are not in a switch statement. */
2346 parser->in_switch_statement_p = false;
2347
4f8163b1
MM
2348 /* We are not parsing a type-id inside an expression. */
2349 parser->in_type_id_in_expr_p = false;
2350
a723baf1
MM
2351 /* The unparsed function queue is empty. */
2352 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2353
2354 /* There are no classes being defined. */
2355 parser->num_classes_being_defined = 0;
2356
2357 /* No template parameters apply. */
2358 parser->num_template_parameter_lists = 0;
2359
2360 return parser;
2361}
2362
2363/* Lexical conventions [gram.lex] */
2364
2365/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2366 identifier. */
2367
21526606 2368static tree
94edc4ab 2369cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2370{
2371 cp_token *token;
2372
2373 /* Look for the identifier. */
2374 token = cp_parser_require (parser, CPP_NAME, "identifier");
2375 /* Return the value. */
2376 return token ? token->value : error_mark_node;
2377}
2378
2379/* Basic concepts [gram.basic] */
2380
2381/* Parse a translation-unit.
2382
2383 translation-unit:
21526606 2384 declaration-seq [opt]
a723baf1
MM
2385
2386 Returns TRUE if all went well. */
2387
2388static bool
94edc4ab 2389cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2390{
2391 while (true)
2392 {
2393 cp_parser_declaration_seq_opt (parser);
2394
2395 /* If there are no tokens left then all went well. */
2396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2397 break;
21526606 2398
a723baf1
MM
2399 /* Otherwise, issue an error message. */
2400 cp_parser_error (parser, "expected declaration");
2401 return false;
2402 }
2403
2404 /* Consume the EOF token. */
2405 cp_parser_require (parser, CPP_EOF, "end-of-file");
21526606 2406
a723baf1
MM
2407 /* Finish up. */
2408 finish_translation_unit ();
2409
2410 /* All went well. */
2411 return true;
2412}
2413
2414/* Expressions [gram.expr] */
2415
2416/* Parse a primary-expression.
2417
2418 primary-expression:
2419 literal
2420 this
2421 ( expression )
2422 id-expression
2423
2424 GNU Extensions:
2425
2426 primary-expression:
2427 ( compound-statement )
2428 __builtin_va_arg ( assignment-expression , type-id )
2429
2430 literal:
2431 __null
2432
21526606 2433 Returns a representation of the expression.
a723baf1 2434
21526606 2435 *IDK indicates what kind of id-expression (if any) was present.
a723baf1
MM
2436
2437 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2438 used as the operand of a pointer-to-member. In that case,
2439 *QUALIFYING_CLASS gives the class that is used as the qualifying
2440 class in the pointer-to-member. */
2441
2442static tree
21526606 2443cp_parser_primary_expression (cp_parser *parser,
b3445994 2444 cp_id_kind *idk,
a723baf1
MM
2445 tree *qualifying_class)
2446{
2447 cp_token *token;
2448
2449 /* Assume the primary expression is not an id-expression. */
b3445994 2450 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2451 /* And that it cannot be used as pointer-to-member. */
2452 *qualifying_class = NULL_TREE;
2453
2454 /* Peek at the next token. */
2455 token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2457 {
2458 /* literal:
2459 integer-literal
2460 character-literal
2461 floating-literal
2462 string-literal
2463 boolean-literal */
2464 case CPP_CHAR:
2465 case CPP_WCHAR:
2466 case CPP_STRING:
2467 case CPP_WSTRING:
2468 case CPP_NUMBER:
2469 token = cp_lexer_consume_token (parser->lexer);
2470 return token->value;
2471
2472 case CPP_OPEN_PAREN:
2473 {
2474 tree expr;
2475 bool saved_greater_than_is_operator_p;
2476
2477 /* Consume the `('. */
2478 cp_lexer_consume_token (parser->lexer);
2479 /* Within a parenthesized expression, a `>' token is always
2480 the greater-than operator. */
21526606 2481 saved_greater_than_is_operator_p
a723baf1
MM
2482 = parser->greater_than_is_operator_p;
2483 parser->greater_than_is_operator_p = true;
2484 /* If we see `( { ' then we are looking at the beginning of
2485 a GNU statement-expression. */
2486 if (cp_parser_allow_gnu_extensions_p (parser)
2487 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2488 {
2489 /* Statement-expressions are not allowed by the standard. */
2490 if (pedantic)
21526606
EC
2491 pedwarn ("ISO C++ forbids braced-groups within expressions");
2492
a723baf1
MM
2493 /* And they're not allowed outside of a function-body; you
2494 cannot, for example, write:
21526606 2495
a723baf1 2496 int i = ({ int j = 3; j + 1; });
21526606 2497
a723baf1
MM
2498 at class or namespace scope. */
2499 if (!at_function_scope_p ())
2500 error ("statement-expressions are allowed only inside functions");
2501 /* Start the statement-expression. */
2502 expr = begin_stmt_expr ();
2503 /* Parse the compound-statement. */
a5bcc582 2504 cp_parser_compound_statement (parser, true);
a723baf1 2505 /* Finish up. */
303b7406 2506 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2507 }
2508 else
2509 {
2510 /* Parse the parenthesized expression. */
2511 expr = cp_parser_expression (parser);
2512 /* Let the front end know that this expression was
2513 enclosed in parentheses. This matters in case, for
2514 example, the expression is of the form `A::B', since
2515 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2516 not. */
2517 finish_parenthesized_expr (expr);
2518 }
2519 /* The `>' token might be the end of a template-id or
2520 template-parameter-list now. */
21526606 2521 parser->greater_than_is_operator_p
a723baf1
MM
2522 = saved_greater_than_is_operator_p;
2523 /* Consume the `)'. */
2524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2525 cp_parser_skip_to_end_of_statement (parser);
2526
2527 return expr;
2528 }
2529
2530 case CPP_KEYWORD:
2531 switch (token->keyword)
2532 {
2533 /* These two are the boolean literals. */
2534 case RID_TRUE:
2535 cp_lexer_consume_token (parser->lexer);
2536 return boolean_true_node;
2537 case RID_FALSE:
2538 cp_lexer_consume_token (parser->lexer);
2539 return boolean_false_node;
21526606 2540
a723baf1
MM
2541 /* The `__null' literal. */
2542 case RID_NULL:
2543 cp_lexer_consume_token (parser->lexer);
2544 return null_node;
2545
2546 /* Recognize the `this' keyword. */
2547 case RID_THIS:
2548 cp_lexer_consume_token (parser->lexer);
2549 if (parser->local_variables_forbidden_p)
2550 {
2551 error ("`this' may not be used in this context");
2552 return error_mark_node;
2553 }
14d22dd6 2554 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2555 if (cp_parser_non_integral_constant_expression (parser,
2556 "`this'"))
2557 return error_mark_node;
a723baf1
MM
2558 return finish_this_expr ();
2559
2560 /* The `operator' keyword can be the beginning of an
2561 id-expression. */
2562 case RID_OPERATOR:
2563 goto id_expression;
2564
2565 case RID_FUNCTION_NAME:
2566 case RID_PRETTY_FUNCTION_NAME:
2567 case RID_C99_FUNCTION_NAME:
2568 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2569 __func__ are the names of variables -- but they are
2570 treated specially. Therefore, they are handled here,
2571 rather than relying on the generic id-expression logic
21526606 2572 below. Grammatically, these names are id-expressions.
a723baf1
MM
2573
2574 Consume the token. */
2575 token = cp_lexer_consume_token (parser->lexer);
2576 /* Look up the name. */
2577 return finish_fname (token->value);
2578
2579 case RID_VA_ARG:
2580 {
2581 tree expression;
2582 tree type;
2583
2584 /* The `__builtin_va_arg' construct is used to handle
2585 `va_arg'. Consume the `__builtin_va_arg' token. */
2586 cp_lexer_consume_token (parser->lexer);
2587 /* Look for the opening `('. */
2588 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2589 /* Now, parse the assignment-expression. */
2590 expression = cp_parser_assignment_expression (parser);
2591 /* Look for the `,'. */
2592 cp_parser_require (parser, CPP_COMMA, "`,'");
2593 /* Parse the type-id. */
2594 type = cp_parser_type_id (parser);
2595 /* Look for the closing `)'. */
2596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2597 /* Using `va_arg' in a constant-expression is not
2598 allowed. */
625cbf93
MM
2599 if (cp_parser_non_integral_constant_expression (parser,
2600 "`va_arg'"))
2601 return error_mark_node;
a723baf1
MM
2602 return build_x_va_arg (expression, type);
2603 }
2604
263ee052
MM
2605 case RID_OFFSETOF:
2606 {
2607 tree expression;
2608 bool saved_in_offsetof_p;
2609
2610 /* Consume the "__offsetof__" token. */
2611 cp_lexer_consume_token (parser->lexer);
2612 /* Consume the opening `('. */
2613 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2614 /* Parse the parenthesized (almost) constant-expression. */
2615 saved_in_offsetof_p = parser->in_offsetof_p;
2616 parser->in_offsetof_p = true;
21526606 2617 expression
263ee052
MM
2618 = cp_parser_constant_expression (parser,
2619 /*allow_non_constant_p=*/false,
2620 /*non_constant_p=*/NULL);
2621 parser->in_offsetof_p = saved_in_offsetof_p;
2622 /* Consume the closing ')'. */
2623 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2624
2625 return expression;
2626 }
2627
a723baf1
MM
2628 default:
2629 cp_parser_error (parser, "expected primary-expression");
2630 return error_mark_node;
2631 }
a723baf1
MM
2632
2633 /* An id-expression can start with either an identifier, a
2634 `::' as the beginning of a qualified-id, or the "operator"
2635 keyword. */
2636 case CPP_NAME:
2637 case CPP_SCOPE:
2638 case CPP_TEMPLATE_ID:
2639 case CPP_NESTED_NAME_SPECIFIER:
2640 {
2641 tree id_expression;
2642 tree decl;
b3445994 2643 const char *error_msg;
a723baf1
MM
2644
2645 id_expression:
2646 /* Parse the id-expression. */
21526606
EC
2647 id_expression
2648 = cp_parser_id_expression (parser,
a723baf1
MM
2649 /*template_keyword_p=*/false,
2650 /*check_dependency_p=*/true,
f3c2dfc6
MM
2651 /*template_p=*/NULL,
2652 /*declarator_p=*/false);
a723baf1
MM
2653 if (id_expression == error_mark_node)
2654 return error_mark_node;
2655 /* If we have a template-id, then no further lookup is
2656 required. If the template-id was for a template-class, we
2657 will sometimes have a TYPE_DECL at this point. */
2658 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2659 || TREE_CODE (id_expression) == TYPE_DECL)
2660 decl = id_expression;
2661 /* Look up the name. */
21526606 2662 else
a723baf1
MM
2663 {
2664 decl = cp_parser_lookup_name_simple (parser, id_expression);
2665 /* If name lookup gives us a SCOPE_REF, then the
2666 qualifying scope was dependent. Just propagate the
2667 name. */
2668 if (TREE_CODE (decl) == SCOPE_REF)
2669 {
2670 if (TYPE_P (TREE_OPERAND (decl, 0)))
2671 *qualifying_class = TREE_OPERAND (decl, 0);
2672 return decl;
2673 }
2674 /* Check to see if DECL is a local variable in a context
2675 where that is forbidden. */
2676 if (parser->local_variables_forbidden_p
2677 && local_variable_p (decl))
2678 {
2679 /* It might be that we only found DECL because we are
2680 trying to be generous with pre-ISO scoping rules.
2681 For example, consider:
2682
2683 int i;
2684 void g() {
2685 for (int i = 0; i < 10; ++i) {}
2686 extern void f(int j = i);
2687 }
2688
21526606 2689 Here, name look up will originally find the out
a723baf1
MM
2690 of scope `i'. We need to issue a warning message,
2691 but then use the global `i'. */
2692 decl = check_for_out_of_scope_variable (decl);
2693 if (local_variable_p (decl))
2694 {
2695 error ("local variable `%D' may not appear in this context",
2696 decl);
2697 return error_mark_node;
2698 }
2699 }
c006d942 2700 }
21526606
EC
2701
2702 decl = finish_id_expression (id_expression, decl, parser->scope,
b3445994 2703 idk, qualifying_class,
67c03833
JM
2704 parser->integral_constant_expression_p,
2705 parser->allow_non_integral_constant_expression_p,
2706 &parser->non_integral_constant_expression_p,
b3445994
MM
2707 &error_msg);
2708 if (error_msg)
2709 cp_parser_error (parser, error_msg);
a723baf1
MM
2710 return decl;
2711 }
2712
2713 /* Anything else is an error. */
2714 default:
2715 cp_parser_error (parser, "expected primary-expression");
2716 return error_mark_node;
2717 }
2718}
2719
2720/* Parse an id-expression.
2721
2722 id-expression:
2723 unqualified-id
2724 qualified-id
2725
2726 qualified-id:
2727 :: [opt] nested-name-specifier template [opt] unqualified-id
2728 :: identifier
2729 :: operator-function-id
2730 :: template-id
2731
2732 Return a representation of the unqualified portion of the
2733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2734 a `::' or nested-name-specifier.
2735
2736 Often, if the id-expression was a qualified-id, the caller will
2737 want to make a SCOPE_REF to represent the qualified-id. This
2738 function does not do this in order to avoid wastefully creating
2739 SCOPE_REFs when they are not required.
2740
a723baf1
MM
2741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2742 `template' keyword.
2743
2744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 2745 uninstantiated templates.
a723baf1 2746
15d2cb19 2747 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2748 `template' keyword is used to explicitly indicate that the entity
21526606 2749 named is a template.
f3c2dfc6
MM
2750
2751 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2752 a declarator, rather than as part of an expression. */
a723baf1
MM
2753
2754static tree
2755cp_parser_id_expression (cp_parser *parser,
2756 bool template_keyword_p,
2757 bool check_dependency_p,
f3c2dfc6
MM
2758 bool *template_p,
2759 bool declarator_p)
a723baf1
MM
2760{
2761 bool global_scope_p;
2762 bool nested_name_specifier_p;
2763
2764 /* Assume the `template' keyword was not used. */
2765 if (template_p)
2766 *template_p = false;
2767
2768 /* Look for the optional `::' operator. */
21526606
EC
2769 global_scope_p
2770 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
2771 != NULL_TREE);
2772 /* Look for the optional nested-name-specifier. */
21526606 2773 nested_name_specifier_p
a723baf1
MM
2774 = (cp_parser_nested_name_specifier_opt (parser,
2775 /*typename_keyword_p=*/false,
2776 check_dependency_p,
a668c6ad
MM
2777 /*type_p=*/false,
2778 /*is_declarator=*/false)
a723baf1
MM
2779 != NULL_TREE);
2780 /* If there is a nested-name-specifier, then we are looking at
2781 the first qualified-id production. */
2782 if (nested_name_specifier_p)
2783 {
2784 tree saved_scope;
2785 tree saved_object_scope;
2786 tree saved_qualifying_scope;
2787 tree unqualified_id;
2788 bool is_template;
2789
2790 /* See if the next token is the `template' keyword. */
2791 if (!template_p)
2792 template_p = &is_template;
2793 *template_p = cp_parser_optional_template_keyword (parser);
2794 /* Name lookup we do during the processing of the
2795 unqualified-id might obliterate SCOPE. */
2796 saved_scope = parser->scope;
2797 saved_object_scope = parser->object_scope;
2798 saved_qualifying_scope = parser->qualifying_scope;
2799 /* Process the final unqualified-id. */
2800 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2801 check_dependency_p,
2802 declarator_p);
a723baf1
MM
2803 /* Restore the SAVED_SCOPE for our caller. */
2804 parser->scope = saved_scope;
2805 parser->object_scope = saved_object_scope;
2806 parser->qualifying_scope = saved_qualifying_scope;
2807
2808 return unqualified_id;
2809 }
2810 /* Otherwise, if we are in global scope, then we are looking at one
2811 of the other qualified-id productions. */
2812 else if (global_scope_p)
2813 {
2814 cp_token *token;
2815 tree id;
2816
e5976695
MM
2817 /* Peek at the next token. */
2818 token = cp_lexer_peek_token (parser->lexer);
2819
2820 /* If it's an identifier, and the next token is not a "<", then
2821 we can avoid the template-id case. This is an optimization
2822 for this common case. */
21526606
EC
2823 if (token->type == CPP_NAME
2824 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 2825 (parser, 2))
e5976695
MM
2826 return cp_parser_identifier (parser);
2827
a723baf1
MM
2828 cp_parser_parse_tentatively (parser);
2829 /* Try a template-id. */
21526606 2830 id = cp_parser_template_id (parser,
a723baf1 2831 /*template_keyword_p=*/false,
a668c6ad
MM
2832 /*check_dependency_p=*/true,
2833 declarator_p);
a723baf1
MM
2834 /* If that worked, we're done. */
2835 if (cp_parser_parse_definitely (parser))
2836 return id;
2837
e5976695
MM
2838 /* Peek at the next token. (Changes in the token buffer may
2839 have invalidated the pointer obtained above.) */
a723baf1
MM
2840 token = cp_lexer_peek_token (parser->lexer);
2841
2842 switch (token->type)
2843 {
2844 case CPP_NAME:
2845 return cp_parser_identifier (parser);
2846
2847 case CPP_KEYWORD:
2848 if (token->keyword == RID_OPERATOR)
2849 return cp_parser_operator_function_id (parser);
2850 /* Fall through. */
21526606 2851
a723baf1
MM
2852 default:
2853 cp_parser_error (parser, "expected id-expression");
2854 return error_mark_node;
2855 }
2856 }
2857 else
2858 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
2859 /*check_dependency_p=*/true,
2860 declarator_p);
a723baf1
MM
2861}
2862
2863/* Parse an unqualified-id.
2864
2865 unqualified-id:
2866 identifier
2867 operator-function-id
2868 conversion-function-id
2869 ~ class-name
2870 template-id
2871
2872 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2873 keyword, in a construct like `A::template ...'.
2874
2875 Returns a representation of unqualified-id. For the `identifier'
2876 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2877 production a BIT_NOT_EXPR is returned; the operand of the
2878 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2879 other productions, see the documentation accompanying the
2880 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
2881 names are looked up in uninstantiated templates. If DECLARATOR_P
2882 is true, the unqualified-id is appearing as part of a declarator,
2883 rather than as part of an expression. */
a723baf1
MM
2884
2885static tree
21526606 2886cp_parser_unqualified_id (cp_parser* parser,
94edc4ab 2887 bool template_keyword_p,
f3c2dfc6
MM
2888 bool check_dependency_p,
2889 bool declarator_p)
a723baf1
MM
2890{
2891 cp_token *token;
2892
2893 /* Peek at the next token. */
2894 token = cp_lexer_peek_token (parser->lexer);
21526606 2895
a723baf1
MM
2896 switch (token->type)
2897 {
2898 case CPP_NAME:
2899 {
2900 tree id;
2901
2902 /* We don't know yet whether or not this will be a
2903 template-id. */
2904 cp_parser_parse_tentatively (parser);
2905 /* Try a template-id. */
2906 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2907 check_dependency_p,
2908 declarator_p);
a723baf1
MM
2909 /* If it worked, we're done. */
2910 if (cp_parser_parse_definitely (parser))
2911 return id;
2912 /* Otherwise, it's an ordinary identifier. */
2913 return cp_parser_identifier (parser);
2914 }
2915
2916 case CPP_TEMPLATE_ID:
2917 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2918 check_dependency_p,
2919 declarator_p);
a723baf1
MM
2920
2921 case CPP_COMPL:
2922 {
2923 tree type_decl;
2924 tree qualifying_scope;
2925 tree object_scope;
2926 tree scope;
2927
2928 /* Consume the `~' token. */
2929 cp_lexer_consume_token (parser->lexer);
2930 /* Parse the class-name. The standard, as written, seems to
2931 say that:
2932
2933 template <typename T> struct S { ~S (); };
2934 template <typename T> S<T>::~S() {}
2935
2936 is invalid, since `~' must be followed by a class-name, but
2937 `S<T>' is dependent, and so not known to be a class.
2938 That's not right; we need to look in uninstantiated
2939 templates. A further complication arises from:
2940
2941 template <typename T> void f(T t) {
2942 t.T::~T();
21526606 2943 }
a723baf1
MM
2944
2945 Here, it is not possible to look up `T' in the scope of `T'
2946 itself. We must look in both the current scope, and the
21526606 2947 scope of the containing complete expression.
a723baf1
MM
2948
2949 Yet another issue is:
2950
2951 struct S {
2952 int S;
2953 ~S();
2954 };
2955
2956 S::~S() {}
2957
2958 The standard does not seem to say that the `S' in `~S'
2959 should refer to the type `S' and not the data member
2960 `S::S'. */
2961
2962 /* DR 244 says that we look up the name after the "~" in the
2963 same scope as we looked up the qualifying name. That idea
2964 isn't fully worked out; it's more complicated than that. */
2965 scope = parser->scope;
2966 object_scope = parser->object_scope;
2967 qualifying_scope = parser->qualifying_scope;
2968
2969 /* If the name is of the form "X::~X" it's OK. */
2970 if (scope && TYPE_P (scope)
2971 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 2973 == CPP_OPEN_PAREN)
21526606 2974 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
2975 == TYPE_IDENTIFIER (scope)))
2976 {
2977 cp_lexer_consume_token (parser->lexer);
2978 return build_nt (BIT_NOT_EXPR, scope);
2979 }
2980
2981 /* If there was an explicit qualification (S::~T), first look
2982 in the scope given by the qualification (i.e., S). */
2983 if (scope)
2984 {
2985 cp_parser_parse_tentatively (parser);
21526606 2986 type_decl = cp_parser_class_name (parser,
a723baf1
MM
2987 /*typename_keyword_p=*/false,
2988 /*template_keyword_p=*/false,
2989 /*type_p=*/false,
a723baf1 2990 /*check_dependency=*/false,
a668c6ad
MM
2991 /*class_head_p=*/false,
2992 declarator_p);
a723baf1
MM
2993 if (cp_parser_parse_definitely (parser))
2994 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2995 }
2996 /* In "N::S::~S", look in "N" as well. */
2997 if (scope && qualifying_scope)
2998 {
2999 cp_parser_parse_tentatively (parser);
3000 parser->scope = qualifying_scope;
3001 parser->object_scope = NULL_TREE;
3002 parser->qualifying_scope = NULL_TREE;
21526606
EC
3003 type_decl
3004 = cp_parser_class_name (parser,
a723baf1
MM
3005 /*typename_keyword_p=*/false,
3006 /*template_keyword_p=*/false,
3007 /*type_p=*/false,
a723baf1 3008 /*check_dependency=*/false,
a668c6ad
MM
3009 /*class_head_p=*/false,
3010 declarator_p);
a723baf1
MM
3011 if (cp_parser_parse_definitely (parser))
3012 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3013 }
3014 /* In "p->S::~T", look in the scope given by "*p" as well. */
3015 else if (object_scope)
3016 {
3017 cp_parser_parse_tentatively (parser);
3018 parser->scope = object_scope;
3019 parser->object_scope = NULL_TREE;
3020 parser->qualifying_scope = NULL_TREE;
21526606
EC
3021 type_decl
3022 = cp_parser_class_name (parser,
a723baf1
MM
3023 /*typename_keyword_p=*/false,
3024 /*template_keyword_p=*/false,
3025 /*type_p=*/false,
a723baf1 3026 /*check_dependency=*/false,
a668c6ad
MM
3027 /*class_head_p=*/false,
3028 declarator_p);
a723baf1
MM
3029 if (cp_parser_parse_definitely (parser))
3030 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3031 }
3032 /* Look in the surrounding context. */
3033 parser->scope = NULL_TREE;
3034 parser->object_scope = NULL_TREE;
3035 parser->qualifying_scope = NULL_TREE;
21526606
EC
3036 type_decl
3037 = cp_parser_class_name (parser,
a723baf1
MM
3038 /*typename_keyword_p=*/false,
3039 /*template_keyword_p=*/false,
3040 /*type_p=*/false,
a723baf1 3041 /*check_dependency=*/false,
a668c6ad
MM
3042 /*class_head_p=*/false,
3043 declarator_p);
a723baf1
MM
3044 /* If an error occurred, assume that the name of the
3045 destructor is the same as the name of the qualifying
3046 class. That allows us to keep parsing after running
3047 into ill-formed destructor names. */
3048 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3049 return build_nt (BIT_NOT_EXPR, scope);
3050 else if (type_decl == error_mark_node)
3051 return error_mark_node;
3052
f3c2dfc6
MM
3053 /* [class.dtor]
3054
3055 A typedef-name that names a class shall not be used as the
3056 identifier in the declarator for a destructor declaration. */
21526606 3057 if (declarator_p
f3c2dfc6
MM
3058 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3059 && !DECL_SELF_REFERENCE_P (type_decl))
3060 error ("typedef-name `%D' used as destructor declarator",
3061 type_decl);
3062
a723baf1
MM
3063 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3064 }
3065
3066 case CPP_KEYWORD:
3067 if (token->keyword == RID_OPERATOR)
3068 {
3069 tree id;
3070
3071 /* This could be a template-id, so we try that first. */
3072 cp_parser_parse_tentatively (parser);
3073 /* Try a template-id. */
3074 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3075 /*check_dependency_p=*/true,
3076 declarator_p);
a723baf1
MM
3077 /* If that worked, we're done. */
3078 if (cp_parser_parse_definitely (parser))
3079 return id;
3080 /* We still don't know whether we're looking at an
3081 operator-function-id or a conversion-function-id. */
3082 cp_parser_parse_tentatively (parser);
3083 /* Try an operator-function-id. */
3084 id = cp_parser_operator_function_id (parser);
3085 /* If that didn't work, try a conversion-function-id. */
3086 if (!cp_parser_parse_definitely (parser))
3087 id = cp_parser_conversion_function_id (parser);
3088
3089 return id;
3090 }
3091 /* Fall through. */
3092
3093 default:
3094 cp_parser_error (parser, "expected unqualified-id");
3095 return error_mark_node;
3096 }
3097}
3098
3099/* Parse an (optional) nested-name-specifier.
3100
3101 nested-name-specifier:
3102 class-or-namespace-name :: nested-name-specifier [opt]
3103 class-or-namespace-name :: template nested-name-specifier [opt]
3104
3105 PARSER->SCOPE should be set appropriately before this function is
3106 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3107 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3108 in name lookups.
3109
3110 Sets PARSER->SCOPE to the class (TYPE) or namespace
3111 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3112 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3113 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3114
3115 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3116 part of a declaration and/or decl-specifier. */
a723baf1
MM
3117
3118static tree
21526606
EC
3119cp_parser_nested_name_specifier_opt (cp_parser *parser,
3120 bool typename_keyword_p,
a723baf1 3121 bool check_dependency_p,
a668c6ad
MM
3122 bool type_p,
3123 bool is_declaration)
a723baf1
MM
3124{
3125 bool success = false;
3126 tree access_check = NULL_TREE;
3127 ptrdiff_t start;
2050a1bb 3128 cp_token* token;
a723baf1
MM
3129
3130 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3131 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3132 false, it may have been true before, in which case something
2050a1bb
MM
3133 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3134 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3135 CHECK_DEPENDENCY_P is false, we have to fall through into the
3136 main loop. */
3137 if (check_dependency_p
3138 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3139 {
3140 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3141 return parser->scope;
3142 }
3143
3144 /* Remember where the nested-name-specifier starts. */
3145 if (cp_parser_parsing_tentatively (parser)
3146 && !cp_parser_committed_to_tentative_parse (parser))
3147 {
2050a1bb 3148 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3149 start = cp_lexer_token_difference (parser->lexer,
3150 parser->lexer->first_token,
2050a1bb 3151 token);
a723baf1
MM
3152 }
3153 else
3154 start = -1;
3155
8d241e0b 3156 push_deferring_access_checks (dk_deferred);
cf22909c 3157
a723baf1
MM
3158 while (true)
3159 {
3160 tree new_scope;
3161 tree old_scope;
3162 tree saved_qualifying_scope;
a723baf1
MM
3163 bool template_keyword_p;
3164
2050a1bb
MM
3165 /* Spot cases that cannot be the beginning of a
3166 nested-name-specifier. */
3167 token = cp_lexer_peek_token (parser->lexer);
3168
3169 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3170 the already parsed nested-name-specifier. */
3171 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3172 {
3173 /* Grab the nested-name-specifier and continue the loop. */
3174 cp_parser_pre_parsed_nested_name_specifier (parser);
3175 success = true;
3176 continue;
3177 }
3178
a723baf1
MM
3179 /* Spot cases that cannot be the beginning of a
3180 nested-name-specifier. On the second and subsequent times
3181 through the loop, we look for the `template' keyword. */
f7b5ecd9 3182 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3183 ;
3184 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3185 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3186 ;
3187 else
3188 {
3189 /* If the next token is not an identifier, then it is
3190 definitely not a class-or-namespace-name. */
f7b5ecd9 3191 if (token->type != CPP_NAME)
a723baf1
MM
3192 break;
3193 /* If the following token is neither a `<' (to begin a
3194 template-id), nor a `::', then we are not looking at a
3195 nested-name-specifier. */
3196 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3197 if (token->type != CPP_SCOPE
3198 && !cp_parser_nth_token_starts_template_argument_list_p
3199 (parser, 2))
a723baf1
MM
3200 break;
3201 }
3202
3203 /* The nested-name-specifier is optional, so we parse
3204 tentatively. */
3205 cp_parser_parse_tentatively (parser);
3206
3207 /* Look for the optional `template' keyword, if this isn't the
3208 first time through the loop. */
3209 if (success)
3210 template_keyword_p = cp_parser_optional_template_keyword (parser);
3211 else
3212 template_keyword_p = false;
3213
3214 /* Save the old scope since the name lookup we are about to do
3215 might destroy it. */
3216 old_scope = parser->scope;
3217 saved_qualifying_scope = parser->qualifying_scope;
3218 /* Parse the qualifying entity. */
21526606 3219 new_scope
a723baf1
MM
3220 = cp_parser_class_or_namespace_name (parser,
3221 typename_keyword_p,
3222 template_keyword_p,
3223 check_dependency_p,
a668c6ad
MM
3224 type_p,
3225 is_declaration);
a723baf1
MM
3226 /* Look for the `::' token. */
3227 cp_parser_require (parser, CPP_SCOPE, "`::'");
3228
3229 /* If we found what we wanted, we keep going; otherwise, we're
3230 done. */
3231 if (!cp_parser_parse_definitely (parser))
3232 {
3233 bool error_p = false;
3234
3235 /* Restore the OLD_SCOPE since it was valid before the
3236 failed attempt at finding the last
3237 class-or-namespace-name. */
3238 parser->scope = old_scope;
3239 parser->qualifying_scope = saved_qualifying_scope;
3240 /* If the next token is an identifier, and the one after
3241 that is a `::', then any valid interpretation would have
3242 found a class-or-namespace-name. */
3243 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3244 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3245 == CPP_SCOPE)
21526606 3246 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3247 != CPP_COMPL))
3248 {
3249 token = cp_lexer_consume_token (parser->lexer);
21526606 3250 if (!error_p)
a723baf1
MM
3251 {
3252 tree decl;
3253
3254 decl = cp_parser_lookup_name_simple (parser, token->value);
3255 if (TREE_CODE (decl) == TEMPLATE_DECL)
3256 error ("`%D' used without template parameters",
3257 decl);
a723baf1 3258 else
21526606
EC
3259 cp_parser_name_lookup_error
3260 (parser, token->value, decl,
4bb8ca28 3261 "is not a class or namespace");
a723baf1
MM
3262 parser->scope = NULL_TREE;
3263 error_p = true;
eea9800f
MM
3264 /* Treat this as a successful nested-name-specifier
3265 due to:
3266
3267 [basic.lookup.qual]
3268
3269 If the name found is not a class-name (clause
3270 _class_) or namespace-name (_namespace.def_), the
3271 program is ill-formed. */
3272 success = true;
a723baf1
MM
3273 }
3274 cp_lexer_consume_token (parser->lexer);
3275 }
3276 break;
3277 }
3278
3279 /* We've found one valid nested-name-specifier. */
3280 success = true;
3281 /* Make sure we look in the right scope the next time through
3282 the loop. */
21526606 3283 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3284 ? TREE_TYPE (new_scope)
3285 : new_scope);
3286 /* If it is a class scope, try to complete it; we are about to
3287 be looking up names inside the class. */
8fbc5ae7
MM
3288 if (TYPE_P (parser->scope)
3289 /* Since checking types for dependency can be expensive,
3290 avoid doing it if the type is already complete. */
3291 && !COMPLETE_TYPE_P (parser->scope)
3292 /* Do not try to complete dependent types. */
1fb3244a 3293 && !dependent_type_p (parser->scope))
a723baf1
MM
3294 complete_type (parser->scope);
3295 }
3296
cf22909c
KL
3297 /* Retrieve any deferred checks. Do not pop this access checks yet
3298 so the memory will not be reclaimed during token replacing below. */
3299 access_check = get_deferred_access_checks ();
3300
a723baf1
MM
3301 /* If parsing tentatively, replace the sequence of tokens that makes
3302 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3303 token. That way, should we re-parse the token stream, we will
3304 not have to repeat the effort required to do the parse, nor will
3305 we issue duplicate error messages. */
3306 if (success && start >= 0)
3307 {
a723baf1
MM
3308 /* Find the token that corresponds to the start of the
3309 template-id. */
21526606 3310 token = cp_lexer_advance_token (parser->lexer,
a723baf1
MM
3311 parser->lexer->first_token,
3312 start);
3313
a723baf1
MM
3314 /* Reset the contents of the START token. */
3315 token->type = CPP_NESTED_NAME_SPECIFIER;
3316 token->value = build_tree_list (access_check, parser->scope);
3317 TREE_TYPE (token->value) = parser->qualifying_scope;
3318 token->keyword = RID_MAX;
3319 /* Purge all subsequent tokens. */
3320 cp_lexer_purge_tokens_after (parser->lexer, token);
3321 }
3322
cf22909c 3323 pop_deferring_access_checks ();
a723baf1
MM
3324 return success ? parser->scope : NULL_TREE;
3325}
3326
3327/* Parse a nested-name-specifier. See
3328 cp_parser_nested_name_specifier_opt for details. This function
3329 behaves identically, except that it will an issue an error if no
3330 nested-name-specifier is present, and it will return
3331 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3332 is present. */
3333
3334static tree
21526606
EC
3335cp_parser_nested_name_specifier (cp_parser *parser,
3336 bool typename_keyword_p,
a723baf1 3337 bool check_dependency_p,
a668c6ad
MM
3338 bool type_p,
3339 bool is_declaration)
a723baf1
MM
3340{
3341 tree scope;
3342
3343 /* Look for the nested-name-specifier. */
3344 scope = cp_parser_nested_name_specifier_opt (parser,
3345 typename_keyword_p,
3346 check_dependency_p,
a668c6ad
MM
3347 type_p,
3348 is_declaration);
a723baf1
MM
3349 /* If it was not present, issue an error message. */
3350 if (!scope)
3351 {
3352 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3353 parser->scope = NULL_TREE;
a723baf1
MM
3354 return error_mark_node;
3355 }
3356
3357 return scope;
3358}
3359
3360/* Parse a class-or-namespace-name.
3361
3362 class-or-namespace-name:
3363 class-name
3364 namespace-name
3365
3366 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3367 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3368 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3369 TYPE_P is TRUE iff the next name should be taken as a class-name,
3370 even the same name is declared to be another entity in the same
3371 scope.
3372
3373 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3374 specified by the class-or-namespace-name. If neither is found the
3375 ERROR_MARK_NODE is returned. */
a723baf1
MM
3376
3377static tree
21526606 3378cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3379 bool typename_keyword_p,
3380 bool template_keyword_p,
3381 bool check_dependency_p,
a668c6ad
MM
3382 bool type_p,
3383 bool is_declaration)
a723baf1
MM
3384{
3385 tree saved_scope;
3386 tree saved_qualifying_scope;
3387 tree saved_object_scope;
3388 tree scope;
eea9800f 3389 bool only_class_p;
a723baf1 3390
a723baf1
MM
3391 /* Before we try to parse the class-name, we must save away the
3392 current PARSER->SCOPE since cp_parser_class_name will destroy
3393 it. */
3394 saved_scope = parser->scope;
3395 saved_qualifying_scope = parser->qualifying_scope;
3396 saved_object_scope = parser->object_scope;
eea9800f
MM
3397 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3398 there is no need to look for a namespace-name. */
bbaab916 3399 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3400 if (!only_class_p)
3401 cp_parser_parse_tentatively (parser);
21526606 3402 scope = cp_parser_class_name (parser,
a723baf1
MM
3403 typename_keyword_p,
3404 template_keyword_p,
3405 type_p,
a723baf1 3406 check_dependency_p,
a668c6ad
MM
3407 /*class_head_p=*/false,
3408 is_declaration);
a723baf1 3409 /* If that didn't work, try for a namespace-name. */
eea9800f 3410 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3411 {
3412 /* Restore the saved scope. */
3413 parser->scope = saved_scope;
3414 parser->qualifying_scope = saved_qualifying_scope;
3415 parser->object_scope = saved_object_scope;
eea9800f
MM
3416 /* If we are not looking at an identifier followed by the scope
3417 resolution operator, then this is not part of a
3418 nested-name-specifier. (Note that this function is only used
3419 to parse the components of a nested-name-specifier.) */
3420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3421 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3422 return error_mark_node;
a723baf1
MM
3423 scope = cp_parser_namespace_name (parser);
3424 }
3425
3426 return scope;
3427}
3428
3429/* Parse a postfix-expression.
3430
3431 postfix-expression:
3432 primary-expression
3433 postfix-expression [ expression ]
3434 postfix-expression ( expression-list [opt] )
3435 simple-type-specifier ( expression-list [opt] )
21526606 3436 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3437 ( expression-list [opt] )
3438 typename :: [opt] nested-name-specifier template [opt] template-id
3439 ( expression-list [opt] )
3440 postfix-expression . template [opt] id-expression
3441 postfix-expression -> template [opt] id-expression
3442 postfix-expression . pseudo-destructor-name
3443 postfix-expression -> pseudo-destructor-name
3444 postfix-expression ++
3445 postfix-expression --
3446 dynamic_cast < type-id > ( expression )
3447 static_cast < type-id > ( expression )
3448 reinterpret_cast < type-id > ( expression )
3449 const_cast < type-id > ( expression )
3450 typeid ( expression )
3451 typeid ( type-id )
3452
3453 GNU Extension:
21526606 3454
a723baf1
MM
3455 postfix-expression:
3456 ( type-id ) { initializer-list , [opt] }
3457
3458 This extension is a GNU version of the C99 compound-literal
3459 construct. (The C99 grammar uses `type-name' instead of `type-id',
3460 but they are essentially the same concept.)
3461
3462 If ADDRESS_P is true, the postfix expression is the operand of the
3463 `&' operator.
3464
3465 Returns a representation of the expression. */
3466
3467static tree
3468cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3469{
3470 cp_token *token;
3471 enum rid keyword;
b3445994 3472 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3473 tree postfix_expression = NULL_TREE;
3474 /* Non-NULL only if the current postfix-expression can be used to
3475 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3476 class used to qualify the member. */
3477 tree qualifying_class = NULL_TREE;
a723baf1
MM
3478
3479 /* Peek at the next token. */
3480 token = cp_lexer_peek_token (parser->lexer);
3481 /* Some of the productions are determined by keywords. */
3482 keyword = token->keyword;
3483 switch (keyword)
3484 {
3485 case RID_DYNCAST:
3486 case RID_STATCAST:
3487 case RID_REINTCAST:
3488 case RID_CONSTCAST:
3489 {
3490 tree type;
3491 tree expression;
3492 const char *saved_message;
3493
3494 /* All of these can be handled in the same way from the point
3495 of view of parsing. Begin by consuming the token
3496 identifying the cast. */
3497 cp_lexer_consume_token (parser->lexer);
21526606 3498
a723baf1
MM
3499 /* New types cannot be defined in the cast. */
3500 saved_message = parser->type_definition_forbidden_message;
3501 parser->type_definition_forbidden_message
3502 = "types may not be defined in casts";
3503
3504 /* Look for the opening `<'. */
3505 cp_parser_require (parser, CPP_LESS, "`<'");
3506 /* Parse the type to which we are casting. */
3507 type = cp_parser_type_id (parser);
3508 /* Look for the closing `>'. */
3509 cp_parser_require (parser, CPP_GREATER, "`>'");
3510 /* Restore the old message. */
3511 parser->type_definition_forbidden_message = saved_message;
3512
3513 /* And the expression which is being cast. */
3514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3515 expression = cp_parser_expression (parser);
3516 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3517
14d22dd6
MM
3518 /* Only type conversions to integral or enumeration types
3519 can be used in constant-expressions. */
67c03833 3520 if (parser->integral_constant_expression_p
14d22dd6 3521 && !dependent_type_p (type)
263ee052
MM
3522 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3523 /* A cast to pointer or reference type is allowed in the
3524 implementation of "offsetof". */
625cbf93
MM
3525 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3526 && (cp_parser_non_integral_constant_expression
3527 (parser,
3528 "a cast to a type other than an integral or "
3529 "enumeration type")))
3530 return error_mark_node;
14d22dd6 3531
a723baf1
MM
3532 switch (keyword)
3533 {
3534 case RID_DYNCAST:
3535 postfix_expression
3536 = build_dynamic_cast (type, expression);
3537 break;
3538 case RID_STATCAST:
3539 postfix_expression
3540 = build_static_cast (type, expression);
3541 break;
3542 case RID_REINTCAST:
3543 postfix_expression
3544 = build_reinterpret_cast (type, expression);
3545 break;
3546 case RID_CONSTCAST:
3547 postfix_expression
3548 = build_const_cast (type, expression);
3549 break;
3550 default:
3551 abort ();
3552 }
3553 }
3554 break;
3555
3556 case RID_TYPEID:
3557 {
3558 tree type;
3559 const char *saved_message;
4f8163b1 3560 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3561
3562 /* Consume the `typeid' token. */
3563 cp_lexer_consume_token (parser->lexer);
3564 /* Look for the `(' token. */
3565 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3566 /* Types cannot be defined in a `typeid' expression. */
3567 saved_message = parser->type_definition_forbidden_message;
3568 parser->type_definition_forbidden_message
3569 = "types may not be defined in a `typeid\' expression";
3570 /* We can't be sure yet whether we're looking at a type-id or an
3571 expression. */
3572 cp_parser_parse_tentatively (parser);
3573 /* Try a type-id first. */
4f8163b1
MM
3574 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3575 parser->in_type_id_in_expr_p = true;
a723baf1 3576 type = cp_parser_type_id (parser);
4f8163b1 3577 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3578 /* Look for the `)' token. Otherwise, we can't be sure that
3579 we're not looking at an expression: consider `typeid (int
3580 (3))', for example. */
3581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3582 /* If all went well, simply lookup the type-id. */
3583 if (cp_parser_parse_definitely (parser))
3584 postfix_expression = get_typeid (type);
3585 /* Otherwise, fall back to the expression variant. */
3586 else
3587 {
3588 tree expression;
3589
3590 /* Look for an expression. */
3591 expression = cp_parser_expression (parser);
3592 /* Compute its typeid. */
3593 postfix_expression = build_typeid (expression);
3594 /* Look for the `)' token. */
3595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3596 }
3597
3598 /* Restore the saved message. */
3599 parser->type_definition_forbidden_message = saved_message;
3600 }
3601 break;
21526606 3602
a723baf1
MM
3603 case RID_TYPENAME:
3604 {
3605 bool template_p = false;
3606 tree id;
3607 tree type;
3608
3609 /* Consume the `typename' token. */
3610 cp_lexer_consume_token (parser->lexer);
3611 /* Look for the optional `::' operator. */
21526606 3612 cp_parser_global_scope_opt (parser,
a723baf1
MM
3613 /*current_scope_valid_p=*/false);
3614 /* Look for the nested-name-specifier. */
3615 cp_parser_nested_name_specifier (parser,
3616 /*typename_keyword_p=*/true,
3617 /*check_dependency_p=*/true,
a668c6ad
MM
3618 /*type_p=*/true,
3619 /*is_declaration=*/true);
a723baf1
MM
3620 /* Look for the optional `template' keyword. */
3621 template_p = cp_parser_optional_template_keyword (parser);
3622 /* We don't know whether we're looking at a template-id or an
3623 identifier. */
3624 cp_parser_parse_tentatively (parser);
3625 /* Try a template-id. */
3626 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3627 /*check_dependency_p=*/true,
3628 /*is_declaration=*/true);
a723baf1
MM
3629 /* If that didn't work, try an identifier. */
3630 if (!cp_parser_parse_definitely (parser))
3631 id = cp_parser_identifier (parser);
26bcf8fc
MM
3632 /* If we look up a template-id in a non-dependent qualifying
3633 scope, there's no need to create a dependent type. */
3634 if (TREE_CODE (id) == TYPE_DECL
3635 && !dependent_type_p (parser->scope))
3636 type = TREE_TYPE (id);
a723baf1
MM
3637 /* Create a TYPENAME_TYPE to represent the type to which the
3638 functional cast is being performed. */
26bcf8fc
MM
3639 else
3640 type = make_typename_type (parser->scope, id,
3641 /*complain=*/1);
a723baf1
MM
3642
3643 postfix_expression = cp_parser_functional_cast (parser, type);
3644 }
3645 break;
3646
3647 default:
3648 {
3649 tree type;
3650
3651 /* If the next thing is a simple-type-specifier, we may be
3652 looking at a functional cast. We could also be looking at
3653 an id-expression. So, we try the functional cast, and if
3654 that doesn't work we fall back to the primary-expression. */
3655 cp_parser_parse_tentatively (parser);
3656 /* Look for the simple-type-specifier. */
21526606 3657 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3658 CP_PARSER_FLAGS_NONE,
3659 /*identifier_p=*/false);
a723baf1
MM
3660 /* Parse the cast itself. */
3661 if (!cp_parser_error_occurred (parser))
21526606 3662 postfix_expression
a723baf1
MM
3663 = cp_parser_functional_cast (parser, type);
3664 /* If that worked, we're done. */
3665 if (cp_parser_parse_definitely (parser))
3666 break;
3667
3668 /* If the functional-cast didn't work out, try a
3669 compound-literal. */
14d22dd6
MM
3670 if (cp_parser_allow_gnu_extensions_p (parser)
3671 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3672 {
3673 tree initializer_list = NULL_TREE;
4f8163b1 3674 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3675
3676 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3677 /* Consume the `('. */
3678 cp_lexer_consume_token (parser->lexer);
3679 /* Parse the type. */
4f8163b1
MM
3680 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3681 parser->in_type_id_in_expr_p = true;
14d22dd6 3682 type = cp_parser_type_id (parser);
4f8163b1 3683 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3684 /* Look for the `)'. */
3685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3686 /* Look for the `{'. */
3687 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3688 /* If things aren't going well, there's no need to
3689 keep going. */
3690 if (!cp_parser_error_occurred (parser))
a723baf1 3691 {
39703eb9 3692 bool non_constant_p;
14d22dd6 3693 /* Parse the initializer-list. */
21526606 3694 initializer_list
39703eb9 3695 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3696 /* Allow a trailing `,'. */
3697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3698 cp_lexer_consume_token (parser->lexer);
3699 /* Look for the final `}'. */
3700 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3701 }
3702 /* If that worked, we're definitely looking at a
3703 compound-literal expression. */
3704 if (cp_parser_parse_definitely (parser))
3705 {
3706 /* Warn the user that a compound literal is not
3707 allowed in standard C++. */
3708 if (pedantic)
3709 pedwarn ("ISO C++ forbids compound-literals");
3710 /* Form the representation of the compound-literal. */
21526606 3711 postfix_expression
a723baf1
MM
3712 = finish_compound_literal (type, initializer_list);
3713 break;
3714 }
3715 }
3716
3717 /* It must be a primary-expression. */
21526606 3718 postfix_expression = cp_parser_primary_expression (parser,
a723baf1
MM
3719 &idk,
3720 &qualifying_class);
3721 }
3722 break;
3723 }
3724
ee76b931
MM
3725 /* If we were avoiding committing to the processing of a
3726 qualified-id until we knew whether or not we had a
3727 pointer-to-member, we now know. */
089d6ea7 3728 if (qualifying_class)
a723baf1 3729 {
ee76b931 3730 bool done;
a723baf1 3731
ee76b931
MM
3732 /* Peek at the next token. */
3733 token = cp_lexer_peek_token (parser->lexer);
3734 done = (token->type != CPP_OPEN_SQUARE
3735 && token->type != CPP_OPEN_PAREN
3736 && token->type != CPP_DOT
3737 && token->type != CPP_DEREF
3738 && token->type != CPP_PLUS_PLUS
3739 && token->type != CPP_MINUS_MINUS);
3740
3741 postfix_expression = finish_qualified_id_expr (qualifying_class,
3742 postfix_expression,
3743 done,
3744 address_p);
3745 if (done)
3746 return postfix_expression;
a723baf1
MM
3747 }
3748
a723baf1
MM
3749 /* Keep looping until the postfix-expression is complete. */
3750 while (true)
3751 {
10b1d5e7
MM
3752 if (idk == CP_ID_KIND_UNQUALIFIED
3753 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3754 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 3755 /* It is not a Koenig lookup function call. */
21526606 3756 postfix_expression
b3445994 3757 = unqualified_name_lookup_error (postfix_expression);
21526606 3758
a723baf1
MM
3759 /* Peek at the next token. */
3760 token = cp_lexer_peek_token (parser->lexer);
3761
3762 switch (token->type)
3763 {
3764 case CPP_OPEN_SQUARE:
3765 /* postfix-expression [ expression ] */
3766 {
3767 tree index;
3768
3769 /* Consume the `[' token. */
3770 cp_lexer_consume_token (parser->lexer);
3771 /* Parse the index expression. */
3772 index = cp_parser_expression (parser);
3773 /* Look for the closing `]'. */
3774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3775
3776 /* Build the ARRAY_REF. */
21526606 3777 postfix_expression
a723baf1 3778 = grok_array_decl (postfix_expression, index);
b3445994 3779 idk = CP_ID_KIND_NONE;
a5ac3982
MM
3780 /* Array references are not permitted in
3781 constant-expressions. */
625cbf93
MM
3782 if (cp_parser_non_integral_constant_expression
3783 (parser, "an array reference"))
3784 postfix_expression = error_mark_node;
a723baf1
MM
3785 }
3786 break;
3787
3788 case CPP_OPEN_PAREN:
3789 /* postfix-expression ( expression-list [opt] ) */
3790 {
6d80c4b9 3791 bool koenig_p;
21526606 3792 tree args = (cp_parser_parenthesized_expression_list
39703eb9 3793 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3794
7efa3e22
NS
3795 if (args == error_mark_node)
3796 {
3797 postfix_expression = error_mark_node;
3798 break;
3799 }
21526606 3800
14d22dd6
MM
3801 /* Function calls are not permitted in
3802 constant-expressions. */
625cbf93
MM
3803 if (cp_parser_non_integral_constant_expression (parser,
3804 "a function call"))
14d22dd6 3805 {
625cbf93
MM
3806 postfix_expression = error_mark_node;
3807 break;
14d22dd6 3808 }
a723baf1 3809
6d80c4b9 3810 koenig_p = false;
399dedb9
NS
3811 if (idk == CP_ID_KIND_UNQUALIFIED)
3812 {
676e33ca
MM
3813 /* We do not perform argument-dependent lookup if
3814 normal lookup finds a non-function, in accordance
3815 with the expected resolution of DR 218. */
399dedb9
NS
3816 if (args
3817 && (is_overloaded_fn (postfix_expression)
399dedb9 3818 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3819 {
3820 koenig_p = true;
21526606 3821 postfix_expression
6d80c4b9
MM
3822 = perform_koenig_lookup (postfix_expression, args);
3823 }
399dedb9
NS
3824 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3825 postfix_expression
3826 = unqualified_fn_lookup_error (postfix_expression);
3827 }
21526606 3828
d17811fd 3829 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3830 {
d17811fd
MM
3831 tree instance = TREE_OPERAND (postfix_expression, 0);
3832 tree fn = TREE_OPERAND (postfix_expression, 1);
3833
3834 if (processing_template_decl
3835 && (type_dependent_expression_p (instance)
3836 || (!BASELINK_P (fn)
3837 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3838 || type_dependent_expression_p (fn)
d17811fd
MM
3839 || any_type_dependent_arguments_p (args)))
3840 {
3841 postfix_expression
3842 = build_min_nt (CALL_EXPR, postfix_expression, args);
3843 break;
3844 }
9f880ef9
MM
3845
3846 if (BASELINK_P (fn))
3847 postfix_expression
21526606
EC
3848 = (build_new_method_call
3849 (instance, fn, args, NULL_TREE,
3850 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
3851 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3852 else
3853 postfix_expression
3854 = finish_call_expr (postfix_expression, args,
3855 /*disallow_virtual=*/false,
3856 /*koenig_p=*/false);
a723baf1 3857 }
d17811fd
MM
3858 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3859 || TREE_CODE (postfix_expression) == MEMBER_REF
3860 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3861 postfix_expression = (build_offset_ref_call_from_tree
3862 (postfix_expression, args));
b3445994 3863 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3864 /* A call to a static class member, or a namespace-scope
3865 function. */
3866 postfix_expression
3867 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3868 /*disallow_virtual=*/true,
3869 koenig_p);
a723baf1 3870 else
2050a1bb 3871 /* All other function calls. */
21526606
EC
3872 postfix_expression
3873 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3874 /*disallow_virtual=*/false,
3875 koenig_p);
a723baf1
MM
3876
3877 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3878 idk = CP_ID_KIND_NONE;
a723baf1
MM
3879 }
3880 break;
21526606 3881
a723baf1
MM
3882 case CPP_DOT:
3883 case CPP_DEREF:
21526606
EC
3884 /* postfix-expression . template [opt] id-expression
3885 postfix-expression . pseudo-destructor-name
a723baf1
MM
3886 postfix-expression -> template [opt] id-expression
3887 postfix-expression -> pseudo-destructor-name */
3888 {
3889 tree name;
3890 bool dependent_p;
3891 bool template_p;
3892 tree scope = NULL_TREE;
a5ac3982 3893 enum cpp_ttype token_type = token->type;
a723baf1
MM
3894
3895 /* If this is a `->' operator, dereference the pointer. */
3896 if (token->type == CPP_DEREF)
3897 postfix_expression = build_x_arrow (postfix_expression);
3898 /* Check to see whether or not the expression is
3899 type-dependent. */
bbaab916 3900 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3901 /* The identifier following the `->' or `.' is not
3902 qualified. */
3903 parser->scope = NULL_TREE;
3904 parser->qualifying_scope = NULL_TREE;
3905 parser->object_scope = NULL_TREE;
b3445994 3906 idk = CP_ID_KIND_NONE;
a723baf1
MM
3907 /* Enter the scope corresponding to the type of the object
3908 given by the POSTFIX_EXPRESSION. */
21526606 3909 if (!dependent_p
a723baf1
MM
3910 && TREE_TYPE (postfix_expression) != NULL_TREE)
3911 {
3912 scope = TREE_TYPE (postfix_expression);
3913 /* According to the standard, no expression should
3914 ever have reference type. Unfortunately, we do not
3915 currently match the standard in this respect in
3916 that our internal representation of an expression
3917 may have reference type even when the standard says
3918 it does not. Therefore, we have to manually obtain
3919 the underlying type here. */
ee76b931 3920 scope = non_reference (scope);
a723baf1
MM
3921 /* The type of the POSTFIX_EXPRESSION must be
3922 complete. */
3923 scope = complete_type_or_else (scope, NULL_TREE);
3924 /* Let the name lookup machinery know that we are
3925 processing a class member access expression. */
3926 parser->context->object_type = scope;
3927 /* If something went wrong, we want to be able to
3928 discern that case, as opposed to the case where
3929 there was no SCOPE due to the type of expression
3930 being dependent. */
3931 if (!scope)
3932 scope = error_mark_node;
be799b1e
MM
3933 /* If the SCOPE was erroneous, make the various
3934 semantic analysis functions exit quickly -- and
3935 without issuing additional error messages. */
3936 if (scope == error_mark_node)
3937 postfix_expression = error_mark_node;
a723baf1
MM
3938 }
3939
3940 /* Consume the `.' or `->' operator. */
3941 cp_lexer_consume_token (parser->lexer);
3942 /* If the SCOPE is not a scalar type, we are looking at an
3943 ordinary class member access expression, rather than a
3944 pseudo-destructor-name. */
3945 if (!scope || !SCALAR_TYPE_P (scope))
3946 {
3947 template_p = cp_parser_optional_template_keyword (parser);
3948 /* Parse the id-expression. */
3949 name = cp_parser_id_expression (parser,
3950 template_p,
3951 /*check_dependency_p=*/true,
f3c2dfc6
MM
3952 /*template_p=*/NULL,
3953 /*declarator_p=*/false);
a723baf1
MM
3954 /* In general, build a SCOPE_REF if the member name is
3955 qualified. However, if the name was not dependent
3956 and has already been resolved; there is no need to
3957 build the SCOPE_REF. For example;
3958
3959 struct X { void f(); };
3960 template <typename T> void f(T* t) { t->X::f(); }
21526606 3961
d17811fd
MM
3962 Even though "t" is dependent, "X::f" is not and has
3963 been resolved to a BASELINK; there is no need to
a723baf1 3964 include scope information. */
a6bd211d
JM
3965
3966 /* But we do need to remember that there was an explicit
3967 scope for virtual function calls. */
3968 if (parser->scope)
b3445994 3969 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3970
21526606 3971 if (name != error_mark_node
a723baf1
MM
3972 && !BASELINK_P (name)
3973 && parser->scope)
3974 {
3975 name = build_nt (SCOPE_REF, parser->scope, name);
3976 parser->scope = NULL_TREE;
3977 parser->qualifying_scope = NULL_TREE;
3978 parser->object_scope = NULL_TREE;
3979 }
26bcf8fc
MM
3980 if (scope && name && BASELINK_P (name))
3981 adjust_result_of_qualified_name_lookup
3982 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
21526606 3983 postfix_expression
a723baf1
MM
3984 = finish_class_member_access_expr (postfix_expression, name);
3985 }
3986 /* Otherwise, try the pseudo-destructor-name production. */
3987 else
3988 {
90808894 3989 tree s = NULL_TREE;
a723baf1
MM
3990 tree type;
3991
3992 /* Parse the pseudo-destructor-name. */
3993 cp_parser_pseudo_destructor_name (parser, &s, &type);
3994 /* Form the call. */
21526606 3995 postfix_expression
a723baf1
MM
3996 = finish_pseudo_destructor_expr (postfix_expression,
3997 s, TREE_TYPE (type));
3998 }
3999
4000 /* We no longer need to look up names in the scope of the
4001 object on the left-hand side of the `.' or `->'
4002 operator. */
4003 parser->context->object_type = NULL_TREE;
a5ac3982 4004 /* These operators may not appear in constant-expressions. */
625cbf93 4005 if (/* The "->" operator is allowed in the implementation
643aee72
MM
4006 of "offsetof". The "." operator may appear in the
4007 name of the member. */
625cbf93
MM
4008 !parser->in_offsetof_p
4009 && (cp_parser_non_integral_constant_expression
4010 (parser,
4011 token_type == CPP_DEREF ? "'->'" : "`.'")))
4012 postfix_expression = error_mark_node;
a723baf1
MM
4013 }
4014 break;
4015
4016 case CPP_PLUS_PLUS:
4017 /* postfix-expression ++ */
4018 /* Consume the `++' token. */
4019 cp_lexer_consume_token (parser->lexer);
a5ac3982 4020 /* Generate a representation for the complete expression. */
21526606
EC
4021 postfix_expression
4022 = finish_increment_expr (postfix_expression,
a5ac3982 4023 POSTINCREMENT_EXPR);
14d22dd6 4024 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4025 if (cp_parser_non_integral_constant_expression (parser,
4026 "an increment"))
4027 postfix_expression = error_mark_node;
b3445994 4028 idk = CP_ID_KIND_NONE;
a723baf1
MM
4029 break;
4030
4031 case CPP_MINUS_MINUS:
4032 /* postfix-expression -- */
4033 /* Consume the `--' token. */
4034 cp_lexer_consume_token (parser->lexer);
a5ac3982 4035 /* Generate a representation for the complete expression. */
21526606
EC
4036 postfix_expression
4037 = finish_increment_expr (postfix_expression,
a5ac3982 4038 POSTDECREMENT_EXPR);
14d22dd6 4039 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4040 if (cp_parser_non_integral_constant_expression (parser,
4041 "a decrement"))
4042 postfix_expression = error_mark_node;
b3445994 4043 idk = CP_ID_KIND_NONE;
a723baf1
MM
4044 break;
4045
4046 default:
4047 return postfix_expression;
4048 }
4049 }
4050
4051 /* We should never get here. */
4052 abort ();
4053 return error_mark_node;
4054}
4055
7efa3e22 4056/* Parse a parenthesized expression-list.
a723baf1
MM
4057
4058 expression-list:
4059 assignment-expression
4060 expression-list, assignment-expression
4061
7efa3e22
NS
4062 attribute-list:
4063 expression-list
4064 identifier
4065 identifier, expression-list
4066
a723baf1
MM
4067 Returns a TREE_LIST. The TREE_VALUE of each node is a
4068 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4069 is returned even if there is only a single expression in the list.
4070 error_mark_node is returned if the ( and or ) are
4071 missing. NULL_TREE is returned on no expressions. The parentheses
4072 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4073 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4074 indicates whether or not all of the expressions in the list were
4075 constant. */
a723baf1
MM
4076
4077static tree
21526606 4078cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9
MM
4079 bool is_attribute_list,
4080 bool *non_constant_p)
a723baf1
MM
4081{
4082 tree expression_list = NULL_TREE;
7efa3e22 4083 tree identifier = NULL_TREE;
39703eb9
MM
4084
4085 /* Assume all the expressions will be constant. */
4086 if (non_constant_p)
4087 *non_constant_p = false;
4088
7efa3e22
NS
4089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4090 return error_mark_node;
21526606 4091
a723baf1 4092 /* Consume expressions until there are no more. */
7efa3e22
NS
4093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4094 while (true)
4095 {
4096 tree expr;
21526606 4097
7efa3e22
NS
4098 /* At the beginning of attribute lists, check to see if the
4099 next token is an identifier. */
4100 if (is_attribute_list
4101 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4102 {
4103 cp_token *token;
21526606 4104
7efa3e22
NS
4105 /* Consume the identifier. */
4106 token = cp_lexer_consume_token (parser->lexer);
4107 /* Save the identifier. */
4108 identifier = token->value;
4109 }
4110 else
4111 {
4112 /* Parse the next assignment-expression. */
39703eb9
MM
4113 if (non_constant_p)
4114 {
4115 bool expr_non_constant_p;
21526606 4116 expr = (cp_parser_constant_expression
39703eb9
MM
4117 (parser, /*allow_non_constant_p=*/true,
4118 &expr_non_constant_p));
4119 if (expr_non_constant_p)
4120 *non_constant_p = true;
4121 }
4122 else
4123 expr = cp_parser_assignment_expression (parser);
a723baf1 4124
7efa3e22
NS
4125 /* Add it to the list. We add error_mark_node
4126 expressions to the list, so that we can still tell if
4127 the correct form for a parenthesized expression-list
4128 is found. That gives better errors. */
4129 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4130
7efa3e22
NS
4131 if (expr == error_mark_node)
4132 goto skip_comma;
4133 }
a723baf1 4134
7efa3e22
NS
4135 /* After the first item, attribute lists look the same as
4136 expression lists. */
4137 is_attribute_list = false;
21526606 4138
7efa3e22
NS
4139 get_comma:;
4140 /* If the next token isn't a `,', then we are done. */
4141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4142 break;
4143
4144 /* Otherwise, consume the `,' and keep going. */
4145 cp_lexer_consume_token (parser->lexer);
4146 }
21526606 4147
7efa3e22
NS
4148 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4149 {
4150 int ending;
21526606 4151
7efa3e22
NS
4152 skip_comma:;
4153 /* We try and resync to an unnested comma, as that will give the
4154 user better diagnostics. */
21526606
EC
4155 ending = cp_parser_skip_to_closing_parenthesis (parser,
4156 /*recovering=*/true,
4bb8ca28 4157 /*or_comma=*/true,
a668c6ad 4158 /*consume_paren=*/true);
7efa3e22
NS
4159 if (ending < 0)
4160 goto get_comma;
4161 if (!ending)
4162 return error_mark_node;
a723baf1
MM
4163 }
4164
4165 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4166 expression_list = nreverse (expression_list);
4167 if (identifier)
4168 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4169
7efa3e22 4170 return expression_list;
a723baf1
MM
4171}
4172
4173/* Parse a pseudo-destructor-name.
4174
4175 pseudo-destructor-name:
4176 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4177 :: [opt] nested-name-specifier template template-id :: ~ type-name
4178 :: [opt] nested-name-specifier [opt] ~ type-name
4179
4180 If either of the first two productions is used, sets *SCOPE to the
4181 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4182 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4183 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4184
4185static void
21526606
EC
4186cp_parser_pseudo_destructor_name (cp_parser* parser,
4187 tree* scope,
94edc4ab 4188 tree* type)
a723baf1
MM
4189{
4190 bool nested_name_specifier_p;
4191
4192 /* Look for the optional `::' operator. */
4193 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4194 /* Look for the optional nested-name-specifier. */
21526606 4195 nested_name_specifier_p
a723baf1
MM
4196 = (cp_parser_nested_name_specifier_opt (parser,
4197 /*typename_keyword_p=*/false,
4198 /*check_dependency_p=*/true,
a668c6ad 4199 /*type_p=*/false,
21526606 4200 /*is_declaration=*/true)
a723baf1
MM
4201 != NULL_TREE);
4202 /* Now, if we saw a nested-name-specifier, we might be doing the
4203 second production. */
21526606 4204 if (nested_name_specifier_p
a723baf1
MM
4205 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4206 {
4207 /* Consume the `template' keyword. */
4208 cp_lexer_consume_token (parser->lexer);
4209 /* Parse the template-id. */
21526606 4210 cp_parser_template_id (parser,
a723baf1 4211 /*template_keyword_p=*/true,
a668c6ad
MM
4212 /*check_dependency_p=*/false,
4213 /*is_declaration=*/true);
a723baf1
MM
4214 /* Look for the `::' token. */
4215 cp_parser_require (parser, CPP_SCOPE, "`::'");
4216 }
4217 /* If the next token is not a `~', then there might be some
9bcb9aae 4218 additional qualification. */
a723baf1
MM
4219 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4220 {
4221 /* Look for the type-name. */
4222 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462
ILT
4223
4224 /* If we didn't get an aggregate type, or we don't have ::~,
4225 then something has gone wrong. Since the only caller of this
4226 function is looking for something after `.' or `->' after a
4227 scalar type, most likely the program is trying to get a
4228 member of a non-aggregate type. */
4229 if (*scope == error_mark_node
4230 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4231 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4232 {
4233 cp_parser_error (parser, "request for member of non-aggregate type");
4234 *type = error_mark_node;
4235 return;
4236 }
4237
a723baf1
MM
4238 /* Look for the `::' token. */
4239 cp_parser_require (parser, CPP_SCOPE, "`::'");
4240 }
4241 else
4242 *scope = NULL_TREE;
4243
4244 /* Look for the `~'. */
4245 cp_parser_require (parser, CPP_COMPL, "`~'");
4246 /* Look for the type-name again. We are not responsible for
4247 checking that it matches the first type-name. */
4248 *type = cp_parser_type_name (parser);
4249}
4250
4251/* Parse a unary-expression.
4252
4253 unary-expression:
4254 postfix-expression
4255 ++ cast-expression
4256 -- cast-expression
4257 unary-operator cast-expression
4258 sizeof unary-expression
4259 sizeof ( type-id )
4260 new-expression
4261 delete-expression
4262
4263 GNU Extensions:
4264
4265 unary-expression:
4266 __extension__ cast-expression
4267 __alignof__ unary-expression
4268 __alignof__ ( type-id )
4269 __real__ cast-expression
4270 __imag__ cast-expression
4271 && identifier
4272
4273 ADDRESS_P is true iff the unary-expression is appearing as the
4274 operand of the `&' operator.
4275
34cd5ae7 4276 Returns a representation of the expression. */
a723baf1
MM
4277
4278static tree
4279cp_parser_unary_expression (cp_parser *parser, bool address_p)
4280{
4281 cp_token *token;
4282 enum tree_code unary_operator;
4283
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 /* Some keywords give away the kind of expression. */
4287 if (token->type == CPP_KEYWORD)
4288 {
4289 enum rid keyword = token->keyword;
4290
4291 switch (keyword)
4292 {
4293 case RID_ALIGNOF:
a723baf1
MM
4294 case RID_SIZEOF:
4295 {
4296 tree operand;
7a18b933 4297 enum tree_code op;
21526606 4298
7a18b933
NS
4299 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4300 /* Consume the token. */
a723baf1
MM
4301 cp_lexer_consume_token (parser->lexer);
4302 /* Parse the operand. */
4303 operand = cp_parser_sizeof_operand (parser, keyword);
4304
7a18b933
NS
4305 if (TYPE_P (operand))
4306 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4307 else
7a18b933 4308 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4309 }
4310
4311 case RID_NEW:
4312 return cp_parser_new_expression (parser);
4313
4314 case RID_DELETE:
4315 return cp_parser_delete_expression (parser);
21526606 4316
a723baf1
MM
4317 case RID_EXTENSION:
4318 {
4319 /* The saved value of the PEDANTIC flag. */
4320 int saved_pedantic;
4321 tree expr;
4322
4323 /* Save away the PEDANTIC flag. */
4324 cp_parser_extension_opt (parser, &saved_pedantic);
4325 /* Parse the cast-expression. */
d6b4ea85 4326 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4327 /* Restore the PEDANTIC flag. */
4328 pedantic = saved_pedantic;
4329
4330 return expr;
4331 }
4332
4333 case RID_REALPART:
4334 case RID_IMAGPART:
4335 {
4336 tree expression;
4337
4338 /* Consume the `__real__' or `__imag__' token. */
4339 cp_lexer_consume_token (parser->lexer);
4340 /* Parse the cast-expression. */
d6b4ea85 4341 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4342 /* Create the complete representation. */
4343 return build_x_unary_op ((keyword == RID_REALPART
4344 ? REALPART_EXPR : IMAGPART_EXPR),
4345 expression);
4346 }
4347 break;
4348
4349 default:
4350 break;
4351 }
4352 }
4353
4354 /* Look for the `:: new' and `:: delete', which also signal the
4355 beginning of a new-expression, or delete-expression,
4356 respectively. If the next token is `::', then it might be one of
4357 these. */
4358 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4359 {
4360 enum rid keyword;
4361
4362 /* See if the token after the `::' is one of the keywords in
4363 which we're interested. */
4364 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4365 /* If it's `new', we have a new-expression. */
4366 if (keyword == RID_NEW)
4367 return cp_parser_new_expression (parser);
4368 /* Similarly, for `delete'. */
4369 else if (keyword == RID_DELETE)
4370 return cp_parser_delete_expression (parser);
4371 }
4372
4373 /* Look for a unary operator. */
4374 unary_operator = cp_parser_unary_operator (token);
4375 /* The `++' and `--' operators can be handled similarly, even though
4376 they are not technically unary-operators in the grammar. */
4377 if (unary_operator == ERROR_MARK)
4378 {
4379 if (token->type == CPP_PLUS_PLUS)
4380 unary_operator = PREINCREMENT_EXPR;
4381 else if (token->type == CPP_MINUS_MINUS)
4382 unary_operator = PREDECREMENT_EXPR;
4383 /* Handle the GNU address-of-label extension. */
4384 else if (cp_parser_allow_gnu_extensions_p (parser)
4385 && token->type == CPP_AND_AND)
4386 {
4387 tree identifier;
4388
4389 /* Consume the '&&' token. */
4390 cp_lexer_consume_token (parser->lexer);
4391 /* Look for the identifier. */
4392 identifier = cp_parser_identifier (parser);
4393 /* Create an expression representing the address. */
4394 return finish_label_address_expr (identifier);
4395 }
4396 }
4397 if (unary_operator != ERROR_MARK)
4398 {
4399 tree cast_expression;
a5ac3982
MM
4400 tree expression = error_mark_node;
4401 const char *non_constant_p = NULL;
a723baf1
MM
4402
4403 /* Consume the operator token. */
4404 token = cp_lexer_consume_token (parser->lexer);
4405 /* Parse the cast-expression. */
21526606 4406 cast_expression
a723baf1
MM
4407 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4408 /* Now, build an appropriate representation. */
4409 switch (unary_operator)
4410 {
4411 case INDIRECT_REF:
a5ac3982
MM
4412 non_constant_p = "`*'";
4413 expression = build_x_indirect_ref (cast_expression, "unary *");
4414 break;
4415
a723baf1 4416 case ADDR_EXPR:
263ee052
MM
4417 /* The "&" operator is allowed in the implementation of
4418 "offsetof". */
4419 if (!parser->in_offsetof_p)
4420 non_constant_p = "`&'";
a5ac3982 4421 /* Fall through. */
d17811fd 4422 case BIT_NOT_EXPR:
a5ac3982
MM
4423 expression = build_x_unary_op (unary_operator, cast_expression);
4424 break;
4425
14d22dd6
MM
4426 case PREINCREMENT_EXPR:
4427 case PREDECREMENT_EXPR:
a5ac3982
MM
4428 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4429 ? "`++'" : "`--'");
14d22dd6 4430 /* Fall through. */
a723baf1
MM
4431 case CONVERT_EXPR:
4432 case NEGATE_EXPR:
4433 case TRUTH_NOT_EXPR:
a5ac3982
MM
4434 expression = finish_unary_op_expr (unary_operator, cast_expression);
4435 break;
a723baf1 4436
a723baf1
MM
4437 default:
4438 abort ();
a723baf1 4439 }
a5ac3982 4440
625cbf93
MM
4441 if (non_constant_p
4442 && cp_parser_non_integral_constant_expression (parser,
4443 non_constant_p))
4444 expression = error_mark_node;
a5ac3982
MM
4445
4446 return expression;
a723baf1
MM
4447 }
4448
4449 return cp_parser_postfix_expression (parser, address_p);
4450}
4451
4452/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4453 unary-operator, the corresponding tree code is returned. */
4454
4455static enum tree_code
94edc4ab 4456cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4457{
4458 switch (token->type)
4459 {
4460 case CPP_MULT:
4461 return INDIRECT_REF;
4462
4463 case CPP_AND:
4464 return ADDR_EXPR;
4465
4466 case CPP_PLUS:
4467 return CONVERT_EXPR;
4468
4469 case CPP_MINUS:
4470 return NEGATE_EXPR;
4471
4472 case CPP_NOT:
4473 return TRUTH_NOT_EXPR;
21526606 4474
a723baf1
MM
4475 case CPP_COMPL:
4476 return BIT_NOT_EXPR;
4477
4478 default:
4479 return ERROR_MARK;
4480 }
4481}
4482
4483/* Parse a new-expression.
4484
ca099ac8 4485 new-expression:
a723baf1
MM
4486 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4487 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4488
4489 Returns a representation of the expression. */
4490
4491static tree
94edc4ab 4492cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4493{
4494 bool global_scope_p;
4495 tree placement;
4496 tree type;
4497 tree initializer;
4498
4499 /* Look for the optional `::' operator. */
21526606 4500 global_scope_p
a723baf1
MM
4501 = (cp_parser_global_scope_opt (parser,
4502 /*current_scope_valid_p=*/false)
4503 != NULL_TREE);
4504 /* Look for the `new' operator. */
4505 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4506 /* There's no easy way to tell a new-placement from the
4507 `( type-id )' construct. */
4508 cp_parser_parse_tentatively (parser);
4509 /* Look for a new-placement. */
4510 placement = cp_parser_new_placement (parser);
4511 /* If that didn't work out, there's no new-placement. */
4512 if (!cp_parser_parse_definitely (parser))
4513 placement = NULL_TREE;
4514
4515 /* If the next token is a `(', then we have a parenthesized
4516 type-id. */
4517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4518 {
4519 /* Consume the `('. */
4520 cp_lexer_consume_token (parser->lexer);
4521 /* Parse the type-id. */
4522 type = cp_parser_type_id (parser);
4523 /* Look for the closing `)'. */
4524 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
063e900f
GB
4525 /* There should not be a direct-new-declarator in this production,
4526 but GCC used to allowed this, so we check and emit a sensible error
4527 message for this case. */
4528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4529 {
4530 error ("array bound forbidden after parenthesized type-id");
4531 inform ("try removing the parentheses around the type-id");
063e900f
GB
4532 cp_parser_direct_new_declarator (parser);
4533 }
a723baf1
MM
4534 }
4535 /* Otherwise, there must be a new-type-id. */
4536 else
4537 type = cp_parser_new_type_id (parser);
4538
4539 /* If the next token is a `(', then we have a new-initializer. */
4540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4541 initializer = cp_parser_new_initializer (parser);
4542 else
4543 initializer = NULL_TREE;
4544
625cbf93
MM
4545 /* A new-expression may not appear in an integral constant
4546 expression. */
4547 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4548 return error_mark_node;
4549
a723baf1
MM
4550 /* Create a representation of the new-expression. */
4551 return build_new (placement, type, initializer, global_scope_p);
4552}
4553
4554/* Parse a new-placement.
4555
4556 new-placement:
4557 ( expression-list )
4558
4559 Returns the same representation as for an expression-list. */
4560
4561static tree
94edc4ab 4562cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4563{
4564 tree expression_list;
4565
a723baf1 4566 /* Parse the expression-list. */
21526606 4567 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4568 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4569
4570 return expression_list;
4571}
4572
4573/* Parse a new-type-id.
4574
4575 new-type-id:
4576 type-specifier-seq new-declarator [opt]
4577
4578 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4579 and whose TREE_VALUE is the new-declarator. */
4580
4581static tree
94edc4ab 4582cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4583{
4584 tree type_specifier_seq;
4585 tree declarator;
4586 const char *saved_message;
4587
4588 /* The type-specifier sequence must not contain type definitions.
4589 (It cannot contain declarations of new types either, but if they
4590 are not definitions we will catch that because they are not
4591 complete.) */
4592 saved_message = parser->type_definition_forbidden_message;
4593 parser->type_definition_forbidden_message
4594 = "types may not be defined in a new-type-id";
4595 /* Parse the type-specifier-seq. */
4596 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4597 /* Restore the old message. */
4598 parser->type_definition_forbidden_message = saved_message;
4599 /* Parse the new-declarator. */
4600 declarator = cp_parser_new_declarator_opt (parser);
4601
4602 return build_tree_list (type_specifier_seq, declarator);
4603}
4604
4605/* Parse an (optional) new-declarator.
4606
4607 new-declarator:
4608 ptr-operator new-declarator [opt]
4609 direct-new-declarator
4610
4611 Returns a representation of the declarator. See
4612 cp_parser_declarator for the representations used. */
4613
4614static tree
94edc4ab 4615cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4616{
4617 enum tree_code code;
4618 tree type;
4619 tree cv_qualifier_seq;
4620
4621 /* We don't know if there's a ptr-operator next, or not. */
4622 cp_parser_parse_tentatively (parser);
4623 /* Look for a ptr-operator. */
4624 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4625 /* If that worked, look for more new-declarators. */
4626 if (cp_parser_parse_definitely (parser))
4627 {
4628 tree declarator;
4629
4630 /* Parse another optional declarator. */
4631 declarator = cp_parser_new_declarator_opt (parser);
4632
4633 /* Create the representation of the declarator. */
4634 if (code == INDIRECT_REF)
4635 declarator = make_pointer_declarator (cv_qualifier_seq,
4636 declarator);
4637 else
4638 declarator = make_reference_declarator (cv_qualifier_seq,
4639 declarator);
4640
4641 /* Handle the pointer-to-member case. */
4642 if (type)
4643 declarator = build_nt (SCOPE_REF, type, declarator);
4644
4645 return declarator;
4646 }
4647
4648 /* If the next token is a `[', there is a direct-new-declarator. */
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4650 return cp_parser_direct_new_declarator (parser);
4651
4652 return NULL_TREE;
4653}
4654
4655/* Parse a direct-new-declarator.
4656
4657 direct-new-declarator:
4658 [ expression ]
21526606 4659 direct-new-declarator [constant-expression]
a723baf1
MM
4660
4661 Returns an ARRAY_REF, following the same conventions as are
4662 documented for cp_parser_direct_declarator. */
4663
4664static tree
94edc4ab 4665cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4666{
4667 tree declarator = NULL_TREE;
4668
4669 while (true)
4670 {
4671 tree expression;
4672
4673 /* Look for the opening `['. */
4674 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4675 /* The first expression is not required to be constant. */
4676 if (!declarator)
4677 {
4678 expression = cp_parser_expression (parser);
4679 /* The standard requires that the expression have integral
4680 type. DR 74 adds enumeration types. We believe that the
4681 real intent is that these expressions be handled like the
4682 expression in a `switch' condition, which also allows
4683 classes with a single conversion to integral or
4684 enumeration type. */
4685 if (!processing_template_decl)
4686 {
21526606 4687 expression
a723baf1
MM
4688 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4689 expression,
b746c5dc 4690 /*complain=*/true);
a723baf1
MM
4691 if (!expression)
4692 {
4693 error ("expression in new-declarator must have integral or enumeration type");
4694 expression = error_mark_node;
4695 }
4696 }
4697 }
4698 /* But all the other expressions must be. */
4699 else
21526606
EC
4700 expression
4701 = cp_parser_constant_expression (parser,
14d22dd6
MM
4702 /*allow_non_constant=*/false,
4703 NULL);
a723baf1
MM
4704 /* Look for the closing `]'. */
4705 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4706
4707 /* Add this bound to the declarator. */
4708 declarator = build_nt (ARRAY_REF, declarator, expression);
4709
4710 /* If the next token is not a `[', then there are no more
4711 bounds. */
4712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4713 break;
4714 }
4715
4716 return declarator;
4717}
4718
4719/* Parse a new-initializer.
4720
4721 new-initializer:
4722 ( expression-list [opt] )
4723
34cd5ae7 4724 Returns a representation of the expression-list. If there is no
a723baf1
MM
4725 expression-list, VOID_ZERO_NODE is returned. */
4726
4727static tree
94edc4ab 4728cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4729{
4730 tree expression_list;
4731
21526606 4732 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4733 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4734 if (!expression_list)
a723baf1 4735 expression_list = void_zero_node;
a723baf1
MM
4736
4737 return expression_list;
4738}
4739
4740/* Parse a delete-expression.
4741
4742 delete-expression:
4743 :: [opt] delete cast-expression
4744 :: [opt] delete [ ] cast-expression
4745
4746 Returns a representation of the expression. */
4747
4748static tree
94edc4ab 4749cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4750{
4751 bool global_scope_p;
4752 bool array_p;
4753 tree expression;
4754
4755 /* Look for the optional `::' operator. */
4756 global_scope_p
4757 = (cp_parser_global_scope_opt (parser,
4758 /*current_scope_valid_p=*/false)
4759 != NULL_TREE);
4760 /* Look for the `delete' keyword. */
4761 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4762 /* See if the array syntax is in use. */
4763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4764 {
4765 /* Consume the `[' token. */
4766 cp_lexer_consume_token (parser->lexer);
4767 /* Look for the `]' token. */
4768 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4769 /* Remember that this is the `[]' construct. */
4770 array_p = true;
4771 }
4772 else
4773 array_p = false;
4774
4775 /* Parse the cast-expression. */
d6b4ea85 4776 expression = cp_parser_simple_cast_expression (parser);
a723baf1 4777
625cbf93
MM
4778 /* A delete-expression may not appear in an integral constant
4779 expression. */
4780 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4781 return error_mark_node;
4782
a723baf1
MM
4783 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4784}
4785
4786/* Parse a cast-expression.
4787
4788 cast-expression:
4789 unary-expression
4790 ( type-id ) cast-expression
4791
4792 Returns a representation of the expression. */
4793
4794static tree
4795cp_parser_cast_expression (cp_parser *parser, bool address_p)
4796{
4797 /* If it's a `(', then we might be looking at a cast. */
4798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4799 {
4800 tree type = NULL_TREE;
4801 tree expr = NULL_TREE;
4802 bool compound_literal_p;
4803 const char *saved_message;
4804
4805 /* There's no way to know yet whether or not this is a cast.
4806 For example, `(int (3))' is a unary-expression, while `(int)
4807 3' is a cast. So, we resort to parsing tentatively. */
4808 cp_parser_parse_tentatively (parser);
4809 /* Types may not be defined in a cast. */
4810 saved_message = parser->type_definition_forbidden_message;
4811 parser->type_definition_forbidden_message
4812 = "types may not be defined in casts";
4813 /* Consume the `('. */
4814 cp_lexer_consume_token (parser->lexer);
4815 /* A very tricky bit is that `(struct S) { 3 }' is a
4816 compound-literal (which we permit in C++ as an extension).
4817 But, that construct is not a cast-expression -- it is a
4818 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4819 is legal; if the compound-literal were a cast-expression,
4820 you'd need an extra set of parentheses.) But, if we parse
4821 the type-id, and it happens to be a class-specifier, then we
4822 will commit to the parse at that point, because we cannot
4823 undo the action that is done when creating a new class. So,
21526606 4824 then we cannot back up and do a postfix-expression.
a723baf1
MM
4825
4826 Therefore, we scan ahead to the closing `)', and check to see
4827 if the token after the `)' is a `{'. If so, we are not
21526606 4828 looking at a cast-expression.
a723baf1
MM
4829
4830 Save tokens so that we can put them back. */
4831 cp_lexer_save_tokens (parser->lexer);
4832 /* Skip tokens until the next token is a closing parenthesis.
4833 If we find the closing `)', and the next token is a `{', then
4834 we are looking at a compound-literal. */
21526606 4835 compound_literal_p
a668c6ad
MM
4836 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4837 /*consume_paren=*/true)
a723baf1
MM
4838 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4839 /* Roll back the tokens we skipped. */
4840 cp_lexer_rollback_tokens (parser->lexer);
4841 /* If we were looking at a compound-literal, simulate an error
4842 so that the call to cp_parser_parse_definitely below will
4843 fail. */
4844 if (compound_literal_p)
4845 cp_parser_simulate_error (parser);
4846 else
4847 {
4f8163b1
MM
4848 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4849 parser->in_type_id_in_expr_p = true;
a723baf1
MM
4850 /* Look for the type-id. */
4851 type = cp_parser_type_id (parser);
4852 /* Look for the closing `)'. */
4853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 4854 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
4855 }
4856
4857 /* Restore the saved message. */
4858 parser->type_definition_forbidden_message = saved_message;
4859
bbaab916
NS
4860 /* If ok so far, parse the dependent expression. We cannot be
4861 sure it is a cast. Consider `(T ())'. It is a parenthesized
4862 ctor of T, but looks like a cast to function returning T
4863 without a dependent expression. */
4864 if (!cp_parser_error_occurred (parser))
d6b4ea85 4865 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4866
a723baf1
MM
4867 if (cp_parser_parse_definitely (parser))
4868 {
a723baf1 4869 /* Warn about old-style casts, if so requested. */
21526606
EC
4870 if (warn_old_style_cast
4871 && !in_system_header
4872 && !VOID_TYPE_P (type)
a723baf1
MM
4873 && current_lang_name != lang_name_c)
4874 warning ("use of old-style cast");
14d22dd6
MM
4875
4876 /* Only type conversions to integral or enumeration types
4877 can be used in constant-expressions. */
67c03833 4878 if (parser->integral_constant_expression_p
14d22dd6 4879 && !dependent_type_p (type)
625cbf93
MM
4880 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4881 && (cp_parser_non_integral_constant_expression
4882 (parser,
4883 "a cast to a type other than an integral or "
4884 "enumeration type")))
4885 return error_mark_node;
4886
a723baf1
MM
4887 /* Perform the cast. */
4888 expr = build_c_cast (type, expr);
bbaab916 4889 return expr;
a723baf1 4890 }
a723baf1
MM
4891 }
4892
4893 /* If we get here, then it's not a cast, so it must be a
4894 unary-expression. */
4895 return cp_parser_unary_expression (parser, address_p);
4896}
4897
4898/* Parse a pm-expression.
4899
4900 pm-expression:
4901 cast-expression
4902 pm-expression .* cast-expression
4903 pm-expression ->* cast-expression
4904
4905 Returns a representation of the expression. */
4906
4907static tree
94edc4ab 4908cp_parser_pm_expression (cp_parser* parser)
a723baf1 4909{
d6b4ea85
MM
4910 static const cp_parser_token_tree_map map = {
4911 { CPP_DEREF_STAR, MEMBER_REF },
4912 { CPP_DOT_STAR, DOTSTAR_EXPR },
4913 { CPP_EOF, ERROR_MARK }
4914 };
a723baf1 4915
21526606 4916 return cp_parser_binary_expression (parser, map,
d6b4ea85 4917 cp_parser_simple_cast_expression);
a723baf1
MM
4918}
4919
4920/* Parse a multiplicative-expression.
4921
77077b39 4922 multiplicative-expression:
a723baf1
MM
4923 pm-expression
4924 multiplicative-expression * pm-expression
4925 multiplicative-expression / pm-expression
4926 multiplicative-expression % pm-expression
4927
4928 Returns a representation of the expression. */
4929
4930static tree
94edc4ab 4931cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4932{
39b1af70 4933 static const cp_parser_token_tree_map map = {
a723baf1
MM
4934 { CPP_MULT, MULT_EXPR },
4935 { CPP_DIV, TRUNC_DIV_EXPR },
4936 { CPP_MOD, TRUNC_MOD_EXPR },
4937 { CPP_EOF, ERROR_MARK }
4938 };
4939
4940 return cp_parser_binary_expression (parser,
4941 map,
4942 cp_parser_pm_expression);
4943}
4944
4945/* Parse an additive-expression.
4946
4947 additive-expression:
4948 multiplicative-expression
4949 additive-expression + multiplicative-expression
4950 additive-expression - multiplicative-expression
4951
4952 Returns a representation of the expression. */
4953
4954static tree
94edc4ab 4955cp_parser_additive_expression (cp_parser* parser)
a723baf1 4956{
39b1af70 4957 static const cp_parser_token_tree_map map = {
a723baf1
MM
4958 { CPP_PLUS, PLUS_EXPR },
4959 { CPP_MINUS, MINUS_EXPR },
4960 { CPP_EOF, ERROR_MARK }
4961 };
4962
4963 return cp_parser_binary_expression (parser,
4964 map,
4965 cp_parser_multiplicative_expression);
4966}
4967
4968/* Parse a shift-expression.
4969
4970 shift-expression:
4971 additive-expression
4972 shift-expression << additive-expression
4973 shift-expression >> additive-expression
4974
4975 Returns a representation of the expression. */
4976
4977static tree
94edc4ab 4978cp_parser_shift_expression (cp_parser* parser)
a723baf1 4979{
39b1af70 4980 static const cp_parser_token_tree_map map = {
a723baf1
MM
4981 { CPP_LSHIFT, LSHIFT_EXPR },
4982 { CPP_RSHIFT, RSHIFT_EXPR },
4983 { CPP_EOF, ERROR_MARK }
4984 };
4985
4986 return cp_parser_binary_expression (parser,
4987 map,
4988 cp_parser_additive_expression);
4989}
4990
4991/* Parse a relational-expression.
4992
4993 relational-expression:
4994 shift-expression
4995 relational-expression < shift-expression
4996 relational-expression > shift-expression
4997 relational-expression <= shift-expression
4998 relational-expression >= shift-expression
4999
5000 GNU Extension:
5001
5002 relational-expression:
5003 relational-expression <? shift-expression
5004 relational-expression >? shift-expression
5005
5006 Returns a representation of the expression. */
5007
5008static tree
94edc4ab 5009cp_parser_relational_expression (cp_parser* parser)
a723baf1 5010{
39b1af70 5011 static const cp_parser_token_tree_map map = {
a723baf1
MM
5012 { CPP_LESS, LT_EXPR },
5013 { CPP_GREATER, GT_EXPR },
5014 { CPP_LESS_EQ, LE_EXPR },
5015 { CPP_GREATER_EQ, GE_EXPR },
5016 { CPP_MIN, MIN_EXPR },
5017 { CPP_MAX, MAX_EXPR },
5018 { CPP_EOF, ERROR_MARK }
5019 };
5020
5021 return cp_parser_binary_expression (parser,
5022 map,
5023 cp_parser_shift_expression);
5024}
5025
5026/* Parse an equality-expression.
5027
5028 equality-expression:
5029 relational-expression
5030 equality-expression == relational-expression
5031 equality-expression != relational-expression
5032
5033 Returns a representation of the expression. */
5034
5035static tree
94edc4ab 5036cp_parser_equality_expression (cp_parser* parser)
a723baf1 5037{
39b1af70 5038 static const cp_parser_token_tree_map map = {
a723baf1
MM
5039 { CPP_EQ_EQ, EQ_EXPR },
5040 { CPP_NOT_EQ, NE_EXPR },
5041 { CPP_EOF, ERROR_MARK }
5042 };
5043
5044 return cp_parser_binary_expression (parser,
5045 map,
5046 cp_parser_relational_expression);
5047}
5048
5049/* Parse an and-expression.
5050
5051 and-expression:
5052 equality-expression
5053 and-expression & equality-expression
5054
5055 Returns a representation of the expression. */
5056
5057static tree
94edc4ab 5058cp_parser_and_expression (cp_parser* parser)
a723baf1 5059{
39b1af70 5060 static const cp_parser_token_tree_map map = {
a723baf1
MM
5061 { CPP_AND, BIT_AND_EXPR },
5062 { CPP_EOF, ERROR_MARK }
5063 };
5064
5065 return cp_parser_binary_expression (parser,
5066 map,
5067 cp_parser_equality_expression);
5068}
5069
5070/* Parse an exclusive-or-expression.
5071
5072 exclusive-or-expression:
5073 and-expression
5074 exclusive-or-expression ^ and-expression
5075
5076 Returns a representation of the expression. */
5077
5078static tree
94edc4ab 5079cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 5080{
39b1af70 5081 static const cp_parser_token_tree_map map = {
a723baf1
MM
5082 { CPP_XOR, BIT_XOR_EXPR },
5083 { CPP_EOF, ERROR_MARK }
5084 };
5085
5086 return cp_parser_binary_expression (parser,
5087 map,
5088 cp_parser_and_expression);
5089}
5090
5091
5092/* Parse an inclusive-or-expression.
5093
5094 inclusive-or-expression:
5095 exclusive-or-expression
5096 inclusive-or-expression | exclusive-or-expression
5097
5098 Returns a representation of the expression. */
5099
5100static tree
94edc4ab 5101cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 5102{
39b1af70 5103 static const cp_parser_token_tree_map map = {
a723baf1
MM
5104 { CPP_OR, BIT_IOR_EXPR },
5105 { CPP_EOF, ERROR_MARK }
5106 };
5107
5108 return cp_parser_binary_expression (parser,
5109 map,
5110 cp_parser_exclusive_or_expression);
5111}
5112
5113/* Parse a logical-and-expression.
5114
5115 logical-and-expression:
5116 inclusive-or-expression
5117 logical-and-expression && inclusive-or-expression
5118
5119 Returns a representation of the expression. */
5120
5121static tree
94edc4ab 5122cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 5123{
39b1af70 5124 static const cp_parser_token_tree_map map = {
a723baf1
MM
5125 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5126 { CPP_EOF, ERROR_MARK }
5127 };
5128
5129 return cp_parser_binary_expression (parser,
5130 map,
5131 cp_parser_inclusive_or_expression);
5132}
5133
5134/* Parse a logical-or-expression.
5135
5136 logical-or-expression:
34cd5ae7 5137 logical-and-expression
a723baf1
MM
5138 logical-or-expression || logical-and-expression
5139
5140 Returns a representation of the expression. */
5141
5142static tree
94edc4ab 5143cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5144{
39b1af70 5145 static const cp_parser_token_tree_map map = {
a723baf1
MM
5146 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5147 { CPP_EOF, ERROR_MARK }
5148 };
5149
5150 return cp_parser_binary_expression (parser,
5151 map,
5152 cp_parser_logical_and_expression);
5153}
5154
a723baf1
MM
5155/* Parse the `? expression : assignment-expression' part of a
5156 conditional-expression. The LOGICAL_OR_EXPR is the
5157 logical-or-expression that started the conditional-expression.
5158 Returns a representation of the entire conditional-expression.
5159
39703eb9 5160 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5161
5162 ? expression : assignment-expression
21526606 5163
a723baf1 5164 GNU Extensions:
21526606 5165
a723baf1
MM
5166 ? : assignment-expression */
5167
5168static tree
94edc4ab 5169cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5170{
5171 tree expr;
5172 tree assignment_expr;
5173
5174 /* Consume the `?' token. */
5175 cp_lexer_consume_token (parser->lexer);
5176 if (cp_parser_allow_gnu_extensions_p (parser)
5177 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5178 /* Implicit true clause. */
5179 expr = NULL_TREE;
5180 else
5181 /* Parse the expression. */
5182 expr = cp_parser_expression (parser);
21526606 5183
a723baf1
MM
5184 /* The next token should be a `:'. */
5185 cp_parser_require (parser, CPP_COLON, "`:'");
5186 /* Parse the assignment-expression. */
5187 assignment_expr = cp_parser_assignment_expression (parser);
5188
5189 /* Build the conditional-expression. */
5190 return build_x_conditional_expr (logical_or_expr,
5191 expr,
5192 assignment_expr);
5193}
5194
5195/* Parse an assignment-expression.
5196
5197 assignment-expression:
5198 conditional-expression
5199 logical-or-expression assignment-operator assignment_expression
5200 throw-expression
5201
5202 Returns a representation for the expression. */
5203
5204static tree
94edc4ab 5205cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5206{
5207 tree expr;
5208
5209 /* If the next token is the `throw' keyword, then we're looking at
5210 a throw-expression. */
5211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5212 expr = cp_parser_throw_expression (parser);
5213 /* Otherwise, it must be that we are looking at a
5214 logical-or-expression. */
5215 else
5216 {
5217 /* Parse the logical-or-expression. */
5218 expr = cp_parser_logical_or_expression (parser);
5219 /* If the next token is a `?' then we're actually looking at a
5220 conditional-expression. */
5221 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5222 return cp_parser_question_colon_clause (parser, expr);
21526606 5223 else
a723baf1
MM
5224 {
5225 enum tree_code assignment_operator;
5226
5227 /* If it's an assignment-operator, we're using the second
5228 production. */
21526606 5229 assignment_operator
a723baf1
MM
5230 = cp_parser_assignment_operator_opt (parser);
5231 if (assignment_operator != ERROR_MARK)
5232 {
5233 tree rhs;
5234
5235 /* Parse the right-hand side of the assignment. */
5236 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5237 /* An assignment may not appear in a
5238 constant-expression. */
625cbf93
MM
5239 if (cp_parser_non_integral_constant_expression (parser,
5240 "an assignment"))
5241 return error_mark_node;
34cd5ae7 5242 /* Build the assignment expression. */
21526606
EC
5243 expr = build_x_modify_expr (expr,
5244 assignment_operator,
a723baf1
MM
5245 rhs);
5246 }
5247 }
5248 }
5249
5250 return expr;
5251}
5252
5253/* Parse an (optional) assignment-operator.
5254
21526606
EC
5255 assignment-operator: one of
5256 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5257
5258 GNU Extension:
21526606 5259
a723baf1
MM
5260 assignment-operator: one of
5261 <?= >?=
5262
5263 If the next token is an assignment operator, the corresponding tree
5264 code is returned, and the token is consumed. For example, for
5265 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5266 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5267 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5268 operator, ERROR_MARK is returned. */
5269
5270static enum tree_code
94edc4ab 5271cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5272{
5273 enum tree_code op;
5274 cp_token *token;
5275
5276 /* Peek at the next toen. */
5277 token = cp_lexer_peek_token (parser->lexer);
5278
5279 switch (token->type)
5280 {
5281 case CPP_EQ:
5282 op = NOP_EXPR;
5283 break;
5284
5285 case CPP_MULT_EQ:
5286 op = MULT_EXPR;
5287 break;
5288
5289 case CPP_DIV_EQ:
5290 op = TRUNC_DIV_EXPR;
5291 break;
5292
5293 case CPP_MOD_EQ:
5294 op = TRUNC_MOD_EXPR;
5295 break;
5296
5297 case CPP_PLUS_EQ:
5298 op = PLUS_EXPR;
5299 break;
5300
5301 case CPP_MINUS_EQ:
5302 op = MINUS_EXPR;
5303 break;
5304
5305 case CPP_RSHIFT_EQ:
5306 op = RSHIFT_EXPR;
5307 break;
5308
5309 case CPP_LSHIFT_EQ:
5310 op = LSHIFT_EXPR;
5311 break;
5312
5313 case CPP_AND_EQ:
5314 op = BIT_AND_EXPR;
5315 break;
5316
5317 case CPP_XOR_EQ:
5318 op = BIT_XOR_EXPR;
5319 break;
5320
5321 case CPP_OR_EQ:
5322 op = BIT_IOR_EXPR;
5323 break;
5324
5325 case CPP_MIN_EQ:
5326 op = MIN_EXPR;
5327 break;
5328
5329 case CPP_MAX_EQ:
5330 op = MAX_EXPR;
5331 break;
5332
21526606 5333 default:
a723baf1
MM
5334 /* Nothing else is an assignment operator. */
5335 op = ERROR_MARK;
5336 }
5337
5338 /* If it was an assignment operator, consume it. */
5339 if (op != ERROR_MARK)
5340 cp_lexer_consume_token (parser->lexer);
5341
5342 return op;
5343}
5344
5345/* Parse an expression.
5346
5347 expression:
5348 assignment-expression
5349 expression , assignment-expression
5350
5351 Returns a representation of the expression. */
5352
5353static tree
94edc4ab 5354cp_parser_expression (cp_parser* parser)
a723baf1
MM
5355{
5356 tree expression = NULL_TREE;
a723baf1
MM
5357
5358 while (true)
5359 {
5360 tree assignment_expression;
5361
5362 /* Parse the next assignment-expression. */
21526606 5363 assignment_expression
a723baf1
MM
5364 = cp_parser_assignment_expression (parser);
5365 /* If this is the first assignment-expression, we can just
5366 save it away. */
5367 if (!expression)
5368 expression = assignment_expression;
a723baf1 5369 else
d17811fd
MM
5370 expression = build_x_compound_expr (expression,
5371 assignment_expression);
a723baf1
MM
5372 /* If the next token is not a comma, then we are done with the
5373 expression. */
5374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5375 break;
5376 /* Consume the `,'. */
5377 cp_lexer_consume_token (parser->lexer);
14d22dd6 5378 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5379 if (cp_parser_non_integral_constant_expression (parser,
5380 "a comma operator"))
5381 expression = error_mark_node;
14d22dd6 5382 }
a723baf1
MM
5383
5384 return expression;
5385}
5386
21526606 5387/* Parse a constant-expression.
a723baf1
MM
5388
5389 constant-expression:
21526606 5390 conditional-expression
14d22dd6
MM
5391
5392 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5393 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5394 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5395 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5396
5397static tree
21526606 5398cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5399 bool allow_non_constant_p,
5400 bool *non_constant_p)
a723baf1 5401{
67c03833
JM
5402 bool saved_integral_constant_expression_p;
5403 bool saved_allow_non_integral_constant_expression_p;
5404 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5405 tree expression;
5406
5407 /* It might seem that we could simply parse the
5408 conditional-expression, and then check to see if it were
5409 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5410 one that the compiler can figure out is constant, possibly after
5411 doing some simplifications or optimizations. The standard has a
5412 precise definition of constant-expression, and we must honor
5413 that, even though it is somewhat more restrictive.
5414
5415 For example:
5416
5417 int i[(2, 3)];
5418
5419 is not a legal declaration, because `(2, 3)' is not a
5420 constant-expression. The `,' operator is forbidden in a
5421 constant-expression. However, GCC's constant-folding machinery
5422 will fold this operation to an INTEGER_CST for `3'. */
5423
14d22dd6 5424 /* Save the old settings. */
67c03833 5425 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5426 saved_allow_non_integral_constant_expression_p
67c03833
JM
5427 = parser->allow_non_integral_constant_expression_p;
5428 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5429 /* We are now parsing a constant-expression. */
67c03833
JM
5430 parser->integral_constant_expression_p = true;
5431 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5432 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5433 /* Although the grammar says "conditional-expression", we parse an
5434 "assignment-expression", which also permits "throw-expression"
5435 and the use of assignment operators. In the case that
5436 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5437 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5438 actually essential that we look for an assignment-expression.
5439 For example, cp_parser_initializer_clauses uses this function to
5440 determine whether a particular assignment-expression is in fact
5441 constant. */
5442 expression = cp_parser_assignment_expression (parser);
14d22dd6 5443 /* Restore the old settings. */
67c03833 5444 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
21526606 5445 parser->allow_non_integral_constant_expression_p
67c03833 5446 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5447 if (allow_non_constant_p)
67c03833
JM
5448 *non_constant_p = parser->non_integral_constant_expression_p;
5449 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5450
5451 return expression;
5452}
5453
5454/* Statements [gram.stmt.stmt] */
5455
21526606 5456/* Parse a statement.
a723baf1
MM
5457
5458 statement:
5459 labeled-statement
5460 expression-statement
5461 compound-statement
5462 selection-statement
5463 iteration-statement
5464 jump-statement
5465 declaration-statement
5466 try-block */
5467
5468static void
a5bcc582 5469cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5470{
5471 tree statement;
5472 cp_token *token;
5473 int statement_line_number;
5474
5475 /* There is no statement yet. */
5476 statement = NULL_TREE;
5477 /* Peek at the next token. */
5478 token = cp_lexer_peek_token (parser->lexer);
5479 /* Remember the line number of the first token in the statement. */
82a98427 5480 statement_line_number = token->location.line;
a723baf1
MM
5481 /* If this is a keyword, then that will often determine what kind of
5482 statement we have. */
5483 if (token->type == CPP_KEYWORD)
5484 {
5485 enum rid keyword = token->keyword;
5486
5487 switch (keyword)
5488 {
5489 case RID_CASE:
5490 case RID_DEFAULT:
a5bcc582
NS
5491 statement = cp_parser_labeled_statement (parser,
5492 in_statement_expr_p);
a723baf1
MM
5493 break;
5494
5495 case RID_IF:
5496 case RID_SWITCH:
5497 statement = cp_parser_selection_statement (parser);
5498 break;
5499
5500 case RID_WHILE:
5501 case RID_DO:
5502 case RID_FOR:
5503 statement = cp_parser_iteration_statement (parser);
5504 break;
5505
5506 case RID_BREAK:
5507 case RID_CONTINUE:
5508 case RID_RETURN:
5509 case RID_GOTO:
5510 statement = cp_parser_jump_statement (parser);
5511 break;
5512
5513 case RID_TRY:
5514 statement = cp_parser_try_block (parser);
5515 break;
5516
5517 default:
5518 /* It might be a keyword like `int' that can start a
5519 declaration-statement. */
5520 break;
5521 }
5522 }
5523 else if (token->type == CPP_NAME)
5524 {
5525 /* If the next token is a `:', then we are looking at a
5526 labeled-statement. */
5527 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5528 if (token->type == CPP_COLON)
a5bcc582 5529 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5530 }
5531 /* Anything that starts with a `{' must be a compound-statement. */
5532 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5533 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5534
5535 /* Everything else must be a declaration-statement or an
21526606 5536 expression-statement. Try for the declaration-statement
a723baf1
MM
5537 first, unless we are looking at a `;', in which case we know that
5538 we have an expression-statement. */
5539 if (!statement)
5540 {
5541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5542 {
5543 cp_parser_parse_tentatively (parser);
5544 /* Try to parse the declaration-statement. */
5545 cp_parser_declaration_statement (parser);
5546 /* If that worked, we're done. */
5547 if (cp_parser_parse_definitely (parser))
5548 return;
5549 }
5550 /* Look for an expression-statement instead. */
a5bcc582 5551 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5552 }
5553
5554 /* Set the line number for the statement. */
009ed910 5555 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5556 STMT_LINENO (statement) = statement_line_number;
5557}
5558
5559/* Parse a labeled-statement.
5560
5561 labeled-statement:
5562 identifier : statement
5563 case constant-expression : statement
98ce043b
MM
5564 default : statement
5565
5566 GNU Extension:
21526606 5567
98ce043b
MM
5568 labeled-statement:
5569 case constant-expression ... constant-expression : statement
a723baf1
MM
5570
5571 Returns the new CASE_LABEL, for a `case' or `default' label. For
5572 an ordinary label, returns a LABEL_STMT. */
5573
5574static tree
a5bcc582 5575cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5576{
5577 cp_token *token;
0e59b3fb 5578 tree statement = error_mark_node;
a723baf1
MM
5579
5580 /* The next token should be an identifier. */
5581 token = cp_lexer_peek_token (parser->lexer);
5582 if (token->type != CPP_NAME
5583 && token->type != CPP_KEYWORD)
5584 {
5585 cp_parser_error (parser, "expected labeled-statement");
5586 return error_mark_node;
5587 }
5588
5589 switch (token->keyword)
5590 {
5591 case RID_CASE:
5592 {
98ce043b
MM
5593 tree expr, expr_hi;
5594 cp_token *ellipsis;
a723baf1
MM
5595
5596 /* Consume the `case' token. */
5597 cp_lexer_consume_token (parser->lexer);
5598 /* Parse the constant-expression. */
21526606 5599 expr = cp_parser_constant_expression (parser,
d17811fd 5600 /*allow_non_constant_p=*/false,
14d22dd6 5601 NULL);
98ce043b
MM
5602
5603 ellipsis = cp_lexer_peek_token (parser->lexer);
5604 if (ellipsis->type == CPP_ELLIPSIS)
5605 {
5606 /* Consume the `...' token. */
5607 cp_lexer_consume_token (parser->lexer);
5608 expr_hi =
5609 cp_parser_constant_expression (parser,
5610 /*allow_non_constant_p=*/false,
5611 NULL);
5612 /* We don't need to emit warnings here, as the common code
5613 will do this for us. */
5614 }
5615 else
5616 expr_hi = NULL_TREE;
5617
0e59b3fb
MM
5618 if (!parser->in_switch_statement_p)
5619 error ("case label `%E' not within a switch statement", expr);
5620 else
98ce043b 5621 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
5622 }
5623 break;
5624
5625 case RID_DEFAULT:
5626 /* Consume the `default' token. */
5627 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5628 if (!parser->in_switch_statement_p)
5629 error ("case label not within a switch statement");
5630 else
5631 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5632 break;
5633
5634 default:
5635 /* Anything else must be an ordinary label. */
5636 statement = finish_label_stmt (cp_parser_identifier (parser));
5637 break;
5638 }
5639
5640 /* Require the `:' token. */
5641 cp_parser_require (parser, CPP_COLON, "`:'");
5642 /* Parse the labeled statement. */
a5bcc582 5643 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5644
5645 /* Return the label, in the case of a `case' or `default' label. */
5646 return statement;
5647}
5648
5649/* Parse an expression-statement.
5650
5651 expression-statement:
5652 expression [opt] ;
5653
5654 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5655 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5656 indicates whether this expression-statement is part of an
5657 expression statement. */
a723baf1
MM
5658
5659static tree
a5bcc582 5660cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5661{
a5bcc582 5662 tree statement = NULL_TREE;
a723baf1 5663
a5bcc582 5664 /* If the next token is a ';', then there is no expression
04c06002 5665 statement. */
a723baf1 5666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582 5667 statement = cp_parser_expression (parser);
21526606 5668
a723baf1 5669 /* Consume the final `;'. */
e0860732 5670 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5671
a5bcc582
NS
5672 if (in_statement_expr_p
5673 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5674 {
5675 /* This is the final expression statement of a statement
5676 expression. */
5677 statement = finish_stmt_expr_expr (statement);
5678 }
5679 else if (statement)
5680 statement = finish_expr_stmt (statement);
5681 else
5682 finish_stmt ();
21526606 5683
a723baf1
MM
5684 return statement;
5685}
5686
5687/* Parse a compound-statement.
5688
5689 compound-statement:
5690 { statement-seq [opt] }
21526606 5691
a723baf1
MM
5692 Returns a COMPOUND_STMT representing the statement. */
5693
5694static tree
a5bcc582 5695cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5696{
5697 tree compound_stmt;
5698
5699 /* Consume the `{'. */
5700 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5701 return error_mark_node;
5702 /* Begin the compound-statement. */
7a3397c7 5703 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5704 /* Parse an (optional) statement-seq. */
a5bcc582 5705 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5706 /* Finish the compound-statement. */
7a3397c7 5707 finish_compound_stmt (compound_stmt);
a723baf1
MM
5708 /* Consume the `}'. */
5709 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5710
5711 return compound_stmt;
5712}
5713
5714/* Parse an (optional) statement-seq.
5715
5716 statement-seq:
5717 statement
5718 statement-seq [opt] statement */
5719
5720static void
a5bcc582 5721cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5722{
5723 /* Scan statements until there aren't any more. */
5724 while (true)
5725 {
5726 /* If we're looking at a `}', then we've run out of statements. */
5727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5728 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5729 break;
5730
5731 /* Parse the statement. */
a5bcc582 5732 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5733 }
5734}
5735
5736/* Parse a selection-statement.
5737
5738 selection-statement:
5739 if ( condition ) statement
5740 if ( condition ) statement else statement
21526606 5741 switch ( condition ) statement
a723baf1
MM
5742
5743 Returns the new IF_STMT or SWITCH_STMT. */
5744
5745static tree
94edc4ab 5746cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5747{
5748 cp_token *token;
5749 enum rid keyword;
5750
5751 /* Peek at the next token. */
5752 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5753
5754 /* See what kind of keyword it is. */
5755 keyword = token->keyword;
5756 switch (keyword)
5757 {
5758 case RID_IF:
5759 case RID_SWITCH:
5760 {
5761 tree statement;
5762 tree condition;
5763
5764 /* Look for the `('. */
5765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5766 {
5767 cp_parser_skip_to_end_of_statement (parser);
5768 return error_mark_node;
5769 }
5770
5771 /* Begin the selection-statement. */
5772 if (keyword == RID_IF)
5773 statement = begin_if_stmt ();
5774 else
5775 statement = begin_switch_stmt ();
5776
5777 /* Parse the condition. */
5778 condition = cp_parser_condition (parser);
5779 /* Look for the `)'. */
5780 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
5781 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5782 /*consume_paren=*/true);
a723baf1
MM
5783
5784 if (keyword == RID_IF)
5785 {
5786 tree then_stmt;
5787
5788 /* Add the condition. */
5789 finish_if_stmt_cond (condition, statement);
5790
5791 /* Parse the then-clause. */
5792 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5793 finish_then_clause (statement);
5794
5795 /* If the next token is `else', parse the else-clause. */
5796 if (cp_lexer_next_token_is_keyword (parser->lexer,
5797 RID_ELSE))
5798 {
5799 tree else_stmt;
5800
5801 /* Consume the `else' keyword. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* Parse the else-clause. */
21526606 5804 else_stmt
a723baf1
MM
5805 = cp_parser_implicitly_scoped_statement (parser);
5806 finish_else_clause (statement);
5807 }
5808
5809 /* Now we're all done with the if-statement. */
5810 finish_if_stmt ();
5811 }
5812 else
5813 {
5814 tree body;
0e59b3fb 5815 bool in_switch_statement_p;
a723baf1
MM
5816
5817 /* Add the condition. */
5818 finish_switch_cond (condition, statement);
5819
5820 /* Parse the body of the switch-statement. */
0e59b3fb
MM
5821 in_switch_statement_p = parser->in_switch_statement_p;
5822 parser->in_switch_statement_p = true;
a723baf1 5823 body = cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5824 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
5825
5826 /* Now we're all done with the switch-statement. */
5827 finish_switch_stmt (statement);
5828 }
5829
5830 return statement;
5831 }
5832 break;
5833
5834 default:
5835 cp_parser_error (parser, "expected selection-statement");
5836 return error_mark_node;
5837 }
5838}
5839
21526606 5840/* Parse a condition.
a723baf1
MM
5841
5842 condition:
5843 expression
21526606 5844 type-specifier-seq declarator = assignment-expression
a723baf1
MM
5845
5846 GNU Extension:
21526606 5847
a723baf1 5848 condition:
21526606 5849 type-specifier-seq declarator asm-specification [opt]
a723baf1 5850 attributes [opt] = assignment-expression
21526606 5851
a723baf1
MM
5852 Returns the expression that should be tested. */
5853
5854static tree
94edc4ab 5855cp_parser_condition (cp_parser* parser)
a723baf1
MM
5856{
5857 tree type_specifiers;
5858 const char *saved_message;
5859
5860 /* Try the declaration first. */
5861 cp_parser_parse_tentatively (parser);
5862 /* New types are not allowed in the type-specifier-seq for a
5863 condition. */
5864 saved_message = parser->type_definition_forbidden_message;
5865 parser->type_definition_forbidden_message
5866 = "types may not be defined in conditions";
5867 /* Parse the type-specifier-seq. */
5868 type_specifiers = cp_parser_type_specifier_seq (parser);
5869 /* Restore the saved message. */
5870 parser->type_definition_forbidden_message = saved_message;
5871 /* If all is well, we might be looking at a declaration. */
5872 if (!cp_parser_error_occurred (parser))
5873 {
5874 tree decl;
5875 tree asm_specification;
5876 tree attributes;
5877 tree declarator;
5878 tree initializer = NULL_TREE;
21526606 5879
a723baf1 5880 /* Parse the declarator. */
62b8a44e 5881 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
5882 /*ctor_dtor_or_conv_p=*/NULL,
5883 /*parenthesized_p=*/NULL);
a723baf1
MM
5884 /* Parse the attributes. */
5885 attributes = cp_parser_attributes_opt (parser);
5886 /* Parse the asm-specification. */
5887 asm_specification = cp_parser_asm_specification_opt (parser);
5888 /* If the next token is not an `=', then we might still be
5889 looking at an expression. For example:
21526606 5890
a723baf1 5891 if (A(a).x)
21526606 5892
a723baf1
MM
5893 looks like a decl-specifier-seq and a declarator -- but then
5894 there is no `=', so this is an expression. */
5895 cp_parser_require (parser, CPP_EQ, "`='");
5896 /* If we did see an `=', then we are looking at a declaration
5897 for sure. */
5898 if (cp_parser_parse_definitely (parser))
5899 {
5900 /* Create the declaration. */
21526606 5901 decl = start_decl (declarator, type_specifiers,
a723baf1
MM
5902 /*initialized_p=*/true,
5903 attributes, /*prefix_attributes=*/NULL_TREE);
5904 /* Parse the assignment-expression. */
5905 initializer = cp_parser_assignment_expression (parser);
21526606 5906
a723baf1 5907 /* Process the initializer. */
21526606
EC
5908 cp_finish_decl (decl,
5909 initializer,
5910 asm_specification,
a723baf1 5911 LOOKUP_ONLYCONVERTING);
21526606 5912
a723baf1
MM
5913 return convert_from_reference (decl);
5914 }
5915 }
5916 /* If we didn't even get past the declarator successfully, we are
5917 definitely not looking at a declaration. */
5918 else
5919 cp_parser_abort_tentative_parse (parser);
5920
5921 /* Otherwise, we are looking at an expression. */
5922 return cp_parser_expression (parser);
5923}
5924
5925/* Parse an iteration-statement.
5926
5927 iteration-statement:
5928 while ( condition ) statement
5929 do statement while ( expression ) ;
5930 for ( for-init-statement condition [opt] ; expression [opt] )
5931 statement
5932
5933 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5934
5935static tree
94edc4ab 5936cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5937{
5938 cp_token *token;
5939 enum rid keyword;
5940 tree statement;
0e59b3fb
MM
5941 bool in_iteration_statement_p;
5942
a723baf1
MM
5943
5944 /* Peek at the next token. */
5945 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5946 if (!token)
5947 return error_mark_node;
5948
0e59b3fb 5949 /* Remember whether or not we are already within an iteration
21526606 5950 statement. */
0e59b3fb
MM
5951 in_iteration_statement_p = parser->in_iteration_statement_p;
5952
a723baf1
MM
5953 /* See what kind of keyword it is. */
5954 keyword = token->keyword;
5955 switch (keyword)
5956 {
5957 case RID_WHILE:
5958 {
5959 tree condition;
5960
5961 /* Begin the while-statement. */
5962 statement = begin_while_stmt ();
5963 /* Look for the `('. */
5964 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5965 /* Parse the condition. */
5966 condition = cp_parser_condition (parser);
5967 finish_while_stmt_cond (condition, statement);
5968 /* Look for the `)'. */
5969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5970 /* Parse the dependent statement. */
0e59b3fb 5971 parser->in_iteration_statement_p = true;
a723baf1 5972 cp_parser_already_scoped_statement (parser);
0e59b3fb 5973 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5974 /* We're done with the while-statement. */
5975 finish_while_stmt (statement);
5976 }
5977 break;
5978
5979 case RID_DO:
5980 {
5981 tree expression;
5982
5983 /* Begin the do-statement. */
5984 statement = begin_do_stmt ();
5985 /* Parse the body of the do-statement. */
0e59b3fb 5986 parser->in_iteration_statement_p = true;
a723baf1 5987 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5988 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5989 finish_do_body (statement);
5990 /* Look for the `while' keyword. */
5991 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5992 /* Look for the `('. */
5993 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5994 /* Parse the expression. */
5995 expression = cp_parser_expression (parser);
5996 /* We're done with the do-statement. */
5997 finish_do_stmt (expression, statement);
5998 /* Look for the `)'. */
5999 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6000 /* Look for the `;'. */
6001 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6002 }
6003 break;
6004
6005 case RID_FOR:
6006 {
6007 tree condition = NULL_TREE;
6008 tree expression = NULL_TREE;
6009
6010 /* Begin the for-statement. */
6011 statement = begin_for_stmt ();
6012 /* Look for the `('. */
6013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6014 /* Parse the initialization. */
6015 cp_parser_for_init_statement (parser);
6016 finish_for_init_stmt (statement);
6017
6018 /* If there's a condition, process it. */
6019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6020 condition = cp_parser_condition (parser);
6021 finish_for_cond (condition, statement);
6022 /* Look for the `;'. */
6023 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6024
6025 /* If there's an expression, process it. */
6026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6027 expression = cp_parser_expression (parser);
6028 finish_for_expr (expression, statement);
6029 /* Look for the `)'. */
6030 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6031
6032 /* Parse the body of the for-statement. */
0e59b3fb 6033 parser->in_iteration_statement_p = true;
a723baf1 6034 cp_parser_already_scoped_statement (parser);
0e59b3fb 6035 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6036
6037 /* We're done with the for-statement. */
6038 finish_for_stmt (statement);
6039 }
6040 break;
6041
6042 default:
6043 cp_parser_error (parser, "expected iteration-statement");
6044 statement = error_mark_node;
6045 break;
6046 }
6047
6048 return statement;
6049}
6050
6051/* Parse a for-init-statement.
6052
6053 for-init-statement:
6054 expression-statement
6055 simple-declaration */
6056
6057static void
94edc4ab 6058cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6059{
6060 /* If the next token is a `;', then we have an empty
34cd5ae7 6061 expression-statement. Grammatically, this is also a
a723baf1
MM
6062 simple-declaration, but an invalid one, because it does not
6063 declare anything. Therefore, if we did not handle this case
6064 specially, we would issue an error message about an invalid
6065 declaration. */
6066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6067 {
6068 /* We're going to speculatively look for a declaration, falling back
6069 to an expression, if necessary. */
6070 cp_parser_parse_tentatively (parser);
6071 /* Parse the declaration. */
6072 cp_parser_simple_declaration (parser,
6073 /*function_definition_allowed_p=*/false);
6074 /* If the tentative parse failed, then we shall need to look for an
6075 expression-statement. */
6076 if (cp_parser_parse_definitely (parser))
6077 return;
6078 }
6079
a5bcc582 6080 cp_parser_expression_statement (parser, false);
a723baf1
MM
6081}
6082
6083/* Parse a jump-statement.
6084
6085 jump-statement:
6086 break ;
6087 continue ;
6088 return expression [opt] ;
21526606 6089 goto identifier ;
a723baf1
MM
6090
6091 GNU extension:
6092
6093 jump-statement:
6094 goto * expression ;
6095
6096 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6097 GOTO_STMT. */
6098
6099static tree
94edc4ab 6100cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6101{
6102 tree statement = error_mark_node;
6103 cp_token *token;
6104 enum rid keyword;
6105
6106 /* Peek at the next token. */
6107 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6108 if (!token)
6109 return error_mark_node;
6110
6111 /* See what kind of keyword it is. */
6112 keyword = token->keyword;
6113 switch (keyword)
6114 {
6115 case RID_BREAK:
0e59b3fb
MM
6116 if (!parser->in_switch_statement_p
6117 && !parser->in_iteration_statement_p)
6118 {
6119 error ("break statement not within loop or switch");
6120 statement = error_mark_node;
6121 }
6122 else
6123 statement = finish_break_stmt ();
a723baf1
MM
6124 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6125 break;
6126
6127 case RID_CONTINUE:
0e59b3fb
MM
6128 if (!parser->in_iteration_statement_p)
6129 {
6130 error ("continue statement not within a loop");
6131 statement = error_mark_node;
6132 }
6133 else
6134 statement = finish_continue_stmt ();
a723baf1
MM
6135 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6136 break;
6137
6138 case RID_RETURN:
6139 {
6140 tree expr;
6141
21526606 6142 /* If the next token is a `;', then there is no
a723baf1
MM
6143 expression. */
6144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6145 expr = cp_parser_expression (parser);
6146 else
6147 expr = NULL_TREE;
6148 /* Build the return-statement. */
6149 statement = finish_return_stmt (expr);
6150 /* Look for the final `;'. */
6151 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6152 }
6153 break;
6154
6155 case RID_GOTO:
6156 /* Create the goto-statement. */
6157 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6158 {
6159 /* Issue a warning about this use of a GNU extension. */
6160 if (pedantic)
6161 pedwarn ("ISO C++ forbids computed gotos");
6162 /* Consume the '*' token. */
6163 cp_lexer_consume_token (parser->lexer);
6164 /* Parse the dependent expression. */
6165 finish_goto_stmt (cp_parser_expression (parser));
6166 }
6167 else
6168 finish_goto_stmt (cp_parser_identifier (parser));
6169 /* Look for the final `;'. */
6170 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6171 break;
6172
6173 default:
6174 cp_parser_error (parser, "expected jump-statement");
6175 break;
6176 }
6177
6178 return statement;
6179}
6180
6181/* Parse a declaration-statement.
6182
6183 declaration-statement:
6184 block-declaration */
6185
6186static void
94edc4ab 6187cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6188{
6189 /* Parse the block-declaration. */
6190 cp_parser_block_declaration (parser, /*statement_p=*/true);
6191
6192 /* Finish off the statement. */
6193 finish_stmt ();
6194}
6195
6196/* Some dependent statements (like `if (cond) statement'), are
6197 implicitly in their own scope. In other words, if the statement is
6198 a single statement (as opposed to a compound-statement), it is
6199 none-the-less treated as if it were enclosed in braces. Any
6200 declarations appearing in the dependent statement are out of scope
6201 after control passes that point. This function parses a statement,
6202 but ensures that is in its own scope, even if it is not a
21526606 6203 compound-statement.
a723baf1
MM
6204
6205 Returns the new statement. */
6206
6207static tree
94edc4ab 6208cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6209{
6210 tree statement;
6211
6212 /* If the token is not a `{', then we must take special action. */
6213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6214 {
6215 /* Create a compound-statement. */
7a3397c7 6216 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 6217 /* Parse the dependent-statement. */
a5bcc582 6218 cp_parser_statement (parser, false);
a723baf1 6219 /* Finish the dummy compound-statement. */
7a3397c7 6220 finish_compound_stmt (statement);
a723baf1
MM
6221 }
6222 /* Otherwise, we simply parse the statement directly. */
6223 else
a5bcc582 6224 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
6225
6226 /* Return the statement. */
6227 return statement;
6228}
6229
6230/* For some dependent statements (like `while (cond) statement'), we
6231 have already created a scope. Therefore, even if the dependent
6232 statement is a compound-statement, we do not want to create another
6233 scope. */
6234
6235static void
94edc4ab 6236cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6237{
6238 /* If the token is not a `{', then we must take special action. */
6239 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6240 {
6241 tree statement;
6242
6243 /* Create a compound-statement. */
7a3397c7 6244 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 6245 /* Parse the dependent-statement. */
a5bcc582 6246 cp_parser_statement (parser, false);
a723baf1 6247 /* Finish the dummy compound-statement. */
7a3397c7 6248 finish_compound_stmt (statement);
a723baf1
MM
6249 }
6250 /* Otherwise, we simply parse the statement directly. */
6251 else
a5bcc582 6252 cp_parser_statement (parser, false);
a723baf1
MM
6253}
6254
6255/* Declarations [gram.dcl.dcl] */
6256
6257/* Parse an optional declaration-sequence.
6258
6259 declaration-seq:
6260 declaration
6261 declaration-seq declaration */
6262
6263static void
94edc4ab 6264cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6265{
6266 while (true)
6267 {
6268 cp_token *token;
6269
6270 token = cp_lexer_peek_token (parser->lexer);
6271
6272 if (token->type == CPP_CLOSE_BRACE
6273 || token->type == CPP_EOF)
6274 break;
6275
21526606 6276 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6277 {
6278 /* A declaration consisting of a single semicolon is
6279 invalid. Allow it unless we're being pedantic. */
499b568f 6280 if (pedantic && !in_system_header)
a723baf1
MM
6281 pedwarn ("extra `;'");
6282 cp_lexer_consume_token (parser->lexer);
6283 continue;
6284 }
6285
c838d82f 6286 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6287 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6288 while (pending_lang_change > 0)
6289 {
6290 push_lang_context (lang_name_c);
6291 --pending_lang_change;
6292 }
6293 while (pending_lang_change < 0)
6294 {
6295 pop_lang_context ();
6296 ++pending_lang_change;
6297 }
6298
6299 /* Parse the declaration itself. */
a723baf1
MM
6300 cp_parser_declaration (parser);
6301 }
6302}
6303
6304/* Parse a declaration.
6305
6306 declaration:
6307 block-declaration
6308 function-definition
6309 template-declaration
6310 explicit-instantiation
6311 explicit-specialization
6312 linkage-specification
21526606 6313 namespace-definition
1092805d
MM
6314
6315 GNU extension:
6316
6317 declaration:
6318 __extension__ declaration */
a723baf1
MM
6319
6320static void
94edc4ab 6321cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6322{
6323 cp_token token1;
6324 cp_token token2;
1092805d
MM
6325 int saved_pedantic;
6326
21526606
EC
6327 /* Set this here since we can be called after
6328 pushing the linkage specification. */
6329 c_lex_string_translate = true;
6330
1092805d
MM
6331 /* Check for the `__extension__' keyword. */
6332 if (cp_parser_extension_opt (parser, &saved_pedantic))
6333 {
6334 /* Parse the qualified declaration. */
6335 cp_parser_declaration (parser);
6336 /* Restore the PEDANTIC flag. */
6337 pedantic = saved_pedantic;
6338
6339 return;
6340 }
a723baf1
MM
6341
6342 /* Try to figure out what kind of declaration is present. */
6343 token1 = *cp_lexer_peek_token (parser->lexer);
21526606
EC
6344
6345 /* Don't translate the CPP_STRING in extern "C". */
6346 if (token1.keyword == RID_EXTERN)
6347 c_lex_string_translate = false;
6348
a723baf1
MM
6349 if (token1.type != CPP_EOF)
6350 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6351
6352 /* If the next token is `extern' and the following token is a string
6353 literal, then we have a linkage specification. */
6354 if (token1.keyword == RID_EXTERN
6355 && cp_parser_is_string_literal (&token2))
6356 cp_parser_linkage_specification (parser);
6357 /* If the next token is `template', then we have either a template
6358 declaration, an explicit instantiation, or an explicit
6359 specialization. */
6360 else if (token1.keyword == RID_TEMPLATE)
6361 {
6362 /* `template <>' indicates a template specialization. */
6363 if (token2.type == CPP_LESS
6364 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6365 cp_parser_explicit_specialization (parser);
6366 /* `template <' indicates a template declaration. */
6367 else if (token2.type == CPP_LESS)
6368 cp_parser_template_declaration (parser, /*member_p=*/false);
6369 /* Anything else must be an explicit instantiation. */
6370 else
6371 cp_parser_explicit_instantiation (parser);
6372 }
6373 /* If the next token is `export', then we have a template
6374 declaration. */
6375 else if (token1.keyword == RID_EXPORT)
6376 cp_parser_template_declaration (parser, /*member_p=*/false);
6377 /* If the next token is `extern', 'static' or 'inline' and the one
6378 after that is `template', we have a GNU extended explicit
6379 instantiation directive. */
6380 else if (cp_parser_allow_gnu_extensions_p (parser)
6381 && (token1.keyword == RID_EXTERN
6382 || token1.keyword == RID_STATIC
6383 || token1.keyword == RID_INLINE)
6384 && token2.keyword == RID_TEMPLATE)
6385 cp_parser_explicit_instantiation (parser);
6386 /* If the next token is `namespace', check for a named or unnamed
6387 namespace definition. */
6388 else if (token1.keyword == RID_NAMESPACE
6389 && (/* A named namespace definition. */
6390 (token2.type == CPP_NAME
21526606 6391 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6392 == CPP_OPEN_BRACE))
6393 /* An unnamed namespace definition. */
6394 || token2.type == CPP_OPEN_BRACE))
6395 cp_parser_namespace_definition (parser);
6396 /* We must have either a block declaration or a function
6397 definition. */
6398 else
6399 /* Try to parse a block-declaration, or a function-definition. */
6400 cp_parser_block_declaration (parser, /*statement_p=*/false);
21526606
EC
6401
6402 c_lex_string_translate = true;
a723baf1
MM
6403}
6404
21526606 6405/* Parse a block-declaration.
a723baf1
MM
6406
6407 block-declaration:
6408 simple-declaration
6409 asm-definition
6410 namespace-alias-definition
6411 using-declaration
21526606 6412 using-directive
a723baf1
MM
6413
6414 GNU Extension:
6415
6416 block-declaration:
21526606 6417 __extension__ block-declaration
a723baf1
MM
6418 label-declaration
6419
34cd5ae7 6420 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6421 part of a declaration-statement. */
6422
6423static void
21526606 6424cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6425 bool statement_p)
6426{
6427 cp_token *token1;
6428 int saved_pedantic;
6429
6430 /* Check for the `__extension__' keyword. */
6431 if (cp_parser_extension_opt (parser, &saved_pedantic))
6432 {
6433 /* Parse the qualified declaration. */
6434 cp_parser_block_declaration (parser, statement_p);
6435 /* Restore the PEDANTIC flag. */
6436 pedantic = saved_pedantic;
6437
6438 return;
6439 }
6440
6441 /* Peek at the next token to figure out which kind of declaration is
6442 present. */
6443 token1 = cp_lexer_peek_token (parser->lexer);
6444
6445 /* If the next keyword is `asm', we have an asm-definition. */
6446 if (token1->keyword == RID_ASM)
6447 {
6448 if (statement_p)
6449 cp_parser_commit_to_tentative_parse (parser);
6450 cp_parser_asm_definition (parser);
6451 }
6452 /* If the next keyword is `namespace', we have a
6453 namespace-alias-definition. */
6454 else if (token1->keyword == RID_NAMESPACE)
6455 cp_parser_namespace_alias_definition (parser);
6456 /* If the next keyword is `using', we have either a
6457 using-declaration or a using-directive. */
6458 else if (token1->keyword == RID_USING)
6459 {
6460 cp_token *token2;
6461
6462 if (statement_p)
6463 cp_parser_commit_to_tentative_parse (parser);
6464 /* If the token after `using' is `namespace', then we have a
6465 using-directive. */
6466 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6467 if (token2->keyword == RID_NAMESPACE)
6468 cp_parser_using_directive (parser);
6469 /* Otherwise, it's a using-declaration. */
6470 else
6471 cp_parser_using_declaration (parser);
6472 }
6473 /* If the next keyword is `__label__' we have a label declaration. */
6474 else if (token1->keyword == RID_LABEL)
6475 {
6476 if (statement_p)
6477 cp_parser_commit_to_tentative_parse (parser);
6478 cp_parser_label_declaration (parser);
6479 }
6480 /* Anything else must be a simple-declaration. */
6481 else
6482 cp_parser_simple_declaration (parser, !statement_p);
6483}
6484
6485/* Parse a simple-declaration.
6486
6487 simple-declaration:
21526606 6488 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6489
6490 init-declarator-list:
6491 init-declarator
21526606 6492 init-declarator-list , init-declarator
a723baf1 6493
34cd5ae7 6494 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6495 function-definition as a simple-declaration. */
a723baf1
MM
6496
6497static void
21526606 6498cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6499 bool function_definition_allowed_p)
a723baf1
MM
6500{
6501 tree decl_specifiers;
6502 tree attributes;
560ad596 6503 int declares_class_or_enum;
a723baf1
MM
6504 bool saw_declarator;
6505
6506 /* Defer access checks until we know what is being declared; the
6507 checks for names appearing in the decl-specifier-seq should be
6508 done as if we were in the scope of the thing being declared. */
8d241e0b 6509 push_deferring_access_checks (dk_deferred);
cf22909c 6510
a723baf1
MM
6511 /* Parse the decl-specifier-seq. We have to keep track of whether
6512 or not the decl-specifier-seq declares a named class or
6513 enumeration type, since that is the only case in which the
21526606 6514 init-declarator-list is allowed to be empty.
a723baf1
MM
6515
6516 [dcl.dcl]
6517
6518 In a simple-declaration, the optional init-declarator-list can be
6519 omitted only when declaring a class or enumeration, that is when
6520 the decl-specifier-seq contains either a class-specifier, an
6521 elaborated-type-specifier, or an enum-specifier. */
6522 decl_specifiers
21526606 6523 = cp_parser_decl_specifier_seq (parser,
a723baf1
MM
6524 CP_PARSER_FLAGS_OPTIONAL,
6525 &attributes,
6526 &declares_class_or_enum);
6527 /* We no longer need to defer access checks. */
cf22909c 6528 stop_deferring_access_checks ();
24c0ef37 6529
39703eb9
MM
6530 /* In a block scope, a valid declaration must always have a
6531 decl-specifier-seq. By not trying to parse declarators, we can
6532 resolve the declaration/expression ambiguity more quickly. */
6533 if (!function_definition_allowed_p && !decl_specifiers)
6534 {
6535 cp_parser_error (parser, "expected declaration");
6536 goto done;
6537 }
6538
8fbc5ae7
MM
6539 /* If the next two tokens are both identifiers, the code is
6540 erroneous. The usual cause of this situation is code like:
6541
6542 T t;
6543
6544 where "T" should name a type -- but does not. */
2097b5f2 6545 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6546 {
8d241e0b 6547 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6548 looking at a declaration. */
6549 cp_parser_commit_to_tentative_parse (parser);
6550 /* Give up. */
39703eb9 6551 goto done;
8fbc5ae7
MM
6552 }
6553
a723baf1
MM
6554 /* Keep going until we hit the `;' at the end of the simple
6555 declaration. */
6556 saw_declarator = false;
21526606 6557 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6558 CPP_SEMICOLON))
6559 {
6560 cp_token *token;
6561 bool function_definition_p;
560ad596 6562 tree decl;
a723baf1
MM
6563
6564 saw_declarator = true;
6565 /* Parse the init-declarator. */
560ad596
MM
6566 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6567 function_definition_allowed_p,
6568 /*member_p=*/false,
6569 declares_class_or_enum,
6570 &function_definition_p);
1fb3244a
MM
6571 /* If an error occurred while parsing tentatively, exit quickly.
6572 (That usually happens when in the body of a function; each
6573 statement is treated as a declaration-statement until proven
6574 otherwise.) */
6575 if (cp_parser_error_occurred (parser))
39703eb9 6576 goto done;
a723baf1
MM
6577 /* Handle function definitions specially. */
6578 if (function_definition_p)
6579 {
6580 /* If the next token is a `,', then we are probably
6581 processing something like:
6582
6583 void f() {}, *p;
6584
6585 which is erroneous. */
6586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6587 error ("mixing declarations and function-definitions is forbidden");
6588 /* Otherwise, we're done with the list of declarators. */
6589 else
24c0ef37 6590 {
cf22909c 6591 pop_deferring_access_checks ();
24c0ef37
GS
6592 return;
6593 }
a723baf1
MM
6594 }
6595 /* The next token should be either a `,' or a `;'. */
6596 token = cp_lexer_peek_token (parser->lexer);
6597 /* If it's a `,', there are more declarators to come. */
6598 if (token->type == CPP_COMMA)
6599 cp_lexer_consume_token (parser->lexer);
6600 /* If it's a `;', we are done. */
6601 else if (token->type == CPP_SEMICOLON)
6602 break;
6603 /* Anything else is an error. */
6604 else
6605 {
6606 cp_parser_error (parser, "expected `,' or `;'");
6607 /* Skip tokens until we reach the end of the statement. */
6608 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
6609 /* If the next token is now a `;', consume it. */
6610 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6611 cp_lexer_consume_token (parser->lexer);
39703eb9 6612 goto done;
a723baf1
MM
6613 }
6614 /* After the first time around, a function-definition is not
6615 allowed -- even if it was OK at first. For example:
6616
6617 int i, f() {}
6618
6619 is not valid. */
6620 function_definition_allowed_p = false;
6621 }
6622
6623 /* Issue an error message if no declarators are present, and the
6624 decl-specifier-seq does not itself declare a class or
6625 enumeration. */
6626 if (!saw_declarator)
6627 {
6628 if (cp_parser_declares_only_class_p (parser))
6629 shadow_tag (decl_specifiers);
6630 /* Perform any deferred access checks. */
cf22909c 6631 perform_deferred_access_checks ();
a723baf1
MM
6632 }
6633
6634 /* Consume the `;'. */
6635 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6636
39703eb9
MM
6637 done:
6638 pop_deferring_access_checks ();
a723baf1
MM
6639}
6640
6641/* Parse a decl-specifier-seq.
6642
6643 decl-specifier-seq:
6644 decl-specifier-seq [opt] decl-specifier
6645
6646 decl-specifier:
6647 storage-class-specifier
6648 type-specifier
6649 function-specifier
6650 friend
21526606 6651 typedef
a723baf1
MM
6652
6653 GNU Extension:
6654
6655 decl-specifier-seq:
6656 decl-specifier-seq [opt] attributes
6657
6658 Returns a TREE_LIST, giving the decl-specifiers in the order they
6659 appear in the source code. The TREE_VALUE of each node is the
6660 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6661 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
21526606 6662 representation of a type-specifier, see cp_parser_type_specifier.
a723baf1
MM
6663
6664 If there are attributes, they will be stored in *ATTRIBUTES,
21526606 6665 represented as described above cp_parser_attributes.
a723baf1
MM
6666
6667 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6668 appears, and the entity that will be a friend is not going to be a
6669 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6670 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
21526606 6671 friendship is granted might not be a class.
560ad596
MM
6672
6673 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 6674 flags:
560ad596
MM
6675
6676 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 6677 (i.e., a type declaration)
560ad596 6678 2: one of the decl-specifiers is an enum-specifier or a
543ca912 6679 class-specifier (i.e., a type definition)
560ad596
MM
6680
6681 */
a723baf1
MM
6682
6683static tree
21526606
EC
6684cp_parser_decl_specifier_seq (cp_parser* parser,
6685 cp_parser_flags flags,
94edc4ab 6686 tree* attributes,
560ad596 6687 int* declares_class_or_enum)
a723baf1
MM
6688{
6689 tree decl_specs = NULL_TREE;
6690 bool friend_p = false;
f2ce60b8 6691 bool constructor_possible_p = !parser->in_declarator_p;
21526606 6692
a723baf1 6693 /* Assume no class or enumeration type is declared. */
560ad596 6694 *declares_class_or_enum = 0;
a723baf1
MM
6695
6696 /* Assume there are no attributes. */
6697 *attributes = NULL_TREE;
6698
6699 /* Keep reading specifiers until there are no more to read. */
6700 while (true)
6701 {
6702 tree decl_spec = NULL_TREE;
6703 bool constructor_p;
6704 cp_token *token;
6705
6706 /* Peek at the next token. */
6707 token = cp_lexer_peek_token (parser->lexer);
6708 /* Handle attributes. */
6709 if (token->keyword == RID_ATTRIBUTE)
6710 {
6711 /* Parse the attributes. */
6712 decl_spec = cp_parser_attributes_opt (parser);
6713 /* Add them to the list. */
6714 *attributes = chainon (*attributes, decl_spec);
6715 continue;
6716 }
6717 /* If the next token is an appropriate keyword, we can simply
6718 add it to the list. */
6719 switch (token->keyword)
6720 {
6721 case RID_FRIEND:
6722 /* decl-specifier:
6723 friend */
1918facf
SB
6724 if (friend_p)
6725 error ("duplicate `friend'");
6726 else
6727 friend_p = true;
a723baf1
MM
6728 /* The representation of the specifier is simply the
6729 appropriate TREE_IDENTIFIER node. */
6730 decl_spec = token->value;
6731 /* Consume the token. */
6732 cp_lexer_consume_token (parser->lexer);
6733 break;
6734
6735 /* function-specifier:
6736 inline
6737 virtual
6738 explicit */
6739 case RID_INLINE:
6740 case RID_VIRTUAL:
6741 case RID_EXPLICIT:
6742 decl_spec = cp_parser_function_specifier_opt (parser);
6743 break;
21526606 6744
a723baf1
MM
6745 /* decl-specifier:
6746 typedef */
6747 case RID_TYPEDEF:
6748 /* The representation of the specifier is simply the
6749 appropriate TREE_IDENTIFIER node. */
6750 decl_spec = token->value;
6751 /* Consume the token. */
6752 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6753 /* A constructor declarator cannot appear in a typedef. */
6754 constructor_possible_p = false;
c006d942
MM
6755 /* The "typedef" keyword can only occur in a declaration; we
6756 may as well commit at this point. */
6757 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6758 break;
6759
6760 /* storage-class-specifier:
6761 auto
6762 register
6763 static
6764 extern
21526606 6765 mutable
a723baf1
MM
6766
6767 GNU Extension:
6768 thread */
6769 case RID_AUTO:
6770 case RID_REGISTER:
6771 case RID_STATIC:
6772 case RID_EXTERN:
6773 case RID_MUTABLE:
6774 case RID_THREAD:
6775 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6776 break;
21526606 6777
a723baf1
MM
6778 default:
6779 break;
6780 }
6781
6782 /* Constructors are a special case. The `S' in `S()' is not a
6783 decl-specifier; it is the beginning of the declarator. */
21526606 6784 constructor_p = (!decl_spec
2050a1bb 6785 && constructor_possible_p
a723baf1
MM
6786 && cp_parser_constructor_declarator_p (parser,
6787 friend_p));
6788
6789 /* If we don't have a DECL_SPEC yet, then we must be looking at
6790 a type-specifier. */
6791 if (!decl_spec && !constructor_p)
6792 {
560ad596 6793 int decl_spec_declares_class_or_enum;
a723baf1
MM
6794 bool is_cv_qualifier;
6795
6796 decl_spec
6797 = cp_parser_type_specifier (parser, flags,
6798 friend_p,
6799 /*is_declaration=*/true,
6800 &decl_spec_declares_class_or_enum,
6801 &is_cv_qualifier);
6802
6803 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6804
6805 /* If this type-specifier referenced a user-defined type
6806 (a typedef, class-name, etc.), then we can't allow any
6807 more such type-specifiers henceforth.
6808
6809 [dcl.spec]
6810
6811 The longest sequence of decl-specifiers that could
6812 possibly be a type name is taken as the
6813 decl-specifier-seq of a declaration. The sequence shall
6814 be self-consistent as described below.
6815
6816 [dcl.type]
6817
6818 As a general rule, at most one type-specifier is allowed
6819 in the complete decl-specifier-seq of a declaration. The
6820 only exceptions are the following:
6821
6822 -- const or volatile can be combined with any other
21526606 6823 type-specifier.
a723baf1
MM
6824
6825 -- signed or unsigned can be combined with char, long,
6826 short, or int.
6827
6828 -- ..
6829
6830 Example:
6831
6832 typedef char* Pc;
6833 void g (const int Pc);
6834
6835 Here, Pc is *not* part of the decl-specifier seq; it's
6836 the declarator. Therefore, once we see a type-specifier
6837 (other than a cv-qualifier), we forbid any additional
6838 user-defined types. We *do* still allow things like `int
6839 int' to be considered a decl-specifier-seq, and issue the
6840 error message later. */
6841 if (decl_spec && !is_cv_qualifier)
6842 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6843 /* A constructor declarator cannot follow a type-specifier. */
6844 if (decl_spec)
6845 constructor_possible_p = false;
a723baf1
MM
6846 }
6847
6848 /* If we still do not have a DECL_SPEC, then there are no more
6849 decl-specifiers. */
6850 if (!decl_spec)
6851 {
6852 /* Issue an error message, unless the entire construct was
6853 optional. */
6854 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6855 {
6856 cp_parser_error (parser, "expected decl specifier");
6857 return error_mark_node;
6858 }
6859
6860 break;
6861 }
6862
6863 /* Add the DECL_SPEC to the list of specifiers. */
e90c7b84
ILT
6864 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6865 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
a723baf1
MM
6866
6867 /* After we see one decl-specifier, further decl-specifiers are
6868 always optional. */
6869 flags |= CP_PARSER_FLAGS_OPTIONAL;
6870 }
6871
0426c4ca
SB
6872 /* Don't allow a friend specifier with a class definition. */
6873 if (friend_p && (*declares_class_or_enum & 2))
6874 error ("class definition may not be declared a friend");
6875
a723baf1
MM
6876 /* We have built up the DECL_SPECS in reverse order. Return them in
6877 the correct order. */
6878 return nreverse (decl_specs);
6879}
6880
21526606 6881/* Parse an (optional) storage-class-specifier.
a723baf1
MM
6882
6883 storage-class-specifier:
6884 auto
6885 register
6886 static
6887 extern
21526606 6888 mutable
a723baf1
MM
6889
6890 GNU Extension:
6891
6892 storage-class-specifier:
6893 thread
6894
6895 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 6896
a723baf1 6897static tree
94edc4ab 6898cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6899{
6900 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6901 {
6902 case RID_AUTO:
6903 case RID_REGISTER:
6904 case RID_STATIC:
6905 case RID_EXTERN:
6906 case RID_MUTABLE:
6907 case RID_THREAD:
6908 /* Consume the token. */
6909 return cp_lexer_consume_token (parser->lexer)->value;
6910
6911 default:
6912 return NULL_TREE;
6913 }
6914}
6915
21526606 6916/* Parse an (optional) function-specifier.
a723baf1
MM
6917
6918 function-specifier:
6919 inline
6920 virtual
6921 explicit
6922
6923 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 6924
a723baf1 6925static tree
94edc4ab 6926cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6927{
6928 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6929 {
6930 case RID_INLINE:
6931 case RID_VIRTUAL:
6932 case RID_EXPLICIT:
6933 /* Consume the token. */
6934 return cp_lexer_consume_token (parser->lexer)->value;
6935
6936 default:
6937 return NULL_TREE;
6938 }
6939}
6940
6941/* Parse a linkage-specification.
6942
6943 linkage-specification:
6944 extern string-literal { declaration-seq [opt] }
6945 extern string-literal declaration */
6946
6947static void
94edc4ab 6948cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6949{
6950 cp_token *token;
6951 tree linkage;
6952
6953 /* Look for the `extern' keyword. */
6954 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6955
6956 /* Peek at the next token. */
6957 token = cp_lexer_peek_token (parser->lexer);
6958 /* If it's not a string-literal, then there's a problem. */
6959 if (!cp_parser_is_string_literal (token))
6960 {
6961 cp_parser_error (parser, "expected language-name");
6962 return;
6963 }
6964 /* Consume the token. */
6965 cp_lexer_consume_token (parser->lexer);
6966
6967 /* Transform the literal into an identifier. If the literal is a
6968 wide-character string, or contains embedded NULs, then we can't
6969 handle it as the user wants. */
6970 if (token->type == CPP_WSTRING
6971 || (strlen (TREE_STRING_POINTER (token->value))
6972 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6973 {
6974 cp_parser_error (parser, "invalid linkage-specification");
6975 /* Assume C++ linkage. */
6976 linkage = get_identifier ("c++");
6977 }
6978 /* If it's a simple string constant, things are easier. */
6979 else
6980 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6981
6982 /* We're now using the new linkage. */
6983 push_lang_context (linkage);
6984
6985 /* If the next token is a `{', then we're using the first
6986 production. */
6987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6988 {
6989 /* Consume the `{' token. */
6990 cp_lexer_consume_token (parser->lexer);
6991 /* Parse the declarations. */
6992 cp_parser_declaration_seq_opt (parser);
6993 /* Look for the closing `}'. */
6994 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6995 }
6996 /* Otherwise, there's just one declaration. */
6997 else
6998 {
6999 bool saved_in_unbraced_linkage_specification_p;
7000
21526606 7001 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7002 = parser->in_unbraced_linkage_specification_p;
7003 parser->in_unbraced_linkage_specification_p = true;
7004 have_extern_spec = true;
7005 cp_parser_declaration (parser);
7006 have_extern_spec = false;
21526606 7007 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7008 = saved_in_unbraced_linkage_specification_p;
7009 }
7010
7011 /* We're done with the linkage-specification. */
7012 pop_lang_context ();
7013}
7014
7015/* Special member functions [gram.special] */
7016
7017/* Parse a conversion-function-id.
7018
7019 conversion-function-id:
21526606 7020 operator conversion-type-id
a723baf1
MM
7021
7022 Returns an IDENTIFIER_NODE representing the operator. */
7023
21526606 7024static tree
94edc4ab 7025cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7026{
7027 tree type;
7028 tree saved_scope;
7029 tree saved_qualifying_scope;
7030 tree saved_object_scope;
91b004e5 7031 bool pop_p = false;
a723baf1
MM
7032
7033 /* Look for the `operator' token. */
7034 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7035 return error_mark_node;
7036 /* When we parse the conversion-type-id, the current scope will be
7037 reset. However, we need that information in able to look up the
7038 conversion function later, so we save it here. */
7039 saved_scope = parser->scope;
7040 saved_qualifying_scope = parser->qualifying_scope;
7041 saved_object_scope = parser->object_scope;
7042 /* We must enter the scope of the class so that the names of
7043 entities declared within the class are available in the
7044 conversion-type-id. For example, consider:
7045
21526606 7046 struct S {
a723baf1
MM
7047 typedef int I;
7048 operator I();
7049 };
7050
7051 S::operator I() { ... }
7052
7053 In order to see that `I' is a type-name in the definition, we
7054 must be in the scope of `S'. */
7055 if (saved_scope)
91b004e5 7056 pop_p = push_scope (saved_scope);
a723baf1
MM
7057 /* Parse the conversion-type-id. */
7058 type = cp_parser_conversion_type_id (parser);
7059 /* Leave the scope of the class, if any. */
91b004e5 7060 if (pop_p)
a723baf1
MM
7061 pop_scope (saved_scope);
7062 /* Restore the saved scope. */
7063 parser->scope = saved_scope;
7064 parser->qualifying_scope = saved_qualifying_scope;
7065 parser->object_scope = saved_object_scope;
7066 /* If the TYPE is invalid, indicate failure. */
7067 if (type == error_mark_node)
7068 return error_mark_node;
7069 return mangle_conv_op_name_for_type (type);
7070}
7071
7072/* Parse a conversion-type-id:
7073
7074 conversion-type-id:
7075 type-specifier-seq conversion-declarator [opt]
7076
7077 Returns the TYPE specified. */
7078
7079static tree
94edc4ab 7080cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7081{
7082 tree attributes;
7083 tree type_specifiers;
7084 tree declarator;
7085
7086 /* Parse the attributes. */
7087 attributes = cp_parser_attributes_opt (parser);
7088 /* Parse the type-specifiers. */
7089 type_specifiers = cp_parser_type_specifier_seq (parser);
7090 /* If that didn't work, stop. */
7091 if (type_specifiers == error_mark_node)
7092 return error_mark_node;
7093 /* Parse the conversion-declarator. */
7094 declarator = cp_parser_conversion_declarator_opt (parser);
7095
7096 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7097 /*initialized=*/0, &attributes);
7098}
7099
7100/* Parse an (optional) conversion-declarator.
7101
7102 conversion-declarator:
21526606 7103 ptr-operator conversion-declarator [opt]
a723baf1
MM
7104
7105 Returns a representation of the declarator. See
7106 cp_parser_declarator for details. */
7107
7108static tree
94edc4ab 7109cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7110{
7111 enum tree_code code;
7112 tree class_type;
7113 tree cv_qualifier_seq;
7114
7115 /* We don't know if there's a ptr-operator next, or not. */
7116 cp_parser_parse_tentatively (parser);
7117 /* Try the ptr-operator. */
21526606 7118 code = cp_parser_ptr_operator (parser, &class_type,
a723baf1
MM
7119 &cv_qualifier_seq);
7120 /* If it worked, look for more conversion-declarators. */
7121 if (cp_parser_parse_definitely (parser))
7122 {
7123 tree declarator;
7124
7125 /* Parse another optional declarator. */
7126 declarator = cp_parser_conversion_declarator_opt (parser);
7127
7128 /* Create the representation of the declarator. */
7129 if (code == INDIRECT_REF)
7130 declarator = make_pointer_declarator (cv_qualifier_seq,
7131 declarator);
7132 else
7133 declarator = make_reference_declarator (cv_qualifier_seq,
7134 declarator);
7135
7136 /* Handle the pointer-to-member case. */
7137 if (class_type)
7138 declarator = build_nt (SCOPE_REF, class_type, declarator);
7139
7140 return declarator;
7141 }
7142
7143 return NULL_TREE;
7144}
7145
7146/* Parse an (optional) ctor-initializer.
7147
7148 ctor-initializer:
21526606 7149 : mem-initializer-list
a723baf1
MM
7150
7151 Returns TRUE iff the ctor-initializer was actually present. */
7152
7153static bool
94edc4ab 7154cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7155{
7156 /* If the next token is not a `:', then there is no
7157 ctor-initializer. */
7158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7159 {
7160 /* Do default initialization of any bases and members. */
7161 if (DECL_CONSTRUCTOR_P (current_function_decl))
7162 finish_mem_initializers (NULL_TREE);
7163
7164 return false;
7165 }
7166
7167 /* Consume the `:' token. */
7168 cp_lexer_consume_token (parser->lexer);
7169 /* And the mem-initializer-list. */
7170 cp_parser_mem_initializer_list (parser);
7171
7172 return true;
7173}
7174
7175/* Parse a mem-initializer-list.
7176
7177 mem-initializer-list:
7178 mem-initializer
7179 mem-initializer , mem-initializer-list */
7180
7181static void
94edc4ab 7182cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7183{
7184 tree mem_initializer_list = NULL_TREE;
7185
7186 /* Let the semantic analysis code know that we are starting the
7187 mem-initializer-list. */
0e136342
MM
7188 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7189 error ("only constructors take base initializers");
a723baf1
MM
7190
7191 /* Loop through the list. */
7192 while (true)
7193 {
7194 tree mem_initializer;
7195
7196 /* Parse the mem-initializer. */
7197 mem_initializer = cp_parser_mem_initializer (parser);
7198 /* Add it to the list, unless it was erroneous. */
7199 if (mem_initializer)
7200 {
7201 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7202 mem_initializer_list = mem_initializer;
7203 }
7204 /* If the next token is not a `,', we're done. */
7205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7206 break;
7207 /* Consume the `,' token. */
7208 cp_lexer_consume_token (parser->lexer);
7209 }
7210
7211 /* Perform semantic analysis. */
0e136342
MM
7212 if (DECL_CONSTRUCTOR_P (current_function_decl))
7213 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7214}
7215
7216/* Parse a mem-initializer.
7217
7218 mem-initializer:
21526606 7219 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7220
7221 GNU extension:
21526606 7222
a723baf1 7223 mem-initializer:
34cd5ae7 7224 ( expression-list [opt] )
a723baf1
MM
7225
7226 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7227 class) or FIELD_DECL (for a non-static data member) to initialize;
7228 the TREE_VALUE is the expression-list. */
7229
7230static tree
94edc4ab 7231cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7232{
7233 tree mem_initializer_id;
7234 tree expression_list;
1f5a253a 7235 tree member;
21526606 7236
a723baf1
MM
7237 /* Find out what is being initialized. */
7238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7239 {
7240 pedwarn ("anachronistic old-style base class initializer");
7241 mem_initializer_id = NULL_TREE;
7242 }
7243 else
7244 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7245 member = expand_member_init (mem_initializer_id);
7246 if (member && !DECL_P (member))
7247 in_base_initializer = 1;
7efa3e22 7248
21526606 7249 expression_list
39703eb9
MM
7250 = cp_parser_parenthesized_expression_list (parser, false,
7251 /*non_constant_p=*/NULL);
7efa3e22 7252 if (!expression_list)
a723baf1 7253 expression_list = void_type_node;
a723baf1 7254
1f5a253a 7255 in_base_initializer = 0;
21526606 7256
1f5a253a 7257 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7258}
7259
7260/* Parse a mem-initializer-id.
7261
7262 mem-initializer-id:
7263 :: [opt] nested-name-specifier [opt] class-name
21526606 7264 identifier
a723baf1
MM
7265
7266 Returns a TYPE indicating the class to be initializer for the first
7267 production. Returns an IDENTIFIER_NODE indicating the data member
7268 to be initialized for the second production. */
7269
7270static tree
94edc4ab 7271cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7272{
7273 bool global_scope_p;
7274 bool nested_name_specifier_p;
7275 tree id;
7276
7277 /* Look for the optional `::' operator. */
21526606
EC
7278 global_scope_p
7279 = (cp_parser_global_scope_opt (parser,
7280 /*current_scope_valid_p=*/false)
a723baf1
MM
7281 != NULL_TREE);
7282 /* Look for the optional nested-name-specifier. The simplest way to
7283 implement:
7284
7285 [temp.res]
7286
7287 The keyword `typename' is not permitted in a base-specifier or
7288 mem-initializer; in these contexts a qualified name that
7289 depends on a template-parameter is implicitly assumed to be a
7290 type name.
7291
7292 is to assume that we have seen the `typename' keyword at this
7293 point. */
21526606 7294 nested_name_specifier_p
a723baf1
MM
7295 = (cp_parser_nested_name_specifier_opt (parser,
7296 /*typename_keyword_p=*/true,
7297 /*check_dependency_p=*/true,
a668c6ad
MM
7298 /*type_p=*/true,
7299 /*is_declaration=*/true)
a723baf1
MM
7300 != NULL_TREE);
7301 /* If there is a `::' operator or a nested-name-specifier, then we
7302 are definitely looking for a class-name. */
7303 if (global_scope_p || nested_name_specifier_p)
7304 return cp_parser_class_name (parser,
7305 /*typename_keyword_p=*/true,
7306 /*template_keyword_p=*/false,
7307 /*type_p=*/false,
a723baf1 7308 /*check_dependency_p=*/true,
a668c6ad
MM
7309 /*class_head_p=*/false,
7310 /*is_declaration=*/true);
a723baf1
MM
7311 /* Otherwise, we could also be looking for an ordinary identifier. */
7312 cp_parser_parse_tentatively (parser);
7313 /* Try a class-name. */
21526606 7314 id = cp_parser_class_name (parser,
a723baf1
MM
7315 /*typename_keyword_p=*/true,
7316 /*template_keyword_p=*/false,
7317 /*type_p=*/false,
a723baf1 7318 /*check_dependency_p=*/true,
a668c6ad
MM
7319 /*class_head_p=*/false,
7320 /*is_declaration=*/true);
a723baf1
MM
7321 /* If we found one, we're done. */
7322 if (cp_parser_parse_definitely (parser))
7323 return id;
7324 /* Otherwise, look for an ordinary identifier. */
7325 return cp_parser_identifier (parser);
7326}
7327
7328/* Overloading [gram.over] */
7329
7330/* Parse an operator-function-id.
7331
7332 operator-function-id:
21526606 7333 operator operator
a723baf1
MM
7334
7335 Returns an IDENTIFIER_NODE for the operator which is a
7336 human-readable spelling of the identifier, e.g., `operator +'. */
7337
21526606 7338static tree
94edc4ab 7339cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7340{
7341 /* Look for the `operator' keyword. */
7342 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7343 return error_mark_node;
7344 /* And then the name of the operator itself. */
7345 return cp_parser_operator (parser);
7346}
7347
7348/* Parse an operator.
7349
7350 operator:
7351 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7352 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7353 || ++ -- , ->* -> () []
7354
7355 GNU Extensions:
21526606 7356
a723baf1
MM
7357 operator:
7358 <? >? <?= >?=
7359
7360 Returns an IDENTIFIER_NODE for the operator which is a
7361 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7362
a723baf1 7363static tree
94edc4ab 7364cp_parser_operator (cp_parser* parser)
a723baf1
MM
7365{
7366 tree id = NULL_TREE;
7367 cp_token *token;
7368
7369 /* Peek at the next token. */
7370 token = cp_lexer_peek_token (parser->lexer);
7371 /* Figure out which operator we have. */
7372 switch (token->type)
7373 {
7374 case CPP_KEYWORD:
7375 {
7376 enum tree_code op;
7377
7378 /* The keyword should be either `new' or `delete'. */
7379 if (token->keyword == RID_NEW)
7380 op = NEW_EXPR;
7381 else if (token->keyword == RID_DELETE)
7382 op = DELETE_EXPR;
7383 else
7384 break;
7385
7386 /* Consume the `new' or `delete' token. */
7387 cp_lexer_consume_token (parser->lexer);
7388
7389 /* Peek at the next token. */
7390 token = cp_lexer_peek_token (parser->lexer);
7391 /* If it's a `[' token then this is the array variant of the
7392 operator. */
7393 if (token->type == CPP_OPEN_SQUARE)
7394 {
7395 /* Consume the `[' token. */
7396 cp_lexer_consume_token (parser->lexer);
7397 /* Look for the `]' token. */
7398 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7399 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7400 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7401 }
7402 /* Otherwise, we have the non-array variant. */
7403 else
7404 id = ansi_opname (op);
7405
7406 return id;
7407 }
7408
7409 case CPP_PLUS:
7410 id = ansi_opname (PLUS_EXPR);
7411 break;
7412
7413 case CPP_MINUS:
7414 id = ansi_opname (MINUS_EXPR);
7415 break;
7416
7417 case CPP_MULT:
7418 id = ansi_opname (MULT_EXPR);
7419 break;
7420
7421 case CPP_DIV:
7422 id = ansi_opname (TRUNC_DIV_EXPR);
7423 break;
7424
7425 case CPP_MOD:
7426 id = ansi_opname (TRUNC_MOD_EXPR);
7427 break;
7428
7429 case CPP_XOR:
7430 id = ansi_opname (BIT_XOR_EXPR);
7431 break;
7432
7433 case CPP_AND:
7434 id = ansi_opname (BIT_AND_EXPR);
7435 break;
7436
7437 case CPP_OR:
7438 id = ansi_opname (BIT_IOR_EXPR);
7439 break;
7440
7441 case CPP_COMPL:
7442 id = ansi_opname (BIT_NOT_EXPR);
7443 break;
21526606 7444
a723baf1
MM
7445 case CPP_NOT:
7446 id = ansi_opname (TRUTH_NOT_EXPR);
7447 break;
7448
7449 case CPP_EQ:
7450 id = ansi_assopname (NOP_EXPR);
7451 break;
7452
7453 case CPP_LESS:
7454 id = ansi_opname (LT_EXPR);
7455 break;
7456
7457 case CPP_GREATER:
7458 id = ansi_opname (GT_EXPR);
7459 break;
7460
7461 case CPP_PLUS_EQ:
7462 id = ansi_assopname (PLUS_EXPR);
7463 break;
7464
7465 case CPP_MINUS_EQ:
7466 id = ansi_assopname (MINUS_EXPR);
7467 break;
7468
7469 case CPP_MULT_EQ:
7470 id = ansi_assopname (MULT_EXPR);
7471 break;
7472
7473 case CPP_DIV_EQ:
7474 id = ansi_assopname (TRUNC_DIV_EXPR);
7475 break;
7476
7477 case CPP_MOD_EQ:
7478 id = ansi_assopname (TRUNC_MOD_EXPR);
7479 break;
7480
7481 case CPP_XOR_EQ:
7482 id = ansi_assopname (BIT_XOR_EXPR);
7483 break;
7484
7485 case CPP_AND_EQ:
7486 id = ansi_assopname (BIT_AND_EXPR);
7487 break;
7488
7489 case CPP_OR_EQ:
7490 id = ansi_assopname (BIT_IOR_EXPR);
7491 break;
7492
7493 case CPP_LSHIFT:
7494 id = ansi_opname (LSHIFT_EXPR);
7495 break;
7496
7497 case CPP_RSHIFT:
7498 id = ansi_opname (RSHIFT_EXPR);
7499 break;
7500
7501 case CPP_LSHIFT_EQ:
7502 id = ansi_assopname (LSHIFT_EXPR);
7503 break;
7504
7505 case CPP_RSHIFT_EQ:
7506 id = ansi_assopname (RSHIFT_EXPR);
7507 break;
7508
7509 case CPP_EQ_EQ:
7510 id = ansi_opname (EQ_EXPR);
7511 break;
7512
7513 case CPP_NOT_EQ:
7514 id = ansi_opname (NE_EXPR);
7515 break;
7516
7517 case CPP_LESS_EQ:
7518 id = ansi_opname (LE_EXPR);
7519 break;
7520
7521 case CPP_GREATER_EQ:
7522 id = ansi_opname (GE_EXPR);
7523 break;
7524
7525 case CPP_AND_AND:
7526 id = ansi_opname (TRUTH_ANDIF_EXPR);
7527 break;
7528
7529 case CPP_OR_OR:
7530 id = ansi_opname (TRUTH_ORIF_EXPR);
7531 break;
21526606 7532
a723baf1
MM
7533 case CPP_PLUS_PLUS:
7534 id = ansi_opname (POSTINCREMENT_EXPR);
7535 break;
7536
7537 case CPP_MINUS_MINUS:
7538 id = ansi_opname (PREDECREMENT_EXPR);
7539 break;
7540
7541 case CPP_COMMA:
7542 id = ansi_opname (COMPOUND_EXPR);
7543 break;
7544
7545 case CPP_DEREF_STAR:
7546 id = ansi_opname (MEMBER_REF);
7547 break;
7548
7549 case CPP_DEREF:
7550 id = ansi_opname (COMPONENT_REF);
7551 break;
7552
7553 case CPP_OPEN_PAREN:
7554 /* Consume the `('. */
7555 cp_lexer_consume_token (parser->lexer);
7556 /* Look for the matching `)'. */
7557 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7558 return ansi_opname (CALL_EXPR);
7559
7560 case CPP_OPEN_SQUARE:
7561 /* Consume the `['. */
7562 cp_lexer_consume_token (parser->lexer);
7563 /* Look for the matching `]'. */
7564 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7565 return ansi_opname (ARRAY_REF);
7566
7567 /* Extensions. */
7568 case CPP_MIN:
7569 id = ansi_opname (MIN_EXPR);
7570 break;
7571
7572 case CPP_MAX:
7573 id = ansi_opname (MAX_EXPR);
7574 break;
7575
7576 case CPP_MIN_EQ:
7577 id = ansi_assopname (MIN_EXPR);
7578 break;
7579
7580 case CPP_MAX_EQ:
7581 id = ansi_assopname (MAX_EXPR);
7582 break;
7583
7584 default:
7585 /* Anything else is an error. */
7586 break;
7587 }
7588
7589 /* If we have selected an identifier, we need to consume the
7590 operator token. */
7591 if (id)
7592 cp_lexer_consume_token (parser->lexer);
7593 /* Otherwise, no valid operator name was present. */
7594 else
7595 {
7596 cp_parser_error (parser, "expected operator");
7597 id = error_mark_node;
7598 }
7599
7600 return id;
7601}
7602
7603/* Parse a template-declaration.
7604
7605 template-declaration:
21526606 7606 export [opt] template < template-parameter-list > declaration
a723baf1
MM
7607
7608 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 7609 class-specifier.
a723baf1
MM
7610
7611 The grammar rule given by the standard isn't correct. What
7612 is really meant is:
7613
7614 template-declaration:
21526606 7615 export [opt] template-parameter-list-seq
a723baf1 7616 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 7617 export [opt] template-parameter-list-seq
a723baf1
MM
7618 function-definition
7619
7620 template-parameter-list-seq:
7621 template-parameter-list-seq [opt]
7622 template < template-parameter-list > */
7623
7624static void
94edc4ab 7625cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7626{
7627 /* Check for `export'. */
7628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7629 {
7630 /* Consume the `export' token. */
7631 cp_lexer_consume_token (parser->lexer);
7632 /* Warn that we do not support `export'. */
7633 warning ("keyword `export' not implemented, and will be ignored");
7634 }
7635
7636 cp_parser_template_declaration_after_export (parser, member_p);
7637}
7638
7639/* Parse a template-parameter-list.
7640
7641 template-parameter-list:
7642 template-parameter
7643 template-parameter-list , template-parameter
7644
7645 Returns a TREE_LIST. Each node represents a template parameter.
7646 The nodes are connected via their TREE_CHAINs. */
7647
7648static tree
94edc4ab 7649cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7650{
7651 tree parameter_list = NULL_TREE;
7652
7653 while (true)
7654 {
7655 tree parameter;
7656 cp_token *token;
7657
7658 /* Parse the template-parameter. */
7659 parameter = cp_parser_template_parameter (parser);
7660 /* Add it to the list. */
7661 parameter_list = process_template_parm (parameter_list,
7662 parameter);
7663
7664 /* Peek at the next token. */
7665 token = cp_lexer_peek_token (parser->lexer);
7666 /* If it's not a `,', we're done. */
7667 if (token->type != CPP_COMMA)
7668 break;
7669 /* Otherwise, consume the `,' token. */
7670 cp_lexer_consume_token (parser->lexer);
7671 }
7672
7673 return parameter_list;
7674}
7675
7676/* Parse a template-parameter.
7677
7678 template-parameter:
7679 type-parameter
7680 parameter-declaration
7681
7682 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7683 TREE_PURPOSE is the default value, if any. */
7684
7685static tree
94edc4ab 7686cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7687{
7688 cp_token *token;
7689
7690 /* Peek at the next token. */
7691 token = cp_lexer_peek_token (parser->lexer);
7692 /* If it is `class' or `template', we have a type-parameter. */
7693 if (token->keyword == RID_TEMPLATE)
7694 return cp_parser_type_parameter (parser);
7695 /* If it is `class' or `typename' we do not know yet whether it is a
7696 type parameter or a non-type parameter. Consider:
7697
7698 template <typename T, typename T::X X> ...
7699
7700 or:
21526606 7701
a723baf1
MM
7702 template <class C, class D*> ...
7703
7704 Here, the first parameter is a type parameter, and the second is
7705 a non-type parameter. We can tell by looking at the token after
7706 the identifier -- if it is a `,', `=', or `>' then we have a type
7707 parameter. */
7708 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7709 {
7710 /* Peek at the token after `class' or `typename'. */
7711 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7712 /* If it's an identifier, skip it. */
7713 if (token->type == CPP_NAME)
7714 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7715 /* Now, see if the token looks like the end of a template
7716 parameter. */
21526606 7717 if (token->type == CPP_COMMA
a723baf1
MM
7718 || token->type == CPP_EQ
7719 || token->type == CPP_GREATER)
7720 return cp_parser_type_parameter (parser);
7721 }
7722
21526606 7723 /* Otherwise, it is a non-type parameter.
a723baf1
MM
7724
7725 [temp.param]
7726
7727 When parsing a default template-argument for a non-type
7728 template-parameter, the first non-nested `>' is taken as the end
7729 of the template parameter-list rather than a greater-than
7730 operator. */
21526606 7731 return
4bb8ca28
MM
7732 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7733 /*parenthesized_p=*/NULL);
a723baf1
MM
7734}
7735
7736/* Parse a type-parameter.
7737
7738 type-parameter:
7739 class identifier [opt]
7740 class identifier [opt] = type-id
7741 typename identifier [opt]
7742 typename identifier [opt] = type-id
7743 template < template-parameter-list > class identifier [opt]
21526606
EC
7744 template < template-parameter-list > class identifier [opt]
7745 = id-expression
a723baf1
MM
7746
7747 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7748 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7749 the declaration of the parameter. */
7750
7751static tree
94edc4ab 7752cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7753{
7754 cp_token *token;
7755 tree parameter;
7756
7757 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 7758 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7759 "`class', `typename', or `template'");
a723baf1
MM
7760 if (!token)
7761 return error_mark_node;
7762
7763 switch (token->keyword)
7764 {
7765 case RID_CLASS:
7766 case RID_TYPENAME:
7767 {
7768 tree identifier;
7769 tree default_argument;
7770
7771 /* If the next token is an identifier, then it names the
7772 parameter. */
7773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7774 identifier = cp_parser_identifier (parser);
7775 else
7776 identifier = NULL_TREE;
7777
7778 /* Create the parameter. */
7779 parameter = finish_template_type_parm (class_type_node, identifier);
7780
7781 /* If the next token is an `=', we have a default argument. */
7782 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7783 {
7784 /* Consume the `=' token. */
7785 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7786 /* Parse the default-argument. */
a723baf1
MM
7787 default_argument = cp_parser_type_id (parser);
7788 }
7789 else
7790 default_argument = NULL_TREE;
7791
7792 /* Create the combined representation of the parameter and the
7793 default argument. */
c67d36d0 7794 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7795 }
7796 break;
7797
7798 case RID_TEMPLATE:
7799 {
7800 tree parameter_list;
7801 tree identifier;
7802 tree default_argument;
7803
7804 /* Look for the `<'. */
7805 cp_parser_require (parser, CPP_LESS, "`<'");
7806 /* Parse the template-parameter-list. */
7807 begin_template_parm_list ();
21526606 7808 parameter_list
a723baf1
MM
7809 = cp_parser_template_parameter_list (parser);
7810 parameter_list = end_template_parm_list (parameter_list);
7811 /* Look for the `>'. */
7812 cp_parser_require (parser, CPP_GREATER, "`>'");
7813 /* Look for the `class' keyword. */
7814 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7815 /* If the next token is an `=', then there is a
7816 default-argument. If the next token is a `>', we are at
7817 the end of the parameter-list. If the next token is a `,',
7818 then we are at the end of this parameter. */
7819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7820 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7821 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7822 identifier = cp_parser_identifier (parser);
7823 else
7824 identifier = NULL_TREE;
7825 /* Create the template parameter. */
7826 parameter = finish_template_template_parm (class_type_node,
7827 identifier);
21526606 7828
a723baf1
MM
7829 /* If the next token is an `=', then there is a
7830 default-argument. */
7831 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7832 {
b0bc6e8e
KL
7833 bool is_template;
7834
a723baf1
MM
7835 /* Consume the `='. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the id-expression. */
21526606 7838 default_argument
a723baf1
MM
7839 = cp_parser_id_expression (parser,
7840 /*template_keyword_p=*/false,
7841 /*check_dependency_p=*/true,
b0bc6e8e 7842 /*template_p=*/&is_template,
f3c2dfc6 7843 /*declarator_p=*/false);
a3a503a5
GB
7844 if (TREE_CODE (default_argument) == TYPE_DECL)
7845 /* If the id-expression was a template-id that refers to
7846 a template-class, we already have the declaration here,
7847 so no further lookup is needed. */
7848 ;
7849 else
7850 /* Look up the name. */
21526606 7851 default_argument
a3a503a5
GB
7852 = cp_parser_lookup_name (parser, default_argument,
7853 /*is_type=*/false,
7854 /*is_template=*/is_template,
7855 /*is_namespace=*/false,
7856 /*check_dependency=*/true);
a723baf1
MM
7857 /* See if the default argument is valid. */
7858 default_argument
7859 = check_template_template_default_arg (default_argument);
7860 }
7861 else
7862 default_argument = NULL_TREE;
7863
7864 /* Create the combined representation of the parameter and the
7865 default argument. */
c67d36d0 7866 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7867 }
7868 break;
7869
7870 default:
7871 /* Anything else is an error. */
7872 cp_parser_error (parser,
7873 "expected `class', `typename', or `template'");
7874 parameter = error_mark_node;
7875 }
21526606 7876
a723baf1
MM
7877 return parameter;
7878}
7879
7880/* Parse a template-id.
7881
7882 template-id:
7883 template-name < template-argument-list [opt] >
7884
7885 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7886 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7887 returned. Otherwise, if the template-name names a function, or set
7888 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 7889 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
7890
7891 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7892 uninstantiated templates. */
7893
7894static tree
21526606
EC
7895cp_parser_template_id (cp_parser *parser,
7896 bool template_keyword_p,
a668c6ad
MM
7897 bool check_dependency_p,
7898 bool is_declaration)
a723baf1
MM
7899{
7900 tree template;
7901 tree arguments;
a723baf1 7902 tree template_id;
a723baf1
MM
7903 ptrdiff_t start_of_id;
7904 tree access_check = NULL_TREE;
f4abade9 7905 cp_token *next_token, *next_token_2;
a668c6ad 7906 bool is_identifier;
a723baf1
MM
7907
7908 /* If the next token corresponds to a template-id, there is no need
7909 to reparse it. */
2050a1bb
MM
7910 next_token = cp_lexer_peek_token (parser->lexer);
7911 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7912 {
7913 tree value;
7914 tree check;
7915
7916 /* Get the stored value. */
7917 value = cp_lexer_consume_token (parser->lexer)->value;
7918 /* Perform any access checks that were deferred. */
7919 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7920 perform_or_defer_access_check (TREE_PURPOSE (check),
7921 TREE_VALUE (check));
a723baf1
MM
7922 /* Return the stored value. */
7923 return TREE_VALUE (value);
7924 }
7925
2050a1bb
MM
7926 /* Avoid performing name lookup if there is no possibility of
7927 finding a template-id. */
7928 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7929 || (next_token->type == CPP_NAME
21526606 7930 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 7931 (parser, 2)))
2050a1bb
MM
7932 {
7933 cp_parser_error (parser, "expected template-id");
7934 return error_mark_node;
7935 }
7936
a723baf1
MM
7937 /* Remember where the template-id starts. */
7938 if (cp_parser_parsing_tentatively (parser)
7939 && !cp_parser_committed_to_tentative_parse (parser))
7940 {
2050a1bb 7941 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7942 start_of_id = cp_lexer_token_difference (parser->lexer,
7943 parser->lexer->first_token,
7944 next_token);
a723baf1
MM
7945 }
7946 else
7947 start_of_id = -1;
7948
8d241e0b 7949 push_deferring_access_checks (dk_deferred);
cf22909c 7950
a723baf1 7951 /* Parse the template-name. */
a668c6ad 7952 is_identifier = false;
a723baf1 7953 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
7954 check_dependency_p,
7955 is_declaration,
7956 &is_identifier);
7957 if (template == error_mark_node || is_identifier)
cf22909c
KL
7958 {
7959 pop_deferring_access_checks ();
a668c6ad 7960 return template;
cf22909c 7961 }
a723baf1 7962
21526606 7963 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
7964 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7965 parse correctly the argument list. */
7966 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7967 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 7968 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 7969 && next_token->flags & DIGRAPH
21526606 7970 && next_token_2->type == CPP_COLON
f4abade9 7971 && !(next_token_2->flags & PREV_WHITE))
cf22909c 7972 {
f4abade9
GB
7973 cp_parser_parse_tentatively (parser);
7974 /* Change `:' into `::'. */
7975 next_token_2->type = CPP_SCOPE;
7976 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7977 CPP_LESS. */
7978 cp_lexer_consume_token (parser->lexer);
7979 /* Parse the arguments. */
7980 arguments = cp_parser_enclosed_template_argument_list (parser);
7981 if (!cp_parser_parse_definitely (parser))
7982 {
7983 /* If we couldn't parse an argument list, then we revert our changes
7984 and return simply an error. Maybe this is not a template-id
7985 after all. */
7986 next_token_2->type = CPP_COLON;
7987 cp_parser_error (parser, "expected `<'");
7988 pop_deferring_access_checks ();
7989 return error_mark_node;
7990 }
7991 /* Otherwise, emit an error about the invalid digraph, but continue
7992 parsing because we got our argument list. */
7993 pedwarn ("`<::' cannot begin a template-argument list");
7994 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7995 "between `<' and `::'");
7996 if (!flag_permissive)
7997 {
7998 static bool hint;
7999 if (!hint)
8000 {
8001 inform ("(if you use `-fpermissive' G++ will accept your code)");
8002 hint = true;
8003 }
8004 }
8005 }
8006 else
8007 {
8008 /* Look for the `<' that starts the template-argument-list. */
8009 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8010 {
8011 pop_deferring_access_checks ();
8012 return error_mark_node;
8013 }
8014 /* Parse the arguments. */
8015 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8016 }
a723baf1
MM
8017
8018 /* Build a representation of the specialization. */
8019 if (TREE_CODE (template) == IDENTIFIER_NODE)
8020 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8021 else if (DECL_CLASS_TEMPLATE_P (template)
8022 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8023 template_id
8024 = finish_template_type (template, arguments,
8025 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8026 CPP_SCOPE));
8027 else
8028 {
8029 /* If it's not a class-template or a template-template, it should be
8030 a function-template. */
8031 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8032 || TREE_CODE (template) == OVERLOAD
8033 || BASELINK_P (template)),
8034 20010716);
21526606 8035
a723baf1
MM
8036 template_id = lookup_template_function (template, arguments);
8037 }
21526606 8038
cf22909c
KL
8039 /* Retrieve any deferred checks. Do not pop this access checks yet
8040 so the memory will not be reclaimed during token replacing below. */
8041 access_check = get_deferred_access_checks ();
8042
a723baf1
MM
8043 /* If parsing tentatively, replace the sequence of tokens that makes
8044 up the template-id with a CPP_TEMPLATE_ID token. That way,
8045 should we re-parse the token stream, we will not have to repeat
8046 the effort required to do the parse, nor will we issue duplicate
8047 error messages about problems during instantiation of the
8048 template. */
8049 if (start_of_id >= 0)
8050 {
8051 cp_token *token;
a723baf1
MM
8052
8053 /* Find the token that corresponds to the start of the
8054 template-id. */
21526606 8055 token = cp_lexer_advance_token (parser->lexer,
a723baf1
MM
8056 parser->lexer->first_token,
8057 start_of_id);
8058
a723baf1
MM
8059 /* Reset the contents of the START_OF_ID token. */
8060 token->type = CPP_TEMPLATE_ID;
8061 token->value = build_tree_list (access_check, template_id);
8062 token->keyword = RID_MAX;
8063 /* Purge all subsequent tokens. */
8064 cp_lexer_purge_tokens_after (parser->lexer, token);
8065 }
8066
cf22909c 8067 pop_deferring_access_checks ();
a723baf1
MM
8068 return template_id;
8069}
8070
8071/* Parse a template-name.
8072
8073 template-name:
8074 identifier
21526606 8075
a723baf1
MM
8076 The standard should actually say:
8077
8078 template-name:
8079 identifier
8080 operator-function-id
a723baf1
MM
8081
8082 A defect report has been filed about this issue.
8083
0d956474
GB
8084 A conversion-function-id cannot be a template name because they cannot
8085 be part of a template-id. In fact, looking at this code:
8086
8087 a.operator K<int>()
8088
8089 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8090 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8091 explicit argument list, since the only allowed template parameter is
8092 the type to which it is converting.
8093
a723baf1
MM
8094 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8095 `template' keyword, in a construction like:
8096
8097 T::template f<3>()
8098
8099 In that case `f' is taken to be a template-name, even though there
8100 is no way of knowing for sure.
8101
8102 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8103 name refers to a set of overloaded functions, at least one of which
8104 is a template, or an IDENTIFIER_NODE with the name of the template,
8105 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8106 names are looked up inside uninstantiated templates. */
8107
8108static tree
21526606
EC
8109cp_parser_template_name (cp_parser* parser,
8110 bool template_keyword_p,
a668c6ad
MM
8111 bool check_dependency_p,
8112 bool is_declaration,
8113 bool *is_identifier)
a723baf1
MM
8114{
8115 tree identifier;
8116 tree decl;
8117 tree fns;
8118
8119 /* If the next token is `operator', then we have either an
8120 operator-function-id or a conversion-function-id. */
8121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8122 {
8123 /* We don't know whether we're looking at an
8124 operator-function-id or a conversion-function-id. */
8125 cp_parser_parse_tentatively (parser);
8126 /* Try an operator-function-id. */
8127 identifier = cp_parser_operator_function_id (parser);
8128 /* If that didn't work, try a conversion-function-id. */
8129 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8130 {
8131 cp_parser_error (parser, "expected template-name");
8132 return error_mark_node;
8133 }
a723baf1
MM
8134 }
8135 /* Look for the identifier. */
8136 else
8137 identifier = cp_parser_identifier (parser);
21526606 8138
a723baf1
MM
8139 /* If we didn't find an identifier, we don't have a template-id. */
8140 if (identifier == error_mark_node)
8141 return error_mark_node;
8142
8143 /* If the name immediately followed the `template' keyword, then it
8144 is a template-name. However, if the next token is not `<', then
8145 we do not treat it as a template-name, since it is not being used
8146 as part of a template-id. This enables us to handle constructs
8147 like:
8148
8149 template <typename T> struct S { S(); };
8150 template <typename T> S<T>::S();
8151
8152 correctly. We would treat `S' as a template -- if it were `S<T>'
8153 -- but we do not if there is no `<'. */
a668c6ad
MM
8154
8155 if (processing_template_decl
f4abade9 8156 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8157 {
8158 /* In a declaration, in a dependent context, we pretend that the
8159 "template" keyword was present in order to improve error
8160 recovery. For example, given:
21526606 8161
a668c6ad 8162 template <typename T> void f(T::X<int>);
21526606 8163
a668c6ad 8164 we want to treat "X<int>" as a template-id. */
21526606
EC
8165 if (is_declaration
8166 && !template_keyword_p
a668c6ad
MM
8167 && parser->scope && TYPE_P (parser->scope)
8168 && dependent_type_p (parser->scope))
8169 {
8170 ptrdiff_t start;
8171 cp_token* token;
8172 /* Explain what went wrong. */
8173 error ("non-template `%D' used as template", identifier);
8174 error ("(use `%T::template %D' to indicate that it is a template)",
8175 parser->scope, identifier);
8176 /* If parsing tentatively, find the location of the "<"
8177 token. */
8178 if (cp_parser_parsing_tentatively (parser)
8179 && !cp_parser_committed_to_tentative_parse (parser))
8180 {
8181 cp_parser_simulate_error (parser);
8182 token = cp_lexer_peek_token (parser->lexer);
8183 token = cp_lexer_prev_token (parser->lexer, token);
8184 start = cp_lexer_token_difference (parser->lexer,
8185 parser->lexer->first_token,
8186 token);
8187 }
8188 else
8189 start = -1;
8190 /* Parse the template arguments so that we can issue error
8191 messages about them. */
8192 cp_lexer_consume_token (parser->lexer);
8193 cp_parser_enclosed_template_argument_list (parser);
8194 /* Skip tokens until we find a good place from which to
8195 continue parsing. */
8196 cp_parser_skip_to_closing_parenthesis (parser,
8197 /*recovering=*/true,
8198 /*or_comma=*/true,
8199 /*consume_paren=*/false);
8200 /* If parsing tentatively, permanently remove the
8201 template argument list. That will prevent duplicate
8202 error messages from being issued about the missing
8203 "template" keyword. */
8204 if (start >= 0)
8205 {
8206 token = cp_lexer_advance_token (parser->lexer,
8207 parser->lexer->first_token,
8208 start);
8209 cp_lexer_purge_tokens_after (parser->lexer, token);
8210 }
8211 if (is_identifier)
8212 *is_identifier = true;
8213 return identifier;
8214 }
9d363a56
MM
8215
8216 /* If the "template" keyword is present, then there is generally
8217 no point in doing name-lookup, so we just return IDENTIFIER.
8218 But, if the qualifying scope is non-dependent then we can
8219 (and must) do name-lookup normally. */
8220 if (template_keyword_p
8221 && (!parser->scope
8222 || (TYPE_P (parser->scope)
8223 && dependent_type_p (parser->scope))))
a668c6ad
MM
8224 return identifier;
8225 }
a723baf1
MM
8226
8227 /* Look up the name. */
8228 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8229 /*is_type=*/false,
b0bc6e8e 8230 /*is_template=*/false,
eea9800f 8231 /*is_namespace=*/false,
a723baf1
MM
8232 check_dependency_p);
8233 decl = maybe_get_template_decl_from_type_decl (decl);
8234
8235 /* If DECL is a template, then the name was a template-name. */
8236 if (TREE_CODE (decl) == TEMPLATE_DECL)
8237 ;
21526606 8238 else
a723baf1
MM
8239 {
8240 /* The standard does not explicitly indicate whether a name that
8241 names a set of overloaded declarations, some of which are
8242 templates, is a template-name. However, such a name should
8243 be a template-name; otherwise, there is no way to form a
8244 template-id for the overloaded templates. */
8245 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8246 if (TREE_CODE (fns) == OVERLOAD)
8247 {
8248 tree fn;
21526606 8249
a723baf1
MM
8250 for (fn = fns; fn; fn = OVL_NEXT (fn))
8251 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8252 break;
8253 }
8254 else
8255 {
8256 /* Otherwise, the name does not name a template. */
8257 cp_parser_error (parser, "expected template-name");
8258 return error_mark_node;
8259 }
8260 }
8261
8262 /* If DECL is dependent, and refers to a function, then just return
8263 its name; we will look it up again during template instantiation. */
8264 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8265 {
8266 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8267 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8268 return identifier;
8269 }
8270
8271 return decl;
8272}
8273
8274/* Parse a template-argument-list.
8275
8276 template-argument-list:
8277 template-argument
8278 template-argument-list , template-argument
8279
04c06002 8280 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8281
8282static tree
94edc4ab 8283cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8284{
bf12d54d
NS
8285 tree fixed_args[10];
8286 unsigned n_args = 0;
8287 unsigned alloced = 10;
8288 tree *arg_ary = fixed_args;
8289 tree vec;
4bb8ca28 8290 bool saved_in_template_argument_list_p;
a723baf1 8291
4bb8ca28
MM
8292 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8293 parser->in_template_argument_list_p = true;
bf12d54d 8294 do
a723baf1
MM
8295 {
8296 tree argument;
8297
bf12d54d 8298 if (n_args)
04c06002 8299 /* Consume the comma. */
bf12d54d 8300 cp_lexer_consume_token (parser->lexer);
21526606 8301
a723baf1
MM
8302 /* Parse the template-argument. */
8303 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8304 if (n_args == alloced)
8305 {
8306 alloced *= 2;
21526606 8307
bf12d54d
NS
8308 if (arg_ary == fixed_args)
8309 {
8310 arg_ary = xmalloc (sizeof (tree) * alloced);
8311 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8312 }
8313 else
8314 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8315 }
8316 arg_ary[n_args++] = argument;
a723baf1 8317 }
bf12d54d
NS
8318 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8319
8320 vec = make_tree_vec (n_args);
a723baf1 8321
bf12d54d
NS
8322 while (n_args--)
8323 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8324
bf12d54d
NS
8325 if (arg_ary != fixed_args)
8326 free (arg_ary);
4bb8ca28 8327 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8328 return vec;
a723baf1
MM
8329}
8330
8331/* Parse a template-argument.
8332
8333 template-argument:
8334 assignment-expression
8335 type-id
8336 id-expression
8337
8338 The representation is that of an assignment-expression, type-id, or
8339 id-expression -- except that the qualified id-expression is
8340 evaluated, so that the value returned is either a DECL or an
21526606 8341 OVERLOAD.
d17811fd
MM
8342
8343 Although the standard says "assignment-expression", it forbids
8344 throw-expressions or assignments in the template argument.
8345 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8346
8347static tree
94edc4ab 8348cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8349{
8350 tree argument;
8351 bool template_p;
d17811fd 8352 bool address_p;
4d5297fa 8353 bool maybe_type_id = false;
d17811fd 8354 cp_token *token;
b3445994 8355 cp_id_kind idk;
d17811fd 8356 tree qualifying_class;
a723baf1
MM
8357
8358 /* There's really no way to know what we're looking at, so we just
21526606 8359 try each alternative in order.
a723baf1
MM
8360
8361 [temp.arg]
8362
8363 In a template-argument, an ambiguity between a type-id and an
8364 expression is resolved to a type-id, regardless of the form of
21526606 8365 the corresponding template-parameter.
a723baf1
MM
8366
8367 Therefore, we try a type-id first. */
8368 cp_parser_parse_tentatively (parser);
a723baf1 8369 argument = cp_parser_type_id (parser);
4d5297fa 8370 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8371 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8372 also valid expressions. For instance:
8373
8374 struct X { int operator >> (int); };
8375 template <int V> struct Foo {};
8376 Foo<X () >> 5> r;
8377
8378 Here 'X()' is a valid type-id of a function type, but the user just
8379 wanted to write the expression "X() >> 5". Thus, we remember that we
8380 found a valid type-id, but we still try to parse the argument as an
8381 expression to see what happens. */
8382 if (!cp_parser_error_occurred (parser)
8383 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8384 {
8385 maybe_type_id = true;
8386 cp_parser_abort_tentative_parse (parser);
8387 }
8388 else
8389 {
8390 /* If the next token isn't a `,' or a `>', then this argument wasn't
8391 really finished. This means that the argument is not a valid
8392 type-id. */
8393 if (!cp_parser_next_token_ends_template_argument_p (parser))
8394 cp_parser_error (parser, "expected template-argument");
8395 /* If that worked, we're done. */
8396 if (cp_parser_parse_definitely (parser))
8397 return argument;
8398 }
a723baf1
MM
8399 /* We're still not sure what the argument will be. */
8400 cp_parser_parse_tentatively (parser);
8401 /* Try a template. */
21526606 8402 argument = cp_parser_id_expression (parser,
a723baf1
MM
8403 /*template_keyword_p=*/false,
8404 /*check_dependency_p=*/true,
f3c2dfc6
MM
8405 &template_p,
8406 /*declarator_p=*/false);
a723baf1
MM
8407 /* If the next token isn't a `,' or a `>', then this argument wasn't
8408 really finished. */
d17811fd 8409 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8410 cp_parser_error (parser, "expected template-argument");
8411 if (!cp_parser_error_occurred (parser))
8412 {
8413 /* Figure out what is being referred to. */
5b4acce1
KL
8414 argument = cp_parser_lookup_name (parser, argument,
8415 /*is_type=*/false,
8416 /*is_template=*/template_p,
8417 /*is_namespace=*/false,
8418 /*check_dependency=*/true);
8419 if (TREE_CODE (argument) != TEMPLATE_DECL
8420 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8421 cp_parser_error (parser, "expected template-name");
8422 }
8423 if (cp_parser_parse_definitely (parser))
8424 return argument;
d17811fd
MM
8425 /* It must be a non-type argument. There permitted cases are given
8426 in [temp.arg.nontype]:
8427
8428 -- an integral constant-expression of integral or enumeration
8429 type; or
8430
8431 -- the name of a non-type template-parameter; or
8432
8433 -- the name of an object or function with external linkage...
8434
8435 -- the address of an object or function with external linkage...
8436
04c06002 8437 -- a pointer to member... */
d17811fd
MM
8438 /* Look for a non-type template parameter. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8440 {
8441 cp_parser_parse_tentatively (parser);
8442 argument = cp_parser_primary_expression (parser,
8443 &idk,
8444 &qualifying_class);
8445 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8446 || !cp_parser_next_token_ends_template_argument_p (parser))
8447 cp_parser_simulate_error (parser);
8448 if (cp_parser_parse_definitely (parser))
8449 return argument;
8450 }
8451 /* If the next token is "&", the argument must be the address of an
8452 object or function with external linkage. */
8453 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8454 if (address_p)
8455 cp_lexer_consume_token (parser->lexer);
8456 /* See if we might have an id-expression. */
8457 token = cp_lexer_peek_token (parser->lexer);
8458 if (token->type == CPP_NAME
8459 || token->keyword == RID_OPERATOR
8460 || token->type == CPP_SCOPE
8461 || token->type == CPP_TEMPLATE_ID
8462 || token->type == CPP_NESTED_NAME_SPECIFIER)
8463 {
8464 cp_parser_parse_tentatively (parser);
8465 argument = cp_parser_primary_expression (parser,
8466 &idk,
8467 &qualifying_class);
8468 if (cp_parser_error_occurred (parser)
8469 || !cp_parser_next_token_ends_template_argument_p (parser))
8470 cp_parser_abort_tentative_parse (parser);
8471 else
8472 {
8473 if (qualifying_class)
8474 argument = finish_qualified_id_expr (qualifying_class,
8475 argument,
8476 /*done=*/true,
8477 address_p);
8478 if (TREE_CODE (argument) == VAR_DECL)
8479 {
8480 /* A variable without external linkage might still be a
8481 valid constant-expression, so no error is issued here
8482 if the external-linkage check fails. */
8483 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8484 cp_parser_simulate_error (parser);
8485 }
8486 else if (is_overloaded_fn (argument))
8487 /* All overloaded functions are allowed; if the external
8488 linkage test does not pass, an error will be issued
8489 later. */
8490 ;
8491 else if (address_p
21526606 8492 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8493 || TREE_CODE (argument) == SCOPE_REF))
8494 /* A pointer-to-member. */
8495 ;
8496 else
8497 cp_parser_simulate_error (parser);
8498
8499 if (cp_parser_parse_definitely (parser))
8500 {
8501 if (address_p)
8502 argument = build_x_unary_op (ADDR_EXPR, argument);
8503 return argument;
8504 }
8505 }
8506 }
8507 /* If the argument started with "&", there are no other valid
8508 alternatives at this point. */
8509 if (address_p)
8510 {
8511 cp_parser_error (parser, "invalid non-type template argument");
8512 return error_mark_node;
8513 }
4d5297fa 8514 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8515 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8516 Otherwise, we try parsing the constant-expression tentatively,
8517 because the argument could really be a type-id. */
8518 if (maybe_type_id)
8519 cp_parser_parse_tentatively (parser);
21526606 8520 argument = cp_parser_constant_expression (parser,
d17811fd
MM
8521 /*allow_non_constant_p=*/false,
8522 /*non_constant_p=*/NULL);
9baa27a9 8523 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
8524 if (!maybe_type_id)
8525 return argument;
8526 if (!cp_parser_next_token_ends_template_argument_p (parser))
8527 cp_parser_error (parser, "expected template-argument");
8528 if (cp_parser_parse_definitely (parser))
8529 return argument;
8530 /* We did our best to parse the argument as a non type-id, but that
8531 was the only alternative that matched (albeit with a '>' after
21526606 8532 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
8533 diagnostic will then be issued. */
8534 return cp_parser_type_id (parser);
a723baf1
MM
8535}
8536
8537/* Parse an explicit-instantiation.
8538
8539 explicit-instantiation:
21526606 8540 template declaration
a723baf1
MM
8541
8542 Although the standard says `declaration', what it really means is:
8543
8544 explicit-instantiation:
21526606 8545 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
8546
8547 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8548 supposed to be allowed. A defect report has been filed about this
21526606 8549 issue.
a723baf1
MM
8550
8551 GNU Extension:
21526606 8552
a723baf1 8553 explicit-instantiation:
21526606 8554 storage-class-specifier template
a723baf1 8555 decl-specifier-seq [opt] declarator [opt] ;
21526606 8556 function-specifier template
a723baf1
MM
8557 decl-specifier-seq [opt] declarator [opt] ; */
8558
8559static void
94edc4ab 8560cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8561{
560ad596 8562 int declares_class_or_enum;
a723baf1
MM
8563 tree decl_specifiers;
8564 tree attributes;
8565 tree extension_specifier = NULL_TREE;
8566
8567 /* Look for an (optional) storage-class-specifier or
8568 function-specifier. */
8569 if (cp_parser_allow_gnu_extensions_p (parser))
8570 {
21526606 8571 extension_specifier
a723baf1
MM
8572 = cp_parser_storage_class_specifier_opt (parser);
8573 if (!extension_specifier)
8574 extension_specifier = cp_parser_function_specifier_opt (parser);
8575 }
8576
8577 /* Look for the `template' keyword. */
8578 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8579 /* Let the front end know that we are processing an explicit
8580 instantiation. */
8581 begin_explicit_instantiation ();
8582 /* [temp.explicit] says that we are supposed to ignore access
8583 control while processing explicit instantiation directives. */
78757caa 8584 push_deferring_access_checks (dk_no_check);
a723baf1 8585 /* Parse a decl-specifier-seq. */
21526606 8586 decl_specifiers
a723baf1
MM
8587 = cp_parser_decl_specifier_seq (parser,
8588 CP_PARSER_FLAGS_OPTIONAL,
8589 &attributes,
8590 &declares_class_or_enum);
8591 /* If there was exactly one decl-specifier, and it declared a class,
8592 and there's no declarator, then we have an explicit type
8593 instantiation. */
8594 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8595 {
8596 tree type;
8597
8598 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8599 /* Turn access control back on for names used during
8600 template instantiation. */
8601 pop_deferring_access_checks ();
a723baf1
MM
8602 if (type)
8603 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8604 }
8605 else
8606 {
8607 tree declarator;
8608 tree decl;
8609
8610 /* Parse the declarator. */
21526606 8611 declarator
62b8a44e 8612 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
8613 /*ctor_dtor_or_conv_p=*/NULL,
8614 /*parenthesized_p=*/NULL);
21526606 8615 cp_parser_check_for_definition_in_return_type (declarator,
560ad596 8616 declares_class_or_enum);
216bb6e1
MM
8617 if (declarator != error_mark_node)
8618 {
21526606 8619 decl = grokdeclarator (declarator, decl_specifiers,
216bb6e1
MM
8620 NORMAL, 0, NULL);
8621 /* Turn access control back on for names used during
8622 template instantiation. */
8623 pop_deferring_access_checks ();
8624 /* Do the explicit instantiation. */
8625 do_decl_instantiation (decl, extension_specifier);
8626 }
8627 else
8628 {
8629 pop_deferring_access_checks ();
8630 /* Skip the body of the explicit instantiation. */
8631 cp_parser_skip_to_end_of_statement (parser);
8632 }
a723baf1
MM
8633 }
8634 /* We're done with the instantiation. */
8635 end_explicit_instantiation ();
a723baf1 8636
e0860732 8637 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8638}
8639
8640/* Parse an explicit-specialization.
8641
8642 explicit-specialization:
21526606 8643 template < > declaration
a723baf1
MM
8644
8645 Although the standard says `declaration', what it really means is:
8646
8647 explicit-specialization:
8648 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 8649 template <> function-definition
a723baf1
MM
8650 template <> explicit-specialization
8651 template <> template-declaration */
8652
8653static void
94edc4ab 8654cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8655{
8656 /* Look for the `template' keyword. */
8657 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8658 /* Look for the `<'. */
8659 cp_parser_require (parser, CPP_LESS, "`<'");
8660 /* Look for the `>'. */
8661 cp_parser_require (parser, CPP_GREATER, "`>'");
8662 /* We have processed another parameter list. */
8663 ++parser->num_template_parameter_lists;
8664 /* Let the front end know that we are beginning a specialization. */
8665 begin_specialization ();
8666
8667 /* If the next keyword is `template', we need to figure out whether
8668 or not we're looking a template-declaration. */
8669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8670 {
8671 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8672 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8673 cp_parser_template_declaration_after_export (parser,
8674 /*member_p=*/false);
8675 else
8676 cp_parser_explicit_specialization (parser);
8677 }
8678 else
8679 /* Parse the dependent declaration. */
21526606 8680 cp_parser_single_declaration (parser,
a723baf1
MM
8681 /*member_p=*/false,
8682 /*friend_p=*/NULL);
8683
8684 /* We're done with the specialization. */
8685 end_specialization ();
8686 /* We're done with this parameter list. */
8687 --parser->num_template_parameter_lists;
8688}
8689
8690/* Parse a type-specifier.
8691
8692 type-specifier:
8693 simple-type-specifier
8694 class-specifier
8695 enum-specifier
8696 elaborated-type-specifier
8697 cv-qualifier
8698
8699 GNU Extension:
8700
8701 type-specifier:
8702 __complex__
8703
8704 Returns a representation of the type-specifier. If the
8705 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8706 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8707 For a class-specifier, enum-specifier, or elaborated-type-specifier
8708 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8709
8710 If IS_FRIEND is TRUE then this type-specifier is being declared a
8711 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8712 appearing in a decl-specifier-seq.
8713
8714 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8715 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8716 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8717 if a type is declared; 2 if it is defined. Otherwise, it is set to
8718 zero.
a723baf1
MM
8719
8720 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8721 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8722 is set to FALSE. */
8723
8724static tree
21526606
EC
8725cp_parser_type_specifier (cp_parser* parser,
8726 cp_parser_flags flags,
94edc4ab
NN
8727 bool is_friend,
8728 bool is_declaration,
560ad596 8729 int* declares_class_or_enum,
94edc4ab 8730 bool* is_cv_qualifier)
a723baf1
MM
8731{
8732 tree type_spec = NULL_TREE;
8733 cp_token *token;
8734 enum rid keyword;
8735
8736 /* Assume this type-specifier does not declare a new type. */
8737 if (declares_class_or_enum)
543ca912 8738 *declares_class_or_enum = 0;
a723baf1
MM
8739 /* And that it does not specify a cv-qualifier. */
8740 if (is_cv_qualifier)
8741 *is_cv_qualifier = false;
8742 /* Peek at the next token. */
8743 token = cp_lexer_peek_token (parser->lexer);
8744
8745 /* If we're looking at a keyword, we can use that to guide the
8746 production we choose. */
8747 keyword = token->keyword;
8748 switch (keyword)
8749 {
8750 /* Any of these indicate either a class-specifier, or an
8751 elaborated-type-specifier. */
8752 case RID_CLASS:
8753 case RID_STRUCT:
8754 case RID_UNION:
8755 case RID_ENUM:
8756 /* Parse tentatively so that we can back up if we don't find a
8757 class-specifier or enum-specifier. */
8758 cp_parser_parse_tentatively (parser);
8759 /* Look for the class-specifier or enum-specifier. */
8760 if (keyword == RID_ENUM)
8761 type_spec = cp_parser_enum_specifier (parser);
8762 else
8763 type_spec = cp_parser_class_specifier (parser);
8764
8765 /* If that worked, we're done. */
8766 if (cp_parser_parse_definitely (parser))
8767 {
8768 if (declares_class_or_enum)
560ad596 8769 *declares_class_or_enum = 2;
a723baf1
MM
8770 return type_spec;
8771 }
8772
8773 /* Fall through. */
8774
8775 case RID_TYPENAME:
8776 /* Look for an elaborated-type-specifier. */
8777 type_spec = cp_parser_elaborated_type_specifier (parser,
8778 is_friend,
8779 is_declaration);
8780 /* We're declaring a class or enum -- unless we're using
8781 `typename'. */
8782 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8783 *declares_class_or_enum = 1;
a723baf1
MM
8784 return type_spec;
8785
8786 case RID_CONST:
8787 case RID_VOLATILE:
8788 case RID_RESTRICT:
8789 type_spec = cp_parser_cv_qualifier_opt (parser);
8790 /* Even though we call a routine that looks for an optional
8791 qualifier, we know that there should be one. */
8792 my_friendly_assert (type_spec != NULL, 20000328);
8793 /* This type-specifier was a cv-qualified. */
8794 if (is_cv_qualifier)
8795 *is_cv_qualifier = true;
8796
8797 return type_spec;
8798
8799 case RID_COMPLEX:
8800 /* The `__complex__' keyword is a GNU extension. */
8801 return cp_lexer_consume_token (parser->lexer)->value;
8802
8803 default:
8804 break;
8805 }
8806
8807 /* If we do not already have a type-specifier, assume we are looking
8808 at a simple-type-specifier. */
21526606 8809 type_spec = cp_parser_simple_type_specifier (parser, flags,
4b0d3cbe 8810 /*identifier_p=*/true);
a723baf1
MM
8811
8812 /* If we didn't find a type-specifier, and a type-specifier was not
8813 optional in this context, issue an error message. */
8814 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8815 {
8816 cp_parser_error (parser, "expected type specifier");
8817 return error_mark_node;
8818 }
8819
8820 return type_spec;
8821}
8822
8823/* Parse a simple-type-specifier.
8824
8825 simple-type-specifier:
8826 :: [opt] nested-name-specifier [opt] type-name
8827 :: [opt] nested-name-specifier template template-id
8828 char
8829 wchar_t
8830 bool
8831 short
8832 int
8833 long
8834 signed
8835 unsigned
8836 float
8837 double
21526606 8838 void
a723baf1
MM
8839
8840 GNU Extension:
8841
8842 simple-type-specifier:
8843 __typeof__ unary-expression
8844 __typeof__ ( type-id )
8845
8846 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8847 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8848 For the first two productions, and if IDENTIFIER_P is false, the
8849 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8850
8851static tree
4b0d3cbe
MM
8852cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8853 bool identifier_p)
a723baf1
MM
8854{
8855 tree type = NULL_TREE;
8856 cp_token *token;
8857
8858 /* Peek at the next token. */
8859 token = cp_lexer_peek_token (parser->lexer);
8860
8861 /* If we're looking at a keyword, things are easy. */
8862 switch (token->keyword)
8863 {
8864 case RID_CHAR:
4b0d3cbe
MM
8865 type = char_type_node;
8866 break;
a723baf1 8867 case RID_WCHAR:
4b0d3cbe
MM
8868 type = wchar_type_node;
8869 break;
a723baf1 8870 case RID_BOOL:
4b0d3cbe
MM
8871 type = boolean_type_node;
8872 break;
a723baf1 8873 case RID_SHORT:
4b0d3cbe
MM
8874 type = short_integer_type_node;
8875 break;
a723baf1 8876 case RID_INT:
4b0d3cbe
MM
8877 type = integer_type_node;
8878 break;
a723baf1 8879 case RID_LONG:
4b0d3cbe
MM
8880 type = long_integer_type_node;
8881 break;
a723baf1 8882 case RID_SIGNED:
4b0d3cbe
MM
8883 type = integer_type_node;
8884 break;
a723baf1 8885 case RID_UNSIGNED:
4b0d3cbe
MM
8886 type = unsigned_type_node;
8887 break;
a723baf1 8888 case RID_FLOAT:
4b0d3cbe
MM
8889 type = float_type_node;
8890 break;
a723baf1 8891 case RID_DOUBLE:
4b0d3cbe
MM
8892 type = double_type_node;
8893 break;
a723baf1 8894 case RID_VOID:
4b0d3cbe
MM
8895 type = void_type_node;
8896 break;
a723baf1
MM
8897
8898 case RID_TYPEOF:
8899 {
8900 tree operand;
8901
8902 /* Consume the `typeof' token. */
8903 cp_lexer_consume_token (parser->lexer);
04c06002 8904 /* Parse the operand to `typeof'. */
a723baf1
MM
8905 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8906 /* If it is not already a TYPE, take its type. */
8907 if (!TYPE_P (operand))
8908 operand = finish_typeof (operand);
8909
8910 return operand;
8911 }
8912
8913 default:
8914 break;
8915 }
8916
4b0d3cbe
MM
8917 /* If the type-specifier was for a built-in type, we're done. */
8918 if (type)
8919 {
8920 tree id;
8921
8922 /* Consume the token. */
8923 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
8924
8925 /* There is no valid C++ program where a non-template type is
8926 followed by a "<". That usually indicates that the user thought
8927 that the type was a template. */
8928 cp_parser_check_for_invalid_template_id (parser, type);
8929
4b0d3cbe
MM
8930 return identifier_p ? id : TYPE_NAME (type);
8931 }
8932
a723baf1 8933 /* The type-specifier must be a user-defined type. */
21526606 8934 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1
MM
8935 {
8936 /* Don't gobble tokens or issue error messages if this is an
8937 optional type-specifier. */
8938 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8939 cp_parser_parse_tentatively (parser);
8940
8941 /* Look for the optional `::' operator. */
8942 cp_parser_global_scope_opt (parser,
8943 /*current_scope_valid_p=*/false);
8944 /* Look for the nested-name specifier. */
8945 cp_parser_nested_name_specifier_opt (parser,
8946 /*typename_keyword_p=*/false,
8947 /*check_dependency_p=*/true,
a668c6ad
MM
8948 /*type_p=*/false,
8949 /*is_declaration=*/false);
a723baf1
MM
8950 /* If we have seen a nested-name-specifier, and the next token
8951 is `template', then we are using the template-id production. */
21526606 8952 if (parser->scope
a723baf1
MM
8953 && cp_parser_optional_template_keyword (parser))
8954 {
8955 /* Look for the template-id. */
21526606 8956 type = cp_parser_template_id (parser,
a723baf1 8957 /*template_keyword_p=*/true,
a668c6ad
MM
8958 /*check_dependency_p=*/true,
8959 /*is_declaration=*/false);
a723baf1
MM
8960 /* If the template-id did not name a type, we are out of
8961 luck. */
8962 if (TREE_CODE (type) != TYPE_DECL)
8963 {
8964 cp_parser_error (parser, "expected template-id for type");
8965 type = NULL_TREE;
8966 }
8967 }
8968 /* Otherwise, look for a type-name. */
8969 else
4bb8ca28 8970 type = cp_parser_type_name (parser);
a723baf1 8971 /* If it didn't work out, we don't have a TYPE. */
21526606 8972 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
8973 && !cp_parser_parse_definitely (parser))
8974 type = NULL_TREE;
8975 }
8976
8977 /* If we didn't get a type-name, issue an error message. */
8978 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8979 {
8980 cp_parser_error (parser, "expected type-name");
8981 return error_mark_node;
8982 }
8983
a668c6ad
MM
8984 /* There is no valid C++ program where a non-template type is
8985 followed by a "<". That usually indicates that the user thought
8986 that the type was a template. */
4bb8ca28 8987 if (type && type != error_mark_node)
ee43dab5 8988 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 8989
a723baf1
MM
8990 return type;
8991}
8992
8993/* Parse a type-name.
8994
8995 type-name:
8996 class-name
8997 enum-name
21526606 8998 typedef-name
a723baf1
MM
8999
9000 enum-name:
9001 identifier
9002
9003 typedef-name:
21526606 9004 identifier
a723baf1
MM
9005
9006 Returns a TYPE_DECL for the the type. */
9007
9008static tree
94edc4ab 9009cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9010{
9011 tree type_decl;
9012 tree identifier;
9013
9014 /* We can't know yet whether it is a class-name or not. */
9015 cp_parser_parse_tentatively (parser);
9016 /* Try a class-name. */
21526606 9017 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9018 /*typename_keyword_p=*/false,
9019 /*template_keyword_p=*/false,
9020 /*type_p=*/false,
a723baf1 9021 /*check_dependency_p=*/true,
a668c6ad
MM
9022 /*class_head_p=*/false,
9023 /*is_declaration=*/false);
a723baf1
MM
9024 /* If it's not a class-name, keep looking. */
9025 if (!cp_parser_parse_definitely (parser))
9026 {
9027 /* It must be a typedef-name or an enum-name. */
9028 identifier = cp_parser_identifier (parser);
9029 if (identifier == error_mark_node)
9030 return error_mark_node;
21526606 9031
a723baf1
MM
9032 /* Look up the type-name. */
9033 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9034 /* Issue an error if we did not find a type-name. */
9035 if (TREE_CODE (type_decl) != TYPE_DECL)
9036 {
4bb8ca28 9037 if (!cp_parser_simulate_error (parser))
21526606 9038 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9039 "is not a type");
a723baf1
MM
9040 type_decl = error_mark_node;
9041 }
9042 /* Remember that the name was used in the definition of the
9043 current class so that we can check later to see if the
9044 meaning would have been different after the class was
9045 entirely defined. */
9046 else if (type_decl != error_mark_node
9047 && !parser->scope)
9048 maybe_note_name_used_in_class (identifier, type_decl);
9049 }
21526606 9050
a723baf1
MM
9051 return type_decl;
9052}
9053
9054
9055/* Parse an elaborated-type-specifier. Note that the grammar given
9056 here incorporates the resolution to DR68.
9057
9058 elaborated-type-specifier:
9059 class-key :: [opt] nested-name-specifier [opt] identifier
9060 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9061 enum :: [opt] nested-name-specifier [opt] identifier
9062 typename :: [opt] nested-name-specifier identifier
21526606
EC
9063 typename :: [opt] nested-name-specifier template [opt]
9064 template-id
a723baf1 9065
360d1b99
MM
9066 GNU extension:
9067
9068 elaborated-type-specifier:
9069 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9070 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9071 template [opt] template-id
9072 enum attributes :: [opt] nested-name-specifier [opt] identifier
9073
a723baf1
MM
9074 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9075 declared `friend'. If IS_DECLARATION is TRUE, then this
9076 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9077 something is being declared.
9078
9079 Returns the TYPE specified. */
9080
9081static tree
21526606
EC
9082cp_parser_elaborated_type_specifier (cp_parser* parser,
9083 bool is_friend,
94edc4ab 9084 bool is_declaration)
a723baf1
MM
9085{
9086 enum tag_types tag_type;
9087 tree identifier;
9088 tree type = NULL_TREE;
360d1b99 9089 tree attributes = NULL_TREE;
a723baf1
MM
9090
9091 /* See if we're looking at the `enum' keyword. */
9092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9093 {
9094 /* Consume the `enum' token. */
9095 cp_lexer_consume_token (parser->lexer);
9096 /* Remember that it's an enumeration type. */
9097 tag_type = enum_type;
360d1b99
MM
9098 /* Parse the attributes. */
9099 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9100 }
9101 /* Or, it might be `typename'. */
9102 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9103 RID_TYPENAME))
9104 {
9105 /* Consume the `typename' token. */
9106 cp_lexer_consume_token (parser->lexer);
9107 /* Remember that it's a `typename' type. */
9108 tag_type = typename_type;
9109 /* The `typename' keyword is only allowed in templates. */
9110 if (!processing_template_decl)
9111 pedwarn ("using `typename' outside of template");
9112 }
9113 /* Otherwise it must be a class-key. */
9114 else
9115 {
9116 tag_type = cp_parser_class_key (parser);
9117 if (tag_type == none_type)
9118 return error_mark_node;
360d1b99
MM
9119 /* Parse the attributes. */
9120 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9121 }
9122
9123 /* Look for the `::' operator. */
21526606 9124 cp_parser_global_scope_opt (parser,
a723baf1
MM
9125 /*current_scope_valid_p=*/false);
9126 /* Look for the nested-name-specifier. */
9127 if (tag_type == typename_type)
8fa1ad0e
MM
9128 {
9129 if (cp_parser_nested_name_specifier (parser,
9130 /*typename_keyword_p=*/true,
9131 /*check_dependency_p=*/true,
a668c6ad 9132 /*type_p=*/true,
21526606 9133 is_declaration)
8fa1ad0e
MM
9134 == error_mark_node)
9135 return error_mark_node;
9136 }
a723baf1
MM
9137 else
9138 /* Even though `typename' is not present, the proposed resolution
9139 to Core Issue 180 says that in `class A<T>::B', `B' should be
9140 considered a type-name, even if `A<T>' is dependent. */
9141 cp_parser_nested_name_specifier_opt (parser,
9142 /*typename_keyword_p=*/true,
9143 /*check_dependency_p=*/true,
a668c6ad
MM
9144 /*type_p=*/true,
9145 is_declaration);
a723baf1
MM
9146 /* For everything but enumeration types, consider a template-id. */
9147 if (tag_type != enum_type)
9148 {
9149 bool template_p = false;
9150 tree decl;
9151
9152 /* Allow the `template' keyword. */
9153 template_p = cp_parser_optional_template_keyword (parser);
9154 /* If we didn't see `template', we don't know if there's a
9155 template-id or not. */
9156 if (!template_p)
9157 cp_parser_parse_tentatively (parser);
9158 /* Parse the template-id. */
9159 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9160 /*check_dependency_p=*/true,
9161 is_declaration);
a723baf1
MM
9162 /* If we didn't find a template-id, look for an ordinary
9163 identifier. */
9164 if (!template_p && !cp_parser_parse_definitely (parser))
9165 ;
9166 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9167 in effect, then we must assume that, upon instantiation, the
9168 template will correspond to a class. */
9169 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9170 && tag_type == typename_type)
9171 type = make_typename_type (parser->scope, decl,
9172 /*complain=*/1);
21526606 9173 else
a723baf1
MM
9174 type = TREE_TYPE (decl);
9175 }
9176
9177 /* For an enumeration type, consider only a plain identifier. */
9178 if (!type)
9179 {
9180 identifier = cp_parser_identifier (parser);
9181
9182 if (identifier == error_mark_node)
eb5abb39
NS
9183 {
9184 parser->scope = NULL_TREE;
9185 return error_mark_node;
9186 }
a723baf1
MM
9187
9188 /* For a `typename', we needn't call xref_tag. */
9189 if (tag_type == typename_type)
21526606 9190 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9191 identifier);
a723baf1
MM
9192 /* Look up a qualified name in the usual way. */
9193 if (parser->scope)
9194 {
9195 tree decl;
9196
9197 /* In an elaborated-type-specifier, names are assumed to name
9198 types, so we set IS_TYPE to TRUE when calling
9199 cp_parser_lookup_name. */
21526606 9200 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 9201 /*is_type=*/true,
b0bc6e8e 9202 /*is_template=*/false,
eea9800f 9203 /*is_namespace=*/false,
a723baf1 9204 /*check_dependency=*/true);
710b73e6
KL
9205
9206 /* If we are parsing friend declaration, DECL may be a
9207 TEMPLATE_DECL tree node here. However, we need to check
9208 whether this TEMPLATE_DECL results in valid code. Consider
9209 the following example:
9210
9211 namespace N {
9212 template <class T> class C {};
9213 }
9214 class X {
9215 template <class T> friend class N::C; // #1, valid code
9216 };
9217 template <class T> class Y {
9218 friend class N::C; // #2, invalid code
9219 };
9220
9221 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9222 name lookup of `N::C'. We see that friend declaration must
9223 be template for the code to be valid. Note that
9224 processing_template_decl does not work here since it is
9225 always 1 for the above two cases. */
9226
21526606 9227 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9228 (decl, /*tag_name_p=*/is_friend
9229 && parser->num_template_parameter_lists));
a723baf1
MM
9230
9231 if (TREE_CODE (decl) != TYPE_DECL)
9232 {
9233 error ("expected type-name");
9234 return error_mark_node;
9235 }
560ad596
MM
9236
9237 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9238 check_elaborated_type_specifier
4b0d3cbe 9239 (tag_type, decl,
560ad596
MM
9240 (parser->num_template_parameter_lists
9241 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9242
9243 type = TREE_TYPE (decl);
9244 }
21526606 9245 else
a723baf1
MM
9246 {
9247 /* An elaborated-type-specifier sometimes introduces a new type and
9248 sometimes names an existing type. Normally, the rule is that it
9249 introduces a new type only if there is not an existing type of
9250 the same name already in scope. For example, given:
9251
9252 struct S {};
9253 void f() { struct S s; }
9254
9255 the `struct S' in the body of `f' is the same `struct S' as in
9256 the global scope; the existing definition is used. However, if
21526606 9257 there were no global declaration, this would introduce a new
a723baf1
MM
9258 local class named `S'.
9259
9260 An exception to this rule applies to the following code:
9261
9262 namespace N { struct S; }
9263
9264 Here, the elaborated-type-specifier names a new type
9265 unconditionally; even if there is already an `S' in the
9266 containing scope this declaration names a new type.
9267 This exception only applies if the elaborated-type-specifier
9268 forms the complete declaration:
9269
21526606 9270 [class.name]
a723baf1
MM
9271
9272 A declaration consisting solely of `class-key identifier ;' is
9273 either a redeclaration of the name in the current scope or a
9274 forward declaration of the identifier as a class name. It
9275 introduces the name into the current scope.
9276
9277 We are in this situation precisely when the next token is a `;'.
9278
9279 An exception to the exception is that a `friend' declaration does
9280 *not* name a new type; i.e., given:
9281
9282 struct S { friend struct T; };
9283
21526606 9284 `T' is not a new type in the scope of `S'.
a723baf1
MM
9285
9286 Also, `new struct S' or `sizeof (struct S)' never results in the
9287 definition of a new type; a new type can only be declared in a
9bcb9aae 9288 declaration context. */
a723baf1 9289
e0fed25b
DS
9290 /* Warn about attributes. They are ignored. */
9291 if (attributes)
9292 warning ("type attributes are honored only at type definition");
9293
21526606 9294 type = xref_tag (tag_type, identifier,
21526606 9295 (is_friend
a723baf1 9296 || !is_declaration
21526606 9297 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
9298 CPP_SEMICOLON)),
9299 parser->num_template_parameter_lists);
a723baf1
MM
9300 }
9301 }
9302 if (tag_type != enum_type)
9303 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9304
9305 /* A "<" cannot follow an elaborated type specifier. If that
9306 happens, the user was probably trying to form a template-id. */
9307 cp_parser_check_for_invalid_template_id (parser, type);
9308
a723baf1
MM
9309 return type;
9310}
9311
9312/* Parse an enum-specifier.
9313
9314 enum-specifier:
9315 enum identifier [opt] { enumerator-list [opt] }
9316
9317 Returns an ENUM_TYPE representing the enumeration. */
9318
9319static tree
94edc4ab 9320cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9321{
9322 cp_token *token;
9323 tree identifier = NULL_TREE;
9324 tree type;
9325
9326 /* Look for the `enum' keyword. */
9327 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9328 return error_mark_node;
9329 /* Peek at the next token. */
9330 token = cp_lexer_peek_token (parser->lexer);
9331
9332 /* See if it is an identifier. */
9333 if (token->type == CPP_NAME)
9334 identifier = cp_parser_identifier (parser);
9335
9336 /* Look for the `{'. */
9337 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9338 return error_mark_node;
9339
9340 /* At this point, we're going ahead with the enum-specifier, even
9341 if some other problem occurs. */
9342 cp_parser_commit_to_tentative_parse (parser);
9343
9344 /* Issue an error message if type-definitions are forbidden here. */
9345 cp_parser_check_type_definition (parser);
9346
9347 /* Create the new type. */
9348 type = start_enum (identifier ? identifier : make_anon_name ());
9349
9350 /* Peek at the next token. */
9351 token = cp_lexer_peek_token (parser->lexer);
9352 /* If it's not a `}', then there are some enumerators. */
9353 if (token->type != CPP_CLOSE_BRACE)
9354 cp_parser_enumerator_list (parser, type);
9355 /* Look for the `}'. */
9356 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9357
9358 /* Finish up the enumeration. */
9359 finish_enum (type);
9360
9361 return type;
9362}
9363
9364/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9365 TYPE.
a723baf1
MM
9366
9367 enumerator-list:
9368 enumerator-definition
9369 enumerator-list , enumerator-definition */
9370
9371static void
94edc4ab 9372cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9373{
9374 while (true)
9375 {
9376 cp_token *token;
9377
9378 /* Parse an enumerator-definition. */
9379 cp_parser_enumerator_definition (parser, type);
9380 /* Peek at the next token. */
9381 token = cp_lexer_peek_token (parser->lexer);
21526606 9382 /* If it's not a `,', then we've reached the end of the
a723baf1
MM
9383 list. */
9384 if (token->type != CPP_COMMA)
9385 break;
9386 /* Otherwise, consume the `,' and keep going. */
9387 cp_lexer_consume_token (parser->lexer);
9388 /* If the next token is a `}', there is a trailing comma. */
9389 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9390 {
9391 if (pedantic && !in_system_header)
9392 pedwarn ("comma at end of enumerator list");
9393 break;
9394 }
9395 }
9396}
9397
9398/* Parse an enumerator-definition. The enumerator has the indicated
9399 TYPE.
9400
9401 enumerator-definition:
9402 enumerator
9403 enumerator = constant-expression
21526606 9404
a723baf1
MM
9405 enumerator:
9406 identifier */
9407
9408static void
94edc4ab 9409cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9410{
9411 cp_token *token;
9412 tree identifier;
9413 tree value;
9414
9415 /* Look for the identifier. */
9416 identifier = cp_parser_identifier (parser);
9417 if (identifier == error_mark_node)
9418 return;
21526606 9419
a723baf1
MM
9420 /* Peek at the next token. */
9421 token = cp_lexer_peek_token (parser->lexer);
9422 /* If it's an `=', then there's an explicit value. */
9423 if (token->type == CPP_EQ)
9424 {
9425 /* Consume the `=' token. */
9426 cp_lexer_consume_token (parser->lexer);
9427 /* Parse the value. */
21526606 9428 value = cp_parser_constant_expression (parser,
d17811fd 9429 /*allow_non_constant_p=*/false,
14d22dd6 9430 NULL);
a723baf1
MM
9431 }
9432 else
9433 value = NULL_TREE;
9434
9435 /* Create the enumerator. */
9436 build_enumerator (identifier, value, type);
9437}
9438
9439/* Parse a namespace-name.
9440
9441 namespace-name:
9442 original-namespace-name
9443 namespace-alias
9444
9445 Returns the NAMESPACE_DECL for the namespace. */
9446
9447static tree
94edc4ab 9448cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9449{
9450 tree identifier;
9451 tree namespace_decl;
9452
9453 /* Get the name of the namespace. */
9454 identifier = cp_parser_identifier (parser);
9455 if (identifier == error_mark_node)
9456 return error_mark_node;
9457
eea9800f
MM
9458 /* Look up the identifier in the currently active scope. Look only
9459 for namespaces, due to:
9460
9461 [basic.lookup.udir]
9462
9463 When looking up a namespace-name in a using-directive or alias
21526606 9464 definition, only namespace names are considered.
eea9800f
MM
9465
9466 And:
9467
9468 [basic.lookup.qual]
9469
9470 During the lookup of a name preceding the :: scope resolution
21526606 9471 operator, object, function, and enumerator names are ignored.
eea9800f
MM
9472
9473 (Note that cp_parser_class_or_namespace_name only calls this
9474 function if the token after the name is the scope resolution
9475 operator.) */
9476 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f 9477 /*is_type=*/false,
b0bc6e8e 9478 /*is_template=*/false,
eea9800f
MM
9479 /*is_namespace=*/true,
9480 /*check_dependency=*/true);
a723baf1
MM
9481 /* If it's not a namespace, issue an error. */
9482 if (namespace_decl == error_mark_node
9483 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9484 {
9485 cp_parser_error (parser, "expected namespace-name");
9486 namespace_decl = error_mark_node;
9487 }
21526606 9488
a723baf1
MM
9489 return namespace_decl;
9490}
9491
9492/* Parse a namespace-definition.
9493
9494 namespace-definition:
9495 named-namespace-definition
21526606 9496 unnamed-namespace-definition
a723baf1
MM
9497
9498 named-namespace-definition:
9499 original-namespace-definition
9500 extension-namespace-definition
9501
9502 original-namespace-definition:
9503 namespace identifier { namespace-body }
21526606 9504
a723baf1
MM
9505 extension-namespace-definition:
9506 namespace original-namespace-name { namespace-body }
21526606 9507
a723baf1
MM
9508 unnamed-namespace-definition:
9509 namespace { namespace-body } */
9510
9511static void
94edc4ab 9512cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9513{
9514 tree identifier;
9515
9516 /* Look for the `namespace' keyword. */
9517 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9518
9519 /* Get the name of the namespace. We do not attempt to distinguish
9520 between an original-namespace-definition and an
9521 extension-namespace-definition at this point. The semantic
9522 analysis routines are responsible for that. */
9523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9524 identifier = cp_parser_identifier (parser);
9525 else
9526 identifier = NULL_TREE;
9527
9528 /* Look for the `{' to start the namespace. */
9529 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9530 /* Start the namespace. */
9531 push_namespace (identifier);
9532 /* Parse the body of the namespace. */
9533 cp_parser_namespace_body (parser);
9534 /* Finish the namespace. */
9535 pop_namespace ();
9536 /* Look for the final `}'. */
9537 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9538}
9539
9540/* Parse a namespace-body.
9541
9542 namespace-body:
9543 declaration-seq [opt] */
9544
9545static void
94edc4ab 9546cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9547{
9548 cp_parser_declaration_seq_opt (parser);
9549}
9550
9551/* Parse a namespace-alias-definition.
9552
9553 namespace-alias-definition:
9554 namespace identifier = qualified-namespace-specifier ; */
9555
9556static void
94edc4ab 9557cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9558{
9559 tree identifier;
9560 tree namespace_specifier;
9561
9562 /* Look for the `namespace' keyword. */
9563 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9564 /* Look for the identifier. */
9565 identifier = cp_parser_identifier (parser);
9566 if (identifier == error_mark_node)
9567 return;
9568 /* Look for the `=' token. */
9569 cp_parser_require (parser, CPP_EQ, "`='");
9570 /* Look for the qualified-namespace-specifier. */
21526606 9571 namespace_specifier
a723baf1
MM
9572 = cp_parser_qualified_namespace_specifier (parser);
9573 /* Look for the `;' token. */
9574 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9575
9576 /* Register the alias in the symbol table. */
9577 do_namespace_alias (identifier, namespace_specifier);
9578}
9579
9580/* Parse a qualified-namespace-specifier.
9581
9582 qualified-namespace-specifier:
9583 :: [opt] nested-name-specifier [opt] namespace-name
9584
9585 Returns a NAMESPACE_DECL corresponding to the specified
9586 namespace. */
9587
9588static tree
94edc4ab 9589cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9590{
9591 /* Look for the optional `::'. */
21526606 9592 cp_parser_global_scope_opt (parser,
a723baf1
MM
9593 /*current_scope_valid_p=*/false);
9594
9595 /* Look for the optional nested-name-specifier. */
9596 cp_parser_nested_name_specifier_opt (parser,
9597 /*typename_keyword_p=*/false,
9598 /*check_dependency_p=*/true,
a668c6ad
MM
9599 /*type_p=*/false,
9600 /*is_declaration=*/true);
a723baf1
MM
9601
9602 return cp_parser_namespace_name (parser);
9603}
9604
9605/* Parse a using-declaration.
9606
9607 using-declaration:
9608 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9609 using :: unqualified-id ; */
9610
9611static void
94edc4ab 9612cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9613{
9614 cp_token *token;
9615 bool typename_p = false;
9616 bool global_scope_p;
9617 tree decl;
9618 tree identifier;
9619 tree scope;
ed5f054f 9620 tree qscope;
a723baf1
MM
9621
9622 /* Look for the `using' keyword. */
9623 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 9624
a723baf1
MM
9625 /* Peek at the next token. */
9626 token = cp_lexer_peek_token (parser->lexer);
9627 /* See if it's `typename'. */
9628 if (token->keyword == RID_TYPENAME)
9629 {
9630 /* Remember that we've seen it. */
9631 typename_p = true;
9632 /* Consume the `typename' token. */
9633 cp_lexer_consume_token (parser->lexer);
9634 }
9635
9636 /* Look for the optional global scope qualification. */
21526606 9637 global_scope_p
a723baf1 9638 = (cp_parser_global_scope_opt (parser,
21526606 9639 /*current_scope_valid_p=*/false)
a723baf1
MM
9640 != NULL_TREE);
9641
9642 /* If we saw `typename', or didn't see `::', then there must be a
9643 nested-name-specifier present. */
9644 if (typename_p || !global_scope_p)
21526606 9645 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
9646 /*check_dependency_p=*/true,
9647 /*type_p=*/false,
9648 /*is_declaration=*/true);
a723baf1
MM
9649 /* Otherwise, we could be in either of the two productions. In that
9650 case, treat the nested-name-specifier as optional. */
9651 else
ed5f054f
AO
9652 qscope = cp_parser_nested_name_specifier_opt (parser,
9653 /*typename_keyword_p=*/false,
9654 /*check_dependency_p=*/true,
9655 /*type_p=*/false,
9656 /*is_declaration=*/true);
9657 if (!qscope)
9658 qscope = global_namespace;
a723baf1
MM
9659
9660 /* Parse the unqualified-id. */
21526606 9661 identifier = cp_parser_unqualified_id (parser,
a723baf1 9662 /*template_keyword_p=*/false,
f3c2dfc6
MM
9663 /*check_dependency_p=*/true,
9664 /*declarator_p=*/true);
a723baf1
MM
9665
9666 /* The function we call to handle a using-declaration is different
9667 depending on what scope we are in. */
f3c2dfc6
MM
9668 if (identifier == error_mark_node)
9669 ;
9670 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9671 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9672 /* [namespace.udecl]
9673
9674 A using declaration shall not name a template-id. */
9675 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9676 else
9677 {
f3c2dfc6
MM
9678 scope = current_scope ();
9679 if (scope && TYPE_P (scope))
4eb6d609 9680 {
f3c2dfc6
MM
9681 /* Create the USING_DECL. */
9682 decl = do_class_using_decl (build_nt (SCOPE_REF,
9683 parser->scope,
9684 identifier));
9685 /* Add it to the list of members in this class. */
9686 finish_member_declaration (decl);
4eb6d609 9687 }
a723baf1 9688 else
f3c2dfc6
MM
9689 {
9690 decl = cp_parser_lookup_name_simple (parser, identifier);
9691 if (decl == error_mark_node)
4bb8ca28 9692 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6 9693 else if (scope)
ed5f054f 9694 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 9695 else
ed5f054f 9696 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 9697 }
a723baf1
MM
9698 }
9699
9700 /* Look for the final `;'. */
9701 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9702}
9703
21526606
EC
9704/* Parse a using-directive.
9705
a723baf1
MM
9706 using-directive:
9707 using namespace :: [opt] nested-name-specifier [opt]
9708 namespace-name ; */
9709
9710static void
94edc4ab 9711cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9712{
9713 tree namespace_decl;
86098eb8 9714 tree attribs;
a723baf1
MM
9715
9716 /* Look for the `using' keyword. */
9717 cp_parser_require_keyword (parser, RID_USING, "`using'");
9718 /* And the `namespace' keyword. */
9719 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9720 /* Look for the optional `::' operator. */
9721 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9722 /* And the optional nested-name-specifier. */
a723baf1
MM
9723 cp_parser_nested_name_specifier_opt (parser,
9724 /*typename_keyword_p=*/false,
9725 /*check_dependency_p=*/true,
a668c6ad
MM
9726 /*type_p=*/false,
9727 /*is_declaration=*/true);
a723baf1
MM
9728 /* Get the namespace being used. */
9729 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
9730 /* And any specified attributes. */
9731 attribs = cp_parser_attributes_opt (parser);
a723baf1 9732 /* Update the symbol table. */
86098eb8 9733 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
9734 /* Look for the final `;'. */
9735 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9736}
9737
9738/* Parse an asm-definition.
9739
9740 asm-definition:
21526606 9741 asm ( string-literal ) ;
a723baf1
MM
9742
9743 GNU Extension:
9744
9745 asm-definition:
9746 asm volatile [opt] ( string-literal ) ;
9747 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9748 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9749 : asm-operand-list [opt] ) ;
21526606
EC
9750 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9751 : asm-operand-list [opt]
a723baf1
MM
9752 : asm-operand-list [opt] ) ; */
9753
9754static void
94edc4ab 9755cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9756{
9757 cp_token *token;
9758 tree string;
9759 tree outputs = NULL_TREE;
9760 tree inputs = NULL_TREE;
9761 tree clobbers = NULL_TREE;
9762 tree asm_stmt;
9763 bool volatile_p = false;
9764 bool extended_p = false;
9765
9766 /* Look for the `asm' keyword. */
9767 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9768 /* See if the next token is `volatile'. */
9769 if (cp_parser_allow_gnu_extensions_p (parser)
9770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9771 {
9772 /* Remember that we saw the `volatile' keyword. */
9773 volatile_p = true;
9774 /* Consume the token. */
9775 cp_lexer_consume_token (parser->lexer);
9776 }
9777 /* Look for the opening `('. */
9778 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9779 /* Look for the string. */
21526606 9780 c_lex_string_translate = false;
a723baf1
MM
9781 token = cp_parser_require (parser, CPP_STRING, "asm body");
9782 if (!token)
21526606 9783 goto finish;
a723baf1
MM
9784 string = token->value;
9785 /* If we're allowing GNU extensions, check for the extended assembly
21526606 9786 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
9787 a space in C, and so, for compatibility, we tolerate that here
9788 too. Doing that means that we have to treat the `::' operator as
9789 two `:' tokens. */
9790 if (cp_parser_allow_gnu_extensions_p (parser)
9791 && at_function_scope_p ()
9792 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9793 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9794 {
9795 bool inputs_p = false;
9796 bool clobbers_p = false;
9797
9798 /* The extended syntax was used. */
9799 extended_p = true;
9800
9801 /* Look for outputs. */
9802 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9803 {
9804 /* Consume the `:'. */
9805 cp_lexer_consume_token (parser->lexer);
9806 /* Parse the output-operands. */
21526606 9807 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
9808 CPP_COLON)
9809 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9810 CPP_SCOPE)
9811 && cp_lexer_next_token_is_not (parser->lexer,
9812 CPP_CLOSE_PAREN))
a723baf1
MM
9813 outputs = cp_parser_asm_operand_list (parser);
9814 }
9815 /* If the next token is `::', there are no outputs, and the
9816 next token is the beginning of the inputs. */
9817 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9818 {
9819 /* Consume the `::' token. */
9820 cp_lexer_consume_token (parser->lexer);
9821 /* The inputs are coming next. */
9822 inputs_p = true;
9823 }
9824
9825 /* Look for inputs. */
9826 if (inputs_p
9827 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9828 {
9829 if (!inputs_p)
9830 /* Consume the `:'. */
9831 cp_lexer_consume_token (parser->lexer);
9832 /* Parse the output-operands. */
21526606 9833 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
9834 CPP_COLON)
9835 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9836 CPP_SCOPE)
9837 && cp_lexer_next_token_is_not (parser->lexer,
9838 CPP_CLOSE_PAREN))
a723baf1
MM
9839 inputs = cp_parser_asm_operand_list (parser);
9840 }
9841 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9842 /* The clobbers are coming next. */
9843 clobbers_p = true;
9844
9845 /* Look for clobbers. */
21526606 9846 if (clobbers_p
a723baf1
MM
9847 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9848 {
9849 if (!clobbers_p)
9850 /* Consume the `:'. */
9851 cp_lexer_consume_token (parser->lexer);
9852 /* Parse the clobbers. */
8caf4c38
MM
9853 if (cp_lexer_next_token_is_not (parser->lexer,
9854 CPP_CLOSE_PAREN))
9855 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9856 }
9857 }
9858 /* Look for the closing `)'. */
9859 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
9860 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9861 /*consume_paren=*/true);
a723baf1
MM
9862 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9863
9864 /* Create the ASM_STMT. */
9865 if (at_function_scope_p ())
9866 {
21526606
EC
9867 asm_stmt =
9868 finish_asm_stmt (volatile_p
a723baf1
MM
9869 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9870 string, outputs, inputs, clobbers);
9871 /* If the extended syntax was not used, mark the ASM_STMT. */
9872 if (!extended_p)
9873 ASM_INPUT_P (asm_stmt) = 1;
9874 }
9875 else
9876 assemble_asm (string);
21526606
EC
9877
9878 finish:
9879 c_lex_string_translate = true;
a723baf1
MM
9880}
9881
9882/* Declarators [gram.dcl.decl] */
9883
9884/* Parse an init-declarator.
9885
9886 init-declarator:
9887 declarator initializer [opt]
9888
9889 GNU Extension:
9890
9891 init-declarator:
9892 declarator asm-specification [opt] attributes [opt] initializer [opt]
9893
4bb8ca28
MM
9894 function-definition:
9895 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
9896 function-body
9897 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
9898
9899 GNU Extension:
9900
9901 function-definition:
21526606 9902 __extension__ function-definition
4bb8ca28 9903
a723baf1 9904 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9905 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9906 then this declarator appears in a class scope. The new DECL created
9907 by this declarator is returned.
a723baf1
MM
9908
9909 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9910 for a function-definition here as well. If the declarator is a
9911 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9912 be TRUE upon return. By that point, the function-definition will
9913 have been completely parsed.
9914
9915 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9916 is FALSE. */
9917
9918static tree
21526606
EC
9919cp_parser_init_declarator (cp_parser* parser,
9920 tree decl_specifiers,
94edc4ab
NN
9921 tree prefix_attributes,
9922 bool function_definition_allowed_p,
9923 bool member_p,
560ad596 9924 int declares_class_or_enum,
94edc4ab 9925 bool* function_definition_p)
a723baf1
MM
9926{
9927 cp_token *token;
9928 tree declarator;
9929 tree attributes;
9930 tree asm_specification;
9931 tree initializer;
9932 tree decl = NULL_TREE;
9933 tree scope;
a723baf1
MM
9934 bool is_initialized;
9935 bool is_parenthesized_init;
39703eb9 9936 bool is_non_constant_init;
7efa3e22 9937 int ctor_dtor_or_conv_p;
a723baf1 9938 bool friend_p;
91b004e5 9939 bool pop_p = false;
a723baf1
MM
9940
9941 /* Assume that this is not the declarator for a function
9942 definition. */
9943 if (function_definition_p)
9944 *function_definition_p = false;
9945
9946 /* Defer access checks while parsing the declarator; we cannot know
21526606 9947 what names are accessible until we know what is being
a723baf1 9948 declared. */
cf22909c
KL
9949 resume_deferring_access_checks ();
9950
a723baf1 9951 /* Parse the declarator. */
21526606 9952 declarator
62b8a44e 9953 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9954 &ctor_dtor_or_conv_p,
9955 /*parenthesized_p=*/NULL);
a723baf1 9956 /* Gather up the deferred checks. */
cf22909c 9957 stop_deferring_access_checks ();
24c0ef37 9958
a723baf1
MM
9959 /* If the DECLARATOR was erroneous, there's no need to go
9960 further. */
9961 if (declarator == error_mark_node)
cf22909c 9962 return error_mark_node;
a723baf1 9963
560ad596
MM
9964 cp_parser_check_for_definition_in_return_type (declarator,
9965 declares_class_or_enum);
9966
a723baf1
MM
9967 /* Figure out what scope the entity declared by the DECLARATOR is
9968 located in. `grokdeclarator' sometimes changes the scope, so
9969 we compute it now. */
9970 scope = get_scope_of_declarator (declarator);
9971
9972 /* If we're allowing GNU extensions, look for an asm-specification
9973 and attributes. */
9974 if (cp_parser_allow_gnu_extensions_p (parser))
9975 {
9976 /* Look for an asm-specification. */
9977 asm_specification = cp_parser_asm_specification_opt (parser);
9978 /* And attributes. */
9979 attributes = cp_parser_attributes_opt (parser);
9980 }
9981 else
9982 {
9983 asm_specification = NULL_TREE;
9984 attributes = NULL_TREE;
9985 }
9986
9987 /* Peek at the next token. */
9988 token = cp_lexer_peek_token (parser->lexer);
9989 /* Check to see if the token indicates the start of a
9990 function-definition. */
9991 if (cp_parser_token_starts_function_definition_p (token))
9992 {
9993 if (!function_definition_allowed_p)
9994 {
9995 /* If a function-definition should not appear here, issue an
9996 error message. */
9997 cp_parser_error (parser,
9998 "a function-definition is not allowed here");
9999 return error_mark_node;
10000 }
10001 else
10002 {
a723baf1
MM
10003 /* Neither attributes nor an asm-specification are allowed
10004 on a function-definition. */
10005 if (asm_specification)
10006 error ("an asm-specification is not allowed on a function-definition");
10007 if (attributes)
10008 error ("attributes are not allowed on a function-definition");
10009 /* This is a function-definition. */
10010 *function_definition_p = true;
10011
a723baf1 10012 /* Parse the function definition. */
4bb8ca28
MM
10013 if (member_p)
10014 decl = cp_parser_save_member_function_body (parser,
10015 decl_specifiers,
10016 declarator,
10017 prefix_attributes);
10018 else
21526606 10019 decl
4bb8ca28
MM
10020 = (cp_parser_function_definition_from_specifiers_and_declarator
10021 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10022
a723baf1
MM
10023 return decl;
10024 }
10025 }
10026
10027 /* [dcl.dcl]
10028
10029 Only in function declarations for constructors, destructors, and
21526606 10030 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10031
10032 We explicitly postpone this check past the point where we handle
10033 function-definitions because we tolerate function-definitions
10034 that are missing their return types in some modes. */
7efa3e22 10035 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1 10036 {
21526606 10037 cp_parser_error (parser,
a723baf1
MM
10038 "expected constructor, destructor, or type conversion");
10039 return error_mark_node;
10040 }
10041
10042 /* An `=' or an `(' indicates an initializer. */
21526606 10043 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10044 || token->type == CPP_OPEN_PAREN);
10045 /* If the init-declarator isn't initialized and isn't followed by a
10046 `,' or `;', it's not a valid init-declarator. */
21526606 10047 if (!is_initialized
a723baf1
MM
10048 && token->type != CPP_COMMA
10049 && token->type != CPP_SEMICOLON)
10050 {
10051 cp_parser_error (parser, "expected init-declarator");
10052 return error_mark_node;
10053 }
10054
10055 /* Because start_decl has side-effects, we should only call it if we
10056 know we're going ahead. By this point, we know that we cannot
10057 possibly be looking at any other construct. */
10058 cp_parser_commit_to_tentative_parse (parser);
10059
e90c7b84
ILT
10060 /* If the decl specifiers were bad, issue an error now that we're
10061 sure this was intended to be a declarator. Then continue
10062 declaring the variable(s), as int, to try to cut down on further
10063 errors. */
10064 if (decl_specifiers != NULL
10065 && TREE_VALUE (decl_specifiers) == error_mark_node)
10066 {
10067 cp_parser_error (parser, "invalid type in declaration");
10068 TREE_VALUE (decl_specifiers) = integer_type_node;
10069 }
10070
a723baf1
MM
10071 /* Check to see whether or not this declaration is a friend. */
10072 friend_p = cp_parser_friend_p (decl_specifiers);
10073
10074 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10075 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10076 return error_mark_node;
a723baf1
MM
10077
10078 /* Enter the newly declared entry in the symbol table. If we're
10079 processing a declaration in a class-specifier, we wait until
10080 after processing the initializer. */
10081 if (!member_p)
10082 {
10083 if (parser->in_unbraced_linkage_specification_p)
10084 {
10085 decl_specifiers = tree_cons (error_mark_node,
10086 get_identifier ("extern"),
10087 decl_specifiers);
10088 have_extern_spec = false;
10089 }
ee3071ef
NS
10090 decl = start_decl (declarator, decl_specifiers,
10091 is_initialized, attributes, prefix_attributes);
a723baf1
MM
10092 }
10093
10094 /* Enter the SCOPE. That way unqualified names appearing in the
10095 initializer will be looked up in SCOPE. */
10096 if (scope)
91b004e5 10097 pop_p = push_scope (scope);
a723baf1
MM
10098
10099 /* Perform deferred access control checks, now that we know in which
10100 SCOPE the declared entity resides. */
21526606 10101 if (!member_p && decl)
a723baf1
MM
10102 {
10103 tree saved_current_function_decl = NULL_TREE;
10104
10105 /* If the entity being declared is a function, pretend that we
10106 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10107 things that would not otherwise be accessible. */
a723baf1
MM
10108 if (TREE_CODE (decl) == FUNCTION_DECL)
10109 {
10110 saved_current_function_decl = current_function_decl;
10111 current_function_decl = decl;
10112 }
21526606 10113
cf22909c
KL
10114 /* Perform the access control checks for the declarator and the
10115 the decl-specifiers. */
10116 perform_deferred_access_checks ();
a723baf1
MM
10117
10118 /* Restore the saved value. */
10119 if (TREE_CODE (decl) == FUNCTION_DECL)
10120 current_function_decl = saved_current_function_decl;
10121 }
10122
10123 /* Parse the initializer. */
10124 if (is_initialized)
21526606 10125 initializer = cp_parser_initializer (parser,
39703eb9
MM
10126 &is_parenthesized_init,
10127 &is_non_constant_init);
a723baf1
MM
10128 else
10129 {
10130 initializer = NULL_TREE;
10131 is_parenthesized_init = false;
39703eb9 10132 is_non_constant_init = true;
a723baf1
MM
10133 }
10134
10135 /* The old parser allows attributes to appear after a parenthesized
10136 initializer. Mark Mitchell proposed removing this functionality
10137 on the GCC mailing lists on 2002-08-13. This parser accepts the
10138 attributes -- but ignores them. */
10139 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10140 if (cp_parser_attributes_opt (parser))
10141 warning ("attributes after parenthesized initializer ignored");
10142
10143 /* Leave the SCOPE, now that we have processed the initializer. It
10144 is important to do this before calling cp_finish_decl because it
10145 makes decisions about whether to create DECL_STMTs or not based
10146 on the current scope. */
91b004e5 10147 if (pop_p)
a723baf1
MM
10148 pop_scope (scope);
10149
10150 /* For an in-class declaration, use `grokfield' to create the
10151 declaration. */
10152 if (member_p)
8db1028e
NS
10153 {
10154 decl = grokfield (declarator, decl_specifiers,
10155 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10156 /*attributes=*/NULL_TREE);
8db1028e
NS
10157 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10158 cp_parser_save_default_args (parser, decl);
10159 }
21526606 10160
a723baf1
MM
10161 /* Finish processing the declaration. But, skip friend
10162 declarations. */
10163 if (!friend_p && decl)
21526606
EC
10164 cp_finish_decl (decl,
10165 initializer,
a723baf1
MM
10166 asm_specification,
10167 /* If the initializer is in parentheses, then this is
10168 a direct-initialization, which means that an
10169 `explicit' constructor is OK. Otherwise, an
10170 `explicit' constructor cannot be used. */
10171 ((is_parenthesized_init || !is_initialized)
10172 ? 0 : LOOKUP_ONLYCONVERTING));
10173
39703eb9
MM
10174 /* Remember whether or not variables were initialized by
10175 constant-expressions. */
21526606 10176 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10177 && is_initialized && !is_non_constant_init)
10178 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10179
a723baf1
MM
10180 return decl;
10181}
10182
10183/* Parse a declarator.
21526606 10184
a723baf1
MM
10185 declarator:
10186 direct-declarator
21526606 10187 ptr-operator declarator
a723baf1
MM
10188
10189 abstract-declarator:
10190 ptr-operator abstract-declarator [opt]
10191 direct-abstract-declarator
10192
10193 GNU Extensions:
10194
10195 declarator:
10196 attributes [opt] direct-declarator
21526606 10197 attributes [opt] ptr-operator declarator
a723baf1
MM
10198
10199 abstract-declarator:
10200 attributes [opt] ptr-operator abstract-declarator [opt]
10201 attributes [opt] direct-abstract-declarator
21526606 10202
a723baf1
MM
10203 Returns a representation of the declarator. If the declarator has
10204 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 10205 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
10206 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10207 used. The first operand is the TYPE for `X'. The second operand
10208 is an INDIRECT_REF whose operand is the sub-declarator.
10209
34cd5ae7 10210 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
10211
10212 (It would be better to define a structure type to represent
10213 declarators, rather than abusing `tree' nodes to represent
10214 declarators. That would be much clearer and save some memory.
10215 There is no reason for declarators to be garbage-collected, for
10216 example; they are created during parser and no longer needed after
10217 `grokdeclarator' has been called.)
10218
10219 For a ptr-operator that has the optional cv-qualifier-seq,
10220 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10221 node.
10222
7efa3e22
NS
10223 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10224 detect constructor, destructor or conversion operators. It is set
10225 to -1 if the declarator is a name, and +1 if it is a
10226 function. Otherwise it is set to zero. Usually you just want to
10227 test for >0, but internally the negative value is used.
21526606 10228
a723baf1
MM
10229 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10230 a decl-specifier-seq unless it declares a constructor, destructor,
10231 or conversion. It might seem that we could check this condition in
10232 semantic analysis, rather than parsing, but that makes it difficult
10233 to handle something like `f()'. We want to notice that there are
10234 no decl-specifiers, and therefore realize that this is an
21526606
EC
10235 expression, not a declaration.)
10236
4bb8ca28
MM
10237 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10238 the declarator is a direct-declarator of the form "(...)". */
a723baf1
MM
10239
10240static tree
21526606
EC
10241cp_parser_declarator (cp_parser* parser,
10242 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
10243 int* ctor_dtor_or_conv_p,
10244 bool* parenthesized_p)
a723baf1
MM
10245{
10246 cp_token *token;
10247 tree declarator;
10248 enum tree_code code;
10249 tree cv_qualifier_seq;
10250 tree class_type;
10251 tree attributes = NULL_TREE;
10252
10253 /* Assume this is not a constructor, destructor, or type-conversion
10254 operator. */
10255 if (ctor_dtor_or_conv_p)
7efa3e22 10256 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10257
10258 if (cp_parser_allow_gnu_extensions_p (parser))
10259 attributes = cp_parser_attributes_opt (parser);
21526606 10260
a723baf1
MM
10261 /* Peek at the next token. */
10262 token = cp_lexer_peek_token (parser->lexer);
21526606 10263
a723baf1
MM
10264 /* Check for the ptr-operator production. */
10265 cp_parser_parse_tentatively (parser);
10266 /* Parse the ptr-operator. */
21526606
EC
10267 code = cp_parser_ptr_operator (parser,
10268 &class_type,
a723baf1
MM
10269 &cv_qualifier_seq);
10270 /* If that worked, then we have a ptr-operator. */
10271 if (cp_parser_parse_definitely (parser))
10272 {
4bb8ca28
MM
10273 /* If a ptr-operator was found, then this declarator was not
10274 parenthesized. */
10275 if (parenthesized_p)
10276 *parenthesized_p = true;
a723baf1
MM
10277 /* The dependent declarator is optional if we are parsing an
10278 abstract-declarator. */
62b8a44e 10279 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10280 cp_parser_parse_tentatively (parser);
10281
10282 /* Parse the dependent declarator. */
62b8a44e 10283 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
10284 /*ctor_dtor_or_conv_p=*/NULL,
10285 /*parenthesized_p=*/NULL);
a723baf1
MM
10286
10287 /* If we are parsing an abstract-declarator, we must handle the
10288 case where the dependent declarator is absent. */
62b8a44e
NS
10289 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10290 && !cp_parser_parse_definitely (parser))
a723baf1 10291 declarator = NULL_TREE;
21526606 10292
a723baf1
MM
10293 /* Build the representation of the ptr-operator. */
10294 if (code == INDIRECT_REF)
21526606 10295 declarator = make_pointer_declarator (cv_qualifier_seq,
a723baf1
MM
10296 declarator);
10297 else
10298 declarator = make_reference_declarator (cv_qualifier_seq,
10299 declarator);
10300 /* Handle the pointer-to-member case. */
10301 if (class_type)
10302 declarator = build_nt (SCOPE_REF, class_type, declarator);
10303 }
10304 /* Everything else is a direct-declarator. */
10305 else
4bb8ca28
MM
10306 {
10307 if (parenthesized_p)
10308 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10309 CPP_OPEN_PAREN);
10310 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10311 ctor_dtor_or_conv_p);
10312 }
a723baf1
MM
10313
10314 if (attributes && declarator != error_mark_node)
10315 declarator = tree_cons (attributes, declarator, NULL_TREE);
21526606 10316
a723baf1
MM
10317 return declarator;
10318}
10319
10320/* Parse a direct-declarator or direct-abstract-declarator.
10321
10322 direct-declarator:
10323 declarator-id
10324 direct-declarator ( parameter-declaration-clause )
21526606 10325 cv-qualifier-seq [opt]
a723baf1
MM
10326 exception-specification [opt]
10327 direct-declarator [ constant-expression [opt] ]
21526606 10328 ( declarator )
a723baf1
MM
10329
10330 direct-abstract-declarator:
10331 direct-abstract-declarator [opt]
21526606 10332 ( parameter-declaration-clause )
a723baf1
MM
10333 cv-qualifier-seq [opt]
10334 exception-specification [opt]
10335 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10336 ( abstract-declarator )
10337
62b8a44e
NS
10338 Returns a representation of the declarator. DCL_KIND is
10339 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10340 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10341 we are parsing a direct-declarator. It is
10342 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10343 of ambiguity we prefer an abstract declarator, as per
10344 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10345 cp_parser_declarator.
10346
10347 For the declarator-id production, the representation is as for an
10348 id-expression, except that a qualified name is represented as a
10349 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10350 see the documentation of the FUNCTION_DECLARATOR_* macros for
10351 information about how to find the various declarator components.
10352 An array-declarator is represented as an ARRAY_REF. The
10353 direct-declarator is the first operand; the constant-expression
10354 indicating the size of the array is the second operand. */
10355
10356static tree
94edc4ab
NN
10357cp_parser_direct_declarator (cp_parser* parser,
10358 cp_parser_declarator_kind dcl_kind,
7efa3e22 10359 int* ctor_dtor_or_conv_p)
a723baf1
MM
10360{
10361 cp_token *token;
62b8a44e 10362 tree declarator = NULL_TREE;
a723baf1
MM
10363 tree scope = NULL_TREE;
10364 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10365 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10366 bool first = true;
91b004e5 10367 bool pop_p = false;
21526606 10368
62b8a44e 10369 while (true)
a723baf1 10370 {
62b8a44e
NS
10371 /* Peek at the next token. */
10372 token = cp_lexer_peek_token (parser->lexer);
10373 if (token->type == CPP_OPEN_PAREN)
a723baf1 10374 {
62b8a44e
NS
10375 /* This is either a parameter-declaration-clause, or a
10376 parenthesized declarator. When we know we are parsing a
34cd5ae7 10377 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10378 if FIRST is true. For instance, `(int)' is a
10379 parameter-declaration-clause, with an omitted
10380 direct-abstract-declarator. But `((*))', is a
10381 parenthesized abstract declarator. Finally, when T is a
10382 template parameter `(T)' is a
34cd5ae7 10383 parameter-declaration-clause, and not a parenthesized
62b8a44e 10384 named declarator.
21526606 10385
62b8a44e
NS
10386 We first try and parse a parameter-declaration-clause,
10387 and then try a nested declarator (if FIRST is true).
a723baf1 10388
62b8a44e
NS
10389 It is not an error for it not to be a
10390 parameter-declaration-clause, even when FIRST is
10391 false. Consider,
10392
10393 int i (int);
10394 int i (3);
10395
10396 The first is the declaration of a function while the
10397 second is a the definition of a variable, including its
10398 initializer.
10399
10400 Having seen only the parenthesis, we cannot know which of
10401 these two alternatives should be selected. Even more
10402 complex are examples like:
10403
10404 int i (int (a));
10405 int i (int (3));
10406
10407 The former is a function-declaration; the latter is a
21526606 10408 variable initialization.
62b8a44e 10409
34cd5ae7 10410 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10411 that fails, we back out and return. */
10412
10413 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10414 {
62b8a44e 10415 tree params;
4047b164 10416 unsigned saved_num_template_parameter_lists;
21526606 10417
62b8a44e 10418 cp_parser_parse_tentatively (parser);
a723baf1 10419
62b8a44e
NS
10420 /* Consume the `('. */
10421 cp_lexer_consume_token (parser->lexer);
10422 if (first)
10423 {
10424 /* If this is going to be an abstract declarator, we're
10425 in a declarator and we can't have default args. */
10426 parser->default_arg_ok_p = false;
10427 parser->in_declarator_p = true;
10428 }
21526606 10429
4047b164
KL
10430 /* Inside the function parameter list, surrounding
10431 template-parameter-lists do not apply. */
10432 saved_num_template_parameter_lists
10433 = parser->num_template_parameter_lists;
10434 parser->num_template_parameter_lists = 0;
10435
62b8a44e
NS
10436 /* Parse the parameter-declaration-clause. */
10437 params = cp_parser_parameter_declaration_clause (parser);
10438
4047b164
KL
10439 parser->num_template_parameter_lists
10440 = saved_num_template_parameter_lists;
10441
62b8a44e 10442 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10443 exception-specification. */
62b8a44e
NS
10444 if (cp_parser_parse_definitely (parser))
10445 {
10446 tree cv_qualifiers;
10447 tree exception_specification;
7efa3e22
NS
10448
10449 if (ctor_dtor_or_conv_p)
10450 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10451 first = false;
10452 /* Consume the `)'. */
10453 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10454
10455 /* Parse the cv-qualifier-seq. */
10456 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10457 /* And the exception-specification. */
21526606 10458 exception_specification
62b8a44e
NS
10459 = cp_parser_exception_specification_opt (parser);
10460
10461 /* Create the function-declarator. */
10462 declarator = make_call_declarator (declarator,
10463 params,
10464 cv_qualifiers,
10465 exception_specification);
10466 /* Any subsequent parameter lists are to do with
10467 return type, so are not those of the declared
10468 function. */
10469 parser->default_arg_ok_p = false;
21526606 10470
62b8a44e
NS
10471 /* Repeat the main loop. */
10472 continue;
10473 }
10474 }
21526606 10475
62b8a44e
NS
10476 /* If this is the first, we can try a parenthesized
10477 declarator. */
10478 if (first)
a723baf1 10479 {
a7324e75
MM
10480 bool saved_in_type_id_in_expr_p;
10481
a723baf1 10482 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 10483 parser->in_declarator_p = saved_in_declarator_p;
21526606 10484
62b8a44e
NS
10485 /* Consume the `('. */
10486 cp_lexer_consume_token (parser->lexer);
10487 /* Parse the nested declarator. */
a7324e75
MM
10488 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10489 parser->in_type_id_in_expr_p = true;
21526606 10490 declarator
4bb8ca28
MM
10491 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10492 /*parenthesized_p=*/NULL);
a7324e75 10493 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
10494 first = false;
10495 /* Expect a `)'. */
10496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10497 declarator = error_mark_node;
10498 if (declarator == error_mark_node)
10499 break;
21526606 10500
62b8a44e 10501 goto handle_declarator;
a723baf1 10502 }
9bcb9aae 10503 /* Otherwise, we must be done. */
62b8a44e
NS
10504 else
10505 break;
a723baf1 10506 }
62b8a44e
NS
10507 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10508 && token->type == CPP_OPEN_SQUARE)
a723baf1 10509 {
62b8a44e 10510 /* Parse an array-declarator. */
a723baf1
MM
10511 tree bounds;
10512
7efa3e22
NS
10513 if (ctor_dtor_or_conv_p)
10514 *ctor_dtor_or_conv_p = 0;
21526606 10515
62b8a44e
NS
10516 first = false;
10517 parser->default_arg_ok_p = false;
10518 parser->in_declarator_p = true;
a723baf1
MM
10519 /* Consume the `['. */
10520 cp_lexer_consume_token (parser->lexer);
10521 /* Peek at the next token. */
10522 token = cp_lexer_peek_token (parser->lexer);
10523 /* If the next token is `]', then there is no
10524 constant-expression. */
10525 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10526 {
10527 bool non_constant_p;
10528
21526606 10529 bounds
14d22dd6
MM
10530 = cp_parser_constant_expression (parser,
10531 /*allow_non_constant=*/true,
10532 &non_constant_p);
d17811fd 10533 if (!non_constant_p)
9baa27a9 10534 bounds = fold_non_dependent_expr (bounds);
14d22dd6 10535 }
a723baf1
MM
10536 else
10537 bounds = NULL_TREE;
10538 /* Look for the closing `]'. */
62b8a44e
NS
10539 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10540 {
10541 declarator = error_mark_node;
10542 break;
10543 }
a723baf1
MM
10544
10545 declarator = build_nt (ARRAY_REF, declarator, bounds);
10546 }
62b8a44e 10547 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10548 {
a668c6ad 10549 /* Parse a declarator-id */
62b8a44e
NS
10550 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10551 cp_parser_parse_tentatively (parser);
10552 declarator = cp_parser_declarator_id (parser);
712becab
NS
10553 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10554 {
10555 if (!cp_parser_parse_definitely (parser))
10556 declarator = error_mark_node;
10557 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10558 {
10559 cp_parser_error (parser, "expected unqualified-id");
10560 declarator = error_mark_node;
10561 }
10562 }
21526606 10563
62b8a44e
NS
10564 if (declarator == error_mark_node)
10565 break;
21526606 10566
d9a50301
KL
10567 if (TREE_CODE (declarator) == SCOPE_REF
10568 && !current_scope ())
62b8a44e
NS
10569 {
10570 tree scope = TREE_OPERAND (declarator, 0);
712becab 10571
62b8a44e
NS
10572 /* In the declaration of a member of a template class
10573 outside of the class itself, the SCOPE will sometimes
10574 be a TYPENAME_TYPE. For example, given:
21526606 10575
62b8a44e
NS
10576 template <typename T>
10577 int S<T>::R::i = 3;
21526606 10578
62b8a44e
NS
10579 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10580 this context, we must resolve S<T>::R to an ordinary
10581 type, rather than a typename type.
21526606 10582
62b8a44e
NS
10583 The reason we normally avoid resolving TYPENAME_TYPEs
10584 is that a specialization of `S' might render
10585 `S<T>::R' not a type. However, if `S' is
10586 specialized, then this `i' will not be used, so there
10587 is no harm in resolving the types here. */
10588 if (TREE_CODE (scope) == TYPENAME_TYPE)
10589 {
14d22dd6
MM
10590 tree type;
10591
62b8a44e 10592 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10593 type = resolve_typename_type (scope,
10594 /*only_current_p=*/false);
62b8a44e 10595 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10596 if (type != error_mark_node)
10597 scope = type;
62b8a44e 10598 /* Build a new DECLARATOR. */
21526606 10599 declarator = build_nt (SCOPE_REF,
62b8a44e
NS
10600 scope,
10601 TREE_OPERAND (declarator, 1));
10602 }
10603 }
21526606
EC
10604
10605 /* Check to see whether the declarator-id names a constructor,
62b8a44e 10606 destructor, or conversion. */
21526606
EC
10607 if (declarator && ctor_dtor_or_conv_p
10608 && ((TREE_CODE (declarator) == SCOPE_REF
62b8a44e
NS
10609 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10610 || (TREE_CODE (declarator) != SCOPE_REF
10611 && at_class_scope_p ())))
a723baf1 10612 {
62b8a44e
NS
10613 tree unqualified_name;
10614 tree class_type;
10615
10616 /* Get the unqualified part of the name. */
10617 if (TREE_CODE (declarator) == SCOPE_REF)
10618 {
10619 class_type = TREE_OPERAND (declarator, 0);
10620 unqualified_name = TREE_OPERAND (declarator, 1);
10621 }
10622 else
10623 {
10624 class_type = current_class_type;
10625 unqualified_name = declarator;
10626 }
10627
10628 /* See if it names ctor, dtor or conv. */
10629 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10630 || IDENTIFIER_TYPENAME_P (unqualified_name)
ab73670a
MM
10631 || constructor_name_p (unqualified_name, class_type)
10632 || (TREE_CODE (unqualified_name) == TYPE_DECL
10633 && same_type_p (TREE_TYPE (unqualified_name),
10634 class_type)))
7efa3e22 10635 *ctor_dtor_or_conv_p = -1;
a723baf1 10636 }
62b8a44e
NS
10637
10638 handle_declarator:;
10639 scope = get_scope_of_declarator (declarator);
10640 if (scope)
91b004e5
MM
10641 /* Any names that appear after the declarator-id for a
10642 member are looked up in the containing scope. */
10643 pop_p = push_scope (scope);
62b8a44e
NS
10644 parser->in_declarator_p = true;
10645 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10646 || (declarator
10647 && (TREE_CODE (declarator) == SCOPE_REF
10648 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10649 /* Default args are only allowed on function
10650 declarations. */
10651 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10652 else
62b8a44e
NS
10653 parser->default_arg_ok_p = false;
10654
10655 first = false;
a723baf1 10656 }
62b8a44e 10657 /* We're done. */
a723baf1
MM
10658 else
10659 break;
a723baf1
MM
10660 }
10661
10662 /* For an abstract declarator, we might wind up with nothing at this
10663 point. That's an error; the declarator is not optional. */
10664 if (!declarator)
10665 cp_parser_error (parser, "expected declarator");
10666
10667 /* If we entered a scope, we must exit it now. */
91b004e5 10668 if (pop_p)
a723baf1
MM
10669 pop_scope (scope);
10670
10671 parser->default_arg_ok_p = saved_default_arg_ok_p;
10672 parser->in_declarator_p = saved_in_declarator_p;
21526606 10673
a723baf1
MM
10674 return declarator;
10675}
10676
21526606 10677/* Parse a ptr-operator.
a723baf1
MM
10678
10679 ptr-operator:
10680 * cv-qualifier-seq [opt]
10681 &
10682 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10683
10684 GNU Extension:
10685
10686 ptr-operator:
10687 & cv-qualifier-seq [opt]
10688
10689 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10690 used. Returns ADDR_EXPR if a reference was used. In the
21526606 10691 case of a pointer-to-member, *TYPE is filled in with the
a723baf1
MM
10692 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10693 with the cv-qualifier-seq, or NULL_TREE, if there are no
10694 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
21526606 10695
a723baf1 10696static enum tree_code
21526606
EC
10697cp_parser_ptr_operator (cp_parser* parser,
10698 tree* type,
94edc4ab 10699 tree* cv_qualifier_seq)
a723baf1
MM
10700{
10701 enum tree_code code = ERROR_MARK;
10702 cp_token *token;
10703
10704 /* Assume that it's not a pointer-to-member. */
10705 *type = NULL_TREE;
10706 /* And that there are no cv-qualifiers. */
10707 *cv_qualifier_seq = NULL_TREE;
10708
10709 /* Peek at the next token. */
10710 token = cp_lexer_peek_token (parser->lexer);
10711 /* If it's a `*' or `&' we have a pointer or reference. */
10712 if (token->type == CPP_MULT || token->type == CPP_AND)
10713 {
10714 /* Remember which ptr-operator we were processing. */
10715 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10716
10717 /* Consume the `*' or `&'. */
10718 cp_lexer_consume_token (parser->lexer);
10719
10720 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10721 `&', if we are allowing GNU extensions. (The only qualifier
10722 that can legally appear after `&' is `restrict', but that is
10723 enforced during semantic analysis. */
21526606 10724 if (code == INDIRECT_REF
a723baf1
MM
10725 || cp_parser_allow_gnu_extensions_p (parser))
10726 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10727 }
10728 else
10729 {
10730 /* Try the pointer-to-member case. */
10731 cp_parser_parse_tentatively (parser);
10732 /* Look for the optional `::' operator. */
10733 cp_parser_global_scope_opt (parser,
10734 /*current_scope_valid_p=*/false);
10735 /* Look for the nested-name specifier. */
10736 cp_parser_nested_name_specifier (parser,
10737 /*typename_keyword_p=*/false,
10738 /*check_dependency_p=*/true,
a668c6ad
MM
10739 /*type_p=*/false,
10740 /*is_declaration=*/false);
a723baf1
MM
10741 /* If we found it, and the next token is a `*', then we are
10742 indeed looking at a pointer-to-member operator. */
10743 if (!cp_parser_error_occurred (parser)
10744 && cp_parser_require (parser, CPP_MULT, "`*'"))
10745 {
10746 /* The type of which the member is a member is given by the
10747 current SCOPE. */
10748 *type = parser->scope;
10749 /* The next name will not be qualified. */
10750 parser->scope = NULL_TREE;
10751 parser->qualifying_scope = NULL_TREE;
10752 parser->object_scope = NULL_TREE;
10753 /* Indicate that the `*' operator was used. */
10754 code = INDIRECT_REF;
10755 /* Look for the optional cv-qualifier-seq. */
10756 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10757 }
10758 /* If that didn't work we don't have a ptr-operator. */
10759 if (!cp_parser_parse_definitely (parser))
10760 cp_parser_error (parser, "expected ptr-operator");
10761 }
10762
10763 return code;
10764}
10765
10766/* Parse an (optional) cv-qualifier-seq.
10767
10768 cv-qualifier-seq:
21526606 10769 cv-qualifier cv-qualifier-seq [opt]
a723baf1
MM
10770
10771 Returns a TREE_LIST. The TREE_VALUE of each node is the
10772 representation of a cv-qualifier. */
10773
10774static tree
94edc4ab 10775cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10776{
10777 tree cv_qualifiers = NULL_TREE;
21526606 10778
a723baf1
MM
10779 while (true)
10780 {
10781 tree cv_qualifier;
10782
10783 /* Look for the next cv-qualifier. */
10784 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10785 /* If we didn't find one, we're done. */
10786 if (!cv_qualifier)
10787 break;
10788
10789 /* Add this cv-qualifier to the list. */
21526606 10790 cv_qualifiers
a723baf1
MM
10791 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10792 }
10793
10794 /* We built up the list in reverse order. */
10795 return nreverse (cv_qualifiers);
10796}
10797
10798/* Parse an (optional) cv-qualifier.
10799
10800 cv-qualifier:
10801 const
21526606 10802 volatile
a723baf1
MM
10803
10804 GNU Extension:
10805
10806 cv-qualifier:
10807 __restrict__ */
10808
10809static tree
94edc4ab 10810cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10811{
10812 cp_token *token;
10813 tree cv_qualifier = NULL_TREE;
10814
10815 /* Peek at the next token. */
10816 token = cp_lexer_peek_token (parser->lexer);
10817 /* See if it's a cv-qualifier. */
10818 switch (token->keyword)
10819 {
10820 case RID_CONST:
10821 case RID_VOLATILE:
10822 case RID_RESTRICT:
10823 /* Save the value of the token. */
10824 cv_qualifier = token->value;
10825 /* Consume the token. */
10826 cp_lexer_consume_token (parser->lexer);
10827 break;
10828
10829 default:
10830 break;
10831 }
10832
10833 return cv_qualifier;
10834}
10835
10836/* Parse a declarator-id.
10837
10838 declarator-id:
10839 id-expression
21526606 10840 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
10841
10842 In the `id-expression' case, the value returned is as for
10843 cp_parser_id_expression if the id-expression was an unqualified-id.
10844 If the id-expression was a qualified-id, then a SCOPE_REF is
10845 returned. The first operand is the scope (either a NAMESPACE_DECL
10846 or TREE_TYPE), but the second is still just a representation of an
10847 unqualified-id. */
10848
10849static tree
94edc4ab 10850cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10851{
10852 tree id_expression;
10853
10854 /* The expression must be an id-expression. Assume that qualified
10855 names are the names of types so that:
10856
10857 template <class T>
10858 int S<T>::R::i = 3;
10859
10860 will work; we must treat `S<T>::R' as the name of a type.
10861 Similarly, assume that qualified names are templates, where
10862 required, so that:
10863
10864 template <class T>
10865 int S<T>::R<T>::i = 3;
10866
10867 will work, too. */
10868 id_expression = cp_parser_id_expression (parser,
10869 /*template_keyword_p=*/false,
10870 /*check_dependency_p=*/false,
f3c2dfc6
MM
10871 /*template_p=*/NULL,
10872 /*declarator_p=*/true);
21526606 10873 /* If the name was qualified, create a SCOPE_REF to represent
a723baf1
MM
10874 that. */
10875 if (parser->scope)
ec20aa6c
MM
10876 {
10877 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10878 parser->scope = NULL_TREE;
10879 }
a723baf1
MM
10880
10881 return id_expression;
10882}
10883
10884/* Parse a type-id.
10885
10886 type-id:
10887 type-specifier-seq abstract-declarator [opt]
10888
10889 Returns the TYPE specified. */
10890
10891static tree
94edc4ab 10892cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10893{
10894 tree type_specifier_seq;
10895 tree abstract_declarator;
10896
10897 /* Parse the type-specifier-seq. */
21526606 10898 type_specifier_seq
a723baf1
MM
10899 = cp_parser_type_specifier_seq (parser);
10900 if (type_specifier_seq == error_mark_node)
10901 return error_mark_node;
10902
10903 /* There might or might not be an abstract declarator. */
10904 cp_parser_parse_tentatively (parser);
10905 /* Look for the declarator. */
21526606 10906 abstract_declarator
4bb8ca28
MM
10907 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10908 /*parenthesized_p=*/NULL);
a723baf1
MM
10909 /* Check to see if there really was a declarator. */
10910 if (!cp_parser_parse_definitely (parser))
10911 abstract_declarator = NULL_TREE;
10912
10913 return groktypename (build_tree_list (type_specifier_seq,
10914 abstract_declarator));
10915}
10916
10917/* Parse a type-specifier-seq.
10918
10919 type-specifier-seq:
10920 type-specifier type-specifier-seq [opt]
10921
10922 GNU extension:
10923
10924 type-specifier-seq:
10925 attributes type-specifier-seq [opt]
10926
10927 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10928 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10929
10930static tree
94edc4ab 10931cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10932{
10933 bool seen_type_specifier = false;
10934 tree type_specifier_seq = NULL_TREE;
10935
10936 /* Parse the type-specifiers and attributes. */
10937 while (true)
10938 {
10939 tree type_specifier;
10940
10941 /* Check for attributes first. */
10942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10943 {
10944 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10945 NULL_TREE,
10946 type_specifier_seq);
10947 continue;
10948 }
10949
10950 /* After the first type-specifier, others are optional. */
10951 if (seen_type_specifier)
10952 cp_parser_parse_tentatively (parser);
10953 /* Look for the type-specifier. */
21526606 10954 type_specifier = cp_parser_type_specifier (parser,
a723baf1
MM
10955 CP_PARSER_FLAGS_NONE,
10956 /*is_friend=*/false,
10957 /*is_declaration=*/false,
10958 NULL,
10959 NULL);
10960 /* If the first type-specifier could not be found, this is not a
10961 type-specifier-seq at all. */
10962 if (!seen_type_specifier && type_specifier == error_mark_node)
10963 return error_mark_node;
10964 /* If subsequent type-specifiers could not be found, the
10965 type-specifier-seq is complete. */
10966 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10967 break;
10968
10969 /* Add the new type-specifier to the list. */
21526606 10970 type_specifier_seq
a723baf1
MM
10971 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10972 seen_type_specifier = true;
10973 }
10974
10975 /* We built up the list in reverse order. */
10976 return nreverse (type_specifier_seq);
10977}
10978
10979/* Parse a parameter-declaration-clause.
10980
10981 parameter-declaration-clause:
10982 parameter-declaration-list [opt] ... [opt]
10983 parameter-declaration-list , ...
10984
10985 Returns a representation for the parameter declarations. Each node
10986 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10987 representation.) If the parameter-declaration-clause ends with an
10988 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10989 list. A return value of NULL_TREE indicates a
10990 parameter-declaration-clause consisting only of an ellipsis. */
10991
10992static tree
94edc4ab 10993cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10994{
10995 tree parameters;
10996 cp_token *token;
10997 bool ellipsis_p;
10998
10999 /* Peek at the next token. */
11000 token = cp_lexer_peek_token (parser->lexer);
11001 /* Check for trivial parameter-declaration-clauses. */
11002 if (token->type == CPP_ELLIPSIS)
11003 {
11004 /* Consume the `...' token. */
11005 cp_lexer_consume_token (parser->lexer);
11006 return NULL_TREE;
11007 }
11008 else if (token->type == CPP_CLOSE_PAREN)
11009 /* There are no parameters. */
c73aecdf
DE
11010 {
11011#ifndef NO_IMPLICIT_EXTERN_C
11012 if (in_system_header && current_class_type == NULL
11013 && current_lang_name == lang_name_c)
11014 return NULL_TREE;
11015 else
11016#endif
11017 return void_list_node;
11018 }
a723baf1
MM
11019 /* Check for `(void)', too, which is a special case. */
11020 else if (token->keyword == RID_VOID
21526606 11021 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11022 == CPP_CLOSE_PAREN))
11023 {
11024 /* Consume the `void' token. */
11025 cp_lexer_consume_token (parser->lexer);
11026 /* There are no parameters. */
11027 return void_list_node;
11028 }
21526606 11029
a723baf1
MM
11030 /* Parse the parameter-declaration-list. */
11031 parameters = cp_parser_parameter_declaration_list (parser);
11032 /* If a parse error occurred while parsing the
11033 parameter-declaration-list, then the entire
11034 parameter-declaration-clause is erroneous. */
11035 if (parameters == error_mark_node)
11036 return error_mark_node;
11037
11038 /* Peek at the next token. */
11039 token = cp_lexer_peek_token (parser->lexer);
11040 /* If it's a `,', the clause should terminate with an ellipsis. */
11041 if (token->type == CPP_COMMA)
11042 {
11043 /* Consume the `,'. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Expect an ellipsis. */
21526606 11046 ellipsis_p
a723baf1
MM
11047 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11048 }
21526606 11049 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11050 omitted. */
11051 else if (token->type == CPP_ELLIPSIS)
11052 {
11053 /* Consume the `...' token. */
11054 cp_lexer_consume_token (parser->lexer);
11055 /* And remember that we saw it. */
11056 ellipsis_p = true;
11057 }
11058 else
11059 ellipsis_p = false;
11060
11061 /* Finish the parameter list. */
11062 return finish_parmlist (parameters, ellipsis_p);
11063}
11064
11065/* Parse a parameter-declaration-list.
11066
11067 parameter-declaration-list:
11068 parameter-declaration
11069 parameter-declaration-list , parameter-declaration
11070
11071 Returns a representation of the parameter-declaration-list, as for
11072 cp_parser_parameter_declaration_clause. However, the
11073 `void_list_node' is never appended to the list. */
11074
11075static tree
94edc4ab 11076cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
11077{
11078 tree parameters = NULL_TREE;
11079
11080 /* Look for more parameters. */
11081 while (true)
11082 {
11083 tree parameter;
4bb8ca28 11084 bool parenthesized_p;
a723baf1 11085 /* Parse the parameter. */
21526606
EC
11086 parameter
11087 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11088 /*template_parm_p=*/false,
11089 &parenthesized_p);
ec194454 11090
34cd5ae7 11091 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
11092 then the entire parameter-declaration-list is erroneous. */
11093 if (parameter == error_mark_node)
11094 {
11095 parameters = error_mark_node;
11096 break;
11097 }
11098 /* Add the new parameter to the list. */
11099 TREE_CHAIN (parameter) = parameters;
11100 parameters = parameter;
11101
11102 /* Peek at the next token. */
11103 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11104 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11105 /* The parameter-declaration-list is complete. */
11106 break;
11107 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11108 {
11109 cp_token *token;
11110
11111 /* Peek at the next token. */
11112 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11113 /* If it's an ellipsis, then the list is complete. */
11114 if (token->type == CPP_ELLIPSIS)
11115 break;
11116 /* Otherwise, there must be more parameters. Consume the
11117 `,'. */
11118 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11119 /* When parsing something like:
11120
11121 int i(float f, double d)
21526606 11122
4bb8ca28
MM
11123 we can tell after seeing the declaration for "f" that we
11124 are not looking at an initialization of a variable "i",
21526606 11125 but rather at the declaration of a function "i".
4bb8ca28
MM
11126
11127 Due to the fact that the parsing of template arguments
11128 (as specified to a template-id) requires backtracking we
11129 cannot use this technique when inside a template argument
11130 list. */
11131 if (!parser->in_template_argument_list_p
4d5fe289 11132 && !parser->in_type_id_in_expr_p
4bb8ca28
MM
11133 && cp_parser_parsing_tentatively (parser)
11134 && !cp_parser_committed_to_tentative_parse (parser)
11135 /* However, a parameter-declaration of the form
11136 "foat(f)" (which is a valid declaration of a
11137 parameter "f") can also be interpreted as an
11138 expression (the conversion of "f" to "float"). */
11139 && !parenthesized_p)
11140 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11141 }
11142 else
11143 {
11144 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
11145 if (!cp_parser_parsing_tentatively (parser)
11146 || cp_parser_committed_to_tentative_parse (parser))
21526606 11147 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11148 /*recovering=*/true,
5c832178 11149 /*or_comma=*/false,
4bb8ca28 11150 /*consume_paren=*/false);
a723baf1
MM
11151 break;
11152 }
11153 }
11154
11155 /* We built up the list in reverse order; straighten it out now. */
11156 return nreverse (parameters);
11157}
11158
11159/* Parse a parameter declaration.
11160
11161 parameter-declaration:
11162 decl-specifier-seq declarator
11163 decl-specifier-seq declarator = assignment-expression
11164 decl-specifier-seq abstract-declarator [opt]
11165 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11166
ec194454
MM
11167 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11168 declares a template parameter. (In that case, a non-nested `>'
11169 token encountered during the parsing of the assignment-expression
11170 is not interpreted as a greater-than operator.)
a723baf1
MM
11171
11172 Returns a TREE_LIST representing the parameter-declaration. The
4bb8ca28
MM
11173 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11174 there is no default argument. The TREE_VALUE is a representation
11175 of the decl-specifier-seq and declarator. In particular, the
11176 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11177 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11178 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11179 the declarator is of the form "(p)". */
a723baf1
MM
11180
11181static tree
21526606 11182cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11183 bool template_parm_p,
11184 bool *parenthesized_p)
a723baf1 11185{
560ad596 11186 int declares_class_or_enum;
ec194454 11187 bool greater_than_is_operator_p;
a723baf1
MM
11188 tree decl_specifiers;
11189 tree attributes;
11190 tree declarator;
11191 tree default_argument;
11192 tree parameter;
11193 cp_token *token;
11194 const char *saved_message;
11195
ec194454
MM
11196 /* In a template parameter, `>' is not an operator.
11197
11198 [temp.param]
11199
11200 When parsing a default template-argument for a non-type
11201 template-parameter, the first non-nested `>' is taken as the end
11202 of the template parameter-list rather than a greater-than
11203 operator. */
11204 greater_than_is_operator_p = !template_parm_p;
11205
a723baf1
MM
11206 /* Type definitions may not appear in parameter types. */
11207 saved_message = parser->type_definition_forbidden_message;
21526606 11208 parser->type_definition_forbidden_message
a723baf1
MM
11209 = "types may not be defined in parameter types";
11210
11211 /* Parse the declaration-specifiers. */
21526606 11212 decl_specifiers
a723baf1
MM
11213 = cp_parser_decl_specifier_seq (parser,
11214 CP_PARSER_FLAGS_NONE,
11215 &attributes,
11216 &declares_class_or_enum);
11217 /* If an error occurred, there's no reason to attempt to parse the
11218 rest of the declaration. */
11219 if (cp_parser_error_occurred (parser))
11220 {
11221 parser->type_definition_forbidden_message = saved_message;
11222 return error_mark_node;
11223 }
11224
11225 /* Peek at the next token. */
11226 token = cp_lexer_peek_token (parser->lexer);
11227 /* If the next token is a `)', `,', `=', `>', or `...', then there
11228 is no declarator. */
21526606 11229 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11230 || token->type == CPP_COMMA
11231 || token->type == CPP_EQ
11232 || token->type == CPP_ELLIPSIS
11233 || token->type == CPP_GREATER)
4bb8ca28
MM
11234 {
11235 declarator = NULL_TREE;
11236 if (parenthesized_p)
11237 *parenthesized_p = false;
11238 }
a723baf1
MM
11239 /* Otherwise, there should be a declarator. */
11240 else
11241 {
11242 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11243 parser->default_arg_ok_p = false;
21526606 11244
5c832178
MM
11245 /* After seeing a decl-specifier-seq, if the next token is not a
11246 "(", there is no possibility that the code is a valid
4f8163b1
MM
11247 expression. Therefore, if parsing tentatively, we commit at
11248 this point. */
5c832178 11249 if (!parser->in_template_argument_list_p
643aee72 11250 /* In an expression context, having seen:
4f8163b1 11251
a7324e75 11252 (int((char ...
4f8163b1
MM
11253
11254 we cannot be sure whether we are looking at a
a7324e75
MM
11255 function-type (taking a "char" as a parameter) or a cast
11256 of some object of type "char" to "int". */
4f8163b1 11257 && !parser->in_type_id_in_expr_p
5c832178
MM
11258 && cp_parser_parsing_tentatively (parser)
11259 && !cp_parser_committed_to_tentative_parse (parser)
11260 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11261 cp_parser_commit_to_tentative_parse (parser);
11262 /* Parse the declarator. */
a723baf1 11263 declarator = cp_parser_declarator (parser,
62b8a44e 11264 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
11265 /*ctor_dtor_or_conv_p=*/NULL,
11266 parenthesized_p);
a723baf1 11267 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
11268 /* After the declarator, allow more attributes. */
11269 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
11270 }
11271
62b8a44e 11272 /* The restriction on defining new types applies only to the type
a723baf1
MM
11273 of the parameter, not to the default argument. */
11274 parser->type_definition_forbidden_message = saved_message;
11275
11276 /* If the next token is `=', then process a default argument. */
11277 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11278 {
11279 bool saved_greater_than_is_operator_p;
11280 /* Consume the `='. */
11281 cp_lexer_consume_token (parser->lexer);
11282
11283 /* If we are defining a class, then the tokens that make up the
11284 default argument must be saved and processed later. */
21526606 11285 if (!template_parm_p && at_class_scope_p ()
ec194454 11286 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11287 {
11288 unsigned depth = 0;
11289
11290 /* Create a DEFAULT_ARG to represented the unparsed default
11291 argument. */
11292 default_argument = make_node (DEFAULT_ARG);
11293 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11294
11295 /* Add tokens until we have processed the entire default
11296 argument. */
11297 while (true)
11298 {
11299 bool done = false;
11300 cp_token *token;
11301
11302 /* Peek at the next token. */
11303 token = cp_lexer_peek_token (parser->lexer);
11304 /* What we do depends on what token we have. */
11305 switch (token->type)
11306 {
11307 /* In valid code, a default argument must be
11308 immediately followed by a `,' `)', or `...'. */
11309 case CPP_COMMA:
11310 case CPP_CLOSE_PAREN:
11311 case CPP_ELLIPSIS:
11312 /* If we run into a non-nested `;', `}', or `]',
11313 then the code is invalid -- but the default
11314 argument is certainly over. */
11315 case CPP_SEMICOLON:
11316 case CPP_CLOSE_BRACE:
11317 case CPP_CLOSE_SQUARE:
11318 if (depth == 0)
11319 done = true;
11320 /* Update DEPTH, if necessary. */
11321 else if (token->type == CPP_CLOSE_PAREN
11322 || token->type == CPP_CLOSE_BRACE
11323 || token->type == CPP_CLOSE_SQUARE)
11324 --depth;
11325 break;
11326
11327 case CPP_OPEN_PAREN:
11328 case CPP_OPEN_SQUARE:
11329 case CPP_OPEN_BRACE:
11330 ++depth;
11331 break;
11332
11333 case CPP_GREATER:
11334 /* If we see a non-nested `>', and `>' is not an
11335 operator, then it marks the end of the default
11336 argument. */
11337 if (!depth && !greater_than_is_operator_p)
11338 done = true;
11339 break;
11340
11341 /* If we run out of tokens, issue an error message. */
11342 case CPP_EOF:
11343 error ("file ends in default argument");
11344 done = true;
11345 break;
11346
11347 case CPP_NAME:
11348 case CPP_SCOPE:
11349 /* In these cases, we should look for template-ids.
21526606 11350 For example, if the default argument is
a723baf1
MM
11351 `X<int, double>()', we need to do name lookup to
11352 figure out whether or not `X' is a template; if
34cd5ae7 11353 so, the `,' does not end the default argument.
a723baf1
MM
11354
11355 That is not yet done. */
11356 break;
11357
11358 default:
11359 break;
11360 }
11361
11362 /* If we've reached the end, stop. */
11363 if (done)
11364 break;
21526606 11365
a723baf1
MM
11366 /* Add the token to the token block. */
11367 token = cp_lexer_consume_token (parser->lexer);
11368 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11369 token);
11370 }
11371 }
11372 /* Outside of a class definition, we can just parse the
11373 assignment-expression. */
11374 else
11375 {
11376 bool saved_local_variables_forbidden_p;
11377
11378 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11379 set correctly. */
21526606 11380 saved_greater_than_is_operator_p
a723baf1
MM
11381 = parser->greater_than_is_operator_p;
11382 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11383 /* Local variable names (and the `this' keyword) may not
11384 appear in a default argument. */
21526606 11385 saved_local_variables_forbidden_p
a723baf1
MM
11386 = parser->local_variables_forbidden_p;
11387 parser->local_variables_forbidden_p = true;
11388 /* Parse the assignment-expression. */
11389 default_argument = cp_parser_assignment_expression (parser);
11390 /* Restore saved state. */
21526606 11391 parser->greater_than_is_operator_p
a723baf1 11392 = saved_greater_than_is_operator_p;
21526606
EC
11393 parser->local_variables_forbidden_p
11394 = saved_local_variables_forbidden_p;
a723baf1
MM
11395 }
11396 if (!parser->default_arg_ok_p)
11397 {
c67d36d0
NS
11398 if (!flag_pedantic_errors)
11399 warning ("deprecated use of default argument for parameter of non-function");
11400 else
11401 {
11402 error ("default arguments are only permitted for function parameters");
11403 default_argument = NULL_TREE;
11404 }
a723baf1
MM
11405 }
11406 }
11407 else
11408 default_argument = NULL_TREE;
21526606 11409
a723baf1
MM
11410 /* Create the representation of the parameter. */
11411 if (attributes)
11412 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
21526606 11413 parameter = build_tree_list (default_argument,
a723baf1
MM
11414 build_tree_list (decl_specifiers,
11415 declarator));
11416
11417 return parameter;
11418}
11419
a723baf1
MM
11420/* Parse a function-body.
11421
11422 function-body:
11423 compound_statement */
11424
11425static void
11426cp_parser_function_body (cp_parser *parser)
11427{
a5bcc582 11428 cp_parser_compound_statement (parser, false);
a723baf1
MM
11429}
11430
11431/* Parse a ctor-initializer-opt followed by a function-body. Return
11432 true if a ctor-initializer was present. */
11433
11434static bool
11435cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11436{
11437 tree body;
11438 bool ctor_initializer_p;
11439
11440 /* Begin the function body. */
11441 body = begin_function_body ();
11442 /* Parse the optional ctor-initializer. */
11443 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11444 /* Parse the function-body. */
11445 cp_parser_function_body (parser);
11446 /* Finish the function body. */
11447 finish_function_body (body);
11448
11449 return ctor_initializer_p;
11450}
11451
11452/* Parse an initializer.
11453
11454 initializer:
11455 = initializer-clause
21526606 11456 ( expression-list )
a723baf1
MM
11457
11458 Returns a expression representing the initializer. If no
21526606 11459 initializer is present, NULL_TREE is returned.
a723baf1
MM
11460
11461 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11462 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11463 set to FALSE if there is no initializer present. If there is an
11464 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11465 is set to true; otherwise it is set to false. */
a723baf1
MM
11466
11467static tree
39703eb9
MM
11468cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11469 bool* non_constant_p)
a723baf1
MM
11470{
11471 cp_token *token;
11472 tree init;
11473
11474 /* Peek at the next token. */
11475 token = cp_lexer_peek_token (parser->lexer);
11476
11477 /* Let our caller know whether or not this initializer was
11478 parenthesized. */
11479 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11480 /* Assume that the initializer is constant. */
11481 *non_constant_p = false;
a723baf1
MM
11482
11483 if (token->type == CPP_EQ)
11484 {
11485 /* Consume the `='. */
11486 cp_lexer_consume_token (parser->lexer);
11487 /* Parse the initializer-clause. */
39703eb9 11488 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11489 }
11490 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11491 init = cp_parser_parenthesized_expression_list (parser, false,
11492 non_constant_p);
a723baf1
MM
11493 else
11494 {
11495 /* Anything else is an error. */
11496 cp_parser_error (parser, "expected initializer");
11497 init = error_mark_node;
11498 }
11499
11500 return init;
11501}
11502
21526606 11503/* Parse an initializer-clause.
a723baf1
MM
11504
11505 initializer-clause:
11506 assignment-expression
11507 { initializer-list , [opt] }
11508 { }
11509
21526606 11510 Returns an expression representing the initializer.
a723baf1
MM
11511
11512 If the `assignment-expression' production is used the value
21526606 11513 returned is simply a representation for the expression.
a723baf1
MM
11514
11515 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11516 the elements of the initializer-list (or NULL_TREE, if the last
11517 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11518 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11519 trailing `,' was provided. NON_CONSTANT_P is as for
11520 cp_parser_initializer. */
a723baf1
MM
11521
11522static tree
39703eb9 11523cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11524{
11525 tree initializer;
11526
11527 /* If it is not a `{', then we are looking at an
11528 assignment-expression. */
11529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e
GB
11530 {
11531 initializer
11532 = cp_parser_constant_expression (parser,
11533 /*allow_non_constant_p=*/true,
11534 non_constant_p);
11535 if (!*non_constant_p)
11536 initializer = fold_non_dependent_expr (initializer);
11537 }
a723baf1
MM
11538 else
11539 {
11540 /* Consume the `{' token. */
11541 cp_lexer_consume_token (parser->lexer);
11542 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11543 initializer = make_node (CONSTRUCTOR);
11544 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
21526606 11545 necessary, but check_initializer depends upon it, for
a723baf1
MM
11546 now. */
11547 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11548 /* If it's not a `}', then there is a non-trivial initializer. */
11549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11550 {
11551 /* Parse the initializer list. */
11552 CONSTRUCTOR_ELTS (initializer)
39703eb9 11553 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
11554 /* A trailing `,' token is allowed. */
11555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11556 cp_lexer_consume_token (parser->lexer);
11557 }
a723baf1
MM
11558 /* Now, there should be a trailing `}'. */
11559 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11560 }
11561
11562 return initializer;
11563}
11564
11565/* Parse an initializer-list.
11566
11567 initializer-list:
11568 initializer-clause
11569 initializer-list , initializer-clause
11570
11571 GNU Extension:
21526606 11572
a723baf1
MM
11573 initializer-list:
11574 identifier : initializer-clause
11575 initializer-list, identifier : initializer-clause
11576
11577 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11578 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
11579 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11580 as for cp_parser_initializer. */
a723baf1
MM
11581
11582static tree
39703eb9 11583cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11584{
11585 tree initializers = NULL_TREE;
11586
39703eb9
MM
11587 /* Assume all of the expressions are constant. */
11588 *non_constant_p = false;
11589
a723baf1
MM
11590 /* Parse the rest of the list. */
11591 while (true)
11592 {
11593 cp_token *token;
11594 tree identifier;
11595 tree initializer;
39703eb9
MM
11596 bool clause_non_constant_p;
11597
a723baf1
MM
11598 /* If the next token is an identifier and the following one is a
11599 colon, we are looking at the GNU designated-initializer
11600 syntax. */
11601 if (cp_parser_allow_gnu_extensions_p (parser)
11602 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11603 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11604 {
11605 /* Consume the identifier. */
11606 identifier = cp_lexer_consume_token (parser->lexer)->value;
11607 /* Consume the `:'. */
11608 cp_lexer_consume_token (parser->lexer);
11609 }
11610 else
11611 identifier = NULL_TREE;
11612
11613 /* Parse the initializer. */
21526606 11614 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
11615 &clause_non_constant_p);
11616 /* If any clause is non-constant, so is the entire initializer. */
11617 if (clause_non_constant_p)
11618 *non_constant_p = true;
a723baf1
MM
11619 /* Add it to the list. */
11620 initializers = tree_cons (identifier, initializer, initializers);
11621
11622 /* If the next token is not a comma, we have reached the end of
11623 the list. */
11624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11625 break;
11626
11627 /* Peek at the next token. */
11628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11629 /* If the next token is a `}', then we're still done. An
11630 initializer-clause can have a trailing `,' after the
11631 initializer-list and before the closing `}'. */
11632 if (token->type == CPP_CLOSE_BRACE)
11633 break;
11634
11635 /* Consume the `,' token. */
11636 cp_lexer_consume_token (parser->lexer);
11637 }
11638
11639 /* The initializers were built up in reverse order, so we need to
11640 reverse them now. */
11641 return nreverse (initializers);
11642}
11643
11644/* Classes [gram.class] */
11645
11646/* Parse a class-name.
11647
11648 class-name:
11649 identifier
11650 template-id
11651
11652 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11653 to indicate that names looked up in dependent types should be
11654 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11655 keyword has been used to indicate that the name that appears next
11656 is a template. TYPE_P is true iff the next name should be treated
11657 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11658 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11659 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11660 being defined in a class-head.
a723baf1
MM
11661
11662 Returns the TYPE_DECL representing the class. */
11663
11664static tree
21526606
EC
11665cp_parser_class_name (cp_parser *parser,
11666 bool typename_keyword_p,
11667 bool template_keyword_p,
a723baf1 11668 bool type_p,
a723baf1 11669 bool check_dependency_p,
a668c6ad
MM
11670 bool class_head_p,
11671 bool is_declaration)
a723baf1
MM
11672{
11673 tree decl;
11674 tree scope;
11675 bool typename_p;
e5976695
MM
11676 cp_token *token;
11677
11678 /* All class-names start with an identifier. */
11679 token = cp_lexer_peek_token (parser->lexer);
11680 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11681 {
11682 cp_parser_error (parser, "expected class-name");
11683 return error_mark_node;
11684 }
21526606 11685
a723baf1
MM
11686 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11687 to a template-id, so we save it here. */
11688 scope = parser->scope;
3adee96c
KL
11689 if (scope == error_mark_node)
11690 return error_mark_node;
21526606 11691
a723baf1
MM
11692 /* Any name names a type if we're following the `typename' keyword
11693 in a qualified name where the enclosing scope is type-dependent. */
11694 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11695 && dependent_type_p (scope));
e5976695
MM
11696 /* Handle the common case (an identifier, but not a template-id)
11697 efficiently. */
21526606 11698 if (token->type == CPP_NAME
f4abade9 11699 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 11700 {
a723baf1
MM
11701 tree identifier;
11702
11703 /* Look for the identifier. */
11704 identifier = cp_parser_identifier (parser);
11705 /* If the next token isn't an identifier, we are certainly not
11706 looking at a class-name. */
11707 if (identifier == error_mark_node)
11708 decl = error_mark_node;
11709 /* If we know this is a type-name, there's no need to look it
11710 up. */
11711 else if (typename_p)
11712 decl = identifier;
11713 else
11714 {
11715 /* If the next token is a `::', then the name must be a type
11716 name.
11717
11718 [basic.lookup.qual]
11719
11720 During the lookup for a name preceding the :: scope
11721 resolution operator, object, function, and enumerator
11722 names are ignored. */
11723 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11724 type_p = true;
11725 /* Look up the name. */
21526606 11726 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11727 type_p,
b0bc6e8e 11728 /*is_template=*/false,
eea9800f 11729 /*is_namespace=*/false,
a723baf1
MM
11730 check_dependency_p);
11731 }
11732 }
e5976695
MM
11733 else
11734 {
11735 /* Try a template-id. */
11736 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
11737 check_dependency_p,
11738 is_declaration);
e5976695
MM
11739 if (decl == error_mark_node)
11740 return error_mark_node;
11741 }
a723baf1
MM
11742
11743 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11744
11745 /* If this is a typename, create a TYPENAME_TYPE. */
11746 if (typename_p && decl != error_mark_node)
4bfb8bba
MM
11747 {
11748 decl = make_typename_type (scope, decl, /*complain=*/1);
11749 if (decl != error_mark_node)
11750 decl = TYPE_NAME (decl);
11751 }
a723baf1
MM
11752
11753 /* Check to see that it is really the name of a class. */
21526606 11754 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
11755 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11756 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11757 /* Situations like this:
11758
11759 template <typename T> struct A {
21526606 11760 typename T::template X<int>::I i;
a723baf1
MM
11761 };
11762
11763 are problematic. Is `T::template X<int>' a class-name? The
11764 standard does not seem to be definitive, but there is no other
11765 valid interpretation of the following `::'. Therefore, those
11766 names are considered class-names. */
78757caa 11767 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11768 else if (decl == error_mark_node
11769 || TREE_CODE (decl) != TYPE_DECL
11770 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11771 {
11772 cp_parser_error (parser, "expected class-name");
11773 return error_mark_node;
11774 }
11775
11776 return decl;
11777}
11778
11779/* Parse a class-specifier.
11780
11781 class-specifier:
11782 class-head { member-specification [opt] }
11783
11784 Returns the TREE_TYPE representing the class. */
11785
11786static tree
94edc4ab 11787cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11788{
11789 cp_token *token;
11790 tree type;
38b305d0 11791 tree attributes;
a723baf1
MM
11792 int has_trailing_semicolon;
11793 bool nested_name_specifier_p;
a723baf1 11794 unsigned saved_num_template_parameter_lists;
91b004e5 11795 bool pop_p = false;
a723baf1 11796
8d241e0b 11797 push_deferring_access_checks (dk_no_deferred);
cf22909c 11798
a723baf1
MM
11799 /* Parse the class-head. */
11800 type = cp_parser_class_head (parser,
38b305d0
JM
11801 &nested_name_specifier_p,
11802 &attributes);
a723baf1
MM
11803 /* If the class-head was a semantic disaster, skip the entire body
11804 of the class. */
11805 if (!type)
11806 {
11807 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11808 pop_deferring_access_checks ();
a723baf1
MM
11809 return error_mark_node;
11810 }
cf22909c 11811
a723baf1
MM
11812 /* Look for the `{'. */
11813 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11814 {
11815 pop_deferring_access_checks ();
11816 return error_mark_node;
11817 }
11818
a723baf1
MM
11819 /* Issue an error message if type-definitions are forbidden here. */
11820 cp_parser_check_type_definition (parser);
11821 /* Remember that we are defining one more class. */
11822 ++parser->num_classes_being_defined;
11823 /* Inside the class, surrounding template-parameter-lists do not
11824 apply. */
21526606
EC
11825 saved_num_template_parameter_lists
11826 = parser->num_template_parameter_lists;
a723baf1 11827 parser->num_template_parameter_lists = 0;
78757caa 11828
a723baf1 11829 /* Start the class. */
eeb23c11 11830 if (nested_name_specifier_p)
91b004e5 11831 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11832 type = begin_class_definition (type);
11833 if (type == error_mark_node)
9bcb9aae 11834 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11835 cp_parser_skip_to_closing_brace (parser);
11836 else
11837 /* Parse the member-specification. */
11838 cp_parser_member_specification_opt (parser);
11839 /* Look for the trailing `}'. */
11840 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11841 /* We get better error messages by noticing a common problem: a
11842 missing trailing `;'. */
11843 token = cp_lexer_peek_token (parser->lexer);
11844 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 11845 /* Look for trailing attributes to apply to this class. */
a723baf1 11846 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 11847 {
38b305d0
JM
11848 tree sub_attr = cp_parser_attributes_opt (parser);
11849 attributes = chainon (attributes, sub_attr);
560ad596 11850 }
38b305d0
JM
11851 if (type != error_mark_node)
11852 type = finish_struct (type, attributes);
91b004e5 11853 if (pop_p)
560ad596 11854 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11855 /* If this class is not itself within the scope of another class,
11856 then we need to parse the bodies of all of the queued function
11857 definitions. Note that the queued functions defined in a class
11858 are not always processed immediately following the
11859 class-specifier for that class. Consider:
11860
11861 struct A {
11862 struct B { void f() { sizeof (A); } };
11863 };
11864
11865 If `f' were processed before the processing of `A' were
11866 completed, there would be no way to compute the size of `A'.
11867 Note that the nesting we are interested in here is lexical --
11868 not the semantic nesting given by TYPE_CONTEXT. In particular,
11869 for:
11870
11871 struct A { struct B; };
11872 struct A::B { void f() { } };
11873
11874 there is no need to delay the parsing of `A::B::f'. */
21526606 11875 if (--parser->num_classes_being_defined == 0)
a723baf1 11876 {
8218bd34
MM
11877 tree queue_entry;
11878 tree fn;
a723baf1 11879
8218bd34
MM
11880 /* In a first pass, parse default arguments to the functions.
11881 Then, in a second pass, parse the bodies of the functions.
11882 This two-phased approach handles cases like:
21526606
EC
11883
11884 struct S {
11885 void f() { g(); }
8218bd34
MM
11886 void g(int i = 3);
11887 };
11888
11889 */
8db1028e
NS
11890 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11891 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11892 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11893 TREE_PURPOSE (parser->unparsed_functions_queues)
11894 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11895 {
11896 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11897 /* Make sure that any template parameters are in scope. */
11898 maybe_begin_member_template_processing (fn);
11899 /* If there are default arguments that have not yet been processed,
11900 take care of them now. */
11901 cp_parser_late_parsing_default_args (parser, fn);
11902 /* Remove any template parameters from the symbol table. */
11903 maybe_end_member_template_processing ();
11904 }
11905 /* Now parse the body of the functions. */
8db1028e
NS
11906 for (TREE_VALUE (parser->unparsed_functions_queues)
11907 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11908 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11909 TREE_VALUE (parser->unparsed_functions_queues)
11910 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11911 {
a723baf1 11912 /* Figure out which function we need to process. */
a723baf1
MM
11913 fn = TREE_VALUE (queue_entry);
11914
4543ee47
ZD
11915 /* A hack to prevent garbage collection. */
11916 function_depth++;
11917
a723baf1
MM
11918 /* Parse the function. */
11919 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 11920 function_depth--;
a723baf1
MM
11921 }
11922
a723baf1
MM
11923 }
11924
11925 /* Put back any saved access checks. */
cf22909c 11926 pop_deferring_access_checks ();
a723baf1
MM
11927
11928 /* Restore the count of active template-parameter-lists. */
11929 parser->num_template_parameter_lists
11930 = saved_num_template_parameter_lists;
11931
11932 return type;
11933}
11934
11935/* Parse a class-head.
11936
11937 class-head:
11938 class-key identifier [opt] base-clause [opt]
11939 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
11940 class-key nested-name-specifier [opt] template-id
11941 base-clause [opt]
a723baf1
MM
11942
11943 GNU Extensions:
11944 class-key attributes identifier [opt] base-clause [opt]
11945 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
11946 class-key attributes nested-name-specifier [opt] template-id
11947 base-clause [opt]
a723baf1
MM
11948
11949 Returns the TYPE of the indicated class. Sets
11950 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11951 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11952
11953 Returns NULL_TREE if the class-head is syntactically valid, but
11954 semantically invalid in a way that means we should skip the entire
11955 body of the class. */
11956
11957static tree
21526606 11958cp_parser_class_head (cp_parser* parser,
38b305d0
JM
11959 bool* nested_name_specifier_p,
11960 tree *attributes_p)
a723baf1
MM
11961{
11962 cp_token *token;
11963 tree nested_name_specifier;
11964 enum tag_types class_key;
11965 tree id = NULL_TREE;
11966 tree type = NULL_TREE;
11967 tree attributes;
11968 bool template_id_p = false;
11969 bool qualified_p = false;
11970 bool invalid_nested_name_p = false;
afb0918a 11971 bool invalid_explicit_specialization_p = false;
91b004e5 11972 bool pop_p = false;
a723baf1
MM
11973 unsigned num_templates;
11974
11975 /* Assume no nested-name-specifier will be present. */
11976 *nested_name_specifier_p = false;
11977 /* Assume no template parameter lists will be used in defining the
11978 type. */
11979 num_templates = 0;
11980
11981 /* Look for the class-key. */
11982 class_key = cp_parser_class_key (parser);
11983 if (class_key == none_type)
11984 return error_mark_node;
11985
11986 /* Parse the attributes. */
11987 attributes = cp_parser_attributes_opt (parser);
11988
11989 /* If the next token is `::', that is invalid -- but sometimes
11990 people do try to write:
11991
21526606 11992 struct ::S {};
a723baf1
MM
11993
11994 Handle this gracefully by accepting the extra qualifier, and then
11995 issuing an error about it later if this really is a
2050a1bb 11996 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11997 specifier, remain silent. */
11998 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11999 qualified_p = true;
12000
8d241e0b
KL
12001 push_deferring_access_checks (dk_no_check);
12002
a723baf1
MM
12003 /* Determine the name of the class. Begin by looking for an
12004 optional nested-name-specifier. */
21526606 12005 nested_name_specifier
a723baf1
MM
12006 = cp_parser_nested_name_specifier_opt (parser,
12007 /*typename_keyword_p=*/false,
66d418e6 12008 /*check_dependency_p=*/false,
a668c6ad
MM
12009 /*type_p=*/false,
12010 /*is_declaration=*/false);
a723baf1
MM
12011 /* If there was a nested-name-specifier, then there *must* be an
12012 identifier. */
12013 if (nested_name_specifier)
12014 {
12015 /* Although the grammar says `identifier', it really means
12016 `class-name' or `template-name'. You are only allowed to
12017 define a class that has already been declared with this
21526606 12018 syntax.
a723baf1
MM
12019
12020 The proposed resolution for Core Issue 180 says that whever
12021 you see `class T::X' you should treat `X' as a type-name.
21526606 12022
a723baf1 12023 It is OK to define an inaccessible class; for example:
21526606 12024
a723baf1
MM
12025 class A { class B; };
12026 class A::B {};
21526606 12027
a723baf1
MM
12028 We do not know if we will see a class-name, or a
12029 template-name. We look for a class-name first, in case the
12030 class-name is a template-id; if we looked for the
12031 template-name first we would stop after the template-name. */
12032 cp_parser_parse_tentatively (parser);
12033 type = cp_parser_class_name (parser,
12034 /*typename_keyword_p=*/false,
12035 /*template_keyword_p=*/false,
12036 /*type_p=*/true,
a723baf1 12037 /*check_dependency_p=*/false,
a668c6ad
MM
12038 /*class_head_p=*/true,
12039 /*is_declaration=*/false);
a723baf1
MM
12040 /* If that didn't work, ignore the nested-name-specifier. */
12041 if (!cp_parser_parse_definitely (parser))
12042 {
12043 invalid_nested_name_p = true;
12044 id = cp_parser_identifier (parser);
12045 if (id == error_mark_node)
12046 id = NULL_TREE;
12047 }
12048 /* If we could not find a corresponding TYPE, treat this
12049 declaration like an unqualified declaration. */
12050 if (type == error_mark_node)
12051 nested_name_specifier = NULL_TREE;
12052 /* Otherwise, count the number of templates used in TYPE and its
12053 containing scopes. */
21526606 12054 else
a723baf1
MM
12055 {
12056 tree scope;
12057
21526606 12058 for (scope = TREE_TYPE (type);
a723baf1 12059 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12060 scope = (TYPE_P (scope)
a723baf1 12061 ? TYPE_CONTEXT (scope)
21526606
EC
12062 : DECL_CONTEXT (scope)))
12063 if (TYPE_P (scope)
a723baf1
MM
12064 && CLASS_TYPE_P (scope)
12065 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12066 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12067 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12068 ++num_templates;
12069 }
12070 }
12071 /* Otherwise, the identifier is optional. */
12072 else
12073 {
12074 /* We don't know whether what comes next is a template-id,
12075 an identifier, or nothing at all. */
12076 cp_parser_parse_tentatively (parser);
12077 /* Check for a template-id. */
21526606 12078 id = cp_parser_template_id (parser,
a723baf1 12079 /*template_keyword_p=*/false,
a668c6ad
MM
12080 /*check_dependency_p=*/true,
12081 /*is_declaration=*/true);
a723baf1
MM
12082 /* If that didn't work, it could still be an identifier. */
12083 if (!cp_parser_parse_definitely (parser))
12084 {
12085 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12086 id = cp_parser_identifier (parser);
12087 else
12088 id = NULL_TREE;
12089 }
12090 else
12091 {
12092 template_id_p = true;
12093 ++num_templates;
12094 }
12095 }
12096
8d241e0b
KL
12097 pop_deferring_access_checks ();
12098
ee43dab5
MM
12099 cp_parser_check_for_invalid_template_id (parser, id);
12100
a723baf1
MM
12101 /* If it's not a `:' or a `{' then we can't really be looking at a
12102 class-head, since a class-head only appears as part of a
12103 class-specifier. We have to detect this situation before calling
12104 xref_tag, since that has irreversible side-effects. */
12105 if (!cp_parser_next_token_starts_class_definition_p (parser))
12106 {
12107 cp_parser_error (parser, "expected `{' or `:'");
12108 return error_mark_node;
12109 }
12110
12111 /* At this point, we're going ahead with the class-specifier, even
12112 if some other problem occurs. */
12113 cp_parser_commit_to_tentative_parse (parser);
12114 /* Issue the error about the overly-qualified name now. */
12115 if (qualified_p)
12116 cp_parser_error (parser,
12117 "global qualification of class name is invalid");
12118 else if (invalid_nested_name_p)
12119 cp_parser_error (parser,
12120 "qualified name does not name a class");
88081599
MM
12121 else if (nested_name_specifier)
12122 {
12123 tree scope;
12124 /* Figure out in what scope the declaration is being placed. */
12125 scope = current_scope ();
12126 if (!scope)
12127 scope = current_namespace;
12128 /* If that scope does not contain the scope in which the
12129 class was originally declared, the program is invalid. */
12130 if (scope && !is_ancestor (scope, nested_name_specifier))
12131 {
12132 error ("declaration of `%D' in `%D' which does not "
12133 "enclose `%D'", type, scope, nested_name_specifier);
12134 type = NULL_TREE;
12135 goto done;
12136 }
12137 /* [dcl.meaning]
12138
12139 A declarator-id shall not be qualified exception of the
12140 definition of a ... nested class outside of its class
12141 ... [or] a the definition or explicit instantiation of a
12142 class member of a namespace outside of its namespace. */
12143 if (scope == nested_name_specifier)
12144 {
12145 pedwarn ("extra qualification ignored");
12146 nested_name_specifier = NULL_TREE;
12147 num_templates = 0;
12148 }
12149 }
afb0918a
MM
12150 /* An explicit-specialization must be preceded by "template <>". If
12151 it is not, try to recover gracefully. */
21526606 12152 if (at_namespace_scope_p ()
afb0918a 12153 && parser->num_template_parameter_lists == 0
eeb23c11 12154 && template_id_p)
afb0918a
MM
12155 {
12156 error ("an explicit specialization must be preceded by 'template <>'");
12157 invalid_explicit_specialization_p = true;
12158 /* Take the same action that would have been taken by
12159 cp_parser_explicit_specialization. */
12160 ++parser->num_template_parameter_lists;
12161 begin_specialization ();
12162 }
12163 /* There must be no "return" statements between this point and the
12164 end of this function; set "type "to the correct return value and
12165 use "goto done;" to return. */
a723baf1
MM
12166 /* Make sure that the right number of template parameters were
12167 present. */
12168 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12169 {
12170 /* If something went wrong, there is no point in even trying to
12171 process the class-definition. */
12172 type = NULL_TREE;
12173 goto done;
12174 }
a723baf1 12175
a723baf1
MM
12176 /* Look up the type. */
12177 if (template_id_p)
12178 {
12179 type = TREE_TYPE (id);
12180 maybe_process_partial_specialization (type);
12181 }
12182 else if (!nested_name_specifier)
12183 {
12184 /* If the class was unnamed, create a dummy name. */
12185 if (!id)
12186 id = make_anon_name ();
38b305d0 12187 type = xref_tag (class_key, id, /*globalize=*/false,
cbd63935 12188 parser->num_template_parameter_lists);
a723baf1
MM
12189 }
12190 else
12191 {
a723baf1 12192 tree class_type;
91b004e5 12193 bool pop_p = false;
a723baf1
MM
12194
12195 /* Given:
12196
12197 template <typename T> struct S { struct T };
14d22dd6 12198 template <typename T> struct S<T>::T { };
a723baf1
MM
12199
12200 we will get a TYPENAME_TYPE when processing the definition of
12201 `S::T'. We need to resolve it to the actual type before we
12202 try to define it. */
12203 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12204 {
14d22dd6
MM
12205 class_type = resolve_typename_type (TREE_TYPE (type),
12206 /*only_current_p=*/false);
12207 if (class_type != error_mark_node)
12208 type = TYPE_NAME (class_type);
12209 else
12210 {
12211 cp_parser_error (parser, "could not resolve typename type");
12212 type = error_mark_node;
12213 }
a723baf1
MM
12214 }
12215
560ad596
MM
12216 maybe_process_partial_specialization (TREE_TYPE (type));
12217 class_type = current_class_type;
12218 /* Enter the scope indicated by the nested-name-specifier. */
12219 if (nested_name_specifier)
91b004e5 12220 pop_p = push_scope (nested_name_specifier);
560ad596
MM
12221 /* Get the canonical version of this type. */
12222 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12223 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12224 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12225 type = push_template_decl (type);
12226 type = TREE_TYPE (type);
12227 if (nested_name_specifier)
eeb23c11
MM
12228 {
12229 *nested_name_specifier_p = true;
91b004e5
MM
12230 if (pop_p)
12231 pop_scope (nested_name_specifier);
eeb23c11 12232 }
a723baf1
MM
12233 }
12234 /* Indicate whether this class was declared as a `class' or as a
12235 `struct'. */
12236 if (TREE_CODE (type) == RECORD_TYPE)
12237 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12238 cp_parser_check_class_key (class_key, type);
12239
12240 /* Enter the scope containing the class; the names of base classes
12241 should be looked up in that context. For example, given:
12242
12243 struct A { struct B {}; struct C; };
12244 struct A::C : B {};
12245
12246 is valid. */
12247 if (nested_name_specifier)
91b004e5 12248 pop_p = push_scope (nested_name_specifier);
a723baf1
MM
12249 /* Now, look for the base-clause. */
12250 token = cp_lexer_peek_token (parser->lexer);
12251 if (token->type == CPP_COLON)
12252 {
12253 tree bases;
12254
12255 /* Get the list of base-classes. */
12256 bases = cp_parser_base_clause (parser);
12257 /* Process them. */
12258 xref_basetypes (type, bases);
12259 }
12260 /* Leave the scope given by the nested-name-specifier. We will
12261 enter the class scope itself while processing the members. */
91b004e5 12262 if (pop_p)
a723baf1
MM
12263 pop_scope (nested_name_specifier);
12264
afb0918a
MM
12265 done:
12266 if (invalid_explicit_specialization_p)
12267 {
12268 end_specialization ();
12269 --parser->num_template_parameter_lists;
12270 }
38b305d0 12271 *attributes_p = attributes;
a723baf1
MM
12272 return type;
12273}
12274
12275/* Parse a class-key.
12276
12277 class-key:
12278 class
12279 struct
12280 union
12281
12282 Returns the kind of class-key specified, or none_type to indicate
12283 error. */
12284
12285static enum tag_types
94edc4ab 12286cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12287{
12288 cp_token *token;
12289 enum tag_types tag_type;
12290
12291 /* Look for the class-key. */
12292 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12293 if (!token)
12294 return none_type;
12295
12296 /* Check to see if the TOKEN is a class-key. */
12297 tag_type = cp_parser_token_is_class_key (token);
12298 if (!tag_type)
12299 cp_parser_error (parser, "expected class-key");
12300 return tag_type;
12301}
12302
12303/* Parse an (optional) member-specification.
12304
12305 member-specification:
12306 member-declaration member-specification [opt]
12307 access-specifier : member-specification [opt] */
12308
12309static void
94edc4ab 12310cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12311{
12312 while (true)
12313 {
12314 cp_token *token;
12315 enum rid keyword;
12316
12317 /* Peek at the next token. */
12318 token = cp_lexer_peek_token (parser->lexer);
12319 /* If it's a `}', or EOF then we've seen all the members. */
12320 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12321 break;
12322
12323 /* See if this token is a keyword. */
12324 keyword = token->keyword;
12325 switch (keyword)
12326 {
12327 case RID_PUBLIC:
12328 case RID_PROTECTED:
12329 case RID_PRIVATE:
12330 /* Consume the access-specifier. */
12331 cp_lexer_consume_token (parser->lexer);
12332 /* Remember which access-specifier is active. */
12333 current_access_specifier = token->value;
12334 /* Look for the `:'. */
12335 cp_parser_require (parser, CPP_COLON, "`:'");
12336 break;
12337
12338 default:
12339 /* Otherwise, the next construction must be a
12340 member-declaration. */
12341 cp_parser_member_declaration (parser);
a723baf1
MM
12342 }
12343 }
12344}
12345
21526606 12346/* Parse a member-declaration.
a723baf1
MM
12347
12348 member-declaration:
12349 decl-specifier-seq [opt] member-declarator-list [opt] ;
12350 function-definition ; [opt]
12351 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12352 using-declaration
21526606 12353 template-declaration
a723baf1
MM
12354
12355 member-declarator-list:
12356 member-declarator
12357 member-declarator-list , member-declarator
12358
12359 member-declarator:
21526606 12360 declarator pure-specifier [opt]
a723baf1 12361 declarator constant-initializer [opt]
21526606 12362 identifier [opt] : constant-expression
a723baf1
MM
12363
12364 GNU Extensions:
12365
12366 member-declaration:
12367 __extension__ member-declaration
12368
12369 member-declarator:
12370 declarator attributes [opt] pure-specifier [opt]
12371 declarator attributes [opt] constant-initializer [opt]
12372 identifier [opt] attributes [opt] : constant-expression */
12373
12374static void
94edc4ab 12375cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
12376{
12377 tree decl_specifiers;
12378 tree prefix_attributes;
12379 tree decl;
560ad596 12380 int declares_class_or_enum;
a723baf1
MM
12381 bool friend_p;
12382 cp_token *token;
12383 int saved_pedantic;
12384
12385 /* Check for the `__extension__' keyword. */
12386 if (cp_parser_extension_opt (parser, &saved_pedantic))
12387 {
12388 /* Recurse. */
12389 cp_parser_member_declaration (parser);
12390 /* Restore the old value of the PEDANTIC flag. */
12391 pedantic = saved_pedantic;
12392
12393 return;
12394 }
12395
12396 /* Check for a template-declaration. */
12397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12398 {
12399 /* Parse the template-declaration. */
12400 cp_parser_template_declaration (parser, /*member_p=*/true);
12401
12402 return;
12403 }
12404
12405 /* Check for a using-declaration. */
12406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12407 {
12408 /* Parse the using-declaration. */
12409 cp_parser_using_declaration (parser);
12410
12411 return;
12412 }
21526606 12413
a723baf1 12414 /* Parse the decl-specifier-seq. */
21526606 12415 decl_specifiers
a723baf1
MM
12416 = cp_parser_decl_specifier_seq (parser,
12417 CP_PARSER_FLAGS_OPTIONAL,
12418 &prefix_attributes,
12419 &declares_class_or_enum);
8fbc5ae7 12420 /* Check for an invalid type-name. */
2097b5f2 12421 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 12422 return;
a723baf1
MM
12423 /* If there is no declarator, then the decl-specifier-seq should
12424 specify a type. */
12425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12426 {
12427 /* If there was no decl-specifier-seq, and the next token is a
12428 `;', then we have something like:
12429
12430 struct S { ; };
12431
12432 [class.mem]
12433
12434 Each member-declaration shall declare at least one member
12435 name of the class. */
12436 if (!decl_specifiers)
12437 {
12438 if (pedantic)
12439 pedwarn ("extra semicolon");
12440 }
21526606 12441 else
a723baf1
MM
12442 {
12443 tree type;
21526606 12444
a723baf1
MM
12445 /* See if this declaration is a friend. */
12446 friend_p = cp_parser_friend_p (decl_specifiers);
12447 /* If there were decl-specifiers, check to see if there was
12448 a class-declaration. */
12449 type = check_tag_decl (decl_specifiers);
12450 /* Nested classes have already been added to the class, but
12451 a `friend' needs to be explicitly registered. */
12452 if (friend_p)
12453 {
12454 /* If the `friend' keyword was present, the friend must
12455 be introduced with a class-key. */
12456 if (!declares_class_or_enum)
12457 error ("a class-key must be used when declaring a friend");
12458 /* In this case:
12459
21526606
EC
12460 template <typename T> struct A {
12461 friend struct A<T>::B;
a723baf1 12462 };
21526606 12463
a723baf1
MM
12464 A<T>::B will be represented by a TYPENAME_TYPE, and
12465 therefore not recognized by check_tag_decl. */
12466 if (!type)
12467 {
12468 tree specifier;
12469
21526606 12470 for (specifier = decl_specifiers;
a723baf1
MM
12471 specifier;
12472 specifier = TREE_CHAIN (specifier))
12473 {
12474 tree s = TREE_VALUE (specifier);
12475
c003e212
GDR
12476 if (TREE_CODE (s) == IDENTIFIER_NODE)
12477 get_global_value_if_present (s, &type);
a723baf1
MM
12478 if (TREE_CODE (s) == TYPE_DECL)
12479 s = TREE_TYPE (s);
12480 if (TYPE_P (s))
12481 {
12482 type = s;
12483 break;
12484 }
12485 }
12486 }
fdd09134 12487 if (!type || !TYPE_P (type))
a723baf1
MM
12488 error ("friend declaration does not name a class or "
12489 "function");
12490 else
19db77ce
KL
12491 make_friend_class (current_class_type, type,
12492 /*complain=*/true);
a723baf1
MM
12493 }
12494 /* If there is no TYPE, an error message will already have
12495 been issued. */
12496 else if (!type)
12497 ;
12498 /* An anonymous aggregate has to be handled specially; such
12499 a declaration really declares a data member (with a
12500 particular type), as opposed to a nested class. */
12501 else if (ANON_AGGR_TYPE_P (type))
12502 {
12503 /* Remove constructors and such from TYPE, now that we
34cd5ae7 12504 know it is an anonymous aggregate. */
a723baf1
MM
12505 fixup_anonymous_aggr (type);
12506 /* And make the corresponding data member. */
12507 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12508 /* Add it to the class. */
12509 finish_member_declaration (decl);
12510 }
37d407a1
KL
12511 else
12512 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
12513 }
12514 }
12515 else
12516 {
12517 /* See if these declarations will be friends. */
12518 friend_p = cp_parser_friend_p (decl_specifiers);
12519
21526606 12520 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
12521 declaration. */
12522 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12523 {
12524 tree attributes = NULL_TREE;
12525 tree first_attribute;
12526
12527 /* Peek at the next token. */
12528 token = cp_lexer_peek_token (parser->lexer);
12529
12530 /* Check for a bitfield declaration. */
12531 if (token->type == CPP_COLON
12532 || (token->type == CPP_NAME
21526606 12533 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
12534 == CPP_COLON))
12535 {
12536 tree identifier;
12537 tree width;
12538
12539 /* Get the name of the bitfield. Note that we cannot just
12540 check TOKEN here because it may have been invalidated by
12541 the call to cp_lexer_peek_nth_token above. */
12542 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12543 identifier = cp_parser_identifier (parser);
12544 else
12545 identifier = NULL_TREE;
12546
12547 /* Consume the `:' token. */
12548 cp_lexer_consume_token (parser->lexer);
12549 /* Get the width of the bitfield. */
21526606 12550 width
14d22dd6
MM
12551 = cp_parser_constant_expression (parser,
12552 /*allow_non_constant=*/false,
12553 NULL);
a723baf1
MM
12554
12555 /* Look for attributes that apply to the bitfield. */
12556 attributes = cp_parser_attributes_opt (parser);
12557 /* Remember which attributes are prefix attributes and
12558 which are not. */
12559 first_attribute = attributes;
12560 /* Combine the attributes. */
12561 attributes = chainon (prefix_attributes, attributes);
12562
12563 /* Create the bitfield declaration. */
21526606 12564 decl = grokbitfield (identifier,
a723baf1
MM
12565 decl_specifiers,
12566 width);
12567 /* Apply the attributes. */
12568 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12569 }
12570 else
12571 {
12572 tree declarator;
12573 tree initializer;
12574 tree asm_specification;
7efa3e22 12575 int ctor_dtor_or_conv_p;
a723baf1
MM
12576
12577 /* Parse the declarator. */
21526606 12578 declarator
62b8a44e 12579 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
12580 &ctor_dtor_or_conv_p,
12581 /*parenthesized_p=*/NULL);
a723baf1
MM
12582
12583 /* If something went wrong parsing the declarator, make sure
12584 that we at least consume some tokens. */
12585 if (declarator == error_mark_node)
12586 {
12587 /* Skip to the end of the statement. */
12588 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
12589 /* If the next token is not a semicolon, that is
12590 probably because we just skipped over the body of
12591 a function. So, we consume a semicolon if
12592 present, but do not issue an error message if it
12593 is not present. */
12594 if (cp_lexer_next_token_is (parser->lexer,
12595 CPP_SEMICOLON))
12596 cp_lexer_consume_token (parser->lexer);
12597 return;
a723baf1
MM
12598 }
12599
21526606 12600 cp_parser_check_for_definition_in_return_type
560ad596
MM
12601 (declarator, declares_class_or_enum);
12602
a723baf1
MM
12603 /* Look for an asm-specification. */
12604 asm_specification = cp_parser_asm_specification_opt (parser);
12605 /* Look for attributes that apply to the declaration. */
12606 attributes = cp_parser_attributes_opt (parser);
12607 /* Remember which attributes are prefix attributes and
12608 which are not. */
12609 first_attribute = attributes;
12610 /* Combine the attributes. */
12611 attributes = chainon (prefix_attributes, attributes);
12612
12613 /* If it's an `=', then we have a constant-initializer or a
12614 pure-specifier. It is not correct to parse the
12615 initializer before registering the member declaration
12616 since the member declaration should be in scope while
12617 its initializer is processed. However, the rest of the
12618 front end does not yet provide an interface that allows
12619 us to handle this correctly. */
12620 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12621 {
12622 /* In [class.mem]:
12623
12624 A pure-specifier shall be used only in the declaration of
21526606 12625 a virtual function.
a723baf1
MM
12626
12627 A member-declarator can contain a constant-initializer
12628 only if it declares a static member of integral or
21526606 12629 enumeration type.
a723baf1
MM
12630
12631 Therefore, if the DECLARATOR is for a function, we look
12632 for a pure-specifier; otherwise, we look for a
12633 constant-initializer. When we call `grokfield', it will
12634 perform more stringent semantics checks. */
12635 if (TREE_CODE (declarator) == CALL_EXPR)
12636 initializer = cp_parser_pure_specifier (parser);
12637 else
4bb8ca28
MM
12638 /* Parse the initializer. */
12639 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
12640 }
12641 /* Otherwise, there is no initializer. */
12642 else
12643 initializer = NULL_TREE;
12644
12645 /* See if we are probably looking at a function
12646 definition. We are certainly not looking at at a
12647 member-declarator. Calling `grokfield' has
12648 side-effects, so we must not do it unless we are sure
12649 that we are looking at a member-declarator. */
21526606 12650 if (cp_parser_token_starts_function_definition_p
a723baf1 12651 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
12652 {
12653 /* The grammar does not allow a pure-specifier to be
12654 used when a member function is defined. (It is
12655 possible that this fact is an oversight in the
12656 standard, since a pure function may be defined
12657 outside of the class-specifier. */
12658 if (initializer)
12659 error ("pure-specifier on function-definition");
12660 decl = cp_parser_save_member_function_body (parser,
12661 decl_specifiers,
12662 declarator,
12663 attributes);
12664 /* If the member was not a friend, declare it here. */
12665 if (!friend_p)
12666 finish_member_declaration (decl);
12667 /* Peek at the next token. */
12668 token = cp_lexer_peek_token (parser->lexer);
12669 /* If the next token is a semicolon, consume it. */
12670 if (token->type == CPP_SEMICOLON)
12671 cp_lexer_consume_token (parser->lexer);
12672 return;
12673 }
a723baf1 12674 else
39703eb9
MM
12675 {
12676 /* Create the declaration. */
21526606 12677 decl = grokfield (declarator, decl_specifiers,
ee3071ef 12678 initializer, asm_specification,
39703eb9
MM
12679 attributes);
12680 /* Any initialization must have been from a
12681 constant-expression. */
12682 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12683 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12684 }
a723baf1
MM
12685 }
12686
12687 /* Reset PREFIX_ATTRIBUTES. */
12688 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12689 attributes = TREE_CHAIN (attributes);
12690 if (attributes)
12691 TREE_CHAIN (attributes) = NULL_TREE;
12692
12693 /* If there is any qualification still in effect, clear it
12694 now; we will be starting fresh with the next declarator. */
12695 parser->scope = NULL_TREE;
12696 parser->qualifying_scope = NULL_TREE;
12697 parser->object_scope = NULL_TREE;
12698 /* If it's a `,', then there are more declarators. */
12699 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12700 cp_lexer_consume_token (parser->lexer);
12701 /* If the next token isn't a `;', then we have a parse error. */
12702 else if (cp_lexer_next_token_is_not (parser->lexer,
12703 CPP_SEMICOLON))
12704 {
12705 cp_parser_error (parser, "expected `;'");
04c06002 12706 /* Skip tokens until we find a `;'. */
a723baf1
MM
12707 cp_parser_skip_to_end_of_statement (parser);
12708
12709 break;
12710 }
12711
12712 if (decl)
12713 {
12714 /* Add DECL to the list of members. */
12715 if (!friend_p)
12716 finish_member_declaration (decl);
12717
a723baf1 12718 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12719 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12720 }
12721 }
12722 }
12723
4bb8ca28 12724 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
12725}
12726
12727/* Parse a pure-specifier.
12728
12729 pure-specifier:
12730 = 0
12731
12732 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12733 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12734
12735static tree
94edc4ab 12736cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12737{
12738 cp_token *token;
12739
12740 /* Look for the `=' token. */
12741 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12742 return error_mark_node;
12743 /* Look for the `0' token. */
12744 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12745 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12746 to get information from the lexer about how the number was
12747 spelled in order to fix this problem. */
12748 if (!token || !integer_zerop (token->value))
12749 return error_mark_node;
12750
12751 return integer_zero_node;
12752}
12753
12754/* Parse a constant-initializer.
12755
12756 constant-initializer:
12757 = constant-expression
12758
12759 Returns a representation of the constant-expression. */
12760
12761static tree
94edc4ab 12762cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12763{
12764 /* Look for the `=' token. */
12765 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12766 return error_mark_node;
12767
12768 /* It is invalid to write:
12769
12770 struct S { static const int i = { 7 }; };
12771
12772 */
12773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12774 {
12775 cp_parser_error (parser,
12776 "a brace-enclosed initializer is not allowed here");
12777 /* Consume the opening brace. */
12778 cp_lexer_consume_token (parser->lexer);
12779 /* Skip the initializer. */
12780 cp_parser_skip_to_closing_brace (parser);
12781 /* Look for the trailing `}'. */
12782 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 12783
a723baf1
MM
12784 return error_mark_node;
12785 }
12786
21526606 12787 return cp_parser_constant_expression (parser,
14d22dd6
MM
12788 /*allow_non_constant=*/false,
12789 NULL);
a723baf1
MM
12790}
12791
12792/* Derived classes [gram.class.derived] */
12793
12794/* Parse a base-clause.
12795
12796 base-clause:
21526606 12797 : base-specifier-list
a723baf1
MM
12798
12799 base-specifier-list:
12800 base-specifier
12801 base-specifier-list , base-specifier
12802
12803 Returns a TREE_LIST representing the base-classes, in the order in
12804 which they were declared. The representation of each node is as
21526606 12805 described by cp_parser_base_specifier.
a723baf1
MM
12806
12807 In the case that no bases are specified, this function will return
12808 NULL_TREE, not ERROR_MARK_NODE. */
12809
12810static tree
94edc4ab 12811cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12812{
12813 tree bases = NULL_TREE;
12814
12815 /* Look for the `:' that begins the list. */
12816 cp_parser_require (parser, CPP_COLON, "`:'");
12817
12818 /* Scan the base-specifier-list. */
12819 while (true)
12820 {
12821 cp_token *token;
12822 tree base;
12823
12824 /* Look for the base-specifier. */
12825 base = cp_parser_base_specifier (parser);
12826 /* Add BASE to the front of the list. */
12827 if (base != error_mark_node)
12828 {
12829 TREE_CHAIN (base) = bases;
12830 bases = base;
12831 }
12832 /* Peek at the next token. */
12833 token = cp_lexer_peek_token (parser->lexer);
12834 /* If it's not a comma, then the list is complete. */
12835 if (token->type != CPP_COMMA)
12836 break;
12837 /* Consume the `,'. */
12838 cp_lexer_consume_token (parser->lexer);
12839 }
12840
12841 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12842 base class had a qualified name. However, the next name that
12843 appears is certainly not qualified. */
12844 parser->scope = NULL_TREE;
12845 parser->qualifying_scope = NULL_TREE;
12846 parser->object_scope = NULL_TREE;
12847
12848 return nreverse (bases);
12849}
12850
12851/* Parse a base-specifier.
12852
12853 base-specifier:
12854 :: [opt] nested-name-specifier [opt] class-name
12855 virtual access-specifier [opt] :: [opt] nested-name-specifier
12856 [opt] class-name
12857 access-specifier virtual [opt] :: [opt] nested-name-specifier
12858 [opt] class-name
12859
12860 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12861 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12862 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12863 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 12864
a723baf1 12865static tree
94edc4ab 12866cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12867{
12868 cp_token *token;
12869 bool done = false;
12870 bool virtual_p = false;
12871 bool duplicate_virtual_error_issued_p = false;
12872 bool duplicate_access_error_issued_p = false;
bbaab916 12873 bool class_scope_p, template_p;
dbbf88d1 12874 tree access = access_default_node;
a723baf1
MM
12875 tree type;
12876
12877 /* Process the optional `virtual' and `access-specifier'. */
12878 while (!done)
12879 {
12880 /* Peek at the next token. */
12881 token = cp_lexer_peek_token (parser->lexer);
12882 /* Process `virtual'. */
12883 switch (token->keyword)
12884 {
12885 case RID_VIRTUAL:
12886 /* If `virtual' appears more than once, issue an error. */
12887 if (virtual_p && !duplicate_virtual_error_issued_p)
12888 {
12889 cp_parser_error (parser,
12890 "`virtual' specified more than once in base-specified");
12891 duplicate_virtual_error_issued_p = true;
12892 }
12893
12894 virtual_p = true;
12895
12896 /* Consume the `virtual' token. */
12897 cp_lexer_consume_token (parser->lexer);
12898
12899 break;
12900
12901 case RID_PUBLIC:
12902 case RID_PROTECTED:
12903 case RID_PRIVATE:
12904 /* If more than one access specifier appears, issue an
12905 error. */
dbbf88d1
NS
12906 if (access != access_default_node
12907 && !duplicate_access_error_issued_p)
a723baf1
MM
12908 {
12909 cp_parser_error (parser,
12910 "more than one access specifier in base-specified");
12911 duplicate_access_error_issued_p = true;
12912 }
12913
dbbf88d1 12914 access = ridpointers[(int) token->keyword];
a723baf1
MM
12915
12916 /* Consume the access-specifier. */
12917 cp_lexer_consume_token (parser->lexer);
12918
12919 break;
12920
12921 default:
12922 done = true;
12923 break;
12924 }
12925 }
852dcbdd 12926 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 12927 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
12928 as base classes. */
12929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12930 {
12931 if (!processing_template_decl)
12932 error ("keyword `typename' not allowed outside of templates");
12933 else
12934 error ("keyword `typename' not allowed in this context "
12935 "(the base class is implicitly a type)");
12936 cp_lexer_consume_token (parser->lexer);
12937 }
a723baf1 12938
a723baf1
MM
12939 /* Look for the optional `::' operator. */
12940 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12941 /* Look for the nested-name-specifier. The simplest way to
12942 implement:
12943
12944 [temp.res]
12945
12946 The keyword `typename' is not permitted in a base-specifier or
12947 mem-initializer; in these contexts a qualified name that
12948 depends on a template-parameter is implicitly assumed to be a
12949 type name.
12950
12951 is to pretend that we have seen the `typename' keyword at this
21526606 12952 point. */
a723baf1
MM
12953 cp_parser_nested_name_specifier_opt (parser,
12954 /*typename_keyword_p=*/true,
12955 /*check_dependency_p=*/true,
a668c6ad
MM
12956 /*type_p=*/true,
12957 /*is_declaration=*/true);
a723baf1
MM
12958 /* If the base class is given by a qualified name, assume that names
12959 we see are type names or templates, as appropriate. */
12960 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 12961 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 12962
a723baf1 12963 /* Finally, look for the class-name. */
21526606 12964 type = cp_parser_class_name (parser,
a723baf1 12965 class_scope_p,
bbaab916 12966 template_p,
a723baf1 12967 /*type_p=*/true,
a723baf1 12968 /*check_dependency_p=*/true,
a668c6ad
MM
12969 /*class_head_p=*/false,
12970 /*is_declaration=*/true);
a723baf1
MM
12971
12972 if (type == error_mark_node)
12973 return error_mark_node;
12974
dbbf88d1 12975 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12976}
12977
12978/* Exception handling [gram.exception] */
12979
12980/* Parse an (optional) exception-specification.
12981
12982 exception-specification:
12983 throw ( type-id-list [opt] )
12984
12985 Returns a TREE_LIST representing the exception-specification. The
12986 TREE_VALUE of each node is a type. */
12987
12988static tree
94edc4ab 12989cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12990{
12991 cp_token *token;
12992 tree type_id_list;
12993
12994 /* Peek at the next token. */
12995 token = cp_lexer_peek_token (parser->lexer);
12996 /* If it's not `throw', then there's no exception-specification. */
12997 if (!cp_parser_is_keyword (token, RID_THROW))
12998 return NULL_TREE;
12999
13000 /* Consume the `throw'. */
13001 cp_lexer_consume_token (parser->lexer);
13002
13003 /* Look for the `('. */
13004 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13005
13006 /* Peek at the next token. */
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* If it's not a `)', then there is a type-id-list. */
13009 if (token->type != CPP_CLOSE_PAREN)
13010 {
13011 const char *saved_message;
13012
13013 /* Types may not be defined in an exception-specification. */
13014 saved_message = parser->type_definition_forbidden_message;
13015 parser->type_definition_forbidden_message
13016 = "types may not be defined in an exception-specification";
13017 /* Parse the type-id-list. */
13018 type_id_list = cp_parser_type_id_list (parser);
13019 /* Restore the saved message. */
13020 parser->type_definition_forbidden_message = saved_message;
13021 }
13022 else
13023 type_id_list = empty_except_spec;
13024
13025 /* Look for the `)'. */
13026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13027
13028 return type_id_list;
13029}
13030
13031/* Parse an (optional) type-id-list.
13032
13033 type-id-list:
13034 type-id
13035 type-id-list , type-id
13036
13037 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13038 in the order that the types were presented. */
13039
13040static tree
94edc4ab 13041cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13042{
13043 tree types = NULL_TREE;
13044
13045 while (true)
13046 {
13047 cp_token *token;
13048 tree type;
13049
13050 /* Get the next type-id. */
13051 type = cp_parser_type_id (parser);
13052 /* Add it to the list. */
13053 types = add_exception_specifier (types, type, /*complain=*/1);
13054 /* Peek at the next token. */
13055 token = cp_lexer_peek_token (parser->lexer);
13056 /* If it is not a `,', we are done. */
13057 if (token->type != CPP_COMMA)
13058 break;
13059 /* Consume the `,'. */
13060 cp_lexer_consume_token (parser->lexer);
13061 }
13062
13063 return nreverse (types);
13064}
13065
13066/* Parse a try-block.
13067
13068 try-block:
13069 try compound-statement handler-seq */
13070
13071static tree
94edc4ab 13072cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13073{
13074 tree try_block;
13075
13076 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13077 try_block = begin_try_block ();
a5bcc582 13078 cp_parser_compound_statement (parser, false);
a723baf1
MM
13079 finish_try_block (try_block);
13080 cp_parser_handler_seq (parser);
13081 finish_handler_sequence (try_block);
13082
13083 return try_block;
13084}
13085
13086/* Parse a function-try-block.
13087
13088 function-try-block:
13089 try ctor-initializer [opt] function-body handler-seq */
13090
13091static bool
94edc4ab 13092cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13093{
13094 tree try_block;
13095 bool ctor_initializer_p;
13096
13097 /* Look for the `try' keyword. */
13098 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13099 return false;
13100 /* Let the rest of the front-end know where we are. */
13101 try_block = begin_function_try_block ();
13102 /* Parse the function-body. */
21526606 13103 ctor_initializer_p
a723baf1
MM
13104 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13105 /* We're done with the `try' part. */
13106 finish_function_try_block (try_block);
13107 /* Parse the handlers. */
13108 cp_parser_handler_seq (parser);
13109 /* We're done with the handlers. */
13110 finish_function_handler_sequence (try_block);
13111
13112 return ctor_initializer_p;
13113}
13114
13115/* Parse a handler-seq.
13116
13117 handler-seq:
13118 handler handler-seq [opt] */
13119
13120static void
94edc4ab 13121cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13122{
13123 while (true)
13124 {
13125 cp_token *token;
13126
13127 /* Parse the handler. */
13128 cp_parser_handler (parser);
13129 /* Peek at the next token. */
13130 token = cp_lexer_peek_token (parser->lexer);
13131 /* If it's not `catch' then there are no more handlers. */
13132 if (!cp_parser_is_keyword (token, RID_CATCH))
13133 break;
13134 }
13135}
13136
13137/* Parse a handler.
13138
13139 handler:
13140 catch ( exception-declaration ) compound-statement */
13141
13142static void
94edc4ab 13143cp_parser_handler (cp_parser* parser)
a723baf1
MM
13144{
13145 tree handler;
13146 tree declaration;
13147
13148 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13149 handler = begin_handler ();
13150 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13151 declaration = cp_parser_exception_declaration (parser);
13152 finish_handler_parms (declaration, handler);
13153 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 13154 cp_parser_compound_statement (parser, false);
a723baf1
MM
13155 finish_handler (handler);
13156}
13157
13158/* Parse an exception-declaration.
13159
13160 exception-declaration:
13161 type-specifier-seq declarator
13162 type-specifier-seq abstract-declarator
13163 type-specifier-seq
21526606 13164 ...
a723baf1
MM
13165
13166 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13167 ellipsis variant is used. */
13168
13169static tree
94edc4ab 13170cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
13171{
13172 tree type_specifiers;
13173 tree declarator;
13174 const char *saved_message;
13175
13176 /* If it's an ellipsis, it's easy to handle. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13178 {
13179 /* Consume the `...' token. */
13180 cp_lexer_consume_token (parser->lexer);
13181 return NULL_TREE;
13182 }
13183
13184 /* Types may not be defined in exception-declarations. */
13185 saved_message = parser->type_definition_forbidden_message;
13186 parser->type_definition_forbidden_message
13187 = "types may not be defined in exception-declarations";
13188
13189 /* Parse the type-specifier-seq. */
13190 type_specifiers = cp_parser_type_specifier_seq (parser);
13191 /* If it's a `)', then there is no declarator. */
13192 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13193 declarator = NULL_TREE;
13194 else
62b8a44e 13195 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
13196 /*ctor_dtor_or_conv_p=*/NULL,
13197 /*parenthesized_p=*/NULL);
a723baf1
MM
13198
13199 /* Restore the saved message. */
13200 parser->type_definition_forbidden_message = saved_message;
13201
13202 return start_handler_parms (type_specifiers, declarator);
13203}
13204
21526606 13205/* Parse a throw-expression.
a723baf1
MM
13206
13207 throw-expression:
34cd5ae7 13208 throw assignment-expression [opt]
a723baf1
MM
13209
13210 Returns a THROW_EXPR representing the throw-expression. */
13211
13212static tree
94edc4ab 13213cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13214{
13215 tree expression;
89f1a6ec 13216 cp_token* token;
a723baf1
MM
13217
13218 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13219 token = cp_lexer_peek_token (parser->lexer);
13220 /* Figure out whether or not there is an assignment-expression
13221 following the "throw" keyword. */
13222 if (token->type == CPP_COMMA
13223 || token->type == CPP_SEMICOLON
13224 || token->type == CPP_CLOSE_PAREN
13225 || token->type == CPP_CLOSE_SQUARE
13226 || token->type == CPP_CLOSE_BRACE
13227 || token->type == CPP_COLON)
a723baf1 13228 expression = NULL_TREE;
89f1a6ec
MM
13229 else
13230 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13231
13232 return build_throw (expression);
13233}
13234
13235/* GNU Extensions */
13236
13237/* Parse an (optional) asm-specification.
13238
13239 asm-specification:
13240 asm ( string-literal )
13241
13242 If the asm-specification is present, returns a STRING_CST
13243 corresponding to the string-literal. Otherwise, returns
13244 NULL_TREE. */
13245
13246static tree
94edc4ab 13247cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13248{
13249 cp_token *token;
13250 tree asm_specification;
13251
13252 /* Peek at the next token. */
13253 token = cp_lexer_peek_token (parser->lexer);
21526606 13254 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13255 asm-specification. */
13256 if (!cp_parser_is_keyword (token, RID_ASM))
13257 return NULL_TREE;
13258
13259 /* Consume the `asm' token. */
13260 cp_lexer_consume_token (parser->lexer);
13261 /* Look for the `('. */
13262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13263
13264 /* Look for the string-literal. */
13265 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13266 if (token)
13267 asm_specification = token->value;
13268 else
13269 asm_specification = NULL_TREE;
13270
13271 /* Look for the `)'. */
13272 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13273
13274 return asm_specification;
13275}
13276
21526606 13277/* Parse an asm-operand-list.
a723baf1
MM
13278
13279 asm-operand-list:
13280 asm-operand
13281 asm-operand-list , asm-operand
21526606 13282
a723baf1 13283 asm-operand:
21526606 13284 string-literal ( expression )
a723baf1
MM
13285 [ string-literal ] string-literal ( expression )
13286
13287 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13288 each node is the expression. The TREE_PURPOSE is itself a
13289 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13290 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13291 is a STRING_CST for the string literal before the parenthesis. */
13292
13293static tree
94edc4ab 13294cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13295{
13296 tree asm_operands = NULL_TREE;
13297
13298 while (true)
13299 {
13300 tree string_literal;
13301 tree expression;
13302 tree name;
13303 cp_token *token;
21526606
EC
13304
13305 c_lex_string_translate = false;
13306
13307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13308 {
13309 /* Consume the `[' token. */
13310 cp_lexer_consume_token (parser->lexer);
13311 /* Read the operand name. */
13312 name = cp_parser_identifier (parser);
21526606 13313 if (name != error_mark_node)
a723baf1
MM
13314 name = build_string (IDENTIFIER_LENGTH (name),
13315 IDENTIFIER_POINTER (name));
13316 /* Look for the closing `]'. */
13317 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13318 }
13319 else
13320 name = NULL_TREE;
13321 /* Look for the string-literal. */
13322 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13323 string_literal = token ? token->value : error_mark_node;
21526606 13324 c_lex_string_translate = true;
a723baf1
MM
13325 /* Look for the `('. */
13326 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13327 /* Parse the expression. */
13328 expression = cp_parser_expression (parser);
13329 /* Look for the `)'. */
13330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
21526606 13331 c_lex_string_translate = false;
a723baf1
MM
13332 /* Add this operand to the list. */
13333 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13334 expression,
a723baf1 13335 asm_operands);
21526606 13336 /* If the next token is not a `,', there are no more
a723baf1
MM
13337 operands. */
13338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13339 break;
13340 /* Consume the `,'. */
13341 cp_lexer_consume_token (parser->lexer);
13342 }
13343
13344 return nreverse (asm_operands);
13345}
13346
21526606 13347/* Parse an asm-clobber-list.
a723baf1
MM
13348
13349 asm-clobber-list:
13350 string-literal
21526606 13351 asm-clobber-list , string-literal
a723baf1
MM
13352
13353 Returns a TREE_LIST, indicating the clobbers in the order that they
13354 appeared. The TREE_VALUE of each node is a STRING_CST. */
13355
13356static tree
94edc4ab 13357cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13358{
13359 tree clobbers = NULL_TREE;
13360
13361 while (true)
13362 {
13363 cp_token *token;
13364 tree string_literal;
13365
13366 /* Look for the string literal. */
13367 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13368 string_literal = token ? token->value : error_mark_node;
13369 /* Add it to the list. */
13370 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13371 /* If the next token is not a `,', then the list is
a723baf1
MM
13372 complete. */
13373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13374 break;
13375 /* Consume the `,' token. */
13376 cp_lexer_consume_token (parser->lexer);
13377 }
13378
13379 return clobbers;
13380}
13381
13382/* Parse an (optional) series of attributes.
13383
13384 attributes:
13385 attributes attribute
13386
13387 attribute:
21526606 13388 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13389
13390 The return value is as for cp_parser_attribute_list. */
21526606 13391
a723baf1 13392static tree
94edc4ab 13393cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13394{
13395 tree attributes = NULL_TREE;
13396
13397 while (true)
13398 {
13399 cp_token *token;
13400 tree attribute_list;
13401
13402 /* Peek at the next token. */
13403 token = cp_lexer_peek_token (parser->lexer);
13404 /* If it's not `__attribute__', then we're done. */
13405 if (token->keyword != RID_ATTRIBUTE)
13406 break;
13407
13408 /* Consume the `__attribute__' keyword. */
13409 cp_lexer_consume_token (parser->lexer);
13410 /* Look for the two `(' tokens. */
13411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13412 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13413
13414 /* Peek at the next token. */
13415 token = cp_lexer_peek_token (parser->lexer);
13416 if (token->type != CPP_CLOSE_PAREN)
13417 /* Parse the attribute-list. */
13418 attribute_list = cp_parser_attribute_list (parser);
13419 else
13420 /* If the next token is a `)', then there is no attribute
13421 list. */
13422 attribute_list = NULL;
13423
13424 /* Look for the two `)' tokens. */
13425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13426 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13427
13428 /* Add these new attributes to the list. */
13429 attributes = chainon (attributes, attribute_list);
13430 }
13431
13432 return attributes;
13433}
13434
21526606 13435/* Parse an attribute-list.
a723baf1 13436
21526606
EC
13437 attribute-list:
13438 attribute
a723baf1
MM
13439 attribute-list , attribute
13440
13441 attribute:
21526606 13442 identifier
a723baf1
MM
13443 identifier ( identifier )
13444 identifier ( identifier , expression-list )
21526606 13445 identifier ( expression-list )
a723baf1
MM
13446
13447 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13448 TREE_PURPOSE of each node is the identifier indicating which
13449 attribute is in use. The TREE_VALUE represents the arguments, if
13450 any. */
13451
13452static tree
94edc4ab 13453cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13454{
13455 tree attribute_list = NULL_TREE;
13456
21526606 13457 c_lex_string_translate = false;
a723baf1
MM
13458 while (true)
13459 {
13460 cp_token *token;
13461 tree identifier;
13462 tree attribute;
13463
13464 /* Look for the identifier. We also allow keywords here; for
13465 example `__attribute__ ((const))' is legal. */
13466 token = cp_lexer_peek_token (parser->lexer);
21526606 13467 if (token->type != CPP_NAME
a723baf1
MM
13468 && token->type != CPP_KEYWORD)
13469 return error_mark_node;
13470 /* Consume the token. */
13471 token = cp_lexer_consume_token (parser->lexer);
21526606 13472
a723baf1
MM
13473 /* Save away the identifier that indicates which attribute this is. */
13474 identifier = token->value;
13475 attribute = build_tree_list (identifier, NULL_TREE);
13476
13477 /* Peek at the next token. */
13478 token = cp_lexer_peek_token (parser->lexer);
13479 /* If it's an `(', then parse the attribute arguments. */
13480 if (token->type == CPP_OPEN_PAREN)
13481 {
13482 tree arguments;
a723baf1 13483
21526606 13484 arguments = (cp_parser_parenthesized_expression_list
39703eb9 13485 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13486 /* Save the identifier and arguments away. */
13487 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13488 }
13489
13490 /* Add this attribute to the list. */
13491 TREE_CHAIN (attribute) = attribute_list;
13492 attribute_list = attribute;
13493
13494 /* Now, look for more attributes. */
13495 token = cp_lexer_peek_token (parser->lexer);
13496 /* If the next token isn't a `,', we're done. */
13497 if (token->type != CPP_COMMA)
13498 break;
13499
cd0be382 13500 /* Consume the comma and keep going. */
a723baf1
MM
13501 cp_lexer_consume_token (parser->lexer);
13502 }
21526606 13503 c_lex_string_translate = true;
a723baf1
MM
13504
13505 /* We built up the list in reverse order. */
13506 return nreverse (attribute_list);
13507}
13508
13509/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13510 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13511 current value of the PEDANTIC flag, regardless of whether or not
13512 the `__extension__' keyword is present. The caller is responsible
13513 for restoring the value of the PEDANTIC flag. */
13514
13515static bool
94edc4ab 13516cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13517{
13518 /* Save the old value of the PEDANTIC flag. */
13519 *saved_pedantic = pedantic;
13520
13521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13522 {
13523 /* Consume the `__extension__' token. */
13524 cp_lexer_consume_token (parser->lexer);
13525 /* We're not being pedantic while the `__extension__' keyword is
13526 in effect. */
13527 pedantic = 0;
13528
13529 return true;
13530 }
13531
13532 return false;
13533}
13534
13535/* Parse a label declaration.
13536
13537 label-declaration:
13538 __label__ label-declarator-seq ;
13539
13540 label-declarator-seq:
13541 identifier , label-declarator-seq
13542 identifier */
13543
13544static void
94edc4ab 13545cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13546{
13547 /* Look for the `__label__' keyword. */
13548 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13549
13550 while (true)
13551 {
13552 tree identifier;
13553
13554 /* Look for an identifier. */
13555 identifier = cp_parser_identifier (parser);
13556 /* Declare it as a lobel. */
13557 finish_label_decl (identifier);
13558 /* If the next token is a `;', stop. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13560 break;
13561 /* Look for the `,' separating the label declarations. */
13562 cp_parser_require (parser, CPP_COMMA, "`,'");
13563 }
13564
13565 /* Look for the final `;'. */
13566 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13567}
13568
13569/* Support Functions */
13570
13571/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13572 NAME should have one of the representations used for an
13573 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13574 is returned. If PARSER->SCOPE is a dependent type, then a
13575 SCOPE_REF is returned.
13576
13577 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13578 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13579 was formed. Abstractly, such entities should not be passed to this
13580 function, because they do not need to be looked up, but it is
13581 simpler to check for this special case here, rather than at the
13582 call-sites.
13583
13584 In cases not explicitly covered above, this function returns a
13585 DECL, OVERLOAD, or baselink representing the result of the lookup.
13586 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13587 is returned.
13588
a723baf1
MM
13589 If IS_TYPE is TRUE, bindings that do not refer to types are
13590 ignored.
13591
b0bc6e8e
KL
13592 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13593 ignored.
13594
eea9800f
MM
13595 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13596 are ignored.
13597
a723baf1
MM
13598 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13599 types. */
13600
13601static tree
21526606 13602cp_parser_lookup_name (cp_parser *parser, tree name,
b0bc6e8e
KL
13603 bool is_type, bool is_template, bool is_namespace,
13604 bool check_dependency)
a723baf1
MM
13605{
13606 tree decl;
13607 tree object_type = parser->context->object_type;
13608
13609 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13610 no longer valid. Note that if we are parsing tentatively, and
13611 the parse fails, OBJECT_TYPE will be automatically restored. */
13612 parser->context->object_type = NULL_TREE;
13613
13614 if (name == error_mark_node)
13615 return error_mark_node;
13616
13617 /* A template-id has already been resolved; there is no lookup to
13618 do. */
13619 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13620 return name;
13621 if (BASELINK_P (name))
13622 {
13623 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13624 == TEMPLATE_ID_EXPR),
13625 20020909);
13626 return name;
13627 }
13628
13629 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13630 it should already have been checked to make sure that the name
13631 used matches the type being destroyed. */
13632 if (TREE_CODE (name) == BIT_NOT_EXPR)
13633 {
13634 tree type;
13635
13636 /* Figure out to which type this destructor applies. */
13637 if (parser->scope)
13638 type = parser->scope;
13639 else if (object_type)
13640 type = object_type;
13641 else
13642 type = current_class_type;
13643 /* If that's not a class type, there is no destructor. */
13644 if (!type || !CLASS_TYPE_P (type))
13645 return error_mark_node;
fd6e3cce
GB
13646 if (!CLASSTYPE_DESTRUCTORS (type))
13647 return error_mark_node;
a723baf1
MM
13648 /* If it was a class type, return the destructor. */
13649 return CLASSTYPE_DESTRUCTORS (type);
13650 }
13651
13652 /* By this point, the NAME should be an ordinary identifier. If
13653 the id-expression was a qualified name, the qualifying scope is
13654 stored in PARSER->SCOPE at this point. */
13655 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13656 20000619);
21526606 13657
a723baf1
MM
13658 /* Perform the lookup. */
13659 if (parser->scope)
21526606 13660 {
1fb3244a 13661 bool dependent_p;
a723baf1
MM
13662
13663 if (parser->scope == error_mark_node)
13664 return error_mark_node;
13665
13666 /* If the SCOPE is dependent, the lookup must be deferred until
13667 the template is instantiated -- unless we are explicitly
13668 looking up names in uninstantiated templates. Even then, we
13669 cannot look up the name if the scope is not a class type; it
13670 might, for example, be a template type parameter. */
1fb3244a
MM
13671 dependent_p = (TYPE_P (parser->scope)
13672 && !(parser->in_declarator_p
13673 && currently_open_class (parser->scope))
13674 && dependent_type_p (parser->scope));
a723baf1 13675 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13676 && dependent_p)
a723baf1 13677 {
b0bc6e8e 13678 if (is_type)
a723baf1
MM
13679 /* The resolution to Core Issue 180 says that `struct A::B'
13680 should be considered a type-name, even if `A' is
13681 dependent. */
13682 decl = TYPE_NAME (make_typename_type (parser->scope,
13683 name,
13684 /*complain=*/1));
b0bc6e8e 13685 else if (is_template)
5b4acce1
KL
13686 decl = make_unbound_class_template (parser->scope,
13687 name,
13688 /*complain=*/1);
b0bc6e8e
KL
13689 else
13690 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
13691 }
13692 else
13693 {
91b004e5
MM
13694 bool pop_p = false;
13695
a723baf1
MM
13696 /* If PARSER->SCOPE is a dependent type, then it must be a
13697 class type, and we must not be checking dependencies;
13698 otherwise, we would have processed this lookup above. So
13699 that PARSER->SCOPE is not considered a dependent base by
13700 lookup_member, we must enter the scope here. */
1fb3244a 13701 if (dependent_p)
91b004e5 13702 pop_p = push_scope (parser->scope);
a723baf1
MM
13703 /* If the PARSER->SCOPE is a a template specialization, it
13704 may be instantiated during name lookup. In that case,
13705 errors may be issued. Even if we rollback the current
13706 tentative parse, those errors are valid. */
5e08432e
MM
13707 decl = lookup_qualified_name (parser->scope, name, is_type,
13708 /*complain=*/true);
91b004e5 13709 if (pop_p)
a723baf1
MM
13710 pop_scope (parser->scope);
13711 }
13712 parser->qualifying_scope = parser->scope;
13713 parser->object_scope = NULL_TREE;
13714 }
13715 else if (object_type)
13716 {
13717 tree object_decl = NULL_TREE;
13718 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13719 OBJECT_TYPE is not a class. */
13720 if (CLASS_TYPE_P (object_type))
13721 /* If the OBJECT_TYPE is a template specialization, it may
13722 be instantiated during name lookup. In that case, errors
13723 may be issued. Even if we rollback the current tentative
13724 parse, those errors are valid. */
13725 object_decl = lookup_member (object_type,
13726 name,
13727 /*protect=*/0, is_type);
13728 /* Look it up in the enclosing context, too. */
21526606 13729 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13730 is_namespace,
a723baf1
MM
13731 /*flags=*/0);
13732 parser->object_scope = object_type;
13733 parser->qualifying_scope = NULL_TREE;
13734 if (object_decl)
13735 decl = object_decl;
13736 }
13737 else
13738 {
21526606 13739 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13740 is_namespace,
a723baf1
MM
13741 /*flags=*/0);
13742 parser->qualifying_scope = NULL_TREE;
13743 parser->object_scope = NULL_TREE;
13744 }
13745
13746 /* If the lookup failed, let our caller know. */
21526606 13747 if (!decl
a723baf1 13748 || decl == error_mark_node
21526606 13749 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
13750 && DECL_ANTICIPATED (decl)))
13751 return error_mark_node;
13752
13753 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13754 if (TREE_CODE (decl) == TREE_LIST)
13755 {
13756 /* The error message we have to print is too complicated for
13757 cp_parser_error, so we incorporate its actions directly. */
e5976695 13758 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13759 {
13760 error ("reference to `%D' is ambiguous", name);
13761 print_candidates (decl);
13762 }
13763 return error_mark_node;
13764 }
13765
21526606 13766 my_friendly_assert (DECL_P (decl)
a723baf1
MM
13767 || TREE_CODE (decl) == OVERLOAD
13768 || TREE_CODE (decl) == SCOPE_REF
5b4acce1 13769 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
a723baf1
MM
13770 || BASELINK_P (decl),
13771 20000619);
13772
13773 /* If we have resolved the name of a member declaration, check to
13774 see if the declaration is accessible. When the name resolves to
34cd5ae7 13775 set of overloaded functions, accessibility is checked when
21526606 13776 overload resolution is done.
a723baf1
MM
13777
13778 During an explicit instantiation, access is not checked at all,
13779 as per [temp.explicit]. */
8d241e0b 13780 if (DECL_P (decl))
ee76b931 13781 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13782
13783 return decl;
13784}
13785
13786/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
13787 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13788 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
13789
13790static tree
94edc4ab 13791cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 13792{
21526606 13793 return cp_parser_lookup_name (parser, name,
eea9800f 13794 /*is_type=*/false,
b0bc6e8e 13795 /*is_template=*/false,
eea9800f 13796 /*is_namespace=*/false,
a723baf1
MM
13797 /*check_dependency=*/true);
13798}
13799
a723baf1
MM
13800/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13801 the current context, return the TYPE_DECL. If TAG_NAME_P is
13802 true, the DECL indicates the class being defined in a class-head,
13803 or declared in an elaborated-type-specifier.
13804
13805 Otherwise, return DECL. */
13806
13807static tree
13808cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13809{
710b73e6
KL
13810 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13811 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 13812
21526606 13813 struct A {
a723baf1
MM
13814 template <typename T> struct B;
13815 };
13816
21526606
EC
13817 template <typename T> struct A::B {};
13818
a723baf1
MM
13819 Similarly, in a elaborated-type-specifier:
13820
13821 namespace N { struct X{}; }
13822
13823 struct A {
13824 template <typename T> friend struct N::X;
13825 };
13826
710b73e6
KL
13827 However, if the DECL refers to a class type, and we are in
13828 the scope of the class, then the name lookup automatically
13829 finds the TYPE_DECL created by build_self_reference rather
13830 than a TEMPLATE_DECL. For example, in:
13831
13832 template <class T> struct S {
13833 S s;
13834 };
13835
13836 there is no need to handle such case. */
13837
13838 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13839 return DECL_TEMPLATE_RESULT (decl);
13840
13841 return decl;
13842}
13843
13844/* If too many, or too few, template-parameter lists apply to the
13845 declarator, issue an error message. Returns TRUE if all went well,
13846 and FALSE otherwise. */
13847
13848static bool
21526606 13849cp_parser_check_declarator_template_parameters (cp_parser* parser,
94edc4ab 13850 tree declarator)
a723baf1
MM
13851{
13852 unsigned num_templates;
13853
13854 /* We haven't seen any classes that involve template parameters yet. */
13855 num_templates = 0;
13856
13857 switch (TREE_CODE (declarator))
13858 {
13859 case CALL_EXPR:
13860 case ARRAY_REF:
13861 case INDIRECT_REF:
13862 case ADDR_EXPR:
13863 {
13864 tree main_declarator = TREE_OPERAND (declarator, 0);
13865 return
21526606 13866 cp_parser_check_declarator_template_parameters (parser,
a723baf1
MM
13867 main_declarator);
13868 }
13869
13870 case SCOPE_REF:
13871 {
13872 tree scope;
13873 tree member;
13874
13875 scope = TREE_OPERAND (declarator, 0);
13876 member = TREE_OPERAND (declarator, 1);
13877
13878 /* If this is a pointer-to-member, then we are not interested
13879 in the SCOPE, because it does not qualify the thing that is
13880 being declared. */
13881 if (TREE_CODE (member) == INDIRECT_REF)
13882 return (cp_parser_check_declarator_template_parameters
13883 (parser, member));
13884
13885 while (scope && CLASS_TYPE_P (scope))
13886 {
13887 /* You're supposed to have one `template <...>'
13888 for every template class, but you don't need one
13889 for a full specialization. For example:
21526606 13890
a723baf1
MM
13891 template <class T> struct S{};
13892 template <> struct S<int> { void f(); };
13893 void S<int>::f () {}
21526606 13894
a723baf1
MM
13895 is correct; there shouldn't be a `template <>' for
13896 the definition of `S<int>::f'. */
13897 if (CLASSTYPE_TEMPLATE_INFO (scope)
13898 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13899 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13900 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13901 ++num_templates;
13902
13903 scope = TYPE_CONTEXT (scope);
13904 }
13905 }
13906
13907 /* Fall through. */
13908
13909 default:
13910 /* If the DECLARATOR has the form `X<y>' then it uses one
13911 additional level of template parameters. */
13912 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13913 ++num_templates;
13914
21526606 13915 return cp_parser_check_template_parameters (parser,
a723baf1
MM
13916 num_templates);
13917 }
13918}
13919
13920/* NUM_TEMPLATES were used in the current declaration. If that is
13921 invalid, return FALSE and issue an error messages. Otherwise,
13922 return TRUE. */
13923
13924static bool
94edc4ab
NN
13925cp_parser_check_template_parameters (cp_parser* parser,
13926 unsigned num_templates)
a723baf1
MM
13927{
13928 /* If there are more template classes than parameter lists, we have
13929 something like:
21526606 13930
a723baf1
MM
13931 template <class T> void S<T>::R<T>::f (); */
13932 if (parser->num_template_parameter_lists < num_templates)
13933 {
13934 error ("too few template-parameter-lists");
13935 return false;
13936 }
13937 /* If there are the same number of template classes and parameter
13938 lists, that's OK. */
13939 if (parser->num_template_parameter_lists == num_templates)
13940 return true;
13941 /* If there are more, but only one more, then we are referring to a
13942 member template. That's OK too. */
13943 if (parser->num_template_parameter_lists == num_templates + 1)
13944 return true;
13945 /* Otherwise, there are too many template parameter lists. We have
13946 something like:
13947
13948 template <class T> template <class U> void S::f(); */
13949 error ("too many template-parameter-lists");
13950 return false;
13951}
13952
13953/* Parse a binary-expression of the general form:
13954
13955 binary-expression:
13956 <expr>
13957 binary-expression <token> <expr>
13958
13959 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13960 to parser the <expr>s. If the first production is used, then the
13961 value returned by FN is returned directly. Otherwise, a node with
13962 the indicated EXPR_TYPE is returned, with operands corresponding to
13963 the two sub-expressions. */
13964
13965static tree
21526606
EC
13966cp_parser_binary_expression (cp_parser* parser,
13967 const cp_parser_token_tree_map token_tree_map,
94edc4ab 13968 cp_parser_expression_fn fn)
a723baf1
MM
13969{
13970 tree lhs;
13971
13972 /* Parse the first expression. */
13973 lhs = (*fn) (parser);
13974 /* Now, look for more expressions. */
13975 while (true)
13976 {
13977 cp_token *token;
39b1af70 13978 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13979 tree rhs;
13980
13981 /* Peek at the next token. */
13982 token = cp_lexer_peek_token (parser->lexer);
13983 /* If the token is `>', and that's not an operator at the
13984 moment, then we're done. */
13985 if (token->type == CPP_GREATER
13986 && !parser->greater_than_is_operator_p)
13987 break;
34cd5ae7 13988 /* If we find one of the tokens we want, build the corresponding
a723baf1 13989 tree representation. */
21526606 13990 for (map_node = token_tree_map;
a723baf1
MM
13991 map_node->token_type != CPP_EOF;
13992 ++map_node)
13993 if (map_node->token_type == token->type)
13994 {
ec835fb2
MM
13995 /* Assume that an overloaded operator will not be used. */
13996 bool overloaded_p = false;
13997
a723baf1
MM
13998 /* Consume the operator token. */
13999 cp_lexer_consume_token (parser->lexer);
14000 /* Parse the right-hand side of the expression. */
14001 rhs = (*fn) (parser);
14002 /* Build the binary tree node. */
ec835fb2
MM
14003 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14004 &overloaded_p);
14005 /* If the binary operator required the use of an
14006 overloaded operator, then this expression cannot be an
14007 integral constant-expression. An overloaded operator
14008 can be used even if both operands are otherwise
14009 permissible in an integral constant-expression if at
14010 least one of the operands is of enumeration type. */
14011 if (overloaded_p
14012 && (cp_parser_non_integral_constant_expression
14013 (parser, "calls to overloaded operators")))
14014 lhs = error_mark_node;
a723baf1
MM
14015 break;
14016 }
14017
14018 /* If the token wasn't one of the ones we want, we're done. */
14019 if (map_node->token_type == CPP_EOF)
14020 break;
14021 }
14022
14023 return lhs;
14024}
14025
14026/* Parse an optional `::' token indicating that the following name is
14027 from the global namespace. If so, PARSER->SCOPE is set to the
14028 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14029 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14030 Returns the new value of PARSER->SCOPE, if the `::' token is
14031 present, and NULL_TREE otherwise. */
14032
14033static tree
94edc4ab 14034cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14035{
14036 cp_token *token;
14037
14038 /* Peek at the next token. */
14039 token = cp_lexer_peek_token (parser->lexer);
14040 /* If we're looking at a `::' token then we're starting from the
14041 global namespace, not our current location. */
14042 if (token->type == CPP_SCOPE)
14043 {
14044 /* Consume the `::' token. */
14045 cp_lexer_consume_token (parser->lexer);
14046 /* Set the SCOPE so that we know where to start the lookup. */
14047 parser->scope = global_namespace;
14048 parser->qualifying_scope = global_namespace;
14049 parser->object_scope = NULL_TREE;
14050
14051 return parser->scope;
14052 }
14053 else if (!current_scope_valid_p)
14054 {
14055 parser->scope = NULL_TREE;
14056 parser->qualifying_scope = NULL_TREE;
14057 parser->object_scope = NULL_TREE;
14058 }
14059
14060 return NULL_TREE;
14061}
14062
14063/* Returns TRUE if the upcoming token sequence is the start of a
14064 constructor declarator. If FRIEND_P is true, the declarator is
14065 preceded by the `friend' specifier. */
14066
14067static bool
14068cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14069{
14070 bool constructor_p;
14071 tree type_decl = NULL_TREE;
14072 bool nested_name_p;
2050a1bb
MM
14073 cp_token *next_token;
14074
14075 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14076 try to avoid doing lots of work if at all possible. It's not
14077 valid declare a constructor at function scope. */
14078 if (at_function_scope_p ())
14079 return false;
14080 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14081 next_token = cp_lexer_peek_token (parser->lexer);
14082 if (next_token->type != CPP_NAME
14083 && next_token->type != CPP_SCOPE
14084 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14085 && next_token->type != CPP_TEMPLATE_ID)
14086 return false;
a723baf1
MM
14087
14088 /* Parse tentatively; we are going to roll back all of the tokens
14089 consumed here. */
14090 cp_parser_parse_tentatively (parser);
14091 /* Assume that we are looking at a constructor declarator. */
14092 constructor_p = true;
8d241e0b 14093
a723baf1
MM
14094 /* Look for the optional `::' operator. */
14095 cp_parser_global_scope_opt (parser,
14096 /*current_scope_valid_p=*/false);
14097 /* Look for the nested-name-specifier. */
21526606 14098 nested_name_p
a723baf1
MM
14099 = (cp_parser_nested_name_specifier_opt (parser,
14100 /*typename_keyword_p=*/false,
14101 /*check_dependency_p=*/false,
a668c6ad
MM
14102 /*type_p=*/false,
14103 /*is_declaration=*/false)
a723baf1
MM
14104 != NULL_TREE);
14105 /* Outside of a class-specifier, there must be a
14106 nested-name-specifier. */
21526606 14107 if (!nested_name_p &&
a723baf1
MM
14108 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14109 || friend_p))
14110 constructor_p = false;
14111 /* If we still think that this might be a constructor-declarator,
14112 look for a class-name. */
14113 if (constructor_p)
14114 {
14115 /* If we have:
14116
8fbc5ae7 14117 template <typename T> struct S { S(); };
a723baf1
MM
14118 template <typename T> S<T>::S ();
14119
14120 we must recognize that the nested `S' names a class.
14121 Similarly, for:
14122
14123 template <typename T> S<T>::S<T> ();
14124
14125 we must recognize that the nested `S' names a template. */
14126 type_decl = cp_parser_class_name (parser,
14127 /*typename_keyword_p=*/false,
14128 /*template_keyword_p=*/false,
14129 /*type_p=*/false,
a723baf1 14130 /*check_dependency_p=*/false,
a668c6ad
MM
14131 /*class_head_p=*/false,
14132 /*is_declaration=*/false);
a723baf1
MM
14133 /* If there was no class-name, then this is not a constructor. */
14134 constructor_p = !cp_parser_error_occurred (parser);
14135 }
8d241e0b 14136
a723baf1
MM
14137 /* If we're still considering a constructor, we have to see a `(',
14138 to begin the parameter-declaration-clause, followed by either a
14139 `)', an `...', or a decl-specifier. We need to check for a
14140 type-specifier to avoid being fooled into thinking that:
14141
14142 S::S (f) (int);
14143
14144 is a constructor. (It is actually a function named `f' that
14145 takes one parameter (of type `int') and returns a value of type
14146 `S::S'. */
21526606 14147 if (constructor_p
a723baf1
MM
14148 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14149 {
14150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14151 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14152 && !cp_parser_storage_class_specifier_opt (parser))
14153 {
5dae1114 14154 tree type;
91b004e5 14155 bool pop_p = false;
4047b164 14156 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14157
14158 /* Names appearing in the type-specifier should be looked up
14159 in the scope of the class. */
14160 if (current_class_type)
14161 type = NULL_TREE;
a723baf1
MM
14162 else
14163 {
5dae1114
MM
14164 type = TREE_TYPE (type_decl);
14165 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14166 {
21526606 14167 type = resolve_typename_type (type,
14d22dd6
MM
14168 /*only_current_p=*/false);
14169 if (type == error_mark_node)
14170 {
14171 cp_parser_abort_tentative_parse (parser);
14172 return false;
14173 }
14174 }
91b004e5 14175 pop_p = push_scope (type);
a723baf1 14176 }
4047b164
KL
14177
14178 /* Inside the constructor parameter list, surrounding
14179 template-parameter-lists do not apply. */
14180 saved_num_template_parameter_lists
14181 = parser->num_template_parameter_lists;
14182 parser->num_template_parameter_lists = 0;
14183
5dae1114
MM
14184 /* Look for the type-specifier. */
14185 cp_parser_type_specifier (parser,
14186 CP_PARSER_FLAGS_NONE,
14187 /*is_friend=*/false,
14188 /*is_declarator=*/true,
14189 /*declares_class_or_enum=*/NULL,
14190 /*is_cv_qualifier=*/NULL);
4047b164
KL
14191
14192 parser->num_template_parameter_lists
14193 = saved_num_template_parameter_lists;
14194
5dae1114 14195 /* Leave the scope of the class. */
91b004e5 14196 if (pop_p)
5dae1114
MM
14197 pop_scope (type);
14198
14199 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14200 }
14201 }
14202 else
14203 constructor_p = false;
14204 /* We did not really want to consume any tokens. */
14205 cp_parser_abort_tentative_parse (parser);
14206
14207 return constructor_p;
14208}
14209
14210/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14211 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14212 they must be performed once we are in the scope of the function.
14213
14214 Returns the function defined. */
14215
14216static tree
14217cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
14218 (cp_parser* parser,
14219 tree decl_specifiers,
14220 tree attributes,
14221 tree declarator)
a723baf1
MM
14222{
14223 tree fn;
14224 bool success_p;
14225
14226 /* Begin the function-definition. */
21526606
EC
14227 success_p = begin_function_definition (decl_specifiers,
14228 attributes,
a723baf1
MM
14229 declarator);
14230
14231 /* If there were names looked up in the decl-specifier-seq that we
14232 did not check, check them now. We must wait until we are in the
14233 scope of the function to perform the checks, since the function
14234 might be a friend. */
cf22909c 14235 perform_deferred_access_checks ();
a723baf1
MM
14236
14237 if (!success_p)
14238 {
14239 /* If begin_function_definition didn't like the definition, skip
14240 the entire function. */
14241 error ("invalid function declaration");
14242 cp_parser_skip_to_end_of_block_or_statement (parser);
14243 fn = error_mark_node;
14244 }
14245 else
14246 fn = cp_parser_function_definition_after_declarator (parser,
14247 /*inline_p=*/false);
14248
14249 return fn;
14250}
14251
14252/* Parse the part of a function-definition that follows the
14253 declarator. INLINE_P is TRUE iff this function is an inline
14254 function defined with a class-specifier.
14255
14256 Returns the function defined. */
14257
21526606
EC
14258static tree
14259cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14260 bool inline_p)
a723baf1
MM
14261{
14262 tree fn;
14263 bool ctor_initializer_p = false;
14264 bool saved_in_unbraced_linkage_specification_p;
14265 unsigned saved_num_template_parameter_lists;
14266
14267 /* If the next token is `return', then the code may be trying to
14268 make use of the "named return value" extension that G++ used to
14269 support. */
14270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14271 {
14272 /* Consume the `return' keyword. */
14273 cp_lexer_consume_token (parser->lexer);
14274 /* Look for the identifier that indicates what value is to be
14275 returned. */
14276 cp_parser_identifier (parser);
14277 /* Issue an error message. */
14278 error ("named return values are no longer supported");
14279 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14280 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14281 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14282 cp_lexer_consume_token (parser->lexer);
14283 }
14284 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14285 anything declared inside `f'. */
21526606 14286 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14287 = parser->in_unbraced_linkage_specification_p;
14288 parser->in_unbraced_linkage_specification_p = false;
14289 /* Inside the function, surrounding template-parameter-lists do not
14290 apply. */
21526606
EC
14291 saved_num_template_parameter_lists
14292 = parser->num_template_parameter_lists;
a723baf1
MM
14293 parser->num_template_parameter_lists = 0;
14294 /* If the next token is `try', then we are looking at a
14295 function-try-block. */
14296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14297 ctor_initializer_p = cp_parser_function_try_block (parser);
14298 /* A function-try-block includes the function-body, so we only do
14299 this next part if we're not processing a function-try-block. */
14300 else
21526606 14301 ctor_initializer_p
a723baf1
MM
14302 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14303
14304 /* Finish the function. */
21526606 14305 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14306 (inline_p ? 2 : 0));
14307 /* Generate code for it, if necessary. */
8cd2462c 14308 expand_or_defer_fn (fn);
a723baf1 14309 /* Restore the saved values. */
21526606 14310 parser->in_unbraced_linkage_specification_p
a723baf1 14311 = saved_in_unbraced_linkage_specification_p;
21526606 14312 parser->num_template_parameter_lists
a723baf1
MM
14313 = saved_num_template_parameter_lists;
14314
14315 return fn;
14316}
14317
14318/* Parse a template-declaration, assuming that the `export' (and
14319 `extern') keywords, if present, has already been scanned. MEMBER_P
14320 is as for cp_parser_template_declaration. */
14321
14322static void
94edc4ab 14323cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14324{
14325 tree decl = NULL_TREE;
14326 tree parameter_list;
14327 bool friend_p = false;
14328
14329 /* Look for the `template' keyword. */
14330 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14331 return;
21526606 14332
a723baf1
MM
14333 /* And the `<'. */
14334 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14335 return;
21526606 14336
a723baf1
MM
14337 /* If the next token is `>', then we have an invalid
14338 specialization. Rather than complain about an invalid template
14339 parameter, issue an error message here. */
14340 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14341 {
14342 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14343 begin_specialization ();
a723baf1
MM
14344 parameter_list = NULL_TREE;
14345 }
14346 else
2f9afd51
KL
14347 {
14348 /* Parse the template parameters. */
14349 begin_template_parm_list ();
14350 parameter_list = cp_parser_template_parameter_list (parser);
14351 parameter_list = end_template_parm_list (parameter_list);
14352 }
14353
a723baf1
MM
14354 /* Look for the `>'. */
14355 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14356 /* We just processed one more parameter list. */
14357 ++parser->num_template_parameter_lists;
14358 /* If the next token is `template', there are more template
14359 parameters. */
21526606 14360 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14361 RID_TEMPLATE))
14362 cp_parser_template_declaration_after_export (parser, member_p);
14363 else
14364 {
14365 decl = cp_parser_single_declaration (parser,
14366 member_p,
14367 &friend_p);
14368
14369 /* If this is a member template declaration, let the front
14370 end know. */
14371 if (member_p && !friend_p && decl)
37d407a1
KL
14372 {
14373 if (TREE_CODE (decl) == TYPE_DECL)
14374 cp_parser_check_access_in_redeclaration (decl);
14375
14376 decl = finish_member_template_decl (decl);
14377 }
a723baf1 14378 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14379 make_friend_class (current_class_type, TREE_TYPE (decl),
14380 /*complain=*/true);
a723baf1
MM
14381 }
14382 /* We are done with the current parameter list. */
14383 --parser->num_template_parameter_lists;
14384
14385 /* Finish up. */
14386 finish_template_decl (parameter_list);
14387
14388 /* Register member declarations. */
14389 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14390 finish_member_declaration (decl);
14391
14392 /* If DECL is a function template, we must return to parse it later.
14393 (Even though there is no definition, there might be default
14394 arguments that need handling.) */
21526606 14395 if (member_p && decl
a723baf1
MM
14396 && (TREE_CODE (decl) == FUNCTION_DECL
14397 || DECL_FUNCTION_TEMPLATE_P (decl)))
14398 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14399 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14400 TREE_VALUE (parser->unparsed_functions_queues));
14401}
14402
14403/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14404 `function-definition' sequence. MEMBER_P is true, this declaration
14405 appears in a class scope.
14406
14407 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14408 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14409
14410static tree
21526606 14411cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14412 bool member_p,
14413 bool* friend_p)
a723baf1 14414{
560ad596 14415 int declares_class_or_enum;
a723baf1
MM
14416 tree decl = NULL_TREE;
14417 tree decl_specifiers;
14418 tree attributes;
4bb8ca28 14419 bool function_definition_p = false;
a723baf1 14420
a723baf1 14421 /* Defer access checks until we know what is being declared. */
8d241e0b 14422 push_deferring_access_checks (dk_deferred);
cf22909c 14423
a723baf1
MM
14424 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14425 alternative. */
21526606 14426 decl_specifiers
a723baf1
MM
14427 = cp_parser_decl_specifier_seq (parser,
14428 CP_PARSER_FLAGS_OPTIONAL,
14429 &attributes,
14430 &declares_class_or_enum);
4bb8ca28
MM
14431 if (friend_p)
14432 *friend_p = cp_parser_friend_p (decl_specifiers);
a723baf1
MM
14433 /* Gather up the access checks that occurred the
14434 decl-specifier-seq. */
cf22909c
KL
14435 stop_deferring_access_checks ();
14436
a723baf1
MM
14437 /* Check for the declaration of a template class. */
14438 if (declares_class_or_enum)
14439 {
14440 if (cp_parser_declares_only_class_p (parser))
14441 {
14442 decl = shadow_tag (decl_specifiers);
14443 if (decl)
14444 decl = TYPE_NAME (decl);
14445 else
14446 decl = error_mark_node;
14447 }
14448 }
14449 else
14450 decl = NULL_TREE;
14451 /* If it's not a template class, try for a template function. If
14452 the next token is a `;', then this declaration does not declare
14453 anything. But, if there were errors in the decl-specifiers, then
14454 the error might well have come from an attempted class-specifier.
14455 In that case, there's no need to warn about a missing declarator. */
14456 if (!decl
14457 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14458 || !value_member (error_mark_node, decl_specifiers)))
21526606 14459 decl = cp_parser_init_declarator (parser,
a723baf1
MM
14460 decl_specifiers,
14461 attributes,
4bb8ca28 14462 /*function_definition_allowed_p=*/true,
a723baf1 14463 member_p,
560ad596 14464 declares_class_or_enum,
4bb8ca28 14465 &function_definition_p);
cf22909c
KL
14466
14467 pop_deferring_access_checks ();
14468
a723baf1
MM
14469 /* Clear any current qualification; whatever comes next is the start
14470 of something new. */
14471 parser->scope = NULL_TREE;
14472 parser->qualifying_scope = NULL_TREE;
14473 parser->object_scope = NULL_TREE;
14474 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14475 if (!function_definition_p
14476 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14477 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14478
14479 return decl;
14480}
14481
d6b4ea85
MM
14482/* Parse a cast-expression that is not the operand of a unary "&". */
14483
14484static tree
14485cp_parser_simple_cast_expression (cp_parser *parser)
14486{
14487 return cp_parser_cast_expression (parser, /*address_p=*/false);
14488}
14489
a723baf1
MM
14490/* Parse a functional cast to TYPE. Returns an expression
14491 representing the cast. */
14492
14493static tree
94edc4ab 14494cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14495{
14496 tree expression_list;
d36d5600 14497 tree cast;
a723baf1 14498
21526606 14499 expression_list
39703eb9
MM
14500 = cp_parser_parenthesized_expression_list (parser, false,
14501 /*non_constant_p=*/NULL);
a723baf1 14502
d36d5600
GB
14503 cast = build_functional_cast (type, expression_list);
14504 /* [expr.const]/1: In an integral constant expression "only type
14505 conversions to integral or enumeration type can be used". */
14506 if (cast != error_mark_node && !type_dependent_expression_p (type)
14507 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14508 {
14509 if (cp_parser_non_integral_constant_expression
14510 (parser, "a call to a constructor"))
14511 return error_mark_node;
14512 }
14513 return cast;
a723baf1
MM
14514}
14515
4bb8ca28
MM
14516/* Save the tokens that make up the body of a member function defined
14517 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14518 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14519 specifiers applied to the declaration. Returns the FUNCTION_DECL
14520 for the member function. */
14521
7ce27103 14522static tree
4bb8ca28
MM
14523cp_parser_save_member_function_body (cp_parser* parser,
14524 tree decl_specifiers,
14525 tree declarator,
14526 tree attributes)
14527{
14528 cp_token_cache *cache;
14529 tree fn;
14530
14531 /* Create the function-declaration. */
14532 fn = start_method (decl_specifiers, declarator, attributes);
14533 /* If something went badly wrong, bail out now. */
14534 if (fn == error_mark_node)
14535 {
14536 /* If there's a function-body, skip it. */
21526606 14537 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
14538 (cp_lexer_peek_token (parser->lexer)))
14539 cp_parser_skip_to_end_of_block_or_statement (parser);
14540 return error_mark_node;
14541 }
14542
14543 /* Remember it, if there default args to post process. */
14544 cp_parser_save_default_args (parser, fn);
14545
14546 /* Create a token cache. */
14547 cache = cp_token_cache_new ();
21526606 14548 /* Save away the tokens that make up the body of the
4bb8ca28
MM
14549 function. */
14550 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14551 /* Handle function try blocks. */
14552 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14553 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14554
14555 /* Save away the inline definition; we will process it when the
14556 class is complete. */
14557 DECL_PENDING_INLINE_INFO (fn) = cache;
14558 DECL_PENDING_INLINE_P (fn) = 1;
14559
14560 /* We need to know that this was defined in the class, so that
14561 friend templates are handled correctly. */
14562 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14563
14564 /* We're done with the inline definition. */
14565 finish_method (fn);
14566
14567 /* Add FN to the queue of functions to be parsed later. */
14568 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14569 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
14570 TREE_VALUE (parser->unparsed_functions_queues));
14571
14572 return fn;
14573}
14574
ec75414f
MM
14575/* Parse a template-argument-list, as well as the trailing ">" (but
14576 not the opening ">"). See cp_parser_template_argument_list for the
14577 return value. */
14578
14579static tree
14580cp_parser_enclosed_template_argument_list (cp_parser* parser)
14581{
14582 tree arguments;
14583 tree saved_scope;
14584 tree saved_qualifying_scope;
14585 tree saved_object_scope;
14586 bool saved_greater_than_is_operator_p;
14587
14588 /* [temp.names]
14589
14590 When parsing a template-id, the first non-nested `>' is taken as
14591 the end of the template-argument-list rather than a greater-than
14592 operator. */
21526606 14593 saved_greater_than_is_operator_p
ec75414f
MM
14594 = parser->greater_than_is_operator_p;
14595 parser->greater_than_is_operator_p = false;
14596 /* Parsing the argument list may modify SCOPE, so we save it
14597 here. */
14598 saved_scope = parser->scope;
14599 saved_qualifying_scope = parser->qualifying_scope;
14600 saved_object_scope = parser->object_scope;
14601 /* Parse the template-argument-list itself. */
14602 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14603 arguments = NULL_TREE;
14604 else
14605 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
14606 /* Look for the `>' that ends the template-argument-list. If we find
14607 a '>>' instead, it's probably just a typo. */
14608 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14609 {
14610 if (!saved_greater_than_is_operator_p)
14611 {
14612 /* If we're in a nested template argument list, the '>>' has to be
14613 a typo for '> >'. We emit the error message, but we continue
14614 parsing and we push a '>' as next token, so that the argument
14615 list will be parsed correctly.. */
14616 cp_token* token;
14617 error ("`>>' should be `> >' within a nested template argument list");
14618 token = cp_lexer_peek_token (parser->lexer);
14619 token->type = CPP_GREATER;
14620 }
14621 else
14622 {
14623 /* If this is not a nested template argument list, the '>>' is
14624 a typo for '>'. Emit an error message and continue. */
14625 error ("spurious `>>', use `>' to terminate a template argument list");
14626 cp_lexer_consume_token (parser->lexer);
14627 }
14628 }
6c0cc713
GB
14629 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14630 error ("missing `>' to terminate the template argument list");
ec75414f 14631 /* The `>' token might be a greater-than operator again now. */
21526606 14632 parser->greater_than_is_operator_p
ec75414f
MM
14633 = saved_greater_than_is_operator_p;
14634 /* Restore the SAVED_SCOPE. */
14635 parser->scope = saved_scope;
14636 parser->qualifying_scope = saved_qualifying_scope;
14637 parser->object_scope = saved_object_scope;
14638
14639 return arguments;
14640}
14641
a723baf1
MM
14642/* MEMBER_FUNCTION is a member function, or a friend. If default
14643 arguments, or the body of the function have not yet been parsed,
14644 parse them now. */
14645
14646static void
94edc4ab 14647cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14648{
14649 cp_lexer *saved_lexer;
14650
14651 /* If this member is a template, get the underlying
14652 FUNCTION_DECL. */
14653 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14654 member_function = DECL_TEMPLATE_RESULT (member_function);
14655
14656 /* There should not be any class definitions in progress at this
14657 point; the bodies of members are only parsed outside of all class
14658 definitions. */
14659 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14660 /* While we're parsing the member functions we might encounter more
14661 classes. We want to handle them right away, but we don't want
14662 them getting mixed up with functions that are currently in the
14663 queue. */
14664 parser->unparsed_functions_queues
14665 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14666
14667 /* Make sure that any template parameters are in scope. */
14668 maybe_begin_member_template_processing (member_function);
14669
a723baf1
MM
14670 /* If the body of the function has not yet been parsed, parse it
14671 now. */
14672 if (DECL_PENDING_INLINE_P (member_function))
14673 {
14674 tree function_scope;
14675 cp_token_cache *tokens;
14676
14677 /* The function is no longer pending; we are processing it. */
14678 tokens = DECL_PENDING_INLINE_INFO (member_function);
14679 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14680 DECL_PENDING_INLINE_P (member_function) = 0;
14681 /* If this was an inline function in a local class, enter the scope
14682 of the containing function. */
14683 function_scope = decl_function_context (member_function);
14684 if (function_scope)
14685 push_function_context_to (function_scope);
21526606 14686
a723baf1
MM
14687 /* Save away the current lexer. */
14688 saved_lexer = parser->lexer;
14689 /* Make a new lexer to feed us the tokens saved for this function. */
14690 parser->lexer = cp_lexer_new_from_tokens (tokens);
14691 parser->lexer->next = saved_lexer;
21526606 14692
a723baf1
MM
14693 /* Set the current source position to be the location of the first
14694 token in the saved inline body. */
3466b292 14695 cp_lexer_peek_token (parser->lexer);
21526606 14696
a723baf1
MM
14697 /* Let the front end know that we going to be defining this
14698 function. */
14699 start_function (NULL_TREE, member_function, NULL_TREE,
14700 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 14701
a723baf1
MM
14702 /* Now, parse the body of the function. */
14703 cp_parser_function_definition_after_declarator (parser,
14704 /*inline_p=*/true);
21526606 14705
a723baf1
MM
14706 /* Leave the scope of the containing function. */
14707 if (function_scope)
14708 pop_function_context_from (function_scope);
14709 /* Restore the lexer. */
14710 parser->lexer = saved_lexer;
14711 }
14712
14713 /* Remove any template parameters from the symbol table. */
14714 maybe_end_member_template_processing ();
14715
14716 /* Restore the queue. */
21526606 14717 parser->unparsed_functions_queues
a723baf1
MM
14718 = TREE_CHAIN (parser->unparsed_functions_queues);
14719}
14720
cd0be382 14721/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
14722 functions queue. */
14723
14724static void
14725cp_parser_save_default_args (cp_parser* parser, tree decl)
14726{
14727 tree probe;
14728
14729 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14730 probe;
14731 probe = TREE_CHAIN (probe))
14732 if (TREE_PURPOSE (probe))
14733 {
14734 TREE_PURPOSE (parser->unparsed_functions_queues)
21526606 14735 = tree_cons (NULL_TREE, decl,
8db1028e
NS
14736 TREE_PURPOSE (parser->unparsed_functions_queues));
14737 break;
14738 }
14739 return;
14740}
14741
8218bd34
MM
14742/* FN is a FUNCTION_DECL which may contains a parameter with an
14743 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14744
14745static void
8218bd34 14746cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14747{
14748 cp_lexer *saved_lexer;
14749 cp_token_cache *tokens;
14750 bool saved_local_variables_forbidden_p;
14751 tree parameters;
8218bd34 14752
b92bc2a0
NS
14753 /* While we're parsing the default args, we might (due to the
14754 statement expression extension) encounter more classes. We want
14755 to handle them right away, but we don't want them getting mixed
14756 up with default args that are currently in the queue. */
14757 parser->unparsed_functions_queues
14758 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14759
8218bd34 14760 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14761 parameters;
14762 parameters = TREE_CHAIN (parameters))
14763 {
14764 if (!TREE_PURPOSE (parameters)
14765 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14766 continue;
21526606 14767
a723baf1
MM
14768 /* Save away the current lexer. */
14769 saved_lexer = parser->lexer;
14770 /* Create a new one, using the tokens we have saved. */
14771 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14772 parser->lexer = cp_lexer_new_from_tokens (tokens);
14773
14774 /* Set the current source position to be the location of the
14775 first token in the default argument. */
3466b292 14776 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14777
14778 /* Local variable names (and the `this' keyword) may not appear
14779 in a default argument. */
14780 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14781 parser->local_variables_forbidden_p = true;
14782 /* Parse the assignment-expression. */
f128e1f3 14783 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 14784 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14785 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 14786 if (DECL_CLASS_SCOPE_P (fn))
e5976695 14787 pop_nested_class ();
a723baf1 14788
676e33ca
MM
14789 /* If the token stream has not been completely used up, then
14790 there was extra junk after the end of the default
14791 argument. */
14792 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14793 cp_parser_error (parser, "expected `,'");
14794
a723baf1
MM
14795 /* Restore saved state. */
14796 parser->lexer = saved_lexer;
14797 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14798 }
b92bc2a0
NS
14799
14800 /* Restore the queue. */
21526606 14801 parser->unparsed_functions_queues
b92bc2a0 14802 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
14803}
14804
14805/* Parse the operand of `sizeof' (or a similar operator). Returns
14806 either a TYPE or an expression, depending on the form of the
14807 input. The KEYWORD indicates which kind of expression we have
14808 encountered. */
14809
14810static tree
94edc4ab 14811cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14812{
14813 static const char *format;
14814 tree expr = NULL_TREE;
14815 const char *saved_message;
67c03833 14816 bool saved_integral_constant_expression_p;
a723baf1
MM
14817
14818 /* Initialize FORMAT the first time we get here. */
14819 if (!format)
14820 format = "types may not be defined in `%s' expressions";
14821
14822 /* Types cannot be defined in a `sizeof' expression. Save away the
14823 old message. */
14824 saved_message = parser->type_definition_forbidden_message;
14825 /* And create the new one. */
21526606
EC
14826 parser->type_definition_forbidden_message
14827 = xmalloc (strlen (format)
c68b0a84
KG
14828 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14829 + 1 /* `\0' */);
a723baf1
MM
14830 sprintf ((char *) parser->type_definition_forbidden_message,
14831 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14832
14833 /* The restrictions on constant-expressions do not apply inside
14834 sizeof expressions. */
67c03833
JM
14835 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14836 parser->integral_constant_expression_p = false;
a723baf1 14837
3beb3abf
MM
14838 /* Do not actually evaluate the expression. */
14839 ++skip_evaluation;
a723baf1
MM
14840 /* If it's a `(', then we might be looking at the type-id
14841 construction. */
14842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14843 {
14844 tree type;
4f8163b1 14845 bool saved_in_type_id_in_expr_p;
a723baf1
MM
14846
14847 /* We can't be sure yet whether we're looking at a type-id or an
14848 expression. */
14849 cp_parser_parse_tentatively (parser);
14850 /* Consume the `('. */
14851 cp_lexer_consume_token (parser->lexer);
14852 /* Parse the type-id. */
4f8163b1
MM
14853 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14854 parser->in_type_id_in_expr_p = true;
a723baf1 14855 type = cp_parser_type_id (parser);
4f8163b1 14856 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
14857 /* Now, look for the trailing `)'. */
14858 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14859 /* If all went well, then we're done. */
14860 if (cp_parser_parse_definitely (parser))
14861 {
14862 /* Build a list of decl-specifiers; right now, we have only
14863 a single type-specifier. */
14864 type = build_tree_list (NULL_TREE,
14865 type);
14866
14867 /* Call grokdeclarator to figure out what type this is. */
14868 expr = grokdeclarator (NULL_TREE,
14869 type,
14870 TYPENAME,
14871 /*initialized=*/0,
14872 /*attrlist=*/NULL);
14873 }
14874 }
14875
14876 /* If the type-id production did not work out, then we must be
14877 looking at the unary-expression production. */
14878 if (!expr)
14879 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14880 /* Go back to evaluating expressions. */
14881 --skip_evaluation;
a723baf1
MM
14882
14883 /* Free the message we created. */
14884 free ((char *) parser->type_definition_forbidden_message);
14885 /* And restore the old one. */
14886 parser->type_definition_forbidden_message = saved_message;
67c03833 14887 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
14888
14889 return expr;
14890}
14891
14892/* If the current declaration has no declarator, return true. */
14893
14894static bool
14895cp_parser_declares_only_class_p (cp_parser *parser)
14896{
21526606 14897 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
14898 declarator. */
14899 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14900 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14901}
14902
14903/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14904 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14905
14906static bool
94edc4ab 14907cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14908{
14909 while (decl_specifiers)
14910 {
14911 /* See if this decl-specifier is `friend'. */
14912 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14913 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14914 return true;
14915
14916 /* Go on to the next decl-specifier. */
14917 decl_specifiers = TREE_CHAIN (decl_specifiers);
14918 }
14919
14920 return false;
14921}
14922
14923/* If the next token is of the indicated TYPE, consume it. Otherwise,
14924 issue an error message indicating that TOKEN_DESC was expected.
21526606 14925
a723baf1
MM
14926 Returns the token consumed, if the token had the appropriate type.
14927 Otherwise, returns NULL. */
14928
14929static cp_token *
94edc4ab
NN
14930cp_parser_require (cp_parser* parser,
14931 enum cpp_ttype type,
14932 const char* token_desc)
a723baf1
MM
14933{
14934 if (cp_lexer_next_token_is (parser->lexer, type))
14935 return cp_lexer_consume_token (parser->lexer);
14936 else
14937 {
e5976695
MM
14938 /* Output the MESSAGE -- unless we're parsing tentatively. */
14939 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
14940 {
14941 char *message = concat ("expected ", token_desc, NULL);
14942 cp_parser_error (parser, message);
14943 free (message);
14944 }
a723baf1
MM
14945 return NULL;
14946 }
14947}
14948
14949/* Like cp_parser_require, except that tokens will be skipped until
14950 the desired token is found. An error message is still produced if
14951 the next token is not as expected. */
14952
14953static void
21526606
EC
14954cp_parser_skip_until_found (cp_parser* parser,
14955 enum cpp_ttype type,
94edc4ab 14956 const char* token_desc)
a723baf1
MM
14957{
14958 cp_token *token;
14959 unsigned nesting_depth = 0;
14960
14961 if (cp_parser_require (parser, type, token_desc))
14962 return;
14963
14964 /* Skip tokens until the desired token is found. */
14965 while (true)
14966 {
14967 /* Peek at the next token. */
14968 token = cp_lexer_peek_token (parser->lexer);
21526606 14969 /* If we've reached the token we want, consume it and
a723baf1
MM
14970 stop. */
14971 if (token->type == type && !nesting_depth)
14972 {
14973 cp_lexer_consume_token (parser->lexer);
14974 return;
14975 }
14976 /* If we've run out of tokens, stop. */
14977 if (token->type == CPP_EOF)
14978 return;
21526606 14979 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
14980 || token->type == CPP_OPEN_PAREN
14981 || token->type == CPP_OPEN_SQUARE)
14982 ++nesting_depth;
21526606 14983 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
14984 || token->type == CPP_CLOSE_PAREN
14985 || token->type == CPP_CLOSE_SQUARE)
14986 {
14987 if (nesting_depth-- == 0)
14988 return;
14989 }
14990 /* Consume this token. */
14991 cp_lexer_consume_token (parser->lexer);
14992 }
14993}
14994
14995/* If the next token is the indicated keyword, consume it. Otherwise,
14996 issue an error message indicating that TOKEN_DESC was expected.
21526606 14997
a723baf1
MM
14998 Returns the token consumed, if the token had the appropriate type.
14999 Otherwise, returns NULL. */
15000
15001static cp_token *
94edc4ab
NN
15002cp_parser_require_keyword (cp_parser* parser,
15003 enum rid keyword,
15004 const char* token_desc)
a723baf1
MM
15005{
15006 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15007
15008 if (token && token->keyword != keyword)
15009 {
15010 dyn_string_t error_msg;
15011
15012 /* Format the error message. */
15013 error_msg = dyn_string_new (0);
15014 dyn_string_append_cstr (error_msg, "expected ");
15015 dyn_string_append_cstr (error_msg, token_desc);
15016 cp_parser_error (parser, error_msg->s);
15017 dyn_string_delete (error_msg);
15018 return NULL;
15019 }
15020
15021 return token;
15022}
15023
15024/* Returns TRUE iff TOKEN is a token that can begin the body of a
15025 function-definition. */
15026
21526606 15027static bool
94edc4ab 15028cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15029{
15030 return (/* An ordinary function-body begins with an `{'. */
15031 token->type == CPP_OPEN_BRACE
15032 /* A ctor-initializer begins with a `:'. */
15033 || token->type == CPP_COLON
15034 /* A function-try-block begins with `try'. */
15035 || token->keyword == RID_TRY
15036 /* The named return value extension begins with `return'. */
15037 || token->keyword == RID_RETURN);
15038}
15039
15040/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15041 definition. */
15042
15043static bool
15044cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15045{
15046 cp_token *token;
15047
15048 token = cp_lexer_peek_token (parser->lexer);
15049 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15050}
15051
d17811fd 15052/* Returns TRUE iff the next token is the "," or ">" ending a
4d5297fa
GB
15053 template-argument. ">>" is also accepted (after the full
15054 argument was parsed) because it's probably a typo for "> >",
15055 and there is a specific diagnostic for this. */
d17811fd
MM
15056
15057static bool
15058cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15059{
15060 cp_token *token;
15061
15062 token = cp_lexer_peek_token (parser->lexer);
21526606 15063 return (token->type == CPP_COMMA || token->type == CPP_GREATER
4d5297fa 15064 || token->type == CPP_RSHIFT);
d17811fd 15065}
f4abade9
GB
15066
15067/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15068 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15069
15070static bool
21526606 15071cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15072 size_t n)
15073{
15074 cp_token *token;
15075
15076 token = cp_lexer_peek_nth_token (parser->lexer, n);
15077 if (token->type == CPP_LESS)
15078 return true;
15079 /* Check for the sequence `<::' in the original code. It would be lexed as
15080 `[:', where `[' is a digraph, and there is no whitespace before
15081 `:'. */
15082 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15083 {
15084 cp_token *token2;
15085 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15086 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15087 return true;
15088 }
15089 return false;
15090}
21526606 15091
a723baf1
MM
15092/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15093 or none_type otherwise. */
15094
15095static enum tag_types
94edc4ab 15096cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15097{
15098 switch (token->keyword)
15099 {
15100 case RID_CLASS:
15101 return class_type;
15102 case RID_STRUCT:
15103 return record_type;
15104 case RID_UNION:
15105 return union_type;
21526606 15106
a723baf1
MM
15107 default:
15108 return none_type;
15109 }
15110}
15111
15112/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15113
15114static void
15115cp_parser_check_class_key (enum tag_types class_key, tree type)
15116{
15117 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15118 pedwarn ("`%s' tag used in naming `%#T'",
15119 class_key == union_type ? "union"
21526606 15120 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15121 type);
15122}
21526606 15123
cd0be382 15124/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15125 access than its original declaration [class.access.spec/3].
15126 This applies to nested classes and nested class templates.
15127 [class.mem/1]. */
15128
15129static void cp_parser_check_access_in_redeclaration (tree decl)
15130{
15131 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15132 return;
15133
15134 if ((TREE_PRIVATE (decl)
15135 != (current_access_specifier == access_private_node))
15136 || (TREE_PROTECTED (decl)
15137 != (current_access_specifier == access_protected_node)))
15138 error ("%D redeclared with different access", decl);
15139}
15140
a723baf1 15141/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15142 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15143 consumed. */
15144
15145static bool
15146cp_parser_optional_template_keyword (cp_parser *parser)
15147{
15148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15149 {
15150 /* The `template' keyword can only be used within templates;
15151 outside templates the parser can always figure out what is a
15152 template and what is not. */
15153 if (!processing_template_decl)
15154 {
15155 error ("`template' (as a disambiguator) is only allowed "
15156 "within templates");
15157 /* If this part of the token stream is rescanned, the same
15158 error message would be generated. So, we purge the token
15159 from the stream. */
15160 cp_lexer_purge_token (parser->lexer);
15161 return false;
15162 }
15163 else
15164 {
15165 /* Consume the `template' keyword. */
15166 cp_lexer_consume_token (parser->lexer);
15167 return true;
15168 }
15169 }
15170
15171 return false;
15172}
15173
2050a1bb
MM
15174/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15175 set PARSER->SCOPE, and perform other related actions. */
15176
15177static void
15178cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15179{
15180 tree value;
15181 tree check;
15182
15183 /* Get the stored value. */
15184 value = cp_lexer_consume_token (parser->lexer)->value;
15185 /* Perform any access checks that were deferred. */
15186 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15187 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15188 /* Set the scope from the stored value. */
15189 parser->scope = TREE_VALUE (value);
15190 parser->qualifying_scope = TREE_TYPE (value);
15191 parser->object_scope = NULL_TREE;
15192}
15193
852dcbdd 15194/* Add tokens to CACHE until a non-nested END token appears. */
a723baf1
MM
15195
15196static void
21526606 15197cp_parser_cache_group (cp_parser *parser,
a723baf1
MM
15198 cp_token_cache *cache,
15199 enum cpp_ttype end,
15200 unsigned depth)
15201{
15202 while (true)
15203 {
15204 cp_token *token;
15205
15206 /* Abort a parenthesized expression if we encounter a brace. */
15207 if ((end == CPP_CLOSE_PAREN || depth == 0)
15208 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15209 return;
a723baf1 15210 /* If we've reached the end of the file, stop. */
4bfb8bba 15211 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15212 return;
4bfb8bba
MM
15213 /* Consume the next token. */
15214 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15215 /* Add this token to the tokens we are saving. */
15216 cp_token_cache_push_token (cache, token);
15217 /* See if it starts a new group. */
15218 if (token->type == CPP_OPEN_BRACE)
15219 {
15220 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15221 if (depth == 0)
15222 return;
15223 }
15224 else if (token->type == CPP_OPEN_PAREN)
15225 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15226 else if (token->type == end)
15227 return;
15228 }
15229}
15230
15231/* Begin parsing tentatively. We always save tokens while parsing
15232 tentatively so that if the tentative parsing fails we can restore the
15233 tokens. */
15234
15235static void
94edc4ab 15236cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15237{
15238 /* Enter a new parsing context. */
15239 parser->context = cp_parser_context_new (parser->context);
15240 /* Begin saving tokens. */
15241 cp_lexer_save_tokens (parser->lexer);
15242 /* In order to avoid repetitive access control error messages,
15243 access checks are queued up until we are no longer parsing
15244 tentatively. */
8d241e0b 15245 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15246}
15247
15248/* Commit to the currently active tentative parse. */
15249
15250static void
94edc4ab 15251cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15252{
15253 cp_parser_context *context;
15254 cp_lexer *lexer;
15255
15256 /* Mark all of the levels as committed. */
15257 lexer = parser->lexer;
15258 for (context = parser->context; context->next; context = context->next)
15259 {
15260 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15261 break;
15262 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15263 while (!cp_lexer_saving_tokens (lexer))
15264 lexer = lexer->next;
15265 cp_lexer_commit_tokens (lexer);
15266 }
15267}
15268
15269/* Abort the currently active tentative parse. All consumed tokens
15270 will be rolled back, and no diagnostics will be issued. */
15271
15272static void
94edc4ab 15273cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15274{
15275 cp_parser_simulate_error (parser);
15276 /* Now, pretend that we want to see if the construct was
15277 successfully parsed. */
15278 cp_parser_parse_definitely (parser);
15279}
15280
34cd5ae7 15281/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15282 token stream. Otherwise, commit to the tokens we have consumed.
15283 Returns true if no error occurred; false otherwise. */
15284
15285static bool
94edc4ab 15286cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15287{
15288 bool error_occurred;
15289 cp_parser_context *context;
15290
34cd5ae7 15291 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15292 destroy that information. */
15293 error_occurred = cp_parser_error_occurred (parser);
15294 /* Remove the topmost context from the stack. */
15295 context = parser->context;
15296 parser->context = context->next;
15297 /* If no parse errors occurred, commit to the tentative parse. */
15298 if (!error_occurred)
15299 {
15300 /* Commit to the tokens read tentatively, unless that was
15301 already done. */
15302 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15303 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15304
15305 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15306 }
15307 /* Otherwise, if errors occurred, roll back our state so that things
15308 are just as they were before we began the tentative parse. */
15309 else
cf22909c
KL
15310 {
15311 cp_lexer_rollback_tokens (parser->lexer);
15312 pop_deferring_access_checks ();
15313 }
e5976695
MM
15314 /* Add the context to the front of the free list. */
15315 context->next = cp_parser_context_free_list;
15316 cp_parser_context_free_list = context;
15317
15318 return !error_occurred;
a723baf1
MM
15319}
15320
a723baf1
MM
15321/* Returns true if we are parsing tentatively -- but have decided that
15322 we will stick with this tentative parse, even if errors occur. */
15323
15324static bool
94edc4ab 15325cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15326{
15327 return (cp_parser_parsing_tentatively (parser)
15328 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15329}
15330
4de8668e 15331/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15332 tentative parse. */
21526606 15333
a723baf1 15334static bool
94edc4ab 15335cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15336{
15337 return (cp_parser_parsing_tentatively (parser)
15338 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15339}
15340
4de8668e 15341/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15342
15343static bool
94edc4ab 15344cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15345{
15346 return parser->allow_gnu_extensions_p;
15347}
15348
15349\f
a723baf1
MM
15350/* The parser. */
15351
15352static GTY (()) cp_parser *the_parser;
15353
15354/* External interface. */
15355
d1bd0ded 15356/* Parse one entire translation unit. */
a723baf1 15357
d1bd0ded
GK
15358void
15359c_parse_file (void)
a723baf1
MM
15360{
15361 bool error_occurred;
f75fbaf7
ZW
15362 static bool already_called = false;
15363
15364 if (already_called)
15365 {
15366 sorry ("inter-module optimizations not implemented for C++");
15367 return;
15368 }
15369 already_called = true;
a723baf1
MM
15370
15371 the_parser = cp_parser_new ();
78757caa
KL
15372 push_deferring_access_checks (flag_access_control
15373 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15374 error_occurred = cp_parser_translation_unit (the_parser);
15375 the_parser = NULL;
a723baf1
MM
15376}
15377
a723baf1
MM
15378/* This variable must be provided by every front end. */
15379
15380int yydebug;
15381
15382#include "gt-cp-parser.h"