]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
coverage.c (read_counts_file): Better error messages...
[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. */
df2b750f 71 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
df2b750f 74 ENUM_BITFIELD (rid) keyword : 8;
522df488
DN
75 /* The value associated with this token, if any. */
76 tree value;
82a98427
NS
77 /* The location at which this token was found. */
78 location_t location;
a723baf1
MM
79} cp_token;
80
522df488
DN
81/* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
a723baf1 83
522df488 84#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
a723baf1
MM
85
86/* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
90 complete.)
91
92 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 93 variable-length array), is used due to constraints imposed by the
a723baf1
MM
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
96
97typedef struct cp_token_block GTY (())
98{
99 /* The tokens. */
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
102 size_t num_tokens;
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
107} cp_token_block;
108
109typedef struct cp_token_cache GTY (())
110{
111 /* The first block in the cache. NULL if there are no tokens in the
112 cache. */
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
115 cache. */
116 cp_token_block *last;
117} cp_token_cache;
118
9bcb9aae 119/* Prototypes. */
a723baf1
MM
120
121static cp_token_cache *cp_token_cache_new
122 (void);
123static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
125
126/* Create a new cp_token_cache. */
127
128static cp_token_cache *
bf9d3c27 129cp_token_cache_new (void)
a723baf1 130{
c68b0a84 131 return ggc_alloc_cleared (sizeof (cp_token_cache));
a723baf1
MM
132}
133
134/* Add *TOKEN to *CACHE. */
135
136static void
137cp_token_cache_push_token (cp_token_cache *cache,
138 cp_token *token)
139{
140 cp_token_block *b = cache->last;
141
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144 {
c68b0a84 145 b = ggc_alloc_cleared (sizeof (cp_token_block));
a723baf1
MM
146 b->prev = cache->last;
147 if (cache->last)
148 {
149 cache->last->next = b;
150 cache->last = b;
151 }
152 else
153 cache->first = cache->last = b;
154 }
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
157}
158
159/* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
161 it to the parser. */
162
163typedef struct cp_lexer GTY (())
164{
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
181
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
186
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
191
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
196
197 /* The STRING_CST tokens encountered while processing the current
198 string literal. */
199 varray_type string_tokens;
200
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
203 bool main_lexer_p;
204
205 /* True if we should output debugging information. */
206 bool debugging_p;
207
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
210} cp_lexer;
211
212/* Prototypes. */
213
17211ab5 214static cp_lexer *cp_lexer_new_main
94edc4ab 215 (void);
a723baf1 216static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 217 (struct cp_token_cache *);
a723baf1 218static int cp_lexer_saving_tokens
94edc4ab 219 (const cp_lexer *);
a723baf1 220static cp_token *cp_lexer_next_token
94edc4ab 221 (cp_lexer *, cp_token *);
a668c6ad
MM
222static cp_token *cp_lexer_prev_token
223 (cp_lexer *, cp_token *);
94edc4ab
NN
224static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer *, cp_token *, cp_token *);
a723baf1 226static cp_token *cp_lexer_read_token
94edc4ab 227 (cp_lexer *);
a723baf1 228static void cp_lexer_maybe_grow_buffer
94edc4ab 229 (cp_lexer *);
a723baf1 230static void cp_lexer_get_preprocessor_token
94edc4ab 231 (cp_lexer *, cp_token *);
a723baf1 232static cp_token *cp_lexer_peek_token
94edc4ab 233 (cp_lexer *);
a723baf1 234static cp_token *cp_lexer_peek_nth_token
94edc4ab 235 (cp_lexer *, size_t);
f7b5ecd9 236static inline bool cp_lexer_next_token_is
94edc4ab 237 (cp_lexer *, enum cpp_ttype);
a723baf1 238static bool cp_lexer_next_token_is_not
94edc4ab 239 (cp_lexer *, enum cpp_ttype);
a723baf1 240static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
241 (cp_lexer *, enum rid);
242static cp_token *cp_lexer_consume_token
243 (cp_lexer *);
a723baf1
MM
244static void cp_lexer_purge_token
245 (cp_lexer *);
246static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248static void cp_lexer_save_tokens
94edc4ab 249 (cp_lexer *);
a723baf1 250static void cp_lexer_commit_tokens
94edc4ab 251 (cp_lexer *);
a723baf1 252static void cp_lexer_rollback_tokens
94edc4ab 253 (cp_lexer *);
f7b5ecd9 254static inline void cp_lexer_set_source_position_from_token
94edc4ab 255 (cp_lexer *, const cp_token *);
a723baf1 256static void cp_lexer_print_token
94edc4ab 257 (FILE *, cp_token *);
f7b5ecd9 258static inline bool cp_lexer_debugging_p
94edc4ab 259 (cp_lexer *);
a723baf1 260static void cp_lexer_start_debugging
94edc4ab 261 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 262static void cp_lexer_stop_debugging
94edc4ab 263 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
264
265/* Manifest constants. */
266
267#define CP_TOKEN_BUFFER_SIZE 5
268#define CP_SAVED_TOKENS_SIZE 5
269
270/* A token type for keywords, as opposed to ordinary identifiers. */
271#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273/* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279/* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286/* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290/* Variables. */
291
292/* The stream to which debugging output should be written. */
293static FILE *cp_lexer_debug_stream;
294
17211ab5
GK
295/* Create a new main C++ lexer, the lexer that gets tokens from the
296 preprocessor. */
a723baf1
MM
297
298static cp_lexer *
17211ab5 299cp_lexer_new_main (void)
a723baf1
MM
300{
301 cp_lexer *lexer;
17211ab5
GK
302 cp_token first_token;
303
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 308 c_common_no_more_pch ();
a723baf1
MM
309
310 /* Allocate the memory. */
c68b0a84 311 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
312
313 /* Create the circular buffer. */
c68b0a84 314 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
17211ab5
GK
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
322
323 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 324 lexer->main_lexer_p = true;
a723baf1
MM
325
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
334
335 return lexer;
336}
337
338/* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
340
341static cp_lexer *
342cp_lexer_new_from_tokens (cp_token_cache *tokens)
343{
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
348
17211ab5 349 /* Allocate the memory. */
c68b0a84 350 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
351
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
c68b0a84 356 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1
MM
357 lexer->buffer_end = lexer->buffer + num_tokens;
358
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
362 {
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
365 }
366
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
373
17211ab5
GK
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
376
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
385
a723baf1
MM
386 return lexer;
387}
388
4de8668e 389/* Returns nonzero if debugging information should be output. */
a723baf1 390
f7b5ecd9
MM
391static inline bool
392cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 393{
f7b5ecd9
MM
394 return lexer->debugging_p;
395}
396
397/* Set the current source position from the information stored in
398 TOKEN. */
399
400static inline void
94edc4ab
NN
401cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
f7b5ecd9
MM
403{
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
406
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
82a98427 409 input_location = token->location;
a723baf1
MM
410}
411
412/* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
414
f7b5ecd9 415static inline cp_token *
94edc4ab 416cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
417{
418 token++;
419 if (token == lexer->buffer_end)
420 token = lexer->buffer;
421 return token;
422}
423
a668c6ad
MM
424/* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
426
427static inline cp_token *
428cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
429{
430 if (token == lexer->buffer)
431 token = lexer->buffer_end;
432 return token - 1;
433}
434
4de8668e 435/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
436
437static int
94edc4ab 438cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
439{
440 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
441}
442
a723baf1
MM
443/* Return a pointer to the token that is N tokens beyond TOKEN in the
444 buffer. */
445
446static cp_token *
447cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
448{
449 token += n;
450 if (token >= lexer->buffer_end)
451 token = lexer->buffer + (token - lexer->buffer_end);
452 return token;
453}
454
455/* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
457
458static ptrdiff_t
94edc4ab 459cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
460{
461 if (finish >= start)
462 return finish - start;
463 else
464 return ((lexer->buffer_end - lexer->buffer)
465 - (start - finish));
466}
467
468/* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
470
471static cp_token *
94edc4ab 472cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
473{
474 cp_token *token;
475
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
478
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
485
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
488
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
491
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
494
e6cc3a24
ZW
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
497 constant elements.
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500 && flag_const_strings)
a723baf1 501 {
e6cc3a24
ZW
502 tree type;
503
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type = TREE_TYPE (token->value);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509 /* Reset the type of the token. */
510 TREE_TYPE (token->value) = type;
a723baf1
MM
511 }
512
513 return token;
514}
515
516/* If the circular buffer is full, make it bigger. */
517
518static void
94edc4ab 519cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
520{
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
523 {
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
529
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
532 value. */
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
c68b0a84
KG
537 new_buffer = ggc_realloc (lexer->buffer,
538 2 * buffer_length * sizeof (cp_token));
a723baf1
MM
539
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
544 buffer. */
545 num_tokens_to_copy = (lexer->first_token - old_buffer);
546 memcpy (new_buffer + buffer_length,
547 new_buffer,
548 num_tokens_to_copy * sizeof (cp_token));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
552 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
553
554 /* Now recompute all of the buffer pointers. */
555 new_first_token
556 = new_buffer + (lexer->first_token - old_buffer);
557 if (lexer->next_token != NULL)
558 {
559 ptrdiff_t next_token_delta;
560
561 if (lexer->next_token > lexer->first_token)
562 next_token_delta = lexer->next_token - lexer->first_token;
563 else
564 next_token_delta =
565 buffer_length - (lexer->first_token - lexer->next_token);
566 lexer->next_token = new_first_token + next_token_delta;
567 }
568 lexer->last_token = new_first_token + buffer_length;
569 lexer->buffer = new_buffer;
570 lexer->buffer_end = new_buffer + buffer_length * 2;
571 lexer->first_token = new_first_token;
572 }
573}
574
575/* Store the next token from the preprocessor in *TOKEN. */
576
577static void
94edc4ab
NN
578cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
579 cp_token *token)
a723baf1
MM
580{
581 bool done;
582
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 584 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
585 {
586 token->type = CPP_EOF;
82a98427
NS
587 token->location.line = 0;
588 token->location.file = NULL;
a723baf1
MM
589 token->value = NULL_TREE;
590 token->keyword = RID_MAX;
591
592 return;
593 }
594
595 done = false;
596 /* Keep going until we get a token we like. */
597 while (!done)
598 {
599 /* Get a new token from the preprocessor. */
600 token->type = c_lex (&token->value);
601 /* Issue messages about tokens we cannot process. */
602 switch (token->type)
603 {
604 case CPP_ATSIGN:
605 case CPP_HASH:
606 case CPP_PASTE:
607 error ("invalid token");
608 break;
609
a723baf1
MM
610 default:
611 /* This is a good token, so we exit the loop. */
612 done = true;
613 break;
614 }
615 }
616 /* Now we've got our token. */
82a98427 617 token->location = input_location;
a723baf1
MM
618
619 /* Check to see if this token is a keyword. */
620 if (token->type == CPP_NAME
621 && C_IS_RESERVED_WORD (token->value))
622 {
623 /* Mark this token as a keyword. */
624 token->type = CPP_KEYWORD;
625 /* Record which keyword. */
626 token->keyword = C_RID_CODE (token->value);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token->value = ridpointers[token->keyword];
632 }
633 else
634 token->keyword = RID_MAX;
635}
636
637/* Return a pointer to the next token in the token stream, but do not
638 consume it. */
639
640static cp_token *
94edc4ab 641cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
642{
643 cp_token *token;
644
645 /* If there are no tokens, read one now. */
646 if (!lexer->next_token)
647 cp_lexer_read_token (lexer);
648
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 {
652 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654 fprintf (cp_lexer_debug_stream, "\n");
655 }
656
657 token = lexer->next_token;
658 cp_lexer_set_source_position_from_token (lexer, token);
659 return token;
660}
661
662/* Return true if the next token has the indicated TYPE. */
663
664static bool
94edc4ab 665cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
666{
667 cp_token *token;
668
669 /* Peek at the next token. */
670 token = cp_lexer_peek_token (lexer);
671 /* Check to see if it has the indicated TYPE. */
672 return token->type == type;
673}
674
675/* Return true if the next token does not have the indicated TYPE. */
676
677static bool
94edc4ab 678cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
679{
680 return !cp_lexer_next_token_is (lexer, type);
681}
682
683/* Return true if the next token is the indicated KEYWORD. */
684
685static bool
94edc4ab 686cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
687{
688 cp_token *token;
689
690 /* Peek at the next token. */
691 token = cp_lexer_peek_token (lexer);
692 /* Check to see if it is the indicated keyword. */
693 return token->keyword == keyword;
694}
695
696/* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
698
699static cp_token *
94edc4ab 700cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
701{
702 cp_token *token;
703
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n > 0, 20000224);
706
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token = lexer->next_token;
709 /* If there are no tokens in the buffer, get one now. */
710 if (!token)
711 {
712 cp_lexer_read_token (lexer);
713 token = lexer->next_token;
714 }
715
716 /* Now, read tokens until we have enough. */
717 while (--n > 0)
718 {
719 /* Advance to the next token. */
720 token = cp_lexer_next_token (lexer, token);
721 /* If that's all the tokens we have, read a new one. */
722 if (token == lexer->last_token)
723 token = cp_lexer_read_token (lexer);
724 }
725
726 return token;
727}
728
729/* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
732 time. */
733
734static cp_token *
94edc4ab 735cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
736{
737 cp_token *token;
738
739 /* If there are no tokens, read one now. */
740 if (!lexer->next_token)
741 cp_lexer_read_token (lexer);
742
743 /* Remember the token we'll be returning. */
744 token = lexer->next_token;
745
746 /* Increment NEXT_TOKEN. */
747 lexer->next_token = cp_lexer_next_token (lexer,
748 lexer->next_token);
749 /* Check to see if we're all out of tokens. */
750 if (lexer->next_token == lexer->last_token)
751 lexer->next_token = NULL;
752
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer))
755 {
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer->next_token)
758 lexer->first_token = NULL;
759 else
760 lexer->first_token = lexer->next_token;
761 }
762
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer))
765 {
766 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream, token);
768 fprintf (cp_lexer_debug_stream, "\n");
769 }
770
771 return token;
772}
773
774/* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
777
778static void
779cp_lexer_purge_token (cp_lexer *lexer)
780{
781 cp_token *token;
782 cp_token *next_token;
783
784 token = lexer->next_token;
785 while (true)
786 {
787 next_token = cp_lexer_next_token (lexer, token);
788 if (next_token == lexer->last_token)
789 break;
790 *token = *next_token;
791 token = next_token;
792 }
793
794 lexer->last_token = token;
795 /* The token purged may have been the only token remaining; if so,
796 clear NEXT_TOKEN. */
797 if (lexer->next_token == token)
798 lexer->next_token = NULL;
799}
800
801/* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
804
805static void
806cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
807{
808 cp_token *peek;
809 cp_token *t1;
810 cp_token *t2;
811
812 if (lexer->next_token)
813 {
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1 = cp_lexer_next_token (lexer, token);
817 t2 = peek = cp_lexer_peek_token (lexer);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2 != lexer->last_token)
820 {
821 *t1 = *t2;
822 t1 = cp_lexer_next_token (lexer, t1);
823 t2 = cp_lexer_next_token (lexer, t2);
824 }
825 /* Now, the next available token is right after TOKEN. */
826 lexer->next_token = cp_lexer_next_token (lexer, token);
827 /* And the last token is wherever we ended up. */
828 lexer->last_token = t1;
829 }
830 else
831 {
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer->last_token = cp_lexer_next_token (lexer, token);
835 }
836}
837
838/* Begin saving tokens. All tokens consumed after this point will be
839 preserved. */
840
841static void
94edc4ab 842cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
843{
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer))
846 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
847
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer->next_token)
851 cp_lexer_read_token (lexer);
852
853 VARRAY_PUSH_INT (lexer->saved_tokens,
854 cp_lexer_token_difference (lexer,
855 lexer->first_token,
856 lexer->next_token));
857}
858
859/* Commit to the portion of the token stream most recently saved. */
860
861static void
94edc4ab 862cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
863{
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer))
866 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
867
868 VARRAY_POP (lexer->saved_tokens);
869}
870
871/* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
873
874static void
94edc4ab 875cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
876{
877 size_t delta;
878
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer))
881 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
882
883 /* Find the token that was the NEXT_TOKEN when we started saving
884 tokens. */
885 delta = VARRAY_TOP_INT(lexer->saved_tokens);
886 /* Make it the next token again now. */
887 lexer->next_token = cp_lexer_advance_token (lexer,
888 lexer->first_token,
889 delta);
15d2cb19 890 /* It might be the case that there were no tokens when we started
a723baf1
MM
891 saving tokens, but that there are some tokens now. */
892 if (!lexer->next_token && lexer->first_token)
893 lexer->next_token = lexer->first_token;
894
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer->saved_tokens);
897}
898
a723baf1
MM
899/* Print a representation of the TOKEN on the STREAM. */
900
901static void
94edc4ab 902cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
903{
904 const char *token_type = NULL;
905
906 /* Figure out what kind of token this is. */
907 switch (token->type)
908 {
909 case CPP_EQ:
910 token_type = "EQ";
911 break;
912
913 case CPP_COMMA:
914 token_type = "COMMA";
915 break;
916
917 case CPP_OPEN_PAREN:
918 token_type = "OPEN_PAREN";
919 break;
920
921 case CPP_CLOSE_PAREN:
922 token_type = "CLOSE_PAREN";
923 break;
924
925 case CPP_OPEN_BRACE:
926 token_type = "OPEN_BRACE";
927 break;
928
929 case CPP_CLOSE_BRACE:
930 token_type = "CLOSE_BRACE";
931 break;
932
933 case CPP_SEMICOLON:
934 token_type = "SEMICOLON";
935 break;
936
937 case CPP_NAME:
938 token_type = "NAME";
939 break;
940
941 case CPP_EOF:
942 token_type = "EOF";
943 break;
944
945 case CPP_KEYWORD:
946 token_type = "keyword";
947 break;
948
949 /* This is not a token that we know how to handle yet. */
950 default:
951 break;
952 }
953
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
956 if (token_type)
957 fprintf (stream, "%s", token_type);
958 else
959 fprintf (stream, "%d", token->type);
960 /* And, for an identifier, print the identifier name. */
961 if (token->type == CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token->type == CPP_KEYWORD
965 && TREE_CODE (token->value) == IDENTIFIER_NODE))
966 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
967}
968
a723baf1
MM
969/* Start emitting debugging information. */
970
971static void
94edc4ab 972cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
973{
974 ++lexer->debugging_p;
975}
976
977/* Stop emitting debugging information. */
978
979static void
94edc4ab 980cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
981{
982 --lexer->debugging_p;
983}
984
985\f
986/* The parser. */
987
988/* Overview
989 --------
990
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
998
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1002
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1011
1012 Methodology
1013 -----------
1014
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1031
1032 Future Improvements
1033 -------------------
1034
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1037
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1042
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1047
1048/* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1050
1051typedef enum cp_parser_flags
1052{
1053 /* No flags. */
1054 CP_PARSER_FLAGS_NONE = 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1060} cp_parser_flags;
1061
62b8a44e
NS
1062/* The different kinds of declarators we want to parse. */
1063
1064typedef enum cp_parser_declarator_kind
1065{
9bcb9aae 1066 /* We want an abstract declartor. */
62b8a44e
NS
1067 CP_PARSER_DECLARATOR_ABSTRACT,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED,
04c06002 1070 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1071 CP_PARSER_DECLARATOR_EITHER
1072} cp_parser_declarator_kind;
1073
a723baf1
MM
1074/* A mapping from a token type to a corresponding tree node type. */
1075
1076typedef struct cp_parser_token_tree_map_node
1077{
1078 /* The token type. */
df2b750f 1079 ENUM_BITFIELD (cpp_ttype) token_type : 8;
a723baf1 1080 /* The corresponding tree code. */
df2b750f 1081 ENUM_BITFIELD (tree_code) tree_type : 8;
a723baf1
MM
1082} cp_parser_token_tree_map_node;
1083
1084/* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1086
1087typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1088
1089/* The status of a tentative parse. */
1090
1091typedef enum cp_parser_status_kind
1092{
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1098 has occurred. */
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100} cp_parser_status_kind;
1101
1102/* Context that is saved and restored when parsing tentatively. */
1103
1104typedef struct cp_parser_context GTY (())
1105{
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
a723baf1
MM
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116} cp_parser_context;
1117
1118/* Prototypes. */
1119
1120/* Constructors and destructors. */
1121
1122static cp_parser_context *cp_parser_context_new
94edc4ab 1123 (cp_parser_context *);
a723baf1 1124
e5976695
MM
1125/* Class variables. */
1126
92bc1323 1127static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1128
a723baf1
MM
1129/* Constructors and destructors. */
1130
1131/* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1133
1134static cp_parser_context *
94edc4ab 1135cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1136{
1137 cp_parser_context *context;
1138
1139 /* Allocate the storage. */
e5976695
MM
1140 if (cp_parser_context_free_list != NULL)
1141 {
1142 /* Pull the first entry from the free list. */
1143 context = cp_parser_context_free_list;
1144 cp_parser_context_free_list = context->next;
c68b0a84 1145 memset (context, 0, sizeof (*context));
e5976695
MM
1146 }
1147 else
c68b0a84 1148 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1149 /* No errors have occurred yet in this context. */
1150 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1153 if (next)
1154 {
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context->object_type = next->object_type;
a723baf1
MM
1158 /* Thread the stack. */
1159 context->next = next;
1160 }
1161
1162 return context;
1163}
1164
1165/* The cp_parser structure represents the C++ parser. */
1166
1167typedef struct cp_parser GTY(())
1168{
1169 /* The lexer from which we are obtaining tokens. */
1170 cp_lexer *lexer;
1171
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1176
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1184 tree scope;
1185
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1191 tree object_scope;
1192 tree qualifying_scope;
1193
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1196
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context *context;
1204
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p;
1208
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p;
1213
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1216 them permissible. */
a723baf1
MM
1217 bool default_arg_ok_p;
1218
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
67c03833 1221 bool integral_constant_expression_p;
a723baf1 1222
14d22dd6
MM
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
67c03833 1227 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1228
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
67c03833 1231 bool non_integral_constant_expression_p;
14d22dd6 1232
263ee052
MM
1233 /* TRUE if we are parsing the argument to "__offsetof__". */
1234 bool in_offsetof_p;
1235
a723baf1
MM
1236 /* TRUE if local variable names and `this' are forbidden in the
1237 current context. */
1238 bool local_variables_forbidden_p;
1239
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1242 declaration'. */
1243 bool in_unbraced_linkage_specification_p;
1244
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p;
1248
4bb8ca28
MM
1249 /* TRUE if we are presently parsing a template-argument-list. */
1250 bool in_template_argument_list_p;
1251
0e59b3fb
MM
1252 /* TRUE if we are presently parsing the body of an
1253 iteration-statement. */
1254 bool in_iteration_statement_p;
1255
1256 /* TRUE if we are presently parsing the body of a switch
1257 statement. */
1258 bool in_switch_statement_p;
1259
a723baf1
MM
1260 /* If non-NULL, then we are parsing a construct where new type
1261 definitions are not permitted. The string stored here will be
1262 issued as an error message if a type is defined. */
1263 const char *type_definition_forbidden_message;
1264
8db1028e
NS
1265 /* A list of lists. The outer list is a stack, used for member
1266 functions of local classes. At each level there are two sub-list,
1267 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1268 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1269 TREE_VALUE's. The functions are chained in reverse declaration
1270 order.
1271
1272 The TREE_PURPOSE sublist contains those functions with default
1273 arguments that need post processing, and the TREE_VALUE sublist
1274 contains those functions with definitions that need post
1275 processing.
1276
1277 These lists can only be processed once the outermost class being
9bcb9aae 1278 defined is complete. */
a723baf1
MM
1279 tree unparsed_functions_queues;
1280
1281 /* The number of classes whose definitions are currently in
1282 progress. */
1283 unsigned num_classes_being_defined;
1284
1285 /* The number of template parameter lists that apply directly to the
1286 current declaration. */
1287 unsigned num_template_parameter_lists;
1288} cp_parser;
1289
04c06002 1290/* The type of a function that parses some kind of expression. */
94edc4ab 1291typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1292
1293/* Prototypes. */
1294
1295/* Constructors and destructors. */
1296
1297static cp_parser *cp_parser_new
94edc4ab 1298 (void);
a723baf1
MM
1299
1300/* Routines to parse various constructs.
1301
1302 Those that return `tree' will return the error_mark_node (rather
1303 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1304 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1305 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1306 whether or not a parse error occurred, you should always use
1307 cp_parser_error_occurred. If the construct is optional (indicated
1308 either by an `_opt' in the name of the function that does the
1309 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1310 the construct is not present. */
1311
1312/* Lexical conventions [gram.lex] */
1313
1314static tree cp_parser_identifier
94edc4ab 1315 (cp_parser *);
a723baf1
MM
1316
1317/* Basic concepts [gram.basic] */
1318
1319static bool cp_parser_translation_unit
94edc4ab 1320 (cp_parser *);
a723baf1
MM
1321
1322/* Expressions [gram.expr] */
1323
1324static tree cp_parser_primary_expression
b3445994 1325 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1326static tree cp_parser_id_expression
f3c2dfc6 1327 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1328static tree cp_parser_unqualified_id
f3c2dfc6 1329 (cp_parser *, bool, bool, bool);
a723baf1 1330static tree cp_parser_nested_name_specifier_opt
a668c6ad 1331 (cp_parser *, bool, bool, bool, bool);
a723baf1 1332static tree cp_parser_nested_name_specifier
a723baf1 1333 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1334static tree cp_parser_class_or_namespace_name
1335 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1336static tree cp_parser_postfix_expression
1337 (cp_parser *, bool);
7efa3e22 1338static tree cp_parser_parenthesized_expression_list
39703eb9 1339 (cp_parser *, bool, bool *);
a723baf1 1340static void cp_parser_pseudo_destructor_name
94edc4ab 1341 (cp_parser *, tree *, tree *);
a723baf1
MM
1342static tree cp_parser_unary_expression
1343 (cp_parser *, bool);
1344static enum tree_code cp_parser_unary_operator
94edc4ab 1345 (cp_token *);
a723baf1 1346static tree cp_parser_new_expression
94edc4ab 1347 (cp_parser *);
a723baf1 1348static tree cp_parser_new_placement
94edc4ab 1349 (cp_parser *);
a723baf1 1350static tree cp_parser_new_type_id
94edc4ab 1351 (cp_parser *);
a723baf1 1352static tree cp_parser_new_declarator_opt
94edc4ab 1353 (cp_parser *);
a723baf1 1354static tree cp_parser_direct_new_declarator
94edc4ab 1355 (cp_parser *);
a723baf1 1356static tree cp_parser_new_initializer
94edc4ab 1357 (cp_parser *);
a723baf1 1358static tree cp_parser_delete_expression
94edc4ab 1359 (cp_parser *);
a723baf1
MM
1360static tree cp_parser_cast_expression
1361 (cp_parser *, bool);
1362static tree cp_parser_pm_expression
94edc4ab 1363 (cp_parser *);
a723baf1 1364static tree cp_parser_multiplicative_expression
94edc4ab 1365 (cp_parser *);
a723baf1 1366static tree cp_parser_additive_expression
94edc4ab 1367 (cp_parser *);
a723baf1 1368static tree cp_parser_shift_expression
94edc4ab 1369 (cp_parser *);
a723baf1 1370static tree cp_parser_relational_expression
94edc4ab 1371 (cp_parser *);
a723baf1 1372static tree cp_parser_equality_expression
94edc4ab 1373 (cp_parser *);
a723baf1 1374static tree cp_parser_and_expression
94edc4ab 1375 (cp_parser *);
a723baf1 1376static tree cp_parser_exclusive_or_expression
94edc4ab 1377 (cp_parser *);
a723baf1 1378static tree cp_parser_inclusive_or_expression
94edc4ab 1379 (cp_parser *);
a723baf1 1380static tree cp_parser_logical_and_expression
94edc4ab 1381 (cp_parser *);
a723baf1 1382static tree cp_parser_logical_or_expression
94edc4ab 1383 (cp_parser *);
a723baf1 1384static tree cp_parser_question_colon_clause
94edc4ab 1385 (cp_parser *, tree);
a723baf1 1386static tree cp_parser_assignment_expression
94edc4ab 1387 (cp_parser *);
a723baf1 1388static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1389 (cp_parser *);
a723baf1 1390static tree cp_parser_expression
94edc4ab 1391 (cp_parser *);
a723baf1 1392static tree cp_parser_constant_expression
14d22dd6 1393 (cp_parser *, bool, bool *);
a723baf1
MM
1394
1395/* Statements [gram.stmt.stmt] */
1396
1397static void cp_parser_statement
a5bcc582 1398 (cp_parser *, bool);
a723baf1 1399static tree cp_parser_labeled_statement
a5bcc582 1400 (cp_parser *, bool);
a723baf1 1401static tree cp_parser_expression_statement
a5bcc582 1402 (cp_parser *, bool);
a723baf1 1403static tree cp_parser_compound_statement
a5bcc582 1404 (cp_parser *, bool);
a723baf1 1405static void cp_parser_statement_seq_opt
a5bcc582 1406 (cp_parser *, bool);
a723baf1 1407static tree cp_parser_selection_statement
94edc4ab 1408 (cp_parser *);
a723baf1 1409static tree cp_parser_condition
94edc4ab 1410 (cp_parser *);
a723baf1 1411static tree cp_parser_iteration_statement
94edc4ab 1412 (cp_parser *);
a723baf1 1413static void cp_parser_for_init_statement
94edc4ab 1414 (cp_parser *);
a723baf1 1415static tree cp_parser_jump_statement
94edc4ab 1416 (cp_parser *);
a723baf1 1417static void cp_parser_declaration_statement
94edc4ab 1418 (cp_parser *);
a723baf1
MM
1419
1420static tree cp_parser_implicitly_scoped_statement
94edc4ab 1421 (cp_parser *);
a723baf1 1422static void cp_parser_already_scoped_statement
94edc4ab 1423 (cp_parser *);
a723baf1
MM
1424
1425/* Declarations [gram.dcl.dcl] */
1426
1427static void cp_parser_declaration_seq_opt
94edc4ab 1428 (cp_parser *);
a723baf1 1429static void cp_parser_declaration
94edc4ab 1430 (cp_parser *);
a723baf1 1431static void cp_parser_block_declaration
94edc4ab 1432 (cp_parser *, bool);
a723baf1 1433static void cp_parser_simple_declaration
94edc4ab 1434 (cp_parser *, bool);
a723baf1 1435static tree cp_parser_decl_specifier_seq
560ad596 1436 (cp_parser *, cp_parser_flags, tree *, int *);
a723baf1 1437static tree cp_parser_storage_class_specifier_opt
94edc4ab 1438 (cp_parser *);
a723baf1 1439static tree cp_parser_function_specifier_opt
94edc4ab 1440 (cp_parser *);
a723baf1 1441static tree cp_parser_type_specifier
560ad596 1442 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
a723baf1 1443static tree cp_parser_simple_type_specifier
4b0d3cbe 1444 (cp_parser *, cp_parser_flags, bool);
a723baf1 1445static tree cp_parser_type_name
94edc4ab 1446 (cp_parser *);
a723baf1 1447static tree cp_parser_elaborated_type_specifier
94edc4ab 1448 (cp_parser *, bool, bool);
a723baf1 1449static tree cp_parser_enum_specifier
94edc4ab 1450 (cp_parser *);
a723baf1 1451static void cp_parser_enumerator_list
94edc4ab 1452 (cp_parser *, tree);
a723baf1 1453static void cp_parser_enumerator_definition
94edc4ab 1454 (cp_parser *, tree);
a723baf1 1455static tree cp_parser_namespace_name
94edc4ab 1456 (cp_parser *);
a723baf1 1457static void cp_parser_namespace_definition
94edc4ab 1458 (cp_parser *);
a723baf1 1459static void cp_parser_namespace_body
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_qualified_namespace_specifier
94edc4ab 1462 (cp_parser *);
a723baf1 1463static void cp_parser_namespace_alias_definition
94edc4ab 1464 (cp_parser *);
a723baf1 1465static void cp_parser_using_declaration
94edc4ab 1466 (cp_parser *);
a723baf1 1467static void cp_parser_using_directive
94edc4ab 1468 (cp_parser *);
a723baf1 1469static void cp_parser_asm_definition
94edc4ab 1470 (cp_parser *);
a723baf1 1471static void cp_parser_linkage_specification
94edc4ab 1472 (cp_parser *);
a723baf1
MM
1473
1474/* Declarators [gram.dcl.decl] */
1475
1476static tree cp_parser_init_declarator
560ad596 1477 (cp_parser *, tree, tree, bool, bool, int, bool *);
a723baf1 1478static tree cp_parser_declarator
4bb8ca28 1479 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
a723baf1 1480static tree cp_parser_direct_declarator
7efa3e22 1481 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1482static enum tree_code cp_parser_ptr_operator
94edc4ab 1483 (cp_parser *, tree *, tree *);
a723baf1 1484static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1485 (cp_parser *);
a723baf1 1486static tree cp_parser_cv_qualifier_opt
94edc4ab 1487 (cp_parser *);
a723baf1 1488static tree cp_parser_declarator_id
94edc4ab 1489 (cp_parser *);
a723baf1 1490static tree cp_parser_type_id
94edc4ab 1491 (cp_parser *);
a723baf1 1492static tree cp_parser_type_specifier_seq
94edc4ab 1493 (cp_parser *);
a723baf1 1494static tree cp_parser_parameter_declaration_clause
94edc4ab 1495 (cp_parser *);
a723baf1 1496static tree cp_parser_parameter_declaration_list
94edc4ab 1497 (cp_parser *);
a723baf1 1498static tree cp_parser_parameter_declaration
4bb8ca28 1499 (cp_parser *, bool, bool *);
a723baf1
MM
1500static void cp_parser_function_body
1501 (cp_parser *);
1502static tree cp_parser_initializer
39703eb9 1503 (cp_parser *, bool *, bool *);
a723baf1 1504static tree cp_parser_initializer_clause
39703eb9 1505 (cp_parser *, bool *);
a723baf1 1506static tree cp_parser_initializer_list
39703eb9 1507 (cp_parser *, bool *);
a723baf1
MM
1508
1509static bool cp_parser_ctor_initializer_opt_and_function_body
1510 (cp_parser *);
1511
1512/* Classes [gram.class] */
1513
1514static tree cp_parser_class_name
a668c6ad 1515 (cp_parser *, bool, bool, bool, bool, bool, bool);
a723baf1 1516static tree cp_parser_class_specifier
94edc4ab 1517 (cp_parser *);
a723baf1 1518static tree cp_parser_class_head
94edc4ab 1519 (cp_parser *, bool *);
a723baf1 1520static enum tag_types cp_parser_class_key
94edc4ab 1521 (cp_parser *);
a723baf1 1522static void cp_parser_member_specification_opt
94edc4ab 1523 (cp_parser *);
a723baf1 1524static void cp_parser_member_declaration
94edc4ab 1525 (cp_parser *);
a723baf1 1526static tree cp_parser_pure_specifier
94edc4ab 1527 (cp_parser *);
a723baf1 1528static tree cp_parser_constant_initializer
94edc4ab 1529 (cp_parser *);
a723baf1
MM
1530
1531/* Derived classes [gram.class.derived] */
1532
1533static tree cp_parser_base_clause
94edc4ab 1534 (cp_parser *);
a723baf1 1535static tree cp_parser_base_specifier
94edc4ab 1536 (cp_parser *);
a723baf1
MM
1537
1538/* Special member functions [gram.special] */
1539
1540static tree cp_parser_conversion_function_id
94edc4ab 1541 (cp_parser *);
a723baf1 1542static tree cp_parser_conversion_type_id
94edc4ab 1543 (cp_parser *);
a723baf1 1544static tree cp_parser_conversion_declarator_opt
94edc4ab 1545 (cp_parser *);
a723baf1 1546static bool cp_parser_ctor_initializer_opt
94edc4ab 1547 (cp_parser *);
a723baf1 1548static void cp_parser_mem_initializer_list
94edc4ab 1549 (cp_parser *);
a723baf1 1550static tree cp_parser_mem_initializer
94edc4ab 1551 (cp_parser *);
a723baf1 1552static tree cp_parser_mem_initializer_id
94edc4ab 1553 (cp_parser *);
a723baf1
MM
1554
1555/* Overloading [gram.over] */
1556
1557static tree cp_parser_operator_function_id
94edc4ab 1558 (cp_parser *);
a723baf1 1559static tree cp_parser_operator
94edc4ab 1560 (cp_parser *);
a723baf1
MM
1561
1562/* Templates [gram.temp] */
1563
1564static void cp_parser_template_declaration
94edc4ab 1565 (cp_parser *, bool);
a723baf1 1566static tree cp_parser_template_parameter_list
94edc4ab 1567 (cp_parser *);
a723baf1 1568static tree cp_parser_template_parameter
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_type_parameter
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_template_id
a668c6ad 1573 (cp_parser *, bool, bool, bool);
a723baf1 1574static tree cp_parser_template_name
a668c6ad 1575 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1576static tree cp_parser_template_argument_list
94edc4ab 1577 (cp_parser *);
a723baf1 1578static tree cp_parser_template_argument
94edc4ab 1579 (cp_parser *);
a723baf1 1580static void cp_parser_explicit_instantiation
94edc4ab 1581 (cp_parser *);
a723baf1 1582static void cp_parser_explicit_specialization
94edc4ab 1583 (cp_parser *);
a723baf1
MM
1584
1585/* Exception handling [gram.exception] */
1586
1587static tree cp_parser_try_block
94edc4ab 1588 (cp_parser *);
a723baf1 1589static bool cp_parser_function_try_block
94edc4ab 1590 (cp_parser *);
a723baf1 1591static void cp_parser_handler_seq
94edc4ab 1592 (cp_parser *);
a723baf1 1593static void cp_parser_handler
94edc4ab 1594 (cp_parser *);
a723baf1 1595static tree cp_parser_exception_declaration
94edc4ab 1596 (cp_parser *);
a723baf1 1597static tree cp_parser_throw_expression
94edc4ab 1598 (cp_parser *);
a723baf1 1599static tree cp_parser_exception_specification_opt
94edc4ab 1600 (cp_parser *);
a723baf1 1601static tree cp_parser_type_id_list
94edc4ab 1602 (cp_parser *);
a723baf1
MM
1603
1604/* GNU Extensions */
1605
1606static tree cp_parser_asm_specification_opt
94edc4ab 1607 (cp_parser *);
a723baf1 1608static tree cp_parser_asm_operand_list
94edc4ab 1609 (cp_parser *);
a723baf1 1610static tree cp_parser_asm_clobber_list
94edc4ab 1611 (cp_parser *);
a723baf1 1612static tree cp_parser_attributes_opt
94edc4ab 1613 (cp_parser *);
a723baf1 1614static tree cp_parser_attribute_list
94edc4ab 1615 (cp_parser *);
a723baf1 1616static bool cp_parser_extension_opt
94edc4ab 1617 (cp_parser *, int *);
a723baf1 1618static void cp_parser_label_declaration
94edc4ab 1619 (cp_parser *);
a723baf1
MM
1620
1621/* Utility Routines */
1622
1623static tree cp_parser_lookup_name
8d241e0b 1624 (cp_parser *, tree, bool, bool, bool);
a723baf1 1625static tree cp_parser_lookup_name_simple
94edc4ab 1626 (cp_parser *, tree);
a723baf1
MM
1627static tree cp_parser_maybe_treat_template_as_class
1628 (tree, bool);
1629static bool cp_parser_check_declarator_template_parameters
94edc4ab 1630 (cp_parser *, tree);
a723baf1 1631static bool cp_parser_check_template_parameters
94edc4ab 1632 (cp_parser *, unsigned);
d6b4ea85
MM
1633static tree cp_parser_simple_cast_expression
1634 (cp_parser *);
a723baf1 1635static tree cp_parser_binary_expression
94edc4ab 1636 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1637static tree cp_parser_global_scope_opt
94edc4ab 1638 (cp_parser *, bool);
a723baf1
MM
1639static bool cp_parser_constructor_declarator_p
1640 (cp_parser *, bool);
1641static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1642 (cp_parser *, tree, tree, tree);
a723baf1 1643static tree cp_parser_function_definition_after_declarator
94edc4ab 1644 (cp_parser *, bool);
a723baf1 1645static void cp_parser_template_declaration_after_export
94edc4ab 1646 (cp_parser *, bool);
a723baf1 1647static tree cp_parser_single_declaration
94edc4ab 1648 (cp_parser *, bool, bool *);
a723baf1 1649static tree cp_parser_functional_cast
94edc4ab 1650 (cp_parser *, tree);
4bb8ca28
MM
1651static tree cp_parser_save_member_function_body
1652 (cp_parser *, tree, tree, tree);
ec75414f
MM
1653static tree cp_parser_enclosed_template_argument_list
1654 (cp_parser *);
8db1028e
NS
1655static void cp_parser_save_default_args
1656 (cp_parser *, tree);
a723baf1 1657static void cp_parser_late_parsing_for_member
94edc4ab 1658 (cp_parser *, tree);
a723baf1 1659static void cp_parser_late_parsing_default_args
8218bd34 1660 (cp_parser *, tree);
a723baf1 1661static tree cp_parser_sizeof_operand
94edc4ab 1662 (cp_parser *, enum rid);
a723baf1 1663static bool cp_parser_declares_only_class_p
94edc4ab 1664 (cp_parser *);
d17811fd
MM
1665static tree cp_parser_fold_non_dependent_expr
1666 (tree);
a723baf1 1667static bool cp_parser_friend_p
94edc4ab 1668 (tree);
a723baf1 1669static cp_token *cp_parser_require
94edc4ab 1670 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1671static cp_token *cp_parser_require_keyword
94edc4ab 1672 (cp_parser *, enum rid, const char *);
a723baf1 1673static bool cp_parser_token_starts_function_definition_p
94edc4ab 1674 (cp_token *);
a723baf1
MM
1675static bool cp_parser_next_token_starts_class_definition_p
1676 (cp_parser *);
d17811fd
MM
1677static bool cp_parser_next_token_ends_template_argument_p
1678 (cp_parser *);
a723baf1 1679static enum tag_types cp_parser_token_is_class_key
94edc4ab 1680 (cp_token *);
a723baf1
MM
1681static void cp_parser_check_class_key
1682 (enum tag_types, tree type);
37d407a1
KL
1683static void cp_parser_check_access_in_redeclaration
1684 (tree type);
a723baf1
MM
1685static bool cp_parser_optional_template_keyword
1686 (cp_parser *);
2050a1bb
MM
1687static void cp_parser_pre_parsed_nested_name_specifier
1688 (cp_parser *);
a723baf1
MM
1689static void cp_parser_cache_group
1690 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1691static void cp_parser_parse_tentatively
94edc4ab 1692 (cp_parser *);
a723baf1 1693static void cp_parser_commit_to_tentative_parse
94edc4ab 1694 (cp_parser *);
a723baf1 1695static void cp_parser_abort_tentative_parse
94edc4ab 1696 (cp_parser *);
a723baf1 1697static bool cp_parser_parse_definitely
94edc4ab 1698 (cp_parser *);
f7b5ecd9 1699static inline bool cp_parser_parsing_tentatively
94edc4ab 1700 (cp_parser *);
a723baf1 1701static bool cp_parser_committed_to_tentative_parse
94edc4ab 1702 (cp_parser *);
a723baf1 1703static void cp_parser_error
94edc4ab 1704 (cp_parser *, const char *);
4bb8ca28
MM
1705static void cp_parser_name_lookup_error
1706 (cp_parser *, tree, tree, const char *);
e5976695 1707static bool cp_parser_simulate_error
94edc4ab 1708 (cp_parser *);
a723baf1 1709static void cp_parser_check_type_definition
94edc4ab 1710 (cp_parser *);
560ad596
MM
1711static void cp_parser_check_for_definition_in_return_type
1712 (tree, int);
ee43dab5
MM
1713static void cp_parser_check_for_invalid_template_id
1714 (cp_parser *, tree);
67c03833 1715static tree cp_parser_non_integral_constant_expression
14d22dd6 1716 (const char *);
8fbc5ae7
MM
1717static bool cp_parser_diagnose_invalid_type_name
1718 (cp_parser *);
7efa3e22 1719static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1720 (cp_parser *, bool, bool, bool);
a723baf1 1721static void cp_parser_skip_to_end_of_statement
94edc4ab 1722 (cp_parser *);
e0860732
MM
1723static void cp_parser_consume_semicolon_at_end_of_statement
1724 (cp_parser *);
a723baf1 1725static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1726 (cp_parser *);
a723baf1
MM
1727static void cp_parser_skip_to_closing_brace
1728 (cp_parser *);
1729static void cp_parser_skip_until_found
94edc4ab 1730 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1731static bool cp_parser_error_occurred
94edc4ab 1732 (cp_parser *);
a723baf1 1733static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1734 (cp_parser *);
a723baf1 1735static bool cp_parser_is_string_literal
94edc4ab 1736 (cp_token *);
a723baf1 1737static bool cp_parser_is_keyword
94edc4ab 1738 (cp_token *, enum rid);
a723baf1 1739
4de8668e 1740/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1741
1742static inline bool
94edc4ab 1743cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1744{
1745 return parser->context->next != NULL;
1746}
1747
4de8668e 1748/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1749
1750static bool
94edc4ab 1751cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1752{
1753 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1754}
1755
4de8668e 1756/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1757
1758static bool
94edc4ab 1759cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1760{
1761 return token->keyword == keyword;
1762}
1763
a723baf1
MM
1764/* Issue the indicated error MESSAGE. */
1765
1766static void
94edc4ab 1767cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1768{
a723baf1 1769 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1770 if (!cp_parser_simulate_error (parser))
4bb8ca28
MM
1771 {
1772 cp_token *token;
1773 token = cp_lexer_peek_token (parser->lexer);
5c832178
MM
1774 c_parse_error (message,
1775 /* Because c_parser_error does not understand
1776 CPP_KEYWORD, keywords are treated like
1777 identifiers. */
1778 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1779 token->value);
4bb8ca28
MM
1780 }
1781}
1782
1783/* Issue an error about name-lookup failing. NAME is the
1784 IDENTIFIER_NODE DECL is the result of
1785 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1786 the thing that we hoped to find. */
1787
1788static void
1789cp_parser_name_lookup_error (cp_parser* parser,
1790 tree name,
1791 tree decl,
1792 const char* desired)
1793{
1794 /* If name lookup completely failed, tell the user that NAME was not
1795 declared. */
1796 if (decl == error_mark_node)
1797 {
1798 if (parser->scope && parser->scope != global_namespace)
1799 error ("`%D::%D' has not been declared",
1800 parser->scope, name);
1801 else if (parser->scope == global_namespace)
1802 error ("`::%D' has not been declared", name);
1803 else
1804 error ("`%D' has not been declared", name);
1805 }
1806 else if (parser->scope && parser->scope != global_namespace)
1807 error ("`%D::%D' %s", parser->scope, name, desired);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' %s", name, desired);
1810 else
1811 error ("`%D' %s", name, desired);
a723baf1
MM
1812}
1813
1814/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1815 during this tentative parse. Returns true if the error was
1816 simulated; false if a messgae should be issued by the caller. */
a723baf1 1817
e5976695 1818static bool
94edc4ab 1819cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1820{
1821 if (cp_parser_parsing_tentatively (parser)
1822 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1823 {
1824 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1825 return true;
1826 }
1827 return false;
a723baf1
MM
1828}
1829
1830/* This function is called when a type is defined. If type
1831 definitions are forbidden at this point, an error message is
1832 issued. */
1833
1834static void
94edc4ab 1835cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1836{
1837 /* If types are forbidden here, issue a message. */
1838 if (parser->type_definition_forbidden_message)
1839 /* Use `%s' to print the string in case there are any escape
1840 characters in the message. */
1841 error ("%s", parser->type_definition_forbidden_message);
1842}
1843
560ad596
MM
1844/* This function is called when a declaration is parsed. If
1845 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1846 indicates that a type was defined in the decl-specifiers for DECL,
1847 then an error is issued. */
1848
1849static void
1850cp_parser_check_for_definition_in_return_type (tree declarator,
1851 int declares_class_or_enum)
1852{
1853 /* [dcl.fct] forbids type definitions in return types.
1854 Unfortunately, it's not easy to know whether or not we are
1855 processing a return type until after the fact. */
1856 while (declarator
1857 && (TREE_CODE (declarator) == INDIRECT_REF
1858 || TREE_CODE (declarator) == ADDR_EXPR))
1859 declarator = TREE_OPERAND (declarator, 0);
1860 if (declarator
1861 && TREE_CODE (declarator) == CALL_EXPR
1862 && declares_class_or_enum & 2)
1863 error ("new types may not be defined in a return type");
1864}
1865
ee43dab5
MM
1866/* A type-specifier (TYPE) has been parsed which cannot be followed by
1867 "<" in any valid C++ program. If the next token is indeed "<",
1868 issue a message warning the user about what appears to be an
1869 invalid attempt to form a template-id. */
1870
1871static void
1872cp_parser_check_for_invalid_template_id (cp_parser* parser,
1873 tree type)
1874{
1875 ptrdiff_t start;
1876 cp_token *token;
1877
1878 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1879 {
1880 if (TYPE_P (type))
1881 error ("`%T' is not a template", type);
1882 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1883 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1884 else
1885 error ("invalid template-id");
1886 /* Remember the location of the invalid "<". */
1887 if (cp_parser_parsing_tentatively (parser)
1888 && !cp_parser_committed_to_tentative_parse (parser))
1889 {
1890 token = cp_lexer_peek_token (parser->lexer);
1891 token = cp_lexer_prev_token (parser->lexer, token);
1892 start = cp_lexer_token_difference (parser->lexer,
1893 parser->lexer->first_token,
1894 token);
1895 }
1896 else
1897 start = -1;
1898 /* Consume the "<". */
1899 cp_lexer_consume_token (parser->lexer);
1900 /* Parse the template arguments. */
1901 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1902 /* Permanently remove the invalid template arguments so that
ee43dab5
MM
1903 this error message is not issued again. */
1904 if (start >= 0)
1905 {
1906 token = cp_lexer_advance_token (parser->lexer,
1907 parser->lexer->first_token,
1908 start);
1909 cp_lexer_purge_tokens_after (parser->lexer, token);
1910 }
1911 }
1912}
1913
cd0be382 1914/* Issue an error message about the fact that THING appeared in a
14d22dd6
MM
1915 constant-expression. Returns ERROR_MARK_NODE. */
1916
1917static tree
67c03833 1918cp_parser_non_integral_constant_expression (const char *thing)
14d22dd6
MM
1919{
1920 error ("%s cannot appear in a constant-expression", thing);
1921 return error_mark_node;
1922}
1923
8fbc5ae7
MM
1924/* Check for a common situation where a type-name should be present,
1925 but is not, and issue a sensible error message. Returns true if an
1926 invalid type-name was detected. */
1927
1928static bool
1929cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1930{
1931 /* If the next two tokens are both identifiers, the code is
1932 erroneous. The usual cause of this situation is code like:
1933
1934 T t;
1935
1936 where "T" should name a type -- but does not. */
1937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1938 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1939 {
1940 tree name;
1941
8d241e0b 1942 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1943 looking at a declaration. */
1944 /* Consume the first identifier. */
1945 name = cp_lexer_consume_token (parser->lexer)->value;
1946 /* Issue an error message. */
1947 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1948 /* If we're in a template class, it's possible that the user was
1949 referring to a type from a base class. For example:
1950
1951 template <typename T> struct A { typedef T X; };
1952 template <typename T> struct B : public A<T> { X x; };
1953
1954 The user should have said "typename A<T>::X". */
1955 if (processing_template_decl && current_class_type)
1956 {
1957 tree b;
1958
1959 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1960 b;
1961 b = TREE_CHAIN (b))
1962 {
1963 tree base_type = BINFO_TYPE (b);
1964 if (CLASS_TYPE_P (base_type)
1fb3244a 1965 && dependent_type_p (base_type))
8fbc5ae7
MM
1966 {
1967 tree field;
1968 /* Go from a particular instantiation of the
1969 template (which will have an empty TYPE_FIELDs),
1970 to the main version. */
353b4fc0 1971 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1972 for (field = TYPE_FIELDS (base_type);
1973 field;
1974 field = TREE_CHAIN (field))
1975 if (TREE_CODE (field) == TYPE_DECL
1976 && DECL_NAME (field) == name)
1977 {
1978 error ("(perhaps `typename %T::%s' was intended)",
1979 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1980 break;
1981 }
1982 if (field)
1983 break;
1984 }
1985 }
1986 }
1987 /* Skip to the end of the declaration; there's no point in
1988 trying to process it. */
1989 cp_parser_skip_to_end_of_statement (parser);
1990
1991 return true;
1992 }
1993
1994 return false;
1995}
1996
a723baf1 1997/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
1998 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1999 are doing error recovery. Returns -1 if OR_COMMA is true and we
2000 found an unnested comma. */
a723baf1 2001
7efa3e22
NS
2002static int
2003cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
a668c6ad
MM
2004 bool recovering,
2005 bool or_comma,
2006 bool consume_paren)
a723baf1 2007{
7efa3e22
NS
2008 unsigned paren_depth = 0;
2009 unsigned brace_depth = 0;
a723baf1 2010
7efa3e22
NS
2011 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2012 && !cp_parser_committed_to_tentative_parse (parser))
2013 return 0;
2014
a723baf1
MM
2015 while (true)
2016 {
2017 cp_token *token;
7efa3e22 2018
a723baf1
MM
2019 /* If we've run out of tokens, then there is no closing `)'. */
2020 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 2021 return 0;
a723baf1 2022
a668c6ad
MM
2023 token = cp_lexer_peek_token (parser->lexer);
2024
f4f206f4 2025 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad
MM
2026 if (token->type == CPP_SEMICOLON && !brace_depth)
2027 return 0;
2028 if (token->type == CPP_OPEN_BRACE)
2029 ++brace_depth;
2030 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2031 {
a668c6ad 2032 if (!brace_depth--)
7efa3e22 2033 return 0;
7efa3e22 2034 }
a668c6ad
MM
2035 if (recovering && or_comma && token->type == CPP_COMMA
2036 && !brace_depth && !paren_depth)
2037 return -1;
7efa3e22 2038
7efa3e22
NS
2039 if (!brace_depth)
2040 {
2041 /* If it is an `(', we have entered another level of nesting. */
2042 if (token->type == CPP_OPEN_PAREN)
2043 ++paren_depth;
2044 /* If it is a `)', then we might be done. */
2045 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2046 {
2047 if (consume_paren)
2048 cp_lexer_consume_token (parser->lexer);
2049 return 1;
2050 }
7efa3e22 2051 }
a668c6ad
MM
2052
2053 /* Consume the token. */
2054 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2055 }
2056}
2057
2058/* Consume tokens until we reach the end of the current statement.
2059 Normally, that will be just before consuming a `;'. However, if a
2060 non-nested `}' comes first, then we stop before consuming that. */
2061
2062static void
94edc4ab 2063cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2064{
2065 unsigned nesting_depth = 0;
2066
2067 while (true)
2068 {
2069 cp_token *token;
2070
2071 /* Peek at the next token. */
2072 token = cp_lexer_peek_token (parser->lexer);
2073 /* If we've run out of tokens, stop. */
2074 if (token->type == CPP_EOF)
2075 break;
2076 /* If the next token is a `;', we have reached the end of the
2077 statement. */
2078 if (token->type == CPP_SEMICOLON && !nesting_depth)
2079 break;
2080 /* If the next token is a non-nested `}', then we have reached
2081 the end of the current block. */
2082 if (token->type == CPP_CLOSE_BRACE)
2083 {
2084 /* If this is a non-nested `}', stop before consuming it.
2085 That way, when confronted with something like:
2086
2087 { 3 + }
2088
2089 we stop before consuming the closing `}', even though we
2090 have not yet reached a `;'. */
2091 if (nesting_depth == 0)
2092 break;
2093 /* If it is the closing `}' for a block that we have
2094 scanned, stop -- but only after consuming the token.
2095 That way given:
2096
2097 void f g () { ... }
2098 typedef int I;
2099
2100 we will stop after the body of the erroneously declared
2101 function, but before consuming the following `typedef'
2102 declaration. */
2103 if (--nesting_depth == 0)
2104 {
2105 cp_lexer_consume_token (parser->lexer);
2106 break;
2107 }
2108 }
2109 /* If it the next token is a `{', then we are entering a new
2110 block. Consume the entire block. */
2111 else if (token->type == CPP_OPEN_BRACE)
2112 ++nesting_depth;
2113 /* Consume the token. */
2114 cp_lexer_consume_token (parser->lexer);
2115 }
2116}
2117
e0860732
MM
2118/* This function is called at the end of a statement or declaration.
2119 If the next token is a semicolon, it is consumed; otherwise, error
2120 recovery is attempted. */
2121
2122static void
2123cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2124{
2125 /* Look for the trailing `;'. */
2126 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2127 {
2128 /* If there is additional (erroneous) input, skip to the end of
2129 the statement. */
2130 cp_parser_skip_to_end_of_statement (parser);
2131 /* If the next token is now a `;', consume it. */
2132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2133 cp_lexer_consume_token (parser->lexer);
2134 }
2135}
2136
a723baf1
MM
2137/* Skip tokens until we have consumed an entire block, or until we
2138 have consumed a non-nested `;'. */
2139
2140static void
94edc4ab 2141cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2142{
2143 unsigned nesting_depth = 0;
2144
2145 while (true)
2146 {
2147 cp_token *token;
2148
2149 /* Peek at the next token. */
2150 token = cp_lexer_peek_token (parser->lexer);
2151 /* If we've run out of tokens, stop. */
2152 if (token->type == CPP_EOF)
2153 break;
2154 /* If the next token is a `;', we have reached the end of the
2155 statement. */
2156 if (token->type == CPP_SEMICOLON && !nesting_depth)
2157 {
2158 /* Consume the `;'. */
2159 cp_lexer_consume_token (parser->lexer);
2160 break;
2161 }
2162 /* Consume the token. */
2163 token = cp_lexer_consume_token (parser->lexer);
2164 /* If the next token is a non-nested `}', then we have reached
2165 the end of the current block. */
2166 if (token->type == CPP_CLOSE_BRACE
2167 && (nesting_depth == 0 || --nesting_depth == 0))
2168 break;
2169 /* If it the next token is a `{', then we are entering a new
2170 block. Consume the entire block. */
2171 if (token->type == CPP_OPEN_BRACE)
2172 ++nesting_depth;
2173 }
2174}
2175
2176/* Skip tokens until a non-nested closing curly brace is the next
2177 token. */
2178
2179static void
2180cp_parser_skip_to_closing_brace (cp_parser *parser)
2181{
2182 unsigned nesting_depth = 0;
2183
2184 while (true)
2185 {
2186 cp_token *token;
2187
2188 /* Peek at the next token. */
2189 token = cp_lexer_peek_token (parser->lexer);
2190 /* If we've run out of tokens, stop. */
2191 if (token->type == CPP_EOF)
2192 break;
2193 /* If the next token is a non-nested `}', then we have reached
2194 the end of the current block. */
2195 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2196 break;
2197 /* If it the next token is a `{', then we are entering a new
2198 block. Consume the entire block. */
2199 else if (token->type == CPP_OPEN_BRACE)
2200 ++nesting_depth;
2201 /* Consume the token. */
2202 cp_lexer_consume_token (parser->lexer);
2203 }
2204}
2205
2206/* Create a new C++ parser. */
2207
2208static cp_parser *
94edc4ab 2209cp_parser_new (void)
a723baf1
MM
2210{
2211 cp_parser *parser;
17211ab5
GK
2212 cp_lexer *lexer;
2213
2214 /* cp_lexer_new_main is called before calling ggc_alloc because
2215 cp_lexer_new_main might load a PCH file. */
2216 lexer = cp_lexer_new_main ();
a723baf1 2217
c68b0a84 2218 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2219 parser->lexer = lexer;
a723baf1
MM
2220 parser->context = cp_parser_context_new (NULL);
2221
2222 /* For now, we always accept GNU extensions. */
2223 parser->allow_gnu_extensions_p = 1;
2224
2225 /* The `>' token is a greater-than operator, not the end of a
2226 template-id. */
2227 parser->greater_than_is_operator_p = true;
2228
2229 parser->default_arg_ok_p = true;
2230
2231 /* We are not parsing a constant-expression. */
67c03833
JM
2232 parser->integral_constant_expression_p = false;
2233 parser->allow_non_integral_constant_expression_p = false;
2234 parser->non_integral_constant_expression_p = false;
a723baf1 2235
263ee052
MM
2236 /* We are not parsing offsetof. */
2237 parser->in_offsetof_p = false;
2238
a723baf1
MM
2239 /* Local variable names are not forbidden. */
2240 parser->local_variables_forbidden_p = false;
2241
34cd5ae7 2242 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2243 parser->in_unbraced_linkage_specification_p = false;
2244
2245 /* We are not processing a declarator. */
2246 parser->in_declarator_p = false;
2247
4bb8ca28
MM
2248 /* We are not processing a template-argument-list. */
2249 parser->in_template_argument_list_p = false;
2250
0e59b3fb
MM
2251 /* We are not in an iteration statement. */
2252 parser->in_iteration_statement_p = false;
2253
2254 /* We are not in a switch statement. */
2255 parser->in_switch_statement_p = false;
2256
a723baf1
MM
2257 /* The unparsed function queue is empty. */
2258 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2259
2260 /* There are no classes being defined. */
2261 parser->num_classes_being_defined = 0;
2262
2263 /* No template parameters apply. */
2264 parser->num_template_parameter_lists = 0;
2265
2266 return parser;
2267}
2268
2269/* Lexical conventions [gram.lex] */
2270
2271/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2272 identifier. */
2273
2274static tree
94edc4ab 2275cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2276{
2277 cp_token *token;
2278
2279 /* Look for the identifier. */
2280 token = cp_parser_require (parser, CPP_NAME, "identifier");
2281 /* Return the value. */
2282 return token ? token->value : error_mark_node;
2283}
2284
2285/* Basic concepts [gram.basic] */
2286
2287/* Parse a translation-unit.
2288
2289 translation-unit:
2290 declaration-seq [opt]
2291
2292 Returns TRUE if all went well. */
2293
2294static bool
94edc4ab 2295cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2296{
2297 while (true)
2298 {
2299 cp_parser_declaration_seq_opt (parser);
2300
2301 /* If there are no tokens left then all went well. */
2302 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2303 break;
2304
2305 /* Otherwise, issue an error message. */
2306 cp_parser_error (parser, "expected declaration");
2307 return false;
2308 }
2309
2310 /* Consume the EOF token. */
2311 cp_parser_require (parser, CPP_EOF, "end-of-file");
2312
2313 /* Finish up. */
2314 finish_translation_unit ();
2315
2316 /* All went well. */
2317 return true;
2318}
2319
2320/* Expressions [gram.expr] */
2321
2322/* Parse a primary-expression.
2323
2324 primary-expression:
2325 literal
2326 this
2327 ( expression )
2328 id-expression
2329
2330 GNU Extensions:
2331
2332 primary-expression:
2333 ( compound-statement )
2334 __builtin_va_arg ( assignment-expression , type-id )
2335
2336 literal:
2337 __null
2338
2339 Returns a representation of the expression.
2340
2341 *IDK indicates what kind of id-expression (if any) was present.
2342
2343 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2344 used as the operand of a pointer-to-member. In that case,
2345 *QUALIFYING_CLASS gives the class that is used as the qualifying
2346 class in the pointer-to-member. */
2347
2348static tree
2349cp_parser_primary_expression (cp_parser *parser,
b3445994 2350 cp_id_kind *idk,
a723baf1
MM
2351 tree *qualifying_class)
2352{
2353 cp_token *token;
2354
2355 /* Assume the primary expression is not an id-expression. */
b3445994 2356 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2357 /* And that it cannot be used as pointer-to-member. */
2358 *qualifying_class = NULL_TREE;
2359
2360 /* Peek at the next token. */
2361 token = cp_lexer_peek_token (parser->lexer);
2362 switch (token->type)
2363 {
2364 /* literal:
2365 integer-literal
2366 character-literal
2367 floating-literal
2368 string-literal
2369 boolean-literal */
2370 case CPP_CHAR:
2371 case CPP_WCHAR:
2372 case CPP_STRING:
2373 case CPP_WSTRING:
2374 case CPP_NUMBER:
2375 token = cp_lexer_consume_token (parser->lexer);
2376 return token->value;
2377
2378 case CPP_OPEN_PAREN:
2379 {
2380 tree expr;
2381 bool saved_greater_than_is_operator_p;
2382
2383 /* Consume the `('. */
2384 cp_lexer_consume_token (parser->lexer);
2385 /* Within a parenthesized expression, a `>' token is always
2386 the greater-than operator. */
2387 saved_greater_than_is_operator_p
2388 = parser->greater_than_is_operator_p;
2389 parser->greater_than_is_operator_p = true;
2390 /* If we see `( { ' then we are looking at the beginning of
2391 a GNU statement-expression. */
2392 if (cp_parser_allow_gnu_extensions_p (parser)
2393 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2394 {
2395 /* Statement-expressions are not allowed by the standard. */
2396 if (pedantic)
2397 pedwarn ("ISO C++ forbids braced-groups within expressions");
2398
2399 /* And they're not allowed outside of a function-body; you
2400 cannot, for example, write:
2401
2402 int i = ({ int j = 3; j + 1; });
2403
2404 at class or namespace scope. */
2405 if (!at_function_scope_p ())
2406 error ("statement-expressions are allowed only inside functions");
2407 /* Start the statement-expression. */
2408 expr = begin_stmt_expr ();
2409 /* Parse the compound-statement. */
a5bcc582 2410 cp_parser_compound_statement (parser, true);
a723baf1 2411 /* Finish up. */
303b7406 2412 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2413 }
2414 else
2415 {
2416 /* Parse the parenthesized expression. */
2417 expr = cp_parser_expression (parser);
2418 /* Let the front end know that this expression was
2419 enclosed in parentheses. This matters in case, for
2420 example, the expression is of the form `A::B', since
2421 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2422 not. */
2423 finish_parenthesized_expr (expr);
2424 }
2425 /* The `>' token might be the end of a template-id or
2426 template-parameter-list now. */
2427 parser->greater_than_is_operator_p
2428 = saved_greater_than_is_operator_p;
2429 /* Consume the `)'. */
2430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2431 cp_parser_skip_to_end_of_statement (parser);
2432
2433 return expr;
2434 }
2435
2436 case CPP_KEYWORD:
2437 switch (token->keyword)
2438 {
2439 /* These two are the boolean literals. */
2440 case RID_TRUE:
2441 cp_lexer_consume_token (parser->lexer);
2442 return boolean_true_node;
2443 case RID_FALSE:
2444 cp_lexer_consume_token (parser->lexer);
2445 return boolean_false_node;
2446
2447 /* The `__null' literal. */
2448 case RID_NULL:
2449 cp_lexer_consume_token (parser->lexer);
2450 return null_node;
2451
2452 /* Recognize the `this' keyword. */
2453 case RID_THIS:
2454 cp_lexer_consume_token (parser->lexer);
2455 if (parser->local_variables_forbidden_p)
2456 {
2457 error ("`this' may not be used in this context");
2458 return error_mark_node;
2459 }
14d22dd6 2460 /* Pointers cannot appear in constant-expressions. */
67c03833 2461 if (parser->integral_constant_expression_p)
14d22dd6 2462 {
67c03833
JM
2463 if (!parser->allow_non_integral_constant_expression_p)
2464 return cp_parser_non_integral_constant_expression ("`this'");
2465 parser->non_integral_constant_expression_p = true;
14d22dd6 2466 }
a723baf1
MM
2467 return finish_this_expr ();
2468
2469 /* The `operator' keyword can be the beginning of an
2470 id-expression. */
2471 case RID_OPERATOR:
2472 goto id_expression;
2473
2474 case RID_FUNCTION_NAME:
2475 case RID_PRETTY_FUNCTION_NAME:
2476 case RID_C99_FUNCTION_NAME:
2477 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2478 __func__ are the names of variables -- but they are
2479 treated specially. Therefore, they are handled here,
2480 rather than relying on the generic id-expression logic
34cd5ae7 2481 below. Grammatically, these names are id-expressions.
a723baf1
MM
2482
2483 Consume the token. */
2484 token = cp_lexer_consume_token (parser->lexer);
2485 /* Look up the name. */
2486 return finish_fname (token->value);
2487
2488 case RID_VA_ARG:
2489 {
2490 tree expression;
2491 tree type;
2492
2493 /* The `__builtin_va_arg' construct is used to handle
2494 `va_arg'. Consume the `__builtin_va_arg' token. */
2495 cp_lexer_consume_token (parser->lexer);
2496 /* Look for the opening `('. */
2497 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2498 /* Now, parse the assignment-expression. */
2499 expression = cp_parser_assignment_expression (parser);
2500 /* Look for the `,'. */
2501 cp_parser_require (parser, CPP_COMMA, "`,'");
2502 /* Parse the type-id. */
2503 type = cp_parser_type_id (parser);
2504 /* Look for the closing `)'. */
2505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2506 /* Using `va_arg' in a constant-expression is not
2507 allowed. */
67c03833 2508 if (parser->integral_constant_expression_p)
14d22dd6 2509 {
67c03833
JM
2510 if (!parser->allow_non_integral_constant_expression_p)
2511 return cp_parser_non_integral_constant_expression ("`va_arg'");
2512 parser->non_integral_constant_expression_p = true;
14d22dd6 2513 }
a723baf1
MM
2514 return build_x_va_arg (expression, type);
2515 }
2516
263ee052
MM
2517 case RID_OFFSETOF:
2518 {
2519 tree expression;
2520 bool saved_in_offsetof_p;
2521
2522 /* Consume the "__offsetof__" token. */
2523 cp_lexer_consume_token (parser->lexer);
2524 /* Consume the opening `('. */
2525 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2526 /* Parse the parenthesized (almost) constant-expression. */
2527 saved_in_offsetof_p = parser->in_offsetof_p;
2528 parser->in_offsetof_p = true;
2529 expression
2530 = cp_parser_constant_expression (parser,
2531 /*allow_non_constant_p=*/false,
2532 /*non_constant_p=*/NULL);
2533 parser->in_offsetof_p = saved_in_offsetof_p;
2534 /* Consume the closing ')'. */
2535 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2536
2537 return expression;
2538 }
2539
a723baf1
MM
2540 default:
2541 cp_parser_error (parser, "expected primary-expression");
2542 return error_mark_node;
2543 }
a723baf1
MM
2544
2545 /* An id-expression can start with either an identifier, a
2546 `::' as the beginning of a qualified-id, or the "operator"
2547 keyword. */
2548 case CPP_NAME:
2549 case CPP_SCOPE:
2550 case CPP_TEMPLATE_ID:
2551 case CPP_NESTED_NAME_SPECIFIER:
2552 {
2553 tree id_expression;
2554 tree decl;
b3445994 2555 const char *error_msg;
a723baf1
MM
2556
2557 id_expression:
2558 /* Parse the id-expression. */
2559 id_expression
2560 = cp_parser_id_expression (parser,
2561 /*template_keyword_p=*/false,
2562 /*check_dependency_p=*/true,
f3c2dfc6
MM
2563 /*template_p=*/NULL,
2564 /*declarator_p=*/false);
a723baf1
MM
2565 if (id_expression == error_mark_node)
2566 return error_mark_node;
2567 /* If we have a template-id, then no further lookup is
2568 required. If the template-id was for a template-class, we
2569 will sometimes have a TYPE_DECL at this point. */
2570 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2571 || TREE_CODE (id_expression) == TYPE_DECL)
2572 decl = id_expression;
2573 /* Look up the name. */
2574 else
2575 {
2576 decl = cp_parser_lookup_name_simple (parser, id_expression);
2577 /* If name lookup gives us a SCOPE_REF, then the
2578 qualifying scope was dependent. Just propagate the
2579 name. */
2580 if (TREE_CODE (decl) == SCOPE_REF)
2581 {
2582 if (TYPE_P (TREE_OPERAND (decl, 0)))
2583 *qualifying_class = TREE_OPERAND (decl, 0);
2584 return decl;
2585 }
2586 /* Check to see if DECL is a local variable in a context
2587 where that is forbidden. */
2588 if (parser->local_variables_forbidden_p
2589 && local_variable_p (decl))
2590 {
2591 /* It might be that we only found DECL because we are
2592 trying to be generous with pre-ISO scoping rules.
2593 For example, consider:
2594
2595 int i;
2596 void g() {
2597 for (int i = 0; i < 10; ++i) {}
2598 extern void f(int j = i);
2599 }
2600
2601 Here, name look up will originally find the out
2602 of scope `i'. We need to issue a warning message,
2603 but then use the global `i'. */
2604 decl = check_for_out_of_scope_variable (decl);
2605 if (local_variable_p (decl))
2606 {
2607 error ("local variable `%D' may not appear in this context",
2608 decl);
2609 return error_mark_node;
2610 }
2611 }
c006d942 2612 }
b3445994
MM
2613
2614 decl = finish_id_expression (id_expression, decl, parser->scope,
2615 idk, qualifying_class,
67c03833
JM
2616 parser->integral_constant_expression_p,
2617 parser->allow_non_integral_constant_expression_p,
2618 &parser->non_integral_constant_expression_p,
b3445994
MM
2619 &error_msg);
2620 if (error_msg)
2621 cp_parser_error (parser, error_msg);
a723baf1
MM
2622 return decl;
2623 }
2624
2625 /* Anything else is an error. */
2626 default:
2627 cp_parser_error (parser, "expected primary-expression");
2628 return error_mark_node;
2629 }
2630}
2631
2632/* Parse an id-expression.
2633
2634 id-expression:
2635 unqualified-id
2636 qualified-id
2637
2638 qualified-id:
2639 :: [opt] nested-name-specifier template [opt] unqualified-id
2640 :: identifier
2641 :: operator-function-id
2642 :: template-id
2643
2644 Return a representation of the unqualified portion of the
2645 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2646 a `::' or nested-name-specifier.
2647
2648 Often, if the id-expression was a qualified-id, the caller will
2649 want to make a SCOPE_REF to represent the qualified-id. This
2650 function does not do this in order to avoid wastefully creating
2651 SCOPE_REFs when they are not required.
2652
a723baf1
MM
2653 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2654 `template' keyword.
2655
2656 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2657 uninstantiated templates.
2658
15d2cb19 2659 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2660 `template' keyword is used to explicitly indicate that the entity
f3c2dfc6
MM
2661 named is a template.
2662
2663 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2664 a declarator, rather than as part of an expression. */
a723baf1
MM
2665
2666static tree
2667cp_parser_id_expression (cp_parser *parser,
2668 bool template_keyword_p,
2669 bool check_dependency_p,
f3c2dfc6
MM
2670 bool *template_p,
2671 bool declarator_p)
a723baf1
MM
2672{
2673 bool global_scope_p;
2674 bool nested_name_specifier_p;
2675
2676 /* Assume the `template' keyword was not used. */
2677 if (template_p)
2678 *template_p = false;
2679
2680 /* Look for the optional `::' operator. */
2681 global_scope_p
2682 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2683 != NULL_TREE);
2684 /* Look for the optional nested-name-specifier. */
2685 nested_name_specifier_p
2686 = (cp_parser_nested_name_specifier_opt (parser,
2687 /*typename_keyword_p=*/false,
2688 check_dependency_p,
a668c6ad
MM
2689 /*type_p=*/false,
2690 /*is_declarator=*/false)
a723baf1
MM
2691 != NULL_TREE);
2692 /* If there is a nested-name-specifier, then we are looking at
2693 the first qualified-id production. */
2694 if (nested_name_specifier_p)
2695 {
2696 tree saved_scope;
2697 tree saved_object_scope;
2698 tree saved_qualifying_scope;
2699 tree unqualified_id;
2700 bool is_template;
2701
2702 /* See if the next token is the `template' keyword. */
2703 if (!template_p)
2704 template_p = &is_template;
2705 *template_p = cp_parser_optional_template_keyword (parser);
2706 /* Name lookup we do during the processing of the
2707 unqualified-id might obliterate SCOPE. */
2708 saved_scope = parser->scope;
2709 saved_object_scope = parser->object_scope;
2710 saved_qualifying_scope = parser->qualifying_scope;
2711 /* Process the final unqualified-id. */
2712 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2713 check_dependency_p,
2714 declarator_p);
a723baf1
MM
2715 /* Restore the SAVED_SCOPE for our caller. */
2716 parser->scope = saved_scope;
2717 parser->object_scope = saved_object_scope;
2718 parser->qualifying_scope = saved_qualifying_scope;
2719
2720 return unqualified_id;
2721 }
2722 /* Otherwise, if we are in global scope, then we are looking at one
2723 of the other qualified-id productions. */
2724 else if (global_scope_p)
2725 {
2726 cp_token *token;
2727 tree id;
2728
e5976695
MM
2729 /* Peek at the next token. */
2730 token = cp_lexer_peek_token (parser->lexer);
2731
2732 /* If it's an identifier, and the next token is not a "<", then
2733 we can avoid the template-id case. This is an optimization
2734 for this common case. */
2735 if (token->type == CPP_NAME
2736 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2737 return cp_parser_identifier (parser);
2738
a723baf1
MM
2739 cp_parser_parse_tentatively (parser);
2740 /* Try a template-id. */
2741 id = cp_parser_template_id (parser,
2742 /*template_keyword_p=*/false,
a668c6ad
MM
2743 /*check_dependency_p=*/true,
2744 declarator_p);
a723baf1
MM
2745 /* If that worked, we're done. */
2746 if (cp_parser_parse_definitely (parser))
2747 return id;
2748
e5976695
MM
2749 /* Peek at the next token. (Changes in the token buffer may
2750 have invalidated the pointer obtained above.) */
a723baf1
MM
2751 token = cp_lexer_peek_token (parser->lexer);
2752
2753 switch (token->type)
2754 {
2755 case CPP_NAME:
2756 return cp_parser_identifier (parser);
2757
2758 case CPP_KEYWORD:
2759 if (token->keyword == RID_OPERATOR)
2760 return cp_parser_operator_function_id (parser);
2761 /* Fall through. */
2762
2763 default:
2764 cp_parser_error (parser, "expected id-expression");
2765 return error_mark_node;
2766 }
2767 }
2768 else
2769 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
2770 /*check_dependency_p=*/true,
2771 declarator_p);
a723baf1
MM
2772}
2773
2774/* Parse an unqualified-id.
2775
2776 unqualified-id:
2777 identifier
2778 operator-function-id
2779 conversion-function-id
2780 ~ class-name
2781 template-id
2782
2783 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2784 keyword, in a construct like `A::template ...'.
2785
2786 Returns a representation of unqualified-id. For the `identifier'
2787 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2788 production a BIT_NOT_EXPR is returned; the operand of the
2789 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2790 other productions, see the documentation accompanying the
2791 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
2792 names are looked up in uninstantiated templates. If DECLARATOR_P
2793 is true, the unqualified-id is appearing as part of a declarator,
2794 rather than as part of an expression. */
a723baf1
MM
2795
2796static tree
94edc4ab
NN
2797cp_parser_unqualified_id (cp_parser* parser,
2798 bool template_keyword_p,
f3c2dfc6
MM
2799 bool check_dependency_p,
2800 bool declarator_p)
a723baf1
MM
2801{
2802 cp_token *token;
2803
2804 /* Peek at the next token. */
2805 token = cp_lexer_peek_token (parser->lexer);
2806
2807 switch (token->type)
2808 {
2809 case CPP_NAME:
2810 {
2811 tree id;
2812
2813 /* We don't know yet whether or not this will be a
2814 template-id. */
2815 cp_parser_parse_tentatively (parser);
2816 /* Try a template-id. */
2817 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2818 check_dependency_p,
2819 declarator_p);
a723baf1
MM
2820 /* If it worked, we're done. */
2821 if (cp_parser_parse_definitely (parser))
2822 return id;
2823 /* Otherwise, it's an ordinary identifier. */
2824 return cp_parser_identifier (parser);
2825 }
2826
2827 case CPP_TEMPLATE_ID:
2828 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2829 check_dependency_p,
2830 declarator_p);
a723baf1
MM
2831
2832 case CPP_COMPL:
2833 {
2834 tree type_decl;
2835 tree qualifying_scope;
2836 tree object_scope;
2837 tree scope;
2838
2839 /* Consume the `~' token. */
2840 cp_lexer_consume_token (parser->lexer);
2841 /* Parse the class-name. The standard, as written, seems to
2842 say that:
2843
2844 template <typename T> struct S { ~S (); };
2845 template <typename T> S<T>::~S() {}
2846
2847 is invalid, since `~' must be followed by a class-name, but
2848 `S<T>' is dependent, and so not known to be a class.
2849 That's not right; we need to look in uninstantiated
2850 templates. A further complication arises from:
2851
2852 template <typename T> void f(T t) {
2853 t.T::~T();
2854 }
2855
2856 Here, it is not possible to look up `T' in the scope of `T'
2857 itself. We must look in both the current scope, and the
2858 scope of the containing complete expression.
2859
2860 Yet another issue is:
2861
2862 struct S {
2863 int S;
2864 ~S();
2865 };
2866
2867 S::~S() {}
2868
2869 The standard does not seem to say that the `S' in `~S'
2870 should refer to the type `S' and not the data member
2871 `S::S'. */
2872
2873 /* DR 244 says that we look up the name after the "~" in the
2874 same scope as we looked up the qualifying name. That idea
2875 isn't fully worked out; it's more complicated than that. */
2876 scope = parser->scope;
2877 object_scope = parser->object_scope;
2878 qualifying_scope = parser->qualifying_scope;
2879
2880 /* If the name is of the form "X::~X" it's OK. */
2881 if (scope && TYPE_P (scope)
2882 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2883 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2884 == CPP_OPEN_PAREN)
2885 && (cp_lexer_peek_token (parser->lexer)->value
2886 == TYPE_IDENTIFIER (scope)))
2887 {
2888 cp_lexer_consume_token (parser->lexer);
2889 return build_nt (BIT_NOT_EXPR, scope);
2890 }
2891
2892 /* If there was an explicit qualification (S::~T), first look
2893 in the scope given by the qualification (i.e., S). */
2894 if (scope)
2895 {
2896 cp_parser_parse_tentatively (parser);
2897 type_decl = cp_parser_class_name (parser,
2898 /*typename_keyword_p=*/false,
2899 /*template_keyword_p=*/false,
2900 /*type_p=*/false,
a723baf1 2901 /*check_dependency=*/false,
a668c6ad
MM
2902 /*class_head_p=*/false,
2903 declarator_p);
a723baf1
MM
2904 if (cp_parser_parse_definitely (parser))
2905 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2906 }
2907 /* In "N::S::~S", look in "N" as well. */
2908 if (scope && qualifying_scope)
2909 {
2910 cp_parser_parse_tentatively (parser);
2911 parser->scope = qualifying_scope;
2912 parser->object_scope = NULL_TREE;
2913 parser->qualifying_scope = NULL_TREE;
2914 type_decl
2915 = cp_parser_class_name (parser,
2916 /*typename_keyword_p=*/false,
2917 /*template_keyword_p=*/false,
2918 /*type_p=*/false,
a723baf1 2919 /*check_dependency=*/false,
a668c6ad
MM
2920 /*class_head_p=*/false,
2921 declarator_p);
a723baf1
MM
2922 if (cp_parser_parse_definitely (parser))
2923 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2924 }
2925 /* In "p->S::~T", look in the scope given by "*p" as well. */
2926 else if (object_scope)
2927 {
2928 cp_parser_parse_tentatively (parser);
2929 parser->scope = object_scope;
2930 parser->object_scope = NULL_TREE;
2931 parser->qualifying_scope = NULL_TREE;
2932 type_decl
2933 = cp_parser_class_name (parser,
2934 /*typename_keyword_p=*/false,
2935 /*template_keyword_p=*/false,
2936 /*type_p=*/false,
a723baf1 2937 /*check_dependency=*/false,
a668c6ad
MM
2938 /*class_head_p=*/false,
2939 declarator_p);
a723baf1
MM
2940 if (cp_parser_parse_definitely (parser))
2941 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2942 }
2943 /* Look in the surrounding context. */
2944 parser->scope = NULL_TREE;
2945 parser->object_scope = NULL_TREE;
2946 parser->qualifying_scope = NULL_TREE;
2947 type_decl
2948 = cp_parser_class_name (parser,
2949 /*typename_keyword_p=*/false,
2950 /*template_keyword_p=*/false,
2951 /*type_p=*/false,
a723baf1 2952 /*check_dependency=*/false,
a668c6ad
MM
2953 /*class_head_p=*/false,
2954 declarator_p);
a723baf1
MM
2955 /* If an error occurred, assume that the name of the
2956 destructor is the same as the name of the qualifying
2957 class. That allows us to keep parsing after running
2958 into ill-formed destructor names. */
2959 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2960 return build_nt (BIT_NOT_EXPR, scope);
2961 else if (type_decl == error_mark_node)
2962 return error_mark_node;
2963
f3c2dfc6
MM
2964 /* [class.dtor]
2965
2966 A typedef-name that names a class shall not be used as the
2967 identifier in the declarator for a destructor declaration. */
2968 if (declarator_p
2969 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2970 && !DECL_SELF_REFERENCE_P (type_decl))
2971 error ("typedef-name `%D' used as destructor declarator",
2972 type_decl);
2973
a723baf1
MM
2974 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2975 }
2976
2977 case CPP_KEYWORD:
2978 if (token->keyword == RID_OPERATOR)
2979 {
2980 tree id;
2981
2982 /* This could be a template-id, so we try that first. */
2983 cp_parser_parse_tentatively (parser);
2984 /* Try a template-id. */
2985 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2986 /*check_dependency_p=*/true,
2987 declarator_p);
a723baf1
MM
2988 /* If that worked, we're done. */
2989 if (cp_parser_parse_definitely (parser))
2990 return id;
2991 /* We still don't know whether we're looking at an
2992 operator-function-id or a conversion-function-id. */
2993 cp_parser_parse_tentatively (parser);
2994 /* Try an operator-function-id. */
2995 id = cp_parser_operator_function_id (parser);
2996 /* If that didn't work, try a conversion-function-id. */
2997 if (!cp_parser_parse_definitely (parser))
2998 id = cp_parser_conversion_function_id (parser);
2999
3000 return id;
3001 }
3002 /* Fall through. */
3003
3004 default:
3005 cp_parser_error (parser, "expected unqualified-id");
3006 return error_mark_node;
3007 }
3008}
3009
3010/* Parse an (optional) nested-name-specifier.
3011
3012 nested-name-specifier:
3013 class-or-namespace-name :: nested-name-specifier [opt]
3014 class-or-namespace-name :: template nested-name-specifier [opt]
3015
3016 PARSER->SCOPE should be set appropriately before this function is
3017 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3018 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3019 in name lookups.
3020
3021 Sets PARSER->SCOPE to the class (TYPE) or namespace
3022 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3023 it unchanged if there is no nested-name-specifier. Returns the new
a668c6ad
MM
3024 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3025
3026 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3027 part of a declaration and/or decl-specifier. */
a723baf1
MM
3028
3029static tree
3030cp_parser_nested_name_specifier_opt (cp_parser *parser,
3031 bool typename_keyword_p,
3032 bool check_dependency_p,
a668c6ad
MM
3033 bool type_p,
3034 bool is_declaration)
a723baf1
MM
3035{
3036 bool success = false;
3037 tree access_check = NULL_TREE;
3038 ptrdiff_t start;
2050a1bb 3039 cp_token* token;
a723baf1
MM
3040
3041 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3042 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3043 false, it may have been true before, in which case something
3044 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3045 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3046 CHECK_DEPENDENCY_P is false, we have to fall through into the
3047 main loop. */
3048 if (check_dependency_p
3049 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3050 {
3051 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3052 return parser->scope;
3053 }
3054
3055 /* Remember where the nested-name-specifier starts. */
3056 if (cp_parser_parsing_tentatively (parser)
3057 && !cp_parser_committed_to_tentative_parse (parser))
3058 {
2050a1bb 3059 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3060 start = cp_lexer_token_difference (parser->lexer,
3061 parser->lexer->first_token,
2050a1bb 3062 token);
a723baf1
MM
3063 }
3064 else
3065 start = -1;
3066
8d241e0b 3067 push_deferring_access_checks (dk_deferred);
cf22909c 3068
a723baf1
MM
3069 while (true)
3070 {
3071 tree new_scope;
3072 tree old_scope;
3073 tree saved_qualifying_scope;
a723baf1
MM
3074 bool template_keyword_p;
3075
2050a1bb
MM
3076 /* Spot cases that cannot be the beginning of a
3077 nested-name-specifier. */
3078 token = cp_lexer_peek_token (parser->lexer);
3079
3080 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3081 the already parsed nested-name-specifier. */
3082 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3083 {
3084 /* Grab the nested-name-specifier and continue the loop. */
3085 cp_parser_pre_parsed_nested_name_specifier (parser);
3086 success = true;
3087 continue;
3088 }
3089
a723baf1
MM
3090 /* Spot cases that cannot be the beginning of a
3091 nested-name-specifier. On the second and subsequent times
3092 through the loop, we look for the `template' keyword. */
f7b5ecd9 3093 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3094 ;
3095 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3096 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3097 ;
3098 else
3099 {
3100 /* If the next token is not an identifier, then it is
3101 definitely not a class-or-namespace-name. */
f7b5ecd9 3102 if (token->type != CPP_NAME)
a723baf1
MM
3103 break;
3104 /* If the following token is neither a `<' (to begin a
3105 template-id), nor a `::', then we are not looking at a
3106 nested-name-specifier. */
3107 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3108 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3109 break;
3110 }
3111
3112 /* The nested-name-specifier is optional, so we parse
3113 tentatively. */
3114 cp_parser_parse_tentatively (parser);
3115
3116 /* Look for the optional `template' keyword, if this isn't the
3117 first time through the loop. */
3118 if (success)
3119 template_keyword_p = cp_parser_optional_template_keyword (parser);
3120 else
3121 template_keyword_p = false;
3122
3123 /* Save the old scope since the name lookup we are about to do
3124 might destroy it. */
3125 old_scope = parser->scope;
3126 saved_qualifying_scope = parser->qualifying_scope;
3127 /* Parse the qualifying entity. */
3128 new_scope
3129 = cp_parser_class_or_namespace_name (parser,
3130 typename_keyword_p,
3131 template_keyword_p,
3132 check_dependency_p,
a668c6ad
MM
3133 type_p,
3134 is_declaration);
a723baf1
MM
3135 /* Look for the `::' token. */
3136 cp_parser_require (parser, CPP_SCOPE, "`::'");
3137
3138 /* If we found what we wanted, we keep going; otherwise, we're
3139 done. */
3140 if (!cp_parser_parse_definitely (parser))
3141 {
3142 bool error_p = false;
3143
3144 /* Restore the OLD_SCOPE since it was valid before the
3145 failed attempt at finding the last
3146 class-or-namespace-name. */
3147 parser->scope = old_scope;
3148 parser->qualifying_scope = saved_qualifying_scope;
3149 /* If the next token is an identifier, and the one after
3150 that is a `::', then any valid interpretation would have
3151 found a class-or-namespace-name. */
3152 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3153 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3154 == CPP_SCOPE)
3155 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3156 != CPP_COMPL))
3157 {
3158 token = cp_lexer_consume_token (parser->lexer);
3159 if (!error_p)
3160 {
3161 tree decl;
3162
3163 decl = cp_parser_lookup_name_simple (parser, token->value);
3164 if (TREE_CODE (decl) == TEMPLATE_DECL)
3165 error ("`%D' used without template parameters",
3166 decl);
a723baf1 3167 else
4bb8ca28
MM
3168 cp_parser_name_lookup_error
3169 (parser, token->value, decl,
3170 "is not a class or namespace");
a723baf1
MM
3171 parser->scope = NULL_TREE;
3172 error_p = true;
eea9800f
MM
3173 /* Treat this as a successful nested-name-specifier
3174 due to:
3175
3176 [basic.lookup.qual]
3177
3178 If the name found is not a class-name (clause
3179 _class_) or namespace-name (_namespace.def_), the
3180 program is ill-formed. */
3181 success = true;
a723baf1
MM
3182 }
3183 cp_lexer_consume_token (parser->lexer);
3184 }
3185 break;
3186 }
3187
3188 /* We've found one valid nested-name-specifier. */
3189 success = true;
3190 /* Make sure we look in the right scope the next time through
3191 the loop. */
3192 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3193 ? TREE_TYPE (new_scope)
3194 : new_scope);
3195 /* If it is a class scope, try to complete it; we are about to
3196 be looking up names inside the class. */
8fbc5ae7
MM
3197 if (TYPE_P (parser->scope)
3198 /* Since checking types for dependency can be expensive,
3199 avoid doing it if the type is already complete. */
3200 && !COMPLETE_TYPE_P (parser->scope)
3201 /* Do not try to complete dependent types. */
1fb3244a 3202 && !dependent_type_p (parser->scope))
a723baf1
MM
3203 complete_type (parser->scope);
3204 }
3205
cf22909c
KL
3206 /* Retrieve any deferred checks. Do not pop this access checks yet
3207 so the memory will not be reclaimed during token replacing below. */
3208 access_check = get_deferred_access_checks ();
3209
a723baf1
MM
3210 /* If parsing tentatively, replace the sequence of tokens that makes
3211 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3212 token. That way, should we re-parse the token stream, we will
3213 not have to repeat the effort required to do the parse, nor will
3214 we issue duplicate error messages. */
3215 if (success && start >= 0)
3216 {
a723baf1
MM
3217 /* Find the token that corresponds to the start of the
3218 template-id. */
3219 token = cp_lexer_advance_token (parser->lexer,
3220 parser->lexer->first_token,
3221 start);
3222
a723baf1
MM
3223 /* Reset the contents of the START token. */
3224 token->type = CPP_NESTED_NAME_SPECIFIER;
3225 token->value = build_tree_list (access_check, parser->scope);
3226 TREE_TYPE (token->value) = parser->qualifying_scope;
3227 token->keyword = RID_MAX;
3228 /* Purge all subsequent tokens. */
3229 cp_lexer_purge_tokens_after (parser->lexer, token);
3230 }
3231
cf22909c 3232 pop_deferring_access_checks ();
a723baf1
MM
3233 return success ? parser->scope : NULL_TREE;
3234}
3235
3236/* Parse a nested-name-specifier. See
3237 cp_parser_nested_name_specifier_opt for details. This function
3238 behaves identically, except that it will an issue an error if no
3239 nested-name-specifier is present, and it will return
3240 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3241 is present. */
3242
3243static tree
3244cp_parser_nested_name_specifier (cp_parser *parser,
3245 bool typename_keyword_p,
3246 bool check_dependency_p,
a668c6ad
MM
3247 bool type_p,
3248 bool is_declaration)
a723baf1
MM
3249{
3250 tree scope;
3251
3252 /* Look for the nested-name-specifier. */
3253 scope = cp_parser_nested_name_specifier_opt (parser,
3254 typename_keyword_p,
3255 check_dependency_p,
a668c6ad
MM
3256 type_p,
3257 is_declaration);
a723baf1
MM
3258 /* If it was not present, issue an error message. */
3259 if (!scope)
3260 {
3261 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3262 parser->scope = NULL_TREE;
a723baf1
MM
3263 return error_mark_node;
3264 }
3265
3266 return scope;
3267}
3268
3269/* Parse a class-or-namespace-name.
3270
3271 class-or-namespace-name:
3272 class-name
3273 namespace-name
3274
3275 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3276 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3277 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3278 TYPE_P is TRUE iff the next name should be taken as a class-name,
3279 even the same name is declared to be another entity in the same
3280 scope.
3281
3282 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3283 specified by the class-or-namespace-name. If neither is found the
3284 ERROR_MARK_NODE is returned. */
a723baf1
MM
3285
3286static tree
3287cp_parser_class_or_namespace_name (cp_parser *parser,
3288 bool typename_keyword_p,
3289 bool template_keyword_p,
3290 bool check_dependency_p,
a668c6ad
MM
3291 bool type_p,
3292 bool is_declaration)
a723baf1
MM
3293{
3294 tree saved_scope;
3295 tree saved_qualifying_scope;
3296 tree saved_object_scope;
3297 tree scope;
eea9800f 3298 bool only_class_p;
a723baf1 3299
a723baf1
MM
3300 /* Before we try to parse the class-name, we must save away the
3301 current PARSER->SCOPE since cp_parser_class_name will destroy
3302 it. */
3303 saved_scope = parser->scope;
3304 saved_qualifying_scope = parser->qualifying_scope;
3305 saved_object_scope = parser->object_scope;
eea9800f
MM
3306 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3307 there is no need to look for a namespace-name. */
bbaab916 3308 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3309 if (!only_class_p)
3310 cp_parser_parse_tentatively (parser);
a723baf1
MM
3311 scope = cp_parser_class_name (parser,
3312 typename_keyword_p,
3313 template_keyword_p,
3314 type_p,
a723baf1 3315 check_dependency_p,
a668c6ad
MM
3316 /*class_head_p=*/false,
3317 is_declaration);
a723baf1 3318 /* If that didn't work, try for a namespace-name. */
eea9800f 3319 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3320 {
3321 /* Restore the saved scope. */
3322 parser->scope = saved_scope;
3323 parser->qualifying_scope = saved_qualifying_scope;
3324 parser->object_scope = saved_object_scope;
eea9800f
MM
3325 /* If we are not looking at an identifier followed by the scope
3326 resolution operator, then this is not part of a
3327 nested-name-specifier. (Note that this function is only used
3328 to parse the components of a nested-name-specifier.) */
3329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3330 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3331 return error_mark_node;
a723baf1
MM
3332 scope = cp_parser_namespace_name (parser);
3333 }
3334
3335 return scope;
3336}
3337
3338/* Parse a postfix-expression.
3339
3340 postfix-expression:
3341 primary-expression
3342 postfix-expression [ expression ]
3343 postfix-expression ( expression-list [opt] )
3344 simple-type-specifier ( expression-list [opt] )
3345 typename :: [opt] nested-name-specifier identifier
3346 ( expression-list [opt] )
3347 typename :: [opt] nested-name-specifier template [opt] template-id
3348 ( expression-list [opt] )
3349 postfix-expression . template [opt] id-expression
3350 postfix-expression -> template [opt] id-expression
3351 postfix-expression . pseudo-destructor-name
3352 postfix-expression -> pseudo-destructor-name
3353 postfix-expression ++
3354 postfix-expression --
3355 dynamic_cast < type-id > ( expression )
3356 static_cast < type-id > ( expression )
3357 reinterpret_cast < type-id > ( expression )
3358 const_cast < type-id > ( expression )
3359 typeid ( expression )
3360 typeid ( type-id )
3361
3362 GNU Extension:
3363
3364 postfix-expression:
3365 ( type-id ) { initializer-list , [opt] }
3366
3367 This extension is a GNU version of the C99 compound-literal
3368 construct. (The C99 grammar uses `type-name' instead of `type-id',
3369 but they are essentially the same concept.)
3370
3371 If ADDRESS_P is true, the postfix expression is the operand of the
3372 `&' operator.
3373
3374 Returns a representation of the expression. */
3375
3376static tree
3377cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3378{
3379 cp_token *token;
3380 enum rid keyword;
b3445994 3381 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3382 tree postfix_expression = NULL_TREE;
3383 /* Non-NULL only if the current postfix-expression can be used to
3384 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3385 class used to qualify the member. */
3386 tree qualifying_class = NULL_TREE;
a723baf1
MM
3387
3388 /* Peek at the next token. */
3389 token = cp_lexer_peek_token (parser->lexer);
3390 /* Some of the productions are determined by keywords. */
3391 keyword = token->keyword;
3392 switch (keyword)
3393 {
3394 case RID_DYNCAST:
3395 case RID_STATCAST:
3396 case RID_REINTCAST:
3397 case RID_CONSTCAST:
3398 {
3399 tree type;
3400 tree expression;
3401 const char *saved_message;
3402
3403 /* All of these can be handled in the same way from the point
3404 of view of parsing. Begin by consuming the token
3405 identifying the cast. */
3406 cp_lexer_consume_token (parser->lexer);
3407
3408 /* New types cannot be defined in the cast. */
3409 saved_message = parser->type_definition_forbidden_message;
3410 parser->type_definition_forbidden_message
3411 = "types may not be defined in casts";
3412
3413 /* Look for the opening `<'. */
3414 cp_parser_require (parser, CPP_LESS, "`<'");
3415 /* Parse the type to which we are casting. */
3416 type = cp_parser_type_id (parser);
3417 /* Look for the closing `>'. */
3418 cp_parser_require (parser, CPP_GREATER, "`>'");
3419 /* Restore the old message. */
3420 parser->type_definition_forbidden_message = saved_message;
3421
3422 /* And the expression which is being cast. */
3423 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3424 expression = cp_parser_expression (parser);
3425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3426
14d22dd6
MM
3427 /* Only type conversions to integral or enumeration types
3428 can be used in constant-expressions. */
67c03833 3429 if (parser->integral_constant_expression_p
14d22dd6 3430 && !dependent_type_p (type)
263ee052
MM
3431 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3432 /* A cast to pointer or reference type is allowed in the
3433 implementation of "offsetof". */
3434 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
14d22dd6 3435 {
67c03833
JM
3436 if (!parser->allow_non_integral_constant_expression_p)
3437 return (cp_parser_non_integral_constant_expression
14d22dd6
MM
3438 ("a cast to a type other than an integral or "
3439 "enumeration type"));
67c03833 3440 parser->non_integral_constant_expression_p = true;
14d22dd6
MM
3441 }
3442
a723baf1
MM
3443 switch (keyword)
3444 {
3445 case RID_DYNCAST:
3446 postfix_expression
3447 = build_dynamic_cast (type, expression);
3448 break;
3449 case RID_STATCAST:
3450 postfix_expression
3451 = build_static_cast (type, expression);
3452 break;
3453 case RID_REINTCAST:
3454 postfix_expression
3455 = build_reinterpret_cast (type, expression);
3456 break;
3457 case RID_CONSTCAST:
3458 postfix_expression
3459 = build_const_cast (type, expression);
3460 break;
3461 default:
3462 abort ();
3463 }
3464 }
3465 break;
3466
3467 case RID_TYPEID:
3468 {
3469 tree type;
3470 const char *saved_message;
3471
3472 /* Consume the `typeid' token. */
3473 cp_lexer_consume_token (parser->lexer);
3474 /* Look for the `(' token. */
3475 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3476 /* Types cannot be defined in a `typeid' expression. */
3477 saved_message = parser->type_definition_forbidden_message;
3478 parser->type_definition_forbidden_message
3479 = "types may not be defined in a `typeid\' expression";
3480 /* We can't be sure yet whether we're looking at a type-id or an
3481 expression. */
3482 cp_parser_parse_tentatively (parser);
3483 /* Try a type-id first. */
3484 type = cp_parser_type_id (parser);
3485 /* Look for the `)' token. Otherwise, we can't be sure that
3486 we're not looking at an expression: consider `typeid (int
3487 (3))', for example. */
3488 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3489 /* If all went well, simply lookup the type-id. */
3490 if (cp_parser_parse_definitely (parser))
3491 postfix_expression = get_typeid (type);
3492 /* Otherwise, fall back to the expression variant. */
3493 else
3494 {
3495 tree expression;
3496
3497 /* Look for an expression. */
3498 expression = cp_parser_expression (parser);
3499 /* Compute its typeid. */
3500 postfix_expression = build_typeid (expression);
3501 /* Look for the `)' token. */
3502 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3503 }
3504
3505 /* Restore the saved message. */
3506 parser->type_definition_forbidden_message = saved_message;
3507 }
3508 break;
3509
3510 case RID_TYPENAME:
3511 {
3512 bool template_p = false;
3513 tree id;
3514 tree type;
3515
3516 /* Consume the `typename' token. */
3517 cp_lexer_consume_token (parser->lexer);
3518 /* Look for the optional `::' operator. */
3519 cp_parser_global_scope_opt (parser,
3520 /*current_scope_valid_p=*/false);
3521 /* Look for the nested-name-specifier. */
3522 cp_parser_nested_name_specifier (parser,
3523 /*typename_keyword_p=*/true,
3524 /*check_dependency_p=*/true,
a668c6ad
MM
3525 /*type_p=*/true,
3526 /*is_declaration=*/true);
a723baf1
MM
3527 /* Look for the optional `template' keyword. */
3528 template_p = cp_parser_optional_template_keyword (parser);
3529 /* We don't know whether we're looking at a template-id or an
3530 identifier. */
3531 cp_parser_parse_tentatively (parser);
3532 /* Try a template-id. */
3533 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3534 /*check_dependency_p=*/true,
3535 /*is_declaration=*/true);
a723baf1
MM
3536 /* If that didn't work, try an identifier. */
3537 if (!cp_parser_parse_definitely (parser))
3538 id = cp_parser_identifier (parser);
3539 /* Create a TYPENAME_TYPE to represent the type to which the
3540 functional cast is being performed. */
3541 type = make_typename_type (parser->scope, id,
3542 /*complain=*/1);
3543
3544 postfix_expression = cp_parser_functional_cast (parser, type);
3545 }
3546 break;
3547
3548 default:
3549 {
3550 tree type;
3551
3552 /* If the next thing is a simple-type-specifier, we may be
3553 looking at a functional cast. We could also be looking at
3554 an id-expression. So, we try the functional cast, and if
3555 that doesn't work we fall back to the primary-expression. */
3556 cp_parser_parse_tentatively (parser);
3557 /* Look for the simple-type-specifier. */
3558 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3559 CP_PARSER_FLAGS_NONE,
3560 /*identifier_p=*/false);
a723baf1
MM
3561 /* Parse the cast itself. */
3562 if (!cp_parser_error_occurred (parser))
3563 postfix_expression
3564 = cp_parser_functional_cast (parser, type);
3565 /* If that worked, we're done. */
3566 if (cp_parser_parse_definitely (parser))
3567 break;
3568
3569 /* If the functional-cast didn't work out, try a
3570 compound-literal. */
14d22dd6
MM
3571 if (cp_parser_allow_gnu_extensions_p (parser)
3572 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3573 {
3574 tree initializer_list = NULL_TREE;
3575
3576 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3577 /* Consume the `('. */
3578 cp_lexer_consume_token (parser->lexer);
3579 /* Parse the type. */
3580 type = cp_parser_type_id (parser);
3581 /* Look for the `)'. */
3582 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3583 /* Look for the `{'. */
3584 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3585 /* If things aren't going well, there's no need to
3586 keep going. */
3587 if (!cp_parser_error_occurred (parser))
a723baf1 3588 {
39703eb9 3589 bool non_constant_p;
14d22dd6
MM
3590 /* Parse the initializer-list. */
3591 initializer_list
39703eb9 3592 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3593 /* Allow a trailing `,'. */
3594 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3595 cp_lexer_consume_token (parser->lexer);
3596 /* Look for the final `}'. */
3597 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3598 }
3599 /* If that worked, we're definitely looking at a
3600 compound-literal expression. */
3601 if (cp_parser_parse_definitely (parser))
3602 {
3603 /* Warn the user that a compound literal is not
3604 allowed in standard C++. */
3605 if (pedantic)
3606 pedwarn ("ISO C++ forbids compound-literals");
3607 /* Form the representation of the compound-literal. */
3608 postfix_expression
3609 = finish_compound_literal (type, initializer_list);
3610 break;
3611 }
3612 }
3613
3614 /* It must be a primary-expression. */
3615 postfix_expression = cp_parser_primary_expression (parser,
3616 &idk,
3617 &qualifying_class);
3618 }
3619 break;
3620 }
3621
ee76b931
MM
3622 /* If we were avoiding committing to the processing of a
3623 qualified-id until we knew whether or not we had a
3624 pointer-to-member, we now know. */
089d6ea7 3625 if (qualifying_class)
a723baf1 3626 {
ee76b931 3627 bool done;
a723baf1 3628
ee76b931
MM
3629 /* Peek at the next token. */
3630 token = cp_lexer_peek_token (parser->lexer);
3631 done = (token->type != CPP_OPEN_SQUARE
3632 && token->type != CPP_OPEN_PAREN
3633 && token->type != CPP_DOT
3634 && token->type != CPP_DEREF
3635 && token->type != CPP_PLUS_PLUS
3636 && token->type != CPP_MINUS_MINUS);
3637
3638 postfix_expression = finish_qualified_id_expr (qualifying_class,
3639 postfix_expression,
3640 done,
3641 address_p);
3642 if (done)
3643 return postfix_expression;
a723baf1
MM
3644 }
3645
a723baf1
MM
3646 /* Keep looping until the postfix-expression is complete. */
3647 while (true)
3648 {
10b1d5e7
MM
3649 if (idk == CP_ID_KIND_UNQUALIFIED
3650 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3651 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3652 /* It is not a Koenig lookup function call. */
3653 postfix_expression
3654 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3655
3656 /* Peek at the next token. */
3657 token = cp_lexer_peek_token (parser->lexer);
3658
3659 switch (token->type)
3660 {
3661 case CPP_OPEN_SQUARE:
3662 /* postfix-expression [ expression ] */
3663 {
3664 tree index;
3665
3666 /* Consume the `[' token. */
3667 cp_lexer_consume_token (parser->lexer);
3668 /* Parse the index expression. */
3669 index = cp_parser_expression (parser);
3670 /* Look for the closing `]'. */
3671 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3672
3673 /* Build the ARRAY_REF. */
3674 postfix_expression
3675 = grok_array_decl (postfix_expression, index);
b3445994 3676 idk = CP_ID_KIND_NONE;
a5ac3982
MM
3677 /* Array references are not permitted in
3678 constant-expressions. */
67c03833 3679 if (parser->integral_constant_expression_p)
a5ac3982 3680 {
67c03833 3681 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3682 postfix_expression
67c03833
JM
3683 = cp_parser_non_integral_constant_expression ("an array reference");
3684 parser->non_integral_constant_expression_p = true;
a5ac3982 3685 }
a723baf1
MM
3686 }
3687 break;
3688
3689 case CPP_OPEN_PAREN:
3690 /* postfix-expression ( expression-list [opt] ) */
3691 {
6d80c4b9 3692 bool koenig_p;
39703eb9
MM
3693 tree args = (cp_parser_parenthesized_expression_list
3694 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3695
7efa3e22
NS
3696 if (args == error_mark_node)
3697 {
3698 postfix_expression = error_mark_node;
3699 break;
3700 }
3701
14d22dd6
MM
3702 /* Function calls are not permitted in
3703 constant-expressions. */
67c03833 3704 if (parser->integral_constant_expression_p)
14d22dd6 3705 {
67c03833 3706 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982
MM
3707 {
3708 postfix_expression
67c03833 3709 = cp_parser_non_integral_constant_expression ("a function call");
a5ac3982
MM
3710 break;
3711 }
67c03833 3712 parser->non_integral_constant_expression_p = true;
14d22dd6 3713 }
a723baf1 3714
6d80c4b9 3715 koenig_p = false;
399dedb9
NS
3716 if (idk == CP_ID_KIND_UNQUALIFIED)
3717 {
3718 if (args
3719 && (is_overloaded_fn (postfix_expression)
3720 || DECL_P (postfix_expression)
3721 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3722 {
3723 koenig_p = true;
3724 postfix_expression
3725 = perform_koenig_lookup (postfix_expression, args);
3726 }
399dedb9
NS
3727 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3728 postfix_expression
3729 = unqualified_fn_lookup_error (postfix_expression);
3730 }
3731
d17811fd 3732 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3733 {
d17811fd
MM
3734 tree instance = TREE_OPERAND (postfix_expression, 0);
3735 tree fn = TREE_OPERAND (postfix_expression, 1);
3736
3737 if (processing_template_decl
3738 && (type_dependent_expression_p (instance)
3739 || (!BASELINK_P (fn)
3740 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3741 || type_dependent_expression_p (fn)
d17811fd
MM
3742 || any_type_dependent_arguments_p (args)))
3743 {
3744 postfix_expression
3745 = build_min_nt (CALL_EXPR, postfix_expression, args);
3746 break;
3747 }
3748
3749 postfix_expression
3750 = (build_new_method_call
3751 (instance, fn, args, NULL_TREE,
b3445994 3752 (idk == CP_ID_KIND_QUALIFIED
d17811fd 3753 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
a723baf1 3754 }
d17811fd
MM
3755 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3756 || TREE_CODE (postfix_expression) == MEMBER_REF
3757 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3758 postfix_expression = (build_offset_ref_call_from_tree
3759 (postfix_expression, args));
b3445994 3760 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3761 /* A call to a static class member, or a namespace-scope
3762 function. */
3763 postfix_expression
3764 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3765 /*disallow_virtual=*/true,
3766 koenig_p);
a723baf1 3767 else
2050a1bb
MM
3768 /* All other function calls. */
3769 postfix_expression
3770 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3771 /*disallow_virtual=*/false,
3772 koenig_p);
a723baf1
MM
3773
3774 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3775 idk = CP_ID_KIND_NONE;
a723baf1
MM
3776 }
3777 break;
3778
3779 case CPP_DOT:
3780 case CPP_DEREF:
3781 /* postfix-expression . template [opt] id-expression
3782 postfix-expression . pseudo-destructor-name
3783 postfix-expression -> template [opt] id-expression
3784 postfix-expression -> pseudo-destructor-name */
3785 {
3786 tree name;
3787 bool dependent_p;
3788 bool template_p;
3789 tree scope = NULL_TREE;
a5ac3982 3790 enum cpp_ttype token_type = token->type;
a723baf1
MM
3791
3792 /* If this is a `->' operator, dereference the pointer. */
3793 if (token->type == CPP_DEREF)
3794 postfix_expression = build_x_arrow (postfix_expression);
3795 /* Check to see whether or not the expression is
3796 type-dependent. */
bbaab916 3797 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3798 /* The identifier following the `->' or `.' is not
3799 qualified. */
3800 parser->scope = NULL_TREE;
3801 parser->qualifying_scope = NULL_TREE;
3802 parser->object_scope = NULL_TREE;
b3445994 3803 idk = CP_ID_KIND_NONE;
a723baf1
MM
3804 /* Enter the scope corresponding to the type of the object
3805 given by the POSTFIX_EXPRESSION. */
3806 if (!dependent_p
3807 && TREE_TYPE (postfix_expression) != NULL_TREE)
3808 {
3809 scope = TREE_TYPE (postfix_expression);
3810 /* According to the standard, no expression should
3811 ever have reference type. Unfortunately, we do not
3812 currently match the standard in this respect in
3813 that our internal representation of an expression
3814 may have reference type even when the standard says
3815 it does not. Therefore, we have to manually obtain
3816 the underlying type here. */
ee76b931 3817 scope = non_reference (scope);
a723baf1
MM
3818 /* The type of the POSTFIX_EXPRESSION must be
3819 complete. */
3820 scope = complete_type_or_else (scope, NULL_TREE);
3821 /* Let the name lookup machinery know that we are
3822 processing a class member access expression. */
3823 parser->context->object_type = scope;
3824 /* If something went wrong, we want to be able to
3825 discern that case, as opposed to the case where
3826 there was no SCOPE due to the type of expression
3827 being dependent. */
3828 if (!scope)
3829 scope = error_mark_node;
3830 }
3831
3832 /* Consume the `.' or `->' operator. */
3833 cp_lexer_consume_token (parser->lexer);
3834 /* If the SCOPE is not a scalar type, we are looking at an
3835 ordinary class member access expression, rather than a
3836 pseudo-destructor-name. */
3837 if (!scope || !SCALAR_TYPE_P (scope))
3838 {
3839 template_p = cp_parser_optional_template_keyword (parser);
3840 /* Parse the id-expression. */
3841 name = cp_parser_id_expression (parser,
3842 template_p,
3843 /*check_dependency_p=*/true,
f3c2dfc6
MM
3844 /*template_p=*/NULL,
3845 /*declarator_p=*/false);
a723baf1
MM
3846 /* In general, build a SCOPE_REF if the member name is
3847 qualified. However, if the name was not dependent
3848 and has already been resolved; there is no need to
3849 build the SCOPE_REF. For example;
3850
3851 struct X { void f(); };
3852 template <typename T> void f(T* t) { t->X::f(); }
3853
d17811fd
MM
3854 Even though "t" is dependent, "X::f" is not and has
3855 been resolved to a BASELINK; there is no need to
a723baf1 3856 include scope information. */
a6bd211d
JM
3857
3858 /* But we do need to remember that there was an explicit
3859 scope for virtual function calls. */
3860 if (parser->scope)
b3445994 3861 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3862
a723baf1
MM
3863 if (name != error_mark_node
3864 && !BASELINK_P (name)
3865 && parser->scope)
3866 {
3867 name = build_nt (SCOPE_REF, parser->scope, name);
3868 parser->scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
3870 parser->object_scope = NULL_TREE;
3871 }
3872 postfix_expression
3873 = finish_class_member_access_expr (postfix_expression, name);
3874 }
3875 /* Otherwise, try the pseudo-destructor-name production. */
3876 else
3877 {
90808894 3878 tree s = NULL_TREE;
a723baf1
MM
3879 tree type;
3880
3881 /* Parse the pseudo-destructor-name. */
3882 cp_parser_pseudo_destructor_name (parser, &s, &type);
3883 /* Form the call. */
3884 postfix_expression
3885 = finish_pseudo_destructor_expr (postfix_expression,
3886 s, TREE_TYPE (type));
3887 }
3888
3889 /* We no longer need to look up names in the scope of the
3890 object on the left-hand side of the `.' or `->'
3891 operator. */
3892 parser->context->object_type = NULL_TREE;
a5ac3982 3893 /* These operators may not appear in constant-expressions. */
67c03833 3894 if (parser->integral_constant_expression_p
263ee052
MM
3895 /* The "->" operator is allowed in the implementation
3896 of "offsetof". */
3897 && !(parser->in_offsetof_p && token_type == CPP_DEREF))
a5ac3982 3898 {
67c03833 3899 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3900 postfix_expression
67c03833 3901 = (cp_parser_non_integral_constant_expression
a5ac3982 3902 (token_type == CPP_DEREF ? "'->'" : "`.'"));
67c03833 3903 parser->non_integral_constant_expression_p = true;
a5ac3982 3904 }
a723baf1
MM
3905 }
3906 break;
3907
3908 case CPP_PLUS_PLUS:
3909 /* postfix-expression ++ */
3910 /* Consume the `++' token. */
3911 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3912 /* Generate a representation for the complete expression. */
3913 postfix_expression
3914 = finish_increment_expr (postfix_expression,
3915 POSTINCREMENT_EXPR);
14d22dd6 3916 /* Increments may not appear in constant-expressions. */
67c03833 3917 if (parser->integral_constant_expression_p)
14d22dd6 3918 {
67c03833 3919 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3920 postfix_expression
67c03833
JM
3921 = cp_parser_non_integral_constant_expression ("an increment");
3922 parser->non_integral_constant_expression_p = true;
14d22dd6 3923 }
b3445994 3924 idk = CP_ID_KIND_NONE;
a723baf1
MM
3925 break;
3926
3927 case CPP_MINUS_MINUS:
3928 /* postfix-expression -- */
3929 /* Consume the `--' token. */
3930 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3931 /* Generate a representation for the complete expression. */
3932 postfix_expression
3933 = finish_increment_expr (postfix_expression,
3934 POSTDECREMENT_EXPR);
14d22dd6 3935 /* Decrements may not appear in constant-expressions. */
67c03833 3936 if (parser->integral_constant_expression_p)
14d22dd6 3937 {
67c03833 3938 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3939 postfix_expression
67c03833
JM
3940 = cp_parser_non_integral_constant_expression ("a decrement");
3941 parser->non_integral_constant_expression_p = true;
14d22dd6 3942 }
b3445994 3943 idk = CP_ID_KIND_NONE;
a723baf1
MM
3944 break;
3945
3946 default:
3947 return postfix_expression;
3948 }
3949 }
3950
3951 /* We should never get here. */
3952 abort ();
3953 return error_mark_node;
3954}
3955
7efa3e22 3956/* Parse a parenthesized expression-list.
a723baf1
MM
3957
3958 expression-list:
3959 assignment-expression
3960 expression-list, assignment-expression
3961
7efa3e22
NS
3962 attribute-list:
3963 expression-list
3964 identifier
3965 identifier, expression-list
3966
a723baf1
MM
3967 Returns a TREE_LIST. The TREE_VALUE of each node is a
3968 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
3969 is returned even if there is only a single expression in the list.
3970 error_mark_node is returned if the ( and or ) are
3971 missing. NULL_TREE is returned on no expressions. The parentheses
3972 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
3973 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3974 indicates whether or not all of the expressions in the list were
3975 constant. */
a723baf1
MM
3976
3977static tree
39703eb9
MM
3978cp_parser_parenthesized_expression_list (cp_parser* parser,
3979 bool is_attribute_list,
3980 bool *non_constant_p)
a723baf1
MM
3981{
3982 tree expression_list = NULL_TREE;
7efa3e22 3983 tree identifier = NULL_TREE;
39703eb9
MM
3984
3985 /* Assume all the expressions will be constant. */
3986 if (non_constant_p)
3987 *non_constant_p = false;
3988
7efa3e22
NS
3989 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3990 return error_mark_node;
3991
a723baf1 3992 /* Consume expressions until there are no more. */
7efa3e22
NS
3993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3994 while (true)
3995 {
3996 tree expr;
3997
3998 /* At the beginning of attribute lists, check to see if the
3999 next token is an identifier. */
4000 if (is_attribute_list
4001 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4002 {
4003 cp_token *token;
4004
4005 /* Consume the identifier. */
4006 token = cp_lexer_consume_token (parser->lexer);
4007 /* Save the identifier. */
4008 identifier = token->value;
4009 }
4010 else
4011 {
4012 /* Parse the next assignment-expression. */
39703eb9
MM
4013 if (non_constant_p)
4014 {
4015 bool expr_non_constant_p;
4016 expr = (cp_parser_constant_expression
4017 (parser, /*allow_non_constant_p=*/true,
4018 &expr_non_constant_p));
4019 if (expr_non_constant_p)
4020 *non_constant_p = true;
4021 }
4022 else
4023 expr = cp_parser_assignment_expression (parser);
a723baf1 4024
7efa3e22
NS
4025 /* Add it to the list. We add error_mark_node
4026 expressions to the list, so that we can still tell if
4027 the correct form for a parenthesized expression-list
4028 is found. That gives better errors. */
4029 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4030
7efa3e22
NS
4031 if (expr == error_mark_node)
4032 goto skip_comma;
4033 }
a723baf1 4034
7efa3e22
NS
4035 /* After the first item, attribute lists look the same as
4036 expression lists. */
4037 is_attribute_list = false;
4038
4039 get_comma:;
4040 /* If the next token isn't a `,', then we are done. */
4041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4042 break;
4043
4044 /* Otherwise, consume the `,' and keep going. */
4045 cp_lexer_consume_token (parser->lexer);
4046 }
4047
4048 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4049 {
4050 int ending;
4051
4052 skip_comma:;
4053 /* We try and resync to an unnested comma, as that will give the
4054 user better diagnostics. */
4bb8ca28
MM
4055 ending = cp_parser_skip_to_closing_parenthesis (parser,
4056 /*recovering=*/true,
4057 /*or_comma=*/true,
a668c6ad 4058 /*consume_paren=*/true);
7efa3e22
NS
4059 if (ending < 0)
4060 goto get_comma;
4061 if (!ending)
4062 return error_mark_node;
a723baf1
MM
4063 }
4064
4065 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4066 expression_list = nreverse (expression_list);
4067 if (identifier)
4068 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4069
4070 return expression_list;
a723baf1
MM
4071}
4072
4073/* Parse a pseudo-destructor-name.
4074
4075 pseudo-destructor-name:
4076 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4077 :: [opt] nested-name-specifier template template-id :: ~ type-name
4078 :: [opt] nested-name-specifier [opt] ~ type-name
4079
4080 If either of the first two productions is used, sets *SCOPE to the
4081 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4082 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4083 or ERROR_MARK_NODE if no type-name is present. */
4084
4085static void
94edc4ab
NN
4086cp_parser_pseudo_destructor_name (cp_parser* parser,
4087 tree* scope,
4088 tree* type)
a723baf1
MM
4089{
4090 bool nested_name_specifier_p;
4091
4092 /* Look for the optional `::' operator. */
4093 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4094 /* Look for the optional nested-name-specifier. */
4095 nested_name_specifier_p
4096 = (cp_parser_nested_name_specifier_opt (parser,
4097 /*typename_keyword_p=*/false,
4098 /*check_dependency_p=*/true,
a668c6ad
MM
4099 /*type_p=*/false,
4100 /*is_declaration=*/true)
a723baf1
MM
4101 != NULL_TREE);
4102 /* Now, if we saw a nested-name-specifier, we might be doing the
4103 second production. */
4104 if (nested_name_specifier_p
4105 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4106 {
4107 /* Consume the `template' keyword. */
4108 cp_lexer_consume_token (parser->lexer);
4109 /* Parse the template-id. */
4110 cp_parser_template_id (parser,
4111 /*template_keyword_p=*/true,
a668c6ad
MM
4112 /*check_dependency_p=*/false,
4113 /*is_declaration=*/true);
a723baf1
MM
4114 /* Look for the `::' token. */
4115 cp_parser_require (parser, CPP_SCOPE, "`::'");
4116 }
4117 /* If the next token is not a `~', then there might be some
9bcb9aae 4118 additional qualification. */
a723baf1
MM
4119 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4120 {
4121 /* Look for the type-name. */
4122 *scope = TREE_TYPE (cp_parser_type_name (parser));
4123 /* Look for the `::' token. */
4124 cp_parser_require (parser, CPP_SCOPE, "`::'");
4125 }
4126 else
4127 *scope = NULL_TREE;
4128
4129 /* Look for the `~'. */
4130 cp_parser_require (parser, CPP_COMPL, "`~'");
4131 /* Look for the type-name again. We are not responsible for
4132 checking that it matches the first type-name. */
4133 *type = cp_parser_type_name (parser);
4134}
4135
4136/* Parse a unary-expression.
4137
4138 unary-expression:
4139 postfix-expression
4140 ++ cast-expression
4141 -- cast-expression
4142 unary-operator cast-expression
4143 sizeof unary-expression
4144 sizeof ( type-id )
4145 new-expression
4146 delete-expression
4147
4148 GNU Extensions:
4149
4150 unary-expression:
4151 __extension__ cast-expression
4152 __alignof__ unary-expression
4153 __alignof__ ( type-id )
4154 __real__ cast-expression
4155 __imag__ cast-expression
4156 && identifier
4157
4158 ADDRESS_P is true iff the unary-expression is appearing as the
4159 operand of the `&' operator.
4160
34cd5ae7 4161 Returns a representation of the expression. */
a723baf1
MM
4162
4163static tree
4164cp_parser_unary_expression (cp_parser *parser, bool address_p)
4165{
4166 cp_token *token;
4167 enum tree_code unary_operator;
4168
4169 /* Peek at the next token. */
4170 token = cp_lexer_peek_token (parser->lexer);
4171 /* Some keywords give away the kind of expression. */
4172 if (token->type == CPP_KEYWORD)
4173 {
4174 enum rid keyword = token->keyword;
4175
4176 switch (keyword)
4177 {
4178 case RID_ALIGNOF:
a723baf1
MM
4179 case RID_SIZEOF:
4180 {
4181 tree operand;
7a18b933 4182 enum tree_code op;
a723baf1 4183
7a18b933
NS
4184 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4185 /* Consume the token. */
a723baf1
MM
4186 cp_lexer_consume_token (parser->lexer);
4187 /* Parse the operand. */
4188 operand = cp_parser_sizeof_operand (parser, keyword);
4189
7a18b933
NS
4190 if (TYPE_P (operand))
4191 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4192 else
7a18b933 4193 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4194 }
4195
4196 case RID_NEW:
4197 return cp_parser_new_expression (parser);
4198
4199 case RID_DELETE:
4200 return cp_parser_delete_expression (parser);
4201
4202 case RID_EXTENSION:
4203 {
4204 /* The saved value of the PEDANTIC flag. */
4205 int saved_pedantic;
4206 tree expr;
4207
4208 /* Save away the PEDANTIC flag. */
4209 cp_parser_extension_opt (parser, &saved_pedantic);
4210 /* Parse the cast-expression. */
d6b4ea85 4211 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4212 /* Restore the PEDANTIC flag. */
4213 pedantic = saved_pedantic;
4214
4215 return expr;
4216 }
4217
4218 case RID_REALPART:
4219 case RID_IMAGPART:
4220 {
4221 tree expression;
4222
4223 /* Consume the `__real__' or `__imag__' token. */
4224 cp_lexer_consume_token (parser->lexer);
4225 /* Parse the cast-expression. */
d6b4ea85 4226 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4227 /* Create the complete representation. */
4228 return build_x_unary_op ((keyword == RID_REALPART
4229 ? REALPART_EXPR : IMAGPART_EXPR),
4230 expression);
4231 }
4232 break;
4233
4234 default:
4235 break;
4236 }
4237 }
4238
4239 /* Look for the `:: new' and `:: delete', which also signal the
4240 beginning of a new-expression, or delete-expression,
4241 respectively. If the next token is `::', then it might be one of
4242 these. */
4243 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4244 {
4245 enum rid keyword;
4246
4247 /* See if the token after the `::' is one of the keywords in
4248 which we're interested. */
4249 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4250 /* If it's `new', we have a new-expression. */
4251 if (keyword == RID_NEW)
4252 return cp_parser_new_expression (parser);
4253 /* Similarly, for `delete'. */
4254 else if (keyword == RID_DELETE)
4255 return cp_parser_delete_expression (parser);
4256 }
4257
4258 /* Look for a unary operator. */
4259 unary_operator = cp_parser_unary_operator (token);
4260 /* The `++' and `--' operators can be handled similarly, even though
4261 they are not technically unary-operators in the grammar. */
4262 if (unary_operator == ERROR_MARK)
4263 {
4264 if (token->type == CPP_PLUS_PLUS)
4265 unary_operator = PREINCREMENT_EXPR;
4266 else if (token->type == CPP_MINUS_MINUS)
4267 unary_operator = PREDECREMENT_EXPR;
4268 /* Handle the GNU address-of-label extension. */
4269 else if (cp_parser_allow_gnu_extensions_p (parser)
4270 && token->type == CPP_AND_AND)
4271 {
4272 tree identifier;
4273
4274 /* Consume the '&&' token. */
4275 cp_lexer_consume_token (parser->lexer);
4276 /* Look for the identifier. */
4277 identifier = cp_parser_identifier (parser);
4278 /* Create an expression representing the address. */
4279 return finish_label_address_expr (identifier);
4280 }
4281 }
4282 if (unary_operator != ERROR_MARK)
4283 {
4284 tree cast_expression;
a5ac3982
MM
4285 tree expression = error_mark_node;
4286 const char *non_constant_p = NULL;
a723baf1
MM
4287
4288 /* Consume the operator token. */
4289 token = cp_lexer_consume_token (parser->lexer);
4290 /* Parse the cast-expression. */
4291 cast_expression
4292 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4293 /* Now, build an appropriate representation. */
4294 switch (unary_operator)
4295 {
4296 case INDIRECT_REF:
a5ac3982
MM
4297 non_constant_p = "`*'";
4298 expression = build_x_indirect_ref (cast_expression, "unary *");
4299 break;
4300
a723baf1 4301 case ADDR_EXPR:
263ee052
MM
4302 /* The "&" operator is allowed in the implementation of
4303 "offsetof". */
4304 if (!parser->in_offsetof_p)
4305 non_constant_p = "`&'";
a5ac3982 4306 /* Fall through. */
d17811fd 4307 case BIT_NOT_EXPR:
a5ac3982
MM
4308 expression = build_x_unary_op (unary_operator, cast_expression);
4309 break;
4310
14d22dd6
MM
4311 case PREINCREMENT_EXPR:
4312 case PREDECREMENT_EXPR:
a5ac3982
MM
4313 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4314 ? "`++'" : "`--'");
14d22dd6 4315 /* Fall through. */
a723baf1
MM
4316 case CONVERT_EXPR:
4317 case NEGATE_EXPR:
4318 case TRUTH_NOT_EXPR:
a5ac3982
MM
4319 expression = finish_unary_op_expr (unary_operator, cast_expression);
4320 break;
a723baf1 4321
a723baf1
MM
4322 default:
4323 abort ();
a723baf1 4324 }
a5ac3982 4325
67c03833 4326 if (non_constant_p && parser->integral_constant_expression_p)
a5ac3982 4327 {
67c03833
JM
4328 if (!parser->allow_non_integral_constant_expression_p)
4329 return cp_parser_non_integral_constant_expression (non_constant_p);
4330 parser->non_integral_constant_expression_p = true;
a5ac3982
MM
4331 }
4332
4333 return expression;
a723baf1
MM
4334 }
4335
4336 return cp_parser_postfix_expression (parser, address_p);
4337}
4338
4339/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4340 unary-operator, the corresponding tree code is returned. */
4341
4342static enum tree_code
94edc4ab 4343cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4344{
4345 switch (token->type)
4346 {
4347 case CPP_MULT:
4348 return INDIRECT_REF;
4349
4350 case CPP_AND:
4351 return ADDR_EXPR;
4352
4353 case CPP_PLUS:
4354 return CONVERT_EXPR;
4355
4356 case CPP_MINUS:
4357 return NEGATE_EXPR;
4358
4359 case CPP_NOT:
4360 return TRUTH_NOT_EXPR;
4361
4362 case CPP_COMPL:
4363 return BIT_NOT_EXPR;
4364
4365 default:
4366 return ERROR_MARK;
4367 }
4368}
4369
4370/* Parse a new-expression.
4371
ca099ac8 4372 new-expression:
a723baf1
MM
4373 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4374 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4375
4376 Returns a representation of the expression. */
4377
4378static tree
94edc4ab 4379cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4380{
4381 bool global_scope_p;
4382 tree placement;
4383 tree type;
4384 tree initializer;
4385
4386 /* Look for the optional `::' operator. */
4387 global_scope_p
4388 = (cp_parser_global_scope_opt (parser,
4389 /*current_scope_valid_p=*/false)
4390 != NULL_TREE);
4391 /* Look for the `new' operator. */
4392 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4393 /* There's no easy way to tell a new-placement from the
4394 `( type-id )' construct. */
4395 cp_parser_parse_tentatively (parser);
4396 /* Look for a new-placement. */
4397 placement = cp_parser_new_placement (parser);
4398 /* If that didn't work out, there's no new-placement. */
4399 if (!cp_parser_parse_definitely (parser))
4400 placement = NULL_TREE;
4401
4402 /* If the next token is a `(', then we have a parenthesized
4403 type-id. */
4404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4405 {
4406 /* Consume the `('. */
4407 cp_lexer_consume_token (parser->lexer);
4408 /* Parse the type-id. */
4409 type = cp_parser_type_id (parser);
4410 /* Look for the closing `)'. */
4411 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4412 }
4413 /* Otherwise, there must be a new-type-id. */
4414 else
4415 type = cp_parser_new_type_id (parser);
4416
4417 /* If the next token is a `(', then we have a new-initializer. */
4418 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4419 initializer = cp_parser_new_initializer (parser);
4420 else
4421 initializer = NULL_TREE;
4422
4423 /* Create a representation of the new-expression. */
4424 return build_new (placement, type, initializer, global_scope_p);
4425}
4426
4427/* Parse a new-placement.
4428
4429 new-placement:
4430 ( expression-list )
4431
4432 Returns the same representation as for an expression-list. */
4433
4434static tree
94edc4ab 4435cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4436{
4437 tree expression_list;
4438
a723baf1 4439 /* Parse the expression-list. */
39703eb9
MM
4440 expression_list = (cp_parser_parenthesized_expression_list
4441 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4442
4443 return expression_list;
4444}
4445
4446/* Parse a new-type-id.
4447
4448 new-type-id:
4449 type-specifier-seq new-declarator [opt]
4450
4451 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4452 and whose TREE_VALUE is the new-declarator. */
4453
4454static tree
94edc4ab 4455cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4456{
4457 tree type_specifier_seq;
4458 tree declarator;
4459 const char *saved_message;
4460
4461 /* The type-specifier sequence must not contain type definitions.
4462 (It cannot contain declarations of new types either, but if they
4463 are not definitions we will catch that because they are not
4464 complete.) */
4465 saved_message = parser->type_definition_forbidden_message;
4466 parser->type_definition_forbidden_message
4467 = "types may not be defined in a new-type-id";
4468 /* Parse the type-specifier-seq. */
4469 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4470 /* Restore the old message. */
4471 parser->type_definition_forbidden_message = saved_message;
4472 /* Parse the new-declarator. */
4473 declarator = cp_parser_new_declarator_opt (parser);
4474
4475 return build_tree_list (type_specifier_seq, declarator);
4476}
4477
4478/* Parse an (optional) new-declarator.
4479
4480 new-declarator:
4481 ptr-operator new-declarator [opt]
4482 direct-new-declarator
4483
4484 Returns a representation of the declarator. See
4485 cp_parser_declarator for the representations used. */
4486
4487static tree
94edc4ab 4488cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4489{
4490 enum tree_code code;
4491 tree type;
4492 tree cv_qualifier_seq;
4493
4494 /* We don't know if there's a ptr-operator next, or not. */
4495 cp_parser_parse_tentatively (parser);
4496 /* Look for a ptr-operator. */
4497 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4498 /* If that worked, look for more new-declarators. */
4499 if (cp_parser_parse_definitely (parser))
4500 {
4501 tree declarator;
4502
4503 /* Parse another optional declarator. */
4504 declarator = cp_parser_new_declarator_opt (parser);
4505
4506 /* Create the representation of the declarator. */
4507 if (code == INDIRECT_REF)
4508 declarator = make_pointer_declarator (cv_qualifier_seq,
4509 declarator);
4510 else
4511 declarator = make_reference_declarator (cv_qualifier_seq,
4512 declarator);
4513
4514 /* Handle the pointer-to-member case. */
4515 if (type)
4516 declarator = build_nt (SCOPE_REF, type, declarator);
4517
4518 return declarator;
4519 }
4520
4521 /* If the next token is a `[', there is a direct-new-declarator. */
4522 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4523 return cp_parser_direct_new_declarator (parser);
4524
4525 return NULL_TREE;
4526}
4527
4528/* Parse a direct-new-declarator.
4529
4530 direct-new-declarator:
4531 [ expression ]
4532 direct-new-declarator [constant-expression]
4533
4534 Returns an ARRAY_REF, following the same conventions as are
4535 documented for cp_parser_direct_declarator. */
4536
4537static tree
94edc4ab 4538cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4539{
4540 tree declarator = NULL_TREE;
4541
4542 while (true)
4543 {
4544 tree expression;
4545
4546 /* Look for the opening `['. */
4547 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4548 /* The first expression is not required to be constant. */
4549 if (!declarator)
4550 {
4551 expression = cp_parser_expression (parser);
4552 /* The standard requires that the expression have integral
4553 type. DR 74 adds enumeration types. We believe that the
4554 real intent is that these expressions be handled like the
4555 expression in a `switch' condition, which also allows
4556 classes with a single conversion to integral or
4557 enumeration type. */
4558 if (!processing_template_decl)
4559 {
4560 expression
4561 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4562 expression,
b746c5dc 4563 /*complain=*/true);
a723baf1
MM
4564 if (!expression)
4565 {
4566 error ("expression in new-declarator must have integral or enumeration type");
4567 expression = error_mark_node;
4568 }
4569 }
4570 }
4571 /* But all the other expressions must be. */
4572 else
14d22dd6
MM
4573 expression
4574 = cp_parser_constant_expression (parser,
4575 /*allow_non_constant=*/false,
4576 NULL);
a723baf1
MM
4577 /* Look for the closing `]'. */
4578 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4579
4580 /* Add this bound to the declarator. */
4581 declarator = build_nt (ARRAY_REF, declarator, expression);
4582
4583 /* If the next token is not a `[', then there are no more
4584 bounds. */
4585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4586 break;
4587 }
4588
4589 return declarator;
4590}
4591
4592/* Parse a new-initializer.
4593
4594 new-initializer:
4595 ( expression-list [opt] )
4596
34cd5ae7 4597 Returns a representation of the expression-list. If there is no
a723baf1
MM
4598 expression-list, VOID_ZERO_NODE is returned. */
4599
4600static tree
94edc4ab 4601cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4602{
4603 tree expression_list;
4604
39703eb9
MM
4605 expression_list = (cp_parser_parenthesized_expression_list
4606 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4607 if (!expression_list)
a723baf1 4608 expression_list = void_zero_node;
a723baf1
MM
4609
4610 return expression_list;
4611}
4612
4613/* Parse a delete-expression.
4614
4615 delete-expression:
4616 :: [opt] delete cast-expression
4617 :: [opt] delete [ ] cast-expression
4618
4619 Returns a representation of the expression. */
4620
4621static tree
94edc4ab 4622cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4623{
4624 bool global_scope_p;
4625 bool array_p;
4626 tree expression;
4627
4628 /* Look for the optional `::' operator. */
4629 global_scope_p
4630 = (cp_parser_global_scope_opt (parser,
4631 /*current_scope_valid_p=*/false)
4632 != NULL_TREE);
4633 /* Look for the `delete' keyword. */
4634 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4635 /* See if the array syntax is in use. */
4636 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4637 {
4638 /* Consume the `[' token. */
4639 cp_lexer_consume_token (parser->lexer);
4640 /* Look for the `]' token. */
4641 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4642 /* Remember that this is the `[]' construct. */
4643 array_p = true;
4644 }
4645 else
4646 array_p = false;
4647
4648 /* Parse the cast-expression. */
d6b4ea85 4649 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4650
4651 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4652}
4653
4654/* Parse a cast-expression.
4655
4656 cast-expression:
4657 unary-expression
4658 ( type-id ) cast-expression
4659
4660 Returns a representation of the expression. */
4661
4662static tree
4663cp_parser_cast_expression (cp_parser *parser, bool address_p)
4664{
4665 /* If it's a `(', then we might be looking at a cast. */
4666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4667 {
4668 tree type = NULL_TREE;
4669 tree expr = NULL_TREE;
4670 bool compound_literal_p;
4671 const char *saved_message;
4672
4673 /* There's no way to know yet whether or not this is a cast.
4674 For example, `(int (3))' is a unary-expression, while `(int)
4675 3' is a cast. So, we resort to parsing tentatively. */
4676 cp_parser_parse_tentatively (parser);
4677 /* Types may not be defined in a cast. */
4678 saved_message = parser->type_definition_forbidden_message;
4679 parser->type_definition_forbidden_message
4680 = "types may not be defined in casts";
4681 /* Consume the `('. */
4682 cp_lexer_consume_token (parser->lexer);
4683 /* A very tricky bit is that `(struct S) { 3 }' is a
4684 compound-literal (which we permit in C++ as an extension).
4685 But, that construct is not a cast-expression -- it is a
4686 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4687 is legal; if the compound-literal were a cast-expression,
4688 you'd need an extra set of parentheses.) But, if we parse
4689 the type-id, and it happens to be a class-specifier, then we
4690 will commit to the parse at that point, because we cannot
4691 undo the action that is done when creating a new class. So,
4692 then we cannot back up and do a postfix-expression.
4693
4694 Therefore, we scan ahead to the closing `)', and check to see
4695 if the token after the `)' is a `{'. If so, we are not
4696 looking at a cast-expression.
4697
4698 Save tokens so that we can put them back. */
4699 cp_lexer_save_tokens (parser->lexer);
4700 /* Skip tokens until the next token is a closing parenthesis.
4701 If we find the closing `)', and the next token is a `{', then
4702 we are looking at a compound-literal. */
4703 compound_literal_p
a668c6ad
MM
4704 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4705 /*consume_paren=*/true)
a723baf1
MM
4706 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4707 /* Roll back the tokens we skipped. */
4708 cp_lexer_rollback_tokens (parser->lexer);
4709 /* If we were looking at a compound-literal, simulate an error
4710 so that the call to cp_parser_parse_definitely below will
4711 fail. */
4712 if (compound_literal_p)
4713 cp_parser_simulate_error (parser);
4714 else
4715 {
4716 /* Look for the type-id. */
4717 type = cp_parser_type_id (parser);
4718 /* Look for the closing `)'. */
4719 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4720 }
4721
4722 /* Restore the saved message. */
4723 parser->type_definition_forbidden_message = saved_message;
4724
bbaab916
NS
4725 /* If ok so far, parse the dependent expression. We cannot be
4726 sure it is a cast. Consider `(T ())'. It is a parenthesized
4727 ctor of T, but looks like a cast to function returning T
4728 without a dependent expression. */
4729 if (!cp_parser_error_occurred (parser))
d6b4ea85 4730 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4731
a723baf1
MM
4732 if (cp_parser_parse_definitely (parser))
4733 {
a723baf1
MM
4734 /* Warn about old-style casts, if so requested. */
4735 if (warn_old_style_cast
4736 && !in_system_header
4737 && !VOID_TYPE_P (type)
4738 && current_lang_name != lang_name_c)
4739 warning ("use of old-style cast");
14d22dd6
MM
4740
4741 /* Only type conversions to integral or enumeration types
4742 can be used in constant-expressions. */
67c03833 4743 if (parser->integral_constant_expression_p
14d22dd6
MM
4744 && !dependent_type_p (type)
4745 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4746 {
67c03833
JM
4747 if (!parser->allow_non_integral_constant_expression_p)
4748 return (cp_parser_non_integral_constant_expression
14d22dd6
MM
4749 ("a casts to a type other than an integral or "
4750 "enumeration type"));
67c03833 4751 parser->non_integral_constant_expression_p = true;
14d22dd6 4752 }
a723baf1
MM
4753 /* Perform the cast. */
4754 expr = build_c_cast (type, expr);
bbaab916 4755 return expr;
a723baf1 4756 }
a723baf1
MM
4757 }
4758
4759 /* If we get here, then it's not a cast, so it must be a
4760 unary-expression. */
4761 return cp_parser_unary_expression (parser, address_p);
4762}
4763
4764/* Parse a pm-expression.
4765
4766 pm-expression:
4767 cast-expression
4768 pm-expression .* cast-expression
4769 pm-expression ->* cast-expression
4770
4771 Returns a representation of the expression. */
4772
4773static tree
94edc4ab 4774cp_parser_pm_expression (cp_parser* parser)
a723baf1 4775{
d6b4ea85
MM
4776 static const cp_parser_token_tree_map map = {
4777 { CPP_DEREF_STAR, MEMBER_REF },
4778 { CPP_DOT_STAR, DOTSTAR_EXPR },
4779 { CPP_EOF, ERROR_MARK }
4780 };
a723baf1 4781
d6b4ea85
MM
4782 return cp_parser_binary_expression (parser, map,
4783 cp_parser_simple_cast_expression);
a723baf1
MM
4784}
4785
4786/* Parse a multiplicative-expression.
4787
4788 mulitplicative-expression:
4789 pm-expression
4790 multiplicative-expression * pm-expression
4791 multiplicative-expression / pm-expression
4792 multiplicative-expression % pm-expression
4793
4794 Returns a representation of the expression. */
4795
4796static tree
94edc4ab 4797cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4798{
39b1af70 4799 static const cp_parser_token_tree_map map = {
a723baf1
MM
4800 { CPP_MULT, MULT_EXPR },
4801 { CPP_DIV, TRUNC_DIV_EXPR },
4802 { CPP_MOD, TRUNC_MOD_EXPR },
4803 { CPP_EOF, ERROR_MARK }
4804 };
4805
4806 return cp_parser_binary_expression (parser,
4807 map,
4808 cp_parser_pm_expression);
4809}
4810
4811/* Parse an additive-expression.
4812
4813 additive-expression:
4814 multiplicative-expression
4815 additive-expression + multiplicative-expression
4816 additive-expression - multiplicative-expression
4817
4818 Returns a representation of the expression. */
4819
4820static tree
94edc4ab 4821cp_parser_additive_expression (cp_parser* parser)
a723baf1 4822{
39b1af70 4823 static const cp_parser_token_tree_map map = {
a723baf1
MM
4824 { CPP_PLUS, PLUS_EXPR },
4825 { CPP_MINUS, MINUS_EXPR },
4826 { CPP_EOF, ERROR_MARK }
4827 };
4828
4829 return cp_parser_binary_expression (parser,
4830 map,
4831 cp_parser_multiplicative_expression);
4832}
4833
4834/* Parse a shift-expression.
4835
4836 shift-expression:
4837 additive-expression
4838 shift-expression << additive-expression
4839 shift-expression >> additive-expression
4840
4841 Returns a representation of the expression. */
4842
4843static tree
94edc4ab 4844cp_parser_shift_expression (cp_parser* parser)
a723baf1 4845{
39b1af70 4846 static const cp_parser_token_tree_map map = {
a723baf1
MM
4847 { CPP_LSHIFT, LSHIFT_EXPR },
4848 { CPP_RSHIFT, RSHIFT_EXPR },
4849 { CPP_EOF, ERROR_MARK }
4850 };
4851
4852 return cp_parser_binary_expression (parser,
4853 map,
4854 cp_parser_additive_expression);
4855}
4856
4857/* Parse a relational-expression.
4858
4859 relational-expression:
4860 shift-expression
4861 relational-expression < shift-expression
4862 relational-expression > shift-expression
4863 relational-expression <= shift-expression
4864 relational-expression >= shift-expression
4865
4866 GNU Extension:
4867
4868 relational-expression:
4869 relational-expression <? shift-expression
4870 relational-expression >? shift-expression
4871
4872 Returns a representation of the expression. */
4873
4874static tree
94edc4ab 4875cp_parser_relational_expression (cp_parser* parser)
a723baf1 4876{
39b1af70 4877 static const cp_parser_token_tree_map map = {
a723baf1
MM
4878 { CPP_LESS, LT_EXPR },
4879 { CPP_GREATER, GT_EXPR },
4880 { CPP_LESS_EQ, LE_EXPR },
4881 { CPP_GREATER_EQ, GE_EXPR },
4882 { CPP_MIN, MIN_EXPR },
4883 { CPP_MAX, MAX_EXPR },
4884 { CPP_EOF, ERROR_MARK }
4885 };
4886
4887 return cp_parser_binary_expression (parser,
4888 map,
4889 cp_parser_shift_expression);
4890}
4891
4892/* Parse an equality-expression.
4893
4894 equality-expression:
4895 relational-expression
4896 equality-expression == relational-expression
4897 equality-expression != relational-expression
4898
4899 Returns a representation of the expression. */
4900
4901static tree
94edc4ab 4902cp_parser_equality_expression (cp_parser* parser)
a723baf1 4903{
39b1af70 4904 static const cp_parser_token_tree_map map = {
a723baf1
MM
4905 { CPP_EQ_EQ, EQ_EXPR },
4906 { CPP_NOT_EQ, NE_EXPR },
4907 { CPP_EOF, ERROR_MARK }
4908 };
4909
4910 return cp_parser_binary_expression (parser,
4911 map,
4912 cp_parser_relational_expression);
4913}
4914
4915/* Parse an and-expression.
4916
4917 and-expression:
4918 equality-expression
4919 and-expression & equality-expression
4920
4921 Returns a representation of the expression. */
4922
4923static tree
94edc4ab 4924cp_parser_and_expression (cp_parser* parser)
a723baf1 4925{
39b1af70 4926 static const cp_parser_token_tree_map map = {
a723baf1
MM
4927 { CPP_AND, BIT_AND_EXPR },
4928 { CPP_EOF, ERROR_MARK }
4929 };
4930
4931 return cp_parser_binary_expression (parser,
4932 map,
4933 cp_parser_equality_expression);
4934}
4935
4936/* Parse an exclusive-or-expression.
4937
4938 exclusive-or-expression:
4939 and-expression
4940 exclusive-or-expression ^ and-expression
4941
4942 Returns a representation of the expression. */
4943
4944static tree
94edc4ab 4945cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4946{
39b1af70 4947 static const cp_parser_token_tree_map map = {
a723baf1
MM
4948 { CPP_XOR, BIT_XOR_EXPR },
4949 { CPP_EOF, ERROR_MARK }
4950 };
4951
4952 return cp_parser_binary_expression (parser,
4953 map,
4954 cp_parser_and_expression);
4955}
4956
4957
4958/* Parse an inclusive-or-expression.
4959
4960 inclusive-or-expression:
4961 exclusive-or-expression
4962 inclusive-or-expression | exclusive-or-expression
4963
4964 Returns a representation of the expression. */
4965
4966static tree
94edc4ab 4967cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 4968{
39b1af70 4969 static const cp_parser_token_tree_map map = {
a723baf1
MM
4970 { CPP_OR, BIT_IOR_EXPR },
4971 { CPP_EOF, ERROR_MARK }
4972 };
4973
4974 return cp_parser_binary_expression (parser,
4975 map,
4976 cp_parser_exclusive_or_expression);
4977}
4978
4979/* Parse a logical-and-expression.
4980
4981 logical-and-expression:
4982 inclusive-or-expression
4983 logical-and-expression && inclusive-or-expression
4984
4985 Returns a representation of the expression. */
4986
4987static tree
94edc4ab 4988cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 4989{
39b1af70 4990 static const cp_parser_token_tree_map map = {
a723baf1
MM
4991 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4992 { CPP_EOF, ERROR_MARK }
4993 };
4994
4995 return cp_parser_binary_expression (parser,
4996 map,
4997 cp_parser_inclusive_or_expression);
4998}
4999
5000/* Parse a logical-or-expression.
5001
5002 logical-or-expression:
34cd5ae7 5003 logical-and-expression
a723baf1
MM
5004 logical-or-expression || logical-and-expression
5005
5006 Returns a representation of the expression. */
5007
5008static tree
94edc4ab 5009cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5010{
39b1af70 5011 static const cp_parser_token_tree_map map = {
a723baf1
MM
5012 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5013 { CPP_EOF, ERROR_MARK }
5014 };
5015
5016 return cp_parser_binary_expression (parser,
5017 map,
5018 cp_parser_logical_and_expression);
5019}
5020
a723baf1
MM
5021/* Parse the `? expression : assignment-expression' part of a
5022 conditional-expression. The LOGICAL_OR_EXPR is the
5023 logical-or-expression that started the conditional-expression.
5024 Returns a representation of the entire conditional-expression.
5025
39703eb9 5026 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5027
5028 ? expression : assignment-expression
5029
5030 GNU Extensions:
5031
5032 ? : assignment-expression */
5033
5034static tree
94edc4ab 5035cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5036{
5037 tree expr;
5038 tree assignment_expr;
5039
5040 /* Consume the `?' token. */
5041 cp_lexer_consume_token (parser->lexer);
5042 if (cp_parser_allow_gnu_extensions_p (parser)
5043 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5044 /* Implicit true clause. */
5045 expr = NULL_TREE;
5046 else
5047 /* Parse the expression. */
5048 expr = cp_parser_expression (parser);
5049
5050 /* The next token should be a `:'. */
5051 cp_parser_require (parser, CPP_COLON, "`:'");
5052 /* Parse the assignment-expression. */
5053 assignment_expr = cp_parser_assignment_expression (parser);
5054
5055 /* Build the conditional-expression. */
5056 return build_x_conditional_expr (logical_or_expr,
5057 expr,
5058 assignment_expr);
5059}
5060
5061/* Parse an assignment-expression.
5062
5063 assignment-expression:
5064 conditional-expression
5065 logical-or-expression assignment-operator assignment_expression
5066 throw-expression
5067
5068 Returns a representation for the expression. */
5069
5070static tree
94edc4ab 5071cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5072{
5073 tree expr;
5074
5075 /* If the next token is the `throw' keyword, then we're looking at
5076 a throw-expression. */
5077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5078 expr = cp_parser_throw_expression (parser);
5079 /* Otherwise, it must be that we are looking at a
5080 logical-or-expression. */
5081 else
5082 {
5083 /* Parse the logical-or-expression. */
5084 expr = cp_parser_logical_or_expression (parser);
5085 /* If the next token is a `?' then we're actually looking at a
5086 conditional-expression. */
5087 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5088 return cp_parser_question_colon_clause (parser, expr);
5089 else
5090 {
5091 enum tree_code assignment_operator;
5092
5093 /* If it's an assignment-operator, we're using the second
5094 production. */
5095 assignment_operator
5096 = cp_parser_assignment_operator_opt (parser);
5097 if (assignment_operator != ERROR_MARK)
5098 {
5099 tree rhs;
5100
5101 /* Parse the right-hand side of the assignment. */
5102 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5103 /* An assignment may not appear in a
5104 constant-expression. */
67c03833 5105 if (parser->integral_constant_expression_p)
14d22dd6 5106 {
67c03833
JM
5107 if (!parser->allow_non_integral_constant_expression_p)
5108 return cp_parser_non_integral_constant_expression ("an assignment");
5109 parser->non_integral_constant_expression_p = true;
14d22dd6 5110 }
34cd5ae7 5111 /* Build the assignment expression. */
a723baf1
MM
5112 expr = build_x_modify_expr (expr,
5113 assignment_operator,
5114 rhs);
5115 }
5116 }
5117 }
5118
5119 return expr;
5120}
5121
5122/* Parse an (optional) assignment-operator.
5123
5124 assignment-operator: one of
5125 = *= /= %= += -= >>= <<= &= ^= |=
5126
5127 GNU Extension:
5128
5129 assignment-operator: one of
5130 <?= >?=
5131
5132 If the next token is an assignment operator, the corresponding tree
5133 code is returned, and the token is consumed. For example, for
5134 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5135 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5136 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5137 operator, ERROR_MARK is returned. */
5138
5139static enum tree_code
94edc4ab 5140cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5141{
5142 enum tree_code op;
5143 cp_token *token;
5144
5145 /* Peek at the next toen. */
5146 token = cp_lexer_peek_token (parser->lexer);
5147
5148 switch (token->type)
5149 {
5150 case CPP_EQ:
5151 op = NOP_EXPR;
5152 break;
5153
5154 case CPP_MULT_EQ:
5155 op = MULT_EXPR;
5156 break;
5157
5158 case CPP_DIV_EQ:
5159 op = TRUNC_DIV_EXPR;
5160 break;
5161
5162 case CPP_MOD_EQ:
5163 op = TRUNC_MOD_EXPR;
5164 break;
5165
5166 case CPP_PLUS_EQ:
5167 op = PLUS_EXPR;
5168 break;
5169
5170 case CPP_MINUS_EQ:
5171 op = MINUS_EXPR;
5172 break;
5173
5174 case CPP_RSHIFT_EQ:
5175 op = RSHIFT_EXPR;
5176 break;
5177
5178 case CPP_LSHIFT_EQ:
5179 op = LSHIFT_EXPR;
5180 break;
5181
5182 case CPP_AND_EQ:
5183 op = BIT_AND_EXPR;
5184 break;
5185
5186 case CPP_XOR_EQ:
5187 op = BIT_XOR_EXPR;
5188 break;
5189
5190 case CPP_OR_EQ:
5191 op = BIT_IOR_EXPR;
5192 break;
5193
5194 case CPP_MIN_EQ:
5195 op = MIN_EXPR;
5196 break;
5197
5198 case CPP_MAX_EQ:
5199 op = MAX_EXPR;
5200 break;
5201
5202 default:
5203 /* Nothing else is an assignment operator. */
5204 op = ERROR_MARK;
5205 }
5206
5207 /* If it was an assignment operator, consume it. */
5208 if (op != ERROR_MARK)
5209 cp_lexer_consume_token (parser->lexer);
5210
5211 return op;
5212}
5213
5214/* Parse an expression.
5215
5216 expression:
5217 assignment-expression
5218 expression , assignment-expression
5219
5220 Returns a representation of the expression. */
5221
5222static tree
94edc4ab 5223cp_parser_expression (cp_parser* parser)
a723baf1
MM
5224{
5225 tree expression = NULL_TREE;
a723baf1
MM
5226
5227 while (true)
5228 {
5229 tree assignment_expression;
5230
5231 /* Parse the next assignment-expression. */
5232 assignment_expression
5233 = cp_parser_assignment_expression (parser);
5234 /* If this is the first assignment-expression, we can just
5235 save it away. */
5236 if (!expression)
5237 expression = assignment_expression;
a723baf1 5238 else
d17811fd
MM
5239 expression = build_x_compound_expr (expression,
5240 assignment_expression);
a723baf1
MM
5241 /* If the next token is not a comma, then we are done with the
5242 expression. */
5243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5244 break;
5245 /* Consume the `,'. */
5246 cp_lexer_consume_token (parser->lexer);
14d22dd6 5247 /* A comma operator cannot appear in a constant-expression. */
67c03833 5248 if (parser->integral_constant_expression_p)
14d22dd6 5249 {
67c03833 5250 if (!parser->allow_non_integral_constant_expression_p)
d17811fd 5251 expression
67c03833
JM
5252 = cp_parser_non_integral_constant_expression ("a comma operator");
5253 parser->non_integral_constant_expression_p = true;
14d22dd6 5254 }
14d22dd6 5255 }
a723baf1
MM
5256
5257 return expression;
5258}
5259
5260/* Parse a constant-expression.
5261
5262 constant-expression:
14d22dd6
MM
5263 conditional-expression
5264
5265 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5266 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5267 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5268 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5269
5270static tree
14d22dd6
MM
5271cp_parser_constant_expression (cp_parser* parser,
5272 bool allow_non_constant_p,
5273 bool *non_constant_p)
a723baf1 5274{
67c03833
JM
5275 bool saved_integral_constant_expression_p;
5276 bool saved_allow_non_integral_constant_expression_p;
5277 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5278 tree expression;
5279
5280 /* It might seem that we could simply parse the
5281 conditional-expression, and then check to see if it were
5282 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5283 one that the compiler can figure out is constant, possibly after
5284 doing some simplifications or optimizations. The standard has a
5285 precise definition of constant-expression, and we must honor
5286 that, even though it is somewhat more restrictive.
5287
5288 For example:
5289
5290 int i[(2, 3)];
5291
5292 is not a legal declaration, because `(2, 3)' is not a
5293 constant-expression. The `,' operator is forbidden in a
5294 constant-expression. However, GCC's constant-folding machinery
5295 will fold this operation to an INTEGER_CST for `3'. */
5296
14d22dd6 5297 /* Save the old settings. */
67c03833
JM
5298 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5299 saved_allow_non_integral_constant_expression_p
5300 = parser->allow_non_integral_constant_expression_p;
5301 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5302 /* We are now parsing a constant-expression. */
67c03833
JM
5303 parser->integral_constant_expression_p = true;
5304 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5305 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5306 /* Although the grammar says "conditional-expression", we parse an
5307 "assignment-expression", which also permits "throw-expression"
5308 and the use of assignment operators. In the case that
5309 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5310 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5311 actually essential that we look for an assignment-expression.
5312 For example, cp_parser_initializer_clauses uses this function to
5313 determine whether a particular assignment-expression is in fact
5314 constant. */
5315 expression = cp_parser_assignment_expression (parser);
14d22dd6 5316 /* Restore the old settings. */
67c03833
JM
5317 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5318 parser->allow_non_integral_constant_expression_p
5319 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5320 if (allow_non_constant_p)
67c03833
JM
5321 *non_constant_p = parser->non_integral_constant_expression_p;
5322 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5323
5324 return expression;
5325}
5326
5327/* Statements [gram.stmt.stmt] */
5328
5329/* Parse a statement.
5330
5331 statement:
5332 labeled-statement
5333 expression-statement
5334 compound-statement
5335 selection-statement
5336 iteration-statement
5337 jump-statement
5338 declaration-statement
5339 try-block */
5340
5341static void
a5bcc582 5342cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5343{
5344 tree statement;
5345 cp_token *token;
5346 int statement_line_number;
5347
5348 /* There is no statement yet. */
5349 statement = NULL_TREE;
5350 /* Peek at the next token. */
5351 token = cp_lexer_peek_token (parser->lexer);
5352 /* Remember the line number of the first token in the statement. */
82a98427 5353 statement_line_number = token->location.line;
a723baf1
MM
5354 /* If this is a keyword, then that will often determine what kind of
5355 statement we have. */
5356 if (token->type == CPP_KEYWORD)
5357 {
5358 enum rid keyword = token->keyword;
5359
5360 switch (keyword)
5361 {
5362 case RID_CASE:
5363 case RID_DEFAULT:
a5bcc582
NS
5364 statement = cp_parser_labeled_statement (parser,
5365 in_statement_expr_p);
a723baf1
MM
5366 break;
5367
5368 case RID_IF:
5369 case RID_SWITCH:
5370 statement = cp_parser_selection_statement (parser);
5371 break;
5372
5373 case RID_WHILE:
5374 case RID_DO:
5375 case RID_FOR:
5376 statement = cp_parser_iteration_statement (parser);
5377 break;
5378
5379 case RID_BREAK:
5380 case RID_CONTINUE:
5381 case RID_RETURN:
5382 case RID_GOTO:
5383 statement = cp_parser_jump_statement (parser);
5384 break;
5385
5386 case RID_TRY:
5387 statement = cp_parser_try_block (parser);
5388 break;
5389
5390 default:
5391 /* It might be a keyword like `int' that can start a
5392 declaration-statement. */
5393 break;
5394 }
5395 }
5396 else if (token->type == CPP_NAME)
5397 {
5398 /* If the next token is a `:', then we are looking at a
5399 labeled-statement. */
5400 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5401 if (token->type == CPP_COLON)
a5bcc582 5402 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5403 }
5404 /* Anything that starts with a `{' must be a compound-statement. */
5405 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5406 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5407
5408 /* Everything else must be a declaration-statement or an
5409 expression-statement. Try for the declaration-statement
5410 first, unless we are looking at a `;', in which case we know that
5411 we have an expression-statement. */
5412 if (!statement)
5413 {
5414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5415 {
5416 cp_parser_parse_tentatively (parser);
5417 /* Try to parse the declaration-statement. */
5418 cp_parser_declaration_statement (parser);
5419 /* If that worked, we're done. */
5420 if (cp_parser_parse_definitely (parser))
5421 return;
5422 }
5423 /* Look for an expression-statement instead. */
a5bcc582 5424 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5425 }
5426
5427 /* Set the line number for the statement. */
009ed910 5428 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5429 STMT_LINENO (statement) = statement_line_number;
5430}
5431
5432/* Parse a labeled-statement.
5433
5434 labeled-statement:
5435 identifier : statement
5436 case constant-expression : statement
5437 default : statement
5438
5439 Returns the new CASE_LABEL, for a `case' or `default' label. For
5440 an ordinary label, returns a LABEL_STMT. */
5441
5442static tree
a5bcc582 5443cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5444{
5445 cp_token *token;
0e59b3fb 5446 tree statement = error_mark_node;
a723baf1
MM
5447
5448 /* The next token should be an identifier. */
5449 token = cp_lexer_peek_token (parser->lexer);
5450 if (token->type != CPP_NAME
5451 && token->type != CPP_KEYWORD)
5452 {
5453 cp_parser_error (parser, "expected labeled-statement");
5454 return error_mark_node;
5455 }
5456
5457 switch (token->keyword)
5458 {
5459 case RID_CASE:
5460 {
5461 tree expr;
5462
5463 /* Consume the `case' token. */
5464 cp_lexer_consume_token (parser->lexer);
5465 /* Parse the constant-expression. */
14d22dd6 5466 expr = cp_parser_constant_expression (parser,
d17811fd 5467 /*allow_non_constant_p=*/false,
14d22dd6 5468 NULL);
0e59b3fb
MM
5469 if (!parser->in_switch_statement_p)
5470 error ("case label `%E' not within a switch statement", expr);
5471 else
5472 statement = finish_case_label (expr, NULL_TREE);
a723baf1
MM
5473 }
5474 break;
5475
5476 case RID_DEFAULT:
5477 /* Consume the `default' token. */
5478 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5479 if (!parser->in_switch_statement_p)
5480 error ("case label not within a switch statement");
5481 else
5482 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5483 break;
5484
5485 default:
5486 /* Anything else must be an ordinary label. */
5487 statement = finish_label_stmt (cp_parser_identifier (parser));
5488 break;
5489 }
5490
5491 /* Require the `:' token. */
5492 cp_parser_require (parser, CPP_COLON, "`:'");
5493 /* Parse the labeled statement. */
a5bcc582 5494 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5495
5496 /* Return the label, in the case of a `case' or `default' label. */
5497 return statement;
5498}
5499
5500/* Parse an expression-statement.
5501
5502 expression-statement:
5503 expression [opt] ;
5504
5505 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5506 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5507 indicates whether this expression-statement is part of an
5508 expression statement. */
a723baf1
MM
5509
5510static tree
a5bcc582 5511cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5512{
a5bcc582 5513 tree statement = NULL_TREE;
a723baf1 5514
a5bcc582 5515 /* If the next token is a ';', then there is no expression
04c06002 5516 statement. */
a723baf1 5517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582
NS
5518 statement = cp_parser_expression (parser);
5519
a723baf1 5520 /* Consume the final `;'. */
e0860732 5521 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5522
a5bcc582
NS
5523 if (in_statement_expr_p
5524 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5525 {
5526 /* This is the final expression statement of a statement
5527 expression. */
5528 statement = finish_stmt_expr_expr (statement);
5529 }
5530 else if (statement)
5531 statement = finish_expr_stmt (statement);
5532 else
5533 finish_stmt ();
5534
a723baf1
MM
5535 return statement;
5536}
5537
5538/* Parse a compound-statement.
5539
5540 compound-statement:
5541 { statement-seq [opt] }
5542
5543 Returns a COMPOUND_STMT representing the statement. */
5544
5545static tree
a5bcc582 5546cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5547{
5548 tree compound_stmt;
5549
5550 /* Consume the `{'. */
5551 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5552 return error_mark_node;
5553 /* Begin the compound-statement. */
7a3397c7 5554 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5555 /* Parse an (optional) statement-seq. */
a5bcc582 5556 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5557 /* Finish the compound-statement. */
7a3397c7 5558 finish_compound_stmt (compound_stmt);
a723baf1
MM
5559 /* Consume the `}'. */
5560 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5561
5562 return compound_stmt;
5563}
5564
5565/* Parse an (optional) statement-seq.
5566
5567 statement-seq:
5568 statement
5569 statement-seq [opt] statement */
5570
5571static void
a5bcc582 5572cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5573{
5574 /* Scan statements until there aren't any more. */
5575 while (true)
5576 {
5577 /* If we're looking at a `}', then we've run out of statements. */
5578 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5579 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5580 break;
5581
5582 /* Parse the statement. */
a5bcc582 5583 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5584 }
5585}
5586
5587/* Parse a selection-statement.
5588
5589 selection-statement:
5590 if ( condition ) statement
5591 if ( condition ) statement else statement
5592 switch ( condition ) statement
5593
5594 Returns the new IF_STMT or SWITCH_STMT. */
5595
5596static tree
94edc4ab 5597cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5598{
5599 cp_token *token;
5600 enum rid keyword;
5601
5602 /* Peek at the next token. */
5603 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5604
5605 /* See what kind of keyword it is. */
5606 keyword = token->keyword;
5607 switch (keyword)
5608 {
5609 case RID_IF:
5610 case RID_SWITCH:
5611 {
5612 tree statement;
5613 tree condition;
5614
5615 /* Look for the `('. */
5616 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5617 {
5618 cp_parser_skip_to_end_of_statement (parser);
5619 return error_mark_node;
5620 }
5621
5622 /* Begin the selection-statement. */
5623 if (keyword == RID_IF)
5624 statement = begin_if_stmt ();
5625 else
5626 statement = begin_switch_stmt ();
5627
5628 /* Parse the condition. */
5629 condition = cp_parser_condition (parser);
5630 /* Look for the `)'. */
5631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
5632 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5633 /*consume_paren=*/true);
a723baf1
MM
5634
5635 if (keyword == RID_IF)
5636 {
5637 tree then_stmt;
5638
5639 /* Add the condition. */
5640 finish_if_stmt_cond (condition, statement);
5641
5642 /* Parse the then-clause. */
5643 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5644 finish_then_clause (statement);
5645
5646 /* If the next token is `else', parse the else-clause. */
5647 if (cp_lexer_next_token_is_keyword (parser->lexer,
5648 RID_ELSE))
5649 {
5650 tree else_stmt;
5651
5652 /* Consume the `else' keyword. */
5653 cp_lexer_consume_token (parser->lexer);
5654 /* Parse the else-clause. */
5655 else_stmt
5656 = cp_parser_implicitly_scoped_statement (parser);
5657 finish_else_clause (statement);
5658 }
5659
5660 /* Now we're all done with the if-statement. */
5661 finish_if_stmt ();
5662 }
5663 else
5664 {
5665 tree body;
0e59b3fb 5666 bool in_switch_statement_p;
a723baf1
MM
5667
5668 /* Add the condition. */
5669 finish_switch_cond (condition, statement);
5670
5671 /* Parse the body of the switch-statement. */
0e59b3fb
MM
5672 in_switch_statement_p = parser->in_switch_statement_p;
5673 parser->in_switch_statement_p = true;
a723baf1 5674 body = cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5675 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
5676
5677 /* Now we're all done with the switch-statement. */
5678 finish_switch_stmt (statement);
5679 }
5680
5681 return statement;
5682 }
5683 break;
5684
5685 default:
5686 cp_parser_error (parser, "expected selection-statement");
5687 return error_mark_node;
5688 }
5689}
5690
5691/* Parse a condition.
5692
5693 condition:
5694 expression
5695 type-specifier-seq declarator = assignment-expression
5696
5697 GNU Extension:
5698
5699 condition:
5700 type-specifier-seq declarator asm-specification [opt]
5701 attributes [opt] = assignment-expression
5702
5703 Returns the expression that should be tested. */
5704
5705static tree
94edc4ab 5706cp_parser_condition (cp_parser* parser)
a723baf1
MM
5707{
5708 tree type_specifiers;
5709 const char *saved_message;
5710
5711 /* Try the declaration first. */
5712 cp_parser_parse_tentatively (parser);
5713 /* New types are not allowed in the type-specifier-seq for a
5714 condition. */
5715 saved_message = parser->type_definition_forbidden_message;
5716 parser->type_definition_forbidden_message
5717 = "types may not be defined in conditions";
5718 /* Parse the type-specifier-seq. */
5719 type_specifiers = cp_parser_type_specifier_seq (parser);
5720 /* Restore the saved message. */
5721 parser->type_definition_forbidden_message = saved_message;
5722 /* If all is well, we might be looking at a declaration. */
5723 if (!cp_parser_error_occurred (parser))
5724 {
5725 tree decl;
5726 tree asm_specification;
5727 tree attributes;
5728 tree declarator;
5729 tree initializer = NULL_TREE;
5730
5731 /* Parse the declarator. */
62b8a44e 5732 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
5733 /*ctor_dtor_or_conv_p=*/NULL,
5734 /*parenthesized_p=*/NULL);
a723baf1
MM
5735 /* Parse the attributes. */
5736 attributes = cp_parser_attributes_opt (parser);
5737 /* Parse the asm-specification. */
5738 asm_specification = cp_parser_asm_specification_opt (parser);
5739 /* If the next token is not an `=', then we might still be
5740 looking at an expression. For example:
5741
5742 if (A(a).x)
5743
5744 looks like a decl-specifier-seq and a declarator -- but then
5745 there is no `=', so this is an expression. */
5746 cp_parser_require (parser, CPP_EQ, "`='");
5747 /* If we did see an `=', then we are looking at a declaration
5748 for sure. */
5749 if (cp_parser_parse_definitely (parser))
5750 {
5751 /* Create the declaration. */
5752 decl = start_decl (declarator, type_specifiers,
5753 /*initialized_p=*/true,
5754 attributes, /*prefix_attributes=*/NULL_TREE);
5755 /* Parse the assignment-expression. */
5756 initializer = cp_parser_assignment_expression (parser);
5757
5758 /* Process the initializer. */
5759 cp_finish_decl (decl,
5760 initializer,
5761 asm_specification,
5762 LOOKUP_ONLYCONVERTING);
5763
5764 return convert_from_reference (decl);
5765 }
5766 }
5767 /* If we didn't even get past the declarator successfully, we are
5768 definitely not looking at a declaration. */
5769 else
5770 cp_parser_abort_tentative_parse (parser);
5771
5772 /* Otherwise, we are looking at an expression. */
5773 return cp_parser_expression (parser);
5774}
5775
5776/* Parse an iteration-statement.
5777
5778 iteration-statement:
5779 while ( condition ) statement
5780 do statement while ( expression ) ;
5781 for ( for-init-statement condition [opt] ; expression [opt] )
5782 statement
5783
5784 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5785
5786static tree
94edc4ab 5787cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5788{
5789 cp_token *token;
5790 enum rid keyword;
5791 tree statement;
0e59b3fb
MM
5792 bool in_iteration_statement_p;
5793
a723baf1
MM
5794
5795 /* Peek at the next token. */
5796 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5797 if (!token)
5798 return error_mark_node;
5799
0e59b3fb
MM
5800 /* Remember whether or not we are already within an iteration
5801 statement. */
5802 in_iteration_statement_p = parser->in_iteration_statement_p;
5803
a723baf1
MM
5804 /* See what kind of keyword it is. */
5805 keyword = token->keyword;
5806 switch (keyword)
5807 {
5808 case RID_WHILE:
5809 {
5810 tree condition;
5811
5812 /* Begin the while-statement. */
5813 statement = begin_while_stmt ();
5814 /* Look for the `('. */
5815 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5816 /* Parse the condition. */
5817 condition = cp_parser_condition (parser);
5818 finish_while_stmt_cond (condition, statement);
5819 /* Look for the `)'. */
5820 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5821 /* Parse the dependent statement. */
0e59b3fb 5822 parser->in_iteration_statement_p = true;
a723baf1 5823 cp_parser_already_scoped_statement (parser);
0e59b3fb 5824 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5825 /* We're done with the while-statement. */
5826 finish_while_stmt (statement);
5827 }
5828 break;
5829
5830 case RID_DO:
5831 {
5832 tree expression;
5833
5834 /* Begin the do-statement. */
5835 statement = begin_do_stmt ();
5836 /* Parse the body of the do-statement. */
0e59b3fb 5837 parser->in_iteration_statement_p = true;
a723baf1 5838 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5839 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5840 finish_do_body (statement);
5841 /* Look for the `while' keyword. */
5842 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5843 /* Look for the `('. */
5844 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5845 /* Parse the expression. */
5846 expression = cp_parser_expression (parser);
5847 /* We're done with the do-statement. */
5848 finish_do_stmt (expression, statement);
5849 /* Look for the `)'. */
5850 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5851 /* Look for the `;'. */
5852 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5853 }
5854 break;
5855
5856 case RID_FOR:
5857 {
5858 tree condition = NULL_TREE;
5859 tree expression = NULL_TREE;
5860
5861 /* Begin the for-statement. */
5862 statement = begin_for_stmt ();
5863 /* Look for the `('. */
5864 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5865 /* Parse the initialization. */
5866 cp_parser_for_init_statement (parser);
5867 finish_for_init_stmt (statement);
5868
5869 /* If there's a condition, process it. */
5870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5871 condition = cp_parser_condition (parser);
5872 finish_for_cond (condition, statement);
5873 /* Look for the `;'. */
5874 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5875
5876 /* If there's an expression, process it. */
5877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5878 expression = cp_parser_expression (parser);
5879 finish_for_expr (expression, statement);
5880 /* Look for the `)'. */
5881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5882
5883 /* Parse the body of the for-statement. */
0e59b3fb 5884 parser->in_iteration_statement_p = true;
a723baf1 5885 cp_parser_already_scoped_statement (parser);
0e59b3fb 5886 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5887
5888 /* We're done with the for-statement. */
5889 finish_for_stmt (statement);
5890 }
5891 break;
5892
5893 default:
5894 cp_parser_error (parser, "expected iteration-statement");
5895 statement = error_mark_node;
5896 break;
5897 }
5898
5899 return statement;
5900}
5901
5902/* Parse a for-init-statement.
5903
5904 for-init-statement:
5905 expression-statement
5906 simple-declaration */
5907
5908static void
94edc4ab 5909cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5910{
5911 /* If the next token is a `;', then we have an empty
34cd5ae7 5912 expression-statement. Grammatically, this is also a
a723baf1
MM
5913 simple-declaration, but an invalid one, because it does not
5914 declare anything. Therefore, if we did not handle this case
5915 specially, we would issue an error message about an invalid
5916 declaration. */
5917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5918 {
5919 /* We're going to speculatively look for a declaration, falling back
5920 to an expression, if necessary. */
5921 cp_parser_parse_tentatively (parser);
5922 /* Parse the declaration. */
5923 cp_parser_simple_declaration (parser,
5924 /*function_definition_allowed_p=*/false);
5925 /* If the tentative parse failed, then we shall need to look for an
5926 expression-statement. */
5927 if (cp_parser_parse_definitely (parser))
5928 return;
5929 }
5930
a5bcc582 5931 cp_parser_expression_statement (parser, false);
a723baf1
MM
5932}
5933
5934/* Parse a jump-statement.
5935
5936 jump-statement:
5937 break ;
5938 continue ;
5939 return expression [opt] ;
5940 goto identifier ;
5941
5942 GNU extension:
5943
5944 jump-statement:
5945 goto * expression ;
5946
5947 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5948 GOTO_STMT. */
5949
5950static tree
94edc4ab 5951cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
5952{
5953 tree statement = error_mark_node;
5954 cp_token *token;
5955 enum rid keyword;
5956
5957 /* Peek at the next token. */
5958 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5959 if (!token)
5960 return error_mark_node;
5961
5962 /* See what kind of keyword it is. */
5963 keyword = token->keyword;
5964 switch (keyword)
5965 {
5966 case RID_BREAK:
0e59b3fb
MM
5967 if (!parser->in_switch_statement_p
5968 && !parser->in_iteration_statement_p)
5969 {
5970 error ("break statement not within loop or switch");
5971 statement = error_mark_node;
5972 }
5973 else
5974 statement = finish_break_stmt ();
a723baf1
MM
5975 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5976 break;
5977
5978 case RID_CONTINUE:
0e59b3fb
MM
5979 if (!parser->in_iteration_statement_p)
5980 {
5981 error ("continue statement not within a loop");
5982 statement = error_mark_node;
5983 }
5984 else
5985 statement = finish_continue_stmt ();
a723baf1
MM
5986 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5987 break;
5988
5989 case RID_RETURN:
5990 {
5991 tree expr;
5992
5993 /* If the next token is a `;', then there is no
5994 expression. */
5995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5996 expr = cp_parser_expression (parser);
5997 else
5998 expr = NULL_TREE;
5999 /* Build the return-statement. */
6000 statement = finish_return_stmt (expr);
6001 /* Look for the final `;'. */
6002 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6003 }
6004 break;
6005
6006 case RID_GOTO:
6007 /* Create the goto-statement. */
6008 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6009 {
6010 /* Issue a warning about this use of a GNU extension. */
6011 if (pedantic)
6012 pedwarn ("ISO C++ forbids computed gotos");
6013 /* Consume the '*' token. */
6014 cp_lexer_consume_token (parser->lexer);
6015 /* Parse the dependent expression. */
6016 finish_goto_stmt (cp_parser_expression (parser));
6017 }
6018 else
6019 finish_goto_stmt (cp_parser_identifier (parser));
6020 /* Look for the final `;'. */
6021 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6022 break;
6023
6024 default:
6025 cp_parser_error (parser, "expected jump-statement");
6026 break;
6027 }
6028
6029 return statement;
6030}
6031
6032/* Parse a declaration-statement.
6033
6034 declaration-statement:
6035 block-declaration */
6036
6037static void
94edc4ab 6038cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6039{
6040 /* Parse the block-declaration. */
6041 cp_parser_block_declaration (parser, /*statement_p=*/true);
6042
6043 /* Finish off the statement. */
6044 finish_stmt ();
6045}
6046
6047/* Some dependent statements (like `if (cond) statement'), are
6048 implicitly in their own scope. In other words, if the statement is
6049 a single statement (as opposed to a compound-statement), it is
6050 none-the-less treated as if it were enclosed in braces. Any
6051 declarations appearing in the dependent statement are out of scope
6052 after control passes that point. This function parses a statement,
6053 but ensures that is in its own scope, even if it is not a
6054 compound-statement.
6055
6056 Returns the new statement. */
6057
6058static tree
94edc4ab 6059cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6060{
6061 tree statement;
6062
6063 /* If the token is not a `{', then we must take special action. */
6064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6065 {
6066 /* Create a compound-statement. */
7a3397c7 6067 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 6068 /* Parse the dependent-statement. */
a5bcc582 6069 cp_parser_statement (parser, false);
a723baf1 6070 /* Finish the dummy compound-statement. */
7a3397c7 6071 finish_compound_stmt (statement);
a723baf1
MM
6072 }
6073 /* Otherwise, we simply parse the statement directly. */
6074 else
a5bcc582 6075 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
6076
6077 /* Return the statement. */
6078 return statement;
6079}
6080
6081/* For some dependent statements (like `while (cond) statement'), we
6082 have already created a scope. Therefore, even if the dependent
6083 statement is a compound-statement, we do not want to create another
6084 scope. */
6085
6086static void
94edc4ab 6087cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6088{
6089 /* If the token is not a `{', then we must take special action. */
6090 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6091 {
6092 tree statement;
6093
6094 /* Create a compound-statement. */
7a3397c7 6095 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 6096 /* Parse the dependent-statement. */
a5bcc582 6097 cp_parser_statement (parser, false);
a723baf1 6098 /* Finish the dummy compound-statement. */
7a3397c7 6099 finish_compound_stmt (statement);
a723baf1
MM
6100 }
6101 /* Otherwise, we simply parse the statement directly. */
6102 else
a5bcc582 6103 cp_parser_statement (parser, false);
a723baf1
MM
6104}
6105
6106/* Declarations [gram.dcl.dcl] */
6107
6108/* Parse an optional declaration-sequence.
6109
6110 declaration-seq:
6111 declaration
6112 declaration-seq declaration */
6113
6114static void
94edc4ab 6115cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6116{
6117 while (true)
6118 {
6119 cp_token *token;
6120
6121 token = cp_lexer_peek_token (parser->lexer);
6122
6123 if (token->type == CPP_CLOSE_BRACE
6124 || token->type == CPP_EOF)
6125 break;
6126
6127 if (token->type == CPP_SEMICOLON)
6128 {
6129 /* A declaration consisting of a single semicolon is
6130 invalid. Allow it unless we're being pedantic. */
499b568f 6131 if (pedantic && !in_system_header)
a723baf1
MM
6132 pedwarn ("extra `;'");
6133 cp_lexer_consume_token (parser->lexer);
6134 continue;
6135 }
6136
c838d82f 6137 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6138 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6139 while (pending_lang_change > 0)
6140 {
6141 push_lang_context (lang_name_c);
6142 --pending_lang_change;
6143 }
6144 while (pending_lang_change < 0)
6145 {
6146 pop_lang_context ();
6147 ++pending_lang_change;
6148 }
6149
6150 /* Parse the declaration itself. */
a723baf1
MM
6151 cp_parser_declaration (parser);
6152 }
6153}
6154
6155/* Parse a declaration.
6156
6157 declaration:
6158 block-declaration
6159 function-definition
6160 template-declaration
6161 explicit-instantiation
6162 explicit-specialization
6163 linkage-specification
1092805d
MM
6164 namespace-definition
6165
6166 GNU extension:
6167
6168 declaration:
6169 __extension__ declaration */
a723baf1
MM
6170
6171static void
94edc4ab 6172cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6173{
6174 cp_token token1;
6175 cp_token token2;
1092805d
MM
6176 int saved_pedantic;
6177
6178 /* Check for the `__extension__' keyword. */
6179 if (cp_parser_extension_opt (parser, &saved_pedantic))
6180 {
6181 /* Parse the qualified declaration. */
6182 cp_parser_declaration (parser);
6183 /* Restore the PEDANTIC flag. */
6184 pedantic = saved_pedantic;
6185
6186 return;
6187 }
a723baf1
MM
6188
6189 /* Try to figure out what kind of declaration is present. */
6190 token1 = *cp_lexer_peek_token (parser->lexer);
6191 if (token1.type != CPP_EOF)
6192 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6193
6194 /* If the next token is `extern' and the following token is a string
6195 literal, then we have a linkage specification. */
6196 if (token1.keyword == RID_EXTERN
6197 && cp_parser_is_string_literal (&token2))
6198 cp_parser_linkage_specification (parser);
6199 /* If the next token is `template', then we have either a template
6200 declaration, an explicit instantiation, or an explicit
6201 specialization. */
6202 else if (token1.keyword == RID_TEMPLATE)
6203 {
6204 /* `template <>' indicates a template specialization. */
6205 if (token2.type == CPP_LESS
6206 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6207 cp_parser_explicit_specialization (parser);
6208 /* `template <' indicates a template declaration. */
6209 else if (token2.type == CPP_LESS)
6210 cp_parser_template_declaration (parser, /*member_p=*/false);
6211 /* Anything else must be an explicit instantiation. */
6212 else
6213 cp_parser_explicit_instantiation (parser);
6214 }
6215 /* If the next token is `export', then we have a template
6216 declaration. */
6217 else if (token1.keyword == RID_EXPORT)
6218 cp_parser_template_declaration (parser, /*member_p=*/false);
6219 /* If the next token is `extern', 'static' or 'inline' and the one
6220 after that is `template', we have a GNU extended explicit
6221 instantiation directive. */
6222 else if (cp_parser_allow_gnu_extensions_p (parser)
6223 && (token1.keyword == RID_EXTERN
6224 || token1.keyword == RID_STATIC
6225 || token1.keyword == RID_INLINE)
6226 && token2.keyword == RID_TEMPLATE)
6227 cp_parser_explicit_instantiation (parser);
6228 /* If the next token is `namespace', check for a named or unnamed
6229 namespace definition. */
6230 else if (token1.keyword == RID_NAMESPACE
6231 && (/* A named namespace definition. */
6232 (token2.type == CPP_NAME
6233 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6234 == CPP_OPEN_BRACE))
6235 /* An unnamed namespace definition. */
6236 || token2.type == CPP_OPEN_BRACE))
6237 cp_parser_namespace_definition (parser);
6238 /* We must have either a block declaration or a function
6239 definition. */
6240 else
6241 /* Try to parse a block-declaration, or a function-definition. */
6242 cp_parser_block_declaration (parser, /*statement_p=*/false);
6243}
6244
6245/* Parse a block-declaration.
6246
6247 block-declaration:
6248 simple-declaration
6249 asm-definition
6250 namespace-alias-definition
6251 using-declaration
6252 using-directive
6253
6254 GNU Extension:
6255
6256 block-declaration:
6257 __extension__ block-declaration
6258 label-declaration
6259
34cd5ae7 6260 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6261 part of a declaration-statement. */
6262
6263static void
6264cp_parser_block_declaration (cp_parser *parser,
6265 bool statement_p)
6266{
6267 cp_token *token1;
6268 int saved_pedantic;
6269
6270 /* Check for the `__extension__' keyword. */
6271 if (cp_parser_extension_opt (parser, &saved_pedantic))
6272 {
6273 /* Parse the qualified declaration. */
6274 cp_parser_block_declaration (parser, statement_p);
6275 /* Restore the PEDANTIC flag. */
6276 pedantic = saved_pedantic;
6277
6278 return;
6279 }
6280
6281 /* Peek at the next token to figure out which kind of declaration is
6282 present. */
6283 token1 = cp_lexer_peek_token (parser->lexer);
6284
6285 /* If the next keyword is `asm', we have an asm-definition. */
6286 if (token1->keyword == RID_ASM)
6287 {
6288 if (statement_p)
6289 cp_parser_commit_to_tentative_parse (parser);
6290 cp_parser_asm_definition (parser);
6291 }
6292 /* If the next keyword is `namespace', we have a
6293 namespace-alias-definition. */
6294 else if (token1->keyword == RID_NAMESPACE)
6295 cp_parser_namespace_alias_definition (parser);
6296 /* If the next keyword is `using', we have either a
6297 using-declaration or a using-directive. */
6298 else if (token1->keyword == RID_USING)
6299 {
6300 cp_token *token2;
6301
6302 if (statement_p)
6303 cp_parser_commit_to_tentative_parse (parser);
6304 /* If the token after `using' is `namespace', then we have a
6305 using-directive. */
6306 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6307 if (token2->keyword == RID_NAMESPACE)
6308 cp_parser_using_directive (parser);
6309 /* Otherwise, it's a using-declaration. */
6310 else
6311 cp_parser_using_declaration (parser);
6312 }
6313 /* If the next keyword is `__label__' we have a label declaration. */
6314 else if (token1->keyword == RID_LABEL)
6315 {
6316 if (statement_p)
6317 cp_parser_commit_to_tentative_parse (parser);
6318 cp_parser_label_declaration (parser);
6319 }
6320 /* Anything else must be a simple-declaration. */
6321 else
6322 cp_parser_simple_declaration (parser, !statement_p);
6323}
6324
6325/* Parse a simple-declaration.
6326
6327 simple-declaration:
6328 decl-specifier-seq [opt] init-declarator-list [opt] ;
6329
6330 init-declarator-list:
6331 init-declarator
6332 init-declarator-list , init-declarator
6333
34cd5ae7 6334 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6335 function-definition as a simple-declaration. */
a723baf1
MM
6336
6337static void
94edc4ab
NN
6338cp_parser_simple_declaration (cp_parser* parser,
6339 bool function_definition_allowed_p)
a723baf1
MM
6340{
6341 tree decl_specifiers;
6342 tree attributes;
560ad596 6343 int declares_class_or_enum;
a723baf1
MM
6344 bool saw_declarator;
6345
6346 /* Defer access checks until we know what is being declared; the
6347 checks for names appearing in the decl-specifier-seq should be
6348 done as if we were in the scope of the thing being declared. */
8d241e0b 6349 push_deferring_access_checks (dk_deferred);
cf22909c 6350
a723baf1
MM
6351 /* Parse the decl-specifier-seq. We have to keep track of whether
6352 or not the decl-specifier-seq declares a named class or
6353 enumeration type, since that is the only case in which the
6354 init-declarator-list is allowed to be empty.
6355
6356 [dcl.dcl]
6357
6358 In a simple-declaration, the optional init-declarator-list can be
6359 omitted only when declaring a class or enumeration, that is when
6360 the decl-specifier-seq contains either a class-specifier, an
6361 elaborated-type-specifier, or an enum-specifier. */
6362 decl_specifiers
6363 = cp_parser_decl_specifier_seq (parser,
6364 CP_PARSER_FLAGS_OPTIONAL,
6365 &attributes,
6366 &declares_class_or_enum);
6367 /* We no longer need to defer access checks. */
cf22909c 6368 stop_deferring_access_checks ();
24c0ef37 6369
39703eb9
MM
6370 /* In a block scope, a valid declaration must always have a
6371 decl-specifier-seq. By not trying to parse declarators, we can
6372 resolve the declaration/expression ambiguity more quickly. */
6373 if (!function_definition_allowed_p && !decl_specifiers)
6374 {
6375 cp_parser_error (parser, "expected declaration");
6376 goto done;
6377 }
6378
8fbc5ae7
MM
6379 /* If the next two tokens are both identifiers, the code is
6380 erroneous. The usual cause of this situation is code like:
6381
6382 T t;
6383
6384 where "T" should name a type -- but does not. */
6385 if (cp_parser_diagnose_invalid_type_name (parser))
6386 {
8d241e0b 6387 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6388 looking at a declaration. */
6389 cp_parser_commit_to_tentative_parse (parser);
6390 /* Give up. */
39703eb9 6391 goto done;
8fbc5ae7
MM
6392 }
6393
a723baf1
MM
6394 /* Keep going until we hit the `;' at the end of the simple
6395 declaration. */
6396 saw_declarator = false;
6397 while (cp_lexer_next_token_is_not (parser->lexer,
6398 CPP_SEMICOLON))
6399 {
6400 cp_token *token;
6401 bool function_definition_p;
560ad596 6402 tree decl;
a723baf1
MM
6403
6404 saw_declarator = true;
6405 /* Parse the init-declarator. */
560ad596
MM
6406 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6407 function_definition_allowed_p,
6408 /*member_p=*/false,
6409 declares_class_or_enum,
6410 &function_definition_p);
1fb3244a
MM
6411 /* If an error occurred while parsing tentatively, exit quickly.
6412 (That usually happens when in the body of a function; each
6413 statement is treated as a declaration-statement until proven
6414 otherwise.) */
6415 if (cp_parser_error_occurred (parser))
39703eb9 6416 goto done;
a723baf1
MM
6417 /* Handle function definitions specially. */
6418 if (function_definition_p)
6419 {
6420 /* If the next token is a `,', then we are probably
6421 processing something like:
6422
6423 void f() {}, *p;
6424
6425 which is erroneous. */
6426 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6427 error ("mixing declarations and function-definitions is forbidden");
6428 /* Otherwise, we're done with the list of declarators. */
6429 else
24c0ef37 6430 {
cf22909c 6431 pop_deferring_access_checks ();
24c0ef37
GS
6432 return;
6433 }
a723baf1
MM
6434 }
6435 /* The next token should be either a `,' or a `;'. */
6436 token = cp_lexer_peek_token (parser->lexer);
6437 /* If it's a `,', there are more declarators to come. */
6438 if (token->type == CPP_COMMA)
6439 cp_lexer_consume_token (parser->lexer);
6440 /* If it's a `;', we are done. */
6441 else if (token->type == CPP_SEMICOLON)
6442 break;
6443 /* Anything else is an error. */
6444 else
6445 {
6446 cp_parser_error (parser, "expected `,' or `;'");
6447 /* Skip tokens until we reach the end of the statement. */
6448 cp_parser_skip_to_end_of_statement (parser);
39703eb9 6449 goto done;
a723baf1
MM
6450 }
6451 /* After the first time around, a function-definition is not
6452 allowed -- even if it was OK at first. For example:
6453
6454 int i, f() {}
6455
6456 is not valid. */
6457 function_definition_allowed_p = false;
6458 }
6459
6460 /* Issue an error message if no declarators are present, and the
6461 decl-specifier-seq does not itself declare a class or
6462 enumeration. */
6463 if (!saw_declarator)
6464 {
6465 if (cp_parser_declares_only_class_p (parser))
6466 shadow_tag (decl_specifiers);
6467 /* Perform any deferred access checks. */
cf22909c 6468 perform_deferred_access_checks ();
a723baf1
MM
6469 }
6470
6471 /* Consume the `;'. */
6472 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6473
39703eb9
MM
6474 done:
6475 pop_deferring_access_checks ();
a723baf1
MM
6476}
6477
6478/* Parse a decl-specifier-seq.
6479
6480 decl-specifier-seq:
6481 decl-specifier-seq [opt] decl-specifier
6482
6483 decl-specifier:
6484 storage-class-specifier
6485 type-specifier
6486 function-specifier
6487 friend
6488 typedef
6489
6490 GNU Extension:
6491
6492 decl-specifier-seq:
6493 decl-specifier-seq [opt] attributes
6494
6495 Returns a TREE_LIST, giving the decl-specifiers in the order they
6496 appear in the source code. The TREE_VALUE of each node is the
6497 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6498 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6499 representation of a type-specifier, see cp_parser_type_specifier.
6500
6501 If there are attributes, they will be stored in *ATTRIBUTES,
6502 represented as described above cp_parser_attributes.
6503
6504 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6505 appears, and the entity that will be a friend is not going to be a
6506 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6507 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
560ad596
MM
6508 friendship is granted might not be a class.
6509
6510 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6511 *flags:
6512
6513 1: one of the decl-specifiers is an elaborated-type-specifier
6514 2: one of the decl-specifiers is an enum-specifier or a
6515 class-specifier
6516
6517 */
a723baf1
MM
6518
6519static tree
94edc4ab
NN
6520cp_parser_decl_specifier_seq (cp_parser* parser,
6521 cp_parser_flags flags,
6522 tree* attributes,
560ad596 6523 int* declares_class_or_enum)
a723baf1
MM
6524{
6525 tree decl_specs = NULL_TREE;
6526 bool friend_p = false;
f2ce60b8
NS
6527 bool constructor_possible_p = !parser->in_declarator_p;
6528
a723baf1 6529 /* Assume no class or enumeration type is declared. */
560ad596 6530 *declares_class_or_enum = 0;
a723baf1
MM
6531
6532 /* Assume there are no attributes. */
6533 *attributes = NULL_TREE;
6534
6535 /* Keep reading specifiers until there are no more to read. */
6536 while (true)
6537 {
6538 tree decl_spec = NULL_TREE;
6539 bool constructor_p;
6540 cp_token *token;
6541
6542 /* Peek at the next token. */
6543 token = cp_lexer_peek_token (parser->lexer);
6544 /* Handle attributes. */
6545 if (token->keyword == RID_ATTRIBUTE)
6546 {
6547 /* Parse the attributes. */
6548 decl_spec = cp_parser_attributes_opt (parser);
6549 /* Add them to the list. */
6550 *attributes = chainon (*attributes, decl_spec);
6551 continue;
6552 }
6553 /* If the next token is an appropriate keyword, we can simply
6554 add it to the list. */
6555 switch (token->keyword)
6556 {
6557 case RID_FRIEND:
6558 /* decl-specifier:
6559 friend */
1918facf
SB
6560 if (friend_p)
6561 error ("duplicate `friend'");
6562 else
6563 friend_p = true;
a723baf1
MM
6564 /* The representation of the specifier is simply the
6565 appropriate TREE_IDENTIFIER node. */
6566 decl_spec = token->value;
6567 /* Consume the token. */
6568 cp_lexer_consume_token (parser->lexer);
6569 break;
6570
6571 /* function-specifier:
6572 inline
6573 virtual
6574 explicit */
6575 case RID_INLINE:
6576 case RID_VIRTUAL:
6577 case RID_EXPLICIT:
6578 decl_spec = cp_parser_function_specifier_opt (parser);
6579 break;
6580
6581 /* decl-specifier:
6582 typedef */
6583 case RID_TYPEDEF:
6584 /* The representation of the specifier is simply the
6585 appropriate TREE_IDENTIFIER node. */
6586 decl_spec = token->value;
6587 /* Consume the token. */
6588 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6589 /* A constructor declarator cannot appear in a typedef. */
6590 constructor_possible_p = false;
c006d942
MM
6591 /* The "typedef" keyword can only occur in a declaration; we
6592 may as well commit at this point. */
6593 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6594 break;
6595
6596 /* storage-class-specifier:
6597 auto
6598 register
6599 static
6600 extern
6601 mutable
6602
6603 GNU Extension:
6604 thread */
6605 case RID_AUTO:
6606 case RID_REGISTER:
6607 case RID_STATIC:
6608 case RID_EXTERN:
6609 case RID_MUTABLE:
6610 case RID_THREAD:
6611 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6612 break;
6613
6614 default:
6615 break;
6616 }
6617
6618 /* Constructors are a special case. The `S' in `S()' is not a
6619 decl-specifier; it is the beginning of the declarator. */
6620 constructor_p = (!decl_spec
2050a1bb 6621 && constructor_possible_p
a723baf1
MM
6622 && cp_parser_constructor_declarator_p (parser,
6623 friend_p));
6624
6625 /* If we don't have a DECL_SPEC yet, then we must be looking at
6626 a type-specifier. */
6627 if (!decl_spec && !constructor_p)
6628 {
560ad596 6629 int decl_spec_declares_class_or_enum;
a723baf1
MM
6630 bool is_cv_qualifier;
6631
6632 decl_spec
6633 = cp_parser_type_specifier (parser, flags,
6634 friend_p,
6635 /*is_declaration=*/true,
6636 &decl_spec_declares_class_or_enum,
6637 &is_cv_qualifier);
6638
6639 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6640
6641 /* If this type-specifier referenced a user-defined type
6642 (a typedef, class-name, etc.), then we can't allow any
6643 more such type-specifiers henceforth.
6644
6645 [dcl.spec]
6646
6647 The longest sequence of decl-specifiers that could
6648 possibly be a type name is taken as the
6649 decl-specifier-seq of a declaration. The sequence shall
6650 be self-consistent as described below.
6651
6652 [dcl.type]
6653
6654 As a general rule, at most one type-specifier is allowed
6655 in the complete decl-specifier-seq of a declaration. The
6656 only exceptions are the following:
6657
6658 -- const or volatile can be combined with any other
6659 type-specifier.
6660
6661 -- signed or unsigned can be combined with char, long,
6662 short, or int.
6663
6664 -- ..
6665
6666 Example:
6667
6668 typedef char* Pc;
6669 void g (const int Pc);
6670
6671 Here, Pc is *not* part of the decl-specifier seq; it's
6672 the declarator. Therefore, once we see a type-specifier
6673 (other than a cv-qualifier), we forbid any additional
6674 user-defined types. We *do* still allow things like `int
6675 int' to be considered a decl-specifier-seq, and issue the
6676 error message later. */
6677 if (decl_spec && !is_cv_qualifier)
6678 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6679 /* A constructor declarator cannot follow a type-specifier. */
6680 if (decl_spec)
6681 constructor_possible_p = false;
a723baf1
MM
6682 }
6683
6684 /* If we still do not have a DECL_SPEC, then there are no more
6685 decl-specifiers. */
6686 if (!decl_spec)
6687 {
6688 /* Issue an error message, unless the entire construct was
6689 optional. */
6690 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6691 {
6692 cp_parser_error (parser, "expected decl specifier");
6693 return error_mark_node;
6694 }
6695
6696 break;
6697 }
6698
6699 /* Add the DECL_SPEC to the list of specifiers. */
6700 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6701
6702 /* After we see one decl-specifier, further decl-specifiers are
6703 always optional. */
6704 flags |= CP_PARSER_FLAGS_OPTIONAL;
6705 }
6706
6707 /* We have built up the DECL_SPECS in reverse order. Return them in
6708 the correct order. */
6709 return nreverse (decl_specs);
6710}
6711
6712/* Parse an (optional) storage-class-specifier.
6713
6714 storage-class-specifier:
6715 auto
6716 register
6717 static
6718 extern
6719 mutable
6720
6721 GNU Extension:
6722
6723 storage-class-specifier:
6724 thread
6725
6726 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6727
6728static tree
94edc4ab 6729cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6730{
6731 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6732 {
6733 case RID_AUTO:
6734 case RID_REGISTER:
6735 case RID_STATIC:
6736 case RID_EXTERN:
6737 case RID_MUTABLE:
6738 case RID_THREAD:
6739 /* Consume the token. */
6740 return cp_lexer_consume_token (parser->lexer)->value;
6741
6742 default:
6743 return NULL_TREE;
6744 }
6745}
6746
6747/* Parse an (optional) function-specifier.
6748
6749 function-specifier:
6750 inline
6751 virtual
6752 explicit
6753
6754 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6755
6756static tree
94edc4ab 6757cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6758{
6759 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6760 {
6761 case RID_INLINE:
6762 case RID_VIRTUAL:
6763 case RID_EXPLICIT:
6764 /* Consume the token. */
6765 return cp_lexer_consume_token (parser->lexer)->value;
6766
6767 default:
6768 return NULL_TREE;
6769 }
6770}
6771
6772/* Parse a linkage-specification.
6773
6774 linkage-specification:
6775 extern string-literal { declaration-seq [opt] }
6776 extern string-literal declaration */
6777
6778static void
94edc4ab 6779cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6780{
6781 cp_token *token;
6782 tree linkage;
6783
6784 /* Look for the `extern' keyword. */
6785 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6786
6787 /* Peek at the next token. */
6788 token = cp_lexer_peek_token (parser->lexer);
6789 /* If it's not a string-literal, then there's a problem. */
6790 if (!cp_parser_is_string_literal (token))
6791 {
6792 cp_parser_error (parser, "expected language-name");
6793 return;
6794 }
6795 /* Consume the token. */
6796 cp_lexer_consume_token (parser->lexer);
6797
6798 /* Transform the literal into an identifier. If the literal is a
6799 wide-character string, or contains embedded NULs, then we can't
6800 handle it as the user wants. */
6801 if (token->type == CPP_WSTRING
6802 || (strlen (TREE_STRING_POINTER (token->value))
6803 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6804 {
6805 cp_parser_error (parser, "invalid linkage-specification");
6806 /* Assume C++ linkage. */
6807 linkage = get_identifier ("c++");
6808 }
6809 /* If it's a simple string constant, things are easier. */
6810 else
6811 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6812
6813 /* We're now using the new linkage. */
6814 push_lang_context (linkage);
6815
6816 /* If the next token is a `{', then we're using the first
6817 production. */
6818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6819 {
6820 /* Consume the `{' token. */
6821 cp_lexer_consume_token (parser->lexer);
6822 /* Parse the declarations. */
6823 cp_parser_declaration_seq_opt (parser);
6824 /* Look for the closing `}'. */
6825 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6826 }
6827 /* Otherwise, there's just one declaration. */
6828 else
6829 {
6830 bool saved_in_unbraced_linkage_specification_p;
6831
6832 saved_in_unbraced_linkage_specification_p
6833 = parser->in_unbraced_linkage_specification_p;
6834 parser->in_unbraced_linkage_specification_p = true;
6835 have_extern_spec = true;
6836 cp_parser_declaration (parser);
6837 have_extern_spec = false;
6838 parser->in_unbraced_linkage_specification_p
6839 = saved_in_unbraced_linkage_specification_p;
6840 }
6841
6842 /* We're done with the linkage-specification. */
6843 pop_lang_context ();
6844}
6845
6846/* Special member functions [gram.special] */
6847
6848/* Parse a conversion-function-id.
6849
6850 conversion-function-id:
6851 operator conversion-type-id
6852
6853 Returns an IDENTIFIER_NODE representing the operator. */
6854
6855static tree
94edc4ab 6856cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6857{
6858 tree type;
6859 tree saved_scope;
6860 tree saved_qualifying_scope;
6861 tree saved_object_scope;
6862
6863 /* Look for the `operator' token. */
6864 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6865 return error_mark_node;
6866 /* When we parse the conversion-type-id, the current scope will be
6867 reset. However, we need that information in able to look up the
6868 conversion function later, so we save it here. */
6869 saved_scope = parser->scope;
6870 saved_qualifying_scope = parser->qualifying_scope;
6871 saved_object_scope = parser->object_scope;
6872 /* We must enter the scope of the class so that the names of
6873 entities declared within the class are available in the
6874 conversion-type-id. For example, consider:
6875
6876 struct S {
6877 typedef int I;
6878 operator I();
6879 };
6880
6881 S::operator I() { ... }
6882
6883 In order to see that `I' is a type-name in the definition, we
6884 must be in the scope of `S'. */
6885 if (saved_scope)
6886 push_scope (saved_scope);
6887 /* Parse the conversion-type-id. */
6888 type = cp_parser_conversion_type_id (parser);
6889 /* Leave the scope of the class, if any. */
6890 if (saved_scope)
6891 pop_scope (saved_scope);
6892 /* Restore the saved scope. */
6893 parser->scope = saved_scope;
6894 parser->qualifying_scope = saved_qualifying_scope;
6895 parser->object_scope = saved_object_scope;
6896 /* If the TYPE is invalid, indicate failure. */
6897 if (type == error_mark_node)
6898 return error_mark_node;
6899 return mangle_conv_op_name_for_type (type);
6900}
6901
6902/* Parse a conversion-type-id:
6903
6904 conversion-type-id:
6905 type-specifier-seq conversion-declarator [opt]
6906
6907 Returns the TYPE specified. */
6908
6909static tree
94edc4ab 6910cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6911{
6912 tree attributes;
6913 tree type_specifiers;
6914 tree declarator;
6915
6916 /* Parse the attributes. */
6917 attributes = cp_parser_attributes_opt (parser);
6918 /* Parse the type-specifiers. */
6919 type_specifiers = cp_parser_type_specifier_seq (parser);
6920 /* If that didn't work, stop. */
6921 if (type_specifiers == error_mark_node)
6922 return error_mark_node;
6923 /* Parse the conversion-declarator. */
6924 declarator = cp_parser_conversion_declarator_opt (parser);
6925
6926 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6927 /*initialized=*/0, &attributes);
6928}
6929
6930/* Parse an (optional) conversion-declarator.
6931
6932 conversion-declarator:
6933 ptr-operator conversion-declarator [opt]
6934
6935 Returns a representation of the declarator. See
6936 cp_parser_declarator for details. */
6937
6938static tree
94edc4ab 6939cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
6940{
6941 enum tree_code code;
6942 tree class_type;
6943 tree cv_qualifier_seq;
6944
6945 /* We don't know if there's a ptr-operator next, or not. */
6946 cp_parser_parse_tentatively (parser);
6947 /* Try the ptr-operator. */
6948 code = cp_parser_ptr_operator (parser, &class_type,
6949 &cv_qualifier_seq);
6950 /* If it worked, look for more conversion-declarators. */
6951 if (cp_parser_parse_definitely (parser))
6952 {
6953 tree declarator;
6954
6955 /* Parse another optional declarator. */
6956 declarator = cp_parser_conversion_declarator_opt (parser);
6957
6958 /* Create the representation of the declarator. */
6959 if (code == INDIRECT_REF)
6960 declarator = make_pointer_declarator (cv_qualifier_seq,
6961 declarator);
6962 else
6963 declarator = make_reference_declarator (cv_qualifier_seq,
6964 declarator);
6965
6966 /* Handle the pointer-to-member case. */
6967 if (class_type)
6968 declarator = build_nt (SCOPE_REF, class_type, declarator);
6969
6970 return declarator;
6971 }
6972
6973 return NULL_TREE;
6974}
6975
6976/* Parse an (optional) ctor-initializer.
6977
6978 ctor-initializer:
6979 : mem-initializer-list
6980
6981 Returns TRUE iff the ctor-initializer was actually present. */
6982
6983static bool
94edc4ab 6984cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
6985{
6986 /* If the next token is not a `:', then there is no
6987 ctor-initializer. */
6988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6989 {
6990 /* Do default initialization of any bases and members. */
6991 if (DECL_CONSTRUCTOR_P (current_function_decl))
6992 finish_mem_initializers (NULL_TREE);
6993
6994 return false;
6995 }
6996
6997 /* Consume the `:' token. */
6998 cp_lexer_consume_token (parser->lexer);
6999 /* And the mem-initializer-list. */
7000 cp_parser_mem_initializer_list (parser);
7001
7002 return true;
7003}
7004
7005/* Parse a mem-initializer-list.
7006
7007 mem-initializer-list:
7008 mem-initializer
7009 mem-initializer , mem-initializer-list */
7010
7011static void
94edc4ab 7012cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7013{
7014 tree mem_initializer_list = NULL_TREE;
7015
7016 /* Let the semantic analysis code know that we are starting the
7017 mem-initializer-list. */
0e136342
MM
7018 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7019 error ("only constructors take base initializers");
a723baf1
MM
7020
7021 /* Loop through the list. */
7022 while (true)
7023 {
7024 tree mem_initializer;
7025
7026 /* Parse the mem-initializer. */
7027 mem_initializer = cp_parser_mem_initializer (parser);
7028 /* Add it to the list, unless it was erroneous. */
7029 if (mem_initializer)
7030 {
7031 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7032 mem_initializer_list = mem_initializer;
7033 }
7034 /* If the next token is not a `,', we're done. */
7035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7036 break;
7037 /* Consume the `,' token. */
7038 cp_lexer_consume_token (parser->lexer);
7039 }
7040
7041 /* Perform semantic analysis. */
0e136342
MM
7042 if (DECL_CONSTRUCTOR_P (current_function_decl))
7043 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7044}
7045
7046/* Parse a mem-initializer.
7047
7048 mem-initializer:
7049 mem-initializer-id ( expression-list [opt] )
7050
7051 GNU extension:
7052
7053 mem-initializer:
34cd5ae7 7054 ( expression-list [opt] )
a723baf1
MM
7055
7056 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7057 class) or FIELD_DECL (for a non-static data member) to initialize;
7058 the TREE_VALUE is the expression-list. */
7059
7060static tree
94edc4ab 7061cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7062{
7063 tree mem_initializer_id;
7064 tree expression_list;
1f5a253a
NS
7065 tree member;
7066
a723baf1
MM
7067 /* Find out what is being initialized. */
7068 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7069 {
7070 pedwarn ("anachronistic old-style base class initializer");
7071 mem_initializer_id = NULL_TREE;
7072 }
7073 else
7074 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7075 member = expand_member_init (mem_initializer_id);
7076 if (member && !DECL_P (member))
7077 in_base_initializer = 1;
7efa3e22 7078
39703eb9
MM
7079 expression_list
7080 = cp_parser_parenthesized_expression_list (parser, false,
7081 /*non_constant_p=*/NULL);
7efa3e22 7082 if (!expression_list)
a723baf1 7083 expression_list = void_type_node;
a723baf1 7084
1f5a253a
NS
7085 in_base_initializer = 0;
7086
7087 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7088}
7089
7090/* Parse a mem-initializer-id.
7091
7092 mem-initializer-id:
7093 :: [opt] nested-name-specifier [opt] class-name
7094 identifier
7095
7096 Returns a TYPE indicating the class to be initializer for the first
7097 production. Returns an IDENTIFIER_NODE indicating the data member
7098 to be initialized for the second production. */
7099
7100static tree
94edc4ab 7101cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7102{
7103 bool global_scope_p;
7104 bool nested_name_specifier_p;
7105 tree id;
7106
7107 /* Look for the optional `::' operator. */
7108 global_scope_p
7109 = (cp_parser_global_scope_opt (parser,
7110 /*current_scope_valid_p=*/false)
7111 != NULL_TREE);
7112 /* Look for the optional nested-name-specifier. The simplest way to
7113 implement:
7114
7115 [temp.res]
7116
7117 The keyword `typename' is not permitted in a base-specifier or
7118 mem-initializer; in these contexts a qualified name that
7119 depends on a template-parameter is implicitly assumed to be a
7120 type name.
7121
7122 is to assume that we have seen the `typename' keyword at this
7123 point. */
7124 nested_name_specifier_p
7125 = (cp_parser_nested_name_specifier_opt (parser,
7126 /*typename_keyword_p=*/true,
7127 /*check_dependency_p=*/true,
a668c6ad
MM
7128 /*type_p=*/true,
7129 /*is_declaration=*/true)
a723baf1
MM
7130 != NULL_TREE);
7131 /* If there is a `::' operator or a nested-name-specifier, then we
7132 are definitely looking for a class-name. */
7133 if (global_scope_p || nested_name_specifier_p)
7134 return cp_parser_class_name (parser,
7135 /*typename_keyword_p=*/true,
7136 /*template_keyword_p=*/false,
7137 /*type_p=*/false,
a723baf1 7138 /*check_dependency_p=*/true,
a668c6ad
MM
7139 /*class_head_p=*/false,
7140 /*is_declaration=*/true);
a723baf1
MM
7141 /* Otherwise, we could also be looking for an ordinary identifier. */
7142 cp_parser_parse_tentatively (parser);
7143 /* Try a class-name. */
7144 id = cp_parser_class_name (parser,
7145 /*typename_keyword_p=*/true,
7146 /*template_keyword_p=*/false,
7147 /*type_p=*/false,
a723baf1 7148 /*check_dependency_p=*/true,
a668c6ad
MM
7149 /*class_head_p=*/false,
7150 /*is_declaration=*/true);
a723baf1
MM
7151 /* If we found one, we're done. */
7152 if (cp_parser_parse_definitely (parser))
7153 return id;
7154 /* Otherwise, look for an ordinary identifier. */
7155 return cp_parser_identifier (parser);
7156}
7157
7158/* Overloading [gram.over] */
7159
7160/* Parse an operator-function-id.
7161
7162 operator-function-id:
7163 operator operator
7164
7165 Returns an IDENTIFIER_NODE for the operator which is a
7166 human-readable spelling of the identifier, e.g., `operator +'. */
7167
7168static tree
94edc4ab 7169cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7170{
7171 /* Look for the `operator' keyword. */
7172 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7173 return error_mark_node;
7174 /* And then the name of the operator itself. */
7175 return cp_parser_operator (parser);
7176}
7177
7178/* Parse an operator.
7179
7180 operator:
7181 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7182 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7183 || ++ -- , ->* -> () []
7184
7185 GNU Extensions:
7186
7187 operator:
7188 <? >? <?= >?=
7189
7190 Returns an IDENTIFIER_NODE for the operator which is a
7191 human-readable spelling of the identifier, e.g., `operator +'. */
7192
7193static tree
94edc4ab 7194cp_parser_operator (cp_parser* parser)
a723baf1
MM
7195{
7196 tree id = NULL_TREE;
7197 cp_token *token;
7198
7199 /* Peek at the next token. */
7200 token = cp_lexer_peek_token (parser->lexer);
7201 /* Figure out which operator we have. */
7202 switch (token->type)
7203 {
7204 case CPP_KEYWORD:
7205 {
7206 enum tree_code op;
7207
7208 /* The keyword should be either `new' or `delete'. */
7209 if (token->keyword == RID_NEW)
7210 op = NEW_EXPR;
7211 else if (token->keyword == RID_DELETE)
7212 op = DELETE_EXPR;
7213 else
7214 break;
7215
7216 /* Consume the `new' or `delete' token. */
7217 cp_lexer_consume_token (parser->lexer);
7218
7219 /* Peek at the next token. */
7220 token = cp_lexer_peek_token (parser->lexer);
7221 /* If it's a `[' token then this is the array variant of the
7222 operator. */
7223 if (token->type == CPP_OPEN_SQUARE)
7224 {
7225 /* Consume the `[' token. */
7226 cp_lexer_consume_token (parser->lexer);
7227 /* Look for the `]' token. */
7228 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7229 id = ansi_opname (op == NEW_EXPR
7230 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7231 }
7232 /* Otherwise, we have the non-array variant. */
7233 else
7234 id = ansi_opname (op);
7235
7236 return id;
7237 }
7238
7239 case CPP_PLUS:
7240 id = ansi_opname (PLUS_EXPR);
7241 break;
7242
7243 case CPP_MINUS:
7244 id = ansi_opname (MINUS_EXPR);
7245 break;
7246
7247 case CPP_MULT:
7248 id = ansi_opname (MULT_EXPR);
7249 break;
7250
7251 case CPP_DIV:
7252 id = ansi_opname (TRUNC_DIV_EXPR);
7253 break;
7254
7255 case CPP_MOD:
7256 id = ansi_opname (TRUNC_MOD_EXPR);
7257 break;
7258
7259 case CPP_XOR:
7260 id = ansi_opname (BIT_XOR_EXPR);
7261 break;
7262
7263 case CPP_AND:
7264 id = ansi_opname (BIT_AND_EXPR);
7265 break;
7266
7267 case CPP_OR:
7268 id = ansi_opname (BIT_IOR_EXPR);
7269 break;
7270
7271 case CPP_COMPL:
7272 id = ansi_opname (BIT_NOT_EXPR);
7273 break;
7274
7275 case CPP_NOT:
7276 id = ansi_opname (TRUTH_NOT_EXPR);
7277 break;
7278
7279 case CPP_EQ:
7280 id = ansi_assopname (NOP_EXPR);
7281 break;
7282
7283 case CPP_LESS:
7284 id = ansi_opname (LT_EXPR);
7285 break;
7286
7287 case CPP_GREATER:
7288 id = ansi_opname (GT_EXPR);
7289 break;
7290
7291 case CPP_PLUS_EQ:
7292 id = ansi_assopname (PLUS_EXPR);
7293 break;
7294
7295 case CPP_MINUS_EQ:
7296 id = ansi_assopname (MINUS_EXPR);
7297 break;
7298
7299 case CPP_MULT_EQ:
7300 id = ansi_assopname (MULT_EXPR);
7301 break;
7302
7303 case CPP_DIV_EQ:
7304 id = ansi_assopname (TRUNC_DIV_EXPR);
7305 break;
7306
7307 case CPP_MOD_EQ:
7308 id = ansi_assopname (TRUNC_MOD_EXPR);
7309 break;
7310
7311 case CPP_XOR_EQ:
7312 id = ansi_assopname (BIT_XOR_EXPR);
7313 break;
7314
7315 case CPP_AND_EQ:
7316 id = ansi_assopname (BIT_AND_EXPR);
7317 break;
7318
7319 case CPP_OR_EQ:
7320 id = ansi_assopname (BIT_IOR_EXPR);
7321 break;
7322
7323 case CPP_LSHIFT:
7324 id = ansi_opname (LSHIFT_EXPR);
7325 break;
7326
7327 case CPP_RSHIFT:
7328 id = ansi_opname (RSHIFT_EXPR);
7329 break;
7330
7331 case CPP_LSHIFT_EQ:
7332 id = ansi_assopname (LSHIFT_EXPR);
7333 break;
7334
7335 case CPP_RSHIFT_EQ:
7336 id = ansi_assopname (RSHIFT_EXPR);
7337 break;
7338
7339 case CPP_EQ_EQ:
7340 id = ansi_opname (EQ_EXPR);
7341 break;
7342
7343 case CPP_NOT_EQ:
7344 id = ansi_opname (NE_EXPR);
7345 break;
7346
7347 case CPP_LESS_EQ:
7348 id = ansi_opname (LE_EXPR);
7349 break;
7350
7351 case CPP_GREATER_EQ:
7352 id = ansi_opname (GE_EXPR);
7353 break;
7354
7355 case CPP_AND_AND:
7356 id = ansi_opname (TRUTH_ANDIF_EXPR);
7357 break;
7358
7359 case CPP_OR_OR:
7360 id = ansi_opname (TRUTH_ORIF_EXPR);
7361 break;
7362
7363 case CPP_PLUS_PLUS:
7364 id = ansi_opname (POSTINCREMENT_EXPR);
7365 break;
7366
7367 case CPP_MINUS_MINUS:
7368 id = ansi_opname (PREDECREMENT_EXPR);
7369 break;
7370
7371 case CPP_COMMA:
7372 id = ansi_opname (COMPOUND_EXPR);
7373 break;
7374
7375 case CPP_DEREF_STAR:
7376 id = ansi_opname (MEMBER_REF);
7377 break;
7378
7379 case CPP_DEREF:
7380 id = ansi_opname (COMPONENT_REF);
7381 break;
7382
7383 case CPP_OPEN_PAREN:
7384 /* Consume the `('. */
7385 cp_lexer_consume_token (parser->lexer);
7386 /* Look for the matching `)'. */
7387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7388 return ansi_opname (CALL_EXPR);
7389
7390 case CPP_OPEN_SQUARE:
7391 /* Consume the `['. */
7392 cp_lexer_consume_token (parser->lexer);
7393 /* Look for the matching `]'. */
7394 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7395 return ansi_opname (ARRAY_REF);
7396
7397 /* Extensions. */
7398 case CPP_MIN:
7399 id = ansi_opname (MIN_EXPR);
7400 break;
7401
7402 case CPP_MAX:
7403 id = ansi_opname (MAX_EXPR);
7404 break;
7405
7406 case CPP_MIN_EQ:
7407 id = ansi_assopname (MIN_EXPR);
7408 break;
7409
7410 case CPP_MAX_EQ:
7411 id = ansi_assopname (MAX_EXPR);
7412 break;
7413
7414 default:
7415 /* Anything else is an error. */
7416 break;
7417 }
7418
7419 /* If we have selected an identifier, we need to consume the
7420 operator token. */
7421 if (id)
7422 cp_lexer_consume_token (parser->lexer);
7423 /* Otherwise, no valid operator name was present. */
7424 else
7425 {
7426 cp_parser_error (parser, "expected operator");
7427 id = error_mark_node;
7428 }
7429
7430 return id;
7431}
7432
7433/* Parse a template-declaration.
7434
7435 template-declaration:
7436 export [opt] template < template-parameter-list > declaration
7437
7438 If MEMBER_P is TRUE, this template-declaration occurs within a
7439 class-specifier.
7440
7441 The grammar rule given by the standard isn't correct. What
7442 is really meant is:
7443
7444 template-declaration:
7445 export [opt] template-parameter-list-seq
7446 decl-specifier-seq [opt] init-declarator [opt] ;
7447 export [opt] template-parameter-list-seq
7448 function-definition
7449
7450 template-parameter-list-seq:
7451 template-parameter-list-seq [opt]
7452 template < template-parameter-list > */
7453
7454static void
94edc4ab 7455cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7456{
7457 /* Check for `export'. */
7458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7459 {
7460 /* Consume the `export' token. */
7461 cp_lexer_consume_token (parser->lexer);
7462 /* Warn that we do not support `export'. */
7463 warning ("keyword `export' not implemented, and will be ignored");
7464 }
7465
7466 cp_parser_template_declaration_after_export (parser, member_p);
7467}
7468
7469/* Parse a template-parameter-list.
7470
7471 template-parameter-list:
7472 template-parameter
7473 template-parameter-list , template-parameter
7474
7475 Returns a TREE_LIST. Each node represents a template parameter.
7476 The nodes are connected via their TREE_CHAINs. */
7477
7478static tree
94edc4ab 7479cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7480{
7481 tree parameter_list = NULL_TREE;
7482
7483 while (true)
7484 {
7485 tree parameter;
7486 cp_token *token;
7487
7488 /* Parse the template-parameter. */
7489 parameter = cp_parser_template_parameter (parser);
7490 /* Add it to the list. */
7491 parameter_list = process_template_parm (parameter_list,
7492 parameter);
7493
7494 /* Peek at the next token. */
7495 token = cp_lexer_peek_token (parser->lexer);
7496 /* If it's not a `,', we're done. */
7497 if (token->type != CPP_COMMA)
7498 break;
7499 /* Otherwise, consume the `,' token. */
7500 cp_lexer_consume_token (parser->lexer);
7501 }
7502
7503 return parameter_list;
7504}
7505
7506/* Parse a template-parameter.
7507
7508 template-parameter:
7509 type-parameter
7510 parameter-declaration
7511
7512 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7513 TREE_PURPOSE is the default value, if any. */
7514
7515static tree
94edc4ab 7516cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7517{
7518 cp_token *token;
7519
7520 /* Peek at the next token. */
7521 token = cp_lexer_peek_token (parser->lexer);
7522 /* If it is `class' or `template', we have a type-parameter. */
7523 if (token->keyword == RID_TEMPLATE)
7524 return cp_parser_type_parameter (parser);
7525 /* If it is `class' or `typename' we do not know yet whether it is a
7526 type parameter or a non-type parameter. Consider:
7527
7528 template <typename T, typename T::X X> ...
7529
7530 or:
7531
7532 template <class C, class D*> ...
7533
7534 Here, the first parameter is a type parameter, and the second is
7535 a non-type parameter. We can tell by looking at the token after
7536 the identifier -- if it is a `,', `=', or `>' then we have a type
7537 parameter. */
7538 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7539 {
7540 /* Peek at the token after `class' or `typename'. */
7541 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7542 /* If it's an identifier, skip it. */
7543 if (token->type == CPP_NAME)
7544 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7545 /* Now, see if the token looks like the end of a template
7546 parameter. */
7547 if (token->type == CPP_COMMA
7548 || token->type == CPP_EQ
7549 || token->type == CPP_GREATER)
7550 return cp_parser_type_parameter (parser);
7551 }
7552
7553 /* Otherwise, it is a non-type parameter.
7554
7555 [temp.param]
7556
7557 When parsing a default template-argument for a non-type
7558 template-parameter, the first non-nested `>' is taken as the end
7559 of the template parameter-list rather than a greater-than
7560 operator. */
7561 return
4bb8ca28
MM
7562 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7563 /*parenthesized_p=*/NULL);
a723baf1
MM
7564}
7565
7566/* Parse a type-parameter.
7567
7568 type-parameter:
7569 class identifier [opt]
7570 class identifier [opt] = type-id
7571 typename identifier [opt]
7572 typename identifier [opt] = type-id
7573 template < template-parameter-list > class identifier [opt]
7574 template < template-parameter-list > class identifier [opt]
7575 = id-expression
7576
7577 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7578 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7579 the declaration of the parameter. */
7580
7581static tree
94edc4ab 7582cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7583{
7584 cp_token *token;
7585 tree parameter;
7586
7587 /* Look for a keyword to tell us what kind of parameter this is. */
7588 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7589 "`class', `typename', or `template'");
a723baf1
MM
7590 if (!token)
7591 return error_mark_node;
7592
7593 switch (token->keyword)
7594 {
7595 case RID_CLASS:
7596 case RID_TYPENAME:
7597 {
7598 tree identifier;
7599 tree default_argument;
7600
7601 /* If the next token is an identifier, then it names the
7602 parameter. */
7603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7604 identifier = cp_parser_identifier (parser);
7605 else
7606 identifier = NULL_TREE;
7607
7608 /* Create the parameter. */
7609 parameter = finish_template_type_parm (class_type_node, identifier);
7610
7611 /* If the next token is an `=', we have a default argument. */
7612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7613 {
7614 /* Consume the `=' token. */
7615 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7616 /* Parse the default-argument. */
a723baf1
MM
7617 default_argument = cp_parser_type_id (parser);
7618 }
7619 else
7620 default_argument = NULL_TREE;
7621
7622 /* Create the combined representation of the parameter and the
7623 default argument. */
c67d36d0 7624 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7625 }
7626 break;
7627
7628 case RID_TEMPLATE:
7629 {
7630 tree parameter_list;
7631 tree identifier;
7632 tree default_argument;
7633
7634 /* Look for the `<'. */
7635 cp_parser_require (parser, CPP_LESS, "`<'");
7636 /* Parse the template-parameter-list. */
7637 begin_template_parm_list ();
7638 parameter_list
7639 = cp_parser_template_parameter_list (parser);
7640 parameter_list = end_template_parm_list (parameter_list);
7641 /* Look for the `>'. */
7642 cp_parser_require (parser, CPP_GREATER, "`>'");
7643 /* Look for the `class' keyword. */
7644 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7645 /* If the next token is an `=', then there is a
7646 default-argument. If the next token is a `>', we are at
7647 the end of the parameter-list. If the next token is a `,',
7648 then we are at the end of this parameter. */
7649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7650 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7651 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7652 identifier = cp_parser_identifier (parser);
7653 else
7654 identifier = NULL_TREE;
7655 /* Create the template parameter. */
7656 parameter = finish_template_template_parm (class_type_node,
7657 identifier);
7658
7659 /* If the next token is an `=', then there is a
7660 default-argument. */
7661 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7662 {
7663 /* Consume the `='. */
7664 cp_lexer_consume_token (parser->lexer);
7665 /* Parse the id-expression. */
7666 default_argument
7667 = cp_parser_id_expression (parser,
7668 /*template_keyword_p=*/false,
7669 /*check_dependency_p=*/true,
f3c2dfc6
MM
7670 /*template_p=*/NULL,
7671 /*declarator_p=*/false);
a723baf1
MM
7672 /* Look up the name. */
7673 default_argument
7674 = cp_parser_lookup_name_simple (parser, default_argument);
7675 /* See if the default argument is valid. */
7676 default_argument
7677 = check_template_template_default_arg (default_argument);
7678 }
7679 else
7680 default_argument = NULL_TREE;
7681
7682 /* Create the combined representation of the parameter and the
7683 default argument. */
c67d36d0 7684 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7685 }
7686 break;
7687
7688 default:
7689 /* Anything else is an error. */
7690 cp_parser_error (parser,
7691 "expected `class', `typename', or `template'");
7692 parameter = error_mark_node;
7693 }
7694
7695 return parameter;
7696}
7697
7698/* Parse a template-id.
7699
7700 template-id:
7701 template-name < template-argument-list [opt] >
7702
7703 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7704 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7705 returned. Otherwise, if the template-name names a function, or set
7706 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7707 names a class, returns a TYPE_DECL for the specialization.
7708
7709 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7710 uninstantiated templates. */
7711
7712static tree
7713cp_parser_template_id (cp_parser *parser,
7714 bool template_keyword_p,
a668c6ad
MM
7715 bool check_dependency_p,
7716 bool is_declaration)
a723baf1
MM
7717{
7718 tree template;
7719 tree arguments;
a723baf1 7720 tree template_id;
a723baf1
MM
7721 ptrdiff_t start_of_id;
7722 tree access_check = NULL_TREE;
2050a1bb 7723 cp_token *next_token;
a668c6ad 7724 bool is_identifier;
a723baf1
MM
7725
7726 /* If the next token corresponds to a template-id, there is no need
7727 to reparse it. */
2050a1bb
MM
7728 next_token = cp_lexer_peek_token (parser->lexer);
7729 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7730 {
7731 tree value;
7732 tree check;
7733
7734 /* Get the stored value. */
7735 value = cp_lexer_consume_token (parser->lexer)->value;
7736 /* Perform any access checks that were deferred. */
7737 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7738 perform_or_defer_access_check (TREE_PURPOSE (check),
7739 TREE_VALUE (check));
a723baf1
MM
7740 /* Return the stored value. */
7741 return TREE_VALUE (value);
7742 }
7743
2050a1bb
MM
7744 /* Avoid performing name lookup if there is no possibility of
7745 finding a template-id. */
7746 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7747 || (next_token->type == CPP_NAME
7748 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7749 {
7750 cp_parser_error (parser, "expected template-id");
7751 return error_mark_node;
7752 }
7753
a723baf1
MM
7754 /* Remember where the template-id starts. */
7755 if (cp_parser_parsing_tentatively (parser)
7756 && !cp_parser_committed_to_tentative_parse (parser))
7757 {
2050a1bb 7758 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7759 start_of_id = cp_lexer_token_difference (parser->lexer,
7760 parser->lexer->first_token,
7761 next_token);
a723baf1
MM
7762 }
7763 else
7764 start_of_id = -1;
7765
8d241e0b 7766 push_deferring_access_checks (dk_deferred);
cf22909c 7767
a723baf1 7768 /* Parse the template-name. */
a668c6ad 7769 is_identifier = false;
a723baf1 7770 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
7771 check_dependency_p,
7772 is_declaration,
7773 &is_identifier);
7774 if (template == error_mark_node || is_identifier)
cf22909c
KL
7775 {
7776 pop_deferring_access_checks ();
a668c6ad 7777 return template;
cf22909c 7778 }
a723baf1
MM
7779
7780 /* Look for the `<' that starts the template-argument-list. */
7781 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7782 {
7783 pop_deferring_access_checks ();
7784 return error_mark_node;
7785 }
a723baf1 7786
ec75414f
MM
7787 /* Parse the arguments. */
7788 arguments = cp_parser_enclosed_template_argument_list (parser);
a723baf1
MM
7789
7790 /* Build a representation of the specialization. */
7791 if (TREE_CODE (template) == IDENTIFIER_NODE)
7792 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7793 else if (DECL_CLASS_TEMPLATE_P (template)
7794 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7795 template_id
7796 = finish_template_type (template, arguments,
7797 cp_lexer_next_token_is (parser->lexer,
7798 CPP_SCOPE));
7799 else
7800 {
7801 /* If it's not a class-template or a template-template, it should be
7802 a function-template. */
7803 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7804 || TREE_CODE (template) == OVERLOAD
7805 || BASELINK_P (template)),
7806 20010716);
7807
7808 template_id = lookup_template_function (template, arguments);
7809 }
7810
cf22909c
KL
7811 /* Retrieve any deferred checks. Do not pop this access checks yet
7812 so the memory will not be reclaimed during token replacing below. */
7813 access_check = get_deferred_access_checks ();
7814
a723baf1
MM
7815 /* If parsing tentatively, replace the sequence of tokens that makes
7816 up the template-id with a CPP_TEMPLATE_ID token. That way,
7817 should we re-parse the token stream, we will not have to repeat
7818 the effort required to do the parse, nor will we issue duplicate
7819 error messages about problems during instantiation of the
7820 template. */
7821 if (start_of_id >= 0)
7822 {
7823 cp_token *token;
a723baf1
MM
7824
7825 /* Find the token that corresponds to the start of the
7826 template-id. */
7827 token = cp_lexer_advance_token (parser->lexer,
7828 parser->lexer->first_token,
7829 start_of_id);
7830
a723baf1
MM
7831 /* Reset the contents of the START_OF_ID token. */
7832 token->type = CPP_TEMPLATE_ID;
7833 token->value = build_tree_list (access_check, template_id);
7834 token->keyword = RID_MAX;
7835 /* Purge all subsequent tokens. */
7836 cp_lexer_purge_tokens_after (parser->lexer, token);
7837 }
7838
cf22909c 7839 pop_deferring_access_checks ();
a723baf1
MM
7840 return template_id;
7841}
7842
7843/* Parse a template-name.
7844
7845 template-name:
7846 identifier
7847
7848 The standard should actually say:
7849
7850 template-name:
7851 identifier
7852 operator-function-id
7853 conversion-function-id
7854
7855 A defect report has been filed about this issue.
7856
7857 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7858 `template' keyword, in a construction like:
7859
7860 T::template f<3>()
7861
7862 In that case `f' is taken to be a template-name, even though there
7863 is no way of knowing for sure.
7864
7865 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7866 name refers to a set of overloaded functions, at least one of which
7867 is a template, or an IDENTIFIER_NODE with the name of the template,
7868 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7869 names are looked up inside uninstantiated templates. */
7870
7871static tree
94edc4ab
NN
7872cp_parser_template_name (cp_parser* parser,
7873 bool template_keyword_p,
a668c6ad
MM
7874 bool check_dependency_p,
7875 bool is_declaration,
7876 bool *is_identifier)
a723baf1
MM
7877{
7878 tree identifier;
7879 tree decl;
7880 tree fns;
7881
7882 /* If the next token is `operator', then we have either an
7883 operator-function-id or a conversion-function-id. */
7884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7885 {
7886 /* We don't know whether we're looking at an
7887 operator-function-id or a conversion-function-id. */
7888 cp_parser_parse_tentatively (parser);
7889 /* Try an operator-function-id. */
7890 identifier = cp_parser_operator_function_id (parser);
7891 /* If that didn't work, try a conversion-function-id. */
7892 if (!cp_parser_parse_definitely (parser))
7893 identifier = cp_parser_conversion_function_id (parser);
7894 }
7895 /* Look for the identifier. */
7896 else
7897 identifier = cp_parser_identifier (parser);
7898
7899 /* If we didn't find an identifier, we don't have a template-id. */
7900 if (identifier == error_mark_node)
7901 return error_mark_node;
7902
7903 /* If the name immediately followed the `template' keyword, then it
7904 is a template-name. However, if the next token is not `<', then
7905 we do not treat it as a template-name, since it is not being used
7906 as part of a template-id. This enables us to handle constructs
7907 like:
7908
7909 template <typename T> struct S { S(); };
7910 template <typename T> S<T>::S();
7911
7912 correctly. We would treat `S' as a template -- if it were `S<T>'
7913 -- but we do not if there is no `<'. */
a668c6ad
MM
7914
7915 if (processing_template_decl
a723baf1 7916 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
a668c6ad
MM
7917 {
7918 /* In a declaration, in a dependent context, we pretend that the
7919 "template" keyword was present in order to improve error
7920 recovery. For example, given:
7921
7922 template <typename T> void f(T::X<int>);
7923
7924 we want to treat "X<int>" as a template-id. */
7925 if (is_declaration
7926 && !template_keyword_p
7927 && parser->scope && TYPE_P (parser->scope)
7928 && dependent_type_p (parser->scope))
7929 {
7930 ptrdiff_t start;
7931 cp_token* token;
7932 /* Explain what went wrong. */
7933 error ("non-template `%D' used as template", identifier);
7934 error ("(use `%T::template %D' to indicate that it is a template)",
7935 parser->scope, identifier);
7936 /* If parsing tentatively, find the location of the "<"
7937 token. */
7938 if (cp_parser_parsing_tentatively (parser)
7939 && !cp_parser_committed_to_tentative_parse (parser))
7940 {
7941 cp_parser_simulate_error (parser);
7942 token = cp_lexer_peek_token (parser->lexer);
7943 token = cp_lexer_prev_token (parser->lexer, token);
7944 start = cp_lexer_token_difference (parser->lexer,
7945 parser->lexer->first_token,
7946 token);
7947 }
7948 else
7949 start = -1;
7950 /* Parse the template arguments so that we can issue error
7951 messages about them. */
7952 cp_lexer_consume_token (parser->lexer);
7953 cp_parser_enclosed_template_argument_list (parser);
7954 /* Skip tokens until we find a good place from which to
7955 continue parsing. */
7956 cp_parser_skip_to_closing_parenthesis (parser,
7957 /*recovering=*/true,
7958 /*or_comma=*/true,
7959 /*consume_paren=*/false);
7960 /* If parsing tentatively, permanently remove the
7961 template argument list. That will prevent duplicate
7962 error messages from being issued about the missing
7963 "template" keyword. */
7964 if (start >= 0)
7965 {
7966 token = cp_lexer_advance_token (parser->lexer,
7967 parser->lexer->first_token,
7968 start);
7969 cp_lexer_purge_tokens_after (parser->lexer, token);
7970 }
7971 if (is_identifier)
7972 *is_identifier = true;
7973 return identifier;
7974 }
7975 if (template_keyword_p)
7976 return identifier;
7977 }
a723baf1
MM
7978
7979 /* Look up the name. */
7980 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 7981 /*is_type=*/false,
eea9800f 7982 /*is_namespace=*/false,
a723baf1
MM
7983 check_dependency_p);
7984 decl = maybe_get_template_decl_from_type_decl (decl);
7985
7986 /* If DECL is a template, then the name was a template-name. */
7987 if (TREE_CODE (decl) == TEMPLATE_DECL)
7988 ;
7989 else
7990 {
7991 /* The standard does not explicitly indicate whether a name that
7992 names a set of overloaded declarations, some of which are
7993 templates, is a template-name. However, such a name should
7994 be a template-name; otherwise, there is no way to form a
7995 template-id for the overloaded templates. */
7996 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7997 if (TREE_CODE (fns) == OVERLOAD)
7998 {
7999 tree fn;
8000
8001 for (fn = fns; fn; fn = OVL_NEXT (fn))
8002 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8003 break;
8004 }
8005 else
8006 {
8007 /* Otherwise, the name does not name a template. */
8008 cp_parser_error (parser, "expected template-name");
8009 return error_mark_node;
8010 }
8011 }
8012
8013 /* If DECL is dependent, and refers to a function, then just return
8014 its name; we will look it up again during template instantiation. */
8015 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8016 {
8017 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8018 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8019 return identifier;
8020 }
8021
8022 return decl;
8023}
8024
8025/* Parse a template-argument-list.
8026
8027 template-argument-list:
8028 template-argument
8029 template-argument-list , template-argument
8030
04c06002 8031 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8032
8033static tree
94edc4ab 8034cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8035{
bf12d54d
NS
8036 tree fixed_args[10];
8037 unsigned n_args = 0;
8038 unsigned alloced = 10;
8039 tree *arg_ary = fixed_args;
8040 tree vec;
4bb8ca28 8041 bool saved_in_template_argument_list_p;
a723baf1 8042
4bb8ca28
MM
8043 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8044 parser->in_template_argument_list_p = true;
bf12d54d 8045 do
a723baf1
MM
8046 {
8047 tree argument;
8048
bf12d54d 8049 if (n_args)
04c06002 8050 /* Consume the comma. */
bf12d54d
NS
8051 cp_lexer_consume_token (parser->lexer);
8052
a723baf1
MM
8053 /* Parse the template-argument. */
8054 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8055 if (n_args == alloced)
8056 {
8057 alloced *= 2;
8058
8059 if (arg_ary == fixed_args)
8060 {
8061 arg_ary = xmalloc (sizeof (tree) * alloced);
8062 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8063 }
8064 else
8065 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8066 }
8067 arg_ary[n_args++] = argument;
a723baf1 8068 }
bf12d54d
NS
8069 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8070
8071 vec = make_tree_vec (n_args);
a723baf1 8072
bf12d54d
NS
8073 while (n_args--)
8074 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8075
8076 if (arg_ary != fixed_args)
8077 free (arg_ary);
4bb8ca28 8078 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8079 return vec;
a723baf1
MM
8080}
8081
8082/* Parse a template-argument.
8083
8084 template-argument:
8085 assignment-expression
8086 type-id
8087 id-expression
8088
8089 The representation is that of an assignment-expression, type-id, or
8090 id-expression -- except that the qualified id-expression is
8091 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
8092 OVERLOAD.
8093
8094 Although the standard says "assignment-expression", it forbids
8095 throw-expressions or assignments in the template argument.
8096 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8097
8098static tree
94edc4ab 8099cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8100{
8101 tree argument;
8102 bool template_p;
d17811fd 8103 bool address_p;
4d5297fa 8104 bool maybe_type_id = false;
d17811fd 8105 cp_token *token;
b3445994 8106 cp_id_kind idk;
d17811fd 8107 tree qualifying_class;
a723baf1
MM
8108
8109 /* There's really no way to know what we're looking at, so we just
8110 try each alternative in order.
8111
8112 [temp.arg]
8113
8114 In a template-argument, an ambiguity between a type-id and an
8115 expression is resolved to a type-id, regardless of the form of
8116 the corresponding template-parameter.
8117
8118 Therefore, we try a type-id first. */
8119 cp_parser_parse_tentatively (parser);
a723baf1 8120 argument = cp_parser_type_id (parser);
4d5297fa
GB
8121 /* If there was no error parsing the type-id but the next token is a '>>',
8122 we probably found a typo for '> >'. But there are type-id which are
8123 also valid expressions. For instance:
8124
8125 struct X { int operator >> (int); };
8126 template <int V> struct Foo {};
8127 Foo<X () >> 5> r;
8128
8129 Here 'X()' is a valid type-id of a function type, but the user just
8130 wanted to write the expression "X() >> 5". Thus, we remember that we
8131 found a valid type-id, but we still try to parse the argument as an
8132 expression to see what happens. */
8133 if (!cp_parser_error_occurred (parser)
8134 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8135 {
8136 maybe_type_id = true;
8137 cp_parser_abort_tentative_parse (parser);
8138 }
8139 else
8140 {
8141 /* If the next token isn't a `,' or a `>', then this argument wasn't
8142 really finished. This means that the argument is not a valid
8143 type-id. */
8144 if (!cp_parser_next_token_ends_template_argument_p (parser))
8145 cp_parser_error (parser, "expected template-argument");
8146 /* If that worked, we're done. */
8147 if (cp_parser_parse_definitely (parser))
8148 return argument;
8149 }
a723baf1
MM
8150 /* We're still not sure what the argument will be. */
8151 cp_parser_parse_tentatively (parser);
8152 /* Try a template. */
8153 argument = cp_parser_id_expression (parser,
8154 /*template_keyword_p=*/false,
8155 /*check_dependency_p=*/true,
f3c2dfc6
MM
8156 &template_p,
8157 /*declarator_p=*/false);
a723baf1
MM
8158 /* If the next token isn't a `,' or a `>', then this argument wasn't
8159 really finished. */
d17811fd 8160 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8161 cp_parser_error (parser, "expected template-argument");
8162 if (!cp_parser_error_occurred (parser))
8163 {
8164 /* Figure out what is being referred to. */
8165 argument = cp_parser_lookup_name_simple (parser, argument);
8166 if (template_p)
8167 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8168 TREE_OPERAND (argument, 1),
78757caa 8169 tf_error);
a723baf1
MM
8170 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8171 cp_parser_error (parser, "expected template-name");
8172 }
8173 if (cp_parser_parse_definitely (parser))
8174 return argument;
d17811fd
MM
8175 /* It must be a non-type argument. There permitted cases are given
8176 in [temp.arg.nontype]:
8177
8178 -- an integral constant-expression of integral or enumeration
8179 type; or
8180
8181 -- the name of a non-type template-parameter; or
8182
8183 -- the name of an object or function with external linkage...
8184
8185 -- the address of an object or function with external linkage...
8186
04c06002 8187 -- a pointer to member... */
d17811fd
MM
8188 /* Look for a non-type template parameter. */
8189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8190 {
8191 cp_parser_parse_tentatively (parser);
8192 argument = cp_parser_primary_expression (parser,
8193 &idk,
8194 &qualifying_class);
8195 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8196 || !cp_parser_next_token_ends_template_argument_p (parser))
8197 cp_parser_simulate_error (parser);
8198 if (cp_parser_parse_definitely (parser))
8199 return argument;
8200 }
8201 /* If the next token is "&", the argument must be the address of an
8202 object or function with external linkage. */
8203 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8204 if (address_p)
8205 cp_lexer_consume_token (parser->lexer);
8206 /* See if we might have an id-expression. */
8207 token = cp_lexer_peek_token (parser->lexer);
8208 if (token->type == CPP_NAME
8209 || token->keyword == RID_OPERATOR
8210 || token->type == CPP_SCOPE
8211 || token->type == CPP_TEMPLATE_ID
8212 || token->type == CPP_NESTED_NAME_SPECIFIER)
8213 {
8214 cp_parser_parse_tentatively (parser);
8215 argument = cp_parser_primary_expression (parser,
8216 &idk,
8217 &qualifying_class);
8218 if (cp_parser_error_occurred (parser)
8219 || !cp_parser_next_token_ends_template_argument_p (parser))
8220 cp_parser_abort_tentative_parse (parser);
8221 else
8222 {
8223 if (qualifying_class)
8224 argument = finish_qualified_id_expr (qualifying_class,
8225 argument,
8226 /*done=*/true,
8227 address_p);
8228 if (TREE_CODE (argument) == VAR_DECL)
8229 {
8230 /* A variable without external linkage might still be a
8231 valid constant-expression, so no error is issued here
8232 if the external-linkage check fails. */
8233 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8234 cp_parser_simulate_error (parser);
8235 }
8236 else if (is_overloaded_fn (argument))
8237 /* All overloaded functions are allowed; if the external
8238 linkage test does not pass, an error will be issued
8239 later. */
8240 ;
8241 else if (address_p
8242 && (TREE_CODE (argument) == OFFSET_REF
8243 || TREE_CODE (argument) == SCOPE_REF))
8244 /* A pointer-to-member. */
8245 ;
8246 else
8247 cp_parser_simulate_error (parser);
8248
8249 if (cp_parser_parse_definitely (parser))
8250 {
8251 if (address_p)
8252 argument = build_x_unary_op (ADDR_EXPR, argument);
8253 return argument;
8254 }
8255 }
8256 }
8257 /* If the argument started with "&", there are no other valid
8258 alternatives at this point. */
8259 if (address_p)
8260 {
8261 cp_parser_error (parser, "invalid non-type template argument");
8262 return error_mark_node;
8263 }
4d5297fa
GB
8264 /* If the argument wasn't successfully parsed as a type-id followed
8265 by '>>', the argument can only be a constant expression now.
8266 Otherwise, we try parsing the constant-expression tentatively,
8267 because the argument could really be a type-id. */
8268 if (maybe_type_id)
8269 cp_parser_parse_tentatively (parser);
d17811fd
MM
8270 argument = cp_parser_constant_expression (parser,
8271 /*allow_non_constant_p=*/false,
8272 /*non_constant_p=*/NULL);
4d5297fa
GB
8273 argument = cp_parser_fold_non_dependent_expr (argument);
8274 if (!maybe_type_id)
8275 return argument;
8276 if (!cp_parser_next_token_ends_template_argument_p (parser))
8277 cp_parser_error (parser, "expected template-argument");
8278 if (cp_parser_parse_definitely (parser))
8279 return argument;
8280 /* We did our best to parse the argument as a non type-id, but that
8281 was the only alternative that matched (albeit with a '>' after
8282 it). We can assume it's just a typo from the user, and a
8283 diagnostic will then be issued. */
8284 return cp_parser_type_id (parser);
a723baf1
MM
8285}
8286
8287/* Parse an explicit-instantiation.
8288
8289 explicit-instantiation:
8290 template declaration
8291
8292 Although the standard says `declaration', what it really means is:
8293
8294 explicit-instantiation:
8295 template decl-specifier-seq [opt] declarator [opt] ;
8296
8297 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8298 supposed to be allowed. A defect report has been filed about this
8299 issue.
8300
8301 GNU Extension:
8302
8303 explicit-instantiation:
8304 storage-class-specifier template
8305 decl-specifier-seq [opt] declarator [opt] ;
8306 function-specifier template
8307 decl-specifier-seq [opt] declarator [opt] ; */
8308
8309static void
94edc4ab 8310cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8311{
560ad596 8312 int declares_class_or_enum;
a723baf1
MM
8313 tree decl_specifiers;
8314 tree attributes;
8315 tree extension_specifier = NULL_TREE;
8316
8317 /* Look for an (optional) storage-class-specifier or
8318 function-specifier. */
8319 if (cp_parser_allow_gnu_extensions_p (parser))
8320 {
8321 extension_specifier
8322 = cp_parser_storage_class_specifier_opt (parser);
8323 if (!extension_specifier)
8324 extension_specifier = cp_parser_function_specifier_opt (parser);
8325 }
8326
8327 /* Look for the `template' keyword. */
8328 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8329 /* Let the front end know that we are processing an explicit
8330 instantiation. */
8331 begin_explicit_instantiation ();
8332 /* [temp.explicit] says that we are supposed to ignore access
8333 control while processing explicit instantiation directives. */
78757caa 8334 push_deferring_access_checks (dk_no_check);
a723baf1
MM
8335 /* Parse a decl-specifier-seq. */
8336 decl_specifiers
8337 = cp_parser_decl_specifier_seq (parser,
8338 CP_PARSER_FLAGS_OPTIONAL,
8339 &attributes,
8340 &declares_class_or_enum);
8341 /* If there was exactly one decl-specifier, and it declared a class,
8342 and there's no declarator, then we have an explicit type
8343 instantiation. */
8344 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8345 {
8346 tree type;
8347
8348 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8349 /* Turn access control back on for names used during
8350 template instantiation. */
8351 pop_deferring_access_checks ();
a723baf1
MM
8352 if (type)
8353 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8354 }
8355 else
8356 {
8357 tree declarator;
8358 tree decl;
8359
8360 /* Parse the declarator. */
8361 declarator
62b8a44e 8362 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
8363 /*ctor_dtor_or_conv_p=*/NULL,
8364 /*parenthesized_p=*/NULL);
560ad596
MM
8365 cp_parser_check_for_definition_in_return_type (declarator,
8366 declares_class_or_enum);
a723baf1
MM
8367 decl = grokdeclarator (declarator, decl_specifiers,
8368 NORMAL, 0, NULL);
b7fc8b57
KL
8369 /* Turn access control back on for names used during
8370 template instantiation. */
8371 pop_deferring_access_checks ();
a723baf1
MM
8372 /* Do the explicit instantiation. */
8373 do_decl_instantiation (decl, extension_specifier);
8374 }
8375 /* We're done with the instantiation. */
8376 end_explicit_instantiation ();
a723baf1 8377
e0860732 8378 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8379}
8380
8381/* Parse an explicit-specialization.
8382
8383 explicit-specialization:
8384 template < > declaration
8385
8386 Although the standard says `declaration', what it really means is:
8387
8388 explicit-specialization:
8389 template <> decl-specifier [opt] init-declarator [opt] ;
8390 template <> function-definition
8391 template <> explicit-specialization
8392 template <> template-declaration */
8393
8394static void
94edc4ab 8395cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8396{
8397 /* Look for the `template' keyword. */
8398 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8399 /* Look for the `<'. */
8400 cp_parser_require (parser, CPP_LESS, "`<'");
8401 /* Look for the `>'. */
8402 cp_parser_require (parser, CPP_GREATER, "`>'");
8403 /* We have processed another parameter list. */
8404 ++parser->num_template_parameter_lists;
8405 /* Let the front end know that we are beginning a specialization. */
8406 begin_specialization ();
8407
8408 /* If the next keyword is `template', we need to figure out whether
8409 or not we're looking a template-declaration. */
8410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8411 {
8412 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8413 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8414 cp_parser_template_declaration_after_export (parser,
8415 /*member_p=*/false);
8416 else
8417 cp_parser_explicit_specialization (parser);
8418 }
8419 else
8420 /* Parse the dependent declaration. */
8421 cp_parser_single_declaration (parser,
8422 /*member_p=*/false,
8423 /*friend_p=*/NULL);
8424
8425 /* We're done with the specialization. */
8426 end_specialization ();
8427 /* We're done with this parameter list. */
8428 --parser->num_template_parameter_lists;
8429}
8430
8431/* Parse a type-specifier.
8432
8433 type-specifier:
8434 simple-type-specifier
8435 class-specifier
8436 enum-specifier
8437 elaborated-type-specifier
8438 cv-qualifier
8439
8440 GNU Extension:
8441
8442 type-specifier:
8443 __complex__
8444
8445 Returns a representation of the type-specifier. If the
8446 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8447 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8448 For a class-specifier, enum-specifier, or elaborated-type-specifier
8449 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8450
8451 If IS_FRIEND is TRUE then this type-specifier is being declared a
8452 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8453 appearing in a decl-specifier-seq.
8454
8455 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8456 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8457 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8458 if a type is declared; 2 if it is defined. Otherwise, it is set to
8459 zero.
a723baf1
MM
8460
8461 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8462 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8463 is set to FALSE. */
8464
8465static tree
94edc4ab
NN
8466cp_parser_type_specifier (cp_parser* parser,
8467 cp_parser_flags flags,
8468 bool is_friend,
8469 bool is_declaration,
560ad596 8470 int* declares_class_or_enum,
94edc4ab 8471 bool* is_cv_qualifier)
a723baf1
MM
8472{
8473 tree type_spec = NULL_TREE;
8474 cp_token *token;
8475 enum rid keyword;
8476
8477 /* Assume this type-specifier does not declare a new type. */
8478 if (declares_class_or_enum)
8479 *declares_class_or_enum = false;
8480 /* And that it does not specify a cv-qualifier. */
8481 if (is_cv_qualifier)
8482 *is_cv_qualifier = false;
8483 /* Peek at the next token. */
8484 token = cp_lexer_peek_token (parser->lexer);
8485
8486 /* If we're looking at a keyword, we can use that to guide the
8487 production we choose. */
8488 keyword = token->keyword;
8489 switch (keyword)
8490 {
8491 /* Any of these indicate either a class-specifier, or an
8492 elaborated-type-specifier. */
8493 case RID_CLASS:
8494 case RID_STRUCT:
8495 case RID_UNION:
8496 case RID_ENUM:
8497 /* Parse tentatively so that we can back up if we don't find a
8498 class-specifier or enum-specifier. */
8499 cp_parser_parse_tentatively (parser);
8500 /* Look for the class-specifier or enum-specifier. */
8501 if (keyword == RID_ENUM)
8502 type_spec = cp_parser_enum_specifier (parser);
8503 else
8504 type_spec = cp_parser_class_specifier (parser);
8505
8506 /* If that worked, we're done. */
8507 if (cp_parser_parse_definitely (parser))
8508 {
8509 if (declares_class_or_enum)
560ad596 8510 *declares_class_or_enum = 2;
a723baf1
MM
8511 return type_spec;
8512 }
8513
8514 /* Fall through. */
8515
8516 case RID_TYPENAME:
8517 /* Look for an elaborated-type-specifier. */
8518 type_spec = cp_parser_elaborated_type_specifier (parser,
8519 is_friend,
8520 is_declaration);
8521 /* We're declaring a class or enum -- unless we're using
8522 `typename'. */
8523 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8524 *declares_class_or_enum = 1;
a723baf1
MM
8525 return type_spec;
8526
8527 case RID_CONST:
8528 case RID_VOLATILE:
8529 case RID_RESTRICT:
8530 type_spec = cp_parser_cv_qualifier_opt (parser);
8531 /* Even though we call a routine that looks for an optional
8532 qualifier, we know that there should be one. */
8533 my_friendly_assert (type_spec != NULL, 20000328);
8534 /* This type-specifier was a cv-qualified. */
8535 if (is_cv_qualifier)
8536 *is_cv_qualifier = true;
8537
8538 return type_spec;
8539
8540 case RID_COMPLEX:
8541 /* The `__complex__' keyword is a GNU extension. */
8542 return cp_lexer_consume_token (parser->lexer)->value;
8543
8544 default:
8545 break;
8546 }
8547
8548 /* If we do not already have a type-specifier, assume we are looking
8549 at a simple-type-specifier. */
4b0d3cbe
MM
8550 type_spec = cp_parser_simple_type_specifier (parser, flags,
8551 /*identifier_p=*/true);
a723baf1
MM
8552
8553 /* If we didn't find a type-specifier, and a type-specifier was not
8554 optional in this context, issue an error message. */
8555 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8556 {
8557 cp_parser_error (parser, "expected type specifier");
8558 return error_mark_node;
8559 }
8560
8561 return type_spec;
8562}
8563
8564/* Parse a simple-type-specifier.
8565
8566 simple-type-specifier:
8567 :: [opt] nested-name-specifier [opt] type-name
8568 :: [opt] nested-name-specifier template template-id
8569 char
8570 wchar_t
8571 bool
8572 short
8573 int
8574 long
8575 signed
8576 unsigned
8577 float
8578 double
8579 void
8580
8581 GNU Extension:
8582
8583 simple-type-specifier:
8584 __typeof__ unary-expression
8585 __typeof__ ( type-id )
8586
8587 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8588 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8589 For the first two productions, and if IDENTIFIER_P is false, the
8590 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8591
8592static tree
4b0d3cbe
MM
8593cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8594 bool identifier_p)
a723baf1
MM
8595{
8596 tree type = NULL_TREE;
8597 cp_token *token;
8598
8599 /* Peek at the next token. */
8600 token = cp_lexer_peek_token (parser->lexer);
8601
8602 /* If we're looking at a keyword, things are easy. */
8603 switch (token->keyword)
8604 {
8605 case RID_CHAR:
4b0d3cbe
MM
8606 type = char_type_node;
8607 break;
a723baf1 8608 case RID_WCHAR:
4b0d3cbe
MM
8609 type = wchar_type_node;
8610 break;
a723baf1 8611 case RID_BOOL:
4b0d3cbe
MM
8612 type = boolean_type_node;
8613 break;
a723baf1 8614 case RID_SHORT:
4b0d3cbe
MM
8615 type = short_integer_type_node;
8616 break;
a723baf1 8617 case RID_INT:
4b0d3cbe
MM
8618 type = integer_type_node;
8619 break;
a723baf1 8620 case RID_LONG:
4b0d3cbe
MM
8621 type = long_integer_type_node;
8622 break;
a723baf1 8623 case RID_SIGNED:
4b0d3cbe
MM
8624 type = integer_type_node;
8625 break;
a723baf1 8626 case RID_UNSIGNED:
4b0d3cbe
MM
8627 type = unsigned_type_node;
8628 break;
a723baf1 8629 case RID_FLOAT:
4b0d3cbe
MM
8630 type = float_type_node;
8631 break;
a723baf1 8632 case RID_DOUBLE:
4b0d3cbe
MM
8633 type = double_type_node;
8634 break;
a723baf1 8635 case RID_VOID:
4b0d3cbe
MM
8636 type = void_type_node;
8637 break;
a723baf1
MM
8638
8639 case RID_TYPEOF:
8640 {
8641 tree operand;
8642
8643 /* Consume the `typeof' token. */
8644 cp_lexer_consume_token (parser->lexer);
04c06002 8645 /* Parse the operand to `typeof'. */
a723baf1
MM
8646 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8647 /* If it is not already a TYPE, take its type. */
8648 if (!TYPE_P (operand))
8649 operand = finish_typeof (operand);
8650
8651 return operand;
8652 }
8653
8654 default:
8655 break;
8656 }
8657
4b0d3cbe
MM
8658 /* If the type-specifier was for a built-in type, we're done. */
8659 if (type)
8660 {
8661 tree id;
8662
8663 /* Consume the token. */
8664 id = cp_lexer_consume_token (parser->lexer)->value;
8665 return identifier_p ? id : TYPE_NAME (type);
8666 }
8667
a723baf1
MM
8668 /* The type-specifier must be a user-defined type. */
8669 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8670 {
8671 /* Don't gobble tokens or issue error messages if this is an
8672 optional type-specifier. */
8673 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8674 cp_parser_parse_tentatively (parser);
8675
8676 /* Look for the optional `::' operator. */
8677 cp_parser_global_scope_opt (parser,
8678 /*current_scope_valid_p=*/false);
8679 /* Look for the nested-name specifier. */
8680 cp_parser_nested_name_specifier_opt (parser,
8681 /*typename_keyword_p=*/false,
8682 /*check_dependency_p=*/true,
a668c6ad
MM
8683 /*type_p=*/false,
8684 /*is_declaration=*/false);
a723baf1
MM
8685 /* If we have seen a nested-name-specifier, and the next token
8686 is `template', then we are using the template-id production. */
8687 if (parser->scope
8688 && cp_parser_optional_template_keyword (parser))
8689 {
8690 /* Look for the template-id. */
8691 type = cp_parser_template_id (parser,
8692 /*template_keyword_p=*/true,
a668c6ad
MM
8693 /*check_dependency_p=*/true,
8694 /*is_declaration=*/false);
a723baf1
MM
8695 /* If the template-id did not name a type, we are out of
8696 luck. */
8697 if (TREE_CODE (type) != TYPE_DECL)
8698 {
8699 cp_parser_error (parser, "expected template-id for type");
8700 type = NULL_TREE;
8701 }
8702 }
8703 /* Otherwise, look for a type-name. */
8704 else
4bb8ca28 8705 type = cp_parser_type_name (parser);
a723baf1
MM
8706 /* If it didn't work out, we don't have a TYPE. */
8707 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8708 && !cp_parser_parse_definitely (parser))
8709 type = NULL_TREE;
8710 }
8711
8712 /* If we didn't get a type-name, issue an error message. */
8713 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8714 {
8715 cp_parser_error (parser, "expected type-name");
8716 return error_mark_node;
8717 }
8718
a668c6ad
MM
8719 /* There is no valid C++ program where a non-template type is
8720 followed by a "<". That usually indicates that the user thought
8721 that the type was a template. */
4bb8ca28 8722 if (type && type != error_mark_node)
ee43dab5 8723 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 8724
a723baf1
MM
8725 return type;
8726}
8727
8728/* Parse a type-name.
8729
8730 type-name:
8731 class-name
8732 enum-name
8733 typedef-name
8734
8735 enum-name:
8736 identifier
8737
8738 typedef-name:
8739 identifier
8740
8741 Returns a TYPE_DECL for the the type. */
8742
8743static tree
94edc4ab 8744cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8745{
8746 tree type_decl;
8747 tree identifier;
8748
8749 /* We can't know yet whether it is a class-name or not. */
8750 cp_parser_parse_tentatively (parser);
8751 /* Try a class-name. */
8752 type_decl = cp_parser_class_name (parser,
8753 /*typename_keyword_p=*/false,
8754 /*template_keyword_p=*/false,
8755 /*type_p=*/false,
a723baf1 8756 /*check_dependency_p=*/true,
a668c6ad
MM
8757 /*class_head_p=*/false,
8758 /*is_declaration=*/false);
a723baf1
MM
8759 /* If it's not a class-name, keep looking. */
8760 if (!cp_parser_parse_definitely (parser))
8761 {
8762 /* It must be a typedef-name or an enum-name. */
8763 identifier = cp_parser_identifier (parser);
8764 if (identifier == error_mark_node)
8765 return error_mark_node;
8766
8767 /* Look up the type-name. */
8768 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8769 /* Issue an error if we did not find a type-name. */
8770 if (TREE_CODE (type_decl) != TYPE_DECL)
8771 {
4bb8ca28
MM
8772 if (!cp_parser_simulate_error (parser))
8773 cp_parser_name_lookup_error (parser, identifier, type_decl,
8774 "is not a type");
a723baf1
MM
8775 type_decl = error_mark_node;
8776 }
8777 /* Remember that the name was used in the definition of the
8778 current class so that we can check later to see if the
8779 meaning would have been different after the class was
8780 entirely defined. */
8781 else if (type_decl != error_mark_node
8782 && !parser->scope)
8783 maybe_note_name_used_in_class (identifier, type_decl);
8784 }
8785
8786 return type_decl;
8787}
8788
8789
8790/* Parse an elaborated-type-specifier. Note that the grammar given
8791 here incorporates the resolution to DR68.
8792
8793 elaborated-type-specifier:
8794 class-key :: [opt] nested-name-specifier [opt] identifier
8795 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8796 enum :: [opt] nested-name-specifier [opt] identifier
8797 typename :: [opt] nested-name-specifier identifier
8798 typename :: [opt] nested-name-specifier template [opt]
8799 template-id
8800
360d1b99
MM
8801 GNU extension:
8802
8803 elaborated-type-specifier:
8804 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8805 class-key attributes :: [opt] nested-name-specifier [opt]
8806 template [opt] template-id
8807 enum attributes :: [opt] nested-name-specifier [opt] identifier
8808
a723baf1
MM
8809 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8810 declared `friend'. If IS_DECLARATION is TRUE, then this
8811 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8812 something is being declared.
8813
8814 Returns the TYPE specified. */
8815
8816static tree
94edc4ab
NN
8817cp_parser_elaborated_type_specifier (cp_parser* parser,
8818 bool is_friend,
8819 bool is_declaration)
a723baf1
MM
8820{
8821 enum tag_types tag_type;
8822 tree identifier;
8823 tree type = NULL_TREE;
360d1b99 8824 tree attributes = NULL_TREE;
a723baf1
MM
8825
8826 /* See if we're looking at the `enum' keyword. */
8827 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8828 {
8829 /* Consume the `enum' token. */
8830 cp_lexer_consume_token (parser->lexer);
8831 /* Remember that it's an enumeration type. */
8832 tag_type = enum_type;
360d1b99
MM
8833 /* Parse the attributes. */
8834 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
8835 }
8836 /* Or, it might be `typename'. */
8837 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8838 RID_TYPENAME))
8839 {
8840 /* Consume the `typename' token. */
8841 cp_lexer_consume_token (parser->lexer);
8842 /* Remember that it's a `typename' type. */
8843 tag_type = typename_type;
8844 /* The `typename' keyword is only allowed in templates. */
8845 if (!processing_template_decl)
8846 pedwarn ("using `typename' outside of template");
8847 }
8848 /* Otherwise it must be a class-key. */
8849 else
8850 {
8851 tag_type = cp_parser_class_key (parser);
8852 if (tag_type == none_type)
8853 return error_mark_node;
360d1b99
MM
8854 /* Parse the attributes. */
8855 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
8856 }
8857
8858 /* Look for the `::' operator. */
8859 cp_parser_global_scope_opt (parser,
8860 /*current_scope_valid_p=*/false);
8861 /* Look for the nested-name-specifier. */
8862 if (tag_type == typename_type)
8fa1ad0e
MM
8863 {
8864 if (cp_parser_nested_name_specifier (parser,
8865 /*typename_keyword_p=*/true,
8866 /*check_dependency_p=*/true,
a668c6ad
MM
8867 /*type_p=*/true,
8868 is_declaration)
8fa1ad0e
MM
8869 == error_mark_node)
8870 return error_mark_node;
8871 }
a723baf1
MM
8872 else
8873 /* Even though `typename' is not present, the proposed resolution
8874 to Core Issue 180 says that in `class A<T>::B', `B' should be
8875 considered a type-name, even if `A<T>' is dependent. */
8876 cp_parser_nested_name_specifier_opt (parser,
8877 /*typename_keyword_p=*/true,
8878 /*check_dependency_p=*/true,
a668c6ad
MM
8879 /*type_p=*/true,
8880 is_declaration);
a723baf1
MM
8881 /* For everything but enumeration types, consider a template-id. */
8882 if (tag_type != enum_type)
8883 {
8884 bool template_p = false;
8885 tree decl;
8886
8887 /* Allow the `template' keyword. */
8888 template_p = cp_parser_optional_template_keyword (parser);
8889 /* If we didn't see `template', we don't know if there's a
8890 template-id or not. */
8891 if (!template_p)
8892 cp_parser_parse_tentatively (parser);
8893 /* Parse the template-id. */
8894 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
8895 /*check_dependency_p=*/true,
8896 is_declaration);
a723baf1
MM
8897 /* If we didn't find a template-id, look for an ordinary
8898 identifier. */
8899 if (!template_p && !cp_parser_parse_definitely (parser))
8900 ;
8901 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8902 in effect, then we must assume that, upon instantiation, the
8903 template will correspond to a class. */
8904 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8905 && tag_type == typename_type)
8906 type = make_typename_type (parser->scope, decl,
8907 /*complain=*/1);
8908 else
8909 type = TREE_TYPE (decl);
8910 }
8911
8912 /* For an enumeration type, consider only a plain identifier. */
8913 if (!type)
8914 {
8915 identifier = cp_parser_identifier (parser);
8916
8917 if (identifier == error_mark_node)
eb5abb39
NS
8918 {
8919 parser->scope = NULL_TREE;
8920 return error_mark_node;
8921 }
a723baf1
MM
8922
8923 /* For a `typename', we needn't call xref_tag. */
8924 if (tag_type == typename_type)
8925 return make_typename_type (parser->scope, identifier,
8926 /*complain=*/1);
8927 /* Look up a qualified name in the usual way. */
8928 if (parser->scope)
8929 {
8930 tree decl;
8931
8932 /* In an elaborated-type-specifier, names are assumed to name
8933 types, so we set IS_TYPE to TRUE when calling
8934 cp_parser_lookup_name. */
8935 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8936 /*is_type=*/true,
eea9800f 8937 /*is_namespace=*/false,
a723baf1 8938 /*check_dependency=*/true);
710b73e6
KL
8939
8940 /* If we are parsing friend declaration, DECL may be a
8941 TEMPLATE_DECL tree node here. However, we need to check
8942 whether this TEMPLATE_DECL results in valid code. Consider
8943 the following example:
8944
8945 namespace N {
8946 template <class T> class C {};
8947 }
8948 class X {
8949 template <class T> friend class N::C; // #1, valid code
8950 };
8951 template <class T> class Y {
8952 friend class N::C; // #2, invalid code
8953 };
8954
8955 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8956 name lookup of `N::C'. We see that friend declaration must
8957 be template for the code to be valid. Note that
8958 processing_template_decl does not work here since it is
8959 always 1 for the above two cases. */
8960
a723baf1 8961 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8962 (decl, /*tag_name_p=*/is_friend
8963 && parser->num_template_parameter_lists));
a723baf1
MM
8964
8965 if (TREE_CODE (decl) != TYPE_DECL)
8966 {
8967 error ("expected type-name");
8968 return error_mark_node;
8969 }
560ad596
MM
8970
8971 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8972 check_elaborated_type_specifier
4b0d3cbe 8973 (tag_type, decl,
560ad596
MM
8974 (parser->num_template_parameter_lists
8975 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
8976
8977 type = TREE_TYPE (decl);
8978 }
8979 else
8980 {
8981 /* An elaborated-type-specifier sometimes introduces a new type and
8982 sometimes names an existing type. Normally, the rule is that it
8983 introduces a new type only if there is not an existing type of
8984 the same name already in scope. For example, given:
8985
8986 struct S {};
8987 void f() { struct S s; }
8988
8989 the `struct S' in the body of `f' is the same `struct S' as in
8990 the global scope; the existing definition is used. However, if
8991 there were no global declaration, this would introduce a new
8992 local class named `S'.
8993
8994 An exception to this rule applies to the following code:
8995
8996 namespace N { struct S; }
8997
8998 Here, the elaborated-type-specifier names a new type
8999 unconditionally; even if there is already an `S' in the
9000 containing scope this declaration names a new type.
9001 This exception only applies if the elaborated-type-specifier
9002 forms the complete declaration:
9003
9004 [class.name]
9005
9006 A declaration consisting solely of `class-key identifier ;' is
9007 either a redeclaration of the name in the current scope or a
9008 forward declaration of the identifier as a class name. It
9009 introduces the name into the current scope.
9010
9011 We are in this situation precisely when the next token is a `;'.
9012
9013 An exception to the exception is that a `friend' declaration does
9014 *not* name a new type; i.e., given:
9015
9016 struct S { friend struct T; };
9017
9018 `T' is not a new type in the scope of `S'.
9019
9020 Also, `new struct S' or `sizeof (struct S)' never results in the
9021 definition of a new type; a new type can only be declared in a
9bcb9aae 9022 declaration context. */
a723baf1
MM
9023
9024 type = xref_tag (tag_type, identifier,
360d1b99 9025 attributes,
a723baf1
MM
9026 (is_friend
9027 || !is_declaration
9028 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
9029 CPP_SEMICOLON)),
9030 parser->num_template_parameter_lists);
a723baf1
MM
9031 }
9032 }
9033 if (tag_type != enum_type)
9034 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9035
9036 /* A "<" cannot follow an elaborated type specifier. If that
9037 happens, the user was probably trying to form a template-id. */
9038 cp_parser_check_for_invalid_template_id (parser, type);
9039
a723baf1
MM
9040 return type;
9041}
9042
9043/* Parse an enum-specifier.
9044
9045 enum-specifier:
9046 enum identifier [opt] { enumerator-list [opt] }
9047
9048 Returns an ENUM_TYPE representing the enumeration. */
9049
9050static tree
94edc4ab 9051cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9052{
9053 cp_token *token;
9054 tree identifier = NULL_TREE;
9055 tree type;
9056
9057 /* Look for the `enum' keyword. */
9058 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9059 return error_mark_node;
9060 /* Peek at the next token. */
9061 token = cp_lexer_peek_token (parser->lexer);
9062
9063 /* See if it is an identifier. */
9064 if (token->type == CPP_NAME)
9065 identifier = cp_parser_identifier (parser);
9066
9067 /* Look for the `{'. */
9068 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9069 return error_mark_node;
9070
9071 /* At this point, we're going ahead with the enum-specifier, even
9072 if some other problem occurs. */
9073 cp_parser_commit_to_tentative_parse (parser);
9074
9075 /* Issue an error message if type-definitions are forbidden here. */
9076 cp_parser_check_type_definition (parser);
9077
9078 /* Create the new type. */
9079 type = start_enum (identifier ? identifier : make_anon_name ());
9080
9081 /* Peek at the next token. */
9082 token = cp_lexer_peek_token (parser->lexer);
9083 /* If it's not a `}', then there are some enumerators. */
9084 if (token->type != CPP_CLOSE_BRACE)
9085 cp_parser_enumerator_list (parser, type);
9086 /* Look for the `}'. */
9087 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9088
9089 /* Finish up the enumeration. */
9090 finish_enum (type);
9091
9092 return type;
9093}
9094
9095/* Parse an enumerator-list. The enumerators all have the indicated
9096 TYPE.
9097
9098 enumerator-list:
9099 enumerator-definition
9100 enumerator-list , enumerator-definition */
9101
9102static void
94edc4ab 9103cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9104{
9105 while (true)
9106 {
9107 cp_token *token;
9108
9109 /* Parse an enumerator-definition. */
9110 cp_parser_enumerator_definition (parser, type);
9111 /* Peek at the next token. */
9112 token = cp_lexer_peek_token (parser->lexer);
9113 /* If it's not a `,', then we've reached the end of the
9114 list. */
9115 if (token->type != CPP_COMMA)
9116 break;
9117 /* Otherwise, consume the `,' and keep going. */
9118 cp_lexer_consume_token (parser->lexer);
9119 /* If the next token is a `}', there is a trailing comma. */
9120 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9121 {
9122 if (pedantic && !in_system_header)
9123 pedwarn ("comma at end of enumerator list");
9124 break;
9125 }
9126 }
9127}
9128
9129/* Parse an enumerator-definition. The enumerator has the indicated
9130 TYPE.
9131
9132 enumerator-definition:
9133 enumerator
9134 enumerator = constant-expression
9135
9136 enumerator:
9137 identifier */
9138
9139static void
94edc4ab 9140cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9141{
9142 cp_token *token;
9143 tree identifier;
9144 tree value;
9145
9146 /* Look for the identifier. */
9147 identifier = cp_parser_identifier (parser);
9148 if (identifier == error_mark_node)
9149 return;
9150
9151 /* Peek at the next token. */
9152 token = cp_lexer_peek_token (parser->lexer);
9153 /* If it's an `=', then there's an explicit value. */
9154 if (token->type == CPP_EQ)
9155 {
9156 /* Consume the `=' token. */
9157 cp_lexer_consume_token (parser->lexer);
9158 /* Parse the value. */
14d22dd6 9159 value = cp_parser_constant_expression (parser,
d17811fd 9160 /*allow_non_constant_p=*/false,
14d22dd6 9161 NULL);
a723baf1
MM
9162 }
9163 else
9164 value = NULL_TREE;
9165
9166 /* Create the enumerator. */
9167 build_enumerator (identifier, value, type);
9168}
9169
9170/* Parse a namespace-name.
9171
9172 namespace-name:
9173 original-namespace-name
9174 namespace-alias
9175
9176 Returns the NAMESPACE_DECL for the namespace. */
9177
9178static tree
94edc4ab 9179cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9180{
9181 tree identifier;
9182 tree namespace_decl;
9183
9184 /* Get the name of the namespace. */
9185 identifier = cp_parser_identifier (parser);
9186 if (identifier == error_mark_node)
9187 return error_mark_node;
9188
eea9800f
MM
9189 /* Look up the identifier in the currently active scope. Look only
9190 for namespaces, due to:
9191
9192 [basic.lookup.udir]
9193
9194 When looking up a namespace-name in a using-directive or alias
9195 definition, only namespace names are considered.
9196
9197 And:
9198
9199 [basic.lookup.qual]
9200
9201 During the lookup of a name preceding the :: scope resolution
9202 operator, object, function, and enumerator names are ignored.
9203
9204 (Note that cp_parser_class_or_namespace_name only calls this
9205 function if the token after the name is the scope resolution
9206 operator.) */
9207 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f
MM
9208 /*is_type=*/false,
9209 /*is_namespace=*/true,
9210 /*check_dependency=*/true);
a723baf1
MM
9211 /* If it's not a namespace, issue an error. */
9212 if (namespace_decl == error_mark_node
9213 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9214 {
9215 cp_parser_error (parser, "expected namespace-name");
9216 namespace_decl = error_mark_node;
9217 }
9218
9219 return namespace_decl;
9220}
9221
9222/* Parse a namespace-definition.
9223
9224 namespace-definition:
9225 named-namespace-definition
9226 unnamed-namespace-definition
9227
9228 named-namespace-definition:
9229 original-namespace-definition
9230 extension-namespace-definition
9231
9232 original-namespace-definition:
9233 namespace identifier { namespace-body }
9234
9235 extension-namespace-definition:
9236 namespace original-namespace-name { namespace-body }
9237
9238 unnamed-namespace-definition:
9239 namespace { namespace-body } */
9240
9241static void
94edc4ab 9242cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9243{
9244 tree identifier;
9245
9246 /* Look for the `namespace' keyword. */
9247 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9248
9249 /* Get the name of the namespace. We do not attempt to distinguish
9250 between an original-namespace-definition and an
9251 extension-namespace-definition at this point. The semantic
9252 analysis routines are responsible for that. */
9253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9254 identifier = cp_parser_identifier (parser);
9255 else
9256 identifier = NULL_TREE;
9257
9258 /* Look for the `{' to start the namespace. */
9259 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9260 /* Start the namespace. */
9261 push_namespace (identifier);
9262 /* Parse the body of the namespace. */
9263 cp_parser_namespace_body (parser);
9264 /* Finish the namespace. */
9265 pop_namespace ();
9266 /* Look for the final `}'. */
9267 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9268}
9269
9270/* Parse a namespace-body.
9271
9272 namespace-body:
9273 declaration-seq [opt] */
9274
9275static void
94edc4ab 9276cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9277{
9278 cp_parser_declaration_seq_opt (parser);
9279}
9280
9281/* Parse a namespace-alias-definition.
9282
9283 namespace-alias-definition:
9284 namespace identifier = qualified-namespace-specifier ; */
9285
9286static void
94edc4ab 9287cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9288{
9289 tree identifier;
9290 tree namespace_specifier;
9291
9292 /* Look for the `namespace' keyword. */
9293 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9294 /* Look for the identifier. */
9295 identifier = cp_parser_identifier (parser);
9296 if (identifier == error_mark_node)
9297 return;
9298 /* Look for the `=' token. */
9299 cp_parser_require (parser, CPP_EQ, "`='");
9300 /* Look for the qualified-namespace-specifier. */
9301 namespace_specifier
9302 = cp_parser_qualified_namespace_specifier (parser);
9303 /* Look for the `;' token. */
9304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9305
9306 /* Register the alias in the symbol table. */
9307 do_namespace_alias (identifier, namespace_specifier);
9308}
9309
9310/* Parse a qualified-namespace-specifier.
9311
9312 qualified-namespace-specifier:
9313 :: [opt] nested-name-specifier [opt] namespace-name
9314
9315 Returns a NAMESPACE_DECL corresponding to the specified
9316 namespace. */
9317
9318static tree
94edc4ab 9319cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9320{
9321 /* Look for the optional `::'. */
9322 cp_parser_global_scope_opt (parser,
9323 /*current_scope_valid_p=*/false);
9324
9325 /* Look for the optional nested-name-specifier. */
9326 cp_parser_nested_name_specifier_opt (parser,
9327 /*typename_keyword_p=*/false,
9328 /*check_dependency_p=*/true,
a668c6ad
MM
9329 /*type_p=*/false,
9330 /*is_declaration=*/true);
a723baf1
MM
9331
9332 return cp_parser_namespace_name (parser);
9333}
9334
9335/* Parse a using-declaration.
9336
9337 using-declaration:
9338 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9339 using :: unqualified-id ; */
9340
9341static void
94edc4ab 9342cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9343{
9344 cp_token *token;
9345 bool typename_p = false;
9346 bool global_scope_p;
9347 tree decl;
9348 tree identifier;
9349 tree scope;
9350
9351 /* Look for the `using' keyword. */
9352 cp_parser_require_keyword (parser, RID_USING, "`using'");
9353
9354 /* Peek at the next token. */
9355 token = cp_lexer_peek_token (parser->lexer);
9356 /* See if it's `typename'. */
9357 if (token->keyword == RID_TYPENAME)
9358 {
9359 /* Remember that we've seen it. */
9360 typename_p = true;
9361 /* Consume the `typename' token. */
9362 cp_lexer_consume_token (parser->lexer);
9363 }
9364
9365 /* Look for the optional global scope qualification. */
9366 global_scope_p
9367 = (cp_parser_global_scope_opt (parser,
9368 /*current_scope_valid_p=*/false)
9369 != NULL_TREE);
9370
9371 /* If we saw `typename', or didn't see `::', then there must be a
9372 nested-name-specifier present. */
9373 if (typename_p || !global_scope_p)
9374 cp_parser_nested_name_specifier (parser, typename_p,
9375 /*check_dependency_p=*/true,
a668c6ad
MM
9376 /*type_p=*/false,
9377 /*is_declaration=*/true);
a723baf1
MM
9378 /* Otherwise, we could be in either of the two productions. In that
9379 case, treat the nested-name-specifier as optional. */
9380 else
9381 cp_parser_nested_name_specifier_opt (parser,
9382 /*typename_keyword_p=*/false,
9383 /*check_dependency_p=*/true,
a668c6ad
MM
9384 /*type_p=*/false,
9385 /*is_declaration=*/true);
a723baf1
MM
9386
9387 /* Parse the unqualified-id. */
9388 identifier = cp_parser_unqualified_id (parser,
9389 /*template_keyword_p=*/false,
f3c2dfc6
MM
9390 /*check_dependency_p=*/true,
9391 /*declarator_p=*/true);
a723baf1
MM
9392
9393 /* The function we call to handle a using-declaration is different
9394 depending on what scope we are in. */
f3c2dfc6
MM
9395 if (identifier == error_mark_node)
9396 ;
9397 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9398 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9399 /* [namespace.udecl]
9400
9401 A using declaration shall not name a template-id. */
9402 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9403 else
9404 {
f3c2dfc6
MM
9405 scope = current_scope ();
9406 if (scope && TYPE_P (scope))
4eb6d609 9407 {
f3c2dfc6
MM
9408 /* Create the USING_DECL. */
9409 decl = do_class_using_decl (build_nt (SCOPE_REF,
9410 parser->scope,
9411 identifier));
9412 /* Add it to the list of members in this class. */
9413 finish_member_declaration (decl);
4eb6d609 9414 }
a723baf1 9415 else
f3c2dfc6
MM
9416 {
9417 decl = cp_parser_lookup_name_simple (parser, identifier);
9418 if (decl == error_mark_node)
4bb8ca28 9419 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6
MM
9420 else if (scope)
9421 do_local_using_decl (decl);
9422 else
9423 do_toplevel_using_decl (decl);
9424 }
a723baf1
MM
9425 }
9426
9427 /* Look for the final `;'. */
9428 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9429}
9430
9431/* Parse a using-directive.
9432
9433 using-directive:
9434 using namespace :: [opt] nested-name-specifier [opt]
9435 namespace-name ; */
9436
9437static void
94edc4ab 9438cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9439{
9440 tree namespace_decl;
86098eb8 9441 tree attribs;
a723baf1
MM
9442
9443 /* Look for the `using' keyword. */
9444 cp_parser_require_keyword (parser, RID_USING, "`using'");
9445 /* And the `namespace' keyword. */
9446 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9447 /* Look for the optional `::' operator. */
9448 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9449 /* And the optional nested-name-specifier. */
a723baf1
MM
9450 cp_parser_nested_name_specifier_opt (parser,
9451 /*typename_keyword_p=*/false,
9452 /*check_dependency_p=*/true,
a668c6ad
MM
9453 /*type_p=*/false,
9454 /*is_declaration=*/true);
a723baf1
MM
9455 /* Get the namespace being used. */
9456 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
9457 /* And any specified attributes. */
9458 attribs = cp_parser_attributes_opt (parser);
a723baf1 9459 /* Update the symbol table. */
86098eb8 9460 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
9461 /* Look for the final `;'. */
9462 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9463}
9464
9465/* Parse an asm-definition.
9466
9467 asm-definition:
9468 asm ( string-literal ) ;
9469
9470 GNU Extension:
9471
9472 asm-definition:
9473 asm volatile [opt] ( string-literal ) ;
9474 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9475 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9476 : asm-operand-list [opt] ) ;
9477 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9478 : asm-operand-list [opt]
9479 : asm-operand-list [opt] ) ; */
9480
9481static void
94edc4ab 9482cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9483{
9484 cp_token *token;
9485 tree string;
9486 tree outputs = NULL_TREE;
9487 tree inputs = NULL_TREE;
9488 tree clobbers = NULL_TREE;
9489 tree asm_stmt;
9490 bool volatile_p = false;
9491 bool extended_p = false;
9492
9493 /* Look for the `asm' keyword. */
9494 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9495 /* See if the next token is `volatile'. */
9496 if (cp_parser_allow_gnu_extensions_p (parser)
9497 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9498 {
9499 /* Remember that we saw the `volatile' keyword. */
9500 volatile_p = true;
9501 /* Consume the token. */
9502 cp_lexer_consume_token (parser->lexer);
9503 }
9504 /* Look for the opening `('. */
9505 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9506 /* Look for the string. */
9507 token = cp_parser_require (parser, CPP_STRING, "asm body");
9508 if (!token)
9509 return;
9510 string = token->value;
9511 /* If we're allowing GNU extensions, check for the extended assembly
9512 syntax. Unfortunately, the `:' tokens need not be separated by
9513 a space in C, and so, for compatibility, we tolerate that here
9514 too. Doing that means that we have to treat the `::' operator as
9515 two `:' tokens. */
9516 if (cp_parser_allow_gnu_extensions_p (parser)
9517 && at_function_scope_p ()
9518 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9519 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9520 {
9521 bool inputs_p = false;
9522 bool clobbers_p = false;
9523
9524 /* The extended syntax was used. */
9525 extended_p = true;
9526
9527 /* Look for outputs. */
9528 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9529 {
9530 /* Consume the `:'. */
9531 cp_lexer_consume_token (parser->lexer);
9532 /* Parse the output-operands. */
9533 if (cp_lexer_next_token_is_not (parser->lexer,
9534 CPP_COLON)
9535 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9536 CPP_SCOPE)
9537 && cp_lexer_next_token_is_not (parser->lexer,
9538 CPP_CLOSE_PAREN))
a723baf1
MM
9539 outputs = cp_parser_asm_operand_list (parser);
9540 }
9541 /* If the next token is `::', there are no outputs, and the
9542 next token is the beginning of the inputs. */
9543 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9544 {
9545 /* Consume the `::' token. */
9546 cp_lexer_consume_token (parser->lexer);
9547 /* The inputs are coming next. */
9548 inputs_p = true;
9549 }
9550
9551 /* Look for inputs. */
9552 if (inputs_p
9553 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9554 {
9555 if (!inputs_p)
9556 /* Consume the `:'. */
9557 cp_lexer_consume_token (parser->lexer);
9558 /* Parse the output-operands. */
9559 if (cp_lexer_next_token_is_not (parser->lexer,
9560 CPP_COLON)
9561 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9562 CPP_SCOPE)
9563 && cp_lexer_next_token_is_not (parser->lexer,
9564 CPP_CLOSE_PAREN))
a723baf1
MM
9565 inputs = cp_parser_asm_operand_list (parser);
9566 }
9567 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9568 /* The clobbers are coming next. */
9569 clobbers_p = true;
9570
9571 /* Look for clobbers. */
9572 if (clobbers_p
9573 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9574 {
9575 if (!clobbers_p)
9576 /* Consume the `:'. */
9577 cp_lexer_consume_token (parser->lexer);
9578 /* Parse the clobbers. */
8caf4c38
MM
9579 if (cp_lexer_next_token_is_not (parser->lexer,
9580 CPP_CLOSE_PAREN))
9581 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9582 }
9583 }
9584 /* Look for the closing `)'. */
9585 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
9586 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9587 /*consume_paren=*/true);
a723baf1
MM
9588 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9589
9590 /* Create the ASM_STMT. */
9591 if (at_function_scope_p ())
9592 {
9593 asm_stmt =
9594 finish_asm_stmt (volatile_p
9595 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9596 string, outputs, inputs, clobbers);
9597 /* If the extended syntax was not used, mark the ASM_STMT. */
9598 if (!extended_p)
9599 ASM_INPUT_P (asm_stmt) = 1;
9600 }
9601 else
9602 assemble_asm (string);
9603}
9604
9605/* Declarators [gram.dcl.decl] */
9606
9607/* Parse an init-declarator.
9608
9609 init-declarator:
9610 declarator initializer [opt]
9611
9612 GNU Extension:
9613
9614 init-declarator:
9615 declarator asm-specification [opt] attributes [opt] initializer [opt]
9616
4bb8ca28
MM
9617 function-definition:
9618 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9619 function-body
9620 decl-specifier-seq [opt] declarator function-try-block
9621
9622 GNU Extension:
9623
9624 function-definition:
9625 __extension__ function-definition
9626
a723baf1 9627 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9628 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9629 then this declarator appears in a class scope. The new DECL created
9630 by this declarator is returned.
a723baf1
MM
9631
9632 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9633 for a function-definition here as well. If the declarator is a
9634 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9635 be TRUE upon return. By that point, the function-definition will
9636 have been completely parsed.
9637
9638 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9639 is FALSE. */
9640
9641static tree
94edc4ab
NN
9642cp_parser_init_declarator (cp_parser* parser,
9643 tree decl_specifiers,
9644 tree prefix_attributes,
9645 bool function_definition_allowed_p,
9646 bool member_p,
560ad596 9647 int declares_class_or_enum,
94edc4ab 9648 bool* function_definition_p)
a723baf1
MM
9649{
9650 cp_token *token;
9651 tree declarator;
9652 tree attributes;
9653 tree asm_specification;
9654 tree initializer;
9655 tree decl = NULL_TREE;
9656 tree scope;
a723baf1
MM
9657 bool is_initialized;
9658 bool is_parenthesized_init;
39703eb9 9659 bool is_non_constant_init;
7efa3e22 9660 int ctor_dtor_or_conv_p;
a723baf1
MM
9661 bool friend_p;
9662
9663 /* Assume that this is not the declarator for a function
9664 definition. */
9665 if (function_definition_p)
9666 *function_definition_p = false;
9667
9668 /* Defer access checks while parsing the declarator; we cannot know
9669 what names are accessible until we know what is being
9670 declared. */
cf22909c
KL
9671 resume_deferring_access_checks ();
9672
a723baf1
MM
9673 /* Parse the declarator. */
9674 declarator
62b8a44e 9675 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9676 &ctor_dtor_or_conv_p,
9677 /*parenthesized_p=*/NULL);
a723baf1 9678 /* Gather up the deferred checks. */
cf22909c 9679 stop_deferring_access_checks ();
24c0ef37 9680
a723baf1
MM
9681 /* If the DECLARATOR was erroneous, there's no need to go
9682 further. */
9683 if (declarator == error_mark_node)
cf22909c 9684 return error_mark_node;
a723baf1 9685
560ad596
MM
9686 cp_parser_check_for_definition_in_return_type (declarator,
9687 declares_class_or_enum);
9688
a723baf1
MM
9689 /* Figure out what scope the entity declared by the DECLARATOR is
9690 located in. `grokdeclarator' sometimes changes the scope, so
9691 we compute it now. */
9692 scope = get_scope_of_declarator (declarator);
9693
9694 /* If we're allowing GNU extensions, look for an asm-specification
9695 and attributes. */
9696 if (cp_parser_allow_gnu_extensions_p (parser))
9697 {
9698 /* Look for an asm-specification. */
9699 asm_specification = cp_parser_asm_specification_opt (parser);
9700 /* And attributes. */
9701 attributes = cp_parser_attributes_opt (parser);
9702 }
9703 else
9704 {
9705 asm_specification = NULL_TREE;
9706 attributes = NULL_TREE;
9707 }
9708
9709 /* Peek at the next token. */
9710 token = cp_lexer_peek_token (parser->lexer);
9711 /* Check to see if the token indicates the start of a
9712 function-definition. */
9713 if (cp_parser_token_starts_function_definition_p (token))
9714 {
9715 if (!function_definition_allowed_p)
9716 {
9717 /* If a function-definition should not appear here, issue an
9718 error message. */
9719 cp_parser_error (parser,
9720 "a function-definition is not allowed here");
9721 return error_mark_node;
9722 }
9723 else
9724 {
a723baf1
MM
9725 /* Neither attributes nor an asm-specification are allowed
9726 on a function-definition. */
9727 if (asm_specification)
9728 error ("an asm-specification is not allowed on a function-definition");
9729 if (attributes)
9730 error ("attributes are not allowed on a function-definition");
9731 /* This is a function-definition. */
9732 *function_definition_p = true;
9733
a723baf1 9734 /* Parse the function definition. */
4bb8ca28
MM
9735 if (member_p)
9736 decl = cp_parser_save_member_function_body (parser,
9737 decl_specifiers,
9738 declarator,
9739 prefix_attributes);
9740 else
9741 decl
9742 = (cp_parser_function_definition_from_specifiers_and_declarator
9743 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9744
a723baf1
MM
9745 return decl;
9746 }
9747 }
9748
9749 /* [dcl.dcl]
9750
9751 Only in function declarations for constructors, destructors, and
9752 type conversions can the decl-specifier-seq be omitted.
9753
9754 We explicitly postpone this check past the point where we handle
9755 function-definitions because we tolerate function-definitions
9756 that are missing their return types in some modes. */
7efa3e22 9757 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9758 {
9759 cp_parser_error (parser,
9760 "expected constructor, destructor, or type conversion");
9761 return error_mark_node;
9762 }
9763
9764 /* An `=' or an `(' indicates an initializer. */
9765 is_initialized = (token->type == CPP_EQ
9766 || token->type == CPP_OPEN_PAREN);
9767 /* If the init-declarator isn't initialized and isn't followed by a
9768 `,' or `;', it's not a valid init-declarator. */
9769 if (!is_initialized
9770 && token->type != CPP_COMMA
9771 && token->type != CPP_SEMICOLON)
9772 {
9773 cp_parser_error (parser, "expected init-declarator");
9774 return error_mark_node;
9775 }
9776
9777 /* Because start_decl has side-effects, we should only call it if we
9778 know we're going ahead. By this point, we know that we cannot
9779 possibly be looking at any other construct. */
9780 cp_parser_commit_to_tentative_parse (parser);
9781
9782 /* Check to see whether or not this declaration is a friend. */
9783 friend_p = cp_parser_friend_p (decl_specifiers);
9784
9785 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 9786 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 9787 return error_mark_node;
a723baf1
MM
9788
9789 /* Enter the newly declared entry in the symbol table. If we're
9790 processing a declaration in a class-specifier, we wait until
9791 after processing the initializer. */
9792 if (!member_p)
9793 {
9794 if (parser->in_unbraced_linkage_specification_p)
9795 {
9796 decl_specifiers = tree_cons (error_mark_node,
9797 get_identifier ("extern"),
9798 decl_specifiers);
9799 have_extern_spec = false;
9800 }
ee3071ef
NS
9801 decl = start_decl (declarator, decl_specifiers,
9802 is_initialized, attributes, prefix_attributes);
a723baf1
MM
9803 }
9804
9805 /* Enter the SCOPE. That way unqualified names appearing in the
9806 initializer will be looked up in SCOPE. */
9807 if (scope)
9808 push_scope (scope);
9809
9810 /* Perform deferred access control checks, now that we know in which
9811 SCOPE the declared entity resides. */
9812 if (!member_p && decl)
9813 {
9814 tree saved_current_function_decl = NULL_TREE;
9815
9816 /* If the entity being declared is a function, pretend that we
9817 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9818 things that would not otherwise be accessible. */
a723baf1
MM
9819 if (TREE_CODE (decl) == FUNCTION_DECL)
9820 {
9821 saved_current_function_decl = current_function_decl;
9822 current_function_decl = decl;
9823 }
9824
cf22909c
KL
9825 /* Perform the access control checks for the declarator and the
9826 the decl-specifiers. */
9827 perform_deferred_access_checks ();
a723baf1
MM
9828
9829 /* Restore the saved value. */
9830 if (TREE_CODE (decl) == FUNCTION_DECL)
9831 current_function_decl = saved_current_function_decl;
9832 }
9833
9834 /* Parse the initializer. */
9835 if (is_initialized)
39703eb9
MM
9836 initializer = cp_parser_initializer (parser,
9837 &is_parenthesized_init,
9838 &is_non_constant_init);
a723baf1
MM
9839 else
9840 {
9841 initializer = NULL_TREE;
9842 is_parenthesized_init = false;
39703eb9 9843 is_non_constant_init = true;
a723baf1
MM
9844 }
9845
9846 /* The old parser allows attributes to appear after a parenthesized
9847 initializer. Mark Mitchell proposed removing this functionality
9848 on the GCC mailing lists on 2002-08-13. This parser accepts the
9849 attributes -- but ignores them. */
9850 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9851 if (cp_parser_attributes_opt (parser))
9852 warning ("attributes after parenthesized initializer ignored");
9853
9854 /* Leave the SCOPE, now that we have processed the initializer. It
9855 is important to do this before calling cp_finish_decl because it
9856 makes decisions about whether to create DECL_STMTs or not based
9857 on the current scope. */
9858 if (scope)
9859 pop_scope (scope);
9860
9861 /* For an in-class declaration, use `grokfield' to create the
9862 declaration. */
9863 if (member_p)
8db1028e
NS
9864 {
9865 decl = grokfield (declarator, decl_specifiers,
9866 initializer, /*asmspec=*/NULL_TREE,
a723baf1 9867 /*attributes=*/NULL_TREE);
8db1028e
NS
9868 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9869 cp_parser_save_default_args (parser, decl);
9870 }
9871
a723baf1
MM
9872 /* Finish processing the declaration. But, skip friend
9873 declarations. */
9874 if (!friend_p && decl)
9875 cp_finish_decl (decl,
9876 initializer,
9877 asm_specification,
9878 /* If the initializer is in parentheses, then this is
9879 a direct-initialization, which means that an
9880 `explicit' constructor is OK. Otherwise, an
9881 `explicit' constructor cannot be used. */
9882 ((is_parenthesized_init || !is_initialized)
9883 ? 0 : LOOKUP_ONLYCONVERTING));
9884
39703eb9
MM
9885 /* Remember whether or not variables were initialized by
9886 constant-expressions. */
9887 if (decl && TREE_CODE (decl) == VAR_DECL
9888 && is_initialized && !is_non_constant_init)
9889 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9890
a723baf1
MM
9891 return decl;
9892}
9893
9894/* Parse a declarator.
9895
9896 declarator:
9897 direct-declarator
9898 ptr-operator declarator
9899
9900 abstract-declarator:
9901 ptr-operator abstract-declarator [opt]
9902 direct-abstract-declarator
9903
9904 GNU Extensions:
9905
9906 declarator:
9907 attributes [opt] direct-declarator
9908 attributes [opt] ptr-operator declarator
9909
9910 abstract-declarator:
9911 attributes [opt] ptr-operator abstract-declarator [opt]
9912 attributes [opt] direct-abstract-declarator
9913
9914 Returns a representation of the declarator. If the declarator has
9915 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 9916 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
9917 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9918 used. The first operand is the TYPE for `X'. The second operand
9919 is an INDIRECT_REF whose operand is the sub-declarator.
9920
34cd5ae7 9921 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
9922
9923 (It would be better to define a structure type to represent
9924 declarators, rather than abusing `tree' nodes to represent
9925 declarators. That would be much clearer and save some memory.
9926 There is no reason for declarators to be garbage-collected, for
9927 example; they are created during parser and no longer needed after
9928 `grokdeclarator' has been called.)
9929
9930 For a ptr-operator that has the optional cv-qualifier-seq,
9931 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9932 node.
9933
7efa3e22
NS
9934 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9935 detect constructor, destructor or conversion operators. It is set
9936 to -1 if the declarator is a name, and +1 if it is a
9937 function. Otherwise it is set to zero. Usually you just want to
9938 test for >0, but internally the negative value is used.
9939
a723baf1
MM
9940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9941 a decl-specifier-seq unless it declares a constructor, destructor,
9942 or conversion. It might seem that we could check this condition in
9943 semantic analysis, rather than parsing, but that makes it difficult
9944 to handle something like `f()'. We want to notice that there are
9945 no decl-specifiers, and therefore realize that this is an
4bb8ca28
MM
9946 expression, not a declaration.)
9947
9948 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9949 the declarator is a direct-declarator of the form "(...)". */
a723baf1
MM
9950
9951static tree
94edc4ab
NN
9952cp_parser_declarator (cp_parser* parser,
9953 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
9954 int* ctor_dtor_or_conv_p,
9955 bool* parenthesized_p)
a723baf1
MM
9956{
9957 cp_token *token;
9958 tree declarator;
9959 enum tree_code code;
9960 tree cv_qualifier_seq;
9961 tree class_type;
9962 tree attributes = NULL_TREE;
9963
9964 /* Assume this is not a constructor, destructor, or type-conversion
9965 operator. */
9966 if (ctor_dtor_or_conv_p)
7efa3e22 9967 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
9968
9969 if (cp_parser_allow_gnu_extensions_p (parser))
9970 attributes = cp_parser_attributes_opt (parser);
9971
9972 /* Peek at the next token. */
9973 token = cp_lexer_peek_token (parser->lexer);
9974
9975 /* Check for the ptr-operator production. */
9976 cp_parser_parse_tentatively (parser);
9977 /* Parse the ptr-operator. */
9978 code = cp_parser_ptr_operator (parser,
9979 &class_type,
9980 &cv_qualifier_seq);
9981 /* If that worked, then we have a ptr-operator. */
9982 if (cp_parser_parse_definitely (parser))
9983 {
4bb8ca28
MM
9984 /* If a ptr-operator was found, then this declarator was not
9985 parenthesized. */
9986 if (parenthesized_p)
9987 *parenthesized_p = true;
a723baf1
MM
9988 /* The dependent declarator is optional if we are parsing an
9989 abstract-declarator. */
62b8a44e 9990 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9991 cp_parser_parse_tentatively (parser);
9992
9993 /* Parse the dependent declarator. */
62b8a44e 9994 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
9995 /*ctor_dtor_or_conv_p=*/NULL,
9996 /*parenthesized_p=*/NULL);
a723baf1
MM
9997
9998 /* If we are parsing an abstract-declarator, we must handle the
9999 case where the dependent declarator is absent. */
62b8a44e
NS
10000 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10001 && !cp_parser_parse_definitely (parser))
a723baf1
MM
10002 declarator = NULL_TREE;
10003
10004 /* Build the representation of the ptr-operator. */
10005 if (code == INDIRECT_REF)
10006 declarator = make_pointer_declarator (cv_qualifier_seq,
10007 declarator);
10008 else
10009 declarator = make_reference_declarator (cv_qualifier_seq,
10010 declarator);
10011 /* Handle the pointer-to-member case. */
10012 if (class_type)
10013 declarator = build_nt (SCOPE_REF, class_type, declarator);
10014 }
10015 /* Everything else is a direct-declarator. */
10016 else
4bb8ca28
MM
10017 {
10018 if (parenthesized_p)
10019 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10020 CPP_OPEN_PAREN);
10021 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10022 ctor_dtor_or_conv_p);
10023 }
a723baf1
MM
10024
10025 if (attributes && declarator != error_mark_node)
10026 declarator = tree_cons (attributes, declarator, NULL_TREE);
10027
10028 return declarator;
10029}
10030
10031/* Parse a direct-declarator or direct-abstract-declarator.
10032
10033 direct-declarator:
10034 declarator-id
10035 direct-declarator ( parameter-declaration-clause )
10036 cv-qualifier-seq [opt]
10037 exception-specification [opt]
10038 direct-declarator [ constant-expression [opt] ]
10039 ( declarator )
10040
10041 direct-abstract-declarator:
10042 direct-abstract-declarator [opt]
10043 ( parameter-declaration-clause )
10044 cv-qualifier-seq [opt]
10045 exception-specification [opt]
10046 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10047 ( abstract-declarator )
10048
62b8a44e
NS
10049 Returns a representation of the declarator. DCL_KIND is
10050 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10051 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10052 we are parsing a direct-declarator. It is
10053 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10054 of ambiguity we prefer an abstract declarator, as per
10055 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10056 cp_parser_declarator.
10057
10058 For the declarator-id production, the representation is as for an
10059 id-expression, except that a qualified name is represented as a
10060 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10061 see the documentation of the FUNCTION_DECLARATOR_* macros for
10062 information about how to find the various declarator components.
10063 An array-declarator is represented as an ARRAY_REF. The
10064 direct-declarator is the first operand; the constant-expression
10065 indicating the size of the array is the second operand. */
10066
10067static tree
94edc4ab
NN
10068cp_parser_direct_declarator (cp_parser* parser,
10069 cp_parser_declarator_kind dcl_kind,
7efa3e22 10070 int* ctor_dtor_or_conv_p)
a723baf1
MM
10071{
10072 cp_token *token;
62b8a44e 10073 tree declarator = NULL_TREE;
a723baf1
MM
10074 tree scope = NULL_TREE;
10075 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10076 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
10077 bool first = true;
10078
10079 while (true)
a723baf1 10080 {
62b8a44e
NS
10081 /* Peek at the next token. */
10082 token = cp_lexer_peek_token (parser->lexer);
10083 if (token->type == CPP_OPEN_PAREN)
a723baf1 10084 {
62b8a44e
NS
10085 /* This is either a parameter-declaration-clause, or a
10086 parenthesized declarator. When we know we are parsing a
34cd5ae7 10087 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10088 if FIRST is true. For instance, `(int)' is a
10089 parameter-declaration-clause, with an omitted
10090 direct-abstract-declarator. But `((*))', is a
10091 parenthesized abstract declarator. Finally, when T is a
10092 template parameter `(T)' is a
34cd5ae7 10093 parameter-declaration-clause, and not a parenthesized
62b8a44e 10094 named declarator.
a723baf1 10095
62b8a44e
NS
10096 We first try and parse a parameter-declaration-clause,
10097 and then try a nested declarator (if FIRST is true).
a723baf1 10098
62b8a44e
NS
10099 It is not an error for it not to be a
10100 parameter-declaration-clause, even when FIRST is
10101 false. Consider,
10102
10103 int i (int);
10104 int i (3);
10105
10106 The first is the declaration of a function while the
10107 second is a the definition of a variable, including its
10108 initializer.
10109
10110 Having seen only the parenthesis, we cannot know which of
10111 these two alternatives should be selected. Even more
10112 complex are examples like:
10113
10114 int i (int (a));
10115 int i (int (3));
10116
10117 The former is a function-declaration; the latter is a
10118 variable initialization.
10119
34cd5ae7 10120 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10121 that fails, we back out and return. */
10122
10123 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10124 {
62b8a44e 10125 tree params;
4047b164 10126 unsigned saved_num_template_parameter_lists;
62b8a44e
NS
10127
10128 cp_parser_parse_tentatively (parser);
a723baf1 10129
62b8a44e
NS
10130 /* Consume the `('. */
10131 cp_lexer_consume_token (parser->lexer);
10132 if (first)
10133 {
10134 /* If this is going to be an abstract declarator, we're
10135 in a declarator and we can't have default args. */
10136 parser->default_arg_ok_p = false;
10137 parser->in_declarator_p = true;
10138 }
10139
4047b164
KL
10140 /* Inside the function parameter list, surrounding
10141 template-parameter-lists do not apply. */
10142 saved_num_template_parameter_lists
10143 = parser->num_template_parameter_lists;
10144 parser->num_template_parameter_lists = 0;
10145
62b8a44e
NS
10146 /* Parse the parameter-declaration-clause. */
10147 params = cp_parser_parameter_declaration_clause (parser);
10148
4047b164
KL
10149 parser->num_template_parameter_lists
10150 = saved_num_template_parameter_lists;
10151
62b8a44e 10152 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10153 exception-specification. */
62b8a44e
NS
10154 if (cp_parser_parse_definitely (parser))
10155 {
10156 tree cv_qualifiers;
10157 tree exception_specification;
7efa3e22
NS
10158
10159 if (ctor_dtor_or_conv_p)
10160 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10161 first = false;
10162 /* Consume the `)'. */
10163 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10164
10165 /* Parse the cv-qualifier-seq. */
10166 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10167 /* And the exception-specification. */
10168 exception_specification
10169 = cp_parser_exception_specification_opt (parser);
10170
10171 /* Create the function-declarator. */
10172 declarator = make_call_declarator (declarator,
10173 params,
10174 cv_qualifiers,
10175 exception_specification);
10176 /* Any subsequent parameter lists are to do with
10177 return type, so are not those of the declared
10178 function. */
10179 parser->default_arg_ok_p = false;
10180
10181 /* Repeat the main loop. */
10182 continue;
10183 }
10184 }
10185
10186 /* If this is the first, we can try a parenthesized
10187 declarator. */
10188 if (first)
a723baf1 10189 {
a723baf1 10190 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10191 parser->in_declarator_p = saved_in_declarator_p;
10192
10193 /* Consume the `('. */
10194 cp_lexer_consume_token (parser->lexer);
10195 /* Parse the nested declarator. */
10196 declarator
4bb8ca28
MM
10197 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10198 /*parenthesized_p=*/NULL);
62b8a44e
NS
10199 first = false;
10200 /* Expect a `)'. */
10201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10202 declarator = error_mark_node;
10203 if (declarator == error_mark_node)
10204 break;
10205
10206 goto handle_declarator;
a723baf1 10207 }
9bcb9aae 10208 /* Otherwise, we must be done. */
62b8a44e
NS
10209 else
10210 break;
a723baf1 10211 }
62b8a44e
NS
10212 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10213 && token->type == CPP_OPEN_SQUARE)
a723baf1 10214 {
62b8a44e 10215 /* Parse an array-declarator. */
a723baf1
MM
10216 tree bounds;
10217
7efa3e22
NS
10218 if (ctor_dtor_or_conv_p)
10219 *ctor_dtor_or_conv_p = 0;
10220
62b8a44e
NS
10221 first = false;
10222 parser->default_arg_ok_p = false;
10223 parser->in_declarator_p = true;
a723baf1
MM
10224 /* Consume the `['. */
10225 cp_lexer_consume_token (parser->lexer);
10226 /* Peek at the next token. */
10227 token = cp_lexer_peek_token (parser->lexer);
10228 /* If the next token is `]', then there is no
10229 constant-expression. */
10230 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10231 {
10232 bool non_constant_p;
10233
10234 bounds
10235 = cp_parser_constant_expression (parser,
10236 /*allow_non_constant=*/true,
10237 &non_constant_p);
d17811fd
MM
10238 if (!non_constant_p)
10239 bounds = cp_parser_fold_non_dependent_expr (bounds);
14d22dd6 10240 }
a723baf1
MM
10241 else
10242 bounds = NULL_TREE;
10243 /* Look for the closing `]'. */
62b8a44e
NS
10244 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10245 {
10246 declarator = error_mark_node;
10247 break;
10248 }
a723baf1
MM
10249
10250 declarator = build_nt (ARRAY_REF, declarator, bounds);
10251 }
62b8a44e 10252 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10253 {
a668c6ad 10254 /* Parse a declarator-id */
62b8a44e
NS
10255 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10256 cp_parser_parse_tentatively (parser);
10257 declarator = cp_parser_declarator_id (parser);
712becab
NS
10258 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10259 {
10260 if (!cp_parser_parse_definitely (parser))
10261 declarator = error_mark_node;
10262 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10263 {
10264 cp_parser_error (parser, "expected unqualified-id");
10265 declarator = error_mark_node;
10266 }
10267 }
10268
62b8a44e
NS
10269 if (declarator == error_mark_node)
10270 break;
a723baf1 10271
d9a50301
KL
10272 if (TREE_CODE (declarator) == SCOPE_REF
10273 && !current_scope ())
62b8a44e
NS
10274 {
10275 tree scope = TREE_OPERAND (declarator, 0);
712becab 10276
62b8a44e
NS
10277 /* In the declaration of a member of a template class
10278 outside of the class itself, the SCOPE will sometimes
10279 be a TYPENAME_TYPE. For example, given:
10280
10281 template <typename T>
10282 int S<T>::R::i = 3;
10283
10284 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10285 this context, we must resolve S<T>::R to an ordinary
10286 type, rather than a typename type.
10287
10288 The reason we normally avoid resolving TYPENAME_TYPEs
10289 is that a specialization of `S' might render
10290 `S<T>::R' not a type. However, if `S' is
10291 specialized, then this `i' will not be used, so there
10292 is no harm in resolving the types here. */
10293 if (TREE_CODE (scope) == TYPENAME_TYPE)
10294 {
14d22dd6
MM
10295 tree type;
10296
62b8a44e 10297 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10298 type = resolve_typename_type (scope,
10299 /*only_current_p=*/false);
62b8a44e 10300 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10301 if (type != error_mark_node)
10302 scope = type;
62b8a44e
NS
10303 /* Build a new DECLARATOR. */
10304 declarator = build_nt (SCOPE_REF,
10305 scope,
10306 TREE_OPERAND (declarator, 1));
10307 }
10308 }
10309
10310 /* Check to see whether the declarator-id names a constructor,
10311 destructor, or conversion. */
10312 if (declarator && ctor_dtor_or_conv_p
10313 && ((TREE_CODE (declarator) == SCOPE_REF
10314 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10315 || (TREE_CODE (declarator) != SCOPE_REF
10316 && at_class_scope_p ())))
a723baf1 10317 {
62b8a44e
NS
10318 tree unqualified_name;
10319 tree class_type;
10320
10321 /* Get the unqualified part of the name. */
10322 if (TREE_CODE (declarator) == SCOPE_REF)
10323 {
10324 class_type = TREE_OPERAND (declarator, 0);
10325 unqualified_name = TREE_OPERAND (declarator, 1);
10326 }
10327 else
10328 {
10329 class_type = current_class_type;
10330 unqualified_name = declarator;
10331 }
10332
10333 /* See if it names ctor, dtor or conv. */
10334 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10335 || IDENTIFIER_TYPENAME_P (unqualified_name)
10336 || constructor_name_p (unqualified_name, class_type))
7efa3e22 10337 *ctor_dtor_or_conv_p = -1;
a723baf1 10338 }
62b8a44e
NS
10339
10340 handle_declarator:;
10341 scope = get_scope_of_declarator (declarator);
10342 if (scope)
10343 /* Any names that appear after the declarator-id for a member
10344 are looked up in the containing scope. */
10345 push_scope (scope);
10346 parser->in_declarator_p = true;
10347 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10348 || (declarator
10349 && (TREE_CODE (declarator) == SCOPE_REF
10350 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10351 /* Default args are only allowed on function
10352 declarations. */
10353 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10354 else
62b8a44e
NS
10355 parser->default_arg_ok_p = false;
10356
10357 first = false;
a723baf1 10358 }
62b8a44e 10359 /* We're done. */
a723baf1
MM
10360 else
10361 break;
a723baf1
MM
10362 }
10363
10364 /* For an abstract declarator, we might wind up with nothing at this
10365 point. That's an error; the declarator is not optional. */
10366 if (!declarator)
10367 cp_parser_error (parser, "expected declarator");
10368
10369 /* If we entered a scope, we must exit it now. */
10370 if (scope)
10371 pop_scope (scope);
10372
10373 parser->default_arg_ok_p = saved_default_arg_ok_p;
10374 parser->in_declarator_p = saved_in_declarator_p;
10375
10376 return declarator;
10377}
10378
10379/* Parse a ptr-operator.
10380
10381 ptr-operator:
10382 * cv-qualifier-seq [opt]
10383 &
10384 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10385
10386 GNU Extension:
10387
10388 ptr-operator:
10389 & cv-qualifier-seq [opt]
10390
10391 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10392 used. Returns ADDR_EXPR if a reference was used. In the
10393 case of a pointer-to-member, *TYPE is filled in with the
10394 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10395 with the cv-qualifier-seq, or NULL_TREE, if there are no
10396 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10397
10398static enum tree_code
94edc4ab
NN
10399cp_parser_ptr_operator (cp_parser* parser,
10400 tree* type,
10401 tree* cv_qualifier_seq)
a723baf1
MM
10402{
10403 enum tree_code code = ERROR_MARK;
10404 cp_token *token;
10405
10406 /* Assume that it's not a pointer-to-member. */
10407 *type = NULL_TREE;
10408 /* And that there are no cv-qualifiers. */
10409 *cv_qualifier_seq = NULL_TREE;
10410
10411 /* Peek at the next token. */
10412 token = cp_lexer_peek_token (parser->lexer);
10413 /* If it's a `*' or `&' we have a pointer or reference. */
10414 if (token->type == CPP_MULT || token->type == CPP_AND)
10415 {
10416 /* Remember which ptr-operator we were processing. */
10417 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10418
10419 /* Consume the `*' or `&'. */
10420 cp_lexer_consume_token (parser->lexer);
10421
10422 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10423 `&', if we are allowing GNU extensions. (The only qualifier
10424 that can legally appear after `&' is `restrict', but that is
10425 enforced during semantic analysis. */
10426 if (code == INDIRECT_REF
10427 || cp_parser_allow_gnu_extensions_p (parser))
10428 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10429 }
10430 else
10431 {
10432 /* Try the pointer-to-member case. */
10433 cp_parser_parse_tentatively (parser);
10434 /* Look for the optional `::' operator. */
10435 cp_parser_global_scope_opt (parser,
10436 /*current_scope_valid_p=*/false);
10437 /* Look for the nested-name specifier. */
10438 cp_parser_nested_name_specifier (parser,
10439 /*typename_keyword_p=*/false,
10440 /*check_dependency_p=*/true,
a668c6ad
MM
10441 /*type_p=*/false,
10442 /*is_declaration=*/false);
a723baf1
MM
10443 /* If we found it, and the next token is a `*', then we are
10444 indeed looking at a pointer-to-member operator. */
10445 if (!cp_parser_error_occurred (parser)
10446 && cp_parser_require (parser, CPP_MULT, "`*'"))
10447 {
10448 /* The type of which the member is a member is given by the
10449 current SCOPE. */
10450 *type = parser->scope;
10451 /* The next name will not be qualified. */
10452 parser->scope = NULL_TREE;
10453 parser->qualifying_scope = NULL_TREE;
10454 parser->object_scope = NULL_TREE;
10455 /* Indicate that the `*' operator was used. */
10456 code = INDIRECT_REF;
10457 /* Look for the optional cv-qualifier-seq. */
10458 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10459 }
10460 /* If that didn't work we don't have a ptr-operator. */
10461 if (!cp_parser_parse_definitely (parser))
10462 cp_parser_error (parser, "expected ptr-operator");
10463 }
10464
10465 return code;
10466}
10467
10468/* Parse an (optional) cv-qualifier-seq.
10469
10470 cv-qualifier-seq:
10471 cv-qualifier cv-qualifier-seq [opt]
10472
10473 Returns a TREE_LIST. The TREE_VALUE of each node is the
10474 representation of a cv-qualifier. */
10475
10476static tree
94edc4ab 10477cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10478{
10479 tree cv_qualifiers = NULL_TREE;
10480
10481 while (true)
10482 {
10483 tree cv_qualifier;
10484
10485 /* Look for the next cv-qualifier. */
10486 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10487 /* If we didn't find one, we're done. */
10488 if (!cv_qualifier)
10489 break;
10490
10491 /* Add this cv-qualifier to the list. */
10492 cv_qualifiers
10493 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10494 }
10495
10496 /* We built up the list in reverse order. */
10497 return nreverse (cv_qualifiers);
10498}
10499
10500/* Parse an (optional) cv-qualifier.
10501
10502 cv-qualifier:
10503 const
10504 volatile
10505
10506 GNU Extension:
10507
10508 cv-qualifier:
10509 __restrict__ */
10510
10511static tree
94edc4ab 10512cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10513{
10514 cp_token *token;
10515 tree cv_qualifier = NULL_TREE;
10516
10517 /* Peek at the next token. */
10518 token = cp_lexer_peek_token (parser->lexer);
10519 /* See if it's a cv-qualifier. */
10520 switch (token->keyword)
10521 {
10522 case RID_CONST:
10523 case RID_VOLATILE:
10524 case RID_RESTRICT:
10525 /* Save the value of the token. */
10526 cv_qualifier = token->value;
10527 /* Consume the token. */
10528 cp_lexer_consume_token (parser->lexer);
10529 break;
10530
10531 default:
10532 break;
10533 }
10534
10535 return cv_qualifier;
10536}
10537
10538/* Parse a declarator-id.
10539
10540 declarator-id:
10541 id-expression
10542 :: [opt] nested-name-specifier [opt] type-name
10543
10544 In the `id-expression' case, the value returned is as for
10545 cp_parser_id_expression if the id-expression was an unqualified-id.
10546 If the id-expression was a qualified-id, then a SCOPE_REF is
10547 returned. The first operand is the scope (either a NAMESPACE_DECL
10548 or TREE_TYPE), but the second is still just a representation of an
10549 unqualified-id. */
10550
10551static tree
94edc4ab 10552cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10553{
10554 tree id_expression;
10555
10556 /* The expression must be an id-expression. Assume that qualified
10557 names are the names of types so that:
10558
10559 template <class T>
10560 int S<T>::R::i = 3;
10561
10562 will work; we must treat `S<T>::R' as the name of a type.
10563 Similarly, assume that qualified names are templates, where
10564 required, so that:
10565
10566 template <class T>
10567 int S<T>::R<T>::i = 3;
10568
10569 will work, too. */
10570 id_expression = cp_parser_id_expression (parser,
10571 /*template_keyword_p=*/false,
10572 /*check_dependency_p=*/false,
f3c2dfc6
MM
10573 /*template_p=*/NULL,
10574 /*declarator_p=*/true);
a723baf1
MM
10575 /* If the name was qualified, create a SCOPE_REF to represent
10576 that. */
10577 if (parser->scope)
ec20aa6c
MM
10578 {
10579 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10580 parser->scope = NULL_TREE;
10581 }
a723baf1
MM
10582
10583 return id_expression;
10584}
10585
10586/* Parse a type-id.
10587
10588 type-id:
10589 type-specifier-seq abstract-declarator [opt]
10590
10591 Returns the TYPE specified. */
10592
10593static tree
94edc4ab 10594cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10595{
10596 tree type_specifier_seq;
10597 tree abstract_declarator;
10598
10599 /* Parse the type-specifier-seq. */
10600 type_specifier_seq
10601 = cp_parser_type_specifier_seq (parser);
10602 if (type_specifier_seq == error_mark_node)
10603 return error_mark_node;
10604
10605 /* There might or might not be an abstract declarator. */
10606 cp_parser_parse_tentatively (parser);
10607 /* Look for the declarator. */
10608 abstract_declarator
4bb8ca28
MM
10609 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10610 /*parenthesized_p=*/NULL);
a723baf1
MM
10611 /* Check to see if there really was a declarator. */
10612 if (!cp_parser_parse_definitely (parser))
10613 abstract_declarator = NULL_TREE;
10614
10615 return groktypename (build_tree_list (type_specifier_seq,
10616 abstract_declarator));
10617}
10618
10619/* Parse a type-specifier-seq.
10620
10621 type-specifier-seq:
10622 type-specifier type-specifier-seq [opt]
10623
10624 GNU extension:
10625
10626 type-specifier-seq:
10627 attributes type-specifier-seq [opt]
10628
10629 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10630 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10631
10632static tree
94edc4ab 10633cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10634{
10635 bool seen_type_specifier = false;
10636 tree type_specifier_seq = NULL_TREE;
10637
10638 /* Parse the type-specifiers and attributes. */
10639 while (true)
10640 {
10641 tree type_specifier;
10642
10643 /* Check for attributes first. */
10644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10645 {
10646 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10647 NULL_TREE,
10648 type_specifier_seq);
10649 continue;
10650 }
10651
10652 /* After the first type-specifier, others are optional. */
10653 if (seen_type_specifier)
10654 cp_parser_parse_tentatively (parser);
10655 /* Look for the type-specifier. */
10656 type_specifier = cp_parser_type_specifier (parser,
10657 CP_PARSER_FLAGS_NONE,
10658 /*is_friend=*/false,
10659 /*is_declaration=*/false,
10660 NULL,
10661 NULL);
10662 /* If the first type-specifier could not be found, this is not a
10663 type-specifier-seq at all. */
10664 if (!seen_type_specifier && type_specifier == error_mark_node)
10665 return error_mark_node;
10666 /* If subsequent type-specifiers could not be found, the
10667 type-specifier-seq is complete. */
10668 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10669 break;
10670
10671 /* Add the new type-specifier to the list. */
10672 type_specifier_seq
10673 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10674 seen_type_specifier = true;
10675 }
10676
10677 /* We built up the list in reverse order. */
10678 return nreverse (type_specifier_seq);
10679}
10680
10681/* Parse a parameter-declaration-clause.
10682
10683 parameter-declaration-clause:
10684 parameter-declaration-list [opt] ... [opt]
10685 parameter-declaration-list , ...
10686
10687 Returns a representation for the parameter declarations. Each node
10688 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10689 representation.) If the parameter-declaration-clause ends with an
10690 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10691 list. A return value of NULL_TREE indicates a
10692 parameter-declaration-clause consisting only of an ellipsis. */
10693
10694static tree
94edc4ab 10695cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10696{
10697 tree parameters;
10698 cp_token *token;
10699 bool ellipsis_p;
10700
10701 /* Peek at the next token. */
10702 token = cp_lexer_peek_token (parser->lexer);
10703 /* Check for trivial parameter-declaration-clauses. */
10704 if (token->type == CPP_ELLIPSIS)
10705 {
10706 /* Consume the `...' token. */
10707 cp_lexer_consume_token (parser->lexer);
10708 return NULL_TREE;
10709 }
10710 else if (token->type == CPP_CLOSE_PAREN)
10711 /* There are no parameters. */
c73aecdf
DE
10712 {
10713#ifndef NO_IMPLICIT_EXTERN_C
10714 if (in_system_header && current_class_type == NULL
10715 && current_lang_name == lang_name_c)
10716 return NULL_TREE;
10717 else
10718#endif
10719 return void_list_node;
10720 }
a723baf1
MM
10721 /* Check for `(void)', too, which is a special case. */
10722 else if (token->keyword == RID_VOID
10723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10724 == CPP_CLOSE_PAREN))
10725 {
10726 /* Consume the `void' token. */
10727 cp_lexer_consume_token (parser->lexer);
10728 /* There are no parameters. */
10729 return void_list_node;
10730 }
10731
10732 /* Parse the parameter-declaration-list. */
10733 parameters = cp_parser_parameter_declaration_list (parser);
10734 /* If a parse error occurred while parsing the
10735 parameter-declaration-list, then the entire
10736 parameter-declaration-clause is erroneous. */
10737 if (parameters == error_mark_node)
10738 return error_mark_node;
10739
10740 /* Peek at the next token. */
10741 token = cp_lexer_peek_token (parser->lexer);
10742 /* If it's a `,', the clause should terminate with an ellipsis. */
10743 if (token->type == CPP_COMMA)
10744 {
10745 /* Consume the `,'. */
10746 cp_lexer_consume_token (parser->lexer);
10747 /* Expect an ellipsis. */
10748 ellipsis_p
10749 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10750 }
10751 /* It might also be `...' if the optional trailing `,' was
10752 omitted. */
10753 else if (token->type == CPP_ELLIPSIS)
10754 {
10755 /* Consume the `...' token. */
10756 cp_lexer_consume_token (parser->lexer);
10757 /* And remember that we saw it. */
10758 ellipsis_p = true;
10759 }
10760 else
10761 ellipsis_p = false;
10762
10763 /* Finish the parameter list. */
10764 return finish_parmlist (parameters, ellipsis_p);
10765}
10766
10767/* Parse a parameter-declaration-list.
10768
10769 parameter-declaration-list:
10770 parameter-declaration
10771 parameter-declaration-list , parameter-declaration
10772
10773 Returns a representation of the parameter-declaration-list, as for
10774 cp_parser_parameter_declaration_clause. However, the
10775 `void_list_node' is never appended to the list. */
10776
10777static tree
94edc4ab 10778cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10779{
10780 tree parameters = NULL_TREE;
10781
10782 /* Look for more parameters. */
10783 while (true)
10784 {
10785 tree parameter;
4bb8ca28 10786 bool parenthesized_p;
a723baf1
MM
10787 /* Parse the parameter. */
10788 parameter
4bb8ca28
MM
10789 = cp_parser_parameter_declaration (parser,
10790 /*template_parm_p=*/false,
10791 &parenthesized_p);
ec194454 10792
34cd5ae7 10793 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10794 then the entire parameter-declaration-list is erroneous. */
10795 if (parameter == error_mark_node)
10796 {
10797 parameters = error_mark_node;
10798 break;
10799 }
10800 /* Add the new parameter to the list. */
10801 TREE_CHAIN (parameter) = parameters;
10802 parameters = parameter;
10803
10804 /* Peek at the next token. */
10805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10806 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10807 /* The parameter-declaration-list is complete. */
10808 break;
10809 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10810 {
10811 cp_token *token;
10812
10813 /* Peek at the next token. */
10814 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10815 /* If it's an ellipsis, then the list is complete. */
10816 if (token->type == CPP_ELLIPSIS)
10817 break;
10818 /* Otherwise, there must be more parameters. Consume the
10819 `,'. */
10820 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
10821 /* When parsing something like:
10822
10823 int i(float f, double d)
10824
10825 we can tell after seeing the declaration for "f" that we
10826 are not looking at an initialization of a variable "i",
10827 but rather at the declaration of a function "i".
10828
10829 Due to the fact that the parsing of template arguments
10830 (as specified to a template-id) requires backtracking we
10831 cannot use this technique when inside a template argument
10832 list. */
10833 if (!parser->in_template_argument_list_p
10834 && cp_parser_parsing_tentatively (parser)
10835 && !cp_parser_committed_to_tentative_parse (parser)
10836 /* However, a parameter-declaration of the form
10837 "foat(f)" (which is a valid declaration of a
10838 parameter "f") can also be interpreted as an
10839 expression (the conversion of "f" to "float"). */
10840 && !parenthesized_p)
10841 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
10842 }
10843 else
10844 {
10845 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
10846 if (!cp_parser_parsing_tentatively (parser)
10847 || cp_parser_committed_to_tentative_parse (parser))
10848 cp_parser_skip_to_closing_parenthesis (parser,
10849 /*recovering=*/true,
5c832178 10850 /*or_comma=*/false,
4bb8ca28 10851 /*consume_paren=*/false);
a723baf1
MM
10852 break;
10853 }
10854 }
10855
10856 /* We built up the list in reverse order; straighten it out now. */
10857 return nreverse (parameters);
10858}
10859
10860/* Parse a parameter declaration.
10861
10862 parameter-declaration:
10863 decl-specifier-seq declarator
10864 decl-specifier-seq declarator = assignment-expression
10865 decl-specifier-seq abstract-declarator [opt]
10866 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10867
ec194454
MM
10868 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10869 declares a template parameter. (In that case, a non-nested `>'
10870 token encountered during the parsing of the assignment-expression
10871 is not interpreted as a greater-than operator.)
a723baf1
MM
10872
10873 Returns a TREE_LIST representing the parameter-declaration. The
4bb8ca28
MM
10874 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10875 there is no default argument. The TREE_VALUE is a representation
10876 of the decl-specifier-seq and declarator. In particular, the
10877 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10878 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10879 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10880 the declarator is of the form "(p)". */
a723baf1
MM
10881
10882static tree
ec194454 10883cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
10884 bool template_parm_p,
10885 bool *parenthesized_p)
a723baf1 10886{
560ad596 10887 int declares_class_or_enum;
ec194454 10888 bool greater_than_is_operator_p;
a723baf1
MM
10889 tree decl_specifiers;
10890 tree attributes;
10891 tree declarator;
10892 tree default_argument;
10893 tree parameter;
10894 cp_token *token;
10895 const char *saved_message;
10896
ec194454
MM
10897 /* In a template parameter, `>' is not an operator.
10898
10899 [temp.param]
10900
10901 When parsing a default template-argument for a non-type
10902 template-parameter, the first non-nested `>' is taken as the end
10903 of the template parameter-list rather than a greater-than
10904 operator. */
10905 greater_than_is_operator_p = !template_parm_p;
10906
a723baf1
MM
10907 /* Type definitions may not appear in parameter types. */
10908 saved_message = parser->type_definition_forbidden_message;
10909 parser->type_definition_forbidden_message
10910 = "types may not be defined in parameter types";
10911
10912 /* Parse the declaration-specifiers. */
10913 decl_specifiers
10914 = cp_parser_decl_specifier_seq (parser,
10915 CP_PARSER_FLAGS_NONE,
10916 &attributes,
10917 &declares_class_or_enum);
10918 /* If an error occurred, there's no reason to attempt to parse the
10919 rest of the declaration. */
10920 if (cp_parser_error_occurred (parser))
10921 {
10922 parser->type_definition_forbidden_message = saved_message;
10923 return error_mark_node;
10924 }
10925
10926 /* Peek at the next token. */
10927 token = cp_lexer_peek_token (parser->lexer);
10928 /* If the next token is a `)', `,', `=', `>', or `...', then there
10929 is no declarator. */
10930 if (token->type == CPP_CLOSE_PAREN
10931 || token->type == CPP_COMMA
10932 || token->type == CPP_EQ
10933 || token->type == CPP_ELLIPSIS
10934 || token->type == CPP_GREATER)
4bb8ca28
MM
10935 {
10936 declarator = NULL_TREE;
10937 if (parenthesized_p)
10938 *parenthesized_p = false;
10939 }
a723baf1
MM
10940 /* Otherwise, there should be a declarator. */
10941 else
10942 {
10943 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10944 parser->default_arg_ok_p = false;
10945
5c832178
MM
10946 /* After seeing a decl-specifier-seq, if the next token is not a
10947 "(", there is no possibility that the code is a valid
10948 expression initializer. Therefore, if parsing tentatively,
10949 we commit at this point. */
10950 if (!parser->in_template_argument_list_p
10951 && cp_parser_parsing_tentatively (parser)
10952 && !cp_parser_committed_to_tentative_parse (parser)
10953 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
10954 cp_parser_commit_to_tentative_parse (parser);
10955 /* Parse the declarator. */
a723baf1 10956 declarator = cp_parser_declarator (parser,
62b8a44e 10957 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
10958 /*ctor_dtor_or_conv_p=*/NULL,
10959 parenthesized_p);
a723baf1 10960 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10961 /* After the declarator, allow more attributes. */
10962 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10963 }
10964
62b8a44e 10965 /* The restriction on defining new types applies only to the type
a723baf1
MM
10966 of the parameter, not to the default argument. */
10967 parser->type_definition_forbidden_message = saved_message;
10968
10969 /* If the next token is `=', then process a default argument. */
10970 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10971 {
10972 bool saved_greater_than_is_operator_p;
10973 /* Consume the `='. */
10974 cp_lexer_consume_token (parser->lexer);
10975
10976 /* If we are defining a class, then the tokens that make up the
10977 default argument must be saved and processed later. */
ec194454
MM
10978 if (!template_parm_p && at_class_scope_p ()
10979 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10980 {
10981 unsigned depth = 0;
10982
10983 /* Create a DEFAULT_ARG to represented the unparsed default
10984 argument. */
10985 default_argument = make_node (DEFAULT_ARG);
10986 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10987
10988 /* Add tokens until we have processed the entire default
10989 argument. */
10990 while (true)
10991 {
10992 bool done = false;
10993 cp_token *token;
10994
10995 /* Peek at the next token. */
10996 token = cp_lexer_peek_token (parser->lexer);
10997 /* What we do depends on what token we have. */
10998 switch (token->type)
10999 {
11000 /* In valid code, a default argument must be
11001 immediately followed by a `,' `)', or `...'. */
11002 case CPP_COMMA:
11003 case CPP_CLOSE_PAREN:
11004 case CPP_ELLIPSIS:
11005 /* If we run into a non-nested `;', `}', or `]',
11006 then the code is invalid -- but the default
11007 argument is certainly over. */
11008 case CPP_SEMICOLON:
11009 case CPP_CLOSE_BRACE:
11010 case CPP_CLOSE_SQUARE:
11011 if (depth == 0)
11012 done = true;
11013 /* Update DEPTH, if necessary. */
11014 else if (token->type == CPP_CLOSE_PAREN
11015 || token->type == CPP_CLOSE_BRACE
11016 || token->type == CPP_CLOSE_SQUARE)
11017 --depth;
11018 break;
11019
11020 case CPP_OPEN_PAREN:
11021 case CPP_OPEN_SQUARE:
11022 case CPP_OPEN_BRACE:
11023 ++depth;
11024 break;
11025
11026 case CPP_GREATER:
11027 /* If we see a non-nested `>', and `>' is not an
11028 operator, then it marks the end of the default
11029 argument. */
11030 if (!depth && !greater_than_is_operator_p)
11031 done = true;
11032 break;
11033
11034 /* If we run out of tokens, issue an error message. */
11035 case CPP_EOF:
11036 error ("file ends in default argument");
11037 done = true;
11038 break;
11039
11040 case CPP_NAME:
11041 case CPP_SCOPE:
11042 /* In these cases, we should look for template-ids.
11043 For example, if the default argument is
11044 `X<int, double>()', we need to do name lookup to
11045 figure out whether or not `X' is a template; if
34cd5ae7 11046 so, the `,' does not end the default argument.
a723baf1
MM
11047
11048 That is not yet done. */
11049 break;
11050
11051 default:
11052 break;
11053 }
11054
11055 /* If we've reached the end, stop. */
11056 if (done)
11057 break;
11058
11059 /* Add the token to the token block. */
11060 token = cp_lexer_consume_token (parser->lexer);
11061 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11062 token);
11063 }
11064 }
11065 /* Outside of a class definition, we can just parse the
11066 assignment-expression. */
11067 else
11068 {
11069 bool saved_local_variables_forbidden_p;
11070
11071 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11072 set correctly. */
11073 saved_greater_than_is_operator_p
11074 = parser->greater_than_is_operator_p;
11075 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11076 /* Local variable names (and the `this' keyword) may not
11077 appear in a default argument. */
11078 saved_local_variables_forbidden_p
11079 = parser->local_variables_forbidden_p;
11080 parser->local_variables_forbidden_p = true;
11081 /* Parse the assignment-expression. */
11082 default_argument = cp_parser_assignment_expression (parser);
11083 /* Restore saved state. */
11084 parser->greater_than_is_operator_p
11085 = saved_greater_than_is_operator_p;
11086 parser->local_variables_forbidden_p
11087 = saved_local_variables_forbidden_p;
11088 }
11089 if (!parser->default_arg_ok_p)
11090 {
c67d36d0
NS
11091 if (!flag_pedantic_errors)
11092 warning ("deprecated use of default argument for parameter of non-function");
11093 else
11094 {
11095 error ("default arguments are only permitted for function parameters");
11096 default_argument = NULL_TREE;
11097 }
a723baf1
MM
11098 }
11099 }
11100 else
11101 default_argument = NULL_TREE;
11102
11103 /* Create the representation of the parameter. */
11104 if (attributes)
11105 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11106 parameter = build_tree_list (default_argument,
11107 build_tree_list (decl_specifiers,
11108 declarator));
11109
11110 return parameter;
11111}
11112
a723baf1
MM
11113/* Parse a function-body.
11114
11115 function-body:
11116 compound_statement */
11117
11118static void
11119cp_parser_function_body (cp_parser *parser)
11120{
a5bcc582 11121 cp_parser_compound_statement (parser, false);
a723baf1
MM
11122}
11123
11124/* Parse a ctor-initializer-opt followed by a function-body. Return
11125 true if a ctor-initializer was present. */
11126
11127static bool
11128cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11129{
11130 tree body;
11131 bool ctor_initializer_p;
11132
11133 /* Begin the function body. */
11134 body = begin_function_body ();
11135 /* Parse the optional ctor-initializer. */
11136 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11137 /* Parse the function-body. */
11138 cp_parser_function_body (parser);
11139 /* Finish the function body. */
11140 finish_function_body (body);
11141
11142 return ctor_initializer_p;
11143}
11144
11145/* Parse an initializer.
11146
11147 initializer:
11148 = initializer-clause
11149 ( expression-list )
11150
11151 Returns a expression representing the initializer. If no
11152 initializer is present, NULL_TREE is returned.
11153
11154 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11155 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11156 set to FALSE if there is no initializer present. If there is an
11157 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11158 is set to true; otherwise it is set to false. */
a723baf1
MM
11159
11160static tree
39703eb9
MM
11161cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11162 bool* non_constant_p)
a723baf1
MM
11163{
11164 cp_token *token;
11165 tree init;
11166
11167 /* Peek at the next token. */
11168 token = cp_lexer_peek_token (parser->lexer);
11169
11170 /* Let our caller know whether or not this initializer was
11171 parenthesized. */
11172 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11173 /* Assume that the initializer is constant. */
11174 *non_constant_p = false;
a723baf1
MM
11175
11176 if (token->type == CPP_EQ)
11177 {
11178 /* Consume the `='. */
11179 cp_lexer_consume_token (parser->lexer);
11180 /* Parse the initializer-clause. */
39703eb9 11181 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11182 }
11183 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11184 init = cp_parser_parenthesized_expression_list (parser, false,
11185 non_constant_p);
a723baf1
MM
11186 else
11187 {
11188 /* Anything else is an error. */
11189 cp_parser_error (parser, "expected initializer");
11190 init = error_mark_node;
11191 }
11192
11193 return init;
11194}
11195
11196/* Parse an initializer-clause.
11197
11198 initializer-clause:
11199 assignment-expression
11200 { initializer-list , [opt] }
11201 { }
11202
11203 Returns an expression representing the initializer.
11204
11205 If the `assignment-expression' production is used the value
34cd5ae7 11206 returned is simply a representation for the expression.
a723baf1
MM
11207
11208 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11209 the elements of the initializer-list (or NULL_TREE, if the last
11210 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11211 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11212 trailing `,' was provided. NON_CONSTANT_P is as for
11213 cp_parser_initializer. */
a723baf1
MM
11214
11215static tree
39703eb9 11216cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11217{
11218 tree initializer;
11219
11220 /* If it is not a `{', then we are looking at an
11221 assignment-expression. */
11222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
11223 initializer
11224 = cp_parser_constant_expression (parser,
11225 /*allow_non_constant_p=*/true,
11226 non_constant_p);
a723baf1
MM
11227 else
11228 {
11229 /* Consume the `{' token. */
11230 cp_lexer_consume_token (parser->lexer);
11231 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11232 initializer = make_node (CONSTRUCTOR);
11233 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11234 necessary, but check_initializer depends upon it, for
11235 now. */
11236 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11237 /* If it's not a `}', then there is a non-trivial initializer. */
11238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11239 {
11240 /* Parse the initializer list. */
11241 CONSTRUCTOR_ELTS (initializer)
39703eb9 11242 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
11243 /* A trailing `,' token is allowed. */
11244 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11245 cp_lexer_consume_token (parser->lexer);
11246 }
a723baf1
MM
11247 /* Now, there should be a trailing `}'. */
11248 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11249 }
11250
11251 return initializer;
11252}
11253
11254/* Parse an initializer-list.
11255
11256 initializer-list:
11257 initializer-clause
11258 initializer-list , initializer-clause
11259
11260 GNU Extension:
11261
11262 initializer-list:
11263 identifier : initializer-clause
11264 initializer-list, identifier : initializer-clause
11265
11266 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11267 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
11268 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11269 as for cp_parser_initializer. */
a723baf1
MM
11270
11271static tree
39703eb9 11272cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11273{
11274 tree initializers = NULL_TREE;
11275
39703eb9
MM
11276 /* Assume all of the expressions are constant. */
11277 *non_constant_p = false;
11278
a723baf1
MM
11279 /* Parse the rest of the list. */
11280 while (true)
11281 {
11282 cp_token *token;
11283 tree identifier;
11284 tree initializer;
39703eb9
MM
11285 bool clause_non_constant_p;
11286
a723baf1
MM
11287 /* If the next token is an identifier and the following one is a
11288 colon, we are looking at the GNU designated-initializer
11289 syntax. */
11290 if (cp_parser_allow_gnu_extensions_p (parser)
11291 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11292 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11293 {
11294 /* Consume the identifier. */
11295 identifier = cp_lexer_consume_token (parser->lexer)->value;
11296 /* Consume the `:'. */
11297 cp_lexer_consume_token (parser->lexer);
11298 }
11299 else
11300 identifier = NULL_TREE;
11301
11302 /* Parse the initializer. */
39703eb9
MM
11303 initializer = cp_parser_initializer_clause (parser,
11304 &clause_non_constant_p);
11305 /* If any clause is non-constant, so is the entire initializer. */
11306 if (clause_non_constant_p)
11307 *non_constant_p = true;
a723baf1
MM
11308 /* Add it to the list. */
11309 initializers = tree_cons (identifier, initializer, initializers);
11310
11311 /* If the next token is not a comma, we have reached the end of
11312 the list. */
11313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11314 break;
11315
11316 /* Peek at the next token. */
11317 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11318 /* If the next token is a `}', then we're still done. An
11319 initializer-clause can have a trailing `,' after the
11320 initializer-list and before the closing `}'. */
11321 if (token->type == CPP_CLOSE_BRACE)
11322 break;
11323
11324 /* Consume the `,' token. */
11325 cp_lexer_consume_token (parser->lexer);
11326 }
11327
11328 /* The initializers were built up in reverse order, so we need to
11329 reverse them now. */
11330 return nreverse (initializers);
11331}
11332
11333/* Classes [gram.class] */
11334
11335/* Parse a class-name.
11336
11337 class-name:
11338 identifier
11339 template-id
11340
11341 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11342 to indicate that names looked up in dependent types should be
11343 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11344 keyword has been used to indicate that the name that appears next
11345 is a template. TYPE_P is true iff the next name should be treated
11346 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11347 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11348 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11349 being defined in a class-head.
a723baf1
MM
11350
11351 Returns the TYPE_DECL representing the class. */
11352
11353static tree
11354cp_parser_class_name (cp_parser *parser,
11355 bool typename_keyword_p,
11356 bool template_keyword_p,
11357 bool type_p,
a723baf1 11358 bool check_dependency_p,
a668c6ad
MM
11359 bool class_head_p,
11360 bool is_declaration)
a723baf1
MM
11361{
11362 tree decl;
11363 tree scope;
11364 bool typename_p;
e5976695
MM
11365 cp_token *token;
11366
11367 /* All class-names start with an identifier. */
11368 token = cp_lexer_peek_token (parser->lexer);
11369 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11370 {
11371 cp_parser_error (parser, "expected class-name");
11372 return error_mark_node;
11373 }
11374
a723baf1
MM
11375 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11376 to a template-id, so we save it here. */
11377 scope = parser->scope;
3adee96c
KL
11378 if (scope == error_mark_node)
11379 return error_mark_node;
11380
a723baf1
MM
11381 /* Any name names a type if we're following the `typename' keyword
11382 in a qualified name where the enclosing scope is type-dependent. */
11383 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11384 && dependent_type_p (scope));
e5976695
MM
11385 /* Handle the common case (an identifier, but not a template-id)
11386 efficiently. */
11387 if (token->type == CPP_NAME
11388 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11389 {
a723baf1
MM
11390 tree identifier;
11391
11392 /* Look for the identifier. */
11393 identifier = cp_parser_identifier (parser);
11394 /* If the next token isn't an identifier, we are certainly not
11395 looking at a class-name. */
11396 if (identifier == error_mark_node)
11397 decl = error_mark_node;
11398 /* If we know this is a type-name, there's no need to look it
11399 up. */
11400 else if (typename_p)
11401 decl = identifier;
11402 else
11403 {
11404 /* If the next token is a `::', then the name must be a type
11405 name.
11406
11407 [basic.lookup.qual]
11408
11409 During the lookup for a name preceding the :: scope
11410 resolution operator, object, function, and enumerator
11411 names are ignored. */
11412 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11413 type_p = true;
11414 /* Look up the name. */
11415 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11416 type_p,
eea9800f 11417 /*is_namespace=*/false,
a723baf1
MM
11418 check_dependency_p);
11419 }
11420 }
e5976695
MM
11421 else
11422 {
11423 /* Try a template-id. */
11424 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
11425 check_dependency_p,
11426 is_declaration);
e5976695
MM
11427 if (decl == error_mark_node)
11428 return error_mark_node;
11429 }
a723baf1
MM
11430
11431 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11432
11433 /* If this is a typename, create a TYPENAME_TYPE. */
11434 if (typename_p && decl != error_mark_node)
11435 decl = TYPE_NAME (make_typename_type (scope, decl,
11436 /*complain=*/1));
11437
11438 /* Check to see that it is really the name of a class. */
11439 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11440 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11441 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11442 /* Situations like this:
11443
11444 template <typename T> struct A {
11445 typename T::template X<int>::I i;
11446 };
11447
11448 are problematic. Is `T::template X<int>' a class-name? The
11449 standard does not seem to be definitive, but there is no other
11450 valid interpretation of the following `::'. Therefore, those
11451 names are considered class-names. */
78757caa 11452 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11453 else if (decl == error_mark_node
11454 || TREE_CODE (decl) != TYPE_DECL
11455 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11456 {
11457 cp_parser_error (parser, "expected class-name");
11458 return error_mark_node;
11459 }
11460
11461 return decl;
11462}
11463
11464/* Parse a class-specifier.
11465
11466 class-specifier:
11467 class-head { member-specification [opt] }
11468
11469 Returns the TREE_TYPE representing the class. */
11470
11471static tree
94edc4ab 11472cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11473{
11474 cp_token *token;
11475 tree type;
11476 tree attributes = NULL_TREE;
11477 int has_trailing_semicolon;
11478 bool nested_name_specifier_p;
a723baf1
MM
11479 unsigned saved_num_template_parameter_lists;
11480
8d241e0b 11481 push_deferring_access_checks (dk_no_deferred);
cf22909c 11482
a723baf1
MM
11483 /* Parse the class-head. */
11484 type = cp_parser_class_head (parser,
cf22909c 11485 &nested_name_specifier_p);
a723baf1
MM
11486 /* If the class-head was a semantic disaster, skip the entire body
11487 of the class. */
11488 if (!type)
11489 {
11490 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11491 pop_deferring_access_checks ();
a723baf1
MM
11492 return error_mark_node;
11493 }
cf22909c 11494
a723baf1
MM
11495 /* Look for the `{'. */
11496 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11497 {
11498 pop_deferring_access_checks ();
11499 return error_mark_node;
11500 }
11501
a723baf1
MM
11502 /* Issue an error message if type-definitions are forbidden here. */
11503 cp_parser_check_type_definition (parser);
11504 /* Remember that we are defining one more class. */
11505 ++parser->num_classes_being_defined;
11506 /* Inside the class, surrounding template-parameter-lists do not
11507 apply. */
11508 saved_num_template_parameter_lists
11509 = parser->num_template_parameter_lists;
11510 parser->num_template_parameter_lists = 0;
78757caa 11511
a723baf1 11512 /* Start the class. */
eeb23c11
MM
11513 if (nested_name_specifier_p)
11514 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11515 type = begin_class_definition (type);
11516 if (type == error_mark_node)
9bcb9aae 11517 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11518 cp_parser_skip_to_closing_brace (parser);
11519 else
11520 /* Parse the member-specification. */
11521 cp_parser_member_specification_opt (parser);
11522 /* Look for the trailing `}'. */
11523 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11524 /* We get better error messages by noticing a common problem: a
11525 missing trailing `;'. */
11526 token = cp_lexer_peek_token (parser->lexer);
11527 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11528 /* Look for attributes to apply to this class. */
11529 if (cp_parser_allow_gnu_extensions_p (parser))
11530 attributes = cp_parser_attributes_opt (parser);
560ad596
MM
11531 /* If we got any attributes in class_head, xref_tag will stick them in
11532 TREE_TYPE of the type. Grab them now. */
11533 if (type != error_mark_node)
11534 {
11535 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11536 TYPE_ATTRIBUTES (type) = NULL_TREE;
11537 type = finish_struct (type, attributes);
11538 }
11539 if (nested_name_specifier_p)
11540 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11541 /* If this class is not itself within the scope of another class,
11542 then we need to parse the bodies of all of the queued function
11543 definitions. Note that the queued functions defined in a class
11544 are not always processed immediately following the
11545 class-specifier for that class. Consider:
11546
11547 struct A {
11548 struct B { void f() { sizeof (A); } };
11549 };
11550
11551 If `f' were processed before the processing of `A' were
11552 completed, there would be no way to compute the size of `A'.
11553 Note that the nesting we are interested in here is lexical --
11554 not the semantic nesting given by TYPE_CONTEXT. In particular,
11555 for:
11556
11557 struct A { struct B; };
11558 struct A::B { void f() { } };
11559
11560 there is no need to delay the parsing of `A::B::f'. */
11561 if (--parser->num_classes_being_defined == 0)
11562 {
8218bd34
MM
11563 tree queue_entry;
11564 tree fn;
a723baf1 11565
8218bd34
MM
11566 /* In a first pass, parse default arguments to the functions.
11567 Then, in a second pass, parse the bodies of the functions.
11568 This two-phased approach handles cases like:
11569
11570 struct S {
11571 void f() { g(); }
11572 void g(int i = 3);
11573 };
11574
11575 */
8db1028e
NS
11576 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11577 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11578 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11579 TREE_PURPOSE (parser->unparsed_functions_queues)
11580 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11581 {
11582 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11583 /* Make sure that any template parameters are in scope. */
11584 maybe_begin_member_template_processing (fn);
11585 /* If there are default arguments that have not yet been processed,
11586 take care of them now. */
11587 cp_parser_late_parsing_default_args (parser, fn);
11588 /* Remove any template parameters from the symbol table. */
11589 maybe_end_member_template_processing ();
11590 }
11591 /* Now parse the body of the functions. */
8db1028e
NS
11592 for (TREE_VALUE (parser->unparsed_functions_queues)
11593 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11594 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11595 TREE_VALUE (parser->unparsed_functions_queues)
11596 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11597 {
a723baf1 11598 /* Figure out which function we need to process. */
a723baf1
MM
11599 fn = TREE_VALUE (queue_entry);
11600
11601 /* Parse the function. */
11602 cp_parser_late_parsing_for_member (parser, fn);
a723baf1
MM
11603 }
11604
a723baf1
MM
11605 }
11606
11607 /* Put back any saved access checks. */
cf22909c 11608 pop_deferring_access_checks ();
a723baf1
MM
11609
11610 /* Restore the count of active template-parameter-lists. */
11611 parser->num_template_parameter_lists
11612 = saved_num_template_parameter_lists;
11613
11614 return type;
11615}
11616
11617/* Parse a class-head.
11618
11619 class-head:
11620 class-key identifier [opt] base-clause [opt]
11621 class-key nested-name-specifier identifier base-clause [opt]
11622 class-key nested-name-specifier [opt] template-id
11623 base-clause [opt]
11624
11625 GNU Extensions:
11626 class-key attributes identifier [opt] base-clause [opt]
11627 class-key attributes nested-name-specifier identifier base-clause [opt]
11628 class-key attributes nested-name-specifier [opt] template-id
11629 base-clause [opt]
11630
11631 Returns the TYPE of the indicated class. Sets
11632 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11633 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11634
11635 Returns NULL_TREE if the class-head is syntactically valid, but
11636 semantically invalid in a way that means we should skip the entire
11637 body of the class. */
11638
11639static tree
94edc4ab
NN
11640cp_parser_class_head (cp_parser* parser,
11641 bool* nested_name_specifier_p)
a723baf1
MM
11642{
11643 cp_token *token;
11644 tree nested_name_specifier;
11645 enum tag_types class_key;
11646 tree id = NULL_TREE;
11647 tree type = NULL_TREE;
11648 tree attributes;
11649 bool template_id_p = false;
11650 bool qualified_p = false;
11651 bool invalid_nested_name_p = false;
afb0918a 11652 bool invalid_explicit_specialization_p = false;
a723baf1
MM
11653 unsigned num_templates;
11654
11655 /* Assume no nested-name-specifier will be present. */
11656 *nested_name_specifier_p = false;
11657 /* Assume no template parameter lists will be used in defining the
11658 type. */
11659 num_templates = 0;
11660
11661 /* Look for the class-key. */
11662 class_key = cp_parser_class_key (parser);
11663 if (class_key == none_type)
11664 return error_mark_node;
11665
11666 /* Parse the attributes. */
11667 attributes = cp_parser_attributes_opt (parser);
11668
11669 /* If the next token is `::', that is invalid -- but sometimes
11670 people do try to write:
11671
11672 struct ::S {};
11673
11674 Handle this gracefully by accepting the extra qualifier, and then
11675 issuing an error about it later if this really is a
2050a1bb 11676 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11677 specifier, remain silent. */
11678 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11679 qualified_p = true;
11680
8d241e0b
KL
11681 push_deferring_access_checks (dk_no_check);
11682
a723baf1
MM
11683 /* Determine the name of the class. Begin by looking for an
11684 optional nested-name-specifier. */
11685 nested_name_specifier
11686 = cp_parser_nested_name_specifier_opt (parser,
11687 /*typename_keyword_p=*/false,
66d418e6 11688 /*check_dependency_p=*/false,
a668c6ad
MM
11689 /*type_p=*/false,
11690 /*is_declaration=*/false);
a723baf1
MM
11691 /* If there was a nested-name-specifier, then there *must* be an
11692 identifier. */
11693 if (nested_name_specifier)
11694 {
11695 /* Although the grammar says `identifier', it really means
11696 `class-name' or `template-name'. You are only allowed to
11697 define a class that has already been declared with this
11698 syntax.
11699
11700 The proposed resolution for Core Issue 180 says that whever
11701 you see `class T::X' you should treat `X' as a type-name.
11702
11703 It is OK to define an inaccessible class; for example:
11704
11705 class A { class B; };
11706 class A::B {};
11707
a723baf1
MM
11708 We do not know if we will see a class-name, or a
11709 template-name. We look for a class-name first, in case the
11710 class-name is a template-id; if we looked for the
11711 template-name first we would stop after the template-name. */
11712 cp_parser_parse_tentatively (parser);
11713 type = cp_parser_class_name (parser,
11714 /*typename_keyword_p=*/false,
11715 /*template_keyword_p=*/false,
11716 /*type_p=*/true,
a723baf1 11717 /*check_dependency_p=*/false,
a668c6ad
MM
11718 /*class_head_p=*/true,
11719 /*is_declaration=*/false);
a723baf1
MM
11720 /* If that didn't work, ignore the nested-name-specifier. */
11721 if (!cp_parser_parse_definitely (parser))
11722 {
11723 invalid_nested_name_p = true;
11724 id = cp_parser_identifier (parser);
11725 if (id == error_mark_node)
11726 id = NULL_TREE;
11727 }
11728 /* If we could not find a corresponding TYPE, treat this
11729 declaration like an unqualified declaration. */
11730 if (type == error_mark_node)
11731 nested_name_specifier = NULL_TREE;
11732 /* Otherwise, count the number of templates used in TYPE and its
11733 containing scopes. */
11734 else
11735 {
11736 tree scope;
11737
11738 for (scope = TREE_TYPE (type);
11739 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11740 scope = (TYPE_P (scope)
11741 ? TYPE_CONTEXT (scope)
11742 : DECL_CONTEXT (scope)))
11743 if (TYPE_P (scope)
11744 && CLASS_TYPE_P (scope)
11745 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11746 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11747 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11748 ++num_templates;
11749 }
11750 }
11751 /* Otherwise, the identifier is optional. */
11752 else
11753 {
11754 /* We don't know whether what comes next is a template-id,
11755 an identifier, or nothing at all. */
11756 cp_parser_parse_tentatively (parser);
11757 /* Check for a template-id. */
11758 id = cp_parser_template_id (parser,
11759 /*template_keyword_p=*/false,
a668c6ad
MM
11760 /*check_dependency_p=*/true,
11761 /*is_declaration=*/true);
a723baf1
MM
11762 /* If that didn't work, it could still be an identifier. */
11763 if (!cp_parser_parse_definitely (parser))
11764 {
11765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11766 id = cp_parser_identifier (parser);
11767 else
11768 id = NULL_TREE;
11769 }
11770 else
11771 {
11772 template_id_p = true;
11773 ++num_templates;
11774 }
11775 }
11776
8d241e0b
KL
11777 pop_deferring_access_checks ();
11778
ee43dab5
MM
11779 cp_parser_check_for_invalid_template_id (parser, id);
11780
a723baf1
MM
11781 /* If it's not a `:' or a `{' then we can't really be looking at a
11782 class-head, since a class-head only appears as part of a
11783 class-specifier. We have to detect this situation before calling
11784 xref_tag, since that has irreversible side-effects. */
11785 if (!cp_parser_next_token_starts_class_definition_p (parser))
11786 {
11787 cp_parser_error (parser, "expected `{' or `:'");
11788 return error_mark_node;
11789 }
11790
11791 /* At this point, we're going ahead with the class-specifier, even
11792 if some other problem occurs. */
11793 cp_parser_commit_to_tentative_parse (parser);
11794 /* Issue the error about the overly-qualified name now. */
11795 if (qualified_p)
11796 cp_parser_error (parser,
11797 "global qualification of class name is invalid");
11798 else if (invalid_nested_name_p)
11799 cp_parser_error (parser,
11800 "qualified name does not name a class");
afb0918a
MM
11801 /* An explicit-specialization must be preceded by "template <>". If
11802 it is not, try to recover gracefully. */
11803 if (at_namespace_scope_p ()
11804 && parser->num_template_parameter_lists == 0
eeb23c11 11805 && template_id_p)
afb0918a
MM
11806 {
11807 error ("an explicit specialization must be preceded by 'template <>'");
11808 invalid_explicit_specialization_p = true;
11809 /* Take the same action that would have been taken by
11810 cp_parser_explicit_specialization. */
11811 ++parser->num_template_parameter_lists;
11812 begin_specialization ();
11813 }
11814 /* There must be no "return" statements between this point and the
11815 end of this function; set "type "to the correct return value and
11816 use "goto done;" to return. */
a723baf1
MM
11817 /* Make sure that the right number of template parameters were
11818 present. */
11819 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
11820 {
11821 /* If something went wrong, there is no point in even trying to
11822 process the class-definition. */
11823 type = NULL_TREE;
11824 goto done;
11825 }
a723baf1 11826
a723baf1
MM
11827 /* Look up the type. */
11828 if (template_id_p)
11829 {
11830 type = TREE_TYPE (id);
11831 maybe_process_partial_specialization (type);
11832 }
11833 else if (!nested_name_specifier)
11834 {
11835 /* If the class was unnamed, create a dummy name. */
11836 if (!id)
11837 id = make_anon_name ();
cbd63935
KL
11838 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11839 parser->num_template_parameter_lists);
a723baf1
MM
11840 }
11841 else
11842 {
a723baf1 11843 tree class_type;
089d6ea7 11844 tree scope;
a723baf1
MM
11845
11846 /* Given:
11847
11848 template <typename T> struct S { struct T };
14d22dd6 11849 template <typename T> struct S<T>::T { };
a723baf1
MM
11850
11851 we will get a TYPENAME_TYPE when processing the definition of
11852 `S::T'. We need to resolve it to the actual type before we
11853 try to define it. */
11854 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11855 {
14d22dd6
MM
11856 class_type = resolve_typename_type (TREE_TYPE (type),
11857 /*only_current_p=*/false);
11858 if (class_type != error_mark_node)
11859 type = TYPE_NAME (class_type);
11860 else
11861 {
11862 cp_parser_error (parser, "could not resolve typename type");
11863 type = error_mark_node;
11864 }
a723baf1
MM
11865 }
11866
089d6ea7
MM
11867 /* Figure out in what scope the declaration is being placed. */
11868 scope = current_scope ();
11869 if (!scope)
11870 scope = current_namespace;
11871 /* If that scope does not contain the scope in which the
11872 class was originally declared, the program is invalid. */
11873 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11874 {
0e136342 11875 error ("declaration of `%D' in `%D' which does not "
089d6ea7 11876 "enclose `%D'", type, scope, nested_name_specifier);
afb0918a
MM
11877 type = NULL_TREE;
11878 goto done;
089d6ea7 11879 }
560ad596 11880 /* [dcl.meaning]
089d6ea7 11881
560ad596
MM
11882 A declarator-id shall not be qualified exception of the
11883 definition of a ... nested class outside of its class
11884 ... [or] a the definition or explicit instantiation of a
11885 class member of a namespace outside of its namespace. */
11886 if (scope == CP_DECL_CONTEXT (type))
a723baf1 11887 {
560ad596
MM
11888 pedwarn ("extra qualification ignored");
11889 nested_name_specifier = NULL_TREE;
a723baf1 11890 }
560ad596
MM
11891
11892 maybe_process_partial_specialization (TREE_TYPE (type));
11893 class_type = current_class_type;
11894 /* Enter the scope indicated by the nested-name-specifier. */
11895 if (nested_name_specifier)
11896 push_scope (nested_name_specifier);
11897 /* Get the canonical version of this type. */
11898 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11899 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11900 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11901 type = push_template_decl (type);
11902 type = TREE_TYPE (type);
11903 if (nested_name_specifier)
eeb23c11
MM
11904 {
11905 *nested_name_specifier_p = true;
11906 pop_scope (nested_name_specifier);
11907 }
a723baf1
MM
11908 }
11909 /* Indicate whether this class was declared as a `class' or as a
11910 `struct'. */
11911 if (TREE_CODE (type) == RECORD_TYPE)
11912 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11913 cp_parser_check_class_key (class_key, type);
11914
11915 /* Enter the scope containing the class; the names of base classes
11916 should be looked up in that context. For example, given:
11917
11918 struct A { struct B {}; struct C; };
11919 struct A::C : B {};
11920
11921 is valid. */
11922 if (nested_name_specifier)
11923 push_scope (nested_name_specifier);
11924 /* Now, look for the base-clause. */
11925 token = cp_lexer_peek_token (parser->lexer);
11926 if (token->type == CPP_COLON)
11927 {
11928 tree bases;
11929
11930 /* Get the list of base-classes. */
11931 bases = cp_parser_base_clause (parser);
11932 /* Process them. */
11933 xref_basetypes (type, bases);
11934 }
11935 /* Leave the scope given by the nested-name-specifier. We will
11936 enter the class scope itself while processing the members. */
11937 if (nested_name_specifier)
11938 pop_scope (nested_name_specifier);
11939
afb0918a
MM
11940 done:
11941 if (invalid_explicit_specialization_p)
11942 {
11943 end_specialization ();
11944 --parser->num_template_parameter_lists;
11945 }
a723baf1
MM
11946 return type;
11947}
11948
11949/* Parse a class-key.
11950
11951 class-key:
11952 class
11953 struct
11954 union
11955
11956 Returns the kind of class-key specified, or none_type to indicate
11957 error. */
11958
11959static enum tag_types
94edc4ab 11960cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11961{
11962 cp_token *token;
11963 enum tag_types tag_type;
11964
11965 /* Look for the class-key. */
11966 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11967 if (!token)
11968 return none_type;
11969
11970 /* Check to see if the TOKEN is a class-key. */
11971 tag_type = cp_parser_token_is_class_key (token);
11972 if (!tag_type)
11973 cp_parser_error (parser, "expected class-key");
11974 return tag_type;
11975}
11976
11977/* Parse an (optional) member-specification.
11978
11979 member-specification:
11980 member-declaration member-specification [opt]
11981 access-specifier : member-specification [opt] */
11982
11983static void
94edc4ab 11984cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11985{
11986 while (true)
11987 {
11988 cp_token *token;
11989 enum rid keyword;
11990
11991 /* Peek at the next token. */
11992 token = cp_lexer_peek_token (parser->lexer);
11993 /* If it's a `}', or EOF then we've seen all the members. */
11994 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11995 break;
11996
11997 /* See if this token is a keyword. */
11998 keyword = token->keyword;
11999 switch (keyword)
12000 {
12001 case RID_PUBLIC:
12002 case RID_PROTECTED:
12003 case RID_PRIVATE:
12004 /* Consume the access-specifier. */
12005 cp_lexer_consume_token (parser->lexer);
12006 /* Remember which access-specifier is active. */
12007 current_access_specifier = token->value;
12008 /* Look for the `:'. */
12009 cp_parser_require (parser, CPP_COLON, "`:'");
12010 break;
12011
12012 default:
12013 /* Otherwise, the next construction must be a
12014 member-declaration. */
12015 cp_parser_member_declaration (parser);
a723baf1
MM
12016 }
12017 }
12018}
12019
12020/* Parse a member-declaration.
12021
12022 member-declaration:
12023 decl-specifier-seq [opt] member-declarator-list [opt] ;
12024 function-definition ; [opt]
12025 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12026 using-declaration
12027 template-declaration
12028
12029 member-declarator-list:
12030 member-declarator
12031 member-declarator-list , member-declarator
12032
12033 member-declarator:
12034 declarator pure-specifier [opt]
12035 declarator constant-initializer [opt]
12036 identifier [opt] : constant-expression
12037
12038 GNU Extensions:
12039
12040 member-declaration:
12041 __extension__ member-declaration
12042
12043 member-declarator:
12044 declarator attributes [opt] pure-specifier [opt]
12045 declarator attributes [opt] constant-initializer [opt]
12046 identifier [opt] attributes [opt] : constant-expression */
12047
12048static void
94edc4ab 12049cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
12050{
12051 tree decl_specifiers;
12052 tree prefix_attributes;
12053 tree decl;
560ad596 12054 int declares_class_or_enum;
a723baf1
MM
12055 bool friend_p;
12056 cp_token *token;
12057 int saved_pedantic;
12058
12059 /* Check for the `__extension__' keyword. */
12060 if (cp_parser_extension_opt (parser, &saved_pedantic))
12061 {
12062 /* Recurse. */
12063 cp_parser_member_declaration (parser);
12064 /* Restore the old value of the PEDANTIC flag. */
12065 pedantic = saved_pedantic;
12066
12067 return;
12068 }
12069
12070 /* Check for a template-declaration. */
12071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12072 {
12073 /* Parse the template-declaration. */
12074 cp_parser_template_declaration (parser, /*member_p=*/true);
12075
12076 return;
12077 }
12078
12079 /* Check for a using-declaration. */
12080 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12081 {
12082 /* Parse the using-declaration. */
12083 cp_parser_using_declaration (parser);
12084
12085 return;
12086 }
12087
a723baf1
MM
12088 /* Parse the decl-specifier-seq. */
12089 decl_specifiers
12090 = cp_parser_decl_specifier_seq (parser,
12091 CP_PARSER_FLAGS_OPTIONAL,
12092 &prefix_attributes,
12093 &declares_class_or_enum);
8fbc5ae7
MM
12094 /* Check for an invalid type-name. */
12095 if (cp_parser_diagnose_invalid_type_name (parser))
12096 return;
a723baf1
MM
12097 /* If there is no declarator, then the decl-specifier-seq should
12098 specify a type. */
12099 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12100 {
12101 /* If there was no decl-specifier-seq, and the next token is a
12102 `;', then we have something like:
12103
12104 struct S { ; };
12105
12106 [class.mem]
12107
12108 Each member-declaration shall declare at least one member
12109 name of the class. */
12110 if (!decl_specifiers)
12111 {
12112 if (pedantic)
12113 pedwarn ("extra semicolon");
12114 }
12115 else
12116 {
12117 tree type;
12118
12119 /* See if this declaration is a friend. */
12120 friend_p = cp_parser_friend_p (decl_specifiers);
12121 /* If there were decl-specifiers, check to see if there was
12122 a class-declaration. */
12123 type = check_tag_decl (decl_specifiers);
12124 /* Nested classes have already been added to the class, but
12125 a `friend' needs to be explicitly registered. */
12126 if (friend_p)
12127 {
12128 /* If the `friend' keyword was present, the friend must
12129 be introduced with a class-key. */
12130 if (!declares_class_or_enum)
12131 error ("a class-key must be used when declaring a friend");
12132 /* In this case:
12133
12134 template <typename T> struct A {
12135 friend struct A<T>::B;
12136 };
12137
12138 A<T>::B will be represented by a TYPENAME_TYPE, and
12139 therefore not recognized by check_tag_decl. */
12140 if (!type)
12141 {
12142 tree specifier;
12143
12144 for (specifier = decl_specifiers;
12145 specifier;
12146 specifier = TREE_CHAIN (specifier))
12147 {
12148 tree s = TREE_VALUE (specifier);
12149
c003e212
GDR
12150 if (TREE_CODE (s) == IDENTIFIER_NODE)
12151 get_global_value_if_present (s, &type);
a723baf1
MM
12152 if (TREE_CODE (s) == TYPE_DECL)
12153 s = TREE_TYPE (s);
12154 if (TYPE_P (s))
12155 {
12156 type = s;
12157 break;
12158 }
12159 }
12160 }
12161 if (!type)
12162 error ("friend declaration does not name a class or "
12163 "function");
12164 else
19db77ce
KL
12165 make_friend_class (current_class_type, type,
12166 /*complain=*/true);
a723baf1
MM
12167 }
12168 /* If there is no TYPE, an error message will already have
12169 been issued. */
12170 else if (!type)
12171 ;
12172 /* An anonymous aggregate has to be handled specially; such
12173 a declaration really declares a data member (with a
12174 particular type), as opposed to a nested class. */
12175 else if (ANON_AGGR_TYPE_P (type))
12176 {
12177 /* Remove constructors and such from TYPE, now that we
34cd5ae7 12178 know it is an anonymous aggregate. */
a723baf1
MM
12179 fixup_anonymous_aggr (type);
12180 /* And make the corresponding data member. */
12181 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12182 /* Add it to the class. */
12183 finish_member_declaration (decl);
12184 }
37d407a1
KL
12185 else
12186 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
12187 }
12188 }
12189 else
12190 {
12191 /* See if these declarations will be friends. */
12192 friend_p = cp_parser_friend_p (decl_specifiers);
12193
12194 /* Keep going until we hit the `;' at the end of the
12195 declaration. */
12196 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12197 {
12198 tree attributes = NULL_TREE;
12199 tree first_attribute;
12200
12201 /* Peek at the next token. */
12202 token = cp_lexer_peek_token (parser->lexer);
12203
12204 /* Check for a bitfield declaration. */
12205 if (token->type == CPP_COLON
12206 || (token->type == CPP_NAME
12207 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12208 == CPP_COLON))
12209 {
12210 tree identifier;
12211 tree width;
12212
12213 /* Get the name of the bitfield. Note that we cannot just
12214 check TOKEN here because it may have been invalidated by
12215 the call to cp_lexer_peek_nth_token above. */
12216 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12217 identifier = cp_parser_identifier (parser);
12218 else
12219 identifier = NULL_TREE;
12220
12221 /* Consume the `:' token. */
12222 cp_lexer_consume_token (parser->lexer);
12223 /* Get the width of the bitfield. */
14d22dd6
MM
12224 width
12225 = cp_parser_constant_expression (parser,
12226 /*allow_non_constant=*/false,
12227 NULL);
a723baf1
MM
12228
12229 /* Look for attributes that apply to the bitfield. */
12230 attributes = cp_parser_attributes_opt (parser);
12231 /* Remember which attributes are prefix attributes and
12232 which are not. */
12233 first_attribute = attributes;
12234 /* Combine the attributes. */
12235 attributes = chainon (prefix_attributes, attributes);
12236
12237 /* Create the bitfield declaration. */
12238 decl = grokbitfield (identifier,
12239 decl_specifiers,
12240 width);
12241 /* Apply the attributes. */
12242 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12243 }
12244 else
12245 {
12246 tree declarator;
12247 tree initializer;
12248 tree asm_specification;
7efa3e22 12249 int ctor_dtor_or_conv_p;
a723baf1
MM
12250
12251 /* Parse the declarator. */
12252 declarator
62b8a44e 12253 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
12254 &ctor_dtor_or_conv_p,
12255 /*parenthesized_p=*/NULL);
a723baf1
MM
12256
12257 /* If something went wrong parsing the declarator, make sure
12258 that we at least consume some tokens. */
12259 if (declarator == error_mark_node)
12260 {
12261 /* Skip to the end of the statement. */
12262 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
12263 /* If the next token is not a semicolon, that is
12264 probably because we just skipped over the body of
12265 a function. So, we consume a semicolon if
12266 present, but do not issue an error message if it
12267 is not present. */
12268 if (cp_lexer_next_token_is (parser->lexer,
12269 CPP_SEMICOLON))
12270 cp_lexer_consume_token (parser->lexer);
12271 return;
a723baf1
MM
12272 }
12273
560ad596
MM
12274 cp_parser_check_for_definition_in_return_type
12275 (declarator, declares_class_or_enum);
12276
a723baf1
MM
12277 /* Look for an asm-specification. */
12278 asm_specification = cp_parser_asm_specification_opt (parser);
12279 /* Look for attributes that apply to the declaration. */
12280 attributes = cp_parser_attributes_opt (parser);
12281 /* Remember which attributes are prefix attributes and
12282 which are not. */
12283 first_attribute = attributes;
12284 /* Combine the attributes. */
12285 attributes = chainon (prefix_attributes, attributes);
12286
12287 /* If it's an `=', then we have a constant-initializer or a
12288 pure-specifier. It is not correct to parse the
12289 initializer before registering the member declaration
12290 since the member declaration should be in scope while
12291 its initializer is processed. However, the rest of the
12292 front end does not yet provide an interface that allows
12293 us to handle this correctly. */
12294 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12295 {
12296 /* In [class.mem]:
12297
12298 A pure-specifier shall be used only in the declaration of
12299 a virtual function.
12300
12301 A member-declarator can contain a constant-initializer
12302 only if it declares a static member of integral or
12303 enumeration type.
12304
12305 Therefore, if the DECLARATOR is for a function, we look
12306 for a pure-specifier; otherwise, we look for a
12307 constant-initializer. When we call `grokfield', it will
12308 perform more stringent semantics checks. */
12309 if (TREE_CODE (declarator) == CALL_EXPR)
12310 initializer = cp_parser_pure_specifier (parser);
12311 else
4bb8ca28
MM
12312 /* Parse the initializer. */
12313 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
12314 }
12315 /* Otherwise, there is no initializer. */
12316 else
12317 initializer = NULL_TREE;
12318
12319 /* See if we are probably looking at a function
12320 definition. We are certainly not looking at at a
12321 member-declarator. Calling `grokfield' has
12322 side-effects, so we must not do it unless we are sure
12323 that we are looking at a member-declarator. */
12324 if (cp_parser_token_starts_function_definition_p
12325 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
12326 {
12327 /* The grammar does not allow a pure-specifier to be
12328 used when a member function is defined. (It is
12329 possible that this fact is an oversight in the
12330 standard, since a pure function may be defined
12331 outside of the class-specifier. */
12332 if (initializer)
12333 error ("pure-specifier on function-definition");
12334 decl = cp_parser_save_member_function_body (parser,
12335 decl_specifiers,
12336 declarator,
12337 attributes);
12338 /* If the member was not a friend, declare it here. */
12339 if (!friend_p)
12340 finish_member_declaration (decl);
12341 /* Peek at the next token. */
12342 token = cp_lexer_peek_token (parser->lexer);
12343 /* If the next token is a semicolon, consume it. */
12344 if (token->type == CPP_SEMICOLON)
12345 cp_lexer_consume_token (parser->lexer);
12346 return;
12347 }
a723baf1 12348 else
39703eb9
MM
12349 {
12350 /* Create the declaration. */
ee3071ef
NS
12351 decl = grokfield (declarator, decl_specifiers,
12352 initializer, asm_specification,
39703eb9
MM
12353 attributes);
12354 /* Any initialization must have been from a
12355 constant-expression. */
12356 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12357 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12358 }
a723baf1
MM
12359 }
12360
12361 /* Reset PREFIX_ATTRIBUTES. */
12362 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12363 attributes = TREE_CHAIN (attributes);
12364 if (attributes)
12365 TREE_CHAIN (attributes) = NULL_TREE;
12366
12367 /* If there is any qualification still in effect, clear it
12368 now; we will be starting fresh with the next declarator. */
12369 parser->scope = NULL_TREE;
12370 parser->qualifying_scope = NULL_TREE;
12371 parser->object_scope = NULL_TREE;
12372 /* If it's a `,', then there are more declarators. */
12373 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12374 cp_lexer_consume_token (parser->lexer);
12375 /* If the next token isn't a `;', then we have a parse error. */
12376 else if (cp_lexer_next_token_is_not (parser->lexer,
12377 CPP_SEMICOLON))
12378 {
12379 cp_parser_error (parser, "expected `;'");
04c06002 12380 /* Skip tokens until we find a `;'. */
a723baf1
MM
12381 cp_parser_skip_to_end_of_statement (parser);
12382
12383 break;
12384 }
12385
12386 if (decl)
12387 {
12388 /* Add DECL to the list of members. */
12389 if (!friend_p)
12390 finish_member_declaration (decl);
12391
a723baf1 12392 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12393 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12394 }
12395 }
12396 }
12397
4bb8ca28 12398 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
12399}
12400
12401/* Parse a pure-specifier.
12402
12403 pure-specifier:
12404 = 0
12405
12406 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12407 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12408
12409static tree
94edc4ab 12410cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12411{
12412 cp_token *token;
12413
12414 /* Look for the `=' token. */
12415 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12416 return error_mark_node;
12417 /* Look for the `0' token. */
12418 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12419 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12420 to get information from the lexer about how the number was
12421 spelled in order to fix this problem. */
12422 if (!token || !integer_zerop (token->value))
12423 return error_mark_node;
12424
12425 return integer_zero_node;
12426}
12427
12428/* Parse a constant-initializer.
12429
12430 constant-initializer:
12431 = constant-expression
12432
12433 Returns a representation of the constant-expression. */
12434
12435static tree
94edc4ab 12436cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12437{
12438 /* Look for the `=' token. */
12439 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12440 return error_mark_node;
12441
12442 /* It is invalid to write:
12443
12444 struct S { static const int i = { 7 }; };
12445
12446 */
12447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12448 {
12449 cp_parser_error (parser,
12450 "a brace-enclosed initializer is not allowed here");
12451 /* Consume the opening brace. */
12452 cp_lexer_consume_token (parser->lexer);
12453 /* Skip the initializer. */
12454 cp_parser_skip_to_closing_brace (parser);
12455 /* Look for the trailing `}'. */
12456 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12457
12458 return error_mark_node;
12459 }
12460
14d22dd6
MM
12461 return cp_parser_constant_expression (parser,
12462 /*allow_non_constant=*/false,
12463 NULL);
a723baf1
MM
12464}
12465
12466/* Derived classes [gram.class.derived] */
12467
12468/* Parse a base-clause.
12469
12470 base-clause:
12471 : base-specifier-list
12472
12473 base-specifier-list:
12474 base-specifier
12475 base-specifier-list , base-specifier
12476
12477 Returns a TREE_LIST representing the base-classes, in the order in
12478 which they were declared. The representation of each node is as
12479 described by cp_parser_base_specifier.
12480
12481 In the case that no bases are specified, this function will return
12482 NULL_TREE, not ERROR_MARK_NODE. */
12483
12484static tree
94edc4ab 12485cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12486{
12487 tree bases = NULL_TREE;
12488
12489 /* Look for the `:' that begins the list. */
12490 cp_parser_require (parser, CPP_COLON, "`:'");
12491
12492 /* Scan the base-specifier-list. */
12493 while (true)
12494 {
12495 cp_token *token;
12496 tree base;
12497
12498 /* Look for the base-specifier. */
12499 base = cp_parser_base_specifier (parser);
12500 /* Add BASE to the front of the list. */
12501 if (base != error_mark_node)
12502 {
12503 TREE_CHAIN (base) = bases;
12504 bases = base;
12505 }
12506 /* Peek at the next token. */
12507 token = cp_lexer_peek_token (parser->lexer);
12508 /* If it's not a comma, then the list is complete. */
12509 if (token->type != CPP_COMMA)
12510 break;
12511 /* Consume the `,'. */
12512 cp_lexer_consume_token (parser->lexer);
12513 }
12514
12515 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12516 base class had a qualified name. However, the next name that
12517 appears is certainly not qualified. */
12518 parser->scope = NULL_TREE;
12519 parser->qualifying_scope = NULL_TREE;
12520 parser->object_scope = NULL_TREE;
12521
12522 return nreverse (bases);
12523}
12524
12525/* Parse a base-specifier.
12526
12527 base-specifier:
12528 :: [opt] nested-name-specifier [opt] class-name
12529 virtual access-specifier [opt] :: [opt] nested-name-specifier
12530 [opt] class-name
12531 access-specifier virtual [opt] :: [opt] nested-name-specifier
12532 [opt] class-name
12533
12534 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12535 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12536 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12537 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12538
12539static tree
94edc4ab 12540cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12541{
12542 cp_token *token;
12543 bool done = false;
12544 bool virtual_p = false;
12545 bool duplicate_virtual_error_issued_p = false;
12546 bool duplicate_access_error_issued_p = false;
bbaab916 12547 bool class_scope_p, template_p;
dbbf88d1 12548 tree access = access_default_node;
a723baf1
MM
12549 tree type;
12550
12551 /* Process the optional `virtual' and `access-specifier'. */
12552 while (!done)
12553 {
12554 /* Peek at the next token. */
12555 token = cp_lexer_peek_token (parser->lexer);
12556 /* Process `virtual'. */
12557 switch (token->keyword)
12558 {
12559 case RID_VIRTUAL:
12560 /* If `virtual' appears more than once, issue an error. */
12561 if (virtual_p && !duplicate_virtual_error_issued_p)
12562 {
12563 cp_parser_error (parser,
12564 "`virtual' specified more than once in base-specified");
12565 duplicate_virtual_error_issued_p = true;
12566 }
12567
12568 virtual_p = true;
12569
12570 /* Consume the `virtual' token. */
12571 cp_lexer_consume_token (parser->lexer);
12572
12573 break;
12574
12575 case RID_PUBLIC:
12576 case RID_PROTECTED:
12577 case RID_PRIVATE:
12578 /* If more than one access specifier appears, issue an
12579 error. */
dbbf88d1
NS
12580 if (access != access_default_node
12581 && !duplicate_access_error_issued_p)
a723baf1
MM
12582 {
12583 cp_parser_error (parser,
12584 "more than one access specifier in base-specified");
12585 duplicate_access_error_issued_p = true;
12586 }
12587
dbbf88d1 12588 access = ridpointers[(int) token->keyword];
a723baf1
MM
12589
12590 /* Consume the access-specifier. */
12591 cp_lexer_consume_token (parser->lexer);
12592
12593 break;
12594
12595 default:
12596 done = true;
12597 break;
12598 }
12599 }
12600
a723baf1
MM
12601 /* Look for the optional `::' operator. */
12602 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12603 /* Look for the nested-name-specifier. The simplest way to
12604 implement:
12605
12606 [temp.res]
12607
12608 The keyword `typename' is not permitted in a base-specifier or
12609 mem-initializer; in these contexts a qualified name that
12610 depends on a template-parameter is implicitly assumed to be a
12611 type name.
12612
12613 is to pretend that we have seen the `typename' keyword at this
12614 point. */
12615 cp_parser_nested_name_specifier_opt (parser,
12616 /*typename_keyword_p=*/true,
12617 /*check_dependency_p=*/true,
a668c6ad
MM
12618 /*type_p=*/true,
12619 /*is_declaration=*/true);
a723baf1
MM
12620 /* If the base class is given by a qualified name, assume that names
12621 we see are type names or templates, as appropriate. */
12622 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12623 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12624
a723baf1
MM
12625 /* Finally, look for the class-name. */
12626 type = cp_parser_class_name (parser,
12627 class_scope_p,
bbaab916 12628 template_p,
a723baf1 12629 /*type_p=*/true,
a723baf1 12630 /*check_dependency_p=*/true,
a668c6ad
MM
12631 /*class_head_p=*/false,
12632 /*is_declaration=*/true);
a723baf1
MM
12633
12634 if (type == error_mark_node)
12635 return error_mark_node;
12636
dbbf88d1 12637 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12638}
12639
12640/* Exception handling [gram.exception] */
12641
12642/* Parse an (optional) exception-specification.
12643
12644 exception-specification:
12645 throw ( type-id-list [opt] )
12646
12647 Returns a TREE_LIST representing the exception-specification. The
12648 TREE_VALUE of each node is a type. */
12649
12650static tree
94edc4ab 12651cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12652{
12653 cp_token *token;
12654 tree type_id_list;
12655
12656 /* Peek at the next token. */
12657 token = cp_lexer_peek_token (parser->lexer);
12658 /* If it's not `throw', then there's no exception-specification. */
12659 if (!cp_parser_is_keyword (token, RID_THROW))
12660 return NULL_TREE;
12661
12662 /* Consume the `throw'. */
12663 cp_lexer_consume_token (parser->lexer);
12664
12665 /* Look for the `('. */
12666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12667
12668 /* Peek at the next token. */
12669 token = cp_lexer_peek_token (parser->lexer);
12670 /* If it's not a `)', then there is a type-id-list. */
12671 if (token->type != CPP_CLOSE_PAREN)
12672 {
12673 const char *saved_message;
12674
12675 /* Types may not be defined in an exception-specification. */
12676 saved_message = parser->type_definition_forbidden_message;
12677 parser->type_definition_forbidden_message
12678 = "types may not be defined in an exception-specification";
12679 /* Parse the type-id-list. */
12680 type_id_list = cp_parser_type_id_list (parser);
12681 /* Restore the saved message. */
12682 parser->type_definition_forbidden_message = saved_message;
12683 }
12684 else
12685 type_id_list = empty_except_spec;
12686
12687 /* Look for the `)'. */
12688 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12689
12690 return type_id_list;
12691}
12692
12693/* Parse an (optional) type-id-list.
12694
12695 type-id-list:
12696 type-id
12697 type-id-list , type-id
12698
12699 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12700 in the order that the types were presented. */
12701
12702static tree
94edc4ab 12703cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12704{
12705 tree types = NULL_TREE;
12706
12707 while (true)
12708 {
12709 cp_token *token;
12710 tree type;
12711
12712 /* Get the next type-id. */
12713 type = cp_parser_type_id (parser);
12714 /* Add it to the list. */
12715 types = add_exception_specifier (types, type, /*complain=*/1);
12716 /* Peek at the next token. */
12717 token = cp_lexer_peek_token (parser->lexer);
12718 /* If it is not a `,', we are done. */
12719 if (token->type != CPP_COMMA)
12720 break;
12721 /* Consume the `,'. */
12722 cp_lexer_consume_token (parser->lexer);
12723 }
12724
12725 return nreverse (types);
12726}
12727
12728/* Parse a try-block.
12729
12730 try-block:
12731 try compound-statement handler-seq */
12732
12733static tree
94edc4ab 12734cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12735{
12736 tree try_block;
12737
12738 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12739 try_block = begin_try_block ();
a5bcc582 12740 cp_parser_compound_statement (parser, false);
a723baf1
MM
12741 finish_try_block (try_block);
12742 cp_parser_handler_seq (parser);
12743 finish_handler_sequence (try_block);
12744
12745 return try_block;
12746}
12747
12748/* Parse a function-try-block.
12749
12750 function-try-block:
12751 try ctor-initializer [opt] function-body handler-seq */
12752
12753static bool
94edc4ab 12754cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12755{
12756 tree try_block;
12757 bool ctor_initializer_p;
12758
12759 /* Look for the `try' keyword. */
12760 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12761 return false;
12762 /* Let the rest of the front-end know where we are. */
12763 try_block = begin_function_try_block ();
12764 /* Parse the function-body. */
12765 ctor_initializer_p
12766 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12767 /* We're done with the `try' part. */
12768 finish_function_try_block (try_block);
12769 /* Parse the handlers. */
12770 cp_parser_handler_seq (parser);
12771 /* We're done with the handlers. */
12772 finish_function_handler_sequence (try_block);
12773
12774 return ctor_initializer_p;
12775}
12776
12777/* Parse a handler-seq.
12778
12779 handler-seq:
12780 handler handler-seq [opt] */
12781
12782static void
94edc4ab 12783cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12784{
12785 while (true)
12786 {
12787 cp_token *token;
12788
12789 /* Parse the handler. */
12790 cp_parser_handler (parser);
12791 /* Peek at the next token. */
12792 token = cp_lexer_peek_token (parser->lexer);
12793 /* If it's not `catch' then there are no more handlers. */
12794 if (!cp_parser_is_keyword (token, RID_CATCH))
12795 break;
12796 }
12797}
12798
12799/* Parse a handler.
12800
12801 handler:
12802 catch ( exception-declaration ) compound-statement */
12803
12804static void
94edc4ab 12805cp_parser_handler (cp_parser* parser)
a723baf1
MM
12806{
12807 tree handler;
12808 tree declaration;
12809
12810 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12811 handler = begin_handler ();
12812 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12813 declaration = cp_parser_exception_declaration (parser);
12814 finish_handler_parms (declaration, handler);
12815 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 12816 cp_parser_compound_statement (parser, false);
a723baf1
MM
12817 finish_handler (handler);
12818}
12819
12820/* Parse an exception-declaration.
12821
12822 exception-declaration:
12823 type-specifier-seq declarator
12824 type-specifier-seq abstract-declarator
12825 type-specifier-seq
12826 ...
12827
12828 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12829 ellipsis variant is used. */
12830
12831static tree
94edc4ab 12832cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12833{
12834 tree type_specifiers;
12835 tree declarator;
12836 const char *saved_message;
12837
12838 /* If it's an ellipsis, it's easy to handle. */
12839 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12840 {
12841 /* Consume the `...' token. */
12842 cp_lexer_consume_token (parser->lexer);
12843 return NULL_TREE;
12844 }
12845
12846 /* Types may not be defined in exception-declarations. */
12847 saved_message = parser->type_definition_forbidden_message;
12848 parser->type_definition_forbidden_message
12849 = "types may not be defined in exception-declarations";
12850
12851 /* Parse the type-specifier-seq. */
12852 type_specifiers = cp_parser_type_specifier_seq (parser);
12853 /* If it's a `)', then there is no declarator. */
12854 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12855 declarator = NULL_TREE;
12856 else
62b8a44e 12857 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
12858 /*ctor_dtor_or_conv_p=*/NULL,
12859 /*parenthesized_p=*/NULL);
a723baf1
MM
12860
12861 /* Restore the saved message. */
12862 parser->type_definition_forbidden_message = saved_message;
12863
12864 return start_handler_parms (type_specifiers, declarator);
12865}
12866
12867/* Parse a throw-expression.
12868
12869 throw-expression:
34cd5ae7 12870 throw assignment-expression [opt]
a723baf1
MM
12871
12872 Returns a THROW_EXPR representing the throw-expression. */
12873
12874static tree
94edc4ab 12875cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12876{
12877 tree expression;
89f1a6ec 12878 cp_token* token;
a723baf1
MM
12879
12880 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
12881 token = cp_lexer_peek_token (parser->lexer);
12882 /* Figure out whether or not there is an assignment-expression
12883 following the "throw" keyword. */
12884 if (token->type == CPP_COMMA
12885 || token->type == CPP_SEMICOLON
12886 || token->type == CPP_CLOSE_PAREN
12887 || token->type == CPP_CLOSE_SQUARE
12888 || token->type == CPP_CLOSE_BRACE
12889 || token->type == CPP_COLON)
a723baf1 12890 expression = NULL_TREE;
89f1a6ec
MM
12891 else
12892 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
12893
12894 return build_throw (expression);
12895}
12896
12897/* GNU Extensions */
12898
12899/* Parse an (optional) asm-specification.
12900
12901 asm-specification:
12902 asm ( string-literal )
12903
12904 If the asm-specification is present, returns a STRING_CST
12905 corresponding to the string-literal. Otherwise, returns
12906 NULL_TREE. */
12907
12908static tree
94edc4ab 12909cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12910{
12911 cp_token *token;
12912 tree asm_specification;
12913
12914 /* Peek at the next token. */
12915 token = cp_lexer_peek_token (parser->lexer);
12916 /* If the next token isn't the `asm' keyword, then there's no
12917 asm-specification. */
12918 if (!cp_parser_is_keyword (token, RID_ASM))
12919 return NULL_TREE;
12920
12921 /* Consume the `asm' token. */
12922 cp_lexer_consume_token (parser->lexer);
12923 /* Look for the `('. */
12924 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12925
12926 /* Look for the string-literal. */
12927 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12928 if (token)
12929 asm_specification = token->value;
12930 else
12931 asm_specification = NULL_TREE;
12932
12933 /* Look for the `)'. */
12934 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12935
12936 return asm_specification;
12937}
12938
12939/* Parse an asm-operand-list.
12940
12941 asm-operand-list:
12942 asm-operand
12943 asm-operand-list , asm-operand
12944
12945 asm-operand:
12946 string-literal ( expression )
12947 [ string-literal ] string-literal ( expression )
12948
12949 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12950 each node is the expression. The TREE_PURPOSE is itself a
12951 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12952 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12953 is a STRING_CST for the string literal before the parenthesis. */
12954
12955static tree
94edc4ab 12956cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12957{
12958 tree asm_operands = NULL_TREE;
12959
12960 while (true)
12961 {
12962 tree string_literal;
12963 tree expression;
12964 tree name;
12965 cp_token *token;
12966
12967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12968 {
12969 /* Consume the `[' token. */
12970 cp_lexer_consume_token (parser->lexer);
12971 /* Read the operand name. */
12972 name = cp_parser_identifier (parser);
12973 if (name != error_mark_node)
12974 name = build_string (IDENTIFIER_LENGTH (name),
12975 IDENTIFIER_POINTER (name));
12976 /* Look for the closing `]'. */
12977 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12978 }
12979 else
12980 name = NULL_TREE;
12981 /* Look for the string-literal. */
12982 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12983 string_literal = token ? token->value : error_mark_node;
12984 /* Look for the `('. */
12985 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12986 /* Parse the expression. */
12987 expression = cp_parser_expression (parser);
12988 /* Look for the `)'. */
12989 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12990 /* Add this operand to the list. */
12991 asm_operands = tree_cons (build_tree_list (name, string_literal),
12992 expression,
12993 asm_operands);
12994 /* If the next token is not a `,', there are no more
12995 operands. */
12996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12997 break;
12998 /* Consume the `,'. */
12999 cp_lexer_consume_token (parser->lexer);
13000 }
13001
13002 return nreverse (asm_operands);
13003}
13004
13005/* Parse an asm-clobber-list.
13006
13007 asm-clobber-list:
13008 string-literal
13009 asm-clobber-list , string-literal
13010
13011 Returns a TREE_LIST, indicating the clobbers in the order that they
13012 appeared. The TREE_VALUE of each node is a STRING_CST. */
13013
13014static tree
94edc4ab 13015cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13016{
13017 tree clobbers = NULL_TREE;
13018
13019 while (true)
13020 {
13021 cp_token *token;
13022 tree string_literal;
13023
13024 /* Look for the string literal. */
13025 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13026 string_literal = token ? token->value : error_mark_node;
13027 /* Add it to the list. */
13028 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13029 /* If the next token is not a `,', then the list is
13030 complete. */
13031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13032 break;
13033 /* Consume the `,' token. */
13034 cp_lexer_consume_token (parser->lexer);
13035 }
13036
13037 return clobbers;
13038}
13039
13040/* Parse an (optional) series of attributes.
13041
13042 attributes:
13043 attributes attribute
13044
13045 attribute:
13046 __attribute__ (( attribute-list [opt] ))
13047
13048 The return value is as for cp_parser_attribute_list. */
13049
13050static tree
94edc4ab 13051cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13052{
13053 tree attributes = NULL_TREE;
13054
13055 while (true)
13056 {
13057 cp_token *token;
13058 tree attribute_list;
13059
13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
13062 /* If it's not `__attribute__', then we're done. */
13063 if (token->keyword != RID_ATTRIBUTE)
13064 break;
13065
13066 /* Consume the `__attribute__' keyword. */
13067 cp_lexer_consume_token (parser->lexer);
13068 /* Look for the two `(' tokens. */
13069 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13070 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13071
13072 /* Peek at the next token. */
13073 token = cp_lexer_peek_token (parser->lexer);
13074 if (token->type != CPP_CLOSE_PAREN)
13075 /* Parse the attribute-list. */
13076 attribute_list = cp_parser_attribute_list (parser);
13077 else
13078 /* If the next token is a `)', then there is no attribute
13079 list. */
13080 attribute_list = NULL;
13081
13082 /* Look for the two `)' tokens. */
13083 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13084 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13085
13086 /* Add these new attributes to the list. */
13087 attributes = chainon (attributes, attribute_list);
13088 }
13089
13090 return attributes;
13091}
13092
13093/* Parse an attribute-list.
13094
13095 attribute-list:
13096 attribute
13097 attribute-list , attribute
13098
13099 attribute:
13100 identifier
13101 identifier ( identifier )
13102 identifier ( identifier , expression-list )
13103 identifier ( expression-list )
13104
13105 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13106 TREE_PURPOSE of each node is the identifier indicating which
13107 attribute is in use. The TREE_VALUE represents the arguments, if
13108 any. */
13109
13110static tree
94edc4ab 13111cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13112{
13113 tree attribute_list = NULL_TREE;
13114
13115 while (true)
13116 {
13117 cp_token *token;
13118 tree identifier;
13119 tree attribute;
13120
13121 /* Look for the identifier. We also allow keywords here; for
13122 example `__attribute__ ((const))' is legal. */
13123 token = cp_lexer_peek_token (parser->lexer);
13124 if (token->type != CPP_NAME
13125 && token->type != CPP_KEYWORD)
13126 return error_mark_node;
13127 /* Consume the token. */
13128 token = cp_lexer_consume_token (parser->lexer);
13129
13130 /* Save away the identifier that indicates which attribute this is. */
13131 identifier = token->value;
13132 attribute = build_tree_list (identifier, NULL_TREE);
13133
13134 /* Peek at the next token. */
13135 token = cp_lexer_peek_token (parser->lexer);
13136 /* If it's an `(', then parse the attribute arguments. */
13137 if (token->type == CPP_OPEN_PAREN)
13138 {
13139 tree arguments;
a723baf1 13140
39703eb9
MM
13141 arguments = (cp_parser_parenthesized_expression_list
13142 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13143 /* Save the identifier and arguments away. */
13144 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13145 }
13146
13147 /* Add this attribute to the list. */
13148 TREE_CHAIN (attribute) = attribute_list;
13149 attribute_list = attribute;
13150
13151 /* Now, look for more attributes. */
13152 token = cp_lexer_peek_token (parser->lexer);
13153 /* If the next token isn't a `,', we're done. */
13154 if (token->type != CPP_COMMA)
13155 break;
13156
cd0be382 13157 /* Consume the comma and keep going. */
a723baf1
MM
13158 cp_lexer_consume_token (parser->lexer);
13159 }
13160
13161 /* We built up the list in reverse order. */
13162 return nreverse (attribute_list);
13163}
13164
13165/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13166 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13167 current value of the PEDANTIC flag, regardless of whether or not
13168 the `__extension__' keyword is present. The caller is responsible
13169 for restoring the value of the PEDANTIC flag. */
13170
13171static bool
94edc4ab 13172cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13173{
13174 /* Save the old value of the PEDANTIC flag. */
13175 *saved_pedantic = pedantic;
13176
13177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13178 {
13179 /* Consume the `__extension__' token. */
13180 cp_lexer_consume_token (parser->lexer);
13181 /* We're not being pedantic while the `__extension__' keyword is
13182 in effect. */
13183 pedantic = 0;
13184
13185 return true;
13186 }
13187
13188 return false;
13189}
13190
13191/* Parse a label declaration.
13192
13193 label-declaration:
13194 __label__ label-declarator-seq ;
13195
13196 label-declarator-seq:
13197 identifier , label-declarator-seq
13198 identifier */
13199
13200static void
94edc4ab 13201cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13202{
13203 /* Look for the `__label__' keyword. */
13204 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13205
13206 while (true)
13207 {
13208 tree identifier;
13209
13210 /* Look for an identifier. */
13211 identifier = cp_parser_identifier (parser);
13212 /* Declare it as a lobel. */
13213 finish_label_decl (identifier);
13214 /* If the next token is a `;', stop. */
13215 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13216 break;
13217 /* Look for the `,' separating the label declarations. */
13218 cp_parser_require (parser, CPP_COMMA, "`,'");
13219 }
13220
13221 /* Look for the final `;'. */
13222 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13223}
13224
13225/* Support Functions */
13226
13227/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13228 NAME should have one of the representations used for an
13229 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13230 is returned. If PARSER->SCOPE is a dependent type, then a
13231 SCOPE_REF is returned.
13232
13233 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13234 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13235 was formed. Abstractly, such entities should not be passed to this
13236 function, because they do not need to be looked up, but it is
13237 simpler to check for this special case here, rather than at the
13238 call-sites.
13239
13240 In cases not explicitly covered above, this function returns a
13241 DECL, OVERLOAD, or baselink representing the result of the lookup.
13242 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13243 is returned.
13244
a723baf1
MM
13245 If IS_TYPE is TRUE, bindings that do not refer to types are
13246 ignored.
13247
eea9800f
MM
13248 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13249 are ignored.
13250
a723baf1
MM
13251 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13252 types. */
13253
13254static tree
8d241e0b 13255cp_parser_lookup_name (cp_parser *parser, tree name,
eea9800f 13256 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
13257{
13258 tree decl;
13259 tree object_type = parser->context->object_type;
13260
13261 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13262 no longer valid. Note that if we are parsing tentatively, and
13263 the parse fails, OBJECT_TYPE will be automatically restored. */
13264 parser->context->object_type = NULL_TREE;
13265
13266 if (name == error_mark_node)
13267 return error_mark_node;
13268
13269 /* A template-id has already been resolved; there is no lookup to
13270 do. */
13271 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13272 return name;
13273 if (BASELINK_P (name))
13274 {
13275 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13276 == TEMPLATE_ID_EXPR),
13277 20020909);
13278 return name;
13279 }
13280
13281 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13282 it should already have been checked to make sure that the name
13283 used matches the type being destroyed. */
13284 if (TREE_CODE (name) == BIT_NOT_EXPR)
13285 {
13286 tree type;
13287
13288 /* Figure out to which type this destructor applies. */
13289 if (parser->scope)
13290 type = parser->scope;
13291 else if (object_type)
13292 type = object_type;
13293 else
13294 type = current_class_type;
13295 /* If that's not a class type, there is no destructor. */
13296 if (!type || !CLASS_TYPE_P (type))
13297 return error_mark_node;
13298 /* If it was a class type, return the destructor. */
13299 return CLASSTYPE_DESTRUCTORS (type);
13300 }
13301
13302 /* By this point, the NAME should be an ordinary identifier. If
13303 the id-expression was a qualified name, the qualifying scope is
13304 stored in PARSER->SCOPE at this point. */
13305 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13306 20000619);
13307
13308 /* Perform the lookup. */
13309 if (parser->scope)
13310 {
1fb3244a 13311 bool dependent_p;
a723baf1
MM
13312
13313 if (parser->scope == error_mark_node)
13314 return error_mark_node;
13315
13316 /* If the SCOPE is dependent, the lookup must be deferred until
13317 the template is instantiated -- unless we are explicitly
13318 looking up names in uninstantiated templates. Even then, we
13319 cannot look up the name if the scope is not a class type; it
13320 might, for example, be a template type parameter. */
1fb3244a
MM
13321 dependent_p = (TYPE_P (parser->scope)
13322 && !(parser->in_declarator_p
13323 && currently_open_class (parser->scope))
13324 && dependent_type_p (parser->scope));
a723baf1 13325 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13326 && dependent_p)
a723baf1
MM
13327 {
13328 if (!is_type)
13329 decl = build_nt (SCOPE_REF, parser->scope, name);
13330 else
13331 /* The resolution to Core Issue 180 says that `struct A::B'
13332 should be considered a type-name, even if `A' is
13333 dependent. */
13334 decl = TYPE_NAME (make_typename_type (parser->scope,
13335 name,
13336 /*complain=*/1));
13337 }
13338 else
13339 {
13340 /* If PARSER->SCOPE is a dependent type, then it must be a
13341 class type, and we must not be checking dependencies;
13342 otherwise, we would have processed this lookup above. So
13343 that PARSER->SCOPE is not considered a dependent base by
13344 lookup_member, we must enter the scope here. */
1fb3244a 13345 if (dependent_p)
a723baf1
MM
13346 push_scope (parser->scope);
13347 /* If the PARSER->SCOPE is a a template specialization, it
13348 may be instantiated during name lookup. In that case,
13349 errors may be issued. Even if we rollback the current
13350 tentative parse, those errors are valid. */
5e08432e
MM
13351 decl = lookup_qualified_name (parser->scope, name, is_type,
13352 /*complain=*/true);
1fb3244a 13353 if (dependent_p)
a723baf1
MM
13354 pop_scope (parser->scope);
13355 }
13356 parser->qualifying_scope = parser->scope;
13357 parser->object_scope = NULL_TREE;
13358 }
13359 else if (object_type)
13360 {
13361 tree object_decl = NULL_TREE;
13362 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13363 OBJECT_TYPE is not a class. */
13364 if (CLASS_TYPE_P (object_type))
13365 /* If the OBJECT_TYPE is a template specialization, it may
13366 be instantiated during name lookup. In that case, errors
13367 may be issued. Even if we rollback the current tentative
13368 parse, those errors are valid. */
13369 object_decl = lookup_member (object_type,
13370 name,
13371 /*protect=*/0, is_type);
13372 /* Look it up in the enclosing context, too. */
13373 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13374 is_namespace,
a723baf1
MM
13375 /*flags=*/0);
13376 parser->object_scope = object_type;
13377 parser->qualifying_scope = NULL_TREE;
13378 if (object_decl)
13379 decl = object_decl;
13380 }
13381 else
13382 {
13383 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13384 is_namespace,
a723baf1
MM
13385 /*flags=*/0);
13386 parser->qualifying_scope = NULL_TREE;
13387 parser->object_scope = NULL_TREE;
13388 }
13389
13390 /* If the lookup failed, let our caller know. */
13391 if (!decl
13392 || decl == error_mark_node
13393 || (TREE_CODE (decl) == FUNCTION_DECL
13394 && DECL_ANTICIPATED (decl)))
13395 return error_mark_node;
13396
13397 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13398 if (TREE_CODE (decl) == TREE_LIST)
13399 {
13400 /* The error message we have to print is too complicated for
13401 cp_parser_error, so we incorporate its actions directly. */
e5976695 13402 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13403 {
13404 error ("reference to `%D' is ambiguous", name);
13405 print_candidates (decl);
13406 }
13407 return error_mark_node;
13408 }
13409
13410 my_friendly_assert (DECL_P (decl)
13411 || TREE_CODE (decl) == OVERLOAD
13412 || TREE_CODE (decl) == SCOPE_REF
13413 || BASELINK_P (decl),
13414 20000619);
13415
13416 /* If we have resolved the name of a member declaration, check to
13417 see if the declaration is accessible. When the name resolves to
34cd5ae7 13418 set of overloaded functions, accessibility is checked when
a723baf1
MM
13419 overload resolution is done.
13420
13421 During an explicit instantiation, access is not checked at all,
13422 as per [temp.explicit]. */
8d241e0b 13423 if (DECL_P (decl))
ee76b931 13424 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13425
13426 return decl;
13427}
13428
13429/* Like cp_parser_lookup_name, but for use in the typical case where
13430 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13431 TRUE. */
13432
13433static tree
94edc4ab 13434cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
13435{
13436 return cp_parser_lookup_name (parser, name,
eea9800f
MM
13437 /*is_type=*/false,
13438 /*is_namespace=*/false,
a723baf1
MM
13439 /*check_dependency=*/true);
13440}
13441
a723baf1
MM
13442/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13443 the current context, return the TYPE_DECL. If TAG_NAME_P is
13444 true, the DECL indicates the class being defined in a class-head,
13445 or declared in an elaborated-type-specifier.
13446
13447 Otherwise, return DECL. */
13448
13449static tree
13450cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13451{
710b73e6
KL
13452 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13453 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
13454
13455 struct A {
13456 template <typename T> struct B;
13457 };
13458
13459 template <typename T> struct A::B {};
13460
13461 Similarly, in a elaborated-type-specifier:
13462
13463 namespace N { struct X{}; }
13464
13465 struct A {
13466 template <typename T> friend struct N::X;
13467 };
13468
710b73e6
KL
13469 However, if the DECL refers to a class type, and we are in
13470 the scope of the class, then the name lookup automatically
13471 finds the TYPE_DECL created by build_self_reference rather
13472 than a TEMPLATE_DECL. For example, in:
13473
13474 template <class T> struct S {
13475 S s;
13476 };
13477
13478 there is no need to handle such case. */
13479
13480 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13481 return DECL_TEMPLATE_RESULT (decl);
13482
13483 return decl;
13484}
13485
13486/* If too many, or too few, template-parameter lists apply to the
13487 declarator, issue an error message. Returns TRUE if all went well,
13488 and FALSE otherwise. */
13489
13490static bool
94edc4ab
NN
13491cp_parser_check_declarator_template_parameters (cp_parser* parser,
13492 tree declarator)
a723baf1
MM
13493{
13494 unsigned num_templates;
13495
13496 /* We haven't seen any classes that involve template parameters yet. */
13497 num_templates = 0;
13498
13499 switch (TREE_CODE (declarator))
13500 {
13501 case CALL_EXPR:
13502 case ARRAY_REF:
13503 case INDIRECT_REF:
13504 case ADDR_EXPR:
13505 {
13506 tree main_declarator = TREE_OPERAND (declarator, 0);
13507 return
13508 cp_parser_check_declarator_template_parameters (parser,
13509 main_declarator);
13510 }
13511
13512 case SCOPE_REF:
13513 {
13514 tree scope;
13515 tree member;
13516
13517 scope = TREE_OPERAND (declarator, 0);
13518 member = TREE_OPERAND (declarator, 1);
13519
13520 /* If this is a pointer-to-member, then we are not interested
13521 in the SCOPE, because it does not qualify the thing that is
13522 being declared. */
13523 if (TREE_CODE (member) == INDIRECT_REF)
13524 return (cp_parser_check_declarator_template_parameters
13525 (parser, member));
13526
13527 while (scope && CLASS_TYPE_P (scope))
13528 {
13529 /* You're supposed to have one `template <...>'
13530 for every template class, but you don't need one
13531 for a full specialization. For example:
13532
13533 template <class T> struct S{};
13534 template <> struct S<int> { void f(); };
13535 void S<int>::f () {}
13536
13537 is correct; there shouldn't be a `template <>' for
13538 the definition of `S<int>::f'. */
13539 if (CLASSTYPE_TEMPLATE_INFO (scope)
13540 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13541 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13543 ++num_templates;
13544
13545 scope = TYPE_CONTEXT (scope);
13546 }
13547 }
13548
13549 /* Fall through. */
13550
13551 default:
13552 /* If the DECLARATOR has the form `X<y>' then it uses one
13553 additional level of template parameters. */
13554 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13555 ++num_templates;
13556
13557 return cp_parser_check_template_parameters (parser,
13558 num_templates);
13559 }
13560}
13561
13562/* NUM_TEMPLATES were used in the current declaration. If that is
13563 invalid, return FALSE and issue an error messages. Otherwise,
13564 return TRUE. */
13565
13566static bool
94edc4ab
NN
13567cp_parser_check_template_parameters (cp_parser* parser,
13568 unsigned num_templates)
a723baf1
MM
13569{
13570 /* If there are more template classes than parameter lists, we have
13571 something like:
13572
13573 template <class T> void S<T>::R<T>::f (); */
13574 if (parser->num_template_parameter_lists < num_templates)
13575 {
13576 error ("too few template-parameter-lists");
13577 return false;
13578 }
13579 /* If there are the same number of template classes and parameter
13580 lists, that's OK. */
13581 if (parser->num_template_parameter_lists == num_templates)
13582 return true;
13583 /* If there are more, but only one more, then we are referring to a
13584 member template. That's OK too. */
13585 if (parser->num_template_parameter_lists == num_templates + 1)
13586 return true;
13587 /* Otherwise, there are too many template parameter lists. We have
13588 something like:
13589
13590 template <class T> template <class U> void S::f(); */
13591 error ("too many template-parameter-lists");
13592 return false;
13593}
13594
13595/* Parse a binary-expression of the general form:
13596
13597 binary-expression:
13598 <expr>
13599 binary-expression <token> <expr>
13600
13601 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13602 to parser the <expr>s. If the first production is used, then the
13603 value returned by FN is returned directly. Otherwise, a node with
13604 the indicated EXPR_TYPE is returned, with operands corresponding to
13605 the two sub-expressions. */
13606
13607static tree
94edc4ab
NN
13608cp_parser_binary_expression (cp_parser* parser,
13609 const cp_parser_token_tree_map token_tree_map,
13610 cp_parser_expression_fn fn)
a723baf1
MM
13611{
13612 tree lhs;
13613
13614 /* Parse the first expression. */
13615 lhs = (*fn) (parser);
13616 /* Now, look for more expressions. */
13617 while (true)
13618 {
13619 cp_token *token;
39b1af70 13620 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13621 tree rhs;
13622
13623 /* Peek at the next token. */
13624 token = cp_lexer_peek_token (parser->lexer);
13625 /* If the token is `>', and that's not an operator at the
13626 moment, then we're done. */
13627 if (token->type == CPP_GREATER
13628 && !parser->greater_than_is_operator_p)
13629 break;
34cd5ae7 13630 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13631 tree representation. */
13632 for (map_node = token_tree_map;
13633 map_node->token_type != CPP_EOF;
13634 ++map_node)
13635 if (map_node->token_type == token->type)
13636 {
13637 /* Consume the operator token. */
13638 cp_lexer_consume_token (parser->lexer);
13639 /* Parse the right-hand side of the expression. */
13640 rhs = (*fn) (parser);
13641 /* Build the binary tree node. */
13642 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13643 break;
13644 }
13645
13646 /* If the token wasn't one of the ones we want, we're done. */
13647 if (map_node->token_type == CPP_EOF)
13648 break;
13649 }
13650
13651 return lhs;
13652}
13653
13654/* Parse an optional `::' token indicating that the following name is
13655 from the global namespace. If so, PARSER->SCOPE is set to the
13656 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13657 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13658 Returns the new value of PARSER->SCOPE, if the `::' token is
13659 present, and NULL_TREE otherwise. */
13660
13661static tree
94edc4ab 13662cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13663{
13664 cp_token *token;
13665
13666 /* Peek at the next token. */
13667 token = cp_lexer_peek_token (parser->lexer);
13668 /* If we're looking at a `::' token then we're starting from the
13669 global namespace, not our current location. */
13670 if (token->type == CPP_SCOPE)
13671 {
13672 /* Consume the `::' token. */
13673 cp_lexer_consume_token (parser->lexer);
13674 /* Set the SCOPE so that we know where to start the lookup. */
13675 parser->scope = global_namespace;
13676 parser->qualifying_scope = global_namespace;
13677 parser->object_scope = NULL_TREE;
13678
13679 return parser->scope;
13680 }
13681 else if (!current_scope_valid_p)
13682 {
13683 parser->scope = NULL_TREE;
13684 parser->qualifying_scope = NULL_TREE;
13685 parser->object_scope = NULL_TREE;
13686 }
13687
13688 return NULL_TREE;
13689}
13690
13691/* Returns TRUE if the upcoming token sequence is the start of a
13692 constructor declarator. If FRIEND_P is true, the declarator is
13693 preceded by the `friend' specifier. */
13694
13695static bool
13696cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13697{
13698 bool constructor_p;
13699 tree type_decl = NULL_TREE;
13700 bool nested_name_p;
2050a1bb
MM
13701 cp_token *next_token;
13702
13703 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13704 try to avoid doing lots of work if at all possible. It's not
13705 valid declare a constructor at function scope. */
13706 if (at_function_scope_p ())
13707 return false;
13708 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13709 next_token = cp_lexer_peek_token (parser->lexer);
13710 if (next_token->type != CPP_NAME
13711 && next_token->type != CPP_SCOPE
13712 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13713 && next_token->type != CPP_TEMPLATE_ID)
13714 return false;
a723baf1
MM
13715
13716 /* Parse tentatively; we are going to roll back all of the tokens
13717 consumed here. */
13718 cp_parser_parse_tentatively (parser);
13719 /* Assume that we are looking at a constructor declarator. */
13720 constructor_p = true;
8d241e0b 13721
a723baf1
MM
13722 /* Look for the optional `::' operator. */
13723 cp_parser_global_scope_opt (parser,
13724 /*current_scope_valid_p=*/false);
13725 /* Look for the nested-name-specifier. */
13726 nested_name_p
13727 = (cp_parser_nested_name_specifier_opt (parser,
13728 /*typename_keyword_p=*/false,
13729 /*check_dependency_p=*/false,
a668c6ad
MM
13730 /*type_p=*/false,
13731 /*is_declaration=*/false)
a723baf1
MM
13732 != NULL_TREE);
13733 /* Outside of a class-specifier, there must be a
13734 nested-name-specifier. */
13735 if (!nested_name_p &&
13736 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13737 || friend_p))
13738 constructor_p = false;
13739 /* If we still think that this might be a constructor-declarator,
13740 look for a class-name. */
13741 if (constructor_p)
13742 {
13743 /* If we have:
13744
8fbc5ae7 13745 template <typename T> struct S { S(); };
a723baf1
MM
13746 template <typename T> S<T>::S ();
13747
13748 we must recognize that the nested `S' names a class.
13749 Similarly, for:
13750
13751 template <typename T> S<T>::S<T> ();
13752
13753 we must recognize that the nested `S' names a template. */
13754 type_decl = cp_parser_class_name (parser,
13755 /*typename_keyword_p=*/false,
13756 /*template_keyword_p=*/false,
13757 /*type_p=*/false,
a723baf1 13758 /*check_dependency_p=*/false,
a668c6ad
MM
13759 /*class_head_p=*/false,
13760 /*is_declaration=*/false);
a723baf1
MM
13761 /* If there was no class-name, then this is not a constructor. */
13762 constructor_p = !cp_parser_error_occurred (parser);
13763 }
8d241e0b 13764
a723baf1
MM
13765 /* If we're still considering a constructor, we have to see a `(',
13766 to begin the parameter-declaration-clause, followed by either a
13767 `)', an `...', or a decl-specifier. We need to check for a
13768 type-specifier to avoid being fooled into thinking that:
13769
13770 S::S (f) (int);
13771
13772 is a constructor. (It is actually a function named `f' that
13773 takes one parameter (of type `int') and returns a value of type
13774 `S::S'. */
13775 if (constructor_p
13776 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13777 {
13778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13779 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13780 && !cp_parser_storage_class_specifier_opt (parser))
13781 {
5dae1114 13782 tree type;
4047b164 13783 unsigned saved_num_template_parameter_lists;
5dae1114
MM
13784
13785 /* Names appearing in the type-specifier should be looked up
13786 in the scope of the class. */
13787 if (current_class_type)
13788 type = NULL_TREE;
a723baf1
MM
13789 else
13790 {
5dae1114
MM
13791 type = TREE_TYPE (type_decl);
13792 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13793 {
13794 type = resolve_typename_type (type,
13795 /*only_current_p=*/false);
13796 if (type == error_mark_node)
13797 {
13798 cp_parser_abort_tentative_parse (parser);
13799 return false;
13800 }
13801 }
5dae1114 13802 push_scope (type);
a723baf1 13803 }
4047b164
KL
13804
13805 /* Inside the constructor parameter list, surrounding
13806 template-parameter-lists do not apply. */
13807 saved_num_template_parameter_lists
13808 = parser->num_template_parameter_lists;
13809 parser->num_template_parameter_lists = 0;
13810
5dae1114
MM
13811 /* Look for the type-specifier. */
13812 cp_parser_type_specifier (parser,
13813 CP_PARSER_FLAGS_NONE,
13814 /*is_friend=*/false,
13815 /*is_declarator=*/true,
13816 /*declares_class_or_enum=*/NULL,
13817 /*is_cv_qualifier=*/NULL);
4047b164
KL
13818
13819 parser->num_template_parameter_lists
13820 = saved_num_template_parameter_lists;
13821
5dae1114
MM
13822 /* Leave the scope of the class. */
13823 if (type)
13824 pop_scope (type);
13825
13826 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13827 }
13828 }
13829 else
13830 constructor_p = false;
13831 /* We did not really want to consume any tokens. */
13832 cp_parser_abort_tentative_parse (parser);
13833
13834 return constructor_p;
13835}
13836
13837/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13838 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13839 they must be performed once we are in the scope of the function.
13840
13841 Returns the function defined. */
13842
13843static tree
13844cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13845 (cp_parser* parser,
13846 tree decl_specifiers,
13847 tree attributes,
13848 tree declarator)
a723baf1
MM
13849{
13850 tree fn;
13851 bool success_p;
13852
13853 /* Begin the function-definition. */
13854 success_p = begin_function_definition (decl_specifiers,
13855 attributes,
13856 declarator);
13857
13858 /* If there were names looked up in the decl-specifier-seq that we
13859 did not check, check them now. We must wait until we are in the
13860 scope of the function to perform the checks, since the function
13861 might be a friend. */
cf22909c 13862 perform_deferred_access_checks ();
a723baf1
MM
13863
13864 if (!success_p)
13865 {
13866 /* If begin_function_definition didn't like the definition, skip
13867 the entire function. */
13868 error ("invalid function declaration");
13869 cp_parser_skip_to_end_of_block_or_statement (parser);
13870 fn = error_mark_node;
13871 }
13872 else
13873 fn = cp_parser_function_definition_after_declarator (parser,
13874 /*inline_p=*/false);
13875
13876 return fn;
13877}
13878
13879/* Parse the part of a function-definition that follows the
13880 declarator. INLINE_P is TRUE iff this function is an inline
13881 function defined with a class-specifier.
13882
13883 Returns the function defined. */
13884
13885static tree
94edc4ab
NN
13886cp_parser_function_definition_after_declarator (cp_parser* parser,
13887 bool inline_p)
a723baf1
MM
13888{
13889 tree fn;
13890 bool ctor_initializer_p = false;
13891 bool saved_in_unbraced_linkage_specification_p;
13892 unsigned saved_num_template_parameter_lists;
13893
13894 /* If the next token is `return', then the code may be trying to
13895 make use of the "named return value" extension that G++ used to
13896 support. */
13897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13898 {
13899 /* Consume the `return' keyword. */
13900 cp_lexer_consume_token (parser->lexer);
13901 /* Look for the identifier that indicates what value is to be
13902 returned. */
13903 cp_parser_identifier (parser);
13904 /* Issue an error message. */
13905 error ("named return values are no longer supported");
13906 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
13907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13908 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
13909 cp_lexer_consume_token (parser->lexer);
13910 }
13911 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13912 anything declared inside `f'. */
13913 saved_in_unbraced_linkage_specification_p
13914 = parser->in_unbraced_linkage_specification_p;
13915 parser->in_unbraced_linkage_specification_p = false;
13916 /* Inside the function, surrounding template-parameter-lists do not
13917 apply. */
13918 saved_num_template_parameter_lists
13919 = parser->num_template_parameter_lists;
13920 parser->num_template_parameter_lists = 0;
13921 /* If the next token is `try', then we are looking at a
13922 function-try-block. */
13923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13924 ctor_initializer_p = cp_parser_function_try_block (parser);
13925 /* A function-try-block includes the function-body, so we only do
13926 this next part if we're not processing a function-try-block. */
13927 else
13928 ctor_initializer_p
13929 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13930
13931 /* Finish the function. */
13932 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13933 (inline_p ? 2 : 0));
13934 /* Generate code for it, if necessary. */
8cd2462c 13935 expand_or_defer_fn (fn);
a723baf1
MM
13936 /* Restore the saved values. */
13937 parser->in_unbraced_linkage_specification_p
13938 = saved_in_unbraced_linkage_specification_p;
13939 parser->num_template_parameter_lists
13940 = saved_num_template_parameter_lists;
13941
13942 return fn;
13943}
13944
13945/* Parse a template-declaration, assuming that the `export' (and
13946 `extern') keywords, if present, has already been scanned. MEMBER_P
13947 is as for cp_parser_template_declaration. */
13948
13949static void
94edc4ab 13950cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13951{
13952 tree decl = NULL_TREE;
13953 tree parameter_list;
13954 bool friend_p = false;
13955
13956 /* Look for the `template' keyword. */
13957 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13958 return;
13959
13960 /* And the `<'. */
13961 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13962 return;
13963
13964 /* Parse the template parameters. */
13965 begin_template_parm_list ();
13966 /* If the next token is `>', then we have an invalid
13967 specialization. Rather than complain about an invalid template
13968 parameter, issue an error message here. */
13969 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13970 {
13971 cp_parser_error (parser, "invalid explicit specialization");
13972 parameter_list = NULL_TREE;
13973 }
13974 else
13975 parameter_list = cp_parser_template_parameter_list (parser);
13976 parameter_list = end_template_parm_list (parameter_list);
13977 /* Look for the `>'. */
13978 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13979 /* We just processed one more parameter list. */
13980 ++parser->num_template_parameter_lists;
13981 /* If the next token is `template', there are more template
13982 parameters. */
13983 if (cp_lexer_next_token_is_keyword (parser->lexer,
13984 RID_TEMPLATE))
13985 cp_parser_template_declaration_after_export (parser, member_p);
13986 else
13987 {
13988 decl = cp_parser_single_declaration (parser,
13989 member_p,
13990 &friend_p);
13991
13992 /* If this is a member template declaration, let the front
13993 end know. */
13994 if (member_p && !friend_p && decl)
37d407a1
KL
13995 {
13996 if (TREE_CODE (decl) == TYPE_DECL)
13997 cp_parser_check_access_in_redeclaration (decl);
13998
13999 decl = finish_member_template_decl (decl);
14000 }
a723baf1 14001 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14002 make_friend_class (current_class_type, TREE_TYPE (decl),
14003 /*complain=*/true);
a723baf1
MM
14004 }
14005 /* We are done with the current parameter list. */
14006 --parser->num_template_parameter_lists;
14007
14008 /* Finish up. */
14009 finish_template_decl (parameter_list);
14010
14011 /* Register member declarations. */
14012 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14013 finish_member_declaration (decl);
14014
14015 /* If DECL is a function template, we must return to parse it later.
14016 (Even though there is no definition, there might be default
14017 arguments that need handling.) */
14018 if (member_p && decl
14019 && (TREE_CODE (decl) == FUNCTION_DECL
14020 || DECL_FUNCTION_TEMPLATE_P (decl)))
14021 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 14022 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14023 TREE_VALUE (parser->unparsed_functions_queues));
14024}
14025
14026/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14027 `function-definition' sequence. MEMBER_P is true, this declaration
14028 appears in a class scope.
14029
14030 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14031 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14032
14033static tree
94edc4ab
NN
14034cp_parser_single_declaration (cp_parser* parser,
14035 bool member_p,
14036 bool* friend_p)
a723baf1 14037{
560ad596 14038 int declares_class_or_enum;
a723baf1
MM
14039 tree decl = NULL_TREE;
14040 tree decl_specifiers;
14041 tree attributes;
4bb8ca28 14042 bool function_definition_p = false;
a723baf1 14043
a723baf1 14044 /* Defer access checks until we know what is being declared. */
8d241e0b 14045 push_deferring_access_checks (dk_deferred);
cf22909c 14046
a723baf1
MM
14047 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14048 alternative. */
14049 decl_specifiers
14050 = cp_parser_decl_specifier_seq (parser,
14051 CP_PARSER_FLAGS_OPTIONAL,
14052 &attributes,
14053 &declares_class_or_enum);
4bb8ca28
MM
14054 if (friend_p)
14055 *friend_p = cp_parser_friend_p (decl_specifiers);
a723baf1
MM
14056 /* Gather up the access checks that occurred the
14057 decl-specifier-seq. */
cf22909c
KL
14058 stop_deferring_access_checks ();
14059
a723baf1
MM
14060 /* Check for the declaration of a template class. */
14061 if (declares_class_or_enum)
14062 {
14063 if (cp_parser_declares_only_class_p (parser))
14064 {
14065 decl = shadow_tag (decl_specifiers);
14066 if (decl)
14067 decl = TYPE_NAME (decl);
14068 else
14069 decl = error_mark_node;
14070 }
14071 }
14072 else
14073 decl = NULL_TREE;
14074 /* If it's not a template class, try for a template function. If
14075 the next token is a `;', then this declaration does not declare
14076 anything. But, if there were errors in the decl-specifiers, then
14077 the error might well have come from an attempted class-specifier.
14078 In that case, there's no need to warn about a missing declarator. */
14079 if (!decl
14080 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14081 || !value_member (error_mark_node, decl_specifiers)))
14082 decl = cp_parser_init_declarator (parser,
14083 decl_specifiers,
14084 attributes,
4bb8ca28 14085 /*function_definition_allowed_p=*/true,
a723baf1 14086 member_p,
560ad596 14087 declares_class_or_enum,
4bb8ca28 14088 &function_definition_p);
cf22909c
KL
14089
14090 pop_deferring_access_checks ();
14091
a723baf1
MM
14092 /* Clear any current qualification; whatever comes next is the start
14093 of something new. */
14094 parser->scope = NULL_TREE;
14095 parser->qualifying_scope = NULL_TREE;
14096 parser->object_scope = NULL_TREE;
14097 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14098 if (!function_definition_p
14099 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14100 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14101
14102 return decl;
14103}
14104
d6b4ea85
MM
14105/* Parse a cast-expression that is not the operand of a unary "&". */
14106
14107static tree
14108cp_parser_simple_cast_expression (cp_parser *parser)
14109{
14110 return cp_parser_cast_expression (parser, /*address_p=*/false);
14111}
14112
a723baf1
MM
14113/* Parse a functional cast to TYPE. Returns an expression
14114 representing the cast. */
14115
14116static tree
94edc4ab 14117cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14118{
14119 tree expression_list;
14120
39703eb9
MM
14121 expression_list
14122 = cp_parser_parenthesized_expression_list (parser, false,
14123 /*non_constant_p=*/NULL);
a723baf1
MM
14124
14125 return build_functional_cast (type, expression_list);
14126}
14127
4bb8ca28
MM
14128/* Save the tokens that make up the body of a member function defined
14129 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14130 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14131 specifiers applied to the declaration. Returns the FUNCTION_DECL
14132 for the member function. */
14133
14134tree
14135cp_parser_save_member_function_body (cp_parser* parser,
14136 tree decl_specifiers,
14137 tree declarator,
14138 tree attributes)
14139{
14140 cp_token_cache *cache;
14141 tree fn;
14142
14143 /* Create the function-declaration. */
14144 fn = start_method (decl_specifiers, declarator, attributes);
14145 /* If something went badly wrong, bail out now. */
14146 if (fn == error_mark_node)
14147 {
14148 /* If there's a function-body, skip it. */
14149 if (cp_parser_token_starts_function_definition_p
14150 (cp_lexer_peek_token (parser->lexer)))
14151 cp_parser_skip_to_end_of_block_or_statement (parser);
14152 return error_mark_node;
14153 }
14154
14155 /* Remember it, if there default args to post process. */
14156 cp_parser_save_default_args (parser, fn);
14157
14158 /* Create a token cache. */
14159 cache = cp_token_cache_new ();
14160 /* Save away the tokens that make up the body of the
14161 function. */
14162 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14163 /* Handle function try blocks. */
14164 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14165 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14166
14167 /* Save away the inline definition; we will process it when the
14168 class is complete. */
14169 DECL_PENDING_INLINE_INFO (fn) = cache;
14170 DECL_PENDING_INLINE_P (fn) = 1;
14171
14172 /* We need to know that this was defined in the class, so that
14173 friend templates are handled correctly. */
14174 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14175
14176 /* We're done with the inline definition. */
14177 finish_method (fn);
14178
14179 /* Add FN to the queue of functions to be parsed later. */
14180 TREE_VALUE (parser->unparsed_functions_queues)
14181 = tree_cons (NULL_TREE, fn,
14182 TREE_VALUE (parser->unparsed_functions_queues));
14183
14184 return fn;
14185}
14186
ec75414f
MM
14187/* Parse a template-argument-list, as well as the trailing ">" (but
14188 not the opening ">"). See cp_parser_template_argument_list for the
14189 return value. */
14190
14191static tree
14192cp_parser_enclosed_template_argument_list (cp_parser* parser)
14193{
14194 tree arguments;
14195 tree saved_scope;
14196 tree saved_qualifying_scope;
14197 tree saved_object_scope;
14198 bool saved_greater_than_is_operator_p;
14199
14200 /* [temp.names]
14201
14202 When parsing a template-id, the first non-nested `>' is taken as
14203 the end of the template-argument-list rather than a greater-than
14204 operator. */
14205 saved_greater_than_is_operator_p
14206 = parser->greater_than_is_operator_p;
14207 parser->greater_than_is_operator_p = false;
14208 /* Parsing the argument list may modify SCOPE, so we save it
14209 here. */
14210 saved_scope = parser->scope;
14211 saved_qualifying_scope = parser->qualifying_scope;
14212 saved_object_scope = parser->object_scope;
14213 /* Parse the template-argument-list itself. */
14214 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14215 arguments = NULL_TREE;
14216 else
14217 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
14218 /* Look for the `>' that ends the template-argument-list. If we find
14219 a '>>' instead, it's probably just a typo. */
14220 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14221 {
14222 if (!saved_greater_than_is_operator_p)
14223 {
14224 /* If we're in a nested template argument list, the '>>' has to be
14225 a typo for '> >'. We emit the error message, but we continue
14226 parsing and we push a '>' as next token, so that the argument
14227 list will be parsed correctly.. */
14228 cp_token* token;
14229 error ("`>>' should be `> >' within a nested template argument list");
14230 token = cp_lexer_peek_token (parser->lexer);
14231 token->type = CPP_GREATER;
14232 }
14233 else
14234 {
14235 /* If this is not a nested template argument list, the '>>' is
14236 a typo for '>'. Emit an error message and continue. */
14237 error ("spurious `>>', use `>' to terminate a template argument list");
14238 cp_lexer_consume_token (parser->lexer);
14239 }
14240 }
14241 else
14242 cp_parser_require (parser, CPP_GREATER, "`>'");
ec75414f
MM
14243 /* The `>' token might be a greater-than operator again now. */
14244 parser->greater_than_is_operator_p
14245 = saved_greater_than_is_operator_p;
14246 /* Restore the SAVED_SCOPE. */
14247 parser->scope = saved_scope;
14248 parser->qualifying_scope = saved_qualifying_scope;
14249 parser->object_scope = saved_object_scope;
14250
14251 return arguments;
14252}
14253
a723baf1
MM
14254/* MEMBER_FUNCTION is a member function, or a friend. If default
14255 arguments, or the body of the function have not yet been parsed,
14256 parse them now. */
14257
14258static void
94edc4ab 14259cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14260{
14261 cp_lexer *saved_lexer;
14262
14263 /* If this member is a template, get the underlying
14264 FUNCTION_DECL. */
14265 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14266 member_function = DECL_TEMPLATE_RESULT (member_function);
14267
14268 /* There should not be any class definitions in progress at this
14269 point; the bodies of members are only parsed outside of all class
14270 definitions. */
14271 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14272 /* While we're parsing the member functions we might encounter more
14273 classes. We want to handle them right away, but we don't want
14274 them getting mixed up with functions that are currently in the
14275 queue. */
14276 parser->unparsed_functions_queues
14277 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14278
14279 /* Make sure that any template parameters are in scope. */
14280 maybe_begin_member_template_processing (member_function);
14281
a723baf1
MM
14282 /* If the body of the function has not yet been parsed, parse it
14283 now. */
14284 if (DECL_PENDING_INLINE_P (member_function))
14285 {
14286 tree function_scope;
14287 cp_token_cache *tokens;
14288
14289 /* The function is no longer pending; we are processing it. */
14290 tokens = DECL_PENDING_INLINE_INFO (member_function);
14291 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14292 DECL_PENDING_INLINE_P (member_function) = 0;
14293 /* If this was an inline function in a local class, enter the scope
14294 of the containing function. */
14295 function_scope = decl_function_context (member_function);
14296 if (function_scope)
14297 push_function_context_to (function_scope);
14298
14299 /* Save away the current lexer. */
14300 saved_lexer = parser->lexer;
14301 /* Make a new lexer to feed us the tokens saved for this function. */
14302 parser->lexer = cp_lexer_new_from_tokens (tokens);
14303 parser->lexer->next = saved_lexer;
14304
14305 /* Set the current source position to be the location of the first
14306 token in the saved inline body. */
3466b292 14307 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14308
14309 /* Let the front end know that we going to be defining this
14310 function. */
14311 start_function (NULL_TREE, member_function, NULL_TREE,
14312 SF_PRE_PARSED | SF_INCLASS_INLINE);
14313
14314 /* Now, parse the body of the function. */
14315 cp_parser_function_definition_after_declarator (parser,
14316 /*inline_p=*/true);
14317
14318 /* Leave the scope of the containing function. */
14319 if (function_scope)
14320 pop_function_context_from (function_scope);
14321 /* Restore the lexer. */
14322 parser->lexer = saved_lexer;
14323 }
14324
14325 /* Remove any template parameters from the symbol table. */
14326 maybe_end_member_template_processing ();
14327
14328 /* Restore the queue. */
14329 parser->unparsed_functions_queues
14330 = TREE_CHAIN (parser->unparsed_functions_queues);
14331}
14332
cd0be382 14333/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
14334 functions queue. */
14335
14336static void
14337cp_parser_save_default_args (cp_parser* parser, tree decl)
14338{
14339 tree probe;
14340
14341 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14342 probe;
14343 probe = TREE_CHAIN (probe))
14344 if (TREE_PURPOSE (probe))
14345 {
14346 TREE_PURPOSE (parser->unparsed_functions_queues)
14347 = tree_cons (NULL_TREE, decl,
14348 TREE_PURPOSE (parser->unparsed_functions_queues));
14349 break;
14350 }
14351 return;
14352}
14353
8218bd34
MM
14354/* FN is a FUNCTION_DECL which may contains a parameter with an
14355 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14356
14357static void
8218bd34 14358cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14359{
14360 cp_lexer *saved_lexer;
14361 cp_token_cache *tokens;
14362 bool saved_local_variables_forbidden_p;
14363 tree parameters;
8218bd34 14364
b92bc2a0
NS
14365 /* While we're parsing the default args, we might (due to the
14366 statement expression extension) encounter more classes. We want
14367 to handle them right away, but we don't want them getting mixed
14368 up with default args that are currently in the queue. */
14369 parser->unparsed_functions_queues
14370 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14371
8218bd34 14372 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14373 parameters;
14374 parameters = TREE_CHAIN (parameters))
14375 {
14376 if (!TREE_PURPOSE (parameters)
14377 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14378 continue;
14379
14380 /* Save away the current lexer. */
14381 saved_lexer = parser->lexer;
14382 /* Create a new one, using the tokens we have saved. */
14383 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14384 parser->lexer = cp_lexer_new_from_tokens (tokens);
14385
14386 /* Set the current source position to be the location of the
14387 first token in the default argument. */
3466b292 14388 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14389
14390 /* Local variable names (and the `this' keyword) may not appear
14391 in a default argument. */
14392 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14393 parser->local_variables_forbidden_p = true;
14394 /* Parse the assignment-expression. */
f128e1f3 14395 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 14396 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14397 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 14398 if (DECL_CLASS_SCOPE_P (fn))
e5976695 14399 pop_nested_class ();
a723baf1
MM
14400
14401 /* Restore saved state. */
14402 parser->lexer = saved_lexer;
14403 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14404 }
b92bc2a0
NS
14405
14406 /* Restore the queue. */
14407 parser->unparsed_functions_queues
14408 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
14409}
14410
14411/* Parse the operand of `sizeof' (or a similar operator). Returns
14412 either a TYPE or an expression, depending on the form of the
14413 input. The KEYWORD indicates which kind of expression we have
14414 encountered. */
14415
14416static tree
94edc4ab 14417cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14418{
14419 static const char *format;
14420 tree expr = NULL_TREE;
14421 const char *saved_message;
67c03833 14422 bool saved_integral_constant_expression_p;
a723baf1
MM
14423
14424 /* Initialize FORMAT the first time we get here. */
14425 if (!format)
14426 format = "types may not be defined in `%s' expressions";
14427
14428 /* Types cannot be defined in a `sizeof' expression. Save away the
14429 old message. */
14430 saved_message = parser->type_definition_forbidden_message;
14431 /* And create the new one. */
14432 parser->type_definition_forbidden_message
c68b0a84
KG
14433 = xmalloc (strlen (format)
14434 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14435 + 1 /* `\0' */);
a723baf1
MM
14436 sprintf ((char *) parser->type_definition_forbidden_message,
14437 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14438
14439 /* The restrictions on constant-expressions do not apply inside
14440 sizeof expressions. */
67c03833
JM
14441 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14442 parser->integral_constant_expression_p = false;
a723baf1 14443
3beb3abf
MM
14444 /* Do not actually evaluate the expression. */
14445 ++skip_evaluation;
a723baf1
MM
14446 /* If it's a `(', then we might be looking at the type-id
14447 construction. */
14448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14449 {
14450 tree type;
14451
14452 /* We can't be sure yet whether we're looking at a type-id or an
14453 expression. */
14454 cp_parser_parse_tentatively (parser);
14455 /* Consume the `('. */
14456 cp_lexer_consume_token (parser->lexer);
14457 /* Parse the type-id. */
14458 type = cp_parser_type_id (parser);
14459 /* Now, look for the trailing `)'. */
14460 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14461 /* If all went well, then we're done. */
14462 if (cp_parser_parse_definitely (parser))
14463 {
14464 /* Build a list of decl-specifiers; right now, we have only
14465 a single type-specifier. */
14466 type = build_tree_list (NULL_TREE,
14467 type);
14468
14469 /* Call grokdeclarator to figure out what type this is. */
14470 expr = grokdeclarator (NULL_TREE,
14471 type,
14472 TYPENAME,
14473 /*initialized=*/0,
14474 /*attrlist=*/NULL);
14475 }
14476 }
14477
14478 /* If the type-id production did not work out, then we must be
14479 looking at the unary-expression production. */
14480 if (!expr)
14481 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14482 /* Go back to evaluating expressions. */
14483 --skip_evaluation;
a723baf1
MM
14484
14485 /* Free the message we created. */
14486 free ((char *) parser->type_definition_forbidden_message);
14487 /* And restore the old one. */
14488 parser->type_definition_forbidden_message = saved_message;
67c03833 14489 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
14490
14491 return expr;
14492}
14493
14494/* If the current declaration has no declarator, return true. */
14495
14496static bool
14497cp_parser_declares_only_class_p (cp_parser *parser)
14498{
14499 /* If the next token is a `;' or a `,' then there is no
14500 declarator. */
14501 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14502 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14503}
14504
d17811fd
MM
14505/* Simplify EXPR if it is a non-dependent expression. Returns the
14506 (possibly simplified) expression. */
14507
14508static tree
14509cp_parser_fold_non_dependent_expr (tree expr)
14510{
14511 /* If we're in a template, but EXPR isn't value dependent, simplify
14512 it. We're supposed to treat:
14513
14514 template <typename T> void f(T[1 + 1]);
14515 template <typename T> void f(T[2]);
14516
14517 as two declarations of the same function, for example. */
14518 if (processing_template_decl
14519 && !type_dependent_expression_p (expr)
14520 && !value_dependent_expression_p (expr))
14521 {
14522 HOST_WIDE_INT saved_processing_template_decl;
14523
14524 saved_processing_template_decl = processing_template_decl;
14525 processing_template_decl = 0;
14526 expr = tsubst_copy_and_build (expr,
14527 /*args=*/NULL_TREE,
14528 tf_error,
b3445994
MM
14529 /*in_decl=*/NULL_TREE,
14530 /*function_p=*/false);
d17811fd
MM
14531 processing_template_decl = saved_processing_template_decl;
14532 }
14533 return expr;
14534}
14535
a723baf1
MM
14536/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14537 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14538
14539static bool
94edc4ab 14540cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14541{
14542 while (decl_specifiers)
14543 {
14544 /* See if this decl-specifier is `friend'. */
14545 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14546 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14547 return true;
14548
14549 /* Go on to the next decl-specifier. */
14550 decl_specifiers = TREE_CHAIN (decl_specifiers);
14551 }
14552
14553 return false;
14554}
14555
14556/* If the next token is of the indicated TYPE, consume it. Otherwise,
14557 issue an error message indicating that TOKEN_DESC was expected.
14558
14559 Returns the token consumed, if the token had the appropriate type.
14560 Otherwise, returns NULL. */
14561
14562static cp_token *
94edc4ab
NN
14563cp_parser_require (cp_parser* parser,
14564 enum cpp_ttype type,
14565 const char* token_desc)
a723baf1
MM
14566{
14567 if (cp_lexer_next_token_is (parser->lexer, type))
14568 return cp_lexer_consume_token (parser->lexer);
14569 else
14570 {
e5976695
MM
14571 /* Output the MESSAGE -- unless we're parsing tentatively. */
14572 if (!cp_parser_simulate_error (parser))
14573 error ("expected %s", token_desc);
a723baf1
MM
14574 return NULL;
14575 }
14576}
14577
14578/* Like cp_parser_require, except that tokens will be skipped until
14579 the desired token is found. An error message is still produced if
14580 the next token is not as expected. */
14581
14582static void
94edc4ab
NN
14583cp_parser_skip_until_found (cp_parser* parser,
14584 enum cpp_ttype type,
14585 const char* token_desc)
a723baf1
MM
14586{
14587 cp_token *token;
14588 unsigned nesting_depth = 0;
14589
14590 if (cp_parser_require (parser, type, token_desc))
14591 return;
14592
14593 /* Skip tokens until the desired token is found. */
14594 while (true)
14595 {
14596 /* Peek at the next token. */
14597 token = cp_lexer_peek_token (parser->lexer);
14598 /* If we've reached the token we want, consume it and
14599 stop. */
14600 if (token->type == type && !nesting_depth)
14601 {
14602 cp_lexer_consume_token (parser->lexer);
14603 return;
14604 }
14605 /* If we've run out of tokens, stop. */
14606 if (token->type == CPP_EOF)
14607 return;
14608 if (token->type == CPP_OPEN_BRACE
14609 || token->type == CPP_OPEN_PAREN
14610 || token->type == CPP_OPEN_SQUARE)
14611 ++nesting_depth;
14612 else if (token->type == CPP_CLOSE_BRACE
14613 || token->type == CPP_CLOSE_PAREN
14614 || token->type == CPP_CLOSE_SQUARE)
14615 {
14616 if (nesting_depth-- == 0)
14617 return;
14618 }
14619 /* Consume this token. */
14620 cp_lexer_consume_token (parser->lexer);
14621 }
14622}
14623
14624/* If the next token is the indicated keyword, consume it. Otherwise,
14625 issue an error message indicating that TOKEN_DESC was expected.
14626
14627 Returns the token consumed, if the token had the appropriate type.
14628 Otherwise, returns NULL. */
14629
14630static cp_token *
94edc4ab
NN
14631cp_parser_require_keyword (cp_parser* parser,
14632 enum rid keyword,
14633 const char* token_desc)
a723baf1
MM
14634{
14635 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14636
14637 if (token && token->keyword != keyword)
14638 {
14639 dyn_string_t error_msg;
14640
14641 /* Format the error message. */
14642 error_msg = dyn_string_new (0);
14643 dyn_string_append_cstr (error_msg, "expected ");
14644 dyn_string_append_cstr (error_msg, token_desc);
14645 cp_parser_error (parser, error_msg->s);
14646 dyn_string_delete (error_msg);
14647 return NULL;
14648 }
14649
14650 return token;
14651}
14652
14653/* Returns TRUE iff TOKEN is a token that can begin the body of a
14654 function-definition. */
14655
14656static bool
94edc4ab 14657cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14658{
14659 return (/* An ordinary function-body begins with an `{'. */
14660 token->type == CPP_OPEN_BRACE
14661 /* A ctor-initializer begins with a `:'. */
14662 || token->type == CPP_COLON
14663 /* A function-try-block begins with `try'. */
14664 || token->keyword == RID_TRY
14665 /* The named return value extension begins with `return'. */
14666 || token->keyword == RID_RETURN);
14667}
14668
14669/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14670 definition. */
14671
14672static bool
14673cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14674{
14675 cp_token *token;
14676
14677 token = cp_lexer_peek_token (parser->lexer);
14678 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14679}
14680
d17811fd 14681/* Returns TRUE iff the next token is the "," or ">" ending a
4d5297fa
GB
14682 template-argument. ">>" is also accepted (after the full
14683 argument was parsed) because it's probably a typo for "> >",
14684 and there is a specific diagnostic for this. */
d17811fd
MM
14685
14686static bool
14687cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14688{
14689 cp_token *token;
14690
14691 token = cp_lexer_peek_token (parser->lexer);
4d5297fa
GB
14692 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14693 || token->type == CPP_RSHIFT);
d17811fd
MM
14694}
14695
a723baf1
MM
14696/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14697 or none_type otherwise. */
14698
14699static enum tag_types
94edc4ab 14700cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14701{
14702 switch (token->keyword)
14703 {
14704 case RID_CLASS:
14705 return class_type;
14706 case RID_STRUCT:
14707 return record_type;
14708 case RID_UNION:
14709 return union_type;
14710
14711 default:
14712 return none_type;
14713 }
14714}
14715
14716/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14717
14718static void
14719cp_parser_check_class_key (enum tag_types class_key, tree type)
14720{
14721 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14722 pedwarn ("`%s' tag used in naming `%#T'",
14723 class_key == union_type ? "union"
14724 : class_key == record_type ? "struct" : "class",
14725 type);
14726}
14727
cd0be382 14728/* Issue an error message if DECL is redeclared with different
37d407a1
KL
14729 access than its original declaration [class.access.spec/3].
14730 This applies to nested classes and nested class templates.
14731 [class.mem/1]. */
14732
14733static void cp_parser_check_access_in_redeclaration (tree decl)
14734{
14735 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14736 return;
14737
14738 if ((TREE_PRIVATE (decl)
14739 != (current_access_specifier == access_private_node))
14740 || (TREE_PROTECTED (decl)
14741 != (current_access_specifier == access_protected_node)))
14742 error ("%D redeclared with different access", decl);
14743}
14744
a723baf1
MM
14745/* Look for the `template' keyword, as a syntactic disambiguator.
14746 Return TRUE iff it is present, in which case it will be
14747 consumed. */
14748
14749static bool
14750cp_parser_optional_template_keyword (cp_parser *parser)
14751{
14752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14753 {
14754 /* The `template' keyword can only be used within templates;
14755 outside templates the parser can always figure out what is a
14756 template and what is not. */
14757 if (!processing_template_decl)
14758 {
14759 error ("`template' (as a disambiguator) is only allowed "
14760 "within templates");
14761 /* If this part of the token stream is rescanned, the same
14762 error message would be generated. So, we purge the token
14763 from the stream. */
14764 cp_lexer_purge_token (parser->lexer);
14765 return false;
14766 }
14767 else
14768 {
14769 /* Consume the `template' keyword. */
14770 cp_lexer_consume_token (parser->lexer);
14771 return true;
14772 }
14773 }
14774
14775 return false;
14776}
14777
2050a1bb
MM
14778/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14779 set PARSER->SCOPE, and perform other related actions. */
14780
14781static void
14782cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14783{
14784 tree value;
14785 tree check;
14786
14787 /* Get the stored value. */
14788 value = cp_lexer_consume_token (parser->lexer)->value;
14789 /* Perform any access checks that were deferred. */
14790 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14791 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14792 /* Set the scope from the stored value. */
14793 parser->scope = TREE_VALUE (value);
14794 parser->qualifying_scope = TREE_TYPE (value);
14795 parser->object_scope = NULL_TREE;
14796}
14797
a723baf1
MM
14798/* Add tokens to CACHE until an non-nested END token appears. */
14799
14800static void
14801cp_parser_cache_group (cp_parser *parser,
14802 cp_token_cache *cache,
14803 enum cpp_ttype end,
14804 unsigned depth)
14805{
14806 while (true)
14807 {
14808 cp_token *token;
14809
14810 /* Abort a parenthesized expression if we encounter a brace. */
14811 if ((end == CPP_CLOSE_PAREN || depth == 0)
14812 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14813 return;
14814 /* Consume the next token. */
14815 token = cp_lexer_consume_token (parser->lexer);
14816 /* If we've reached the end of the file, stop. */
14817 if (token->type == CPP_EOF)
14818 return;
14819 /* Add this token to the tokens we are saving. */
14820 cp_token_cache_push_token (cache, token);
14821 /* See if it starts a new group. */
14822 if (token->type == CPP_OPEN_BRACE)
14823 {
14824 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14825 if (depth == 0)
14826 return;
14827 }
14828 else if (token->type == CPP_OPEN_PAREN)
14829 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14830 else if (token->type == end)
14831 return;
14832 }
14833}
14834
14835/* Begin parsing tentatively. We always save tokens while parsing
14836 tentatively so that if the tentative parsing fails we can restore the
14837 tokens. */
14838
14839static void
94edc4ab 14840cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14841{
14842 /* Enter a new parsing context. */
14843 parser->context = cp_parser_context_new (parser->context);
14844 /* Begin saving tokens. */
14845 cp_lexer_save_tokens (parser->lexer);
14846 /* In order to avoid repetitive access control error messages,
14847 access checks are queued up until we are no longer parsing
14848 tentatively. */
8d241e0b 14849 push_deferring_access_checks (dk_deferred);
a723baf1
MM
14850}
14851
14852/* Commit to the currently active tentative parse. */
14853
14854static void
94edc4ab 14855cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14856{
14857 cp_parser_context *context;
14858 cp_lexer *lexer;
14859
14860 /* Mark all of the levels as committed. */
14861 lexer = parser->lexer;
14862 for (context = parser->context; context->next; context = context->next)
14863 {
14864 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14865 break;
14866 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14867 while (!cp_lexer_saving_tokens (lexer))
14868 lexer = lexer->next;
14869 cp_lexer_commit_tokens (lexer);
14870 }
14871}
14872
14873/* Abort the currently active tentative parse. All consumed tokens
14874 will be rolled back, and no diagnostics will be issued. */
14875
14876static void
94edc4ab 14877cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14878{
14879 cp_parser_simulate_error (parser);
14880 /* Now, pretend that we want to see if the construct was
14881 successfully parsed. */
14882 cp_parser_parse_definitely (parser);
14883}
14884
34cd5ae7 14885/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
14886 token stream. Otherwise, commit to the tokens we have consumed.
14887 Returns true if no error occurred; false otherwise. */
14888
14889static bool
94edc4ab 14890cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14891{
14892 bool error_occurred;
14893 cp_parser_context *context;
14894
34cd5ae7 14895 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
14896 destroy that information. */
14897 error_occurred = cp_parser_error_occurred (parser);
14898 /* Remove the topmost context from the stack. */
14899 context = parser->context;
14900 parser->context = context->next;
14901 /* If no parse errors occurred, commit to the tentative parse. */
14902 if (!error_occurred)
14903 {
14904 /* Commit to the tokens read tentatively, unless that was
14905 already done. */
14906 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14907 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14908
14909 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14910 }
14911 /* Otherwise, if errors occurred, roll back our state so that things
14912 are just as they were before we began the tentative parse. */
14913 else
cf22909c
KL
14914 {
14915 cp_lexer_rollback_tokens (parser->lexer);
14916 pop_deferring_access_checks ();
14917 }
e5976695
MM
14918 /* Add the context to the front of the free list. */
14919 context->next = cp_parser_context_free_list;
14920 cp_parser_context_free_list = context;
14921
14922 return !error_occurred;
a723baf1
MM
14923}
14924
a723baf1
MM
14925/* Returns true if we are parsing tentatively -- but have decided that
14926 we will stick with this tentative parse, even if errors occur. */
14927
14928static bool
94edc4ab 14929cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14930{
14931 return (cp_parser_parsing_tentatively (parser)
14932 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14933}
14934
4de8668e 14935/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
14936 tentative parse. */
14937
14938static bool
94edc4ab 14939cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14940{
14941 return (cp_parser_parsing_tentatively (parser)
14942 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14943}
14944
4de8668e 14945/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
14946
14947static bool
94edc4ab 14948cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14949{
14950 return parser->allow_gnu_extensions_p;
14951}
14952
14953\f
14954
14955/* The parser. */
14956
14957static GTY (()) cp_parser *the_parser;
14958
14959/* External interface. */
14960
d1bd0ded 14961/* Parse one entire translation unit. */
a723baf1 14962
d1bd0ded
GK
14963void
14964c_parse_file (void)
a723baf1
MM
14965{
14966 bool error_occurred;
14967
14968 the_parser = cp_parser_new ();
78757caa
KL
14969 push_deferring_access_checks (flag_access_control
14970 ? dk_no_deferred : dk_no_check);
a723baf1
MM
14971 error_occurred = cp_parser_translation_unit (the_parser);
14972 the_parser = NULL;
a723baf1
MM
14973}
14974
14975/* Clean up after parsing the entire translation unit. */
14976
14977void
94edc4ab 14978free_parser_stacks (void)
a723baf1
MM
14979{
14980 /* Nothing to do. */
14981}
14982
14983/* This variable must be provided by every front end. */
14984
14985int yydebug;
14986
14987#include "gt-cp-parser.h"