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