]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
Makefile.in: Refine dependencies.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
3beb3abf 2 Copyright (C) 2000, 2001, 2002, 2003 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.
61
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. */
71 enum cpp_ttype type;
72 /* The value associated with this token, if any. */
73 tree value;
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
76 enum rid keyword;
82a98427
NS
77 /* The location at which this token was found. */
78 location_t location;
a723baf1
MM
79} cp_token;
80
81/* The number of tokens in a single token block. */
82
83#define CP_TOKEN_BLOCK_NUM_TOKENS 32
84
85/* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
89 complete.)
90
91 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 92 variable-length array), is used due to constraints imposed by the
a723baf1
MM
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
95
96typedef struct cp_token_block GTY (())
97{
98 /* The tokens. */
99 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100 /* The number of tokens in this block. */
101 size_t num_tokens;
102 /* The next token block in the chain. */
103 struct cp_token_block *next;
104 /* The previous block in the chain. */
105 struct cp_token_block *prev;
106} cp_token_block;
107
108typedef struct cp_token_cache GTY (())
109{
110 /* The first block in the cache. NULL if there are no tokens in the
111 cache. */
112 cp_token_block *first;
113 /* The last block in the cache. NULL If there are no tokens in the
114 cache. */
115 cp_token_block *last;
116} cp_token_cache;
117
9bcb9aae 118/* Prototypes. */
a723baf1
MM
119
120static cp_token_cache *cp_token_cache_new
121 (void);
122static void cp_token_cache_push_token
123 (cp_token_cache *, cp_token *);
124
125/* Create a new cp_token_cache. */
126
127static cp_token_cache *
128cp_token_cache_new ()
129{
c68b0a84 130 return ggc_alloc_cleared (sizeof (cp_token_cache));
a723baf1
MM
131}
132
133/* Add *TOKEN to *CACHE. */
134
135static void
136cp_token_cache_push_token (cp_token_cache *cache,
137 cp_token *token)
138{
139 cp_token_block *b = cache->last;
140
141 /* See if we need to allocate a new token block. */
142 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
143 {
c68b0a84 144 b = ggc_alloc_cleared (sizeof (cp_token_block));
a723baf1
MM
145 b->prev = cache->last;
146 if (cache->last)
147 {
148 cache->last->next = b;
149 cache->last = b;
150 }
151 else
152 cache->first = cache->last = b;
153 }
154 /* Add this token to the current token block. */
155 b->tokens[b->num_tokens++] = *token;
156}
157
158/* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
160 it to the parser. */
161
162typedef struct cp_lexer GTY (())
163{
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token * GTY ((skip (""))) buffer_end;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token * GTY ((skip (""))) first_token;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token * GTY ((skip (""))) next_token;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token * GTY ((skip (""))) last_token;
180
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
185
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
190
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens;
195
196 /* The STRING_CST tokens encountered while processing the current
197 string literal. */
198 varray_type string_tokens;
199
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
202 bool main_lexer_p;
203
204 /* True if we should output debugging information. */
205 bool debugging_p;
206
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer *next;
209} cp_lexer;
210
211/* Prototypes. */
212
17211ab5 213static cp_lexer *cp_lexer_new_main
94edc4ab 214 (void);
a723baf1 215static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 216 (struct cp_token_cache *);
a723baf1 217static int cp_lexer_saving_tokens
94edc4ab 218 (const cp_lexer *);
a723baf1 219static cp_token *cp_lexer_next_token
94edc4ab
NN
220 (cp_lexer *, cp_token *);
221static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer *, cp_token *, cp_token *);
a723baf1 223static cp_token *cp_lexer_read_token
94edc4ab 224 (cp_lexer *);
a723baf1 225static void cp_lexer_maybe_grow_buffer
94edc4ab 226 (cp_lexer *);
a723baf1 227static void cp_lexer_get_preprocessor_token
94edc4ab 228 (cp_lexer *, cp_token *);
a723baf1 229static cp_token *cp_lexer_peek_token
94edc4ab 230 (cp_lexer *);
a723baf1 231static cp_token *cp_lexer_peek_nth_token
94edc4ab 232 (cp_lexer *, size_t);
f7b5ecd9 233static inline bool cp_lexer_next_token_is
94edc4ab 234 (cp_lexer *, enum cpp_ttype);
a723baf1 235static bool cp_lexer_next_token_is_not
94edc4ab 236 (cp_lexer *, enum cpp_ttype);
a723baf1 237static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
238 (cp_lexer *, enum rid);
239static cp_token *cp_lexer_consume_token
240 (cp_lexer *);
a723baf1
MM
241static void cp_lexer_purge_token
242 (cp_lexer *);
243static void cp_lexer_purge_tokens_after
244 (cp_lexer *, cp_token *);
245static void cp_lexer_save_tokens
94edc4ab 246 (cp_lexer *);
a723baf1 247static void cp_lexer_commit_tokens
94edc4ab 248 (cp_lexer *);
a723baf1 249static void cp_lexer_rollback_tokens
94edc4ab 250 (cp_lexer *);
f7b5ecd9 251static inline void cp_lexer_set_source_position_from_token
94edc4ab 252 (cp_lexer *, const cp_token *);
a723baf1 253static void cp_lexer_print_token
94edc4ab 254 (FILE *, cp_token *);
f7b5ecd9 255static inline bool cp_lexer_debugging_p
94edc4ab 256 (cp_lexer *);
a723baf1 257static void cp_lexer_start_debugging
94edc4ab 258 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 259static void cp_lexer_stop_debugging
94edc4ab 260 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
261
262/* Manifest constants. */
263
264#define CP_TOKEN_BUFFER_SIZE 5
265#define CP_SAVED_TOKENS_SIZE 5
266
267/* A token type for keywords, as opposed to ordinary identifiers. */
268#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269
270/* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
275
276/* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
282
283/* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
286
287/* Variables. */
288
289/* The stream to which debugging output should be written. */
290static FILE *cp_lexer_debug_stream;
291
17211ab5
GK
292/* Create a new main C++ lexer, the lexer that gets tokens from the
293 preprocessor. */
a723baf1
MM
294
295static cp_lexer *
17211ab5 296cp_lexer_new_main (void)
a723baf1
MM
297{
298 cp_lexer *lexer;
17211ab5
GK
299 cp_token first_token;
300
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 305 c_common_no_more_pch ();
a723baf1
MM
306
307 /* Allocate the memory. */
c68b0a84 308 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
309
310 /* Create the circular buffer. */
c68b0a84 311 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
312 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
313
17211ab5
GK
314 /* There is one token in the buffer. */
315 lexer->last_token = lexer->buffer + 1;
316 lexer->first_token = lexer->buffer;
317 lexer->next_token = lexer->buffer;
318 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
319
320 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 321 lexer->main_lexer_p = true;
a723baf1
MM
322
323 /* Create the SAVED_TOKENS stack. */
324 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
325
326 /* Create the STRINGS array. */
327 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
328
329 /* Assume we are not debugging. */
330 lexer->debugging_p = false;
331
332 return lexer;
333}
334
335/* Create a new lexer whose token stream is primed with the TOKENS.
336 When these tokens are exhausted, no new tokens will be read. */
337
338static cp_lexer *
339cp_lexer_new_from_tokens (cp_token_cache *tokens)
340{
341 cp_lexer *lexer;
342 cp_token *token;
343 cp_token_block *block;
344 ptrdiff_t num_tokens;
345
17211ab5 346 /* Allocate the memory. */
c68b0a84 347 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
348
349 /* Create a new buffer, appropriately sized. */
350 num_tokens = 0;
351 for (block = tokens->first; block != NULL; block = block->next)
352 num_tokens += block->num_tokens;
c68b0a84 353 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1
MM
354 lexer->buffer_end = lexer->buffer + num_tokens;
355
356 /* Install the tokens. */
357 token = lexer->buffer;
358 for (block = tokens->first; block != NULL; block = block->next)
359 {
360 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
361 token += block->num_tokens;
362 }
363
364 /* The FIRST_TOKEN is the beginning of the buffer. */
365 lexer->first_token = lexer->buffer;
366 /* The next available token is also at the beginning of the buffer. */
367 lexer->next_token = lexer->buffer;
368 /* The buffer is full. */
369 lexer->last_token = lexer->first_token;
370
17211ab5
GK
371 /* This lexer doesn't obtain more tokens. */
372 lexer->main_lexer_p = false;
373
374 /* Create the SAVED_TOKENS stack. */
375 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
376
377 /* Create the STRINGS array. */
378 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
379
380 /* Assume we are not debugging. */
381 lexer->debugging_p = false;
382
a723baf1
MM
383 return lexer;
384}
385
4de8668e 386/* Returns nonzero if debugging information should be output. */
a723baf1 387
f7b5ecd9
MM
388static inline bool
389cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 390{
f7b5ecd9
MM
391 return lexer->debugging_p;
392}
393
394/* Set the current source position from the information stored in
395 TOKEN. */
396
397static inline void
94edc4ab
NN
398cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
399 const cp_token *token)
f7b5ecd9
MM
400{
401 /* Ideally, the source position information would not be a global
402 variable, but it is. */
403
404 /* Update the line number. */
405 if (token->type != CPP_EOF)
82a98427 406 input_location = token->location;
a723baf1
MM
407}
408
409/* TOKEN points into the circular token buffer. Return a pointer to
410 the next token in the buffer. */
411
f7b5ecd9 412static inline cp_token *
94edc4ab 413cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
414{
415 token++;
416 if (token == lexer->buffer_end)
417 token = lexer->buffer;
418 return token;
419}
420
4de8668e 421/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
422
423static int
94edc4ab 424cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
425{
426 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
427}
428
a723baf1
MM
429/* Return a pointer to the token that is N tokens beyond TOKEN in the
430 buffer. */
431
432static cp_token *
433cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
434{
435 token += n;
436 if (token >= lexer->buffer_end)
437 token = lexer->buffer + (token - lexer->buffer_end);
438 return token;
439}
440
441/* Returns the number of times that START would have to be incremented
442 to reach FINISH. If START and FINISH are the same, returns zero. */
443
444static ptrdiff_t
94edc4ab 445cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
446{
447 if (finish >= start)
448 return finish - start;
449 else
450 return ((lexer->buffer_end - lexer->buffer)
451 - (start - finish));
452}
453
454/* Obtain another token from the C preprocessor and add it to the
455 token buffer. Returns the newly read token. */
456
457static cp_token *
94edc4ab 458cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
459{
460 cp_token *token;
461
462 /* Make sure there is room in the buffer. */
463 cp_lexer_maybe_grow_buffer (lexer);
464
465 /* If there weren't any tokens, then this one will be the first. */
466 if (!lexer->first_token)
467 lexer->first_token = lexer->last_token;
468 /* Similarly, if there were no available tokens, there is one now. */
469 if (!lexer->next_token)
470 lexer->next_token = lexer->last_token;
471
472 /* Figure out where we're going to store the new token. */
473 token = lexer->last_token;
474
475 /* Get a new token from the preprocessor. */
476 cp_lexer_get_preprocessor_token (lexer, token);
477
478 /* Increment LAST_TOKEN. */
479 lexer->last_token = cp_lexer_next_token (lexer, token);
480
e6cc3a24
ZW
481 /* Strings should have type `const char []'. Right now, we will
482 have an ARRAY_TYPE that is constant rather than an array of
483 constant elements.
484 FIXME: Make fix_string_type get this right in the first place. */
485 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
486 && flag_const_strings)
a723baf1 487 {
e6cc3a24
ZW
488 tree type;
489
490 /* Get the current type. It will be an ARRAY_TYPE. */
491 type = TREE_TYPE (token->value);
492 /* Use build_cplus_array_type to rebuild the array, thereby
493 getting the right type. */
494 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
495 /* Reset the type of the token. */
496 TREE_TYPE (token->value) = type;
a723baf1
MM
497 }
498
499 return token;
500}
501
502/* If the circular buffer is full, make it bigger. */
503
504static void
94edc4ab 505cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
506{
507 /* If the buffer is full, enlarge it. */
508 if (lexer->last_token == lexer->first_token)
509 {
510 cp_token *new_buffer;
511 cp_token *old_buffer;
512 cp_token *new_first_token;
513 ptrdiff_t buffer_length;
514 size_t num_tokens_to_copy;
515
516 /* Remember the current buffer pointer. It will become invalid,
517 but we will need to do pointer arithmetic involving this
518 value. */
519 old_buffer = lexer->buffer;
520 /* Compute the current buffer size. */
521 buffer_length = lexer->buffer_end - lexer->buffer;
522 /* Allocate a buffer twice as big. */
c68b0a84
KG
523 new_buffer = ggc_realloc (lexer->buffer,
524 2 * buffer_length * sizeof (cp_token));
a723baf1
MM
525
526 /* Because the buffer is circular, logically consecutive tokens
527 are not necessarily placed consecutively in memory.
528 Therefore, we must keep move the tokens that were before
529 FIRST_TOKEN to the second half of the newly allocated
530 buffer. */
531 num_tokens_to_copy = (lexer->first_token - old_buffer);
532 memcpy (new_buffer + buffer_length,
533 new_buffer,
534 num_tokens_to_copy * sizeof (cp_token));
535 /* Clear the rest of the buffer. We never look at this storage,
536 but the garbage collector may. */
537 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
538 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
539
540 /* Now recompute all of the buffer pointers. */
541 new_first_token
542 = new_buffer + (lexer->first_token - old_buffer);
543 if (lexer->next_token != NULL)
544 {
545 ptrdiff_t next_token_delta;
546
547 if (lexer->next_token > lexer->first_token)
548 next_token_delta = lexer->next_token - lexer->first_token;
549 else
550 next_token_delta =
551 buffer_length - (lexer->first_token - lexer->next_token);
552 lexer->next_token = new_first_token + next_token_delta;
553 }
554 lexer->last_token = new_first_token + buffer_length;
555 lexer->buffer = new_buffer;
556 lexer->buffer_end = new_buffer + buffer_length * 2;
557 lexer->first_token = new_first_token;
558 }
559}
560
561/* Store the next token from the preprocessor in *TOKEN. */
562
563static void
94edc4ab
NN
564cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
565 cp_token *token)
a723baf1
MM
566{
567 bool done;
568
569 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 570 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
571 {
572 token->type = CPP_EOF;
82a98427
NS
573 token->location.line = 0;
574 token->location.file = NULL;
a723baf1
MM
575 token->value = NULL_TREE;
576 token->keyword = RID_MAX;
577
578 return;
579 }
580
581 done = false;
582 /* Keep going until we get a token we like. */
583 while (!done)
584 {
585 /* Get a new token from the preprocessor. */
586 token->type = c_lex (&token->value);
587 /* Issue messages about tokens we cannot process. */
588 switch (token->type)
589 {
590 case CPP_ATSIGN:
591 case CPP_HASH:
592 case CPP_PASTE:
593 error ("invalid token");
594 break;
595
a723baf1
MM
596 default:
597 /* This is a good token, so we exit the loop. */
598 done = true;
599 break;
600 }
601 }
602 /* Now we've got our token. */
82a98427 603 token->location = input_location;
a723baf1
MM
604
605 /* Check to see if this token is a keyword. */
606 if (token->type == CPP_NAME
607 && C_IS_RESERVED_WORD (token->value))
608 {
609 /* Mark this token as a keyword. */
610 token->type = CPP_KEYWORD;
611 /* Record which keyword. */
612 token->keyword = C_RID_CODE (token->value);
613 /* Update the value. Some keywords are mapped to particular
614 entities, rather than simply having the value of the
615 corresponding IDENTIFIER_NODE. For example, `__const' is
616 mapped to `const'. */
617 token->value = ridpointers[token->keyword];
618 }
619 else
620 token->keyword = RID_MAX;
621}
622
623/* Return a pointer to the next token in the token stream, but do not
624 consume it. */
625
626static cp_token *
94edc4ab 627cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
628{
629 cp_token *token;
630
631 /* If there are no tokens, read one now. */
632 if (!lexer->next_token)
633 cp_lexer_read_token (lexer);
634
635 /* Provide debugging output. */
636 if (cp_lexer_debugging_p (lexer))
637 {
638 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
639 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
640 fprintf (cp_lexer_debug_stream, "\n");
641 }
642
643 token = lexer->next_token;
644 cp_lexer_set_source_position_from_token (lexer, token);
645 return token;
646}
647
648/* Return true if the next token has the indicated TYPE. */
649
650static bool
94edc4ab 651cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
652{
653 cp_token *token;
654
655 /* Peek at the next token. */
656 token = cp_lexer_peek_token (lexer);
657 /* Check to see if it has the indicated TYPE. */
658 return token->type == type;
659}
660
661/* Return true if the next token does not have the indicated TYPE. */
662
663static bool
94edc4ab 664cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
665{
666 return !cp_lexer_next_token_is (lexer, type);
667}
668
669/* Return true if the next token is the indicated KEYWORD. */
670
671static bool
94edc4ab 672cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
673{
674 cp_token *token;
675
676 /* Peek at the next token. */
677 token = cp_lexer_peek_token (lexer);
678 /* Check to see if it is the indicated keyword. */
679 return token->keyword == keyword;
680}
681
682/* Return a pointer to the Nth token in the token stream. If N is 1,
683 then this is precisely equivalent to cp_lexer_peek_token. */
684
685static cp_token *
94edc4ab 686cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
687{
688 cp_token *token;
689
690 /* N is 1-based, not zero-based. */
691 my_friendly_assert (n > 0, 20000224);
692
693 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
694 token = lexer->next_token;
695 /* If there are no tokens in the buffer, get one now. */
696 if (!token)
697 {
698 cp_lexer_read_token (lexer);
699 token = lexer->next_token;
700 }
701
702 /* Now, read tokens until we have enough. */
703 while (--n > 0)
704 {
705 /* Advance to the next token. */
706 token = cp_lexer_next_token (lexer, token);
707 /* If that's all the tokens we have, read a new one. */
708 if (token == lexer->last_token)
709 token = cp_lexer_read_token (lexer);
710 }
711
712 return token;
713}
714
715/* Consume the next token. The pointer returned is valid only until
716 another token is read. Callers should preserve copy the token
717 explicitly if they will need its value for a longer period of
718 time. */
719
720static cp_token *
94edc4ab 721cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
722{
723 cp_token *token;
724
725 /* If there are no tokens, read one now. */
726 if (!lexer->next_token)
727 cp_lexer_read_token (lexer);
728
729 /* Remember the token we'll be returning. */
730 token = lexer->next_token;
731
732 /* Increment NEXT_TOKEN. */
733 lexer->next_token = cp_lexer_next_token (lexer,
734 lexer->next_token);
735 /* Check to see if we're all out of tokens. */
736 if (lexer->next_token == lexer->last_token)
737 lexer->next_token = NULL;
738
739 /* If we're not saving tokens, then move FIRST_TOKEN too. */
740 if (!cp_lexer_saving_tokens (lexer))
741 {
742 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
743 if (!lexer->next_token)
744 lexer->first_token = NULL;
745 else
746 lexer->first_token = lexer->next_token;
747 }
748
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 {
752 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
753 cp_lexer_print_token (cp_lexer_debug_stream, token);
754 fprintf (cp_lexer_debug_stream, "\n");
755 }
756
757 return token;
758}
759
760/* Permanently remove the next token from the token stream. There
761 must be a valid next token already; this token never reads
762 additional tokens from the preprocessor. */
763
764static void
765cp_lexer_purge_token (cp_lexer *lexer)
766{
767 cp_token *token;
768 cp_token *next_token;
769
770 token = lexer->next_token;
771 while (true)
772 {
773 next_token = cp_lexer_next_token (lexer, token);
774 if (next_token == lexer->last_token)
775 break;
776 *token = *next_token;
777 token = next_token;
778 }
779
780 lexer->last_token = token;
781 /* The token purged may have been the only token remaining; if so,
782 clear NEXT_TOKEN. */
783 if (lexer->next_token == token)
784 lexer->next_token = NULL;
785}
786
787/* Permanently remove all tokens after TOKEN, up to, but not
788 including, the token that will be returned next by
789 cp_lexer_peek_token. */
790
791static void
792cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
793{
794 cp_token *peek;
795 cp_token *t1;
796 cp_token *t2;
797
798 if (lexer->next_token)
799 {
800 /* Copy the tokens that have not yet been read to the location
801 immediately following TOKEN. */
802 t1 = cp_lexer_next_token (lexer, token);
803 t2 = peek = cp_lexer_peek_token (lexer);
804 /* Move tokens into the vacant area between TOKEN and PEEK. */
805 while (t2 != lexer->last_token)
806 {
807 *t1 = *t2;
808 t1 = cp_lexer_next_token (lexer, t1);
809 t2 = cp_lexer_next_token (lexer, t2);
810 }
811 /* Now, the next available token is right after TOKEN. */
812 lexer->next_token = cp_lexer_next_token (lexer, token);
813 /* And the last token is wherever we ended up. */
814 lexer->last_token = t1;
815 }
816 else
817 {
818 /* There are no tokens in the buffer, so there is nothing to
819 copy. The last token in the buffer is TOKEN itself. */
820 lexer->last_token = cp_lexer_next_token (lexer, token);
821 }
822}
823
824/* Begin saving tokens. All tokens consumed after this point will be
825 preserved. */
826
827static void
94edc4ab 828cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
829{
830 /* Provide debugging output. */
831 if (cp_lexer_debugging_p (lexer))
832 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
833
834 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
835 restore the tokens if required. */
836 if (!lexer->next_token)
837 cp_lexer_read_token (lexer);
838
839 VARRAY_PUSH_INT (lexer->saved_tokens,
840 cp_lexer_token_difference (lexer,
841 lexer->first_token,
842 lexer->next_token));
843}
844
845/* Commit to the portion of the token stream most recently saved. */
846
847static void
94edc4ab 848cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
849{
850 /* Provide debugging output. */
851 if (cp_lexer_debugging_p (lexer))
852 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
853
854 VARRAY_POP (lexer->saved_tokens);
855}
856
857/* Return all tokens saved since the last call to cp_lexer_save_tokens
858 to the token stream. Stop saving tokens. */
859
860static void
94edc4ab 861cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
862{
863 size_t delta;
864
865 /* Provide debugging output. */
866 if (cp_lexer_debugging_p (lexer))
867 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
868
869 /* Find the token that was the NEXT_TOKEN when we started saving
870 tokens. */
871 delta = VARRAY_TOP_INT(lexer->saved_tokens);
872 /* Make it the next token again now. */
873 lexer->next_token = cp_lexer_advance_token (lexer,
874 lexer->first_token,
875 delta);
15d2cb19 876 /* It might be the case that there were no tokens when we started
a723baf1
MM
877 saving tokens, but that there are some tokens now. */
878 if (!lexer->next_token && lexer->first_token)
879 lexer->next_token = lexer->first_token;
880
881 /* Stop saving tokens. */
882 VARRAY_POP (lexer->saved_tokens);
883}
884
a723baf1
MM
885/* Print a representation of the TOKEN on the STREAM. */
886
887static void
94edc4ab 888cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
889{
890 const char *token_type = NULL;
891
892 /* Figure out what kind of token this is. */
893 switch (token->type)
894 {
895 case CPP_EQ:
896 token_type = "EQ";
897 break;
898
899 case CPP_COMMA:
900 token_type = "COMMA";
901 break;
902
903 case CPP_OPEN_PAREN:
904 token_type = "OPEN_PAREN";
905 break;
906
907 case CPP_CLOSE_PAREN:
908 token_type = "CLOSE_PAREN";
909 break;
910
911 case CPP_OPEN_BRACE:
912 token_type = "OPEN_BRACE";
913 break;
914
915 case CPP_CLOSE_BRACE:
916 token_type = "CLOSE_BRACE";
917 break;
918
919 case CPP_SEMICOLON:
920 token_type = "SEMICOLON";
921 break;
922
923 case CPP_NAME:
924 token_type = "NAME";
925 break;
926
927 case CPP_EOF:
928 token_type = "EOF";
929 break;
930
931 case CPP_KEYWORD:
932 token_type = "keyword";
933 break;
934
935 /* This is not a token that we know how to handle yet. */
936 default:
937 break;
938 }
939
940 /* If we have a name for the token, print it out. Otherwise, we
941 simply give the numeric code. */
942 if (token_type)
943 fprintf (stream, "%s", token_type);
944 else
945 fprintf (stream, "%d", token->type);
946 /* And, for an identifier, print the identifier name. */
947 if (token->type == CPP_NAME
948 /* Some keywords have a value that is not an IDENTIFIER_NODE.
949 For example, `struct' is mapped to an INTEGER_CST. */
950 || (token->type == CPP_KEYWORD
951 && TREE_CODE (token->value) == IDENTIFIER_NODE))
952 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
953}
954
a723baf1
MM
955/* Start emitting debugging information. */
956
957static void
94edc4ab 958cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
959{
960 ++lexer->debugging_p;
961}
962
963/* Stop emitting debugging information. */
964
965static void
94edc4ab 966cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
967{
968 --lexer->debugging_p;
969}
970
971\f
972/* The parser. */
973
974/* Overview
975 --------
976
977 A cp_parser parses the token stream as specified by the C++
978 grammar. Its job is purely parsing, not semantic analysis. For
979 example, the parser breaks the token stream into declarators,
980 expressions, statements, and other similar syntactic constructs.
981 It does not check that the types of the expressions on either side
982 of an assignment-statement are compatible, or that a function is
983 not declared with a parameter of type `void'.
984
985 The parser invokes routines elsewhere in the compiler to perform
986 semantic analysis and to build up the abstract syntax tree for the
987 code processed.
988
989 The parser (and the template instantiation code, which is, in a
990 way, a close relative of parsing) are the only parts of the
991 compiler that should be calling push_scope and pop_scope, or
992 related functions. The parser (and template instantiation code)
993 keeps track of what scope is presently active; everything else
994 should simply honor that. (The code that generates static
995 initializers may also need to set the scope, in order to check
996 access control correctly when emitting the initializers.)
997
998 Methodology
999 -----------
1000
1001 The parser is of the standard recursive-descent variety. Upcoming
1002 tokens in the token stream are examined in order to determine which
1003 production to use when parsing a non-terminal. Some C++ constructs
1004 require arbitrary look ahead to disambiguate. For example, it is
1005 impossible, in the general case, to tell whether a statement is an
1006 expression or declaration without scanning the entire statement.
1007 Therefore, the parser is capable of "parsing tentatively." When the
1008 parser is not sure what construct comes next, it enters this mode.
1009 Then, while we attempt to parse the construct, the parser queues up
1010 error messages, rather than issuing them immediately, and saves the
1011 tokens it consumes. If the construct is parsed successfully, the
1012 parser "commits", i.e., it issues any queued error messages and
1013 the tokens that were being preserved are permanently discarded.
1014 If, however, the construct is not parsed successfully, the parser
1015 rolls back its state completely so that it can resume parsing using
1016 a different alternative.
1017
1018 Future Improvements
1019 -------------------
1020
1021 The performance of the parser could probably be improved
1022 substantially. Some possible improvements include:
1023
1024 - The expression parser recurses through the various levels of
1025 precedence as specified in the grammar, rather than using an
1026 operator-precedence technique. Therefore, parsing a simple
1027 identifier requires multiple recursive calls.
1028
1029 - We could often eliminate the need to parse tentatively by
1030 looking ahead a little bit. In some places, this approach
1031 might not entirely eliminate the need to parse tentatively, but
1032 it might still speed up the average case. */
1033
1034/* Flags that are passed to some parsing functions. These values can
1035 be bitwise-ored together. */
1036
1037typedef enum cp_parser_flags
1038{
1039 /* No flags. */
1040 CP_PARSER_FLAGS_NONE = 0x0,
1041 /* The construct is optional. If it is not present, then no error
1042 should be issued. */
1043 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1044 /* When parsing a type-specifier, do not allow user-defined types. */
1045 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1046} cp_parser_flags;
1047
62b8a44e
NS
1048/* The different kinds of declarators we want to parse. */
1049
1050typedef enum cp_parser_declarator_kind
1051{
9bcb9aae 1052 /* We want an abstract declartor. */
62b8a44e
NS
1053 CP_PARSER_DECLARATOR_ABSTRACT,
1054 /* We want a named declarator. */
1055 CP_PARSER_DECLARATOR_NAMED,
712becab 1056 /* We don't mind, but the name must be an unqualified-id */
62b8a44e
NS
1057 CP_PARSER_DECLARATOR_EITHER
1058} cp_parser_declarator_kind;
1059
a723baf1
MM
1060/* A mapping from a token type to a corresponding tree node type. */
1061
1062typedef struct cp_parser_token_tree_map_node
1063{
1064 /* The token type. */
1065 enum cpp_ttype token_type;
1066 /* The corresponding tree code. */
1067 enum tree_code tree_type;
1068} cp_parser_token_tree_map_node;
1069
1070/* A complete map consists of several ordinary entries, followed by a
1071 terminator. The terminating entry has a token_type of CPP_EOF. */
1072
1073typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1074
1075/* The status of a tentative parse. */
1076
1077typedef enum cp_parser_status_kind
1078{
1079 /* No errors have occurred. */
1080 CP_PARSER_STATUS_KIND_NO_ERROR,
1081 /* An error has occurred. */
1082 CP_PARSER_STATUS_KIND_ERROR,
1083 /* We are committed to this tentative parse, whether or not an error
1084 has occurred. */
1085 CP_PARSER_STATUS_KIND_COMMITTED
1086} cp_parser_status_kind;
1087
1088/* Context that is saved and restored when parsing tentatively. */
1089
1090typedef struct cp_parser_context GTY (())
1091{
1092 /* If this is a tentative parsing context, the status of the
1093 tentative parse. */
1094 enum cp_parser_status_kind status;
1095 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1096 that are looked up in this context must be looked up both in the
1097 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1098 the context of the containing expression. */
1099 tree object_type;
a723baf1
MM
1100 /* The next parsing context in the stack. */
1101 struct cp_parser_context *next;
1102} cp_parser_context;
1103
1104/* Prototypes. */
1105
1106/* Constructors and destructors. */
1107
1108static cp_parser_context *cp_parser_context_new
94edc4ab 1109 (cp_parser_context *);
a723baf1 1110
e5976695
MM
1111/* Class variables. */
1112
92bc1323 1113static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1114
a723baf1
MM
1115/* Constructors and destructors. */
1116
1117/* Construct a new context. The context below this one on the stack
1118 is given by NEXT. */
1119
1120static cp_parser_context *
94edc4ab 1121cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1122{
1123 cp_parser_context *context;
1124
1125 /* Allocate the storage. */
e5976695
MM
1126 if (cp_parser_context_free_list != NULL)
1127 {
1128 /* Pull the first entry from the free list. */
1129 context = cp_parser_context_free_list;
1130 cp_parser_context_free_list = context->next;
c68b0a84 1131 memset (context, 0, sizeof (*context));
e5976695
MM
1132 }
1133 else
c68b0a84 1134 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1135 /* No errors have occurred yet in this context. */
1136 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1137 /* If this is not the bottomost context, copy information that we
1138 need from the previous context. */
1139 if (next)
1140 {
1141 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1142 expression, then we are parsing one in this context, too. */
1143 context->object_type = next->object_type;
a723baf1
MM
1144 /* Thread the stack. */
1145 context->next = next;
1146 }
1147
1148 return context;
1149}
1150
1151/* The cp_parser structure represents the C++ parser. */
1152
1153typedef struct cp_parser GTY(())
1154{
1155 /* The lexer from which we are obtaining tokens. */
1156 cp_lexer *lexer;
1157
1158 /* The scope in which names should be looked up. If NULL_TREE, then
1159 we look up names in the scope that is currently open in the
1160 source program. If non-NULL, this is either a TYPE or
1161 NAMESPACE_DECL for the scope in which we should look.
1162
1163 This value is not cleared automatically after a name is looked
1164 up, so we must be careful to clear it before starting a new look
1165 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1166 will look up `Z' in the scope of `X', rather than the current
1167 scope.) Unfortunately, it is difficult to tell when name lookup
1168 is complete, because we sometimes peek at a token, look it up,
1169 and then decide not to consume it. */
1170 tree scope;
1171
1172 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1173 last lookup took place. OBJECT_SCOPE is used if an expression
1174 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1175 respectively. QUALIFYING_SCOPE is used for an expression of the
1176 form "X::Y"; it refers to X. */
1177 tree object_scope;
1178 tree qualifying_scope;
1179
1180 /* A stack of parsing contexts. All but the bottom entry on the
1181 stack will be tentative contexts.
1182
1183 We parse tentatively in order to determine which construct is in
1184 use in some situations. For example, in order to determine
1185 whether a statement is an expression-statement or a
1186 declaration-statement we parse it tentatively as a
1187 declaration-statement. If that fails, we then reparse the same
1188 token stream as an expression-statement. */
1189 cp_parser_context *context;
1190
1191 /* True if we are parsing GNU C++. If this flag is not set, then
1192 GNU extensions are not recognized. */
1193 bool allow_gnu_extensions_p;
1194
1195 /* TRUE if the `>' token should be interpreted as the greater-than
1196 operator. FALSE if it is the end of a template-id or
1197 template-parameter-list. */
1198 bool greater_than_is_operator_p;
1199
1200 /* TRUE if default arguments are allowed within a parameter list
1201 that starts at this point. FALSE if only a gnu extension makes
1202 them permissable. */
1203 bool default_arg_ok_p;
1204
1205 /* TRUE if we are parsing an integral constant-expression. See
1206 [expr.const] for a precise definition. */
a723baf1
MM
1207 bool constant_expression_p;
1208
14d22dd6
MM
1209 /* TRUE if we are parsing an integral constant-expression -- but a
1210 non-constant expression should be permitted as well. This flag
1211 is used when parsing an array bound so that GNU variable-length
1212 arrays are tolerated. */
1213 bool allow_non_constant_expression_p;
1214
1215 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1216 been seen that makes the expression non-constant. */
1217 bool non_constant_expression_p;
1218
a723baf1
MM
1219 /* TRUE if local variable names and `this' are forbidden in the
1220 current context. */
1221 bool local_variables_forbidden_p;
1222
1223 /* TRUE if the declaration we are parsing is part of a
1224 linkage-specification of the form `extern string-literal
1225 declaration'. */
1226 bool in_unbraced_linkage_specification_p;
1227
1228 /* TRUE if we are presently parsing a declarator, after the
1229 direct-declarator. */
1230 bool in_declarator_p;
1231
1232 /* If non-NULL, then we are parsing a construct where new type
1233 definitions are not permitted. The string stored here will be
1234 issued as an error message if a type is defined. */
1235 const char *type_definition_forbidden_message;
1236
8db1028e
NS
1237 /* A list of lists. The outer list is a stack, used for member
1238 functions of local classes. At each level there are two sub-list,
1239 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1240 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1241 TREE_VALUE's. The functions are chained in reverse declaration
1242 order.
1243
1244 The TREE_PURPOSE sublist contains those functions with default
1245 arguments that need post processing, and the TREE_VALUE sublist
1246 contains those functions with definitions that need post
1247 processing.
1248
1249 These lists can only be processed once the outermost class being
9bcb9aae 1250 defined is complete. */
a723baf1
MM
1251 tree unparsed_functions_queues;
1252
1253 /* The number of classes whose definitions are currently in
1254 progress. */
1255 unsigned num_classes_being_defined;
1256
1257 /* The number of template parameter lists that apply directly to the
1258 current declaration. */
1259 unsigned num_template_parameter_lists;
1260} cp_parser;
1261
1262/* The type of a function that parses some kind of expression */
94edc4ab 1263typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1264
1265/* Prototypes. */
1266
1267/* Constructors and destructors. */
1268
1269static cp_parser *cp_parser_new
94edc4ab 1270 (void);
a723baf1
MM
1271
1272/* Routines to parse various constructs.
1273
1274 Those that return `tree' will return the error_mark_node (rather
1275 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1276 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1277 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1278 whether or not a parse error occurred, you should always use
1279 cp_parser_error_occurred. If the construct is optional (indicated
1280 either by an `_opt' in the name of the function that does the
1281 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1282 the construct is not present. */
1283
1284/* Lexical conventions [gram.lex] */
1285
1286static tree cp_parser_identifier
94edc4ab 1287 (cp_parser *);
a723baf1
MM
1288
1289/* Basic concepts [gram.basic] */
1290
1291static bool cp_parser_translation_unit
94edc4ab 1292 (cp_parser *);
a723baf1
MM
1293
1294/* Expressions [gram.expr] */
1295
1296static tree cp_parser_primary_expression
b3445994 1297 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1298static tree cp_parser_id_expression
94edc4ab 1299 (cp_parser *, bool, bool, bool *);
a723baf1 1300static tree cp_parser_unqualified_id
94edc4ab 1301 (cp_parser *, bool, bool);
a723baf1
MM
1302static tree cp_parser_nested_name_specifier_opt
1303 (cp_parser *, bool, bool, bool);
1304static tree cp_parser_nested_name_specifier
1305 (cp_parser *, bool, bool, bool);
1306static tree cp_parser_class_or_namespace_name
1307 (cp_parser *, bool, bool, bool, bool);
1308static tree cp_parser_postfix_expression
1309 (cp_parser *, bool);
7efa3e22 1310static tree cp_parser_parenthesized_expression_list
39703eb9 1311 (cp_parser *, bool, bool *);
a723baf1 1312static void cp_parser_pseudo_destructor_name
94edc4ab 1313 (cp_parser *, tree *, tree *);
a723baf1
MM
1314static tree cp_parser_unary_expression
1315 (cp_parser *, bool);
1316static enum tree_code cp_parser_unary_operator
94edc4ab 1317 (cp_token *);
a723baf1 1318static tree cp_parser_new_expression
94edc4ab 1319 (cp_parser *);
a723baf1 1320static tree cp_parser_new_placement
94edc4ab 1321 (cp_parser *);
a723baf1 1322static tree cp_parser_new_type_id
94edc4ab 1323 (cp_parser *);
a723baf1 1324static tree cp_parser_new_declarator_opt
94edc4ab 1325 (cp_parser *);
a723baf1 1326static tree cp_parser_direct_new_declarator
94edc4ab 1327 (cp_parser *);
a723baf1 1328static tree cp_parser_new_initializer
94edc4ab 1329 (cp_parser *);
a723baf1 1330static tree cp_parser_delete_expression
94edc4ab 1331 (cp_parser *);
a723baf1
MM
1332static tree cp_parser_cast_expression
1333 (cp_parser *, bool);
1334static tree cp_parser_pm_expression
94edc4ab 1335 (cp_parser *);
a723baf1 1336static tree cp_parser_multiplicative_expression
94edc4ab 1337 (cp_parser *);
a723baf1 1338static tree cp_parser_additive_expression
94edc4ab 1339 (cp_parser *);
a723baf1 1340static tree cp_parser_shift_expression
94edc4ab 1341 (cp_parser *);
a723baf1 1342static tree cp_parser_relational_expression
94edc4ab 1343 (cp_parser *);
a723baf1 1344static tree cp_parser_equality_expression
94edc4ab 1345 (cp_parser *);
a723baf1 1346static tree cp_parser_and_expression
94edc4ab 1347 (cp_parser *);
a723baf1 1348static tree cp_parser_exclusive_or_expression
94edc4ab 1349 (cp_parser *);
a723baf1 1350static tree cp_parser_inclusive_or_expression
94edc4ab 1351 (cp_parser *);
a723baf1 1352static tree cp_parser_logical_and_expression
94edc4ab 1353 (cp_parser *);
a723baf1 1354static tree cp_parser_logical_or_expression
94edc4ab 1355 (cp_parser *);
a723baf1 1356static tree cp_parser_question_colon_clause
94edc4ab 1357 (cp_parser *, tree);
a723baf1 1358static tree cp_parser_assignment_expression
94edc4ab 1359 (cp_parser *);
a723baf1 1360static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1361 (cp_parser *);
a723baf1 1362static tree cp_parser_expression
94edc4ab 1363 (cp_parser *);
a723baf1 1364static tree cp_parser_constant_expression
14d22dd6 1365 (cp_parser *, bool, bool *);
a723baf1
MM
1366
1367/* Statements [gram.stmt.stmt] */
1368
1369static void cp_parser_statement
a5bcc582 1370 (cp_parser *, bool);
a723baf1 1371static tree cp_parser_labeled_statement
a5bcc582 1372 (cp_parser *, bool);
a723baf1 1373static tree cp_parser_expression_statement
a5bcc582 1374 (cp_parser *, bool);
a723baf1 1375static tree cp_parser_compound_statement
a5bcc582 1376 (cp_parser *, bool);
a723baf1 1377static void cp_parser_statement_seq_opt
a5bcc582 1378 (cp_parser *, bool);
a723baf1 1379static tree cp_parser_selection_statement
94edc4ab 1380 (cp_parser *);
a723baf1 1381static tree cp_parser_condition
94edc4ab 1382 (cp_parser *);
a723baf1 1383static tree cp_parser_iteration_statement
94edc4ab 1384 (cp_parser *);
a723baf1 1385static void cp_parser_for_init_statement
94edc4ab 1386 (cp_parser *);
a723baf1 1387static tree cp_parser_jump_statement
94edc4ab 1388 (cp_parser *);
a723baf1 1389static void cp_parser_declaration_statement
94edc4ab 1390 (cp_parser *);
a723baf1
MM
1391
1392static tree cp_parser_implicitly_scoped_statement
94edc4ab 1393 (cp_parser *);
a723baf1 1394static void cp_parser_already_scoped_statement
94edc4ab 1395 (cp_parser *);
a723baf1
MM
1396
1397/* Declarations [gram.dcl.dcl] */
1398
1399static void cp_parser_declaration_seq_opt
94edc4ab 1400 (cp_parser *);
a723baf1 1401static void cp_parser_declaration
94edc4ab 1402 (cp_parser *);
a723baf1 1403static void cp_parser_block_declaration
94edc4ab 1404 (cp_parser *, bool);
a723baf1 1405static void cp_parser_simple_declaration
94edc4ab 1406 (cp_parser *, bool);
a723baf1 1407static tree cp_parser_decl_specifier_seq
94edc4ab 1408 (cp_parser *, cp_parser_flags, tree *, bool *);
a723baf1 1409static tree cp_parser_storage_class_specifier_opt
94edc4ab 1410 (cp_parser *);
a723baf1 1411static tree cp_parser_function_specifier_opt
94edc4ab 1412 (cp_parser *);
a723baf1 1413static tree cp_parser_type_specifier
94edc4ab 1414 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
a723baf1 1415static tree cp_parser_simple_type_specifier
94edc4ab 1416 (cp_parser *, cp_parser_flags);
a723baf1 1417static tree cp_parser_type_name
94edc4ab 1418 (cp_parser *);
a723baf1 1419static tree cp_parser_elaborated_type_specifier
94edc4ab 1420 (cp_parser *, bool, bool);
a723baf1 1421static tree cp_parser_enum_specifier
94edc4ab 1422 (cp_parser *);
a723baf1 1423static void cp_parser_enumerator_list
94edc4ab 1424 (cp_parser *, tree);
a723baf1 1425static void cp_parser_enumerator_definition
94edc4ab 1426 (cp_parser *, tree);
a723baf1 1427static tree cp_parser_namespace_name
94edc4ab 1428 (cp_parser *);
a723baf1 1429static void cp_parser_namespace_definition
94edc4ab 1430 (cp_parser *);
a723baf1 1431static void cp_parser_namespace_body
94edc4ab 1432 (cp_parser *);
a723baf1 1433static tree cp_parser_qualified_namespace_specifier
94edc4ab 1434 (cp_parser *);
a723baf1 1435static void cp_parser_namespace_alias_definition
94edc4ab 1436 (cp_parser *);
a723baf1 1437static void cp_parser_using_declaration
94edc4ab 1438 (cp_parser *);
a723baf1 1439static void cp_parser_using_directive
94edc4ab 1440 (cp_parser *);
a723baf1 1441static void cp_parser_asm_definition
94edc4ab 1442 (cp_parser *);
a723baf1 1443static void cp_parser_linkage_specification
94edc4ab 1444 (cp_parser *);
a723baf1
MM
1445
1446/* Declarators [gram.dcl.decl] */
1447
1448static tree cp_parser_init_declarator
94edc4ab 1449 (cp_parser *, tree, tree, bool, bool, bool *);
a723baf1 1450static tree cp_parser_declarator
7efa3e22 1451 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1452static tree cp_parser_direct_declarator
7efa3e22 1453 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1454static enum tree_code cp_parser_ptr_operator
94edc4ab 1455 (cp_parser *, tree *, tree *);
a723baf1 1456static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1457 (cp_parser *);
a723baf1 1458static tree cp_parser_cv_qualifier_opt
94edc4ab 1459 (cp_parser *);
a723baf1 1460static tree cp_parser_declarator_id
94edc4ab 1461 (cp_parser *);
a723baf1 1462static tree cp_parser_type_id
94edc4ab 1463 (cp_parser *);
a723baf1 1464static tree cp_parser_type_specifier_seq
94edc4ab 1465 (cp_parser *);
a723baf1 1466static tree cp_parser_parameter_declaration_clause
94edc4ab 1467 (cp_parser *);
a723baf1 1468static tree cp_parser_parameter_declaration_list
94edc4ab 1469 (cp_parser *);
a723baf1 1470static tree cp_parser_parameter_declaration
94edc4ab 1471 (cp_parser *, bool);
a723baf1 1472static tree cp_parser_function_definition
94edc4ab 1473 (cp_parser *, bool *);
a723baf1
MM
1474static void cp_parser_function_body
1475 (cp_parser *);
1476static tree cp_parser_initializer
39703eb9 1477 (cp_parser *, bool *, bool *);
a723baf1 1478static tree cp_parser_initializer_clause
39703eb9 1479 (cp_parser *, bool *);
a723baf1 1480static tree cp_parser_initializer_list
39703eb9 1481 (cp_parser *, bool *);
a723baf1
MM
1482
1483static bool cp_parser_ctor_initializer_opt_and_function_body
1484 (cp_parser *);
1485
1486/* Classes [gram.class] */
1487
1488static tree cp_parser_class_name
8d241e0b 1489 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1490static tree cp_parser_class_specifier
94edc4ab 1491 (cp_parser *);
a723baf1 1492static tree cp_parser_class_head
94edc4ab 1493 (cp_parser *, bool *);
a723baf1 1494static enum tag_types cp_parser_class_key
94edc4ab 1495 (cp_parser *);
a723baf1 1496static void cp_parser_member_specification_opt
94edc4ab 1497 (cp_parser *);
a723baf1 1498static void cp_parser_member_declaration
94edc4ab 1499 (cp_parser *);
a723baf1 1500static tree cp_parser_pure_specifier
94edc4ab 1501 (cp_parser *);
a723baf1 1502static tree cp_parser_constant_initializer
94edc4ab 1503 (cp_parser *);
a723baf1
MM
1504
1505/* Derived classes [gram.class.derived] */
1506
1507static tree cp_parser_base_clause
94edc4ab 1508 (cp_parser *);
a723baf1 1509static tree cp_parser_base_specifier
94edc4ab 1510 (cp_parser *);
a723baf1
MM
1511
1512/* Special member functions [gram.special] */
1513
1514static tree cp_parser_conversion_function_id
94edc4ab 1515 (cp_parser *);
a723baf1 1516static tree cp_parser_conversion_type_id
94edc4ab 1517 (cp_parser *);
a723baf1 1518static tree cp_parser_conversion_declarator_opt
94edc4ab 1519 (cp_parser *);
a723baf1 1520static bool cp_parser_ctor_initializer_opt
94edc4ab 1521 (cp_parser *);
a723baf1 1522static void cp_parser_mem_initializer_list
94edc4ab 1523 (cp_parser *);
a723baf1 1524static tree cp_parser_mem_initializer
94edc4ab 1525 (cp_parser *);
a723baf1 1526static tree cp_parser_mem_initializer_id
94edc4ab 1527 (cp_parser *);
a723baf1
MM
1528
1529/* Overloading [gram.over] */
1530
1531static tree cp_parser_operator_function_id
94edc4ab 1532 (cp_parser *);
a723baf1 1533static tree cp_parser_operator
94edc4ab 1534 (cp_parser *);
a723baf1
MM
1535
1536/* Templates [gram.temp] */
1537
1538static void cp_parser_template_declaration
94edc4ab 1539 (cp_parser *, bool);
a723baf1 1540static tree cp_parser_template_parameter_list
94edc4ab 1541 (cp_parser *);
a723baf1 1542static tree cp_parser_template_parameter
94edc4ab 1543 (cp_parser *);
a723baf1 1544static tree cp_parser_type_parameter
94edc4ab 1545 (cp_parser *);
a723baf1 1546static tree cp_parser_template_id
94edc4ab 1547 (cp_parser *, bool, bool);
a723baf1 1548static tree cp_parser_template_name
94edc4ab 1549 (cp_parser *, bool, bool);
a723baf1 1550static tree cp_parser_template_argument_list
94edc4ab 1551 (cp_parser *);
a723baf1 1552static tree cp_parser_template_argument
94edc4ab 1553 (cp_parser *);
a723baf1 1554static void cp_parser_explicit_instantiation
94edc4ab 1555 (cp_parser *);
a723baf1 1556static void cp_parser_explicit_specialization
94edc4ab 1557 (cp_parser *);
a723baf1
MM
1558
1559/* Exception handling [gram.exception] */
1560
1561static tree cp_parser_try_block
94edc4ab 1562 (cp_parser *);
a723baf1 1563static bool cp_parser_function_try_block
94edc4ab 1564 (cp_parser *);
a723baf1 1565static void cp_parser_handler_seq
94edc4ab 1566 (cp_parser *);
a723baf1 1567static void cp_parser_handler
94edc4ab 1568 (cp_parser *);
a723baf1 1569static tree cp_parser_exception_declaration
94edc4ab 1570 (cp_parser *);
a723baf1 1571static tree cp_parser_throw_expression
94edc4ab 1572 (cp_parser *);
a723baf1 1573static tree cp_parser_exception_specification_opt
94edc4ab 1574 (cp_parser *);
a723baf1 1575static tree cp_parser_type_id_list
94edc4ab 1576 (cp_parser *);
a723baf1
MM
1577
1578/* GNU Extensions */
1579
1580static tree cp_parser_asm_specification_opt
94edc4ab 1581 (cp_parser *);
a723baf1 1582static tree cp_parser_asm_operand_list
94edc4ab 1583 (cp_parser *);
a723baf1 1584static tree cp_parser_asm_clobber_list
94edc4ab 1585 (cp_parser *);
a723baf1 1586static tree cp_parser_attributes_opt
94edc4ab 1587 (cp_parser *);
a723baf1 1588static tree cp_parser_attribute_list
94edc4ab 1589 (cp_parser *);
a723baf1 1590static bool cp_parser_extension_opt
94edc4ab 1591 (cp_parser *, int *);
a723baf1 1592static void cp_parser_label_declaration
94edc4ab 1593 (cp_parser *);
a723baf1
MM
1594
1595/* Utility Routines */
1596
1597static tree cp_parser_lookup_name
8d241e0b 1598 (cp_parser *, tree, bool, bool, bool);
a723baf1 1599static tree cp_parser_lookup_name_simple
94edc4ab 1600 (cp_parser *, tree);
a723baf1
MM
1601static tree cp_parser_maybe_treat_template_as_class
1602 (tree, bool);
1603static bool cp_parser_check_declarator_template_parameters
94edc4ab 1604 (cp_parser *, tree);
a723baf1 1605static bool cp_parser_check_template_parameters
94edc4ab 1606 (cp_parser *, unsigned);
d6b4ea85
MM
1607static tree cp_parser_simple_cast_expression
1608 (cp_parser *);
a723baf1 1609static tree cp_parser_binary_expression
94edc4ab 1610 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1611static tree cp_parser_global_scope_opt
94edc4ab 1612 (cp_parser *, bool);
a723baf1
MM
1613static bool cp_parser_constructor_declarator_p
1614 (cp_parser *, bool);
1615static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1616 (cp_parser *, tree, tree, tree);
a723baf1 1617static tree cp_parser_function_definition_after_declarator
94edc4ab 1618 (cp_parser *, bool);
a723baf1 1619static void cp_parser_template_declaration_after_export
94edc4ab 1620 (cp_parser *, bool);
a723baf1 1621static tree cp_parser_single_declaration
94edc4ab 1622 (cp_parser *, bool, bool *);
a723baf1 1623static tree cp_parser_functional_cast
94edc4ab 1624 (cp_parser *, tree);
8db1028e
NS
1625static void cp_parser_save_default_args
1626 (cp_parser *, tree);
a723baf1 1627static void cp_parser_late_parsing_for_member
94edc4ab 1628 (cp_parser *, tree);
a723baf1 1629static void cp_parser_late_parsing_default_args
8218bd34 1630 (cp_parser *, tree);
a723baf1 1631static tree cp_parser_sizeof_operand
94edc4ab 1632 (cp_parser *, enum rid);
a723baf1 1633static bool cp_parser_declares_only_class_p
94edc4ab 1634 (cp_parser *);
d17811fd
MM
1635static tree cp_parser_fold_non_dependent_expr
1636 (tree);
a723baf1 1637static bool cp_parser_friend_p
94edc4ab 1638 (tree);
a723baf1 1639static cp_token *cp_parser_require
94edc4ab 1640 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1641static cp_token *cp_parser_require_keyword
94edc4ab 1642 (cp_parser *, enum rid, const char *);
a723baf1 1643static bool cp_parser_token_starts_function_definition_p
94edc4ab 1644 (cp_token *);
a723baf1
MM
1645static bool cp_parser_next_token_starts_class_definition_p
1646 (cp_parser *);
d17811fd
MM
1647static bool cp_parser_next_token_ends_template_argument_p
1648 (cp_parser *);
a723baf1 1649static enum tag_types cp_parser_token_is_class_key
94edc4ab 1650 (cp_token *);
a723baf1
MM
1651static void cp_parser_check_class_key
1652 (enum tag_types, tree type);
1653static bool cp_parser_optional_template_keyword
1654 (cp_parser *);
2050a1bb
MM
1655static void cp_parser_pre_parsed_nested_name_specifier
1656 (cp_parser *);
a723baf1
MM
1657static void cp_parser_cache_group
1658 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1659static void cp_parser_parse_tentatively
94edc4ab 1660 (cp_parser *);
a723baf1 1661static void cp_parser_commit_to_tentative_parse
94edc4ab 1662 (cp_parser *);
a723baf1 1663static void cp_parser_abort_tentative_parse
94edc4ab 1664 (cp_parser *);
a723baf1 1665static bool cp_parser_parse_definitely
94edc4ab 1666 (cp_parser *);
f7b5ecd9 1667static inline bool cp_parser_parsing_tentatively
94edc4ab 1668 (cp_parser *);
a723baf1 1669static bool cp_parser_committed_to_tentative_parse
94edc4ab 1670 (cp_parser *);
a723baf1 1671static void cp_parser_error
94edc4ab 1672 (cp_parser *, const char *);
e5976695 1673static bool cp_parser_simulate_error
94edc4ab 1674 (cp_parser *);
a723baf1 1675static void cp_parser_check_type_definition
94edc4ab 1676 (cp_parser *);
14d22dd6
MM
1677static tree cp_parser_non_constant_expression
1678 (const char *);
8fbc5ae7
MM
1679static bool cp_parser_diagnose_invalid_type_name
1680 (cp_parser *);
7efa3e22
NS
1681static int cp_parser_skip_to_closing_parenthesis
1682 (cp_parser *, bool, bool);
a723baf1 1683static void cp_parser_skip_to_end_of_statement
94edc4ab 1684 (cp_parser *);
e0860732
MM
1685static void cp_parser_consume_semicolon_at_end_of_statement
1686 (cp_parser *);
a723baf1 1687static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1688 (cp_parser *);
a723baf1
MM
1689static void cp_parser_skip_to_closing_brace
1690 (cp_parser *);
1691static void cp_parser_skip_until_found
94edc4ab 1692 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1693static bool cp_parser_error_occurred
94edc4ab 1694 (cp_parser *);
a723baf1 1695static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1696 (cp_parser *);
a723baf1 1697static bool cp_parser_is_string_literal
94edc4ab 1698 (cp_token *);
a723baf1 1699static bool cp_parser_is_keyword
94edc4ab 1700 (cp_token *, enum rid);
a723baf1 1701
4de8668e 1702/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1703
1704static inline bool
94edc4ab 1705cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1706{
1707 return parser->context->next != NULL;
1708}
1709
4de8668e 1710/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1711
1712static bool
94edc4ab 1713cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1714{
1715 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1716}
1717
4de8668e 1718/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1719
1720static bool
94edc4ab 1721cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1722{
1723 return token->keyword == keyword;
1724}
1725
a723baf1
MM
1726/* Issue the indicated error MESSAGE. */
1727
1728static void
94edc4ab 1729cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1730{
a723baf1 1731 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1732 if (!cp_parser_simulate_error (parser))
a723baf1
MM
1733 error (message);
1734}
1735
1736/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1737 during this tentative parse. Returns true if the error was
1738 simulated; false if a messgae should be issued by the caller. */
a723baf1 1739
e5976695 1740static bool
94edc4ab 1741cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1742{
1743 if (cp_parser_parsing_tentatively (parser)
1744 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1745 {
1746 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1747 return true;
1748 }
1749 return false;
a723baf1
MM
1750}
1751
1752/* This function is called when a type is defined. If type
1753 definitions are forbidden at this point, an error message is
1754 issued. */
1755
1756static void
94edc4ab 1757cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1758{
1759 /* If types are forbidden here, issue a message. */
1760 if (parser->type_definition_forbidden_message)
1761 /* Use `%s' to print the string in case there are any escape
1762 characters in the message. */
1763 error ("%s", parser->type_definition_forbidden_message);
1764}
1765
14d22dd6
MM
1766/* Issue an eror message about the fact that THING appeared in a
1767 constant-expression. Returns ERROR_MARK_NODE. */
1768
1769static tree
1770cp_parser_non_constant_expression (const char *thing)
1771{
1772 error ("%s cannot appear in a constant-expression", thing);
1773 return error_mark_node;
1774}
1775
8fbc5ae7
MM
1776/* Check for a common situation where a type-name should be present,
1777 but is not, and issue a sensible error message. Returns true if an
1778 invalid type-name was detected. */
1779
1780static bool
1781cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1782{
1783 /* If the next two tokens are both identifiers, the code is
1784 erroneous. The usual cause of this situation is code like:
1785
1786 T t;
1787
1788 where "T" should name a type -- but does not. */
1789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1790 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1791 {
1792 tree name;
1793
8d241e0b 1794 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1795 looking at a declaration. */
1796 /* Consume the first identifier. */
1797 name = cp_lexer_consume_token (parser->lexer)->value;
1798 /* Issue an error message. */
1799 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1800 /* If we're in a template class, it's possible that the user was
1801 referring to a type from a base class. For example:
1802
1803 template <typename T> struct A { typedef T X; };
1804 template <typename T> struct B : public A<T> { X x; };
1805
1806 The user should have said "typename A<T>::X". */
1807 if (processing_template_decl && current_class_type)
1808 {
1809 tree b;
1810
1811 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1812 b;
1813 b = TREE_CHAIN (b))
1814 {
1815 tree base_type = BINFO_TYPE (b);
1816 if (CLASS_TYPE_P (base_type)
1fb3244a 1817 && dependent_type_p (base_type))
8fbc5ae7
MM
1818 {
1819 tree field;
1820 /* Go from a particular instantiation of the
1821 template (which will have an empty TYPE_FIELDs),
1822 to the main version. */
353b4fc0 1823 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1824 for (field = TYPE_FIELDS (base_type);
1825 field;
1826 field = TREE_CHAIN (field))
1827 if (TREE_CODE (field) == TYPE_DECL
1828 && DECL_NAME (field) == name)
1829 {
1830 error ("(perhaps `typename %T::%s' was intended)",
1831 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1832 break;
1833 }
1834 if (field)
1835 break;
1836 }
1837 }
1838 }
1839 /* Skip to the end of the declaration; there's no point in
1840 trying to process it. */
1841 cp_parser_skip_to_end_of_statement (parser);
1842
1843 return true;
1844 }
1845
1846 return false;
1847}
1848
a723baf1 1849/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
1850 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1851 are doing error recovery. Returns -1 if OR_COMMA is true and we
1852 found an unnested comma. */
a723baf1 1853
7efa3e22
NS
1854static int
1855cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1856 bool recovering, bool or_comma)
a723baf1 1857{
7efa3e22
NS
1858 unsigned paren_depth = 0;
1859 unsigned brace_depth = 0;
a723baf1 1860
7efa3e22
NS
1861 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1862 && !cp_parser_committed_to_tentative_parse (parser))
1863 return 0;
1864
a723baf1
MM
1865 while (true)
1866 {
1867 cp_token *token;
7efa3e22 1868
a723baf1
MM
1869 /* If we've run out of tokens, then there is no closing `)'. */
1870 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 1871 return 0;
a723baf1 1872
7efa3e22
NS
1873 if (recovering)
1874 {
1875 token = cp_lexer_peek_token (parser->lexer);
a723baf1 1876
7efa3e22
NS
1877 /* This matches the processing in skip_to_end_of_statement */
1878 if (token->type == CPP_SEMICOLON && !brace_depth)
1879 return 0;
1880 if (token->type == CPP_OPEN_BRACE)
1881 ++brace_depth;
1882 if (token->type == CPP_CLOSE_BRACE)
1883 {
1884 if (!brace_depth--)
1885 return 0;
1886 }
1887 if (or_comma && token->type == CPP_COMMA
1888 && !brace_depth && !paren_depth)
1889 return -1;
1890 }
1891
a723baf1
MM
1892 /* Consume the token. */
1893 token = cp_lexer_consume_token (parser->lexer);
7efa3e22
NS
1894
1895 if (!brace_depth)
1896 {
1897 /* If it is an `(', we have entered another level of nesting. */
1898 if (token->type == CPP_OPEN_PAREN)
1899 ++paren_depth;
1900 /* If it is a `)', then we might be done. */
1901 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1902 return 1;
1903 }
a723baf1
MM
1904 }
1905}
1906
1907/* Consume tokens until we reach the end of the current statement.
1908 Normally, that will be just before consuming a `;'. However, if a
1909 non-nested `}' comes first, then we stop before consuming that. */
1910
1911static void
94edc4ab 1912cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
1913{
1914 unsigned nesting_depth = 0;
1915
1916 while (true)
1917 {
1918 cp_token *token;
1919
1920 /* Peek at the next token. */
1921 token = cp_lexer_peek_token (parser->lexer);
1922 /* If we've run out of tokens, stop. */
1923 if (token->type == CPP_EOF)
1924 break;
1925 /* If the next token is a `;', we have reached the end of the
1926 statement. */
1927 if (token->type == CPP_SEMICOLON && !nesting_depth)
1928 break;
1929 /* If the next token is a non-nested `}', then we have reached
1930 the end of the current block. */
1931 if (token->type == CPP_CLOSE_BRACE)
1932 {
1933 /* If this is a non-nested `}', stop before consuming it.
1934 That way, when confronted with something like:
1935
1936 { 3 + }
1937
1938 we stop before consuming the closing `}', even though we
1939 have not yet reached a `;'. */
1940 if (nesting_depth == 0)
1941 break;
1942 /* If it is the closing `}' for a block that we have
1943 scanned, stop -- but only after consuming the token.
1944 That way given:
1945
1946 void f g () { ... }
1947 typedef int I;
1948
1949 we will stop after the body of the erroneously declared
1950 function, but before consuming the following `typedef'
1951 declaration. */
1952 if (--nesting_depth == 0)
1953 {
1954 cp_lexer_consume_token (parser->lexer);
1955 break;
1956 }
1957 }
1958 /* If it the next token is a `{', then we are entering a new
1959 block. Consume the entire block. */
1960 else if (token->type == CPP_OPEN_BRACE)
1961 ++nesting_depth;
1962 /* Consume the token. */
1963 cp_lexer_consume_token (parser->lexer);
1964 }
1965}
1966
e0860732
MM
1967/* This function is called at the end of a statement or declaration.
1968 If the next token is a semicolon, it is consumed; otherwise, error
1969 recovery is attempted. */
1970
1971static void
1972cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1973{
1974 /* Look for the trailing `;'. */
1975 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
1976 {
1977 /* If there is additional (erroneous) input, skip to the end of
1978 the statement. */
1979 cp_parser_skip_to_end_of_statement (parser);
1980 /* If the next token is now a `;', consume it. */
1981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
1982 cp_lexer_consume_token (parser->lexer);
1983 }
1984}
1985
a723baf1
MM
1986/* Skip tokens until we have consumed an entire block, or until we
1987 have consumed a non-nested `;'. */
1988
1989static void
94edc4ab 1990cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
1991{
1992 unsigned nesting_depth = 0;
1993
1994 while (true)
1995 {
1996 cp_token *token;
1997
1998 /* Peek at the next token. */
1999 token = cp_lexer_peek_token (parser->lexer);
2000 /* If we've run out of tokens, stop. */
2001 if (token->type == CPP_EOF)
2002 break;
2003 /* If the next token is a `;', we have reached the end of the
2004 statement. */
2005 if (token->type == CPP_SEMICOLON && !nesting_depth)
2006 {
2007 /* Consume the `;'. */
2008 cp_lexer_consume_token (parser->lexer);
2009 break;
2010 }
2011 /* Consume the token. */
2012 token = cp_lexer_consume_token (parser->lexer);
2013 /* If the next token is a non-nested `}', then we have reached
2014 the end of the current block. */
2015 if (token->type == CPP_CLOSE_BRACE
2016 && (nesting_depth == 0 || --nesting_depth == 0))
2017 break;
2018 /* If it the next token is a `{', then we are entering a new
2019 block. Consume the entire block. */
2020 if (token->type == CPP_OPEN_BRACE)
2021 ++nesting_depth;
2022 }
2023}
2024
2025/* Skip tokens until a non-nested closing curly brace is the next
2026 token. */
2027
2028static void
2029cp_parser_skip_to_closing_brace (cp_parser *parser)
2030{
2031 unsigned nesting_depth = 0;
2032
2033 while (true)
2034 {
2035 cp_token *token;
2036
2037 /* Peek at the next token. */
2038 token = cp_lexer_peek_token (parser->lexer);
2039 /* If we've run out of tokens, stop. */
2040 if (token->type == CPP_EOF)
2041 break;
2042 /* If the next token is a non-nested `}', then we have reached
2043 the end of the current block. */
2044 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2045 break;
2046 /* If it the next token is a `{', then we are entering a new
2047 block. Consume the entire block. */
2048 else if (token->type == CPP_OPEN_BRACE)
2049 ++nesting_depth;
2050 /* Consume the token. */
2051 cp_lexer_consume_token (parser->lexer);
2052 }
2053}
2054
2055/* Create a new C++ parser. */
2056
2057static cp_parser *
94edc4ab 2058cp_parser_new (void)
a723baf1
MM
2059{
2060 cp_parser *parser;
17211ab5
GK
2061 cp_lexer *lexer;
2062
2063 /* cp_lexer_new_main is called before calling ggc_alloc because
2064 cp_lexer_new_main might load a PCH file. */
2065 lexer = cp_lexer_new_main ();
a723baf1 2066
c68b0a84 2067 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2068 parser->lexer = lexer;
a723baf1
MM
2069 parser->context = cp_parser_context_new (NULL);
2070
2071 /* For now, we always accept GNU extensions. */
2072 parser->allow_gnu_extensions_p = 1;
2073
2074 /* The `>' token is a greater-than operator, not the end of a
2075 template-id. */
2076 parser->greater_than_is_operator_p = true;
2077
2078 parser->default_arg_ok_p = true;
2079
2080 /* We are not parsing a constant-expression. */
2081 parser->constant_expression_p = false;
14d22dd6
MM
2082 parser->allow_non_constant_expression_p = false;
2083 parser->non_constant_expression_p = false;
a723baf1
MM
2084
2085 /* Local variable names are not forbidden. */
2086 parser->local_variables_forbidden_p = false;
2087
34cd5ae7 2088 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2089 parser->in_unbraced_linkage_specification_p = false;
2090
2091 /* We are not processing a declarator. */
2092 parser->in_declarator_p = false;
2093
a723baf1
MM
2094 /* The unparsed function queue is empty. */
2095 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2096
2097 /* There are no classes being defined. */
2098 parser->num_classes_being_defined = 0;
2099
2100 /* No template parameters apply. */
2101 parser->num_template_parameter_lists = 0;
2102
2103 return parser;
2104}
2105
2106/* Lexical conventions [gram.lex] */
2107
2108/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2109 identifier. */
2110
2111static tree
94edc4ab 2112cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2113{
2114 cp_token *token;
2115
2116 /* Look for the identifier. */
2117 token = cp_parser_require (parser, CPP_NAME, "identifier");
2118 /* Return the value. */
2119 return token ? token->value : error_mark_node;
2120}
2121
2122/* Basic concepts [gram.basic] */
2123
2124/* Parse a translation-unit.
2125
2126 translation-unit:
2127 declaration-seq [opt]
2128
2129 Returns TRUE if all went well. */
2130
2131static bool
94edc4ab 2132cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2133{
2134 while (true)
2135 {
2136 cp_parser_declaration_seq_opt (parser);
2137
2138 /* If there are no tokens left then all went well. */
2139 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2140 break;
2141
2142 /* Otherwise, issue an error message. */
2143 cp_parser_error (parser, "expected declaration");
2144 return false;
2145 }
2146
2147 /* Consume the EOF token. */
2148 cp_parser_require (parser, CPP_EOF, "end-of-file");
2149
2150 /* Finish up. */
2151 finish_translation_unit ();
2152
2153 /* All went well. */
2154 return true;
2155}
2156
2157/* Expressions [gram.expr] */
2158
2159/* Parse a primary-expression.
2160
2161 primary-expression:
2162 literal
2163 this
2164 ( expression )
2165 id-expression
2166
2167 GNU Extensions:
2168
2169 primary-expression:
2170 ( compound-statement )
2171 __builtin_va_arg ( assignment-expression , type-id )
2172
2173 literal:
2174 __null
2175
2176 Returns a representation of the expression.
2177
2178 *IDK indicates what kind of id-expression (if any) was present.
2179
2180 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2181 used as the operand of a pointer-to-member. In that case,
2182 *QUALIFYING_CLASS gives the class that is used as the qualifying
2183 class in the pointer-to-member. */
2184
2185static tree
2186cp_parser_primary_expression (cp_parser *parser,
b3445994 2187 cp_id_kind *idk,
a723baf1
MM
2188 tree *qualifying_class)
2189{
2190 cp_token *token;
2191
2192 /* Assume the primary expression is not an id-expression. */
b3445994 2193 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2194 /* And that it cannot be used as pointer-to-member. */
2195 *qualifying_class = NULL_TREE;
2196
2197 /* Peek at the next token. */
2198 token = cp_lexer_peek_token (parser->lexer);
2199 switch (token->type)
2200 {
2201 /* literal:
2202 integer-literal
2203 character-literal
2204 floating-literal
2205 string-literal
2206 boolean-literal */
2207 case CPP_CHAR:
2208 case CPP_WCHAR:
2209 case CPP_STRING:
2210 case CPP_WSTRING:
2211 case CPP_NUMBER:
2212 token = cp_lexer_consume_token (parser->lexer);
2213 return token->value;
2214
2215 case CPP_OPEN_PAREN:
2216 {
2217 tree expr;
2218 bool saved_greater_than_is_operator_p;
2219
2220 /* Consume the `('. */
2221 cp_lexer_consume_token (parser->lexer);
2222 /* Within a parenthesized expression, a `>' token is always
2223 the greater-than operator. */
2224 saved_greater_than_is_operator_p
2225 = parser->greater_than_is_operator_p;
2226 parser->greater_than_is_operator_p = true;
2227 /* If we see `( { ' then we are looking at the beginning of
2228 a GNU statement-expression. */
2229 if (cp_parser_allow_gnu_extensions_p (parser)
2230 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2231 {
2232 /* Statement-expressions are not allowed by the standard. */
2233 if (pedantic)
2234 pedwarn ("ISO C++ forbids braced-groups within expressions");
2235
2236 /* And they're not allowed outside of a function-body; you
2237 cannot, for example, write:
2238
2239 int i = ({ int j = 3; j + 1; });
2240
2241 at class or namespace scope. */
2242 if (!at_function_scope_p ())
2243 error ("statement-expressions are allowed only inside functions");
2244 /* Start the statement-expression. */
2245 expr = begin_stmt_expr ();
2246 /* Parse the compound-statement. */
a5bcc582 2247 cp_parser_compound_statement (parser, true);
a723baf1
MM
2248 /* Finish up. */
2249 expr = finish_stmt_expr (expr);
2250 }
2251 else
2252 {
2253 /* Parse the parenthesized expression. */
2254 expr = cp_parser_expression (parser);
2255 /* Let the front end know that this expression was
2256 enclosed in parentheses. This matters in case, for
2257 example, the expression is of the form `A::B', since
2258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2259 not. */
2260 finish_parenthesized_expr (expr);
2261 }
2262 /* The `>' token might be the end of a template-id or
2263 template-parameter-list now. */
2264 parser->greater_than_is_operator_p
2265 = saved_greater_than_is_operator_p;
2266 /* Consume the `)'. */
2267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2268 cp_parser_skip_to_end_of_statement (parser);
2269
2270 return expr;
2271 }
2272
2273 case CPP_KEYWORD:
2274 switch (token->keyword)
2275 {
2276 /* These two are the boolean literals. */
2277 case RID_TRUE:
2278 cp_lexer_consume_token (parser->lexer);
2279 return boolean_true_node;
2280 case RID_FALSE:
2281 cp_lexer_consume_token (parser->lexer);
2282 return boolean_false_node;
2283
2284 /* The `__null' literal. */
2285 case RID_NULL:
2286 cp_lexer_consume_token (parser->lexer);
2287 return null_node;
2288
2289 /* Recognize the `this' keyword. */
2290 case RID_THIS:
2291 cp_lexer_consume_token (parser->lexer);
2292 if (parser->local_variables_forbidden_p)
2293 {
2294 error ("`this' may not be used in this context");
2295 return error_mark_node;
2296 }
14d22dd6
MM
2297 /* Pointers cannot appear in constant-expressions. */
2298 if (parser->constant_expression_p)
2299 {
2300 if (!parser->allow_non_constant_expression_p)
2301 return cp_parser_non_constant_expression ("`this'");
2302 parser->non_constant_expression_p = true;
2303 }
a723baf1
MM
2304 return finish_this_expr ();
2305
2306 /* The `operator' keyword can be the beginning of an
2307 id-expression. */
2308 case RID_OPERATOR:
2309 goto id_expression;
2310
2311 case RID_FUNCTION_NAME:
2312 case RID_PRETTY_FUNCTION_NAME:
2313 case RID_C99_FUNCTION_NAME:
2314 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2315 __func__ are the names of variables -- but they are
2316 treated specially. Therefore, they are handled here,
2317 rather than relying on the generic id-expression logic
34cd5ae7 2318 below. Grammatically, these names are id-expressions.
a723baf1
MM
2319
2320 Consume the token. */
2321 token = cp_lexer_consume_token (parser->lexer);
2322 /* Look up the name. */
2323 return finish_fname (token->value);
2324
2325 case RID_VA_ARG:
2326 {
2327 tree expression;
2328 tree type;
2329
2330 /* The `__builtin_va_arg' construct is used to handle
2331 `va_arg'. Consume the `__builtin_va_arg' token. */
2332 cp_lexer_consume_token (parser->lexer);
2333 /* Look for the opening `('. */
2334 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2335 /* Now, parse the assignment-expression. */
2336 expression = cp_parser_assignment_expression (parser);
2337 /* Look for the `,'. */
2338 cp_parser_require (parser, CPP_COMMA, "`,'");
2339 /* Parse the type-id. */
2340 type = cp_parser_type_id (parser);
2341 /* Look for the closing `)'. */
2342 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2343 /* Using `va_arg' in a constant-expression is not
2344 allowed. */
2345 if (parser->constant_expression_p)
2346 {
2347 if (!parser->allow_non_constant_expression_p)
2348 return cp_parser_non_constant_expression ("`va_arg'");
2349 parser->non_constant_expression_p = true;
2350 }
a723baf1
MM
2351 return build_x_va_arg (expression, type);
2352 }
2353
2354 default:
2355 cp_parser_error (parser, "expected primary-expression");
2356 return error_mark_node;
2357 }
a723baf1
MM
2358
2359 /* An id-expression can start with either an identifier, a
2360 `::' as the beginning of a qualified-id, or the "operator"
2361 keyword. */
2362 case CPP_NAME:
2363 case CPP_SCOPE:
2364 case CPP_TEMPLATE_ID:
2365 case CPP_NESTED_NAME_SPECIFIER:
2366 {
2367 tree id_expression;
2368 tree decl;
b3445994 2369 const char *error_msg;
a723baf1
MM
2370
2371 id_expression:
2372 /* Parse the id-expression. */
2373 id_expression
2374 = cp_parser_id_expression (parser,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL);
2378 if (id_expression == error_mark_node)
2379 return error_mark_node;
2380 /* If we have a template-id, then no further lookup is
2381 required. If the template-id was for a template-class, we
2382 will sometimes have a TYPE_DECL at this point. */
2383 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2384 || TREE_CODE (id_expression) == TYPE_DECL)
2385 decl = id_expression;
2386 /* Look up the name. */
2387 else
2388 {
2389 decl = cp_parser_lookup_name_simple (parser, id_expression);
2390 /* If name lookup gives us a SCOPE_REF, then the
2391 qualifying scope was dependent. Just propagate the
2392 name. */
2393 if (TREE_CODE (decl) == SCOPE_REF)
2394 {
2395 if (TYPE_P (TREE_OPERAND (decl, 0)))
2396 *qualifying_class = TREE_OPERAND (decl, 0);
2397 return decl;
2398 }
2399 /* Check to see if DECL is a local variable in a context
2400 where that is forbidden. */
2401 if (parser->local_variables_forbidden_p
2402 && local_variable_p (decl))
2403 {
2404 /* It might be that we only found DECL because we are
2405 trying to be generous with pre-ISO scoping rules.
2406 For example, consider:
2407
2408 int i;
2409 void g() {
2410 for (int i = 0; i < 10; ++i) {}
2411 extern void f(int j = i);
2412 }
2413
2414 Here, name look up will originally find the out
2415 of scope `i'. We need to issue a warning message,
2416 but then use the global `i'. */
2417 decl = check_for_out_of_scope_variable (decl);
2418 if (local_variable_p (decl))
2419 {
2420 error ("local variable `%D' may not appear in this context",
2421 decl);
2422 return error_mark_node;
2423 }
2424 }
c006d942 2425 }
b3445994
MM
2426
2427 decl = finish_id_expression (id_expression, decl, parser->scope,
2428 idk, qualifying_class,
2429 parser->constant_expression_p,
2430 parser->allow_non_constant_expression_p,
2431 &parser->non_constant_expression_p,
2432 &error_msg);
2433 if (error_msg)
2434 cp_parser_error (parser, error_msg);
a723baf1
MM
2435 return decl;
2436 }
2437
2438 /* Anything else is an error. */
2439 default:
2440 cp_parser_error (parser, "expected primary-expression");
2441 return error_mark_node;
2442 }
2443}
2444
2445/* Parse an id-expression.
2446
2447 id-expression:
2448 unqualified-id
2449 qualified-id
2450
2451 qualified-id:
2452 :: [opt] nested-name-specifier template [opt] unqualified-id
2453 :: identifier
2454 :: operator-function-id
2455 :: template-id
2456
2457 Return a representation of the unqualified portion of the
2458 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2459 a `::' or nested-name-specifier.
2460
2461 Often, if the id-expression was a qualified-id, the caller will
2462 want to make a SCOPE_REF to represent the qualified-id. This
2463 function does not do this in order to avoid wastefully creating
2464 SCOPE_REFs when they are not required.
2465
a723baf1
MM
2466 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2467 `template' keyword.
2468
2469 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2470 uninstantiated templates.
2471
15d2cb19 2472 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1
MM
2473 `template' keyword is used to explicitly indicate that the entity
2474 named is a template. */
2475
2476static tree
2477cp_parser_id_expression (cp_parser *parser,
2478 bool template_keyword_p,
2479 bool check_dependency_p,
2480 bool *template_p)
2481{
2482 bool global_scope_p;
2483 bool nested_name_specifier_p;
2484
2485 /* Assume the `template' keyword was not used. */
2486 if (template_p)
2487 *template_p = false;
2488
2489 /* Look for the optional `::' operator. */
2490 global_scope_p
2491 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2492 != NULL_TREE);
2493 /* Look for the optional nested-name-specifier. */
2494 nested_name_specifier_p
2495 = (cp_parser_nested_name_specifier_opt (parser,
2496 /*typename_keyword_p=*/false,
2497 check_dependency_p,
2498 /*type_p=*/false)
2499 != NULL_TREE);
2500 /* If there is a nested-name-specifier, then we are looking at
2501 the first qualified-id production. */
2502 if (nested_name_specifier_p)
2503 {
2504 tree saved_scope;
2505 tree saved_object_scope;
2506 tree saved_qualifying_scope;
2507 tree unqualified_id;
2508 bool is_template;
2509
2510 /* See if the next token is the `template' keyword. */
2511 if (!template_p)
2512 template_p = &is_template;
2513 *template_p = cp_parser_optional_template_keyword (parser);
2514 /* Name lookup we do during the processing of the
2515 unqualified-id might obliterate SCOPE. */
2516 saved_scope = parser->scope;
2517 saved_object_scope = parser->object_scope;
2518 saved_qualifying_scope = parser->qualifying_scope;
2519 /* Process the final unqualified-id. */
2520 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2521 check_dependency_p);
2522 /* Restore the SAVED_SCOPE for our caller. */
2523 parser->scope = saved_scope;
2524 parser->object_scope = saved_object_scope;
2525 parser->qualifying_scope = saved_qualifying_scope;
2526
2527 return unqualified_id;
2528 }
2529 /* Otherwise, if we are in global scope, then we are looking at one
2530 of the other qualified-id productions. */
2531 else if (global_scope_p)
2532 {
2533 cp_token *token;
2534 tree id;
2535
e5976695
MM
2536 /* Peek at the next token. */
2537 token = cp_lexer_peek_token (parser->lexer);
2538
2539 /* If it's an identifier, and the next token is not a "<", then
2540 we can avoid the template-id case. This is an optimization
2541 for this common case. */
2542 if (token->type == CPP_NAME
2543 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2544 return cp_parser_identifier (parser);
2545
a723baf1
MM
2546 cp_parser_parse_tentatively (parser);
2547 /* Try a template-id. */
2548 id = cp_parser_template_id (parser,
2549 /*template_keyword_p=*/false,
2550 /*check_dependency_p=*/true);
2551 /* If that worked, we're done. */
2552 if (cp_parser_parse_definitely (parser))
2553 return id;
2554
e5976695
MM
2555 /* Peek at the next token. (Changes in the token buffer may
2556 have invalidated the pointer obtained above.) */
a723baf1
MM
2557 token = cp_lexer_peek_token (parser->lexer);
2558
2559 switch (token->type)
2560 {
2561 case CPP_NAME:
2562 return cp_parser_identifier (parser);
2563
2564 case CPP_KEYWORD:
2565 if (token->keyword == RID_OPERATOR)
2566 return cp_parser_operator_function_id (parser);
2567 /* Fall through. */
2568
2569 default:
2570 cp_parser_error (parser, "expected id-expression");
2571 return error_mark_node;
2572 }
2573 }
2574 else
2575 return cp_parser_unqualified_id (parser, template_keyword_p,
2576 /*check_dependency_p=*/true);
2577}
2578
2579/* Parse an unqualified-id.
2580
2581 unqualified-id:
2582 identifier
2583 operator-function-id
2584 conversion-function-id
2585 ~ class-name
2586 template-id
2587
2588 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2589 keyword, in a construct like `A::template ...'.
2590
2591 Returns a representation of unqualified-id. For the `identifier'
2592 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2593 production a BIT_NOT_EXPR is returned; the operand of the
2594 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2595 other productions, see the documentation accompanying the
2596 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2597 names are looked up in uninstantiated templates. */
2598
2599static tree
94edc4ab
NN
2600cp_parser_unqualified_id (cp_parser* parser,
2601 bool template_keyword_p,
2602 bool check_dependency_p)
a723baf1
MM
2603{
2604 cp_token *token;
2605
2606 /* Peek at the next token. */
2607 token = cp_lexer_peek_token (parser->lexer);
2608
2609 switch (token->type)
2610 {
2611 case CPP_NAME:
2612 {
2613 tree id;
2614
2615 /* We don't know yet whether or not this will be a
2616 template-id. */
2617 cp_parser_parse_tentatively (parser);
2618 /* Try a template-id. */
2619 id = cp_parser_template_id (parser, template_keyword_p,
2620 check_dependency_p);
2621 /* If it worked, we're done. */
2622 if (cp_parser_parse_definitely (parser))
2623 return id;
2624 /* Otherwise, it's an ordinary identifier. */
2625 return cp_parser_identifier (parser);
2626 }
2627
2628 case CPP_TEMPLATE_ID:
2629 return cp_parser_template_id (parser, template_keyword_p,
2630 check_dependency_p);
2631
2632 case CPP_COMPL:
2633 {
2634 tree type_decl;
2635 tree qualifying_scope;
2636 tree object_scope;
2637 tree scope;
2638
2639 /* Consume the `~' token. */
2640 cp_lexer_consume_token (parser->lexer);
2641 /* Parse the class-name. The standard, as written, seems to
2642 say that:
2643
2644 template <typename T> struct S { ~S (); };
2645 template <typename T> S<T>::~S() {}
2646
2647 is invalid, since `~' must be followed by a class-name, but
2648 `S<T>' is dependent, and so not known to be a class.
2649 That's not right; we need to look in uninstantiated
2650 templates. A further complication arises from:
2651
2652 template <typename T> void f(T t) {
2653 t.T::~T();
2654 }
2655
2656 Here, it is not possible to look up `T' in the scope of `T'
2657 itself. We must look in both the current scope, and the
2658 scope of the containing complete expression.
2659
2660 Yet another issue is:
2661
2662 struct S {
2663 int S;
2664 ~S();
2665 };
2666
2667 S::~S() {}
2668
2669 The standard does not seem to say that the `S' in `~S'
2670 should refer to the type `S' and not the data member
2671 `S::S'. */
2672
2673 /* DR 244 says that we look up the name after the "~" in the
2674 same scope as we looked up the qualifying name. That idea
2675 isn't fully worked out; it's more complicated than that. */
2676 scope = parser->scope;
2677 object_scope = parser->object_scope;
2678 qualifying_scope = parser->qualifying_scope;
2679
2680 /* If the name is of the form "X::~X" it's OK. */
2681 if (scope && TYPE_P (scope)
2682 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2683 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2684 == CPP_OPEN_PAREN)
2685 && (cp_lexer_peek_token (parser->lexer)->value
2686 == TYPE_IDENTIFIER (scope)))
2687 {
2688 cp_lexer_consume_token (parser->lexer);
2689 return build_nt (BIT_NOT_EXPR, scope);
2690 }
2691
2692 /* If there was an explicit qualification (S::~T), first look
2693 in the scope given by the qualification (i.e., S). */
2694 if (scope)
2695 {
2696 cp_parser_parse_tentatively (parser);
2697 type_decl = cp_parser_class_name (parser,
2698 /*typename_keyword_p=*/false,
2699 /*template_keyword_p=*/false,
2700 /*type_p=*/false,
a723baf1
MM
2701 /*check_dependency=*/false,
2702 /*class_head_p=*/false);
2703 if (cp_parser_parse_definitely (parser))
2704 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2705 }
2706 /* In "N::S::~S", look in "N" as well. */
2707 if (scope && qualifying_scope)
2708 {
2709 cp_parser_parse_tentatively (parser);
2710 parser->scope = qualifying_scope;
2711 parser->object_scope = NULL_TREE;
2712 parser->qualifying_scope = NULL_TREE;
2713 type_decl
2714 = cp_parser_class_name (parser,
2715 /*typename_keyword_p=*/false,
2716 /*template_keyword_p=*/false,
2717 /*type_p=*/false,
a723baf1
MM
2718 /*check_dependency=*/false,
2719 /*class_head_p=*/false);
2720 if (cp_parser_parse_definitely (parser))
2721 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2722 }
2723 /* In "p->S::~T", look in the scope given by "*p" as well. */
2724 else if (object_scope)
2725 {
2726 cp_parser_parse_tentatively (parser);
2727 parser->scope = object_scope;
2728 parser->object_scope = NULL_TREE;
2729 parser->qualifying_scope = NULL_TREE;
2730 type_decl
2731 = cp_parser_class_name (parser,
2732 /*typename_keyword_p=*/false,
2733 /*template_keyword_p=*/false,
2734 /*type_p=*/false,
a723baf1
MM
2735 /*check_dependency=*/false,
2736 /*class_head_p=*/false);
2737 if (cp_parser_parse_definitely (parser))
2738 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2739 }
2740 /* Look in the surrounding context. */
2741 parser->scope = NULL_TREE;
2742 parser->object_scope = NULL_TREE;
2743 parser->qualifying_scope = NULL_TREE;
2744 type_decl
2745 = cp_parser_class_name (parser,
2746 /*typename_keyword_p=*/false,
2747 /*template_keyword_p=*/false,
2748 /*type_p=*/false,
a723baf1
MM
2749 /*check_dependency=*/false,
2750 /*class_head_p=*/false);
2751 /* If an error occurred, assume that the name of the
2752 destructor is the same as the name of the qualifying
2753 class. That allows us to keep parsing after running
2754 into ill-formed destructor names. */
2755 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2756 return build_nt (BIT_NOT_EXPR, scope);
2757 else if (type_decl == error_mark_node)
2758 return error_mark_node;
2759
2760 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2761 }
2762
2763 case CPP_KEYWORD:
2764 if (token->keyword == RID_OPERATOR)
2765 {
2766 tree id;
2767
2768 /* This could be a template-id, so we try that first. */
2769 cp_parser_parse_tentatively (parser);
2770 /* Try a template-id. */
2771 id = cp_parser_template_id (parser, template_keyword_p,
2772 /*check_dependency_p=*/true);
2773 /* If that worked, we're done. */
2774 if (cp_parser_parse_definitely (parser))
2775 return id;
2776 /* We still don't know whether we're looking at an
2777 operator-function-id or a conversion-function-id. */
2778 cp_parser_parse_tentatively (parser);
2779 /* Try an operator-function-id. */
2780 id = cp_parser_operator_function_id (parser);
2781 /* If that didn't work, try a conversion-function-id. */
2782 if (!cp_parser_parse_definitely (parser))
2783 id = cp_parser_conversion_function_id (parser);
2784
2785 return id;
2786 }
2787 /* Fall through. */
2788
2789 default:
2790 cp_parser_error (parser, "expected unqualified-id");
2791 return error_mark_node;
2792 }
2793}
2794
2795/* Parse an (optional) nested-name-specifier.
2796
2797 nested-name-specifier:
2798 class-or-namespace-name :: nested-name-specifier [opt]
2799 class-or-namespace-name :: template nested-name-specifier [opt]
2800
2801 PARSER->SCOPE should be set appropriately before this function is
2802 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2803 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2804 in name lookups.
2805
2806 Sets PARSER->SCOPE to the class (TYPE) or namespace
2807 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2808 it unchanged if there is no nested-name-specifier. Returns the new
2809 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2810
2811static tree
2812cp_parser_nested_name_specifier_opt (cp_parser *parser,
2813 bool typename_keyword_p,
2814 bool check_dependency_p,
2815 bool type_p)
2816{
2817 bool success = false;
2818 tree access_check = NULL_TREE;
2819 ptrdiff_t start;
2050a1bb 2820 cp_token* token;
a723baf1
MM
2821
2822 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
2823 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2824 false, it may have been true before, in which case something
2825 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2826 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2827 CHECK_DEPENDENCY_P is false, we have to fall through into the
2828 main loop. */
2829 if (check_dependency_p
2830 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2831 {
2832 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
2833 return parser->scope;
2834 }
2835
2836 /* Remember where the nested-name-specifier starts. */
2837 if (cp_parser_parsing_tentatively (parser)
2838 && !cp_parser_committed_to_tentative_parse (parser))
2839 {
2050a1bb 2840 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
2841 start = cp_lexer_token_difference (parser->lexer,
2842 parser->lexer->first_token,
2050a1bb 2843 token);
a723baf1
MM
2844 }
2845 else
2846 start = -1;
2847
8d241e0b 2848 push_deferring_access_checks (dk_deferred);
cf22909c 2849
a723baf1
MM
2850 while (true)
2851 {
2852 tree new_scope;
2853 tree old_scope;
2854 tree saved_qualifying_scope;
a723baf1
MM
2855 bool template_keyword_p;
2856
2050a1bb
MM
2857 /* Spot cases that cannot be the beginning of a
2858 nested-name-specifier. */
2859 token = cp_lexer_peek_token (parser->lexer);
2860
2861 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2862 the already parsed nested-name-specifier. */
2863 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2864 {
2865 /* Grab the nested-name-specifier and continue the loop. */
2866 cp_parser_pre_parsed_nested_name_specifier (parser);
2867 success = true;
2868 continue;
2869 }
2870
a723baf1
MM
2871 /* Spot cases that cannot be the beginning of a
2872 nested-name-specifier. On the second and subsequent times
2873 through the loop, we look for the `template' keyword. */
f7b5ecd9 2874 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
2875 ;
2876 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 2877 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
2878 ;
2879 else
2880 {
2881 /* If the next token is not an identifier, then it is
2882 definitely not a class-or-namespace-name. */
f7b5ecd9 2883 if (token->type != CPP_NAME)
a723baf1
MM
2884 break;
2885 /* If the following token is neither a `<' (to begin a
2886 template-id), nor a `::', then we are not looking at a
2887 nested-name-specifier. */
2888 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2889 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2890 break;
2891 }
2892
2893 /* The nested-name-specifier is optional, so we parse
2894 tentatively. */
2895 cp_parser_parse_tentatively (parser);
2896
2897 /* Look for the optional `template' keyword, if this isn't the
2898 first time through the loop. */
2899 if (success)
2900 template_keyword_p = cp_parser_optional_template_keyword (parser);
2901 else
2902 template_keyword_p = false;
2903
2904 /* Save the old scope since the name lookup we are about to do
2905 might destroy it. */
2906 old_scope = parser->scope;
2907 saved_qualifying_scope = parser->qualifying_scope;
2908 /* Parse the qualifying entity. */
2909 new_scope
2910 = cp_parser_class_or_namespace_name (parser,
2911 typename_keyword_p,
2912 template_keyword_p,
2913 check_dependency_p,
2914 type_p);
2915 /* Look for the `::' token. */
2916 cp_parser_require (parser, CPP_SCOPE, "`::'");
2917
2918 /* If we found what we wanted, we keep going; otherwise, we're
2919 done. */
2920 if (!cp_parser_parse_definitely (parser))
2921 {
2922 bool error_p = false;
2923
2924 /* Restore the OLD_SCOPE since it was valid before the
2925 failed attempt at finding the last
2926 class-or-namespace-name. */
2927 parser->scope = old_scope;
2928 parser->qualifying_scope = saved_qualifying_scope;
2929 /* If the next token is an identifier, and the one after
2930 that is a `::', then any valid interpretation would have
2931 found a class-or-namespace-name. */
2932 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2934 == CPP_SCOPE)
2935 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2936 != CPP_COMPL))
2937 {
2938 token = cp_lexer_consume_token (parser->lexer);
2939 if (!error_p)
2940 {
2941 tree decl;
2942
2943 decl = cp_parser_lookup_name_simple (parser, token->value);
2944 if (TREE_CODE (decl) == TEMPLATE_DECL)
2945 error ("`%D' used without template parameters",
2946 decl);
2947 else if (parser->scope)
2948 {
2949 if (TYPE_P (parser->scope))
2950 error ("`%T::%D' is not a class-name or "
2951 "namespace-name",
2952 parser->scope, token->value);
2953 else
2954 error ("`%D::%D' is not a class-name or "
2955 "namespace-name",
2956 parser->scope, token->value);
2957 }
2958 else
2959 error ("`%D' is not a class-name or namespace-name",
2960 token->value);
2961 parser->scope = NULL_TREE;
2962 error_p = true;
eea9800f
MM
2963 /* Treat this as a successful nested-name-specifier
2964 due to:
2965
2966 [basic.lookup.qual]
2967
2968 If the name found is not a class-name (clause
2969 _class_) or namespace-name (_namespace.def_), the
2970 program is ill-formed. */
2971 success = true;
a723baf1
MM
2972 }
2973 cp_lexer_consume_token (parser->lexer);
2974 }
2975 break;
2976 }
2977
2978 /* We've found one valid nested-name-specifier. */
2979 success = true;
2980 /* Make sure we look in the right scope the next time through
2981 the loop. */
2982 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
2983 ? TREE_TYPE (new_scope)
2984 : new_scope);
2985 /* If it is a class scope, try to complete it; we are about to
2986 be looking up names inside the class. */
8fbc5ae7
MM
2987 if (TYPE_P (parser->scope)
2988 /* Since checking types for dependency can be expensive,
2989 avoid doing it if the type is already complete. */
2990 && !COMPLETE_TYPE_P (parser->scope)
2991 /* Do not try to complete dependent types. */
1fb3244a 2992 && !dependent_type_p (parser->scope))
a723baf1
MM
2993 complete_type (parser->scope);
2994 }
2995
cf22909c
KL
2996 /* Retrieve any deferred checks. Do not pop this access checks yet
2997 so the memory will not be reclaimed during token replacing below. */
2998 access_check = get_deferred_access_checks ();
2999
a723baf1
MM
3000 /* If parsing tentatively, replace the sequence of tokens that makes
3001 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3002 token. That way, should we re-parse the token stream, we will
3003 not have to repeat the effort required to do the parse, nor will
3004 we issue duplicate error messages. */
3005 if (success && start >= 0)
3006 {
a723baf1
MM
3007 /* Find the token that corresponds to the start of the
3008 template-id. */
3009 token = cp_lexer_advance_token (parser->lexer,
3010 parser->lexer->first_token,
3011 start);
3012
a723baf1
MM
3013 /* Reset the contents of the START token. */
3014 token->type = CPP_NESTED_NAME_SPECIFIER;
3015 token->value = build_tree_list (access_check, parser->scope);
3016 TREE_TYPE (token->value) = parser->qualifying_scope;
3017 token->keyword = RID_MAX;
3018 /* Purge all subsequent tokens. */
3019 cp_lexer_purge_tokens_after (parser->lexer, token);
3020 }
3021
cf22909c 3022 pop_deferring_access_checks ();
a723baf1
MM
3023 return success ? parser->scope : NULL_TREE;
3024}
3025
3026/* Parse a nested-name-specifier. See
3027 cp_parser_nested_name_specifier_opt for details. This function
3028 behaves identically, except that it will an issue an error if no
3029 nested-name-specifier is present, and it will return
3030 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3031 is present. */
3032
3033static tree
3034cp_parser_nested_name_specifier (cp_parser *parser,
3035 bool typename_keyword_p,
3036 bool check_dependency_p,
3037 bool type_p)
3038{
3039 tree scope;
3040
3041 /* Look for the nested-name-specifier. */
3042 scope = cp_parser_nested_name_specifier_opt (parser,
3043 typename_keyword_p,
3044 check_dependency_p,
3045 type_p);
3046 /* If it was not present, issue an error message. */
3047 if (!scope)
3048 {
3049 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3050 parser->scope = NULL_TREE;
a723baf1
MM
3051 return error_mark_node;
3052 }
3053
3054 return scope;
3055}
3056
3057/* Parse a class-or-namespace-name.
3058
3059 class-or-namespace-name:
3060 class-name
3061 namespace-name
3062
3063 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3064 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3065 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3066 TYPE_P is TRUE iff the next name should be taken as a class-name,
3067 even the same name is declared to be another entity in the same
3068 scope.
3069
3070 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3071 specified by the class-or-namespace-name. If neither is found the
3072 ERROR_MARK_NODE is returned. */
a723baf1
MM
3073
3074static tree
3075cp_parser_class_or_namespace_name (cp_parser *parser,
3076 bool typename_keyword_p,
3077 bool template_keyword_p,
3078 bool check_dependency_p,
3079 bool type_p)
3080{
3081 tree saved_scope;
3082 tree saved_qualifying_scope;
3083 tree saved_object_scope;
3084 tree scope;
eea9800f 3085 bool only_class_p;
a723baf1 3086
a723baf1
MM
3087 /* Before we try to parse the class-name, we must save away the
3088 current PARSER->SCOPE since cp_parser_class_name will destroy
3089 it. */
3090 saved_scope = parser->scope;
3091 saved_qualifying_scope = parser->qualifying_scope;
3092 saved_object_scope = parser->object_scope;
eea9800f
MM
3093 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3094 there is no need to look for a namespace-name. */
bbaab916 3095 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3096 if (!only_class_p)
3097 cp_parser_parse_tentatively (parser);
a723baf1
MM
3098 scope = cp_parser_class_name (parser,
3099 typename_keyword_p,
3100 template_keyword_p,
3101 type_p,
a723baf1
MM
3102 check_dependency_p,
3103 /*class_head_p=*/false);
3104 /* If that didn't work, try for a namespace-name. */
eea9800f 3105 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3106 {
3107 /* Restore the saved scope. */
3108 parser->scope = saved_scope;
3109 parser->qualifying_scope = saved_qualifying_scope;
3110 parser->object_scope = saved_object_scope;
eea9800f
MM
3111 /* If we are not looking at an identifier followed by the scope
3112 resolution operator, then this is not part of a
3113 nested-name-specifier. (Note that this function is only used
3114 to parse the components of a nested-name-specifier.) */
3115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3116 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3117 return error_mark_node;
a723baf1
MM
3118 scope = cp_parser_namespace_name (parser);
3119 }
3120
3121 return scope;
3122}
3123
3124/* Parse a postfix-expression.
3125
3126 postfix-expression:
3127 primary-expression
3128 postfix-expression [ expression ]
3129 postfix-expression ( expression-list [opt] )
3130 simple-type-specifier ( expression-list [opt] )
3131 typename :: [opt] nested-name-specifier identifier
3132 ( expression-list [opt] )
3133 typename :: [opt] nested-name-specifier template [opt] template-id
3134 ( expression-list [opt] )
3135 postfix-expression . template [opt] id-expression
3136 postfix-expression -> template [opt] id-expression
3137 postfix-expression . pseudo-destructor-name
3138 postfix-expression -> pseudo-destructor-name
3139 postfix-expression ++
3140 postfix-expression --
3141 dynamic_cast < type-id > ( expression )
3142 static_cast < type-id > ( expression )
3143 reinterpret_cast < type-id > ( expression )
3144 const_cast < type-id > ( expression )
3145 typeid ( expression )
3146 typeid ( type-id )
3147
3148 GNU Extension:
3149
3150 postfix-expression:
3151 ( type-id ) { initializer-list , [opt] }
3152
3153 This extension is a GNU version of the C99 compound-literal
3154 construct. (The C99 grammar uses `type-name' instead of `type-id',
3155 but they are essentially the same concept.)
3156
3157 If ADDRESS_P is true, the postfix expression is the operand of the
3158 `&' operator.
3159
3160 Returns a representation of the expression. */
3161
3162static tree
3163cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3164{
3165 cp_token *token;
3166 enum rid keyword;
b3445994 3167 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3168 tree postfix_expression = NULL_TREE;
3169 /* Non-NULL only if the current postfix-expression can be used to
3170 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3171 class used to qualify the member. */
3172 tree qualifying_class = NULL_TREE;
a723baf1
MM
3173
3174 /* Peek at the next token. */
3175 token = cp_lexer_peek_token (parser->lexer);
3176 /* Some of the productions are determined by keywords. */
3177 keyword = token->keyword;
3178 switch (keyword)
3179 {
3180 case RID_DYNCAST:
3181 case RID_STATCAST:
3182 case RID_REINTCAST:
3183 case RID_CONSTCAST:
3184 {
3185 tree type;
3186 tree expression;
3187 const char *saved_message;
3188
3189 /* All of these can be handled in the same way from the point
3190 of view of parsing. Begin by consuming the token
3191 identifying the cast. */
3192 cp_lexer_consume_token (parser->lexer);
3193
3194 /* New types cannot be defined in the cast. */
3195 saved_message = parser->type_definition_forbidden_message;
3196 parser->type_definition_forbidden_message
3197 = "types may not be defined in casts";
3198
3199 /* Look for the opening `<'. */
3200 cp_parser_require (parser, CPP_LESS, "`<'");
3201 /* Parse the type to which we are casting. */
3202 type = cp_parser_type_id (parser);
3203 /* Look for the closing `>'. */
3204 cp_parser_require (parser, CPP_GREATER, "`>'");
3205 /* Restore the old message. */
3206 parser->type_definition_forbidden_message = saved_message;
3207
3208 /* And the expression which is being cast. */
3209 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3210 expression = cp_parser_expression (parser);
3211 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3212
14d22dd6
MM
3213 /* Only type conversions to integral or enumeration types
3214 can be used in constant-expressions. */
3215 if (parser->constant_expression_p
3216 && !dependent_type_p (type)
3217 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3218 {
3219 if (!parser->allow_non_constant_expression_p)
3220 return (cp_parser_non_constant_expression
3221 ("a cast to a type other than an integral or "
3222 "enumeration type"));
3223 parser->non_constant_expression_p = true;
3224 }
3225
a723baf1
MM
3226 switch (keyword)
3227 {
3228 case RID_DYNCAST:
3229 postfix_expression
3230 = build_dynamic_cast (type, expression);
3231 break;
3232 case RID_STATCAST:
3233 postfix_expression
3234 = build_static_cast (type, expression);
3235 break;
3236 case RID_REINTCAST:
3237 postfix_expression
3238 = build_reinterpret_cast (type, expression);
3239 break;
3240 case RID_CONSTCAST:
3241 postfix_expression
3242 = build_const_cast (type, expression);
3243 break;
3244 default:
3245 abort ();
3246 }
3247 }
3248 break;
3249
3250 case RID_TYPEID:
3251 {
3252 tree type;
3253 const char *saved_message;
3254
3255 /* Consume the `typeid' token. */
3256 cp_lexer_consume_token (parser->lexer);
3257 /* Look for the `(' token. */
3258 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3259 /* Types cannot be defined in a `typeid' expression. */
3260 saved_message = parser->type_definition_forbidden_message;
3261 parser->type_definition_forbidden_message
3262 = "types may not be defined in a `typeid\' expression";
3263 /* We can't be sure yet whether we're looking at a type-id or an
3264 expression. */
3265 cp_parser_parse_tentatively (parser);
3266 /* Try a type-id first. */
3267 type = cp_parser_type_id (parser);
3268 /* Look for the `)' token. Otherwise, we can't be sure that
3269 we're not looking at an expression: consider `typeid (int
3270 (3))', for example. */
3271 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3272 /* If all went well, simply lookup the type-id. */
3273 if (cp_parser_parse_definitely (parser))
3274 postfix_expression = get_typeid (type);
3275 /* Otherwise, fall back to the expression variant. */
3276 else
3277 {
3278 tree expression;
3279
3280 /* Look for an expression. */
3281 expression = cp_parser_expression (parser);
3282 /* Compute its typeid. */
3283 postfix_expression = build_typeid (expression);
3284 /* Look for the `)' token. */
3285 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3286 }
3287
3288 /* Restore the saved message. */
3289 parser->type_definition_forbidden_message = saved_message;
3290 }
3291 break;
3292
3293 case RID_TYPENAME:
3294 {
3295 bool template_p = false;
3296 tree id;
3297 tree type;
3298
3299 /* Consume the `typename' token. */
3300 cp_lexer_consume_token (parser->lexer);
3301 /* Look for the optional `::' operator. */
3302 cp_parser_global_scope_opt (parser,
3303 /*current_scope_valid_p=*/false);
3304 /* Look for the nested-name-specifier. */
3305 cp_parser_nested_name_specifier (parser,
3306 /*typename_keyword_p=*/true,
3307 /*check_dependency_p=*/true,
3308 /*type_p=*/true);
3309 /* Look for the optional `template' keyword. */
3310 template_p = cp_parser_optional_template_keyword (parser);
3311 /* We don't know whether we're looking at a template-id or an
3312 identifier. */
3313 cp_parser_parse_tentatively (parser);
3314 /* Try a template-id. */
3315 id = cp_parser_template_id (parser, template_p,
3316 /*check_dependency_p=*/true);
3317 /* If that didn't work, try an identifier. */
3318 if (!cp_parser_parse_definitely (parser))
3319 id = cp_parser_identifier (parser);
3320 /* Create a TYPENAME_TYPE to represent the type to which the
3321 functional cast is being performed. */
3322 type = make_typename_type (parser->scope, id,
3323 /*complain=*/1);
3324
3325 postfix_expression = cp_parser_functional_cast (parser, type);
3326 }
3327 break;
3328
3329 default:
3330 {
3331 tree type;
3332
3333 /* If the next thing is a simple-type-specifier, we may be
3334 looking at a functional cast. We could also be looking at
3335 an id-expression. So, we try the functional cast, and if
3336 that doesn't work we fall back to the primary-expression. */
3337 cp_parser_parse_tentatively (parser);
3338 /* Look for the simple-type-specifier. */
3339 type = cp_parser_simple_type_specifier (parser,
3340 CP_PARSER_FLAGS_NONE);
3341 /* Parse the cast itself. */
3342 if (!cp_parser_error_occurred (parser))
3343 postfix_expression
3344 = cp_parser_functional_cast (parser, type);
3345 /* If that worked, we're done. */
3346 if (cp_parser_parse_definitely (parser))
3347 break;
3348
3349 /* If the functional-cast didn't work out, try a
3350 compound-literal. */
14d22dd6
MM
3351 if (cp_parser_allow_gnu_extensions_p (parser)
3352 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3353 {
3354 tree initializer_list = NULL_TREE;
3355
3356 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3357 /* Consume the `('. */
3358 cp_lexer_consume_token (parser->lexer);
3359 /* Parse the type. */
3360 type = cp_parser_type_id (parser);
3361 /* Look for the `)'. */
3362 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3363 /* Look for the `{'. */
3364 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3365 /* If things aren't going well, there's no need to
3366 keep going. */
3367 if (!cp_parser_error_occurred (parser))
a723baf1 3368 {
39703eb9 3369 bool non_constant_p;
14d22dd6
MM
3370 /* Parse the initializer-list. */
3371 initializer_list
39703eb9 3372 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3373 /* Allow a trailing `,'. */
3374 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3375 cp_lexer_consume_token (parser->lexer);
3376 /* Look for the final `}'. */
3377 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3378 }
3379 /* If that worked, we're definitely looking at a
3380 compound-literal expression. */
3381 if (cp_parser_parse_definitely (parser))
3382 {
3383 /* Warn the user that a compound literal is not
3384 allowed in standard C++. */
3385 if (pedantic)
3386 pedwarn ("ISO C++ forbids compound-literals");
3387 /* Form the representation of the compound-literal. */
3388 postfix_expression
3389 = finish_compound_literal (type, initializer_list);
3390 break;
3391 }
3392 }
3393
3394 /* It must be a primary-expression. */
3395 postfix_expression = cp_parser_primary_expression (parser,
3396 &idk,
3397 &qualifying_class);
3398 }
3399 break;
3400 }
3401
ee76b931
MM
3402 /* If we were avoiding committing to the processing of a
3403 qualified-id until we knew whether or not we had a
3404 pointer-to-member, we now know. */
089d6ea7 3405 if (qualifying_class)
a723baf1 3406 {
ee76b931 3407 bool done;
a723baf1 3408
ee76b931
MM
3409 /* Peek at the next token. */
3410 token = cp_lexer_peek_token (parser->lexer);
3411 done = (token->type != CPP_OPEN_SQUARE
3412 && token->type != CPP_OPEN_PAREN
3413 && token->type != CPP_DOT
3414 && token->type != CPP_DEREF
3415 && token->type != CPP_PLUS_PLUS
3416 && token->type != CPP_MINUS_MINUS);
3417
3418 postfix_expression = finish_qualified_id_expr (qualifying_class,
3419 postfix_expression,
3420 done,
3421 address_p);
3422 if (done)
3423 return postfix_expression;
a723baf1
MM
3424 }
3425
a723baf1
MM
3426 /* Keep looping until the postfix-expression is complete. */
3427 while (true)
3428 {
10b1d5e7
MM
3429 if (idk == CP_ID_KIND_UNQUALIFIED
3430 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3431 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3432 /* It is not a Koenig lookup function call. */
3433 postfix_expression
3434 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3435
3436 /* Peek at the next token. */
3437 token = cp_lexer_peek_token (parser->lexer);
3438
3439 switch (token->type)
3440 {
3441 case CPP_OPEN_SQUARE:
3442 /* postfix-expression [ expression ] */
3443 {
3444 tree index;
3445
3446 /* Consume the `[' token. */
3447 cp_lexer_consume_token (parser->lexer);
3448 /* Parse the index expression. */
3449 index = cp_parser_expression (parser);
3450 /* Look for the closing `]'. */
3451 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3452
3453 /* Build the ARRAY_REF. */
3454 postfix_expression
3455 = grok_array_decl (postfix_expression, index);
b3445994 3456 idk = CP_ID_KIND_NONE;
a723baf1
MM
3457 }
3458 break;
3459
3460 case CPP_OPEN_PAREN:
3461 /* postfix-expression ( expression-list [opt] ) */
3462 {
39703eb9
MM
3463 tree args = (cp_parser_parenthesized_expression_list
3464 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3465
7efa3e22
NS
3466 if (args == error_mark_node)
3467 {
3468 postfix_expression = error_mark_node;
3469 break;
3470 }
3471
14d22dd6
MM
3472 /* Function calls are not permitted in
3473 constant-expressions. */
3474 if (parser->constant_expression_p)
3475 {
3476 if (!parser->allow_non_constant_expression_p)
3477 return cp_parser_non_constant_expression ("a function call");
3478 parser->non_constant_expression_p = true;
3479 }
a723baf1 3480
399dedb9
NS
3481 if (idk == CP_ID_KIND_UNQUALIFIED)
3482 {
3483 if (args
3484 && (is_overloaded_fn (postfix_expression)
3485 || DECL_P (postfix_expression)
3486 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3487 postfix_expression
3488 = perform_koenig_lookup (postfix_expression, args);
3489 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3490 postfix_expression
3491 = unqualified_fn_lookup_error (postfix_expression);
3492 }
3493
d17811fd 3494 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3495 {
d17811fd
MM
3496 tree instance = TREE_OPERAND (postfix_expression, 0);
3497 tree fn = TREE_OPERAND (postfix_expression, 1);
3498
3499 if (processing_template_decl
3500 && (type_dependent_expression_p (instance)
3501 || (!BASELINK_P (fn)
3502 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3503 || type_dependent_expression_p (fn)
d17811fd
MM
3504 || any_type_dependent_arguments_p (args)))
3505 {
3506 postfix_expression
3507 = build_min_nt (CALL_EXPR, postfix_expression, args);
3508 break;
3509 }
3510
3511 postfix_expression
3512 = (build_new_method_call
3513 (instance, fn, args, NULL_TREE,
b3445994 3514 (idk == CP_ID_KIND_QUALIFIED
d17811fd 3515 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
a723baf1 3516 }
d17811fd
MM
3517 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3518 || TREE_CODE (postfix_expression) == MEMBER_REF
3519 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3520 postfix_expression = (build_offset_ref_call_from_tree
3521 (postfix_expression, args));
b3445994 3522 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3523 /* A call to a static class member, or a namespace-scope
3524 function. */
3525 postfix_expression
3526 = finish_call_expr (postfix_expression, args,
3527 /*disallow_virtual=*/true);
a723baf1 3528 else
2050a1bb
MM
3529 /* All other function calls. */
3530 postfix_expression
3531 = finish_call_expr (postfix_expression, args,
3532 /*disallow_virtual=*/false);
a723baf1
MM
3533
3534 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3535 idk = CP_ID_KIND_NONE;
a723baf1
MM
3536 }
3537 break;
3538
3539 case CPP_DOT:
3540 case CPP_DEREF:
3541 /* postfix-expression . template [opt] id-expression
3542 postfix-expression . pseudo-destructor-name
3543 postfix-expression -> template [opt] id-expression
3544 postfix-expression -> pseudo-destructor-name */
3545 {
3546 tree name;
3547 bool dependent_p;
3548 bool template_p;
3549 tree scope = NULL_TREE;
3550
3551 /* If this is a `->' operator, dereference the pointer. */
3552 if (token->type == CPP_DEREF)
3553 postfix_expression = build_x_arrow (postfix_expression);
3554 /* Check to see whether or not the expression is
3555 type-dependent. */
bbaab916 3556 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3557 /* The identifier following the `->' or `.' is not
3558 qualified. */
3559 parser->scope = NULL_TREE;
3560 parser->qualifying_scope = NULL_TREE;
3561 parser->object_scope = NULL_TREE;
b3445994 3562 idk = CP_ID_KIND_NONE;
a723baf1
MM
3563 /* Enter the scope corresponding to the type of the object
3564 given by the POSTFIX_EXPRESSION. */
3565 if (!dependent_p
3566 && TREE_TYPE (postfix_expression) != NULL_TREE)
3567 {
3568 scope = TREE_TYPE (postfix_expression);
3569 /* According to the standard, no expression should
3570 ever have reference type. Unfortunately, we do not
3571 currently match the standard in this respect in
3572 that our internal representation of an expression
3573 may have reference type even when the standard says
3574 it does not. Therefore, we have to manually obtain
3575 the underlying type here. */
ee76b931 3576 scope = non_reference (scope);
a723baf1
MM
3577 /* The type of the POSTFIX_EXPRESSION must be
3578 complete. */
3579 scope = complete_type_or_else (scope, NULL_TREE);
3580 /* Let the name lookup machinery know that we are
3581 processing a class member access expression. */
3582 parser->context->object_type = scope;
3583 /* If something went wrong, we want to be able to
3584 discern that case, as opposed to the case where
3585 there was no SCOPE due to the type of expression
3586 being dependent. */
3587 if (!scope)
3588 scope = error_mark_node;
3589 }
3590
3591 /* Consume the `.' or `->' operator. */
3592 cp_lexer_consume_token (parser->lexer);
3593 /* If the SCOPE is not a scalar type, we are looking at an
3594 ordinary class member access expression, rather than a
3595 pseudo-destructor-name. */
3596 if (!scope || !SCALAR_TYPE_P (scope))
3597 {
3598 template_p = cp_parser_optional_template_keyword (parser);
3599 /* Parse the id-expression. */
3600 name = cp_parser_id_expression (parser,
3601 template_p,
3602 /*check_dependency_p=*/true,
3603 /*template_p=*/NULL);
3604 /* In general, build a SCOPE_REF if the member name is
3605 qualified. However, if the name was not dependent
3606 and has already been resolved; there is no need to
3607 build the SCOPE_REF. For example;
3608
3609 struct X { void f(); };
3610 template <typename T> void f(T* t) { t->X::f(); }
3611
d17811fd
MM
3612 Even though "t" is dependent, "X::f" is not and has
3613 been resolved to a BASELINK; there is no need to
a723baf1 3614 include scope information. */
a6bd211d
JM
3615
3616 /* But we do need to remember that there was an explicit
3617 scope for virtual function calls. */
3618 if (parser->scope)
b3445994 3619 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3620
a723baf1
MM
3621 if (name != error_mark_node
3622 && !BASELINK_P (name)
3623 && parser->scope)
3624 {
3625 name = build_nt (SCOPE_REF, parser->scope, name);
3626 parser->scope = NULL_TREE;
3627 parser->qualifying_scope = NULL_TREE;
3628 parser->object_scope = NULL_TREE;
3629 }
3630 postfix_expression
3631 = finish_class_member_access_expr (postfix_expression, name);
3632 }
3633 /* Otherwise, try the pseudo-destructor-name production. */
3634 else
3635 {
3636 tree s;
3637 tree type;
3638
3639 /* Parse the pseudo-destructor-name. */
3640 cp_parser_pseudo_destructor_name (parser, &s, &type);
3641 /* Form the call. */
3642 postfix_expression
3643 = finish_pseudo_destructor_expr (postfix_expression,
3644 s, TREE_TYPE (type));
3645 }
3646
3647 /* We no longer need to look up names in the scope of the
3648 object on the left-hand side of the `.' or `->'
3649 operator. */
3650 parser->context->object_type = NULL_TREE;
a723baf1
MM
3651 }
3652 break;
3653
3654 case CPP_PLUS_PLUS:
3655 /* postfix-expression ++ */
3656 /* Consume the `++' token. */
3657 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3658 /* Increments may not appear in constant-expressions. */
3659 if (parser->constant_expression_p)
3660 {
3661 if (!parser->allow_non_constant_expression_p)
3662 return cp_parser_non_constant_expression ("an increment");
3663 parser->non_constant_expression_p = true;
3664 }
34cd5ae7 3665 /* Generate a representation for the complete expression. */
a723baf1
MM
3666 postfix_expression
3667 = finish_increment_expr (postfix_expression,
3668 POSTINCREMENT_EXPR);
b3445994 3669 idk = CP_ID_KIND_NONE;
a723baf1
MM
3670 break;
3671
3672 case CPP_MINUS_MINUS:
3673 /* postfix-expression -- */
3674 /* Consume the `--' token. */
3675 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3676 /* Decrements may not appear in constant-expressions. */
3677 if (parser->constant_expression_p)
3678 {
3679 if (!parser->allow_non_constant_expression_p)
3680 return cp_parser_non_constant_expression ("a decrement");
3681 parser->non_constant_expression_p = true;
3682 }
34cd5ae7 3683 /* Generate a representation for the complete expression. */
a723baf1
MM
3684 postfix_expression
3685 = finish_increment_expr (postfix_expression,
3686 POSTDECREMENT_EXPR);
b3445994 3687 idk = CP_ID_KIND_NONE;
a723baf1
MM
3688 break;
3689
3690 default:
3691 return postfix_expression;
3692 }
3693 }
3694
3695 /* We should never get here. */
3696 abort ();
3697 return error_mark_node;
3698}
3699
7efa3e22 3700/* Parse a parenthesized expression-list.
a723baf1
MM
3701
3702 expression-list:
3703 assignment-expression
3704 expression-list, assignment-expression
3705
7efa3e22
NS
3706 attribute-list:
3707 expression-list
3708 identifier
3709 identifier, expression-list
3710
a723baf1
MM
3711 Returns a TREE_LIST. The TREE_VALUE of each node is a
3712 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
3713 is returned even if there is only a single expression in the list.
3714 error_mark_node is returned if the ( and or ) are
3715 missing. NULL_TREE is returned on no expressions. The parentheses
3716 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
3717 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3718 indicates whether or not all of the expressions in the list were
3719 constant. */
a723baf1
MM
3720
3721static tree
39703eb9
MM
3722cp_parser_parenthesized_expression_list (cp_parser* parser,
3723 bool is_attribute_list,
3724 bool *non_constant_p)
a723baf1
MM
3725{
3726 tree expression_list = NULL_TREE;
7efa3e22 3727 tree identifier = NULL_TREE;
39703eb9
MM
3728
3729 /* Assume all the expressions will be constant. */
3730 if (non_constant_p)
3731 *non_constant_p = false;
3732
7efa3e22
NS
3733 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3734 return error_mark_node;
3735
a723baf1 3736 /* Consume expressions until there are no more. */
7efa3e22
NS
3737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3738 while (true)
3739 {
3740 tree expr;
3741
3742 /* At the beginning of attribute lists, check to see if the
3743 next token is an identifier. */
3744 if (is_attribute_list
3745 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3746 {
3747 cp_token *token;
3748
3749 /* Consume the identifier. */
3750 token = cp_lexer_consume_token (parser->lexer);
3751 /* Save the identifier. */
3752 identifier = token->value;
3753 }
3754 else
3755 {
3756 /* Parse the next assignment-expression. */
39703eb9
MM
3757 if (non_constant_p)
3758 {
3759 bool expr_non_constant_p;
3760 expr = (cp_parser_constant_expression
3761 (parser, /*allow_non_constant_p=*/true,
3762 &expr_non_constant_p));
3763 if (expr_non_constant_p)
3764 *non_constant_p = true;
3765 }
3766 else
3767 expr = cp_parser_assignment_expression (parser);
a723baf1 3768
7efa3e22
NS
3769 /* Add it to the list. We add error_mark_node
3770 expressions to the list, so that we can still tell if
3771 the correct form for a parenthesized expression-list
3772 is found. That gives better errors. */
3773 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 3774
7efa3e22
NS
3775 if (expr == error_mark_node)
3776 goto skip_comma;
3777 }
a723baf1 3778
7efa3e22
NS
3779 /* After the first item, attribute lists look the same as
3780 expression lists. */
3781 is_attribute_list = false;
3782
3783 get_comma:;
3784 /* If the next token isn't a `,', then we are done. */
3785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3786 break;
3787
3788 /* Otherwise, consume the `,' and keep going. */
3789 cp_lexer_consume_token (parser->lexer);
3790 }
3791
3792 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3793 {
3794 int ending;
3795
3796 skip_comma:;
3797 /* We try and resync to an unnested comma, as that will give the
3798 user better diagnostics. */
3799 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3800 if (ending < 0)
3801 goto get_comma;
3802 if (!ending)
3803 return error_mark_node;
a723baf1
MM
3804 }
3805
3806 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
3807 expression_list = nreverse (expression_list);
3808 if (identifier)
3809 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3810
3811 return expression_list;
a723baf1
MM
3812}
3813
3814/* Parse a pseudo-destructor-name.
3815
3816 pseudo-destructor-name:
3817 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3818 :: [opt] nested-name-specifier template template-id :: ~ type-name
3819 :: [opt] nested-name-specifier [opt] ~ type-name
3820
3821 If either of the first two productions is used, sets *SCOPE to the
3822 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3823 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3824 or ERROR_MARK_NODE if no type-name is present. */
3825
3826static void
94edc4ab
NN
3827cp_parser_pseudo_destructor_name (cp_parser* parser,
3828 tree* scope,
3829 tree* type)
a723baf1
MM
3830{
3831 bool nested_name_specifier_p;
3832
3833 /* Look for the optional `::' operator. */
3834 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3835 /* Look for the optional nested-name-specifier. */
3836 nested_name_specifier_p
3837 = (cp_parser_nested_name_specifier_opt (parser,
3838 /*typename_keyword_p=*/false,
3839 /*check_dependency_p=*/true,
3840 /*type_p=*/false)
3841 != NULL_TREE);
3842 /* Now, if we saw a nested-name-specifier, we might be doing the
3843 second production. */
3844 if (nested_name_specifier_p
3845 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3846 {
3847 /* Consume the `template' keyword. */
3848 cp_lexer_consume_token (parser->lexer);
3849 /* Parse the template-id. */
3850 cp_parser_template_id (parser,
3851 /*template_keyword_p=*/true,
3852 /*check_dependency_p=*/false);
3853 /* Look for the `::' token. */
3854 cp_parser_require (parser, CPP_SCOPE, "`::'");
3855 }
3856 /* If the next token is not a `~', then there might be some
9bcb9aae 3857 additional qualification. */
a723baf1
MM
3858 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3859 {
3860 /* Look for the type-name. */
3861 *scope = TREE_TYPE (cp_parser_type_name (parser));
3862 /* Look for the `::' token. */
3863 cp_parser_require (parser, CPP_SCOPE, "`::'");
3864 }
3865 else
3866 *scope = NULL_TREE;
3867
3868 /* Look for the `~'. */
3869 cp_parser_require (parser, CPP_COMPL, "`~'");
3870 /* Look for the type-name again. We are not responsible for
3871 checking that it matches the first type-name. */
3872 *type = cp_parser_type_name (parser);
3873}
3874
3875/* Parse a unary-expression.
3876
3877 unary-expression:
3878 postfix-expression
3879 ++ cast-expression
3880 -- cast-expression
3881 unary-operator cast-expression
3882 sizeof unary-expression
3883 sizeof ( type-id )
3884 new-expression
3885 delete-expression
3886
3887 GNU Extensions:
3888
3889 unary-expression:
3890 __extension__ cast-expression
3891 __alignof__ unary-expression
3892 __alignof__ ( type-id )
3893 __real__ cast-expression
3894 __imag__ cast-expression
3895 && identifier
3896
3897 ADDRESS_P is true iff the unary-expression is appearing as the
3898 operand of the `&' operator.
3899
34cd5ae7 3900 Returns a representation of the expression. */
a723baf1
MM
3901
3902static tree
3903cp_parser_unary_expression (cp_parser *parser, bool address_p)
3904{
3905 cp_token *token;
3906 enum tree_code unary_operator;
3907
3908 /* Peek at the next token. */
3909 token = cp_lexer_peek_token (parser->lexer);
3910 /* Some keywords give away the kind of expression. */
3911 if (token->type == CPP_KEYWORD)
3912 {
3913 enum rid keyword = token->keyword;
3914
3915 switch (keyword)
3916 {
3917 case RID_ALIGNOF:
3918 {
3919 /* Consume the `alignof' token. */
3920 cp_lexer_consume_token (parser->lexer);
3921 /* Parse the operand. */
3922 return finish_alignof (cp_parser_sizeof_operand
3923 (parser, keyword));
3924 }
3925
3926 case RID_SIZEOF:
3927 {
3928 tree operand;
3929
9bcb9aae 3930 /* Consume the `sizeof' token. */
a723baf1
MM
3931 cp_lexer_consume_token (parser->lexer);
3932 /* Parse the operand. */
3933 operand = cp_parser_sizeof_operand (parser, keyword);
3934
3935 /* If the type of the operand cannot be determined build a
3936 SIZEOF_EXPR. */
3937 if (TYPE_P (operand)
1fb3244a
MM
3938 ? dependent_type_p (operand)
3939 : type_dependent_expression_p (operand))
a723baf1
MM
3940 return build_min (SIZEOF_EXPR, size_type_node, operand);
3941 /* Otherwise, compute the constant value. */
3942 else
3943 return finish_sizeof (operand);
3944 }
3945
3946 case RID_NEW:
3947 return cp_parser_new_expression (parser);
3948
3949 case RID_DELETE:
3950 return cp_parser_delete_expression (parser);
3951
3952 case RID_EXTENSION:
3953 {
3954 /* The saved value of the PEDANTIC flag. */
3955 int saved_pedantic;
3956 tree expr;
3957
3958 /* Save away the PEDANTIC flag. */
3959 cp_parser_extension_opt (parser, &saved_pedantic);
3960 /* Parse the cast-expression. */
d6b4ea85 3961 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
3962 /* Restore the PEDANTIC flag. */
3963 pedantic = saved_pedantic;
3964
3965 return expr;
3966 }
3967
3968 case RID_REALPART:
3969 case RID_IMAGPART:
3970 {
3971 tree expression;
3972
3973 /* Consume the `__real__' or `__imag__' token. */
3974 cp_lexer_consume_token (parser->lexer);
3975 /* Parse the cast-expression. */
d6b4ea85 3976 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
3977 /* Create the complete representation. */
3978 return build_x_unary_op ((keyword == RID_REALPART
3979 ? REALPART_EXPR : IMAGPART_EXPR),
3980 expression);
3981 }
3982 break;
3983
3984 default:
3985 break;
3986 }
3987 }
3988
3989 /* Look for the `:: new' and `:: delete', which also signal the
3990 beginning of a new-expression, or delete-expression,
3991 respectively. If the next token is `::', then it might be one of
3992 these. */
3993 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
3994 {
3995 enum rid keyword;
3996
3997 /* See if the token after the `::' is one of the keywords in
3998 which we're interested. */
3999 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4000 /* If it's `new', we have a new-expression. */
4001 if (keyword == RID_NEW)
4002 return cp_parser_new_expression (parser);
4003 /* Similarly, for `delete'. */
4004 else if (keyword == RID_DELETE)
4005 return cp_parser_delete_expression (parser);
4006 }
4007
4008 /* Look for a unary operator. */
4009 unary_operator = cp_parser_unary_operator (token);
4010 /* The `++' and `--' operators can be handled similarly, even though
4011 they are not technically unary-operators in the grammar. */
4012 if (unary_operator == ERROR_MARK)
4013 {
4014 if (token->type == CPP_PLUS_PLUS)
4015 unary_operator = PREINCREMENT_EXPR;
4016 else if (token->type == CPP_MINUS_MINUS)
4017 unary_operator = PREDECREMENT_EXPR;
4018 /* Handle the GNU address-of-label extension. */
4019 else if (cp_parser_allow_gnu_extensions_p (parser)
4020 && token->type == CPP_AND_AND)
4021 {
4022 tree identifier;
4023
4024 /* Consume the '&&' token. */
4025 cp_lexer_consume_token (parser->lexer);
4026 /* Look for the identifier. */
4027 identifier = cp_parser_identifier (parser);
4028 /* Create an expression representing the address. */
4029 return finish_label_address_expr (identifier);
4030 }
4031 }
4032 if (unary_operator != ERROR_MARK)
4033 {
4034 tree cast_expression;
4035
4036 /* Consume the operator token. */
4037 token = cp_lexer_consume_token (parser->lexer);
4038 /* Parse the cast-expression. */
4039 cast_expression
4040 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4041 /* Now, build an appropriate representation. */
4042 switch (unary_operator)
4043 {
4044 case INDIRECT_REF:
4045 return build_x_indirect_ref (cast_expression, "unary *");
4046
4047 case ADDR_EXPR:
d17811fd
MM
4048 case BIT_NOT_EXPR:
4049 return build_x_unary_op (unary_operator, cast_expression);
a723baf1 4050
14d22dd6
MM
4051 case PREINCREMENT_EXPR:
4052 case PREDECREMENT_EXPR:
4053 if (parser->constant_expression_p)
4054 {
4055 if (!parser->allow_non_constant_expression_p)
4056 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4057 ? "an increment"
4058 : "a decrement");
4059 parser->non_constant_expression_p = true;
4060 }
4061 /* Fall through. */
a723baf1
MM
4062 case CONVERT_EXPR:
4063 case NEGATE_EXPR:
4064 case TRUTH_NOT_EXPR:
a723baf1
MM
4065 return finish_unary_op_expr (unary_operator, cast_expression);
4066
a723baf1
MM
4067 default:
4068 abort ();
4069 return error_mark_node;
4070 }
4071 }
4072
4073 return cp_parser_postfix_expression (parser, address_p);
4074}
4075
4076/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4077 unary-operator, the corresponding tree code is returned. */
4078
4079static enum tree_code
94edc4ab 4080cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4081{
4082 switch (token->type)
4083 {
4084 case CPP_MULT:
4085 return INDIRECT_REF;
4086
4087 case CPP_AND:
4088 return ADDR_EXPR;
4089
4090 case CPP_PLUS:
4091 return CONVERT_EXPR;
4092
4093 case CPP_MINUS:
4094 return NEGATE_EXPR;
4095
4096 case CPP_NOT:
4097 return TRUTH_NOT_EXPR;
4098
4099 case CPP_COMPL:
4100 return BIT_NOT_EXPR;
4101
4102 default:
4103 return ERROR_MARK;
4104 }
4105}
4106
4107/* Parse a new-expression.
4108
ca099ac8 4109 new-expression:
a723baf1
MM
4110 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4111 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4112
4113 Returns a representation of the expression. */
4114
4115static tree
94edc4ab 4116cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4117{
4118 bool global_scope_p;
4119 tree placement;
4120 tree type;
4121 tree initializer;
4122
4123 /* Look for the optional `::' operator. */
4124 global_scope_p
4125 = (cp_parser_global_scope_opt (parser,
4126 /*current_scope_valid_p=*/false)
4127 != NULL_TREE);
4128 /* Look for the `new' operator. */
4129 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4130 /* There's no easy way to tell a new-placement from the
4131 `( type-id )' construct. */
4132 cp_parser_parse_tentatively (parser);
4133 /* Look for a new-placement. */
4134 placement = cp_parser_new_placement (parser);
4135 /* If that didn't work out, there's no new-placement. */
4136 if (!cp_parser_parse_definitely (parser))
4137 placement = NULL_TREE;
4138
4139 /* If the next token is a `(', then we have a parenthesized
4140 type-id. */
4141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4142 {
4143 /* Consume the `('. */
4144 cp_lexer_consume_token (parser->lexer);
4145 /* Parse the type-id. */
4146 type = cp_parser_type_id (parser);
4147 /* Look for the closing `)'. */
4148 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4149 }
4150 /* Otherwise, there must be a new-type-id. */
4151 else
4152 type = cp_parser_new_type_id (parser);
4153
4154 /* If the next token is a `(', then we have a new-initializer. */
4155 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4156 initializer = cp_parser_new_initializer (parser);
4157 else
4158 initializer = NULL_TREE;
4159
4160 /* Create a representation of the new-expression. */
4161 return build_new (placement, type, initializer, global_scope_p);
4162}
4163
4164/* Parse a new-placement.
4165
4166 new-placement:
4167 ( expression-list )
4168
4169 Returns the same representation as for an expression-list. */
4170
4171static tree
94edc4ab 4172cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4173{
4174 tree expression_list;
4175
a723baf1 4176 /* Parse the expression-list. */
39703eb9
MM
4177 expression_list = (cp_parser_parenthesized_expression_list
4178 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4179
4180 return expression_list;
4181}
4182
4183/* Parse a new-type-id.
4184
4185 new-type-id:
4186 type-specifier-seq new-declarator [opt]
4187
4188 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4189 and whose TREE_VALUE is the new-declarator. */
4190
4191static tree
94edc4ab 4192cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4193{
4194 tree type_specifier_seq;
4195 tree declarator;
4196 const char *saved_message;
4197
4198 /* The type-specifier sequence must not contain type definitions.
4199 (It cannot contain declarations of new types either, but if they
4200 are not definitions we will catch that because they are not
4201 complete.) */
4202 saved_message = parser->type_definition_forbidden_message;
4203 parser->type_definition_forbidden_message
4204 = "types may not be defined in a new-type-id";
4205 /* Parse the type-specifier-seq. */
4206 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4207 /* Restore the old message. */
4208 parser->type_definition_forbidden_message = saved_message;
4209 /* Parse the new-declarator. */
4210 declarator = cp_parser_new_declarator_opt (parser);
4211
4212 return build_tree_list (type_specifier_seq, declarator);
4213}
4214
4215/* Parse an (optional) new-declarator.
4216
4217 new-declarator:
4218 ptr-operator new-declarator [opt]
4219 direct-new-declarator
4220
4221 Returns a representation of the declarator. See
4222 cp_parser_declarator for the representations used. */
4223
4224static tree
94edc4ab 4225cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4226{
4227 enum tree_code code;
4228 tree type;
4229 tree cv_qualifier_seq;
4230
4231 /* We don't know if there's a ptr-operator next, or not. */
4232 cp_parser_parse_tentatively (parser);
4233 /* Look for a ptr-operator. */
4234 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4235 /* If that worked, look for more new-declarators. */
4236 if (cp_parser_parse_definitely (parser))
4237 {
4238 tree declarator;
4239
4240 /* Parse another optional declarator. */
4241 declarator = cp_parser_new_declarator_opt (parser);
4242
4243 /* Create the representation of the declarator. */
4244 if (code == INDIRECT_REF)
4245 declarator = make_pointer_declarator (cv_qualifier_seq,
4246 declarator);
4247 else
4248 declarator = make_reference_declarator (cv_qualifier_seq,
4249 declarator);
4250
4251 /* Handle the pointer-to-member case. */
4252 if (type)
4253 declarator = build_nt (SCOPE_REF, type, declarator);
4254
4255 return declarator;
4256 }
4257
4258 /* If the next token is a `[', there is a direct-new-declarator. */
4259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4260 return cp_parser_direct_new_declarator (parser);
4261
4262 return NULL_TREE;
4263}
4264
4265/* Parse a direct-new-declarator.
4266
4267 direct-new-declarator:
4268 [ expression ]
4269 direct-new-declarator [constant-expression]
4270
4271 Returns an ARRAY_REF, following the same conventions as are
4272 documented for cp_parser_direct_declarator. */
4273
4274static tree
94edc4ab 4275cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4276{
4277 tree declarator = NULL_TREE;
4278
4279 while (true)
4280 {
4281 tree expression;
4282
4283 /* Look for the opening `['. */
4284 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4285 /* The first expression is not required to be constant. */
4286 if (!declarator)
4287 {
4288 expression = cp_parser_expression (parser);
4289 /* The standard requires that the expression have integral
4290 type. DR 74 adds enumeration types. We believe that the
4291 real intent is that these expressions be handled like the
4292 expression in a `switch' condition, which also allows
4293 classes with a single conversion to integral or
4294 enumeration type. */
4295 if (!processing_template_decl)
4296 {
4297 expression
4298 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4299 expression,
b746c5dc 4300 /*complain=*/true);
a723baf1
MM
4301 if (!expression)
4302 {
4303 error ("expression in new-declarator must have integral or enumeration type");
4304 expression = error_mark_node;
4305 }
4306 }
4307 }
4308 /* But all the other expressions must be. */
4309 else
14d22dd6
MM
4310 expression
4311 = cp_parser_constant_expression (parser,
4312 /*allow_non_constant=*/false,
4313 NULL);
a723baf1
MM
4314 /* Look for the closing `]'. */
4315 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4316
4317 /* Add this bound to the declarator. */
4318 declarator = build_nt (ARRAY_REF, declarator, expression);
4319
4320 /* If the next token is not a `[', then there are no more
4321 bounds. */
4322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4323 break;
4324 }
4325
4326 return declarator;
4327}
4328
4329/* Parse a new-initializer.
4330
4331 new-initializer:
4332 ( expression-list [opt] )
4333
34cd5ae7 4334 Returns a representation of the expression-list. If there is no
a723baf1
MM
4335 expression-list, VOID_ZERO_NODE is returned. */
4336
4337static tree
94edc4ab 4338cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4339{
4340 tree expression_list;
4341
39703eb9
MM
4342 expression_list = (cp_parser_parenthesized_expression_list
4343 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4344 if (!expression_list)
a723baf1 4345 expression_list = void_zero_node;
a723baf1
MM
4346
4347 return expression_list;
4348}
4349
4350/* Parse a delete-expression.
4351
4352 delete-expression:
4353 :: [opt] delete cast-expression
4354 :: [opt] delete [ ] cast-expression
4355
4356 Returns a representation of the expression. */
4357
4358static tree
94edc4ab 4359cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4360{
4361 bool global_scope_p;
4362 bool array_p;
4363 tree expression;
4364
4365 /* Look for the optional `::' operator. */
4366 global_scope_p
4367 = (cp_parser_global_scope_opt (parser,
4368 /*current_scope_valid_p=*/false)
4369 != NULL_TREE);
4370 /* Look for the `delete' keyword. */
4371 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4372 /* See if the array syntax is in use. */
4373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4374 {
4375 /* Consume the `[' token. */
4376 cp_lexer_consume_token (parser->lexer);
4377 /* Look for the `]' token. */
4378 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4379 /* Remember that this is the `[]' construct. */
4380 array_p = true;
4381 }
4382 else
4383 array_p = false;
4384
4385 /* Parse the cast-expression. */
d6b4ea85 4386 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4387
4388 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4389}
4390
4391/* Parse a cast-expression.
4392
4393 cast-expression:
4394 unary-expression
4395 ( type-id ) cast-expression
4396
4397 Returns a representation of the expression. */
4398
4399static tree
4400cp_parser_cast_expression (cp_parser *parser, bool address_p)
4401{
4402 /* If it's a `(', then we might be looking at a cast. */
4403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4404 {
4405 tree type = NULL_TREE;
4406 tree expr = NULL_TREE;
4407 bool compound_literal_p;
4408 const char *saved_message;
4409
4410 /* There's no way to know yet whether or not this is a cast.
4411 For example, `(int (3))' is a unary-expression, while `(int)
4412 3' is a cast. So, we resort to parsing tentatively. */
4413 cp_parser_parse_tentatively (parser);
4414 /* Types may not be defined in a cast. */
4415 saved_message = parser->type_definition_forbidden_message;
4416 parser->type_definition_forbidden_message
4417 = "types may not be defined in casts";
4418 /* Consume the `('. */
4419 cp_lexer_consume_token (parser->lexer);
4420 /* A very tricky bit is that `(struct S) { 3 }' is a
4421 compound-literal (which we permit in C++ as an extension).
4422 But, that construct is not a cast-expression -- it is a
4423 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4424 is legal; if the compound-literal were a cast-expression,
4425 you'd need an extra set of parentheses.) But, if we parse
4426 the type-id, and it happens to be a class-specifier, then we
4427 will commit to the parse at that point, because we cannot
4428 undo the action that is done when creating a new class. So,
4429 then we cannot back up and do a postfix-expression.
4430
4431 Therefore, we scan ahead to the closing `)', and check to see
4432 if the token after the `)' is a `{'. If so, we are not
4433 looking at a cast-expression.
4434
4435 Save tokens so that we can put them back. */
4436 cp_lexer_save_tokens (parser->lexer);
4437 /* Skip tokens until the next token is a closing parenthesis.
4438 If we find the closing `)', and the next token is a `{', then
4439 we are looking at a compound-literal. */
4440 compound_literal_p
7efa3e22 4441 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
a723baf1
MM
4442 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4443 /* Roll back the tokens we skipped. */
4444 cp_lexer_rollback_tokens (parser->lexer);
4445 /* If we were looking at a compound-literal, simulate an error
4446 so that the call to cp_parser_parse_definitely below will
4447 fail. */
4448 if (compound_literal_p)
4449 cp_parser_simulate_error (parser);
4450 else
4451 {
4452 /* Look for the type-id. */
4453 type = cp_parser_type_id (parser);
4454 /* Look for the closing `)'. */
4455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4456 }
4457
4458 /* Restore the saved message. */
4459 parser->type_definition_forbidden_message = saved_message;
4460
bbaab916
NS
4461 /* If ok so far, parse the dependent expression. We cannot be
4462 sure it is a cast. Consider `(T ())'. It is a parenthesized
4463 ctor of T, but looks like a cast to function returning T
4464 without a dependent expression. */
4465 if (!cp_parser_error_occurred (parser))
d6b4ea85 4466 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4467
a723baf1
MM
4468 if (cp_parser_parse_definitely (parser))
4469 {
a723baf1
MM
4470 /* Warn about old-style casts, if so requested. */
4471 if (warn_old_style_cast
4472 && !in_system_header
4473 && !VOID_TYPE_P (type)
4474 && current_lang_name != lang_name_c)
4475 warning ("use of old-style cast");
14d22dd6
MM
4476
4477 /* Only type conversions to integral or enumeration types
4478 can be used in constant-expressions. */
4479 if (parser->constant_expression_p
4480 && !dependent_type_p (type)
4481 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4482 {
4483 if (!parser->allow_non_constant_expression_p)
4484 return (cp_parser_non_constant_expression
4485 ("a casts to a type other than an integral or "
4486 "enumeration type"));
4487 parser->non_constant_expression_p = true;
4488 }
a723baf1
MM
4489 /* Perform the cast. */
4490 expr = build_c_cast (type, expr);
bbaab916 4491 return expr;
a723baf1 4492 }
a723baf1
MM
4493 }
4494
4495 /* If we get here, then it's not a cast, so it must be a
4496 unary-expression. */
4497 return cp_parser_unary_expression (parser, address_p);
4498}
4499
4500/* Parse a pm-expression.
4501
4502 pm-expression:
4503 cast-expression
4504 pm-expression .* cast-expression
4505 pm-expression ->* cast-expression
4506
4507 Returns a representation of the expression. */
4508
4509static tree
94edc4ab 4510cp_parser_pm_expression (cp_parser* parser)
a723baf1 4511{
d6b4ea85
MM
4512 static const cp_parser_token_tree_map map = {
4513 { CPP_DEREF_STAR, MEMBER_REF },
4514 { CPP_DOT_STAR, DOTSTAR_EXPR },
4515 { CPP_EOF, ERROR_MARK }
4516 };
a723baf1 4517
d6b4ea85
MM
4518 return cp_parser_binary_expression (parser, map,
4519 cp_parser_simple_cast_expression);
a723baf1
MM
4520}
4521
4522/* Parse a multiplicative-expression.
4523
4524 mulitplicative-expression:
4525 pm-expression
4526 multiplicative-expression * pm-expression
4527 multiplicative-expression / pm-expression
4528 multiplicative-expression % pm-expression
4529
4530 Returns a representation of the expression. */
4531
4532static tree
94edc4ab 4533cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4534{
39b1af70 4535 static const cp_parser_token_tree_map map = {
a723baf1
MM
4536 { CPP_MULT, MULT_EXPR },
4537 { CPP_DIV, TRUNC_DIV_EXPR },
4538 { CPP_MOD, TRUNC_MOD_EXPR },
4539 { CPP_EOF, ERROR_MARK }
4540 };
4541
4542 return cp_parser_binary_expression (parser,
4543 map,
4544 cp_parser_pm_expression);
4545}
4546
4547/* Parse an additive-expression.
4548
4549 additive-expression:
4550 multiplicative-expression
4551 additive-expression + multiplicative-expression
4552 additive-expression - multiplicative-expression
4553
4554 Returns a representation of the expression. */
4555
4556static tree
94edc4ab 4557cp_parser_additive_expression (cp_parser* parser)
a723baf1 4558{
39b1af70 4559 static const cp_parser_token_tree_map map = {
a723baf1
MM
4560 { CPP_PLUS, PLUS_EXPR },
4561 { CPP_MINUS, MINUS_EXPR },
4562 { CPP_EOF, ERROR_MARK }
4563 };
4564
4565 return cp_parser_binary_expression (parser,
4566 map,
4567 cp_parser_multiplicative_expression);
4568}
4569
4570/* Parse a shift-expression.
4571
4572 shift-expression:
4573 additive-expression
4574 shift-expression << additive-expression
4575 shift-expression >> additive-expression
4576
4577 Returns a representation of the expression. */
4578
4579static tree
94edc4ab 4580cp_parser_shift_expression (cp_parser* parser)
a723baf1 4581{
39b1af70 4582 static const cp_parser_token_tree_map map = {
a723baf1
MM
4583 { CPP_LSHIFT, LSHIFT_EXPR },
4584 { CPP_RSHIFT, RSHIFT_EXPR },
4585 { CPP_EOF, ERROR_MARK }
4586 };
4587
4588 return cp_parser_binary_expression (parser,
4589 map,
4590 cp_parser_additive_expression);
4591}
4592
4593/* Parse a relational-expression.
4594
4595 relational-expression:
4596 shift-expression
4597 relational-expression < shift-expression
4598 relational-expression > shift-expression
4599 relational-expression <= shift-expression
4600 relational-expression >= shift-expression
4601
4602 GNU Extension:
4603
4604 relational-expression:
4605 relational-expression <? shift-expression
4606 relational-expression >? shift-expression
4607
4608 Returns a representation of the expression. */
4609
4610static tree
94edc4ab 4611cp_parser_relational_expression (cp_parser* parser)
a723baf1 4612{
39b1af70 4613 static const cp_parser_token_tree_map map = {
a723baf1
MM
4614 { CPP_LESS, LT_EXPR },
4615 { CPP_GREATER, GT_EXPR },
4616 { CPP_LESS_EQ, LE_EXPR },
4617 { CPP_GREATER_EQ, GE_EXPR },
4618 { CPP_MIN, MIN_EXPR },
4619 { CPP_MAX, MAX_EXPR },
4620 { CPP_EOF, ERROR_MARK }
4621 };
4622
4623 return cp_parser_binary_expression (parser,
4624 map,
4625 cp_parser_shift_expression);
4626}
4627
4628/* Parse an equality-expression.
4629
4630 equality-expression:
4631 relational-expression
4632 equality-expression == relational-expression
4633 equality-expression != relational-expression
4634
4635 Returns a representation of the expression. */
4636
4637static tree
94edc4ab 4638cp_parser_equality_expression (cp_parser* parser)
a723baf1 4639{
39b1af70 4640 static const cp_parser_token_tree_map map = {
a723baf1
MM
4641 { CPP_EQ_EQ, EQ_EXPR },
4642 { CPP_NOT_EQ, NE_EXPR },
4643 { CPP_EOF, ERROR_MARK }
4644 };
4645
4646 return cp_parser_binary_expression (parser,
4647 map,
4648 cp_parser_relational_expression);
4649}
4650
4651/* Parse an and-expression.
4652
4653 and-expression:
4654 equality-expression
4655 and-expression & equality-expression
4656
4657 Returns a representation of the expression. */
4658
4659static tree
94edc4ab 4660cp_parser_and_expression (cp_parser* parser)
a723baf1 4661{
39b1af70 4662 static const cp_parser_token_tree_map map = {
a723baf1
MM
4663 { CPP_AND, BIT_AND_EXPR },
4664 { CPP_EOF, ERROR_MARK }
4665 };
4666
4667 return cp_parser_binary_expression (parser,
4668 map,
4669 cp_parser_equality_expression);
4670}
4671
4672/* Parse an exclusive-or-expression.
4673
4674 exclusive-or-expression:
4675 and-expression
4676 exclusive-or-expression ^ and-expression
4677
4678 Returns a representation of the expression. */
4679
4680static tree
94edc4ab 4681cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4682{
39b1af70 4683 static const cp_parser_token_tree_map map = {
a723baf1
MM
4684 { CPP_XOR, BIT_XOR_EXPR },
4685 { CPP_EOF, ERROR_MARK }
4686 };
4687
4688 return cp_parser_binary_expression (parser,
4689 map,
4690 cp_parser_and_expression);
4691}
4692
4693
4694/* Parse an inclusive-or-expression.
4695
4696 inclusive-or-expression:
4697 exclusive-or-expression
4698 inclusive-or-expression | exclusive-or-expression
4699
4700 Returns a representation of the expression. */
4701
4702static tree
94edc4ab 4703cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 4704{
39b1af70 4705 static const cp_parser_token_tree_map map = {
a723baf1
MM
4706 { CPP_OR, BIT_IOR_EXPR },
4707 { CPP_EOF, ERROR_MARK }
4708 };
4709
4710 return cp_parser_binary_expression (parser,
4711 map,
4712 cp_parser_exclusive_or_expression);
4713}
4714
4715/* Parse a logical-and-expression.
4716
4717 logical-and-expression:
4718 inclusive-or-expression
4719 logical-and-expression && inclusive-or-expression
4720
4721 Returns a representation of the expression. */
4722
4723static tree
94edc4ab 4724cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 4725{
39b1af70 4726 static const cp_parser_token_tree_map map = {
a723baf1
MM
4727 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4728 { CPP_EOF, ERROR_MARK }
4729 };
4730
4731 return cp_parser_binary_expression (parser,
4732 map,
4733 cp_parser_inclusive_or_expression);
4734}
4735
4736/* Parse a logical-or-expression.
4737
4738 logical-or-expression:
34cd5ae7 4739 logical-and-expression
a723baf1
MM
4740 logical-or-expression || logical-and-expression
4741
4742 Returns a representation of the expression. */
4743
4744static tree
94edc4ab 4745cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 4746{
39b1af70 4747 static const cp_parser_token_tree_map map = {
a723baf1
MM
4748 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4749 { CPP_EOF, ERROR_MARK }
4750 };
4751
4752 return cp_parser_binary_expression (parser,
4753 map,
4754 cp_parser_logical_and_expression);
4755}
4756
a723baf1
MM
4757/* Parse the `? expression : assignment-expression' part of a
4758 conditional-expression. The LOGICAL_OR_EXPR is the
4759 logical-or-expression that started the conditional-expression.
4760 Returns a representation of the entire conditional-expression.
4761
39703eb9 4762 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
4763
4764 ? expression : assignment-expression
4765
4766 GNU Extensions:
4767
4768 ? : assignment-expression */
4769
4770static tree
94edc4ab 4771cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
4772{
4773 tree expr;
4774 tree assignment_expr;
4775
4776 /* Consume the `?' token. */
4777 cp_lexer_consume_token (parser->lexer);
4778 if (cp_parser_allow_gnu_extensions_p (parser)
4779 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4780 /* Implicit true clause. */
4781 expr = NULL_TREE;
4782 else
4783 /* Parse the expression. */
4784 expr = cp_parser_expression (parser);
4785
4786 /* The next token should be a `:'. */
4787 cp_parser_require (parser, CPP_COLON, "`:'");
4788 /* Parse the assignment-expression. */
4789 assignment_expr = cp_parser_assignment_expression (parser);
4790
4791 /* Build the conditional-expression. */
4792 return build_x_conditional_expr (logical_or_expr,
4793 expr,
4794 assignment_expr);
4795}
4796
4797/* Parse an assignment-expression.
4798
4799 assignment-expression:
4800 conditional-expression
4801 logical-or-expression assignment-operator assignment_expression
4802 throw-expression
4803
4804 Returns a representation for the expression. */
4805
4806static tree
94edc4ab 4807cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
4808{
4809 tree expr;
4810
4811 /* If the next token is the `throw' keyword, then we're looking at
4812 a throw-expression. */
4813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4814 expr = cp_parser_throw_expression (parser);
4815 /* Otherwise, it must be that we are looking at a
4816 logical-or-expression. */
4817 else
4818 {
4819 /* Parse the logical-or-expression. */
4820 expr = cp_parser_logical_or_expression (parser);
4821 /* If the next token is a `?' then we're actually looking at a
4822 conditional-expression. */
4823 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4824 return cp_parser_question_colon_clause (parser, expr);
4825 else
4826 {
4827 enum tree_code assignment_operator;
4828
4829 /* If it's an assignment-operator, we're using the second
4830 production. */
4831 assignment_operator
4832 = cp_parser_assignment_operator_opt (parser);
4833 if (assignment_operator != ERROR_MARK)
4834 {
4835 tree rhs;
4836
4837 /* Parse the right-hand side of the assignment. */
4838 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
4839 /* An assignment may not appear in a
4840 constant-expression. */
4841 if (parser->constant_expression_p)
4842 {
4843 if (!parser->allow_non_constant_expression_p)
4844 return cp_parser_non_constant_expression ("an assignment");
4845 parser->non_constant_expression_p = true;
4846 }
34cd5ae7 4847 /* Build the assignment expression. */
a723baf1
MM
4848 expr = build_x_modify_expr (expr,
4849 assignment_operator,
4850 rhs);
4851 }
4852 }
4853 }
4854
4855 return expr;
4856}
4857
4858/* Parse an (optional) assignment-operator.
4859
4860 assignment-operator: one of
4861 = *= /= %= += -= >>= <<= &= ^= |=
4862
4863 GNU Extension:
4864
4865 assignment-operator: one of
4866 <?= >?=
4867
4868 If the next token is an assignment operator, the corresponding tree
4869 code is returned, and the token is consumed. For example, for
4870 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4871 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4872 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4873 operator, ERROR_MARK is returned. */
4874
4875static enum tree_code
94edc4ab 4876cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
4877{
4878 enum tree_code op;
4879 cp_token *token;
4880
4881 /* Peek at the next toen. */
4882 token = cp_lexer_peek_token (parser->lexer);
4883
4884 switch (token->type)
4885 {
4886 case CPP_EQ:
4887 op = NOP_EXPR;
4888 break;
4889
4890 case CPP_MULT_EQ:
4891 op = MULT_EXPR;
4892 break;
4893
4894 case CPP_DIV_EQ:
4895 op = TRUNC_DIV_EXPR;
4896 break;
4897
4898 case CPP_MOD_EQ:
4899 op = TRUNC_MOD_EXPR;
4900 break;
4901
4902 case CPP_PLUS_EQ:
4903 op = PLUS_EXPR;
4904 break;
4905
4906 case CPP_MINUS_EQ:
4907 op = MINUS_EXPR;
4908 break;
4909
4910 case CPP_RSHIFT_EQ:
4911 op = RSHIFT_EXPR;
4912 break;
4913
4914 case CPP_LSHIFT_EQ:
4915 op = LSHIFT_EXPR;
4916 break;
4917
4918 case CPP_AND_EQ:
4919 op = BIT_AND_EXPR;
4920 break;
4921
4922 case CPP_XOR_EQ:
4923 op = BIT_XOR_EXPR;
4924 break;
4925
4926 case CPP_OR_EQ:
4927 op = BIT_IOR_EXPR;
4928 break;
4929
4930 case CPP_MIN_EQ:
4931 op = MIN_EXPR;
4932 break;
4933
4934 case CPP_MAX_EQ:
4935 op = MAX_EXPR;
4936 break;
4937
4938 default:
4939 /* Nothing else is an assignment operator. */
4940 op = ERROR_MARK;
4941 }
4942
4943 /* If it was an assignment operator, consume it. */
4944 if (op != ERROR_MARK)
4945 cp_lexer_consume_token (parser->lexer);
4946
4947 return op;
4948}
4949
4950/* Parse an expression.
4951
4952 expression:
4953 assignment-expression
4954 expression , assignment-expression
4955
4956 Returns a representation of the expression. */
4957
4958static tree
94edc4ab 4959cp_parser_expression (cp_parser* parser)
a723baf1
MM
4960{
4961 tree expression = NULL_TREE;
a723baf1
MM
4962
4963 while (true)
4964 {
4965 tree assignment_expression;
4966
4967 /* Parse the next assignment-expression. */
4968 assignment_expression
4969 = cp_parser_assignment_expression (parser);
4970 /* If this is the first assignment-expression, we can just
4971 save it away. */
4972 if (!expression)
4973 expression = assignment_expression;
a723baf1 4974 else
d17811fd
MM
4975 expression = build_x_compound_expr (expression,
4976 assignment_expression);
a723baf1
MM
4977 /* If the next token is not a comma, then we are done with the
4978 expression. */
4979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4980 break;
4981 /* Consume the `,'. */
4982 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
4983 /* A comma operator cannot appear in a constant-expression. */
4984 if (parser->constant_expression_p)
4985 {
4986 if (!parser->allow_non_constant_expression_p)
d17811fd
MM
4987 expression
4988 = cp_parser_non_constant_expression ("a comma operator");
14d22dd6
MM
4989 parser->non_constant_expression_p = true;
4990 }
14d22dd6 4991 }
a723baf1
MM
4992
4993 return expression;
4994}
4995
4996/* Parse a constant-expression.
4997
4998 constant-expression:
14d22dd6
MM
4999 conditional-expression
5000
5001 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5002 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5003 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5004 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5005
5006static tree
14d22dd6
MM
5007cp_parser_constant_expression (cp_parser* parser,
5008 bool allow_non_constant_p,
5009 bool *non_constant_p)
a723baf1
MM
5010{
5011 bool saved_constant_expression_p;
14d22dd6
MM
5012 bool saved_allow_non_constant_expression_p;
5013 bool saved_non_constant_expression_p;
a723baf1
MM
5014 tree expression;
5015
5016 /* It might seem that we could simply parse the
5017 conditional-expression, and then check to see if it were
5018 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5019 one that the compiler can figure out is constant, possibly after
5020 doing some simplifications or optimizations. The standard has a
5021 precise definition of constant-expression, and we must honor
5022 that, even though it is somewhat more restrictive.
5023
5024 For example:
5025
5026 int i[(2, 3)];
5027
5028 is not a legal declaration, because `(2, 3)' is not a
5029 constant-expression. The `,' operator is forbidden in a
5030 constant-expression. However, GCC's constant-folding machinery
5031 will fold this operation to an INTEGER_CST for `3'. */
5032
14d22dd6 5033 /* Save the old settings. */
a723baf1 5034 saved_constant_expression_p = parser->constant_expression_p;
14d22dd6
MM
5035 saved_allow_non_constant_expression_p
5036 = parser->allow_non_constant_expression_p;
5037 saved_non_constant_expression_p = parser->non_constant_expression_p;
a723baf1
MM
5038 /* We are now parsing a constant-expression. */
5039 parser->constant_expression_p = true;
14d22dd6
MM
5040 parser->allow_non_constant_expression_p = allow_non_constant_p;
5041 parser->non_constant_expression_p = false;
39703eb9
MM
5042 /* Although the grammar says "conditional-expression", we parse an
5043 "assignment-expression", which also permits "throw-expression"
5044 and the use of assignment operators. In the case that
5045 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5046 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5047 actually essential that we look for an assignment-expression.
5048 For example, cp_parser_initializer_clauses uses this function to
5049 determine whether a particular assignment-expression is in fact
5050 constant. */
5051 expression = cp_parser_assignment_expression (parser);
14d22dd6 5052 /* Restore the old settings. */
a723baf1 5053 parser->constant_expression_p = saved_constant_expression_p;
14d22dd6
MM
5054 parser->allow_non_constant_expression_p
5055 = saved_allow_non_constant_expression_p;
5056 if (allow_non_constant_p)
5057 *non_constant_p = parser->non_constant_expression_p;
5058 parser->non_constant_expression_p = saved_non_constant_expression_p;
a723baf1
MM
5059
5060 return expression;
5061}
5062
5063/* Statements [gram.stmt.stmt] */
5064
5065/* Parse a statement.
5066
5067 statement:
5068 labeled-statement
5069 expression-statement
5070 compound-statement
5071 selection-statement
5072 iteration-statement
5073 jump-statement
5074 declaration-statement
5075 try-block */
5076
5077static void
a5bcc582 5078cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5079{
5080 tree statement;
5081 cp_token *token;
5082 int statement_line_number;
5083
5084 /* There is no statement yet. */
5085 statement = NULL_TREE;
5086 /* Peek at the next token. */
5087 token = cp_lexer_peek_token (parser->lexer);
5088 /* Remember the line number of the first token in the statement. */
82a98427 5089 statement_line_number = token->location.line;
a723baf1
MM
5090 /* If this is a keyword, then that will often determine what kind of
5091 statement we have. */
5092 if (token->type == CPP_KEYWORD)
5093 {
5094 enum rid keyword = token->keyword;
5095
5096 switch (keyword)
5097 {
5098 case RID_CASE:
5099 case RID_DEFAULT:
a5bcc582
NS
5100 statement = cp_parser_labeled_statement (parser,
5101 in_statement_expr_p);
a723baf1
MM
5102 break;
5103
5104 case RID_IF:
5105 case RID_SWITCH:
5106 statement = cp_parser_selection_statement (parser);
5107 break;
5108
5109 case RID_WHILE:
5110 case RID_DO:
5111 case RID_FOR:
5112 statement = cp_parser_iteration_statement (parser);
5113 break;
5114
5115 case RID_BREAK:
5116 case RID_CONTINUE:
5117 case RID_RETURN:
5118 case RID_GOTO:
5119 statement = cp_parser_jump_statement (parser);
5120 break;
5121
5122 case RID_TRY:
5123 statement = cp_parser_try_block (parser);
5124 break;
5125
5126 default:
5127 /* It might be a keyword like `int' that can start a
5128 declaration-statement. */
5129 break;
5130 }
5131 }
5132 else if (token->type == CPP_NAME)
5133 {
5134 /* If the next token is a `:', then we are looking at a
5135 labeled-statement. */
5136 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5137 if (token->type == CPP_COLON)
a5bcc582 5138 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5139 }
5140 /* Anything that starts with a `{' must be a compound-statement. */
5141 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5142 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5143
5144 /* Everything else must be a declaration-statement or an
5145 expression-statement. Try for the declaration-statement
5146 first, unless we are looking at a `;', in which case we know that
5147 we have an expression-statement. */
5148 if (!statement)
5149 {
5150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5151 {
5152 cp_parser_parse_tentatively (parser);
5153 /* Try to parse the declaration-statement. */
5154 cp_parser_declaration_statement (parser);
5155 /* If that worked, we're done. */
5156 if (cp_parser_parse_definitely (parser))
5157 return;
5158 }
5159 /* Look for an expression-statement instead. */
a5bcc582 5160 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5161 }
5162
5163 /* Set the line number for the statement. */
009ed910 5164 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5165 STMT_LINENO (statement) = statement_line_number;
5166}
5167
5168/* Parse a labeled-statement.
5169
5170 labeled-statement:
5171 identifier : statement
5172 case constant-expression : statement
5173 default : statement
5174
5175 Returns the new CASE_LABEL, for a `case' or `default' label. For
5176 an ordinary label, returns a LABEL_STMT. */
5177
5178static tree
a5bcc582 5179cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5180{
5181 cp_token *token;
5182 tree statement = NULL_TREE;
5183
5184 /* The next token should be an identifier. */
5185 token = cp_lexer_peek_token (parser->lexer);
5186 if (token->type != CPP_NAME
5187 && token->type != CPP_KEYWORD)
5188 {
5189 cp_parser_error (parser, "expected labeled-statement");
5190 return error_mark_node;
5191 }
5192
5193 switch (token->keyword)
5194 {
5195 case RID_CASE:
5196 {
5197 tree expr;
5198
5199 /* Consume the `case' token. */
5200 cp_lexer_consume_token (parser->lexer);
5201 /* Parse the constant-expression. */
14d22dd6 5202 expr = cp_parser_constant_expression (parser,
d17811fd 5203 /*allow_non_constant_p=*/false,
14d22dd6 5204 NULL);
a723baf1
MM
5205 /* Create the label. */
5206 statement = finish_case_label (expr, NULL_TREE);
5207 }
5208 break;
5209
5210 case RID_DEFAULT:
5211 /* Consume the `default' token. */
5212 cp_lexer_consume_token (parser->lexer);
5213 /* Create the label. */
5214 statement = finish_case_label (NULL_TREE, NULL_TREE);
5215 break;
5216
5217 default:
5218 /* Anything else must be an ordinary label. */
5219 statement = finish_label_stmt (cp_parser_identifier (parser));
5220 break;
5221 }
5222
5223 /* Require the `:' token. */
5224 cp_parser_require (parser, CPP_COLON, "`:'");
5225 /* Parse the labeled statement. */
a5bcc582 5226 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5227
5228 /* Return the label, in the case of a `case' or `default' label. */
5229 return statement;
5230}
5231
5232/* Parse an expression-statement.
5233
5234 expression-statement:
5235 expression [opt] ;
5236
5237 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5238 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5239 indicates whether this expression-statement is part of an
5240 expression statement. */
a723baf1
MM
5241
5242static tree
a5bcc582 5243cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5244{
a5bcc582 5245 tree statement = NULL_TREE;
a723baf1 5246
a5bcc582
NS
5247 /* If the next token is a ';', then there is no expression
5248 statement. */
a723baf1 5249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582
NS
5250 statement = cp_parser_expression (parser);
5251
a723baf1 5252 /* Consume the final `;'. */
e0860732 5253 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5254
a5bcc582
NS
5255 if (in_statement_expr_p
5256 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5257 {
5258 /* This is the final expression statement of a statement
5259 expression. */
5260 statement = finish_stmt_expr_expr (statement);
5261 }
5262 else if (statement)
5263 statement = finish_expr_stmt (statement);
5264 else
5265 finish_stmt ();
5266
a723baf1
MM
5267 return statement;
5268}
5269
5270/* Parse a compound-statement.
5271
5272 compound-statement:
5273 { statement-seq [opt] }
5274
5275 Returns a COMPOUND_STMT representing the statement. */
5276
5277static tree
a5bcc582 5278cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5279{
5280 tree compound_stmt;
5281
5282 /* Consume the `{'. */
5283 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5284 return error_mark_node;
5285 /* Begin the compound-statement. */
7a3397c7 5286 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5287 /* Parse an (optional) statement-seq. */
a5bcc582 5288 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5289 /* Finish the compound-statement. */
7a3397c7 5290 finish_compound_stmt (compound_stmt);
a723baf1
MM
5291 /* Consume the `}'. */
5292 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5293
5294 return compound_stmt;
5295}
5296
5297/* Parse an (optional) statement-seq.
5298
5299 statement-seq:
5300 statement
5301 statement-seq [opt] statement */
5302
5303static void
a5bcc582 5304cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5305{
5306 /* Scan statements until there aren't any more. */
5307 while (true)
5308 {
5309 /* If we're looking at a `}', then we've run out of statements. */
5310 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5311 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5312 break;
5313
5314 /* Parse the statement. */
a5bcc582 5315 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5316 }
5317}
5318
5319/* Parse a selection-statement.
5320
5321 selection-statement:
5322 if ( condition ) statement
5323 if ( condition ) statement else statement
5324 switch ( condition ) statement
5325
5326 Returns the new IF_STMT or SWITCH_STMT. */
5327
5328static tree
94edc4ab 5329cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5330{
5331 cp_token *token;
5332 enum rid keyword;
5333
5334 /* Peek at the next token. */
5335 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5336
5337 /* See what kind of keyword it is. */
5338 keyword = token->keyword;
5339 switch (keyword)
5340 {
5341 case RID_IF:
5342 case RID_SWITCH:
5343 {
5344 tree statement;
5345 tree condition;
5346
5347 /* Look for the `('. */
5348 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5349 {
5350 cp_parser_skip_to_end_of_statement (parser);
5351 return error_mark_node;
5352 }
5353
5354 /* Begin the selection-statement. */
5355 if (keyword == RID_IF)
5356 statement = begin_if_stmt ();
5357 else
5358 statement = begin_switch_stmt ();
5359
5360 /* Parse the condition. */
5361 condition = cp_parser_condition (parser);
5362 /* Look for the `)'. */
5363 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 5364 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
5365
5366 if (keyword == RID_IF)
5367 {
5368 tree then_stmt;
5369
5370 /* Add the condition. */
5371 finish_if_stmt_cond (condition, statement);
5372
5373 /* Parse the then-clause. */
5374 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5375 finish_then_clause (statement);
5376
5377 /* If the next token is `else', parse the else-clause. */
5378 if (cp_lexer_next_token_is_keyword (parser->lexer,
5379 RID_ELSE))
5380 {
5381 tree else_stmt;
5382
5383 /* Consume the `else' keyword. */
5384 cp_lexer_consume_token (parser->lexer);
5385 /* Parse the else-clause. */
5386 else_stmt
5387 = cp_parser_implicitly_scoped_statement (parser);
5388 finish_else_clause (statement);
5389 }
5390
5391 /* Now we're all done with the if-statement. */
5392 finish_if_stmt ();
5393 }
5394 else
5395 {
5396 tree body;
5397
5398 /* Add the condition. */
5399 finish_switch_cond (condition, statement);
5400
5401 /* Parse the body of the switch-statement. */
5402 body = cp_parser_implicitly_scoped_statement (parser);
5403
5404 /* Now we're all done with the switch-statement. */
5405 finish_switch_stmt (statement);
5406 }
5407
5408 return statement;
5409 }
5410 break;
5411
5412 default:
5413 cp_parser_error (parser, "expected selection-statement");
5414 return error_mark_node;
5415 }
5416}
5417
5418/* Parse a condition.
5419
5420 condition:
5421 expression
5422 type-specifier-seq declarator = assignment-expression
5423
5424 GNU Extension:
5425
5426 condition:
5427 type-specifier-seq declarator asm-specification [opt]
5428 attributes [opt] = assignment-expression
5429
5430 Returns the expression that should be tested. */
5431
5432static tree
94edc4ab 5433cp_parser_condition (cp_parser* parser)
a723baf1
MM
5434{
5435 tree type_specifiers;
5436 const char *saved_message;
5437
5438 /* Try the declaration first. */
5439 cp_parser_parse_tentatively (parser);
5440 /* New types are not allowed in the type-specifier-seq for a
5441 condition. */
5442 saved_message = parser->type_definition_forbidden_message;
5443 parser->type_definition_forbidden_message
5444 = "types may not be defined in conditions";
5445 /* Parse the type-specifier-seq. */
5446 type_specifiers = cp_parser_type_specifier_seq (parser);
5447 /* Restore the saved message. */
5448 parser->type_definition_forbidden_message = saved_message;
5449 /* If all is well, we might be looking at a declaration. */
5450 if (!cp_parser_error_occurred (parser))
5451 {
5452 tree decl;
5453 tree asm_specification;
5454 tree attributes;
5455 tree declarator;
5456 tree initializer = NULL_TREE;
5457
5458 /* Parse the declarator. */
62b8a44e 5459 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
5460 /*ctor_dtor_or_conv_p=*/NULL);
5461 /* Parse the attributes. */
5462 attributes = cp_parser_attributes_opt (parser);
5463 /* Parse the asm-specification. */
5464 asm_specification = cp_parser_asm_specification_opt (parser);
5465 /* If the next token is not an `=', then we might still be
5466 looking at an expression. For example:
5467
5468 if (A(a).x)
5469
5470 looks like a decl-specifier-seq and a declarator -- but then
5471 there is no `=', so this is an expression. */
5472 cp_parser_require (parser, CPP_EQ, "`='");
5473 /* If we did see an `=', then we are looking at a declaration
5474 for sure. */
5475 if (cp_parser_parse_definitely (parser))
5476 {
5477 /* Create the declaration. */
5478 decl = start_decl (declarator, type_specifiers,
5479 /*initialized_p=*/true,
5480 attributes, /*prefix_attributes=*/NULL_TREE);
5481 /* Parse the assignment-expression. */
5482 initializer = cp_parser_assignment_expression (parser);
5483
5484 /* Process the initializer. */
5485 cp_finish_decl (decl,
5486 initializer,
5487 asm_specification,
5488 LOOKUP_ONLYCONVERTING);
5489
5490 return convert_from_reference (decl);
5491 }
5492 }
5493 /* If we didn't even get past the declarator successfully, we are
5494 definitely not looking at a declaration. */
5495 else
5496 cp_parser_abort_tentative_parse (parser);
5497
5498 /* Otherwise, we are looking at an expression. */
5499 return cp_parser_expression (parser);
5500}
5501
5502/* Parse an iteration-statement.
5503
5504 iteration-statement:
5505 while ( condition ) statement
5506 do statement while ( expression ) ;
5507 for ( for-init-statement condition [opt] ; expression [opt] )
5508 statement
5509
5510 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5511
5512static tree
94edc4ab 5513cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5514{
5515 cp_token *token;
5516 enum rid keyword;
5517 tree statement;
5518
5519 /* Peek at the next token. */
5520 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5521 if (!token)
5522 return error_mark_node;
5523
5524 /* See what kind of keyword it is. */
5525 keyword = token->keyword;
5526 switch (keyword)
5527 {
5528 case RID_WHILE:
5529 {
5530 tree condition;
5531
5532 /* Begin the while-statement. */
5533 statement = begin_while_stmt ();
5534 /* Look for the `('. */
5535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5536 /* Parse the condition. */
5537 condition = cp_parser_condition (parser);
5538 finish_while_stmt_cond (condition, statement);
5539 /* Look for the `)'. */
5540 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5541 /* Parse the dependent statement. */
5542 cp_parser_already_scoped_statement (parser);
5543 /* We're done with the while-statement. */
5544 finish_while_stmt (statement);
5545 }
5546 break;
5547
5548 case RID_DO:
5549 {
5550 tree expression;
5551
5552 /* Begin the do-statement. */
5553 statement = begin_do_stmt ();
5554 /* Parse the body of the do-statement. */
5555 cp_parser_implicitly_scoped_statement (parser);
5556 finish_do_body (statement);
5557 /* Look for the `while' keyword. */
5558 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5559 /* Look for the `('. */
5560 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5561 /* Parse the expression. */
5562 expression = cp_parser_expression (parser);
5563 /* We're done with the do-statement. */
5564 finish_do_stmt (expression, statement);
5565 /* Look for the `)'. */
5566 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5567 /* Look for the `;'. */
5568 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5569 }
5570 break;
5571
5572 case RID_FOR:
5573 {
5574 tree condition = NULL_TREE;
5575 tree expression = NULL_TREE;
5576
5577 /* Begin the for-statement. */
5578 statement = begin_for_stmt ();
5579 /* Look for the `('. */
5580 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5581 /* Parse the initialization. */
5582 cp_parser_for_init_statement (parser);
5583 finish_for_init_stmt (statement);
5584
5585 /* If there's a condition, process it. */
5586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5587 condition = cp_parser_condition (parser);
5588 finish_for_cond (condition, statement);
5589 /* Look for the `;'. */
5590 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5591
5592 /* If there's an expression, process it. */
5593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5594 expression = cp_parser_expression (parser);
5595 finish_for_expr (expression, statement);
5596 /* Look for the `)'. */
5597 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5598
5599 /* Parse the body of the for-statement. */
5600 cp_parser_already_scoped_statement (parser);
5601
5602 /* We're done with the for-statement. */
5603 finish_for_stmt (statement);
5604 }
5605 break;
5606
5607 default:
5608 cp_parser_error (parser, "expected iteration-statement");
5609 statement = error_mark_node;
5610 break;
5611 }
5612
5613 return statement;
5614}
5615
5616/* Parse a for-init-statement.
5617
5618 for-init-statement:
5619 expression-statement
5620 simple-declaration */
5621
5622static void
94edc4ab 5623cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5624{
5625 /* If the next token is a `;', then we have an empty
34cd5ae7 5626 expression-statement. Grammatically, this is also a
a723baf1
MM
5627 simple-declaration, but an invalid one, because it does not
5628 declare anything. Therefore, if we did not handle this case
5629 specially, we would issue an error message about an invalid
5630 declaration. */
5631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5632 {
5633 /* We're going to speculatively look for a declaration, falling back
5634 to an expression, if necessary. */
5635 cp_parser_parse_tentatively (parser);
5636 /* Parse the declaration. */
5637 cp_parser_simple_declaration (parser,
5638 /*function_definition_allowed_p=*/false);
5639 /* If the tentative parse failed, then we shall need to look for an
5640 expression-statement. */
5641 if (cp_parser_parse_definitely (parser))
5642 return;
5643 }
5644
a5bcc582 5645 cp_parser_expression_statement (parser, false);
a723baf1
MM
5646}
5647
5648/* Parse a jump-statement.
5649
5650 jump-statement:
5651 break ;
5652 continue ;
5653 return expression [opt] ;
5654 goto identifier ;
5655
5656 GNU extension:
5657
5658 jump-statement:
5659 goto * expression ;
5660
5661 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5662 GOTO_STMT. */
5663
5664static tree
94edc4ab 5665cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
5666{
5667 tree statement = error_mark_node;
5668 cp_token *token;
5669 enum rid keyword;
5670
5671 /* Peek at the next token. */
5672 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5673 if (!token)
5674 return error_mark_node;
5675
5676 /* See what kind of keyword it is. */
5677 keyword = token->keyword;
5678 switch (keyword)
5679 {
5680 case RID_BREAK:
5681 statement = finish_break_stmt ();
5682 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5683 break;
5684
5685 case RID_CONTINUE:
5686 statement = finish_continue_stmt ();
5687 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5688 break;
5689
5690 case RID_RETURN:
5691 {
5692 tree expr;
5693
5694 /* If the next token is a `;', then there is no
5695 expression. */
5696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5697 expr = cp_parser_expression (parser);
5698 else
5699 expr = NULL_TREE;
5700 /* Build the return-statement. */
5701 statement = finish_return_stmt (expr);
5702 /* Look for the final `;'. */
5703 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5704 }
5705 break;
5706
5707 case RID_GOTO:
5708 /* Create the goto-statement. */
5709 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5710 {
5711 /* Issue a warning about this use of a GNU extension. */
5712 if (pedantic)
5713 pedwarn ("ISO C++ forbids computed gotos");
5714 /* Consume the '*' token. */
5715 cp_lexer_consume_token (parser->lexer);
5716 /* Parse the dependent expression. */
5717 finish_goto_stmt (cp_parser_expression (parser));
5718 }
5719 else
5720 finish_goto_stmt (cp_parser_identifier (parser));
5721 /* Look for the final `;'. */
5722 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5723 break;
5724
5725 default:
5726 cp_parser_error (parser, "expected jump-statement");
5727 break;
5728 }
5729
5730 return statement;
5731}
5732
5733/* Parse a declaration-statement.
5734
5735 declaration-statement:
5736 block-declaration */
5737
5738static void
94edc4ab 5739cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
5740{
5741 /* Parse the block-declaration. */
5742 cp_parser_block_declaration (parser, /*statement_p=*/true);
5743
5744 /* Finish off the statement. */
5745 finish_stmt ();
5746}
5747
5748/* Some dependent statements (like `if (cond) statement'), are
5749 implicitly in their own scope. In other words, if the statement is
5750 a single statement (as opposed to a compound-statement), it is
5751 none-the-less treated as if it were enclosed in braces. Any
5752 declarations appearing in the dependent statement are out of scope
5753 after control passes that point. This function parses a statement,
5754 but ensures that is in its own scope, even if it is not a
5755 compound-statement.
5756
5757 Returns the new statement. */
5758
5759static tree
94edc4ab 5760cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
5761{
5762 tree statement;
5763
5764 /* If the token is not a `{', then we must take special action. */
5765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5766 {
5767 /* Create a compound-statement. */
7a3397c7 5768 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5769 /* Parse the dependent-statement. */
a5bcc582 5770 cp_parser_statement (parser, false);
a723baf1 5771 /* Finish the dummy compound-statement. */
7a3397c7 5772 finish_compound_stmt (statement);
a723baf1
MM
5773 }
5774 /* Otherwise, we simply parse the statement directly. */
5775 else
a5bcc582 5776 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5777
5778 /* Return the statement. */
5779 return statement;
5780}
5781
5782/* For some dependent statements (like `while (cond) statement'), we
5783 have already created a scope. Therefore, even if the dependent
5784 statement is a compound-statement, we do not want to create another
5785 scope. */
5786
5787static void
94edc4ab 5788cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
5789{
5790 /* If the token is not a `{', then we must take special action. */
5791 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5792 {
5793 tree statement;
5794
5795 /* Create a compound-statement. */
7a3397c7 5796 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 5797 /* Parse the dependent-statement. */
a5bcc582 5798 cp_parser_statement (parser, false);
a723baf1 5799 /* Finish the dummy compound-statement. */
7a3397c7 5800 finish_compound_stmt (statement);
a723baf1
MM
5801 }
5802 /* Otherwise, we simply parse the statement directly. */
5803 else
a5bcc582 5804 cp_parser_statement (parser, false);
a723baf1
MM
5805}
5806
5807/* Declarations [gram.dcl.dcl] */
5808
5809/* Parse an optional declaration-sequence.
5810
5811 declaration-seq:
5812 declaration
5813 declaration-seq declaration */
5814
5815static void
94edc4ab 5816cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
5817{
5818 while (true)
5819 {
5820 cp_token *token;
5821
5822 token = cp_lexer_peek_token (parser->lexer);
5823
5824 if (token->type == CPP_CLOSE_BRACE
5825 || token->type == CPP_EOF)
5826 break;
5827
5828 if (token->type == CPP_SEMICOLON)
5829 {
5830 /* A declaration consisting of a single semicolon is
5831 invalid. Allow it unless we're being pedantic. */
5832 if (pedantic)
5833 pedwarn ("extra `;'");
5834 cp_lexer_consume_token (parser->lexer);
5835 continue;
5836 }
5837
c838d82f 5838 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 5839 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
5840 while (pending_lang_change > 0)
5841 {
5842 push_lang_context (lang_name_c);
5843 --pending_lang_change;
5844 }
5845 while (pending_lang_change < 0)
5846 {
5847 pop_lang_context ();
5848 ++pending_lang_change;
5849 }
5850
5851 /* Parse the declaration itself. */
a723baf1
MM
5852 cp_parser_declaration (parser);
5853 }
5854}
5855
5856/* Parse a declaration.
5857
5858 declaration:
5859 block-declaration
5860 function-definition
5861 template-declaration
5862 explicit-instantiation
5863 explicit-specialization
5864 linkage-specification
1092805d
MM
5865 namespace-definition
5866
5867 GNU extension:
5868
5869 declaration:
5870 __extension__ declaration */
a723baf1
MM
5871
5872static void
94edc4ab 5873cp_parser_declaration (cp_parser* parser)
a723baf1
MM
5874{
5875 cp_token token1;
5876 cp_token token2;
1092805d
MM
5877 int saved_pedantic;
5878
5879 /* Check for the `__extension__' keyword. */
5880 if (cp_parser_extension_opt (parser, &saved_pedantic))
5881 {
5882 /* Parse the qualified declaration. */
5883 cp_parser_declaration (parser);
5884 /* Restore the PEDANTIC flag. */
5885 pedantic = saved_pedantic;
5886
5887 return;
5888 }
a723baf1
MM
5889
5890 /* Try to figure out what kind of declaration is present. */
5891 token1 = *cp_lexer_peek_token (parser->lexer);
5892 if (token1.type != CPP_EOF)
5893 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5894
5895 /* If the next token is `extern' and the following token is a string
5896 literal, then we have a linkage specification. */
5897 if (token1.keyword == RID_EXTERN
5898 && cp_parser_is_string_literal (&token2))
5899 cp_parser_linkage_specification (parser);
5900 /* If the next token is `template', then we have either a template
5901 declaration, an explicit instantiation, or an explicit
5902 specialization. */
5903 else if (token1.keyword == RID_TEMPLATE)
5904 {
5905 /* `template <>' indicates a template specialization. */
5906 if (token2.type == CPP_LESS
5907 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5908 cp_parser_explicit_specialization (parser);
5909 /* `template <' indicates a template declaration. */
5910 else if (token2.type == CPP_LESS)
5911 cp_parser_template_declaration (parser, /*member_p=*/false);
5912 /* Anything else must be an explicit instantiation. */
5913 else
5914 cp_parser_explicit_instantiation (parser);
5915 }
5916 /* If the next token is `export', then we have a template
5917 declaration. */
5918 else if (token1.keyword == RID_EXPORT)
5919 cp_parser_template_declaration (parser, /*member_p=*/false);
5920 /* If the next token is `extern', 'static' or 'inline' and the one
5921 after that is `template', we have a GNU extended explicit
5922 instantiation directive. */
5923 else if (cp_parser_allow_gnu_extensions_p (parser)
5924 && (token1.keyword == RID_EXTERN
5925 || token1.keyword == RID_STATIC
5926 || token1.keyword == RID_INLINE)
5927 && token2.keyword == RID_TEMPLATE)
5928 cp_parser_explicit_instantiation (parser);
5929 /* If the next token is `namespace', check for a named or unnamed
5930 namespace definition. */
5931 else if (token1.keyword == RID_NAMESPACE
5932 && (/* A named namespace definition. */
5933 (token2.type == CPP_NAME
5934 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5935 == CPP_OPEN_BRACE))
5936 /* An unnamed namespace definition. */
5937 || token2.type == CPP_OPEN_BRACE))
5938 cp_parser_namespace_definition (parser);
5939 /* We must have either a block declaration or a function
5940 definition. */
5941 else
5942 /* Try to parse a block-declaration, or a function-definition. */
5943 cp_parser_block_declaration (parser, /*statement_p=*/false);
5944}
5945
5946/* Parse a block-declaration.
5947
5948 block-declaration:
5949 simple-declaration
5950 asm-definition
5951 namespace-alias-definition
5952 using-declaration
5953 using-directive
5954
5955 GNU Extension:
5956
5957 block-declaration:
5958 __extension__ block-declaration
5959 label-declaration
5960
34cd5ae7 5961 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
5962 part of a declaration-statement. */
5963
5964static void
5965cp_parser_block_declaration (cp_parser *parser,
5966 bool statement_p)
5967{
5968 cp_token *token1;
5969 int saved_pedantic;
5970
5971 /* Check for the `__extension__' keyword. */
5972 if (cp_parser_extension_opt (parser, &saved_pedantic))
5973 {
5974 /* Parse the qualified declaration. */
5975 cp_parser_block_declaration (parser, statement_p);
5976 /* Restore the PEDANTIC flag. */
5977 pedantic = saved_pedantic;
5978
5979 return;
5980 }
5981
5982 /* Peek at the next token to figure out which kind of declaration is
5983 present. */
5984 token1 = cp_lexer_peek_token (parser->lexer);
5985
5986 /* If the next keyword is `asm', we have an asm-definition. */
5987 if (token1->keyword == RID_ASM)
5988 {
5989 if (statement_p)
5990 cp_parser_commit_to_tentative_parse (parser);
5991 cp_parser_asm_definition (parser);
5992 }
5993 /* If the next keyword is `namespace', we have a
5994 namespace-alias-definition. */
5995 else if (token1->keyword == RID_NAMESPACE)
5996 cp_parser_namespace_alias_definition (parser);
5997 /* If the next keyword is `using', we have either a
5998 using-declaration or a using-directive. */
5999 else if (token1->keyword == RID_USING)
6000 {
6001 cp_token *token2;
6002
6003 if (statement_p)
6004 cp_parser_commit_to_tentative_parse (parser);
6005 /* If the token after `using' is `namespace', then we have a
6006 using-directive. */
6007 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6008 if (token2->keyword == RID_NAMESPACE)
6009 cp_parser_using_directive (parser);
6010 /* Otherwise, it's a using-declaration. */
6011 else
6012 cp_parser_using_declaration (parser);
6013 }
6014 /* If the next keyword is `__label__' we have a label declaration. */
6015 else if (token1->keyword == RID_LABEL)
6016 {
6017 if (statement_p)
6018 cp_parser_commit_to_tentative_parse (parser);
6019 cp_parser_label_declaration (parser);
6020 }
6021 /* Anything else must be a simple-declaration. */
6022 else
6023 cp_parser_simple_declaration (parser, !statement_p);
6024}
6025
6026/* Parse a simple-declaration.
6027
6028 simple-declaration:
6029 decl-specifier-seq [opt] init-declarator-list [opt] ;
6030
6031 init-declarator-list:
6032 init-declarator
6033 init-declarator-list , init-declarator
6034
34cd5ae7 6035 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6036 function-definition as a simple-declaration. */
a723baf1
MM
6037
6038static void
94edc4ab
NN
6039cp_parser_simple_declaration (cp_parser* parser,
6040 bool function_definition_allowed_p)
a723baf1
MM
6041{
6042 tree decl_specifiers;
6043 tree attributes;
a723baf1
MM
6044 bool declares_class_or_enum;
6045 bool saw_declarator;
6046
6047 /* Defer access checks until we know what is being declared; the
6048 checks for names appearing in the decl-specifier-seq should be
6049 done as if we were in the scope of the thing being declared. */
8d241e0b 6050 push_deferring_access_checks (dk_deferred);
cf22909c 6051
a723baf1
MM
6052 /* Parse the decl-specifier-seq. We have to keep track of whether
6053 or not the decl-specifier-seq declares a named class or
6054 enumeration type, since that is the only case in which the
6055 init-declarator-list is allowed to be empty.
6056
6057 [dcl.dcl]
6058
6059 In a simple-declaration, the optional init-declarator-list can be
6060 omitted only when declaring a class or enumeration, that is when
6061 the decl-specifier-seq contains either a class-specifier, an
6062 elaborated-type-specifier, or an enum-specifier. */
6063 decl_specifiers
6064 = cp_parser_decl_specifier_seq (parser,
6065 CP_PARSER_FLAGS_OPTIONAL,
6066 &attributes,
6067 &declares_class_or_enum);
6068 /* We no longer need to defer access checks. */
cf22909c 6069 stop_deferring_access_checks ();
24c0ef37 6070
39703eb9
MM
6071 /* In a block scope, a valid declaration must always have a
6072 decl-specifier-seq. By not trying to parse declarators, we can
6073 resolve the declaration/expression ambiguity more quickly. */
6074 if (!function_definition_allowed_p && !decl_specifiers)
6075 {
6076 cp_parser_error (parser, "expected declaration");
6077 goto done;
6078 }
6079
8fbc5ae7
MM
6080 /* If the next two tokens are both identifiers, the code is
6081 erroneous. The usual cause of this situation is code like:
6082
6083 T t;
6084
6085 where "T" should name a type -- but does not. */
6086 if (cp_parser_diagnose_invalid_type_name (parser))
6087 {
8d241e0b 6088 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6089 looking at a declaration. */
6090 cp_parser_commit_to_tentative_parse (parser);
6091 /* Give up. */
39703eb9 6092 goto done;
8fbc5ae7
MM
6093 }
6094
a723baf1
MM
6095 /* Keep going until we hit the `;' at the end of the simple
6096 declaration. */
6097 saw_declarator = false;
6098 while (cp_lexer_next_token_is_not (parser->lexer,
6099 CPP_SEMICOLON))
6100 {
6101 cp_token *token;
6102 bool function_definition_p;
6103
6104 saw_declarator = true;
6105 /* Parse the init-declarator. */
6106 cp_parser_init_declarator (parser, decl_specifiers, attributes,
a723baf1
MM
6107 function_definition_allowed_p,
6108 /*member_p=*/false,
6109 &function_definition_p);
1fb3244a
MM
6110 /* If an error occurred while parsing tentatively, exit quickly.
6111 (That usually happens when in the body of a function; each
6112 statement is treated as a declaration-statement until proven
6113 otherwise.) */
6114 if (cp_parser_error_occurred (parser))
39703eb9 6115 goto done;
a723baf1
MM
6116 /* Handle function definitions specially. */
6117 if (function_definition_p)
6118 {
6119 /* If the next token is a `,', then we are probably
6120 processing something like:
6121
6122 void f() {}, *p;
6123
6124 which is erroneous. */
6125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6126 error ("mixing declarations and function-definitions is forbidden");
6127 /* Otherwise, we're done with the list of declarators. */
6128 else
24c0ef37 6129 {
cf22909c 6130 pop_deferring_access_checks ();
24c0ef37
GS
6131 return;
6132 }
a723baf1
MM
6133 }
6134 /* The next token should be either a `,' or a `;'. */
6135 token = cp_lexer_peek_token (parser->lexer);
6136 /* If it's a `,', there are more declarators to come. */
6137 if (token->type == CPP_COMMA)
6138 cp_lexer_consume_token (parser->lexer);
6139 /* If it's a `;', we are done. */
6140 else if (token->type == CPP_SEMICOLON)
6141 break;
6142 /* Anything else is an error. */
6143 else
6144 {
6145 cp_parser_error (parser, "expected `,' or `;'");
6146 /* Skip tokens until we reach the end of the statement. */
6147 cp_parser_skip_to_end_of_statement (parser);
39703eb9 6148 goto done;
a723baf1
MM
6149 }
6150 /* After the first time around, a function-definition is not
6151 allowed -- even if it was OK at first. For example:
6152
6153 int i, f() {}
6154
6155 is not valid. */
6156 function_definition_allowed_p = false;
6157 }
6158
6159 /* Issue an error message if no declarators are present, and the
6160 decl-specifier-seq does not itself declare a class or
6161 enumeration. */
6162 if (!saw_declarator)
6163 {
6164 if (cp_parser_declares_only_class_p (parser))
6165 shadow_tag (decl_specifiers);
6166 /* Perform any deferred access checks. */
cf22909c 6167 perform_deferred_access_checks ();
a723baf1
MM
6168 }
6169
6170 /* Consume the `;'. */
6171 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6172
6173 /* Mark all the classes that appeared in the decl-specifier-seq as
6174 having received a `;'. */
6175 note_list_got_semicolon (decl_specifiers);
39703eb9
MM
6176
6177 done:
6178 pop_deferring_access_checks ();
a723baf1
MM
6179}
6180
6181/* Parse a decl-specifier-seq.
6182
6183 decl-specifier-seq:
6184 decl-specifier-seq [opt] decl-specifier
6185
6186 decl-specifier:
6187 storage-class-specifier
6188 type-specifier
6189 function-specifier
6190 friend
6191 typedef
6192
6193 GNU Extension:
6194
6195 decl-specifier-seq:
6196 decl-specifier-seq [opt] attributes
6197
6198 Returns a TREE_LIST, giving the decl-specifiers in the order they
6199 appear in the source code. The TREE_VALUE of each node is the
6200 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6201 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6202 representation of a type-specifier, see cp_parser_type_specifier.
6203
6204 If there are attributes, they will be stored in *ATTRIBUTES,
6205 represented as described above cp_parser_attributes.
6206
6207 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6208 appears, and the entity that will be a friend is not going to be a
6209 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6210 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6211 friendship is granted might not be a class. */
6212
6213static tree
94edc4ab
NN
6214cp_parser_decl_specifier_seq (cp_parser* parser,
6215 cp_parser_flags flags,
6216 tree* attributes,
6217 bool* declares_class_or_enum)
a723baf1
MM
6218{
6219 tree decl_specs = NULL_TREE;
6220 bool friend_p = false;
f2ce60b8
NS
6221 bool constructor_possible_p = !parser->in_declarator_p;
6222
a723baf1
MM
6223 /* Assume no class or enumeration type is declared. */
6224 *declares_class_or_enum = false;
6225
6226 /* Assume there are no attributes. */
6227 *attributes = NULL_TREE;
6228
6229 /* Keep reading specifiers until there are no more to read. */
6230 while (true)
6231 {
6232 tree decl_spec = NULL_TREE;
6233 bool constructor_p;
6234 cp_token *token;
6235
6236 /* Peek at the next token. */
6237 token = cp_lexer_peek_token (parser->lexer);
6238 /* Handle attributes. */
6239 if (token->keyword == RID_ATTRIBUTE)
6240 {
6241 /* Parse the attributes. */
6242 decl_spec = cp_parser_attributes_opt (parser);
6243 /* Add them to the list. */
6244 *attributes = chainon (*attributes, decl_spec);
6245 continue;
6246 }
6247 /* If the next token is an appropriate keyword, we can simply
6248 add it to the list. */
6249 switch (token->keyword)
6250 {
6251 case RID_FRIEND:
6252 /* decl-specifier:
6253 friend */
6254 friend_p = true;
6255 /* The representation of the specifier is simply the
6256 appropriate TREE_IDENTIFIER node. */
6257 decl_spec = token->value;
6258 /* Consume the token. */
6259 cp_lexer_consume_token (parser->lexer);
6260 break;
6261
6262 /* function-specifier:
6263 inline
6264 virtual
6265 explicit */
6266 case RID_INLINE:
6267 case RID_VIRTUAL:
6268 case RID_EXPLICIT:
6269 decl_spec = cp_parser_function_specifier_opt (parser);
6270 break;
6271
6272 /* decl-specifier:
6273 typedef */
6274 case RID_TYPEDEF:
6275 /* The representation of the specifier is simply the
6276 appropriate TREE_IDENTIFIER node. */
6277 decl_spec = token->value;
6278 /* Consume the token. */
6279 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6280 /* A constructor declarator cannot appear in a typedef. */
6281 constructor_possible_p = false;
c006d942
MM
6282 /* The "typedef" keyword can only occur in a declaration; we
6283 may as well commit at this point. */
6284 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6285 break;
6286
6287 /* storage-class-specifier:
6288 auto
6289 register
6290 static
6291 extern
6292 mutable
6293
6294 GNU Extension:
6295 thread */
6296 case RID_AUTO:
6297 case RID_REGISTER:
6298 case RID_STATIC:
6299 case RID_EXTERN:
6300 case RID_MUTABLE:
6301 case RID_THREAD:
6302 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6303 break;
6304
6305 default:
6306 break;
6307 }
6308
6309 /* Constructors are a special case. The `S' in `S()' is not a
6310 decl-specifier; it is the beginning of the declarator. */
6311 constructor_p = (!decl_spec
2050a1bb 6312 && constructor_possible_p
a723baf1
MM
6313 && cp_parser_constructor_declarator_p (parser,
6314 friend_p));
6315
6316 /* If we don't have a DECL_SPEC yet, then we must be looking at
6317 a type-specifier. */
6318 if (!decl_spec && !constructor_p)
6319 {
6320 bool decl_spec_declares_class_or_enum;
6321 bool is_cv_qualifier;
6322
6323 decl_spec
6324 = cp_parser_type_specifier (parser, flags,
6325 friend_p,
6326 /*is_declaration=*/true,
6327 &decl_spec_declares_class_or_enum,
6328 &is_cv_qualifier);
6329
6330 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6331
6332 /* If this type-specifier referenced a user-defined type
6333 (a typedef, class-name, etc.), then we can't allow any
6334 more such type-specifiers henceforth.
6335
6336 [dcl.spec]
6337
6338 The longest sequence of decl-specifiers that could
6339 possibly be a type name is taken as the
6340 decl-specifier-seq of a declaration. The sequence shall
6341 be self-consistent as described below.
6342
6343 [dcl.type]
6344
6345 As a general rule, at most one type-specifier is allowed
6346 in the complete decl-specifier-seq of a declaration. The
6347 only exceptions are the following:
6348
6349 -- const or volatile can be combined with any other
6350 type-specifier.
6351
6352 -- signed or unsigned can be combined with char, long,
6353 short, or int.
6354
6355 -- ..
6356
6357 Example:
6358
6359 typedef char* Pc;
6360 void g (const int Pc);
6361
6362 Here, Pc is *not* part of the decl-specifier seq; it's
6363 the declarator. Therefore, once we see a type-specifier
6364 (other than a cv-qualifier), we forbid any additional
6365 user-defined types. We *do* still allow things like `int
6366 int' to be considered a decl-specifier-seq, and issue the
6367 error message later. */
6368 if (decl_spec && !is_cv_qualifier)
6369 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6370 /* A constructor declarator cannot follow a type-specifier. */
6371 if (decl_spec)
6372 constructor_possible_p = false;
a723baf1
MM
6373 }
6374
6375 /* If we still do not have a DECL_SPEC, then there are no more
6376 decl-specifiers. */
6377 if (!decl_spec)
6378 {
6379 /* Issue an error message, unless the entire construct was
6380 optional. */
6381 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6382 {
6383 cp_parser_error (parser, "expected decl specifier");
6384 return error_mark_node;
6385 }
6386
6387 break;
6388 }
6389
6390 /* Add the DECL_SPEC to the list of specifiers. */
6391 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6392
6393 /* After we see one decl-specifier, further decl-specifiers are
6394 always optional. */
6395 flags |= CP_PARSER_FLAGS_OPTIONAL;
6396 }
6397
6398 /* We have built up the DECL_SPECS in reverse order. Return them in
6399 the correct order. */
6400 return nreverse (decl_specs);
6401}
6402
6403/* Parse an (optional) storage-class-specifier.
6404
6405 storage-class-specifier:
6406 auto
6407 register
6408 static
6409 extern
6410 mutable
6411
6412 GNU Extension:
6413
6414 storage-class-specifier:
6415 thread
6416
6417 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6418
6419static tree
94edc4ab 6420cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6421{
6422 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6423 {
6424 case RID_AUTO:
6425 case RID_REGISTER:
6426 case RID_STATIC:
6427 case RID_EXTERN:
6428 case RID_MUTABLE:
6429 case RID_THREAD:
6430 /* Consume the token. */
6431 return cp_lexer_consume_token (parser->lexer)->value;
6432
6433 default:
6434 return NULL_TREE;
6435 }
6436}
6437
6438/* Parse an (optional) function-specifier.
6439
6440 function-specifier:
6441 inline
6442 virtual
6443 explicit
6444
6445 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6446
6447static tree
94edc4ab 6448cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6449{
6450 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6451 {
6452 case RID_INLINE:
6453 case RID_VIRTUAL:
6454 case RID_EXPLICIT:
6455 /* Consume the token. */
6456 return cp_lexer_consume_token (parser->lexer)->value;
6457
6458 default:
6459 return NULL_TREE;
6460 }
6461}
6462
6463/* Parse a linkage-specification.
6464
6465 linkage-specification:
6466 extern string-literal { declaration-seq [opt] }
6467 extern string-literal declaration */
6468
6469static void
94edc4ab 6470cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6471{
6472 cp_token *token;
6473 tree linkage;
6474
6475 /* Look for the `extern' keyword. */
6476 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6477
6478 /* Peek at the next token. */
6479 token = cp_lexer_peek_token (parser->lexer);
6480 /* If it's not a string-literal, then there's a problem. */
6481 if (!cp_parser_is_string_literal (token))
6482 {
6483 cp_parser_error (parser, "expected language-name");
6484 return;
6485 }
6486 /* Consume the token. */
6487 cp_lexer_consume_token (parser->lexer);
6488
6489 /* Transform the literal into an identifier. If the literal is a
6490 wide-character string, or contains embedded NULs, then we can't
6491 handle it as the user wants. */
6492 if (token->type == CPP_WSTRING
6493 || (strlen (TREE_STRING_POINTER (token->value))
6494 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6495 {
6496 cp_parser_error (parser, "invalid linkage-specification");
6497 /* Assume C++ linkage. */
6498 linkage = get_identifier ("c++");
6499 }
6500 /* If it's a simple string constant, things are easier. */
6501 else
6502 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6503
6504 /* We're now using the new linkage. */
6505 push_lang_context (linkage);
6506
6507 /* If the next token is a `{', then we're using the first
6508 production. */
6509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6510 {
6511 /* Consume the `{' token. */
6512 cp_lexer_consume_token (parser->lexer);
6513 /* Parse the declarations. */
6514 cp_parser_declaration_seq_opt (parser);
6515 /* Look for the closing `}'. */
6516 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6517 }
6518 /* Otherwise, there's just one declaration. */
6519 else
6520 {
6521 bool saved_in_unbraced_linkage_specification_p;
6522
6523 saved_in_unbraced_linkage_specification_p
6524 = parser->in_unbraced_linkage_specification_p;
6525 parser->in_unbraced_linkage_specification_p = true;
6526 have_extern_spec = true;
6527 cp_parser_declaration (parser);
6528 have_extern_spec = false;
6529 parser->in_unbraced_linkage_specification_p
6530 = saved_in_unbraced_linkage_specification_p;
6531 }
6532
6533 /* We're done with the linkage-specification. */
6534 pop_lang_context ();
6535}
6536
6537/* Special member functions [gram.special] */
6538
6539/* Parse a conversion-function-id.
6540
6541 conversion-function-id:
6542 operator conversion-type-id
6543
6544 Returns an IDENTIFIER_NODE representing the operator. */
6545
6546static tree
94edc4ab 6547cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6548{
6549 tree type;
6550 tree saved_scope;
6551 tree saved_qualifying_scope;
6552 tree saved_object_scope;
6553
6554 /* Look for the `operator' token. */
6555 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6556 return error_mark_node;
6557 /* When we parse the conversion-type-id, the current scope will be
6558 reset. However, we need that information in able to look up the
6559 conversion function later, so we save it here. */
6560 saved_scope = parser->scope;
6561 saved_qualifying_scope = parser->qualifying_scope;
6562 saved_object_scope = parser->object_scope;
6563 /* We must enter the scope of the class so that the names of
6564 entities declared within the class are available in the
6565 conversion-type-id. For example, consider:
6566
6567 struct S {
6568 typedef int I;
6569 operator I();
6570 };
6571
6572 S::operator I() { ... }
6573
6574 In order to see that `I' is a type-name in the definition, we
6575 must be in the scope of `S'. */
6576 if (saved_scope)
6577 push_scope (saved_scope);
6578 /* Parse the conversion-type-id. */
6579 type = cp_parser_conversion_type_id (parser);
6580 /* Leave the scope of the class, if any. */
6581 if (saved_scope)
6582 pop_scope (saved_scope);
6583 /* Restore the saved scope. */
6584 parser->scope = saved_scope;
6585 parser->qualifying_scope = saved_qualifying_scope;
6586 parser->object_scope = saved_object_scope;
6587 /* If the TYPE is invalid, indicate failure. */
6588 if (type == error_mark_node)
6589 return error_mark_node;
6590 return mangle_conv_op_name_for_type (type);
6591}
6592
6593/* Parse a conversion-type-id:
6594
6595 conversion-type-id:
6596 type-specifier-seq conversion-declarator [opt]
6597
6598 Returns the TYPE specified. */
6599
6600static tree
94edc4ab 6601cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6602{
6603 tree attributes;
6604 tree type_specifiers;
6605 tree declarator;
6606
6607 /* Parse the attributes. */
6608 attributes = cp_parser_attributes_opt (parser);
6609 /* Parse the type-specifiers. */
6610 type_specifiers = cp_parser_type_specifier_seq (parser);
6611 /* If that didn't work, stop. */
6612 if (type_specifiers == error_mark_node)
6613 return error_mark_node;
6614 /* Parse the conversion-declarator. */
6615 declarator = cp_parser_conversion_declarator_opt (parser);
6616
6617 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6618 /*initialized=*/0, &attributes);
6619}
6620
6621/* Parse an (optional) conversion-declarator.
6622
6623 conversion-declarator:
6624 ptr-operator conversion-declarator [opt]
6625
6626 Returns a representation of the declarator. See
6627 cp_parser_declarator for details. */
6628
6629static tree
94edc4ab 6630cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
6631{
6632 enum tree_code code;
6633 tree class_type;
6634 tree cv_qualifier_seq;
6635
6636 /* We don't know if there's a ptr-operator next, or not. */
6637 cp_parser_parse_tentatively (parser);
6638 /* Try the ptr-operator. */
6639 code = cp_parser_ptr_operator (parser, &class_type,
6640 &cv_qualifier_seq);
6641 /* If it worked, look for more conversion-declarators. */
6642 if (cp_parser_parse_definitely (parser))
6643 {
6644 tree declarator;
6645
6646 /* Parse another optional declarator. */
6647 declarator = cp_parser_conversion_declarator_opt (parser);
6648
6649 /* Create the representation of the declarator. */
6650 if (code == INDIRECT_REF)
6651 declarator = make_pointer_declarator (cv_qualifier_seq,
6652 declarator);
6653 else
6654 declarator = make_reference_declarator (cv_qualifier_seq,
6655 declarator);
6656
6657 /* Handle the pointer-to-member case. */
6658 if (class_type)
6659 declarator = build_nt (SCOPE_REF, class_type, declarator);
6660
6661 return declarator;
6662 }
6663
6664 return NULL_TREE;
6665}
6666
6667/* Parse an (optional) ctor-initializer.
6668
6669 ctor-initializer:
6670 : mem-initializer-list
6671
6672 Returns TRUE iff the ctor-initializer was actually present. */
6673
6674static bool
94edc4ab 6675cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
6676{
6677 /* If the next token is not a `:', then there is no
6678 ctor-initializer. */
6679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6680 {
6681 /* Do default initialization of any bases and members. */
6682 if (DECL_CONSTRUCTOR_P (current_function_decl))
6683 finish_mem_initializers (NULL_TREE);
6684
6685 return false;
6686 }
6687
6688 /* Consume the `:' token. */
6689 cp_lexer_consume_token (parser->lexer);
6690 /* And the mem-initializer-list. */
6691 cp_parser_mem_initializer_list (parser);
6692
6693 return true;
6694}
6695
6696/* Parse a mem-initializer-list.
6697
6698 mem-initializer-list:
6699 mem-initializer
6700 mem-initializer , mem-initializer-list */
6701
6702static void
94edc4ab 6703cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
6704{
6705 tree mem_initializer_list = NULL_TREE;
6706
6707 /* Let the semantic analysis code know that we are starting the
6708 mem-initializer-list. */
0e136342
MM
6709 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6710 error ("only constructors take base initializers");
a723baf1
MM
6711
6712 /* Loop through the list. */
6713 while (true)
6714 {
6715 tree mem_initializer;
6716
6717 /* Parse the mem-initializer. */
6718 mem_initializer = cp_parser_mem_initializer (parser);
6719 /* Add it to the list, unless it was erroneous. */
6720 if (mem_initializer)
6721 {
6722 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6723 mem_initializer_list = mem_initializer;
6724 }
6725 /* If the next token is not a `,', we're done. */
6726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6727 break;
6728 /* Consume the `,' token. */
6729 cp_lexer_consume_token (parser->lexer);
6730 }
6731
6732 /* Perform semantic analysis. */
0e136342
MM
6733 if (DECL_CONSTRUCTOR_P (current_function_decl))
6734 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
6735}
6736
6737/* Parse a mem-initializer.
6738
6739 mem-initializer:
6740 mem-initializer-id ( expression-list [opt] )
6741
6742 GNU extension:
6743
6744 mem-initializer:
34cd5ae7 6745 ( expression-list [opt] )
a723baf1
MM
6746
6747 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6748 class) or FIELD_DECL (for a non-static data member) to initialize;
6749 the TREE_VALUE is the expression-list. */
6750
6751static tree
94edc4ab 6752cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
6753{
6754 tree mem_initializer_id;
6755 tree expression_list;
1f5a253a
NS
6756 tree member;
6757
a723baf1
MM
6758 /* Find out what is being initialized. */
6759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6760 {
6761 pedwarn ("anachronistic old-style base class initializer");
6762 mem_initializer_id = NULL_TREE;
6763 }
6764 else
6765 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
6766 member = expand_member_init (mem_initializer_id);
6767 if (member && !DECL_P (member))
6768 in_base_initializer = 1;
7efa3e22 6769
39703eb9
MM
6770 expression_list
6771 = cp_parser_parenthesized_expression_list (parser, false,
6772 /*non_constant_p=*/NULL);
7efa3e22 6773 if (!expression_list)
a723baf1 6774 expression_list = void_type_node;
a723baf1 6775
1f5a253a
NS
6776 in_base_initializer = 0;
6777
6778 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
6779}
6780
6781/* Parse a mem-initializer-id.
6782
6783 mem-initializer-id:
6784 :: [opt] nested-name-specifier [opt] class-name
6785 identifier
6786
6787 Returns a TYPE indicating the class to be initializer for the first
6788 production. Returns an IDENTIFIER_NODE indicating the data member
6789 to be initialized for the second production. */
6790
6791static tree
94edc4ab 6792cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
6793{
6794 bool global_scope_p;
6795 bool nested_name_specifier_p;
6796 tree id;
6797
6798 /* Look for the optional `::' operator. */
6799 global_scope_p
6800 = (cp_parser_global_scope_opt (parser,
6801 /*current_scope_valid_p=*/false)
6802 != NULL_TREE);
6803 /* Look for the optional nested-name-specifier. The simplest way to
6804 implement:
6805
6806 [temp.res]
6807
6808 The keyword `typename' is not permitted in a base-specifier or
6809 mem-initializer; in these contexts a qualified name that
6810 depends on a template-parameter is implicitly assumed to be a
6811 type name.
6812
6813 is to assume that we have seen the `typename' keyword at this
6814 point. */
6815 nested_name_specifier_p
6816 = (cp_parser_nested_name_specifier_opt (parser,
6817 /*typename_keyword_p=*/true,
6818 /*check_dependency_p=*/true,
6819 /*type_p=*/true)
6820 != NULL_TREE);
6821 /* If there is a `::' operator or a nested-name-specifier, then we
6822 are definitely looking for a class-name. */
6823 if (global_scope_p || nested_name_specifier_p)
6824 return cp_parser_class_name (parser,
6825 /*typename_keyword_p=*/true,
6826 /*template_keyword_p=*/false,
6827 /*type_p=*/false,
a723baf1
MM
6828 /*check_dependency_p=*/true,
6829 /*class_head_p=*/false);
6830 /* Otherwise, we could also be looking for an ordinary identifier. */
6831 cp_parser_parse_tentatively (parser);
6832 /* Try a class-name. */
6833 id = cp_parser_class_name (parser,
6834 /*typename_keyword_p=*/true,
6835 /*template_keyword_p=*/false,
6836 /*type_p=*/false,
a723baf1
MM
6837 /*check_dependency_p=*/true,
6838 /*class_head_p=*/false);
6839 /* If we found one, we're done. */
6840 if (cp_parser_parse_definitely (parser))
6841 return id;
6842 /* Otherwise, look for an ordinary identifier. */
6843 return cp_parser_identifier (parser);
6844}
6845
6846/* Overloading [gram.over] */
6847
6848/* Parse an operator-function-id.
6849
6850 operator-function-id:
6851 operator operator
6852
6853 Returns an IDENTIFIER_NODE for the operator which is a
6854 human-readable spelling of the identifier, e.g., `operator +'. */
6855
6856static tree
94edc4ab 6857cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
6858{
6859 /* Look for the `operator' keyword. */
6860 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6861 return error_mark_node;
6862 /* And then the name of the operator itself. */
6863 return cp_parser_operator (parser);
6864}
6865
6866/* Parse an operator.
6867
6868 operator:
6869 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6870 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6871 || ++ -- , ->* -> () []
6872
6873 GNU Extensions:
6874
6875 operator:
6876 <? >? <?= >?=
6877
6878 Returns an IDENTIFIER_NODE for the operator which is a
6879 human-readable spelling of the identifier, e.g., `operator +'. */
6880
6881static tree
94edc4ab 6882cp_parser_operator (cp_parser* parser)
a723baf1
MM
6883{
6884 tree id = NULL_TREE;
6885 cp_token *token;
6886
6887 /* Peek at the next token. */
6888 token = cp_lexer_peek_token (parser->lexer);
6889 /* Figure out which operator we have. */
6890 switch (token->type)
6891 {
6892 case CPP_KEYWORD:
6893 {
6894 enum tree_code op;
6895
6896 /* The keyword should be either `new' or `delete'. */
6897 if (token->keyword == RID_NEW)
6898 op = NEW_EXPR;
6899 else if (token->keyword == RID_DELETE)
6900 op = DELETE_EXPR;
6901 else
6902 break;
6903
6904 /* Consume the `new' or `delete' token. */
6905 cp_lexer_consume_token (parser->lexer);
6906
6907 /* Peek at the next token. */
6908 token = cp_lexer_peek_token (parser->lexer);
6909 /* If it's a `[' token then this is the array variant of the
6910 operator. */
6911 if (token->type == CPP_OPEN_SQUARE)
6912 {
6913 /* Consume the `[' token. */
6914 cp_lexer_consume_token (parser->lexer);
6915 /* Look for the `]' token. */
6916 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6917 id = ansi_opname (op == NEW_EXPR
6918 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6919 }
6920 /* Otherwise, we have the non-array variant. */
6921 else
6922 id = ansi_opname (op);
6923
6924 return id;
6925 }
6926
6927 case CPP_PLUS:
6928 id = ansi_opname (PLUS_EXPR);
6929 break;
6930
6931 case CPP_MINUS:
6932 id = ansi_opname (MINUS_EXPR);
6933 break;
6934
6935 case CPP_MULT:
6936 id = ansi_opname (MULT_EXPR);
6937 break;
6938
6939 case CPP_DIV:
6940 id = ansi_opname (TRUNC_DIV_EXPR);
6941 break;
6942
6943 case CPP_MOD:
6944 id = ansi_opname (TRUNC_MOD_EXPR);
6945 break;
6946
6947 case CPP_XOR:
6948 id = ansi_opname (BIT_XOR_EXPR);
6949 break;
6950
6951 case CPP_AND:
6952 id = ansi_opname (BIT_AND_EXPR);
6953 break;
6954
6955 case CPP_OR:
6956 id = ansi_opname (BIT_IOR_EXPR);
6957 break;
6958
6959 case CPP_COMPL:
6960 id = ansi_opname (BIT_NOT_EXPR);
6961 break;
6962
6963 case CPP_NOT:
6964 id = ansi_opname (TRUTH_NOT_EXPR);
6965 break;
6966
6967 case CPP_EQ:
6968 id = ansi_assopname (NOP_EXPR);
6969 break;
6970
6971 case CPP_LESS:
6972 id = ansi_opname (LT_EXPR);
6973 break;
6974
6975 case CPP_GREATER:
6976 id = ansi_opname (GT_EXPR);
6977 break;
6978
6979 case CPP_PLUS_EQ:
6980 id = ansi_assopname (PLUS_EXPR);
6981 break;
6982
6983 case CPP_MINUS_EQ:
6984 id = ansi_assopname (MINUS_EXPR);
6985 break;
6986
6987 case CPP_MULT_EQ:
6988 id = ansi_assopname (MULT_EXPR);
6989 break;
6990
6991 case CPP_DIV_EQ:
6992 id = ansi_assopname (TRUNC_DIV_EXPR);
6993 break;
6994
6995 case CPP_MOD_EQ:
6996 id = ansi_assopname (TRUNC_MOD_EXPR);
6997 break;
6998
6999 case CPP_XOR_EQ:
7000 id = ansi_assopname (BIT_XOR_EXPR);
7001 break;
7002
7003 case CPP_AND_EQ:
7004 id = ansi_assopname (BIT_AND_EXPR);
7005 break;
7006
7007 case CPP_OR_EQ:
7008 id = ansi_assopname (BIT_IOR_EXPR);
7009 break;
7010
7011 case CPP_LSHIFT:
7012 id = ansi_opname (LSHIFT_EXPR);
7013 break;
7014
7015 case CPP_RSHIFT:
7016 id = ansi_opname (RSHIFT_EXPR);
7017 break;
7018
7019 case CPP_LSHIFT_EQ:
7020 id = ansi_assopname (LSHIFT_EXPR);
7021 break;
7022
7023 case CPP_RSHIFT_EQ:
7024 id = ansi_assopname (RSHIFT_EXPR);
7025 break;
7026
7027 case CPP_EQ_EQ:
7028 id = ansi_opname (EQ_EXPR);
7029 break;
7030
7031 case CPP_NOT_EQ:
7032 id = ansi_opname (NE_EXPR);
7033 break;
7034
7035 case CPP_LESS_EQ:
7036 id = ansi_opname (LE_EXPR);
7037 break;
7038
7039 case CPP_GREATER_EQ:
7040 id = ansi_opname (GE_EXPR);
7041 break;
7042
7043 case CPP_AND_AND:
7044 id = ansi_opname (TRUTH_ANDIF_EXPR);
7045 break;
7046
7047 case CPP_OR_OR:
7048 id = ansi_opname (TRUTH_ORIF_EXPR);
7049 break;
7050
7051 case CPP_PLUS_PLUS:
7052 id = ansi_opname (POSTINCREMENT_EXPR);
7053 break;
7054
7055 case CPP_MINUS_MINUS:
7056 id = ansi_opname (PREDECREMENT_EXPR);
7057 break;
7058
7059 case CPP_COMMA:
7060 id = ansi_opname (COMPOUND_EXPR);
7061 break;
7062
7063 case CPP_DEREF_STAR:
7064 id = ansi_opname (MEMBER_REF);
7065 break;
7066
7067 case CPP_DEREF:
7068 id = ansi_opname (COMPONENT_REF);
7069 break;
7070
7071 case CPP_OPEN_PAREN:
7072 /* Consume the `('. */
7073 cp_lexer_consume_token (parser->lexer);
7074 /* Look for the matching `)'. */
7075 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7076 return ansi_opname (CALL_EXPR);
7077
7078 case CPP_OPEN_SQUARE:
7079 /* Consume the `['. */
7080 cp_lexer_consume_token (parser->lexer);
7081 /* Look for the matching `]'. */
7082 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7083 return ansi_opname (ARRAY_REF);
7084
7085 /* Extensions. */
7086 case CPP_MIN:
7087 id = ansi_opname (MIN_EXPR);
7088 break;
7089
7090 case CPP_MAX:
7091 id = ansi_opname (MAX_EXPR);
7092 break;
7093
7094 case CPP_MIN_EQ:
7095 id = ansi_assopname (MIN_EXPR);
7096 break;
7097
7098 case CPP_MAX_EQ:
7099 id = ansi_assopname (MAX_EXPR);
7100 break;
7101
7102 default:
7103 /* Anything else is an error. */
7104 break;
7105 }
7106
7107 /* If we have selected an identifier, we need to consume the
7108 operator token. */
7109 if (id)
7110 cp_lexer_consume_token (parser->lexer);
7111 /* Otherwise, no valid operator name was present. */
7112 else
7113 {
7114 cp_parser_error (parser, "expected operator");
7115 id = error_mark_node;
7116 }
7117
7118 return id;
7119}
7120
7121/* Parse a template-declaration.
7122
7123 template-declaration:
7124 export [opt] template < template-parameter-list > declaration
7125
7126 If MEMBER_P is TRUE, this template-declaration occurs within a
7127 class-specifier.
7128
7129 The grammar rule given by the standard isn't correct. What
7130 is really meant is:
7131
7132 template-declaration:
7133 export [opt] template-parameter-list-seq
7134 decl-specifier-seq [opt] init-declarator [opt] ;
7135 export [opt] template-parameter-list-seq
7136 function-definition
7137
7138 template-parameter-list-seq:
7139 template-parameter-list-seq [opt]
7140 template < template-parameter-list > */
7141
7142static void
94edc4ab 7143cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7144{
7145 /* Check for `export'. */
7146 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7147 {
7148 /* Consume the `export' token. */
7149 cp_lexer_consume_token (parser->lexer);
7150 /* Warn that we do not support `export'. */
7151 warning ("keyword `export' not implemented, and will be ignored");
7152 }
7153
7154 cp_parser_template_declaration_after_export (parser, member_p);
7155}
7156
7157/* Parse a template-parameter-list.
7158
7159 template-parameter-list:
7160 template-parameter
7161 template-parameter-list , template-parameter
7162
7163 Returns a TREE_LIST. Each node represents a template parameter.
7164 The nodes are connected via their TREE_CHAINs. */
7165
7166static tree
94edc4ab 7167cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7168{
7169 tree parameter_list = NULL_TREE;
7170
7171 while (true)
7172 {
7173 tree parameter;
7174 cp_token *token;
7175
7176 /* Parse the template-parameter. */
7177 parameter = cp_parser_template_parameter (parser);
7178 /* Add it to the list. */
7179 parameter_list = process_template_parm (parameter_list,
7180 parameter);
7181
7182 /* Peek at the next token. */
7183 token = cp_lexer_peek_token (parser->lexer);
7184 /* If it's not a `,', we're done. */
7185 if (token->type != CPP_COMMA)
7186 break;
7187 /* Otherwise, consume the `,' token. */
7188 cp_lexer_consume_token (parser->lexer);
7189 }
7190
7191 return parameter_list;
7192}
7193
7194/* Parse a template-parameter.
7195
7196 template-parameter:
7197 type-parameter
7198 parameter-declaration
7199
7200 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7201 TREE_PURPOSE is the default value, if any. */
7202
7203static tree
94edc4ab 7204cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7205{
7206 cp_token *token;
7207
7208 /* Peek at the next token. */
7209 token = cp_lexer_peek_token (parser->lexer);
7210 /* If it is `class' or `template', we have a type-parameter. */
7211 if (token->keyword == RID_TEMPLATE)
7212 return cp_parser_type_parameter (parser);
7213 /* If it is `class' or `typename' we do not know yet whether it is a
7214 type parameter or a non-type parameter. Consider:
7215
7216 template <typename T, typename T::X X> ...
7217
7218 or:
7219
7220 template <class C, class D*> ...
7221
7222 Here, the first parameter is a type parameter, and the second is
7223 a non-type parameter. We can tell by looking at the token after
7224 the identifier -- if it is a `,', `=', or `>' then we have a type
7225 parameter. */
7226 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7227 {
7228 /* Peek at the token after `class' or `typename'. */
7229 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7230 /* If it's an identifier, skip it. */
7231 if (token->type == CPP_NAME)
7232 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7233 /* Now, see if the token looks like the end of a template
7234 parameter. */
7235 if (token->type == CPP_COMMA
7236 || token->type == CPP_EQ
7237 || token->type == CPP_GREATER)
7238 return cp_parser_type_parameter (parser);
7239 }
7240
7241 /* Otherwise, it is a non-type parameter.
7242
7243 [temp.param]
7244
7245 When parsing a default template-argument for a non-type
7246 template-parameter, the first non-nested `>' is taken as the end
7247 of the template parameter-list rather than a greater-than
7248 operator. */
7249 return
ec194454 7250 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7251}
7252
7253/* Parse a type-parameter.
7254
7255 type-parameter:
7256 class identifier [opt]
7257 class identifier [opt] = type-id
7258 typename identifier [opt]
7259 typename identifier [opt] = type-id
7260 template < template-parameter-list > class identifier [opt]
7261 template < template-parameter-list > class identifier [opt]
7262 = id-expression
7263
7264 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7265 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7266 the declaration of the parameter. */
7267
7268static tree
94edc4ab 7269cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7270{
7271 cp_token *token;
7272 tree parameter;
7273
7274 /* Look for a keyword to tell us what kind of parameter this is. */
7275 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7276 "`class', `typename', or `template'");
a723baf1
MM
7277 if (!token)
7278 return error_mark_node;
7279
7280 switch (token->keyword)
7281 {
7282 case RID_CLASS:
7283 case RID_TYPENAME:
7284 {
7285 tree identifier;
7286 tree default_argument;
7287
7288 /* If the next token is an identifier, then it names the
7289 parameter. */
7290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7291 identifier = cp_parser_identifier (parser);
7292 else
7293 identifier = NULL_TREE;
7294
7295 /* Create the parameter. */
7296 parameter = finish_template_type_parm (class_type_node, identifier);
7297
7298 /* If the next token is an `=', we have a default argument. */
7299 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7300 {
7301 /* Consume the `=' token. */
7302 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7303 /* Parse the default-argument. */
a723baf1
MM
7304 default_argument = cp_parser_type_id (parser);
7305 }
7306 else
7307 default_argument = NULL_TREE;
7308
7309 /* Create the combined representation of the parameter and the
7310 default argument. */
c67d36d0 7311 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7312 }
7313 break;
7314
7315 case RID_TEMPLATE:
7316 {
7317 tree parameter_list;
7318 tree identifier;
7319 tree default_argument;
7320
7321 /* Look for the `<'. */
7322 cp_parser_require (parser, CPP_LESS, "`<'");
7323 /* Parse the template-parameter-list. */
7324 begin_template_parm_list ();
7325 parameter_list
7326 = cp_parser_template_parameter_list (parser);
7327 parameter_list = end_template_parm_list (parameter_list);
7328 /* Look for the `>'. */
7329 cp_parser_require (parser, CPP_GREATER, "`>'");
7330 /* Look for the `class' keyword. */
7331 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7332 /* If the next token is an `=', then there is a
7333 default-argument. If the next token is a `>', we are at
7334 the end of the parameter-list. If the next token is a `,',
7335 then we are at the end of this parameter. */
7336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7337 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7338 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7339 identifier = cp_parser_identifier (parser);
7340 else
7341 identifier = NULL_TREE;
7342 /* Create the template parameter. */
7343 parameter = finish_template_template_parm (class_type_node,
7344 identifier);
7345
7346 /* If the next token is an `=', then there is a
7347 default-argument. */
7348 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7349 {
7350 /* Consume the `='. */
7351 cp_lexer_consume_token (parser->lexer);
7352 /* Parse the id-expression. */
7353 default_argument
7354 = cp_parser_id_expression (parser,
7355 /*template_keyword_p=*/false,
7356 /*check_dependency_p=*/true,
7357 /*template_p=*/NULL);
7358 /* Look up the name. */
7359 default_argument
7360 = cp_parser_lookup_name_simple (parser, default_argument);
7361 /* See if the default argument is valid. */
7362 default_argument
7363 = check_template_template_default_arg (default_argument);
7364 }
7365 else
7366 default_argument = NULL_TREE;
7367
7368 /* Create the combined representation of the parameter and the
7369 default argument. */
c67d36d0 7370 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7371 }
7372 break;
7373
7374 default:
7375 /* Anything else is an error. */
7376 cp_parser_error (parser,
7377 "expected `class', `typename', or `template'");
7378 parameter = error_mark_node;
7379 }
7380
7381 return parameter;
7382}
7383
7384/* Parse a template-id.
7385
7386 template-id:
7387 template-name < template-argument-list [opt] >
7388
7389 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7390 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7391 returned. Otherwise, if the template-name names a function, or set
7392 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7393 names a class, returns a TYPE_DECL for the specialization.
7394
7395 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7396 uninstantiated templates. */
7397
7398static tree
7399cp_parser_template_id (cp_parser *parser,
7400 bool template_keyword_p,
7401 bool check_dependency_p)
7402{
7403 tree template;
7404 tree arguments;
7405 tree saved_scope;
7406 tree saved_qualifying_scope;
7407 tree saved_object_scope;
7408 tree template_id;
7409 bool saved_greater_than_is_operator_p;
7410 ptrdiff_t start_of_id;
7411 tree access_check = NULL_TREE;
2050a1bb 7412 cp_token *next_token;
a723baf1
MM
7413
7414 /* If the next token corresponds to a template-id, there is no need
7415 to reparse it. */
2050a1bb
MM
7416 next_token = cp_lexer_peek_token (parser->lexer);
7417 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7418 {
7419 tree value;
7420 tree check;
7421
7422 /* Get the stored value. */
7423 value = cp_lexer_consume_token (parser->lexer)->value;
7424 /* Perform any access checks that were deferred. */
7425 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7426 perform_or_defer_access_check (TREE_PURPOSE (check),
7427 TREE_VALUE (check));
a723baf1
MM
7428 /* Return the stored value. */
7429 return TREE_VALUE (value);
7430 }
7431
2050a1bb
MM
7432 /* Avoid performing name lookup if there is no possibility of
7433 finding a template-id. */
7434 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7435 || (next_token->type == CPP_NAME
7436 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7437 {
7438 cp_parser_error (parser, "expected template-id");
7439 return error_mark_node;
7440 }
7441
a723baf1
MM
7442 /* Remember where the template-id starts. */
7443 if (cp_parser_parsing_tentatively (parser)
7444 && !cp_parser_committed_to_tentative_parse (parser))
7445 {
2050a1bb 7446 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7447 start_of_id = cp_lexer_token_difference (parser->lexer,
7448 parser->lexer->first_token,
7449 next_token);
a723baf1
MM
7450 }
7451 else
7452 start_of_id = -1;
7453
8d241e0b 7454 push_deferring_access_checks (dk_deferred);
cf22909c 7455
a723baf1
MM
7456 /* Parse the template-name. */
7457 template = cp_parser_template_name (parser, template_keyword_p,
7458 check_dependency_p);
7459 if (template == error_mark_node)
cf22909c
KL
7460 {
7461 pop_deferring_access_checks ();
7462 return error_mark_node;
7463 }
a723baf1
MM
7464
7465 /* Look for the `<' that starts the template-argument-list. */
7466 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7467 {
7468 pop_deferring_access_checks ();
7469 return error_mark_node;
7470 }
a723baf1
MM
7471
7472 /* [temp.names]
7473
7474 When parsing a template-id, the first non-nested `>' is taken as
7475 the end of the template-argument-list rather than a greater-than
7476 operator. */
7477 saved_greater_than_is_operator_p
7478 = parser->greater_than_is_operator_p;
7479 parser->greater_than_is_operator_p = false;
7480 /* Parsing the argument list may modify SCOPE, so we save it
7481 here. */
7482 saved_scope = parser->scope;
7483 saved_qualifying_scope = parser->qualifying_scope;
7484 saved_object_scope = parser->object_scope;
7485 /* Parse the template-argument-list itself. */
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7487 arguments = NULL_TREE;
7488 else
7489 arguments = cp_parser_template_argument_list (parser);
7490 /* Look for the `>' that ends the template-argument-list. */
7491 cp_parser_require (parser, CPP_GREATER, "`>'");
7492 /* The `>' token might be a greater-than operator again now. */
7493 parser->greater_than_is_operator_p
7494 = saved_greater_than_is_operator_p;
7495 /* Restore the SAVED_SCOPE. */
7496 parser->scope = saved_scope;
7497 parser->qualifying_scope = saved_qualifying_scope;
7498 parser->object_scope = saved_object_scope;
7499
7500 /* Build a representation of the specialization. */
7501 if (TREE_CODE (template) == IDENTIFIER_NODE)
7502 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7503 else if (DECL_CLASS_TEMPLATE_P (template)
7504 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7505 template_id
7506 = finish_template_type (template, arguments,
7507 cp_lexer_next_token_is (parser->lexer,
7508 CPP_SCOPE));
7509 else
7510 {
7511 /* If it's not a class-template or a template-template, it should be
7512 a function-template. */
7513 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7514 || TREE_CODE (template) == OVERLOAD
7515 || BASELINK_P (template)),
7516 20010716);
7517
7518 template_id = lookup_template_function (template, arguments);
7519 }
7520
cf22909c
KL
7521 /* Retrieve any deferred checks. Do not pop this access checks yet
7522 so the memory will not be reclaimed during token replacing below. */
7523 access_check = get_deferred_access_checks ();
7524
a723baf1
MM
7525 /* If parsing tentatively, replace the sequence of tokens that makes
7526 up the template-id with a CPP_TEMPLATE_ID token. That way,
7527 should we re-parse the token stream, we will not have to repeat
7528 the effort required to do the parse, nor will we issue duplicate
7529 error messages about problems during instantiation of the
7530 template. */
7531 if (start_of_id >= 0)
7532 {
7533 cp_token *token;
a723baf1
MM
7534
7535 /* Find the token that corresponds to the start of the
7536 template-id. */
7537 token = cp_lexer_advance_token (parser->lexer,
7538 parser->lexer->first_token,
7539 start_of_id);
7540
a723baf1
MM
7541 /* Reset the contents of the START_OF_ID token. */
7542 token->type = CPP_TEMPLATE_ID;
7543 token->value = build_tree_list (access_check, template_id);
7544 token->keyword = RID_MAX;
7545 /* Purge all subsequent tokens. */
7546 cp_lexer_purge_tokens_after (parser->lexer, token);
7547 }
7548
cf22909c 7549 pop_deferring_access_checks ();
a723baf1
MM
7550 return template_id;
7551}
7552
7553/* Parse a template-name.
7554
7555 template-name:
7556 identifier
7557
7558 The standard should actually say:
7559
7560 template-name:
7561 identifier
7562 operator-function-id
7563 conversion-function-id
7564
7565 A defect report has been filed about this issue.
7566
7567 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7568 `template' keyword, in a construction like:
7569
7570 T::template f<3>()
7571
7572 In that case `f' is taken to be a template-name, even though there
7573 is no way of knowing for sure.
7574
7575 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7576 name refers to a set of overloaded functions, at least one of which
7577 is a template, or an IDENTIFIER_NODE with the name of the template,
7578 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7579 names are looked up inside uninstantiated templates. */
7580
7581static tree
94edc4ab
NN
7582cp_parser_template_name (cp_parser* parser,
7583 bool template_keyword_p,
7584 bool check_dependency_p)
a723baf1
MM
7585{
7586 tree identifier;
7587 tree decl;
7588 tree fns;
7589
7590 /* If the next token is `operator', then we have either an
7591 operator-function-id or a conversion-function-id. */
7592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7593 {
7594 /* We don't know whether we're looking at an
7595 operator-function-id or a conversion-function-id. */
7596 cp_parser_parse_tentatively (parser);
7597 /* Try an operator-function-id. */
7598 identifier = cp_parser_operator_function_id (parser);
7599 /* If that didn't work, try a conversion-function-id. */
7600 if (!cp_parser_parse_definitely (parser))
7601 identifier = cp_parser_conversion_function_id (parser);
7602 }
7603 /* Look for the identifier. */
7604 else
7605 identifier = cp_parser_identifier (parser);
7606
7607 /* If we didn't find an identifier, we don't have a template-id. */
7608 if (identifier == error_mark_node)
7609 return error_mark_node;
7610
7611 /* If the name immediately followed the `template' keyword, then it
7612 is a template-name. However, if the next token is not `<', then
7613 we do not treat it as a template-name, since it is not being used
7614 as part of a template-id. This enables us to handle constructs
7615 like:
7616
7617 template <typename T> struct S { S(); };
7618 template <typename T> S<T>::S();
7619
7620 correctly. We would treat `S' as a template -- if it were `S<T>'
7621 -- but we do not if there is no `<'. */
7622 if (template_keyword_p && processing_template_decl
7623 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7624 return identifier;
7625
7626 /* Look up the name. */
7627 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 7628 /*is_type=*/false,
eea9800f 7629 /*is_namespace=*/false,
a723baf1
MM
7630 check_dependency_p);
7631 decl = maybe_get_template_decl_from_type_decl (decl);
7632
7633 /* If DECL is a template, then the name was a template-name. */
7634 if (TREE_CODE (decl) == TEMPLATE_DECL)
7635 ;
7636 else
7637 {
7638 /* The standard does not explicitly indicate whether a name that
7639 names a set of overloaded declarations, some of which are
7640 templates, is a template-name. However, such a name should
7641 be a template-name; otherwise, there is no way to form a
7642 template-id for the overloaded templates. */
7643 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7644 if (TREE_CODE (fns) == OVERLOAD)
7645 {
7646 tree fn;
7647
7648 for (fn = fns; fn; fn = OVL_NEXT (fn))
7649 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7650 break;
7651 }
7652 else
7653 {
7654 /* Otherwise, the name does not name a template. */
7655 cp_parser_error (parser, "expected template-name");
7656 return error_mark_node;
7657 }
7658 }
7659
7660 /* If DECL is dependent, and refers to a function, then just return
7661 its name; we will look it up again during template instantiation. */
7662 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7663 {
7664 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 7665 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
7666 return identifier;
7667 }
7668
7669 return decl;
7670}
7671
7672/* Parse a template-argument-list.
7673
7674 template-argument-list:
7675 template-argument
7676 template-argument-list , template-argument
7677
7678 Returns a TREE_LIST representing the arguments, in the order they
7679 appeared. The TREE_VALUE of each node is a representation of the
7680 argument. */
7681
7682static tree
94edc4ab 7683cp_parser_template_argument_list (cp_parser* parser)
a723baf1
MM
7684{
7685 tree arguments = NULL_TREE;
7686
7687 while (true)
7688 {
7689 tree argument;
7690
7691 /* Parse the template-argument. */
7692 argument = cp_parser_template_argument (parser);
7693 /* Add it to the list. */
7694 arguments = tree_cons (NULL_TREE, argument, arguments);
7695 /* If it is not a `,', then there are no more arguments. */
7696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7697 break;
7698 /* Otherwise, consume the ','. */
7699 cp_lexer_consume_token (parser->lexer);
7700 }
7701
7702 /* We built up the arguments in reverse order. */
7703 return nreverse (arguments);
7704}
7705
7706/* Parse a template-argument.
7707
7708 template-argument:
7709 assignment-expression
7710 type-id
7711 id-expression
7712
7713 The representation is that of an assignment-expression, type-id, or
7714 id-expression -- except that the qualified id-expression is
7715 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
7716 OVERLOAD.
7717
7718 Although the standard says "assignment-expression", it forbids
7719 throw-expressions or assignments in the template argument.
7720 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
7721
7722static tree
94edc4ab 7723cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
7724{
7725 tree argument;
7726 bool template_p;
d17811fd
MM
7727 bool address_p;
7728 cp_token *token;
b3445994 7729 cp_id_kind idk;
d17811fd 7730 tree qualifying_class;
a723baf1
MM
7731
7732 /* There's really no way to know what we're looking at, so we just
7733 try each alternative in order.
7734
7735 [temp.arg]
7736
7737 In a template-argument, an ambiguity between a type-id and an
7738 expression is resolved to a type-id, regardless of the form of
7739 the corresponding template-parameter.
7740
7741 Therefore, we try a type-id first. */
7742 cp_parser_parse_tentatively (parser);
a723baf1
MM
7743 argument = cp_parser_type_id (parser);
7744 /* If the next token isn't a `,' or a `>', then this argument wasn't
7745 really finished. */
d17811fd 7746 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7747 cp_parser_error (parser, "expected template-argument");
7748 /* If that worked, we're done. */
7749 if (cp_parser_parse_definitely (parser))
7750 return argument;
7751 /* We're still not sure what the argument will be. */
7752 cp_parser_parse_tentatively (parser);
7753 /* Try a template. */
7754 argument = cp_parser_id_expression (parser,
7755 /*template_keyword_p=*/false,
7756 /*check_dependency_p=*/true,
7757 &template_p);
7758 /* If the next token isn't a `,' or a `>', then this argument wasn't
7759 really finished. */
d17811fd 7760 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7761 cp_parser_error (parser, "expected template-argument");
7762 if (!cp_parser_error_occurred (parser))
7763 {
7764 /* Figure out what is being referred to. */
7765 argument = cp_parser_lookup_name_simple (parser, argument);
7766 if (template_p)
7767 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7768 TREE_OPERAND (argument, 1),
78757caa 7769 tf_error);
a723baf1
MM
7770 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7771 cp_parser_error (parser, "expected template-name");
7772 }
7773 if (cp_parser_parse_definitely (parser))
7774 return argument;
d17811fd
MM
7775 /* It must be a non-type argument. There permitted cases are given
7776 in [temp.arg.nontype]:
7777
7778 -- an integral constant-expression of integral or enumeration
7779 type; or
7780
7781 -- the name of a non-type template-parameter; or
7782
7783 -- the name of an object or function with external linkage...
7784
7785 -- the address of an object or function with external linkage...
7786
7787 -- a pointer to member... */
7788 /* Look for a non-type template parameter. */
7789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7790 {
7791 cp_parser_parse_tentatively (parser);
7792 argument = cp_parser_primary_expression (parser,
7793 &idk,
7794 &qualifying_class);
7795 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7796 || !cp_parser_next_token_ends_template_argument_p (parser))
7797 cp_parser_simulate_error (parser);
7798 if (cp_parser_parse_definitely (parser))
7799 return argument;
7800 }
7801 /* If the next token is "&", the argument must be the address of an
7802 object or function with external linkage. */
7803 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7804 if (address_p)
7805 cp_lexer_consume_token (parser->lexer);
7806 /* See if we might have an id-expression. */
7807 token = cp_lexer_peek_token (parser->lexer);
7808 if (token->type == CPP_NAME
7809 || token->keyword == RID_OPERATOR
7810 || token->type == CPP_SCOPE
7811 || token->type == CPP_TEMPLATE_ID
7812 || token->type == CPP_NESTED_NAME_SPECIFIER)
7813 {
7814 cp_parser_parse_tentatively (parser);
7815 argument = cp_parser_primary_expression (parser,
7816 &idk,
7817 &qualifying_class);
7818 if (cp_parser_error_occurred (parser)
7819 || !cp_parser_next_token_ends_template_argument_p (parser))
7820 cp_parser_abort_tentative_parse (parser);
7821 else
7822 {
7823 if (qualifying_class)
7824 argument = finish_qualified_id_expr (qualifying_class,
7825 argument,
7826 /*done=*/true,
7827 address_p);
7828 if (TREE_CODE (argument) == VAR_DECL)
7829 {
7830 /* A variable without external linkage might still be a
7831 valid constant-expression, so no error is issued here
7832 if the external-linkage check fails. */
7833 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7834 cp_parser_simulate_error (parser);
7835 }
7836 else if (is_overloaded_fn (argument))
7837 /* All overloaded functions are allowed; if the external
7838 linkage test does not pass, an error will be issued
7839 later. */
7840 ;
7841 else if (address_p
7842 && (TREE_CODE (argument) == OFFSET_REF
7843 || TREE_CODE (argument) == SCOPE_REF))
7844 /* A pointer-to-member. */
7845 ;
7846 else
7847 cp_parser_simulate_error (parser);
7848
7849 if (cp_parser_parse_definitely (parser))
7850 {
7851 if (address_p)
7852 argument = build_x_unary_op (ADDR_EXPR, argument);
7853 return argument;
7854 }
7855 }
7856 }
7857 /* If the argument started with "&", there are no other valid
7858 alternatives at this point. */
7859 if (address_p)
7860 {
7861 cp_parser_error (parser, "invalid non-type template argument");
7862 return error_mark_node;
7863 }
7864 /* The argument must be a constant-expression. */
7865 argument = cp_parser_constant_expression (parser,
7866 /*allow_non_constant_p=*/false,
7867 /*non_constant_p=*/NULL);
7868 /* If it's non-dependent, simplify it. */
7869 return cp_parser_fold_non_dependent_expr (argument);
a723baf1
MM
7870}
7871
7872/* Parse an explicit-instantiation.
7873
7874 explicit-instantiation:
7875 template declaration
7876
7877 Although the standard says `declaration', what it really means is:
7878
7879 explicit-instantiation:
7880 template decl-specifier-seq [opt] declarator [opt] ;
7881
7882 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7883 supposed to be allowed. A defect report has been filed about this
7884 issue.
7885
7886 GNU Extension:
7887
7888 explicit-instantiation:
7889 storage-class-specifier template
7890 decl-specifier-seq [opt] declarator [opt] ;
7891 function-specifier template
7892 decl-specifier-seq [opt] declarator [opt] ; */
7893
7894static void
94edc4ab 7895cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1
MM
7896{
7897 bool declares_class_or_enum;
7898 tree decl_specifiers;
7899 tree attributes;
7900 tree extension_specifier = NULL_TREE;
7901
7902 /* Look for an (optional) storage-class-specifier or
7903 function-specifier. */
7904 if (cp_parser_allow_gnu_extensions_p (parser))
7905 {
7906 extension_specifier
7907 = cp_parser_storage_class_specifier_opt (parser);
7908 if (!extension_specifier)
7909 extension_specifier = cp_parser_function_specifier_opt (parser);
7910 }
7911
7912 /* Look for the `template' keyword. */
7913 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7914 /* Let the front end know that we are processing an explicit
7915 instantiation. */
7916 begin_explicit_instantiation ();
7917 /* [temp.explicit] says that we are supposed to ignore access
7918 control while processing explicit instantiation directives. */
78757caa 7919 push_deferring_access_checks (dk_no_check);
a723baf1
MM
7920 /* Parse a decl-specifier-seq. */
7921 decl_specifiers
7922 = cp_parser_decl_specifier_seq (parser,
7923 CP_PARSER_FLAGS_OPTIONAL,
7924 &attributes,
7925 &declares_class_or_enum);
7926 /* If there was exactly one decl-specifier, and it declared a class,
7927 and there's no declarator, then we have an explicit type
7928 instantiation. */
7929 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
7930 {
7931 tree type;
7932
7933 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
7934 /* Turn access control back on for names used during
7935 template instantiation. */
7936 pop_deferring_access_checks ();
a723baf1
MM
7937 if (type)
7938 do_type_instantiation (type, extension_specifier, /*complain=*/1);
7939 }
7940 else
7941 {
7942 tree declarator;
7943 tree decl;
7944
7945 /* Parse the declarator. */
7946 declarator
62b8a44e 7947 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
7948 /*ctor_dtor_or_conv_p=*/NULL);
7949 decl = grokdeclarator (declarator, decl_specifiers,
7950 NORMAL, 0, NULL);
b7fc8b57
KL
7951 /* Turn access control back on for names used during
7952 template instantiation. */
7953 pop_deferring_access_checks ();
a723baf1
MM
7954 /* Do the explicit instantiation. */
7955 do_decl_instantiation (decl, extension_specifier);
7956 }
7957 /* We're done with the instantiation. */
7958 end_explicit_instantiation ();
a723baf1 7959
e0860732 7960 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
7961}
7962
7963/* Parse an explicit-specialization.
7964
7965 explicit-specialization:
7966 template < > declaration
7967
7968 Although the standard says `declaration', what it really means is:
7969
7970 explicit-specialization:
7971 template <> decl-specifier [opt] init-declarator [opt] ;
7972 template <> function-definition
7973 template <> explicit-specialization
7974 template <> template-declaration */
7975
7976static void
94edc4ab 7977cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
7978{
7979 /* Look for the `template' keyword. */
7980 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7981 /* Look for the `<'. */
7982 cp_parser_require (parser, CPP_LESS, "`<'");
7983 /* Look for the `>'. */
7984 cp_parser_require (parser, CPP_GREATER, "`>'");
7985 /* We have processed another parameter list. */
7986 ++parser->num_template_parameter_lists;
7987 /* Let the front end know that we are beginning a specialization. */
7988 begin_specialization ();
7989
7990 /* If the next keyword is `template', we need to figure out whether
7991 or not we're looking a template-declaration. */
7992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7993 {
7994 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
7995 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
7996 cp_parser_template_declaration_after_export (parser,
7997 /*member_p=*/false);
7998 else
7999 cp_parser_explicit_specialization (parser);
8000 }
8001 else
8002 /* Parse the dependent declaration. */
8003 cp_parser_single_declaration (parser,
8004 /*member_p=*/false,
8005 /*friend_p=*/NULL);
8006
8007 /* We're done with the specialization. */
8008 end_specialization ();
8009 /* We're done with this parameter list. */
8010 --parser->num_template_parameter_lists;
8011}
8012
8013/* Parse a type-specifier.
8014
8015 type-specifier:
8016 simple-type-specifier
8017 class-specifier
8018 enum-specifier
8019 elaborated-type-specifier
8020 cv-qualifier
8021
8022 GNU Extension:
8023
8024 type-specifier:
8025 __complex__
8026
8027 Returns a representation of the type-specifier. If the
8028 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8029 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8030 For a class-specifier, enum-specifier, or elaborated-type-specifier
8031 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8032
8033 If IS_FRIEND is TRUE then this type-specifier is being declared a
8034 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8035 appearing in a decl-specifier-seq.
8036
8037 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8038 class-specifier, enum-specifier, or elaborated-type-specifier, then
8039 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8040 FALSE.
8041
8042 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8043 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8044 is set to FALSE. */
8045
8046static tree
94edc4ab
NN
8047cp_parser_type_specifier (cp_parser* parser,
8048 cp_parser_flags flags,
8049 bool is_friend,
8050 bool is_declaration,
8051 bool* declares_class_or_enum,
8052 bool* is_cv_qualifier)
a723baf1
MM
8053{
8054 tree type_spec = NULL_TREE;
8055 cp_token *token;
8056 enum rid keyword;
8057
8058 /* Assume this type-specifier does not declare a new type. */
8059 if (declares_class_or_enum)
8060 *declares_class_or_enum = false;
8061 /* And that it does not specify a cv-qualifier. */
8062 if (is_cv_qualifier)
8063 *is_cv_qualifier = false;
8064 /* Peek at the next token. */
8065 token = cp_lexer_peek_token (parser->lexer);
8066
8067 /* If we're looking at a keyword, we can use that to guide the
8068 production we choose. */
8069 keyword = token->keyword;
8070 switch (keyword)
8071 {
8072 /* Any of these indicate either a class-specifier, or an
8073 elaborated-type-specifier. */
8074 case RID_CLASS:
8075 case RID_STRUCT:
8076 case RID_UNION:
8077 case RID_ENUM:
8078 /* Parse tentatively so that we can back up if we don't find a
8079 class-specifier or enum-specifier. */
8080 cp_parser_parse_tentatively (parser);
8081 /* Look for the class-specifier or enum-specifier. */
8082 if (keyword == RID_ENUM)
8083 type_spec = cp_parser_enum_specifier (parser);
8084 else
8085 type_spec = cp_parser_class_specifier (parser);
8086
8087 /* If that worked, we're done. */
8088 if (cp_parser_parse_definitely (parser))
8089 {
8090 if (declares_class_or_enum)
8091 *declares_class_or_enum = true;
8092 return type_spec;
8093 }
8094
8095 /* Fall through. */
8096
8097 case RID_TYPENAME:
8098 /* Look for an elaborated-type-specifier. */
8099 type_spec = cp_parser_elaborated_type_specifier (parser,
8100 is_friend,
8101 is_declaration);
8102 /* We're declaring a class or enum -- unless we're using
8103 `typename'. */
8104 if (declares_class_or_enum && keyword != RID_TYPENAME)
8105 *declares_class_or_enum = true;
8106 return type_spec;
8107
8108 case RID_CONST:
8109 case RID_VOLATILE:
8110 case RID_RESTRICT:
8111 type_spec = cp_parser_cv_qualifier_opt (parser);
8112 /* Even though we call a routine that looks for an optional
8113 qualifier, we know that there should be one. */
8114 my_friendly_assert (type_spec != NULL, 20000328);
8115 /* This type-specifier was a cv-qualified. */
8116 if (is_cv_qualifier)
8117 *is_cv_qualifier = true;
8118
8119 return type_spec;
8120
8121 case RID_COMPLEX:
8122 /* The `__complex__' keyword is a GNU extension. */
8123 return cp_lexer_consume_token (parser->lexer)->value;
8124
8125 default:
8126 break;
8127 }
8128
8129 /* If we do not already have a type-specifier, assume we are looking
8130 at a simple-type-specifier. */
8131 type_spec = cp_parser_simple_type_specifier (parser, flags);
8132
8133 /* If we didn't find a type-specifier, and a type-specifier was not
8134 optional in this context, issue an error message. */
8135 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8136 {
8137 cp_parser_error (parser, "expected type specifier");
8138 return error_mark_node;
8139 }
8140
8141 return type_spec;
8142}
8143
8144/* Parse a simple-type-specifier.
8145
8146 simple-type-specifier:
8147 :: [opt] nested-name-specifier [opt] type-name
8148 :: [opt] nested-name-specifier template template-id
8149 char
8150 wchar_t
8151 bool
8152 short
8153 int
8154 long
8155 signed
8156 unsigned
8157 float
8158 double
8159 void
8160
8161 GNU Extension:
8162
8163 simple-type-specifier:
8164 __typeof__ unary-expression
8165 __typeof__ ( type-id )
8166
8167 For the various keywords, the value returned is simply the
8168 TREE_IDENTIFIER representing the keyword. For the first two
8169 productions, the value returned is the indicated TYPE_DECL. */
8170
8171static tree
94edc4ab 8172cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
a723baf1
MM
8173{
8174 tree type = NULL_TREE;
8175 cp_token *token;
8176
8177 /* Peek at the next token. */
8178 token = cp_lexer_peek_token (parser->lexer);
8179
8180 /* If we're looking at a keyword, things are easy. */
8181 switch (token->keyword)
8182 {
8183 case RID_CHAR:
8184 case RID_WCHAR:
8185 case RID_BOOL:
8186 case RID_SHORT:
8187 case RID_INT:
8188 case RID_LONG:
8189 case RID_SIGNED:
8190 case RID_UNSIGNED:
8191 case RID_FLOAT:
8192 case RID_DOUBLE:
8193 case RID_VOID:
8194 /* Consume the token. */
8195 return cp_lexer_consume_token (parser->lexer)->value;
8196
8197 case RID_TYPEOF:
8198 {
8199 tree operand;
8200
8201 /* Consume the `typeof' token. */
8202 cp_lexer_consume_token (parser->lexer);
8203 /* Parse the operand to `typeof' */
8204 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8205 /* If it is not already a TYPE, take its type. */
8206 if (!TYPE_P (operand))
8207 operand = finish_typeof (operand);
8208
8209 return operand;
8210 }
8211
8212 default:
8213 break;
8214 }
8215
8216 /* The type-specifier must be a user-defined type. */
8217 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8218 {
8219 /* Don't gobble tokens or issue error messages if this is an
8220 optional type-specifier. */
8221 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8222 cp_parser_parse_tentatively (parser);
8223
8224 /* Look for the optional `::' operator. */
8225 cp_parser_global_scope_opt (parser,
8226 /*current_scope_valid_p=*/false);
8227 /* Look for the nested-name specifier. */
8228 cp_parser_nested_name_specifier_opt (parser,
8229 /*typename_keyword_p=*/false,
8230 /*check_dependency_p=*/true,
8231 /*type_p=*/false);
8232 /* If we have seen a nested-name-specifier, and the next token
8233 is `template', then we are using the template-id production. */
8234 if (parser->scope
8235 && cp_parser_optional_template_keyword (parser))
8236 {
8237 /* Look for the template-id. */
8238 type = cp_parser_template_id (parser,
8239 /*template_keyword_p=*/true,
8240 /*check_dependency_p=*/true);
8241 /* If the template-id did not name a type, we are out of
8242 luck. */
8243 if (TREE_CODE (type) != TYPE_DECL)
8244 {
8245 cp_parser_error (parser, "expected template-id for type");
8246 type = NULL_TREE;
8247 }
8248 }
8249 /* Otherwise, look for a type-name. */
8250 else
8251 {
8252 type = cp_parser_type_name (parser);
8253 if (type == error_mark_node)
8254 type = NULL_TREE;
8255 }
8256
8257 /* If it didn't work out, we don't have a TYPE. */
8258 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8259 && !cp_parser_parse_definitely (parser))
8260 type = NULL_TREE;
8261 }
8262
8263 /* If we didn't get a type-name, issue an error message. */
8264 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8265 {
8266 cp_parser_error (parser, "expected type-name");
8267 return error_mark_node;
8268 }
8269
8270 return type;
8271}
8272
8273/* Parse a type-name.
8274
8275 type-name:
8276 class-name
8277 enum-name
8278 typedef-name
8279
8280 enum-name:
8281 identifier
8282
8283 typedef-name:
8284 identifier
8285
8286 Returns a TYPE_DECL for the the type. */
8287
8288static tree
94edc4ab 8289cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8290{
8291 tree type_decl;
8292 tree identifier;
8293
8294 /* We can't know yet whether it is a class-name or not. */
8295 cp_parser_parse_tentatively (parser);
8296 /* Try a class-name. */
8297 type_decl = cp_parser_class_name (parser,
8298 /*typename_keyword_p=*/false,
8299 /*template_keyword_p=*/false,
8300 /*type_p=*/false,
a723baf1
MM
8301 /*check_dependency_p=*/true,
8302 /*class_head_p=*/false);
8303 /* If it's not a class-name, keep looking. */
8304 if (!cp_parser_parse_definitely (parser))
8305 {
8306 /* It must be a typedef-name or an enum-name. */
8307 identifier = cp_parser_identifier (parser);
8308 if (identifier == error_mark_node)
8309 return error_mark_node;
8310
8311 /* Look up the type-name. */
8312 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8313 /* Issue an error if we did not find a type-name. */
8314 if (TREE_CODE (type_decl) != TYPE_DECL)
8315 {
8316 cp_parser_error (parser, "expected type-name");
8317 type_decl = error_mark_node;
8318 }
8319 /* Remember that the name was used in the definition of the
8320 current class so that we can check later to see if the
8321 meaning would have been different after the class was
8322 entirely defined. */
8323 else if (type_decl != error_mark_node
8324 && !parser->scope)
8325 maybe_note_name_used_in_class (identifier, type_decl);
8326 }
8327
8328 return type_decl;
8329}
8330
8331
8332/* Parse an elaborated-type-specifier. Note that the grammar given
8333 here incorporates the resolution to DR68.
8334
8335 elaborated-type-specifier:
8336 class-key :: [opt] nested-name-specifier [opt] identifier
8337 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8338 enum :: [opt] nested-name-specifier [opt] identifier
8339 typename :: [opt] nested-name-specifier identifier
8340 typename :: [opt] nested-name-specifier template [opt]
8341 template-id
8342
8343 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8344 declared `friend'. If IS_DECLARATION is TRUE, then this
8345 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8346 something is being declared.
8347
8348 Returns the TYPE specified. */
8349
8350static tree
94edc4ab
NN
8351cp_parser_elaborated_type_specifier (cp_parser* parser,
8352 bool is_friend,
8353 bool is_declaration)
a723baf1
MM
8354{
8355 enum tag_types tag_type;
8356 tree identifier;
8357 tree type = NULL_TREE;
8358
8359 /* See if we're looking at the `enum' keyword. */
8360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8361 {
8362 /* Consume the `enum' token. */
8363 cp_lexer_consume_token (parser->lexer);
8364 /* Remember that it's an enumeration type. */
8365 tag_type = enum_type;
8366 }
8367 /* Or, it might be `typename'. */
8368 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8369 RID_TYPENAME))
8370 {
8371 /* Consume the `typename' token. */
8372 cp_lexer_consume_token (parser->lexer);
8373 /* Remember that it's a `typename' type. */
8374 tag_type = typename_type;
8375 /* The `typename' keyword is only allowed in templates. */
8376 if (!processing_template_decl)
8377 pedwarn ("using `typename' outside of template");
8378 }
8379 /* Otherwise it must be a class-key. */
8380 else
8381 {
8382 tag_type = cp_parser_class_key (parser);
8383 if (tag_type == none_type)
8384 return error_mark_node;
8385 }
8386
8387 /* Look for the `::' operator. */
8388 cp_parser_global_scope_opt (parser,
8389 /*current_scope_valid_p=*/false);
8390 /* Look for the nested-name-specifier. */
8391 if (tag_type == typename_type)
8fa1ad0e
MM
8392 {
8393 if (cp_parser_nested_name_specifier (parser,
8394 /*typename_keyword_p=*/true,
8395 /*check_dependency_p=*/true,
8396 /*type_p=*/true)
8397 == error_mark_node)
8398 return error_mark_node;
8399 }
a723baf1
MM
8400 else
8401 /* Even though `typename' is not present, the proposed resolution
8402 to Core Issue 180 says that in `class A<T>::B', `B' should be
8403 considered a type-name, even if `A<T>' is dependent. */
8404 cp_parser_nested_name_specifier_opt (parser,
8405 /*typename_keyword_p=*/true,
8406 /*check_dependency_p=*/true,
8407 /*type_p=*/true);
8408 /* For everything but enumeration types, consider a template-id. */
8409 if (tag_type != enum_type)
8410 {
8411 bool template_p = false;
8412 tree decl;
8413
8414 /* Allow the `template' keyword. */
8415 template_p = cp_parser_optional_template_keyword (parser);
8416 /* If we didn't see `template', we don't know if there's a
8417 template-id or not. */
8418 if (!template_p)
8419 cp_parser_parse_tentatively (parser);
8420 /* Parse the template-id. */
8421 decl = cp_parser_template_id (parser, template_p,
8422 /*check_dependency_p=*/true);
8423 /* If we didn't find a template-id, look for an ordinary
8424 identifier. */
8425 if (!template_p && !cp_parser_parse_definitely (parser))
8426 ;
8427 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8428 in effect, then we must assume that, upon instantiation, the
8429 template will correspond to a class. */
8430 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8431 && tag_type == typename_type)
8432 type = make_typename_type (parser->scope, decl,
8433 /*complain=*/1);
8434 else
8435 type = TREE_TYPE (decl);
8436 }
8437
8438 /* For an enumeration type, consider only a plain identifier. */
8439 if (!type)
8440 {
8441 identifier = cp_parser_identifier (parser);
8442
8443 if (identifier == error_mark_node)
eb5abb39
NS
8444 {
8445 parser->scope = NULL_TREE;
8446 return error_mark_node;
8447 }
a723baf1
MM
8448
8449 /* For a `typename', we needn't call xref_tag. */
8450 if (tag_type == typename_type)
8451 return make_typename_type (parser->scope, identifier,
8452 /*complain=*/1);
8453 /* Look up a qualified name in the usual way. */
8454 if (parser->scope)
8455 {
8456 tree decl;
8457
8458 /* In an elaborated-type-specifier, names are assumed to name
8459 types, so we set IS_TYPE to TRUE when calling
8460 cp_parser_lookup_name. */
8461 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8462 /*is_type=*/true,
eea9800f 8463 /*is_namespace=*/false,
a723baf1 8464 /*check_dependency=*/true);
710b73e6
KL
8465
8466 /* If we are parsing friend declaration, DECL may be a
8467 TEMPLATE_DECL tree node here. However, we need to check
8468 whether this TEMPLATE_DECL results in valid code. Consider
8469 the following example:
8470
8471 namespace N {
8472 template <class T> class C {};
8473 }
8474 class X {
8475 template <class T> friend class N::C; // #1, valid code
8476 };
8477 template <class T> class Y {
8478 friend class N::C; // #2, invalid code
8479 };
8480
8481 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8482 name lookup of `N::C'. We see that friend declaration must
8483 be template for the code to be valid. Note that
8484 processing_template_decl does not work here since it is
8485 always 1 for the above two cases. */
8486
a723baf1 8487 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8488 (decl, /*tag_name_p=*/is_friend
8489 && parser->num_template_parameter_lists));
a723baf1
MM
8490
8491 if (TREE_CODE (decl) != TYPE_DECL)
8492 {
8493 error ("expected type-name");
8494 return error_mark_node;
8495 }
8496 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8497 && tag_type != enum_type)
8498 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8499 tag_type == record_type ? "struct" : "class");
8500 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8501 && tag_type == enum_type)
8502 error ("`%T' referred to as enum", TREE_TYPE (decl));
8503
8504 type = TREE_TYPE (decl);
8505 }
8506 else
8507 {
8508 /* An elaborated-type-specifier sometimes introduces a new type and
8509 sometimes names an existing type. Normally, the rule is that it
8510 introduces a new type only if there is not an existing type of
8511 the same name already in scope. For example, given:
8512
8513 struct S {};
8514 void f() { struct S s; }
8515
8516 the `struct S' in the body of `f' is the same `struct S' as in
8517 the global scope; the existing definition is used. However, if
8518 there were no global declaration, this would introduce a new
8519 local class named `S'.
8520
8521 An exception to this rule applies to the following code:
8522
8523 namespace N { struct S; }
8524
8525 Here, the elaborated-type-specifier names a new type
8526 unconditionally; even if there is already an `S' in the
8527 containing scope this declaration names a new type.
8528 This exception only applies if the elaborated-type-specifier
8529 forms the complete declaration:
8530
8531 [class.name]
8532
8533 A declaration consisting solely of `class-key identifier ;' is
8534 either a redeclaration of the name in the current scope or a
8535 forward declaration of the identifier as a class name. It
8536 introduces the name into the current scope.
8537
8538 We are in this situation precisely when the next token is a `;'.
8539
8540 An exception to the exception is that a `friend' declaration does
8541 *not* name a new type; i.e., given:
8542
8543 struct S { friend struct T; };
8544
8545 `T' is not a new type in the scope of `S'.
8546
8547 Also, `new struct S' or `sizeof (struct S)' never results in the
8548 definition of a new type; a new type can only be declared in a
9bcb9aae 8549 declaration context. */
a723baf1
MM
8550
8551 type = xref_tag (tag_type, identifier,
8552 /*attributes=*/NULL_TREE,
8553 (is_friend
8554 || !is_declaration
8555 || cp_lexer_next_token_is_not (parser->lexer,
8556 CPP_SEMICOLON)));
8557 }
8558 }
8559 if (tag_type != enum_type)
8560 cp_parser_check_class_key (tag_type, type);
8561 return type;
8562}
8563
8564/* Parse an enum-specifier.
8565
8566 enum-specifier:
8567 enum identifier [opt] { enumerator-list [opt] }
8568
8569 Returns an ENUM_TYPE representing the enumeration. */
8570
8571static tree
94edc4ab 8572cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
8573{
8574 cp_token *token;
8575 tree identifier = NULL_TREE;
8576 tree type;
8577
8578 /* Look for the `enum' keyword. */
8579 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8580 return error_mark_node;
8581 /* Peek at the next token. */
8582 token = cp_lexer_peek_token (parser->lexer);
8583
8584 /* See if it is an identifier. */
8585 if (token->type == CPP_NAME)
8586 identifier = cp_parser_identifier (parser);
8587
8588 /* Look for the `{'. */
8589 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8590 return error_mark_node;
8591
8592 /* At this point, we're going ahead with the enum-specifier, even
8593 if some other problem occurs. */
8594 cp_parser_commit_to_tentative_parse (parser);
8595
8596 /* Issue an error message if type-definitions are forbidden here. */
8597 cp_parser_check_type_definition (parser);
8598
8599 /* Create the new type. */
8600 type = start_enum (identifier ? identifier : make_anon_name ());
8601
8602 /* Peek at the next token. */
8603 token = cp_lexer_peek_token (parser->lexer);
8604 /* If it's not a `}', then there are some enumerators. */
8605 if (token->type != CPP_CLOSE_BRACE)
8606 cp_parser_enumerator_list (parser, type);
8607 /* Look for the `}'. */
8608 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8609
8610 /* Finish up the enumeration. */
8611 finish_enum (type);
8612
8613 return type;
8614}
8615
8616/* Parse an enumerator-list. The enumerators all have the indicated
8617 TYPE.
8618
8619 enumerator-list:
8620 enumerator-definition
8621 enumerator-list , enumerator-definition */
8622
8623static void
94edc4ab 8624cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
8625{
8626 while (true)
8627 {
8628 cp_token *token;
8629
8630 /* Parse an enumerator-definition. */
8631 cp_parser_enumerator_definition (parser, type);
8632 /* Peek at the next token. */
8633 token = cp_lexer_peek_token (parser->lexer);
8634 /* If it's not a `,', then we've reached the end of the
8635 list. */
8636 if (token->type != CPP_COMMA)
8637 break;
8638 /* Otherwise, consume the `,' and keep going. */
8639 cp_lexer_consume_token (parser->lexer);
8640 /* If the next token is a `}', there is a trailing comma. */
8641 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8642 {
8643 if (pedantic && !in_system_header)
8644 pedwarn ("comma at end of enumerator list");
8645 break;
8646 }
8647 }
8648}
8649
8650/* Parse an enumerator-definition. The enumerator has the indicated
8651 TYPE.
8652
8653 enumerator-definition:
8654 enumerator
8655 enumerator = constant-expression
8656
8657 enumerator:
8658 identifier */
8659
8660static void
94edc4ab 8661cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
8662{
8663 cp_token *token;
8664 tree identifier;
8665 tree value;
8666
8667 /* Look for the identifier. */
8668 identifier = cp_parser_identifier (parser);
8669 if (identifier == error_mark_node)
8670 return;
8671
8672 /* Peek at the next token. */
8673 token = cp_lexer_peek_token (parser->lexer);
8674 /* If it's an `=', then there's an explicit value. */
8675 if (token->type == CPP_EQ)
8676 {
8677 /* Consume the `=' token. */
8678 cp_lexer_consume_token (parser->lexer);
8679 /* Parse the value. */
14d22dd6 8680 value = cp_parser_constant_expression (parser,
d17811fd 8681 /*allow_non_constant_p=*/false,
14d22dd6 8682 NULL);
a723baf1
MM
8683 }
8684 else
8685 value = NULL_TREE;
8686
8687 /* Create the enumerator. */
8688 build_enumerator (identifier, value, type);
8689}
8690
8691/* Parse a namespace-name.
8692
8693 namespace-name:
8694 original-namespace-name
8695 namespace-alias
8696
8697 Returns the NAMESPACE_DECL for the namespace. */
8698
8699static tree
94edc4ab 8700cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
8701{
8702 tree identifier;
8703 tree namespace_decl;
8704
8705 /* Get the name of the namespace. */
8706 identifier = cp_parser_identifier (parser);
8707 if (identifier == error_mark_node)
8708 return error_mark_node;
8709
eea9800f
MM
8710 /* Look up the identifier in the currently active scope. Look only
8711 for namespaces, due to:
8712
8713 [basic.lookup.udir]
8714
8715 When looking up a namespace-name in a using-directive or alias
8716 definition, only namespace names are considered.
8717
8718 And:
8719
8720 [basic.lookup.qual]
8721
8722 During the lookup of a name preceding the :: scope resolution
8723 operator, object, function, and enumerator names are ignored.
8724
8725 (Note that cp_parser_class_or_namespace_name only calls this
8726 function if the token after the name is the scope resolution
8727 operator.) */
8728 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f
MM
8729 /*is_type=*/false,
8730 /*is_namespace=*/true,
8731 /*check_dependency=*/true);
a723baf1
MM
8732 /* If it's not a namespace, issue an error. */
8733 if (namespace_decl == error_mark_node
8734 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8735 {
8736 cp_parser_error (parser, "expected namespace-name");
8737 namespace_decl = error_mark_node;
8738 }
8739
8740 return namespace_decl;
8741}
8742
8743/* Parse a namespace-definition.
8744
8745 namespace-definition:
8746 named-namespace-definition
8747 unnamed-namespace-definition
8748
8749 named-namespace-definition:
8750 original-namespace-definition
8751 extension-namespace-definition
8752
8753 original-namespace-definition:
8754 namespace identifier { namespace-body }
8755
8756 extension-namespace-definition:
8757 namespace original-namespace-name { namespace-body }
8758
8759 unnamed-namespace-definition:
8760 namespace { namespace-body } */
8761
8762static void
94edc4ab 8763cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
8764{
8765 tree identifier;
8766
8767 /* Look for the `namespace' keyword. */
8768 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8769
8770 /* Get the name of the namespace. We do not attempt to distinguish
8771 between an original-namespace-definition and an
8772 extension-namespace-definition at this point. The semantic
8773 analysis routines are responsible for that. */
8774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8775 identifier = cp_parser_identifier (parser);
8776 else
8777 identifier = NULL_TREE;
8778
8779 /* Look for the `{' to start the namespace. */
8780 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8781 /* Start the namespace. */
8782 push_namespace (identifier);
8783 /* Parse the body of the namespace. */
8784 cp_parser_namespace_body (parser);
8785 /* Finish the namespace. */
8786 pop_namespace ();
8787 /* Look for the final `}'. */
8788 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8789}
8790
8791/* Parse a namespace-body.
8792
8793 namespace-body:
8794 declaration-seq [opt] */
8795
8796static void
94edc4ab 8797cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
8798{
8799 cp_parser_declaration_seq_opt (parser);
8800}
8801
8802/* Parse a namespace-alias-definition.
8803
8804 namespace-alias-definition:
8805 namespace identifier = qualified-namespace-specifier ; */
8806
8807static void
94edc4ab 8808cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
8809{
8810 tree identifier;
8811 tree namespace_specifier;
8812
8813 /* Look for the `namespace' keyword. */
8814 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8815 /* Look for the identifier. */
8816 identifier = cp_parser_identifier (parser);
8817 if (identifier == error_mark_node)
8818 return;
8819 /* Look for the `=' token. */
8820 cp_parser_require (parser, CPP_EQ, "`='");
8821 /* Look for the qualified-namespace-specifier. */
8822 namespace_specifier
8823 = cp_parser_qualified_namespace_specifier (parser);
8824 /* Look for the `;' token. */
8825 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8826
8827 /* Register the alias in the symbol table. */
8828 do_namespace_alias (identifier, namespace_specifier);
8829}
8830
8831/* Parse a qualified-namespace-specifier.
8832
8833 qualified-namespace-specifier:
8834 :: [opt] nested-name-specifier [opt] namespace-name
8835
8836 Returns a NAMESPACE_DECL corresponding to the specified
8837 namespace. */
8838
8839static tree
94edc4ab 8840cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
8841{
8842 /* Look for the optional `::'. */
8843 cp_parser_global_scope_opt (parser,
8844 /*current_scope_valid_p=*/false);
8845
8846 /* Look for the optional nested-name-specifier. */
8847 cp_parser_nested_name_specifier_opt (parser,
8848 /*typename_keyword_p=*/false,
8849 /*check_dependency_p=*/true,
8850 /*type_p=*/false);
8851
8852 return cp_parser_namespace_name (parser);
8853}
8854
8855/* Parse a using-declaration.
8856
8857 using-declaration:
8858 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8859 using :: unqualified-id ; */
8860
8861static void
94edc4ab 8862cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
8863{
8864 cp_token *token;
8865 bool typename_p = false;
8866 bool global_scope_p;
8867 tree decl;
8868 tree identifier;
8869 tree scope;
8870
8871 /* Look for the `using' keyword. */
8872 cp_parser_require_keyword (parser, RID_USING, "`using'");
8873
8874 /* Peek at the next token. */
8875 token = cp_lexer_peek_token (parser->lexer);
8876 /* See if it's `typename'. */
8877 if (token->keyword == RID_TYPENAME)
8878 {
8879 /* Remember that we've seen it. */
8880 typename_p = true;
8881 /* Consume the `typename' token. */
8882 cp_lexer_consume_token (parser->lexer);
8883 }
8884
8885 /* Look for the optional global scope qualification. */
8886 global_scope_p
8887 = (cp_parser_global_scope_opt (parser,
8888 /*current_scope_valid_p=*/false)
8889 != NULL_TREE);
8890
8891 /* If we saw `typename', or didn't see `::', then there must be a
8892 nested-name-specifier present. */
8893 if (typename_p || !global_scope_p)
8894 cp_parser_nested_name_specifier (parser, typename_p,
8895 /*check_dependency_p=*/true,
8896 /*type_p=*/false);
8897 /* Otherwise, we could be in either of the two productions. In that
8898 case, treat the nested-name-specifier as optional. */
8899 else
8900 cp_parser_nested_name_specifier_opt (parser,
8901 /*typename_keyword_p=*/false,
8902 /*check_dependency_p=*/true,
8903 /*type_p=*/false);
8904
8905 /* Parse the unqualified-id. */
8906 identifier = cp_parser_unqualified_id (parser,
8907 /*template_keyword_p=*/false,
8908 /*check_dependency_p=*/true);
8909
8910 /* The function we call to handle a using-declaration is different
8911 depending on what scope we are in. */
8912 scope = current_scope ();
8913 if (scope && TYPE_P (scope))
8914 {
8915 /* Create the USING_DECL. */
8916 decl = do_class_using_decl (build_nt (SCOPE_REF,
8917 parser->scope,
8918 identifier));
8919 /* Add it to the list of members in this class. */
8920 finish_member_declaration (decl);
8921 }
8922 else
8923 {
8924 decl = cp_parser_lookup_name_simple (parser, identifier);
4eb6d609
MM
8925 if (decl == error_mark_node)
8926 {
8927 if (parser->scope && parser->scope != global_namespace)
8928 error ("`%D::%D' has not been declared",
8929 parser->scope, identifier);
8930 else
8931 error ("`::%D' has not been declared", identifier);
8932 }
8933 else if (scope)
a723baf1
MM
8934 do_local_using_decl (decl);
8935 else
8936 do_toplevel_using_decl (decl);
8937 }
8938
8939 /* Look for the final `;'. */
8940 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8941}
8942
8943/* Parse a using-directive.
8944
8945 using-directive:
8946 using namespace :: [opt] nested-name-specifier [opt]
8947 namespace-name ; */
8948
8949static void
94edc4ab 8950cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
8951{
8952 tree namespace_decl;
8953
8954 /* Look for the `using' keyword. */
8955 cp_parser_require_keyword (parser, RID_USING, "`using'");
8956 /* And the `namespace' keyword. */
8957 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8958 /* Look for the optional `::' operator. */
8959 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 8960 /* And the optional nested-name-specifier. */
a723baf1
MM
8961 cp_parser_nested_name_specifier_opt (parser,
8962 /*typename_keyword_p=*/false,
8963 /*check_dependency_p=*/true,
8964 /*type_p=*/false);
8965 /* Get the namespace being used. */
8966 namespace_decl = cp_parser_namespace_name (parser);
8967 /* Update the symbol table. */
8968 do_using_directive (namespace_decl);
8969 /* Look for the final `;'. */
8970 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8971}
8972
8973/* Parse an asm-definition.
8974
8975 asm-definition:
8976 asm ( string-literal ) ;
8977
8978 GNU Extension:
8979
8980 asm-definition:
8981 asm volatile [opt] ( string-literal ) ;
8982 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
8983 asm volatile [opt] ( string-literal : asm-operand-list [opt]
8984 : asm-operand-list [opt] ) ;
8985 asm volatile [opt] ( string-literal : asm-operand-list [opt]
8986 : asm-operand-list [opt]
8987 : asm-operand-list [opt] ) ; */
8988
8989static void
94edc4ab 8990cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
8991{
8992 cp_token *token;
8993 tree string;
8994 tree outputs = NULL_TREE;
8995 tree inputs = NULL_TREE;
8996 tree clobbers = NULL_TREE;
8997 tree asm_stmt;
8998 bool volatile_p = false;
8999 bool extended_p = false;
9000
9001 /* Look for the `asm' keyword. */
9002 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9003 /* See if the next token is `volatile'. */
9004 if (cp_parser_allow_gnu_extensions_p (parser)
9005 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9006 {
9007 /* Remember that we saw the `volatile' keyword. */
9008 volatile_p = true;
9009 /* Consume the token. */
9010 cp_lexer_consume_token (parser->lexer);
9011 }
9012 /* Look for the opening `('. */
9013 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9014 /* Look for the string. */
9015 token = cp_parser_require (parser, CPP_STRING, "asm body");
9016 if (!token)
9017 return;
9018 string = token->value;
9019 /* If we're allowing GNU extensions, check for the extended assembly
9020 syntax. Unfortunately, the `:' tokens need not be separated by
9021 a space in C, and so, for compatibility, we tolerate that here
9022 too. Doing that means that we have to treat the `::' operator as
9023 two `:' tokens. */
9024 if (cp_parser_allow_gnu_extensions_p (parser)
9025 && at_function_scope_p ()
9026 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9027 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9028 {
9029 bool inputs_p = false;
9030 bool clobbers_p = false;
9031
9032 /* The extended syntax was used. */
9033 extended_p = true;
9034
9035 /* Look for outputs. */
9036 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9037 {
9038 /* Consume the `:'. */
9039 cp_lexer_consume_token (parser->lexer);
9040 /* Parse the output-operands. */
9041 if (cp_lexer_next_token_is_not (parser->lexer,
9042 CPP_COLON)
9043 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9044 CPP_SCOPE)
9045 && cp_lexer_next_token_is_not (parser->lexer,
9046 CPP_CLOSE_PAREN))
a723baf1
MM
9047 outputs = cp_parser_asm_operand_list (parser);
9048 }
9049 /* If the next token is `::', there are no outputs, and the
9050 next token is the beginning of the inputs. */
9051 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9052 {
9053 /* Consume the `::' token. */
9054 cp_lexer_consume_token (parser->lexer);
9055 /* The inputs are coming next. */
9056 inputs_p = true;
9057 }
9058
9059 /* Look for inputs. */
9060 if (inputs_p
9061 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9062 {
9063 if (!inputs_p)
9064 /* Consume the `:'. */
9065 cp_lexer_consume_token (parser->lexer);
9066 /* Parse the output-operands. */
9067 if (cp_lexer_next_token_is_not (parser->lexer,
9068 CPP_COLON)
9069 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9070 CPP_SCOPE)
9071 && cp_lexer_next_token_is_not (parser->lexer,
9072 CPP_CLOSE_PAREN))
a723baf1
MM
9073 inputs = cp_parser_asm_operand_list (parser);
9074 }
9075 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9076 /* The clobbers are coming next. */
9077 clobbers_p = true;
9078
9079 /* Look for clobbers. */
9080 if (clobbers_p
9081 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9082 {
9083 if (!clobbers_p)
9084 /* Consume the `:'. */
9085 cp_lexer_consume_token (parser->lexer);
9086 /* Parse the clobbers. */
8caf4c38
MM
9087 if (cp_lexer_next_token_is_not (parser->lexer,
9088 CPP_CLOSE_PAREN))
9089 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9090 }
9091 }
9092 /* Look for the closing `)'. */
9093 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 9094 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
9095 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9096
9097 /* Create the ASM_STMT. */
9098 if (at_function_scope_p ())
9099 {
9100 asm_stmt =
9101 finish_asm_stmt (volatile_p
9102 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9103 string, outputs, inputs, clobbers);
9104 /* If the extended syntax was not used, mark the ASM_STMT. */
9105 if (!extended_p)
9106 ASM_INPUT_P (asm_stmt) = 1;
9107 }
9108 else
9109 assemble_asm (string);
9110}
9111
9112/* Declarators [gram.dcl.decl] */
9113
9114/* Parse an init-declarator.
9115
9116 init-declarator:
9117 declarator initializer [opt]
9118
9119 GNU Extension:
9120
9121 init-declarator:
9122 declarator asm-specification [opt] attributes [opt] initializer [opt]
9123
9124 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9125 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9126 then this declarator appears in a class scope. The new DECL created
9127 by this declarator is returned.
a723baf1
MM
9128
9129 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9130 for a function-definition here as well. If the declarator is a
9131 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9132 be TRUE upon return. By that point, the function-definition will
9133 have been completely parsed.
9134
9135 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9136 is FALSE. */
9137
9138static tree
94edc4ab
NN
9139cp_parser_init_declarator (cp_parser* parser,
9140 tree decl_specifiers,
9141 tree prefix_attributes,
9142 bool function_definition_allowed_p,
9143 bool member_p,
9144 bool* function_definition_p)
a723baf1
MM
9145{
9146 cp_token *token;
9147 tree declarator;
9148 tree attributes;
9149 tree asm_specification;
9150 tree initializer;
9151 tree decl = NULL_TREE;
9152 tree scope;
a723baf1
MM
9153 bool is_initialized;
9154 bool is_parenthesized_init;
39703eb9 9155 bool is_non_constant_init;
7efa3e22 9156 int ctor_dtor_or_conv_p;
a723baf1
MM
9157 bool friend_p;
9158
9159 /* Assume that this is not the declarator for a function
9160 definition. */
9161 if (function_definition_p)
9162 *function_definition_p = false;
9163
9164 /* Defer access checks while parsing the declarator; we cannot know
9165 what names are accessible until we know what is being
9166 declared. */
cf22909c
KL
9167 resume_deferring_access_checks ();
9168
a723baf1
MM
9169 /* Parse the declarator. */
9170 declarator
62b8a44e 9171 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9172 &ctor_dtor_or_conv_p);
9173 /* Gather up the deferred checks. */
cf22909c 9174 stop_deferring_access_checks ();
24c0ef37 9175
a723baf1
MM
9176 /* If the DECLARATOR was erroneous, there's no need to go
9177 further. */
9178 if (declarator == error_mark_node)
cf22909c 9179 return error_mark_node;
a723baf1
MM
9180
9181 /* Figure out what scope the entity declared by the DECLARATOR is
9182 located in. `grokdeclarator' sometimes changes the scope, so
9183 we compute it now. */
9184 scope = get_scope_of_declarator (declarator);
9185
9186 /* If we're allowing GNU extensions, look for an asm-specification
9187 and attributes. */
9188 if (cp_parser_allow_gnu_extensions_p (parser))
9189 {
9190 /* Look for an asm-specification. */
9191 asm_specification = cp_parser_asm_specification_opt (parser);
9192 /* And attributes. */
9193 attributes = cp_parser_attributes_opt (parser);
9194 }
9195 else
9196 {
9197 asm_specification = NULL_TREE;
9198 attributes = NULL_TREE;
9199 }
9200
9201 /* Peek at the next token. */
9202 token = cp_lexer_peek_token (parser->lexer);
9203 /* Check to see if the token indicates the start of a
9204 function-definition. */
9205 if (cp_parser_token_starts_function_definition_p (token))
9206 {
9207 if (!function_definition_allowed_p)
9208 {
9209 /* If a function-definition should not appear here, issue an
9210 error message. */
9211 cp_parser_error (parser,
9212 "a function-definition is not allowed here");
9213 return error_mark_node;
9214 }
9215 else
9216 {
a723baf1
MM
9217 /* Neither attributes nor an asm-specification are allowed
9218 on a function-definition. */
9219 if (asm_specification)
9220 error ("an asm-specification is not allowed on a function-definition");
9221 if (attributes)
9222 error ("attributes are not allowed on a function-definition");
9223 /* This is a function-definition. */
9224 *function_definition_p = true;
9225
a723baf1
MM
9226 /* Parse the function definition. */
9227 decl = (cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 9228 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9229
a723baf1
MM
9230 return decl;
9231 }
9232 }
9233
9234 /* [dcl.dcl]
9235
9236 Only in function declarations for constructors, destructors, and
9237 type conversions can the decl-specifier-seq be omitted.
9238
9239 We explicitly postpone this check past the point where we handle
9240 function-definitions because we tolerate function-definitions
9241 that are missing their return types in some modes. */
7efa3e22 9242 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9243 {
9244 cp_parser_error (parser,
9245 "expected constructor, destructor, or type conversion");
9246 return error_mark_node;
9247 }
9248
9249 /* An `=' or an `(' indicates an initializer. */
9250 is_initialized = (token->type == CPP_EQ
9251 || token->type == CPP_OPEN_PAREN);
9252 /* If the init-declarator isn't initialized and isn't followed by a
9253 `,' or `;', it's not a valid init-declarator. */
9254 if (!is_initialized
9255 && token->type != CPP_COMMA
9256 && token->type != CPP_SEMICOLON)
9257 {
9258 cp_parser_error (parser, "expected init-declarator");
9259 return error_mark_node;
9260 }
9261
9262 /* Because start_decl has side-effects, we should only call it if we
9263 know we're going ahead. By this point, we know that we cannot
9264 possibly be looking at any other construct. */
9265 cp_parser_commit_to_tentative_parse (parser);
9266
9267 /* Check to see whether or not this declaration is a friend. */
9268 friend_p = cp_parser_friend_p (decl_specifiers);
9269
9270 /* Check that the number of template-parameter-lists is OK. */
9271 if (!cp_parser_check_declarator_template_parameters (parser,
9272 declarator))
cf22909c 9273 return error_mark_node;
a723baf1
MM
9274
9275 /* Enter the newly declared entry in the symbol table. If we're
9276 processing a declaration in a class-specifier, we wait until
9277 after processing the initializer. */
9278 if (!member_p)
9279 {
9280 if (parser->in_unbraced_linkage_specification_p)
9281 {
9282 decl_specifiers = tree_cons (error_mark_node,
9283 get_identifier ("extern"),
9284 decl_specifiers);
9285 have_extern_spec = false;
9286 }
9287 decl = start_decl (declarator,
9288 decl_specifiers,
9289 is_initialized,
9290 attributes,
9291 prefix_attributes);
9292 }
9293
9294 /* Enter the SCOPE. That way unqualified names appearing in the
9295 initializer will be looked up in SCOPE. */
9296 if (scope)
9297 push_scope (scope);
9298
9299 /* Perform deferred access control checks, now that we know in which
9300 SCOPE the declared entity resides. */
9301 if (!member_p && decl)
9302 {
9303 tree saved_current_function_decl = NULL_TREE;
9304
9305 /* If the entity being declared is a function, pretend that we
9306 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9307 things that would not otherwise be accessible. */
a723baf1
MM
9308 if (TREE_CODE (decl) == FUNCTION_DECL)
9309 {
9310 saved_current_function_decl = current_function_decl;
9311 current_function_decl = decl;
9312 }
9313
cf22909c
KL
9314 /* Perform the access control checks for the declarator and the
9315 the decl-specifiers. */
9316 perform_deferred_access_checks ();
a723baf1
MM
9317
9318 /* Restore the saved value. */
9319 if (TREE_CODE (decl) == FUNCTION_DECL)
9320 current_function_decl = saved_current_function_decl;
9321 }
9322
9323 /* Parse the initializer. */
9324 if (is_initialized)
39703eb9
MM
9325 initializer = cp_parser_initializer (parser,
9326 &is_parenthesized_init,
9327 &is_non_constant_init);
a723baf1
MM
9328 else
9329 {
9330 initializer = NULL_TREE;
9331 is_parenthesized_init = false;
39703eb9 9332 is_non_constant_init = true;
a723baf1
MM
9333 }
9334
9335 /* The old parser allows attributes to appear after a parenthesized
9336 initializer. Mark Mitchell proposed removing this functionality
9337 on the GCC mailing lists on 2002-08-13. This parser accepts the
9338 attributes -- but ignores them. */
9339 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9340 if (cp_parser_attributes_opt (parser))
9341 warning ("attributes after parenthesized initializer ignored");
9342
9343 /* Leave the SCOPE, now that we have processed the initializer. It
9344 is important to do this before calling cp_finish_decl because it
9345 makes decisions about whether to create DECL_STMTs or not based
9346 on the current scope. */
9347 if (scope)
9348 pop_scope (scope);
9349
9350 /* For an in-class declaration, use `grokfield' to create the
9351 declaration. */
9352 if (member_p)
8db1028e
NS
9353 {
9354 decl = grokfield (declarator, decl_specifiers,
9355 initializer, /*asmspec=*/NULL_TREE,
a723baf1 9356 /*attributes=*/NULL_TREE);
8db1028e
NS
9357 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9358 cp_parser_save_default_args (parser, decl);
9359 }
9360
a723baf1
MM
9361 /* Finish processing the declaration. But, skip friend
9362 declarations. */
9363 if (!friend_p && decl)
9364 cp_finish_decl (decl,
9365 initializer,
9366 asm_specification,
9367 /* If the initializer is in parentheses, then this is
9368 a direct-initialization, which means that an
9369 `explicit' constructor is OK. Otherwise, an
9370 `explicit' constructor cannot be used. */
9371 ((is_parenthesized_init || !is_initialized)
9372 ? 0 : LOOKUP_ONLYCONVERTING));
9373
39703eb9
MM
9374 /* Remember whether or not variables were initialized by
9375 constant-expressions. */
9376 if (decl && TREE_CODE (decl) == VAR_DECL
9377 && is_initialized && !is_non_constant_init)
9378 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9379
a723baf1
MM
9380 return decl;
9381}
9382
9383/* Parse a declarator.
9384
9385 declarator:
9386 direct-declarator
9387 ptr-operator declarator
9388
9389 abstract-declarator:
9390 ptr-operator abstract-declarator [opt]
9391 direct-abstract-declarator
9392
9393 GNU Extensions:
9394
9395 declarator:
9396 attributes [opt] direct-declarator
9397 attributes [opt] ptr-operator declarator
9398
9399 abstract-declarator:
9400 attributes [opt] ptr-operator abstract-declarator [opt]
9401 attributes [opt] direct-abstract-declarator
9402
9403 Returns a representation of the declarator. If the declarator has
9404 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 9405 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
9406 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9407 used. The first operand is the TYPE for `X'. The second operand
9408 is an INDIRECT_REF whose operand is the sub-declarator.
9409
34cd5ae7 9410 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
9411
9412 (It would be better to define a structure type to represent
9413 declarators, rather than abusing `tree' nodes to represent
9414 declarators. That would be much clearer and save some memory.
9415 There is no reason for declarators to be garbage-collected, for
9416 example; they are created during parser and no longer needed after
9417 `grokdeclarator' has been called.)
9418
9419 For a ptr-operator that has the optional cv-qualifier-seq,
9420 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9421 node.
9422
7efa3e22
NS
9423 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9424 detect constructor, destructor or conversion operators. It is set
9425 to -1 if the declarator is a name, and +1 if it is a
9426 function. Otherwise it is set to zero. Usually you just want to
9427 test for >0, but internally the negative value is used.
9428
a723baf1
MM
9429 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9430 a decl-specifier-seq unless it declares a constructor, destructor,
9431 or conversion. It might seem that we could check this condition in
9432 semantic analysis, rather than parsing, but that makes it difficult
9433 to handle something like `f()'. We want to notice that there are
9434 no decl-specifiers, and therefore realize that this is an
9435 expression, not a declaration.) */
9436
9437static tree
94edc4ab
NN
9438cp_parser_declarator (cp_parser* parser,
9439 cp_parser_declarator_kind dcl_kind,
7efa3e22 9440 int* ctor_dtor_or_conv_p)
a723baf1
MM
9441{
9442 cp_token *token;
9443 tree declarator;
9444 enum tree_code code;
9445 tree cv_qualifier_seq;
9446 tree class_type;
9447 tree attributes = NULL_TREE;
9448
9449 /* Assume this is not a constructor, destructor, or type-conversion
9450 operator. */
9451 if (ctor_dtor_or_conv_p)
7efa3e22 9452 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
9453
9454 if (cp_parser_allow_gnu_extensions_p (parser))
9455 attributes = cp_parser_attributes_opt (parser);
9456
9457 /* Peek at the next token. */
9458 token = cp_lexer_peek_token (parser->lexer);
9459
9460 /* Check for the ptr-operator production. */
9461 cp_parser_parse_tentatively (parser);
9462 /* Parse the ptr-operator. */
9463 code = cp_parser_ptr_operator (parser,
9464 &class_type,
9465 &cv_qualifier_seq);
9466 /* If that worked, then we have a ptr-operator. */
9467 if (cp_parser_parse_definitely (parser))
9468 {
9469 /* The dependent declarator is optional if we are parsing an
9470 abstract-declarator. */
62b8a44e 9471 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9472 cp_parser_parse_tentatively (parser);
9473
9474 /* Parse the dependent declarator. */
62b8a44e 9475 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
9476 /*ctor_dtor_or_conv_p=*/NULL);
9477
9478 /* If we are parsing an abstract-declarator, we must handle the
9479 case where the dependent declarator is absent. */
62b8a44e
NS
9480 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9481 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9482 declarator = NULL_TREE;
9483
9484 /* Build the representation of the ptr-operator. */
9485 if (code == INDIRECT_REF)
9486 declarator = make_pointer_declarator (cv_qualifier_seq,
9487 declarator);
9488 else
9489 declarator = make_reference_declarator (cv_qualifier_seq,
9490 declarator);
9491 /* Handle the pointer-to-member case. */
9492 if (class_type)
9493 declarator = build_nt (SCOPE_REF, class_type, declarator);
9494 }
9495 /* Everything else is a direct-declarator. */
9496 else
7efa3e22 9497 declarator = cp_parser_direct_declarator (parser, dcl_kind,
a723baf1
MM
9498 ctor_dtor_or_conv_p);
9499
9500 if (attributes && declarator != error_mark_node)
9501 declarator = tree_cons (attributes, declarator, NULL_TREE);
9502
9503 return declarator;
9504}
9505
9506/* Parse a direct-declarator or direct-abstract-declarator.
9507
9508 direct-declarator:
9509 declarator-id
9510 direct-declarator ( parameter-declaration-clause )
9511 cv-qualifier-seq [opt]
9512 exception-specification [opt]
9513 direct-declarator [ constant-expression [opt] ]
9514 ( declarator )
9515
9516 direct-abstract-declarator:
9517 direct-abstract-declarator [opt]
9518 ( parameter-declaration-clause )
9519 cv-qualifier-seq [opt]
9520 exception-specification [opt]
9521 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9522 ( abstract-declarator )
9523
62b8a44e
NS
9524 Returns a representation of the declarator. DCL_KIND is
9525 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9526 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9527 we are parsing a direct-declarator. It is
9528 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9529 of ambiguity we prefer an abstract declarator, as per
9530 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
9531 cp_parser_declarator.
9532
9533 For the declarator-id production, the representation is as for an
9534 id-expression, except that a qualified name is represented as a
9535 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9536 see the documentation of the FUNCTION_DECLARATOR_* macros for
9537 information about how to find the various declarator components.
9538 An array-declarator is represented as an ARRAY_REF. The
9539 direct-declarator is the first operand; the constant-expression
9540 indicating the size of the array is the second operand. */
9541
9542static tree
94edc4ab
NN
9543cp_parser_direct_declarator (cp_parser* parser,
9544 cp_parser_declarator_kind dcl_kind,
7efa3e22 9545 int* ctor_dtor_or_conv_p)
a723baf1
MM
9546{
9547 cp_token *token;
62b8a44e 9548 tree declarator = NULL_TREE;
a723baf1
MM
9549 tree scope = NULL_TREE;
9550 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9551 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
9552 bool first = true;
9553
9554 while (true)
a723baf1 9555 {
62b8a44e
NS
9556 /* Peek at the next token. */
9557 token = cp_lexer_peek_token (parser->lexer);
9558 if (token->type == CPP_OPEN_PAREN)
a723baf1 9559 {
62b8a44e
NS
9560 /* This is either a parameter-declaration-clause, or a
9561 parenthesized declarator. When we know we are parsing a
34cd5ae7 9562 named declarator, it must be a parenthesized declarator
62b8a44e
NS
9563 if FIRST is true. For instance, `(int)' is a
9564 parameter-declaration-clause, with an omitted
9565 direct-abstract-declarator. But `((*))', is a
9566 parenthesized abstract declarator. Finally, when T is a
9567 template parameter `(T)' is a
34cd5ae7 9568 parameter-declaration-clause, and not a parenthesized
62b8a44e 9569 named declarator.
a723baf1 9570
62b8a44e
NS
9571 We first try and parse a parameter-declaration-clause,
9572 and then try a nested declarator (if FIRST is true).
a723baf1 9573
62b8a44e
NS
9574 It is not an error for it not to be a
9575 parameter-declaration-clause, even when FIRST is
9576 false. Consider,
9577
9578 int i (int);
9579 int i (3);
9580
9581 The first is the declaration of a function while the
9582 second is a the definition of a variable, including its
9583 initializer.
9584
9585 Having seen only the parenthesis, we cannot know which of
9586 these two alternatives should be selected. Even more
9587 complex are examples like:
9588
9589 int i (int (a));
9590 int i (int (3));
9591
9592 The former is a function-declaration; the latter is a
9593 variable initialization.
9594
34cd5ae7 9595 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
9596 that fails, we back out and return. */
9597
9598 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 9599 {
62b8a44e
NS
9600 tree params;
9601
9602 cp_parser_parse_tentatively (parser);
a723baf1 9603
62b8a44e
NS
9604 /* Consume the `('. */
9605 cp_lexer_consume_token (parser->lexer);
9606 if (first)
9607 {
9608 /* If this is going to be an abstract declarator, we're
9609 in a declarator and we can't have default args. */
9610 parser->default_arg_ok_p = false;
9611 parser->in_declarator_p = true;
9612 }
9613
9614 /* Parse the parameter-declaration-clause. */
9615 params = cp_parser_parameter_declaration_clause (parser);
9616
9617 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 9618 exception-specification. */
62b8a44e
NS
9619 if (cp_parser_parse_definitely (parser))
9620 {
9621 tree cv_qualifiers;
9622 tree exception_specification;
7efa3e22
NS
9623
9624 if (ctor_dtor_or_conv_p)
9625 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
9626 first = false;
9627 /* Consume the `)'. */
9628 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9629
9630 /* Parse the cv-qualifier-seq. */
9631 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9632 /* And the exception-specification. */
9633 exception_specification
9634 = cp_parser_exception_specification_opt (parser);
9635
9636 /* Create the function-declarator. */
9637 declarator = make_call_declarator (declarator,
9638 params,
9639 cv_qualifiers,
9640 exception_specification);
9641 /* Any subsequent parameter lists are to do with
9642 return type, so are not those of the declared
9643 function. */
9644 parser->default_arg_ok_p = false;
9645
9646 /* Repeat the main loop. */
9647 continue;
9648 }
9649 }
9650
9651 /* If this is the first, we can try a parenthesized
9652 declarator. */
9653 if (first)
a723baf1 9654 {
a723baf1 9655 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
9656 parser->in_declarator_p = saved_in_declarator_p;
9657
9658 /* Consume the `('. */
9659 cp_lexer_consume_token (parser->lexer);
9660 /* Parse the nested declarator. */
9661 declarator
9662 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9663 first = false;
9664 /* Expect a `)'. */
9665 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9666 declarator = error_mark_node;
9667 if (declarator == error_mark_node)
9668 break;
9669
9670 goto handle_declarator;
a723baf1 9671 }
9bcb9aae 9672 /* Otherwise, we must be done. */
62b8a44e
NS
9673 else
9674 break;
a723baf1 9675 }
62b8a44e
NS
9676 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9677 && token->type == CPP_OPEN_SQUARE)
a723baf1 9678 {
62b8a44e 9679 /* Parse an array-declarator. */
a723baf1
MM
9680 tree bounds;
9681
7efa3e22
NS
9682 if (ctor_dtor_or_conv_p)
9683 *ctor_dtor_or_conv_p = 0;
9684
62b8a44e
NS
9685 first = false;
9686 parser->default_arg_ok_p = false;
9687 parser->in_declarator_p = true;
a723baf1
MM
9688 /* Consume the `['. */
9689 cp_lexer_consume_token (parser->lexer);
9690 /* Peek at the next token. */
9691 token = cp_lexer_peek_token (parser->lexer);
9692 /* If the next token is `]', then there is no
9693 constant-expression. */
9694 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
9695 {
9696 bool non_constant_p;
9697
9698 bounds
9699 = cp_parser_constant_expression (parser,
9700 /*allow_non_constant=*/true,
9701 &non_constant_p);
d17811fd
MM
9702 if (!non_constant_p)
9703 bounds = cp_parser_fold_non_dependent_expr (bounds);
14d22dd6 9704 }
a723baf1
MM
9705 else
9706 bounds = NULL_TREE;
9707 /* Look for the closing `]'. */
62b8a44e
NS
9708 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9709 {
9710 declarator = error_mark_node;
9711 break;
9712 }
a723baf1
MM
9713
9714 declarator = build_nt (ARRAY_REF, declarator, bounds);
9715 }
62b8a44e 9716 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 9717 {
62b8a44e
NS
9718 /* Parse a declarator_id */
9719 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9720 cp_parser_parse_tentatively (parser);
9721 declarator = cp_parser_declarator_id (parser);
712becab
NS
9722 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9723 {
9724 if (!cp_parser_parse_definitely (parser))
9725 declarator = error_mark_node;
9726 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9727 {
9728 cp_parser_error (parser, "expected unqualified-id");
9729 declarator = error_mark_node;
9730 }
9731 }
9732
62b8a44e
NS
9733 if (declarator == error_mark_node)
9734 break;
a723baf1 9735
62b8a44e
NS
9736 if (TREE_CODE (declarator) == SCOPE_REF)
9737 {
9738 tree scope = TREE_OPERAND (declarator, 0);
712becab 9739
62b8a44e
NS
9740 /* In the declaration of a member of a template class
9741 outside of the class itself, the SCOPE will sometimes
9742 be a TYPENAME_TYPE. For example, given:
9743
9744 template <typename T>
9745 int S<T>::R::i = 3;
9746
9747 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9748 this context, we must resolve S<T>::R to an ordinary
9749 type, rather than a typename type.
9750
9751 The reason we normally avoid resolving TYPENAME_TYPEs
9752 is that a specialization of `S' might render
9753 `S<T>::R' not a type. However, if `S' is
9754 specialized, then this `i' will not be used, so there
9755 is no harm in resolving the types here. */
9756 if (TREE_CODE (scope) == TYPENAME_TYPE)
9757 {
14d22dd6
MM
9758 tree type;
9759
62b8a44e 9760 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
9761 type = resolve_typename_type (scope,
9762 /*only_current_p=*/false);
62b8a44e 9763 /* If that failed, the declarator is invalid. */
14d22dd6
MM
9764 if (type != error_mark_node)
9765 scope = type;
62b8a44e
NS
9766 /* Build a new DECLARATOR. */
9767 declarator = build_nt (SCOPE_REF,
9768 scope,
9769 TREE_OPERAND (declarator, 1));
9770 }
9771 }
9772
9773 /* Check to see whether the declarator-id names a constructor,
9774 destructor, or conversion. */
9775 if (declarator && ctor_dtor_or_conv_p
9776 && ((TREE_CODE (declarator) == SCOPE_REF
9777 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9778 || (TREE_CODE (declarator) != SCOPE_REF
9779 && at_class_scope_p ())))
a723baf1 9780 {
62b8a44e
NS
9781 tree unqualified_name;
9782 tree class_type;
9783
9784 /* Get the unqualified part of the name. */
9785 if (TREE_CODE (declarator) == SCOPE_REF)
9786 {
9787 class_type = TREE_OPERAND (declarator, 0);
9788 unqualified_name = TREE_OPERAND (declarator, 1);
9789 }
9790 else
9791 {
9792 class_type = current_class_type;
9793 unqualified_name = declarator;
9794 }
9795
9796 /* See if it names ctor, dtor or conv. */
9797 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9798 || IDENTIFIER_TYPENAME_P (unqualified_name)
9799 || constructor_name_p (unqualified_name, class_type))
7efa3e22 9800 *ctor_dtor_or_conv_p = -1;
a723baf1 9801 }
62b8a44e
NS
9802
9803 handle_declarator:;
9804 scope = get_scope_of_declarator (declarator);
9805 if (scope)
9806 /* Any names that appear after the declarator-id for a member
9807 are looked up in the containing scope. */
9808 push_scope (scope);
9809 parser->in_declarator_p = true;
9810 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9811 || (declarator
9812 && (TREE_CODE (declarator) == SCOPE_REF
9813 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9814 /* Default args are only allowed on function
9815 declarations. */
9816 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 9817 else
62b8a44e
NS
9818 parser->default_arg_ok_p = false;
9819
9820 first = false;
a723baf1 9821 }
62b8a44e 9822 /* We're done. */
a723baf1
MM
9823 else
9824 break;
a723baf1
MM
9825 }
9826
9827 /* For an abstract declarator, we might wind up with nothing at this
9828 point. That's an error; the declarator is not optional. */
9829 if (!declarator)
9830 cp_parser_error (parser, "expected declarator");
9831
9832 /* If we entered a scope, we must exit it now. */
9833 if (scope)
9834 pop_scope (scope);
9835
9836 parser->default_arg_ok_p = saved_default_arg_ok_p;
9837 parser->in_declarator_p = saved_in_declarator_p;
9838
9839 return declarator;
9840}
9841
9842/* Parse a ptr-operator.
9843
9844 ptr-operator:
9845 * cv-qualifier-seq [opt]
9846 &
9847 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9848
9849 GNU Extension:
9850
9851 ptr-operator:
9852 & cv-qualifier-seq [opt]
9853
9854 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9855 used. Returns ADDR_EXPR if a reference was used. In the
9856 case of a pointer-to-member, *TYPE is filled in with the
9857 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9858 with the cv-qualifier-seq, or NULL_TREE, if there are no
9859 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9860
9861static enum tree_code
94edc4ab
NN
9862cp_parser_ptr_operator (cp_parser* parser,
9863 tree* type,
9864 tree* cv_qualifier_seq)
a723baf1
MM
9865{
9866 enum tree_code code = ERROR_MARK;
9867 cp_token *token;
9868
9869 /* Assume that it's not a pointer-to-member. */
9870 *type = NULL_TREE;
9871 /* And that there are no cv-qualifiers. */
9872 *cv_qualifier_seq = NULL_TREE;
9873
9874 /* Peek at the next token. */
9875 token = cp_lexer_peek_token (parser->lexer);
9876 /* If it's a `*' or `&' we have a pointer or reference. */
9877 if (token->type == CPP_MULT || token->type == CPP_AND)
9878 {
9879 /* Remember which ptr-operator we were processing. */
9880 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
9881
9882 /* Consume the `*' or `&'. */
9883 cp_lexer_consume_token (parser->lexer);
9884
9885 /* A `*' can be followed by a cv-qualifier-seq, and so can a
9886 `&', if we are allowing GNU extensions. (The only qualifier
9887 that can legally appear after `&' is `restrict', but that is
9888 enforced during semantic analysis. */
9889 if (code == INDIRECT_REF
9890 || cp_parser_allow_gnu_extensions_p (parser))
9891 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9892 }
9893 else
9894 {
9895 /* Try the pointer-to-member case. */
9896 cp_parser_parse_tentatively (parser);
9897 /* Look for the optional `::' operator. */
9898 cp_parser_global_scope_opt (parser,
9899 /*current_scope_valid_p=*/false);
9900 /* Look for the nested-name specifier. */
9901 cp_parser_nested_name_specifier (parser,
9902 /*typename_keyword_p=*/false,
9903 /*check_dependency_p=*/true,
9904 /*type_p=*/false);
9905 /* If we found it, and the next token is a `*', then we are
9906 indeed looking at a pointer-to-member operator. */
9907 if (!cp_parser_error_occurred (parser)
9908 && cp_parser_require (parser, CPP_MULT, "`*'"))
9909 {
9910 /* The type of which the member is a member is given by the
9911 current SCOPE. */
9912 *type = parser->scope;
9913 /* The next name will not be qualified. */
9914 parser->scope = NULL_TREE;
9915 parser->qualifying_scope = NULL_TREE;
9916 parser->object_scope = NULL_TREE;
9917 /* Indicate that the `*' operator was used. */
9918 code = INDIRECT_REF;
9919 /* Look for the optional cv-qualifier-seq. */
9920 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9921 }
9922 /* If that didn't work we don't have a ptr-operator. */
9923 if (!cp_parser_parse_definitely (parser))
9924 cp_parser_error (parser, "expected ptr-operator");
9925 }
9926
9927 return code;
9928}
9929
9930/* Parse an (optional) cv-qualifier-seq.
9931
9932 cv-qualifier-seq:
9933 cv-qualifier cv-qualifier-seq [opt]
9934
9935 Returns a TREE_LIST. The TREE_VALUE of each node is the
9936 representation of a cv-qualifier. */
9937
9938static tree
94edc4ab 9939cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
9940{
9941 tree cv_qualifiers = NULL_TREE;
9942
9943 while (true)
9944 {
9945 tree cv_qualifier;
9946
9947 /* Look for the next cv-qualifier. */
9948 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
9949 /* If we didn't find one, we're done. */
9950 if (!cv_qualifier)
9951 break;
9952
9953 /* Add this cv-qualifier to the list. */
9954 cv_qualifiers
9955 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
9956 }
9957
9958 /* We built up the list in reverse order. */
9959 return nreverse (cv_qualifiers);
9960}
9961
9962/* Parse an (optional) cv-qualifier.
9963
9964 cv-qualifier:
9965 const
9966 volatile
9967
9968 GNU Extension:
9969
9970 cv-qualifier:
9971 __restrict__ */
9972
9973static tree
94edc4ab 9974cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
9975{
9976 cp_token *token;
9977 tree cv_qualifier = NULL_TREE;
9978
9979 /* Peek at the next token. */
9980 token = cp_lexer_peek_token (parser->lexer);
9981 /* See if it's a cv-qualifier. */
9982 switch (token->keyword)
9983 {
9984 case RID_CONST:
9985 case RID_VOLATILE:
9986 case RID_RESTRICT:
9987 /* Save the value of the token. */
9988 cv_qualifier = token->value;
9989 /* Consume the token. */
9990 cp_lexer_consume_token (parser->lexer);
9991 break;
9992
9993 default:
9994 break;
9995 }
9996
9997 return cv_qualifier;
9998}
9999
10000/* Parse a declarator-id.
10001
10002 declarator-id:
10003 id-expression
10004 :: [opt] nested-name-specifier [opt] type-name
10005
10006 In the `id-expression' case, the value returned is as for
10007 cp_parser_id_expression if the id-expression was an unqualified-id.
10008 If the id-expression was a qualified-id, then a SCOPE_REF is
10009 returned. The first operand is the scope (either a NAMESPACE_DECL
10010 or TREE_TYPE), but the second is still just a representation of an
10011 unqualified-id. */
10012
10013static tree
94edc4ab 10014cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10015{
10016 tree id_expression;
10017
10018 /* The expression must be an id-expression. Assume that qualified
10019 names are the names of types so that:
10020
10021 template <class T>
10022 int S<T>::R::i = 3;
10023
10024 will work; we must treat `S<T>::R' as the name of a type.
10025 Similarly, assume that qualified names are templates, where
10026 required, so that:
10027
10028 template <class T>
10029 int S<T>::R<T>::i = 3;
10030
10031 will work, too. */
10032 id_expression = cp_parser_id_expression (parser,
10033 /*template_keyword_p=*/false,
10034 /*check_dependency_p=*/false,
10035 /*template_p=*/NULL);
10036 /* If the name was qualified, create a SCOPE_REF to represent
10037 that. */
10038 if (parser->scope)
ec20aa6c
MM
10039 {
10040 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10041 parser->scope = NULL_TREE;
10042 }
a723baf1
MM
10043
10044 return id_expression;
10045}
10046
10047/* Parse a type-id.
10048
10049 type-id:
10050 type-specifier-seq abstract-declarator [opt]
10051
10052 Returns the TYPE specified. */
10053
10054static tree
94edc4ab 10055cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10056{
10057 tree type_specifier_seq;
10058 tree abstract_declarator;
10059
10060 /* Parse the type-specifier-seq. */
10061 type_specifier_seq
10062 = cp_parser_type_specifier_seq (parser);
10063 if (type_specifier_seq == error_mark_node)
10064 return error_mark_node;
10065
10066 /* There might or might not be an abstract declarator. */
10067 cp_parser_parse_tentatively (parser);
10068 /* Look for the declarator. */
10069 abstract_declarator
62b8a44e 10070 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10071 /* Check to see if there really was a declarator. */
10072 if (!cp_parser_parse_definitely (parser))
10073 abstract_declarator = NULL_TREE;
10074
10075 return groktypename (build_tree_list (type_specifier_seq,
10076 abstract_declarator));
10077}
10078
10079/* Parse a type-specifier-seq.
10080
10081 type-specifier-seq:
10082 type-specifier type-specifier-seq [opt]
10083
10084 GNU extension:
10085
10086 type-specifier-seq:
10087 attributes type-specifier-seq [opt]
10088
10089 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10090 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10091
10092static tree
94edc4ab 10093cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10094{
10095 bool seen_type_specifier = false;
10096 tree type_specifier_seq = NULL_TREE;
10097
10098 /* Parse the type-specifiers and attributes. */
10099 while (true)
10100 {
10101 tree type_specifier;
10102
10103 /* Check for attributes first. */
10104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10105 {
10106 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10107 NULL_TREE,
10108 type_specifier_seq);
10109 continue;
10110 }
10111
10112 /* After the first type-specifier, others are optional. */
10113 if (seen_type_specifier)
10114 cp_parser_parse_tentatively (parser);
10115 /* Look for the type-specifier. */
10116 type_specifier = cp_parser_type_specifier (parser,
10117 CP_PARSER_FLAGS_NONE,
10118 /*is_friend=*/false,
10119 /*is_declaration=*/false,
10120 NULL,
10121 NULL);
10122 /* If the first type-specifier could not be found, this is not a
10123 type-specifier-seq at all. */
10124 if (!seen_type_specifier && type_specifier == error_mark_node)
10125 return error_mark_node;
10126 /* If subsequent type-specifiers could not be found, the
10127 type-specifier-seq is complete. */
10128 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10129 break;
10130
10131 /* Add the new type-specifier to the list. */
10132 type_specifier_seq
10133 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10134 seen_type_specifier = true;
10135 }
10136
10137 /* We built up the list in reverse order. */
10138 return nreverse (type_specifier_seq);
10139}
10140
10141/* Parse a parameter-declaration-clause.
10142
10143 parameter-declaration-clause:
10144 parameter-declaration-list [opt] ... [opt]
10145 parameter-declaration-list , ...
10146
10147 Returns a representation for the parameter declarations. Each node
10148 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10149 representation.) If the parameter-declaration-clause ends with an
10150 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10151 list. A return value of NULL_TREE indicates a
10152 parameter-declaration-clause consisting only of an ellipsis. */
10153
10154static tree
94edc4ab 10155cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10156{
10157 tree parameters;
10158 cp_token *token;
10159 bool ellipsis_p;
10160
10161 /* Peek at the next token. */
10162 token = cp_lexer_peek_token (parser->lexer);
10163 /* Check for trivial parameter-declaration-clauses. */
10164 if (token->type == CPP_ELLIPSIS)
10165 {
10166 /* Consume the `...' token. */
10167 cp_lexer_consume_token (parser->lexer);
10168 return NULL_TREE;
10169 }
10170 else if (token->type == CPP_CLOSE_PAREN)
10171 /* There are no parameters. */
c73aecdf
DE
10172 {
10173#ifndef NO_IMPLICIT_EXTERN_C
10174 if (in_system_header && current_class_type == NULL
10175 && current_lang_name == lang_name_c)
10176 return NULL_TREE;
10177 else
10178#endif
10179 return void_list_node;
10180 }
a723baf1
MM
10181 /* Check for `(void)', too, which is a special case. */
10182 else if (token->keyword == RID_VOID
10183 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10184 == CPP_CLOSE_PAREN))
10185 {
10186 /* Consume the `void' token. */
10187 cp_lexer_consume_token (parser->lexer);
10188 /* There are no parameters. */
10189 return void_list_node;
10190 }
10191
10192 /* Parse the parameter-declaration-list. */
10193 parameters = cp_parser_parameter_declaration_list (parser);
10194 /* If a parse error occurred while parsing the
10195 parameter-declaration-list, then the entire
10196 parameter-declaration-clause is erroneous. */
10197 if (parameters == error_mark_node)
10198 return error_mark_node;
10199
10200 /* Peek at the next token. */
10201 token = cp_lexer_peek_token (parser->lexer);
10202 /* If it's a `,', the clause should terminate with an ellipsis. */
10203 if (token->type == CPP_COMMA)
10204 {
10205 /* Consume the `,'. */
10206 cp_lexer_consume_token (parser->lexer);
10207 /* Expect an ellipsis. */
10208 ellipsis_p
10209 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10210 }
10211 /* It might also be `...' if the optional trailing `,' was
10212 omitted. */
10213 else if (token->type == CPP_ELLIPSIS)
10214 {
10215 /* Consume the `...' token. */
10216 cp_lexer_consume_token (parser->lexer);
10217 /* And remember that we saw it. */
10218 ellipsis_p = true;
10219 }
10220 else
10221 ellipsis_p = false;
10222
10223 /* Finish the parameter list. */
10224 return finish_parmlist (parameters, ellipsis_p);
10225}
10226
10227/* Parse a parameter-declaration-list.
10228
10229 parameter-declaration-list:
10230 parameter-declaration
10231 parameter-declaration-list , parameter-declaration
10232
10233 Returns a representation of the parameter-declaration-list, as for
10234 cp_parser_parameter_declaration_clause. However, the
10235 `void_list_node' is never appended to the list. */
10236
10237static tree
94edc4ab 10238cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10239{
10240 tree parameters = NULL_TREE;
10241
10242 /* Look for more parameters. */
10243 while (true)
10244 {
10245 tree parameter;
10246 /* Parse the parameter. */
10247 parameter
ec194454
MM
10248 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10249
34cd5ae7 10250 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10251 then the entire parameter-declaration-list is erroneous. */
10252 if (parameter == error_mark_node)
10253 {
10254 parameters = error_mark_node;
10255 break;
10256 }
10257 /* Add the new parameter to the list. */
10258 TREE_CHAIN (parameter) = parameters;
10259 parameters = parameter;
10260
10261 /* Peek at the next token. */
10262 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10263 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10264 /* The parameter-declaration-list is complete. */
10265 break;
10266 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10267 {
10268 cp_token *token;
10269
10270 /* Peek at the next token. */
10271 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10272 /* If it's an ellipsis, then the list is complete. */
10273 if (token->type == CPP_ELLIPSIS)
10274 break;
10275 /* Otherwise, there must be more parameters. Consume the
10276 `,'. */
10277 cp_lexer_consume_token (parser->lexer);
10278 }
10279 else
10280 {
10281 cp_parser_error (parser, "expected `,' or `...'");
10282 break;
10283 }
10284 }
10285
10286 /* We built up the list in reverse order; straighten it out now. */
10287 return nreverse (parameters);
10288}
10289
10290/* Parse a parameter declaration.
10291
10292 parameter-declaration:
10293 decl-specifier-seq declarator
10294 decl-specifier-seq declarator = assignment-expression
10295 decl-specifier-seq abstract-declarator [opt]
10296 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10297
ec194454
MM
10298 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10299 declares a template parameter. (In that case, a non-nested `>'
10300 token encountered during the parsing of the assignment-expression
10301 is not interpreted as a greater-than operator.)
a723baf1
MM
10302
10303 Returns a TREE_LIST representing the parameter-declaration. The
10304 TREE_VALUE is a representation of the decl-specifier-seq and
10305 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10306 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10307 TREE_VALUE represents the declarator. */
10308
10309static tree
ec194454
MM
10310cp_parser_parameter_declaration (cp_parser *parser,
10311 bool template_parm_p)
a723baf1
MM
10312{
10313 bool declares_class_or_enum;
ec194454 10314 bool greater_than_is_operator_p;
a723baf1
MM
10315 tree decl_specifiers;
10316 tree attributes;
10317 tree declarator;
10318 tree default_argument;
10319 tree parameter;
10320 cp_token *token;
10321 const char *saved_message;
10322
ec194454
MM
10323 /* In a template parameter, `>' is not an operator.
10324
10325 [temp.param]
10326
10327 When parsing a default template-argument for a non-type
10328 template-parameter, the first non-nested `>' is taken as the end
10329 of the template parameter-list rather than a greater-than
10330 operator. */
10331 greater_than_is_operator_p = !template_parm_p;
10332
a723baf1
MM
10333 /* Type definitions may not appear in parameter types. */
10334 saved_message = parser->type_definition_forbidden_message;
10335 parser->type_definition_forbidden_message
10336 = "types may not be defined in parameter types";
10337
10338 /* Parse the declaration-specifiers. */
10339 decl_specifiers
10340 = cp_parser_decl_specifier_seq (parser,
10341 CP_PARSER_FLAGS_NONE,
10342 &attributes,
10343 &declares_class_or_enum);
10344 /* If an error occurred, there's no reason to attempt to parse the
10345 rest of the declaration. */
10346 if (cp_parser_error_occurred (parser))
10347 {
10348 parser->type_definition_forbidden_message = saved_message;
10349 return error_mark_node;
10350 }
10351
10352 /* Peek at the next token. */
10353 token = cp_lexer_peek_token (parser->lexer);
10354 /* If the next token is a `)', `,', `=', `>', or `...', then there
10355 is no declarator. */
10356 if (token->type == CPP_CLOSE_PAREN
10357 || token->type == CPP_COMMA
10358 || token->type == CPP_EQ
10359 || token->type == CPP_ELLIPSIS
10360 || token->type == CPP_GREATER)
10361 declarator = NULL_TREE;
10362 /* Otherwise, there should be a declarator. */
10363 else
10364 {
10365 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10366 parser->default_arg_ok_p = false;
10367
a723baf1 10368 declarator = cp_parser_declarator (parser,
62b8a44e 10369 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10370 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10371 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10372 /* After the declarator, allow more attributes. */
10373 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10374 }
10375
62b8a44e 10376 /* The restriction on defining new types applies only to the type
a723baf1
MM
10377 of the parameter, not to the default argument. */
10378 parser->type_definition_forbidden_message = saved_message;
10379
10380 /* If the next token is `=', then process a default argument. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10382 {
10383 bool saved_greater_than_is_operator_p;
10384 /* Consume the `='. */
10385 cp_lexer_consume_token (parser->lexer);
10386
10387 /* If we are defining a class, then the tokens that make up the
10388 default argument must be saved and processed later. */
ec194454
MM
10389 if (!template_parm_p && at_class_scope_p ()
10390 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10391 {
10392 unsigned depth = 0;
10393
10394 /* Create a DEFAULT_ARG to represented the unparsed default
10395 argument. */
10396 default_argument = make_node (DEFAULT_ARG);
10397 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10398
10399 /* Add tokens until we have processed the entire default
10400 argument. */
10401 while (true)
10402 {
10403 bool done = false;
10404 cp_token *token;
10405
10406 /* Peek at the next token. */
10407 token = cp_lexer_peek_token (parser->lexer);
10408 /* What we do depends on what token we have. */
10409 switch (token->type)
10410 {
10411 /* In valid code, a default argument must be
10412 immediately followed by a `,' `)', or `...'. */
10413 case CPP_COMMA:
10414 case CPP_CLOSE_PAREN:
10415 case CPP_ELLIPSIS:
10416 /* If we run into a non-nested `;', `}', or `]',
10417 then the code is invalid -- but the default
10418 argument is certainly over. */
10419 case CPP_SEMICOLON:
10420 case CPP_CLOSE_BRACE:
10421 case CPP_CLOSE_SQUARE:
10422 if (depth == 0)
10423 done = true;
10424 /* Update DEPTH, if necessary. */
10425 else if (token->type == CPP_CLOSE_PAREN
10426 || token->type == CPP_CLOSE_BRACE
10427 || token->type == CPP_CLOSE_SQUARE)
10428 --depth;
10429 break;
10430
10431 case CPP_OPEN_PAREN:
10432 case CPP_OPEN_SQUARE:
10433 case CPP_OPEN_BRACE:
10434 ++depth;
10435 break;
10436
10437 case CPP_GREATER:
10438 /* If we see a non-nested `>', and `>' is not an
10439 operator, then it marks the end of the default
10440 argument. */
10441 if (!depth && !greater_than_is_operator_p)
10442 done = true;
10443 break;
10444
10445 /* If we run out of tokens, issue an error message. */
10446 case CPP_EOF:
10447 error ("file ends in default argument");
10448 done = true;
10449 break;
10450
10451 case CPP_NAME:
10452 case CPP_SCOPE:
10453 /* In these cases, we should look for template-ids.
10454 For example, if the default argument is
10455 `X<int, double>()', we need to do name lookup to
10456 figure out whether or not `X' is a template; if
34cd5ae7 10457 so, the `,' does not end the default argument.
a723baf1
MM
10458
10459 That is not yet done. */
10460 break;
10461
10462 default:
10463 break;
10464 }
10465
10466 /* If we've reached the end, stop. */
10467 if (done)
10468 break;
10469
10470 /* Add the token to the token block. */
10471 token = cp_lexer_consume_token (parser->lexer);
10472 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10473 token);
10474 }
10475 }
10476 /* Outside of a class definition, we can just parse the
10477 assignment-expression. */
10478 else
10479 {
10480 bool saved_local_variables_forbidden_p;
10481
10482 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10483 set correctly. */
10484 saved_greater_than_is_operator_p
10485 = parser->greater_than_is_operator_p;
10486 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10487 /* Local variable names (and the `this' keyword) may not
10488 appear in a default argument. */
10489 saved_local_variables_forbidden_p
10490 = parser->local_variables_forbidden_p;
10491 parser->local_variables_forbidden_p = true;
10492 /* Parse the assignment-expression. */
10493 default_argument = cp_parser_assignment_expression (parser);
10494 /* Restore saved state. */
10495 parser->greater_than_is_operator_p
10496 = saved_greater_than_is_operator_p;
10497 parser->local_variables_forbidden_p
10498 = saved_local_variables_forbidden_p;
10499 }
10500 if (!parser->default_arg_ok_p)
10501 {
c67d36d0
NS
10502 if (!flag_pedantic_errors)
10503 warning ("deprecated use of default argument for parameter of non-function");
10504 else
10505 {
10506 error ("default arguments are only permitted for function parameters");
10507 default_argument = NULL_TREE;
10508 }
a723baf1
MM
10509 }
10510 }
10511 else
10512 default_argument = NULL_TREE;
10513
10514 /* Create the representation of the parameter. */
10515 if (attributes)
10516 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10517 parameter = build_tree_list (default_argument,
10518 build_tree_list (decl_specifiers,
10519 declarator));
10520
10521 return parameter;
10522}
10523
10524/* Parse a function-definition.
10525
10526 function-definition:
10527 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10528 function-body
10529 decl-specifier-seq [opt] declarator function-try-block
10530
10531 GNU Extension:
10532
10533 function-definition:
10534 __extension__ function-definition
10535
10536 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10537 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10538 be a `friend'. */
10539
10540static tree
94edc4ab 10541cp_parser_function_definition (cp_parser* parser, bool* friend_p)
a723baf1
MM
10542{
10543 tree decl_specifiers;
10544 tree attributes;
10545 tree declarator;
10546 tree fn;
a723baf1
MM
10547 cp_token *token;
10548 bool declares_class_or_enum;
10549 bool member_p;
10550 /* The saved value of the PEDANTIC flag. */
10551 int saved_pedantic;
10552
10553 /* Any pending qualification must be cleared by our caller. It is
10554 more robust to force the callers to clear PARSER->SCOPE than to
10555 do it here since if the qualification is in effect here, it might
10556 also end up in effect elsewhere that it is not intended. */
10557 my_friendly_assert (!parser->scope, 20010821);
10558
10559 /* Handle `__extension__'. */
10560 if (cp_parser_extension_opt (parser, &saved_pedantic))
10561 {
10562 /* Parse the function-definition. */
10563 fn = cp_parser_function_definition (parser, friend_p);
10564 /* Restore the PEDANTIC flag. */
10565 pedantic = saved_pedantic;
10566
10567 return fn;
10568 }
10569
10570 /* Check to see if this definition appears in a class-specifier. */
10571 member_p = (at_class_scope_p ()
10572 && TYPE_BEING_DEFINED (current_class_type));
10573 /* Defer access checks in the decl-specifier-seq until we know what
10574 function is being defined. There is no need to do this for the
10575 definition of member functions; we cannot be defining a member
10576 from another class. */
8d241e0b 10577 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
cf22909c 10578
a723baf1
MM
10579 /* Parse the decl-specifier-seq. */
10580 decl_specifiers
10581 = cp_parser_decl_specifier_seq (parser,
10582 CP_PARSER_FLAGS_OPTIONAL,
10583 &attributes,
10584 &declares_class_or_enum);
10585 /* Figure out whether this declaration is a `friend'. */
10586 if (friend_p)
10587 *friend_p = cp_parser_friend_p (decl_specifiers);
10588
10589 /* Parse the declarator. */
62b8a44e 10590 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
10591 /*ctor_dtor_or_conv_p=*/NULL);
10592
10593 /* Gather up any access checks that occurred. */
cf22909c 10594 stop_deferring_access_checks ();
a723baf1
MM
10595
10596 /* If something has already gone wrong, we may as well stop now. */
10597 if (declarator == error_mark_node)
10598 {
10599 /* Skip to the end of the function, or if this wasn't anything
10600 like a function-definition, to a `;' in the hopes of finding
10601 a sensible place from which to continue parsing. */
10602 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10603 pop_deferring_access_checks ();
a723baf1
MM
10604 return error_mark_node;
10605 }
10606
10607 /* The next character should be a `{' (for a simple function
10608 definition), a `:' (for a ctor-initializer), or `try' (for a
10609 function-try block). */
10610 token = cp_lexer_peek_token (parser->lexer);
10611 if (!cp_parser_token_starts_function_definition_p (token))
10612 {
10613 /* Issue the error-message. */
10614 cp_parser_error (parser, "expected function-definition");
10615 /* Skip to the next `;'. */
10616 cp_parser_skip_to_end_of_block_or_statement (parser);
10617
cf22909c 10618 pop_deferring_access_checks ();
a723baf1
MM
10619 return error_mark_node;
10620 }
10621
10622 /* If we are in a class scope, then we must handle
10623 function-definitions specially. In particular, we save away the
10624 tokens that make up the function body, and parse them again
10625 later, in order to handle code like:
10626
10627 struct S {
10628 int f () { return i; }
10629 int i;
10630 };
10631
10632 Here, we cannot parse the body of `f' until after we have seen
10633 the declaration of `i'. */
10634 if (member_p)
10635 {
10636 cp_token_cache *cache;
10637
10638 /* Create the function-declaration. */
10639 fn = start_method (decl_specifiers, declarator, attributes);
10640 /* If something went badly wrong, bail out now. */
10641 if (fn == error_mark_node)
10642 {
10643 /* If there's a function-body, skip it. */
10644 if (cp_parser_token_starts_function_definition_p
10645 (cp_lexer_peek_token (parser->lexer)))
10646 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10647 pop_deferring_access_checks ();
a723baf1
MM
10648 return error_mark_node;
10649 }
10650
8db1028e
NS
10651 /* Remember it, if there default args to post process. */
10652 cp_parser_save_default_args (parser, fn);
10653
a723baf1
MM
10654 /* Create a token cache. */
10655 cache = cp_token_cache_new ();
10656 /* Save away the tokens that make up the body of the
10657 function. */
10658 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10659 /* Handle function try blocks. */
10660 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10661 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10662
10663 /* Save away the inline definition; we will process it when the
10664 class is complete. */
10665 DECL_PENDING_INLINE_INFO (fn) = cache;
10666 DECL_PENDING_INLINE_P (fn) = 1;
10667
649fc72d
NS
10668 /* We need to know that this was defined in the class, so that
10669 friend templates are handled correctly. */
10670 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10671
a723baf1
MM
10672 /* We're done with the inline definition. */
10673 finish_method (fn);
10674
10675 /* Add FN to the queue of functions to be parsed later. */
10676 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 10677 = tree_cons (NULL_TREE, fn,
a723baf1
MM
10678 TREE_VALUE (parser->unparsed_functions_queues));
10679
cf22909c 10680 pop_deferring_access_checks ();
a723baf1
MM
10681 return fn;
10682 }
10683
10684 /* Check that the number of template-parameter-lists is OK. */
10685 if (!cp_parser_check_declarator_template_parameters (parser,
10686 declarator))
10687 {
10688 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10689 pop_deferring_access_checks ();
a723baf1
MM
10690 return error_mark_node;
10691 }
10692
cf22909c
KL
10693 fn = cp_parser_function_definition_from_specifiers_and_declarator
10694 (parser, decl_specifiers, attributes, declarator);
10695 pop_deferring_access_checks ();
10696 return fn;
a723baf1
MM
10697}
10698
10699/* Parse a function-body.
10700
10701 function-body:
10702 compound_statement */
10703
10704static void
10705cp_parser_function_body (cp_parser *parser)
10706{
a5bcc582 10707 cp_parser_compound_statement (parser, false);
a723baf1
MM
10708}
10709
10710/* Parse a ctor-initializer-opt followed by a function-body. Return
10711 true if a ctor-initializer was present. */
10712
10713static bool
10714cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10715{
10716 tree body;
10717 bool ctor_initializer_p;
10718
10719 /* Begin the function body. */
10720 body = begin_function_body ();
10721 /* Parse the optional ctor-initializer. */
10722 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10723 /* Parse the function-body. */
10724 cp_parser_function_body (parser);
10725 /* Finish the function body. */
10726 finish_function_body (body);
10727
10728 return ctor_initializer_p;
10729}
10730
10731/* Parse an initializer.
10732
10733 initializer:
10734 = initializer-clause
10735 ( expression-list )
10736
10737 Returns a expression representing the initializer. If no
10738 initializer is present, NULL_TREE is returned.
10739
10740 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10741 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
10742 set to FALSE if there is no initializer present. If there is an
10743 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10744 is set to true; otherwise it is set to false. */
a723baf1
MM
10745
10746static tree
39703eb9
MM
10747cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10748 bool* non_constant_p)
a723baf1
MM
10749{
10750 cp_token *token;
10751 tree init;
10752
10753 /* Peek at the next token. */
10754 token = cp_lexer_peek_token (parser->lexer);
10755
10756 /* Let our caller know whether or not this initializer was
10757 parenthesized. */
10758 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
10759 /* Assume that the initializer is constant. */
10760 *non_constant_p = false;
a723baf1
MM
10761
10762 if (token->type == CPP_EQ)
10763 {
10764 /* Consume the `='. */
10765 cp_lexer_consume_token (parser->lexer);
10766 /* Parse the initializer-clause. */
39703eb9 10767 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
10768 }
10769 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
10770 init = cp_parser_parenthesized_expression_list (parser, false,
10771 non_constant_p);
a723baf1
MM
10772 else
10773 {
10774 /* Anything else is an error. */
10775 cp_parser_error (parser, "expected initializer");
10776 init = error_mark_node;
10777 }
10778
10779 return init;
10780}
10781
10782/* Parse an initializer-clause.
10783
10784 initializer-clause:
10785 assignment-expression
10786 { initializer-list , [opt] }
10787 { }
10788
10789 Returns an expression representing the initializer.
10790
10791 If the `assignment-expression' production is used the value
34cd5ae7 10792 returned is simply a representation for the expression.
a723baf1
MM
10793
10794 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10795 the elements of the initializer-list (or NULL_TREE, if the last
10796 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10797 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
10798 trailing `,' was provided. NON_CONSTANT_P is as for
10799 cp_parser_initializer. */
a723baf1
MM
10800
10801static tree
39703eb9 10802cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
10803{
10804 tree initializer;
10805
10806 /* If it is not a `{', then we are looking at an
10807 assignment-expression. */
10808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
10809 initializer
10810 = cp_parser_constant_expression (parser,
10811 /*allow_non_constant_p=*/true,
10812 non_constant_p);
a723baf1
MM
10813 else
10814 {
10815 /* Consume the `{' token. */
10816 cp_lexer_consume_token (parser->lexer);
10817 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10818 initializer = make_node (CONSTRUCTOR);
10819 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10820 necessary, but check_initializer depends upon it, for
10821 now. */
10822 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10823 /* If it's not a `}', then there is a non-trivial initializer. */
10824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10825 {
10826 /* Parse the initializer list. */
10827 CONSTRUCTOR_ELTS (initializer)
39703eb9 10828 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
10829 /* A trailing `,' token is allowed. */
10830 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10831 cp_lexer_consume_token (parser->lexer);
10832 }
a723baf1
MM
10833 /* Now, there should be a trailing `}'. */
10834 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10835 }
10836
10837 return initializer;
10838}
10839
10840/* Parse an initializer-list.
10841
10842 initializer-list:
10843 initializer-clause
10844 initializer-list , initializer-clause
10845
10846 GNU Extension:
10847
10848 initializer-list:
10849 identifier : initializer-clause
10850 initializer-list, identifier : initializer-clause
10851
10852 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10853 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
10854 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10855 as for cp_parser_initializer. */
a723baf1
MM
10856
10857static tree
39703eb9 10858cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
10859{
10860 tree initializers = NULL_TREE;
10861
39703eb9
MM
10862 /* Assume all of the expressions are constant. */
10863 *non_constant_p = false;
10864
a723baf1
MM
10865 /* Parse the rest of the list. */
10866 while (true)
10867 {
10868 cp_token *token;
10869 tree identifier;
10870 tree initializer;
39703eb9
MM
10871 bool clause_non_constant_p;
10872
a723baf1
MM
10873 /* If the next token is an identifier and the following one is a
10874 colon, we are looking at the GNU designated-initializer
10875 syntax. */
10876 if (cp_parser_allow_gnu_extensions_p (parser)
10877 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10878 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
10879 {
10880 /* Consume the identifier. */
10881 identifier = cp_lexer_consume_token (parser->lexer)->value;
10882 /* Consume the `:'. */
10883 cp_lexer_consume_token (parser->lexer);
10884 }
10885 else
10886 identifier = NULL_TREE;
10887
10888 /* Parse the initializer. */
39703eb9
MM
10889 initializer = cp_parser_initializer_clause (parser,
10890 &clause_non_constant_p);
10891 /* If any clause is non-constant, so is the entire initializer. */
10892 if (clause_non_constant_p)
10893 *non_constant_p = true;
a723baf1
MM
10894 /* Add it to the list. */
10895 initializers = tree_cons (identifier, initializer, initializers);
10896
10897 /* If the next token is not a comma, we have reached the end of
10898 the list. */
10899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10900 break;
10901
10902 /* Peek at the next token. */
10903 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10904 /* If the next token is a `}', then we're still done. An
10905 initializer-clause can have a trailing `,' after the
10906 initializer-list and before the closing `}'. */
10907 if (token->type == CPP_CLOSE_BRACE)
10908 break;
10909
10910 /* Consume the `,' token. */
10911 cp_lexer_consume_token (parser->lexer);
10912 }
10913
10914 /* The initializers were built up in reverse order, so we need to
10915 reverse them now. */
10916 return nreverse (initializers);
10917}
10918
10919/* Classes [gram.class] */
10920
10921/* Parse a class-name.
10922
10923 class-name:
10924 identifier
10925 template-id
10926
10927 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
10928 to indicate that names looked up in dependent types should be
10929 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
10930 keyword has been used to indicate that the name that appears next
10931 is a template. TYPE_P is true iff the next name should be treated
10932 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
10933 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10934 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
10935 being defined in a class-head.
a723baf1
MM
10936
10937 Returns the TYPE_DECL representing the class. */
10938
10939static tree
10940cp_parser_class_name (cp_parser *parser,
10941 bool typename_keyword_p,
10942 bool template_keyword_p,
10943 bool type_p,
a723baf1
MM
10944 bool check_dependency_p,
10945 bool class_head_p)
10946{
10947 tree decl;
10948 tree scope;
10949 bool typename_p;
e5976695
MM
10950 cp_token *token;
10951
10952 /* All class-names start with an identifier. */
10953 token = cp_lexer_peek_token (parser->lexer);
10954 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
10955 {
10956 cp_parser_error (parser, "expected class-name");
10957 return error_mark_node;
10958 }
10959
a723baf1
MM
10960 /* PARSER->SCOPE can be cleared when parsing the template-arguments
10961 to a template-id, so we save it here. */
10962 scope = parser->scope;
10963 /* Any name names a type if we're following the `typename' keyword
10964 in a qualified name where the enclosing scope is type-dependent. */
10965 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 10966 && dependent_type_p (scope));
e5976695
MM
10967 /* Handle the common case (an identifier, but not a template-id)
10968 efficiently. */
10969 if (token->type == CPP_NAME
10970 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 10971 {
a723baf1
MM
10972 tree identifier;
10973
10974 /* Look for the identifier. */
10975 identifier = cp_parser_identifier (parser);
10976 /* If the next token isn't an identifier, we are certainly not
10977 looking at a class-name. */
10978 if (identifier == error_mark_node)
10979 decl = error_mark_node;
10980 /* If we know this is a type-name, there's no need to look it
10981 up. */
10982 else if (typename_p)
10983 decl = identifier;
10984 else
10985 {
10986 /* If the next token is a `::', then the name must be a type
10987 name.
10988
10989 [basic.lookup.qual]
10990
10991 During the lookup for a name preceding the :: scope
10992 resolution operator, object, function, and enumerator
10993 names are ignored. */
10994 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10995 type_p = true;
10996 /* Look up the name. */
10997 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 10998 type_p,
eea9800f 10999 /*is_namespace=*/false,
a723baf1
MM
11000 check_dependency_p);
11001 }
11002 }
e5976695
MM
11003 else
11004 {
11005 /* Try a template-id. */
11006 decl = cp_parser_template_id (parser, template_keyword_p,
11007 check_dependency_p);
11008 if (decl == error_mark_node)
11009 return error_mark_node;
11010 }
a723baf1
MM
11011
11012 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11013
11014 /* If this is a typename, create a TYPENAME_TYPE. */
11015 if (typename_p && decl != error_mark_node)
11016 decl = TYPE_NAME (make_typename_type (scope, decl,
11017 /*complain=*/1));
11018
11019 /* Check to see that it is really the name of a class. */
11020 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11021 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11022 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11023 /* Situations like this:
11024
11025 template <typename T> struct A {
11026 typename T::template X<int>::I i;
11027 };
11028
11029 are problematic. Is `T::template X<int>' a class-name? The
11030 standard does not seem to be definitive, but there is no other
11031 valid interpretation of the following `::'. Therefore, those
11032 names are considered class-names. */
78757caa 11033 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11034 else if (decl == error_mark_node
11035 || TREE_CODE (decl) != TYPE_DECL
11036 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11037 {
11038 cp_parser_error (parser, "expected class-name");
11039 return error_mark_node;
11040 }
11041
11042 return decl;
11043}
11044
11045/* Parse a class-specifier.
11046
11047 class-specifier:
11048 class-head { member-specification [opt] }
11049
11050 Returns the TREE_TYPE representing the class. */
11051
11052static tree
94edc4ab 11053cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11054{
11055 cp_token *token;
11056 tree type;
11057 tree attributes = NULL_TREE;
11058 int has_trailing_semicolon;
11059 bool nested_name_specifier_p;
a723baf1
MM
11060 unsigned saved_num_template_parameter_lists;
11061
8d241e0b 11062 push_deferring_access_checks (dk_no_deferred);
cf22909c 11063
a723baf1
MM
11064 /* Parse the class-head. */
11065 type = cp_parser_class_head (parser,
cf22909c 11066 &nested_name_specifier_p);
a723baf1
MM
11067 /* If the class-head was a semantic disaster, skip the entire body
11068 of the class. */
11069 if (!type)
11070 {
11071 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11072 pop_deferring_access_checks ();
a723baf1
MM
11073 return error_mark_node;
11074 }
cf22909c 11075
a723baf1
MM
11076 /* Look for the `{'. */
11077 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11078 {
11079 pop_deferring_access_checks ();
11080 return error_mark_node;
11081 }
11082
a723baf1
MM
11083 /* Issue an error message if type-definitions are forbidden here. */
11084 cp_parser_check_type_definition (parser);
11085 /* Remember that we are defining one more class. */
11086 ++parser->num_classes_being_defined;
11087 /* Inside the class, surrounding template-parameter-lists do not
11088 apply. */
11089 saved_num_template_parameter_lists
11090 = parser->num_template_parameter_lists;
11091 parser->num_template_parameter_lists = 0;
78757caa 11092
a723baf1
MM
11093 /* Start the class. */
11094 type = begin_class_definition (type);
11095 if (type == error_mark_node)
9bcb9aae 11096 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11097 cp_parser_skip_to_closing_brace (parser);
11098 else
11099 /* Parse the member-specification. */
11100 cp_parser_member_specification_opt (parser);
11101 /* Look for the trailing `}'. */
11102 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11103 /* We get better error messages by noticing a common problem: a
11104 missing trailing `;'. */
11105 token = cp_lexer_peek_token (parser->lexer);
11106 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11107 /* Look for attributes to apply to this class. */
11108 if (cp_parser_allow_gnu_extensions_p (parser))
11109 attributes = cp_parser_attributes_opt (parser);
11110 /* Finish the class definition. */
11111 type = finish_class_definition (type,
11112 attributes,
11113 has_trailing_semicolon,
11114 nested_name_specifier_p);
11115 /* If this class is not itself within the scope of another class,
11116 then we need to parse the bodies of all of the queued function
11117 definitions. Note that the queued functions defined in a class
11118 are not always processed immediately following the
11119 class-specifier for that class. Consider:
11120
11121 struct A {
11122 struct B { void f() { sizeof (A); } };
11123 };
11124
11125 If `f' were processed before the processing of `A' were
11126 completed, there would be no way to compute the size of `A'.
11127 Note that the nesting we are interested in here is lexical --
11128 not the semantic nesting given by TYPE_CONTEXT. In particular,
11129 for:
11130
11131 struct A { struct B; };
11132 struct A::B { void f() { } };
11133
11134 there is no need to delay the parsing of `A::B::f'. */
11135 if (--parser->num_classes_being_defined == 0)
11136 {
8218bd34
MM
11137 tree queue_entry;
11138 tree fn;
a723baf1 11139
8218bd34
MM
11140 /* In a first pass, parse default arguments to the functions.
11141 Then, in a second pass, parse the bodies of the functions.
11142 This two-phased approach handles cases like:
11143
11144 struct S {
11145 void f() { g(); }
11146 void g(int i = 3);
11147 };
11148
11149 */
8db1028e
NS
11150 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11151 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11152 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11153 TREE_PURPOSE (parser->unparsed_functions_queues)
11154 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11155 {
11156 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11157 /* Make sure that any template parameters are in scope. */
11158 maybe_begin_member_template_processing (fn);
11159 /* If there are default arguments that have not yet been processed,
11160 take care of them now. */
11161 cp_parser_late_parsing_default_args (parser, fn);
11162 /* Remove any template parameters from the symbol table. */
11163 maybe_end_member_template_processing ();
11164 }
11165 /* Now parse the body of the functions. */
8db1028e
NS
11166 for (TREE_VALUE (parser->unparsed_functions_queues)
11167 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11168 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11169 TREE_VALUE (parser->unparsed_functions_queues)
11170 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11171 {
a723baf1 11172 /* Figure out which function we need to process. */
a723baf1
MM
11173 fn = TREE_VALUE (queue_entry);
11174
11175 /* Parse the function. */
11176 cp_parser_late_parsing_for_member (parser, fn);
a723baf1
MM
11177 }
11178
a723baf1
MM
11179 }
11180
11181 /* Put back any saved access checks. */
cf22909c 11182 pop_deferring_access_checks ();
a723baf1
MM
11183
11184 /* Restore the count of active template-parameter-lists. */
11185 parser->num_template_parameter_lists
11186 = saved_num_template_parameter_lists;
11187
11188 return type;
11189}
11190
11191/* Parse a class-head.
11192
11193 class-head:
11194 class-key identifier [opt] base-clause [opt]
11195 class-key nested-name-specifier identifier base-clause [opt]
11196 class-key nested-name-specifier [opt] template-id
11197 base-clause [opt]
11198
11199 GNU Extensions:
11200 class-key attributes identifier [opt] base-clause [opt]
11201 class-key attributes nested-name-specifier identifier base-clause [opt]
11202 class-key attributes nested-name-specifier [opt] template-id
11203 base-clause [opt]
11204
11205 Returns the TYPE of the indicated class. Sets
11206 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11207 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11208
11209 Returns NULL_TREE if the class-head is syntactically valid, but
11210 semantically invalid in a way that means we should skip the entire
11211 body of the class. */
11212
11213static tree
94edc4ab
NN
11214cp_parser_class_head (cp_parser* parser,
11215 bool* nested_name_specifier_p)
a723baf1
MM
11216{
11217 cp_token *token;
11218 tree nested_name_specifier;
11219 enum tag_types class_key;
11220 tree id = NULL_TREE;
11221 tree type = NULL_TREE;
11222 tree attributes;
11223 bool template_id_p = false;
11224 bool qualified_p = false;
11225 bool invalid_nested_name_p = false;
11226 unsigned num_templates;
11227
11228 /* Assume no nested-name-specifier will be present. */
11229 *nested_name_specifier_p = false;
11230 /* Assume no template parameter lists will be used in defining the
11231 type. */
11232 num_templates = 0;
11233
11234 /* Look for the class-key. */
11235 class_key = cp_parser_class_key (parser);
11236 if (class_key == none_type)
11237 return error_mark_node;
11238
11239 /* Parse the attributes. */
11240 attributes = cp_parser_attributes_opt (parser);
11241
11242 /* If the next token is `::', that is invalid -- but sometimes
11243 people do try to write:
11244
11245 struct ::S {};
11246
11247 Handle this gracefully by accepting the extra qualifier, and then
11248 issuing an error about it later if this really is a
2050a1bb 11249 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11250 specifier, remain silent. */
11251 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11252 qualified_p = true;
11253
8d241e0b
KL
11254 push_deferring_access_checks (dk_no_check);
11255
a723baf1
MM
11256 /* Determine the name of the class. Begin by looking for an
11257 optional nested-name-specifier. */
11258 nested_name_specifier
11259 = cp_parser_nested_name_specifier_opt (parser,
11260 /*typename_keyword_p=*/false,
66d418e6 11261 /*check_dependency_p=*/false,
a723baf1
MM
11262 /*type_p=*/false);
11263 /* If there was a nested-name-specifier, then there *must* be an
11264 identifier. */
11265 if (nested_name_specifier)
11266 {
11267 /* Although the grammar says `identifier', it really means
11268 `class-name' or `template-name'. You are only allowed to
11269 define a class that has already been declared with this
11270 syntax.
11271
11272 The proposed resolution for Core Issue 180 says that whever
11273 you see `class T::X' you should treat `X' as a type-name.
11274
11275 It is OK to define an inaccessible class; for example:
11276
11277 class A { class B; };
11278 class A::B {};
11279
a723baf1
MM
11280 We do not know if we will see a class-name, or a
11281 template-name. We look for a class-name first, in case the
11282 class-name is a template-id; if we looked for the
11283 template-name first we would stop after the template-name. */
11284 cp_parser_parse_tentatively (parser);
11285 type = cp_parser_class_name (parser,
11286 /*typename_keyword_p=*/false,
11287 /*template_keyword_p=*/false,
11288 /*type_p=*/true,
a723baf1
MM
11289 /*check_dependency_p=*/false,
11290 /*class_head_p=*/true);
11291 /* If that didn't work, ignore the nested-name-specifier. */
11292 if (!cp_parser_parse_definitely (parser))
11293 {
11294 invalid_nested_name_p = true;
11295 id = cp_parser_identifier (parser);
11296 if (id == error_mark_node)
11297 id = NULL_TREE;
11298 }
11299 /* If we could not find a corresponding TYPE, treat this
11300 declaration like an unqualified declaration. */
11301 if (type == error_mark_node)
11302 nested_name_specifier = NULL_TREE;
11303 /* Otherwise, count the number of templates used in TYPE and its
11304 containing scopes. */
11305 else
11306 {
11307 tree scope;
11308
11309 for (scope = TREE_TYPE (type);
11310 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11311 scope = (TYPE_P (scope)
11312 ? TYPE_CONTEXT (scope)
11313 : DECL_CONTEXT (scope)))
11314 if (TYPE_P (scope)
11315 && CLASS_TYPE_P (scope)
11316 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11317 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11318 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11319 ++num_templates;
11320 }
11321 }
11322 /* Otherwise, the identifier is optional. */
11323 else
11324 {
11325 /* We don't know whether what comes next is a template-id,
11326 an identifier, or nothing at all. */
11327 cp_parser_parse_tentatively (parser);
11328 /* Check for a template-id. */
11329 id = cp_parser_template_id (parser,
11330 /*template_keyword_p=*/false,
11331 /*check_dependency_p=*/true);
11332 /* If that didn't work, it could still be an identifier. */
11333 if (!cp_parser_parse_definitely (parser))
11334 {
11335 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11336 id = cp_parser_identifier (parser);
11337 else
11338 id = NULL_TREE;
11339 }
11340 else
11341 {
11342 template_id_p = true;
11343 ++num_templates;
11344 }
11345 }
11346
8d241e0b
KL
11347 pop_deferring_access_checks ();
11348
a723baf1
MM
11349 /* If it's not a `:' or a `{' then we can't really be looking at a
11350 class-head, since a class-head only appears as part of a
11351 class-specifier. We have to detect this situation before calling
11352 xref_tag, since that has irreversible side-effects. */
11353 if (!cp_parser_next_token_starts_class_definition_p (parser))
11354 {
11355 cp_parser_error (parser, "expected `{' or `:'");
11356 return error_mark_node;
11357 }
11358
11359 /* At this point, we're going ahead with the class-specifier, even
11360 if some other problem occurs. */
11361 cp_parser_commit_to_tentative_parse (parser);
11362 /* Issue the error about the overly-qualified name now. */
11363 if (qualified_p)
11364 cp_parser_error (parser,
11365 "global qualification of class name is invalid");
11366 else if (invalid_nested_name_p)
11367 cp_parser_error (parser,
11368 "qualified name does not name a class");
11369 /* Make sure that the right number of template parameters were
11370 present. */
11371 if (!cp_parser_check_template_parameters (parser, num_templates))
11372 /* If something went wrong, there is no point in even trying to
11373 process the class-definition. */
11374 return NULL_TREE;
11375
a723baf1
MM
11376 /* Look up the type. */
11377 if (template_id_p)
11378 {
11379 type = TREE_TYPE (id);
11380 maybe_process_partial_specialization (type);
11381 }
11382 else if (!nested_name_specifier)
11383 {
11384 /* If the class was unnamed, create a dummy name. */
11385 if (!id)
11386 id = make_anon_name ();
11387 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11388 }
11389 else
11390 {
a723baf1 11391 tree class_type;
089d6ea7 11392 tree scope;
a723baf1
MM
11393
11394 /* Given:
11395
11396 template <typename T> struct S { struct T };
14d22dd6 11397 template <typename T> struct S<T>::T { };
a723baf1
MM
11398
11399 we will get a TYPENAME_TYPE when processing the definition of
11400 `S::T'. We need to resolve it to the actual type before we
11401 try to define it. */
11402 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11403 {
14d22dd6
MM
11404 class_type = resolve_typename_type (TREE_TYPE (type),
11405 /*only_current_p=*/false);
11406 if (class_type != error_mark_node)
11407 type = TYPE_NAME (class_type);
11408 else
11409 {
11410 cp_parser_error (parser, "could not resolve typename type");
11411 type = error_mark_node;
11412 }
a723baf1
MM
11413 }
11414
089d6ea7
MM
11415 /* Figure out in what scope the declaration is being placed. */
11416 scope = current_scope ();
11417 if (!scope)
11418 scope = current_namespace;
11419 /* If that scope does not contain the scope in which the
11420 class was originally declared, the program is invalid. */
11421 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11422 {
0e136342 11423 error ("declaration of `%D' in `%D' which does not "
089d6ea7
MM
11424 "enclose `%D'", type, scope, nested_name_specifier);
11425 return NULL_TREE;
11426 }
11427
a723baf1
MM
11428 maybe_process_partial_specialization (TREE_TYPE (type));
11429 class_type = current_class_type;
11430 type = TREE_TYPE (handle_class_head (class_key,
11431 nested_name_specifier,
11432 type,
089d6ea7 11433 attributes));
a723baf1
MM
11434 if (type != error_mark_node)
11435 {
11436 if (!class_type && TYPE_CONTEXT (type))
11437 *nested_name_specifier_p = true;
11438 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11439 class_type))
11440 *nested_name_specifier_p = true;
11441 }
11442 }
11443 /* Indicate whether this class was declared as a `class' or as a
11444 `struct'. */
11445 if (TREE_CODE (type) == RECORD_TYPE)
11446 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11447 cp_parser_check_class_key (class_key, type);
11448
11449 /* Enter the scope containing the class; the names of base classes
11450 should be looked up in that context. For example, given:
11451
11452 struct A { struct B {}; struct C; };
11453 struct A::C : B {};
11454
11455 is valid. */
11456 if (nested_name_specifier)
11457 push_scope (nested_name_specifier);
11458 /* Now, look for the base-clause. */
11459 token = cp_lexer_peek_token (parser->lexer);
11460 if (token->type == CPP_COLON)
11461 {
11462 tree bases;
11463
11464 /* Get the list of base-classes. */
11465 bases = cp_parser_base_clause (parser);
11466 /* Process them. */
11467 xref_basetypes (type, bases);
11468 }
11469 /* Leave the scope given by the nested-name-specifier. We will
11470 enter the class scope itself while processing the members. */
11471 if (nested_name_specifier)
11472 pop_scope (nested_name_specifier);
11473
11474 return type;
11475}
11476
11477/* Parse a class-key.
11478
11479 class-key:
11480 class
11481 struct
11482 union
11483
11484 Returns the kind of class-key specified, or none_type to indicate
11485 error. */
11486
11487static enum tag_types
94edc4ab 11488cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11489{
11490 cp_token *token;
11491 enum tag_types tag_type;
11492
11493 /* Look for the class-key. */
11494 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11495 if (!token)
11496 return none_type;
11497
11498 /* Check to see if the TOKEN is a class-key. */
11499 tag_type = cp_parser_token_is_class_key (token);
11500 if (!tag_type)
11501 cp_parser_error (parser, "expected class-key");
11502 return tag_type;
11503}
11504
11505/* Parse an (optional) member-specification.
11506
11507 member-specification:
11508 member-declaration member-specification [opt]
11509 access-specifier : member-specification [opt] */
11510
11511static void
94edc4ab 11512cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11513{
11514 while (true)
11515 {
11516 cp_token *token;
11517 enum rid keyword;
11518
11519 /* Peek at the next token. */
11520 token = cp_lexer_peek_token (parser->lexer);
11521 /* If it's a `}', or EOF then we've seen all the members. */
11522 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11523 break;
11524
11525 /* See if this token is a keyword. */
11526 keyword = token->keyword;
11527 switch (keyword)
11528 {
11529 case RID_PUBLIC:
11530 case RID_PROTECTED:
11531 case RID_PRIVATE:
11532 /* Consume the access-specifier. */
11533 cp_lexer_consume_token (parser->lexer);
11534 /* Remember which access-specifier is active. */
11535 current_access_specifier = token->value;
11536 /* Look for the `:'. */
11537 cp_parser_require (parser, CPP_COLON, "`:'");
11538 break;
11539
11540 default:
11541 /* Otherwise, the next construction must be a
11542 member-declaration. */
11543 cp_parser_member_declaration (parser);
a723baf1
MM
11544 }
11545 }
11546}
11547
11548/* Parse a member-declaration.
11549
11550 member-declaration:
11551 decl-specifier-seq [opt] member-declarator-list [opt] ;
11552 function-definition ; [opt]
11553 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11554 using-declaration
11555 template-declaration
11556
11557 member-declarator-list:
11558 member-declarator
11559 member-declarator-list , member-declarator
11560
11561 member-declarator:
11562 declarator pure-specifier [opt]
11563 declarator constant-initializer [opt]
11564 identifier [opt] : constant-expression
11565
11566 GNU Extensions:
11567
11568 member-declaration:
11569 __extension__ member-declaration
11570
11571 member-declarator:
11572 declarator attributes [opt] pure-specifier [opt]
11573 declarator attributes [opt] constant-initializer [opt]
11574 identifier [opt] attributes [opt] : constant-expression */
11575
11576static void
94edc4ab 11577cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
11578{
11579 tree decl_specifiers;
11580 tree prefix_attributes;
11581 tree decl;
11582 bool declares_class_or_enum;
11583 bool friend_p;
11584 cp_token *token;
11585 int saved_pedantic;
11586
11587 /* Check for the `__extension__' keyword. */
11588 if (cp_parser_extension_opt (parser, &saved_pedantic))
11589 {
11590 /* Recurse. */
11591 cp_parser_member_declaration (parser);
11592 /* Restore the old value of the PEDANTIC flag. */
11593 pedantic = saved_pedantic;
11594
11595 return;
11596 }
11597
11598 /* Check for a template-declaration. */
11599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11600 {
11601 /* Parse the template-declaration. */
11602 cp_parser_template_declaration (parser, /*member_p=*/true);
11603
11604 return;
11605 }
11606
11607 /* Check for a using-declaration. */
11608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11609 {
11610 /* Parse the using-declaration. */
11611 cp_parser_using_declaration (parser);
11612
11613 return;
11614 }
11615
11616 /* We can't tell whether we're looking at a declaration or a
11617 function-definition. */
11618 cp_parser_parse_tentatively (parser);
11619
11620 /* Parse the decl-specifier-seq. */
11621 decl_specifiers
11622 = cp_parser_decl_specifier_seq (parser,
11623 CP_PARSER_FLAGS_OPTIONAL,
11624 &prefix_attributes,
11625 &declares_class_or_enum);
8fbc5ae7
MM
11626 /* Check for an invalid type-name. */
11627 if (cp_parser_diagnose_invalid_type_name (parser))
11628 return;
a723baf1
MM
11629 /* If there is no declarator, then the decl-specifier-seq should
11630 specify a type. */
11631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11632 {
11633 /* If there was no decl-specifier-seq, and the next token is a
11634 `;', then we have something like:
11635
11636 struct S { ; };
11637
11638 [class.mem]
11639
11640 Each member-declaration shall declare at least one member
11641 name of the class. */
11642 if (!decl_specifiers)
11643 {
11644 if (pedantic)
11645 pedwarn ("extra semicolon");
11646 }
11647 else
11648 {
11649 tree type;
11650
11651 /* See if this declaration is a friend. */
11652 friend_p = cp_parser_friend_p (decl_specifiers);
11653 /* If there were decl-specifiers, check to see if there was
11654 a class-declaration. */
11655 type = check_tag_decl (decl_specifiers);
11656 /* Nested classes have already been added to the class, but
11657 a `friend' needs to be explicitly registered. */
11658 if (friend_p)
11659 {
11660 /* If the `friend' keyword was present, the friend must
11661 be introduced with a class-key. */
11662 if (!declares_class_or_enum)
11663 error ("a class-key must be used when declaring a friend");
11664 /* In this case:
11665
11666 template <typename T> struct A {
11667 friend struct A<T>::B;
11668 };
11669
11670 A<T>::B will be represented by a TYPENAME_TYPE, and
11671 therefore not recognized by check_tag_decl. */
11672 if (!type)
11673 {
11674 tree specifier;
11675
11676 for (specifier = decl_specifiers;
11677 specifier;
11678 specifier = TREE_CHAIN (specifier))
11679 {
11680 tree s = TREE_VALUE (specifier);
11681
11682 if (TREE_CODE (s) == IDENTIFIER_NODE
11683 && IDENTIFIER_GLOBAL_VALUE (s))
11684 type = IDENTIFIER_GLOBAL_VALUE (s);
11685 if (TREE_CODE (s) == TYPE_DECL)
11686 s = TREE_TYPE (s);
11687 if (TYPE_P (s))
11688 {
11689 type = s;
11690 break;
11691 }
11692 }
11693 }
11694 if (!type)
11695 error ("friend declaration does not name a class or "
11696 "function");
11697 else
11698 make_friend_class (current_class_type, type);
11699 }
11700 /* If there is no TYPE, an error message will already have
11701 been issued. */
11702 else if (!type)
11703 ;
11704 /* An anonymous aggregate has to be handled specially; such
11705 a declaration really declares a data member (with a
11706 particular type), as opposed to a nested class. */
11707 else if (ANON_AGGR_TYPE_P (type))
11708 {
11709 /* Remove constructors and such from TYPE, now that we
34cd5ae7 11710 know it is an anonymous aggregate. */
a723baf1
MM
11711 fixup_anonymous_aggr (type);
11712 /* And make the corresponding data member. */
11713 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11714 /* Add it to the class. */
11715 finish_member_declaration (decl);
11716 }
11717 }
11718 }
11719 else
11720 {
11721 /* See if these declarations will be friends. */
11722 friend_p = cp_parser_friend_p (decl_specifiers);
11723
11724 /* Keep going until we hit the `;' at the end of the
11725 declaration. */
11726 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11727 {
11728 tree attributes = NULL_TREE;
11729 tree first_attribute;
11730
11731 /* Peek at the next token. */
11732 token = cp_lexer_peek_token (parser->lexer);
11733
11734 /* Check for a bitfield declaration. */
11735 if (token->type == CPP_COLON
11736 || (token->type == CPP_NAME
11737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11738 == CPP_COLON))
11739 {
11740 tree identifier;
11741 tree width;
11742
11743 /* Get the name of the bitfield. Note that we cannot just
11744 check TOKEN here because it may have been invalidated by
11745 the call to cp_lexer_peek_nth_token above. */
11746 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11747 identifier = cp_parser_identifier (parser);
11748 else
11749 identifier = NULL_TREE;
11750
11751 /* Consume the `:' token. */
11752 cp_lexer_consume_token (parser->lexer);
11753 /* Get the width of the bitfield. */
14d22dd6
MM
11754 width
11755 = cp_parser_constant_expression (parser,
11756 /*allow_non_constant=*/false,
11757 NULL);
a723baf1
MM
11758
11759 /* Look for attributes that apply to the bitfield. */
11760 attributes = cp_parser_attributes_opt (parser);
11761 /* Remember which attributes are prefix attributes and
11762 which are not. */
11763 first_attribute = attributes;
11764 /* Combine the attributes. */
11765 attributes = chainon (prefix_attributes, attributes);
11766
11767 /* Create the bitfield declaration. */
11768 decl = grokbitfield (identifier,
11769 decl_specifiers,
11770 width);
11771 /* Apply the attributes. */
11772 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11773 }
11774 else
11775 {
11776 tree declarator;
11777 tree initializer;
11778 tree asm_specification;
7efa3e22 11779 int ctor_dtor_or_conv_p;
a723baf1
MM
11780
11781 /* Parse the declarator. */
11782 declarator
62b8a44e 11783 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
11784 &ctor_dtor_or_conv_p);
11785
11786 /* If something went wrong parsing the declarator, make sure
11787 that we at least consume some tokens. */
11788 if (declarator == error_mark_node)
11789 {
11790 /* Skip to the end of the statement. */
11791 cp_parser_skip_to_end_of_statement (parser);
11792 break;
11793 }
11794
11795 /* Look for an asm-specification. */
11796 asm_specification = cp_parser_asm_specification_opt (parser);
11797 /* Look for attributes that apply to the declaration. */
11798 attributes = cp_parser_attributes_opt (parser);
11799 /* Remember which attributes are prefix attributes and
11800 which are not. */
11801 first_attribute = attributes;
11802 /* Combine the attributes. */
11803 attributes = chainon (prefix_attributes, attributes);
11804
11805 /* If it's an `=', then we have a constant-initializer or a
11806 pure-specifier. It is not correct to parse the
11807 initializer before registering the member declaration
11808 since the member declaration should be in scope while
11809 its initializer is processed. However, the rest of the
11810 front end does not yet provide an interface that allows
11811 us to handle this correctly. */
11812 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11813 {
11814 /* In [class.mem]:
11815
11816 A pure-specifier shall be used only in the declaration of
11817 a virtual function.
11818
11819 A member-declarator can contain a constant-initializer
11820 only if it declares a static member of integral or
11821 enumeration type.
11822
11823 Therefore, if the DECLARATOR is for a function, we look
11824 for a pure-specifier; otherwise, we look for a
11825 constant-initializer. When we call `grokfield', it will
11826 perform more stringent semantics checks. */
11827 if (TREE_CODE (declarator) == CALL_EXPR)
11828 initializer = cp_parser_pure_specifier (parser);
11829 else
11830 {
11831 /* This declaration cannot be a function
11832 definition. */
11833 cp_parser_commit_to_tentative_parse (parser);
11834 /* Parse the initializer. */
11835 initializer = cp_parser_constant_initializer (parser);
11836 }
11837 }
11838 /* Otherwise, there is no initializer. */
11839 else
11840 initializer = NULL_TREE;
11841
11842 /* See if we are probably looking at a function
11843 definition. We are certainly not looking at at a
11844 member-declarator. Calling `grokfield' has
11845 side-effects, so we must not do it unless we are sure
11846 that we are looking at a member-declarator. */
11847 if (cp_parser_token_starts_function_definition_p
11848 (cp_lexer_peek_token (parser->lexer)))
11849 decl = error_mark_node;
11850 else
39703eb9
MM
11851 {
11852 /* Create the declaration. */
11853 decl = grokfield (declarator,
11854 decl_specifiers,
11855 initializer,
11856 asm_specification,
11857 attributes);
11858 /* Any initialization must have been from a
11859 constant-expression. */
11860 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
11861 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
11862 }
a723baf1
MM
11863 }
11864
11865 /* Reset PREFIX_ATTRIBUTES. */
11866 while (attributes && TREE_CHAIN (attributes) != first_attribute)
11867 attributes = TREE_CHAIN (attributes);
11868 if (attributes)
11869 TREE_CHAIN (attributes) = NULL_TREE;
11870
11871 /* If there is any qualification still in effect, clear it
11872 now; we will be starting fresh with the next declarator. */
11873 parser->scope = NULL_TREE;
11874 parser->qualifying_scope = NULL_TREE;
11875 parser->object_scope = NULL_TREE;
11876 /* If it's a `,', then there are more declarators. */
11877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11878 cp_lexer_consume_token (parser->lexer);
11879 /* If the next token isn't a `;', then we have a parse error. */
11880 else if (cp_lexer_next_token_is_not (parser->lexer,
11881 CPP_SEMICOLON))
11882 {
11883 cp_parser_error (parser, "expected `;'");
11884 /* Skip tokens until we find a `;' */
11885 cp_parser_skip_to_end_of_statement (parser);
11886
11887 break;
11888 }
11889
11890 if (decl)
11891 {
11892 /* Add DECL to the list of members. */
11893 if (!friend_p)
11894 finish_member_declaration (decl);
11895
a723baf1 11896 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 11897 cp_parser_save_default_args (parser, decl);
a723baf1
MM
11898 }
11899 }
11900 }
11901
11902 /* If everything went well, look for the `;'. */
11903 if (cp_parser_parse_definitely (parser))
11904 {
11905 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11906 return;
11907 }
11908
11909 /* Parse the function-definition. */
11910 decl = cp_parser_function_definition (parser, &friend_p);
11911 /* If the member was not a friend, declare it here. */
11912 if (!friend_p)
11913 finish_member_declaration (decl);
11914 /* Peek at the next token. */
11915 token = cp_lexer_peek_token (parser->lexer);
11916 /* If the next token is a semicolon, consume it. */
11917 if (token->type == CPP_SEMICOLON)
11918 cp_lexer_consume_token (parser->lexer);
11919}
11920
11921/* Parse a pure-specifier.
11922
11923 pure-specifier:
11924 = 0
11925
11926 Returns INTEGER_ZERO_NODE if a pure specifier is found.
11927 Otherwiser, ERROR_MARK_NODE is returned. */
11928
11929static tree
94edc4ab 11930cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
11931{
11932 cp_token *token;
11933
11934 /* Look for the `=' token. */
11935 if (!cp_parser_require (parser, CPP_EQ, "`='"))
11936 return error_mark_node;
11937 /* Look for the `0' token. */
11938 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
11939 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
11940 to get information from the lexer about how the number was
11941 spelled in order to fix this problem. */
11942 if (!token || !integer_zerop (token->value))
11943 return error_mark_node;
11944
11945 return integer_zero_node;
11946}
11947
11948/* Parse a constant-initializer.
11949
11950 constant-initializer:
11951 = constant-expression
11952
11953 Returns a representation of the constant-expression. */
11954
11955static tree
94edc4ab 11956cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
11957{
11958 /* Look for the `=' token. */
11959 if (!cp_parser_require (parser, CPP_EQ, "`='"))
11960 return error_mark_node;
11961
11962 /* It is invalid to write:
11963
11964 struct S { static const int i = { 7 }; };
11965
11966 */
11967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11968 {
11969 cp_parser_error (parser,
11970 "a brace-enclosed initializer is not allowed here");
11971 /* Consume the opening brace. */
11972 cp_lexer_consume_token (parser->lexer);
11973 /* Skip the initializer. */
11974 cp_parser_skip_to_closing_brace (parser);
11975 /* Look for the trailing `}'. */
11976 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11977
11978 return error_mark_node;
11979 }
11980
14d22dd6
MM
11981 return cp_parser_constant_expression (parser,
11982 /*allow_non_constant=*/false,
11983 NULL);
a723baf1
MM
11984}
11985
11986/* Derived classes [gram.class.derived] */
11987
11988/* Parse a base-clause.
11989
11990 base-clause:
11991 : base-specifier-list
11992
11993 base-specifier-list:
11994 base-specifier
11995 base-specifier-list , base-specifier
11996
11997 Returns a TREE_LIST representing the base-classes, in the order in
11998 which they were declared. The representation of each node is as
11999 described by cp_parser_base_specifier.
12000
12001 In the case that no bases are specified, this function will return
12002 NULL_TREE, not ERROR_MARK_NODE. */
12003
12004static tree
94edc4ab 12005cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12006{
12007 tree bases = NULL_TREE;
12008
12009 /* Look for the `:' that begins the list. */
12010 cp_parser_require (parser, CPP_COLON, "`:'");
12011
12012 /* Scan the base-specifier-list. */
12013 while (true)
12014 {
12015 cp_token *token;
12016 tree base;
12017
12018 /* Look for the base-specifier. */
12019 base = cp_parser_base_specifier (parser);
12020 /* Add BASE to the front of the list. */
12021 if (base != error_mark_node)
12022 {
12023 TREE_CHAIN (base) = bases;
12024 bases = base;
12025 }
12026 /* Peek at the next token. */
12027 token = cp_lexer_peek_token (parser->lexer);
12028 /* If it's not a comma, then the list is complete. */
12029 if (token->type != CPP_COMMA)
12030 break;
12031 /* Consume the `,'. */
12032 cp_lexer_consume_token (parser->lexer);
12033 }
12034
12035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12036 base class had a qualified name. However, the next name that
12037 appears is certainly not qualified. */
12038 parser->scope = NULL_TREE;
12039 parser->qualifying_scope = NULL_TREE;
12040 parser->object_scope = NULL_TREE;
12041
12042 return nreverse (bases);
12043}
12044
12045/* Parse a base-specifier.
12046
12047 base-specifier:
12048 :: [opt] nested-name-specifier [opt] class-name
12049 virtual access-specifier [opt] :: [opt] nested-name-specifier
12050 [opt] class-name
12051 access-specifier virtual [opt] :: [opt] nested-name-specifier
12052 [opt] class-name
12053
12054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12058
12059static tree
94edc4ab 12060cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12061{
12062 cp_token *token;
12063 bool done = false;
12064 bool virtual_p = false;
12065 bool duplicate_virtual_error_issued_p = false;
12066 bool duplicate_access_error_issued_p = false;
bbaab916 12067 bool class_scope_p, template_p;
dbbf88d1 12068 tree access = access_default_node;
a723baf1
MM
12069 tree type;
12070
12071 /* Process the optional `virtual' and `access-specifier'. */
12072 while (!done)
12073 {
12074 /* Peek at the next token. */
12075 token = cp_lexer_peek_token (parser->lexer);
12076 /* Process `virtual'. */
12077 switch (token->keyword)
12078 {
12079 case RID_VIRTUAL:
12080 /* If `virtual' appears more than once, issue an error. */
12081 if (virtual_p && !duplicate_virtual_error_issued_p)
12082 {
12083 cp_parser_error (parser,
12084 "`virtual' specified more than once in base-specified");
12085 duplicate_virtual_error_issued_p = true;
12086 }
12087
12088 virtual_p = true;
12089
12090 /* Consume the `virtual' token. */
12091 cp_lexer_consume_token (parser->lexer);
12092
12093 break;
12094
12095 case RID_PUBLIC:
12096 case RID_PROTECTED:
12097 case RID_PRIVATE:
12098 /* If more than one access specifier appears, issue an
12099 error. */
dbbf88d1
NS
12100 if (access != access_default_node
12101 && !duplicate_access_error_issued_p)
a723baf1
MM
12102 {
12103 cp_parser_error (parser,
12104 "more than one access specifier in base-specified");
12105 duplicate_access_error_issued_p = true;
12106 }
12107
dbbf88d1 12108 access = ridpointers[(int) token->keyword];
a723baf1
MM
12109
12110 /* Consume the access-specifier. */
12111 cp_lexer_consume_token (parser->lexer);
12112
12113 break;
12114
12115 default:
12116 done = true;
12117 break;
12118 }
12119 }
12120
a723baf1
MM
12121 /* Look for the optional `::' operator. */
12122 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12123 /* Look for the nested-name-specifier. The simplest way to
12124 implement:
12125
12126 [temp.res]
12127
12128 The keyword `typename' is not permitted in a base-specifier or
12129 mem-initializer; in these contexts a qualified name that
12130 depends on a template-parameter is implicitly assumed to be a
12131 type name.
12132
12133 is to pretend that we have seen the `typename' keyword at this
12134 point. */
12135 cp_parser_nested_name_specifier_opt (parser,
12136 /*typename_keyword_p=*/true,
12137 /*check_dependency_p=*/true,
12138 /*type_p=*/true);
12139 /* If the base class is given by a qualified name, assume that names
12140 we see are type names or templates, as appropriate. */
12141 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12142 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12143
a723baf1
MM
12144 /* Finally, look for the class-name. */
12145 type = cp_parser_class_name (parser,
12146 class_scope_p,
bbaab916 12147 template_p,
a723baf1 12148 /*type_p=*/true,
a723baf1
MM
12149 /*check_dependency_p=*/true,
12150 /*class_head_p=*/false);
12151
12152 if (type == error_mark_node)
12153 return error_mark_node;
12154
dbbf88d1 12155 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12156}
12157
12158/* Exception handling [gram.exception] */
12159
12160/* Parse an (optional) exception-specification.
12161
12162 exception-specification:
12163 throw ( type-id-list [opt] )
12164
12165 Returns a TREE_LIST representing the exception-specification. The
12166 TREE_VALUE of each node is a type. */
12167
12168static tree
94edc4ab 12169cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12170{
12171 cp_token *token;
12172 tree type_id_list;
12173
12174 /* Peek at the next token. */
12175 token = cp_lexer_peek_token (parser->lexer);
12176 /* If it's not `throw', then there's no exception-specification. */
12177 if (!cp_parser_is_keyword (token, RID_THROW))
12178 return NULL_TREE;
12179
12180 /* Consume the `throw'. */
12181 cp_lexer_consume_token (parser->lexer);
12182
12183 /* Look for the `('. */
12184 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12185
12186 /* Peek at the next token. */
12187 token = cp_lexer_peek_token (parser->lexer);
12188 /* If it's not a `)', then there is a type-id-list. */
12189 if (token->type != CPP_CLOSE_PAREN)
12190 {
12191 const char *saved_message;
12192
12193 /* Types may not be defined in an exception-specification. */
12194 saved_message = parser->type_definition_forbidden_message;
12195 parser->type_definition_forbidden_message
12196 = "types may not be defined in an exception-specification";
12197 /* Parse the type-id-list. */
12198 type_id_list = cp_parser_type_id_list (parser);
12199 /* Restore the saved message. */
12200 parser->type_definition_forbidden_message = saved_message;
12201 }
12202 else
12203 type_id_list = empty_except_spec;
12204
12205 /* Look for the `)'. */
12206 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12207
12208 return type_id_list;
12209}
12210
12211/* Parse an (optional) type-id-list.
12212
12213 type-id-list:
12214 type-id
12215 type-id-list , type-id
12216
12217 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12218 in the order that the types were presented. */
12219
12220static tree
94edc4ab 12221cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12222{
12223 tree types = NULL_TREE;
12224
12225 while (true)
12226 {
12227 cp_token *token;
12228 tree type;
12229
12230 /* Get the next type-id. */
12231 type = cp_parser_type_id (parser);
12232 /* Add it to the list. */
12233 types = add_exception_specifier (types, type, /*complain=*/1);
12234 /* Peek at the next token. */
12235 token = cp_lexer_peek_token (parser->lexer);
12236 /* If it is not a `,', we are done. */
12237 if (token->type != CPP_COMMA)
12238 break;
12239 /* Consume the `,'. */
12240 cp_lexer_consume_token (parser->lexer);
12241 }
12242
12243 return nreverse (types);
12244}
12245
12246/* Parse a try-block.
12247
12248 try-block:
12249 try compound-statement handler-seq */
12250
12251static tree
94edc4ab 12252cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12253{
12254 tree try_block;
12255
12256 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12257 try_block = begin_try_block ();
a5bcc582 12258 cp_parser_compound_statement (parser, false);
a723baf1
MM
12259 finish_try_block (try_block);
12260 cp_parser_handler_seq (parser);
12261 finish_handler_sequence (try_block);
12262
12263 return try_block;
12264}
12265
12266/* Parse a function-try-block.
12267
12268 function-try-block:
12269 try ctor-initializer [opt] function-body handler-seq */
12270
12271static bool
94edc4ab 12272cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12273{
12274 tree try_block;
12275 bool ctor_initializer_p;
12276
12277 /* Look for the `try' keyword. */
12278 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12279 return false;
12280 /* Let the rest of the front-end know where we are. */
12281 try_block = begin_function_try_block ();
12282 /* Parse the function-body. */
12283 ctor_initializer_p
12284 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12285 /* We're done with the `try' part. */
12286 finish_function_try_block (try_block);
12287 /* Parse the handlers. */
12288 cp_parser_handler_seq (parser);
12289 /* We're done with the handlers. */
12290 finish_function_handler_sequence (try_block);
12291
12292 return ctor_initializer_p;
12293}
12294
12295/* Parse a handler-seq.
12296
12297 handler-seq:
12298 handler handler-seq [opt] */
12299
12300static void
94edc4ab 12301cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12302{
12303 while (true)
12304 {
12305 cp_token *token;
12306
12307 /* Parse the handler. */
12308 cp_parser_handler (parser);
12309 /* Peek at the next token. */
12310 token = cp_lexer_peek_token (parser->lexer);
12311 /* If it's not `catch' then there are no more handlers. */
12312 if (!cp_parser_is_keyword (token, RID_CATCH))
12313 break;
12314 }
12315}
12316
12317/* Parse a handler.
12318
12319 handler:
12320 catch ( exception-declaration ) compound-statement */
12321
12322static void
94edc4ab 12323cp_parser_handler (cp_parser* parser)
a723baf1
MM
12324{
12325 tree handler;
12326 tree declaration;
12327
12328 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12329 handler = begin_handler ();
12330 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12331 declaration = cp_parser_exception_declaration (parser);
12332 finish_handler_parms (declaration, handler);
12333 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 12334 cp_parser_compound_statement (parser, false);
a723baf1
MM
12335 finish_handler (handler);
12336}
12337
12338/* Parse an exception-declaration.
12339
12340 exception-declaration:
12341 type-specifier-seq declarator
12342 type-specifier-seq abstract-declarator
12343 type-specifier-seq
12344 ...
12345
12346 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12347 ellipsis variant is used. */
12348
12349static tree
94edc4ab 12350cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12351{
12352 tree type_specifiers;
12353 tree declarator;
12354 const char *saved_message;
12355
12356 /* If it's an ellipsis, it's easy to handle. */
12357 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12358 {
12359 /* Consume the `...' token. */
12360 cp_lexer_consume_token (parser->lexer);
12361 return NULL_TREE;
12362 }
12363
12364 /* Types may not be defined in exception-declarations. */
12365 saved_message = parser->type_definition_forbidden_message;
12366 parser->type_definition_forbidden_message
12367 = "types may not be defined in exception-declarations";
12368
12369 /* Parse the type-specifier-seq. */
12370 type_specifiers = cp_parser_type_specifier_seq (parser);
12371 /* If it's a `)', then there is no declarator. */
12372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12373 declarator = NULL_TREE;
12374 else
62b8a44e
NS
12375 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12376 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
12377
12378 /* Restore the saved message. */
12379 parser->type_definition_forbidden_message = saved_message;
12380
12381 return start_handler_parms (type_specifiers, declarator);
12382}
12383
12384/* Parse a throw-expression.
12385
12386 throw-expression:
34cd5ae7 12387 throw assignment-expression [opt]
a723baf1
MM
12388
12389 Returns a THROW_EXPR representing the throw-expression. */
12390
12391static tree
94edc4ab 12392cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12393{
12394 tree expression;
12395
12396 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12397 /* We can't be sure if there is an assignment-expression or not. */
12398 cp_parser_parse_tentatively (parser);
12399 /* Try it. */
12400 expression = cp_parser_assignment_expression (parser);
12401 /* If it didn't work, this is just a rethrow. */
12402 if (!cp_parser_parse_definitely (parser))
12403 expression = NULL_TREE;
12404
12405 return build_throw (expression);
12406}
12407
12408/* GNU Extensions */
12409
12410/* Parse an (optional) asm-specification.
12411
12412 asm-specification:
12413 asm ( string-literal )
12414
12415 If the asm-specification is present, returns a STRING_CST
12416 corresponding to the string-literal. Otherwise, returns
12417 NULL_TREE. */
12418
12419static tree
94edc4ab 12420cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12421{
12422 cp_token *token;
12423 tree asm_specification;
12424
12425 /* Peek at the next token. */
12426 token = cp_lexer_peek_token (parser->lexer);
12427 /* If the next token isn't the `asm' keyword, then there's no
12428 asm-specification. */
12429 if (!cp_parser_is_keyword (token, RID_ASM))
12430 return NULL_TREE;
12431
12432 /* Consume the `asm' token. */
12433 cp_lexer_consume_token (parser->lexer);
12434 /* Look for the `('. */
12435 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12436
12437 /* Look for the string-literal. */
12438 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12439 if (token)
12440 asm_specification = token->value;
12441 else
12442 asm_specification = NULL_TREE;
12443
12444 /* Look for the `)'. */
12445 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12446
12447 return asm_specification;
12448}
12449
12450/* Parse an asm-operand-list.
12451
12452 asm-operand-list:
12453 asm-operand
12454 asm-operand-list , asm-operand
12455
12456 asm-operand:
12457 string-literal ( expression )
12458 [ string-literal ] string-literal ( expression )
12459
12460 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12461 each node is the expression. The TREE_PURPOSE is itself a
12462 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12463 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12464 is a STRING_CST for the string literal before the parenthesis. */
12465
12466static tree
94edc4ab 12467cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12468{
12469 tree asm_operands = NULL_TREE;
12470
12471 while (true)
12472 {
12473 tree string_literal;
12474 tree expression;
12475 tree name;
12476 cp_token *token;
12477
12478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12479 {
12480 /* Consume the `[' token. */
12481 cp_lexer_consume_token (parser->lexer);
12482 /* Read the operand name. */
12483 name = cp_parser_identifier (parser);
12484 if (name != error_mark_node)
12485 name = build_string (IDENTIFIER_LENGTH (name),
12486 IDENTIFIER_POINTER (name));
12487 /* Look for the closing `]'. */
12488 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12489 }
12490 else
12491 name = NULL_TREE;
12492 /* Look for the string-literal. */
12493 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12494 string_literal = token ? token->value : error_mark_node;
12495 /* Look for the `('. */
12496 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12497 /* Parse the expression. */
12498 expression = cp_parser_expression (parser);
12499 /* Look for the `)'. */
12500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12501 /* Add this operand to the list. */
12502 asm_operands = tree_cons (build_tree_list (name, string_literal),
12503 expression,
12504 asm_operands);
12505 /* If the next token is not a `,', there are no more
12506 operands. */
12507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12508 break;
12509 /* Consume the `,'. */
12510 cp_lexer_consume_token (parser->lexer);
12511 }
12512
12513 return nreverse (asm_operands);
12514}
12515
12516/* Parse an asm-clobber-list.
12517
12518 asm-clobber-list:
12519 string-literal
12520 asm-clobber-list , string-literal
12521
12522 Returns a TREE_LIST, indicating the clobbers in the order that they
12523 appeared. The TREE_VALUE of each node is a STRING_CST. */
12524
12525static tree
94edc4ab 12526cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
12527{
12528 tree clobbers = NULL_TREE;
12529
12530 while (true)
12531 {
12532 cp_token *token;
12533 tree string_literal;
12534
12535 /* Look for the string literal. */
12536 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12537 string_literal = token ? token->value : error_mark_node;
12538 /* Add it to the list. */
12539 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12540 /* If the next token is not a `,', then the list is
12541 complete. */
12542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12543 break;
12544 /* Consume the `,' token. */
12545 cp_lexer_consume_token (parser->lexer);
12546 }
12547
12548 return clobbers;
12549}
12550
12551/* Parse an (optional) series of attributes.
12552
12553 attributes:
12554 attributes attribute
12555
12556 attribute:
12557 __attribute__ (( attribute-list [opt] ))
12558
12559 The return value is as for cp_parser_attribute_list. */
12560
12561static tree
94edc4ab 12562cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
12563{
12564 tree attributes = NULL_TREE;
12565
12566 while (true)
12567 {
12568 cp_token *token;
12569 tree attribute_list;
12570
12571 /* Peek at the next token. */
12572 token = cp_lexer_peek_token (parser->lexer);
12573 /* If it's not `__attribute__', then we're done. */
12574 if (token->keyword != RID_ATTRIBUTE)
12575 break;
12576
12577 /* Consume the `__attribute__' keyword. */
12578 cp_lexer_consume_token (parser->lexer);
12579 /* Look for the two `(' tokens. */
12580 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12581 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12582
12583 /* Peek at the next token. */
12584 token = cp_lexer_peek_token (parser->lexer);
12585 if (token->type != CPP_CLOSE_PAREN)
12586 /* Parse the attribute-list. */
12587 attribute_list = cp_parser_attribute_list (parser);
12588 else
12589 /* If the next token is a `)', then there is no attribute
12590 list. */
12591 attribute_list = NULL;
12592
12593 /* Look for the two `)' tokens. */
12594 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12596
12597 /* Add these new attributes to the list. */
12598 attributes = chainon (attributes, attribute_list);
12599 }
12600
12601 return attributes;
12602}
12603
12604/* Parse an attribute-list.
12605
12606 attribute-list:
12607 attribute
12608 attribute-list , attribute
12609
12610 attribute:
12611 identifier
12612 identifier ( identifier )
12613 identifier ( identifier , expression-list )
12614 identifier ( expression-list )
12615
12616 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12617 TREE_PURPOSE of each node is the identifier indicating which
12618 attribute is in use. The TREE_VALUE represents the arguments, if
12619 any. */
12620
12621static tree
94edc4ab 12622cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
12623{
12624 tree attribute_list = NULL_TREE;
12625
12626 while (true)
12627 {
12628 cp_token *token;
12629 tree identifier;
12630 tree attribute;
12631
12632 /* Look for the identifier. We also allow keywords here; for
12633 example `__attribute__ ((const))' is legal. */
12634 token = cp_lexer_peek_token (parser->lexer);
12635 if (token->type != CPP_NAME
12636 && token->type != CPP_KEYWORD)
12637 return error_mark_node;
12638 /* Consume the token. */
12639 token = cp_lexer_consume_token (parser->lexer);
12640
12641 /* Save away the identifier that indicates which attribute this is. */
12642 identifier = token->value;
12643 attribute = build_tree_list (identifier, NULL_TREE);
12644
12645 /* Peek at the next token. */
12646 token = cp_lexer_peek_token (parser->lexer);
12647 /* If it's an `(', then parse the attribute arguments. */
12648 if (token->type == CPP_OPEN_PAREN)
12649 {
12650 tree arguments;
a723baf1 12651
39703eb9
MM
12652 arguments = (cp_parser_parenthesized_expression_list
12653 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
12654 /* Save the identifier and arguments away. */
12655 TREE_VALUE (attribute) = arguments;
a723baf1
MM
12656 }
12657
12658 /* Add this attribute to the list. */
12659 TREE_CHAIN (attribute) = attribute_list;
12660 attribute_list = attribute;
12661
12662 /* Now, look for more attributes. */
12663 token = cp_lexer_peek_token (parser->lexer);
12664 /* If the next token isn't a `,', we're done. */
12665 if (token->type != CPP_COMMA)
12666 break;
12667
12668 /* Consume the commma and keep going. */
12669 cp_lexer_consume_token (parser->lexer);
12670 }
12671
12672 /* We built up the list in reverse order. */
12673 return nreverse (attribute_list);
12674}
12675
12676/* Parse an optional `__extension__' keyword. Returns TRUE if it is
12677 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12678 current value of the PEDANTIC flag, regardless of whether or not
12679 the `__extension__' keyword is present. The caller is responsible
12680 for restoring the value of the PEDANTIC flag. */
12681
12682static bool
94edc4ab 12683cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
12684{
12685 /* Save the old value of the PEDANTIC flag. */
12686 *saved_pedantic = pedantic;
12687
12688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12689 {
12690 /* Consume the `__extension__' token. */
12691 cp_lexer_consume_token (parser->lexer);
12692 /* We're not being pedantic while the `__extension__' keyword is
12693 in effect. */
12694 pedantic = 0;
12695
12696 return true;
12697 }
12698
12699 return false;
12700}
12701
12702/* Parse a label declaration.
12703
12704 label-declaration:
12705 __label__ label-declarator-seq ;
12706
12707 label-declarator-seq:
12708 identifier , label-declarator-seq
12709 identifier */
12710
12711static void
94edc4ab 12712cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
12713{
12714 /* Look for the `__label__' keyword. */
12715 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12716
12717 while (true)
12718 {
12719 tree identifier;
12720
12721 /* Look for an identifier. */
12722 identifier = cp_parser_identifier (parser);
12723 /* Declare it as a lobel. */
12724 finish_label_decl (identifier);
12725 /* If the next token is a `;', stop. */
12726 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12727 break;
12728 /* Look for the `,' separating the label declarations. */
12729 cp_parser_require (parser, CPP_COMMA, "`,'");
12730 }
12731
12732 /* Look for the final `;'. */
12733 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12734}
12735
12736/* Support Functions */
12737
12738/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12739 NAME should have one of the representations used for an
12740 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12741 is returned. If PARSER->SCOPE is a dependent type, then a
12742 SCOPE_REF is returned.
12743
12744 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12745 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12746 was formed. Abstractly, such entities should not be passed to this
12747 function, because they do not need to be looked up, but it is
12748 simpler to check for this special case here, rather than at the
12749 call-sites.
12750
12751 In cases not explicitly covered above, this function returns a
12752 DECL, OVERLOAD, or baselink representing the result of the lookup.
12753 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12754 is returned.
12755
a723baf1
MM
12756 If IS_TYPE is TRUE, bindings that do not refer to types are
12757 ignored.
12758
eea9800f
MM
12759 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12760 are ignored.
12761
a723baf1
MM
12762 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12763 types. */
12764
12765static tree
8d241e0b 12766cp_parser_lookup_name (cp_parser *parser, tree name,
eea9800f 12767 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
12768{
12769 tree decl;
12770 tree object_type = parser->context->object_type;
12771
12772 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12773 no longer valid. Note that if we are parsing tentatively, and
12774 the parse fails, OBJECT_TYPE will be automatically restored. */
12775 parser->context->object_type = NULL_TREE;
12776
12777 if (name == error_mark_node)
12778 return error_mark_node;
12779
12780 /* A template-id has already been resolved; there is no lookup to
12781 do. */
12782 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12783 return name;
12784 if (BASELINK_P (name))
12785 {
12786 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12787 == TEMPLATE_ID_EXPR),
12788 20020909);
12789 return name;
12790 }
12791
12792 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12793 it should already have been checked to make sure that the name
12794 used matches the type being destroyed. */
12795 if (TREE_CODE (name) == BIT_NOT_EXPR)
12796 {
12797 tree type;
12798
12799 /* Figure out to which type this destructor applies. */
12800 if (parser->scope)
12801 type = parser->scope;
12802 else if (object_type)
12803 type = object_type;
12804 else
12805 type = current_class_type;
12806 /* If that's not a class type, there is no destructor. */
12807 if (!type || !CLASS_TYPE_P (type))
12808 return error_mark_node;
12809 /* If it was a class type, return the destructor. */
12810 return CLASSTYPE_DESTRUCTORS (type);
12811 }
12812
12813 /* By this point, the NAME should be an ordinary identifier. If
12814 the id-expression was a qualified name, the qualifying scope is
12815 stored in PARSER->SCOPE at this point. */
12816 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12817 20000619);
12818
12819 /* Perform the lookup. */
12820 if (parser->scope)
12821 {
1fb3244a 12822 bool dependent_p;
a723baf1
MM
12823
12824 if (parser->scope == error_mark_node)
12825 return error_mark_node;
12826
12827 /* If the SCOPE is dependent, the lookup must be deferred until
12828 the template is instantiated -- unless we are explicitly
12829 looking up names in uninstantiated templates. Even then, we
12830 cannot look up the name if the scope is not a class type; it
12831 might, for example, be a template type parameter. */
1fb3244a
MM
12832 dependent_p = (TYPE_P (parser->scope)
12833 && !(parser->in_declarator_p
12834 && currently_open_class (parser->scope))
12835 && dependent_type_p (parser->scope));
a723baf1 12836 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 12837 && dependent_p)
a723baf1
MM
12838 {
12839 if (!is_type)
12840 decl = build_nt (SCOPE_REF, parser->scope, name);
12841 else
12842 /* The resolution to Core Issue 180 says that `struct A::B'
12843 should be considered a type-name, even if `A' is
12844 dependent. */
12845 decl = TYPE_NAME (make_typename_type (parser->scope,
12846 name,
12847 /*complain=*/1));
12848 }
12849 else
12850 {
12851 /* If PARSER->SCOPE is a dependent type, then it must be a
12852 class type, and we must not be checking dependencies;
12853 otherwise, we would have processed this lookup above. So
12854 that PARSER->SCOPE is not considered a dependent base by
12855 lookup_member, we must enter the scope here. */
1fb3244a 12856 if (dependent_p)
a723baf1
MM
12857 push_scope (parser->scope);
12858 /* If the PARSER->SCOPE is a a template specialization, it
12859 may be instantiated during name lookup. In that case,
12860 errors may be issued. Even if we rollback the current
12861 tentative parse, those errors are valid. */
5e08432e
MM
12862 decl = lookup_qualified_name (parser->scope, name, is_type,
12863 /*complain=*/true);
1fb3244a 12864 if (dependent_p)
a723baf1
MM
12865 pop_scope (parser->scope);
12866 }
12867 parser->qualifying_scope = parser->scope;
12868 parser->object_scope = NULL_TREE;
12869 }
12870 else if (object_type)
12871 {
12872 tree object_decl = NULL_TREE;
12873 /* Look up the name in the scope of the OBJECT_TYPE, unless the
12874 OBJECT_TYPE is not a class. */
12875 if (CLASS_TYPE_P (object_type))
12876 /* If the OBJECT_TYPE is a template specialization, it may
12877 be instantiated during name lookup. In that case, errors
12878 may be issued. Even if we rollback the current tentative
12879 parse, those errors are valid. */
12880 object_decl = lookup_member (object_type,
12881 name,
12882 /*protect=*/0, is_type);
12883 /* Look it up in the enclosing context, too. */
12884 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 12885 is_namespace,
a723baf1
MM
12886 /*flags=*/0);
12887 parser->object_scope = object_type;
12888 parser->qualifying_scope = NULL_TREE;
12889 if (object_decl)
12890 decl = object_decl;
12891 }
12892 else
12893 {
12894 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 12895 is_namespace,
a723baf1
MM
12896 /*flags=*/0);
12897 parser->qualifying_scope = NULL_TREE;
12898 parser->object_scope = NULL_TREE;
12899 }
12900
12901 /* If the lookup failed, let our caller know. */
12902 if (!decl
12903 || decl == error_mark_node
12904 || (TREE_CODE (decl) == FUNCTION_DECL
12905 && DECL_ANTICIPATED (decl)))
12906 return error_mark_node;
12907
12908 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
12909 if (TREE_CODE (decl) == TREE_LIST)
12910 {
12911 /* The error message we have to print is too complicated for
12912 cp_parser_error, so we incorporate its actions directly. */
e5976695 12913 if (!cp_parser_simulate_error (parser))
a723baf1
MM
12914 {
12915 error ("reference to `%D' is ambiguous", name);
12916 print_candidates (decl);
12917 }
12918 return error_mark_node;
12919 }
12920
12921 my_friendly_assert (DECL_P (decl)
12922 || TREE_CODE (decl) == OVERLOAD
12923 || TREE_CODE (decl) == SCOPE_REF
12924 || BASELINK_P (decl),
12925 20000619);
12926
12927 /* If we have resolved the name of a member declaration, check to
12928 see if the declaration is accessible. When the name resolves to
34cd5ae7 12929 set of overloaded functions, accessibility is checked when
a723baf1
MM
12930 overload resolution is done.
12931
12932 During an explicit instantiation, access is not checked at all,
12933 as per [temp.explicit]. */
8d241e0b 12934 if (DECL_P (decl))
ee76b931 12935 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
12936
12937 return decl;
12938}
12939
12940/* Like cp_parser_lookup_name, but for use in the typical case where
12941 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
12942 TRUE. */
12943
12944static tree
94edc4ab 12945cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
12946{
12947 return cp_parser_lookup_name (parser, name,
eea9800f
MM
12948 /*is_type=*/false,
12949 /*is_namespace=*/false,
a723baf1
MM
12950 /*check_dependency=*/true);
12951}
12952
a723baf1
MM
12953/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
12954 the current context, return the TYPE_DECL. If TAG_NAME_P is
12955 true, the DECL indicates the class being defined in a class-head,
12956 or declared in an elaborated-type-specifier.
12957
12958 Otherwise, return DECL. */
12959
12960static tree
12961cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
12962{
710b73e6
KL
12963 /* If the TEMPLATE_DECL is being declared as part of a class-head,
12964 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
12965
12966 struct A {
12967 template <typename T> struct B;
12968 };
12969
12970 template <typename T> struct A::B {};
12971
12972 Similarly, in a elaborated-type-specifier:
12973
12974 namespace N { struct X{}; }
12975
12976 struct A {
12977 template <typename T> friend struct N::X;
12978 };
12979
710b73e6
KL
12980 However, if the DECL refers to a class type, and we are in
12981 the scope of the class, then the name lookup automatically
12982 finds the TYPE_DECL created by build_self_reference rather
12983 than a TEMPLATE_DECL. For example, in:
12984
12985 template <class T> struct S {
12986 S s;
12987 };
12988
12989 there is no need to handle such case. */
12990
12991 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
12992 return DECL_TEMPLATE_RESULT (decl);
12993
12994 return decl;
12995}
12996
12997/* If too many, or too few, template-parameter lists apply to the
12998 declarator, issue an error message. Returns TRUE if all went well,
12999 and FALSE otherwise. */
13000
13001static bool
94edc4ab
NN
13002cp_parser_check_declarator_template_parameters (cp_parser* parser,
13003 tree declarator)
a723baf1
MM
13004{
13005 unsigned num_templates;
13006
13007 /* We haven't seen any classes that involve template parameters yet. */
13008 num_templates = 0;
13009
13010 switch (TREE_CODE (declarator))
13011 {
13012 case CALL_EXPR:
13013 case ARRAY_REF:
13014 case INDIRECT_REF:
13015 case ADDR_EXPR:
13016 {
13017 tree main_declarator = TREE_OPERAND (declarator, 0);
13018 return
13019 cp_parser_check_declarator_template_parameters (parser,
13020 main_declarator);
13021 }
13022
13023 case SCOPE_REF:
13024 {
13025 tree scope;
13026 tree member;
13027
13028 scope = TREE_OPERAND (declarator, 0);
13029 member = TREE_OPERAND (declarator, 1);
13030
13031 /* If this is a pointer-to-member, then we are not interested
13032 in the SCOPE, because it does not qualify the thing that is
13033 being declared. */
13034 if (TREE_CODE (member) == INDIRECT_REF)
13035 return (cp_parser_check_declarator_template_parameters
13036 (parser, member));
13037
13038 while (scope && CLASS_TYPE_P (scope))
13039 {
13040 /* You're supposed to have one `template <...>'
13041 for every template class, but you don't need one
13042 for a full specialization. For example:
13043
13044 template <class T> struct S{};
13045 template <> struct S<int> { void f(); };
13046 void S<int>::f () {}
13047
13048 is correct; there shouldn't be a `template <>' for
13049 the definition of `S<int>::f'. */
13050 if (CLASSTYPE_TEMPLATE_INFO (scope)
13051 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13052 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13053 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13054 ++num_templates;
13055
13056 scope = TYPE_CONTEXT (scope);
13057 }
13058 }
13059
13060 /* Fall through. */
13061
13062 default:
13063 /* If the DECLARATOR has the form `X<y>' then it uses one
13064 additional level of template parameters. */
13065 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13066 ++num_templates;
13067
13068 return cp_parser_check_template_parameters (parser,
13069 num_templates);
13070 }
13071}
13072
13073/* NUM_TEMPLATES were used in the current declaration. If that is
13074 invalid, return FALSE and issue an error messages. Otherwise,
13075 return TRUE. */
13076
13077static bool
94edc4ab
NN
13078cp_parser_check_template_parameters (cp_parser* parser,
13079 unsigned num_templates)
a723baf1
MM
13080{
13081 /* If there are more template classes than parameter lists, we have
13082 something like:
13083
13084 template <class T> void S<T>::R<T>::f (); */
13085 if (parser->num_template_parameter_lists < num_templates)
13086 {
13087 error ("too few template-parameter-lists");
13088 return false;
13089 }
13090 /* If there are the same number of template classes and parameter
13091 lists, that's OK. */
13092 if (parser->num_template_parameter_lists == num_templates)
13093 return true;
13094 /* If there are more, but only one more, then we are referring to a
13095 member template. That's OK too. */
13096 if (parser->num_template_parameter_lists == num_templates + 1)
13097 return true;
13098 /* Otherwise, there are too many template parameter lists. We have
13099 something like:
13100
13101 template <class T> template <class U> void S::f(); */
13102 error ("too many template-parameter-lists");
13103 return false;
13104}
13105
13106/* Parse a binary-expression of the general form:
13107
13108 binary-expression:
13109 <expr>
13110 binary-expression <token> <expr>
13111
13112 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13113 to parser the <expr>s. If the first production is used, then the
13114 value returned by FN is returned directly. Otherwise, a node with
13115 the indicated EXPR_TYPE is returned, with operands corresponding to
13116 the two sub-expressions. */
13117
13118static tree
94edc4ab
NN
13119cp_parser_binary_expression (cp_parser* parser,
13120 const cp_parser_token_tree_map token_tree_map,
13121 cp_parser_expression_fn fn)
a723baf1
MM
13122{
13123 tree lhs;
13124
13125 /* Parse the first expression. */
13126 lhs = (*fn) (parser);
13127 /* Now, look for more expressions. */
13128 while (true)
13129 {
13130 cp_token *token;
39b1af70 13131 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13132 tree rhs;
13133
13134 /* Peek at the next token. */
13135 token = cp_lexer_peek_token (parser->lexer);
13136 /* If the token is `>', and that's not an operator at the
13137 moment, then we're done. */
13138 if (token->type == CPP_GREATER
13139 && !parser->greater_than_is_operator_p)
13140 break;
34cd5ae7 13141 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13142 tree representation. */
13143 for (map_node = token_tree_map;
13144 map_node->token_type != CPP_EOF;
13145 ++map_node)
13146 if (map_node->token_type == token->type)
13147 {
13148 /* Consume the operator token. */
13149 cp_lexer_consume_token (parser->lexer);
13150 /* Parse the right-hand side of the expression. */
13151 rhs = (*fn) (parser);
13152 /* Build the binary tree node. */
13153 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13154 break;
13155 }
13156
13157 /* If the token wasn't one of the ones we want, we're done. */
13158 if (map_node->token_type == CPP_EOF)
13159 break;
13160 }
13161
13162 return lhs;
13163}
13164
13165/* Parse an optional `::' token indicating that the following name is
13166 from the global namespace. If so, PARSER->SCOPE is set to the
13167 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13168 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13169 Returns the new value of PARSER->SCOPE, if the `::' token is
13170 present, and NULL_TREE otherwise. */
13171
13172static tree
94edc4ab 13173cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13174{
13175 cp_token *token;
13176
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If we're looking at a `::' token then we're starting from the
13180 global namespace, not our current location. */
13181 if (token->type == CPP_SCOPE)
13182 {
13183 /* Consume the `::' token. */
13184 cp_lexer_consume_token (parser->lexer);
13185 /* Set the SCOPE so that we know where to start the lookup. */
13186 parser->scope = global_namespace;
13187 parser->qualifying_scope = global_namespace;
13188 parser->object_scope = NULL_TREE;
13189
13190 return parser->scope;
13191 }
13192 else if (!current_scope_valid_p)
13193 {
13194 parser->scope = NULL_TREE;
13195 parser->qualifying_scope = NULL_TREE;
13196 parser->object_scope = NULL_TREE;
13197 }
13198
13199 return NULL_TREE;
13200}
13201
13202/* Returns TRUE if the upcoming token sequence is the start of a
13203 constructor declarator. If FRIEND_P is true, the declarator is
13204 preceded by the `friend' specifier. */
13205
13206static bool
13207cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13208{
13209 bool constructor_p;
13210 tree type_decl = NULL_TREE;
13211 bool nested_name_p;
2050a1bb
MM
13212 cp_token *next_token;
13213
13214 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13215 try to avoid doing lots of work if at all possible. It's not
13216 valid declare a constructor at function scope. */
13217 if (at_function_scope_p ())
13218 return false;
13219 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13220 next_token = cp_lexer_peek_token (parser->lexer);
13221 if (next_token->type != CPP_NAME
13222 && next_token->type != CPP_SCOPE
13223 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13224 && next_token->type != CPP_TEMPLATE_ID)
13225 return false;
a723baf1
MM
13226
13227 /* Parse tentatively; we are going to roll back all of the tokens
13228 consumed here. */
13229 cp_parser_parse_tentatively (parser);
13230 /* Assume that we are looking at a constructor declarator. */
13231 constructor_p = true;
8d241e0b 13232
a723baf1
MM
13233 /* Look for the optional `::' operator. */
13234 cp_parser_global_scope_opt (parser,
13235 /*current_scope_valid_p=*/false);
13236 /* Look for the nested-name-specifier. */
13237 nested_name_p
13238 = (cp_parser_nested_name_specifier_opt (parser,
13239 /*typename_keyword_p=*/false,
13240 /*check_dependency_p=*/false,
13241 /*type_p=*/false)
13242 != NULL_TREE);
13243 /* Outside of a class-specifier, there must be a
13244 nested-name-specifier. */
13245 if (!nested_name_p &&
13246 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13247 || friend_p))
13248 constructor_p = false;
13249 /* If we still think that this might be a constructor-declarator,
13250 look for a class-name. */
13251 if (constructor_p)
13252 {
13253 /* If we have:
13254
8fbc5ae7 13255 template <typename T> struct S { S(); };
a723baf1
MM
13256 template <typename T> S<T>::S ();
13257
13258 we must recognize that the nested `S' names a class.
13259 Similarly, for:
13260
13261 template <typename T> S<T>::S<T> ();
13262
13263 we must recognize that the nested `S' names a template. */
13264 type_decl = cp_parser_class_name (parser,
13265 /*typename_keyword_p=*/false,
13266 /*template_keyword_p=*/false,
13267 /*type_p=*/false,
a723baf1
MM
13268 /*check_dependency_p=*/false,
13269 /*class_head_p=*/false);
13270 /* If there was no class-name, then this is not a constructor. */
13271 constructor_p = !cp_parser_error_occurred (parser);
13272 }
8d241e0b 13273
a723baf1
MM
13274 /* If we're still considering a constructor, we have to see a `(',
13275 to begin the parameter-declaration-clause, followed by either a
13276 `)', an `...', or a decl-specifier. We need to check for a
13277 type-specifier to avoid being fooled into thinking that:
13278
13279 S::S (f) (int);
13280
13281 is a constructor. (It is actually a function named `f' that
13282 takes one parameter (of type `int') and returns a value of type
13283 `S::S'. */
13284 if (constructor_p
13285 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13286 {
13287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13288 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13289 && !cp_parser_storage_class_specifier_opt (parser))
13290 {
5dae1114
MM
13291 tree type;
13292
13293 /* Names appearing in the type-specifier should be looked up
13294 in the scope of the class. */
13295 if (current_class_type)
13296 type = NULL_TREE;
a723baf1
MM
13297 else
13298 {
5dae1114
MM
13299 type = TREE_TYPE (type_decl);
13300 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13301 {
13302 type = resolve_typename_type (type,
13303 /*only_current_p=*/false);
13304 if (type == error_mark_node)
13305 {
13306 cp_parser_abort_tentative_parse (parser);
13307 return false;
13308 }
13309 }
5dae1114 13310 push_scope (type);
a723baf1 13311 }
5dae1114
MM
13312 /* Look for the type-specifier. */
13313 cp_parser_type_specifier (parser,
13314 CP_PARSER_FLAGS_NONE,
13315 /*is_friend=*/false,
13316 /*is_declarator=*/true,
13317 /*declares_class_or_enum=*/NULL,
13318 /*is_cv_qualifier=*/NULL);
13319 /* Leave the scope of the class. */
13320 if (type)
13321 pop_scope (type);
13322
13323 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13324 }
13325 }
13326 else
13327 constructor_p = false;
13328 /* We did not really want to consume any tokens. */
13329 cp_parser_abort_tentative_parse (parser);
13330
13331 return constructor_p;
13332}
13333
13334/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13335 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13336 they must be performed once we are in the scope of the function.
13337
13338 Returns the function defined. */
13339
13340static tree
13341cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13342 (cp_parser* parser,
13343 tree decl_specifiers,
13344 tree attributes,
13345 tree declarator)
a723baf1
MM
13346{
13347 tree fn;
13348 bool success_p;
13349
13350 /* Begin the function-definition. */
13351 success_p = begin_function_definition (decl_specifiers,
13352 attributes,
13353 declarator);
13354
13355 /* If there were names looked up in the decl-specifier-seq that we
13356 did not check, check them now. We must wait until we are in the
13357 scope of the function to perform the checks, since the function
13358 might be a friend. */
cf22909c 13359 perform_deferred_access_checks ();
a723baf1
MM
13360
13361 if (!success_p)
13362 {
13363 /* If begin_function_definition didn't like the definition, skip
13364 the entire function. */
13365 error ("invalid function declaration");
13366 cp_parser_skip_to_end_of_block_or_statement (parser);
13367 fn = error_mark_node;
13368 }
13369 else
13370 fn = cp_parser_function_definition_after_declarator (parser,
13371 /*inline_p=*/false);
13372
13373 return fn;
13374}
13375
13376/* Parse the part of a function-definition that follows the
13377 declarator. INLINE_P is TRUE iff this function is an inline
13378 function defined with a class-specifier.
13379
13380 Returns the function defined. */
13381
13382static tree
94edc4ab
NN
13383cp_parser_function_definition_after_declarator (cp_parser* parser,
13384 bool inline_p)
a723baf1
MM
13385{
13386 tree fn;
13387 bool ctor_initializer_p = false;
13388 bool saved_in_unbraced_linkage_specification_p;
13389 unsigned saved_num_template_parameter_lists;
13390
13391 /* If the next token is `return', then the code may be trying to
13392 make use of the "named return value" extension that G++ used to
13393 support. */
13394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13395 {
13396 /* Consume the `return' keyword. */
13397 cp_lexer_consume_token (parser->lexer);
13398 /* Look for the identifier that indicates what value is to be
13399 returned. */
13400 cp_parser_identifier (parser);
13401 /* Issue an error message. */
13402 error ("named return values are no longer supported");
13403 /* Skip tokens until we reach the start of the function body. */
13404 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13405 cp_lexer_consume_token (parser->lexer);
13406 }
13407 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13408 anything declared inside `f'. */
13409 saved_in_unbraced_linkage_specification_p
13410 = parser->in_unbraced_linkage_specification_p;
13411 parser->in_unbraced_linkage_specification_p = false;
13412 /* Inside the function, surrounding template-parameter-lists do not
13413 apply. */
13414 saved_num_template_parameter_lists
13415 = parser->num_template_parameter_lists;
13416 parser->num_template_parameter_lists = 0;
13417 /* If the next token is `try', then we are looking at a
13418 function-try-block. */
13419 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13420 ctor_initializer_p = cp_parser_function_try_block (parser);
13421 /* A function-try-block includes the function-body, so we only do
13422 this next part if we're not processing a function-try-block. */
13423 else
13424 ctor_initializer_p
13425 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13426
13427 /* Finish the function. */
13428 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13429 (inline_p ? 2 : 0));
13430 /* Generate code for it, if necessary. */
8cd2462c 13431 expand_or_defer_fn (fn);
a723baf1
MM
13432 /* Restore the saved values. */
13433 parser->in_unbraced_linkage_specification_p
13434 = saved_in_unbraced_linkage_specification_p;
13435 parser->num_template_parameter_lists
13436 = saved_num_template_parameter_lists;
13437
13438 return fn;
13439}
13440
13441/* Parse a template-declaration, assuming that the `export' (and
13442 `extern') keywords, if present, has already been scanned. MEMBER_P
13443 is as for cp_parser_template_declaration. */
13444
13445static void
94edc4ab 13446cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13447{
13448 tree decl = NULL_TREE;
13449 tree parameter_list;
13450 bool friend_p = false;
13451
13452 /* Look for the `template' keyword. */
13453 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13454 return;
13455
13456 /* And the `<'. */
13457 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13458 return;
13459
13460 /* Parse the template parameters. */
13461 begin_template_parm_list ();
13462 /* If the next token is `>', then we have an invalid
13463 specialization. Rather than complain about an invalid template
13464 parameter, issue an error message here. */
13465 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13466 {
13467 cp_parser_error (parser, "invalid explicit specialization");
13468 parameter_list = NULL_TREE;
13469 }
13470 else
13471 parameter_list = cp_parser_template_parameter_list (parser);
13472 parameter_list = end_template_parm_list (parameter_list);
13473 /* Look for the `>'. */
13474 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13475 /* We just processed one more parameter list. */
13476 ++parser->num_template_parameter_lists;
13477 /* If the next token is `template', there are more template
13478 parameters. */
13479 if (cp_lexer_next_token_is_keyword (parser->lexer,
13480 RID_TEMPLATE))
13481 cp_parser_template_declaration_after_export (parser, member_p);
13482 else
13483 {
13484 decl = cp_parser_single_declaration (parser,
13485 member_p,
13486 &friend_p);
13487
13488 /* If this is a member template declaration, let the front
13489 end know. */
13490 if (member_p && !friend_p && decl)
13491 decl = finish_member_template_decl (decl);
13492 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13493 make_friend_class (current_class_type, TREE_TYPE (decl));
13494 }
13495 /* We are done with the current parameter list. */
13496 --parser->num_template_parameter_lists;
13497
13498 /* Finish up. */
13499 finish_template_decl (parameter_list);
13500
13501 /* Register member declarations. */
13502 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13503 finish_member_declaration (decl);
13504
13505 /* If DECL is a function template, we must return to parse it later.
13506 (Even though there is no definition, there might be default
13507 arguments that need handling.) */
13508 if (member_p && decl
13509 && (TREE_CODE (decl) == FUNCTION_DECL
13510 || DECL_FUNCTION_TEMPLATE_P (decl)))
13511 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 13512 = tree_cons (NULL_TREE, decl,
a723baf1
MM
13513 TREE_VALUE (parser->unparsed_functions_queues));
13514}
13515
13516/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13517 `function-definition' sequence. MEMBER_P is true, this declaration
13518 appears in a class scope.
13519
13520 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13521 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13522
13523static tree
94edc4ab
NN
13524cp_parser_single_declaration (cp_parser* parser,
13525 bool member_p,
13526 bool* friend_p)
a723baf1
MM
13527{
13528 bool declares_class_or_enum;
13529 tree decl = NULL_TREE;
13530 tree decl_specifiers;
13531 tree attributes;
a723baf1
MM
13532
13533 /* Parse the dependent declaration. We don't know yet
13534 whether it will be a function-definition. */
13535 cp_parser_parse_tentatively (parser);
13536 /* Defer access checks until we know what is being declared. */
8d241e0b 13537 push_deferring_access_checks (dk_deferred);
cf22909c 13538
a723baf1
MM
13539 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13540 alternative. */
13541 decl_specifiers
13542 = cp_parser_decl_specifier_seq (parser,
13543 CP_PARSER_FLAGS_OPTIONAL,
13544 &attributes,
13545 &declares_class_or_enum);
13546 /* Gather up the access checks that occurred the
13547 decl-specifier-seq. */
cf22909c
KL
13548 stop_deferring_access_checks ();
13549
a723baf1
MM
13550 /* Check for the declaration of a template class. */
13551 if (declares_class_or_enum)
13552 {
13553 if (cp_parser_declares_only_class_p (parser))
13554 {
13555 decl = shadow_tag (decl_specifiers);
13556 if (decl)
13557 decl = TYPE_NAME (decl);
13558 else
13559 decl = error_mark_node;
13560 }
13561 }
13562 else
13563 decl = NULL_TREE;
13564 /* If it's not a template class, try for a template function. If
13565 the next token is a `;', then this declaration does not declare
13566 anything. But, if there were errors in the decl-specifiers, then
13567 the error might well have come from an attempted class-specifier.
13568 In that case, there's no need to warn about a missing declarator. */
13569 if (!decl
13570 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13571 || !value_member (error_mark_node, decl_specifiers)))
13572 decl = cp_parser_init_declarator (parser,
13573 decl_specifiers,
13574 attributes,
a723baf1
MM
13575 /*function_definition_allowed_p=*/false,
13576 member_p,
13577 /*function_definition_p=*/NULL);
cf22909c
KL
13578
13579 pop_deferring_access_checks ();
13580
a723baf1
MM
13581 /* Clear any current qualification; whatever comes next is the start
13582 of something new. */
13583 parser->scope = NULL_TREE;
13584 parser->qualifying_scope = NULL_TREE;
13585 parser->object_scope = NULL_TREE;
13586 /* Look for a trailing `;' after the declaration. */
8a6393df 13587 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
a723baf1
MM
13588 && cp_parser_committed_to_tentative_parse (parser))
13589 cp_parser_skip_to_end_of_block_or_statement (parser);
13590 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13591 if (cp_parser_parse_definitely (parser))
13592 {
13593 if (friend_p)
13594 *friend_p = cp_parser_friend_p (decl_specifiers);
13595 }
13596 /* Otherwise, try a function-definition. */
13597 else
13598 decl = cp_parser_function_definition (parser, friend_p);
13599
13600 return decl;
13601}
13602
d6b4ea85
MM
13603/* Parse a cast-expression that is not the operand of a unary "&". */
13604
13605static tree
13606cp_parser_simple_cast_expression (cp_parser *parser)
13607{
13608 return cp_parser_cast_expression (parser, /*address_p=*/false);
13609}
13610
a723baf1
MM
13611/* Parse a functional cast to TYPE. Returns an expression
13612 representing the cast. */
13613
13614static tree
94edc4ab 13615cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
13616{
13617 tree expression_list;
13618
39703eb9
MM
13619 expression_list
13620 = cp_parser_parenthesized_expression_list (parser, false,
13621 /*non_constant_p=*/NULL);
a723baf1
MM
13622
13623 return build_functional_cast (type, expression_list);
13624}
13625
13626/* MEMBER_FUNCTION is a member function, or a friend. If default
13627 arguments, or the body of the function have not yet been parsed,
13628 parse them now. */
13629
13630static void
94edc4ab 13631cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
13632{
13633 cp_lexer *saved_lexer;
13634
13635 /* If this member is a template, get the underlying
13636 FUNCTION_DECL. */
13637 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13638 member_function = DECL_TEMPLATE_RESULT (member_function);
13639
13640 /* There should not be any class definitions in progress at this
13641 point; the bodies of members are only parsed outside of all class
13642 definitions. */
13643 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13644 /* While we're parsing the member functions we might encounter more
13645 classes. We want to handle them right away, but we don't want
13646 them getting mixed up with functions that are currently in the
13647 queue. */
13648 parser->unparsed_functions_queues
13649 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13650
13651 /* Make sure that any template parameters are in scope. */
13652 maybe_begin_member_template_processing (member_function);
13653
a723baf1
MM
13654 /* If the body of the function has not yet been parsed, parse it
13655 now. */
13656 if (DECL_PENDING_INLINE_P (member_function))
13657 {
13658 tree function_scope;
13659 cp_token_cache *tokens;
13660
13661 /* The function is no longer pending; we are processing it. */
13662 tokens = DECL_PENDING_INLINE_INFO (member_function);
13663 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13664 DECL_PENDING_INLINE_P (member_function) = 0;
13665 /* If this was an inline function in a local class, enter the scope
13666 of the containing function. */
13667 function_scope = decl_function_context (member_function);
13668 if (function_scope)
13669 push_function_context_to (function_scope);
13670
13671 /* Save away the current lexer. */
13672 saved_lexer = parser->lexer;
13673 /* Make a new lexer to feed us the tokens saved for this function. */
13674 parser->lexer = cp_lexer_new_from_tokens (tokens);
13675 parser->lexer->next = saved_lexer;
13676
13677 /* Set the current source position to be the location of the first
13678 token in the saved inline body. */
3466b292 13679 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13680
13681 /* Let the front end know that we going to be defining this
13682 function. */
13683 start_function (NULL_TREE, member_function, NULL_TREE,
13684 SF_PRE_PARSED | SF_INCLASS_INLINE);
13685
13686 /* Now, parse the body of the function. */
13687 cp_parser_function_definition_after_declarator (parser,
13688 /*inline_p=*/true);
13689
13690 /* Leave the scope of the containing function. */
13691 if (function_scope)
13692 pop_function_context_from (function_scope);
13693 /* Restore the lexer. */
13694 parser->lexer = saved_lexer;
13695 }
13696
13697 /* Remove any template parameters from the symbol table. */
13698 maybe_end_member_template_processing ();
13699
13700 /* Restore the queue. */
13701 parser->unparsed_functions_queues
13702 = TREE_CHAIN (parser->unparsed_functions_queues);
13703}
13704
8db1028e
NS
13705/* If DECL contains any default args, remeber it on the unparsed
13706 functions queue. */
13707
13708static void
13709cp_parser_save_default_args (cp_parser* parser, tree decl)
13710{
13711 tree probe;
13712
13713 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13714 probe;
13715 probe = TREE_CHAIN (probe))
13716 if (TREE_PURPOSE (probe))
13717 {
13718 TREE_PURPOSE (parser->unparsed_functions_queues)
13719 = tree_cons (NULL_TREE, decl,
13720 TREE_PURPOSE (parser->unparsed_functions_queues));
13721 break;
13722 }
13723 return;
13724}
13725
8218bd34
MM
13726/* FN is a FUNCTION_DECL which may contains a parameter with an
13727 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
13728
13729static void
8218bd34 13730cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
13731{
13732 cp_lexer *saved_lexer;
13733 cp_token_cache *tokens;
13734 bool saved_local_variables_forbidden_p;
13735 tree parameters;
8218bd34
MM
13736
13737 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
13738 parameters;
13739 parameters = TREE_CHAIN (parameters))
13740 {
13741 if (!TREE_PURPOSE (parameters)
13742 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13743 continue;
13744
13745 /* Save away the current lexer. */
13746 saved_lexer = parser->lexer;
13747 /* Create a new one, using the tokens we have saved. */
13748 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13749 parser->lexer = cp_lexer_new_from_tokens (tokens);
13750
13751 /* Set the current source position to be the location of the
13752 first token in the default argument. */
3466b292 13753 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13754
13755 /* Local variable names (and the `this' keyword) may not appear
13756 in a default argument. */
13757 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13758 parser->local_variables_forbidden_p = true;
13759 /* Parse the assignment-expression. */
8218bd34 13760 if (DECL_CONTEXT (fn))
14d22dd6 13761 push_nested_class (DECL_CONTEXT (fn));
a723baf1 13762 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 13763 if (DECL_CONTEXT (fn))
e5976695 13764 pop_nested_class ();
a723baf1
MM
13765
13766 /* Restore saved state. */
13767 parser->lexer = saved_lexer;
13768 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13769 }
13770}
13771
13772/* Parse the operand of `sizeof' (or a similar operator). Returns
13773 either a TYPE or an expression, depending on the form of the
13774 input. The KEYWORD indicates which kind of expression we have
13775 encountered. */
13776
13777static tree
94edc4ab 13778cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
13779{
13780 static const char *format;
13781 tree expr = NULL_TREE;
13782 const char *saved_message;
13783 bool saved_constant_expression_p;
13784
13785 /* Initialize FORMAT the first time we get here. */
13786 if (!format)
13787 format = "types may not be defined in `%s' expressions";
13788
13789 /* Types cannot be defined in a `sizeof' expression. Save away the
13790 old message. */
13791 saved_message = parser->type_definition_forbidden_message;
13792 /* And create the new one. */
13793 parser->type_definition_forbidden_message
c68b0a84
KG
13794 = xmalloc (strlen (format)
13795 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13796 + 1 /* `\0' */);
a723baf1
MM
13797 sprintf ((char *) parser->type_definition_forbidden_message,
13798 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13799
13800 /* The restrictions on constant-expressions do not apply inside
13801 sizeof expressions. */
13802 saved_constant_expression_p = parser->constant_expression_p;
13803 parser->constant_expression_p = false;
13804
3beb3abf
MM
13805 /* Do not actually evaluate the expression. */
13806 ++skip_evaluation;
a723baf1
MM
13807 /* If it's a `(', then we might be looking at the type-id
13808 construction. */
13809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13810 {
13811 tree type;
13812
13813 /* We can't be sure yet whether we're looking at a type-id or an
13814 expression. */
13815 cp_parser_parse_tentatively (parser);
13816 /* Consume the `('. */
13817 cp_lexer_consume_token (parser->lexer);
13818 /* Parse the type-id. */
13819 type = cp_parser_type_id (parser);
13820 /* Now, look for the trailing `)'. */
13821 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13822 /* If all went well, then we're done. */
13823 if (cp_parser_parse_definitely (parser))
13824 {
13825 /* Build a list of decl-specifiers; right now, we have only
13826 a single type-specifier. */
13827 type = build_tree_list (NULL_TREE,
13828 type);
13829
13830 /* Call grokdeclarator to figure out what type this is. */
13831 expr = grokdeclarator (NULL_TREE,
13832 type,
13833 TYPENAME,
13834 /*initialized=*/0,
13835 /*attrlist=*/NULL);
13836 }
13837 }
13838
13839 /* If the type-id production did not work out, then we must be
13840 looking at the unary-expression production. */
13841 if (!expr)
13842 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
13843 /* Go back to evaluating expressions. */
13844 --skip_evaluation;
a723baf1
MM
13845
13846 /* Free the message we created. */
13847 free ((char *) parser->type_definition_forbidden_message);
13848 /* And restore the old one. */
13849 parser->type_definition_forbidden_message = saved_message;
13850 parser->constant_expression_p = saved_constant_expression_p;
13851
13852 return expr;
13853}
13854
13855/* If the current declaration has no declarator, return true. */
13856
13857static bool
13858cp_parser_declares_only_class_p (cp_parser *parser)
13859{
13860 /* If the next token is a `;' or a `,' then there is no
13861 declarator. */
13862 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13863 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13864}
13865
d17811fd
MM
13866/* Simplify EXPR if it is a non-dependent expression. Returns the
13867 (possibly simplified) expression. */
13868
13869static tree
13870cp_parser_fold_non_dependent_expr (tree expr)
13871{
13872 /* If we're in a template, but EXPR isn't value dependent, simplify
13873 it. We're supposed to treat:
13874
13875 template <typename T> void f(T[1 + 1]);
13876 template <typename T> void f(T[2]);
13877
13878 as two declarations of the same function, for example. */
13879 if (processing_template_decl
13880 && !type_dependent_expression_p (expr)
13881 && !value_dependent_expression_p (expr))
13882 {
13883 HOST_WIDE_INT saved_processing_template_decl;
13884
13885 saved_processing_template_decl = processing_template_decl;
13886 processing_template_decl = 0;
13887 expr = tsubst_copy_and_build (expr,
13888 /*args=*/NULL_TREE,
13889 tf_error,
b3445994
MM
13890 /*in_decl=*/NULL_TREE,
13891 /*function_p=*/false);
d17811fd
MM
13892 processing_template_decl = saved_processing_template_decl;
13893 }
13894 return expr;
13895}
13896
a723baf1
MM
13897/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
13898 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
13899
13900static bool
94edc4ab 13901cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
13902{
13903 while (decl_specifiers)
13904 {
13905 /* See if this decl-specifier is `friend'. */
13906 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
13907 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
13908 return true;
13909
13910 /* Go on to the next decl-specifier. */
13911 decl_specifiers = TREE_CHAIN (decl_specifiers);
13912 }
13913
13914 return false;
13915}
13916
13917/* If the next token is of the indicated TYPE, consume it. Otherwise,
13918 issue an error message indicating that TOKEN_DESC was expected.
13919
13920 Returns the token consumed, if the token had the appropriate type.
13921 Otherwise, returns NULL. */
13922
13923static cp_token *
94edc4ab
NN
13924cp_parser_require (cp_parser* parser,
13925 enum cpp_ttype type,
13926 const char* token_desc)
a723baf1
MM
13927{
13928 if (cp_lexer_next_token_is (parser->lexer, type))
13929 return cp_lexer_consume_token (parser->lexer);
13930 else
13931 {
e5976695
MM
13932 /* Output the MESSAGE -- unless we're parsing tentatively. */
13933 if (!cp_parser_simulate_error (parser))
13934 error ("expected %s", token_desc);
a723baf1
MM
13935 return NULL;
13936 }
13937}
13938
13939/* Like cp_parser_require, except that tokens will be skipped until
13940 the desired token is found. An error message is still produced if
13941 the next token is not as expected. */
13942
13943static void
94edc4ab
NN
13944cp_parser_skip_until_found (cp_parser* parser,
13945 enum cpp_ttype type,
13946 const char* token_desc)
a723baf1
MM
13947{
13948 cp_token *token;
13949 unsigned nesting_depth = 0;
13950
13951 if (cp_parser_require (parser, type, token_desc))
13952 return;
13953
13954 /* Skip tokens until the desired token is found. */
13955 while (true)
13956 {
13957 /* Peek at the next token. */
13958 token = cp_lexer_peek_token (parser->lexer);
13959 /* If we've reached the token we want, consume it and
13960 stop. */
13961 if (token->type == type && !nesting_depth)
13962 {
13963 cp_lexer_consume_token (parser->lexer);
13964 return;
13965 }
13966 /* If we've run out of tokens, stop. */
13967 if (token->type == CPP_EOF)
13968 return;
13969 if (token->type == CPP_OPEN_BRACE
13970 || token->type == CPP_OPEN_PAREN
13971 || token->type == CPP_OPEN_SQUARE)
13972 ++nesting_depth;
13973 else if (token->type == CPP_CLOSE_BRACE
13974 || token->type == CPP_CLOSE_PAREN
13975 || token->type == CPP_CLOSE_SQUARE)
13976 {
13977 if (nesting_depth-- == 0)
13978 return;
13979 }
13980 /* Consume this token. */
13981 cp_lexer_consume_token (parser->lexer);
13982 }
13983}
13984
13985/* If the next token is the indicated keyword, consume it. Otherwise,
13986 issue an error message indicating that TOKEN_DESC was expected.
13987
13988 Returns the token consumed, if the token had the appropriate type.
13989 Otherwise, returns NULL. */
13990
13991static cp_token *
94edc4ab
NN
13992cp_parser_require_keyword (cp_parser* parser,
13993 enum rid keyword,
13994 const char* token_desc)
a723baf1
MM
13995{
13996 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
13997
13998 if (token && token->keyword != keyword)
13999 {
14000 dyn_string_t error_msg;
14001
14002 /* Format the error message. */
14003 error_msg = dyn_string_new (0);
14004 dyn_string_append_cstr (error_msg, "expected ");
14005 dyn_string_append_cstr (error_msg, token_desc);
14006 cp_parser_error (parser, error_msg->s);
14007 dyn_string_delete (error_msg);
14008 return NULL;
14009 }
14010
14011 return token;
14012}
14013
14014/* Returns TRUE iff TOKEN is a token that can begin the body of a
14015 function-definition. */
14016
14017static bool
94edc4ab 14018cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14019{
14020 return (/* An ordinary function-body begins with an `{'. */
14021 token->type == CPP_OPEN_BRACE
14022 /* A ctor-initializer begins with a `:'. */
14023 || token->type == CPP_COLON
14024 /* A function-try-block begins with `try'. */
14025 || token->keyword == RID_TRY
14026 /* The named return value extension begins with `return'. */
14027 || token->keyword == RID_RETURN);
14028}
14029
14030/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14031 definition. */
14032
14033static bool
14034cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14035{
14036 cp_token *token;
14037
14038 token = cp_lexer_peek_token (parser->lexer);
14039 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14040}
14041
d17811fd
MM
14042/* Returns TRUE iff the next token is the "," or ">" ending a
14043 template-argument. */
14044
14045static bool
14046cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14047{
14048 cp_token *token;
14049
14050 token = cp_lexer_peek_token (parser->lexer);
14051 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14052}
14053
a723baf1
MM
14054/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14055 or none_type otherwise. */
14056
14057static enum tag_types
94edc4ab 14058cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14059{
14060 switch (token->keyword)
14061 {
14062 case RID_CLASS:
14063 return class_type;
14064 case RID_STRUCT:
14065 return record_type;
14066 case RID_UNION:
14067 return union_type;
14068
14069 default:
14070 return none_type;
14071 }
14072}
14073
14074/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14075
14076static void
14077cp_parser_check_class_key (enum tag_types class_key, tree type)
14078{
14079 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14080 pedwarn ("`%s' tag used in naming `%#T'",
14081 class_key == union_type ? "union"
14082 : class_key == record_type ? "struct" : "class",
14083 type);
14084}
14085
14086/* Look for the `template' keyword, as a syntactic disambiguator.
14087 Return TRUE iff it is present, in which case it will be
14088 consumed. */
14089
14090static bool
14091cp_parser_optional_template_keyword (cp_parser *parser)
14092{
14093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14094 {
14095 /* The `template' keyword can only be used within templates;
14096 outside templates the parser can always figure out what is a
14097 template and what is not. */
14098 if (!processing_template_decl)
14099 {
14100 error ("`template' (as a disambiguator) is only allowed "
14101 "within templates");
14102 /* If this part of the token stream is rescanned, the same
14103 error message would be generated. So, we purge the token
14104 from the stream. */
14105 cp_lexer_purge_token (parser->lexer);
14106 return false;
14107 }
14108 else
14109 {
14110 /* Consume the `template' keyword. */
14111 cp_lexer_consume_token (parser->lexer);
14112 return true;
14113 }
14114 }
14115
14116 return false;
14117}
14118
2050a1bb
MM
14119/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14120 set PARSER->SCOPE, and perform other related actions. */
14121
14122static void
14123cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14124{
14125 tree value;
14126 tree check;
14127
14128 /* Get the stored value. */
14129 value = cp_lexer_consume_token (parser->lexer)->value;
14130 /* Perform any access checks that were deferred. */
14131 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14132 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14133 /* Set the scope from the stored value. */
14134 parser->scope = TREE_VALUE (value);
14135 parser->qualifying_scope = TREE_TYPE (value);
14136 parser->object_scope = NULL_TREE;
14137}
14138
a723baf1
MM
14139/* Add tokens to CACHE until an non-nested END token appears. */
14140
14141static void
14142cp_parser_cache_group (cp_parser *parser,
14143 cp_token_cache *cache,
14144 enum cpp_ttype end,
14145 unsigned depth)
14146{
14147 while (true)
14148 {
14149 cp_token *token;
14150
14151 /* Abort a parenthesized expression if we encounter a brace. */
14152 if ((end == CPP_CLOSE_PAREN || depth == 0)
14153 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14154 return;
14155 /* Consume the next token. */
14156 token = cp_lexer_consume_token (parser->lexer);
14157 /* If we've reached the end of the file, stop. */
14158 if (token->type == CPP_EOF)
14159 return;
14160 /* Add this token to the tokens we are saving. */
14161 cp_token_cache_push_token (cache, token);
14162 /* See if it starts a new group. */
14163 if (token->type == CPP_OPEN_BRACE)
14164 {
14165 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14166 if (depth == 0)
14167 return;
14168 }
14169 else if (token->type == CPP_OPEN_PAREN)
14170 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14171 else if (token->type == end)
14172 return;
14173 }
14174}
14175
14176/* Begin parsing tentatively. We always save tokens while parsing
14177 tentatively so that if the tentative parsing fails we can restore the
14178 tokens. */
14179
14180static void
94edc4ab 14181cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14182{
14183 /* Enter a new parsing context. */
14184 parser->context = cp_parser_context_new (parser->context);
14185 /* Begin saving tokens. */
14186 cp_lexer_save_tokens (parser->lexer);
14187 /* In order to avoid repetitive access control error messages,
14188 access checks are queued up until we are no longer parsing
14189 tentatively. */
8d241e0b 14190 push_deferring_access_checks (dk_deferred);
a723baf1
MM
14191}
14192
14193/* Commit to the currently active tentative parse. */
14194
14195static void
94edc4ab 14196cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14197{
14198 cp_parser_context *context;
14199 cp_lexer *lexer;
14200
14201 /* Mark all of the levels as committed. */
14202 lexer = parser->lexer;
14203 for (context = parser->context; context->next; context = context->next)
14204 {
14205 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14206 break;
14207 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14208 while (!cp_lexer_saving_tokens (lexer))
14209 lexer = lexer->next;
14210 cp_lexer_commit_tokens (lexer);
14211 }
14212}
14213
14214/* Abort the currently active tentative parse. All consumed tokens
14215 will be rolled back, and no diagnostics will be issued. */
14216
14217static void
94edc4ab 14218cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14219{
14220 cp_parser_simulate_error (parser);
14221 /* Now, pretend that we want to see if the construct was
14222 successfully parsed. */
14223 cp_parser_parse_definitely (parser);
14224}
14225
34cd5ae7 14226/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
14227 token stream. Otherwise, commit to the tokens we have consumed.
14228 Returns true if no error occurred; false otherwise. */
14229
14230static bool
94edc4ab 14231cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14232{
14233 bool error_occurred;
14234 cp_parser_context *context;
14235
34cd5ae7 14236 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
14237 destroy that information. */
14238 error_occurred = cp_parser_error_occurred (parser);
14239 /* Remove the topmost context from the stack. */
14240 context = parser->context;
14241 parser->context = context->next;
14242 /* If no parse errors occurred, commit to the tentative parse. */
14243 if (!error_occurred)
14244 {
14245 /* Commit to the tokens read tentatively, unless that was
14246 already done. */
14247 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14248 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14249
14250 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14251 }
14252 /* Otherwise, if errors occurred, roll back our state so that things
14253 are just as they were before we began the tentative parse. */
14254 else
cf22909c
KL
14255 {
14256 cp_lexer_rollback_tokens (parser->lexer);
14257 pop_deferring_access_checks ();
14258 }
e5976695
MM
14259 /* Add the context to the front of the free list. */
14260 context->next = cp_parser_context_free_list;
14261 cp_parser_context_free_list = context;
14262
14263 return !error_occurred;
a723baf1
MM
14264}
14265
a723baf1
MM
14266/* Returns true if we are parsing tentatively -- but have decided that
14267 we will stick with this tentative parse, even if errors occur. */
14268
14269static bool
94edc4ab 14270cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14271{
14272 return (cp_parser_parsing_tentatively (parser)
14273 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14274}
14275
4de8668e 14276/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
14277 tentative parse. */
14278
14279static bool
94edc4ab 14280cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14281{
14282 return (cp_parser_parsing_tentatively (parser)
14283 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14284}
14285
4de8668e 14286/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
14287
14288static bool
94edc4ab 14289cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14290{
14291 return parser->allow_gnu_extensions_p;
14292}
14293
14294\f
14295
14296/* The parser. */
14297
14298static GTY (()) cp_parser *the_parser;
14299
14300/* External interface. */
14301
d1bd0ded 14302/* Parse one entire translation unit. */
a723baf1 14303
d1bd0ded
GK
14304void
14305c_parse_file (void)
a723baf1
MM
14306{
14307 bool error_occurred;
14308
14309 the_parser = cp_parser_new ();
78757caa
KL
14310 push_deferring_access_checks (flag_access_control
14311 ? dk_no_deferred : dk_no_check);
a723baf1
MM
14312 error_occurred = cp_parser_translation_unit (the_parser);
14313 the_parser = NULL;
a723baf1
MM
14314}
14315
14316/* Clean up after parsing the entire translation unit. */
14317
14318void
94edc4ab 14319free_parser_stacks (void)
a723baf1
MM
14320{
14321 /* Nothing to do. */
14322}
14323
14324/* This variable must be provided by every front end. */
14325
14326int yydebug;
14327
14328#include "gt-cp-parser.h"