]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
* mklibgcc.in: Remove obsolete MAYBE_USE_COLLECT2.
[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);
3632 /* Create a TYPENAME_TYPE to represent the type to which the
3633 functional cast is being performed. */
21526606 3634 type = make_typename_type (parser->scope, id,
a723baf1
MM
3635 /*complain=*/1);
3636
3637 postfix_expression = cp_parser_functional_cast (parser, type);
3638 }
3639 break;
3640
3641 default:
3642 {
3643 tree type;
3644
3645 /* If the next thing is a simple-type-specifier, we may be
3646 looking at a functional cast. We could also be looking at
3647 an id-expression. So, we try the functional cast, and if
3648 that doesn't work we fall back to the primary-expression. */
3649 cp_parser_parse_tentatively (parser);
3650 /* Look for the simple-type-specifier. */
21526606 3651 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3652 CP_PARSER_FLAGS_NONE,
3653 /*identifier_p=*/false);
a723baf1
MM
3654 /* Parse the cast itself. */
3655 if (!cp_parser_error_occurred (parser))
21526606 3656 postfix_expression
a723baf1
MM
3657 = cp_parser_functional_cast (parser, type);
3658 /* If that worked, we're done. */
3659 if (cp_parser_parse_definitely (parser))
3660 break;
3661
3662 /* If the functional-cast didn't work out, try a
3663 compound-literal. */
14d22dd6
MM
3664 if (cp_parser_allow_gnu_extensions_p (parser)
3665 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3666 {
3667 tree initializer_list = NULL_TREE;
4f8163b1 3668 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3669
3670 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3671 /* Consume the `('. */
3672 cp_lexer_consume_token (parser->lexer);
3673 /* Parse the type. */
4f8163b1
MM
3674 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3675 parser->in_type_id_in_expr_p = true;
14d22dd6 3676 type = cp_parser_type_id (parser);
4f8163b1 3677 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3678 /* Look for the `)'. */
3679 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3680 /* Look for the `{'. */
3681 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3682 /* If things aren't going well, there's no need to
3683 keep going. */
3684 if (!cp_parser_error_occurred (parser))
a723baf1 3685 {
39703eb9 3686 bool non_constant_p;
14d22dd6 3687 /* Parse the initializer-list. */
21526606 3688 initializer_list
39703eb9 3689 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3690 /* Allow a trailing `,'. */
3691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3692 cp_lexer_consume_token (parser->lexer);
3693 /* Look for the final `}'. */
3694 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3695 }
3696 /* If that worked, we're definitely looking at a
3697 compound-literal expression. */
3698 if (cp_parser_parse_definitely (parser))
3699 {
3700 /* Warn the user that a compound literal is not
3701 allowed in standard C++. */
3702 if (pedantic)
3703 pedwarn ("ISO C++ forbids compound-literals");
3704 /* Form the representation of the compound-literal. */
21526606 3705 postfix_expression
a723baf1
MM
3706 = finish_compound_literal (type, initializer_list);
3707 break;
3708 }
3709 }
3710
3711 /* It must be a primary-expression. */
21526606 3712 postfix_expression = cp_parser_primary_expression (parser,
a723baf1
MM
3713 &idk,
3714 &qualifying_class);
3715 }
3716 break;
3717 }
3718
ee76b931
MM
3719 /* If we were avoiding committing to the processing of a
3720 qualified-id until we knew whether or not we had a
3721 pointer-to-member, we now know. */
089d6ea7 3722 if (qualifying_class)
a723baf1 3723 {
ee76b931 3724 bool done;
a723baf1 3725
ee76b931
MM
3726 /* Peek at the next token. */
3727 token = cp_lexer_peek_token (parser->lexer);
3728 done = (token->type != CPP_OPEN_SQUARE
3729 && token->type != CPP_OPEN_PAREN
3730 && token->type != CPP_DOT
3731 && token->type != CPP_DEREF
3732 && token->type != CPP_PLUS_PLUS
3733 && token->type != CPP_MINUS_MINUS);
3734
3735 postfix_expression = finish_qualified_id_expr (qualifying_class,
3736 postfix_expression,
3737 done,
3738 address_p);
3739 if (done)
3740 return postfix_expression;
a723baf1
MM
3741 }
3742
a723baf1
MM
3743 /* Keep looping until the postfix-expression is complete. */
3744 while (true)
3745 {
10b1d5e7
MM
3746 if (idk == CP_ID_KIND_UNQUALIFIED
3747 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3748 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 3749 /* It is not a Koenig lookup function call. */
21526606 3750 postfix_expression
b3445994 3751 = unqualified_name_lookup_error (postfix_expression);
21526606 3752
a723baf1
MM
3753 /* Peek at the next token. */
3754 token = cp_lexer_peek_token (parser->lexer);
3755
3756 switch (token->type)
3757 {
3758 case CPP_OPEN_SQUARE:
3759 /* postfix-expression [ expression ] */
3760 {
3761 tree index;
3762
3763 /* Consume the `[' token. */
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Parse the index expression. */
3766 index = cp_parser_expression (parser);
3767 /* Look for the closing `]'. */
3768 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3769
3770 /* Build the ARRAY_REF. */
21526606 3771 postfix_expression
a723baf1 3772 = grok_array_decl (postfix_expression, index);
b3445994 3773 idk = CP_ID_KIND_NONE;
a5ac3982
MM
3774 /* Array references are not permitted in
3775 constant-expressions. */
625cbf93
MM
3776 if (cp_parser_non_integral_constant_expression
3777 (parser, "an array reference"))
3778 postfix_expression = error_mark_node;
a723baf1
MM
3779 }
3780 break;
3781
3782 case CPP_OPEN_PAREN:
3783 /* postfix-expression ( expression-list [opt] ) */
3784 {
6d80c4b9 3785 bool koenig_p;
21526606 3786 tree args = (cp_parser_parenthesized_expression_list
39703eb9 3787 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3788
7efa3e22
NS
3789 if (args == error_mark_node)
3790 {
3791 postfix_expression = error_mark_node;
3792 break;
3793 }
21526606 3794
14d22dd6
MM
3795 /* Function calls are not permitted in
3796 constant-expressions. */
625cbf93
MM
3797 if (cp_parser_non_integral_constant_expression (parser,
3798 "a function call"))
14d22dd6 3799 {
625cbf93
MM
3800 postfix_expression = error_mark_node;
3801 break;
14d22dd6 3802 }
a723baf1 3803
6d80c4b9 3804 koenig_p = false;
399dedb9
NS
3805 if (idk == CP_ID_KIND_UNQUALIFIED)
3806 {
676e33ca
MM
3807 /* We do not perform argument-dependent lookup if
3808 normal lookup finds a non-function, in accordance
3809 with the expected resolution of DR 218. */
399dedb9
NS
3810 if (args
3811 && (is_overloaded_fn (postfix_expression)
399dedb9 3812 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3813 {
3814 koenig_p = true;
21526606 3815 postfix_expression
6d80c4b9
MM
3816 = perform_koenig_lookup (postfix_expression, args);
3817 }
399dedb9
NS
3818 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3819 postfix_expression
3820 = unqualified_fn_lookup_error (postfix_expression);
3821 }
21526606 3822
d17811fd 3823 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3824 {
d17811fd
MM
3825 tree instance = TREE_OPERAND (postfix_expression, 0);
3826 tree fn = TREE_OPERAND (postfix_expression, 1);
3827
3828 if (processing_template_decl
3829 && (type_dependent_expression_p (instance)
3830 || (!BASELINK_P (fn)
3831 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3832 || type_dependent_expression_p (fn)
d17811fd
MM
3833 || any_type_dependent_arguments_p (args)))
3834 {
3835 postfix_expression
3836 = build_min_nt (CALL_EXPR, postfix_expression, args);
3837 break;
3838 }
9f880ef9
MM
3839
3840 if (BASELINK_P (fn))
3841 postfix_expression
21526606
EC
3842 = (build_new_method_call
3843 (instance, fn, args, NULL_TREE,
3844 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
3845 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3846 else
3847 postfix_expression
3848 = finish_call_expr (postfix_expression, args,
3849 /*disallow_virtual=*/false,
3850 /*koenig_p=*/false);
a723baf1 3851 }
d17811fd
MM
3852 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3853 || TREE_CODE (postfix_expression) == MEMBER_REF
3854 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3855 postfix_expression = (build_offset_ref_call_from_tree
3856 (postfix_expression, args));
b3445994 3857 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3858 /* A call to a static class member, or a namespace-scope
3859 function. */
3860 postfix_expression
3861 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3862 /*disallow_virtual=*/true,
3863 koenig_p);
a723baf1 3864 else
2050a1bb 3865 /* All other function calls. */
21526606
EC
3866 postfix_expression
3867 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3868 /*disallow_virtual=*/false,
3869 koenig_p);
a723baf1
MM
3870
3871 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3872 idk = CP_ID_KIND_NONE;
a723baf1
MM
3873 }
3874 break;
21526606 3875
a723baf1
MM
3876 case CPP_DOT:
3877 case CPP_DEREF:
21526606
EC
3878 /* postfix-expression . template [opt] id-expression
3879 postfix-expression . pseudo-destructor-name
a723baf1
MM
3880 postfix-expression -> template [opt] id-expression
3881 postfix-expression -> pseudo-destructor-name */
3882 {
3883 tree name;
3884 bool dependent_p;
3885 bool template_p;
3886 tree scope = NULL_TREE;
a5ac3982 3887 enum cpp_ttype token_type = token->type;
a723baf1
MM
3888
3889 /* If this is a `->' operator, dereference the pointer. */
3890 if (token->type == CPP_DEREF)
3891 postfix_expression = build_x_arrow (postfix_expression);
3892 /* Check to see whether or not the expression is
3893 type-dependent. */
bbaab916 3894 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3895 /* The identifier following the `->' or `.' is not
3896 qualified. */
3897 parser->scope = NULL_TREE;
3898 parser->qualifying_scope = NULL_TREE;
3899 parser->object_scope = NULL_TREE;
b3445994 3900 idk = CP_ID_KIND_NONE;
a723baf1
MM
3901 /* Enter the scope corresponding to the type of the object
3902 given by the POSTFIX_EXPRESSION. */
21526606 3903 if (!dependent_p
a723baf1
MM
3904 && TREE_TYPE (postfix_expression) != NULL_TREE)
3905 {
3906 scope = TREE_TYPE (postfix_expression);
3907 /* According to the standard, no expression should
3908 ever have reference type. Unfortunately, we do not
3909 currently match the standard in this respect in
3910 that our internal representation of an expression
3911 may have reference type even when the standard says
3912 it does not. Therefore, we have to manually obtain
3913 the underlying type here. */
ee76b931 3914 scope = non_reference (scope);
a723baf1
MM
3915 /* The type of the POSTFIX_EXPRESSION must be
3916 complete. */
3917 scope = complete_type_or_else (scope, NULL_TREE);
3918 /* Let the name lookup machinery know that we are
3919 processing a class member access expression. */
3920 parser->context->object_type = scope;
3921 /* If something went wrong, we want to be able to
3922 discern that case, as opposed to the case where
3923 there was no SCOPE due to the type of expression
3924 being dependent. */
3925 if (!scope)
3926 scope = error_mark_node;
be799b1e
MM
3927 /* If the SCOPE was erroneous, make the various
3928 semantic analysis functions exit quickly -- and
3929 without issuing additional error messages. */
3930 if (scope == error_mark_node)
3931 postfix_expression = error_mark_node;
a723baf1
MM
3932 }
3933
3934 /* Consume the `.' or `->' operator. */
3935 cp_lexer_consume_token (parser->lexer);
3936 /* If the SCOPE is not a scalar type, we are looking at an
3937 ordinary class member access expression, rather than a
3938 pseudo-destructor-name. */
3939 if (!scope || !SCALAR_TYPE_P (scope))
3940 {
3941 template_p = cp_parser_optional_template_keyword (parser);
3942 /* Parse the id-expression. */
3943 name = cp_parser_id_expression (parser,
3944 template_p,
3945 /*check_dependency_p=*/true,
f3c2dfc6
MM
3946 /*template_p=*/NULL,
3947 /*declarator_p=*/false);
a723baf1
MM
3948 /* In general, build a SCOPE_REF if the member name is
3949 qualified. However, if the name was not dependent
3950 and has already been resolved; there is no need to
3951 build the SCOPE_REF. For example;
3952
3953 struct X { void f(); };
3954 template <typename T> void f(T* t) { t->X::f(); }
21526606 3955
d17811fd
MM
3956 Even though "t" is dependent, "X::f" is not and has
3957 been resolved to a BASELINK; there is no need to
a723baf1 3958 include scope information. */
a6bd211d
JM
3959
3960 /* But we do need to remember that there was an explicit
3961 scope for virtual function calls. */
3962 if (parser->scope)
b3445994 3963 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3964
21526606 3965 if (name != error_mark_node
a723baf1
MM
3966 && !BASELINK_P (name)
3967 && parser->scope)
3968 {
3969 name = build_nt (SCOPE_REF, parser->scope, name);
3970 parser->scope = NULL_TREE;
3971 parser->qualifying_scope = NULL_TREE;
3972 parser->object_scope = NULL_TREE;
3973 }
21526606 3974 postfix_expression
a723baf1
MM
3975 = finish_class_member_access_expr (postfix_expression, name);
3976 }
3977 /* Otherwise, try the pseudo-destructor-name production. */
3978 else
3979 {
90808894 3980 tree s = NULL_TREE;
a723baf1
MM
3981 tree type;
3982
3983 /* Parse the pseudo-destructor-name. */
3984 cp_parser_pseudo_destructor_name (parser, &s, &type);
3985 /* Form the call. */
21526606 3986 postfix_expression
a723baf1
MM
3987 = finish_pseudo_destructor_expr (postfix_expression,
3988 s, TREE_TYPE (type));
3989 }
3990
3991 /* We no longer need to look up names in the scope of the
3992 object on the left-hand side of the `.' or `->'
3993 operator. */
3994 parser->context->object_type = NULL_TREE;
a5ac3982 3995 /* These operators may not appear in constant-expressions. */
625cbf93 3996 if (/* The "->" operator is allowed in the implementation
643aee72
MM
3997 of "offsetof". The "." operator may appear in the
3998 name of the member. */
625cbf93
MM
3999 !parser->in_offsetof_p
4000 && (cp_parser_non_integral_constant_expression
4001 (parser,
4002 token_type == CPP_DEREF ? "'->'" : "`.'")))
4003 postfix_expression = error_mark_node;
a723baf1
MM
4004 }
4005 break;
4006
4007 case CPP_PLUS_PLUS:
4008 /* postfix-expression ++ */
4009 /* Consume the `++' token. */
4010 cp_lexer_consume_token (parser->lexer);
a5ac3982 4011 /* Generate a representation for the complete expression. */
21526606
EC
4012 postfix_expression
4013 = finish_increment_expr (postfix_expression,
a5ac3982 4014 POSTINCREMENT_EXPR);
14d22dd6 4015 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4016 if (cp_parser_non_integral_constant_expression (parser,
4017 "an increment"))
4018 postfix_expression = error_mark_node;
b3445994 4019 idk = CP_ID_KIND_NONE;
a723baf1
MM
4020 break;
4021
4022 case CPP_MINUS_MINUS:
4023 /* postfix-expression -- */
4024 /* Consume the `--' token. */
4025 cp_lexer_consume_token (parser->lexer);
a5ac3982 4026 /* Generate a representation for the complete expression. */
21526606
EC
4027 postfix_expression
4028 = finish_increment_expr (postfix_expression,
a5ac3982 4029 POSTDECREMENT_EXPR);
14d22dd6 4030 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4031 if (cp_parser_non_integral_constant_expression (parser,
4032 "a decrement"))
4033 postfix_expression = error_mark_node;
b3445994 4034 idk = CP_ID_KIND_NONE;
a723baf1
MM
4035 break;
4036
4037 default:
4038 return postfix_expression;
4039 }
4040 }
4041
4042 /* We should never get here. */
4043 abort ();
4044 return error_mark_node;
4045}
4046
7efa3e22 4047/* Parse a parenthesized expression-list.
a723baf1
MM
4048
4049 expression-list:
4050 assignment-expression
4051 expression-list, assignment-expression
4052
7efa3e22
NS
4053 attribute-list:
4054 expression-list
4055 identifier
4056 identifier, expression-list
4057
a723baf1
MM
4058 Returns a TREE_LIST. The TREE_VALUE of each node is a
4059 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4060 is returned even if there is only a single expression in the list.
4061 error_mark_node is returned if the ( and or ) are
4062 missing. NULL_TREE is returned on no expressions. The parentheses
4063 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4064 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4065 indicates whether or not all of the expressions in the list were
4066 constant. */
a723baf1
MM
4067
4068static tree
21526606 4069cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9
MM
4070 bool is_attribute_list,
4071 bool *non_constant_p)
a723baf1
MM
4072{
4073 tree expression_list = NULL_TREE;
7efa3e22 4074 tree identifier = NULL_TREE;
39703eb9
MM
4075
4076 /* Assume all the expressions will be constant. */
4077 if (non_constant_p)
4078 *non_constant_p = false;
4079
7efa3e22
NS
4080 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4081 return error_mark_node;
21526606 4082
a723baf1 4083 /* Consume expressions until there are no more. */
7efa3e22
NS
4084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4085 while (true)
4086 {
4087 tree expr;
21526606 4088
7efa3e22
NS
4089 /* At the beginning of attribute lists, check to see if the
4090 next token is an identifier. */
4091 if (is_attribute_list
4092 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4093 {
4094 cp_token *token;
21526606 4095
7efa3e22
NS
4096 /* Consume the identifier. */
4097 token = cp_lexer_consume_token (parser->lexer);
4098 /* Save the identifier. */
4099 identifier = token->value;
4100 }
4101 else
4102 {
4103 /* Parse the next assignment-expression. */
39703eb9
MM
4104 if (non_constant_p)
4105 {
4106 bool expr_non_constant_p;
21526606 4107 expr = (cp_parser_constant_expression
39703eb9
MM
4108 (parser, /*allow_non_constant_p=*/true,
4109 &expr_non_constant_p));
4110 if (expr_non_constant_p)
4111 *non_constant_p = true;
4112 }
4113 else
4114 expr = cp_parser_assignment_expression (parser);
a723baf1 4115
7efa3e22
NS
4116 /* Add it to the list. We add error_mark_node
4117 expressions to the list, so that we can still tell if
4118 the correct form for a parenthesized expression-list
4119 is found. That gives better errors. */
4120 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4121
7efa3e22
NS
4122 if (expr == error_mark_node)
4123 goto skip_comma;
4124 }
a723baf1 4125
7efa3e22
NS
4126 /* After the first item, attribute lists look the same as
4127 expression lists. */
4128 is_attribute_list = false;
21526606 4129
7efa3e22
NS
4130 get_comma:;
4131 /* If the next token isn't a `,', then we are done. */
4132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4133 break;
4134
4135 /* Otherwise, consume the `,' and keep going. */
4136 cp_lexer_consume_token (parser->lexer);
4137 }
21526606 4138
7efa3e22
NS
4139 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4140 {
4141 int ending;
21526606 4142
7efa3e22
NS
4143 skip_comma:;
4144 /* We try and resync to an unnested comma, as that will give the
4145 user better diagnostics. */
21526606
EC
4146 ending = cp_parser_skip_to_closing_parenthesis (parser,
4147 /*recovering=*/true,
4bb8ca28 4148 /*or_comma=*/true,
a668c6ad 4149 /*consume_paren=*/true);
7efa3e22
NS
4150 if (ending < 0)
4151 goto get_comma;
4152 if (!ending)
4153 return error_mark_node;
a723baf1
MM
4154 }
4155
4156 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4157 expression_list = nreverse (expression_list);
4158 if (identifier)
4159 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4160
7efa3e22 4161 return expression_list;
a723baf1
MM
4162}
4163
4164/* Parse a pseudo-destructor-name.
4165
4166 pseudo-destructor-name:
4167 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4168 :: [opt] nested-name-specifier template template-id :: ~ type-name
4169 :: [opt] nested-name-specifier [opt] ~ type-name
4170
4171 If either of the first two productions is used, sets *SCOPE to the
4172 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4173 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4174 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4175
4176static void
21526606
EC
4177cp_parser_pseudo_destructor_name (cp_parser* parser,
4178 tree* scope,
94edc4ab 4179 tree* type)
a723baf1
MM
4180{
4181 bool nested_name_specifier_p;
4182
4183 /* Look for the optional `::' operator. */
4184 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4185 /* Look for the optional nested-name-specifier. */
21526606 4186 nested_name_specifier_p
a723baf1
MM
4187 = (cp_parser_nested_name_specifier_opt (parser,
4188 /*typename_keyword_p=*/false,
4189 /*check_dependency_p=*/true,
a668c6ad 4190 /*type_p=*/false,
21526606 4191 /*is_declaration=*/true)
a723baf1
MM
4192 != NULL_TREE);
4193 /* Now, if we saw a nested-name-specifier, we might be doing the
4194 second production. */
21526606 4195 if (nested_name_specifier_p
a723baf1
MM
4196 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4197 {
4198 /* Consume the `template' keyword. */
4199 cp_lexer_consume_token (parser->lexer);
4200 /* Parse the template-id. */
21526606 4201 cp_parser_template_id (parser,
a723baf1 4202 /*template_keyword_p=*/true,
a668c6ad
MM
4203 /*check_dependency_p=*/false,
4204 /*is_declaration=*/true);
a723baf1
MM
4205 /* Look for the `::' token. */
4206 cp_parser_require (parser, CPP_SCOPE, "`::'");
4207 }
4208 /* If the next token is not a `~', then there might be some
9bcb9aae 4209 additional qualification. */
a723baf1
MM
4210 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4211 {
4212 /* Look for the type-name. */
4213 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462
ILT
4214
4215 /* If we didn't get an aggregate type, or we don't have ::~,
4216 then something has gone wrong. Since the only caller of this
4217 function is looking for something after `.' or `->' after a
4218 scalar type, most likely the program is trying to get a
4219 member of a non-aggregate type. */
4220 if (*scope == error_mark_node
4221 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4222 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4223 {
4224 cp_parser_error (parser, "request for member of non-aggregate type");
4225 *type = error_mark_node;
4226 return;
4227 }
4228
a723baf1
MM
4229 /* Look for the `::' token. */
4230 cp_parser_require (parser, CPP_SCOPE, "`::'");
4231 }
4232 else
4233 *scope = NULL_TREE;
4234
4235 /* Look for the `~'. */
4236 cp_parser_require (parser, CPP_COMPL, "`~'");
4237 /* Look for the type-name again. We are not responsible for
4238 checking that it matches the first type-name. */
4239 *type = cp_parser_type_name (parser);
4240}
4241
4242/* Parse a unary-expression.
4243
4244 unary-expression:
4245 postfix-expression
4246 ++ cast-expression
4247 -- cast-expression
4248 unary-operator cast-expression
4249 sizeof unary-expression
4250 sizeof ( type-id )
4251 new-expression
4252 delete-expression
4253
4254 GNU Extensions:
4255
4256 unary-expression:
4257 __extension__ cast-expression
4258 __alignof__ unary-expression
4259 __alignof__ ( type-id )
4260 __real__ cast-expression
4261 __imag__ cast-expression
4262 && identifier
4263
4264 ADDRESS_P is true iff the unary-expression is appearing as the
4265 operand of the `&' operator.
4266
34cd5ae7 4267 Returns a representation of the expression. */
a723baf1
MM
4268
4269static tree
4270cp_parser_unary_expression (cp_parser *parser, bool address_p)
4271{
4272 cp_token *token;
4273 enum tree_code unary_operator;
4274
4275 /* Peek at the next token. */
4276 token = cp_lexer_peek_token (parser->lexer);
4277 /* Some keywords give away the kind of expression. */
4278 if (token->type == CPP_KEYWORD)
4279 {
4280 enum rid keyword = token->keyword;
4281
4282 switch (keyword)
4283 {
4284 case RID_ALIGNOF:
a723baf1
MM
4285 case RID_SIZEOF:
4286 {
4287 tree operand;
7a18b933 4288 enum tree_code op;
21526606 4289
7a18b933
NS
4290 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4291 /* Consume the token. */
a723baf1
MM
4292 cp_lexer_consume_token (parser->lexer);
4293 /* Parse the operand. */
4294 operand = cp_parser_sizeof_operand (parser, keyword);
4295
7a18b933
NS
4296 if (TYPE_P (operand))
4297 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4298 else
7a18b933 4299 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4300 }
4301
4302 case RID_NEW:
4303 return cp_parser_new_expression (parser);
4304
4305 case RID_DELETE:
4306 return cp_parser_delete_expression (parser);
21526606 4307
a723baf1
MM
4308 case RID_EXTENSION:
4309 {
4310 /* The saved value of the PEDANTIC flag. */
4311 int saved_pedantic;
4312 tree expr;
4313
4314 /* Save away the PEDANTIC flag. */
4315 cp_parser_extension_opt (parser, &saved_pedantic);
4316 /* Parse the cast-expression. */
d6b4ea85 4317 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4318 /* Restore the PEDANTIC flag. */
4319 pedantic = saved_pedantic;
4320
4321 return expr;
4322 }
4323
4324 case RID_REALPART:
4325 case RID_IMAGPART:
4326 {
4327 tree expression;
4328
4329 /* Consume the `__real__' or `__imag__' token. */
4330 cp_lexer_consume_token (parser->lexer);
4331 /* Parse the cast-expression. */
d6b4ea85 4332 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4333 /* Create the complete representation. */
4334 return build_x_unary_op ((keyword == RID_REALPART
4335 ? REALPART_EXPR : IMAGPART_EXPR),
4336 expression);
4337 }
4338 break;
4339
4340 default:
4341 break;
4342 }
4343 }
4344
4345 /* Look for the `:: new' and `:: delete', which also signal the
4346 beginning of a new-expression, or delete-expression,
4347 respectively. If the next token is `::', then it might be one of
4348 these. */
4349 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4350 {
4351 enum rid keyword;
4352
4353 /* See if the token after the `::' is one of the keywords in
4354 which we're interested. */
4355 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4356 /* If it's `new', we have a new-expression. */
4357 if (keyword == RID_NEW)
4358 return cp_parser_new_expression (parser);
4359 /* Similarly, for `delete'. */
4360 else if (keyword == RID_DELETE)
4361 return cp_parser_delete_expression (parser);
4362 }
4363
4364 /* Look for a unary operator. */
4365 unary_operator = cp_parser_unary_operator (token);
4366 /* The `++' and `--' operators can be handled similarly, even though
4367 they are not technically unary-operators in the grammar. */
4368 if (unary_operator == ERROR_MARK)
4369 {
4370 if (token->type == CPP_PLUS_PLUS)
4371 unary_operator = PREINCREMENT_EXPR;
4372 else if (token->type == CPP_MINUS_MINUS)
4373 unary_operator = PREDECREMENT_EXPR;
4374 /* Handle the GNU address-of-label extension. */
4375 else if (cp_parser_allow_gnu_extensions_p (parser)
4376 && token->type == CPP_AND_AND)
4377 {
4378 tree identifier;
4379
4380 /* Consume the '&&' token. */
4381 cp_lexer_consume_token (parser->lexer);
4382 /* Look for the identifier. */
4383 identifier = cp_parser_identifier (parser);
4384 /* Create an expression representing the address. */
4385 return finish_label_address_expr (identifier);
4386 }
4387 }
4388 if (unary_operator != ERROR_MARK)
4389 {
4390 tree cast_expression;
a5ac3982
MM
4391 tree expression = error_mark_node;
4392 const char *non_constant_p = NULL;
a723baf1
MM
4393
4394 /* Consume the operator token. */
4395 token = cp_lexer_consume_token (parser->lexer);
4396 /* Parse the cast-expression. */
21526606 4397 cast_expression
a723baf1
MM
4398 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4399 /* Now, build an appropriate representation. */
4400 switch (unary_operator)
4401 {
4402 case INDIRECT_REF:
a5ac3982
MM
4403 non_constant_p = "`*'";
4404 expression = build_x_indirect_ref (cast_expression, "unary *");
4405 break;
4406
a723baf1 4407 case ADDR_EXPR:
263ee052
MM
4408 /* The "&" operator is allowed in the implementation of
4409 "offsetof". */
4410 if (!parser->in_offsetof_p)
4411 non_constant_p = "`&'";
a5ac3982 4412 /* Fall through. */
d17811fd 4413 case BIT_NOT_EXPR:
a5ac3982
MM
4414 expression = build_x_unary_op (unary_operator, cast_expression);
4415 break;
4416
14d22dd6
MM
4417 case PREINCREMENT_EXPR:
4418 case PREDECREMENT_EXPR:
a5ac3982
MM
4419 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4420 ? "`++'" : "`--'");
14d22dd6 4421 /* Fall through. */
a723baf1
MM
4422 case CONVERT_EXPR:
4423 case NEGATE_EXPR:
4424 case TRUTH_NOT_EXPR:
a5ac3982
MM
4425 expression = finish_unary_op_expr (unary_operator, cast_expression);
4426 break;
a723baf1 4427
a723baf1
MM
4428 default:
4429 abort ();
a723baf1 4430 }
a5ac3982 4431
625cbf93
MM
4432 if (non_constant_p
4433 && cp_parser_non_integral_constant_expression (parser,
4434 non_constant_p))
4435 expression = error_mark_node;
a5ac3982
MM
4436
4437 return expression;
a723baf1
MM
4438 }
4439
4440 return cp_parser_postfix_expression (parser, address_p);
4441}
4442
4443/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4444 unary-operator, the corresponding tree code is returned. */
4445
4446static enum tree_code
94edc4ab 4447cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4448{
4449 switch (token->type)
4450 {
4451 case CPP_MULT:
4452 return INDIRECT_REF;
4453
4454 case CPP_AND:
4455 return ADDR_EXPR;
4456
4457 case CPP_PLUS:
4458 return CONVERT_EXPR;
4459
4460 case CPP_MINUS:
4461 return NEGATE_EXPR;
4462
4463 case CPP_NOT:
4464 return TRUTH_NOT_EXPR;
21526606 4465
a723baf1
MM
4466 case CPP_COMPL:
4467 return BIT_NOT_EXPR;
4468
4469 default:
4470 return ERROR_MARK;
4471 }
4472}
4473
4474/* Parse a new-expression.
4475
ca099ac8 4476 new-expression:
a723baf1
MM
4477 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4478 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4479
4480 Returns a representation of the expression. */
4481
4482static tree
94edc4ab 4483cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4484{
4485 bool global_scope_p;
4486 tree placement;
4487 tree type;
4488 tree initializer;
4489
4490 /* Look for the optional `::' operator. */
21526606 4491 global_scope_p
a723baf1
MM
4492 = (cp_parser_global_scope_opt (parser,
4493 /*current_scope_valid_p=*/false)
4494 != NULL_TREE);
4495 /* Look for the `new' operator. */
4496 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4497 /* There's no easy way to tell a new-placement from the
4498 `( type-id )' construct. */
4499 cp_parser_parse_tentatively (parser);
4500 /* Look for a new-placement. */
4501 placement = cp_parser_new_placement (parser);
4502 /* If that didn't work out, there's no new-placement. */
4503 if (!cp_parser_parse_definitely (parser))
4504 placement = NULL_TREE;
4505
4506 /* If the next token is a `(', then we have a parenthesized
4507 type-id. */
4508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4509 {
4510 /* Consume the `('. */
4511 cp_lexer_consume_token (parser->lexer);
4512 /* Parse the type-id. */
4513 type = cp_parser_type_id (parser);
4514 /* Look for the closing `)'. */
4515 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
063e900f
GB
4516 /* There should not be a direct-new-declarator in this production,
4517 but GCC used to allowed this, so we check and emit a sensible error
4518 message for this case. */
4519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4520 {
4521 error ("array bound forbidden after parenthesized type-id");
4522 inform ("try removing the parentheses around the type-id");
063e900f
GB
4523 cp_parser_direct_new_declarator (parser);
4524 }
a723baf1
MM
4525 }
4526 /* Otherwise, there must be a new-type-id. */
4527 else
4528 type = cp_parser_new_type_id (parser);
4529
4530 /* If the next token is a `(', then we have a new-initializer. */
4531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4532 initializer = cp_parser_new_initializer (parser);
4533 else
4534 initializer = NULL_TREE;
4535
625cbf93
MM
4536 /* A new-expression may not appear in an integral constant
4537 expression. */
4538 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4539 return error_mark_node;
4540
a723baf1
MM
4541 /* Create a representation of the new-expression. */
4542 return build_new (placement, type, initializer, global_scope_p);
4543}
4544
4545/* Parse a new-placement.
4546
4547 new-placement:
4548 ( expression-list )
4549
4550 Returns the same representation as for an expression-list. */
4551
4552static tree
94edc4ab 4553cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4554{
4555 tree expression_list;
4556
a723baf1 4557 /* Parse the expression-list. */
21526606 4558 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4559 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4560
4561 return expression_list;
4562}
4563
4564/* Parse a new-type-id.
4565
4566 new-type-id:
4567 type-specifier-seq new-declarator [opt]
4568
4569 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4570 and whose TREE_VALUE is the new-declarator. */
4571
4572static tree
94edc4ab 4573cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4574{
4575 tree type_specifier_seq;
4576 tree declarator;
4577 const char *saved_message;
4578
4579 /* The type-specifier sequence must not contain type definitions.
4580 (It cannot contain declarations of new types either, but if they
4581 are not definitions we will catch that because they are not
4582 complete.) */
4583 saved_message = parser->type_definition_forbidden_message;
4584 parser->type_definition_forbidden_message
4585 = "types may not be defined in a new-type-id";
4586 /* Parse the type-specifier-seq. */
4587 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4588 /* Restore the old message. */
4589 parser->type_definition_forbidden_message = saved_message;
4590 /* Parse the new-declarator. */
4591 declarator = cp_parser_new_declarator_opt (parser);
4592
4593 return build_tree_list (type_specifier_seq, declarator);
4594}
4595
4596/* Parse an (optional) new-declarator.
4597
4598 new-declarator:
4599 ptr-operator new-declarator [opt]
4600 direct-new-declarator
4601
4602 Returns a representation of the declarator. See
4603 cp_parser_declarator for the representations used. */
4604
4605static tree
94edc4ab 4606cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4607{
4608 enum tree_code code;
4609 tree type;
4610 tree cv_qualifier_seq;
4611
4612 /* We don't know if there's a ptr-operator next, or not. */
4613 cp_parser_parse_tentatively (parser);
4614 /* Look for a ptr-operator. */
4615 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4616 /* If that worked, look for more new-declarators. */
4617 if (cp_parser_parse_definitely (parser))
4618 {
4619 tree declarator;
4620
4621 /* Parse another optional declarator. */
4622 declarator = cp_parser_new_declarator_opt (parser);
4623
4624 /* Create the representation of the declarator. */
4625 if (code == INDIRECT_REF)
4626 declarator = make_pointer_declarator (cv_qualifier_seq,
4627 declarator);
4628 else
4629 declarator = make_reference_declarator (cv_qualifier_seq,
4630 declarator);
4631
4632 /* Handle the pointer-to-member case. */
4633 if (type)
4634 declarator = build_nt (SCOPE_REF, type, declarator);
4635
4636 return declarator;
4637 }
4638
4639 /* If the next token is a `[', there is a direct-new-declarator. */
4640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4641 return cp_parser_direct_new_declarator (parser);
4642
4643 return NULL_TREE;
4644}
4645
4646/* Parse a direct-new-declarator.
4647
4648 direct-new-declarator:
4649 [ expression ]
21526606 4650 direct-new-declarator [constant-expression]
a723baf1
MM
4651
4652 Returns an ARRAY_REF, following the same conventions as are
4653 documented for cp_parser_direct_declarator. */
4654
4655static tree
94edc4ab 4656cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4657{
4658 tree declarator = NULL_TREE;
4659
4660 while (true)
4661 {
4662 tree expression;
4663
4664 /* Look for the opening `['. */
4665 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4666 /* The first expression is not required to be constant. */
4667 if (!declarator)
4668 {
4669 expression = cp_parser_expression (parser);
4670 /* The standard requires that the expression have integral
4671 type. DR 74 adds enumeration types. We believe that the
4672 real intent is that these expressions be handled like the
4673 expression in a `switch' condition, which also allows
4674 classes with a single conversion to integral or
4675 enumeration type. */
4676 if (!processing_template_decl)
4677 {
21526606 4678 expression
a723baf1
MM
4679 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4680 expression,
b746c5dc 4681 /*complain=*/true);
a723baf1
MM
4682 if (!expression)
4683 {
4684 error ("expression in new-declarator must have integral or enumeration type");
4685 expression = error_mark_node;
4686 }
4687 }
4688 }
4689 /* But all the other expressions must be. */
4690 else
21526606
EC
4691 expression
4692 = cp_parser_constant_expression (parser,
14d22dd6
MM
4693 /*allow_non_constant=*/false,
4694 NULL);
a723baf1
MM
4695 /* Look for the closing `]'. */
4696 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4697
4698 /* Add this bound to the declarator. */
4699 declarator = build_nt (ARRAY_REF, declarator, expression);
4700
4701 /* If the next token is not a `[', then there are no more
4702 bounds. */
4703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4704 break;
4705 }
4706
4707 return declarator;
4708}
4709
4710/* Parse a new-initializer.
4711
4712 new-initializer:
4713 ( expression-list [opt] )
4714
34cd5ae7 4715 Returns a representation of the expression-list. If there is no
a723baf1
MM
4716 expression-list, VOID_ZERO_NODE is returned. */
4717
4718static tree
94edc4ab 4719cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4720{
4721 tree expression_list;
4722
21526606 4723 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4724 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4725 if (!expression_list)
a723baf1 4726 expression_list = void_zero_node;
a723baf1
MM
4727
4728 return expression_list;
4729}
4730
4731/* Parse a delete-expression.
4732
4733 delete-expression:
4734 :: [opt] delete cast-expression
4735 :: [opt] delete [ ] cast-expression
4736
4737 Returns a representation of the expression. */
4738
4739static tree
94edc4ab 4740cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4741{
4742 bool global_scope_p;
4743 bool array_p;
4744 tree expression;
4745
4746 /* Look for the optional `::' operator. */
4747 global_scope_p
4748 = (cp_parser_global_scope_opt (parser,
4749 /*current_scope_valid_p=*/false)
4750 != NULL_TREE);
4751 /* Look for the `delete' keyword. */
4752 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4753 /* See if the array syntax is in use. */
4754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4755 {
4756 /* Consume the `[' token. */
4757 cp_lexer_consume_token (parser->lexer);
4758 /* Look for the `]' token. */
4759 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4760 /* Remember that this is the `[]' construct. */
4761 array_p = true;
4762 }
4763 else
4764 array_p = false;
4765
4766 /* Parse the cast-expression. */
d6b4ea85 4767 expression = cp_parser_simple_cast_expression (parser);
a723baf1 4768
625cbf93
MM
4769 /* A delete-expression may not appear in an integral constant
4770 expression. */
4771 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4772 return error_mark_node;
4773
a723baf1
MM
4774 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4775}
4776
4777/* Parse a cast-expression.
4778
4779 cast-expression:
4780 unary-expression
4781 ( type-id ) cast-expression
4782
4783 Returns a representation of the expression. */
4784
4785static tree
4786cp_parser_cast_expression (cp_parser *parser, bool address_p)
4787{
4788 /* If it's a `(', then we might be looking at a cast. */
4789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4790 {
4791 tree type = NULL_TREE;
4792 tree expr = NULL_TREE;
4793 bool compound_literal_p;
4794 const char *saved_message;
4795
4796 /* There's no way to know yet whether or not this is a cast.
4797 For example, `(int (3))' is a unary-expression, while `(int)
4798 3' is a cast. So, we resort to parsing tentatively. */
4799 cp_parser_parse_tentatively (parser);
4800 /* Types may not be defined in a cast. */
4801 saved_message = parser->type_definition_forbidden_message;
4802 parser->type_definition_forbidden_message
4803 = "types may not be defined in casts";
4804 /* Consume the `('. */
4805 cp_lexer_consume_token (parser->lexer);
4806 /* A very tricky bit is that `(struct S) { 3 }' is a
4807 compound-literal (which we permit in C++ as an extension).
4808 But, that construct is not a cast-expression -- it is a
4809 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4810 is legal; if the compound-literal were a cast-expression,
4811 you'd need an extra set of parentheses.) But, if we parse
4812 the type-id, and it happens to be a class-specifier, then we
4813 will commit to the parse at that point, because we cannot
4814 undo the action that is done when creating a new class. So,
21526606 4815 then we cannot back up and do a postfix-expression.
a723baf1
MM
4816
4817 Therefore, we scan ahead to the closing `)', and check to see
4818 if the token after the `)' is a `{'. If so, we are not
21526606 4819 looking at a cast-expression.
a723baf1
MM
4820
4821 Save tokens so that we can put them back. */
4822 cp_lexer_save_tokens (parser->lexer);
4823 /* Skip tokens until the next token is a closing parenthesis.
4824 If we find the closing `)', and the next token is a `{', then
4825 we are looking at a compound-literal. */
21526606 4826 compound_literal_p
a668c6ad
MM
4827 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4828 /*consume_paren=*/true)
a723baf1
MM
4829 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4830 /* Roll back the tokens we skipped. */
4831 cp_lexer_rollback_tokens (parser->lexer);
4832 /* If we were looking at a compound-literal, simulate an error
4833 so that the call to cp_parser_parse_definitely below will
4834 fail. */
4835 if (compound_literal_p)
4836 cp_parser_simulate_error (parser);
4837 else
4838 {
4f8163b1
MM
4839 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4840 parser->in_type_id_in_expr_p = true;
a723baf1
MM
4841 /* Look for the type-id. */
4842 type = cp_parser_type_id (parser);
4843 /* Look for the closing `)'. */
4844 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 4845 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
4846 }
4847
4848 /* Restore the saved message. */
4849 parser->type_definition_forbidden_message = saved_message;
4850
bbaab916
NS
4851 /* If ok so far, parse the dependent expression. We cannot be
4852 sure it is a cast. Consider `(T ())'. It is a parenthesized
4853 ctor of T, but looks like a cast to function returning T
4854 without a dependent expression. */
4855 if (!cp_parser_error_occurred (parser))
d6b4ea85 4856 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4857
a723baf1
MM
4858 if (cp_parser_parse_definitely (parser))
4859 {
a723baf1 4860 /* Warn about old-style casts, if so requested. */
21526606
EC
4861 if (warn_old_style_cast
4862 && !in_system_header
4863 && !VOID_TYPE_P (type)
a723baf1
MM
4864 && current_lang_name != lang_name_c)
4865 warning ("use of old-style cast");
14d22dd6
MM
4866
4867 /* Only type conversions to integral or enumeration types
4868 can be used in constant-expressions. */
67c03833 4869 if (parser->integral_constant_expression_p
14d22dd6 4870 && !dependent_type_p (type)
625cbf93
MM
4871 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4872 && (cp_parser_non_integral_constant_expression
4873 (parser,
4874 "a cast to a type other than an integral or "
4875 "enumeration type")))
4876 return error_mark_node;
4877
a723baf1
MM
4878 /* Perform the cast. */
4879 expr = build_c_cast (type, expr);
bbaab916 4880 return expr;
a723baf1 4881 }
a723baf1
MM
4882 }
4883
4884 /* If we get here, then it's not a cast, so it must be a
4885 unary-expression. */
4886 return cp_parser_unary_expression (parser, address_p);
4887}
4888
4889/* Parse a pm-expression.
4890
4891 pm-expression:
4892 cast-expression
4893 pm-expression .* cast-expression
4894 pm-expression ->* cast-expression
4895
4896 Returns a representation of the expression. */
4897
4898static tree
94edc4ab 4899cp_parser_pm_expression (cp_parser* parser)
a723baf1 4900{
d6b4ea85
MM
4901 static const cp_parser_token_tree_map map = {
4902 { CPP_DEREF_STAR, MEMBER_REF },
4903 { CPP_DOT_STAR, DOTSTAR_EXPR },
4904 { CPP_EOF, ERROR_MARK }
4905 };
a723baf1 4906
21526606 4907 return cp_parser_binary_expression (parser, map,
d6b4ea85 4908 cp_parser_simple_cast_expression);
a723baf1
MM
4909}
4910
4911/* Parse a multiplicative-expression.
4912
77077b39 4913 multiplicative-expression:
a723baf1
MM
4914 pm-expression
4915 multiplicative-expression * pm-expression
4916 multiplicative-expression / pm-expression
4917 multiplicative-expression % pm-expression
4918
4919 Returns a representation of the expression. */
4920
4921static tree
94edc4ab 4922cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4923{
39b1af70 4924 static const cp_parser_token_tree_map map = {
a723baf1
MM
4925 { CPP_MULT, MULT_EXPR },
4926 { CPP_DIV, TRUNC_DIV_EXPR },
4927 { CPP_MOD, TRUNC_MOD_EXPR },
4928 { CPP_EOF, ERROR_MARK }
4929 };
4930
4931 return cp_parser_binary_expression (parser,
4932 map,
4933 cp_parser_pm_expression);
4934}
4935
4936/* Parse an additive-expression.
4937
4938 additive-expression:
4939 multiplicative-expression
4940 additive-expression + multiplicative-expression
4941 additive-expression - multiplicative-expression
4942
4943 Returns a representation of the expression. */
4944
4945static tree
94edc4ab 4946cp_parser_additive_expression (cp_parser* parser)
a723baf1 4947{
39b1af70 4948 static const cp_parser_token_tree_map map = {
a723baf1
MM
4949 { CPP_PLUS, PLUS_EXPR },
4950 { CPP_MINUS, MINUS_EXPR },
4951 { CPP_EOF, ERROR_MARK }
4952 };
4953
4954 return cp_parser_binary_expression (parser,
4955 map,
4956 cp_parser_multiplicative_expression);
4957}
4958
4959/* Parse a shift-expression.
4960
4961 shift-expression:
4962 additive-expression
4963 shift-expression << additive-expression
4964 shift-expression >> additive-expression
4965
4966 Returns a representation of the expression. */
4967
4968static tree
94edc4ab 4969cp_parser_shift_expression (cp_parser* parser)
a723baf1 4970{
39b1af70 4971 static const cp_parser_token_tree_map map = {
a723baf1
MM
4972 { CPP_LSHIFT, LSHIFT_EXPR },
4973 { CPP_RSHIFT, RSHIFT_EXPR },
4974 { CPP_EOF, ERROR_MARK }
4975 };
4976
4977 return cp_parser_binary_expression (parser,
4978 map,
4979 cp_parser_additive_expression);
4980}
4981
4982/* Parse a relational-expression.
4983
4984 relational-expression:
4985 shift-expression
4986 relational-expression < shift-expression
4987 relational-expression > shift-expression
4988 relational-expression <= shift-expression
4989 relational-expression >= shift-expression
4990
4991 GNU Extension:
4992
4993 relational-expression:
4994 relational-expression <? shift-expression
4995 relational-expression >? shift-expression
4996
4997 Returns a representation of the expression. */
4998
4999static tree
94edc4ab 5000cp_parser_relational_expression (cp_parser* parser)
a723baf1 5001{
39b1af70 5002 static const cp_parser_token_tree_map map = {
a723baf1
MM
5003 { CPP_LESS, LT_EXPR },
5004 { CPP_GREATER, GT_EXPR },
5005 { CPP_LESS_EQ, LE_EXPR },
5006 { CPP_GREATER_EQ, GE_EXPR },
5007 { CPP_MIN, MIN_EXPR },
5008 { CPP_MAX, MAX_EXPR },
5009 { CPP_EOF, ERROR_MARK }
5010 };
5011
5012 return cp_parser_binary_expression (parser,
5013 map,
5014 cp_parser_shift_expression);
5015}
5016
5017/* Parse an equality-expression.
5018
5019 equality-expression:
5020 relational-expression
5021 equality-expression == relational-expression
5022 equality-expression != relational-expression
5023
5024 Returns a representation of the expression. */
5025
5026static tree
94edc4ab 5027cp_parser_equality_expression (cp_parser* parser)
a723baf1 5028{
39b1af70 5029 static const cp_parser_token_tree_map map = {
a723baf1
MM
5030 { CPP_EQ_EQ, EQ_EXPR },
5031 { CPP_NOT_EQ, NE_EXPR },
5032 { CPP_EOF, ERROR_MARK }
5033 };
5034
5035 return cp_parser_binary_expression (parser,
5036 map,
5037 cp_parser_relational_expression);
5038}
5039
5040/* Parse an and-expression.
5041
5042 and-expression:
5043 equality-expression
5044 and-expression & equality-expression
5045
5046 Returns a representation of the expression. */
5047
5048static tree
94edc4ab 5049cp_parser_and_expression (cp_parser* parser)
a723baf1 5050{
39b1af70 5051 static const cp_parser_token_tree_map map = {
a723baf1
MM
5052 { CPP_AND, BIT_AND_EXPR },
5053 { CPP_EOF, ERROR_MARK }
5054 };
5055
5056 return cp_parser_binary_expression (parser,
5057 map,
5058 cp_parser_equality_expression);
5059}
5060
5061/* Parse an exclusive-or-expression.
5062
5063 exclusive-or-expression:
5064 and-expression
5065 exclusive-or-expression ^ and-expression
5066
5067 Returns a representation of the expression. */
5068
5069static tree
94edc4ab 5070cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 5071{
39b1af70 5072 static const cp_parser_token_tree_map map = {
a723baf1
MM
5073 { CPP_XOR, BIT_XOR_EXPR },
5074 { CPP_EOF, ERROR_MARK }
5075 };
5076
5077 return cp_parser_binary_expression (parser,
5078 map,
5079 cp_parser_and_expression);
5080}
5081
5082
5083/* Parse an inclusive-or-expression.
5084
5085 inclusive-or-expression:
5086 exclusive-or-expression
5087 inclusive-or-expression | exclusive-or-expression
5088
5089 Returns a representation of the expression. */
5090
5091static tree
94edc4ab 5092cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 5093{
39b1af70 5094 static const cp_parser_token_tree_map map = {
a723baf1
MM
5095 { CPP_OR, BIT_IOR_EXPR },
5096 { CPP_EOF, ERROR_MARK }
5097 };
5098
5099 return cp_parser_binary_expression (parser,
5100 map,
5101 cp_parser_exclusive_or_expression);
5102}
5103
5104/* Parse a logical-and-expression.
5105
5106 logical-and-expression:
5107 inclusive-or-expression
5108 logical-and-expression && inclusive-or-expression
5109
5110 Returns a representation of the expression. */
5111
5112static tree
94edc4ab 5113cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 5114{
39b1af70 5115 static const cp_parser_token_tree_map map = {
a723baf1
MM
5116 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5117 { CPP_EOF, ERROR_MARK }
5118 };
5119
5120 return cp_parser_binary_expression (parser,
5121 map,
5122 cp_parser_inclusive_or_expression);
5123}
5124
5125/* Parse a logical-or-expression.
5126
5127 logical-or-expression:
34cd5ae7 5128 logical-and-expression
a723baf1
MM
5129 logical-or-expression || logical-and-expression
5130
5131 Returns a representation of the expression. */
5132
5133static tree
94edc4ab 5134cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5135{
39b1af70 5136 static const cp_parser_token_tree_map map = {
a723baf1
MM
5137 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5138 { CPP_EOF, ERROR_MARK }
5139 };
5140
5141 return cp_parser_binary_expression (parser,
5142 map,
5143 cp_parser_logical_and_expression);
5144}
5145
a723baf1
MM
5146/* Parse the `? expression : assignment-expression' part of a
5147 conditional-expression. The LOGICAL_OR_EXPR is the
5148 logical-or-expression that started the conditional-expression.
5149 Returns a representation of the entire conditional-expression.
5150
39703eb9 5151 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5152
5153 ? expression : assignment-expression
21526606 5154
a723baf1 5155 GNU Extensions:
21526606 5156
a723baf1
MM
5157 ? : assignment-expression */
5158
5159static tree
94edc4ab 5160cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5161{
5162 tree expr;
5163 tree assignment_expr;
5164
5165 /* Consume the `?' token. */
5166 cp_lexer_consume_token (parser->lexer);
5167 if (cp_parser_allow_gnu_extensions_p (parser)
5168 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5169 /* Implicit true clause. */
5170 expr = NULL_TREE;
5171 else
5172 /* Parse the expression. */
5173 expr = cp_parser_expression (parser);
21526606 5174
a723baf1
MM
5175 /* The next token should be a `:'. */
5176 cp_parser_require (parser, CPP_COLON, "`:'");
5177 /* Parse the assignment-expression. */
5178 assignment_expr = cp_parser_assignment_expression (parser);
5179
5180 /* Build the conditional-expression. */
5181 return build_x_conditional_expr (logical_or_expr,
5182 expr,
5183 assignment_expr);
5184}
5185
5186/* Parse an assignment-expression.
5187
5188 assignment-expression:
5189 conditional-expression
5190 logical-or-expression assignment-operator assignment_expression
5191 throw-expression
5192
5193 Returns a representation for the expression. */
5194
5195static tree
94edc4ab 5196cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5197{
5198 tree expr;
5199
5200 /* If the next token is the `throw' keyword, then we're looking at
5201 a throw-expression. */
5202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5203 expr = cp_parser_throw_expression (parser);
5204 /* Otherwise, it must be that we are looking at a
5205 logical-or-expression. */
5206 else
5207 {
5208 /* Parse the logical-or-expression. */
5209 expr = cp_parser_logical_or_expression (parser);
5210 /* If the next token is a `?' then we're actually looking at a
5211 conditional-expression. */
5212 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5213 return cp_parser_question_colon_clause (parser, expr);
21526606 5214 else
a723baf1
MM
5215 {
5216 enum tree_code assignment_operator;
5217
5218 /* If it's an assignment-operator, we're using the second
5219 production. */
21526606 5220 assignment_operator
a723baf1
MM
5221 = cp_parser_assignment_operator_opt (parser);
5222 if (assignment_operator != ERROR_MARK)
5223 {
5224 tree rhs;
5225
5226 /* Parse the right-hand side of the assignment. */
5227 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5228 /* An assignment may not appear in a
5229 constant-expression. */
625cbf93
MM
5230 if (cp_parser_non_integral_constant_expression (parser,
5231 "an assignment"))
5232 return error_mark_node;
34cd5ae7 5233 /* Build the assignment expression. */
21526606
EC
5234 expr = build_x_modify_expr (expr,
5235 assignment_operator,
a723baf1
MM
5236 rhs);
5237 }
5238 }
5239 }
5240
5241 return expr;
5242}
5243
5244/* Parse an (optional) assignment-operator.
5245
21526606
EC
5246 assignment-operator: one of
5247 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5248
5249 GNU Extension:
21526606 5250
a723baf1
MM
5251 assignment-operator: one of
5252 <?= >?=
5253
5254 If the next token is an assignment operator, the corresponding tree
5255 code is returned, and the token is consumed. For example, for
5256 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5257 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5258 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5259 operator, ERROR_MARK is returned. */
5260
5261static enum tree_code
94edc4ab 5262cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5263{
5264 enum tree_code op;
5265 cp_token *token;
5266
5267 /* Peek at the next toen. */
5268 token = cp_lexer_peek_token (parser->lexer);
5269
5270 switch (token->type)
5271 {
5272 case CPP_EQ:
5273 op = NOP_EXPR;
5274 break;
5275
5276 case CPP_MULT_EQ:
5277 op = MULT_EXPR;
5278 break;
5279
5280 case CPP_DIV_EQ:
5281 op = TRUNC_DIV_EXPR;
5282 break;
5283
5284 case CPP_MOD_EQ:
5285 op = TRUNC_MOD_EXPR;
5286 break;
5287
5288 case CPP_PLUS_EQ:
5289 op = PLUS_EXPR;
5290 break;
5291
5292 case CPP_MINUS_EQ:
5293 op = MINUS_EXPR;
5294 break;
5295
5296 case CPP_RSHIFT_EQ:
5297 op = RSHIFT_EXPR;
5298 break;
5299
5300 case CPP_LSHIFT_EQ:
5301 op = LSHIFT_EXPR;
5302 break;
5303
5304 case CPP_AND_EQ:
5305 op = BIT_AND_EXPR;
5306 break;
5307
5308 case CPP_XOR_EQ:
5309 op = BIT_XOR_EXPR;
5310 break;
5311
5312 case CPP_OR_EQ:
5313 op = BIT_IOR_EXPR;
5314 break;
5315
5316 case CPP_MIN_EQ:
5317 op = MIN_EXPR;
5318 break;
5319
5320 case CPP_MAX_EQ:
5321 op = MAX_EXPR;
5322 break;
5323
21526606 5324 default:
a723baf1
MM
5325 /* Nothing else is an assignment operator. */
5326 op = ERROR_MARK;
5327 }
5328
5329 /* If it was an assignment operator, consume it. */
5330 if (op != ERROR_MARK)
5331 cp_lexer_consume_token (parser->lexer);
5332
5333 return op;
5334}
5335
5336/* Parse an expression.
5337
5338 expression:
5339 assignment-expression
5340 expression , assignment-expression
5341
5342 Returns a representation of the expression. */
5343
5344static tree
94edc4ab 5345cp_parser_expression (cp_parser* parser)
a723baf1
MM
5346{
5347 tree expression = NULL_TREE;
a723baf1
MM
5348
5349 while (true)
5350 {
5351 tree assignment_expression;
5352
5353 /* Parse the next assignment-expression. */
21526606 5354 assignment_expression
a723baf1
MM
5355 = cp_parser_assignment_expression (parser);
5356 /* If this is the first assignment-expression, we can just
5357 save it away. */
5358 if (!expression)
5359 expression = assignment_expression;
a723baf1 5360 else
d17811fd
MM
5361 expression = build_x_compound_expr (expression,
5362 assignment_expression);
a723baf1
MM
5363 /* If the next token is not a comma, then we are done with the
5364 expression. */
5365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5366 break;
5367 /* Consume the `,'. */
5368 cp_lexer_consume_token (parser->lexer);
14d22dd6 5369 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5370 if (cp_parser_non_integral_constant_expression (parser,
5371 "a comma operator"))
5372 expression = error_mark_node;
14d22dd6 5373 }
a723baf1
MM
5374
5375 return expression;
5376}
5377
21526606 5378/* Parse a constant-expression.
a723baf1
MM
5379
5380 constant-expression:
21526606 5381 conditional-expression
14d22dd6
MM
5382
5383 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5384 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5385 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5386 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5387
5388static tree
21526606 5389cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5390 bool allow_non_constant_p,
5391 bool *non_constant_p)
a723baf1 5392{
67c03833
JM
5393 bool saved_integral_constant_expression_p;
5394 bool saved_allow_non_integral_constant_expression_p;
5395 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5396 tree expression;
5397
5398 /* It might seem that we could simply parse the
5399 conditional-expression, and then check to see if it were
5400 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5401 one that the compiler can figure out is constant, possibly after
5402 doing some simplifications or optimizations. The standard has a
5403 precise definition of constant-expression, and we must honor
5404 that, even though it is somewhat more restrictive.
5405
5406 For example:
5407
5408 int i[(2, 3)];
5409
5410 is not a legal declaration, because `(2, 3)' is not a
5411 constant-expression. The `,' operator is forbidden in a
5412 constant-expression. However, GCC's constant-folding machinery
5413 will fold this operation to an INTEGER_CST for `3'. */
5414
14d22dd6 5415 /* Save the old settings. */
67c03833 5416 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5417 saved_allow_non_integral_constant_expression_p
67c03833
JM
5418 = parser->allow_non_integral_constant_expression_p;
5419 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5420 /* We are now parsing a constant-expression. */
67c03833
JM
5421 parser->integral_constant_expression_p = true;
5422 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5423 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5424 /* Although the grammar says "conditional-expression", we parse an
5425 "assignment-expression", which also permits "throw-expression"
5426 and the use of assignment operators. In the case that
5427 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5428 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5429 actually essential that we look for an assignment-expression.
5430 For example, cp_parser_initializer_clauses uses this function to
5431 determine whether a particular assignment-expression is in fact
5432 constant. */
5433 expression = cp_parser_assignment_expression (parser);
14d22dd6 5434 /* Restore the old settings. */
67c03833 5435 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
21526606 5436 parser->allow_non_integral_constant_expression_p
67c03833 5437 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5438 if (allow_non_constant_p)
67c03833
JM
5439 *non_constant_p = parser->non_integral_constant_expression_p;
5440 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5441
5442 return expression;
5443}
5444
5445/* Statements [gram.stmt.stmt] */
5446
21526606 5447/* Parse a statement.
a723baf1
MM
5448
5449 statement:
5450 labeled-statement
5451 expression-statement
5452 compound-statement
5453 selection-statement
5454 iteration-statement
5455 jump-statement
5456 declaration-statement
5457 try-block */
5458
5459static void
a5bcc582 5460cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5461{
5462 tree statement;
5463 cp_token *token;
5464 int statement_line_number;
5465
5466 /* There is no statement yet. */
5467 statement = NULL_TREE;
5468 /* Peek at the next token. */
5469 token = cp_lexer_peek_token (parser->lexer);
5470 /* Remember the line number of the first token in the statement. */
82a98427 5471 statement_line_number = token->location.line;
a723baf1
MM
5472 /* If this is a keyword, then that will often determine what kind of
5473 statement we have. */
5474 if (token->type == CPP_KEYWORD)
5475 {
5476 enum rid keyword = token->keyword;
5477
5478 switch (keyword)
5479 {
5480 case RID_CASE:
5481 case RID_DEFAULT:
a5bcc582
NS
5482 statement = cp_parser_labeled_statement (parser,
5483 in_statement_expr_p);
a723baf1
MM
5484 break;
5485
5486 case RID_IF:
5487 case RID_SWITCH:
5488 statement = cp_parser_selection_statement (parser);
5489 break;
5490
5491 case RID_WHILE:
5492 case RID_DO:
5493 case RID_FOR:
5494 statement = cp_parser_iteration_statement (parser);
5495 break;
5496
5497 case RID_BREAK:
5498 case RID_CONTINUE:
5499 case RID_RETURN:
5500 case RID_GOTO:
5501 statement = cp_parser_jump_statement (parser);
5502 break;
5503
5504 case RID_TRY:
5505 statement = cp_parser_try_block (parser);
5506 break;
5507
5508 default:
5509 /* It might be a keyword like `int' that can start a
5510 declaration-statement. */
5511 break;
5512 }
5513 }
5514 else if (token->type == CPP_NAME)
5515 {
5516 /* If the next token is a `:', then we are looking at a
5517 labeled-statement. */
5518 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5519 if (token->type == CPP_COLON)
a5bcc582 5520 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5521 }
5522 /* Anything that starts with a `{' must be a compound-statement. */
5523 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5524 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5525
5526 /* Everything else must be a declaration-statement or an
21526606 5527 expression-statement. Try for the declaration-statement
a723baf1
MM
5528 first, unless we are looking at a `;', in which case we know that
5529 we have an expression-statement. */
5530 if (!statement)
5531 {
5532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5533 {
5534 cp_parser_parse_tentatively (parser);
5535 /* Try to parse the declaration-statement. */
5536 cp_parser_declaration_statement (parser);
5537 /* If that worked, we're done. */
5538 if (cp_parser_parse_definitely (parser))
5539 return;
5540 }
5541 /* Look for an expression-statement instead. */
a5bcc582 5542 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5543 }
5544
5545 /* Set the line number for the statement. */
009ed910 5546 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5547 STMT_LINENO (statement) = statement_line_number;
5548}
5549
5550/* Parse a labeled-statement.
5551
5552 labeled-statement:
5553 identifier : statement
5554 case constant-expression : statement
98ce043b
MM
5555 default : statement
5556
5557 GNU Extension:
21526606 5558
98ce043b
MM
5559 labeled-statement:
5560 case constant-expression ... constant-expression : statement
a723baf1
MM
5561
5562 Returns the new CASE_LABEL, for a `case' or `default' label. For
5563 an ordinary label, returns a LABEL_STMT. */
5564
5565static tree
a5bcc582 5566cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5567{
5568 cp_token *token;
0e59b3fb 5569 tree statement = error_mark_node;
a723baf1
MM
5570
5571 /* The next token should be an identifier. */
5572 token = cp_lexer_peek_token (parser->lexer);
5573 if (token->type != CPP_NAME
5574 && token->type != CPP_KEYWORD)
5575 {
5576 cp_parser_error (parser, "expected labeled-statement");
5577 return error_mark_node;
5578 }
5579
5580 switch (token->keyword)
5581 {
5582 case RID_CASE:
5583 {
98ce043b
MM
5584 tree expr, expr_hi;
5585 cp_token *ellipsis;
a723baf1
MM
5586
5587 /* Consume the `case' token. */
5588 cp_lexer_consume_token (parser->lexer);
5589 /* Parse the constant-expression. */
21526606 5590 expr = cp_parser_constant_expression (parser,
d17811fd 5591 /*allow_non_constant_p=*/false,
14d22dd6 5592 NULL);
98ce043b
MM
5593
5594 ellipsis = cp_lexer_peek_token (parser->lexer);
5595 if (ellipsis->type == CPP_ELLIPSIS)
5596 {
5597 /* Consume the `...' token. */
5598 cp_lexer_consume_token (parser->lexer);
5599 expr_hi =
5600 cp_parser_constant_expression (parser,
5601 /*allow_non_constant_p=*/false,
5602 NULL);
5603 /* We don't need to emit warnings here, as the common code
5604 will do this for us. */
5605 }
5606 else
5607 expr_hi = NULL_TREE;
5608
0e59b3fb
MM
5609 if (!parser->in_switch_statement_p)
5610 error ("case label `%E' not within a switch statement", expr);
5611 else
98ce043b 5612 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
5613 }
5614 break;
5615
5616 case RID_DEFAULT:
5617 /* Consume the `default' token. */
5618 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5619 if (!parser->in_switch_statement_p)
5620 error ("case label not within a switch statement");
5621 else
5622 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5623 break;
5624
5625 default:
5626 /* Anything else must be an ordinary label. */
5627 statement = finish_label_stmt (cp_parser_identifier (parser));
5628 break;
5629 }
5630
5631 /* Require the `:' token. */
5632 cp_parser_require (parser, CPP_COLON, "`:'");
5633 /* Parse the labeled statement. */
a5bcc582 5634 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5635
5636 /* Return the label, in the case of a `case' or `default' label. */
5637 return statement;
5638}
5639
5640/* Parse an expression-statement.
5641
5642 expression-statement:
5643 expression [opt] ;
5644
5645 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5646 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5647 indicates whether this expression-statement is part of an
5648 expression statement. */
a723baf1
MM
5649
5650static tree
a5bcc582 5651cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5652{
a5bcc582 5653 tree statement = NULL_TREE;
a723baf1 5654
a5bcc582 5655 /* If the next token is a ';', then there is no expression
04c06002 5656 statement. */
a723baf1 5657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582 5658 statement = cp_parser_expression (parser);
21526606 5659
a723baf1 5660 /* Consume the final `;'. */
e0860732 5661 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5662
a5bcc582
NS
5663 if (in_statement_expr_p
5664 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5665 {
5666 /* This is the final expression statement of a statement
5667 expression. */
5668 statement = finish_stmt_expr_expr (statement);
5669 }
5670 else if (statement)
5671 statement = finish_expr_stmt (statement);
5672 else
5673 finish_stmt ();
21526606 5674
a723baf1
MM
5675 return statement;
5676}
5677
5678/* Parse a compound-statement.
5679
5680 compound-statement:
5681 { statement-seq [opt] }
21526606 5682
a723baf1
MM
5683 Returns a COMPOUND_STMT representing the statement. */
5684
5685static tree
a5bcc582 5686cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5687{
5688 tree compound_stmt;
5689
5690 /* Consume the `{'. */
5691 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5692 return error_mark_node;
5693 /* Begin the compound-statement. */
7a3397c7 5694 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5695 /* Parse an (optional) statement-seq. */
a5bcc582 5696 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5697 /* Finish the compound-statement. */
7a3397c7 5698 finish_compound_stmt (compound_stmt);
a723baf1
MM
5699 /* Consume the `}'. */
5700 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5701
5702 return compound_stmt;
5703}
5704
5705/* Parse an (optional) statement-seq.
5706
5707 statement-seq:
5708 statement
5709 statement-seq [opt] statement */
5710
5711static void
a5bcc582 5712cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5713{
5714 /* Scan statements until there aren't any more. */
5715 while (true)
5716 {
5717 /* If we're looking at a `}', then we've run out of statements. */
5718 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5719 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5720 break;
5721
5722 /* Parse the statement. */
a5bcc582 5723 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5724 }
5725}
5726
5727/* Parse a selection-statement.
5728
5729 selection-statement:
5730 if ( condition ) statement
5731 if ( condition ) statement else statement
21526606 5732 switch ( condition ) statement
a723baf1
MM
5733
5734 Returns the new IF_STMT or SWITCH_STMT. */
5735
5736static tree
94edc4ab 5737cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5738{
5739 cp_token *token;
5740 enum rid keyword;
5741
5742 /* Peek at the next token. */
5743 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5744
5745 /* See what kind of keyword it is. */
5746 keyword = token->keyword;
5747 switch (keyword)
5748 {
5749 case RID_IF:
5750 case RID_SWITCH:
5751 {
5752 tree statement;
5753 tree condition;
5754
5755 /* Look for the `('. */
5756 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5757 {
5758 cp_parser_skip_to_end_of_statement (parser);
5759 return error_mark_node;
5760 }
5761
5762 /* Begin the selection-statement. */
5763 if (keyword == RID_IF)
5764 statement = begin_if_stmt ();
5765 else
5766 statement = begin_switch_stmt ();
5767
5768 /* Parse the condition. */
5769 condition = cp_parser_condition (parser);
5770 /* Look for the `)'. */
5771 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
5772 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5773 /*consume_paren=*/true);
a723baf1
MM
5774
5775 if (keyword == RID_IF)
5776 {
5777 tree then_stmt;
5778
5779 /* Add the condition. */
5780 finish_if_stmt_cond (condition, statement);
5781
5782 /* Parse the then-clause. */
5783 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5784 finish_then_clause (statement);
5785
5786 /* If the next token is `else', parse the else-clause. */
5787 if (cp_lexer_next_token_is_keyword (parser->lexer,
5788 RID_ELSE))
5789 {
5790 tree else_stmt;
5791
5792 /* Consume the `else' keyword. */
5793 cp_lexer_consume_token (parser->lexer);
5794 /* Parse the else-clause. */
21526606 5795 else_stmt
a723baf1
MM
5796 = cp_parser_implicitly_scoped_statement (parser);
5797 finish_else_clause (statement);
5798 }
5799
5800 /* Now we're all done with the if-statement. */
5801 finish_if_stmt ();
5802 }
5803 else
5804 {
5805 tree body;
0e59b3fb 5806 bool in_switch_statement_p;
a723baf1
MM
5807
5808 /* Add the condition. */
5809 finish_switch_cond (condition, statement);
5810
5811 /* Parse the body of the switch-statement. */
0e59b3fb
MM
5812 in_switch_statement_p = parser->in_switch_statement_p;
5813 parser->in_switch_statement_p = true;
a723baf1 5814 body = cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5815 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
5816
5817 /* Now we're all done with the switch-statement. */
5818 finish_switch_stmt (statement);
5819 }
5820
5821 return statement;
5822 }
5823 break;
5824
5825 default:
5826 cp_parser_error (parser, "expected selection-statement");
5827 return error_mark_node;
5828 }
5829}
5830
21526606 5831/* Parse a condition.
a723baf1
MM
5832
5833 condition:
5834 expression
21526606 5835 type-specifier-seq declarator = assignment-expression
a723baf1
MM
5836
5837 GNU Extension:
21526606 5838
a723baf1 5839 condition:
21526606 5840 type-specifier-seq declarator asm-specification [opt]
a723baf1 5841 attributes [opt] = assignment-expression
21526606 5842
a723baf1
MM
5843 Returns the expression that should be tested. */
5844
5845static tree
94edc4ab 5846cp_parser_condition (cp_parser* parser)
a723baf1
MM
5847{
5848 tree type_specifiers;
5849 const char *saved_message;
5850
5851 /* Try the declaration first. */
5852 cp_parser_parse_tentatively (parser);
5853 /* New types are not allowed in the type-specifier-seq for a
5854 condition. */
5855 saved_message = parser->type_definition_forbidden_message;
5856 parser->type_definition_forbidden_message
5857 = "types may not be defined in conditions";
5858 /* Parse the type-specifier-seq. */
5859 type_specifiers = cp_parser_type_specifier_seq (parser);
5860 /* Restore the saved message. */
5861 parser->type_definition_forbidden_message = saved_message;
5862 /* If all is well, we might be looking at a declaration. */
5863 if (!cp_parser_error_occurred (parser))
5864 {
5865 tree decl;
5866 tree asm_specification;
5867 tree attributes;
5868 tree declarator;
5869 tree initializer = NULL_TREE;
21526606 5870
a723baf1 5871 /* Parse the declarator. */
62b8a44e 5872 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
5873 /*ctor_dtor_or_conv_p=*/NULL,
5874 /*parenthesized_p=*/NULL);
a723baf1
MM
5875 /* Parse the attributes. */
5876 attributes = cp_parser_attributes_opt (parser);
5877 /* Parse the asm-specification. */
5878 asm_specification = cp_parser_asm_specification_opt (parser);
5879 /* If the next token is not an `=', then we might still be
5880 looking at an expression. For example:
21526606 5881
a723baf1 5882 if (A(a).x)
21526606 5883
a723baf1
MM
5884 looks like a decl-specifier-seq and a declarator -- but then
5885 there is no `=', so this is an expression. */
5886 cp_parser_require (parser, CPP_EQ, "`='");
5887 /* If we did see an `=', then we are looking at a declaration
5888 for sure. */
5889 if (cp_parser_parse_definitely (parser))
5890 {
5891 /* Create the declaration. */
21526606 5892 decl = start_decl (declarator, type_specifiers,
a723baf1
MM
5893 /*initialized_p=*/true,
5894 attributes, /*prefix_attributes=*/NULL_TREE);
5895 /* Parse the assignment-expression. */
5896 initializer = cp_parser_assignment_expression (parser);
21526606 5897
a723baf1 5898 /* Process the initializer. */
21526606
EC
5899 cp_finish_decl (decl,
5900 initializer,
5901 asm_specification,
a723baf1 5902 LOOKUP_ONLYCONVERTING);
21526606 5903
a723baf1
MM
5904 return convert_from_reference (decl);
5905 }
5906 }
5907 /* If we didn't even get past the declarator successfully, we are
5908 definitely not looking at a declaration. */
5909 else
5910 cp_parser_abort_tentative_parse (parser);
5911
5912 /* Otherwise, we are looking at an expression. */
5913 return cp_parser_expression (parser);
5914}
5915
5916/* Parse an iteration-statement.
5917
5918 iteration-statement:
5919 while ( condition ) statement
5920 do statement while ( expression ) ;
5921 for ( for-init-statement condition [opt] ; expression [opt] )
5922 statement
5923
5924 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5925
5926static tree
94edc4ab 5927cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5928{
5929 cp_token *token;
5930 enum rid keyword;
5931 tree statement;
0e59b3fb
MM
5932 bool in_iteration_statement_p;
5933
a723baf1
MM
5934
5935 /* Peek at the next token. */
5936 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5937 if (!token)
5938 return error_mark_node;
5939
0e59b3fb 5940 /* Remember whether or not we are already within an iteration
21526606 5941 statement. */
0e59b3fb
MM
5942 in_iteration_statement_p = parser->in_iteration_statement_p;
5943
a723baf1
MM
5944 /* See what kind of keyword it is. */
5945 keyword = token->keyword;
5946 switch (keyword)
5947 {
5948 case RID_WHILE:
5949 {
5950 tree condition;
5951
5952 /* Begin the while-statement. */
5953 statement = begin_while_stmt ();
5954 /* Look for the `('. */
5955 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5956 /* Parse the condition. */
5957 condition = cp_parser_condition (parser);
5958 finish_while_stmt_cond (condition, statement);
5959 /* Look for the `)'. */
5960 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5961 /* Parse the dependent statement. */
0e59b3fb 5962 parser->in_iteration_statement_p = true;
a723baf1 5963 cp_parser_already_scoped_statement (parser);
0e59b3fb 5964 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5965 /* We're done with the while-statement. */
5966 finish_while_stmt (statement);
5967 }
5968 break;
5969
5970 case RID_DO:
5971 {
5972 tree expression;
5973
5974 /* Begin the do-statement. */
5975 statement = begin_do_stmt ();
5976 /* Parse the body of the do-statement. */
0e59b3fb 5977 parser->in_iteration_statement_p = true;
a723baf1 5978 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5979 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5980 finish_do_body (statement);
5981 /* Look for the `while' keyword. */
5982 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5983 /* Look for the `('. */
5984 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5985 /* Parse the expression. */
5986 expression = cp_parser_expression (parser);
5987 /* We're done with the do-statement. */
5988 finish_do_stmt (expression, statement);
5989 /* Look for the `)'. */
5990 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5991 /* Look for the `;'. */
5992 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5993 }
5994 break;
5995
5996 case RID_FOR:
5997 {
5998 tree condition = NULL_TREE;
5999 tree expression = NULL_TREE;
6000
6001 /* Begin the for-statement. */
6002 statement = begin_for_stmt ();
6003 /* Look for the `('. */
6004 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6005 /* Parse the initialization. */
6006 cp_parser_for_init_statement (parser);
6007 finish_for_init_stmt (statement);
6008
6009 /* If there's a condition, process it. */
6010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6011 condition = cp_parser_condition (parser);
6012 finish_for_cond (condition, statement);
6013 /* Look for the `;'. */
6014 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6015
6016 /* If there's an expression, process it. */
6017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6018 expression = cp_parser_expression (parser);
6019 finish_for_expr (expression, statement);
6020 /* Look for the `)'. */
6021 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6022
6023 /* Parse the body of the for-statement. */
0e59b3fb 6024 parser->in_iteration_statement_p = true;
a723baf1 6025 cp_parser_already_scoped_statement (parser);
0e59b3fb 6026 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6027
6028 /* We're done with the for-statement. */
6029 finish_for_stmt (statement);
6030 }
6031 break;
6032
6033 default:
6034 cp_parser_error (parser, "expected iteration-statement");
6035 statement = error_mark_node;
6036 break;
6037 }
6038
6039 return statement;
6040}
6041
6042/* Parse a for-init-statement.
6043
6044 for-init-statement:
6045 expression-statement
6046 simple-declaration */
6047
6048static void
94edc4ab 6049cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6050{
6051 /* If the next token is a `;', then we have an empty
34cd5ae7 6052 expression-statement. Grammatically, this is also a
a723baf1
MM
6053 simple-declaration, but an invalid one, because it does not
6054 declare anything. Therefore, if we did not handle this case
6055 specially, we would issue an error message about an invalid
6056 declaration. */
6057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6058 {
6059 /* We're going to speculatively look for a declaration, falling back
6060 to an expression, if necessary. */
6061 cp_parser_parse_tentatively (parser);
6062 /* Parse the declaration. */
6063 cp_parser_simple_declaration (parser,
6064 /*function_definition_allowed_p=*/false);
6065 /* If the tentative parse failed, then we shall need to look for an
6066 expression-statement. */
6067 if (cp_parser_parse_definitely (parser))
6068 return;
6069 }
6070
a5bcc582 6071 cp_parser_expression_statement (parser, false);
a723baf1
MM
6072}
6073
6074/* Parse a jump-statement.
6075
6076 jump-statement:
6077 break ;
6078 continue ;
6079 return expression [opt] ;
21526606 6080 goto identifier ;
a723baf1
MM
6081
6082 GNU extension:
6083
6084 jump-statement:
6085 goto * expression ;
6086
6087 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6088 GOTO_STMT. */
6089
6090static tree
94edc4ab 6091cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6092{
6093 tree statement = error_mark_node;
6094 cp_token *token;
6095 enum rid keyword;
6096
6097 /* Peek at the next token. */
6098 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6099 if (!token)
6100 return error_mark_node;
6101
6102 /* See what kind of keyword it is. */
6103 keyword = token->keyword;
6104 switch (keyword)
6105 {
6106 case RID_BREAK:
0e59b3fb
MM
6107 if (!parser->in_switch_statement_p
6108 && !parser->in_iteration_statement_p)
6109 {
6110 error ("break statement not within loop or switch");
6111 statement = error_mark_node;
6112 }
6113 else
6114 statement = finish_break_stmt ();
a723baf1
MM
6115 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6116 break;
6117
6118 case RID_CONTINUE:
0e59b3fb
MM
6119 if (!parser->in_iteration_statement_p)
6120 {
6121 error ("continue statement not within a loop");
6122 statement = error_mark_node;
6123 }
6124 else
6125 statement = finish_continue_stmt ();
a723baf1
MM
6126 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6127 break;
6128
6129 case RID_RETURN:
6130 {
6131 tree expr;
6132
21526606 6133 /* If the next token is a `;', then there is no
a723baf1
MM
6134 expression. */
6135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6136 expr = cp_parser_expression (parser);
6137 else
6138 expr = NULL_TREE;
6139 /* Build the return-statement. */
6140 statement = finish_return_stmt (expr);
6141 /* Look for the final `;'. */
6142 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6143 }
6144 break;
6145
6146 case RID_GOTO:
6147 /* Create the goto-statement. */
6148 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6149 {
6150 /* Issue a warning about this use of a GNU extension. */
6151 if (pedantic)
6152 pedwarn ("ISO C++ forbids computed gotos");
6153 /* Consume the '*' token. */
6154 cp_lexer_consume_token (parser->lexer);
6155 /* Parse the dependent expression. */
6156 finish_goto_stmt (cp_parser_expression (parser));
6157 }
6158 else
6159 finish_goto_stmt (cp_parser_identifier (parser));
6160 /* Look for the final `;'. */
6161 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6162 break;
6163
6164 default:
6165 cp_parser_error (parser, "expected jump-statement");
6166 break;
6167 }
6168
6169 return statement;
6170}
6171
6172/* Parse a declaration-statement.
6173
6174 declaration-statement:
6175 block-declaration */
6176
6177static void
94edc4ab 6178cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6179{
6180 /* Parse the block-declaration. */
6181 cp_parser_block_declaration (parser, /*statement_p=*/true);
6182
6183 /* Finish off the statement. */
6184 finish_stmt ();
6185}
6186
6187/* Some dependent statements (like `if (cond) statement'), are
6188 implicitly in their own scope. In other words, if the statement is
6189 a single statement (as opposed to a compound-statement), it is
6190 none-the-less treated as if it were enclosed in braces. Any
6191 declarations appearing in the dependent statement are out of scope
6192 after control passes that point. This function parses a statement,
6193 but ensures that is in its own scope, even if it is not a
21526606 6194 compound-statement.
a723baf1
MM
6195
6196 Returns the new statement. */
6197
6198static tree
94edc4ab 6199cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6200{
6201 tree statement;
6202
6203 /* If the token is not a `{', then we must take special action. */
6204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6205 {
6206 /* Create a compound-statement. */
7a3397c7 6207 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 6208 /* Parse the dependent-statement. */
a5bcc582 6209 cp_parser_statement (parser, false);
a723baf1 6210 /* Finish the dummy compound-statement. */
7a3397c7 6211 finish_compound_stmt (statement);
a723baf1
MM
6212 }
6213 /* Otherwise, we simply parse the statement directly. */
6214 else
a5bcc582 6215 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
6216
6217 /* Return the statement. */
6218 return statement;
6219}
6220
6221/* For some dependent statements (like `while (cond) statement'), we
6222 have already created a scope. Therefore, even if the dependent
6223 statement is a compound-statement, we do not want to create another
6224 scope. */
6225
6226static void
94edc4ab 6227cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6228{
6229 /* If the token is not a `{', then we must take special action. */
6230 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6231 {
6232 tree statement;
6233
6234 /* Create a compound-statement. */
7a3397c7 6235 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 6236 /* Parse the dependent-statement. */
a5bcc582 6237 cp_parser_statement (parser, false);
a723baf1 6238 /* Finish the dummy compound-statement. */
7a3397c7 6239 finish_compound_stmt (statement);
a723baf1
MM
6240 }
6241 /* Otherwise, we simply parse the statement directly. */
6242 else
a5bcc582 6243 cp_parser_statement (parser, false);
a723baf1
MM
6244}
6245
6246/* Declarations [gram.dcl.dcl] */
6247
6248/* Parse an optional declaration-sequence.
6249
6250 declaration-seq:
6251 declaration
6252 declaration-seq declaration */
6253
6254static void
94edc4ab 6255cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6256{
6257 while (true)
6258 {
6259 cp_token *token;
6260
6261 token = cp_lexer_peek_token (parser->lexer);
6262
6263 if (token->type == CPP_CLOSE_BRACE
6264 || token->type == CPP_EOF)
6265 break;
6266
21526606 6267 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6268 {
6269 /* A declaration consisting of a single semicolon is
6270 invalid. Allow it unless we're being pedantic. */
499b568f 6271 if (pedantic && !in_system_header)
a723baf1
MM
6272 pedwarn ("extra `;'");
6273 cp_lexer_consume_token (parser->lexer);
6274 continue;
6275 }
6276
c838d82f 6277 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6278 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6279 while (pending_lang_change > 0)
6280 {
6281 push_lang_context (lang_name_c);
6282 --pending_lang_change;
6283 }
6284 while (pending_lang_change < 0)
6285 {
6286 pop_lang_context ();
6287 ++pending_lang_change;
6288 }
6289
6290 /* Parse the declaration itself. */
a723baf1
MM
6291 cp_parser_declaration (parser);
6292 }
6293}
6294
6295/* Parse a declaration.
6296
6297 declaration:
6298 block-declaration
6299 function-definition
6300 template-declaration
6301 explicit-instantiation
6302 explicit-specialization
6303 linkage-specification
21526606 6304 namespace-definition
1092805d
MM
6305
6306 GNU extension:
6307
6308 declaration:
6309 __extension__ declaration */
a723baf1
MM
6310
6311static void
94edc4ab 6312cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6313{
6314 cp_token token1;
6315 cp_token token2;
1092805d
MM
6316 int saved_pedantic;
6317
21526606
EC
6318 /* Set this here since we can be called after
6319 pushing the linkage specification. */
6320 c_lex_string_translate = true;
6321
1092805d
MM
6322 /* Check for the `__extension__' keyword. */
6323 if (cp_parser_extension_opt (parser, &saved_pedantic))
6324 {
6325 /* Parse the qualified declaration. */
6326 cp_parser_declaration (parser);
6327 /* Restore the PEDANTIC flag. */
6328 pedantic = saved_pedantic;
6329
6330 return;
6331 }
a723baf1
MM
6332
6333 /* Try to figure out what kind of declaration is present. */
6334 token1 = *cp_lexer_peek_token (parser->lexer);
21526606
EC
6335
6336 /* Don't translate the CPP_STRING in extern "C". */
6337 if (token1.keyword == RID_EXTERN)
6338 c_lex_string_translate = false;
6339
a723baf1
MM
6340 if (token1.type != CPP_EOF)
6341 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6342
6343 /* If the next token is `extern' and the following token is a string
6344 literal, then we have a linkage specification. */
6345 if (token1.keyword == RID_EXTERN
6346 && cp_parser_is_string_literal (&token2))
6347 cp_parser_linkage_specification (parser);
6348 /* If the next token is `template', then we have either a template
6349 declaration, an explicit instantiation, or an explicit
6350 specialization. */
6351 else if (token1.keyword == RID_TEMPLATE)
6352 {
6353 /* `template <>' indicates a template specialization. */
6354 if (token2.type == CPP_LESS
6355 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6356 cp_parser_explicit_specialization (parser);
6357 /* `template <' indicates a template declaration. */
6358 else if (token2.type == CPP_LESS)
6359 cp_parser_template_declaration (parser, /*member_p=*/false);
6360 /* Anything else must be an explicit instantiation. */
6361 else
6362 cp_parser_explicit_instantiation (parser);
6363 }
6364 /* If the next token is `export', then we have a template
6365 declaration. */
6366 else if (token1.keyword == RID_EXPORT)
6367 cp_parser_template_declaration (parser, /*member_p=*/false);
6368 /* If the next token is `extern', 'static' or 'inline' and the one
6369 after that is `template', we have a GNU extended explicit
6370 instantiation directive. */
6371 else if (cp_parser_allow_gnu_extensions_p (parser)
6372 && (token1.keyword == RID_EXTERN
6373 || token1.keyword == RID_STATIC
6374 || token1.keyword == RID_INLINE)
6375 && token2.keyword == RID_TEMPLATE)
6376 cp_parser_explicit_instantiation (parser);
6377 /* If the next token is `namespace', check for a named or unnamed
6378 namespace definition. */
6379 else if (token1.keyword == RID_NAMESPACE
6380 && (/* A named namespace definition. */
6381 (token2.type == CPP_NAME
21526606 6382 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6383 == CPP_OPEN_BRACE))
6384 /* An unnamed namespace definition. */
6385 || token2.type == CPP_OPEN_BRACE))
6386 cp_parser_namespace_definition (parser);
6387 /* We must have either a block declaration or a function
6388 definition. */
6389 else
6390 /* Try to parse a block-declaration, or a function-definition. */
6391 cp_parser_block_declaration (parser, /*statement_p=*/false);
21526606
EC
6392
6393 c_lex_string_translate = true;
a723baf1
MM
6394}
6395
21526606 6396/* Parse a block-declaration.
a723baf1
MM
6397
6398 block-declaration:
6399 simple-declaration
6400 asm-definition
6401 namespace-alias-definition
6402 using-declaration
21526606 6403 using-directive
a723baf1
MM
6404
6405 GNU Extension:
6406
6407 block-declaration:
21526606 6408 __extension__ block-declaration
a723baf1
MM
6409 label-declaration
6410
34cd5ae7 6411 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6412 part of a declaration-statement. */
6413
6414static void
21526606 6415cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6416 bool statement_p)
6417{
6418 cp_token *token1;
6419 int saved_pedantic;
6420
6421 /* Check for the `__extension__' keyword. */
6422 if (cp_parser_extension_opt (parser, &saved_pedantic))
6423 {
6424 /* Parse the qualified declaration. */
6425 cp_parser_block_declaration (parser, statement_p);
6426 /* Restore the PEDANTIC flag. */
6427 pedantic = saved_pedantic;
6428
6429 return;
6430 }
6431
6432 /* Peek at the next token to figure out which kind of declaration is
6433 present. */
6434 token1 = cp_lexer_peek_token (parser->lexer);
6435
6436 /* If the next keyword is `asm', we have an asm-definition. */
6437 if (token1->keyword == RID_ASM)
6438 {
6439 if (statement_p)
6440 cp_parser_commit_to_tentative_parse (parser);
6441 cp_parser_asm_definition (parser);
6442 }
6443 /* If the next keyword is `namespace', we have a
6444 namespace-alias-definition. */
6445 else if (token1->keyword == RID_NAMESPACE)
6446 cp_parser_namespace_alias_definition (parser);
6447 /* If the next keyword is `using', we have either a
6448 using-declaration or a using-directive. */
6449 else if (token1->keyword == RID_USING)
6450 {
6451 cp_token *token2;
6452
6453 if (statement_p)
6454 cp_parser_commit_to_tentative_parse (parser);
6455 /* If the token after `using' is `namespace', then we have a
6456 using-directive. */
6457 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6458 if (token2->keyword == RID_NAMESPACE)
6459 cp_parser_using_directive (parser);
6460 /* Otherwise, it's a using-declaration. */
6461 else
6462 cp_parser_using_declaration (parser);
6463 }
6464 /* If the next keyword is `__label__' we have a label declaration. */
6465 else if (token1->keyword == RID_LABEL)
6466 {
6467 if (statement_p)
6468 cp_parser_commit_to_tentative_parse (parser);
6469 cp_parser_label_declaration (parser);
6470 }
6471 /* Anything else must be a simple-declaration. */
6472 else
6473 cp_parser_simple_declaration (parser, !statement_p);
6474}
6475
6476/* Parse a simple-declaration.
6477
6478 simple-declaration:
21526606 6479 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6480
6481 init-declarator-list:
6482 init-declarator
21526606 6483 init-declarator-list , init-declarator
a723baf1 6484
34cd5ae7 6485 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6486 function-definition as a simple-declaration. */
a723baf1
MM
6487
6488static void
21526606 6489cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6490 bool function_definition_allowed_p)
a723baf1
MM
6491{
6492 tree decl_specifiers;
6493 tree attributes;
560ad596 6494 int declares_class_or_enum;
a723baf1
MM
6495 bool saw_declarator;
6496
6497 /* Defer access checks until we know what is being declared; the
6498 checks for names appearing in the decl-specifier-seq should be
6499 done as if we were in the scope of the thing being declared. */
8d241e0b 6500 push_deferring_access_checks (dk_deferred);
cf22909c 6501
a723baf1
MM
6502 /* Parse the decl-specifier-seq. We have to keep track of whether
6503 or not the decl-specifier-seq declares a named class or
6504 enumeration type, since that is the only case in which the
21526606 6505 init-declarator-list is allowed to be empty.
a723baf1
MM
6506
6507 [dcl.dcl]
6508
6509 In a simple-declaration, the optional init-declarator-list can be
6510 omitted only when declaring a class or enumeration, that is when
6511 the decl-specifier-seq contains either a class-specifier, an
6512 elaborated-type-specifier, or an enum-specifier. */
6513 decl_specifiers
21526606 6514 = cp_parser_decl_specifier_seq (parser,
a723baf1
MM
6515 CP_PARSER_FLAGS_OPTIONAL,
6516 &attributes,
6517 &declares_class_or_enum);
6518 /* We no longer need to defer access checks. */
cf22909c 6519 stop_deferring_access_checks ();
24c0ef37 6520
39703eb9
MM
6521 /* In a block scope, a valid declaration must always have a
6522 decl-specifier-seq. By not trying to parse declarators, we can
6523 resolve the declaration/expression ambiguity more quickly. */
6524 if (!function_definition_allowed_p && !decl_specifiers)
6525 {
6526 cp_parser_error (parser, "expected declaration");
6527 goto done;
6528 }
6529
8fbc5ae7
MM
6530 /* If the next two tokens are both identifiers, the code is
6531 erroneous. The usual cause of this situation is code like:
6532
6533 T t;
6534
6535 where "T" should name a type -- but does not. */
2097b5f2 6536 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6537 {
8d241e0b 6538 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6539 looking at a declaration. */
6540 cp_parser_commit_to_tentative_parse (parser);
6541 /* Give up. */
39703eb9 6542 goto done;
8fbc5ae7
MM
6543 }
6544
a723baf1
MM
6545 /* Keep going until we hit the `;' at the end of the simple
6546 declaration. */
6547 saw_declarator = false;
21526606 6548 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6549 CPP_SEMICOLON))
6550 {
6551 cp_token *token;
6552 bool function_definition_p;
560ad596 6553 tree decl;
a723baf1
MM
6554
6555 saw_declarator = true;
6556 /* Parse the init-declarator. */
560ad596
MM
6557 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6558 function_definition_allowed_p,
6559 /*member_p=*/false,
6560 declares_class_or_enum,
6561 &function_definition_p);
1fb3244a
MM
6562 /* If an error occurred while parsing tentatively, exit quickly.
6563 (That usually happens when in the body of a function; each
6564 statement is treated as a declaration-statement until proven
6565 otherwise.) */
6566 if (cp_parser_error_occurred (parser))
39703eb9 6567 goto done;
a723baf1
MM
6568 /* Handle function definitions specially. */
6569 if (function_definition_p)
6570 {
6571 /* If the next token is a `,', then we are probably
6572 processing something like:
6573
6574 void f() {}, *p;
6575
6576 which is erroneous. */
6577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6578 error ("mixing declarations and function-definitions is forbidden");
6579 /* Otherwise, we're done with the list of declarators. */
6580 else
24c0ef37 6581 {
cf22909c 6582 pop_deferring_access_checks ();
24c0ef37
GS
6583 return;
6584 }
a723baf1
MM
6585 }
6586 /* The next token should be either a `,' or a `;'. */
6587 token = cp_lexer_peek_token (parser->lexer);
6588 /* If it's a `,', there are more declarators to come. */
6589 if (token->type == CPP_COMMA)
6590 cp_lexer_consume_token (parser->lexer);
6591 /* If it's a `;', we are done. */
6592 else if (token->type == CPP_SEMICOLON)
6593 break;
6594 /* Anything else is an error. */
6595 else
6596 {
6597 cp_parser_error (parser, "expected `,' or `;'");
6598 /* Skip tokens until we reach the end of the statement. */
6599 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
6600 /* If the next token is now a `;', consume it. */
6601 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6602 cp_lexer_consume_token (parser->lexer);
39703eb9 6603 goto done;
a723baf1
MM
6604 }
6605 /* After the first time around, a function-definition is not
6606 allowed -- even if it was OK at first. For example:
6607
6608 int i, f() {}
6609
6610 is not valid. */
6611 function_definition_allowed_p = false;
6612 }
6613
6614 /* Issue an error message if no declarators are present, and the
6615 decl-specifier-seq does not itself declare a class or
6616 enumeration. */
6617 if (!saw_declarator)
6618 {
6619 if (cp_parser_declares_only_class_p (parser))
6620 shadow_tag (decl_specifiers);
6621 /* Perform any deferred access checks. */
cf22909c 6622 perform_deferred_access_checks ();
a723baf1
MM
6623 }
6624
6625 /* Consume the `;'. */
6626 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6627
39703eb9
MM
6628 done:
6629 pop_deferring_access_checks ();
a723baf1
MM
6630}
6631
6632/* Parse a decl-specifier-seq.
6633
6634 decl-specifier-seq:
6635 decl-specifier-seq [opt] decl-specifier
6636
6637 decl-specifier:
6638 storage-class-specifier
6639 type-specifier
6640 function-specifier
6641 friend
21526606 6642 typedef
a723baf1
MM
6643
6644 GNU Extension:
6645
6646 decl-specifier-seq:
6647 decl-specifier-seq [opt] attributes
6648
6649 Returns a TREE_LIST, giving the decl-specifiers in the order they
6650 appear in the source code. The TREE_VALUE of each node is the
6651 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6652 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
21526606 6653 representation of a type-specifier, see cp_parser_type_specifier.
a723baf1
MM
6654
6655 If there are attributes, they will be stored in *ATTRIBUTES,
21526606 6656 represented as described above cp_parser_attributes.
a723baf1
MM
6657
6658 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6659 appears, and the entity that will be a friend is not going to be a
6660 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6661 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
21526606 6662 friendship is granted might not be a class.
560ad596
MM
6663
6664 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 6665 flags:
560ad596
MM
6666
6667 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 6668 (i.e., a type declaration)
560ad596 6669 2: one of the decl-specifiers is an enum-specifier or a
543ca912 6670 class-specifier (i.e., a type definition)
560ad596
MM
6671
6672 */
a723baf1
MM
6673
6674static tree
21526606
EC
6675cp_parser_decl_specifier_seq (cp_parser* parser,
6676 cp_parser_flags flags,
94edc4ab 6677 tree* attributes,
560ad596 6678 int* declares_class_or_enum)
a723baf1
MM
6679{
6680 tree decl_specs = NULL_TREE;
6681 bool friend_p = false;
f2ce60b8 6682 bool constructor_possible_p = !parser->in_declarator_p;
21526606 6683
a723baf1 6684 /* Assume no class or enumeration type is declared. */
560ad596 6685 *declares_class_or_enum = 0;
a723baf1
MM
6686
6687 /* Assume there are no attributes. */
6688 *attributes = NULL_TREE;
6689
6690 /* Keep reading specifiers until there are no more to read. */
6691 while (true)
6692 {
6693 tree decl_spec = NULL_TREE;
6694 bool constructor_p;
6695 cp_token *token;
6696
6697 /* Peek at the next token. */
6698 token = cp_lexer_peek_token (parser->lexer);
6699 /* Handle attributes. */
6700 if (token->keyword == RID_ATTRIBUTE)
6701 {
6702 /* Parse the attributes. */
6703 decl_spec = cp_parser_attributes_opt (parser);
6704 /* Add them to the list. */
6705 *attributes = chainon (*attributes, decl_spec);
6706 continue;
6707 }
6708 /* If the next token is an appropriate keyword, we can simply
6709 add it to the list. */
6710 switch (token->keyword)
6711 {
6712 case RID_FRIEND:
6713 /* decl-specifier:
6714 friend */
1918facf
SB
6715 if (friend_p)
6716 error ("duplicate `friend'");
6717 else
6718 friend_p = true;
a723baf1
MM
6719 /* The representation of the specifier is simply the
6720 appropriate TREE_IDENTIFIER node. */
6721 decl_spec = token->value;
6722 /* Consume the token. */
6723 cp_lexer_consume_token (parser->lexer);
6724 break;
6725
6726 /* function-specifier:
6727 inline
6728 virtual
6729 explicit */
6730 case RID_INLINE:
6731 case RID_VIRTUAL:
6732 case RID_EXPLICIT:
6733 decl_spec = cp_parser_function_specifier_opt (parser);
6734 break;
21526606 6735
a723baf1
MM
6736 /* decl-specifier:
6737 typedef */
6738 case RID_TYPEDEF:
6739 /* The representation of the specifier is simply the
6740 appropriate TREE_IDENTIFIER node. */
6741 decl_spec = token->value;
6742 /* Consume the token. */
6743 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6744 /* A constructor declarator cannot appear in a typedef. */
6745 constructor_possible_p = false;
c006d942
MM
6746 /* The "typedef" keyword can only occur in a declaration; we
6747 may as well commit at this point. */
6748 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6749 break;
6750
6751 /* storage-class-specifier:
6752 auto
6753 register
6754 static
6755 extern
21526606 6756 mutable
a723baf1
MM
6757
6758 GNU Extension:
6759 thread */
6760 case RID_AUTO:
6761 case RID_REGISTER:
6762 case RID_STATIC:
6763 case RID_EXTERN:
6764 case RID_MUTABLE:
6765 case RID_THREAD:
6766 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6767 break;
21526606 6768
a723baf1
MM
6769 default:
6770 break;
6771 }
6772
6773 /* Constructors are a special case. The `S' in `S()' is not a
6774 decl-specifier; it is the beginning of the declarator. */
21526606 6775 constructor_p = (!decl_spec
2050a1bb 6776 && constructor_possible_p
a723baf1
MM
6777 && cp_parser_constructor_declarator_p (parser,
6778 friend_p));
6779
6780 /* If we don't have a DECL_SPEC yet, then we must be looking at
6781 a type-specifier. */
6782 if (!decl_spec && !constructor_p)
6783 {
560ad596 6784 int decl_spec_declares_class_or_enum;
a723baf1
MM
6785 bool is_cv_qualifier;
6786
6787 decl_spec
6788 = cp_parser_type_specifier (parser, flags,
6789 friend_p,
6790 /*is_declaration=*/true,
6791 &decl_spec_declares_class_or_enum,
6792 &is_cv_qualifier);
6793
6794 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6795
6796 /* If this type-specifier referenced a user-defined type
6797 (a typedef, class-name, etc.), then we can't allow any
6798 more such type-specifiers henceforth.
6799
6800 [dcl.spec]
6801
6802 The longest sequence of decl-specifiers that could
6803 possibly be a type name is taken as the
6804 decl-specifier-seq of a declaration. The sequence shall
6805 be self-consistent as described below.
6806
6807 [dcl.type]
6808
6809 As a general rule, at most one type-specifier is allowed
6810 in the complete decl-specifier-seq of a declaration. The
6811 only exceptions are the following:
6812
6813 -- const or volatile can be combined with any other
21526606 6814 type-specifier.
a723baf1
MM
6815
6816 -- signed or unsigned can be combined with char, long,
6817 short, or int.
6818
6819 -- ..
6820
6821 Example:
6822
6823 typedef char* Pc;
6824 void g (const int Pc);
6825
6826 Here, Pc is *not* part of the decl-specifier seq; it's
6827 the declarator. Therefore, once we see a type-specifier
6828 (other than a cv-qualifier), we forbid any additional
6829 user-defined types. We *do* still allow things like `int
6830 int' to be considered a decl-specifier-seq, and issue the
6831 error message later. */
6832 if (decl_spec && !is_cv_qualifier)
6833 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6834 /* A constructor declarator cannot follow a type-specifier. */
6835 if (decl_spec)
6836 constructor_possible_p = false;
a723baf1
MM
6837 }
6838
6839 /* If we still do not have a DECL_SPEC, then there are no more
6840 decl-specifiers. */
6841 if (!decl_spec)
6842 {
6843 /* Issue an error message, unless the entire construct was
6844 optional. */
6845 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6846 {
6847 cp_parser_error (parser, "expected decl specifier");
6848 return error_mark_node;
6849 }
6850
6851 break;
6852 }
6853
6854 /* Add the DECL_SPEC to the list of specifiers. */
e90c7b84
ILT
6855 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6856 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
a723baf1
MM
6857
6858 /* After we see one decl-specifier, further decl-specifiers are
6859 always optional. */
6860 flags |= CP_PARSER_FLAGS_OPTIONAL;
6861 }
6862
0426c4ca
SB
6863 /* Don't allow a friend specifier with a class definition. */
6864 if (friend_p && (*declares_class_or_enum & 2))
6865 error ("class definition may not be declared a friend");
6866
a723baf1
MM
6867 /* We have built up the DECL_SPECS in reverse order. Return them in
6868 the correct order. */
6869 return nreverse (decl_specs);
6870}
6871
21526606 6872/* Parse an (optional) storage-class-specifier.
a723baf1
MM
6873
6874 storage-class-specifier:
6875 auto
6876 register
6877 static
6878 extern
21526606 6879 mutable
a723baf1
MM
6880
6881 GNU Extension:
6882
6883 storage-class-specifier:
6884 thread
6885
6886 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 6887
a723baf1 6888static tree
94edc4ab 6889cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6890{
6891 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6892 {
6893 case RID_AUTO:
6894 case RID_REGISTER:
6895 case RID_STATIC:
6896 case RID_EXTERN:
6897 case RID_MUTABLE:
6898 case RID_THREAD:
6899 /* Consume the token. */
6900 return cp_lexer_consume_token (parser->lexer)->value;
6901
6902 default:
6903 return NULL_TREE;
6904 }
6905}
6906
21526606 6907/* Parse an (optional) function-specifier.
a723baf1
MM
6908
6909 function-specifier:
6910 inline
6911 virtual
6912 explicit
6913
6914 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 6915
a723baf1 6916static tree
94edc4ab 6917cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6918{
6919 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6920 {
6921 case RID_INLINE:
6922 case RID_VIRTUAL:
6923 case RID_EXPLICIT:
6924 /* Consume the token. */
6925 return cp_lexer_consume_token (parser->lexer)->value;
6926
6927 default:
6928 return NULL_TREE;
6929 }
6930}
6931
6932/* Parse a linkage-specification.
6933
6934 linkage-specification:
6935 extern string-literal { declaration-seq [opt] }
6936 extern string-literal declaration */
6937
6938static void
94edc4ab 6939cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6940{
6941 cp_token *token;
6942 tree linkage;
6943
6944 /* Look for the `extern' keyword. */
6945 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6946
6947 /* Peek at the next token. */
6948 token = cp_lexer_peek_token (parser->lexer);
6949 /* If it's not a string-literal, then there's a problem. */
6950 if (!cp_parser_is_string_literal (token))
6951 {
6952 cp_parser_error (parser, "expected language-name");
6953 return;
6954 }
6955 /* Consume the token. */
6956 cp_lexer_consume_token (parser->lexer);
6957
6958 /* Transform the literal into an identifier. If the literal is a
6959 wide-character string, or contains embedded NULs, then we can't
6960 handle it as the user wants. */
6961 if (token->type == CPP_WSTRING
6962 || (strlen (TREE_STRING_POINTER (token->value))
6963 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6964 {
6965 cp_parser_error (parser, "invalid linkage-specification");
6966 /* Assume C++ linkage. */
6967 linkage = get_identifier ("c++");
6968 }
6969 /* If it's a simple string constant, things are easier. */
6970 else
6971 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6972
6973 /* We're now using the new linkage. */
6974 push_lang_context (linkage);
6975
6976 /* If the next token is a `{', then we're using the first
6977 production. */
6978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6979 {
6980 /* Consume the `{' token. */
6981 cp_lexer_consume_token (parser->lexer);
6982 /* Parse the declarations. */
6983 cp_parser_declaration_seq_opt (parser);
6984 /* Look for the closing `}'. */
6985 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6986 }
6987 /* Otherwise, there's just one declaration. */
6988 else
6989 {
6990 bool saved_in_unbraced_linkage_specification_p;
6991
21526606 6992 saved_in_unbraced_linkage_specification_p
a723baf1
MM
6993 = parser->in_unbraced_linkage_specification_p;
6994 parser->in_unbraced_linkage_specification_p = true;
6995 have_extern_spec = true;
6996 cp_parser_declaration (parser);
6997 have_extern_spec = false;
21526606 6998 parser->in_unbraced_linkage_specification_p
a723baf1
MM
6999 = saved_in_unbraced_linkage_specification_p;
7000 }
7001
7002 /* We're done with the linkage-specification. */
7003 pop_lang_context ();
7004}
7005
7006/* Special member functions [gram.special] */
7007
7008/* Parse a conversion-function-id.
7009
7010 conversion-function-id:
21526606 7011 operator conversion-type-id
a723baf1
MM
7012
7013 Returns an IDENTIFIER_NODE representing the operator. */
7014
21526606 7015static tree
94edc4ab 7016cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7017{
7018 tree type;
7019 tree saved_scope;
7020 tree saved_qualifying_scope;
7021 tree saved_object_scope;
91b004e5 7022 bool pop_p = false;
a723baf1
MM
7023
7024 /* Look for the `operator' token. */
7025 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7026 return error_mark_node;
7027 /* When we parse the conversion-type-id, the current scope will be
7028 reset. However, we need that information in able to look up the
7029 conversion function later, so we save it here. */
7030 saved_scope = parser->scope;
7031 saved_qualifying_scope = parser->qualifying_scope;
7032 saved_object_scope = parser->object_scope;
7033 /* We must enter the scope of the class so that the names of
7034 entities declared within the class are available in the
7035 conversion-type-id. For example, consider:
7036
21526606 7037 struct S {
a723baf1
MM
7038 typedef int I;
7039 operator I();
7040 };
7041
7042 S::operator I() { ... }
7043
7044 In order to see that `I' is a type-name in the definition, we
7045 must be in the scope of `S'. */
7046 if (saved_scope)
91b004e5 7047 pop_p = push_scope (saved_scope);
a723baf1
MM
7048 /* Parse the conversion-type-id. */
7049 type = cp_parser_conversion_type_id (parser);
7050 /* Leave the scope of the class, if any. */
91b004e5 7051 if (pop_p)
a723baf1
MM
7052 pop_scope (saved_scope);
7053 /* Restore the saved scope. */
7054 parser->scope = saved_scope;
7055 parser->qualifying_scope = saved_qualifying_scope;
7056 parser->object_scope = saved_object_scope;
7057 /* If the TYPE is invalid, indicate failure. */
7058 if (type == error_mark_node)
7059 return error_mark_node;
7060 return mangle_conv_op_name_for_type (type);
7061}
7062
7063/* Parse a conversion-type-id:
7064
7065 conversion-type-id:
7066 type-specifier-seq conversion-declarator [opt]
7067
7068 Returns the TYPE specified. */
7069
7070static tree
94edc4ab 7071cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7072{
7073 tree attributes;
7074 tree type_specifiers;
7075 tree declarator;
7076
7077 /* Parse the attributes. */
7078 attributes = cp_parser_attributes_opt (parser);
7079 /* Parse the type-specifiers. */
7080 type_specifiers = cp_parser_type_specifier_seq (parser);
7081 /* If that didn't work, stop. */
7082 if (type_specifiers == error_mark_node)
7083 return error_mark_node;
7084 /* Parse the conversion-declarator. */
7085 declarator = cp_parser_conversion_declarator_opt (parser);
7086
7087 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7088 /*initialized=*/0, &attributes);
7089}
7090
7091/* Parse an (optional) conversion-declarator.
7092
7093 conversion-declarator:
21526606 7094 ptr-operator conversion-declarator [opt]
a723baf1
MM
7095
7096 Returns a representation of the declarator. See
7097 cp_parser_declarator for details. */
7098
7099static tree
94edc4ab 7100cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7101{
7102 enum tree_code code;
7103 tree class_type;
7104 tree cv_qualifier_seq;
7105
7106 /* We don't know if there's a ptr-operator next, or not. */
7107 cp_parser_parse_tentatively (parser);
7108 /* Try the ptr-operator. */
21526606 7109 code = cp_parser_ptr_operator (parser, &class_type,
a723baf1
MM
7110 &cv_qualifier_seq);
7111 /* If it worked, look for more conversion-declarators. */
7112 if (cp_parser_parse_definitely (parser))
7113 {
7114 tree declarator;
7115
7116 /* Parse another optional declarator. */
7117 declarator = cp_parser_conversion_declarator_opt (parser);
7118
7119 /* Create the representation of the declarator. */
7120 if (code == INDIRECT_REF)
7121 declarator = make_pointer_declarator (cv_qualifier_seq,
7122 declarator);
7123 else
7124 declarator = make_reference_declarator (cv_qualifier_seq,
7125 declarator);
7126
7127 /* Handle the pointer-to-member case. */
7128 if (class_type)
7129 declarator = build_nt (SCOPE_REF, class_type, declarator);
7130
7131 return declarator;
7132 }
7133
7134 return NULL_TREE;
7135}
7136
7137/* Parse an (optional) ctor-initializer.
7138
7139 ctor-initializer:
21526606 7140 : mem-initializer-list
a723baf1
MM
7141
7142 Returns TRUE iff the ctor-initializer was actually present. */
7143
7144static bool
94edc4ab 7145cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7146{
7147 /* If the next token is not a `:', then there is no
7148 ctor-initializer. */
7149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7150 {
7151 /* Do default initialization of any bases and members. */
7152 if (DECL_CONSTRUCTOR_P (current_function_decl))
7153 finish_mem_initializers (NULL_TREE);
7154
7155 return false;
7156 }
7157
7158 /* Consume the `:' token. */
7159 cp_lexer_consume_token (parser->lexer);
7160 /* And the mem-initializer-list. */
7161 cp_parser_mem_initializer_list (parser);
7162
7163 return true;
7164}
7165
7166/* Parse a mem-initializer-list.
7167
7168 mem-initializer-list:
7169 mem-initializer
7170 mem-initializer , mem-initializer-list */
7171
7172static void
94edc4ab 7173cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7174{
7175 tree mem_initializer_list = NULL_TREE;
7176
7177 /* Let the semantic analysis code know that we are starting the
7178 mem-initializer-list. */
0e136342
MM
7179 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7180 error ("only constructors take base initializers");
a723baf1
MM
7181
7182 /* Loop through the list. */
7183 while (true)
7184 {
7185 tree mem_initializer;
7186
7187 /* Parse the mem-initializer. */
7188 mem_initializer = cp_parser_mem_initializer (parser);
7189 /* Add it to the list, unless it was erroneous. */
7190 if (mem_initializer)
7191 {
7192 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7193 mem_initializer_list = mem_initializer;
7194 }
7195 /* If the next token is not a `,', we're done. */
7196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7197 break;
7198 /* Consume the `,' token. */
7199 cp_lexer_consume_token (parser->lexer);
7200 }
7201
7202 /* Perform semantic analysis. */
0e136342
MM
7203 if (DECL_CONSTRUCTOR_P (current_function_decl))
7204 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7205}
7206
7207/* Parse a mem-initializer.
7208
7209 mem-initializer:
21526606 7210 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7211
7212 GNU extension:
21526606 7213
a723baf1 7214 mem-initializer:
34cd5ae7 7215 ( expression-list [opt] )
a723baf1
MM
7216
7217 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7218 class) or FIELD_DECL (for a non-static data member) to initialize;
7219 the TREE_VALUE is the expression-list. */
7220
7221static tree
94edc4ab 7222cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7223{
7224 tree mem_initializer_id;
7225 tree expression_list;
1f5a253a 7226 tree member;
21526606 7227
a723baf1
MM
7228 /* Find out what is being initialized. */
7229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7230 {
7231 pedwarn ("anachronistic old-style base class initializer");
7232 mem_initializer_id = NULL_TREE;
7233 }
7234 else
7235 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7236 member = expand_member_init (mem_initializer_id);
7237 if (member && !DECL_P (member))
7238 in_base_initializer = 1;
7efa3e22 7239
21526606 7240 expression_list
39703eb9
MM
7241 = cp_parser_parenthesized_expression_list (parser, false,
7242 /*non_constant_p=*/NULL);
7efa3e22 7243 if (!expression_list)
a723baf1 7244 expression_list = void_type_node;
a723baf1 7245
1f5a253a 7246 in_base_initializer = 0;
21526606 7247
1f5a253a 7248 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7249}
7250
7251/* Parse a mem-initializer-id.
7252
7253 mem-initializer-id:
7254 :: [opt] nested-name-specifier [opt] class-name
21526606 7255 identifier
a723baf1
MM
7256
7257 Returns a TYPE indicating the class to be initializer for the first
7258 production. Returns an IDENTIFIER_NODE indicating the data member
7259 to be initialized for the second production. */
7260
7261static tree
94edc4ab 7262cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7263{
7264 bool global_scope_p;
7265 bool nested_name_specifier_p;
7266 tree id;
7267
7268 /* Look for the optional `::' operator. */
21526606
EC
7269 global_scope_p
7270 = (cp_parser_global_scope_opt (parser,
7271 /*current_scope_valid_p=*/false)
a723baf1
MM
7272 != NULL_TREE);
7273 /* Look for the optional nested-name-specifier. The simplest way to
7274 implement:
7275
7276 [temp.res]
7277
7278 The keyword `typename' is not permitted in a base-specifier or
7279 mem-initializer; in these contexts a qualified name that
7280 depends on a template-parameter is implicitly assumed to be a
7281 type name.
7282
7283 is to assume that we have seen the `typename' keyword at this
7284 point. */
21526606 7285 nested_name_specifier_p
a723baf1
MM
7286 = (cp_parser_nested_name_specifier_opt (parser,
7287 /*typename_keyword_p=*/true,
7288 /*check_dependency_p=*/true,
a668c6ad
MM
7289 /*type_p=*/true,
7290 /*is_declaration=*/true)
a723baf1
MM
7291 != NULL_TREE);
7292 /* If there is a `::' operator or a nested-name-specifier, then we
7293 are definitely looking for a class-name. */
7294 if (global_scope_p || nested_name_specifier_p)
7295 return cp_parser_class_name (parser,
7296 /*typename_keyword_p=*/true,
7297 /*template_keyword_p=*/false,
7298 /*type_p=*/false,
a723baf1 7299 /*check_dependency_p=*/true,
a668c6ad
MM
7300 /*class_head_p=*/false,
7301 /*is_declaration=*/true);
a723baf1
MM
7302 /* Otherwise, we could also be looking for an ordinary identifier. */
7303 cp_parser_parse_tentatively (parser);
7304 /* Try a class-name. */
21526606 7305 id = cp_parser_class_name (parser,
a723baf1
MM
7306 /*typename_keyword_p=*/true,
7307 /*template_keyword_p=*/false,
7308 /*type_p=*/false,
a723baf1 7309 /*check_dependency_p=*/true,
a668c6ad
MM
7310 /*class_head_p=*/false,
7311 /*is_declaration=*/true);
a723baf1
MM
7312 /* If we found one, we're done. */
7313 if (cp_parser_parse_definitely (parser))
7314 return id;
7315 /* Otherwise, look for an ordinary identifier. */
7316 return cp_parser_identifier (parser);
7317}
7318
7319/* Overloading [gram.over] */
7320
7321/* Parse an operator-function-id.
7322
7323 operator-function-id:
21526606 7324 operator operator
a723baf1
MM
7325
7326 Returns an IDENTIFIER_NODE for the operator which is a
7327 human-readable spelling of the identifier, e.g., `operator +'. */
7328
21526606 7329static tree
94edc4ab 7330cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7331{
7332 /* Look for the `operator' keyword. */
7333 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7334 return error_mark_node;
7335 /* And then the name of the operator itself. */
7336 return cp_parser_operator (parser);
7337}
7338
7339/* Parse an operator.
7340
7341 operator:
7342 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7343 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7344 || ++ -- , ->* -> () []
7345
7346 GNU Extensions:
21526606 7347
a723baf1
MM
7348 operator:
7349 <? >? <?= >?=
7350
7351 Returns an IDENTIFIER_NODE for the operator which is a
7352 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7353
a723baf1 7354static tree
94edc4ab 7355cp_parser_operator (cp_parser* parser)
a723baf1
MM
7356{
7357 tree id = NULL_TREE;
7358 cp_token *token;
7359
7360 /* Peek at the next token. */
7361 token = cp_lexer_peek_token (parser->lexer);
7362 /* Figure out which operator we have. */
7363 switch (token->type)
7364 {
7365 case CPP_KEYWORD:
7366 {
7367 enum tree_code op;
7368
7369 /* The keyword should be either `new' or `delete'. */
7370 if (token->keyword == RID_NEW)
7371 op = NEW_EXPR;
7372 else if (token->keyword == RID_DELETE)
7373 op = DELETE_EXPR;
7374 else
7375 break;
7376
7377 /* Consume the `new' or `delete' token. */
7378 cp_lexer_consume_token (parser->lexer);
7379
7380 /* Peek at the next token. */
7381 token = cp_lexer_peek_token (parser->lexer);
7382 /* If it's a `[' token then this is the array variant of the
7383 operator. */
7384 if (token->type == CPP_OPEN_SQUARE)
7385 {
7386 /* Consume the `[' token. */
7387 cp_lexer_consume_token (parser->lexer);
7388 /* Look for the `]' token. */
7389 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7390 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7391 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7392 }
7393 /* Otherwise, we have the non-array variant. */
7394 else
7395 id = ansi_opname (op);
7396
7397 return id;
7398 }
7399
7400 case CPP_PLUS:
7401 id = ansi_opname (PLUS_EXPR);
7402 break;
7403
7404 case CPP_MINUS:
7405 id = ansi_opname (MINUS_EXPR);
7406 break;
7407
7408 case CPP_MULT:
7409 id = ansi_opname (MULT_EXPR);
7410 break;
7411
7412 case CPP_DIV:
7413 id = ansi_opname (TRUNC_DIV_EXPR);
7414 break;
7415
7416 case CPP_MOD:
7417 id = ansi_opname (TRUNC_MOD_EXPR);
7418 break;
7419
7420 case CPP_XOR:
7421 id = ansi_opname (BIT_XOR_EXPR);
7422 break;
7423
7424 case CPP_AND:
7425 id = ansi_opname (BIT_AND_EXPR);
7426 break;
7427
7428 case CPP_OR:
7429 id = ansi_opname (BIT_IOR_EXPR);
7430 break;
7431
7432 case CPP_COMPL:
7433 id = ansi_opname (BIT_NOT_EXPR);
7434 break;
21526606 7435
a723baf1
MM
7436 case CPP_NOT:
7437 id = ansi_opname (TRUTH_NOT_EXPR);
7438 break;
7439
7440 case CPP_EQ:
7441 id = ansi_assopname (NOP_EXPR);
7442 break;
7443
7444 case CPP_LESS:
7445 id = ansi_opname (LT_EXPR);
7446 break;
7447
7448 case CPP_GREATER:
7449 id = ansi_opname (GT_EXPR);
7450 break;
7451
7452 case CPP_PLUS_EQ:
7453 id = ansi_assopname (PLUS_EXPR);
7454 break;
7455
7456 case CPP_MINUS_EQ:
7457 id = ansi_assopname (MINUS_EXPR);
7458 break;
7459
7460 case CPP_MULT_EQ:
7461 id = ansi_assopname (MULT_EXPR);
7462 break;
7463
7464 case CPP_DIV_EQ:
7465 id = ansi_assopname (TRUNC_DIV_EXPR);
7466 break;
7467
7468 case CPP_MOD_EQ:
7469 id = ansi_assopname (TRUNC_MOD_EXPR);
7470 break;
7471
7472 case CPP_XOR_EQ:
7473 id = ansi_assopname (BIT_XOR_EXPR);
7474 break;
7475
7476 case CPP_AND_EQ:
7477 id = ansi_assopname (BIT_AND_EXPR);
7478 break;
7479
7480 case CPP_OR_EQ:
7481 id = ansi_assopname (BIT_IOR_EXPR);
7482 break;
7483
7484 case CPP_LSHIFT:
7485 id = ansi_opname (LSHIFT_EXPR);
7486 break;
7487
7488 case CPP_RSHIFT:
7489 id = ansi_opname (RSHIFT_EXPR);
7490 break;
7491
7492 case CPP_LSHIFT_EQ:
7493 id = ansi_assopname (LSHIFT_EXPR);
7494 break;
7495
7496 case CPP_RSHIFT_EQ:
7497 id = ansi_assopname (RSHIFT_EXPR);
7498 break;
7499
7500 case CPP_EQ_EQ:
7501 id = ansi_opname (EQ_EXPR);
7502 break;
7503
7504 case CPP_NOT_EQ:
7505 id = ansi_opname (NE_EXPR);
7506 break;
7507
7508 case CPP_LESS_EQ:
7509 id = ansi_opname (LE_EXPR);
7510 break;
7511
7512 case CPP_GREATER_EQ:
7513 id = ansi_opname (GE_EXPR);
7514 break;
7515
7516 case CPP_AND_AND:
7517 id = ansi_opname (TRUTH_ANDIF_EXPR);
7518 break;
7519
7520 case CPP_OR_OR:
7521 id = ansi_opname (TRUTH_ORIF_EXPR);
7522 break;
21526606 7523
a723baf1
MM
7524 case CPP_PLUS_PLUS:
7525 id = ansi_opname (POSTINCREMENT_EXPR);
7526 break;
7527
7528 case CPP_MINUS_MINUS:
7529 id = ansi_opname (PREDECREMENT_EXPR);
7530 break;
7531
7532 case CPP_COMMA:
7533 id = ansi_opname (COMPOUND_EXPR);
7534 break;
7535
7536 case CPP_DEREF_STAR:
7537 id = ansi_opname (MEMBER_REF);
7538 break;
7539
7540 case CPP_DEREF:
7541 id = ansi_opname (COMPONENT_REF);
7542 break;
7543
7544 case CPP_OPEN_PAREN:
7545 /* Consume the `('. */
7546 cp_lexer_consume_token (parser->lexer);
7547 /* Look for the matching `)'. */
7548 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7549 return ansi_opname (CALL_EXPR);
7550
7551 case CPP_OPEN_SQUARE:
7552 /* Consume the `['. */
7553 cp_lexer_consume_token (parser->lexer);
7554 /* Look for the matching `]'. */
7555 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7556 return ansi_opname (ARRAY_REF);
7557
7558 /* Extensions. */
7559 case CPP_MIN:
7560 id = ansi_opname (MIN_EXPR);
7561 break;
7562
7563 case CPP_MAX:
7564 id = ansi_opname (MAX_EXPR);
7565 break;
7566
7567 case CPP_MIN_EQ:
7568 id = ansi_assopname (MIN_EXPR);
7569 break;
7570
7571 case CPP_MAX_EQ:
7572 id = ansi_assopname (MAX_EXPR);
7573 break;
7574
7575 default:
7576 /* Anything else is an error. */
7577 break;
7578 }
7579
7580 /* If we have selected an identifier, we need to consume the
7581 operator token. */
7582 if (id)
7583 cp_lexer_consume_token (parser->lexer);
7584 /* Otherwise, no valid operator name was present. */
7585 else
7586 {
7587 cp_parser_error (parser, "expected operator");
7588 id = error_mark_node;
7589 }
7590
7591 return id;
7592}
7593
7594/* Parse a template-declaration.
7595
7596 template-declaration:
21526606 7597 export [opt] template < template-parameter-list > declaration
a723baf1
MM
7598
7599 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 7600 class-specifier.
a723baf1
MM
7601
7602 The grammar rule given by the standard isn't correct. What
7603 is really meant is:
7604
7605 template-declaration:
21526606 7606 export [opt] template-parameter-list-seq
a723baf1 7607 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 7608 export [opt] template-parameter-list-seq
a723baf1
MM
7609 function-definition
7610
7611 template-parameter-list-seq:
7612 template-parameter-list-seq [opt]
7613 template < template-parameter-list > */
7614
7615static void
94edc4ab 7616cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7617{
7618 /* Check for `export'. */
7619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7620 {
7621 /* Consume the `export' token. */
7622 cp_lexer_consume_token (parser->lexer);
7623 /* Warn that we do not support `export'. */
7624 warning ("keyword `export' not implemented, and will be ignored");
7625 }
7626
7627 cp_parser_template_declaration_after_export (parser, member_p);
7628}
7629
7630/* Parse a template-parameter-list.
7631
7632 template-parameter-list:
7633 template-parameter
7634 template-parameter-list , template-parameter
7635
7636 Returns a TREE_LIST. Each node represents a template parameter.
7637 The nodes are connected via their TREE_CHAINs. */
7638
7639static tree
94edc4ab 7640cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7641{
7642 tree parameter_list = NULL_TREE;
7643
7644 while (true)
7645 {
7646 tree parameter;
7647 cp_token *token;
7648
7649 /* Parse the template-parameter. */
7650 parameter = cp_parser_template_parameter (parser);
7651 /* Add it to the list. */
7652 parameter_list = process_template_parm (parameter_list,
7653 parameter);
7654
7655 /* Peek at the next token. */
7656 token = cp_lexer_peek_token (parser->lexer);
7657 /* If it's not a `,', we're done. */
7658 if (token->type != CPP_COMMA)
7659 break;
7660 /* Otherwise, consume the `,' token. */
7661 cp_lexer_consume_token (parser->lexer);
7662 }
7663
7664 return parameter_list;
7665}
7666
7667/* Parse a template-parameter.
7668
7669 template-parameter:
7670 type-parameter
7671 parameter-declaration
7672
7673 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7674 TREE_PURPOSE is the default value, if any. */
7675
7676static tree
94edc4ab 7677cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7678{
7679 cp_token *token;
7680
7681 /* Peek at the next token. */
7682 token = cp_lexer_peek_token (parser->lexer);
7683 /* If it is `class' or `template', we have a type-parameter. */
7684 if (token->keyword == RID_TEMPLATE)
7685 return cp_parser_type_parameter (parser);
7686 /* If it is `class' or `typename' we do not know yet whether it is a
7687 type parameter or a non-type parameter. Consider:
7688
7689 template <typename T, typename T::X X> ...
7690
7691 or:
21526606 7692
a723baf1
MM
7693 template <class C, class D*> ...
7694
7695 Here, the first parameter is a type parameter, and the second is
7696 a non-type parameter. We can tell by looking at the token after
7697 the identifier -- if it is a `,', `=', or `>' then we have a type
7698 parameter. */
7699 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7700 {
7701 /* Peek at the token after `class' or `typename'. */
7702 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7703 /* If it's an identifier, skip it. */
7704 if (token->type == CPP_NAME)
7705 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7706 /* Now, see if the token looks like the end of a template
7707 parameter. */
21526606 7708 if (token->type == CPP_COMMA
a723baf1
MM
7709 || token->type == CPP_EQ
7710 || token->type == CPP_GREATER)
7711 return cp_parser_type_parameter (parser);
7712 }
7713
21526606 7714 /* Otherwise, it is a non-type parameter.
a723baf1
MM
7715
7716 [temp.param]
7717
7718 When parsing a default template-argument for a non-type
7719 template-parameter, the first non-nested `>' is taken as the end
7720 of the template parameter-list rather than a greater-than
7721 operator. */
21526606 7722 return
4bb8ca28
MM
7723 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7724 /*parenthesized_p=*/NULL);
a723baf1
MM
7725}
7726
7727/* Parse a type-parameter.
7728
7729 type-parameter:
7730 class identifier [opt]
7731 class identifier [opt] = type-id
7732 typename identifier [opt]
7733 typename identifier [opt] = type-id
7734 template < template-parameter-list > class identifier [opt]
21526606
EC
7735 template < template-parameter-list > class identifier [opt]
7736 = id-expression
a723baf1
MM
7737
7738 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7739 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7740 the declaration of the parameter. */
7741
7742static tree
94edc4ab 7743cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7744{
7745 cp_token *token;
7746 tree parameter;
7747
7748 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 7749 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7750 "`class', `typename', or `template'");
a723baf1
MM
7751 if (!token)
7752 return error_mark_node;
7753
7754 switch (token->keyword)
7755 {
7756 case RID_CLASS:
7757 case RID_TYPENAME:
7758 {
7759 tree identifier;
7760 tree default_argument;
7761
7762 /* If the next token is an identifier, then it names the
7763 parameter. */
7764 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7765 identifier = cp_parser_identifier (parser);
7766 else
7767 identifier = NULL_TREE;
7768
7769 /* Create the parameter. */
7770 parameter = finish_template_type_parm (class_type_node, identifier);
7771
7772 /* If the next token is an `=', we have a default argument. */
7773 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7774 {
7775 /* Consume the `=' token. */
7776 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7777 /* Parse the default-argument. */
a723baf1
MM
7778 default_argument = cp_parser_type_id (parser);
7779 }
7780 else
7781 default_argument = NULL_TREE;
7782
7783 /* Create the combined representation of the parameter and the
7784 default argument. */
c67d36d0 7785 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7786 }
7787 break;
7788
7789 case RID_TEMPLATE:
7790 {
7791 tree parameter_list;
7792 tree identifier;
7793 tree default_argument;
7794
7795 /* Look for the `<'. */
7796 cp_parser_require (parser, CPP_LESS, "`<'");
7797 /* Parse the template-parameter-list. */
7798 begin_template_parm_list ();
21526606 7799 parameter_list
a723baf1
MM
7800 = cp_parser_template_parameter_list (parser);
7801 parameter_list = end_template_parm_list (parameter_list);
7802 /* Look for the `>'. */
7803 cp_parser_require (parser, CPP_GREATER, "`>'");
7804 /* Look for the `class' keyword. */
7805 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7806 /* If the next token is an `=', then there is a
7807 default-argument. If the next token is a `>', we are at
7808 the end of the parameter-list. If the next token is a `,',
7809 then we are at the end of this parameter. */
7810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7811 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7812 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7813 identifier = cp_parser_identifier (parser);
7814 else
7815 identifier = NULL_TREE;
7816 /* Create the template parameter. */
7817 parameter = finish_template_template_parm (class_type_node,
7818 identifier);
21526606 7819
a723baf1
MM
7820 /* If the next token is an `=', then there is a
7821 default-argument. */
7822 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7823 {
b0bc6e8e
KL
7824 bool is_template;
7825
a723baf1
MM
7826 /* Consume the `='. */
7827 cp_lexer_consume_token (parser->lexer);
7828 /* Parse the id-expression. */
21526606 7829 default_argument
a723baf1
MM
7830 = cp_parser_id_expression (parser,
7831 /*template_keyword_p=*/false,
7832 /*check_dependency_p=*/true,
b0bc6e8e 7833 /*template_p=*/&is_template,
f3c2dfc6 7834 /*declarator_p=*/false);
a3a503a5
GB
7835 if (TREE_CODE (default_argument) == TYPE_DECL)
7836 /* If the id-expression was a template-id that refers to
7837 a template-class, we already have the declaration here,
7838 so no further lookup is needed. */
7839 ;
7840 else
7841 /* Look up the name. */
21526606 7842 default_argument
a3a503a5
GB
7843 = cp_parser_lookup_name (parser, default_argument,
7844 /*is_type=*/false,
7845 /*is_template=*/is_template,
7846 /*is_namespace=*/false,
7847 /*check_dependency=*/true);
a723baf1
MM
7848 /* See if the default argument is valid. */
7849 default_argument
7850 = check_template_template_default_arg (default_argument);
7851 }
7852 else
7853 default_argument = NULL_TREE;
7854
7855 /* Create the combined representation of the parameter and the
7856 default argument. */
c67d36d0 7857 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7858 }
7859 break;
7860
7861 default:
7862 /* Anything else is an error. */
7863 cp_parser_error (parser,
7864 "expected `class', `typename', or `template'");
7865 parameter = error_mark_node;
7866 }
21526606 7867
a723baf1
MM
7868 return parameter;
7869}
7870
7871/* Parse a template-id.
7872
7873 template-id:
7874 template-name < template-argument-list [opt] >
7875
7876 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7877 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7878 returned. Otherwise, if the template-name names a function, or set
7879 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 7880 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
7881
7882 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7883 uninstantiated templates. */
7884
7885static tree
21526606
EC
7886cp_parser_template_id (cp_parser *parser,
7887 bool template_keyword_p,
a668c6ad
MM
7888 bool check_dependency_p,
7889 bool is_declaration)
a723baf1
MM
7890{
7891 tree template;
7892 tree arguments;
a723baf1 7893 tree template_id;
a723baf1
MM
7894 ptrdiff_t start_of_id;
7895 tree access_check = NULL_TREE;
f4abade9 7896 cp_token *next_token, *next_token_2;
a668c6ad 7897 bool is_identifier;
a723baf1
MM
7898
7899 /* If the next token corresponds to a template-id, there is no need
7900 to reparse it. */
2050a1bb
MM
7901 next_token = cp_lexer_peek_token (parser->lexer);
7902 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7903 {
7904 tree value;
7905 tree check;
7906
7907 /* Get the stored value. */
7908 value = cp_lexer_consume_token (parser->lexer)->value;
7909 /* Perform any access checks that were deferred. */
7910 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7911 perform_or_defer_access_check (TREE_PURPOSE (check),
7912 TREE_VALUE (check));
a723baf1
MM
7913 /* Return the stored value. */
7914 return TREE_VALUE (value);
7915 }
7916
2050a1bb
MM
7917 /* Avoid performing name lookup if there is no possibility of
7918 finding a template-id. */
7919 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7920 || (next_token->type == CPP_NAME
21526606 7921 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 7922 (parser, 2)))
2050a1bb
MM
7923 {
7924 cp_parser_error (parser, "expected template-id");
7925 return error_mark_node;
7926 }
7927
a723baf1
MM
7928 /* Remember where the template-id starts. */
7929 if (cp_parser_parsing_tentatively (parser)
7930 && !cp_parser_committed_to_tentative_parse (parser))
7931 {
2050a1bb 7932 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7933 start_of_id = cp_lexer_token_difference (parser->lexer,
7934 parser->lexer->first_token,
7935 next_token);
a723baf1
MM
7936 }
7937 else
7938 start_of_id = -1;
7939
8d241e0b 7940 push_deferring_access_checks (dk_deferred);
cf22909c 7941
a723baf1 7942 /* Parse the template-name. */
a668c6ad 7943 is_identifier = false;
a723baf1 7944 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
7945 check_dependency_p,
7946 is_declaration,
7947 &is_identifier);
7948 if (template == error_mark_node || is_identifier)
cf22909c
KL
7949 {
7950 pop_deferring_access_checks ();
a668c6ad 7951 return template;
cf22909c 7952 }
a723baf1 7953
21526606 7954 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
7955 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7956 parse correctly the argument list. */
7957 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7958 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 7959 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 7960 && next_token->flags & DIGRAPH
21526606 7961 && next_token_2->type == CPP_COLON
f4abade9 7962 && !(next_token_2->flags & PREV_WHITE))
cf22909c 7963 {
f4abade9
GB
7964 cp_parser_parse_tentatively (parser);
7965 /* Change `:' into `::'. */
7966 next_token_2->type = CPP_SCOPE;
7967 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7968 CPP_LESS. */
7969 cp_lexer_consume_token (parser->lexer);
7970 /* Parse the arguments. */
7971 arguments = cp_parser_enclosed_template_argument_list (parser);
7972 if (!cp_parser_parse_definitely (parser))
7973 {
7974 /* If we couldn't parse an argument list, then we revert our changes
7975 and return simply an error. Maybe this is not a template-id
7976 after all. */
7977 next_token_2->type = CPP_COLON;
7978 cp_parser_error (parser, "expected `<'");
7979 pop_deferring_access_checks ();
7980 return error_mark_node;
7981 }
7982 /* Otherwise, emit an error about the invalid digraph, but continue
7983 parsing because we got our argument list. */
7984 pedwarn ("`<::' cannot begin a template-argument list");
7985 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7986 "between `<' and `::'");
7987 if (!flag_permissive)
7988 {
7989 static bool hint;
7990 if (!hint)
7991 {
7992 inform ("(if you use `-fpermissive' G++ will accept your code)");
7993 hint = true;
7994 }
7995 }
7996 }
7997 else
7998 {
7999 /* Look for the `<' that starts the template-argument-list. */
8000 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8001 {
8002 pop_deferring_access_checks ();
8003 return error_mark_node;
8004 }
8005 /* Parse the arguments. */
8006 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8007 }
a723baf1
MM
8008
8009 /* Build a representation of the specialization. */
8010 if (TREE_CODE (template) == IDENTIFIER_NODE)
8011 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8012 else if (DECL_CLASS_TEMPLATE_P (template)
8013 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8014 template_id
8015 = finish_template_type (template, arguments,
8016 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8017 CPP_SCOPE));
8018 else
8019 {
8020 /* If it's not a class-template or a template-template, it should be
8021 a function-template. */
8022 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8023 || TREE_CODE (template) == OVERLOAD
8024 || BASELINK_P (template)),
8025 20010716);
21526606 8026
a723baf1
MM
8027 template_id = lookup_template_function (template, arguments);
8028 }
21526606 8029
cf22909c
KL
8030 /* Retrieve any deferred checks. Do not pop this access checks yet
8031 so the memory will not be reclaimed during token replacing below. */
8032 access_check = get_deferred_access_checks ();
8033
a723baf1
MM
8034 /* If parsing tentatively, replace the sequence of tokens that makes
8035 up the template-id with a CPP_TEMPLATE_ID token. That way,
8036 should we re-parse the token stream, we will not have to repeat
8037 the effort required to do the parse, nor will we issue duplicate
8038 error messages about problems during instantiation of the
8039 template. */
8040 if (start_of_id >= 0)
8041 {
8042 cp_token *token;
a723baf1
MM
8043
8044 /* Find the token that corresponds to the start of the
8045 template-id. */
21526606 8046 token = cp_lexer_advance_token (parser->lexer,
a723baf1
MM
8047 parser->lexer->first_token,
8048 start_of_id);
8049
a723baf1
MM
8050 /* Reset the contents of the START_OF_ID token. */
8051 token->type = CPP_TEMPLATE_ID;
8052 token->value = build_tree_list (access_check, template_id);
8053 token->keyword = RID_MAX;
8054 /* Purge all subsequent tokens. */
8055 cp_lexer_purge_tokens_after (parser->lexer, token);
8056 }
8057
cf22909c 8058 pop_deferring_access_checks ();
a723baf1
MM
8059 return template_id;
8060}
8061
8062/* Parse a template-name.
8063
8064 template-name:
8065 identifier
21526606 8066
a723baf1
MM
8067 The standard should actually say:
8068
8069 template-name:
8070 identifier
8071 operator-function-id
a723baf1
MM
8072
8073 A defect report has been filed about this issue.
8074
0d956474
GB
8075 A conversion-function-id cannot be a template name because they cannot
8076 be part of a template-id. In fact, looking at this code:
8077
8078 a.operator K<int>()
8079
8080 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8081 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8082 explicit argument list, since the only allowed template parameter is
8083 the type to which it is converting.
8084
a723baf1
MM
8085 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8086 `template' keyword, in a construction like:
8087
8088 T::template f<3>()
8089
8090 In that case `f' is taken to be a template-name, even though there
8091 is no way of knowing for sure.
8092
8093 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8094 name refers to a set of overloaded functions, at least one of which
8095 is a template, or an IDENTIFIER_NODE with the name of the template,
8096 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8097 names are looked up inside uninstantiated templates. */
8098
8099static tree
21526606
EC
8100cp_parser_template_name (cp_parser* parser,
8101 bool template_keyword_p,
a668c6ad
MM
8102 bool check_dependency_p,
8103 bool is_declaration,
8104 bool *is_identifier)
a723baf1
MM
8105{
8106 tree identifier;
8107 tree decl;
8108 tree fns;
8109
8110 /* If the next token is `operator', then we have either an
8111 operator-function-id or a conversion-function-id. */
8112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8113 {
8114 /* We don't know whether we're looking at an
8115 operator-function-id or a conversion-function-id. */
8116 cp_parser_parse_tentatively (parser);
8117 /* Try an operator-function-id. */
8118 identifier = cp_parser_operator_function_id (parser);
8119 /* If that didn't work, try a conversion-function-id. */
8120 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8121 {
8122 cp_parser_error (parser, "expected template-name");
8123 return error_mark_node;
8124 }
a723baf1
MM
8125 }
8126 /* Look for the identifier. */
8127 else
8128 identifier = cp_parser_identifier (parser);
21526606 8129
a723baf1
MM
8130 /* If we didn't find an identifier, we don't have a template-id. */
8131 if (identifier == error_mark_node)
8132 return error_mark_node;
8133
8134 /* If the name immediately followed the `template' keyword, then it
8135 is a template-name. However, if the next token is not `<', then
8136 we do not treat it as a template-name, since it is not being used
8137 as part of a template-id. This enables us to handle constructs
8138 like:
8139
8140 template <typename T> struct S { S(); };
8141 template <typename T> S<T>::S();
8142
8143 correctly. We would treat `S' as a template -- if it were `S<T>'
8144 -- but we do not if there is no `<'. */
a668c6ad
MM
8145
8146 if (processing_template_decl
f4abade9 8147 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8148 {
8149 /* In a declaration, in a dependent context, we pretend that the
8150 "template" keyword was present in order to improve error
8151 recovery. For example, given:
21526606 8152
a668c6ad 8153 template <typename T> void f(T::X<int>);
21526606 8154
a668c6ad 8155 we want to treat "X<int>" as a template-id. */
21526606
EC
8156 if (is_declaration
8157 && !template_keyword_p
a668c6ad
MM
8158 && parser->scope && TYPE_P (parser->scope)
8159 && dependent_type_p (parser->scope))
8160 {
8161 ptrdiff_t start;
8162 cp_token* token;
8163 /* Explain what went wrong. */
8164 error ("non-template `%D' used as template", identifier);
8165 error ("(use `%T::template %D' to indicate that it is a template)",
8166 parser->scope, identifier);
8167 /* If parsing tentatively, find the location of the "<"
8168 token. */
8169 if (cp_parser_parsing_tentatively (parser)
8170 && !cp_parser_committed_to_tentative_parse (parser))
8171 {
8172 cp_parser_simulate_error (parser);
8173 token = cp_lexer_peek_token (parser->lexer);
8174 token = cp_lexer_prev_token (parser->lexer, token);
8175 start = cp_lexer_token_difference (parser->lexer,
8176 parser->lexer->first_token,
8177 token);
8178 }
8179 else
8180 start = -1;
8181 /* Parse the template arguments so that we can issue error
8182 messages about them. */
8183 cp_lexer_consume_token (parser->lexer);
8184 cp_parser_enclosed_template_argument_list (parser);
8185 /* Skip tokens until we find a good place from which to
8186 continue parsing. */
8187 cp_parser_skip_to_closing_parenthesis (parser,
8188 /*recovering=*/true,
8189 /*or_comma=*/true,
8190 /*consume_paren=*/false);
8191 /* If parsing tentatively, permanently remove the
8192 template argument list. That will prevent duplicate
8193 error messages from being issued about the missing
8194 "template" keyword. */
8195 if (start >= 0)
8196 {
8197 token = cp_lexer_advance_token (parser->lexer,
8198 parser->lexer->first_token,
8199 start);
8200 cp_lexer_purge_tokens_after (parser->lexer, token);
8201 }
8202 if (is_identifier)
8203 *is_identifier = true;
8204 return identifier;
8205 }
9d363a56
MM
8206
8207 /* If the "template" keyword is present, then there is generally
8208 no point in doing name-lookup, so we just return IDENTIFIER.
8209 But, if the qualifying scope is non-dependent then we can
8210 (and must) do name-lookup normally. */
8211 if (template_keyword_p
8212 && (!parser->scope
8213 || (TYPE_P (parser->scope)
8214 && dependent_type_p (parser->scope))))
a668c6ad
MM
8215 return identifier;
8216 }
a723baf1
MM
8217
8218 /* Look up the name. */
8219 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8220 /*is_type=*/false,
b0bc6e8e 8221 /*is_template=*/false,
eea9800f 8222 /*is_namespace=*/false,
a723baf1
MM
8223 check_dependency_p);
8224 decl = maybe_get_template_decl_from_type_decl (decl);
8225
8226 /* If DECL is a template, then the name was a template-name. */
8227 if (TREE_CODE (decl) == TEMPLATE_DECL)
8228 ;
21526606 8229 else
a723baf1
MM
8230 {
8231 /* The standard does not explicitly indicate whether a name that
8232 names a set of overloaded declarations, some of which are
8233 templates, is a template-name. However, such a name should
8234 be a template-name; otherwise, there is no way to form a
8235 template-id for the overloaded templates. */
8236 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8237 if (TREE_CODE (fns) == OVERLOAD)
8238 {
8239 tree fn;
21526606 8240
a723baf1
MM
8241 for (fn = fns; fn; fn = OVL_NEXT (fn))
8242 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8243 break;
8244 }
8245 else
8246 {
8247 /* Otherwise, the name does not name a template. */
8248 cp_parser_error (parser, "expected template-name");
8249 return error_mark_node;
8250 }
8251 }
8252
8253 /* If DECL is dependent, and refers to a function, then just return
8254 its name; we will look it up again during template instantiation. */
8255 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8256 {
8257 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8258 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8259 return identifier;
8260 }
8261
8262 return decl;
8263}
8264
8265/* Parse a template-argument-list.
8266
8267 template-argument-list:
8268 template-argument
8269 template-argument-list , template-argument
8270
04c06002 8271 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8272
8273static tree
94edc4ab 8274cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8275{
bf12d54d
NS
8276 tree fixed_args[10];
8277 unsigned n_args = 0;
8278 unsigned alloced = 10;
8279 tree *arg_ary = fixed_args;
8280 tree vec;
4bb8ca28 8281 bool saved_in_template_argument_list_p;
a723baf1 8282
4bb8ca28
MM
8283 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8284 parser->in_template_argument_list_p = true;
bf12d54d 8285 do
a723baf1
MM
8286 {
8287 tree argument;
8288
bf12d54d 8289 if (n_args)
04c06002 8290 /* Consume the comma. */
bf12d54d 8291 cp_lexer_consume_token (parser->lexer);
21526606 8292
a723baf1
MM
8293 /* Parse the template-argument. */
8294 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8295 if (n_args == alloced)
8296 {
8297 alloced *= 2;
21526606 8298
bf12d54d
NS
8299 if (arg_ary == fixed_args)
8300 {
8301 arg_ary = xmalloc (sizeof (tree) * alloced);
8302 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8303 }
8304 else
8305 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8306 }
8307 arg_ary[n_args++] = argument;
a723baf1 8308 }
bf12d54d
NS
8309 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8310
8311 vec = make_tree_vec (n_args);
a723baf1 8312
bf12d54d
NS
8313 while (n_args--)
8314 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8315
bf12d54d
NS
8316 if (arg_ary != fixed_args)
8317 free (arg_ary);
4bb8ca28 8318 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8319 return vec;
a723baf1
MM
8320}
8321
8322/* Parse a template-argument.
8323
8324 template-argument:
8325 assignment-expression
8326 type-id
8327 id-expression
8328
8329 The representation is that of an assignment-expression, type-id, or
8330 id-expression -- except that the qualified id-expression is
8331 evaluated, so that the value returned is either a DECL or an
21526606 8332 OVERLOAD.
d17811fd
MM
8333
8334 Although the standard says "assignment-expression", it forbids
8335 throw-expressions or assignments in the template argument.
8336 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8337
8338static tree
94edc4ab 8339cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8340{
8341 tree argument;
8342 bool template_p;
d17811fd 8343 bool address_p;
4d5297fa 8344 bool maybe_type_id = false;
d17811fd 8345 cp_token *token;
b3445994 8346 cp_id_kind idk;
d17811fd 8347 tree qualifying_class;
a723baf1
MM
8348
8349 /* There's really no way to know what we're looking at, so we just
21526606 8350 try each alternative in order.
a723baf1
MM
8351
8352 [temp.arg]
8353
8354 In a template-argument, an ambiguity between a type-id and an
8355 expression is resolved to a type-id, regardless of the form of
21526606 8356 the corresponding template-parameter.
a723baf1
MM
8357
8358 Therefore, we try a type-id first. */
8359 cp_parser_parse_tentatively (parser);
a723baf1 8360 argument = cp_parser_type_id (parser);
4d5297fa 8361 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8362 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8363 also valid expressions. For instance:
8364
8365 struct X { int operator >> (int); };
8366 template <int V> struct Foo {};
8367 Foo<X () >> 5> r;
8368
8369 Here 'X()' is a valid type-id of a function type, but the user just
8370 wanted to write the expression "X() >> 5". Thus, we remember that we
8371 found a valid type-id, but we still try to parse the argument as an
8372 expression to see what happens. */
8373 if (!cp_parser_error_occurred (parser)
8374 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8375 {
8376 maybe_type_id = true;
8377 cp_parser_abort_tentative_parse (parser);
8378 }
8379 else
8380 {
8381 /* If the next token isn't a `,' or a `>', then this argument wasn't
8382 really finished. This means that the argument is not a valid
8383 type-id. */
8384 if (!cp_parser_next_token_ends_template_argument_p (parser))
8385 cp_parser_error (parser, "expected template-argument");
8386 /* If that worked, we're done. */
8387 if (cp_parser_parse_definitely (parser))
8388 return argument;
8389 }
a723baf1
MM
8390 /* We're still not sure what the argument will be. */
8391 cp_parser_parse_tentatively (parser);
8392 /* Try a template. */
21526606 8393 argument = cp_parser_id_expression (parser,
a723baf1
MM
8394 /*template_keyword_p=*/false,
8395 /*check_dependency_p=*/true,
f3c2dfc6
MM
8396 &template_p,
8397 /*declarator_p=*/false);
a723baf1
MM
8398 /* If the next token isn't a `,' or a `>', then this argument wasn't
8399 really finished. */
d17811fd 8400 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8401 cp_parser_error (parser, "expected template-argument");
8402 if (!cp_parser_error_occurred (parser))
8403 {
8404 /* Figure out what is being referred to. */
5b4acce1
KL
8405 argument = cp_parser_lookup_name (parser, argument,
8406 /*is_type=*/false,
8407 /*is_template=*/template_p,
8408 /*is_namespace=*/false,
8409 /*check_dependency=*/true);
8410 if (TREE_CODE (argument) != TEMPLATE_DECL
8411 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8412 cp_parser_error (parser, "expected template-name");
8413 }
8414 if (cp_parser_parse_definitely (parser))
8415 return argument;
d17811fd
MM
8416 /* It must be a non-type argument. There permitted cases are given
8417 in [temp.arg.nontype]:
8418
8419 -- an integral constant-expression of integral or enumeration
8420 type; or
8421
8422 -- the name of a non-type template-parameter; or
8423
8424 -- the name of an object or function with external linkage...
8425
8426 -- the address of an object or function with external linkage...
8427
04c06002 8428 -- a pointer to member... */
d17811fd
MM
8429 /* Look for a non-type template parameter. */
8430 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8431 {
8432 cp_parser_parse_tentatively (parser);
8433 argument = cp_parser_primary_expression (parser,
8434 &idk,
8435 &qualifying_class);
8436 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8437 || !cp_parser_next_token_ends_template_argument_p (parser))
8438 cp_parser_simulate_error (parser);
8439 if (cp_parser_parse_definitely (parser))
8440 return argument;
8441 }
8442 /* If the next token is "&", the argument must be the address of an
8443 object or function with external linkage. */
8444 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8445 if (address_p)
8446 cp_lexer_consume_token (parser->lexer);
8447 /* See if we might have an id-expression. */
8448 token = cp_lexer_peek_token (parser->lexer);
8449 if (token->type == CPP_NAME
8450 || token->keyword == RID_OPERATOR
8451 || token->type == CPP_SCOPE
8452 || token->type == CPP_TEMPLATE_ID
8453 || token->type == CPP_NESTED_NAME_SPECIFIER)
8454 {
8455 cp_parser_parse_tentatively (parser);
8456 argument = cp_parser_primary_expression (parser,
8457 &idk,
8458 &qualifying_class);
8459 if (cp_parser_error_occurred (parser)
8460 || !cp_parser_next_token_ends_template_argument_p (parser))
8461 cp_parser_abort_tentative_parse (parser);
8462 else
8463 {
8464 if (qualifying_class)
8465 argument = finish_qualified_id_expr (qualifying_class,
8466 argument,
8467 /*done=*/true,
8468 address_p);
8469 if (TREE_CODE (argument) == VAR_DECL)
8470 {
8471 /* A variable without external linkage might still be a
8472 valid constant-expression, so no error is issued here
8473 if the external-linkage check fails. */
8474 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8475 cp_parser_simulate_error (parser);
8476 }
8477 else if (is_overloaded_fn (argument))
8478 /* All overloaded functions are allowed; if the external
8479 linkage test does not pass, an error will be issued
8480 later. */
8481 ;
8482 else if (address_p
21526606 8483 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8484 || TREE_CODE (argument) == SCOPE_REF))
8485 /* A pointer-to-member. */
8486 ;
8487 else
8488 cp_parser_simulate_error (parser);
8489
8490 if (cp_parser_parse_definitely (parser))
8491 {
8492 if (address_p)
8493 argument = build_x_unary_op (ADDR_EXPR, argument);
8494 return argument;
8495 }
8496 }
8497 }
8498 /* If the argument started with "&", there are no other valid
8499 alternatives at this point. */
8500 if (address_p)
8501 {
8502 cp_parser_error (parser, "invalid non-type template argument");
8503 return error_mark_node;
8504 }
4d5297fa 8505 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8506 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8507 Otherwise, we try parsing the constant-expression tentatively,
8508 because the argument could really be a type-id. */
8509 if (maybe_type_id)
8510 cp_parser_parse_tentatively (parser);
21526606 8511 argument = cp_parser_constant_expression (parser,
d17811fd
MM
8512 /*allow_non_constant_p=*/false,
8513 /*non_constant_p=*/NULL);
9baa27a9 8514 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
8515 if (!maybe_type_id)
8516 return argument;
8517 if (!cp_parser_next_token_ends_template_argument_p (parser))
8518 cp_parser_error (parser, "expected template-argument");
8519 if (cp_parser_parse_definitely (parser))
8520 return argument;
8521 /* We did our best to parse the argument as a non type-id, but that
8522 was the only alternative that matched (albeit with a '>' after
21526606 8523 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
8524 diagnostic will then be issued. */
8525 return cp_parser_type_id (parser);
a723baf1
MM
8526}
8527
8528/* Parse an explicit-instantiation.
8529
8530 explicit-instantiation:
21526606 8531 template declaration
a723baf1
MM
8532
8533 Although the standard says `declaration', what it really means is:
8534
8535 explicit-instantiation:
21526606 8536 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
8537
8538 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8539 supposed to be allowed. A defect report has been filed about this
21526606 8540 issue.
a723baf1
MM
8541
8542 GNU Extension:
21526606 8543
a723baf1 8544 explicit-instantiation:
21526606 8545 storage-class-specifier template
a723baf1 8546 decl-specifier-seq [opt] declarator [opt] ;
21526606 8547 function-specifier template
a723baf1
MM
8548 decl-specifier-seq [opt] declarator [opt] ; */
8549
8550static void
94edc4ab 8551cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8552{
560ad596 8553 int declares_class_or_enum;
a723baf1
MM
8554 tree decl_specifiers;
8555 tree attributes;
8556 tree extension_specifier = NULL_TREE;
8557
8558 /* Look for an (optional) storage-class-specifier or
8559 function-specifier. */
8560 if (cp_parser_allow_gnu_extensions_p (parser))
8561 {
21526606 8562 extension_specifier
a723baf1
MM
8563 = cp_parser_storage_class_specifier_opt (parser);
8564 if (!extension_specifier)
8565 extension_specifier = cp_parser_function_specifier_opt (parser);
8566 }
8567
8568 /* Look for the `template' keyword. */
8569 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8570 /* Let the front end know that we are processing an explicit
8571 instantiation. */
8572 begin_explicit_instantiation ();
8573 /* [temp.explicit] says that we are supposed to ignore access
8574 control while processing explicit instantiation directives. */
78757caa 8575 push_deferring_access_checks (dk_no_check);
a723baf1 8576 /* Parse a decl-specifier-seq. */
21526606 8577 decl_specifiers
a723baf1
MM
8578 = cp_parser_decl_specifier_seq (parser,
8579 CP_PARSER_FLAGS_OPTIONAL,
8580 &attributes,
8581 &declares_class_or_enum);
8582 /* If there was exactly one decl-specifier, and it declared a class,
8583 and there's no declarator, then we have an explicit type
8584 instantiation. */
8585 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8586 {
8587 tree type;
8588
8589 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8590 /* Turn access control back on for names used during
8591 template instantiation. */
8592 pop_deferring_access_checks ();
a723baf1
MM
8593 if (type)
8594 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8595 }
8596 else
8597 {
8598 tree declarator;
8599 tree decl;
8600
8601 /* Parse the declarator. */
21526606 8602 declarator
62b8a44e 8603 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
8604 /*ctor_dtor_or_conv_p=*/NULL,
8605 /*parenthesized_p=*/NULL);
21526606 8606 cp_parser_check_for_definition_in_return_type (declarator,
560ad596 8607 declares_class_or_enum);
216bb6e1
MM
8608 if (declarator != error_mark_node)
8609 {
21526606 8610 decl = grokdeclarator (declarator, decl_specifiers,
216bb6e1
MM
8611 NORMAL, 0, NULL);
8612 /* Turn access control back on for names used during
8613 template instantiation. */
8614 pop_deferring_access_checks ();
8615 /* Do the explicit instantiation. */
8616 do_decl_instantiation (decl, extension_specifier);
8617 }
8618 else
8619 {
8620 pop_deferring_access_checks ();
8621 /* Skip the body of the explicit instantiation. */
8622 cp_parser_skip_to_end_of_statement (parser);
8623 }
a723baf1
MM
8624 }
8625 /* We're done with the instantiation. */
8626 end_explicit_instantiation ();
a723baf1 8627
e0860732 8628 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8629}
8630
8631/* Parse an explicit-specialization.
8632
8633 explicit-specialization:
21526606 8634 template < > declaration
a723baf1
MM
8635
8636 Although the standard says `declaration', what it really means is:
8637
8638 explicit-specialization:
8639 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 8640 template <> function-definition
a723baf1
MM
8641 template <> explicit-specialization
8642 template <> template-declaration */
8643
8644static void
94edc4ab 8645cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8646{
8647 /* Look for the `template' keyword. */
8648 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8649 /* Look for the `<'. */
8650 cp_parser_require (parser, CPP_LESS, "`<'");
8651 /* Look for the `>'. */
8652 cp_parser_require (parser, CPP_GREATER, "`>'");
8653 /* We have processed another parameter list. */
8654 ++parser->num_template_parameter_lists;
8655 /* Let the front end know that we are beginning a specialization. */
8656 begin_specialization ();
8657
8658 /* If the next keyword is `template', we need to figure out whether
8659 or not we're looking a template-declaration. */
8660 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8661 {
8662 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8663 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8664 cp_parser_template_declaration_after_export (parser,
8665 /*member_p=*/false);
8666 else
8667 cp_parser_explicit_specialization (parser);
8668 }
8669 else
8670 /* Parse the dependent declaration. */
21526606 8671 cp_parser_single_declaration (parser,
a723baf1
MM
8672 /*member_p=*/false,
8673 /*friend_p=*/NULL);
8674
8675 /* We're done with the specialization. */
8676 end_specialization ();
8677 /* We're done with this parameter list. */
8678 --parser->num_template_parameter_lists;
8679}
8680
8681/* Parse a type-specifier.
8682
8683 type-specifier:
8684 simple-type-specifier
8685 class-specifier
8686 enum-specifier
8687 elaborated-type-specifier
8688 cv-qualifier
8689
8690 GNU Extension:
8691
8692 type-specifier:
8693 __complex__
8694
8695 Returns a representation of the type-specifier. If the
8696 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8697 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8698 For a class-specifier, enum-specifier, or elaborated-type-specifier
8699 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8700
8701 If IS_FRIEND is TRUE then this type-specifier is being declared a
8702 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8703 appearing in a decl-specifier-seq.
8704
8705 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8706 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8707 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8708 if a type is declared; 2 if it is defined. Otherwise, it is set to
8709 zero.
a723baf1
MM
8710
8711 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8712 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8713 is set to FALSE. */
8714
8715static tree
21526606
EC
8716cp_parser_type_specifier (cp_parser* parser,
8717 cp_parser_flags flags,
94edc4ab
NN
8718 bool is_friend,
8719 bool is_declaration,
560ad596 8720 int* declares_class_or_enum,
94edc4ab 8721 bool* is_cv_qualifier)
a723baf1
MM
8722{
8723 tree type_spec = NULL_TREE;
8724 cp_token *token;
8725 enum rid keyword;
8726
8727 /* Assume this type-specifier does not declare a new type. */
8728 if (declares_class_or_enum)
543ca912 8729 *declares_class_or_enum = 0;
a723baf1
MM
8730 /* And that it does not specify a cv-qualifier. */
8731 if (is_cv_qualifier)
8732 *is_cv_qualifier = false;
8733 /* Peek at the next token. */
8734 token = cp_lexer_peek_token (parser->lexer);
8735
8736 /* If we're looking at a keyword, we can use that to guide the
8737 production we choose. */
8738 keyword = token->keyword;
8739 switch (keyword)
8740 {
8741 /* Any of these indicate either a class-specifier, or an
8742 elaborated-type-specifier. */
8743 case RID_CLASS:
8744 case RID_STRUCT:
8745 case RID_UNION:
8746 case RID_ENUM:
8747 /* Parse tentatively so that we can back up if we don't find a
8748 class-specifier or enum-specifier. */
8749 cp_parser_parse_tentatively (parser);
8750 /* Look for the class-specifier or enum-specifier. */
8751 if (keyword == RID_ENUM)
8752 type_spec = cp_parser_enum_specifier (parser);
8753 else
8754 type_spec = cp_parser_class_specifier (parser);
8755
8756 /* If that worked, we're done. */
8757 if (cp_parser_parse_definitely (parser))
8758 {
8759 if (declares_class_or_enum)
560ad596 8760 *declares_class_or_enum = 2;
a723baf1
MM
8761 return type_spec;
8762 }
8763
8764 /* Fall through. */
8765
8766 case RID_TYPENAME:
8767 /* Look for an elaborated-type-specifier. */
8768 type_spec = cp_parser_elaborated_type_specifier (parser,
8769 is_friend,
8770 is_declaration);
8771 /* We're declaring a class or enum -- unless we're using
8772 `typename'. */
8773 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8774 *declares_class_or_enum = 1;
a723baf1
MM
8775 return type_spec;
8776
8777 case RID_CONST:
8778 case RID_VOLATILE:
8779 case RID_RESTRICT:
8780 type_spec = cp_parser_cv_qualifier_opt (parser);
8781 /* Even though we call a routine that looks for an optional
8782 qualifier, we know that there should be one. */
8783 my_friendly_assert (type_spec != NULL, 20000328);
8784 /* This type-specifier was a cv-qualified. */
8785 if (is_cv_qualifier)
8786 *is_cv_qualifier = true;
8787
8788 return type_spec;
8789
8790 case RID_COMPLEX:
8791 /* The `__complex__' keyword is a GNU extension. */
8792 return cp_lexer_consume_token (parser->lexer)->value;
8793
8794 default:
8795 break;
8796 }
8797
8798 /* If we do not already have a type-specifier, assume we are looking
8799 at a simple-type-specifier. */
21526606 8800 type_spec = cp_parser_simple_type_specifier (parser, flags,
4b0d3cbe 8801 /*identifier_p=*/true);
a723baf1
MM
8802
8803 /* If we didn't find a type-specifier, and a type-specifier was not
8804 optional in this context, issue an error message. */
8805 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8806 {
8807 cp_parser_error (parser, "expected type specifier");
8808 return error_mark_node;
8809 }
8810
8811 return type_spec;
8812}
8813
8814/* Parse a simple-type-specifier.
8815
8816 simple-type-specifier:
8817 :: [opt] nested-name-specifier [opt] type-name
8818 :: [opt] nested-name-specifier template template-id
8819 char
8820 wchar_t
8821 bool
8822 short
8823 int
8824 long
8825 signed
8826 unsigned
8827 float
8828 double
21526606 8829 void
a723baf1
MM
8830
8831 GNU Extension:
8832
8833 simple-type-specifier:
8834 __typeof__ unary-expression
8835 __typeof__ ( type-id )
8836
8837 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8838 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8839 For the first two productions, and if IDENTIFIER_P is false, the
8840 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8841
8842static tree
4b0d3cbe
MM
8843cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8844 bool identifier_p)
a723baf1
MM
8845{
8846 tree type = NULL_TREE;
8847 cp_token *token;
8848
8849 /* Peek at the next token. */
8850 token = cp_lexer_peek_token (parser->lexer);
8851
8852 /* If we're looking at a keyword, things are easy. */
8853 switch (token->keyword)
8854 {
8855 case RID_CHAR:
4b0d3cbe
MM
8856 type = char_type_node;
8857 break;
a723baf1 8858 case RID_WCHAR:
4b0d3cbe
MM
8859 type = wchar_type_node;
8860 break;
a723baf1 8861 case RID_BOOL:
4b0d3cbe
MM
8862 type = boolean_type_node;
8863 break;
a723baf1 8864 case RID_SHORT:
4b0d3cbe
MM
8865 type = short_integer_type_node;
8866 break;
a723baf1 8867 case RID_INT:
4b0d3cbe
MM
8868 type = integer_type_node;
8869 break;
a723baf1 8870 case RID_LONG:
4b0d3cbe
MM
8871 type = long_integer_type_node;
8872 break;
a723baf1 8873 case RID_SIGNED:
4b0d3cbe
MM
8874 type = integer_type_node;
8875 break;
a723baf1 8876 case RID_UNSIGNED:
4b0d3cbe
MM
8877 type = unsigned_type_node;
8878 break;
a723baf1 8879 case RID_FLOAT:
4b0d3cbe
MM
8880 type = float_type_node;
8881 break;
a723baf1 8882 case RID_DOUBLE:
4b0d3cbe
MM
8883 type = double_type_node;
8884 break;
a723baf1 8885 case RID_VOID:
4b0d3cbe
MM
8886 type = void_type_node;
8887 break;
a723baf1
MM
8888
8889 case RID_TYPEOF:
8890 {
8891 tree operand;
8892
8893 /* Consume the `typeof' token. */
8894 cp_lexer_consume_token (parser->lexer);
04c06002 8895 /* Parse the operand to `typeof'. */
a723baf1
MM
8896 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8897 /* If it is not already a TYPE, take its type. */
8898 if (!TYPE_P (operand))
8899 operand = finish_typeof (operand);
8900
8901 return operand;
8902 }
8903
8904 default:
8905 break;
8906 }
8907
4b0d3cbe
MM
8908 /* If the type-specifier was for a built-in type, we're done. */
8909 if (type)
8910 {
8911 tree id;
8912
8913 /* Consume the token. */
8914 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
8915
8916 /* There is no valid C++ program where a non-template type is
8917 followed by a "<". That usually indicates that the user thought
8918 that the type was a template. */
8919 cp_parser_check_for_invalid_template_id (parser, type);
8920
4b0d3cbe
MM
8921 return identifier_p ? id : TYPE_NAME (type);
8922 }
8923
a723baf1 8924 /* The type-specifier must be a user-defined type. */
21526606 8925 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1
MM
8926 {
8927 /* Don't gobble tokens or issue error messages if this is an
8928 optional type-specifier. */
8929 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8930 cp_parser_parse_tentatively (parser);
8931
8932 /* Look for the optional `::' operator. */
8933 cp_parser_global_scope_opt (parser,
8934 /*current_scope_valid_p=*/false);
8935 /* Look for the nested-name specifier. */
8936 cp_parser_nested_name_specifier_opt (parser,
8937 /*typename_keyword_p=*/false,
8938 /*check_dependency_p=*/true,
a668c6ad
MM
8939 /*type_p=*/false,
8940 /*is_declaration=*/false);
a723baf1
MM
8941 /* If we have seen a nested-name-specifier, and the next token
8942 is `template', then we are using the template-id production. */
21526606 8943 if (parser->scope
a723baf1
MM
8944 && cp_parser_optional_template_keyword (parser))
8945 {
8946 /* Look for the template-id. */
21526606 8947 type = cp_parser_template_id (parser,
a723baf1 8948 /*template_keyword_p=*/true,
a668c6ad
MM
8949 /*check_dependency_p=*/true,
8950 /*is_declaration=*/false);
a723baf1
MM
8951 /* If the template-id did not name a type, we are out of
8952 luck. */
8953 if (TREE_CODE (type) != TYPE_DECL)
8954 {
8955 cp_parser_error (parser, "expected template-id for type");
8956 type = NULL_TREE;
8957 }
8958 }
8959 /* Otherwise, look for a type-name. */
8960 else
4bb8ca28 8961 type = cp_parser_type_name (parser);
a723baf1 8962 /* If it didn't work out, we don't have a TYPE. */
21526606 8963 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
8964 && !cp_parser_parse_definitely (parser))
8965 type = NULL_TREE;
8966 }
8967
8968 /* If we didn't get a type-name, issue an error message. */
8969 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8970 {
8971 cp_parser_error (parser, "expected type-name");
8972 return error_mark_node;
8973 }
8974
a668c6ad
MM
8975 /* There is no valid C++ program where a non-template type is
8976 followed by a "<". That usually indicates that the user thought
8977 that the type was a template. */
4bb8ca28 8978 if (type && type != error_mark_node)
ee43dab5 8979 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 8980
a723baf1
MM
8981 return type;
8982}
8983
8984/* Parse a type-name.
8985
8986 type-name:
8987 class-name
8988 enum-name
21526606 8989 typedef-name
a723baf1
MM
8990
8991 enum-name:
8992 identifier
8993
8994 typedef-name:
21526606 8995 identifier
a723baf1
MM
8996
8997 Returns a TYPE_DECL for the the type. */
8998
8999static tree
94edc4ab 9000cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9001{
9002 tree type_decl;
9003 tree identifier;
9004
9005 /* We can't know yet whether it is a class-name or not. */
9006 cp_parser_parse_tentatively (parser);
9007 /* Try a class-name. */
21526606 9008 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9009 /*typename_keyword_p=*/false,
9010 /*template_keyword_p=*/false,
9011 /*type_p=*/false,
a723baf1 9012 /*check_dependency_p=*/true,
a668c6ad
MM
9013 /*class_head_p=*/false,
9014 /*is_declaration=*/false);
a723baf1
MM
9015 /* If it's not a class-name, keep looking. */
9016 if (!cp_parser_parse_definitely (parser))
9017 {
9018 /* It must be a typedef-name or an enum-name. */
9019 identifier = cp_parser_identifier (parser);
9020 if (identifier == error_mark_node)
9021 return error_mark_node;
21526606 9022
a723baf1
MM
9023 /* Look up the type-name. */
9024 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9025 /* Issue an error if we did not find a type-name. */
9026 if (TREE_CODE (type_decl) != TYPE_DECL)
9027 {
4bb8ca28 9028 if (!cp_parser_simulate_error (parser))
21526606 9029 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9030 "is not a type");
a723baf1
MM
9031 type_decl = error_mark_node;
9032 }
9033 /* Remember that the name was used in the definition of the
9034 current class so that we can check later to see if the
9035 meaning would have been different after the class was
9036 entirely defined. */
9037 else if (type_decl != error_mark_node
9038 && !parser->scope)
9039 maybe_note_name_used_in_class (identifier, type_decl);
9040 }
21526606 9041
a723baf1
MM
9042 return type_decl;
9043}
9044
9045
9046/* Parse an elaborated-type-specifier. Note that the grammar given
9047 here incorporates the resolution to DR68.
9048
9049 elaborated-type-specifier:
9050 class-key :: [opt] nested-name-specifier [opt] identifier
9051 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9052 enum :: [opt] nested-name-specifier [opt] identifier
9053 typename :: [opt] nested-name-specifier identifier
21526606
EC
9054 typename :: [opt] nested-name-specifier template [opt]
9055 template-id
a723baf1 9056
360d1b99
MM
9057 GNU extension:
9058
9059 elaborated-type-specifier:
9060 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9061 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9062 template [opt] template-id
9063 enum attributes :: [opt] nested-name-specifier [opt] identifier
9064
a723baf1
MM
9065 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9066 declared `friend'. If IS_DECLARATION is TRUE, then this
9067 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9068 something is being declared.
9069
9070 Returns the TYPE specified. */
9071
9072static tree
21526606
EC
9073cp_parser_elaborated_type_specifier (cp_parser* parser,
9074 bool is_friend,
94edc4ab 9075 bool is_declaration)
a723baf1
MM
9076{
9077 enum tag_types tag_type;
9078 tree identifier;
9079 tree type = NULL_TREE;
360d1b99 9080 tree attributes = NULL_TREE;
a723baf1
MM
9081
9082 /* See if we're looking at the `enum' keyword. */
9083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9084 {
9085 /* Consume the `enum' token. */
9086 cp_lexer_consume_token (parser->lexer);
9087 /* Remember that it's an enumeration type. */
9088 tag_type = enum_type;
360d1b99
MM
9089 /* Parse the attributes. */
9090 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9091 }
9092 /* Or, it might be `typename'. */
9093 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9094 RID_TYPENAME))
9095 {
9096 /* Consume the `typename' token. */
9097 cp_lexer_consume_token (parser->lexer);
9098 /* Remember that it's a `typename' type. */
9099 tag_type = typename_type;
9100 /* The `typename' keyword is only allowed in templates. */
9101 if (!processing_template_decl)
9102 pedwarn ("using `typename' outside of template");
9103 }
9104 /* Otherwise it must be a class-key. */
9105 else
9106 {
9107 tag_type = cp_parser_class_key (parser);
9108 if (tag_type == none_type)
9109 return error_mark_node;
360d1b99
MM
9110 /* Parse the attributes. */
9111 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9112 }
9113
9114 /* Look for the `::' operator. */
21526606 9115 cp_parser_global_scope_opt (parser,
a723baf1
MM
9116 /*current_scope_valid_p=*/false);
9117 /* Look for the nested-name-specifier. */
9118 if (tag_type == typename_type)
8fa1ad0e
MM
9119 {
9120 if (cp_parser_nested_name_specifier (parser,
9121 /*typename_keyword_p=*/true,
9122 /*check_dependency_p=*/true,
a668c6ad 9123 /*type_p=*/true,
21526606 9124 is_declaration)
8fa1ad0e
MM
9125 == error_mark_node)
9126 return error_mark_node;
9127 }
a723baf1
MM
9128 else
9129 /* Even though `typename' is not present, the proposed resolution
9130 to Core Issue 180 says that in `class A<T>::B', `B' should be
9131 considered a type-name, even if `A<T>' is dependent. */
9132 cp_parser_nested_name_specifier_opt (parser,
9133 /*typename_keyword_p=*/true,
9134 /*check_dependency_p=*/true,
a668c6ad
MM
9135 /*type_p=*/true,
9136 is_declaration);
a723baf1
MM
9137 /* For everything but enumeration types, consider a template-id. */
9138 if (tag_type != enum_type)
9139 {
9140 bool template_p = false;
9141 tree decl;
9142
9143 /* Allow the `template' keyword. */
9144 template_p = cp_parser_optional_template_keyword (parser);
9145 /* If we didn't see `template', we don't know if there's a
9146 template-id or not. */
9147 if (!template_p)
9148 cp_parser_parse_tentatively (parser);
9149 /* Parse the template-id. */
9150 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9151 /*check_dependency_p=*/true,
9152 is_declaration);
a723baf1
MM
9153 /* If we didn't find a template-id, look for an ordinary
9154 identifier. */
9155 if (!template_p && !cp_parser_parse_definitely (parser))
9156 ;
9157 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9158 in effect, then we must assume that, upon instantiation, the
9159 template will correspond to a class. */
9160 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9161 && tag_type == typename_type)
9162 type = make_typename_type (parser->scope, decl,
9163 /*complain=*/1);
21526606 9164 else
a723baf1
MM
9165 type = TREE_TYPE (decl);
9166 }
9167
9168 /* For an enumeration type, consider only a plain identifier. */
9169 if (!type)
9170 {
9171 identifier = cp_parser_identifier (parser);
9172
9173 if (identifier == error_mark_node)
eb5abb39
NS
9174 {
9175 parser->scope = NULL_TREE;
9176 return error_mark_node;
9177 }
a723baf1
MM
9178
9179 /* For a `typename', we needn't call xref_tag. */
9180 if (tag_type == typename_type)
21526606 9181 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9182 identifier);
a723baf1
MM
9183 /* Look up a qualified name in the usual way. */
9184 if (parser->scope)
9185 {
9186 tree decl;
9187
9188 /* In an elaborated-type-specifier, names are assumed to name
9189 types, so we set IS_TYPE to TRUE when calling
9190 cp_parser_lookup_name. */
21526606 9191 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 9192 /*is_type=*/true,
b0bc6e8e 9193 /*is_template=*/false,
eea9800f 9194 /*is_namespace=*/false,
a723baf1 9195 /*check_dependency=*/true);
710b73e6
KL
9196
9197 /* If we are parsing friend declaration, DECL may be a
9198 TEMPLATE_DECL tree node here. However, we need to check
9199 whether this TEMPLATE_DECL results in valid code. Consider
9200 the following example:
9201
9202 namespace N {
9203 template <class T> class C {};
9204 }
9205 class X {
9206 template <class T> friend class N::C; // #1, valid code
9207 };
9208 template <class T> class Y {
9209 friend class N::C; // #2, invalid code
9210 };
9211
9212 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9213 name lookup of `N::C'. We see that friend declaration must
9214 be template for the code to be valid. Note that
9215 processing_template_decl does not work here since it is
9216 always 1 for the above two cases. */
9217
21526606 9218 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9219 (decl, /*tag_name_p=*/is_friend
9220 && parser->num_template_parameter_lists));
a723baf1
MM
9221
9222 if (TREE_CODE (decl) != TYPE_DECL)
9223 {
9224 error ("expected type-name");
9225 return error_mark_node;
9226 }
560ad596
MM
9227
9228 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9229 check_elaborated_type_specifier
4b0d3cbe 9230 (tag_type, decl,
560ad596
MM
9231 (parser->num_template_parameter_lists
9232 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9233
9234 type = TREE_TYPE (decl);
9235 }
21526606 9236 else
a723baf1
MM
9237 {
9238 /* An elaborated-type-specifier sometimes introduces a new type and
9239 sometimes names an existing type. Normally, the rule is that it
9240 introduces a new type only if there is not an existing type of
9241 the same name already in scope. For example, given:
9242
9243 struct S {};
9244 void f() { struct S s; }
9245
9246 the `struct S' in the body of `f' is the same `struct S' as in
9247 the global scope; the existing definition is used. However, if
21526606 9248 there were no global declaration, this would introduce a new
a723baf1
MM
9249 local class named `S'.
9250
9251 An exception to this rule applies to the following code:
9252
9253 namespace N { struct S; }
9254
9255 Here, the elaborated-type-specifier names a new type
9256 unconditionally; even if there is already an `S' in the
9257 containing scope this declaration names a new type.
9258 This exception only applies if the elaborated-type-specifier
9259 forms the complete declaration:
9260
21526606 9261 [class.name]
a723baf1
MM
9262
9263 A declaration consisting solely of `class-key identifier ;' is
9264 either a redeclaration of the name in the current scope or a
9265 forward declaration of the identifier as a class name. It
9266 introduces the name into the current scope.
9267
9268 We are in this situation precisely when the next token is a `;'.
9269
9270 An exception to the exception is that a `friend' declaration does
9271 *not* name a new type; i.e., given:
9272
9273 struct S { friend struct T; };
9274
21526606 9275 `T' is not a new type in the scope of `S'.
a723baf1
MM
9276
9277 Also, `new struct S' or `sizeof (struct S)' never results in the
9278 definition of a new type; a new type can only be declared in a
9bcb9aae 9279 declaration context. */
a723baf1 9280
e0fed25b
DS
9281 /* Warn about attributes. They are ignored. */
9282 if (attributes)
9283 warning ("type attributes are honored only at type definition");
9284
21526606 9285 type = xref_tag (tag_type, identifier,
21526606 9286 (is_friend
a723baf1 9287 || !is_declaration
21526606 9288 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
9289 CPP_SEMICOLON)),
9290 parser->num_template_parameter_lists);
a723baf1
MM
9291 }
9292 }
9293 if (tag_type != enum_type)
9294 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9295
9296 /* A "<" cannot follow an elaborated type specifier. If that
9297 happens, the user was probably trying to form a template-id. */
9298 cp_parser_check_for_invalid_template_id (parser, type);
9299
a723baf1
MM
9300 return type;
9301}
9302
9303/* Parse an enum-specifier.
9304
9305 enum-specifier:
9306 enum identifier [opt] { enumerator-list [opt] }
9307
9308 Returns an ENUM_TYPE representing the enumeration. */
9309
9310static tree
94edc4ab 9311cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9312{
9313 cp_token *token;
9314 tree identifier = NULL_TREE;
9315 tree type;
9316
9317 /* Look for the `enum' keyword. */
9318 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9319 return error_mark_node;
9320 /* Peek at the next token. */
9321 token = cp_lexer_peek_token (parser->lexer);
9322
9323 /* See if it is an identifier. */
9324 if (token->type == CPP_NAME)
9325 identifier = cp_parser_identifier (parser);
9326
9327 /* Look for the `{'. */
9328 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9329 return error_mark_node;
9330
9331 /* At this point, we're going ahead with the enum-specifier, even
9332 if some other problem occurs. */
9333 cp_parser_commit_to_tentative_parse (parser);
9334
9335 /* Issue an error message if type-definitions are forbidden here. */
9336 cp_parser_check_type_definition (parser);
9337
9338 /* Create the new type. */
9339 type = start_enum (identifier ? identifier : make_anon_name ());
9340
9341 /* Peek at the next token. */
9342 token = cp_lexer_peek_token (parser->lexer);
9343 /* If it's not a `}', then there are some enumerators. */
9344 if (token->type != CPP_CLOSE_BRACE)
9345 cp_parser_enumerator_list (parser, type);
9346 /* Look for the `}'. */
9347 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9348
9349 /* Finish up the enumeration. */
9350 finish_enum (type);
9351
9352 return type;
9353}
9354
9355/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9356 TYPE.
a723baf1
MM
9357
9358 enumerator-list:
9359 enumerator-definition
9360 enumerator-list , enumerator-definition */
9361
9362static void
94edc4ab 9363cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9364{
9365 while (true)
9366 {
9367 cp_token *token;
9368
9369 /* Parse an enumerator-definition. */
9370 cp_parser_enumerator_definition (parser, type);
9371 /* Peek at the next token. */
9372 token = cp_lexer_peek_token (parser->lexer);
21526606 9373 /* If it's not a `,', then we've reached the end of the
a723baf1
MM
9374 list. */
9375 if (token->type != CPP_COMMA)
9376 break;
9377 /* Otherwise, consume the `,' and keep going. */
9378 cp_lexer_consume_token (parser->lexer);
9379 /* If the next token is a `}', there is a trailing comma. */
9380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9381 {
9382 if (pedantic && !in_system_header)
9383 pedwarn ("comma at end of enumerator list");
9384 break;
9385 }
9386 }
9387}
9388
9389/* Parse an enumerator-definition. The enumerator has the indicated
9390 TYPE.
9391
9392 enumerator-definition:
9393 enumerator
9394 enumerator = constant-expression
21526606 9395
a723baf1
MM
9396 enumerator:
9397 identifier */
9398
9399static void
94edc4ab 9400cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9401{
9402 cp_token *token;
9403 tree identifier;
9404 tree value;
9405
9406 /* Look for the identifier. */
9407 identifier = cp_parser_identifier (parser);
9408 if (identifier == error_mark_node)
9409 return;
21526606 9410
a723baf1
MM
9411 /* Peek at the next token. */
9412 token = cp_lexer_peek_token (parser->lexer);
9413 /* If it's an `=', then there's an explicit value. */
9414 if (token->type == CPP_EQ)
9415 {
9416 /* Consume the `=' token. */
9417 cp_lexer_consume_token (parser->lexer);
9418 /* Parse the value. */
21526606 9419 value = cp_parser_constant_expression (parser,
d17811fd 9420 /*allow_non_constant_p=*/false,
14d22dd6 9421 NULL);
a723baf1
MM
9422 }
9423 else
9424 value = NULL_TREE;
9425
9426 /* Create the enumerator. */
9427 build_enumerator (identifier, value, type);
9428}
9429
9430/* Parse a namespace-name.
9431
9432 namespace-name:
9433 original-namespace-name
9434 namespace-alias
9435
9436 Returns the NAMESPACE_DECL for the namespace. */
9437
9438static tree
94edc4ab 9439cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9440{
9441 tree identifier;
9442 tree namespace_decl;
9443
9444 /* Get the name of the namespace. */
9445 identifier = cp_parser_identifier (parser);
9446 if (identifier == error_mark_node)
9447 return error_mark_node;
9448
eea9800f
MM
9449 /* Look up the identifier in the currently active scope. Look only
9450 for namespaces, due to:
9451
9452 [basic.lookup.udir]
9453
9454 When looking up a namespace-name in a using-directive or alias
21526606 9455 definition, only namespace names are considered.
eea9800f
MM
9456
9457 And:
9458
9459 [basic.lookup.qual]
9460
9461 During the lookup of a name preceding the :: scope resolution
21526606 9462 operator, object, function, and enumerator names are ignored.
eea9800f
MM
9463
9464 (Note that cp_parser_class_or_namespace_name only calls this
9465 function if the token after the name is the scope resolution
9466 operator.) */
9467 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f 9468 /*is_type=*/false,
b0bc6e8e 9469 /*is_template=*/false,
eea9800f
MM
9470 /*is_namespace=*/true,
9471 /*check_dependency=*/true);
a723baf1
MM
9472 /* If it's not a namespace, issue an error. */
9473 if (namespace_decl == error_mark_node
9474 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9475 {
9476 cp_parser_error (parser, "expected namespace-name");
9477 namespace_decl = error_mark_node;
9478 }
21526606 9479
a723baf1
MM
9480 return namespace_decl;
9481}
9482
9483/* Parse a namespace-definition.
9484
9485 namespace-definition:
9486 named-namespace-definition
21526606 9487 unnamed-namespace-definition
a723baf1
MM
9488
9489 named-namespace-definition:
9490 original-namespace-definition
9491 extension-namespace-definition
9492
9493 original-namespace-definition:
9494 namespace identifier { namespace-body }
21526606 9495
a723baf1
MM
9496 extension-namespace-definition:
9497 namespace original-namespace-name { namespace-body }
21526606 9498
a723baf1
MM
9499 unnamed-namespace-definition:
9500 namespace { namespace-body } */
9501
9502static void
94edc4ab 9503cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9504{
9505 tree identifier;
9506
9507 /* Look for the `namespace' keyword. */
9508 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9509
9510 /* Get the name of the namespace. We do not attempt to distinguish
9511 between an original-namespace-definition and an
9512 extension-namespace-definition at this point. The semantic
9513 analysis routines are responsible for that. */
9514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9515 identifier = cp_parser_identifier (parser);
9516 else
9517 identifier = NULL_TREE;
9518
9519 /* Look for the `{' to start the namespace. */
9520 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9521 /* Start the namespace. */
9522 push_namespace (identifier);
9523 /* Parse the body of the namespace. */
9524 cp_parser_namespace_body (parser);
9525 /* Finish the namespace. */
9526 pop_namespace ();
9527 /* Look for the final `}'. */
9528 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9529}
9530
9531/* Parse a namespace-body.
9532
9533 namespace-body:
9534 declaration-seq [opt] */
9535
9536static void
94edc4ab 9537cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9538{
9539 cp_parser_declaration_seq_opt (parser);
9540}
9541
9542/* Parse a namespace-alias-definition.
9543
9544 namespace-alias-definition:
9545 namespace identifier = qualified-namespace-specifier ; */
9546
9547static void
94edc4ab 9548cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9549{
9550 tree identifier;
9551 tree namespace_specifier;
9552
9553 /* Look for the `namespace' keyword. */
9554 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9555 /* Look for the identifier. */
9556 identifier = cp_parser_identifier (parser);
9557 if (identifier == error_mark_node)
9558 return;
9559 /* Look for the `=' token. */
9560 cp_parser_require (parser, CPP_EQ, "`='");
9561 /* Look for the qualified-namespace-specifier. */
21526606 9562 namespace_specifier
a723baf1
MM
9563 = cp_parser_qualified_namespace_specifier (parser);
9564 /* Look for the `;' token. */
9565 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9566
9567 /* Register the alias in the symbol table. */
9568 do_namespace_alias (identifier, namespace_specifier);
9569}
9570
9571/* Parse a qualified-namespace-specifier.
9572
9573 qualified-namespace-specifier:
9574 :: [opt] nested-name-specifier [opt] namespace-name
9575
9576 Returns a NAMESPACE_DECL corresponding to the specified
9577 namespace. */
9578
9579static tree
94edc4ab 9580cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9581{
9582 /* Look for the optional `::'. */
21526606 9583 cp_parser_global_scope_opt (parser,
a723baf1
MM
9584 /*current_scope_valid_p=*/false);
9585
9586 /* Look for the optional nested-name-specifier. */
9587 cp_parser_nested_name_specifier_opt (parser,
9588 /*typename_keyword_p=*/false,
9589 /*check_dependency_p=*/true,
a668c6ad
MM
9590 /*type_p=*/false,
9591 /*is_declaration=*/true);
a723baf1
MM
9592
9593 return cp_parser_namespace_name (parser);
9594}
9595
9596/* Parse a using-declaration.
9597
9598 using-declaration:
9599 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9600 using :: unqualified-id ; */
9601
9602static void
94edc4ab 9603cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9604{
9605 cp_token *token;
9606 bool typename_p = false;
9607 bool global_scope_p;
9608 tree decl;
9609 tree identifier;
9610 tree scope;
ed5f054f 9611 tree qscope;
a723baf1
MM
9612
9613 /* Look for the `using' keyword. */
9614 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 9615
a723baf1
MM
9616 /* Peek at the next token. */
9617 token = cp_lexer_peek_token (parser->lexer);
9618 /* See if it's `typename'. */
9619 if (token->keyword == RID_TYPENAME)
9620 {
9621 /* Remember that we've seen it. */
9622 typename_p = true;
9623 /* Consume the `typename' token. */
9624 cp_lexer_consume_token (parser->lexer);
9625 }
9626
9627 /* Look for the optional global scope qualification. */
21526606 9628 global_scope_p
a723baf1 9629 = (cp_parser_global_scope_opt (parser,
21526606 9630 /*current_scope_valid_p=*/false)
a723baf1
MM
9631 != NULL_TREE);
9632
9633 /* If we saw `typename', or didn't see `::', then there must be a
9634 nested-name-specifier present. */
9635 if (typename_p || !global_scope_p)
21526606 9636 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
9637 /*check_dependency_p=*/true,
9638 /*type_p=*/false,
9639 /*is_declaration=*/true);
a723baf1
MM
9640 /* Otherwise, we could be in either of the two productions. In that
9641 case, treat the nested-name-specifier as optional. */
9642 else
ed5f054f
AO
9643 qscope = cp_parser_nested_name_specifier_opt (parser,
9644 /*typename_keyword_p=*/false,
9645 /*check_dependency_p=*/true,
9646 /*type_p=*/false,
9647 /*is_declaration=*/true);
9648 if (!qscope)
9649 qscope = global_namespace;
a723baf1
MM
9650
9651 /* Parse the unqualified-id. */
21526606 9652 identifier = cp_parser_unqualified_id (parser,
a723baf1 9653 /*template_keyword_p=*/false,
f3c2dfc6
MM
9654 /*check_dependency_p=*/true,
9655 /*declarator_p=*/true);
a723baf1
MM
9656
9657 /* The function we call to handle a using-declaration is different
9658 depending on what scope we are in. */
f3c2dfc6
MM
9659 if (identifier == error_mark_node)
9660 ;
9661 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9662 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9663 /* [namespace.udecl]
9664
9665 A using declaration shall not name a template-id. */
9666 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9667 else
9668 {
f3c2dfc6
MM
9669 scope = current_scope ();
9670 if (scope && TYPE_P (scope))
4eb6d609 9671 {
f3c2dfc6
MM
9672 /* Create the USING_DECL. */
9673 decl = do_class_using_decl (build_nt (SCOPE_REF,
9674 parser->scope,
9675 identifier));
9676 /* Add it to the list of members in this class. */
9677 finish_member_declaration (decl);
4eb6d609 9678 }
a723baf1 9679 else
f3c2dfc6
MM
9680 {
9681 decl = cp_parser_lookup_name_simple (parser, identifier);
9682 if (decl == error_mark_node)
4bb8ca28 9683 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6 9684 else if (scope)
ed5f054f 9685 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 9686 else
ed5f054f 9687 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 9688 }
a723baf1
MM
9689 }
9690
9691 /* Look for the final `;'. */
9692 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9693}
9694
21526606
EC
9695/* Parse a using-directive.
9696
a723baf1
MM
9697 using-directive:
9698 using namespace :: [opt] nested-name-specifier [opt]
9699 namespace-name ; */
9700
9701static void
94edc4ab 9702cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9703{
9704 tree namespace_decl;
86098eb8 9705 tree attribs;
a723baf1
MM
9706
9707 /* Look for the `using' keyword. */
9708 cp_parser_require_keyword (parser, RID_USING, "`using'");
9709 /* And the `namespace' keyword. */
9710 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9711 /* Look for the optional `::' operator. */
9712 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9713 /* And the optional nested-name-specifier. */
a723baf1
MM
9714 cp_parser_nested_name_specifier_opt (parser,
9715 /*typename_keyword_p=*/false,
9716 /*check_dependency_p=*/true,
a668c6ad
MM
9717 /*type_p=*/false,
9718 /*is_declaration=*/true);
a723baf1
MM
9719 /* Get the namespace being used. */
9720 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
9721 /* And any specified attributes. */
9722 attribs = cp_parser_attributes_opt (parser);
a723baf1 9723 /* Update the symbol table. */
86098eb8 9724 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
9725 /* Look for the final `;'. */
9726 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9727}
9728
9729/* Parse an asm-definition.
9730
9731 asm-definition:
21526606 9732 asm ( string-literal ) ;
a723baf1
MM
9733
9734 GNU Extension:
9735
9736 asm-definition:
9737 asm volatile [opt] ( string-literal ) ;
9738 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9739 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9740 : asm-operand-list [opt] ) ;
21526606
EC
9741 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9742 : asm-operand-list [opt]
a723baf1
MM
9743 : asm-operand-list [opt] ) ; */
9744
9745static void
94edc4ab 9746cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9747{
9748 cp_token *token;
9749 tree string;
9750 tree outputs = NULL_TREE;
9751 tree inputs = NULL_TREE;
9752 tree clobbers = NULL_TREE;
9753 tree asm_stmt;
9754 bool volatile_p = false;
9755 bool extended_p = false;
9756
9757 /* Look for the `asm' keyword. */
9758 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9759 /* See if the next token is `volatile'. */
9760 if (cp_parser_allow_gnu_extensions_p (parser)
9761 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9762 {
9763 /* Remember that we saw the `volatile' keyword. */
9764 volatile_p = true;
9765 /* Consume the token. */
9766 cp_lexer_consume_token (parser->lexer);
9767 }
9768 /* Look for the opening `('. */
9769 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9770 /* Look for the string. */
21526606 9771 c_lex_string_translate = false;
a723baf1
MM
9772 token = cp_parser_require (parser, CPP_STRING, "asm body");
9773 if (!token)
21526606 9774 goto finish;
a723baf1
MM
9775 string = token->value;
9776 /* If we're allowing GNU extensions, check for the extended assembly
21526606 9777 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
9778 a space in C, and so, for compatibility, we tolerate that here
9779 too. Doing that means that we have to treat the `::' operator as
9780 two `:' tokens. */
9781 if (cp_parser_allow_gnu_extensions_p (parser)
9782 && at_function_scope_p ()
9783 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9784 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9785 {
9786 bool inputs_p = false;
9787 bool clobbers_p = false;
9788
9789 /* The extended syntax was used. */
9790 extended_p = true;
9791
9792 /* Look for outputs. */
9793 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9794 {
9795 /* Consume the `:'. */
9796 cp_lexer_consume_token (parser->lexer);
9797 /* Parse the output-operands. */
21526606 9798 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
9799 CPP_COLON)
9800 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9801 CPP_SCOPE)
9802 && cp_lexer_next_token_is_not (parser->lexer,
9803 CPP_CLOSE_PAREN))
a723baf1
MM
9804 outputs = cp_parser_asm_operand_list (parser);
9805 }
9806 /* If the next token is `::', there are no outputs, and the
9807 next token is the beginning of the inputs. */
9808 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9809 {
9810 /* Consume the `::' token. */
9811 cp_lexer_consume_token (parser->lexer);
9812 /* The inputs are coming next. */
9813 inputs_p = true;
9814 }
9815
9816 /* Look for inputs. */
9817 if (inputs_p
9818 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9819 {
9820 if (!inputs_p)
9821 /* Consume the `:'. */
9822 cp_lexer_consume_token (parser->lexer);
9823 /* Parse the output-operands. */
21526606 9824 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
9825 CPP_COLON)
9826 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9827 CPP_SCOPE)
9828 && cp_lexer_next_token_is_not (parser->lexer,
9829 CPP_CLOSE_PAREN))
a723baf1
MM
9830 inputs = cp_parser_asm_operand_list (parser);
9831 }
9832 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9833 /* The clobbers are coming next. */
9834 clobbers_p = true;
9835
9836 /* Look for clobbers. */
21526606 9837 if (clobbers_p
a723baf1
MM
9838 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9839 {
9840 if (!clobbers_p)
9841 /* Consume the `:'. */
9842 cp_lexer_consume_token (parser->lexer);
9843 /* Parse the clobbers. */
8caf4c38
MM
9844 if (cp_lexer_next_token_is_not (parser->lexer,
9845 CPP_CLOSE_PAREN))
9846 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9847 }
9848 }
9849 /* Look for the closing `)'. */
9850 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
9851 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9852 /*consume_paren=*/true);
a723baf1
MM
9853 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9854
9855 /* Create the ASM_STMT. */
9856 if (at_function_scope_p ())
9857 {
21526606
EC
9858 asm_stmt =
9859 finish_asm_stmt (volatile_p
a723baf1
MM
9860 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9861 string, outputs, inputs, clobbers);
9862 /* If the extended syntax was not used, mark the ASM_STMT. */
9863 if (!extended_p)
9864 ASM_INPUT_P (asm_stmt) = 1;
9865 }
9866 else
9867 assemble_asm (string);
21526606
EC
9868
9869 finish:
9870 c_lex_string_translate = true;
a723baf1
MM
9871}
9872
9873/* Declarators [gram.dcl.decl] */
9874
9875/* Parse an init-declarator.
9876
9877 init-declarator:
9878 declarator initializer [opt]
9879
9880 GNU Extension:
9881
9882 init-declarator:
9883 declarator asm-specification [opt] attributes [opt] initializer [opt]
9884
4bb8ca28
MM
9885 function-definition:
9886 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
9887 function-body
9888 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
9889
9890 GNU Extension:
9891
9892 function-definition:
21526606 9893 __extension__ function-definition
4bb8ca28 9894
a723baf1 9895 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9896 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9897 then this declarator appears in a class scope. The new DECL created
9898 by this declarator is returned.
a723baf1
MM
9899
9900 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9901 for a function-definition here as well. If the declarator is a
9902 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9903 be TRUE upon return. By that point, the function-definition will
9904 have been completely parsed.
9905
9906 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9907 is FALSE. */
9908
9909static tree
21526606
EC
9910cp_parser_init_declarator (cp_parser* parser,
9911 tree decl_specifiers,
94edc4ab
NN
9912 tree prefix_attributes,
9913 bool function_definition_allowed_p,
9914 bool member_p,
560ad596 9915 int declares_class_or_enum,
94edc4ab 9916 bool* function_definition_p)
a723baf1
MM
9917{
9918 cp_token *token;
9919 tree declarator;
9920 tree attributes;
9921 tree asm_specification;
9922 tree initializer;
9923 tree decl = NULL_TREE;
9924 tree scope;
a723baf1
MM
9925 bool is_initialized;
9926 bool is_parenthesized_init;
39703eb9 9927 bool is_non_constant_init;
7efa3e22 9928 int ctor_dtor_or_conv_p;
a723baf1 9929 bool friend_p;
91b004e5 9930 bool pop_p = false;
a723baf1
MM
9931
9932 /* Assume that this is not the declarator for a function
9933 definition. */
9934 if (function_definition_p)
9935 *function_definition_p = false;
9936
9937 /* Defer access checks while parsing the declarator; we cannot know
21526606 9938 what names are accessible until we know what is being
a723baf1 9939 declared. */
cf22909c
KL
9940 resume_deferring_access_checks ();
9941
a723baf1 9942 /* Parse the declarator. */
21526606 9943 declarator
62b8a44e 9944 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9945 &ctor_dtor_or_conv_p,
9946 /*parenthesized_p=*/NULL);
a723baf1 9947 /* Gather up the deferred checks. */
cf22909c 9948 stop_deferring_access_checks ();
24c0ef37 9949
a723baf1
MM
9950 /* If the DECLARATOR was erroneous, there's no need to go
9951 further. */
9952 if (declarator == error_mark_node)
cf22909c 9953 return error_mark_node;
a723baf1 9954
560ad596
MM
9955 cp_parser_check_for_definition_in_return_type (declarator,
9956 declares_class_or_enum);
9957
a723baf1
MM
9958 /* Figure out what scope the entity declared by the DECLARATOR is
9959 located in. `grokdeclarator' sometimes changes the scope, so
9960 we compute it now. */
9961 scope = get_scope_of_declarator (declarator);
9962
9963 /* If we're allowing GNU extensions, look for an asm-specification
9964 and attributes. */
9965 if (cp_parser_allow_gnu_extensions_p (parser))
9966 {
9967 /* Look for an asm-specification. */
9968 asm_specification = cp_parser_asm_specification_opt (parser);
9969 /* And attributes. */
9970 attributes = cp_parser_attributes_opt (parser);
9971 }
9972 else
9973 {
9974 asm_specification = NULL_TREE;
9975 attributes = NULL_TREE;
9976 }
9977
9978 /* Peek at the next token. */
9979 token = cp_lexer_peek_token (parser->lexer);
9980 /* Check to see if the token indicates the start of a
9981 function-definition. */
9982 if (cp_parser_token_starts_function_definition_p (token))
9983 {
9984 if (!function_definition_allowed_p)
9985 {
9986 /* If a function-definition should not appear here, issue an
9987 error message. */
9988 cp_parser_error (parser,
9989 "a function-definition is not allowed here");
9990 return error_mark_node;
9991 }
9992 else
9993 {
a723baf1
MM
9994 /* Neither attributes nor an asm-specification are allowed
9995 on a function-definition. */
9996 if (asm_specification)
9997 error ("an asm-specification is not allowed on a function-definition");
9998 if (attributes)
9999 error ("attributes are not allowed on a function-definition");
10000 /* This is a function-definition. */
10001 *function_definition_p = true;
10002
a723baf1 10003 /* Parse the function definition. */
4bb8ca28
MM
10004 if (member_p)
10005 decl = cp_parser_save_member_function_body (parser,
10006 decl_specifiers,
10007 declarator,
10008 prefix_attributes);
10009 else
21526606 10010 decl
4bb8ca28
MM
10011 = (cp_parser_function_definition_from_specifiers_and_declarator
10012 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10013
a723baf1
MM
10014 return decl;
10015 }
10016 }
10017
10018 /* [dcl.dcl]
10019
10020 Only in function declarations for constructors, destructors, and
21526606 10021 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10022
10023 We explicitly postpone this check past the point where we handle
10024 function-definitions because we tolerate function-definitions
10025 that are missing their return types in some modes. */
7efa3e22 10026 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1 10027 {
21526606 10028 cp_parser_error (parser,
a723baf1
MM
10029 "expected constructor, destructor, or type conversion");
10030 return error_mark_node;
10031 }
10032
10033 /* An `=' or an `(' indicates an initializer. */
21526606 10034 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10035 || token->type == CPP_OPEN_PAREN);
10036 /* If the init-declarator isn't initialized and isn't followed by a
10037 `,' or `;', it's not a valid init-declarator. */
21526606 10038 if (!is_initialized
a723baf1
MM
10039 && token->type != CPP_COMMA
10040 && token->type != CPP_SEMICOLON)
10041 {
10042 cp_parser_error (parser, "expected init-declarator");
10043 return error_mark_node;
10044 }
10045
10046 /* Because start_decl has side-effects, we should only call it if we
10047 know we're going ahead. By this point, we know that we cannot
10048 possibly be looking at any other construct. */
10049 cp_parser_commit_to_tentative_parse (parser);
10050
e90c7b84
ILT
10051 /* If the decl specifiers were bad, issue an error now that we're
10052 sure this was intended to be a declarator. Then continue
10053 declaring the variable(s), as int, to try to cut down on further
10054 errors. */
10055 if (decl_specifiers != NULL
10056 && TREE_VALUE (decl_specifiers) == error_mark_node)
10057 {
10058 cp_parser_error (parser, "invalid type in declaration");
10059 TREE_VALUE (decl_specifiers) = integer_type_node;
10060 }
10061
a723baf1
MM
10062 /* Check to see whether or not this declaration is a friend. */
10063 friend_p = cp_parser_friend_p (decl_specifiers);
10064
10065 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10066 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10067 return error_mark_node;
a723baf1
MM
10068
10069 /* Enter the newly declared entry in the symbol table. If we're
10070 processing a declaration in a class-specifier, we wait until
10071 after processing the initializer. */
10072 if (!member_p)
10073 {
10074 if (parser->in_unbraced_linkage_specification_p)
10075 {
10076 decl_specifiers = tree_cons (error_mark_node,
10077 get_identifier ("extern"),
10078 decl_specifiers);
10079 have_extern_spec = false;
10080 }
ee3071ef
NS
10081 decl = start_decl (declarator, decl_specifiers,
10082 is_initialized, attributes, prefix_attributes);
a723baf1
MM
10083 }
10084
10085 /* Enter the SCOPE. That way unqualified names appearing in the
10086 initializer will be looked up in SCOPE. */
10087 if (scope)
91b004e5 10088 pop_p = push_scope (scope);
a723baf1
MM
10089
10090 /* Perform deferred access control checks, now that we know in which
10091 SCOPE the declared entity resides. */
21526606 10092 if (!member_p && decl)
a723baf1
MM
10093 {
10094 tree saved_current_function_decl = NULL_TREE;
10095
10096 /* If the entity being declared is a function, pretend that we
10097 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10098 things that would not otherwise be accessible. */
a723baf1
MM
10099 if (TREE_CODE (decl) == FUNCTION_DECL)
10100 {
10101 saved_current_function_decl = current_function_decl;
10102 current_function_decl = decl;
10103 }
21526606 10104
cf22909c
KL
10105 /* Perform the access control checks for the declarator and the
10106 the decl-specifiers. */
10107 perform_deferred_access_checks ();
a723baf1
MM
10108
10109 /* Restore the saved value. */
10110 if (TREE_CODE (decl) == FUNCTION_DECL)
10111 current_function_decl = saved_current_function_decl;
10112 }
10113
10114 /* Parse the initializer. */
10115 if (is_initialized)
21526606 10116 initializer = cp_parser_initializer (parser,
39703eb9
MM
10117 &is_parenthesized_init,
10118 &is_non_constant_init);
a723baf1
MM
10119 else
10120 {
10121 initializer = NULL_TREE;
10122 is_parenthesized_init = false;
39703eb9 10123 is_non_constant_init = true;
a723baf1
MM
10124 }
10125
10126 /* The old parser allows attributes to appear after a parenthesized
10127 initializer. Mark Mitchell proposed removing this functionality
10128 on the GCC mailing lists on 2002-08-13. This parser accepts the
10129 attributes -- but ignores them. */
10130 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10131 if (cp_parser_attributes_opt (parser))
10132 warning ("attributes after parenthesized initializer ignored");
10133
10134 /* Leave the SCOPE, now that we have processed the initializer. It
10135 is important to do this before calling cp_finish_decl because it
10136 makes decisions about whether to create DECL_STMTs or not based
10137 on the current scope. */
91b004e5 10138 if (pop_p)
a723baf1
MM
10139 pop_scope (scope);
10140
10141 /* For an in-class declaration, use `grokfield' to create the
10142 declaration. */
10143 if (member_p)
8db1028e
NS
10144 {
10145 decl = grokfield (declarator, decl_specifiers,
10146 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10147 /*attributes=*/NULL_TREE);
8db1028e
NS
10148 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10149 cp_parser_save_default_args (parser, decl);
10150 }
21526606 10151
a723baf1
MM
10152 /* Finish processing the declaration. But, skip friend
10153 declarations. */
10154 if (!friend_p && decl)
21526606
EC
10155 cp_finish_decl (decl,
10156 initializer,
a723baf1
MM
10157 asm_specification,
10158 /* If the initializer is in parentheses, then this is
10159 a direct-initialization, which means that an
10160 `explicit' constructor is OK. Otherwise, an
10161 `explicit' constructor cannot be used. */
10162 ((is_parenthesized_init || !is_initialized)
10163 ? 0 : LOOKUP_ONLYCONVERTING));
10164
39703eb9
MM
10165 /* Remember whether or not variables were initialized by
10166 constant-expressions. */
21526606 10167 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10168 && is_initialized && !is_non_constant_init)
10169 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10170
a723baf1
MM
10171 return decl;
10172}
10173
10174/* Parse a declarator.
21526606 10175
a723baf1
MM
10176 declarator:
10177 direct-declarator
21526606 10178 ptr-operator declarator
a723baf1
MM
10179
10180 abstract-declarator:
10181 ptr-operator abstract-declarator [opt]
10182 direct-abstract-declarator
10183
10184 GNU Extensions:
10185
10186 declarator:
10187 attributes [opt] direct-declarator
21526606 10188 attributes [opt] ptr-operator declarator
a723baf1
MM
10189
10190 abstract-declarator:
10191 attributes [opt] ptr-operator abstract-declarator [opt]
10192 attributes [opt] direct-abstract-declarator
21526606 10193
a723baf1
MM
10194 Returns a representation of the declarator. If the declarator has
10195 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 10196 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
10197 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10198 used. The first operand is the TYPE for `X'. The second operand
10199 is an INDIRECT_REF whose operand is the sub-declarator.
10200
34cd5ae7 10201 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
10202
10203 (It would be better to define a structure type to represent
10204 declarators, rather than abusing `tree' nodes to represent
10205 declarators. That would be much clearer and save some memory.
10206 There is no reason for declarators to be garbage-collected, for
10207 example; they are created during parser and no longer needed after
10208 `grokdeclarator' has been called.)
10209
10210 For a ptr-operator that has the optional cv-qualifier-seq,
10211 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10212 node.
10213
7efa3e22
NS
10214 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10215 detect constructor, destructor or conversion operators. It is set
10216 to -1 if the declarator is a name, and +1 if it is a
10217 function. Otherwise it is set to zero. Usually you just want to
10218 test for >0, but internally the negative value is used.
21526606 10219
a723baf1
MM
10220 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10221 a decl-specifier-seq unless it declares a constructor, destructor,
10222 or conversion. It might seem that we could check this condition in
10223 semantic analysis, rather than parsing, but that makes it difficult
10224 to handle something like `f()'. We want to notice that there are
10225 no decl-specifiers, and therefore realize that this is an
21526606
EC
10226 expression, not a declaration.)
10227
4bb8ca28
MM
10228 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10229 the declarator is a direct-declarator of the form "(...)". */
a723baf1
MM
10230
10231static tree
21526606
EC
10232cp_parser_declarator (cp_parser* parser,
10233 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
10234 int* ctor_dtor_or_conv_p,
10235 bool* parenthesized_p)
a723baf1
MM
10236{
10237 cp_token *token;
10238 tree declarator;
10239 enum tree_code code;
10240 tree cv_qualifier_seq;
10241 tree class_type;
10242 tree attributes = NULL_TREE;
10243
10244 /* Assume this is not a constructor, destructor, or type-conversion
10245 operator. */
10246 if (ctor_dtor_or_conv_p)
7efa3e22 10247 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10248
10249 if (cp_parser_allow_gnu_extensions_p (parser))
10250 attributes = cp_parser_attributes_opt (parser);
21526606 10251
a723baf1
MM
10252 /* Peek at the next token. */
10253 token = cp_lexer_peek_token (parser->lexer);
21526606 10254
a723baf1
MM
10255 /* Check for the ptr-operator production. */
10256 cp_parser_parse_tentatively (parser);
10257 /* Parse the ptr-operator. */
21526606
EC
10258 code = cp_parser_ptr_operator (parser,
10259 &class_type,
a723baf1
MM
10260 &cv_qualifier_seq);
10261 /* If that worked, then we have a ptr-operator. */
10262 if (cp_parser_parse_definitely (parser))
10263 {
4bb8ca28
MM
10264 /* If a ptr-operator was found, then this declarator was not
10265 parenthesized. */
10266 if (parenthesized_p)
10267 *parenthesized_p = true;
a723baf1
MM
10268 /* The dependent declarator is optional if we are parsing an
10269 abstract-declarator. */
62b8a44e 10270 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10271 cp_parser_parse_tentatively (parser);
10272
10273 /* Parse the dependent declarator. */
62b8a44e 10274 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
10275 /*ctor_dtor_or_conv_p=*/NULL,
10276 /*parenthesized_p=*/NULL);
a723baf1
MM
10277
10278 /* If we are parsing an abstract-declarator, we must handle the
10279 case where the dependent declarator is absent. */
62b8a44e
NS
10280 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10281 && !cp_parser_parse_definitely (parser))
a723baf1 10282 declarator = NULL_TREE;
21526606 10283
a723baf1
MM
10284 /* Build the representation of the ptr-operator. */
10285 if (code == INDIRECT_REF)
21526606 10286 declarator = make_pointer_declarator (cv_qualifier_seq,
a723baf1
MM
10287 declarator);
10288 else
10289 declarator = make_reference_declarator (cv_qualifier_seq,
10290 declarator);
10291 /* Handle the pointer-to-member case. */
10292 if (class_type)
10293 declarator = build_nt (SCOPE_REF, class_type, declarator);
10294 }
10295 /* Everything else is a direct-declarator. */
10296 else
4bb8ca28
MM
10297 {
10298 if (parenthesized_p)
10299 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10300 CPP_OPEN_PAREN);
10301 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10302 ctor_dtor_or_conv_p);
10303 }
a723baf1
MM
10304
10305 if (attributes && declarator != error_mark_node)
10306 declarator = tree_cons (attributes, declarator, NULL_TREE);
21526606 10307
a723baf1
MM
10308 return declarator;
10309}
10310
10311/* Parse a direct-declarator or direct-abstract-declarator.
10312
10313 direct-declarator:
10314 declarator-id
10315 direct-declarator ( parameter-declaration-clause )
21526606 10316 cv-qualifier-seq [opt]
a723baf1
MM
10317 exception-specification [opt]
10318 direct-declarator [ constant-expression [opt] ]
21526606 10319 ( declarator )
a723baf1
MM
10320
10321 direct-abstract-declarator:
10322 direct-abstract-declarator [opt]
21526606 10323 ( parameter-declaration-clause )
a723baf1
MM
10324 cv-qualifier-seq [opt]
10325 exception-specification [opt]
10326 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10327 ( abstract-declarator )
10328
62b8a44e
NS
10329 Returns a representation of the declarator. DCL_KIND is
10330 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10331 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10332 we are parsing a direct-declarator. It is
10333 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10334 of ambiguity we prefer an abstract declarator, as per
10335 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10336 cp_parser_declarator.
10337
10338 For the declarator-id production, the representation is as for an
10339 id-expression, except that a qualified name is represented as a
10340 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10341 see the documentation of the FUNCTION_DECLARATOR_* macros for
10342 information about how to find the various declarator components.
10343 An array-declarator is represented as an ARRAY_REF. The
10344 direct-declarator is the first operand; the constant-expression
10345 indicating the size of the array is the second operand. */
10346
10347static tree
94edc4ab
NN
10348cp_parser_direct_declarator (cp_parser* parser,
10349 cp_parser_declarator_kind dcl_kind,
7efa3e22 10350 int* ctor_dtor_or_conv_p)
a723baf1
MM
10351{
10352 cp_token *token;
62b8a44e 10353 tree declarator = NULL_TREE;
a723baf1
MM
10354 tree scope = NULL_TREE;
10355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10356 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10357 bool first = true;
91b004e5 10358 bool pop_p = false;
21526606 10359
62b8a44e 10360 while (true)
a723baf1 10361 {
62b8a44e
NS
10362 /* Peek at the next token. */
10363 token = cp_lexer_peek_token (parser->lexer);
10364 if (token->type == CPP_OPEN_PAREN)
a723baf1 10365 {
62b8a44e
NS
10366 /* This is either a parameter-declaration-clause, or a
10367 parenthesized declarator. When we know we are parsing a
34cd5ae7 10368 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10369 if FIRST is true. For instance, `(int)' is a
10370 parameter-declaration-clause, with an omitted
10371 direct-abstract-declarator. But `((*))', is a
10372 parenthesized abstract declarator. Finally, when T is a
10373 template parameter `(T)' is a
34cd5ae7 10374 parameter-declaration-clause, and not a parenthesized
62b8a44e 10375 named declarator.
21526606 10376
62b8a44e
NS
10377 We first try and parse a parameter-declaration-clause,
10378 and then try a nested declarator (if FIRST is true).
a723baf1 10379
62b8a44e
NS
10380 It is not an error for it not to be a
10381 parameter-declaration-clause, even when FIRST is
10382 false. Consider,
10383
10384 int i (int);
10385 int i (3);
10386
10387 The first is the declaration of a function while the
10388 second is a the definition of a variable, including its
10389 initializer.
10390
10391 Having seen only the parenthesis, we cannot know which of
10392 these two alternatives should be selected. Even more
10393 complex are examples like:
10394
10395 int i (int (a));
10396 int i (int (3));
10397
10398 The former is a function-declaration; the latter is a
21526606 10399 variable initialization.
62b8a44e 10400
34cd5ae7 10401 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10402 that fails, we back out and return. */
10403
10404 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10405 {
62b8a44e 10406 tree params;
4047b164 10407 unsigned saved_num_template_parameter_lists;
21526606 10408
62b8a44e 10409 cp_parser_parse_tentatively (parser);
a723baf1 10410
62b8a44e
NS
10411 /* Consume the `('. */
10412 cp_lexer_consume_token (parser->lexer);
10413 if (first)
10414 {
10415 /* If this is going to be an abstract declarator, we're
10416 in a declarator and we can't have default args. */
10417 parser->default_arg_ok_p = false;
10418 parser->in_declarator_p = true;
10419 }
21526606 10420
4047b164
KL
10421 /* Inside the function parameter list, surrounding
10422 template-parameter-lists do not apply. */
10423 saved_num_template_parameter_lists
10424 = parser->num_template_parameter_lists;
10425 parser->num_template_parameter_lists = 0;
10426
62b8a44e
NS
10427 /* Parse the parameter-declaration-clause. */
10428 params = cp_parser_parameter_declaration_clause (parser);
10429
4047b164
KL
10430 parser->num_template_parameter_lists
10431 = saved_num_template_parameter_lists;
10432
62b8a44e 10433 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10434 exception-specification. */
62b8a44e
NS
10435 if (cp_parser_parse_definitely (parser))
10436 {
10437 tree cv_qualifiers;
10438 tree exception_specification;
7efa3e22
NS
10439
10440 if (ctor_dtor_or_conv_p)
10441 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10442 first = false;
10443 /* Consume the `)'. */
10444 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10445
10446 /* Parse the cv-qualifier-seq. */
10447 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10448 /* And the exception-specification. */
21526606 10449 exception_specification
62b8a44e
NS
10450 = cp_parser_exception_specification_opt (parser);
10451
10452 /* Create the function-declarator. */
10453 declarator = make_call_declarator (declarator,
10454 params,
10455 cv_qualifiers,
10456 exception_specification);
10457 /* Any subsequent parameter lists are to do with
10458 return type, so are not those of the declared
10459 function. */
10460 parser->default_arg_ok_p = false;
21526606 10461
62b8a44e
NS
10462 /* Repeat the main loop. */
10463 continue;
10464 }
10465 }
21526606 10466
62b8a44e
NS
10467 /* If this is the first, we can try a parenthesized
10468 declarator. */
10469 if (first)
a723baf1 10470 {
a7324e75
MM
10471 bool saved_in_type_id_in_expr_p;
10472
a723baf1 10473 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 10474 parser->in_declarator_p = saved_in_declarator_p;
21526606 10475
62b8a44e
NS
10476 /* Consume the `('. */
10477 cp_lexer_consume_token (parser->lexer);
10478 /* Parse the nested declarator. */
a7324e75
MM
10479 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10480 parser->in_type_id_in_expr_p = true;
21526606 10481 declarator
4bb8ca28
MM
10482 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10483 /*parenthesized_p=*/NULL);
a7324e75 10484 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
10485 first = false;
10486 /* Expect a `)'. */
10487 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10488 declarator = error_mark_node;
10489 if (declarator == error_mark_node)
10490 break;
21526606 10491
62b8a44e 10492 goto handle_declarator;
a723baf1 10493 }
9bcb9aae 10494 /* Otherwise, we must be done. */
62b8a44e
NS
10495 else
10496 break;
a723baf1 10497 }
62b8a44e
NS
10498 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10499 && token->type == CPP_OPEN_SQUARE)
a723baf1 10500 {
62b8a44e 10501 /* Parse an array-declarator. */
a723baf1
MM
10502 tree bounds;
10503
7efa3e22
NS
10504 if (ctor_dtor_or_conv_p)
10505 *ctor_dtor_or_conv_p = 0;
21526606 10506
62b8a44e
NS
10507 first = false;
10508 parser->default_arg_ok_p = false;
10509 parser->in_declarator_p = true;
a723baf1
MM
10510 /* Consume the `['. */
10511 cp_lexer_consume_token (parser->lexer);
10512 /* Peek at the next token. */
10513 token = cp_lexer_peek_token (parser->lexer);
10514 /* If the next token is `]', then there is no
10515 constant-expression. */
10516 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10517 {
10518 bool non_constant_p;
10519
21526606 10520 bounds
14d22dd6
MM
10521 = cp_parser_constant_expression (parser,
10522 /*allow_non_constant=*/true,
10523 &non_constant_p);
d17811fd 10524 if (!non_constant_p)
9baa27a9 10525 bounds = fold_non_dependent_expr (bounds);
14d22dd6 10526 }
a723baf1
MM
10527 else
10528 bounds = NULL_TREE;
10529 /* Look for the closing `]'. */
62b8a44e
NS
10530 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10531 {
10532 declarator = error_mark_node;
10533 break;
10534 }
a723baf1
MM
10535
10536 declarator = build_nt (ARRAY_REF, declarator, bounds);
10537 }
62b8a44e 10538 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10539 {
a668c6ad 10540 /* Parse a declarator-id */
62b8a44e
NS
10541 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10542 cp_parser_parse_tentatively (parser);
10543 declarator = cp_parser_declarator_id (parser);
712becab
NS
10544 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10545 {
10546 if (!cp_parser_parse_definitely (parser))
10547 declarator = error_mark_node;
10548 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10549 {
10550 cp_parser_error (parser, "expected unqualified-id");
10551 declarator = error_mark_node;
10552 }
10553 }
21526606 10554
62b8a44e
NS
10555 if (declarator == error_mark_node)
10556 break;
21526606 10557
d9a50301
KL
10558 if (TREE_CODE (declarator) == SCOPE_REF
10559 && !current_scope ())
62b8a44e
NS
10560 {
10561 tree scope = TREE_OPERAND (declarator, 0);
712becab 10562
62b8a44e
NS
10563 /* In the declaration of a member of a template class
10564 outside of the class itself, the SCOPE will sometimes
10565 be a TYPENAME_TYPE. For example, given:
21526606 10566
62b8a44e
NS
10567 template <typename T>
10568 int S<T>::R::i = 3;
21526606 10569
62b8a44e
NS
10570 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10571 this context, we must resolve S<T>::R to an ordinary
10572 type, rather than a typename type.
21526606 10573
62b8a44e
NS
10574 The reason we normally avoid resolving TYPENAME_TYPEs
10575 is that a specialization of `S' might render
10576 `S<T>::R' not a type. However, if `S' is
10577 specialized, then this `i' will not be used, so there
10578 is no harm in resolving the types here. */
10579 if (TREE_CODE (scope) == TYPENAME_TYPE)
10580 {
14d22dd6
MM
10581 tree type;
10582
62b8a44e 10583 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10584 type = resolve_typename_type (scope,
10585 /*only_current_p=*/false);
62b8a44e 10586 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10587 if (type != error_mark_node)
10588 scope = type;
62b8a44e 10589 /* Build a new DECLARATOR. */
21526606 10590 declarator = build_nt (SCOPE_REF,
62b8a44e
NS
10591 scope,
10592 TREE_OPERAND (declarator, 1));
10593 }
10594 }
21526606
EC
10595
10596 /* Check to see whether the declarator-id names a constructor,
62b8a44e 10597 destructor, or conversion. */
21526606
EC
10598 if (declarator && ctor_dtor_or_conv_p
10599 && ((TREE_CODE (declarator) == SCOPE_REF
62b8a44e
NS
10600 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10601 || (TREE_CODE (declarator) != SCOPE_REF
10602 && at_class_scope_p ())))
a723baf1 10603 {
62b8a44e
NS
10604 tree unqualified_name;
10605 tree class_type;
10606
10607 /* Get the unqualified part of the name. */
10608 if (TREE_CODE (declarator) == SCOPE_REF)
10609 {
10610 class_type = TREE_OPERAND (declarator, 0);
10611 unqualified_name = TREE_OPERAND (declarator, 1);
10612 }
10613 else
10614 {
10615 class_type = current_class_type;
10616 unqualified_name = declarator;
10617 }
10618
10619 /* See if it names ctor, dtor or conv. */
10620 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10621 || IDENTIFIER_TYPENAME_P (unqualified_name)
ab73670a
MM
10622 || constructor_name_p (unqualified_name, class_type)
10623 || (TREE_CODE (unqualified_name) == TYPE_DECL
10624 && same_type_p (TREE_TYPE (unqualified_name),
10625 class_type)))
7efa3e22 10626 *ctor_dtor_or_conv_p = -1;
a723baf1 10627 }
62b8a44e
NS
10628
10629 handle_declarator:;
10630 scope = get_scope_of_declarator (declarator);
10631 if (scope)
91b004e5
MM
10632 /* Any names that appear after the declarator-id for a
10633 member are looked up in the containing scope. */
10634 pop_p = push_scope (scope);
62b8a44e
NS
10635 parser->in_declarator_p = true;
10636 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10637 || (declarator
10638 && (TREE_CODE (declarator) == SCOPE_REF
10639 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10640 /* Default args are only allowed on function
10641 declarations. */
10642 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10643 else
62b8a44e
NS
10644 parser->default_arg_ok_p = false;
10645
10646 first = false;
a723baf1 10647 }
62b8a44e 10648 /* We're done. */
a723baf1
MM
10649 else
10650 break;
a723baf1
MM
10651 }
10652
10653 /* For an abstract declarator, we might wind up with nothing at this
10654 point. That's an error; the declarator is not optional. */
10655 if (!declarator)
10656 cp_parser_error (parser, "expected declarator");
10657
10658 /* If we entered a scope, we must exit it now. */
91b004e5 10659 if (pop_p)
a723baf1
MM
10660 pop_scope (scope);
10661
10662 parser->default_arg_ok_p = saved_default_arg_ok_p;
10663 parser->in_declarator_p = saved_in_declarator_p;
21526606 10664
a723baf1
MM
10665 return declarator;
10666}
10667
21526606 10668/* Parse a ptr-operator.
a723baf1
MM
10669
10670 ptr-operator:
10671 * cv-qualifier-seq [opt]
10672 &
10673 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10674
10675 GNU Extension:
10676
10677 ptr-operator:
10678 & cv-qualifier-seq [opt]
10679
10680 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10681 used. Returns ADDR_EXPR if a reference was used. In the
21526606 10682 case of a pointer-to-member, *TYPE is filled in with the
a723baf1
MM
10683 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10684 with the cv-qualifier-seq, or NULL_TREE, if there are no
10685 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
21526606 10686
a723baf1 10687static enum tree_code
21526606
EC
10688cp_parser_ptr_operator (cp_parser* parser,
10689 tree* type,
94edc4ab 10690 tree* cv_qualifier_seq)
a723baf1
MM
10691{
10692 enum tree_code code = ERROR_MARK;
10693 cp_token *token;
10694
10695 /* Assume that it's not a pointer-to-member. */
10696 *type = NULL_TREE;
10697 /* And that there are no cv-qualifiers. */
10698 *cv_qualifier_seq = NULL_TREE;
10699
10700 /* Peek at the next token. */
10701 token = cp_lexer_peek_token (parser->lexer);
10702 /* If it's a `*' or `&' we have a pointer or reference. */
10703 if (token->type == CPP_MULT || token->type == CPP_AND)
10704 {
10705 /* Remember which ptr-operator we were processing. */
10706 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10707
10708 /* Consume the `*' or `&'. */
10709 cp_lexer_consume_token (parser->lexer);
10710
10711 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10712 `&', if we are allowing GNU extensions. (The only qualifier
10713 that can legally appear after `&' is `restrict', but that is
10714 enforced during semantic analysis. */
21526606 10715 if (code == INDIRECT_REF
a723baf1
MM
10716 || cp_parser_allow_gnu_extensions_p (parser))
10717 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10718 }
10719 else
10720 {
10721 /* Try the pointer-to-member case. */
10722 cp_parser_parse_tentatively (parser);
10723 /* Look for the optional `::' operator. */
10724 cp_parser_global_scope_opt (parser,
10725 /*current_scope_valid_p=*/false);
10726 /* Look for the nested-name specifier. */
10727 cp_parser_nested_name_specifier (parser,
10728 /*typename_keyword_p=*/false,
10729 /*check_dependency_p=*/true,
a668c6ad
MM
10730 /*type_p=*/false,
10731 /*is_declaration=*/false);
a723baf1
MM
10732 /* If we found it, and the next token is a `*', then we are
10733 indeed looking at a pointer-to-member operator. */
10734 if (!cp_parser_error_occurred (parser)
10735 && cp_parser_require (parser, CPP_MULT, "`*'"))
10736 {
10737 /* The type of which the member is a member is given by the
10738 current SCOPE. */
10739 *type = parser->scope;
10740 /* The next name will not be qualified. */
10741 parser->scope = NULL_TREE;
10742 parser->qualifying_scope = NULL_TREE;
10743 parser->object_scope = NULL_TREE;
10744 /* Indicate that the `*' operator was used. */
10745 code = INDIRECT_REF;
10746 /* Look for the optional cv-qualifier-seq. */
10747 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10748 }
10749 /* If that didn't work we don't have a ptr-operator. */
10750 if (!cp_parser_parse_definitely (parser))
10751 cp_parser_error (parser, "expected ptr-operator");
10752 }
10753
10754 return code;
10755}
10756
10757/* Parse an (optional) cv-qualifier-seq.
10758
10759 cv-qualifier-seq:
21526606 10760 cv-qualifier cv-qualifier-seq [opt]
a723baf1
MM
10761
10762 Returns a TREE_LIST. The TREE_VALUE of each node is the
10763 representation of a cv-qualifier. */
10764
10765static tree
94edc4ab 10766cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10767{
10768 tree cv_qualifiers = NULL_TREE;
21526606 10769
a723baf1
MM
10770 while (true)
10771 {
10772 tree cv_qualifier;
10773
10774 /* Look for the next cv-qualifier. */
10775 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10776 /* If we didn't find one, we're done. */
10777 if (!cv_qualifier)
10778 break;
10779
10780 /* Add this cv-qualifier to the list. */
21526606 10781 cv_qualifiers
a723baf1
MM
10782 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10783 }
10784
10785 /* We built up the list in reverse order. */
10786 return nreverse (cv_qualifiers);
10787}
10788
10789/* Parse an (optional) cv-qualifier.
10790
10791 cv-qualifier:
10792 const
21526606 10793 volatile
a723baf1
MM
10794
10795 GNU Extension:
10796
10797 cv-qualifier:
10798 __restrict__ */
10799
10800static tree
94edc4ab 10801cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10802{
10803 cp_token *token;
10804 tree cv_qualifier = NULL_TREE;
10805
10806 /* Peek at the next token. */
10807 token = cp_lexer_peek_token (parser->lexer);
10808 /* See if it's a cv-qualifier. */
10809 switch (token->keyword)
10810 {
10811 case RID_CONST:
10812 case RID_VOLATILE:
10813 case RID_RESTRICT:
10814 /* Save the value of the token. */
10815 cv_qualifier = token->value;
10816 /* Consume the token. */
10817 cp_lexer_consume_token (parser->lexer);
10818 break;
10819
10820 default:
10821 break;
10822 }
10823
10824 return cv_qualifier;
10825}
10826
10827/* Parse a declarator-id.
10828
10829 declarator-id:
10830 id-expression
21526606 10831 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
10832
10833 In the `id-expression' case, the value returned is as for
10834 cp_parser_id_expression if the id-expression was an unqualified-id.
10835 If the id-expression was a qualified-id, then a SCOPE_REF is
10836 returned. The first operand is the scope (either a NAMESPACE_DECL
10837 or TREE_TYPE), but the second is still just a representation of an
10838 unqualified-id. */
10839
10840static tree
94edc4ab 10841cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10842{
10843 tree id_expression;
10844
10845 /* The expression must be an id-expression. Assume that qualified
10846 names are the names of types so that:
10847
10848 template <class T>
10849 int S<T>::R::i = 3;
10850
10851 will work; we must treat `S<T>::R' as the name of a type.
10852 Similarly, assume that qualified names are templates, where
10853 required, so that:
10854
10855 template <class T>
10856 int S<T>::R<T>::i = 3;
10857
10858 will work, too. */
10859 id_expression = cp_parser_id_expression (parser,
10860 /*template_keyword_p=*/false,
10861 /*check_dependency_p=*/false,
f3c2dfc6
MM
10862 /*template_p=*/NULL,
10863 /*declarator_p=*/true);
21526606 10864 /* If the name was qualified, create a SCOPE_REF to represent
a723baf1
MM
10865 that. */
10866 if (parser->scope)
ec20aa6c
MM
10867 {
10868 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10869 parser->scope = NULL_TREE;
10870 }
a723baf1
MM
10871
10872 return id_expression;
10873}
10874
10875/* Parse a type-id.
10876
10877 type-id:
10878 type-specifier-seq abstract-declarator [opt]
10879
10880 Returns the TYPE specified. */
10881
10882static tree
94edc4ab 10883cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10884{
10885 tree type_specifier_seq;
10886 tree abstract_declarator;
10887
10888 /* Parse the type-specifier-seq. */
21526606 10889 type_specifier_seq
a723baf1
MM
10890 = cp_parser_type_specifier_seq (parser);
10891 if (type_specifier_seq == error_mark_node)
10892 return error_mark_node;
10893
10894 /* There might or might not be an abstract declarator. */
10895 cp_parser_parse_tentatively (parser);
10896 /* Look for the declarator. */
21526606 10897 abstract_declarator
4bb8ca28
MM
10898 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10899 /*parenthesized_p=*/NULL);
a723baf1
MM
10900 /* Check to see if there really was a declarator. */
10901 if (!cp_parser_parse_definitely (parser))
10902 abstract_declarator = NULL_TREE;
10903
10904 return groktypename (build_tree_list (type_specifier_seq,
10905 abstract_declarator));
10906}
10907
10908/* Parse a type-specifier-seq.
10909
10910 type-specifier-seq:
10911 type-specifier type-specifier-seq [opt]
10912
10913 GNU extension:
10914
10915 type-specifier-seq:
10916 attributes type-specifier-seq [opt]
10917
10918 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10919 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10920
10921static tree
94edc4ab 10922cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10923{
10924 bool seen_type_specifier = false;
10925 tree type_specifier_seq = NULL_TREE;
10926
10927 /* Parse the type-specifiers and attributes. */
10928 while (true)
10929 {
10930 tree type_specifier;
10931
10932 /* Check for attributes first. */
10933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10934 {
10935 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10936 NULL_TREE,
10937 type_specifier_seq);
10938 continue;
10939 }
10940
10941 /* After the first type-specifier, others are optional. */
10942 if (seen_type_specifier)
10943 cp_parser_parse_tentatively (parser);
10944 /* Look for the type-specifier. */
21526606 10945 type_specifier = cp_parser_type_specifier (parser,
a723baf1
MM
10946 CP_PARSER_FLAGS_NONE,
10947 /*is_friend=*/false,
10948 /*is_declaration=*/false,
10949 NULL,
10950 NULL);
10951 /* If the first type-specifier could not be found, this is not a
10952 type-specifier-seq at all. */
10953 if (!seen_type_specifier && type_specifier == error_mark_node)
10954 return error_mark_node;
10955 /* If subsequent type-specifiers could not be found, the
10956 type-specifier-seq is complete. */
10957 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10958 break;
10959
10960 /* Add the new type-specifier to the list. */
21526606 10961 type_specifier_seq
a723baf1
MM
10962 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10963 seen_type_specifier = true;
10964 }
10965
10966 /* We built up the list in reverse order. */
10967 return nreverse (type_specifier_seq);
10968}
10969
10970/* Parse a parameter-declaration-clause.
10971
10972 parameter-declaration-clause:
10973 parameter-declaration-list [opt] ... [opt]
10974 parameter-declaration-list , ...
10975
10976 Returns a representation for the parameter declarations. Each node
10977 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10978 representation.) If the parameter-declaration-clause ends with an
10979 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10980 list. A return value of NULL_TREE indicates a
10981 parameter-declaration-clause consisting only of an ellipsis. */
10982
10983static tree
94edc4ab 10984cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10985{
10986 tree parameters;
10987 cp_token *token;
10988 bool ellipsis_p;
10989
10990 /* Peek at the next token. */
10991 token = cp_lexer_peek_token (parser->lexer);
10992 /* Check for trivial parameter-declaration-clauses. */
10993 if (token->type == CPP_ELLIPSIS)
10994 {
10995 /* Consume the `...' token. */
10996 cp_lexer_consume_token (parser->lexer);
10997 return NULL_TREE;
10998 }
10999 else if (token->type == CPP_CLOSE_PAREN)
11000 /* There are no parameters. */
c73aecdf
DE
11001 {
11002#ifndef NO_IMPLICIT_EXTERN_C
11003 if (in_system_header && current_class_type == NULL
11004 && current_lang_name == lang_name_c)
11005 return NULL_TREE;
11006 else
11007#endif
11008 return void_list_node;
11009 }
a723baf1
MM
11010 /* Check for `(void)', too, which is a special case. */
11011 else if (token->keyword == RID_VOID
21526606 11012 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11013 == CPP_CLOSE_PAREN))
11014 {
11015 /* Consume the `void' token. */
11016 cp_lexer_consume_token (parser->lexer);
11017 /* There are no parameters. */
11018 return void_list_node;
11019 }
21526606 11020
a723baf1
MM
11021 /* Parse the parameter-declaration-list. */
11022 parameters = cp_parser_parameter_declaration_list (parser);
11023 /* If a parse error occurred while parsing the
11024 parameter-declaration-list, then the entire
11025 parameter-declaration-clause is erroneous. */
11026 if (parameters == error_mark_node)
11027 return error_mark_node;
11028
11029 /* Peek at the next token. */
11030 token = cp_lexer_peek_token (parser->lexer);
11031 /* If it's a `,', the clause should terminate with an ellipsis. */
11032 if (token->type == CPP_COMMA)
11033 {
11034 /* Consume the `,'. */
11035 cp_lexer_consume_token (parser->lexer);
11036 /* Expect an ellipsis. */
21526606 11037 ellipsis_p
a723baf1
MM
11038 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11039 }
21526606 11040 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11041 omitted. */
11042 else if (token->type == CPP_ELLIPSIS)
11043 {
11044 /* Consume the `...' token. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* And remember that we saw it. */
11047 ellipsis_p = true;
11048 }
11049 else
11050 ellipsis_p = false;
11051
11052 /* Finish the parameter list. */
11053 return finish_parmlist (parameters, ellipsis_p);
11054}
11055
11056/* Parse a parameter-declaration-list.
11057
11058 parameter-declaration-list:
11059 parameter-declaration
11060 parameter-declaration-list , parameter-declaration
11061
11062 Returns a representation of the parameter-declaration-list, as for
11063 cp_parser_parameter_declaration_clause. However, the
11064 `void_list_node' is never appended to the list. */
11065
11066static tree
94edc4ab 11067cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
11068{
11069 tree parameters = NULL_TREE;
11070
11071 /* Look for more parameters. */
11072 while (true)
11073 {
11074 tree parameter;
4bb8ca28 11075 bool parenthesized_p;
a723baf1 11076 /* Parse the parameter. */
21526606
EC
11077 parameter
11078 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11079 /*template_parm_p=*/false,
11080 &parenthesized_p);
ec194454 11081
34cd5ae7 11082 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
11083 then the entire parameter-declaration-list is erroneous. */
11084 if (parameter == error_mark_node)
11085 {
11086 parameters = error_mark_node;
11087 break;
11088 }
11089 /* Add the new parameter to the list. */
11090 TREE_CHAIN (parameter) = parameters;
11091 parameters = parameter;
11092
11093 /* Peek at the next token. */
11094 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11095 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11096 /* The parameter-declaration-list is complete. */
11097 break;
11098 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11099 {
11100 cp_token *token;
11101
11102 /* Peek at the next token. */
11103 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11104 /* If it's an ellipsis, then the list is complete. */
11105 if (token->type == CPP_ELLIPSIS)
11106 break;
11107 /* Otherwise, there must be more parameters. Consume the
11108 `,'. */
11109 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11110 /* When parsing something like:
11111
11112 int i(float f, double d)
21526606 11113
4bb8ca28
MM
11114 we can tell after seeing the declaration for "f" that we
11115 are not looking at an initialization of a variable "i",
21526606 11116 but rather at the declaration of a function "i".
4bb8ca28
MM
11117
11118 Due to the fact that the parsing of template arguments
11119 (as specified to a template-id) requires backtracking we
11120 cannot use this technique when inside a template argument
11121 list. */
11122 if (!parser->in_template_argument_list_p
4d5fe289 11123 && !parser->in_type_id_in_expr_p
4bb8ca28
MM
11124 && cp_parser_parsing_tentatively (parser)
11125 && !cp_parser_committed_to_tentative_parse (parser)
11126 /* However, a parameter-declaration of the form
11127 "foat(f)" (which is a valid declaration of a
11128 parameter "f") can also be interpreted as an
11129 expression (the conversion of "f" to "float"). */
11130 && !parenthesized_p)
11131 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11132 }
11133 else
11134 {
11135 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
11136 if (!cp_parser_parsing_tentatively (parser)
11137 || cp_parser_committed_to_tentative_parse (parser))
21526606 11138 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11139 /*recovering=*/true,
5c832178 11140 /*or_comma=*/false,
4bb8ca28 11141 /*consume_paren=*/false);
a723baf1
MM
11142 break;
11143 }
11144 }
11145
11146 /* We built up the list in reverse order; straighten it out now. */
11147 return nreverse (parameters);
11148}
11149
11150/* Parse a parameter declaration.
11151
11152 parameter-declaration:
11153 decl-specifier-seq declarator
11154 decl-specifier-seq declarator = assignment-expression
11155 decl-specifier-seq abstract-declarator [opt]
11156 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11157
ec194454
MM
11158 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11159 declares a template parameter. (In that case, a non-nested `>'
11160 token encountered during the parsing of the assignment-expression
11161 is not interpreted as a greater-than operator.)
a723baf1
MM
11162
11163 Returns a TREE_LIST representing the parameter-declaration. The
4bb8ca28
MM
11164 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11165 there is no default argument. The TREE_VALUE is a representation
11166 of the decl-specifier-seq and declarator. In particular, the
11167 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11168 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11169 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11170 the declarator is of the form "(p)". */
a723baf1
MM
11171
11172static tree
21526606 11173cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11174 bool template_parm_p,
11175 bool *parenthesized_p)
a723baf1 11176{
560ad596 11177 int declares_class_or_enum;
ec194454 11178 bool greater_than_is_operator_p;
a723baf1
MM
11179 tree decl_specifiers;
11180 tree attributes;
11181 tree declarator;
11182 tree default_argument;
11183 tree parameter;
11184 cp_token *token;
11185 const char *saved_message;
11186
ec194454
MM
11187 /* In a template parameter, `>' is not an operator.
11188
11189 [temp.param]
11190
11191 When parsing a default template-argument for a non-type
11192 template-parameter, the first non-nested `>' is taken as the end
11193 of the template parameter-list rather than a greater-than
11194 operator. */
11195 greater_than_is_operator_p = !template_parm_p;
11196
a723baf1
MM
11197 /* Type definitions may not appear in parameter types. */
11198 saved_message = parser->type_definition_forbidden_message;
21526606 11199 parser->type_definition_forbidden_message
a723baf1
MM
11200 = "types may not be defined in parameter types";
11201
11202 /* Parse the declaration-specifiers. */
21526606 11203 decl_specifiers
a723baf1
MM
11204 = cp_parser_decl_specifier_seq (parser,
11205 CP_PARSER_FLAGS_NONE,
11206 &attributes,
11207 &declares_class_or_enum);
11208 /* If an error occurred, there's no reason to attempt to parse the
11209 rest of the declaration. */
11210 if (cp_parser_error_occurred (parser))
11211 {
11212 parser->type_definition_forbidden_message = saved_message;
11213 return error_mark_node;
11214 }
11215
11216 /* Peek at the next token. */
11217 token = cp_lexer_peek_token (parser->lexer);
11218 /* If the next token is a `)', `,', `=', `>', or `...', then there
11219 is no declarator. */
21526606 11220 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11221 || token->type == CPP_COMMA
11222 || token->type == CPP_EQ
11223 || token->type == CPP_ELLIPSIS
11224 || token->type == CPP_GREATER)
4bb8ca28
MM
11225 {
11226 declarator = NULL_TREE;
11227 if (parenthesized_p)
11228 *parenthesized_p = false;
11229 }
a723baf1
MM
11230 /* Otherwise, there should be a declarator. */
11231 else
11232 {
11233 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11234 parser->default_arg_ok_p = false;
21526606 11235
5c832178
MM
11236 /* After seeing a decl-specifier-seq, if the next token is not a
11237 "(", there is no possibility that the code is a valid
4f8163b1
MM
11238 expression. Therefore, if parsing tentatively, we commit at
11239 this point. */
5c832178 11240 if (!parser->in_template_argument_list_p
643aee72 11241 /* In an expression context, having seen:
4f8163b1 11242
a7324e75 11243 (int((char ...
4f8163b1
MM
11244
11245 we cannot be sure whether we are looking at a
a7324e75
MM
11246 function-type (taking a "char" as a parameter) or a cast
11247 of some object of type "char" to "int". */
4f8163b1 11248 && !parser->in_type_id_in_expr_p
5c832178
MM
11249 && cp_parser_parsing_tentatively (parser)
11250 && !cp_parser_committed_to_tentative_parse (parser)
11251 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11252 cp_parser_commit_to_tentative_parse (parser);
11253 /* Parse the declarator. */
a723baf1 11254 declarator = cp_parser_declarator (parser,
62b8a44e 11255 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
11256 /*ctor_dtor_or_conv_p=*/NULL,
11257 parenthesized_p);
a723baf1 11258 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
11259 /* After the declarator, allow more attributes. */
11260 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
11261 }
11262
62b8a44e 11263 /* The restriction on defining new types applies only to the type
a723baf1
MM
11264 of the parameter, not to the default argument. */
11265 parser->type_definition_forbidden_message = saved_message;
11266
11267 /* If the next token is `=', then process a default argument. */
11268 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11269 {
11270 bool saved_greater_than_is_operator_p;
11271 /* Consume the `='. */
11272 cp_lexer_consume_token (parser->lexer);
11273
11274 /* If we are defining a class, then the tokens that make up the
11275 default argument must be saved and processed later. */
21526606 11276 if (!template_parm_p && at_class_scope_p ()
ec194454 11277 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11278 {
11279 unsigned depth = 0;
11280
11281 /* Create a DEFAULT_ARG to represented the unparsed default
11282 argument. */
11283 default_argument = make_node (DEFAULT_ARG);
11284 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11285
11286 /* Add tokens until we have processed the entire default
11287 argument. */
11288 while (true)
11289 {
11290 bool done = false;
11291 cp_token *token;
11292
11293 /* Peek at the next token. */
11294 token = cp_lexer_peek_token (parser->lexer);
11295 /* What we do depends on what token we have. */
11296 switch (token->type)
11297 {
11298 /* In valid code, a default argument must be
11299 immediately followed by a `,' `)', or `...'. */
11300 case CPP_COMMA:
11301 case CPP_CLOSE_PAREN:
11302 case CPP_ELLIPSIS:
11303 /* If we run into a non-nested `;', `}', or `]',
11304 then the code is invalid -- but the default
11305 argument is certainly over. */
11306 case CPP_SEMICOLON:
11307 case CPP_CLOSE_BRACE:
11308 case CPP_CLOSE_SQUARE:
11309 if (depth == 0)
11310 done = true;
11311 /* Update DEPTH, if necessary. */
11312 else if (token->type == CPP_CLOSE_PAREN
11313 || token->type == CPP_CLOSE_BRACE
11314 || token->type == CPP_CLOSE_SQUARE)
11315 --depth;
11316 break;
11317
11318 case CPP_OPEN_PAREN:
11319 case CPP_OPEN_SQUARE:
11320 case CPP_OPEN_BRACE:
11321 ++depth;
11322 break;
11323
11324 case CPP_GREATER:
11325 /* If we see a non-nested `>', and `>' is not an
11326 operator, then it marks the end of the default
11327 argument. */
11328 if (!depth && !greater_than_is_operator_p)
11329 done = true;
11330 break;
11331
11332 /* If we run out of tokens, issue an error message. */
11333 case CPP_EOF:
11334 error ("file ends in default argument");
11335 done = true;
11336 break;
11337
11338 case CPP_NAME:
11339 case CPP_SCOPE:
11340 /* In these cases, we should look for template-ids.
21526606 11341 For example, if the default argument is
a723baf1
MM
11342 `X<int, double>()', we need to do name lookup to
11343 figure out whether or not `X' is a template; if
34cd5ae7 11344 so, the `,' does not end the default argument.
a723baf1
MM
11345
11346 That is not yet done. */
11347 break;
11348
11349 default:
11350 break;
11351 }
11352
11353 /* If we've reached the end, stop. */
11354 if (done)
11355 break;
21526606 11356
a723baf1
MM
11357 /* Add the token to the token block. */
11358 token = cp_lexer_consume_token (parser->lexer);
11359 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11360 token);
11361 }
11362 }
11363 /* Outside of a class definition, we can just parse the
11364 assignment-expression. */
11365 else
11366 {
11367 bool saved_local_variables_forbidden_p;
11368
11369 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11370 set correctly. */
21526606 11371 saved_greater_than_is_operator_p
a723baf1
MM
11372 = parser->greater_than_is_operator_p;
11373 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11374 /* Local variable names (and the `this' keyword) may not
11375 appear in a default argument. */
21526606 11376 saved_local_variables_forbidden_p
a723baf1
MM
11377 = parser->local_variables_forbidden_p;
11378 parser->local_variables_forbidden_p = true;
11379 /* Parse the assignment-expression. */
11380 default_argument = cp_parser_assignment_expression (parser);
11381 /* Restore saved state. */
21526606 11382 parser->greater_than_is_operator_p
a723baf1 11383 = saved_greater_than_is_operator_p;
21526606
EC
11384 parser->local_variables_forbidden_p
11385 = saved_local_variables_forbidden_p;
a723baf1
MM
11386 }
11387 if (!parser->default_arg_ok_p)
11388 {
c67d36d0
NS
11389 if (!flag_pedantic_errors)
11390 warning ("deprecated use of default argument for parameter of non-function");
11391 else
11392 {
11393 error ("default arguments are only permitted for function parameters");
11394 default_argument = NULL_TREE;
11395 }
a723baf1
MM
11396 }
11397 }
11398 else
11399 default_argument = NULL_TREE;
21526606 11400
a723baf1
MM
11401 /* Create the representation of the parameter. */
11402 if (attributes)
11403 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
21526606 11404 parameter = build_tree_list (default_argument,
a723baf1
MM
11405 build_tree_list (decl_specifiers,
11406 declarator));
11407
11408 return parameter;
11409}
11410
a723baf1
MM
11411/* Parse a function-body.
11412
11413 function-body:
11414 compound_statement */
11415
11416static void
11417cp_parser_function_body (cp_parser *parser)
11418{
a5bcc582 11419 cp_parser_compound_statement (parser, false);
a723baf1
MM
11420}
11421
11422/* Parse a ctor-initializer-opt followed by a function-body. Return
11423 true if a ctor-initializer was present. */
11424
11425static bool
11426cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11427{
11428 tree body;
11429 bool ctor_initializer_p;
11430
11431 /* Begin the function body. */
11432 body = begin_function_body ();
11433 /* Parse the optional ctor-initializer. */
11434 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11435 /* Parse the function-body. */
11436 cp_parser_function_body (parser);
11437 /* Finish the function body. */
11438 finish_function_body (body);
11439
11440 return ctor_initializer_p;
11441}
11442
11443/* Parse an initializer.
11444
11445 initializer:
11446 = initializer-clause
21526606 11447 ( expression-list )
a723baf1
MM
11448
11449 Returns a expression representing the initializer. If no
21526606 11450 initializer is present, NULL_TREE is returned.
a723baf1
MM
11451
11452 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11453 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11454 set to FALSE if there is no initializer present. If there is an
11455 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11456 is set to true; otherwise it is set to false. */
a723baf1
MM
11457
11458static tree
39703eb9
MM
11459cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11460 bool* non_constant_p)
a723baf1
MM
11461{
11462 cp_token *token;
11463 tree init;
11464
11465 /* Peek at the next token. */
11466 token = cp_lexer_peek_token (parser->lexer);
11467
11468 /* Let our caller know whether or not this initializer was
11469 parenthesized. */
11470 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11471 /* Assume that the initializer is constant. */
11472 *non_constant_p = false;
a723baf1
MM
11473
11474 if (token->type == CPP_EQ)
11475 {
11476 /* Consume the `='. */
11477 cp_lexer_consume_token (parser->lexer);
11478 /* Parse the initializer-clause. */
39703eb9 11479 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11480 }
11481 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11482 init = cp_parser_parenthesized_expression_list (parser, false,
11483 non_constant_p);
a723baf1
MM
11484 else
11485 {
11486 /* Anything else is an error. */
11487 cp_parser_error (parser, "expected initializer");
11488 init = error_mark_node;
11489 }
11490
11491 return init;
11492}
11493
21526606 11494/* Parse an initializer-clause.
a723baf1
MM
11495
11496 initializer-clause:
11497 assignment-expression
11498 { initializer-list , [opt] }
11499 { }
11500
21526606 11501 Returns an expression representing the initializer.
a723baf1
MM
11502
11503 If the `assignment-expression' production is used the value
21526606 11504 returned is simply a representation for the expression.
a723baf1
MM
11505
11506 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11507 the elements of the initializer-list (or NULL_TREE, if the last
11508 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11509 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11510 trailing `,' was provided. NON_CONSTANT_P is as for
11511 cp_parser_initializer. */
a723baf1
MM
11512
11513static tree
39703eb9 11514cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11515{
11516 tree initializer;
11517
11518 /* If it is not a `{', then we are looking at an
11519 assignment-expression. */
11520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e
GB
11521 {
11522 initializer
11523 = cp_parser_constant_expression (parser,
11524 /*allow_non_constant_p=*/true,
11525 non_constant_p);
11526 if (!*non_constant_p)
11527 initializer = fold_non_dependent_expr (initializer);
11528 }
a723baf1
MM
11529 else
11530 {
11531 /* Consume the `{' token. */
11532 cp_lexer_consume_token (parser->lexer);
11533 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11534 initializer = make_node (CONSTRUCTOR);
11535 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
21526606 11536 necessary, but check_initializer depends upon it, for
a723baf1
MM
11537 now. */
11538 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11539 /* If it's not a `}', then there is a non-trivial initializer. */
11540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11541 {
11542 /* Parse the initializer list. */
11543 CONSTRUCTOR_ELTS (initializer)
39703eb9 11544 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
11545 /* A trailing `,' token is allowed. */
11546 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11547 cp_lexer_consume_token (parser->lexer);
11548 }
a723baf1
MM
11549 /* Now, there should be a trailing `}'. */
11550 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11551 }
11552
11553 return initializer;
11554}
11555
11556/* Parse an initializer-list.
11557
11558 initializer-list:
11559 initializer-clause
11560 initializer-list , initializer-clause
11561
11562 GNU Extension:
21526606 11563
a723baf1
MM
11564 initializer-list:
11565 identifier : initializer-clause
11566 initializer-list, identifier : initializer-clause
11567
11568 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11569 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
11570 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11571 as for cp_parser_initializer. */
a723baf1
MM
11572
11573static tree
39703eb9 11574cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11575{
11576 tree initializers = NULL_TREE;
11577
39703eb9
MM
11578 /* Assume all of the expressions are constant. */
11579 *non_constant_p = false;
11580
a723baf1
MM
11581 /* Parse the rest of the list. */
11582 while (true)
11583 {
11584 cp_token *token;
11585 tree identifier;
11586 tree initializer;
39703eb9
MM
11587 bool clause_non_constant_p;
11588
a723baf1
MM
11589 /* If the next token is an identifier and the following one is a
11590 colon, we are looking at the GNU designated-initializer
11591 syntax. */
11592 if (cp_parser_allow_gnu_extensions_p (parser)
11593 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11594 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11595 {
11596 /* Consume the identifier. */
11597 identifier = cp_lexer_consume_token (parser->lexer)->value;
11598 /* Consume the `:'. */
11599 cp_lexer_consume_token (parser->lexer);
11600 }
11601 else
11602 identifier = NULL_TREE;
11603
11604 /* Parse the initializer. */
21526606 11605 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
11606 &clause_non_constant_p);
11607 /* If any clause is non-constant, so is the entire initializer. */
11608 if (clause_non_constant_p)
11609 *non_constant_p = true;
a723baf1
MM
11610 /* Add it to the list. */
11611 initializers = tree_cons (identifier, initializer, initializers);
11612
11613 /* If the next token is not a comma, we have reached the end of
11614 the list. */
11615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11616 break;
11617
11618 /* Peek at the next token. */
11619 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11620 /* If the next token is a `}', then we're still done. An
11621 initializer-clause can have a trailing `,' after the
11622 initializer-list and before the closing `}'. */
11623 if (token->type == CPP_CLOSE_BRACE)
11624 break;
11625
11626 /* Consume the `,' token. */
11627 cp_lexer_consume_token (parser->lexer);
11628 }
11629
11630 /* The initializers were built up in reverse order, so we need to
11631 reverse them now. */
11632 return nreverse (initializers);
11633}
11634
11635/* Classes [gram.class] */
11636
11637/* Parse a class-name.
11638
11639 class-name:
11640 identifier
11641 template-id
11642
11643 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11644 to indicate that names looked up in dependent types should be
11645 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11646 keyword has been used to indicate that the name that appears next
11647 is a template. TYPE_P is true iff the next name should be treated
11648 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11649 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11650 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11651 being defined in a class-head.
a723baf1
MM
11652
11653 Returns the TYPE_DECL representing the class. */
11654
11655static tree
21526606
EC
11656cp_parser_class_name (cp_parser *parser,
11657 bool typename_keyword_p,
11658 bool template_keyword_p,
a723baf1 11659 bool type_p,
a723baf1 11660 bool check_dependency_p,
a668c6ad
MM
11661 bool class_head_p,
11662 bool is_declaration)
a723baf1
MM
11663{
11664 tree decl;
11665 tree scope;
11666 bool typename_p;
e5976695
MM
11667 cp_token *token;
11668
11669 /* All class-names start with an identifier. */
11670 token = cp_lexer_peek_token (parser->lexer);
11671 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11672 {
11673 cp_parser_error (parser, "expected class-name");
11674 return error_mark_node;
11675 }
21526606 11676
a723baf1
MM
11677 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11678 to a template-id, so we save it here. */
11679 scope = parser->scope;
3adee96c
KL
11680 if (scope == error_mark_node)
11681 return error_mark_node;
21526606 11682
a723baf1
MM
11683 /* Any name names a type if we're following the `typename' keyword
11684 in a qualified name where the enclosing scope is type-dependent. */
11685 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11686 && dependent_type_p (scope));
e5976695
MM
11687 /* Handle the common case (an identifier, but not a template-id)
11688 efficiently. */
21526606 11689 if (token->type == CPP_NAME
f4abade9 11690 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 11691 {
a723baf1
MM
11692 tree identifier;
11693
11694 /* Look for the identifier. */
11695 identifier = cp_parser_identifier (parser);
11696 /* If the next token isn't an identifier, we are certainly not
11697 looking at a class-name. */
11698 if (identifier == error_mark_node)
11699 decl = error_mark_node;
11700 /* If we know this is a type-name, there's no need to look it
11701 up. */
11702 else if (typename_p)
11703 decl = identifier;
11704 else
11705 {
11706 /* If the next token is a `::', then the name must be a type
11707 name.
11708
11709 [basic.lookup.qual]
11710
11711 During the lookup for a name preceding the :: scope
11712 resolution operator, object, function, and enumerator
11713 names are ignored. */
11714 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11715 type_p = true;
11716 /* Look up the name. */
21526606 11717 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11718 type_p,
b0bc6e8e 11719 /*is_template=*/false,
eea9800f 11720 /*is_namespace=*/false,
a723baf1
MM
11721 check_dependency_p);
11722 }
11723 }
e5976695
MM
11724 else
11725 {
11726 /* Try a template-id. */
11727 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
11728 check_dependency_p,
11729 is_declaration);
e5976695
MM
11730 if (decl == error_mark_node)
11731 return error_mark_node;
11732 }
a723baf1
MM
11733
11734 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11735
11736 /* If this is a typename, create a TYPENAME_TYPE. */
11737 if (typename_p && decl != error_mark_node)
4bfb8bba
MM
11738 {
11739 decl = make_typename_type (scope, decl, /*complain=*/1);
11740 if (decl != error_mark_node)
11741 decl = TYPE_NAME (decl);
11742 }
a723baf1
MM
11743
11744 /* Check to see that it is really the name of a class. */
21526606 11745 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
11746 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11747 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11748 /* Situations like this:
11749
11750 template <typename T> struct A {
21526606 11751 typename T::template X<int>::I i;
a723baf1
MM
11752 };
11753
11754 are problematic. Is `T::template X<int>' a class-name? The
11755 standard does not seem to be definitive, but there is no other
11756 valid interpretation of the following `::'. Therefore, those
11757 names are considered class-names. */
78757caa 11758 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11759 else if (decl == error_mark_node
11760 || TREE_CODE (decl) != TYPE_DECL
11761 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11762 {
11763 cp_parser_error (parser, "expected class-name");
11764 return error_mark_node;
11765 }
11766
11767 return decl;
11768}
11769
11770/* Parse a class-specifier.
11771
11772 class-specifier:
11773 class-head { member-specification [opt] }
11774
11775 Returns the TREE_TYPE representing the class. */
11776
11777static tree
94edc4ab 11778cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11779{
11780 cp_token *token;
11781 tree type;
38b305d0 11782 tree attributes;
a723baf1
MM
11783 int has_trailing_semicolon;
11784 bool nested_name_specifier_p;
a723baf1 11785 unsigned saved_num_template_parameter_lists;
91b004e5 11786 bool pop_p = false;
a723baf1 11787
8d241e0b 11788 push_deferring_access_checks (dk_no_deferred);
cf22909c 11789
a723baf1
MM
11790 /* Parse the class-head. */
11791 type = cp_parser_class_head (parser,
38b305d0
JM
11792 &nested_name_specifier_p,
11793 &attributes);
a723baf1
MM
11794 /* If the class-head was a semantic disaster, skip the entire body
11795 of the class. */
11796 if (!type)
11797 {
11798 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11799 pop_deferring_access_checks ();
a723baf1
MM
11800 return error_mark_node;
11801 }
cf22909c 11802
a723baf1
MM
11803 /* Look for the `{'. */
11804 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11805 {
11806 pop_deferring_access_checks ();
11807 return error_mark_node;
11808 }
11809
a723baf1
MM
11810 /* Issue an error message if type-definitions are forbidden here. */
11811 cp_parser_check_type_definition (parser);
11812 /* Remember that we are defining one more class. */
11813 ++parser->num_classes_being_defined;
11814 /* Inside the class, surrounding template-parameter-lists do not
11815 apply. */
21526606
EC
11816 saved_num_template_parameter_lists
11817 = parser->num_template_parameter_lists;
a723baf1 11818 parser->num_template_parameter_lists = 0;
78757caa 11819
a723baf1 11820 /* Start the class. */
eeb23c11 11821 if (nested_name_specifier_p)
91b004e5 11822 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11823 type = begin_class_definition (type);
11824 if (type == error_mark_node)
9bcb9aae 11825 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11826 cp_parser_skip_to_closing_brace (parser);
11827 else
11828 /* Parse the member-specification. */
11829 cp_parser_member_specification_opt (parser);
11830 /* Look for the trailing `}'. */
11831 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11832 /* We get better error messages by noticing a common problem: a
11833 missing trailing `;'. */
11834 token = cp_lexer_peek_token (parser->lexer);
11835 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 11836 /* Look for trailing attributes to apply to this class. */
a723baf1 11837 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 11838 {
38b305d0
JM
11839 tree sub_attr = cp_parser_attributes_opt (parser);
11840 attributes = chainon (attributes, sub_attr);
560ad596 11841 }
38b305d0
JM
11842 if (type != error_mark_node)
11843 type = finish_struct (type, attributes);
91b004e5 11844 if (pop_p)
560ad596 11845 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11846 /* If this class is not itself within the scope of another class,
11847 then we need to parse the bodies of all of the queued function
11848 definitions. Note that the queued functions defined in a class
11849 are not always processed immediately following the
11850 class-specifier for that class. Consider:
11851
11852 struct A {
11853 struct B { void f() { sizeof (A); } };
11854 };
11855
11856 If `f' were processed before the processing of `A' were
11857 completed, there would be no way to compute the size of `A'.
11858 Note that the nesting we are interested in here is lexical --
11859 not the semantic nesting given by TYPE_CONTEXT. In particular,
11860 for:
11861
11862 struct A { struct B; };
11863 struct A::B { void f() { } };
11864
11865 there is no need to delay the parsing of `A::B::f'. */
21526606 11866 if (--parser->num_classes_being_defined == 0)
a723baf1 11867 {
8218bd34
MM
11868 tree queue_entry;
11869 tree fn;
a723baf1 11870
8218bd34
MM
11871 /* In a first pass, parse default arguments to the functions.
11872 Then, in a second pass, parse the bodies of the functions.
11873 This two-phased approach handles cases like:
21526606
EC
11874
11875 struct S {
11876 void f() { g(); }
8218bd34
MM
11877 void g(int i = 3);
11878 };
11879
11880 */
8db1028e
NS
11881 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11882 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11883 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11884 TREE_PURPOSE (parser->unparsed_functions_queues)
11885 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11886 {
11887 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11888 /* Make sure that any template parameters are in scope. */
11889 maybe_begin_member_template_processing (fn);
11890 /* If there are default arguments that have not yet been processed,
11891 take care of them now. */
11892 cp_parser_late_parsing_default_args (parser, fn);
11893 /* Remove any template parameters from the symbol table. */
11894 maybe_end_member_template_processing ();
11895 }
11896 /* Now parse the body of the functions. */
8db1028e
NS
11897 for (TREE_VALUE (parser->unparsed_functions_queues)
11898 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11899 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11900 TREE_VALUE (parser->unparsed_functions_queues)
11901 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11902 {
a723baf1 11903 /* Figure out which function we need to process. */
a723baf1
MM
11904 fn = TREE_VALUE (queue_entry);
11905
4543ee47
ZD
11906 /* A hack to prevent garbage collection. */
11907 function_depth++;
11908
a723baf1
MM
11909 /* Parse the function. */
11910 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 11911 function_depth--;
a723baf1
MM
11912 }
11913
a723baf1
MM
11914 }
11915
11916 /* Put back any saved access checks. */
cf22909c 11917 pop_deferring_access_checks ();
a723baf1
MM
11918
11919 /* Restore the count of active template-parameter-lists. */
11920 parser->num_template_parameter_lists
11921 = saved_num_template_parameter_lists;
11922
11923 return type;
11924}
11925
11926/* Parse a class-head.
11927
11928 class-head:
11929 class-key identifier [opt] base-clause [opt]
11930 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
11931 class-key nested-name-specifier [opt] template-id
11932 base-clause [opt]
a723baf1
MM
11933
11934 GNU Extensions:
11935 class-key attributes identifier [opt] base-clause [opt]
11936 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
11937 class-key attributes nested-name-specifier [opt] template-id
11938 base-clause [opt]
a723baf1
MM
11939
11940 Returns the TYPE of the indicated class. Sets
11941 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11942 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11943
11944 Returns NULL_TREE if the class-head is syntactically valid, but
11945 semantically invalid in a way that means we should skip the entire
11946 body of the class. */
11947
11948static tree
21526606 11949cp_parser_class_head (cp_parser* parser,
38b305d0
JM
11950 bool* nested_name_specifier_p,
11951 tree *attributes_p)
a723baf1
MM
11952{
11953 cp_token *token;
11954 tree nested_name_specifier;
11955 enum tag_types class_key;
11956 tree id = NULL_TREE;
11957 tree type = NULL_TREE;
11958 tree attributes;
11959 bool template_id_p = false;
11960 bool qualified_p = false;
11961 bool invalid_nested_name_p = false;
afb0918a 11962 bool invalid_explicit_specialization_p = false;
91b004e5 11963 bool pop_p = false;
a723baf1
MM
11964 unsigned num_templates;
11965
11966 /* Assume no nested-name-specifier will be present. */
11967 *nested_name_specifier_p = false;
11968 /* Assume no template parameter lists will be used in defining the
11969 type. */
11970 num_templates = 0;
11971
11972 /* Look for the class-key. */
11973 class_key = cp_parser_class_key (parser);
11974 if (class_key == none_type)
11975 return error_mark_node;
11976
11977 /* Parse the attributes. */
11978 attributes = cp_parser_attributes_opt (parser);
11979
11980 /* If the next token is `::', that is invalid -- but sometimes
11981 people do try to write:
11982
21526606 11983 struct ::S {};
a723baf1
MM
11984
11985 Handle this gracefully by accepting the extra qualifier, and then
11986 issuing an error about it later if this really is a
2050a1bb 11987 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11988 specifier, remain silent. */
11989 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11990 qualified_p = true;
11991
8d241e0b
KL
11992 push_deferring_access_checks (dk_no_check);
11993
a723baf1
MM
11994 /* Determine the name of the class. Begin by looking for an
11995 optional nested-name-specifier. */
21526606 11996 nested_name_specifier
a723baf1
MM
11997 = cp_parser_nested_name_specifier_opt (parser,
11998 /*typename_keyword_p=*/false,
66d418e6 11999 /*check_dependency_p=*/false,
a668c6ad
MM
12000 /*type_p=*/false,
12001 /*is_declaration=*/false);
a723baf1
MM
12002 /* If there was a nested-name-specifier, then there *must* be an
12003 identifier. */
12004 if (nested_name_specifier)
12005 {
12006 /* Although the grammar says `identifier', it really means
12007 `class-name' or `template-name'. You are only allowed to
12008 define a class that has already been declared with this
21526606 12009 syntax.
a723baf1
MM
12010
12011 The proposed resolution for Core Issue 180 says that whever
12012 you see `class T::X' you should treat `X' as a type-name.
21526606 12013
a723baf1 12014 It is OK to define an inaccessible class; for example:
21526606 12015
a723baf1
MM
12016 class A { class B; };
12017 class A::B {};
21526606 12018
a723baf1
MM
12019 We do not know if we will see a class-name, or a
12020 template-name. We look for a class-name first, in case the
12021 class-name is a template-id; if we looked for the
12022 template-name first we would stop after the template-name. */
12023 cp_parser_parse_tentatively (parser);
12024 type = cp_parser_class_name (parser,
12025 /*typename_keyword_p=*/false,
12026 /*template_keyword_p=*/false,
12027 /*type_p=*/true,
a723baf1 12028 /*check_dependency_p=*/false,
a668c6ad
MM
12029 /*class_head_p=*/true,
12030 /*is_declaration=*/false);
a723baf1
MM
12031 /* If that didn't work, ignore the nested-name-specifier. */
12032 if (!cp_parser_parse_definitely (parser))
12033 {
12034 invalid_nested_name_p = true;
12035 id = cp_parser_identifier (parser);
12036 if (id == error_mark_node)
12037 id = NULL_TREE;
12038 }
12039 /* If we could not find a corresponding TYPE, treat this
12040 declaration like an unqualified declaration. */
12041 if (type == error_mark_node)
12042 nested_name_specifier = NULL_TREE;
12043 /* Otherwise, count the number of templates used in TYPE and its
12044 containing scopes. */
21526606 12045 else
a723baf1
MM
12046 {
12047 tree scope;
12048
21526606 12049 for (scope = TREE_TYPE (type);
a723baf1 12050 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12051 scope = (TYPE_P (scope)
a723baf1 12052 ? TYPE_CONTEXT (scope)
21526606
EC
12053 : DECL_CONTEXT (scope)))
12054 if (TYPE_P (scope)
a723baf1
MM
12055 && CLASS_TYPE_P (scope)
12056 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12057 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12058 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12059 ++num_templates;
12060 }
12061 }
12062 /* Otherwise, the identifier is optional. */
12063 else
12064 {
12065 /* We don't know whether what comes next is a template-id,
12066 an identifier, or nothing at all. */
12067 cp_parser_parse_tentatively (parser);
12068 /* Check for a template-id. */
21526606 12069 id = cp_parser_template_id (parser,
a723baf1 12070 /*template_keyword_p=*/false,
a668c6ad
MM
12071 /*check_dependency_p=*/true,
12072 /*is_declaration=*/true);
a723baf1
MM
12073 /* If that didn't work, it could still be an identifier. */
12074 if (!cp_parser_parse_definitely (parser))
12075 {
12076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12077 id = cp_parser_identifier (parser);
12078 else
12079 id = NULL_TREE;
12080 }
12081 else
12082 {
12083 template_id_p = true;
12084 ++num_templates;
12085 }
12086 }
12087
8d241e0b
KL
12088 pop_deferring_access_checks ();
12089
ee43dab5
MM
12090 cp_parser_check_for_invalid_template_id (parser, id);
12091
a723baf1
MM
12092 /* If it's not a `:' or a `{' then we can't really be looking at a
12093 class-head, since a class-head only appears as part of a
12094 class-specifier. We have to detect this situation before calling
12095 xref_tag, since that has irreversible side-effects. */
12096 if (!cp_parser_next_token_starts_class_definition_p (parser))
12097 {
12098 cp_parser_error (parser, "expected `{' or `:'");
12099 return error_mark_node;
12100 }
12101
12102 /* At this point, we're going ahead with the class-specifier, even
12103 if some other problem occurs. */
12104 cp_parser_commit_to_tentative_parse (parser);
12105 /* Issue the error about the overly-qualified name now. */
12106 if (qualified_p)
12107 cp_parser_error (parser,
12108 "global qualification of class name is invalid");
12109 else if (invalid_nested_name_p)
12110 cp_parser_error (parser,
12111 "qualified name does not name a class");
88081599
MM
12112 else if (nested_name_specifier)
12113 {
12114 tree scope;
12115 /* Figure out in what scope the declaration is being placed. */
12116 scope = current_scope ();
12117 if (!scope)
12118 scope = current_namespace;
12119 /* If that scope does not contain the scope in which the
12120 class was originally declared, the program is invalid. */
12121 if (scope && !is_ancestor (scope, nested_name_specifier))
12122 {
12123 error ("declaration of `%D' in `%D' which does not "
12124 "enclose `%D'", type, scope, nested_name_specifier);
12125 type = NULL_TREE;
12126 goto done;
12127 }
12128 /* [dcl.meaning]
12129
12130 A declarator-id shall not be qualified exception of the
12131 definition of a ... nested class outside of its class
12132 ... [or] a the definition or explicit instantiation of a
12133 class member of a namespace outside of its namespace. */
12134 if (scope == nested_name_specifier)
12135 {
12136 pedwarn ("extra qualification ignored");
12137 nested_name_specifier = NULL_TREE;
12138 num_templates = 0;
12139 }
12140 }
afb0918a
MM
12141 /* An explicit-specialization must be preceded by "template <>". If
12142 it is not, try to recover gracefully. */
21526606 12143 if (at_namespace_scope_p ()
afb0918a 12144 && parser->num_template_parameter_lists == 0
eeb23c11 12145 && template_id_p)
afb0918a
MM
12146 {
12147 error ("an explicit specialization must be preceded by 'template <>'");
12148 invalid_explicit_specialization_p = true;
12149 /* Take the same action that would have been taken by
12150 cp_parser_explicit_specialization. */
12151 ++parser->num_template_parameter_lists;
12152 begin_specialization ();
12153 }
12154 /* There must be no "return" statements between this point and the
12155 end of this function; set "type "to the correct return value and
12156 use "goto done;" to return. */
a723baf1
MM
12157 /* Make sure that the right number of template parameters were
12158 present. */
12159 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12160 {
12161 /* If something went wrong, there is no point in even trying to
12162 process the class-definition. */
12163 type = NULL_TREE;
12164 goto done;
12165 }
a723baf1 12166
a723baf1
MM
12167 /* Look up the type. */
12168 if (template_id_p)
12169 {
12170 type = TREE_TYPE (id);
12171 maybe_process_partial_specialization (type);
12172 }
12173 else if (!nested_name_specifier)
12174 {
12175 /* If the class was unnamed, create a dummy name. */
12176 if (!id)
12177 id = make_anon_name ();
38b305d0 12178 type = xref_tag (class_key, id, /*globalize=*/false,
cbd63935 12179 parser->num_template_parameter_lists);
a723baf1
MM
12180 }
12181 else
12182 {
a723baf1 12183 tree class_type;
91b004e5 12184 bool pop_p = false;
a723baf1
MM
12185
12186 /* Given:
12187
12188 template <typename T> struct S { struct T };
14d22dd6 12189 template <typename T> struct S<T>::T { };
a723baf1
MM
12190
12191 we will get a TYPENAME_TYPE when processing the definition of
12192 `S::T'. We need to resolve it to the actual type before we
12193 try to define it. */
12194 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12195 {
14d22dd6
MM
12196 class_type = resolve_typename_type (TREE_TYPE (type),
12197 /*only_current_p=*/false);
12198 if (class_type != error_mark_node)
12199 type = TYPE_NAME (class_type);
12200 else
12201 {
12202 cp_parser_error (parser, "could not resolve typename type");
12203 type = error_mark_node;
12204 }
a723baf1
MM
12205 }
12206
560ad596
MM
12207 maybe_process_partial_specialization (TREE_TYPE (type));
12208 class_type = current_class_type;
12209 /* Enter the scope indicated by the nested-name-specifier. */
12210 if (nested_name_specifier)
91b004e5 12211 pop_p = push_scope (nested_name_specifier);
560ad596
MM
12212 /* Get the canonical version of this type. */
12213 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12214 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12215 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12216 type = push_template_decl (type);
12217 type = TREE_TYPE (type);
12218 if (nested_name_specifier)
eeb23c11
MM
12219 {
12220 *nested_name_specifier_p = true;
91b004e5
MM
12221 if (pop_p)
12222 pop_scope (nested_name_specifier);
eeb23c11 12223 }
a723baf1
MM
12224 }
12225 /* Indicate whether this class was declared as a `class' or as a
12226 `struct'. */
12227 if (TREE_CODE (type) == RECORD_TYPE)
12228 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12229 cp_parser_check_class_key (class_key, type);
12230
12231 /* Enter the scope containing the class; the names of base classes
12232 should be looked up in that context. For example, given:
12233
12234 struct A { struct B {}; struct C; };
12235 struct A::C : B {};
12236
12237 is valid. */
12238 if (nested_name_specifier)
91b004e5 12239 pop_p = push_scope (nested_name_specifier);
a723baf1
MM
12240 /* Now, look for the base-clause. */
12241 token = cp_lexer_peek_token (parser->lexer);
12242 if (token->type == CPP_COLON)
12243 {
12244 tree bases;
12245
12246 /* Get the list of base-classes. */
12247 bases = cp_parser_base_clause (parser);
12248 /* Process them. */
12249 xref_basetypes (type, bases);
12250 }
12251 /* Leave the scope given by the nested-name-specifier. We will
12252 enter the class scope itself while processing the members. */
91b004e5 12253 if (pop_p)
a723baf1
MM
12254 pop_scope (nested_name_specifier);
12255
afb0918a
MM
12256 done:
12257 if (invalid_explicit_specialization_p)
12258 {
12259 end_specialization ();
12260 --parser->num_template_parameter_lists;
12261 }
38b305d0 12262 *attributes_p = attributes;
a723baf1
MM
12263 return type;
12264}
12265
12266/* Parse a class-key.
12267
12268 class-key:
12269 class
12270 struct
12271 union
12272
12273 Returns the kind of class-key specified, or none_type to indicate
12274 error. */
12275
12276static enum tag_types
94edc4ab 12277cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12278{
12279 cp_token *token;
12280 enum tag_types tag_type;
12281
12282 /* Look for the class-key. */
12283 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12284 if (!token)
12285 return none_type;
12286
12287 /* Check to see if the TOKEN is a class-key. */
12288 tag_type = cp_parser_token_is_class_key (token);
12289 if (!tag_type)
12290 cp_parser_error (parser, "expected class-key");
12291 return tag_type;
12292}
12293
12294/* Parse an (optional) member-specification.
12295
12296 member-specification:
12297 member-declaration member-specification [opt]
12298 access-specifier : member-specification [opt] */
12299
12300static void
94edc4ab 12301cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12302{
12303 while (true)
12304 {
12305 cp_token *token;
12306 enum rid keyword;
12307
12308 /* Peek at the next token. */
12309 token = cp_lexer_peek_token (parser->lexer);
12310 /* If it's a `}', or EOF then we've seen all the members. */
12311 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12312 break;
12313
12314 /* See if this token is a keyword. */
12315 keyword = token->keyword;
12316 switch (keyword)
12317 {
12318 case RID_PUBLIC:
12319 case RID_PROTECTED:
12320 case RID_PRIVATE:
12321 /* Consume the access-specifier. */
12322 cp_lexer_consume_token (parser->lexer);
12323 /* Remember which access-specifier is active. */
12324 current_access_specifier = token->value;
12325 /* Look for the `:'. */
12326 cp_parser_require (parser, CPP_COLON, "`:'");
12327 break;
12328
12329 default:
12330 /* Otherwise, the next construction must be a
12331 member-declaration. */
12332 cp_parser_member_declaration (parser);
a723baf1
MM
12333 }
12334 }
12335}
12336
21526606 12337/* Parse a member-declaration.
a723baf1
MM
12338
12339 member-declaration:
12340 decl-specifier-seq [opt] member-declarator-list [opt] ;
12341 function-definition ; [opt]
12342 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12343 using-declaration
21526606 12344 template-declaration
a723baf1
MM
12345
12346 member-declarator-list:
12347 member-declarator
12348 member-declarator-list , member-declarator
12349
12350 member-declarator:
21526606 12351 declarator pure-specifier [opt]
a723baf1 12352 declarator constant-initializer [opt]
21526606 12353 identifier [opt] : constant-expression
a723baf1
MM
12354
12355 GNU Extensions:
12356
12357 member-declaration:
12358 __extension__ member-declaration
12359
12360 member-declarator:
12361 declarator attributes [opt] pure-specifier [opt]
12362 declarator attributes [opt] constant-initializer [opt]
12363 identifier [opt] attributes [opt] : constant-expression */
12364
12365static void
94edc4ab 12366cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
12367{
12368 tree decl_specifiers;
12369 tree prefix_attributes;
12370 tree decl;
560ad596 12371 int declares_class_or_enum;
a723baf1
MM
12372 bool friend_p;
12373 cp_token *token;
12374 int saved_pedantic;
12375
12376 /* Check for the `__extension__' keyword. */
12377 if (cp_parser_extension_opt (parser, &saved_pedantic))
12378 {
12379 /* Recurse. */
12380 cp_parser_member_declaration (parser);
12381 /* Restore the old value of the PEDANTIC flag. */
12382 pedantic = saved_pedantic;
12383
12384 return;
12385 }
12386
12387 /* Check for a template-declaration. */
12388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12389 {
12390 /* Parse the template-declaration. */
12391 cp_parser_template_declaration (parser, /*member_p=*/true);
12392
12393 return;
12394 }
12395
12396 /* Check for a using-declaration. */
12397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12398 {
12399 /* Parse the using-declaration. */
12400 cp_parser_using_declaration (parser);
12401
12402 return;
12403 }
21526606 12404
a723baf1 12405 /* Parse the decl-specifier-seq. */
21526606 12406 decl_specifiers
a723baf1
MM
12407 = cp_parser_decl_specifier_seq (parser,
12408 CP_PARSER_FLAGS_OPTIONAL,
12409 &prefix_attributes,
12410 &declares_class_or_enum);
8fbc5ae7 12411 /* Check for an invalid type-name. */
2097b5f2 12412 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 12413 return;
a723baf1
MM
12414 /* If there is no declarator, then the decl-specifier-seq should
12415 specify a type. */
12416 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12417 {
12418 /* If there was no decl-specifier-seq, and the next token is a
12419 `;', then we have something like:
12420
12421 struct S { ; };
12422
12423 [class.mem]
12424
12425 Each member-declaration shall declare at least one member
12426 name of the class. */
12427 if (!decl_specifiers)
12428 {
12429 if (pedantic)
12430 pedwarn ("extra semicolon");
12431 }
21526606 12432 else
a723baf1
MM
12433 {
12434 tree type;
21526606 12435
a723baf1
MM
12436 /* See if this declaration is a friend. */
12437 friend_p = cp_parser_friend_p (decl_specifiers);
12438 /* If there were decl-specifiers, check to see if there was
12439 a class-declaration. */
12440 type = check_tag_decl (decl_specifiers);
12441 /* Nested classes have already been added to the class, but
12442 a `friend' needs to be explicitly registered. */
12443 if (friend_p)
12444 {
12445 /* If the `friend' keyword was present, the friend must
12446 be introduced with a class-key. */
12447 if (!declares_class_or_enum)
12448 error ("a class-key must be used when declaring a friend");
12449 /* In this case:
12450
21526606
EC
12451 template <typename T> struct A {
12452 friend struct A<T>::B;
a723baf1 12453 };
21526606 12454
a723baf1
MM
12455 A<T>::B will be represented by a TYPENAME_TYPE, and
12456 therefore not recognized by check_tag_decl. */
12457 if (!type)
12458 {
12459 tree specifier;
12460
21526606 12461 for (specifier = decl_specifiers;
a723baf1
MM
12462 specifier;
12463 specifier = TREE_CHAIN (specifier))
12464 {
12465 tree s = TREE_VALUE (specifier);
12466
c003e212
GDR
12467 if (TREE_CODE (s) == IDENTIFIER_NODE)
12468 get_global_value_if_present (s, &type);
a723baf1
MM
12469 if (TREE_CODE (s) == TYPE_DECL)
12470 s = TREE_TYPE (s);
12471 if (TYPE_P (s))
12472 {
12473 type = s;
12474 break;
12475 }
12476 }
12477 }
fdd09134 12478 if (!type || !TYPE_P (type))
a723baf1
MM
12479 error ("friend declaration does not name a class or "
12480 "function");
12481 else
19db77ce
KL
12482 make_friend_class (current_class_type, type,
12483 /*complain=*/true);
a723baf1
MM
12484 }
12485 /* If there is no TYPE, an error message will already have
12486 been issued. */
12487 else if (!type)
12488 ;
12489 /* An anonymous aggregate has to be handled specially; such
12490 a declaration really declares a data member (with a
12491 particular type), as opposed to a nested class. */
12492 else if (ANON_AGGR_TYPE_P (type))
12493 {
12494 /* Remove constructors and such from TYPE, now that we
34cd5ae7 12495 know it is an anonymous aggregate. */
a723baf1
MM
12496 fixup_anonymous_aggr (type);
12497 /* And make the corresponding data member. */
12498 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12499 /* Add it to the class. */
12500 finish_member_declaration (decl);
12501 }
37d407a1
KL
12502 else
12503 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
12504 }
12505 }
12506 else
12507 {
12508 /* See if these declarations will be friends. */
12509 friend_p = cp_parser_friend_p (decl_specifiers);
12510
21526606 12511 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
12512 declaration. */
12513 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12514 {
12515 tree attributes = NULL_TREE;
12516 tree first_attribute;
12517
12518 /* Peek at the next token. */
12519 token = cp_lexer_peek_token (parser->lexer);
12520
12521 /* Check for a bitfield declaration. */
12522 if (token->type == CPP_COLON
12523 || (token->type == CPP_NAME
21526606 12524 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
12525 == CPP_COLON))
12526 {
12527 tree identifier;
12528 tree width;
12529
12530 /* Get the name of the bitfield. Note that we cannot just
12531 check TOKEN here because it may have been invalidated by
12532 the call to cp_lexer_peek_nth_token above. */
12533 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12534 identifier = cp_parser_identifier (parser);
12535 else
12536 identifier = NULL_TREE;
12537
12538 /* Consume the `:' token. */
12539 cp_lexer_consume_token (parser->lexer);
12540 /* Get the width of the bitfield. */
21526606 12541 width
14d22dd6
MM
12542 = cp_parser_constant_expression (parser,
12543 /*allow_non_constant=*/false,
12544 NULL);
a723baf1
MM
12545
12546 /* Look for attributes that apply to the bitfield. */
12547 attributes = cp_parser_attributes_opt (parser);
12548 /* Remember which attributes are prefix attributes and
12549 which are not. */
12550 first_attribute = attributes;
12551 /* Combine the attributes. */
12552 attributes = chainon (prefix_attributes, attributes);
12553
12554 /* Create the bitfield declaration. */
21526606 12555 decl = grokbitfield (identifier,
a723baf1
MM
12556 decl_specifiers,
12557 width);
12558 /* Apply the attributes. */
12559 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12560 }
12561 else
12562 {
12563 tree declarator;
12564 tree initializer;
12565 tree asm_specification;
7efa3e22 12566 int ctor_dtor_or_conv_p;
a723baf1
MM
12567
12568 /* Parse the declarator. */
21526606 12569 declarator
62b8a44e 12570 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
12571 &ctor_dtor_or_conv_p,
12572 /*parenthesized_p=*/NULL);
a723baf1
MM
12573
12574 /* If something went wrong parsing the declarator, make sure
12575 that we at least consume some tokens. */
12576 if (declarator == error_mark_node)
12577 {
12578 /* Skip to the end of the statement. */
12579 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
12580 /* If the next token is not a semicolon, that is
12581 probably because we just skipped over the body of
12582 a function. So, we consume a semicolon if
12583 present, but do not issue an error message if it
12584 is not present. */
12585 if (cp_lexer_next_token_is (parser->lexer,
12586 CPP_SEMICOLON))
12587 cp_lexer_consume_token (parser->lexer);
12588 return;
a723baf1
MM
12589 }
12590
21526606 12591 cp_parser_check_for_definition_in_return_type
560ad596
MM
12592 (declarator, declares_class_or_enum);
12593
a723baf1
MM
12594 /* Look for an asm-specification. */
12595 asm_specification = cp_parser_asm_specification_opt (parser);
12596 /* Look for attributes that apply to the declaration. */
12597 attributes = cp_parser_attributes_opt (parser);
12598 /* Remember which attributes are prefix attributes and
12599 which are not. */
12600 first_attribute = attributes;
12601 /* Combine the attributes. */
12602 attributes = chainon (prefix_attributes, attributes);
12603
12604 /* If it's an `=', then we have a constant-initializer or a
12605 pure-specifier. It is not correct to parse the
12606 initializer before registering the member declaration
12607 since the member declaration should be in scope while
12608 its initializer is processed. However, the rest of the
12609 front end does not yet provide an interface that allows
12610 us to handle this correctly. */
12611 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12612 {
12613 /* In [class.mem]:
12614
12615 A pure-specifier shall be used only in the declaration of
21526606 12616 a virtual function.
a723baf1
MM
12617
12618 A member-declarator can contain a constant-initializer
12619 only if it declares a static member of integral or
21526606 12620 enumeration type.
a723baf1
MM
12621
12622 Therefore, if the DECLARATOR is for a function, we look
12623 for a pure-specifier; otherwise, we look for a
12624 constant-initializer. When we call `grokfield', it will
12625 perform more stringent semantics checks. */
12626 if (TREE_CODE (declarator) == CALL_EXPR)
12627 initializer = cp_parser_pure_specifier (parser);
12628 else
4bb8ca28
MM
12629 /* Parse the initializer. */
12630 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
12631 }
12632 /* Otherwise, there is no initializer. */
12633 else
12634 initializer = NULL_TREE;
12635
12636 /* See if we are probably looking at a function
12637 definition. We are certainly not looking at at a
12638 member-declarator. Calling `grokfield' has
12639 side-effects, so we must not do it unless we are sure
12640 that we are looking at a member-declarator. */
21526606 12641 if (cp_parser_token_starts_function_definition_p
a723baf1 12642 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
12643 {
12644 /* The grammar does not allow a pure-specifier to be
12645 used when a member function is defined. (It is
12646 possible that this fact is an oversight in the
12647 standard, since a pure function may be defined
12648 outside of the class-specifier. */
12649 if (initializer)
12650 error ("pure-specifier on function-definition");
12651 decl = cp_parser_save_member_function_body (parser,
12652 decl_specifiers,
12653 declarator,
12654 attributes);
12655 /* If the member was not a friend, declare it here. */
12656 if (!friend_p)
12657 finish_member_declaration (decl);
12658 /* Peek at the next token. */
12659 token = cp_lexer_peek_token (parser->lexer);
12660 /* If the next token is a semicolon, consume it. */
12661 if (token->type == CPP_SEMICOLON)
12662 cp_lexer_consume_token (parser->lexer);
12663 return;
12664 }
a723baf1 12665 else
39703eb9
MM
12666 {
12667 /* Create the declaration. */
21526606 12668 decl = grokfield (declarator, decl_specifiers,
ee3071ef 12669 initializer, asm_specification,
39703eb9
MM
12670 attributes);
12671 /* Any initialization must have been from a
12672 constant-expression. */
12673 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12674 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12675 }
a723baf1
MM
12676 }
12677
12678 /* Reset PREFIX_ATTRIBUTES. */
12679 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12680 attributes = TREE_CHAIN (attributes);
12681 if (attributes)
12682 TREE_CHAIN (attributes) = NULL_TREE;
12683
12684 /* If there is any qualification still in effect, clear it
12685 now; we will be starting fresh with the next declarator. */
12686 parser->scope = NULL_TREE;
12687 parser->qualifying_scope = NULL_TREE;
12688 parser->object_scope = NULL_TREE;
12689 /* If it's a `,', then there are more declarators. */
12690 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12691 cp_lexer_consume_token (parser->lexer);
12692 /* If the next token isn't a `;', then we have a parse error. */
12693 else if (cp_lexer_next_token_is_not (parser->lexer,
12694 CPP_SEMICOLON))
12695 {
12696 cp_parser_error (parser, "expected `;'");
04c06002 12697 /* Skip tokens until we find a `;'. */
a723baf1
MM
12698 cp_parser_skip_to_end_of_statement (parser);
12699
12700 break;
12701 }
12702
12703 if (decl)
12704 {
12705 /* Add DECL to the list of members. */
12706 if (!friend_p)
12707 finish_member_declaration (decl);
12708
a723baf1 12709 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12710 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12711 }
12712 }
12713 }
12714
4bb8ca28 12715 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
12716}
12717
12718/* Parse a pure-specifier.
12719
12720 pure-specifier:
12721 = 0
12722
12723 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12724 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12725
12726static tree
94edc4ab 12727cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12728{
12729 cp_token *token;
12730
12731 /* Look for the `=' token. */
12732 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12733 return error_mark_node;
12734 /* Look for the `0' token. */
12735 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12736 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12737 to get information from the lexer about how the number was
12738 spelled in order to fix this problem. */
12739 if (!token || !integer_zerop (token->value))
12740 return error_mark_node;
12741
12742 return integer_zero_node;
12743}
12744
12745/* Parse a constant-initializer.
12746
12747 constant-initializer:
12748 = constant-expression
12749
12750 Returns a representation of the constant-expression. */
12751
12752static tree
94edc4ab 12753cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12754{
12755 /* Look for the `=' token. */
12756 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12757 return error_mark_node;
12758
12759 /* It is invalid to write:
12760
12761 struct S { static const int i = { 7 }; };
12762
12763 */
12764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12765 {
12766 cp_parser_error (parser,
12767 "a brace-enclosed initializer is not allowed here");
12768 /* Consume the opening brace. */
12769 cp_lexer_consume_token (parser->lexer);
12770 /* Skip the initializer. */
12771 cp_parser_skip_to_closing_brace (parser);
12772 /* Look for the trailing `}'. */
12773 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 12774
a723baf1
MM
12775 return error_mark_node;
12776 }
12777
21526606 12778 return cp_parser_constant_expression (parser,
14d22dd6
MM
12779 /*allow_non_constant=*/false,
12780 NULL);
a723baf1
MM
12781}
12782
12783/* Derived classes [gram.class.derived] */
12784
12785/* Parse a base-clause.
12786
12787 base-clause:
21526606 12788 : base-specifier-list
a723baf1
MM
12789
12790 base-specifier-list:
12791 base-specifier
12792 base-specifier-list , base-specifier
12793
12794 Returns a TREE_LIST representing the base-classes, in the order in
12795 which they were declared. The representation of each node is as
21526606 12796 described by cp_parser_base_specifier.
a723baf1
MM
12797
12798 In the case that no bases are specified, this function will return
12799 NULL_TREE, not ERROR_MARK_NODE. */
12800
12801static tree
94edc4ab 12802cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12803{
12804 tree bases = NULL_TREE;
12805
12806 /* Look for the `:' that begins the list. */
12807 cp_parser_require (parser, CPP_COLON, "`:'");
12808
12809 /* Scan the base-specifier-list. */
12810 while (true)
12811 {
12812 cp_token *token;
12813 tree base;
12814
12815 /* Look for the base-specifier. */
12816 base = cp_parser_base_specifier (parser);
12817 /* Add BASE to the front of the list. */
12818 if (base != error_mark_node)
12819 {
12820 TREE_CHAIN (base) = bases;
12821 bases = base;
12822 }
12823 /* Peek at the next token. */
12824 token = cp_lexer_peek_token (parser->lexer);
12825 /* If it's not a comma, then the list is complete. */
12826 if (token->type != CPP_COMMA)
12827 break;
12828 /* Consume the `,'. */
12829 cp_lexer_consume_token (parser->lexer);
12830 }
12831
12832 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12833 base class had a qualified name. However, the next name that
12834 appears is certainly not qualified. */
12835 parser->scope = NULL_TREE;
12836 parser->qualifying_scope = NULL_TREE;
12837 parser->object_scope = NULL_TREE;
12838
12839 return nreverse (bases);
12840}
12841
12842/* Parse a base-specifier.
12843
12844 base-specifier:
12845 :: [opt] nested-name-specifier [opt] class-name
12846 virtual access-specifier [opt] :: [opt] nested-name-specifier
12847 [opt] class-name
12848 access-specifier virtual [opt] :: [opt] nested-name-specifier
12849 [opt] class-name
12850
12851 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12852 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12853 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12854 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 12855
a723baf1 12856static tree
94edc4ab 12857cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12858{
12859 cp_token *token;
12860 bool done = false;
12861 bool virtual_p = false;
12862 bool duplicate_virtual_error_issued_p = false;
12863 bool duplicate_access_error_issued_p = false;
bbaab916 12864 bool class_scope_p, template_p;
dbbf88d1 12865 tree access = access_default_node;
a723baf1
MM
12866 tree type;
12867
12868 /* Process the optional `virtual' and `access-specifier'. */
12869 while (!done)
12870 {
12871 /* Peek at the next token. */
12872 token = cp_lexer_peek_token (parser->lexer);
12873 /* Process `virtual'. */
12874 switch (token->keyword)
12875 {
12876 case RID_VIRTUAL:
12877 /* If `virtual' appears more than once, issue an error. */
12878 if (virtual_p && !duplicate_virtual_error_issued_p)
12879 {
12880 cp_parser_error (parser,
12881 "`virtual' specified more than once in base-specified");
12882 duplicate_virtual_error_issued_p = true;
12883 }
12884
12885 virtual_p = true;
12886
12887 /* Consume the `virtual' token. */
12888 cp_lexer_consume_token (parser->lexer);
12889
12890 break;
12891
12892 case RID_PUBLIC:
12893 case RID_PROTECTED:
12894 case RID_PRIVATE:
12895 /* If more than one access specifier appears, issue an
12896 error. */
dbbf88d1
NS
12897 if (access != access_default_node
12898 && !duplicate_access_error_issued_p)
a723baf1
MM
12899 {
12900 cp_parser_error (parser,
12901 "more than one access specifier in base-specified");
12902 duplicate_access_error_issued_p = true;
12903 }
12904
dbbf88d1 12905 access = ridpointers[(int) token->keyword];
a723baf1
MM
12906
12907 /* Consume the access-specifier. */
12908 cp_lexer_consume_token (parser->lexer);
12909
12910 break;
12911
12912 default:
12913 done = true;
12914 break;
12915 }
12916 }
852dcbdd 12917 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 12918 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
12919 as base classes. */
12920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12921 {
12922 if (!processing_template_decl)
12923 error ("keyword `typename' not allowed outside of templates");
12924 else
12925 error ("keyword `typename' not allowed in this context "
12926 "(the base class is implicitly a type)");
12927 cp_lexer_consume_token (parser->lexer);
12928 }
a723baf1 12929
a723baf1
MM
12930 /* Look for the optional `::' operator. */
12931 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12932 /* Look for the nested-name-specifier. The simplest way to
12933 implement:
12934
12935 [temp.res]
12936
12937 The keyword `typename' is not permitted in a base-specifier or
12938 mem-initializer; in these contexts a qualified name that
12939 depends on a template-parameter is implicitly assumed to be a
12940 type name.
12941
12942 is to pretend that we have seen the `typename' keyword at this
21526606 12943 point. */
a723baf1
MM
12944 cp_parser_nested_name_specifier_opt (parser,
12945 /*typename_keyword_p=*/true,
12946 /*check_dependency_p=*/true,
a668c6ad
MM
12947 /*type_p=*/true,
12948 /*is_declaration=*/true);
a723baf1
MM
12949 /* If the base class is given by a qualified name, assume that names
12950 we see are type names or templates, as appropriate. */
12951 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 12952 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 12953
a723baf1 12954 /* Finally, look for the class-name. */
21526606 12955 type = cp_parser_class_name (parser,
a723baf1 12956 class_scope_p,
bbaab916 12957 template_p,
a723baf1 12958 /*type_p=*/true,
a723baf1 12959 /*check_dependency_p=*/true,
a668c6ad
MM
12960 /*class_head_p=*/false,
12961 /*is_declaration=*/true);
a723baf1
MM
12962
12963 if (type == error_mark_node)
12964 return error_mark_node;
12965
dbbf88d1 12966 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12967}
12968
12969/* Exception handling [gram.exception] */
12970
12971/* Parse an (optional) exception-specification.
12972
12973 exception-specification:
12974 throw ( type-id-list [opt] )
12975
12976 Returns a TREE_LIST representing the exception-specification. The
12977 TREE_VALUE of each node is a type. */
12978
12979static tree
94edc4ab 12980cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12981{
12982 cp_token *token;
12983 tree type_id_list;
12984
12985 /* Peek at the next token. */
12986 token = cp_lexer_peek_token (parser->lexer);
12987 /* If it's not `throw', then there's no exception-specification. */
12988 if (!cp_parser_is_keyword (token, RID_THROW))
12989 return NULL_TREE;
12990
12991 /* Consume the `throw'. */
12992 cp_lexer_consume_token (parser->lexer);
12993
12994 /* Look for the `('. */
12995 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12996
12997 /* Peek at the next token. */
12998 token = cp_lexer_peek_token (parser->lexer);
12999 /* If it's not a `)', then there is a type-id-list. */
13000 if (token->type != CPP_CLOSE_PAREN)
13001 {
13002 const char *saved_message;
13003
13004 /* Types may not be defined in an exception-specification. */
13005 saved_message = parser->type_definition_forbidden_message;
13006 parser->type_definition_forbidden_message
13007 = "types may not be defined in an exception-specification";
13008 /* Parse the type-id-list. */
13009 type_id_list = cp_parser_type_id_list (parser);
13010 /* Restore the saved message. */
13011 parser->type_definition_forbidden_message = saved_message;
13012 }
13013 else
13014 type_id_list = empty_except_spec;
13015
13016 /* Look for the `)'. */
13017 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13018
13019 return type_id_list;
13020}
13021
13022/* Parse an (optional) type-id-list.
13023
13024 type-id-list:
13025 type-id
13026 type-id-list , type-id
13027
13028 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13029 in the order that the types were presented. */
13030
13031static tree
94edc4ab 13032cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13033{
13034 tree types = NULL_TREE;
13035
13036 while (true)
13037 {
13038 cp_token *token;
13039 tree type;
13040
13041 /* Get the next type-id. */
13042 type = cp_parser_type_id (parser);
13043 /* Add it to the list. */
13044 types = add_exception_specifier (types, type, /*complain=*/1);
13045 /* Peek at the next token. */
13046 token = cp_lexer_peek_token (parser->lexer);
13047 /* If it is not a `,', we are done. */
13048 if (token->type != CPP_COMMA)
13049 break;
13050 /* Consume the `,'. */
13051 cp_lexer_consume_token (parser->lexer);
13052 }
13053
13054 return nreverse (types);
13055}
13056
13057/* Parse a try-block.
13058
13059 try-block:
13060 try compound-statement handler-seq */
13061
13062static tree
94edc4ab 13063cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13064{
13065 tree try_block;
13066
13067 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13068 try_block = begin_try_block ();
a5bcc582 13069 cp_parser_compound_statement (parser, false);
a723baf1
MM
13070 finish_try_block (try_block);
13071 cp_parser_handler_seq (parser);
13072 finish_handler_sequence (try_block);
13073
13074 return try_block;
13075}
13076
13077/* Parse a function-try-block.
13078
13079 function-try-block:
13080 try ctor-initializer [opt] function-body handler-seq */
13081
13082static bool
94edc4ab 13083cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13084{
13085 tree try_block;
13086 bool ctor_initializer_p;
13087
13088 /* Look for the `try' keyword. */
13089 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13090 return false;
13091 /* Let the rest of the front-end know where we are. */
13092 try_block = begin_function_try_block ();
13093 /* Parse the function-body. */
21526606 13094 ctor_initializer_p
a723baf1
MM
13095 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13096 /* We're done with the `try' part. */
13097 finish_function_try_block (try_block);
13098 /* Parse the handlers. */
13099 cp_parser_handler_seq (parser);
13100 /* We're done with the handlers. */
13101 finish_function_handler_sequence (try_block);
13102
13103 return ctor_initializer_p;
13104}
13105
13106/* Parse a handler-seq.
13107
13108 handler-seq:
13109 handler handler-seq [opt] */
13110
13111static void
94edc4ab 13112cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13113{
13114 while (true)
13115 {
13116 cp_token *token;
13117
13118 /* Parse the handler. */
13119 cp_parser_handler (parser);
13120 /* Peek at the next token. */
13121 token = cp_lexer_peek_token (parser->lexer);
13122 /* If it's not `catch' then there are no more handlers. */
13123 if (!cp_parser_is_keyword (token, RID_CATCH))
13124 break;
13125 }
13126}
13127
13128/* Parse a handler.
13129
13130 handler:
13131 catch ( exception-declaration ) compound-statement */
13132
13133static void
94edc4ab 13134cp_parser_handler (cp_parser* parser)
a723baf1
MM
13135{
13136 tree handler;
13137 tree declaration;
13138
13139 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13140 handler = begin_handler ();
13141 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13142 declaration = cp_parser_exception_declaration (parser);
13143 finish_handler_parms (declaration, handler);
13144 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 13145 cp_parser_compound_statement (parser, false);
a723baf1
MM
13146 finish_handler (handler);
13147}
13148
13149/* Parse an exception-declaration.
13150
13151 exception-declaration:
13152 type-specifier-seq declarator
13153 type-specifier-seq abstract-declarator
13154 type-specifier-seq
21526606 13155 ...
a723baf1
MM
13156
13157 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13158 ellipsis variant is used. */
13159
13160static tree
94edc4ab 13161cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
13162{
13163 tree type_specifiers;
13164 tree declarator;
13165 const char *saved_message;
13166
13167 /* If it's an ellipsis, it's easy to handle. */
13168 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13169 {
13170 /* Consume the `...' token. */
13171 cp_lexer_consume_token (parser->lexer);
13172 return NULL_TREE;
13173 }
13174
13175 /* Types may not be defined in exception-declarations. */
13176 saved_message = parser->type_definition_forbidden_message;
13177 parser->type_definition_forbidden_message
13178 = "types may not be defined in exception-declarations";
13179
13180 /* Parse the type-specifier-seq. */
13181 type_specifiers = cp_parser_type_specifier_seq (parser);
13182 /* If it's a `)', then there is no declarator. */
13183 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13184 declarator = NULL_TREE;
13185 else
62b8a44e 13186 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
13187 /*ctor_dtor_or_conv_p=*/NULL,
13188 /*parenthesized_p=*/NULL);
a723baf1
MM
13189
13190 /* Restore the saved message. */
13191 parser->type_definition_forbidden_message = saved_message;
13192
13193 return start_handler_parms (type_specifiers, declarator);
13194}
13195
21526606 13196/* Parse a throw-expression.
a723baf1
MM
13197
13198 throw-expression:
34cd5ae7 13199 throw assignment-expression [opt]
a723baf1
MM
13200
13201 Returns a THROW_EXPR representing the throw-expression. */
13202
13203static tree
94edc4ab 13204cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13205{
13206 tree expression;
89f1a6ec 13207 cp_token* token;
a723baf1
MM
13208
13209 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13210 token = cp_lexer_peek_token (parser->lexer);
13211 /* Figure out whether or not there is an assignment-expression
13212 following the "throw" keyword. */
13213 if (token->type == CPP_COMMA
13214 || token->type == CPP_SEMICOLON
13215 || token->type == CPP_CLOSE_PAREN
13216 || token->type == CPP_CLOSE_SQUARE
13217 || token->type == CPP_CLOSE_BRACE
13218 || token->type == CPP_COLON)
a723baf1 13219 expression = NULL_TREE;
89f1a6ec
MM
13220 else
13221 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13222
13223 return build_throw (expression);
13224}
13225
13226/* GNU Extensions */
13227
13228/* Parse an (optional) asm-specification.
13229
13230 asm-specification:
13231 asm ( string-literal )
13232
13233 If the asm-specification is present, returns a STRING_CST
13234 corresponding to the string-literal. Otherwise, returns
13235 NULL_TREE. */
13236
13237static tree
94edc4ab 13238cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13239{
13240 cp_token *token;
13241 tree asm_specification;
13242
13243 /* Peek at the next token. */
13244 token = cp_lexer_peek_token (parser->lexer);
21526606 13245 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13246 asm-specification. */
13247 if (!cp_parser_is_keyword (token, RID_ASM))
13248 return NULL_TREE;
13249
13250 /* Consume the `asm' token. */
13251 cp_lexer_consume_token (parser->lexer);
13252 /* Look for the `('. */
13253 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13254
13255 /* Look for the string-literal. */
13256 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13257 if (token)
13258 asm_specification = token->value;
13259 else
13260 asm_specification = NULL_TREE;
13261
13262 /* Look for the `)'. */
13263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13264
13265 return asm_specification;
13266}
13267
21526606 13268/* Parse an asm-operand-list.
a723baf1
MM
13269
13270 asm-operand-list:
13271 asm-operand
13272 asm-operand-list , asm-operand
21526606 13273
a723baf1 13274 asm-operand:
21526606 13275 string-literal ( expression )
a723baf1
MM
13276 [ string-literal ] string-literal ( expression )
13277
13278 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13279 each node is the expression. The TREE_PURPOSE is itself a
13280 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13281 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13282 is a STRING_CST for the string literal before the parenthesis. */
13283
13284static tree
94edc4ab 13285cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13286{
13287 tree asm_operands = NULL_TREE;
13288
13289 while (true)
13290 {
13291 tree string_literal;
13292 tree expression;
13293 tree name;
13294 cp_token *token;
21526606
EC
13295
13296 c_lex_string_translate = false;
13297
13298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13299 {
13300 /* Consume the `[' token. */
13301 cp_lexer_consume_token (parser->lexer);
13302 /* Read the operand name. */
13303 name = cp_parser_identifier (parser);
21526606 13304 if (name != error_mark_node)
a723baf1
MM
13305 name = build_string (IDENTIFIER_LENGTH (name),
13306 IDENTIFIER_POINTER (name));
13307 /* Look for the closing `]'. */
13308 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13309 }
13310 else
13311 name = NULL_TREE;
13312 /* Look for the string-literal. */
13313 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13314 string_literal = token ? token->value : error_mark_node;
21526606 13315 c_lex_string_translate = true;
a723baf1
MM
13316 /* Look for the `('. */
13317 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13318 /* Parse the expression. */
13319 expression = cp_parser_expression (parser);
13320 /* Look for the `)'. */
13321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
21526606 13322 c_lex_string_translate = false;
a723baf1
MM
13323 /* Add this operand to the list. */
13324 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13325 expression,
a723baf1 13326 asm_operands);
21526606 13327 /* If the next token is not a `,', there are no more
a723baf1
MM
13328 operands. */
13329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13330 break;
13331 /* Consume the `,'. */
13332 cp_lexer_consume_token (parser->lexer);
13333 }
13334
13335 return nreverse (asm_operands);
13336}
13337
21526606 13338/* Parse an asm-clobber-list.
a723baf1
MM
13339
13340 asm-clobber-list:
13341 string-literal
21526606 13342 asm-clobber-list , string-literal
a723baf1
MM
13343
13344 Returns a TREE_LIST, indicating the clobbers in the order that they
13345 appeared. The TREE_VALUE of each node is a STRING_CST. */
13346
13347static tree
94edc4ab 13348cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13349{
13350 tree clobbers = NULL_TREE;
13351
13352 while (true)
13353 {
13354 cp_token *token;
13355 tree string_literal;
13356
13357 /* Look for the string literal. */
13358 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13359 string_literal = token ? token->value : error_mark_node;
13360 /* Add it to the list. */
13361 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13362 /* If the next token is not a `,', then the list is
a723baf1
MM
13363 complete. */
13364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13365 break;
13366 /* Consume the `,' token. */
13367 cp_lexer_consume_token (parser->lexer);
13368 }
13369
13370 return clobbers;
13371}
13372
13373/* Parse an (optional) series of attributes.
13374
13375 attributes:
13376 attributes attribute
13377
13378 attribute:
21526606 13379 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13380
13381 The return value is as for cp_parser_attribute_list. */
21526606 13382
a723baf1 13383static tree
94edc4ab 13384cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13385{
13386 tree attributes = NULL_TREE;
13387
13388 while (true)
13389 {
13390 cp_token *token;
13391 tree attribute_list;
13392
13393 /* Peek at the next token. */
13394 token = cp_lexer_peek_token (parser->lexer);
13395 /* If it's not `__attribute__', then we're done. */
13396 if (token->keyword != RID_ATTRIBUTE)
13397 break;
13398
13399 /* Consume the `__attribute__' keyword. */
13400 cp_lexer_consume_token (parser->lexer);
13401 /* Look for the two `(' tokens. */
13402 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13403 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13404
13405 /* Peek at the next token. */
13406 token = cp_lexer_peek_token (parser->lexer);
13407 if (token->type != CPP_CLOSE_PAREN)
13408 /* Parse the attribute-list. */
13409 attribute_list = cp_parser_attribute_list (parser);
13410 else
13411 /* If the next token is a `)', then there is no attribute
13412 list. */
13413 attribute_list = NULL;
13414
13415 /* Look for the two `)' tokens. */
13416 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13417 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13418
13419 /* Add these new attributes to the list. */
13420 attributes = chainon (attributes, attribute_list);
13421 }
13422
13423 return attributes;
13424}
13425
21526606 13426/* Parse an attribute-list.
a723baf1 13427
21526606
EC
13428 attribute-list:
13429 attribute
a723baf1
MM
13430 attribute-list , attribute
13431
13432 attribute:
21526606 13433 identifier
a723baf1
MM
13434 identifier ( identifier )
13435 identifier ( identifier , expression-list )
21526606 13436 identifier ( expression-list )
a723baf1
MM
13437
13438 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13439 TREE_PURPOSE of each node is the identifier indicating which
13440 attribute is in use. The TREE_VALUE represents the arguments, if
13441 any. */
13442
13443static tree
94edc4ab 13444cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13445{
13446 tree attribute_list = NULL_TREE;
13447
21526606 13448 c_lex_string_translate = false;
a723baf1
MM
13449 while (true)
13450 {
13451 cp_token *token;
13452 tree identifier;
13453 tree attribute;
13454
13455 /* Look for the identifier. We also allow keywords here; for
13456 example `__attribute__ ((const))' is legal. */
13457 token = cp_lexer_peek_token (parser->lexer);
21526606 13458 if (token->type != CPP_NAME
a723baf1
MM
13459 && token->type != CPP_KEYWORD)
13460 return error_mark_node;
13461 /* Consume the token. */
13462 token = cp_lexer_consume_token (parser->lexer);
21526606 13463
a723baf1
MM
13464 /* Save away the identifier that indicates which attribute this is. */
13465 identifier = token->value;
13466 attribute = build_tree_list (identifier, NULL_TREE);
13467
13468 /* Peek at the next token. */
13469 token = cp_lexer_peek_token (parser->lexer);
13470 /* If it's an `(', then parse the attribute arguments. */
13471 if (token->type == CPP_OPEN_PAREN)
13472 {
13473 tree arguments;
a723baf1 13474
21526606 13475 arguments = (cp_parser_parenthesized_expression_list
39703eb9 13476 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13477 /* Save the identifier and arguments away. */
13478 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13479 }
13480
13481 /* Add this attribute to the list. */
13482 TREE_CHAIN (attribute) = attribute_list;
13483 attribute_list = attribute;
13484
13485 /* Now, look for more attributes. */
13486 token = cp_lexer_peek_token (parser->lexer);
13487 /* If the next token isn't a `,', we're done. */
13488 if (token->type != CPP_COMMA)
13489 break;
13490
cd0be382 13491 /* Consume the comma and keep going. */
a723baf1
MM
13492 cp_lexer_consume_token (parser->lexer);
13493 }
21526606 13494 c_lex_string_translate = true;
a723baf1
MM
13495
13496 /* We built up the list in reverse order. */
13497 return nreverse (attribute_list);
13498}
13499
13500/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13501 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13502 current value of the PEDANTIC flag, regardless of whether or not
13503 the `__extension__' keyword is present. The caller is responsible
13504 for restoring the value of the PEDANTIC flag. */
13505
13506static bool
94edc4ab 13507cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13508{
13509 /* Save the old value of the PEDANTIC flag. */
13510 *saved_pedantic = pedantic;
13511
13512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13513 {
13514 /* Consume the `__extension__' token. */
13515 cp_lexer_consume_token (parser->lexer);
13516 /* We're not being pedantic while the `__extension__' keyword is
13517 in effect. */
13518 pedantic = 0;
13519
13520 return true;
13521 }
13522
13523 return false;
13524}
13525
13526/* Parse a label declaration.
13527
13528 label-declaration:
13529 __label__ label-declarator-seq ;
13530
13531 label-declarator-seq:
13532 identifier , label-declarator-seq
13533 identifier */
13534
13535static void
94edc4ab 13536cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13537{
13538 /* Look for the `__label__' keyword. */
13539 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13540
13541 while (true)
13542 {
13543 tree identifier;
13544
13545 /* Look for an identifier. */
13546 identifier = cp_parser_identifier (parser);
13547 /* Declare it as a lobel. */
13548 finish_label_decl (identifier);
13549 /* If the next token is a `;', stop. */
13550 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13551 break;
13552 /* Look for the `,' separating the label declarations. */
13553 cp_parser_require (parser, CPP_COMMA, "`,'");
13554 }
13555
13556 /* Look for the final `;'. */
13557 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13558}
13559
13560/* Support Functions */
13561
13562/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13563 NAME should have one of the representations used for an
13564 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13565 is returned. If PARSER->SCOPE is a dependent type, then a
13566 SCOPE_REF is returned.
13567
13568 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13569 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13570 was formed. Abstractly, such entities should not be passed to this
13571 function, because they do not need to be looked up, but it is
13572 simpler to check for this special case here, rather than at the
13573 call-sites.
13574
13575 In cases not explicitly covered above, this function returns a
13576 DECL, OVERLOAD, or baselink representing the result of the lookup.
13577 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13578 is returned.
13579
a723baf1
MM
13580 If IS_TYPE is TRUE, bindings that do not refer to types are
13581 ignored.
13582
b0bc6e8e
KL
13583 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13584 ignored.
13585
eea9800f
MM
13586 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13587 are ignored.
13588
a723baf1
MM
13589 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13590 types. */
13591
13592static tree
21526606 13593cp_parser_lookup_name (cp_parser *parser, tree name,
b0bc6e8e
KL
13594 bool is_type, bool is_template, bool is_namespace,
13595 bool check_dependency)
a723baf1
MM
13596{
13597 tree decl;
13598 tree object_type = parser->context->object_type;
13599
13600 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13601 no longer valid. Note that if we are parsing tentatively, and
13602 the parse fails, OBJECT_TYPE will be automatically restored. */
13603 parser->context->object_type = NULL_TREE;
13604
13605 if (name == error_mark_node)
13606 return error_mark_node;
13607
13608 /* A template-id has already been resolved; there is no lookup to
13609 do. */
13610 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13611 return name;
13612 if (BASELINK_P (name))
13613 {
13614 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13615 == TEMPLATE_ID_EXPR),
13616 20020909);
13617 return name;
13618 }
13619
13620 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13621 it should already have been checked to make sure that the name
13622 used matches the type being destroyed. */
13623 if (TREE_CODE (name) == BIT_NOT_EXPR)
13624 {
13625 tree type;
13626
13627 /* Figure out to which type this destructor applies. */
13628 if (parser->scope)
13629 type = parser->scope;
13630 else if (object_type)
13631 type = object_type;
13632 else
13633 type = current_class_type;
13634 /* If that's not a class type, there is no destructor. */
13635 if (!type || !CLASS_TYPE_P (type))
13636 return error_mark_node;
fd6e3cce
GB
13637 if (!CLASSTYPE_DESTRUCTORS (type))
13638 return error_mark_node;
a723baf1
MM
13639 /* If it was a class type, return the destructor. */
13640 return CLASSTYPE_DESTRUCTORS (type);
13641 }
13642
13643 /* By this point, the NAME should be an ordinary identifier. If
13644 the id-expression was a qualified name, the qualifying scope is
13645 stored in PARSER->SCOPE at this point. */
13646 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13647 20000619);
21526606 13648
a723baf1
MM
13649 /* Perform the lookup. */
13650 if (parser->scope)
21526606 13651 {
1fb3244a 13652 bool dependent_p;
a723baf1
MM
13653
13654 if (parser->scope == error_mark_node)
13655 return error_mark_node;
13656
13657 /* If the SCOPE is dependent, the lookup must be deferred until
13658 the template is instantiated -- unless we are explicitly
13659 looking up names in uninstantiated templates. Even then, we
13660 cannot look up the name if the scope is not a class type; it
13661 might, for example, be a template type parameter. */
1fb3244a
MM
13662 dependent_p = (TYPE_P (parser->scope)
13663 && !(parser->in_declarator_p
13664 && currently_open_class (parser->scope))
13665 && dependent_type_p (parser->scope));
a723baf1 13666 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13667 && dependent_p)
a723baf1 13668 {
b0bc6e8e 13669 if (is_type)
a723baf1
MM
13670 /* The resolution to Core Issue 180 says that `struct A::B'
13671 should be considered a type-name, even if `A' is
13672 dependent. */
13673 decl = TYPE_NAME (make_typename_type (parser->scope,
13674 name,
13675 /*complain=*/1));
b0bc6e8e 13676 else if (is_template)
5b4acce1
KL
13677 decl = make_unbound_class_template (parser->scope,
13678 name,
13679 /*complain=*/1);
b0bc6e8e
KL
13680 else
13681 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
13682 }
13683 else
13684 {
91b004e5
MM
13685 bool pop_p = false;
13686
a723baf1
MM
13687 /* If PARSER->SCOPE is a dependent type, then it must be a
13688 class type, and we must not be checking dependencies;
13689 otherwise, we would have processed this lookup above. So
13690 that PARSER->SCOPE is not considered a dependent base by
13691 lookup_member, we must enter the scope here. */
1fb3244a 13692 if (dependent_p)
91b004e5 13693 pop_p = push_scope (parser->scope);
a723baf1
MM
13694 /* If the PARSER->SCOPE is a a template specialization, it
13695 may be instantiated during name lookup. In that case,
13696 errors may be issued. Even if we rollback the current
13697 tentative parse, those errors are valid. */
5e08432e
MM
13698 decl = lookup_qualified_name (parser->scope, name, is_type,
13699 /*complain=*/true);
91b004e5 13700 if (pop_p)
a723baf1
MM
13701 pop_scope (parser->scope);
13702 }
13703 parser->qualifying_scope = parser->scope;
13704 parser->object_scope = NULL_TREE;
13705 }
13706 else if (object_type)
13707 {
13708 tree object_decl = NULL_TREE;
13709 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13710 OBJECT_TYPE is not a class. */
13711 if (CLASS_TYPE_P (object_type))
13712 /* If the OBJECT_TYPE is a template specialization, it may
13713 be instantiated during name lookup. In that case, errors
13714 may be issued. Even if we rollback the current tentative
13715 parse, those errors are valid. */
13716 object_decl = lookup_member (object_type,
13717 name,
13718 /*protect=*/0, is_type);
13719 /* Look it up in the enclosing context, too. */
21526606 13720 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13721 is_namespace,
a723baf1
MM
13722 /*flags=*/0);
13723 parser->object_scope = object_type;
13724 parser->qualifying_scope = NULL_TREE;
13725 if (object_decl)
13726 decl = object_decl;
13727 }
13728 else
13729 {
21526606 13730 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13731 is_namespace,
a723baf1
MM
13732 /*flags=*/0);
13733 parser->qualifying_scope = NULL_TREE;
13734 parser->object_scope = NULL_TREE;
13735 }
13736
13737 /* If the lookup failed, let our caller know. */
21526606 13738 if (!decl
a723baf1 13739 || decl == error_mark_node
21526606 13740 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
13741 && DECL_ANTICIPATED (decl)))
13742 return error_mark_node;
13743
13744 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13745 if (TREE_CODE (decl) == TREE_LIST)
13746 {
13747 /* The error message we have to print is too complicated for
13748 cp_parser_error, so we incorporate its actions directly. */
e5976695 13749 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13750 {
13751 error ("reference to `%D' is ambiguous", name);
13752 print_candidates (decl);
13753 }
13754 return error_mark_node;
13755 }
13756
21526606 13757 my_friendly_assert (DECL_P (decl)
a723baf1
MM
13758 || TREE_CODE (decl) == OVERLOAD
13759 || TREE_CODE (decl) == SCOPE_REF
5b4acce1 13760 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
a723baf1
MM
13761 || BASELINK_P (decl),
13762 20000619);
13763
13764 /* If we have resolved the name of a member declaration, check to
13765 see if the declaration is accessible. When the name resolves to
34cd5ae7 13766 set of overloaded functions, accessibility is checked when
21526606 13767 overload resolution is done.
a723baf1
MM
13768
13769 During an explicit instantiation, access is not checked at all,
13770 as per [temp.explicit]. */
8d241e0b 13771 if (DECL_P (decl))
ee76b931 13772 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13773
13774 return decl;
13775}
13776
13777/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
13778 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13779 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
13780
13781static tree
94edc4ab 13782cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 13783{
21526606 13784 return cp_parser_lookup_name (parser, name,
eea9800f 13785 /*is_type=*/false,
b0bc6e8e 13786 /*is_template=*/false,
eea9800f 13787 /*is_namespace=*/false,
a723baf1
MM
13788 /*check_dependency=*/true);
13789}
13790
a723baf1
MM
13791/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13792 the current context, return the TYPE_DECL. If TAG_NAME_P is
13793 true, the DECL indicates the class being defined in a class-head,
13794 or declared in an elaborated-type-specifier.
13795
13796 Otherwise, return DECL. */
13797
13798static tree
13799cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13800{
710b73e6
KL
13801 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13802 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 13803
21526606 13804 struct A {
a723baf1
MM
13805 template <typename T> struct B;
13806 };
13807
21526606
EC
13808 template <typename T> struct A::B {};
13809
a723baf1
MM
13810 Similarly, in a elaborated-type-specifier:
13811
13812 namespace N { struct X{}; }
13813
13814 struct A {
13815 template <typename T> friend struct N::X;
13816 };
13817
710b73e6
KL
13818 However, if the DECL refers to a class type, and we are in
13819 the scope of the class, then the name lookup automatically
13820 finds the TYPE_DECL created by build_self_reference rather
13821 than a TEMPLATE_DECL. For example, in:
13822
13823 template <class T> struct S {
13824 S s;
13825 };
13826
13827 there is no need to handle such case. */
13828
13829 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13830 return DECL_TEMPLATE_RESULT (decl);
13831
13832 return decl;
13833}
13834
13835/* If too many, or too few, template-parameter lists apply to the
13836 declarator, issue an error message. Returns TRUE if all went well,
13837 and FALSE otherwise. */
13838
13839static bool
21526606 13840cp_parser_check_declarator_template_parameters (cp_parser* parser,
94edc4ab 13841 tree declarator)
a723baf1
MM
13842{
13843 unsigned num_templates;
13844
13845 /* We haven't seen any classes that involve template parameters yet. */
13846 num_templates = 0;
13847
13848 switch (TREE_CODE (declarator))
13849 {
13850 case CALL_EXPR:
13851 case ARRAY_REF:
13852 case INDIRECT_REF:
13853 case ADDR_EXPR:
13854 {
13855 tree main_declarator = TREE_OPERAND (declarator, 0);
13856 return
21526606 13857 cp_parser_check_declarator_template_parameters (parser,
a723baf1
MM
13858 main_declarator);
13859 }
13860
13861 case SCOPE_REF:
13862 {
13863 tree scope;
13864 tree member;
13865
13866 scope = TREE_OPERAND (declarator, 0);
13867 member = TREE_OPERAND (declarator, 1);
13868
13869 /* If this is a pointer-to-member, then we are not interested
13870 in the SCOPE, because it does not qualify the thing that is
13871 being declared. */
13872 if (TREE_CODE (member) == INDIRECT_REF)
13873 return (cp_parser_check_declarator_template_parameters
13874 (parser, member));
13875
13876 while (scope && CLASS_TYPE_P (scope))
13877 {
13878 /* You're supposed to have one `template <...>'
13879 for every template class, but you don't need one
13880 for a full specialization. For example:
21526606 13881
a723baf1
MM
13882 template <class T> struct S{};
13883 template <> struct S<int> { void f(); };
13884 void S<int>::f () {}
21526606 13885
a723baf1
MM
13886 is correct; there shouldn't be a `template <>' for
13887 the definition of `S<int>::f'. */
13888 if (CLASSTYPE_TEMPLATE_INFO (scope)
13889 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13890 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13891 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13892 ++num_templates;
13893
13894 scope = TYPE_CONTEXT (scope);
13895 }
13896 }
13897
13898 /* Fall through. */
13899
13900 default:
13901 /* If the DECLARATOR has the form `X<y>' then it uses one
13902 additional level of template parameters. */
13903 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13904 ++num_templates;
13905
21526606 13906 return cp_parser_check_template_parameters (parser,
a723baf1
MM
13907 num_templates);
13908 }
13909}
13910
13911/* NUM_TEMPLATES were used in the current declaration. If that is
13912 invalid, return FALSE and issue an error messages. Otherwise,
13913 return TRUE. */
13914
13915static bool
94edc4ab
NN
13916cp_parser_check_template_parameters (cp_parser* parser,
13917 unsigned num_templates)
a723baf1
MM
13918{
13919 /* If there are more template classes than parameter lists, we have
13920 something like:
21526606 13921
a723baf1
MM
13922 template <class T> void S<T>::R<T>::f (); */
13923 if (parser->num_template_parameter_lists < num_templates)
13924 {
13925 error ("too few template-parameter-lists");
13926 return false;
13927 }
13928 /* If there are the same number of template classes and parameter
13929 lists, that's OK. */
13930 if (parser->num_template_parameter_lists == num_templates)
13931 return true;
13932 /* If there are more, but only one more, then we are referring to a
13933 member template. That's OK too. */
13934 if (parser->num_template_parameter_lists == num_templates + 1)
13935 return true;
13936 /* Otherwise, there are too many template parameter lists. We have
13937 something like:
13938
13939 template <class T> template <class U> void S::f(); */
13940 error ("too many template-parameter-lists");
13941 return false;
13942}
13943
13944/* Parse a binary-expression of the general form:
13945
13946 binary-expression:
13947 <expr>
13948 binary-expression <token> <expr>
13949
13950 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13951 to parser the <expr>s. If the first production is used, then the
13952 value returned by FN is returned directly. Otherwise, a node with
13953 the indicated EXPR_TYPE is returned, with operands corresponding to
13954 the two sub-expressions. */
13955
13956static tree
21526606
EC
13957cp_parser_binary_expression (cp_parser* parser,
13958 const cp_parser_token_tree_map token_tree_map,
94edc4ab 13959 cp_parser_expression_fn fn)
a723baf1
MM
13960{
13961 tree lhs;
13962
13963 /* Parse the first expression. */
13964 lhs = (*fn) (parser);
13965 /* Now, look for more expressions. */
13966 while (true)
13967 {
13968 cp_token *token;
39b1af70 13969 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13970 tree rhs;
13971
13972 /* Peek at the next token. */
13973 token = cp_lexer_peek_token (parser->lexer);
13974 /* If the token is `>', and that's not an operator at the
13975 moment, then we're done. */
13976 if (token->type == CPP_GREATER
13977 && !parser->greater_than_is_operator_p)
13978 break;
34cd5ae7 13979 /* If we find one of the tokens we want, build the corresponding
a723baf1 13980 tree representation. */
21526606 13981 for (map_node = token_tree_map;
a723baf1
MM
13982 map_node->token_type != CPP_EOF;
13983 ++map_node)
13984 if (map_node->token_type == token->type)
13985 {
ec835fb2
MM
13986 /* Assume that an overloaded operator will not be used. */
13987 bool overloaded_p = false;
13988
a723baf1
MM
13989 /* Consume the operator token. */
13990 cp_lexer_consume_token (parser->lexer);
13991 /* Parse the right-hand side of the expression. */
13992 rhs = (*fn) (parser);
13993 /* Build the binary tree node. */
ec835fb2
MM
13994 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
13995 &overloaded_p);
13996 /* If the binary operator required the use of an
13997 overloaded operator, then this expression cannot be an
13998 integral constant-expression. An overloaded operator
13999 can be used even if both operands are otherwise
14000 permissible in an integral constant-expression if at
14001 least one of the operands is of enumeration type. */
14002 if (overloaded_p
14003 && (cp_parser_non_integral_constant_expression
14004 (parser, "calls to overloaded operators")))
14005 lhs = error_mark_node;
a723baf1
MM
14006 break;
14007 }
14008
14009 /* If the token wasn't one of the ones we want, we're done. */
14010 if (map_node->token_type == CPP_EOF)
14011 break;
14012 }
14013
14014 return lhs;
14015}
14016
14017/* Parse an optional `::' token indicating that the following name is
14018 from the global namespace. If so, PARSER->SCOPE is set to the
14019 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14020 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14021 Returns the new value of PARSER->SCOPE, if the `::' token is
14022 present, and NULL_TREE otherwise. */
14023
14024static tree
94edc4ab 14025cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14026{
14027 cp_token *token;
14028
14029 /* Peek at the next token. */
14030 token = cp_lexer_peek_token (parser->lexer);
14031 /* If we're looking at a `::' token then we're starting from the
14032 global namespace, not our current location. */
14033 if (token->type == CPP_SCOPE)
14034 {
14035 /* Consume the `::' token. */
14036 cp_lexer_consume_token (parser->lexer);
14037 /* Set the SCOPE so that we know where to start the lookup. */
14038 parser->scope = global_namespace;
14039 parser->qualifying_scope = global_namespace;
14040 parser->object_scope = NULL_TREE;
14041
14042 return parser->scope;
14043 }
14044 else if (!current_scope_valid_p)
14045 {
14046 parser->scope = NULL_TREE;
14047 parser->qualifying_scope = NULL_TREE;
14048 parser->object_scope = NULL_TREE;
14049 }
14050
14051 return NULL_TREE;
14052}
14053
14054/* Returns TRUE if the upcoming token sequence is the start of a
14055 constructor declarator. If FRIEND_P is true, the declarator is
14056 preceded by the `friend' specifier. */
14057
14058static bool
14059cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14060{
14061 bool constructor_p;
14062 tree type_decl = NULL_TREE;
14063 bool nested_name_p;
2050a1bb
MM
14064 cp_token *next_token;
14065
14066 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14067 try to avoid doing lots of work if at all possible. It's not
14068 valid declare a constructor at function scope. */
14069 if (at_function_scope_p ())
14070 return false;
14071 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14072 next_token = cp_lexer_peek_token (parser->lexer);
14073 if (next_token->type != CPP_NAME
14074 && next_token->type != CPP_SCOPE
14075 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14076 && next_token->type != CPP_TEMPLATE_ID)
14077 return false;
a723baf1
MM
14078
14079 /* Parse tentatively; we are going to roll back all of the tokens
14080 consumed here. */
14081 cp_parser_parse_tentatively (parser);
14082 /* Assume that we are looking at a constructor declarator. */
14083 constructor_p = true;
8d241e0b 14084
a723baf1
MM
14085 /* Look for the optional `::' operator. */
14086 cp_parser_global_scope_opt (parser,
14087 /*current_scope_valid_p=*/false);
14088 /* Look for the nested-name-specifier. */
21526606 14089 nested_name_p
a723baf1
MM
14090 = (cp_parser_nested_name_specifier_opt (parser,
14091 /*typename_keyword_p=*/false,
14092 /*check_dependency_p=*/false,
a668c6ad
MM
14093 /*type_p=*/false,
14094 /*is_declaration=*/false)
a723baf1
MM
14095 != NULL_TREE);
14096 /* Outside of a class-specifier, there must be a
14097 nested-name-specifier. */
21526606 14098 if (!nested_name_p &&
a723baf1
MM
14099 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14100 || friend_p))
14101 constructor_p = false;
14102 /* If we still think that this might be a constructor-declarator,
14103 look for a class-name. */
14104 if (constructor_p)
14105 {
14106 /* If we have:
14107
8fbc5ae7 14108 template <typename T> struct S { S(); };
a723baf1
MM
14109 template <typename T> S<T>::S ();
14110
14111 we must recognize that the nested `S' names a class.
14112 Similarly, for:
14113
14114 template <typename T> S<T>::S<T> ();
14115
14116 we must recognize that the nested `S' names a template. */
14117 type_decl = cp_parser_class_name (parser,
14118 /*typename_keyword_p=*/false,
14119 /*template_keyword_p=*/false,
14120 /*type_p=*/false,
a723baf1 14121 /*check_dependency_p=*/false,
a668c6ad
MM
14122 /*class_head_p=*/false,
14123 /*is_declaration=*/false);
a723baf1
MM
14124 /* If there was no class-name, then this is not a constructor. */
14125 constructor_p = !cp_parser_error_occurred (parser);
14126 }
8d241e0b 14127
a723baf1
MM
14128 /* If we're still considering a constructor, we have to see a `(',
14129 to begin the parameter-declaration-clause, followed by either a
14130 `)', an `...', or a decl-specifier. We need to check for a
14131 type-specifier to avoid being fooled into thinking that:
14132
14133 S::S (f) (int);
14134
14135 is a constructor. (It is actually a function named `f' that
14136 takes one parameter (of type `int') and returns a value of type
14137 `S::S'. */
21526606 14138 if (constructor_p
a723baf1
MM
14139 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14140 {
14141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14142 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14143 && !cp_parser_storage_class_specifier_opt (parser))
14144 {
5dae1114 14145 tree type;
91b004e5 14146 bool pop_p = false;
4047b164 14147 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14148
14149 /* Names appearing in the type-specifier should be looked up
14150 in the scope of the class. */
14151 if (current_class_type)
14152 type = NULL_TREE;
a723baf1
MM
14153 else
14154 {
5dae1114
MM
14155 type = TREE_TYPE (type_decl);
14156 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14157 {
21526606 14158 type = resolve_typename_type (type,
14d22dd6
MM
14159 /*only_current_p=*/false);
14160 if (type == error_mark_node)
14161 {
14162 cp_parser_abort_tentative_parse (parser);
14163 return false;
14164 }
14165 }
91b004e5 14166 pop_p = push_scope (type);
a723baf1 14167 }
4047b164
KL
14168
14169 /* Inside the constructor parameter list, surrounding
14170 template-parameter-lists do not apply. */
14171 saved_num_template_parameter_lists
14172 = parser->num_template_parameter_lists;
14173 parser->num_template_parameter_lists = 0;
14174
5dae1114
MM
14175 /* Look for the type-specifier. */
14176 cp_parser_type_specifier (parser,
14177 CP_PARSER_FLAGS_NONE,
14178 /*is_friend=*/false,
14179 /*is_declarator=*/true,
14180 /*declares_class_or_enum=*/NULL,
14181 /*is_cv_qualifier=*/NULL);
4047b164
KL
14182
14183 parser->num_template_parameter_lists
14184 = saved_num_template_parameter_lists;
14185
5dae1114 14186 /* Leave the scope of the class. */
91b004e5 14187 if (pop_p)
5dae1114
MM
14188 pop_scope (type);
14189
14190 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14191 }
14192 }
14193 else
14194 constructor_p = false;
14195 /* We did not really want to consume any tokens. */
14196 cp_parser_abort_tentative_parse (parser);
14197
14198 return constructor_p;
14199}
14200
14201/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14202 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14203 they must be performed once we are in the scope of the function.
14204
14205 Returns the function defined. */
14206
14207static tree
14208cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
14209 (cp_parser* parser,
14210 tree decl_specifiers,
14211 tree attributes,
14212 tree declarator)
a723baf1
MM
14213{
14214 tree fn;
14215 bool success_p;
14216
14217 /* Begin the function-definition. */
21526606
EC
14218 success_p = begin_function_definition (decl_specifiers,
14219 attributes,
a723baf1
MM
14220 declarator);
14221
14222 /* If there were names looked up in the decl-specifier-seq that we
14223 did not check, check them now. We must wait until we are in the
14224 scope of the function to perform the checks, since the function
14225 might be a friend. */
cf22909c 14226 perform_deferred_access_checks ();
a723baf1
MM
14227
14228 if (!success_p)
14229 {
14230 /* If begin_function_definition didn't like the definition, skip
14231 the entire function. */
14232 error ("invalid function declaration");
14233 cp_parser_skip_to_end_of_block_or_statement (parser);
14234 fn = error_mark_node;
14235 }
14236 else
14237 fn = cp_parser_function_definition_after_declarator (parser,
14238 /*inline_p=*/false);
14239
14240 return fn;
14241}
14242
14243/* Parse the part of a function-definition that follows the
14244 declarator. INLINE_P is TRUE iff this function is an inline
14245 function defined with a class-specifier.
14246
14247 Returns the function defined. */
14248
21526606
EC
14249static tree
14250cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14251 bool inline_p)
a723baf1
MM
14252{
14253 tree fn;
14254 bool ctor_initializer_p = false;
14255 bool saved_in_unbraced_linkage_specification_p;
14256 unsigned saved_num_template_parameter_lists;
14257
14258 /* If the next token is `return', then the code may be trying to
14259 make use of the "named return value" extension that G++ used to
14260 support. */
14261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14262 {
14263 /* Consume the `return' keyword. */
14264 cp_lexer_consume_token (parser->lexer);
14265 /* Look for the identifier that indicates what value is to be
14266 returned. */
14267 cp_parser_identifier (parser);
14268 /* Issue an error message. */
14269 error ("named return values are no longer supported");
14270 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14271 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14272 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14273 cp_lexer_consume_token (parser->lexer);
14274 }
14275 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14276 anything declared inside `f'. */
21526606 14277 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14278 = parser->in_unbraced_linkage_specification_p;
14279 parser->in_unbraced_linkage_specification_p = false;
14280 /* Inside the function, surrounding template-parameter-lists do not
14281 apply. */
21526606
EC
14282 saved_num_template_parameter_lists
14283 = parser->num_template_parameter_lists;
a723baf1
MM
14284 parser->num_template_parameter_lists = 0;
14285 /* If the next token is `try', then we are looking at a
14286 function-try-block. */
14287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14288 ctor_initializer_p = cp_parser_function_try_block (parser);
14289 /* A function-try-block includes the function-body, so we only do
14290 this next part if we're not processing a function-try-block. */
14291 else
21526606 14292 ctor_initializer_p
a723baf1
MM
14293 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14294
14295 /* Finish the function. */
21526606 14296 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14297 (inline_p ? 2 : 0));
14298 /* Generate code for it, if necessary. */
8cd2462c 14299 expand_or_defer_fn (fn);
a723baf1 14300 /* Restore the saved values. */
21526606 14301 parser->in_unbraced_linkage_specification_p
a723baf1 14302 = saved_in_unbraced_linkage_specification_p;
21526606 14303 parser->num_template_parameter_lists
a723baf1
MM
14304 = saved_num_template_parameter_lists;
14305
14306 return fn;
14307}
14308
14309/* Parse a template-declaration, assuming that the `export' (and
14310 `extern') keywords, if present, has already been scanned. MEMBER_P
14311 is as for cp_parser_template_declaration. */
14312
14313static void
94edc4ab 14314cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14315{
14316 tree decl = NULL_TREE;
14317 tree parameter_list;
14318 bool friend_p = false;
14319
14320 /* Look for the `template' keyword. */
14321 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14322 return;
21526606 14323
a723baf1
MM
14324 /* And the `<'. */
14325 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14326 return;
21526606 14327
a723baf1
MM
14328 /* If the next token is `>', then we have an invalid
14329 specialization. Rather than complain about an invalid template
14330 parameter, issue an error message here. */
14331 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14332 {
14333 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14334 begin_specialization ();
a723baf1
MM
14335 parameter_list = NULL_TREE;
14336 }
14337 else
2f9afd51
KL
14338 {
14339 /* Parse the template parameters. */
14340 begin_template_parm_list ();
14341 parameter_list = cp_parser_template_parameter_list (parser);
14342 parameter_list = end_template_parm_list (parameter_list);
14343 }
14344
a723baf1
MM
14345 /* Look for the `>'. */
14346 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14347 /* We just processed one more parameter list. */
14348 ++parser->num_template_parameter_lists;
14349 /* If the next token is `template', there are more template
14350 parameters. */
21526606 14351 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14352 RID_TEMPLATE))
14353 cp_parser_template_declaration_after_export (parser, member_p);
14354 else
14355 {
14356 decl = cp_parser_single_declaration (parser,
14357 member_p,
14358 &friend_p);
14359
14360 /* If this is a member template declaration, let the front
14361 end know. */
14362 if (member_p && !friend_p && decl)
37d407a1
KL
14363 {
14364 if (TREE_CODE (decl) == TYPE_DECL)
14365 cp_parser_check_access_in_redeclaration (decl);
14366
14367 decl = finish_member_template_decl (decl);
14368 }
a723baf1 14369 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14370 make_friend_class (current_class_type, TREE_TYPE (decl),
14371 /*complain=*/true);
a723baf1
MM
14372 }
14373 /* We are done with the current parameter list. */
14374 --parser->num_template_parameter_lists;
14375
14376 /* Finish up. */
14377 finish_template_decl (parameter_list);
14378
14379 /* Register member declarations. */
14380 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14381 finish_member_declaration (decl);
14382
14383 /* If DECL is a function template, we must return to parse it later.
14384 (Even though there is no definition, there might be default
14385 arguments that need handling.) */
21526606 14386 if (member_p && decl
a723baf1
MM
14387 && (TREE_CODE (decl) == FUNCTION_DECL
14388 || DECL_FUNCTION_TEMPLATE_P (decl)))
14389 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14390 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14391 TREE_VALUE (parser->unparsed_functions_queues));
14392}
14393
14394/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14395 `function-definition' sequence. MEMBER_P is true, this declaration
14396 appears in a class scope.
14397
14398 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14399 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14400
14401static tree
21526606 14402cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14403 bool member_p,
14404 bool* friend_p)
a723baf1 14405{
560ad596 14406 int declares_class_or_enum;
a723baf1
MM
14407 tree decl = NULL_TREE;
14408 tree decl_specifiers;
14409 tree attributes;
4bb8ca28 14410 bool function_definition_p = false;
a723baf1 14411
a723baf1 14412 /* Defer access checks until we know what is being declared. */
8d241e0b 14413 push_deferring_access_checks (dk_deferred);
cf22909c 14414
a723baf1
MM
14415 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14416 alternative. */
21526606 14417 decl_specifiers
a723baf1
MM
14418 = cp_parser_decl_specifier_seq (parser,
14419 CP_PARSER_FLAGS_OPTIONAL,
14420 &attributes,
14421 &declares_class_or_enum);
4bb8ca28
MM
14422 if (friend_p)
14423 *friend_p = cp_parser_friend_p (decl_specifiers);
a723baf1
MM
14424 /* Gather up the access checks that occurred the
14425 decl-specifier-seq. */
cf22909c
KL
14426 stop_deferring_access_checks ();
14427
a723baf1
MM
14428 /* Check for the declaration of a template class. */
14429 if (declares_class_or_enum)
14430 {
14431 if (cp_parser_declares_only_class_p (parser))
14432 {
14433 decl = shadow_tag (decl_specifiers);
14434 if (decl)
14435 decl = TYPE_NAME (decl);
14436 else
14437 decl = error_mark_node;
14438 }
14439 }
14440 else
14441 decl = NULL_TREE;
14442 /* If it's not a template class, try for a template function. If
14443 the next token is a `;', then this declaration does not declare
14444 anything. But, if there were errors in the decl-specifiers, then
14445 the error might well have come from an attempted class-specifier.
14446 In that case, there's no need to warn about a missing declarator. */
14447 if (!decl
14448 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14449 || !value_member (error_mark_node, decl_specifiers)))
21526606 14450 decl = cp_parser_init_declarator (parser,
a723baf1
MM
14451 decl_specifiers,
14452 attributes,
4bb8ca28 14453 /*function_definition_allowed_p=*/true,
a723baf1 14454 member_p,
560ad596 14455 declares_class_or_enum,
4bb8ca28 14456 &function_definition_p);
cf22909c
KL
14457
14458 pop_deferring_access_checks ();
14459
a723baf1
MM
14460 /* Clear any current qualification; whatever comes next is the start
14461 of something new. */
14462 parser->scope = NULL_TREE;
14463 parser->qualifying_scope = NULL_TREE;
14464 parser->object_scope = NULL_TREE;
14465 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14466 if (!function_definition_p
14467 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14468 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14469
14470 return decl;
14471}
14472
d6b4ea85
MM
14473/* Parse a cast-expression that is not the operand of a unary "&". */
14474
14475static tree
14476cp_parser_simple_cast_expression (cp_parser *parser)
14477{
14478 return cp_parser_cast_expression (parser, /*address_p=*/false);
14479}
14480
a723baf1
MM
14481/* Parse a functional cast to TYPE. Returns an expression
14482 representing the cast. */
14483
14484static tree
94edc4ab 14485cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14486{
14487 tree expression_list;
14488
21526606 14489 expression_list
39703eb9
MM
14490 = cp_parser_parenthesized_expression_list (parser, false,
14491 /*non_constant_p=*/NULL);
a723baf1
MM
14492
14493 return build_functional_cast (type, expression_list);
14494}
14495
4bb8ca28
MM
14496/* Save the tokens that make up the body of a member function defined
14497 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14498 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14499 specifiers applied to the declaration. Returns the FUNCTION_DECL
14500 for the member function. */
14501
7ce27103 14502static tree
4bb8ca28
MM
14503cp_parser_save_member_function_body (cp_parser* parser,
14504 tree decl_specifiers,
14505 tree declarator,
14506 tree attributes)
14507{
14508 cp_token_cache *cache;
14509 tree fn;
14510
14511 /* Create the function-declaration. */
14512 fn = start_method (decl_specifiers, declarator, attributes);
14513 /* If something went badly wrong, bail out now. */
14514 if (fn == error_mark_node)
14515 {
14516 /* If there's a function-body, skip it. */
21526606 14517 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
14518 (cp_lexer_peek_token (parser->lexer)))
14519 cp_parser_skip_to_end_of_block_or_statement (parser);
14520 return error_mark_node;
14521 }
14522
14523 /* Remember it, if there default args to post process. */
14524 cp_parser_save_default_args (parser, fn);
14525
14526 /* Create a token cache. */
14527 cache = cp_token_cache_new ();
21526606 14528 /* Save away the tokens that make up the body of the
4bb8ca28
MM
14529 function. */
14530 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14531 /* Handle function try blocks. */
14532 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14533 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14534
14535 /* Save away the inline definition; we will process it when the
14536 class is complete. */
14537 DECL_PENDING_INLINE_INFO (fn) = cache;
14538 DECL_PENDING_INLINE_P (fn) = 1;
14539
14540 /* We need to know that this was defined in the class, so that
14541 friend templates are handled correctly. */
14542 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14543
14544 /* We're done with the inline definition. */
14545 finish_method (fn);
14546
14547 /* Add FN to the queue of functions to be parsed later. */
14548 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14549 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
14550 TREE_VALUE (parser->unparsed_functions_queues));
14551
14552 return fn;
14553}
14554
ec75414f
MM
14555/* Parse a template-argument-list, as well as the trailing ">" (but
14556 not the opening ">"). See cp_parser_template_argument_list for the
14557 return value. */
14558
14559static tree
14560cp_parser_enclosed_template_argument_list (cp_parser* parser)
14561{
14562 tree arguments;
14563 tree saved_scope;
14564 tree saved_qualifying_scope;
14565 tree saved_object_scope;
14566 bool saved_greater_than_is_operator_p;
14567
14568 /* [temp.names]
14569
14570 When parsing a template-id, the first non-nested `>' is taken as
14571 the end of the template-argument-list rather than a greater-than
14572 operator. */
21526606 14573 saved_greater_than_is_operator_p
ec75414f
MM
14574 = parser->greater_than_is_operator_p;
14575 parser->greater_than_is_operator_p = false;
14576 /* Parsing the argument list may modify SCOPE, so we save it
14577 here. */
14578 saved_scope = parser->scope;
14579 saved_qualifying_scope = parser->qualifying_scope;
14580 saved_object_scope = parser->object_scope;
14581 /* Parse the template-argument-list itself. */
14582 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14583 arguments = NULL_TREE;
14584 else
14585 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
14586 /* Look for the `>' that ends the template-argument-list. If we find
14587 a '>>' instead, it's probably just a typo. */
14588 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14589 {
14590 if (!saved_greater_than_is_operator_p)
14591 {
14592 /* If we're in a nested template argument list, the '>>' has to be
14593 a typo for '> >'. We emit the error message, but we continue
14594 parsing and we push a '>' as next token, so that the argument
14595 list will be parsed correctly.. */
14596 cp_token* token;
14597 error ("`>>' should be `> >' within a nested template argument list");
14598 token = cp_lexer_peek_token (parser->lexer);
14599 token->type = CPP_GREATER;
14600 }
14601 else
14602 {
14603 /* If this is not a nested template argument list, the '>>' is
14604 a typo for '>'. Emit an error message and continue. */
14605 error ("spurious `>>', use `>' to terminate a template argument list");
14606 cp_lexer_consume_token (parser->lexer);
14607 }
14608 }
6c0cc713
GB
14609 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14610 error ("missing `>' to terminate the template argument list");
ec75414f 14611 /* The `>' token might be a greater-than operator again now. */
21526606 14612 parser->greater_than_is_operator_p
ec75414f
MM
14613 = saved_greater_than_is_operator_p;
14614 /* Restore the SAVED_SCOPE. */
14615 parser->scope = saved_scope;
14616 parser->qualifying_scope = saved_qualifying_scope;
14617 parser->object_scope = saved_object_scope;
14618
14619 return arguments;
14620}
14621
a723baf1
MM
14622/* MEMBER_FUNCTION is a member function, or a friend. If default
14623 arguments, or the body of the function have not yet been parsed,
14624 parse them now. */
14625
14626static void
94edc4ab 14627cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14628{
14629 cp_lexer *saved_lexer;
14630
14631 /* If this member is a template, get the underlying
14632 FUNCTION_DECL. */
14633 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14634 member_function = DECL_TEMPLATE_RESULT (member_function);
14635
14636 /* There should not be any class definitions in progress at this
14637 point; the bodies of members are only parsed outside of all class
14638 definitions. */
14639 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14640 /* While we're parsing the member functions we might encounter more
14641 classes. We want to handle them right away, but we don't want
14642 them getting mixed up with functions that are currently in the
14643 queue. */
14644 parser->unparsed_functions_queues
14645 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14646
14647 /* Make sure that any template parameters are in scope. */
14648 maybe_begin_member_template_processing (member_function);
14649
a723baf1
MM
14650 /* If the body of the function has not yet been parsed, parse it
14651 now. */
14652 if (DECL_PENDING_INLINE_P (member_function))
14653 {
14654 tree function_scope;
14655 cp_token_cache *tokens;
14656
14657 /* The function is no longer pending; we are processing it. */
14658 tokens = DECL_PENDING_INLINE_INFO (member_function);
14659 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14660 DECL_PENDING_INLINE_P (member_function) = 0;
14661 /* If this was an inline function in a local class, enter the scope
14662 of the containing function. */
14663 function_scope = decl_function_context (member_function);
14664 if (function_scope)
14665 push_function_context_to (function_scope);
21526606 14666
a723baf1
MM
14667 /* Save away the current lexer. */
14668 saved_lexer = parser->lexer;
14669 /* Make a new lexer to feed us the tokens saved for this function. */
14670 parser->lexer = cp_lexer_new_from_tokens (tokens);
14671 parser->lexer->next = saved_lexer;
21526606 14672
a723baf1
MM
14673 /* Set the current source position to be the location of the first
14674 token in the saved inline body. */
3466b292 14675 cp_lexer_peek_token (parser->lexer);
21526606 14676
a723baf1
MM
14677 /* Let the front end know that we going to be defining this
14678 function. */
14679 start_function (NULL_TREE, member_function, NULL_TREE,
14680 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 14681
a723baf1
MM
14682 /* Now, parse the body of the function. */
14683 cp_parser_function_definition_after_declarator (parser,
14684 /*inline_p=*/true);
21526606 14685
a723baf1
MM
14686 /* Leave the scope of the containing function. */
14687 if (function_scope)
14688 pop_function_context_from (function_scope);
14689 /* Restore the lexer. */
14690 parser->lexer = saved_lexer;
14691 }
14692
14693 /* Remove any template parameters from the symbol table. */
14694 maybe_end_member_template_processing ();
14695
14696 /* Restore the queue. */
21526606 14697 parser->unparsed_functions_queues
a723baf1
MM
14698 = TREE_CHAIN (parser->unparsed_functions_queues);
14699}
14700
cd0be382 14701/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
14702 functions queue. */
14703
14704static void
14705cp_parser_save_default_args (cp_parser* parser, tree decl)
14706{
14707 tree probe;
14708
14709 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14710 probe;
14711 probe = TREE_CHAIN (probe))
14712 if (TREE_PURPOSE (probe))
14713 {
14714 TREE_PURPOSE (parser->unparsed_functions_queues)
21526606 14715 = tree_cons (NULL_TREE, decl,
8db1028e
NS
14716 TREE_PURPOSE (parser->unparsed_functions_queues));
14717 break;
14718 }
14719 return;
14720}
14721
8218bd34
MM
14722/* FN is a FUNCTION_DECL which may contains a parameter with an
14723 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14724
14725static void
8218bd34 14726cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14727{
14728 cp_lexer *saved_lexer;
14729 cp_token_cache *tokens;
14730 bool saved_local_variables_forbidden_p;
14731 tree parameters;
8218bd34 14732
b92bc2a0
NS
14733 /* While we're parsing the default args, we might (due to the
14734 statement expression extension) encounter more classes. We want
14735 to handle them right away, but we don't want them getting mixed
14736 up with default args that are currently in the queue. */
14737 parser->unparsed_functions_queues
14738 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14739
8218bd34 14740 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14741 parameters;
14742 parameters = TREE_CHAIN (parameters))
14743 {
14744 if (!TREE_PURPOSE (parameters)
14745 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14746 continue;
21526606 14747
a723baf1
MM
14748 /* Save away the current lexer. */
14749 saved_lexer = parser->lexer;
14750 /* Create a new one, using the tokens we have saved. */
14751 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14752 parser->lexer = cp_lexer_new_from_tokens (tokens);
14753
14754 /* Set the current source position to be the location of the
14755 first token in the default argument. */
3466b292 14756 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14757
14758 /* Local variable names (and the `this' keyword) may not appear
14759 in a default argument. */
14760 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14761 parser->local_variables_forbidden_p = true;
14762 /* Parse the assignment-expression. */
f128e1f3 14763 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 14764 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14765 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 14766 if (DECL_CLASS_SCOPE_P (fn))
e5976695 14767 pop_nested_class ();
a723baf1 14768
676e33ca
MM
14769 /* If the token stream has not been completely used up, then
14770 there was extra junk after the end of the default
14771 argument. */
14772 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14773 cp_parser_error (parser, "expected `,'");
14774
a723baf1
MM
14775 /* Restore saved state. */
14776 parser->lexer = saved_lexer;
14777 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14778 }
b92bc2a0
NS
14779
14780 /* Restore the queue. */
21526606 14781 parser->unparsed_functions_queues
b92bc2a0 14782 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
14783}
14784
14785/* Parse the operand of `sizeof' (or a similar operator). Returns
14786 either a TYPE or an expression, depending on the form of the
14787 input. The KEYWORD indicates which kind of expression we have
14788 encountered. */
14789
14790static tree
94edc4ab 14791cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14792{
14793 static const char *format;
14794 tree expr = NULL_TREE;
14795 const char *saved_message;
67c03833 14796 bool saved_integral_constant_expression_p;
a723baf1
MM
14797
14798 /* Initialize FORMAT the first time we get here. */
14799 if (!format)
14800 format = "types may not be defined in `%s' expressions";
14801
14802 /* Types cannot be defined in a `sizeof' expression. Save away the
14803 old message. */
14804 saved_message = parser->type_definition_forbidden_message;
14805 /* And create the new one. */
21526606
EC
14806 parser->type_definition_forbidden_message
14807 = xmalloc (strlen (format)
c68b0a84
KG
14808 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14809 + 1 /* `\0' */);
a723baf1
MM
14810 sprintf ((char *) parser->type_definition_forbidden_message,
14811 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14812
14813 /* The restrictions on constant-expressions do not apply inside
14814 sizeof expressions. */
67c03833
JM
14815 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14816 parser->integral_constant_expression_p = false;
a723baf1 14817
3beb3abf
MM
14818 /* Do not actually evaluate the expression. */
14819 ++skip_evaluation;
a723baf1
MM
14820 /* If it's a `(', then we might be looking at the type-id
14821 construction. */
14822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14823 {
14824 tree type;
4f8163b1 14825 bool saved_in_type_id_in_expr_p;
a723baf1
MM
14826
14827 /* We can't be sure yet whether we're looking at a type-id or an
14828 expression. */
14829 cp_parser_parse_tentatively (parser);
14830 /* Consume the `('. */
14831 cp_lexer_consume_token (parser->lexer);
14832 /* Parse the type-id. */
4f8163b1
MM
14833 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14834 parser->in_type_id_in_expr_p = true;
a723baf1 14835 type = cp_parser_type_id (parser);
4f8163b1 14836 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
14837 /* Now, look for the trailing `)'. */
14838 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14839 /* If all went well, then we're done. */
14840 if (cp_parser_parse_definitely (parser))
14841 {
14842 /* Build a list of decl-specifiers; right now, we have only
14843 a single type-specifier. */
14844 type = build_tree_list (NULL_TREE,
14845 type);
14846
14847 /* Call grokdeclarator to figure out what type this is. */
14848 expr = grokdeclarator (NULL_TREE,
14849 type,
14850 TYPENAME,
14851 /*initialized=*/0,
14852 /*attrlist=*/NULL);
14853 }
14854 }
14855
14856 /* If the type-id production did not work out, then we must be
14857 looking at the unary-expression production. */
14858 if (!expr)
14859 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14860 /* Go back to evaluating expressions. */
14861 --skip_evaluation;
a723baf1
MM
14862
14863 /* Free the message we created. */
14864 free ((char *) parser->type_definition_forbidden_message);
14865 /* And restore the old one. */
14866 parser->type_definition_forbidden_message = saved_message;
67c03833 14867 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
14868
14869 return expr;
14870}
14871
14872/* If the current declaration has no declarator, return true. */
14873
14874static bool
14875cp_parser_declares_only_class_p (cp_parser *parser)
14876{
21526606 14877 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
14878 declarator. */
14879 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14880 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14881}
14882
14883/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14884 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14885
14886static bool
94edc4ab 14887cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14888{
14889 while (decl_specifiers)
14890 {
14891 /* See if this decl-specifier is `friend'. */
14892 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14893 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14894 return true;
14895
14896 /* Go on to the next decl-specifier. */
14897 decl_specifiers = TREE_CHAIN (decl_specifiers);
14898 }
14899
14900 return false;
14901}
14902
14903/* If the next token is of the indicated TYPE, consume it. Otherwise,
14904 issue an error message indicating that TOKEN_DESC was expected.
21526606 14905
a723baf1
MM
14906 Returns the token consumed, if the token had the appropriate type.
14907 Otherwise, returns NULL. */
14908
14909static cp_token *
94edc4ab
NN
14910cp_parser_require (cp_parser* parser,
14911 enum cpp_ttype type,
14912 const char* token_desc)
a723baf1
MM
14913{
14914 if (cp_lexer_next_token_is (parser->lexer, type))
14915 return cp_lexer_consume_token (parser->lexer);
14916 else
14917 {
e5976695
MM
14918 /* Output the MESSAGE -- unless we're parsing tentatively. */
14919 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
14920 {
14921 char *message = concat ("expected ", token_desc, NULL);
14922 cp_parser_error (parser, message);
14923 free (message);
14924 }
a723baf1
MM
14925 return NULL;
14926 }
14927}
14928
14929/* Like cp_parser_require, except that tokens will be skipped until
14930 the desired token is found. An error message is still produced if
14931 the next token is not as expected. */
14932
14933static void
21526606
EC
14934cp_parser_skip_until_found (cp_parser* parser,
14935 enum cpp_ttype type,
94edc4ab 14936 const char* token_desc)
a723baf1
MM
14937{
14938 cp_token *token;
14939 unsigned nesting_depth = 0;
14940
14941 if (cp_parser_require (parser, type, token_desc))
14942 return;
14943
14944 /* Skip tokens until the desired token is found. */
14945 while (true)
14946 {
14947 /* Peek at the next token. */
14948 token = cp_lexer_peek_token (parser->lexer);
21526606 14949 /* If we've reached the token we want, consume it and
a723baf1
MM
14950 stop. */
14951 if (token->type == type && !nesting_depth)
14952 {
14953 cp_lexer_consume_token (parser->lexer);
14954 return;
14955 }
14956 /* If we've run out of tokens, stop. */
14957 if (token->type == CPP_EOF)
14958 return;
21526606 14959 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
14960 || token->type == CPP_OPEN_PAREN
14961 || token->type == CPP_OPEN_SQUARE)
14962 ++nesting_depth;
21526606 14963 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
14964 || token->type == CPP_CLOSE_PAREN
14965 || token->type == CPP_CLOSE_SQUARE)
14966 {
14967 if (nesting_depth-- == 0)
14968 return;
14969 }
14970 /* Consume this token. */
14971 cp_lexer_consume_token (parser->lexer);
14972 }
14973}
14974
14975/* If the next token is the indicated keyword, consume it. Otherwise,
14976 issue an error message indicating that TOKEN_DESC was expected.
21526606 14977
a723baf1
MM
14978 Returns the token consumed, if the token had the appropriate type.
14979 Otherwise, returns NULL. */
14980
14981static cp_token *
94edc4ab
NN
14982cp_parser_require_keyword (cp_parser* parser,
14983 enum rid keyword,
14984 const char* token_desc)
a723baf1
MM
14985{
14986 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14987
14988 if (token && token->keyword != keyword)
14989 {
14990 dyn_string_t error_msg;
14991
14992 /* Format the error message. */
14993 error_msg = dyn_string_new (0);
14994 dyn_string_append_cstr (error_msg, "expected ");
14995 dyn_string_append_cstr (error_msg, token_desc);
14996 cp_parser_error (parser, error_msg->s);
14997 dyn_string_delete (error_msg);
14998 return NULL;
14999 }
15000
15001 return token;
15002}
15003
15004/* Returns TRUE iff TOKEN is a token that can begin the body of a
15005 function-definition. */
15006
21526606 15007static bool
94edc4ab 15008cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15009{
15010 return (/* An ordinary function-body begins with an `{'. */
15011 token->type == CPP_OPEN_BRACE
15012 /* A ctor-initializer begins with a `:'. */
15013 || token->type == CPP_COLON
15014 /* A function-try-block begins with `try'. */
15015 || token->keyword == RID_TRY
15016 /* The named return value extension begins with `return'. */
15017 || token->keyword == RID_RETURN);
15018}
15019
15020/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15021 definition. */
15022
15023static bool
15024cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15025{
15026 cp_token *token;
15027
15028 token = cp_lexer_peek_token (parser->lexer);
15029 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15030}
15031
d17811fd 15032/* Returns TRUE iff the next token is the "," or ">" ending a
4d5297fa
GB
15033 template-argument. ">>" is also accepted (after the full
15034 argument was parsed) because it's probably a typo for "> >",
15035 and there is a specific diagnostic for this. */
d17811fd
MM
15036
15037static bool
15038cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15039{
15040 cp_token *token;
15041
15042 token = cp_lexer_peek_token (parser->lexer);
21526606 15043 return (token->type == CPP_COMMA || token->type == CPP_GREATER
4d5297fa 15044 || token->type == CPP_RSHIFT);
d17811fd 15045}
f4abade9
GB
15046
15047/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15048 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15049
15050static bool
21526606 15051cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15052 size_t n)
15053{
15054 cp_token *token;
15055
15056 token = cp_lexer_peek_nth_token (parser->lexer, n);
15057 if (token->type == CPP_LESS)
15058 return true;
15059 /* Check for the sequence `<::' in the original code. It would be lexed as
15060 `[:', where `[' is a digraph, and there is no whitespace before
15061 `:'. */
15062 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15063 {
15064 cp_token *token2;
15065 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15066 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15067 return true;
15068 }
15069 return false;
15070}
21526606 15071
a723baf1
MM
15072/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15073 or none_type otherwise. */
15074
15075static enum tag_types
94edc4ab 15076cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15077{
15078 switch (token->keyword)
15079 {
15080 case RID_CLASS:
15081 return class_type;
15082 case RID_STRUCT:
15083 return record_type;
15084 case RID_UNION:
15085 return union_type;
21526606 15086
a723baf1
MM
15087 default:
15088 return none_type;
15089 }
15090}
15091
15092/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15093
15094static void
15095cp_parser_check_class_key (enum tag_types class_key, tree type)
15096{
15097 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15098 pedwarn ("`%s' tag used in naming `%#T'",
15099 class_key == union_type ? "union"
21526606 15100 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15101 type);
15102}
21526606 15103
cd0be382 15104/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15105 access than its original declaration [class.access.spec/3].
15106 This applies to nested classes and nested class templates.
15107 [class.mem/1]. */
15108
15109static void cp_parser_check_access_in_redeclaration (tree decl)
15110{
15111 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15112 return;
15113
15114 if ((TREE_PRIVATE (decl)
15115 != (current_access_specifier == access_private_node))
15116 || (TREE_PROTECTED (decl)
15117 != (current_access_specifier == access_protected_node)))
15118 error ("%D redeclared with different access", decl);
15119}
15120
a723baf1 15121/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15122 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15123 consumed. */
15124
15125static bool
15126cp_parser_optional_template_keyword (cp_parser *parser)
15127{
15128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15129 {
15130 /* The `template' keyword can only be used within templates;
15131 outside templates the parser can always figure out what is a
15132 template and what is not. */
15133 if (!processing_template_decl)
15134 {
15135 error ("`template' (as a disambiguator) is only allowed "
15136 "within templates");
15137 /* If this part of the token stream is rescanned, the same
15138 error message would be generated. So, we purge the token
15139 from the stream. */
15140 cp_lexer_purge_token (parser->lexer);
15141 return false;
15142 }
15143 else
15144 {
15145 /* Consume the `template' keyword. */
15146 cp_lexer_consume_token (parser->lexer);
15147 return true;
15148 }
15149 }
15150
15151 return false;
15152}
15153
2050a1bb
MM
15154/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15155 set PARSER->SCOPE, and perform other related actions. */
15156
15157static void
15158cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15159{
15160 tree value;
15161 tree check;
15162
15163 /* Get the stored value. */
15164 value = cp_lexer_consume_token (parser->lexer)->value;
15165 /* Perform any access checks that were deferred. */
15166 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15167 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15168 /* Set the scope from the stored value. */
15169 parser->scope = TREE_VALUE (value);
15170 parser->qualifying_scope = TREE_TYPE (value);
15171 parser->object_scope = NULL_TREE;
15172}
15173
852dcbdd 15174/* Add tokens to CACHE until a non-nested END token appears. */
a723baf1
MM
15175
15176static void
21526606 15177cp_parser_cache_group (cp_parser *parser,
a723baf1
MM
15178 cp_token_cache *cache,
15179 enum cpp_ttype end,
15180 unsigned depth)
15181{
15182 while (true)
15183 {
15184 cp_token *token;
15185
15186 /* Abort a parenthesized expression if we encounter a brace. */
15187 if ((end == CPP_CLOSE_PAREN || depth == 0)
15188 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15189 return;
a723baf1 15190 /* If we've reached the end of the file, stop. */
4bfb8bba 15191 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15192 return;
4bfb8bba
MM
15193 /* Consume the next token. */
15194 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15195 /* Add this token to the tokens we are saving. */
15196 cp_token_cache_push_token (cache, token);
15197 /* See if it starts a new group. */
15198 if (token->type == CPP_OPEN_BRACE)
15199 {
15200 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15201 if (depth == 0)
15202 return;
15203 }
15204 else if (token->type == CPP_OPEN_PAREN)
15205 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15206 else if (token->type == end)
15207 return;
15208 }
15209}
15210
15211/* Begin parsing tentatively. We always save tokens while parsing
15212 tentatively so that if the tentative parsing fails we can restore the
15213 tokens. */
15214
15215static void
94edc4ab 15216cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15217{
15218 /* Enter a new parsing context. */
15219 parser->context = cp_parser_context_new (parser->context);
15220 /* Begin saving tokens. */
15221 cp_lexer_save_tokens (parser->lexer);
15222 /* In order to avoid repetitive access control error messages,
15223 access checks are queued up until we are no longer parsing
15224 tentatively. */
8d241e0b 15225 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15226}
15227
15228/* Commit to the currently active tentative parse. */
15229
15230static void
94edc4ab 15231cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15232{
15233 cp_parser_context *context;
15234 cp_lexer *lexer;
15235
15236 /* Mark all of the levels as committed. */
15237 lexer = parser->lexer;
15238 for (context = parser->context; context->next; context = context->next)
15239 {
15240 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15241 break;
15242 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15243 while (!cp_lexer_saving_tokens (lexer))
15244 lexer = lexer->next;
15245 cp_lexer_commit_tokens (lexer);
15246 }
15247}
15248
15249/* Abort the currently active tentative parse. All consumed tokens
15250 will be rolled back, and no diagnostics will be issued. */
15251
15252static void
94edc4ab 15253cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15254{
15255 cp_parser_simulate_error (parser);
15256 /* Now, pretend that we want to see if the construct was
15257 successfully parsed. */
15258 cp_parser_parse_definitely (parser);
15259}
15260
34cd5ae7 15261/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15262 token stream. Otherwise, commit to the tokens we have consumed.
15263 Returns true if no error occurred; false otherwise. */
15264
15265static bool
94edc4ab 15266cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15267{
15268 bool error_occurred;
15269 cp_parser_context *context;
15270
34cd5ae7 15271 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15272 destroy that information. */
15273 error_occurred = cp_parser_error_occurred (parser);
15274 /* Remove the topmost context from the stack. */
15275 context = parser->context;
15276 parser->context = context->next;
15277 /* If no parse errors occurred, commit to the tentative parse. */
15278 if (!error_occurred)
15279 {
15280 /* Commit to the tokens read tentatively, unless that was
15281 already done. */
15282 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15283 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15284
15285 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15286 }
15287 /* Otherwise, if errors occurred, roll back our state so that things
15288 are just as they were before we began the tentative parse. */
15289 else
cf22909c
KL
15290 {
15291 cp_lexer_rollback_tokens (parser->lexer);
15292 pop_deferring_access_checks ();
15293 }
e5976695
MM
15294 /* Add the context to the front of the free list. */
15295 context->next = cp_parser_context_free_list;
15296 cp_parser_context_free_list = context;
15297
15298 return !error_occurred;
a723baf1
MM
15299}
15300
a723baf1
MM
15301/* Returns true if we are parsing tentatively -- but have decided that
15302 we will stick with this tentative parse, even if errors occur. */
15303
15304static bool
94edc4ab 15305cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15306{
15307 return (cp_parser_parsing_tentatively (parser)
15308 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15309}
15310
4de8668e 15311/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15312 tentative parse. */
21526606 15313
a723baf1 15314static bool
94edc4ab 15315cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15316{
15317 return (cp_parser_parsing_tentatively (parser)
15318 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15319}
15320
4de8668e 15321/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15322
15323static bool
94edc4ab 15324cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15325{
15326 return parser->allow_gnu_extensions_p;
15327}
15328
15329\f
15330
15331/* The parser. */
15332
15333static GTY (()) cp_parser *the_parser;
15334
15335/* External interface. */
15336
d1bd0ded 15337/* Parse one entire translation unit. */
a723baf1 15338
d1bd0ded
GK
15339void
15340c_parse_file (void)
a723baf1
MM
15341{
15342 bool error_occurred;
15343
15344 the_parser = cp_parser_new ();
78757caa
KL
15345 push_deferring_access_checks (flag_access_control
15346 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15347 error_occurred = cp_parser_translation_unit (the_parser);
15348 the_parser = NULL;
a723baf1
MM
15349}
15350
a723baf1
MM
15351/* This variable must be provided by every front end. */
15352
15353int yydebug;
15354
15355#include "gt-cp-parser.h"