]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
ChangeLog: Fix typos.
[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. */
522df488 71 enum 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. */
522df488
DN
74 enum rid keyword : 8;
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
NN
221 (cp_lexer *, cp_token *);
222static ptrdiff_t cp_lexer_token_difference
223 (cp_lexer *, cp_token *, cp_token *);
a723baf1 224static cp_token *cp_lexer_read_token
94edc4ab 225 (cp_lexer *);
a723baf1 226static void cp_lexer_maybe_grow_buffer
94edc4ab 227 (cp_lexer *);
a723baf1 228static void cp_lexer_get_preprocessor_token
94edc4ab 229 (cp_lexer *, cp_token *);
a723baf1 230static cp_token *cp_lexer_peek_token
94edc4ab 231 (cp_lexer *);
a723baf1 232static cp_token *cp_lexer_peek_nth_token
94edc4ab 233 (cp_lexer *, size_t);
f7b5ecd9 234static inline bool cp_lexer_next_token_is
94edc4ab 235 (cp_lexer *, enum cpp_ttype);
a723baf1 236static bool cp_lexer_next_token_is_not
94edc4ab 237 (cp_lexer *, enum cpp_ttype);
a723baf1 238static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
239 (cp_lexer *, enum rid);
240static cp_token *cp_lexer_consume_token
241 (cp_lexer *);
a723baf1
MM
242static void cp_lexer_purge_token
243 (cp_lexer *);
244static void cp_lexer_purge_tokens_after
245 (cp_lexer *, cp_token *);
246static void cp_lexer_save_tokens
94edc4ab 247 (cp_lexer *);
a723baf1 248static void cp_lexer_commit_tokens
94edc4ab 249 (cp_lexer *);
a723baf1 250static void cp_lexer_rollback_tokens
94edc4ab 251 (cp_lexer *);
f7b5ecd9 252static inline void cp_lexer_set_source_position_from_token
94edc4ab 253 (cp_lexer *, const cp_token *);
a723baf1 254static void cp_lexer_print_token
94edc4ab 255 (FILE *, cp_token *);
f7b5ecd9 256static inline bool cp_lexer_debugging_p
94edc4ab 257 (cp_lexer *);
a723baf1 258static void cp_lexer_start_debugging
94edc4ab 259 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 260static void cp_lexer_stop_debugging
94edc4ab 261 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
262
263/* Manifest constants. */
264
265#define CP_TOKEN_BUFFER_SIZE 5
266#define CP_SAVED_TOKENS_SIZE 5
267
268/* A token type for keywords, as opposed to ordinary identifiers. */
269#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
270
271/* A token type for template-ids. If a template-id is processed while
272 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
273 the value of the CPP_TEMPLATE_ID is whatever was returned by
274 cp_parser_template_id. */
275#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
276
277/* A token type for nested-name-specifiers. If a
278 nested-name-specifier is processed while parsing tentatively, it is
279 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
280 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
281 cp_parser_nested_name_specifier_opt. */
282#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
283
284/* A token type for tokens that are not tokens at all; these are used
285 to mark the end of a token block. */
286#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
287
288/* Variables. */
289
290/* The stream to which debugging output should be written. */
291static FILE *cp_lexer_debug_stream;
292
17211ab5
GK
293/* Create a new main C++ lexer, the lexer that gets tokens from the
294 preprocessor. */
a723baf1
MM
295
296static cp_lexer *
17211ab5 297cp_lexer_new_main (void)
a723baf1
MM
298{
299 cp_lexer *lexer;
17211ab5
GK
300 cp_token first_token;
301
302 /* It's possible that lexing the first token will load a PCH file,
303 which is a GC collection point. So we have to grab the first
304 token before allocating any memory. */
305 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 306 c_common_no_more_pch ();
a723baf1
MM
307
308 /* Allocate the memory. */
c68b0a84 309 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
310
311 /* Create the circular buffer. */
c68b0a84 312 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
313 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
314
17211ab5
GK
315 /* There is one token in the buffer. */
316 lexer->last_token = lexer->buffer + 1;
317 lexer->first_token = lexer->buffer;
318 lexer->next_token = lexer->buffer;
319 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
320
321 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 322 lexer->main_lexer_p = true;
a723baf1
MM
323
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
326
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
329
330 /* Assume we are not debugging. */
331 lexer->debugging_p = false;
332
333 return lexer;
334}
335
336/* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
338
339static cp_lexer *
340cp_lexer_new_from_tokens (cp_token_cache *tokens)
341{
342 cp_lexer *lexer;
343 cp_token *token;
344 cp_token_block *block;
345 ptrdiff_t num_tokens;
346
17211ab5 347 /* Allocate the memory. */
c68b0a84 348 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
349
350 /* Create a new buffer, appropriately sized. */
351 num_tokens = 0;
352 for (block = tokens->first; block != NULL; block = block->next)
353 num_tokens += block->num_tokens;
c68b0a84 354 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1
MM
355 lexer->buffer_end = lexer->buffer + num_tokens;
356
357 /* Install the tokens. */
358 token = lexer->buffer;
359 for (block = tokens->first; block != NULL; block = block->next)
360 {
361 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
362 token += block->num_tokens;
363 }
364
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer->first_token = lexer->buffer;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer->next_token = lexer->buffer;
369 /* The buffer is full. */
370 lexer->last_token = lexer->first_token;
371
17211ab5
GK
372 /* This lexer doesn't obtain more tokens. */
373 lexer->main_lexer_p = false;
374
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
377
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
380
381 /* Assume we are not debugging. */
382 lexer->debugging_p = false;
383
a723baf1
MM
384 return lexer;
385}
386
4de8668e 387/* Returns nonzero if debugging information should be output. */
a723baf1 388
f7b5ecd9
MM
389static inline bool
390cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 391{
f7b5ecd9
MM
392 return lexer->debugging_p;
393}
394
395/* Set the current source position from the information stored in
396 TOKEN. */
397
398static inline void
94edc4ab
NN
399cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
400 const cp_token *token)
f7b5ecd9
MM
401{
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
404
405 /* Update the line number. */
406 if (token->type != CPP_EOF)
82a98427 407 input_location = token->location;
a723baf1
MM
408}
409
410/* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
412
f7b5ecd9 413static inline cp_token *
94edc4ab 414cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
415{
416 token++;
417 if (token == lexer->buffer_end)
418 token = lexer->buffer;
419 return token;
420}
421
4de8668e 422/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
423
424static int
94edc4ab 425cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
426{
427 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
428}
429
a723baf1
MM
430/* Return a pointer to the token that is N tokens beyond TOKEN in the
431 buffer. */
432
433static cp_token *
434cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
435{
436 token += n;
437 if (token >= lexer->buffer_end)
438 token = lexer->buffer + (token - lexer->buffer_end);
439 return token;
440}
441
442/* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
444
445static ptrdiff_t
94edc4ab 446cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
447{
448 if (finish >= start)
449 return finish - start;
450 else
451 return ((lexer->buffer_end - lexer->buffer)
452 - (start - finish));
453}
454
455/* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
457
458static cp_token *
94edc4ab 459cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
460{
461 cp_token *token;
462
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer);
465
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer->first_token)
468 lexer->first_token = lexer->last_token;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer->next_token)
471 lexer->next_token = lexer->last_token;
472
473 /* Figure out where we're going to store the new token. */
474 token = lexer->last_token;
475
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer, token);
478
479 /* Increment LAST_TOKEN. */
480 lexer->last_token = cp_lexer_next_token (lexer, token);
481
e6cc3a24
ZW
482 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
484 constant elements.
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
487 && flag_const_strings)
a723baf1 488 {
e6cc3a24
ZW
489 tree type;
490
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type = TREE_TYPE (token->value);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
496 /* Reset the type of the token. */
497 TREE_TYPE (token->value) = type;
a723baf1
MM
498 }
499
500 return token;
501}
502
503/* If the circular buffer is full, make it bigger. */
504
505static void
94edc4ab 506cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
507{
508 /* If the buffer is full, enlarge it. */
509 if (lexer->last_token == lexer->first_token)
510 {
511 cp_token *new_buffer;
512 cp_token *old_buffer;
513 cp_token *new_first_token;
514 ptrdiff_t buffer_length;
515 size_t num_tokens_to_copy;
516
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
519 value. */
520 old_buffer = lexer->buffer;
521 /* Compute the current buffer size. */
522 buffer_length = lexer->buffer_end - lexer->buffer;
523 /* Allocate a buffer twice as big. */
c68b0a84
KG
524 new_buffer = ggc_realloc (lexer->buffer,
525 2 * buffer_length * sizeof (cp_token));
a723baf1
MM
526
527 /* Because the buffer is circular, logically consecutive tokens
528 are not necessarily placed consecutively in memory.
529 Therefore, we must keep move the tokens that were before
530 FIRST_TOKEN to the second half of the newly allocated
531 buffer. */
532 num_tokens_to_copy = (lexer->first_token - old_buffer);
533 memcpy (new_buffer + buffer_length,
534 new_buffer,
535 num_tokens_to_copy * sizeof (cp_token));
536 /* Clear the rest of the buffer. We never look at this storage,
537 but the garbage collector may. */
538 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
539 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
540
541 /* Now recompute all of the buffer pointers. */
542 new_first_token
543 = new_buffer + (lexer->first_token - old_buffer);
544 if (lexer->next_token != NULL)
545 {
546 ptrdiff_t next_token_delta;
547
548 if (lexer->next_token > lexer->first_token)
549 next_token_delta = lexer->next_token - lexer->first_token;
550 else
551 next_token_delta =
552 buffer_length - (lexer->first_token - lexer->next_token);
553 lexer->next_token = new_first_token + next_token_delta;
554 }
555 lexer->last_token = new_first_token + buffer_length;
556 lexer->buffer = new_buffer;
557 lexer->buffer_end = new_buffer + buffer_length * 2;
558 lexer->first_token = new_first_token;
559 }
560}
561
562/* Store the next token from the preprocessor in *TOKEN. */
563
564static void
94edc4ab
NN
565cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
566 cp_token *token)
a723baf1
MM
567{
568 bool done;
569
570 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 571 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
572 {
573 token->type = CPP_EOF;
82a98427
NS
574 token->location.line = 0;
575 token->location.file = NULL;
a723baf1
MM
576 token->value = NULL_TREE;
577 token->keyword = RID_MAX;
578
579 return;
580 }
581
582 done = false;
583 /* Keep going until we get a token we like. */
584 while (!done)
585 {
586 /* Get a new token from the preprocessor. */
587 token->type = c_lex (&token->value);
588 /* Issue messages about tokens we cannot process. */
589 switch (token->type)
590 {
591 case CPP_ATSIGN:
592 case CPP_HASH:
593 case CPP_PASTE:
594 error ("invalid token");
595 break;
596
a723baf1
MM
597 default:
598 /* This is a good token, so we exit the loop. */
599 done = true;
600 break;
601 }
602 }
603 /* Now we've got our token. */
82a98427 604 token->location = input_location;
a723baf1
MM
605
606 /* Check to see if this token is a keyword. */
607 if (token->type == CPP_NAME
608 && C_IS_RESERVED_WORD (token->value))
609 {
610 /* Mark this token as a keyword. */
611 token->type = CPP_KEYWORD;
612 /* Record which keyword. */
613 token->keyword = C_RID_CODE (token->value);
614 /* Update the value. Some keywords are mapped to particular
615 entities, rather than simply having the value of the
616 corresponding IDENTIFIER_NODE. For example, `__const' is
617 mapped to `const'. */
618 token->value = ridpointers[token->keyword];
619 }
620 else
621 token->keyword = RID_MAX;
622}
623
624/* Return a pointer to the next token in the token stream, but do not
625 consume it. */
626
627static cp_token *
94edc4ab 628cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
629{
630 cp_token *token;
631
632 /* If there are no tokens, read one now. */
633 if (!lexer->next_token)
634 cp_lexer_read_token (lexer);
635
636 /* Provide debugging output. */
637 if (cp_lexer_debugging_p (lexer))
638 {
639 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
640 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
641 fprintf (cp_lexer_debug_stream, "\n");
642 }
643
644 token = lexer->next_token;
645 cp_lexer_set_source_position_from_token (lexer, token);
646 return token;
647}
648
649/* Return true if the next token has the indicated TYPE. */
650
651static bool
94edc4ab 652cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
653{
654 cp_token *token;
655
656 /* Peek at the next token. */
657 token = cp_lexer_peek_token (lexer);
658 /* Check to see if it has the indicated TYPE. */
659 return token->type == type;
660}
661
662/* Return true if the next token does not have the indicated TYPE. */
663
664static bool
94edc4ab 665cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
666{
667 return !cp_lexer_next_token_is (lexer, type);
668}
669
670/* Return true if the next token is the indicated KEYWORD. */
671
672static bool
94edc4ab 673cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
674{
675 cp_token *token;
676
677 /* Peek at the next token. */
678 token = cp_lexer_peek_token (lexer);
679 /* Check to see if it is the indicated keyword. */
680 return token->keyword == keyword;
681}
682
683/* Return a pointer to the Nth token in the token stream. If N is 1,
684 then this is precisely equivalent to cp_lexer_peek_token. */
685
686static cp_token *
94edc4ab 687cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
688{
689 cp_token *token;
690
691 /* N is 1-based, not zero-based. */
692 my_friendly_assert (n > 0, 20000224);
693
694 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
695 token = lexer->next_token;
696 /* If there are no tokens in the buffer, get one now. */
697 if (!token)
698 {
699 cp_lexer_read_token (lexer);
700 token = lexer->next_token;
701 }
702
703 /* Now, read tokens until we have enough. */
704 while (--n > 0)
705 {
706 /* Advance to the next token. */
707 token = cp_lexer_next_token (lexer, token);
708 /* If that's all the tokens we have, read a new one. */
709 if (token == lexer->last_token)
710 token = cp_lexer_read_token (lexer);
711 }
712
713 return token;
714}
715
716/* Consume the next token. The pointer returned is valid only until
717 another token is read. Callers should preserve copy the token
718 explicitly if they will need its value for a longer period of
719 time. */
720
721static cp_token *
94edc4ab 722cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
723{
724 cp_token *token;
725
726 /* If there are no tokens, read one now. */
727 if (!lexer->next_token)
728 cp_lexer_read_token (lexer);
729
730 /* Remember the token we'll be returning. */
731 token = lexer->next_token;
732
733 /* Increment NEXT_TOKEN. */
734 lexer->next_token = cp_lexer_next_token (lexer,
735 lexer->next_token);
736 /* Check to see if we're all out of tokens. */
737 if (lexer->next_token == lexer->last_token)
738 lexer->next_token = NULL;
739
740 /* If we're not saving tokens, then move FIRST_TOKEN too. */
741 if (!cp_lexer_saving_tokens (lexer))
742 {
743 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
744 if (!lexer->next_token)
745 lexer->first_token = NULL;
746 else
747 lexer->first_token = lexer->next_token;
748 }
749
750 /* Provide debugging output. */
751 if (cp_lexer_debugging_p (lexer))
752 {
753 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
754 cp_lexer_print_token (cp_lexer_debug_stream, token);
755 fprintf (cp_lexer_debug_stream, "\n");
756 }
757
758 return token;
759}
760
761/* Permanently remove the next token from the token stream. There
762 must be a valid next token already; this token never reads
763 additional tokens from the preprocessor. */
764
765static void
766cp_lexer_purge_token (cp_lexer *lexer)
767{
768 cp_token *token;
769 cp_token *next_token;
770
771 token = lexer->next_token;
772 while (true)
773 {
774 next_token = cp_lexer_next_token (lexer, token);
775 if (next_token == lexer->last_token)
776 break;
777 *token = *next_token;
778 token = next_token;
779 }
780
781 lexer->last_token = token;
782 /* The token purged may have been the only token remaining; if so,
783 clear NEXT_TOKEN. */
784 if (lexer->next_token == token)
785 lexer->next_token = NULL;
786}
787
788/* Permanently remove all tokens after TOKEN, up to, but not
789 including, the token that will be returned next by
790 cp_lexer_peek_token. */
791
792static void
793cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
794{
795 cp_token *peek;
796 cp_token *t1;
797 cp_token *t2;
798
799 if (lexer->next_token)
800 {
801 /* Copy the tokens that have not yet been read to the location
802 immediately following TOKEN. */
803 t1 = cp_lexer_next_token (lexer, token);
804 t2 = peek = cp_lexer_peek_token (lexer);
805 /* Move tokens into the vacant area between TOKEN and PEEK. */
806 while (t2 != lexer->last_token)
807 {
808 *t1 = *t2;
809 t1 = cp_lexer_next_token (lexer, t1);
810 t2 = cp_lexer_next_token (lexer, t2);
811 }
812 /* Now, the next available token is right after TOKEN. */
813 lexer->next_token = cp_lexer_next_token (lexer, token);
814 /* And the last token is wherever we ended up. */
815 lexer->last_token = t1;
816 }
817 else
818 {
819 /* There are no tokens in the buffer, so there is nothing to
820 copy. The last token in the buffer is TOKEN itself. */
821 lexer->last_token = cp_lexer_next_token (lexer, token);
822 }
823}
824
825/* Begin saving tokens. All tokens consumed after this point will be
826 preserved. */
827
828static void
94edc4ab 829cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
830{
831 /* Provide debugging output. */
832 if (cp_lexer_debugging_p (lexer))
833 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
834
835 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
836 restore the tokens if required. */
837 if (!lexer->next_token)
838 cp_lexer_read_token (lexer);
839
840 VARRAY_PUSH_INT (lexer->saved_tokens,
841 cp_lexer_token_difference (lexer,
842 lexer->first_token,
843 lexer->next_token));
844}
845
846/* Commit to the portion of the token stream most recently saved. */
847
848static void
94edc4ab 849cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
850{
851 /* Provide debugging output. */
852 if (cp_lexer_debugging_p (lexer))
853 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
854
855 VARRAY_POP (lexer->saved_tokens);
856}
857
858/* Return all tokens saved since the last call to cp_lexer_save_tokens
859 to the token stream. Stop saving tokens. */
860
861static void
94edc4ab 862cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
863{
864 size_t delta;
865
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
869
870 /* Find the token that was the NEXT_TOKEN when we started saving
871 tokens. */
872 delta = VARRAY_TOP_INT(lexer->saved_tokens);
873 /* Make it the next token again now. */
874 lexer->next_token = cp_lexer_advance_token (lexer,
875 lexer->first_token,
876 delta);
15d2cb19 877 /* It might be the case that there were no tokens when we started
a723baf1
MM
878 saving tokens, but that there are some tokens now. */
879 if (!lexer->next_token && lexer->first_token)
880 lexer->next_token = lexer->first_token;
881
882 /* Stop saving tokens. */
883 VARRAY_POP (lexer->saved_tokens);
884}
885
a723baf1
MM
886/* Print a representation of the TOKEN on the STREAM. */
887
888static void
94edc4ab 889cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
890{
891 const char *token_type = NULL;
892
893 /* Figure out what kind of token this is. */
894 switch (token->type)
895 {
896 case CPP_EQ:
897 token_type = "EQ";
898 break;
899
900 case CPP_COMMA:
901 token_type = "COMMA";
902 break;
903
904 case CPP_OPEN_PAREN:
905 token_type = "OPEN_PAREN";
906 break;
907
908 case CPP_CLOSE_PAREN:
909 token_type = "CLOSE_PAREN";
910 break;
911
912 case CPP_OPEN_BRACE:
913 token_type = "OPEN_BRACE";
914 break;
915
916 case CPP_CLOSE_BRACE:
917 token_type = "CLOSE_BRACE";
918 break;
919
920 case CPP_SEMICOLON:
921 token_type = "SEMICOLON";
922 break;
923
924 case CPP_NAME:
925 token_type = "NAME";
926 break;
927
928 case CPP_EOF:
929 token_type = "EOF";
930 break;
931
932 case CPP_KEYWORD:
933 token_type = "keyword";
934 break;
935
936 /* This is not a token that we know how to handle yet. */
937 default:
938 break;
939 }
940
941 /* If we have a name for the token, print it out. Otherwise, we
942 simply give the numeric code. */
943 if (token_type)
944 fprintf (stream, "%s", token_type);
945 else
946 fprintf (stream, "%d", token->type);
947 /* And, for an identifier, print the identifier name. */
948 if (token->type == CPP_NAME
949 /* Some keywords have a value that is not an IDENTIFIER_NODE.
950 For example, `struct' is mapped to an INTEGER_CST. */
951 || (token->type == CPP_KEYWORD
952 && TREE_CODE (token->value) == IDENTIFIER_NODE))
953 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
954}
955
a723baf1
MM
956/* Start emitting debugging information. */
957
958static void
94edc4ab 959cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
960{
961 ++lexer->debugging_p;
962}
963
964/* Stop emitting debugging information. */
965
966static void
94edc4ab 967cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
968{
969 --lexer->debugging_p;
970}
971
972\f
973/* The parser. */
974
975/* Overview
976 --------
977
978 A cp_parser parses the token stream as specified by the C++
979 grammar. Its job is purely parsing, not semantic analysis. For
980 example, the parser breaks the token stream into declarators,
981 expressions, statements, and other similar syntactic constructs.
982 It does not check that the types of the expressions on either side
983 of an assignment-statement are compatible, or that a function is
984 not declared with a parameter of type `void'.
985
986 The parser invokes routines elsewhere in the compiler to perform
987 semantic analysis and to build up the abstract syntax tree for the
988 code processed.
989
990 The parser (and the template instantiation code, which is, in a
991 way, a close relative of parsing) are the only parts of the
992 compiler that should be calling push_scope and pop_scope, or
993 related functions. The parser (and template instantiation code)
994 keeps track of what scope is presently active; everything else
995 should simply honor that. (The code that generates static
996 initializers may also need to set the scope, in order to check
997 access control correctly when emitting the initializers.)
998
999 Methodology
1000 -----------
1001
1002 The parser is of the standard recursive-descent variety. Upcoming
1003 tokens in the token stream are examined in order to determine which
1004 production to use when parsing a non-terminal. Some C++ constructs
1005 require arbitrary look ahead to disambiguate. For example, it is
1006 impossible, in the general case, to tell whether a statement is an
1007 expression or declaration without scanning the entire statement.
1008 Therefore, the parser is capable of "parsing tentatively." When the
1009 parser is not sure what construct comes next, it enters this mode.
1010 Then, while we attempt to parse the construct, the parser queues up
1011 error messages, rather than issuing them immediately, and saves the
1012 tokens it consumes. If the construct is parsed successfully, the
1013 parser "commits", i.e., it issues any queued error messages and
1014 the tokens that were being preserved are permanently discarded.
1015 If, however, the construct is not parsed successfully, the parser
1016 rolls back its state completely so that it can resume parsing using
1017 a different alternative.
1018
1019 Future Improvements
1020 -------------------
1021
1022 The performance of the parser could probably be improved
1023 substantially. Some possible improvements include:
1024
1025 - The expression parser recurses through the various levels of
1026 precedence as specified in the grammar, rather than using an
1027 operator-precedence technique. Therefore, parsing a simple
1028 identifier requires multiple recursive calls.
1029
1030 - We could often eliminate the need to parse tentatively by
1031 looking ahead a little bit. In some places, this approach
1032 might not entirely eliminate the need to parse tentatively, but
1033 it might still speed up the average case. */
1034
1035/* Flags that are passed to some parsing functions. These values can
1036 be bitwise-ored together. */
1037
1038typedef enum cp_parser_flags
1039{
1040 /* No flags. */
1041 CP_PARSER_FLAGS_NONE = 0x0,
1042 /* The construct is optional. If it is not present, then no error
1043 should be issued. */
1044 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1045 /* When parsing a type-specifier, do not allow user-defined types. */
1046 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1047} cp_parser_flags;
1048
62b8a44e
NS
1049/* The different kinds of declarators we want to parse. */
1050
1051typedef enum cp_parser_declarator_kind
1052{
9bcb9aae 1053 /* We want an abstract declartor. */
62b8a44e
NS
1054 CP_PARSER_DECLARATOR_ABSTRACT,
1055 /* We want a named declarator. */
1056 CP_PARSER_DECLARATOR_NAMED,
712becab 1057 /* We don't mind, but the name must be an unqualified-id */
62b8a44e
NS
1058 CP_PARSER_DECLARATOR_EITHER
1059} cp_parser_declarator_kind;
1060
a723baf1
MM
1061/* A mapping from a token type to a corresponding tree node type. */
1062
1063typedef struct cp_parser_token_tree_map_node
1064{
1065 /* The token type. */
522df488 1066 enum cpp_ttype token_type : 8;
a723baf1 1067 /* The corresponding tree code. */
522df488 1068 enum tree_code tree_type : 8;
a723baf1
MM
1069} cp_parser_token_tree_map_node;
1070
1071/* A complete map consists of several ordinary entries, followed by a
1072 terminator. The terminating entry has a token_type of CPP_EOF. */
1073
1074typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1075
1076/* The status of a tentative parse. */
1077
1078typedef enum cp_parser_status_kind
1079{
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR,
1084 /* We are committed to this tentative parse, whether or not an error
1085 has occurred. */
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087} cp_parser_status_kind;
1088
1089/* Context that is saved and restored when parsing tentatively. */
1090
1091typedef struct cp_parser_context GTY (())
1092{
1093 /* If this is a tentative parsing context, the status of the
1094 tentative parse. */
1095 enum cp_parser_status_kind status;
1096 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1097 that are looked up in this context must be looked up both in the
1098 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1099 the context of the containing expression. */
1100 tree object_type;
a723baf1
MM
1101 /* The next parsing context in the stack. */
1102 struct cp_parser_context *next;
1103} cp_parser_context;
1104
1105/* Prototypes. */
1106
1107/* Constructors and destructors. */
1108
1109static cp_parser_context *cp_parser_context_new
94edc4ab 1110 (cp_parser_context *);
a723baf1 1111
e5976695
MM
1112/* Class variables. */
1113
92bc1323 1114static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1115
a723baf1
MM
1116/* Constructors and destructors. */
1117
1118/* Construct a new context. The context below this one on the stack
1119 is given by NEXT. */
1120
1121static cp_parser_context *
94edc4ab 1122cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1123{
1124 cp_parser_context *context;
1125
1126 /* Allocate the storage. */
e5976695
MM
1127 if (cp_parser_context_free_list != NULL)
1128 {
1129 /* Pull the first entry from the free list. */
1130 context = cp_parser_context_free_list;
1131 cp_parser_context_free_list = context->next;
c68b0a84 1132 memset (context, 0, sizeof (*context));
e5976695
MM
1133 }
1134 else
c68b0a84 1135 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1136 /* No errors have occurred yet in this context. */
1137 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1138 /* If this is not the bottomost context, copy information that we
1139 need from the previous context. */
1140 if (next)
1141 {
1142 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1143 expression, then we are parsing one in this context, too. */
1144 context->object_type = next->object_type;
a723baf1
MM
1145 /* Thread the stack. */
1146 context->next = next;
1147 }
1148
1149 return context;
1150}
1151
1152/* The cp_parser structure represents the C++ parser. */
1153
1154typedef struct cp_parser GTY(())
1155{
1156 /* The lexer from which we are obtaining tokens. */
1157 cp_lexer *lexer;
1158
1159 /* The scope in which names should be looked up. If NULL_TREE, then
1160 we look up names in the scope that is currently open in the
1161 source program. If non-NULL, this is either a TYPE or
1162 NAMESPACE_DECL for the scope in which we should look.
1163
1164 This value is not cleared automatically after a name is looked
1165 up, so we must be careful to clear it before starting a new look
1166 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1167 will look up `Z' in the scope of `X', rather than the current
1168 scope.) Unfortunately, it is difficult to tell when name lookup
1169 is complete, because we sometimes peek at a token, look it up,
1170 and then decide not to consume it. */
1171 tree scope;
1172
1173 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1174 last lookup took place. OBJECT_SCOPE is used if an expression
1175 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1176 respectively. QUALIFYING_SCOPE is used for an expression of the
1177 form "X::Y"; it refers to X. */
1178 tree object_scope;
1179 tree qualifying_scope;
1180
1181 /* A stack of parsing contexts. All but the bottom entry on the
1182 stack will be tentative contexts.
1183
1184 We parse tentatively in order to determine which construct is in
1185 use in some situations. For example, in order to determine
1186 whether a statement is an expression-statement or a
1187 declaration-statement we parse it tentatively as a
1188 declaration-statement. If that fails, we then reparse the same
1189 token stream as an expression-statement. */
1190 cp_parser_context *context;
1191
1192 /* True if we are parsing GNU C++. If this flag is not set, then
1193 GNU extensions are not recognized. */
1194 bool allow_gnu_extensions_p;
1195
1196 /* TRUE if the `>' token should be interpreted as the greater-than
1197 operator. FALSE if it is the end of a template-id or
1198 template-parameter-list. */
1199 bool greater_than_is_operator_p;
1200
1201 /* TRUE if default arguments are allowed within a parameter list
1202 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1203 them permissible. */
a723baf1
MM
1204 bool default_arg_ok_p;
1205
1206 /* TRUE if we are parsing an integral constant-expression. See
1207 [expr.const] for a precise definition. */
a723baf1
MM
1208 bool constant_expression_p;
1209
14d22dd6
MM
1210 /* TRUE if we are parsing an integral constant-expression -- but a
1211 non-constant expression should be permitted as well. This flag
1212 is used when parsing an array bound so that GNU variable-length
1213 arrays are tolerated. */
1214 bool allow_non_constant_expression_p;
1215
1216 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1217 been seen that makes the expression non-constant. */
1218 bool non_constant_expression_p;
1219
a723baf1
MM
1220 /* TRUE if local variable names and `this' are forbidden in the
1221 current context. */
1222 bool local_variables_forbidden_p;
1223
1224 /* TRUE if the declaration we are parsing is part of a
1225 linkage-specification of the form `extern string-literal
1226 declaration'. */
1227 bool in_unbraced_linkage_specification_p;
1228
1229 /* TRUE if we are presently parsing a declarator, after the
1230 direct-declarator. */
1231 bool in_declarator_p;
1232
1233 /* If non-NULL, then we are parsing a construct where new type
1234 definitions are not permitted. The string stored here will be
1235 issued as an error message if a type is defined. */
1236 const char *type_definition_forbidden_message;
1237
8db1028e
NS
1238 /* A list of lists. The outer list is a stack, used for member
1239 functions of local classes. At each level there are two sub-list,
1240 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1241 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1242 TREE_VALUE's. The functions are chained in reverse declaration
1243 order.
1244
1245 The TREE_PURPOSE sublist contains those functions with default
1246 arguments that need post processing, and the TREE_VALUE sublist
1247 contains those functions with definitions that need post
1248 processing.
1249
1250 These lists can only be processed once the outermost class being
9bcb9aae 1251 defined is complete. */
a723baf1
MM
1252 tree unparsed_functions_queues;
1253
1254 /* The number of classes whose definitions are currently in
1255 progress. */
1256 unsigned num_classes_being_defined;
1257
1258 /* The number of template parameter lists that apply directly to the
1259 current declaration. */
1260 unsigned num_template_parameter_lists;
1261} cp_parser;
1262
1263/* The type of a function that parses some kind of expression */
94edc4ab 1264typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1265
1266/* Prototypes. */
1267
1268/* Constructors and destructors. */
1269
1270static cp_parser *cp_parser_new
94edc4ab 1271 (void);
a723baf1
MM
1272
1273/* Routines to parse various constructs.
1274
1275 Those that return `tree' will return the error_mark_node (rather
1276 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1277 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1278 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1279 whether or not a parse error occurred, you should always use
1280 cp_parser_error_occurred. If the construct is optional (indicated
1281 either by an `_opt' in the name of the function that does the
1282 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1283 the construct is not present. */
1284
1285/* Lexical conventions [gram.lex] */
1286
1287static tree cp_parser_identifier
94edc4ab 1288 (cp_parser *);
a723baf1
MM
1289
1290/* Basic concepts [gram.basic] */
1291
1292static bool cp_parser_translation_unit
94edc4ab 1293 (cp_parser *);
a723baf1
MM
1294
1295/* Expressions [gram.expr] */
1296
1297static tree cp_parser_primary_expression
b3445994 1298 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1299static tree cp_parser_id_expression
f3c2dfc6 1300 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1301static tree cp_parser_unqualified_id
f3c2dfc6 1302 (cp_parser *, bool, bool, bool);
a723baf1
MM
1303static tree cp_parser_nested_name_specifier_opt
1304 (cp_parser *, bool, bool, bool);
1305static tree cp_parser_nested_name_specifier
1306 (cp_parser *, bool, bool, bool);
1307static tree cp_parser_class_or_namespace_name
1308 (cp_parser *, bool, bool, bool, bool);
1309static tree cp_parser_postfix_expression
1310 (cp_parser *, bool);
7efa3e22 1311static tree cp_parser_parenthesized_expression_list
39703eb9 1312 (cp_parser *, bool, bool *);
a723baf1 1313static void cp_parser_pseudo_destructor_name
94edc4ab 1314 (cp_parser *, tree *, tree *);
a723baf1
MM
1315static tree cp_parser_unary_expression
1316 (cp_parser *, bool);
1317static enum tree_code cp_parser_unary_operator
94edc4ab 1318 (cp_token *);
a723baf1 1319static tree cp_parser_new_expression
94edc4ab 1320 (cp_parser *);
a723baf1 1321static tree cp_parser_new_placement
94edc4ab 1322 (cp_parser *);
a723baf1 1323static tree cp_parser_new_type_id
94edc4ab 1324 (cp_parser *);
a723baf1 1325static tree cp_parser_new_declarator_opt
94edc4ab 1326 (cp_parser *);
a723baf1 1327static tree cp_parser_direct_new_declarator
94edc4ab 1328 (cp_parser *);
a723baf1 1329static tree cp_parser_new_initializer
94edc4ab 1330 (cp_parser *);
a723baf1 1331static tree cp_parser_delete_expression
94edc4ab 1332 (cp_parser *);
a723baf1
MM
1333static tree cp_parser_cast_expression
1334 (cp_parser *, bool);
1335static tree cp_parser_pm_expression
94edc4ab 1336 (cp_parser *);
a723baf1 1337static tree cp_parser_multiplicative_expression
94edc4ab 1338 (cp_parser *);
a723baf1 1339static tree cp_parser_additive_expression
94edc4ab 1340 (cp_parser *);
a723baf1 1341static tree cp_parser_shift_expression
94edc4ab 1342 (cp_parser *);
a723baf1 1343static tree cp_parser_relational_expression
94edc4ab 1344 (cp_parser *);
a723baf1 1345static tree cp_parser_equality_expression
94edc4ab 1346 (cp_parser *);
a723baf1 1347static tree cp_parser_and_expression
94edc4ab 1348 (cp_parser *);
a723baf1 1349static tree cp_parser_exclusive_or_expression
94edc4ab 1350 (cp_parser *);
a723baf1 1351static tree cp_parser_inclusive_or_expression
94edc4ab 1352 (cp_parser *);
a723baf1 1353static tree cp_parser_logical_and_expression
94edc4ab 1354 (cp_parser *);
a723baf1 1355static tree cp_parser_logical_or_expression
94edc4ab 1356 (cp_parser *);
a723baf1 1357static tree cp_parser_question_colon_clause
94edc4ab 1358 (cp_parser *, tree);
a723baf1 1359static tree cp_parser_assignment_expression
94edc4ab 1360 (cp_parser *);
a723baf1 1361static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1362 (cp_parser *);
a723baf1 1363static tree cp_parser_expression
94edc4ab 1364 (cp_parser *);
a723baf1 1365static tree cp_parser_constant_expression
14d22dd6 1366 (cp_parser *, bool, bool *);
a723baf1
MM
1367
1368/* Statements [gram.stmt.stmt] */
1369
1370static void cp_parser_statement
a5bcc582 1371 (cp_parser *, bool);
a723baf1 1372static tree cp_parser_labeled_statement
a5bcc582 1373 (cp_parser *, bool);
a723baf1 1374static tree cp_parser_expression_statement
a5bcc582 1375 (cp_parser *, bool);
a723baf1 1376static tree cp_parser_compound_statement
a5bcc582 1377 (cp_parser *, bool);
a723baf1 1378static void cp_parser_statement_seq_opt
a5bcc582 1379 (cp_parser *, bool);
a723baf1 1380static tree cp_parser_selection_statement
94edc4ab 1381 (cp_parser *);
a723baf1 1382static tree cp_parser_condition
94edc4ab 1383 (cp_parser *);
a723baf1 1384static tree cp_parser_iteration_statement
94edc4ab 1385 (cp_parser *);
a723baf1 1386static void cp_parser_for_init_statement
94edc4ab 1387 (cp_parser *);
a723baf1 1388static tree cp_parser_jump_statement
94edc4ab 1389 (cp_parser *);
a723baf1 1390static void cp_parser_declaration_statement
94edc4ab 1391 (cp_parser *);
a723baf1
MM
1392
1393static tree cp_parser_implicitly_scoped_statement
94edc4ab 1394 (cp_parser *);
a723baf1 1395static void cp_parser_already_scoped_statement
94edc4ab 1396 (cp_parser *);
a723baf1
MM
1397
1398/* Declarations [gram.dcl.dcl] */
1399
1400static void cp_parser_declaration_seq_opt
94edc4ab 1401 (cp_parser *);
a723baf1 1402static void cp_parser_declaration
94edc4ab 1403 (cp_parser *);
a723baf1 1404static void cp_parser_block_declaration
94edc4ab 1405 (cp_parser *, bool);
a723baf1 1406static void cp_parser_simple_declaration
94edc4ab 1407 (cp_parser *, bool);
a723baf1 1408static tree cp_parser_decl_specifier_seq
560ad596 1409 (cp_parser *, cp_parser_flags, tree *, int *);
a723baf1 1410static tree cp_parser_storage_class_specifier_opt
94edc4ab 1411 (cp_parser *);
a723baf1 1412static tree cp_parser_function_specifier_opt
94edc4ab 1413 (cp_parser *);
a723baf1 1414static tree cp_parser_type_specifier
560ad596 1415 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
a723baf1 1416static tree cp_parser_simple_type_specifier
4b0d3cbe 1417 (cp_parser *, cp_parser_flags, bool);
a723baf1 1418static tree cp_parser_type_name
94edc4ab 1419 (cp_parser *);
a723baf1 1420static tree cp_parser_elaborated_type_specifier
94edc4ab 1421 (cp_parser *, bool, bool);
a723baf1 1422static tree cp_parser_enum_specifier
94edc4ab 1423 (cp_parser *);
a723baf1 1424static void cp_parser_enumerator_list
94edc4ab 1425 (cp_parser *, tree);
a723baf1 1426static void cp_parser_enumerator_definition
94edc4ab 1427 (cp_parser *, tree);
a723baf1 1428static tree cp_parser_namespace_name
94edc4ab 1429 (cp_parser *);
a723baf1 1430static void cp_parser_namespace_definition
94edc4ab 1431 (cp_parser *);
a723baf1 1432static void cp_parser_namespace_body
94edc4ab 1433 (cp_parser *);
a723baf1 1434static tree cp_parser_qualified_namespace_specifier
94edc4ab 1435 (cp_parser *);
a723baf1 1436static void cp_parser_namespace_alias_definition
94edc4ab 1437 (cp_parser *);
a723baf1 1438static void cp_parser_using_declaration
94edc4ab 1439 (cp_parser *);
a723baf1 1440static void cp_parser_using_directive
94edc4ab 1441 (cp_parser *);
a723baf1 1442static void cp_parser_asm_definition
94edc4ab 1443 (cp_parser *);
a723baf1 1444static void cp_parser_linkage_specification
94edc4ab 1445 (cp_parser *);
a723baf1
MM
1446
1447/* Declarators [gram.dcl.decl] */
1448
1449static tree cp_parser_init_declarator
560ad596 1450 (cp_parser *, tree, tree, bool, bool, int, bool *);
a723baf1 1451static tree cp_parser_declarator
7efa3e22 1452 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1453static tree cp_parser_direct_declarator
7efa3e22 1454 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1455static enum tree_code cp_parser_ptr_operator
94edc4ab 1456 (cp_parser *, tree *, tree *);
a723baf1 1457static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1458 (cp_parser *);
a723baf1 1459static tree cp_parser_cv_qualifier_opt
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_declarator_id
94edc4ab 1462 (cp_parser *);
a723baf1 1463static tree cp_parser_type_id
94edc4ab 1464 (cp_parser *);
a723baf1 1465static tree cp_parser_type_specifier_seq
94edc4ab 1466 (cp_parser *);
a723baf1 1467static tree cp_parser_parameter_declaration_clause
94edc4ab 1468 (cp_parser *);
a723baf1 1469static tree cp_parser_parameter_declaration_list
94edc4ab 1470 (cp_parser *);
a723baf1 1471static tree cp_parser_parameter_declaration
94edc4ab 1472 (cp_parser *, bool);
a723baf1 1473static tree cp_parser_function_definition
94edc4ab 1474 (cp_parser *, bool *);
a723baf1
MM
1475static void cp_parser_function_body
1476 (cp_parser *);
1477static tree cp_parser_initializer
39703eb9 1478 (cp_parser *, bool *, bool *);
a723baf1 1479static tree cp_parser_initializer_clause
39703eb9 1480 (cp_parser *, bool *);
a723baf1 1481static tree cp_parser_initializer_list
39703eb9 1482 (cp_parser *, bool *);
a723baf1
MM
1483
1484static bool cp_parser_ctor_initializer_opt_and_function_body
1485 (cp_parser *);
1486
1487/* Classes [gram.class] */
1488
1489static tree cp_parser_class_name
8d241e0b 1490 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1491static tree cp_parser_class_specifier
94edc4ab 1492 (cp_parser *);
a723baf1 1493static tree cp_parser_class_head
94edc4ab 1494 (cp_parser *, bool *);
a723baf1 1495static enum tag_types cp_parser_class_key
94edc4ab 1496 (cp_parser *);
a723baf1 1497static void cp_parser_member_specification_opt
94edc4ab 1498 (cp_parser *);
a723baf1 1499static void cp_parser_member_declaration
94edc4ab 1500 (cp_parser *);
a723baf1 1501static tree cp_parser_pure_specifier
94edc4ab 1502 (cp_parser *);
a723baf1 1503static tree cp_parser_constant_initializer
94edc4ab 1504 (cp_parser *);
a723baf1
MM
1505
1506/* Derived classes [gram.class.derived] */
1507
1508static tree cp_parser_base_clause
94edc4ab 1509 (cp_parser *);
a723baf1 1510static tree cp_parser_base_specifier
94edc4ab 1511 (cp_parser *);
a723baf1
MM
1512
1513/* Special member functions [gram.special] */
1514
1515static tree cp_parser_conversion_function_id
94edc4ab 1516 (cp_parser *);
a723baf1 1517static tree cp_parser_conversion_type_id
94edc4ab 1518 (cp_parser *);
a723baf1 1519static tree cp_parser_conversion_declarator_opt
94edc4ab 1520 (cp_parser *);
a723baf1 1521static bool cp_parser_ctor_initializer_opt
94edc4ab 1522 (cp_parser *);
a723baf1 1523static void cp_parser_mem_initializer_list
94edc4ab 1524 (cp_parser *);
a723baf1 1525static tree cp_parser_mem_initializer
94edc4ab 1526 (cp_parser *);
a723baf1 1527static tree cp_parser_mem_initializer_id
94edc4ab 1528 (cp_parser *);
a723baf1
MM
1529
1530/* Overloading [gram.over] */
1531
1532static tree cp_parser_operator_function_id
94edc4ab 1533 (cp_parser *);
a723baf1 1534static tree cp_parser_operator
94edc4ab 1535 (cp_parser *);
a723baf1
MM
1536
1537/* Templates [gram.temp] */
1538
1539static void cp_parser_template_declaration
94edc4ab 1540 (cp_parser *, bool);
a723baf1 1541static tree cp_parser_template_parameter_list
94edc4ab 1542 (cp_parser *);
a723baf1 1543static tree cp_parser_template_parameter
94edc4ab 1544 (cp_parser *);
a723baf1 1545static tree cp_parser_type_parameter
94edc4ab 1546 (cp_parser *);
a723baf1 1547static tree cp_parser_template_id
94edc4ab 1548 (cp_parser *, bool, bool);
a723baf1 1549static tree cp_parser_template_name
94edc4ab 1550 (cp_parser *, bool, bool);
a723baf1 1551static tree cp_parser_template_argument_list
94edc4ab 1552 (cp_parser *);
a723baf1 1553static tree cp_parser_template_argument
94edc4ab 1554 (cp_parser *);
a723baf1 1555static void cp_parser_explicit_instantiation
94edc4ab 1556 (cp_parser *);
a723baf1 1557static void cp_parser_explicit_specialization
94edc4ab 1558 (cp_parser *);
a723baf1
MM
1559
1560/* Exception handling [gram.exception] */
1561
1562static tree cp_parser_try_block
94edc4ab 1563 (cp_parser *);
a723baf1 1564static bool cp_parser_function_try_block
94edc4ab 1565 (cp_parser *);
a723baf1 1566static void cp_parser_handler_seq
94edc4ab 1567 (cp_parser *);
a723baf1 1568static void cp_parser_handler
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_exception_declaration
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_throw_expression
94edc4ab 1573 (cp_parser *);
a723baf1 1574static tree cp_parser_exception_specification_opt
94edc4ab 1575 (cp_parser *);
a723baf1 1576static tree cp_parser_type_id_list
94edc4ab 1577 (cp_parser *);
a723baf1
MM
1578
1579/* GNU Extensions */
1580
1581static tree cp_parser_asm_specification_opt
94edc4ab 1582 (cp_parser *);
a723baf1 1583static tree cp_parser_asm_operand_list
94edc4ab 1584 (cp_parser *);
a723baf1 1585static tree cp_parser_asm_clobber_list
94edc4ab 1586 (cp_parser *);
a723baf1 1587static tree cp_parser_attributes_opt
94edc4ab 1588 (cp_parser *);
a723baf1 1589static tree cp_parser_attribute_list
94edc4ab 1590 (cp_parser *);
a723baf1 1591static bool cp_parser_extension_opt
94edc4ab 1592 (cp_parser *, int *);
a723baf1 1593static void cp_parser_label_declaration
94edc4ab 1594 (cp_parser *);
a723baf1
MM
1595
1596/* Utility Routines */
1597
1598static tree cp_parser_lookup_name
8d241e0b 1599 (cp_parser *, tree, bool, bool, bool);
a723baf1 1600static tree cp_parser_lookup_name_simple
94edc4ab 1601 (cp_parser *, tree);
a723baf1
MM
1602static tree cp_parser_maybe_treat_template_as_class
1603 (tree, bool);
1604static bool cp_parser_check_declarator_template_parameters
94edc4ab 1605 (cp_parser *, tree);
a723baf1 1606static bool cp_parser_check_template_parameters
94edc4ab 1607 (cp_parser *, unsigned);
d6b4ea85
MM
1608static tree cp_parser_simple_cast_expression
1609 (cp_parser *);
a723baf1 1610static tree cp_parser_binary_expression
94edc4ab 1611 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1612static tree cp_parser_global_scope_opt
94edc4ab 1613 (cp_parser *, bool);
a723baf1
MM
1614static bool cp_parser_constructor_declarator_p
1615 (cp_parser *, bool);
1616static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1617 (cp_parser *, tree, tree, tree);
a723baf1 1618static tree cp_parser_function_definition_after_declarator
94edc4ab 1619 (cp_parser *, bool);
a723baf1 1620static void cp_parser_template_declaration_after_export
94edc4ab 1621 (cp_parser *, bool);
a723baf1 1622static tree cp_parser_single_declaration
94edc4ab 1623 (cp_parser *, bool, bool *);
a723baf1 1624static tree cp_parser_functional_cast
94edc4ab 1625 (cp_parser *, tree);
8db1028e
NS
1626static void cp_parser_save_default_args
1627 (cp_parser *, tree);
a723baf1 1628static void cp_parser_late_parsing_for_member
94edc4ab 1629 (cp_parser *, tree);
a723baf1 1630static void cp_parser_late_parsing_default_args
8218bd34 1631 (cp_parser *, tree);
a723baf1 1632static tree cp_parser_sizeof_operand
94edc4ab 1633 (cp_parser *, enum rid);
a723baf1 1634static bool cp_parser_declares_only_class_p
94edc4ab 1635 (cp_parser *);
d17811fd
MM
1636static tree cp_parser_fold_non_dependent_expr
1637 (tree);
a723baf1 1638static bool cp_parser_friend_p
94edc4ab 1639 (tree);
a723baf1 1640static cp_token *cp_parser_require
94edc4ab 1641 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1642static cp_token *cp_parser_require_keyword
94edc4ab 1643 (cp_parser *, enum rid, const char *);
a723baf1 1644static bool cp_parser_token_starts_function_definition_p
94edc4ab 1645 (cp_token *);
a723baf1
MM
1646static bool cp_parser_next_token_starts_class_definition_p
1647 (cp_parser *);
d17811fd
MM
1648static bool cp_parser_next_token_ends_template_argument_p
1649 (cp_parser *);
a723baf1 1650static enum tag_types cp_parser_token_is_class_key
94edc4ab 1651 (cp_token *);
a723baf1
MM
1652static void cp_parser_check_class_key
1653 (enum tag_types, tree type);
37d407a1
KL
1654static void cp_parser_check_access_in_redeclaration
1655 (tree type);
a723baf1
MM
1656static bool cp_parser_optional_template_keyword
1657 (cp_parser *);
2050a1bb
MM
1658static void cp_parser_pre_parsed_nested_name_specifier
1659 (cp_parser *);
a723baf1
MM
1660static void cp_parser_cache_group
1661 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1662static void cp_parser_parse_tentatively
94edc4ab 1663 (cp_parser *);
a723baf1 1664static void cp_parser_commit_to_tentative_parse
94edc4ab 1665 (cp_parser *);
a723baf1 1666static void cp_parser_abort_tentative_parse
94edc4ab 1667 (cp_parser *);
a723baf1 1668static bool cp_parser_parse_definitely
94edc4ab 1669 (cp_parser *);
f7b5ecd9 1670static inline bool cp_parser_parsing_tentatively
94edc4ab 1671 (cp_parser *);
a723baf1 1672static bool cp_parser_committed_to_tentative_parse
94edc4ab 1673 (cp_parser *);
a723baf1 1674static void cp_parser_error
94edc4ab 1675 (cp_parser *, const char *);
e5976695 1676static bool cp_parser_simulate_error
94edc4ab 1677 (cp_parser *);
a723baf1 1678static void cp_parser_check_type_definition
94edc4ab 1679 (cp_parser *);
560ad596
MM
1680static void cp_parser_check_for_definition_in_return_type
1681 (tree, int);
14d22dd6
MM
1682static tree cp_parser_non_constant_expression
1683 (const char *);
8fbc5ae7
MM
1684static bool cp_parser_diagnose_invalid_type_name
1685 (cp_parser *);
7efa3e22
NS
1686static int cp_parser_skip_to_closing_parenthesis
1687 (cp_parser *, bool, bool);
a723baf1 1688static void cp_parser_skip_to_end_of_statement
94edc4ab 1689 (cp_parser *);
e0860732
MM
1690static void cp_parser_consume_semicolon_at_end_of_statement
1691 (cp_parser *);
a723baf1 1692static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1693 (cp_parser *);
a723baf1
MM
1694static void cp_parser_skip_to_closing_brace
1695 (cp_parser *);
1696static void cp_parser_skip_until_found
94edc4ab 1697 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1698static bool cp_parser_error_occurred
94edc4ab 1699 (cp_parser *);
a723baf1 1700static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1701 (cp_parser *);
a723baf1 1702static bool cp_parser_is_string_literal
94edc4ab 1703 (cp_token *);
a723baf1 1704static bool cp_parser_is_keyword
94edc4ab 1705 (cp_token *, enum rid);
a723baf1 1706
4de8668e 1707/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1708
1709static inline bool
94edc4ab 1710cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1711{
1712 return parser->context->next != NULL;
1713}
1714
4de8668e 1715/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1716
1717static bool
94edc4ab 1718cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1719{
1720 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1721}
1722
4de8668e 1723/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1724
1725static bool
94edc4ab 1726cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1727{
1728 return token->keyword == keyword;
1729}
1730
a723baf1
MM
1731/* Issue the indicated error MESSAGE. */
1732
1733static void
94edc4ab 1734cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1735{
a723baf1 1736 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1737 if (!cp_parser_simulate_error (parser))
a723baf1
MM
1738 error (message);
1739}
1740
1741/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1742 during this tentative parse. Returns true if the error was
1743 simulated; false if a messgae should be issued by the caller. */
a723baf1 1744
e5976695 1745static bool
94edc4ab 1746cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1747{
1748 if (cp_parser_parsing_tentatively (parser)
1749 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1750 {
1751 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1752 return true;
1753 }
1754 return false;
a723baf1
MM
1755}
1756
1757/* This function is called when a type is defined. If type
1758 definitions are forbidden at this point, an error message is
1759 issued. */
1760
1761static void
94edc4ab 1762cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1763{
1764 /* If types are forbidden here, issue a message. */
1765 if (parser->type_definition_forbidden_message)
1766 /* Use `%s' to print the string in case there are any escape
1767 characters in the message. */
1768 error ("%s", parser->type_definition_forbidden_message);
1769}
1770
560ad596
MM
1771/* This function is called when a declaration is parsed. If
1772 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1773 indicates that a type was defined in the decl-specifiers for DECL,
1774 then an error is issued. */
1775
1776static void
1777cp_parser_check_for_definition_in_return_type (tree declarator,
1778 int declares_class_or_enum)
1779{
1780 /* [dcl.fct] forbids type definitions in return types.
1781 Unfortunately, it's not easy to know whether or not we are
1782 processing a return type until after the fact. */
1783 while (declarator
1784 && (TREE_CODE (declarator) == INDIRECT_REF
1785 || TREE_CODE (declarator) == ADDR_EXPR))
1786 declarator = TREE_OPERAND (declarator, 0);
1787 if (declarator
1788 && TREE_CODE (declarator) == CALL_EXPR
1789 && declares_class_or_enum & 2)
1790 error ("new types may not be defined in a return type");
1791}
1792
cd0be382 1793/* Issue an error message about the fact that THING appeared in a
14d22dd6
MM
1794 constant-expression. Returns ERROR_MARK_NODE. */
1795
1796static tree
1797cp_parser_non_constant_expression (const char *thing)
1798{
1799 error ("%s cannot appear in a constant-expression", thing);
1800 return error_mark_node;
1801}
1802
8fbc5ae7
MM
1803/* Check for a common situation where a type-name should be present,
1804 but is not, and issue a sensible error message. Returns true if an
1805 invalid type-name was detected. */
1806
1807static bool
1808cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1809{
1810 /* If the next two tokens are both identifiers, the code is
1811 erroneous. The usual cause of this situation is code like:
1812
1813 T t;
1814
1815 where "T" should name a type -- but does not. */
1816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1817 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1818 {
1819 tree name;
1820
8d241e0b 1821 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1822 looking at a declaration. */
1823 /* Consume the first identifier. */
1824 name = cp_lexer_consume_token (parser->lexer)->value;
1825 /* Issue an error message. */
1826 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1827 /* If we're in a template class, it's possible that the user was
1828 referring to a type from a base class. For example:
1829
1830 template <typename T> struct A { typedef T X; };
1831 template <typename T> struct B : public A<T> { X x; };
1832
1833 The user should have said "typename A<T>::X". */
1834 if (processing_template_decl && current_class_type)
1835 {
1836 tree b;
1837
1838 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1839 b;
1840 b = TREE_CHAIN (b))
1841 {
1842 tree base_type = BINFO_TYPE (b);
1843 if (CLASS_TYPE_P (base_type)
1fb3244a 1844 && dependent_type_p (base_type))
8fbc5ae7
MM
1845 {
1846 tree field;
1847 /* Go from a particular instantiation of the
1848 template (which will have an empty TYPE_FIELDs),
1849 to the main version. */
353b4fc0 1850 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1851 for (field = TYPE_FIELDS (base_type);
1852 field;
1853 field = TREE_CHAIN (field))
1854 if (TREE_CODE (field) == TYPE_DECL
1855 && DECL_NAME (field) == name)
1856 {
1857 error ("(perhaps `typename %T::%s' was intended)",
1858 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1859 break;
1860 }
1861 if (field)
1862 break;
1863 }
1864 }
1865 }
1866 /* Skip to the end of the declaration; there's no point in
1867 trying to process it. */
1868 cp_parser_skip_to_end_of_statement (parser);
1869
1870 return true;
1871 }
1872
1873 return false;
1874}
1875
a723baf1 1876/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
1877 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1878 are doing error recovery. Returns -1 if OR_COMMA is true and we
1879 found an unnested comma. */
a723baf1 1880
7efa3e22
NS
1881static int
1882cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1883 bool recovering, bool or_comma)
a723baf1 1884{
7efa3e22
NS
1885 unsigned paren_depth = 0;
1886 unsigned brace_depth = 0;
a723baf1 1887
7efa3e22
NS
1888 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1889 && !cp_parser_committed_to_tentative_parse (parser))
1890 return 0;
1891
a723baf1
MM
1892 while (true)
1893 {
1894 cp_token *token;
7efa3e22 1895
a723baf1
MM
1896 /* If we've run out of tokens, then there is no closing `)'. */
1897 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 1898 return 0;
a723baf1 1899
7efa3e22
NS
1900 if (recovering)
1901 {
1902 token = cp_lexer_peek_token (parser->lexer);
a723baf1 1903
7efa3e22
NS
1904 /* This matches the processing in skip_to_end_of_statement */
1905 if (token->type == CPP_SEMICOLON && !brace_depth)
1906 return 0;
1907 if (token->type == CPP_OPEN_BRACE)
1908 ++brace_depth;
1909 if (token->type == CPP_CLOSE_BRACE)
1910 {
1911 if (!brace_depth--)
1912 return 0;
1913 }
1914 if (or_comma && token->type == CPP_COMMA
1915 && !brace_depth && !paren_depth)
1916 return -1;
1917 }
1918
a723baf1
MM
1919 /* Consume the token. */
1920 token = cp_lexer_consume_token (parser->lexer);
7efa3e22
NS
1921
1922 if (!brace_depth)
1923 {
1924 /* If it is an `(', we have entered another level of nesting. */
1925 if (token->type == CPP_OPEN_PAREN)
1926 ++paren_depth;
1927 /* If it is a `)', then we might be done. */
1928 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1929 return 1;
1930 }
a723baf1
MM
1931 }
1932}
1933
1934/* Consume tokens until we reach the end of the current statement.
1935 Normally, that will be just before consuming a `;'. However, if a
1936 non-nested `}' comes first, then we stop before consuming that. */
1937
1938static void
94edc4ab 1939cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
1940{
1941 unsigned nesting_depth = 0;
1942
1943 while (true)
1944 {
1945 cp_token *token;
1946
1947 /* Peek at the next token. */
1948 token = cp_lexer_peek_token (parser->lexer);
1949 /* If we've run out of tokens, stop. */
1950 if (token->type == CPP_EOF)
1951 break;
1952 /* If the next token is a `;', we have reached the end of the
1953 statement. */
1954 if (token->type == CPP_SEMICOLON && !nesting_depth)
1955 break;
1956 /* If the next token is a non-nested `}', then we have reached
1957 the end of the current block. */
1958 if (token->type == CPP_CLOSE_BRACE)
1959 {
1960 /* If this is a non-nested `}', stop before consuming it.
1961 That way, when confronted with something like:
1962
1963 { 3 + }
1964
1965 we stop before consuming the closing `}', even though we
1966 have not yet reached a `;'. */
1967 if (nesting_depth == 0)
1968 break;
1969 /* If it is the closing `}' for a block that we have
1970 scanned, stop -- but only after consuming the token.
1971 That way given:
1972
1973 void f g () { ... }
1974 typedef int I;
1975
1976 we will stop after the body of the erroneously declared
1977 function, but before consuming the following `typedef'
1978 declaration. */
1979 if (--nesting_depth == 0)
1980 {
1981 cp_lexer_consume_token (parser->lexer);
1982 break;
1983 }
1984 }
1985 /* If it the next token is a `{', then we are entering a new
1986 block. Consume the entire block. */
1987 else if (token->type == CPP_OPEN_BRACE)
1988 ++nesting_depth;
1989 /* Consume the token. */
1990 cp_lexer_consume_token (parser->lexer);
1991 }
1992}
1993
e0860732
MM
1994/* This function is called at the end of a statement or declaration.
1995 If the next token is a semicolon, it is consumed; otherwise, error
1996 recovery is attempted. */
1997
1998static void
1999cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2000{
2001 /* Look for the trailing `;'. */
2002 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2003 {
2004 /* If there is additional (erroneous) input, skip to the end of
2005 the statement. */
2006 cp_parser_skip_to_end_of_statement (parser);
2007 /* If the next token is now a `;', consume it. */
2008 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2009 cp_lexer_consume_token (parser->lexer);
2010 }
2011}
2012
a723baf1
MM
2013/* Skip tokens until we have consumed an entire block, or until we
2014 have consumed a non-nested `;'. */
2015
2016static void
94edc4ab 2017cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2018{
2019 unsigned nesting_depth = 0;
2020
2021 while (true)
2022 {
2023 cp_token *token;
2024
2025 /* Peek at the next token. */
2026 token = cp_lexer_peek_token (parser->lexer);
2027 /* If we've run out of tokens, stop. */
2028 if (token->type == CPP_EOF)
2029 break;
2030 /* If the next token is a `;', we have reached the end of the
2031 statement. */
2032 if (token->type == CPP_SEMICOLON && !nesting_depth)
2033 {
2034 /* Consume the `;'. */
2035 cp_lexer_consume_token (parser->lexer);
2036 break;
2037 }
2038 /* Consume the token. */
2039 token = cp_lexer_consume_token (parser->lexer);
2040 /* If the next token is a non-nested `}', then we have reached
2041 the end of the current block. */
2042 if (token->type == CPP_CLOSE_BRACE
2043 && (nesting_depth == 0 || --nesting_depth == 0))
2044 break;
2045 /* If it the next token is a `{', then we are entering a new
2046 block. Consume the entire block. */
2047 if (token->type == CPP_OPEN_BRACE)
2048 ++nesting_depth;
2049 }
2050}
2051
2052/* Skip tokens until a non-nested closing curly brace is the next
2053 token. */
2054
2055static void
2056cp_parser_skip_to_closing_brace (cp_parser *parser)
2057{
2058 unsigned nesting_depth = 0;
2059
2060 while (true)
2061 {
2062 cp_token *token;
2063
2064 /* Peek at the next token. */
2065 token = cp_lexer_peek_token (parser->lexer);
2066 /* If we've run out of tokens, stop. */
2067 if (token->type == CPP_EOF)
2068 break;
2069 /* If the next token is a non-nested `}', then we have reached
2070 the end of the current block. */
2071 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2072 break;
2073 /* If it the next token is a `{', then we are entering a new
2074 block. Consume the entire block. */
2075 else if (token->type == CPP_OPEN_BRACE)
2076 ++nesting_depth;
2077 /* Consume the token. */
2078 cp_lexer_consume_token (parser->lexer);
2079 }
2080}
2081
2082/* Create a new C++ parser. */
2083
2084static cp_parser *
94edc4ab 2085cp_parser_new (void)
a723baf1
MM
2086{
2087 cp_parser *parser;
17211ab5
GK
2088 cp_lexer *lexer;
2089
2090 /* cp_lexer_new_main is called before calling ggc_alloc because
2091 cp_lexer_new_main might load a PCH file. */
2092 lexer = cp_lexer_new_main ();
a723baf1 2093
c68b0a84 2094 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2095 parser->lexer = lexer;
a723baf1
MM
2096 parser->context = cp_parser_context_new (NULL);
2097
2098 /* For now, we always accept GNU extensions. */
2099 parser->allow_gnu_extensions_p = 1;
2100
2101 /* The `>' token is a greater-than operator, not the end of a
2102 template-id. */
2103 parser->greater_than_is_operator_p = true;
2104
2105 parser->default_arg_ok_p = true;
2106
2107 /* We are not parsing a constant-expression. */
2108 parser->constant_expression_p = false;
14d22dd6
MM
2109 parser->allow_non_constant_expression_p = false;
2110 parser->non_constant_expression_p = false;
a723baf1
MM
2111
2112 /* Local variable names are not forbidden. */
2113 parser->local_variables_forbidden_p = false;
2114
34cd5ae7 2115 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2116 parser->in_unbraced_linkage_specification_p = false;
2117
2118 /* We are not processing a declarator. */
2119 parser->in_declarator_p = false;
2120
a723baf1
MM
2121 /* The unparsed function queue is empty. */
2122 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2123
2124 /* There are no classes being defined. */
2125 parser->num_classes_being_defined = 0;
2126
2127 /* No template parameters apply. */
2128 parser->num_template_parameter_lists = 0;
2129
2130 return parser;
2131}
2132
2133/* Lexical conventions [gram.lex] */
2134
2135/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2136 identifier. */
2137
2138static tree
94edc4ab 2139cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2140{
2141 cp_token *token;
2142
2143 /* Look for the identifier. */
2144 token = cp_parser_require (parser, CPP_NAME, "identifier");
2145 /* Return the value. */
2146 return token ? token->value : error_mark_node;
2147}
2148
2149/* Basic concepts [gram.basic] */
2150
2151/* Parse a translation-unit.
2152
2153 translation-unit:
2154 declaration-seq [opt]
2155
2156 Returns TRUE if all went well. */
2157
2158static bool
94edc4ab 2159cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2160{
2161 while (true)
2162 {
2163 cp_parser_declaration_seq_opt (parser);
2164
2165 /* If there are no tokens left then all went well. */
2166 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2167 break;
2168
2169 /* Otherwise, issue an error message. */
2170 cp_parser_error (parser, "expected declaration");
2171 return false;
2172 }
2173
2174 /* Consume the EOF token. */
2175 cp_parser_require (parser, CPP_EOF, "end-of-file");
2176
2177 /* Finish up. */
2178 finish_translation_unit ();
2179
2180 /* All went well. */
2181 return true;
2182}
2183
2184/* Expressions [gram.expr] */
2185
2186/* Parse a primary-expression.
2187
2188 primary-expression:
2189 literal
2190 this
2191 ( expression )
2192 id-expression
2193
2194 GNU Extensions:
2195
2196 primary-expression:
2197 ( compound-statement )
2198 __builtin_va_arg ( assignment-expression , type-id )
2199
2200 literal:
2201 __null
2202
2203 Returns a representation of the expression.
2204
2205 *IDK indicates what kind of id-expression (if any) was present.
2206
2207 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2208 used as the operand of a pointer-to-member. In that case,
2209 *QUALIFYING_CLASS gives the class that is used as the qualifying
2210 class in the pointer-to-member. */
2211
2212static tree
2213cp_parser_primary_expression (cp_parser *parser,
b3445994 2214 cp_id_kind *idk,
a723baf1
MM
2215 tree *qualifying_class)
2216{
2217 cp_token *token;
2218
2219 /* Assume the primary expression is not an id-expression. */
b3445994 2220 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2221 /* And that it cannot be used as pointer-to-member. */
2222 *qualifying_class = NULL_TREE;
2223
2224 /* Peek at the next token. */
2225 token = cp_lexer_peek_token (parser->lexer);
2226 switch (token->type)
2227 {
2228 /* literal:
2229 integer-literal
2230 character-literal
2231 floating-literal
2232 string-literal
2233 boolean-literal */
2234 case CPP_CHAR:
2235 case CPP_WCHAR:
2236 case CPP_STRING:
2237 case CPP_WSTRING:
2238 case CPP_NUMBER:
2239 token = cp_lexer_consume_token (parser->lexer);
2240 return token->value;
2241
2242 case CPP_OPEN_PAREN:
2243 {
2244 tree expr;
2245 bool saved_greater_than_is_operator_p;
2246
2247 /* Consume the `('. */
2248 cp_lexer_consume_token (parser->lexer);
2249 /* Within a parenthesized expression, a `>' token is always
2250 the greater-than operator. */
2251 saved_greater_than_is_operator_p
2252 = parser->greater_than_is_operator_p;
2253 parser->greater_than_is_operator_p = true;
2254 /* If we see `( { ' then we are looking at the beginning of
2255 a GNU statement-expression. */
2256 if (cp_parser_allow_gnu_extensions_p (parser)
2257 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2258 {
2259 /* Statement-expressions are not allowed by the standard. */
2260 if (pedantic)
2261 pedwarn ("ISO C++ forbids braced-groups within expressions");
2262
2263 /* And they're not allowed outside of a function-body; you
2264 cannot, for example, write:
2265
2266 int i = ({ int j = 3; j + 1; });
2267
2268 at class or namespace scope. */
2269 if (!at_function_scope_p ())
2270 error ("statement-expressions are allowed only inside functions");
2271 /* Start the statement-expression. */
2272 expr = begin_stmt_expr ();
2273 /* Parse the compound-statement. */
a5bcc582 2274 cp_parser_compound_statement (parser, true);
a723baf1 2275 /* Finish up. */
303b7406 2276 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2277 }
2278 else
2279 {
2280 /* Parse the parenthesized expression. */
2281 expr = cp_parser_expression (parser);
2282 /* Let the front end know that this expression was
2283 enclosed in parentheses. This matters in case, for
2284 example, the expression is of the form `A::B', since
2285 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2286 not. */
2287 finish_parenthesized_expr (expr);
2288 }
2289 /* The `>' token might be the end of a template-id or
2290 template-parameter-list now. */
2291 parser->greater_than_is_operator_p
2292 = saved_greater_than_is_operator_p;
2293 /* Consume the `)'. */
2294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2295 cp_parser_skip_to_end_of_statement (parser);
2296
2297 return expr;
2298 }
2299
2300 case CPP_KEYWORD:
2301 switch (token->keyword)
2302 {
2303 /* These two are the boolean literals. */
2304 case RID_TRUE:
2305 cp_lexer_consume_token (parser->lexer);
2306 return boolean_true_node;
2307 case RID_FALSE:
2308 cp_lexer_consume_token (parser->lexer);
2309 return boolean_false_node;
2310
2311 /* The `__null' literal. */
2312 case RID_NULL:
2313 cp_lexer_consume_token (parser->lexer);
2314 return null_node;
2315
2316 /* Recognize the `this' keyword. */
2317 case RID_THIS:
2318 cp_lexer_consume_token (parser->lexer);
2319 if (parser->local_variables_forbidden_p)
2320 {
2321 error ("`this' may not be used in this context");
2322 return error_mark_node;
2323 }
14d22dd6
MM
2324 /* Pointers cannot appear in constant-expressions. */
2325 if (parser->constant_expression_p)
2326 {
2327 if (!parser->allow_non_constant_expression_p)
2328 return cp_parser_non_constant_expression ("`this'");
2329 parser->non_constant_expression_p = true;
2330 }
a723baf1
MM
2331 return finish_this_expr ();
2332
2333 /* The `operator' keyword can be the beginning of an
2334 id-expression. */
2335 case RID_OPERATOR:
2336 goto id_expression;
2337
2338 case RID_FUNCTION_NAME:
2339 case RID_PRETTY_FUNCTION_NAME:
2340 case RID_C99_FUNCTION_NAME:
2341 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2342 __func__ are the names of variables -- but they are
2343 treated specially. Therefore, they are handled here,
2344 rather than relying on the generic id-expression logic
34cd5ae7 2345 below. Grammatically, these names are id-expressions.
a723baf1
MM
2346
2347 Consume the token. */
2348 token = cp_lexer_consume_token (parser->lexer);
2349 /* Look up the name. */
2350 return finish_fname (token->value);
2351
2352 case RID_VA_ARG:
2353 {
2354 tree expression;
2355 tree type;
2356
2357 /* The `__builtin_va_arg' construct is used to handle
2358 `va_arg'. Consume the `__builtin_va_arg' token. */
2359 cp_lexer_consume_token (parser->lexer);
2360 /* Look for the opening `('. */
2361 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2362 /* Now, parse the assignment-expression. */
2363 expression = cp_parser_assignment_expression (parser);
2364 /* Look for the `,'. */
2365 cp_parser_require (parser, CPP_COMMA, "`,'");
2366 /* Parse the type-id. */
2367 type = cp_parser_type_id (parser);
2368 /* Look for the closing `)'. */
2369 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2370 /* Using `va_arg' in a constant-expression is not
2371 allowed. */
2372 if (parser->constant_expression_p)
2373 {
2374 if (!parser->allow_non_constant_expression_p)
2375 return cp_parser_non_constant_expression ("`va_arg'");
2376 parser->non_constant_expression_p = true;
2377 }
a723baf1
MM
2378 return build_x_va_arg (expression, type);
2379 }
2380
2381 default:
2382 cp_parser_error (parser, "expected primary-expression");
2383 return error_mark_node;
2384 }
a723baf1
MM
2385
2386 /* An id-expression can start with either an identifier, a
2387 `::' as the beginning of a qualified-id, or the "operator"
2388 keyword. */
2389 case CPP_NAME:
2390 case CPP_SCOPE:
2391 case CPP_TEMPLATE_ID:
2392 case CPP_NESTED_NAME_SPECIFIER:
2393 {
2394 tree id_expression;
2395 tree decl;
b3445994 2396 const char *error_msg;
a723baf1
MM
2397
2398 id_expression:
2399 /* Parse the id-expression. */
2400 id_expression
2401 = cp_parser_id_expression (parser,
2402 /*template_keyword_p=*/false,
2403 /*check_dependency_p=*/true,
f3c2dfc6
MM
2404 /*template_p=*/NULL,
2405 /*declarator_p=*/false);
a723baf1
MM
2406 if (id_expression == error_mark_node)
2407 return error_mark_node;
2408 /* If we have a template-id, then no further lookup is
2409 required. If the template-id was for a template-class, we
2410 will sometimes have a TYPE_DECL at this point. */
2411 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2412 || TREE_CODE (id_expression) == TYPE_DECL)
2413 decl = id_expression;
2414 /* Look up the name. */
2415 else
2416 {
2417 decl = cp_parser_lookup_name_simple (parser, id_expression);
2418 /* If name lookup gives us a SCOPE_REF, then the
2419 qualifying scope was dependent. Just propagate the
2420 name. */
2421 if (TREE_CODE (decl) == SCOPE_REF)
2422 {
2423 if (TYPE_P (TREE_OPERAND (decl, 0)))
2424 *qualifying_class = TREE_OPERAND (decl, 0);
2425 return decl;
2426 }
2427 /* Check to see if DECL is a local variable in a context
2428 where that is forbidden. */
2429 if (parser->local_variables_forbidden_p
2430 && local_variable_p (decl))
2431 {
2432 /* It might be that we only found DECL because we are
2433 trying to be generous with pre-ISO scoping rules.
2434 For example, consider:
2435
2436 int i;
2437 void g() {
2438 for (int i = 0; i < 10; ++i) {}
2439 extern void f(int j = i);
2440 }
2441
2442 Here, name look up will originally find the out
2443 of scope `i'. We need to issue a warning message,
2444 but then use the global `i'. */
2445 decl = check_for_out_of_scope_variable (decl);
2446 if (local_variable_p (decl))
2447 {
2448 error ("local variable `%D' may not appear in this context",
2449 decl);
2450 return error_mark_node;
2451 }
2452 }
c006d942 2453 }
b3445994
MM
2454
2455 decl = finish_id_expression (id_expression, decl, parser->scope,
2456 idk, qualifying_class,
2457 parser->constant_expression_p,
2458 parser->allow_non_constant_expression_p,
2459 &parser->non_constant_expression_p,
2460 &error_msg);
2461 if (error_msg)
2462 cp_parser_error (parser, error_msg);
a723baf1
MM
2463 return decl;
2464 }
2465
2466 /* Anything else is an error. */
2467 default:
2468 cp_parser_error (parser, "expected primary-expression");
2469 return error_mark_node;
2470 }
2471}
2472
2473/* Parse an id-expression.
2474
2475 id-expression:
2476 unqualified-id
2477 qualified-id
2478
2479 qualified-id:
2480 :: [opt] nested-name-specifier template [opt] unqualified-id
2481 :: identifier
2482 :: operator-function-id
2483 :: template-id
2484
2485 Return a representation of the unqualified portion of the
2486 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2487 a `::' or nested-name-specifier.
2488
2489 Often, if the id-expression was a qualified-id, the caller will
2490 want to make a SCOPE_REF to represent the qualified-id. This
2491 function does not do this in order to avoid wastefully creating
2492 SCOPE_REFs when they are not required.
2493
a723baf1
MM
2494 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2495 `template' keyword.
2496
2497 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2498 uninstantiated templates.
2499
15d2cb19 2500 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2501 `template' keyword is used to explicitly indicate that the entity
f3c2dfc6
MM
2502 named is a template.
2503
2504 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2505 a declarator, rather than as part of an expression. */
a723baf1
MM
2506
2507static tree
2508cp_parser_id_expression (cp_parser *parser,
2509 bool template_keyword_p,
2510 bool check_dependency_p,
f3c2dfc6
MM
2511 bool *template_p,
2512 bool declarator_p)
a723baf1
MM
2513{
2514 bool global_scope_p;
2515 bool nested_name_specifier_p;
2516
2517 /* Assume the `template' keyword was not used. */
2518 if (template_p)
2519 *template_p = false;
2520
2521 /* Look for the optional `::' operator. */
2522 global_scope_p
2523 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2524 != NULL_TREE);
2525 /* Look for the optional nested-name-specifier. */
2526 nested_name_specifier_p
2527 = (cp_parser_nested_name_specifier_opt (parser,
2528 /*typename_keyword_p=*/false,
2529 check_dependency_p,
2530 /*type_p=*/false)
2531 != NULL_TREE);
2532 /* If there is a nested-name-specifier, then we are looking at
2533 the first qualified-id production. */
2534 if (nested_name_specifier_p)
2535 {
2536 tree saved_scope;
2537 tree saved_object_scope;
2538 tree saved_qualifying_scope;
2539 tree unqualified_id;
2540 bool is_template;
2541
2542 /* See if the next token is the `template' keyword. */
2543 if (!template_p)
2544 template_p = &is_template;
2545 *template_p = cp_parser_optional_template_keyword (parser);
2546 /* Name lookup we do during the processing of the
2547 unqualified-id might obliterate SCOPE. */
2548 saved_scope = parser->scope;
2549 saved_object_scope = parser->object_scope;
2550 saved_qualifying_scope = parser->qualifying_scope;
2551 /* Process the final unqualified-id. */
2552 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2553 check_dependency_p,
2554 declarator_p);
a723baf1
MM
2555 /* Restore the SAVED_SCOPE for our caller. */
2556 parser->scope = saved_scope;
2557 parser->object_scope = saved_object_scope;
2558 parser->qualifying_scope = saved_qualifying_scope;
2559
2560 return unqualified_id;
2561 }
2562 /* Otherwise, if we are in global scope, then we are looking at one
2563 of the other qualified-id productions. */
2564 else if (global_scope_p)
2565 {
2566 cp_token *token;
2567 tree id;
2568
e5976695
MM
2569 /* Peek at the next token. */
2570 token = cp_lexer_peek_token (parser->lexer);
2571
2572 /* If it's an identifier, and the next token is not a "<", then
2573 we can avoid the template-id case. This is an optimization
2574 for this common case. */
2575 if (token->type == CPP_NAME
2576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2577 return cp_parser_identifier (parser);
2578
a723baf1
MM
2579 cp_parser_parse_tentatively (parser);
2580 /* Try a template-id. */
2581 id = cp_parser_template_id (parser,
2582 /*template_keyword_p=*/false,
2583 /*check_dependency_p=*/true);
2584 /* If that worked, we're done. */
2585 if (cp_parser_parse_definitely (parser))
2586 return id;
2587
e5976695
MM
2588 /* Peek at the next token. (Changes in the token buffer may
2589 have invalidated the pointer obtained above.) */
a723baf1
MM
2590 token = cp_lexer_peek_token (parser->lexer);
2591
2592 switch (token->type)
2593 {
2594 case CPP_NAME:
2595 return cp_parser_identifier (parser);
2596
2597 case CPP_KEYWORD:
2598 if (token->keyword == RID_OPERATOR)
2599 return cp_parser_operator_function_id (parser);
2600 /* Fall through. */
2601
2602 default:
2603 cp_parser_error (parser, "expected id-expression");
2604 return error_mark_node;
2605 }
2606 }
2607 else
2608 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
2609 /*check_dependency_p=*/true,
2610 declarator_p);
a723baf1
MM
2611}
2612
2613/* Parse an unqualified-id.
2614
2615 unqualified-id:
2616 identifier
2617 operator-function-id
2618 conversion-function-id
2619 ~ class-name
2620 template-id
2621
2622 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2623 keyword, in a construct like `A::template ...'.
2624
2625 Returns a representation of unqualified-id. For the `identifier'
2626 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2627 production a BIT_NOT_EXPR is returned; the operand of the
2628 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2629 other productions, see the documentation accompanying the
2630 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
2631 names are looked up in uninstantiated templates. If DECLARATOR_P
2632 is true, the unqualified-id is appearing as part of a declarator,
2633 rather than as part of an expression. */
a723baf1
MM
2634
2635static tree
94edc4ab
NN
2636cp_parser_unqualified_id (cp_parser* parser,
2637 bool template_keyword_p,
f3c2dfc6
MM
2638 bool check_dependency_p,
2639 bool declarator_p)
a723baf1
MM
2640{
2641 cp_token *token;
2642
2643 /* Peek at the next token. */
2644 token = cp_lexer_peek_token (parser->lexer);
2645
2646 switch (token->type)
2647 {
2648 case CPP_NAME:
2649 {
2650 tree id;
2651
2652 /* We don't know yet whether or not this will be a
2653 template-id. */
2654 cp_parser_parse_tentatively (parser);
2655 /* Try a template-id. */
2656 id = cp_parser_template_id (parser, template_keyword_p,
2657 check_dependency_p);
2658 /* If it worked, we're done. */
2659 if (cp_parser_parse_definitely (parser))
2660 return id;
2661 /* Otherwise, it's an ordinary identifier. */
2662 return cp_parser_identifier (parser);
2663 }
2664
2665 case CPP_TEMPLATE_ID:
2666 return cp_parser_template_id (parser, template_keyword_p,
2667 check_dependency_p);
2668
2669 case CPP_COMPL:
2670 {
2671 tree type_decl;
2672 tree qualifying_scope;
2673 tree object_scope;
2674 tree scope;
2675
2676 /* Consume the `~' token. */
2677 cp_lexer_consume_token (parser->lexer);
2678 /* Parse the class-name. The standard, as written, seems to
2679 say that:
2680
2681 template <typename T> struct S { ~S (); };
2682 template <typename T> S<T>::~S() {}
2683
2684 is invalid, since `~' must be followed by a class-name, but
2685 `S<T>' is dependent, and so not known to be a class.
2686 That's not right; we need to look in uninstantiated
2687 templates. A further complication arises from:
2688
2689 template <typename T> void f(T t) {
2690 t.T::~T();
2691 }
2692
2693 Here, it is not possible to look up `T' in the scope of `T'
2694 itself. We must look in both the current scope, and the
2695 scope of the containing complete expression.
2696
2697 Yet another issue is:
2698
2699 struct S {
2700 int S;
2701 ~S();
2702 };
2703
2704 S::~S() {}
2705
2706 The standard does not seem to say that the `S' in `~S'
2707 should refer to the type `S' and not the data member
2708 `S::S'. */
2709
2710 /* DR 244 says that we look up the name after the "~" in the
2711 same scope as we looked up the qualifying name. That idea
2712 isn't fully worked out; it's more complicated than that. */
2713 scope = parser->scope;
2714 object_scope = parser->object_scope;
2715 qualifying_scope = parser->qualifying_scope;
2716
2717 /* If the name is of the form "X::~X" it's OK. */
2718 if (scope && TYPE_P (scope)
2719 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2720 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2721 == CPP_OPEN_PAREN)
2722 && (cp_lexer_peek_token (parser->lexer)->value
2723 == TYPE_IDENTIFIER (scope)))
2724 {
2725 cp_lexer_consume_token (parser->lexer);
2726 return build_nt (BIT_NOT_EXPR, scope);
2727 }
2728
2729 /* If there was an explicit qualification (S::~T), first look
2730 in the scope given by the qualification (i.e., S). */
2731 if (scope)
2732 {
2733 cp_parser_parse_tentatively (parser);
2734 type_decl = cp_parser_class_name (parser,
2735 /*typename_keyword_p=*/false,
2736 /*template_keyword_p=*/false,
2737 /*type_p=*/false,
a723baf1
MM
2738 /*check_dependency=*/false,
2739 /*class_head_p=*/false);
2740 if (cp_parser_parse_definitely (parser))
2741 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2742 }
2743 /* In "N::S::~S", look in "N" as well. */
2744 if (scope && qualifying_scope)
2745 {
2746 cp_parser_parse_tentatively (parser);
2747 parser->scope = qualifying_scope;
2748 parser->object_scope = NULL_TREE;
2749 parser->qualifying_scope = NULL_TREE;
2750 type_decl
2751 = cp_parser_class_name (parser,
2752 /*typename_keyword_p=*/false,
2753 /*template_keyword_p=*/false,
2754 /*type_p=*/false,
a723baf1
MM
2755 /*check_dependency=*/false,
2756 /*class_head_p=*/false);
2757 if (cp_parser_parse_definitely (parser))
2758 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2759 }
2760 /* In "p->S::~T", look in the scope given by "*p" as well. */
2761 else if (object_scope)
2762 {
2763 cp_parser_parse_tentatively (parser);
2764 parser->scope = object_scope;
2765 parser->object_scope = NULL_TREE;
2766 parser->qualifying_scope = NULL_TREE;
2767 type_decl
2768 = cp_parser_class_name (parser,
2769 /*typename_keyword_p=*/false,
2770 /*template_keyword_p=*/false,
2771 /*type_p=*/false,
a723baf1
MM
2772 /*check_dependency=*/false,
2773 /*class_head_p=*/false);
2774 if (cp_parser_parse_definitely (parser))
2775 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2776 }
2777 /* Look in the surrounding context. */
2778 parser->scope = NULL_TREE;
2779 parser->object_scope = NULL_TREE;
2780 parser->qualifying_scope = NULL_TREE;
2781 type_decl
2782 = cp_parser_class_name (parser,
2783 /*typename_keyword_p=*/false,
2784 /*template_keyword_p=*/false,
2785 /*type_p=*/false,
a723baf1
MM
2786 /*check_dependency=*/false,
2787 /*class_head_p=*/false);
2788 /* If an error occurred, assume that the name of the
2789 destructor is the same as the name of the qualifying
2790 class. That allows us to keep parsing after running
2791 into ill-formed destructor names. */
2792 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2793 return build_nt (BIT_NOT_EXPR, scope);
2794 else if (type_decl == error_mark_node)
2795 return error_mark_node;
2796
f3c2dfc6
MM
2797 /* [class.dtor]
2798
2799 A typedef-name that names a class shall not be used as the
2800 identifier in the declarator for a destructor declaration. */
2801 if (declarator_p
2802 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2803 && !DECL_SELF_REFERENCE_P (type_decl))
2804 error ("typedef-name `%D' used as destructor declarator",
2805 type_decl);
2806
a723baf1
MM
2807 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2808 }
2809
2810 case CPP_KEYWORD:
2811 if (token->keyword == RID_OPERATOR)
2812 {
2813 tree id;
2814
2815 /* This could be a template-id, so we try that first. */
2816 cp_parser_parse_tentatively (parser);
2817 /* Try a template-id. */
2818 id = cp_parser_template_id (parser, template_keyword_p,
2819 /*check_dependency_p=*/true);
2820 /* If that worked, we're done. */
2821 if (cp_parser_parse_definitely (parser))
2822 return id;
2823 /* We still don't know whether we're looking at an
2824 operator-function-id or a conversion-function-id. */
2825 cp_parser_parse_tentatively (parser);
2826 /* Try an operator-function-id. */
2827 id = cp_parser_operator_function_id (parser);
2828 /* If that didn't work, try a conversion-function-id. */
2829 if (!cp_parser_parse_definitely (parser))
2830 id = cp_parser_conversion_function_id (parser);
2831
2832 return id;
2833 }
2834 /* Fall through. */
2835
2836 default:
2837 cp_parser_error (parser, "expected unqualified-id");
2838 return error_mark_node;
2839 }
2840}
2841
2842/* Parse an (optional) nested-name-specifier.
2843
2844 nested-name-specifier:
2845 class-or-namespace-name :: nested-name-specifier [opt]
2846 class-or-namespace-name :: template nested-name-specifier [opt]
2847
2848 PARSER->SCOPE should be set appropriately before this function is
2849 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2850 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2851 in name lookups.
2852
2853 Sets PARSER->SCOPE to the class (TYPE) or namespace
2854 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2855 it unchanged if there is no nested-name-specifier. Returns the new
2856 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2857
2858static tree
2859cp_parser_nested_name_specifier_opt (cp_parser *parser,
2860 bool typename_keyword_p,
2861 bool check_dependency_p,
2862 bool type_p)
2863{
2864 bool success = false;
2865 tree access_check = NULL_TREE;
2866 ptrdiff_t start;
2050a1bb 2867 cp_token* token;
a723baf1
MM
2868
2869 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
2870 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2871 false, it may have been true before, in which case something
2872 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2873 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2874 CHECK_DEPENDENCY_P is false, we have to fall through into the
2875 main loop. */
2876 if (check_dependency_p
2877 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2878 {
2879 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
2880 return parser->scope;
2881 }
2882
2883 /* Remember where the nested-name-specifier starts. */
2884 if (cp_parser_parsing_tentatively (parser)
2885 && !cp_parser_committed_to_tentative_parse (parser))
2886 {
2050a1bb 2887 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
2888 start = cp_lexer_token_difference (parser->lexer,
2889 parser->lexer->first_token,
2050a1bb 2890 token);
a723baf1
MM
2891 }
2892 else
2893 start = -1;
2894
8d241e0b 2895 push_deferring_access_checks (dk_deferred);
cf22909c 2896
a723baf1
MM
2897 while (true)
2898 {
2899 tree new_scope;
2900 tree old_scope;
2901 tree saved_qualifying_scope;
a723baf1
MM
2902 bool template_keyword_p;
2903
2050a1bb
MM
2904 /* Spot cases that cannot be the beginning of a
2905 nested-name-specifier. */
2906 token = cp_lexer_peek_token (parser->lexer);
2907
2908 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2909 the already parsed nested-name-specifier. */
2910 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2911 {
2912 /* Grab the nested-name-specifier and continue the loop. */
2913 cp_parser_pre_parsed_nested_name_specifier (parser);
2914 success = true;
2915 continue;
2916 }
2917
a723baf1
MM
2918 /* Spot cases that cannot be the beginning of a
2919 nested-name-specifier. On the second and subsequent times
2920 through the loop, we look for the `template' keyword. */
f7b5ecd9 2921 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
2922 ;
2923 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 2924 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
2925 ;
2926 else
2927 {
2928 /* If the next token is not an identifier, then it is
2929 definitely not a class-or-namespace-name. */
f7b5ecd9 2930 if (token->type != CPP_NAME)
a723baf1
MM
2931 break;
2932 /* If the following token is neither a `<' (to begin a
2933 template-id), nor a `::', then we are not looking at a
2934 nested-name-specifier. */
2935 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2936 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2937 break;
2938 }
2939
2940 /* The nested-name-specifier is optional, so we parse
2941 tentatively. */
2942 cp_parser_parse_tentatively (parser);
2943
2944 /* Look for the optional `template' keyword, if this isn't the
2945 first time through the loop. */
2946 if (success)
2947 template_keyword_p = cp_parser_optional_template_keyword (parser);
2948 else
2949 template_keyword_p = false;
2950
2951 /* Save the old scope since the name lookup we are about to do
2952 might destroy it. */
2953 old_scope = parser->scope;
2954 saved_qualifying_scope = parser->qualifying_scope;
2955 /* Parse the qualifying entity. */
2956 new_scope
2957 = cp_parser_class_or_namespace_name (parser,
2958 typename_keyword_p,
2959 template_keyword_p,
2960 check_dependency_p,
2961 type_p);
2962 /* Look for the `::' token. */
2963 cp_parser_require (parser, CPP_SCOPE, "`::'");
2964
2965 /* If we found what we wanted, we keep going; otherwise, we're
2966 done. */
2967 if (!cp_parser_parse_definitely (parser))
2968 {
2969 bool error_p = false;
2970
2971 /* Restore the OLD_SCOPE since it was valid before the
2972 failed attempt at finding the last
2973 class-or-namespace-name. */
2974 parser->scope = old_scope;
2975 parser->qualifying_scope = saved_qualifying_scope;
2976 /* If the next token is an identifier, and the one after
2977 that is a `::', then any valid interpretation would have
2978 found a class-or-namespace-name. */
2979 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2980 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2981 == CPP_SCOPE)
2982 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2983 != CPP_COMPL))
2984 {
2985 token = cp_lexer_consume_token (parser->lexer);
2986 if (!error_p)
2987 {
2988 tree decl;
2989
2990 decl = cp_parser_lookup_name_simple (parser, token->value);
2991 if (TREE_CODE (decl) == TEMPLATE_DECL)
2992 error ("`%D' used without template parameters",
2993 decl);
2994 else if (parser->scope)
2995 {
2996 if (TYPE_P (parser->scope))
2997 error ("`%T::%D' is not a class-name or "
2998 "namespace-name",
2999 parser->scope, token->value);
9075a305
KL
3000 else if (parser->scope == global_namespace)
3001 error ("`::%D' is not a class-name or "
3002 "namespace-name",
3003 token->value);
a723baf1
MM
3004 else
3005 error ("`%D::%D' is not a class-name or "
3006 "namespace-name",
3007 parser->scope, token->value);
3008 }
3009 else
3010 error ("`%D' is not a class-name or namespace-name",
3011 token->value);
3012 parser->scope = NULL_TREE;
3013 error_p = true;
eea9800f
MM
3014 /* Treat this as a successful nested-name-specifier
3015 due to:
3016
3017 [basic.lookup.qual]
3018
3019 If the name found is not a class-name (clause
3020 _class_) or namespace-name (_namespace.def_), the
3021 program is ill-formed. */
3022 success = true;
a723baf1
MM
3023 }
3024 cp_lexer_consume_token (parser->lexer);
3025 }
3026 break;
3027 }
3028
3029 /* We've found one valid nested-name-specifier. */
3030 success = true;
3031 /* Make sure we look in the right scope the next time through
3032 the loop. */
3033 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3034 ? TREE_TYPE (new_scope)
3035 : new_scope);
3036 /* If it is a class scope, try to complete it; we are about to
3037 be looking up names inside the class. */
8fbc5ae7
MM
3038 if (TYPE_P (parser->scope)
3039 /* Since checking types for dependency can be expensive,
3040 avoid doing it if the type is already complete. */
3041 && !COMPLETE_TYPE_P (parser->scope)
3042 /* Do not try to complete dependent types. */
1fb3244a 3043 && !dependent_type_p (parser->scope))
a723baf1
MM
3044 complete_type (parser->scope);
3045 }
3046
cf22909c
KL
3047 /* Retrieve any deferred checks. Do not pop this access checks yet
3048 so the memory will not be reclaimed during token replacing below. */
3049 access_check = get_deferred_access_checks ();
3050
a723baf1
MM
3051 /* If parsing tentatively, replace the sequence of tokens that makes
3052 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3053 token. That way, should we re-parse the token stream, we will
3054 not have to repeat the effort required to do the parse, nor will
3055 we issue duplicate error messages. */
3056 if (success && start >= 0)
3057 {
a723baf1
MM
3058 /* Find the token that corresponds to the start of the
3059 template-id. */
3060 token = cp_lexer_advance_token (parser->lexer,
3061 parser->lexer->first_token,
3062 start);
3063
a723baf1
MM
3064 /* Reset the contents of the START token. */
3065 token->type = CPP_NESTED_NAME_SPECIFIER;
3066 token->value = build_tree_list (access_check, parser->scope);
3067 TREE_TYPE (token->value) = parser->qualifying_scope;
3068 token->keyword = RID_MAX;
3069 /* Purge all subsequent tokens. */
3070 cp_lexer_purge_tokens_after (parser->lexer, token);
3071 }
3072
cf22909c 3073 pop_deferring_access_checks ();
a723baf1
MM
3074 return success ? parser->scope : NULL_TREE;
3075}
3076
3077/* Parse a nested-name-specifier. See
3078 cp_parser_nested_name_specifier_opt for details. This function
3079 behaves identically, except that it will an issue an error if no
3080 nested-name-specifier is present, and it will return
3081 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3082 is present. */
3083
3084static tree
3085cp_parser_nested_name_specifier (cp_parser *parser,
3086 bool typename_keyword_p,
3087 bool check_dependency_p,
3088 bool type_p)
3089{
3090 tree scope;
3091
3092 /* Look for the nested-name-specifier. */
3093 scope = cp_parser_nested_name_specifier_opt (parser,
3094 typename_keyword_p,
3095 check_dependency_p,
3096 type_p);
3097 /* If it was not present, issue an error message. */
3098 if (!scope)
3099 {
3100 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3101 parser->scope = NULL_TREE;
a723baf1
MM
3102 return error_mark_node;
3103 }
3104
3105 return scope;
3106}
3107
3108/* Parse a class-or-namespace-name.
3109
3110 class-or-namespace-name:
3111 class-name
3112 namespace-name
3113
3114 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3115 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3116 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3117 TYPE_P is TRUE iff the next name should be taken as a class-name,
3118 even the same name is declared to be another entity in the same
3119 scope.
3120
3121 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3122 specified by the class-or-namespace-name. If neither is found the
3123 ERROR_MARK_NODE is returned. */
a723baf1
MM
3124
3125static tree
3126cp_parser_class_or_namespace_name (cp_parser *parser,
3127 bool typename_keyword_p,
3128 bool template_keyword_p,
3129 bool check_dependency_p,
3130 bool type_p)
3131{
3132 tree saved_scope;
3133 tree saved_qualifying_scope;
3134 tree saved_object_scope;
3135 tree scope;
eea9800f 3136 bool only_class_p;
a723baf1 3137
a723baf1
MM
3138 /* Before we try to parse the class-name, we must save away the
3139 current PARSER->SCOPE since cp_parser_class_name will destroy
3140 it. */
3141 saved_scope = parser->scope;
3142 saved_qualifying_scope = parser->qualifying_scope;
3143 saved_object_scope = parser->object_scope;
eea9800f
MM
3144 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3145 there is no need to look for a namespace-name. */
bbaab916 3146 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3147 if (!only_class_p)
3148 cp_parser_parse_tentatively (parser);
a723baf1
MM
3149 scope = cp_parser_class_name (parser,
3150 typename_keyword_p,
3151 template_keyword_p,
3152 type_p,
a723baf1
MM
3153 check_dependency_p,
3154 /*class_head_p=*/false);
3155 /* If that didn't work, try for a namespace-name. */
eea9800f 3156 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3157 {
3158 /* Restore the saved scope. */
3159 parser->scope = saved_scope;
3160 parser->qualifying_scope = saved_qualifying_scope;
3161 parser->object_scope = saved_object_scope;
eea9800f
MM
3162 /* If we are not looking at an identifier followed by the scope
3163 resolution operator, then this is not part of a
3164 nested-name-specifier. (Note that this function is only used
3165 to parse the components of a nested-name-specifier.) */
3166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3167 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3168 return error_mark_node;
a723baf1
MM
3169 scope = cp_parser_namespace_name (parser);
3170 }
3171
3172 return scope;
3173}
3174
3175/* Parse a postfix-expression.
3176
3177 postfix-expression:
3178 primary-expression
3179 postfix-expression [ expression ]
3180 postfix-expression ( expression-list [opt] )
3181 simple-type-specifier ( expression-list [opt] )
3182 typename :: [opt] nested-name-specifier identifier
3183 ( expression-list [opt] )
3184 typename :: [opt] nested-name-specifier template [opt] template-id
3185 ( expression-list [opt] )
3186 postfix-expression . template [opt] id-expression
3187 postfix-expression -> template [opt] id-expression
3188 postfix-expression . pseudo-destructor-name
3189 postfix-expression -> pseudo-destructor-name
3190 postfix-expression ++
3191 postfix-expression --
3192 dynamic_cast < type-id > ( expression )
3193 static_cast < type-id > ( expression )
3194 reinterpret_cast < type-id > ( expression )
3195 const_cast < type-id > ( expression )
3196 typeid ( expression )
3197 typeid ( type-id )
3198
3199 GNU Extension:
3200
3201 postfix-expression:
3202 ( type-id ) { initializer-list , [opt] }
3203
3204 This extension is a GNU version of the C99 compound-literal
3205 construct. (The C99 grammar uses `type-name' instead of `type-id',
3206 but they are essentially the same concept.)
3207
3208 If ADDRESS_P is true, the postfix expression is the operand of the
3209 `&' operator.
3210
3211 Returns a representation of the expression. */
3212
3213static tree
3214cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3215{
3216 cp_token *token;
3217 enum rid keyword;
b3445994 3218 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3219 tree postfix_expression = NULL_TREE;
3220 /* Non-NULL only if the current postfix-expression can be used to
3221 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3222 class used to qualify the member. */
3223 tree qualifying_class = NULL_TREE;
a723baf1
MM
3224
3225 /* Peek at the next token. */
3226 token = cp_lexer_peek_token (parser->lexer);
3227 /* Some of the productions are determined by keywords. */
3228 keyword = token->keyword;
3229 switch (keyword)
3230 {
3231 case RID_DYNCAST:
3232 case RID_STATCAST:
3233 case RID_REINTCAST:
3234 case RID_CONSTCAST:
3235 {
3236 tree type;
3237 tree expression;
3238 const char *saved_message;
3239
3240 /* All of these can be handled in the same way from the point
3241 of view of parsing. Begin by consuming the token
3242 identifying the cast. */
3243 cp_lexer_consume_token (parser->lexer);
3244
3245 /* New types cannot be defined in the cast. */
3246 saved_message = parser->type_definition_forbidden_message;
3247 parser->type_definition_forbidden_message
3248 = "types may not be defined in casts";
3249
3250 /* Look for the opening `<'. */
3251 cp_parser_require (parser, CPP_LESS, "`<'");
3252 /* Parse the type to which we are casting. */
3253 type = cp_parser_type_id (parser);
3254 /* Look for the closing `>'. */
3255 cp_parser_require (parser, CPP_GREATER, "`>'");
3256 /* Restore the old message. */
3257 parser->type_definition_forbidden_message = saved_message;
3258
3259 /* And the expression which is being cast. */
3260 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3261 expression = cp_parser_expression (parser);
3262 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3263
14d22dd6
MM
3264 /* Only type conversions to integral or enumeration types
3265 can be used in constant-expressions. */
3266 if (parser->constant_expression_p
3267 && !dependent_type_p (type)
3268 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3269 {
3270 if (!parser->allow_non_constant_expression_p)
3271 return (cp_parser_non_constant_expression
3272 ("a cast to a type other than an integral or "
3273 "enumeration type"));
3274 parser->non_constant_expression_p = true;
3275 }
3276
a723baf1
MM
3277 switch (keyword)
3278 {
3279 case RID_DYNCAST:
3280 postfix_expression
3281 = build_dynamic_cast (type, expression);
3282 break;
3283 case RID_STATCAST:
3284 postfix_expression
3285 = build_static_cast (type, expression);
3286 break;
3287 case RID_REINTCAST:
3288 postfix_expression
3289 = build_reinterpret_cast (type, expression);
3290 break;
3291 case RID_CONSTCAST:
3292 postfix_expression
3293 = build_const_cast (type, expression);
3294 break;
3295 default:
3296 abort ();
3297 }
3298 }
3299 break;
3300
3301 case RID_TYPEID:
3302 {
3303 tree type;
3304 const char *saved_message;
3305
3306 /* Consume the `typeid' token. */
3307 cp_lexer_consume_token (parser->lexer);
3308 /* Look for the `(' token. */
3309 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3310 /* Types cannot be defined in a `typeid' expression. */
3311 saved_message = parser->type_definition_forbidden_message;
3312 parser->type_definition_forbidden_message
3313 = "types may not be defined in a `typeid\' expression";
3314 /* We can't be sure yet whether we're looking at a type-id or an
3315 expression. */
3316 cp_parser_parse_tentatively (parser);
3317 /* Try a type-id first. */
3318 type = cp_parser_type_id (parser);
3319 /* Look for the `)' token. Otherwise, we can't be sure that
3320 we're not looking at an expression: consider `typeid (int
3321 (3))', for example. */
3322 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3323 /* If all went well, simply lookup the type-id. */
3324 if (cp_parser_parse_definitely (parser))
3325 postfix_expression = get_typeid (type);
3326 /* Otherwise, fall back to the expression variant. */
3327 else
3328 {
3329 tree expression;
3330
3331 /* Look for an expression. */
3332 expression = cp_parser_expression (parser);
3333 /* Compute its typeid. */
3334 postfix_expression = build_typeid (expression);
3335 /* Look for the `)' token. */
3336 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3337 }
3338
3339 /* Restore the saved message. */
3340 parser->type_definition_forbidden_message = saved_message;
3341 }
3342 break;
3343
3344 case RID_TYPENAME:
3345 {
3346 bool template_p = false;
3347 tree id;
3348 tree type;
3349
3350 /* Consume the `typename' token. */
3351 cp_lexer_consume_token (parser->lexer);
3352 /* Look for the optional `::' operator. */
3353 cp_parser_global_scope_opt (parser,
3354 /*current_scope_valid_p=*/false);
3355 /* Look for the nested-name-specifier. */
3356 cp_parser_nested_name_specifier (parser,
3357 /*typename_keyword_p=*/true,
3358 /*check_dependency_p=*/true,
3359 /*type_p=*/true);
3360 /* Look for the optional `template' keyword. */
3361 template_p = cp_parser_optional_template_keyword (parser);
3362 /* We don't know whether we're looking at a template-id or an
3363 identifier. */
3364 cp_parser_parse_tentatively (parser);
3365 /* Try a template-id. */
3366 id = cp_parser_template_id (parser, template_p,
3367 /*check_dependency_p=*/true);
3368 /* If that didn't work, try an identifier. */
3369 if (!cp_parser_parse_definitely (parser))
3370 id = cp_parser_identifier (parser);
3371 /* Create a TYPENAME_TYPE to represent the type to which the
3372 functional cast is being performed. */
3373 type = make_typename_type (parser->scope, id,
3374 /*complain=*/1);
3375
3376 postfix_expression = cp_parser_functional_cast (parser, type);
3377 }
3378 break;
3379
3380 default:
3381 {
3382 tree type;
3383
3384 /* If the next thing is a simple-type-specifier, we may be
3385 looking at a functional cast. We could also be looking at
3386 an id-expression. So, we try the functional cast, and if
3387 that doesn't work we fall back to the primary-expression. */
3388 cp_parser_parse_tentatively (parser);
3389 /* Look for the simple-type-specifier. */
3390 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3391 CP_PARSER_FLAGS_NONE,
3392 /*identifier_p=*/false);
a723baf1
MM
3393 /* Parse the cast itself. */
3394 if (!cp_parser_error_occurred (parser))
3395 postfix_expression
3396 = cp_parser_functional_cast (parser, type);
3397 /* If that worked, we're done. */
3398 if (cp_parser_parse_definitely (parser))
3399 break;
3400
3401 /* If the functional-cast didn't work out, try a
3402 compound-literal. */
14d22dd6
MM
3403 if (cp_parser_allow_gnu_extensions_p (parser)
3404 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3405 {
3406 tree initializer_list = NULL_TREE;
3407
3408 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3409 /* Consume the `('. */
3410 cp_lexer_consume_token (parser->lexer);
3411 /* Parse the type. */
3412 type = cp_parser_type_id (parser);
3413 /* Look for the `)'. */
3414 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3415 /* Look for the `{'. */
3416 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3417 /* If things aren't going well, there's no need to
3418 keep going. */
3419 if (!cp_parser_error_occurred (parser))
a723baf1 3420 {
39703eb9 3421 bool non_constant_p;
14d22dd6
MM
3422 /* Parse the initializer-list. */
3423 initializer_list
39703eb9 3424 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3425 /* Allow a trailing `,'. */
3426 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3427 cp_lexer_consume_token (parser->lexer);
3428 /* Look for the final `}'. */
3429 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3430 }
3431 /* If that worked, we're definitely looking at a
3432 compound-literal expression. */
3433 if (cp_parser_parse_definitely (parser))
3434 {
3435 /* Warn the user that a compound literal is not
3436 allowed in standard C++. */
3437 if (pedantic)
3438 pedwarn ("ISO C++ forbids compound-literals");
3439 /* Form the representation of the compound-literal. */
3440 postfix_expression
3441 = finish_compound_literal (type, initializer_list);
3442 break;
3443 }
3444 }
3445
3446 /* It must be a primary-expression. */
3447 postfix_expression = cp_parser_primary_expression (parser,
3448 &idk,
3449 &qualifying_class);
3450 }
3451 break;
3452 }
3453
ee76b931
MM
3454 /* If we were avoiding committing to the processing of a
3455 qualified-id until we knew whether or not we had a
3456 pointer-to-member, we now know. */
089d6ea7 3457 if (qualifying_class)
a723baf1 3458 {
ee76b931 3459 bool done;
a723baf1 3460
ee76b931
MM
3461 /* Peek at the next token. */
3462 token = cp_lexer_peek_token (parser->lexer);
3463 done = (token->type != CPP_OPEN_SQUARE
3464 && token->type != CPP_OPEN_PAREN
3465 && token->type != CPP_DOT
3466 && token->type != CPP_DEREF
3467 && token->type != CPP_PLUS_PLUS
3468 && token->type != CPP_MINUS_MINUS);
3469
3470 postfix_expression = finish_qualified_id_expr (qualifying_class,
3471 postfix_expression,
3472 done,
3473 address_p);
3474 if (done)
3475 return postfix_expression;
a723baf1
MM
3476 }
3477
a723baf1
MM
3478 /* Keep looping until the postfix-expression is complete. */
3479 while (true)
3480 {
10b1d5e7
MM
3481 if (idk == CP_ID_KIND_UNQUALIFIED
3482 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3483 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3484 /* It is not a Koenig lookup function call. */
3485 postfix_expression
3486 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3487
3488 /* Peek at the next token. */
3489 token = cp_lexer_peek_token (parser->lexer);
3490
3491 switch (token->type)
3492 {
3493 case CPP_OPEN_SQUARE:
3494 /* postfix-expression [ expression ] */
3495 {
3496 tree index;
3497
3498 /* Consume the `[' token. */
3499 cp_lexer_consume_token (parser->lexer);
3500 /* Parse the index expression. */
3501 index = cp_parser_expression (parser);
3502 /* Look for the closing `]'. */
3503 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3504
3505 /* Build the ARRAY_REF. */
3506 postfix_expression
3507 = grok_array_decl (postfix_expression, index);
b3445994 3508 idk = CP_ID_KIND_NONE;
a723baf1
MM
3509 }
3510 break;
3511
3512 case CPP_OPEN_PAREN:
3513 /* postfix-expression ( expression-list [opt] ) */
3514 {
6d80c4b9 3515 bool koenig_p;
39703eb9
MM
3516 tree args = (cp_parser_parenthesized_expression_list
3517 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3518
7efa3e22
NS
3519 if (args == error_mark_node)
3520 {
3521 postfix_expression = error_mark_node;
3522 break;
3523 }
3524
14d22dd6
MM
3525 /* Function calls are not permitted in
3526 constant-expressions. */
3527 if (parser->constant_expression_p)
3528 {
3529 if (!parser->allow_non_constant_expression_p)
3530 return cp_parser_non_constant_expression ("a function call");
3531 parser->non_constant_expression_p = true;
3532 }
a723baf1 3533
6d80c4b9 3534 koenig_p = false;
399dedb9
NS
3535 if (idk == CP_ID_KIND_UNQUALIFIED)
3536 {
3537 if (args
3538 && (is_overloaded_fn (postfix_expression)
3539 || DECL_P (postfix_expression)
3540 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3541 {
3542 koenig_p = true;
3543 postfix_expression
3544 = perform_koenig_lookup (postfix_expression, args);
3545 }
399dedb9
NS
3546 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3547 postfix_expression
3548 = unqualified_fn_lookup_error (postfix_expression);
3549 }
3550
d17811fd 3551 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3552 {
d17811fd
MM
3553 tree instance = TREE_OPERAND (postfix_expression, 0);
3554 tree fn = TREE_OPERAND (postfix_expression, 1);
3555
3556 if (processing_template_decl
3557 && (type_dependent_expression_p (instance)
3558 || (!BASELINK_P (fn)
3559 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3560 || type_dependent_expression_p (fn)
d17811fd
MM
3561 || any_type_dependent_arguments_p (args)))
3562 {
3563 postfix_expression
3564 = build_min_nt (CALL_EXPR, postfix_expression, args);
3565 break;
3566 }
3567
3568 postfix_expression
3569 = (build_new_method_call
3570 (instance, fn, args, NULL_TREE,
b3445994 3571 (idk == CP_ID_KIND_QUALIFIED
d17811fd 3572 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
a723baf1 3573 }
d17811fd
MM
3574 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3575 || TREE_CODE (postfix_expression) == MEMBER_REF
3576 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3577 postfix_expression = (build_offset_ref_call_from_tree
3578 (postfix_expression, args));
b3445994 3579 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3580 /* A call to a static class member, or a namespace-scope
3581 function. */
3582 postfix_expression
3583 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3584 /*disallow_virtual=*/true,
3585 koenig_p);
a723baf1 3586 else
2050a1bb
MM
3587 /* All other function calls. */
3588 postfix_expression
3589 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3590 /*disallow_virtual=*/false,
3591 koenig_p);
a723baf1
MM
3592
3593 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3594 idk = CP_ID_KIND_NONE;
a723baf1
MM
3595 }
3596 break;
3597
3598 case CPP_DOT:
3599 case CPP_DEREF:
3600 /* postfix-expression . template [opt] id-expression
3601 postfix-expression . pseudo-destructor-name
3602 postfix-expression -> template [opt] id-expression
3603 postfix-expression -> pseudo-destructor-name */
3604 {
3605 tree name;
3606 bool dependent_p;
3607 bool template_p;
3608 tree scope = NULL_TREE;
3609
3610 /* If this is a `->' operator, dereference the pointer. */
3611 if (token->type == CPP_DEREF)
3612 postfix_expression = build_x_arrow (postfix_expression);
3613 /* Check to see whether or not the expression is
3614 type-dependent. */
bbaab916 3615 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3616 /* The identifier following the `->' or `.' is not
3617 qualified. */
3618 parser->scope = NULL_TREE;
3619 parser->qualifying_scope = NULL_TREE;
3620 parser->object_scope = NULL_TREE;
b3445994 3621 idk = CP_ID_KIND_NONE;
a723baf1
MM
3622 /* Enter the scope corresponding to the type of the object
3623 given by the POSTFIX_EXPRESSION. */
3624 if (!dependent_p
3625 && TREE_TYPE (postfix_expression) != NULL_TREE)
3626 {
3627 scope = TREE_TYPE (postfix_expression);
3628 /* According to the standard, no expression should
3629 ever have reference type. Unfortunately, we do not
3630 currently match the standard in this respect in
3631 that our internal representation of an expression
3632 may have reference type even when the standard says
3633 it does not. Therefore, we have to manually obtain
3634 the underlying type here. */
ee76b931 3635 scope = non_reference (scope);
a723baf1
MM
3636 /* The type of the POSTFIX_EXPRESSION must be
3637 complete. */
3638 scope = complete_type_or_else (scope, NULL_TREE);
3639 /* Let the name lookup machinery know that we are
3640 processing a class member access expression. */
3641 parser->context->object_type = scope;
3642 /* If something went wrong, we want to be able to
3643 discern that case, as opposed to the case where
3644 there was no SCOPE due to the type of expression
3645 being dependent. */
3646 if (!scope)
3647 scope = error_mark_node;
3648 }
3649
3650 /* Consume the `.' or `->' operator. */
3651 cp_lexer_consume_token (parser->lexer);
3652 /* If the SCOPE is not a scalar type, we are looking at an
3653 ordinary class member access expression, rather than a
3654 pseudo-destructor-name. */
3655 if (!scope || !SCALAR_TYPE_P (scope))
3656 {
3657 template_p = cp_parser_optional_template_keyword (parser);
3658 /* Parse the id-expression. */
3659 name = cp_parser_id_expression (parser,
3660 template_p,
3661 /*check_dependency_p=*/true,
f3c2dfc6
MM
3662 /*template_p=*/NULL,
3663 /*declarator_p=*/false);
a723baf1
MM
3664 /* In general, build a SCOPE_REF if the member name is
3665 qualified. However, if the name was not dependent
3666 and has already been resolved; there is no need to
3667 build the SCOPE_REF. For example;
3668
3669 struct X { void f(); };
3670 template <typename T> void f(T* t) { t->X::f(); }
3671
d17811fd
MM
3672 Even though "t" is dependent, "X::f" is not and has
3673 been resolved to a BASELINK; there is no need to
a723baf1 3674 include scope information. */
a6bd211d
JM
3675
3676 /* But we do need to remember that there was an explicit
3677 scope for virtual function calls. */
3678 if (parser->scope)
b3445994 3679 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3680
a723baf1
MM
3681 if (name != error_mark_node
3682 && !BASELINK_P (name)
3683 && parser->scope)
3684 {
3685 name = build_nt (SCOPE_REF, parser->scope, name);
3686 parser->scope = NULL_TREE;
3687 parser->qualifying_scope = NULL_TREE;
3688 parser->object_scope = NULL_TREE;
3689 }
3690 postfix_expression
3691 = finish_class_member_access_expr (postfix_expression, name);
3692 }
3693 /* Otherwise, try the pseudo-destructor-name production. */
3694 else
3695 {
3696 tree s;
3697 tree type;
3698
3699 /* Parse the pseudo-destructor-name. */
3700 cp_parser_pseudo_destructor_name (parser, &s, &type);
3701 /* Form the call. */
3702 postfix_expression
3703 = finish_pseudo_destructor_expr (postfix_expression,
3704 s, TREE_TYPE (type));
3705 }
3706
3707 /* We no longer need to look up names in the scope of the
3708 object on the left-hand side of the `.' or `->'
3709 operator. */
3710 parser->context->object_type = NULL_TREE;
a723baf1
MM
3711 }
3712 break;
3713
3714 case CPP_PLUS_PLUS:
3715 /* postfix-expression ++ */
3716 /* Consume the `++' token. */
3717 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3718 /* Increments may not appear in constant-expressions. */
3719 if (parser->constant_expression_p)
3720 {
3721 if (!parser->allow_non_constant_expression_p)
3722 return cp_parser_non_constant_expression ("an increment");
3723 parser->non_constant_expression_p = true;
3724 }
34cd5ae7 3725 /* Generate a representation for the complete expression. */
a723baf1
MM
3726 postfix_expression
3727 = finish_increment_expr (postfix_expression,
3728 POSTINCREMENT_EXPR);
b3445994 3729 idk = CP_ID_KIND_NONE;
a723baf1
MM
3730 break;
3731
3732 case CPP_MINUS_MINUS:
3733 /* postfix-expression -- */
3734 /* Consume the `--' token. */
3735 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3736 /* Decrements may not appear in constant-expressions. */
3737 if (parser->constant_expression_p)
3738 {
3739 if (!parser->allow_non_constant_expression_p)
3740 return cp_parser_non_constant_expression ("a decrement");
3741 parser->non_constant_expression_p = true;
3742 }
34cd5ae7 3743 /* Generate a representation for the complete expression. */
a723baf1
MM
3744 postfix_expression
3745 = finish_increment_expr (postfix_expression,
3746 POSTDECREMENT_EXPR);
b3445994 3747 idk = CP_ID_KIND_NONE;
a723baf1
MM
3748 break;
3749
3750 default:
3751 return postfix_expression;
3752 }
3753 }
3754
3755 /* We should never get here. */
3756 abort ();
3757 return error_mark_node;
3758}
3759
7efa3e22 3760/* Parse a parenthesized expression-list.
a723baf1
MM
3761
3762 expression-list:
3763 assignment-expression
3764 expression-list, assignment-expression
3765
7efa3e22
NS
3766 attribute-list:
3767 expression-list
3768 identifier
3769 identifier, expression-list
3770
a723baf1
MM
3771 Returns a TREE_LIST. The TREE_VALUE of each node is a
3772 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
3773 is returned even if there is only a single expression in the list.
3774 error_mark_node is returned if the ( and or ) are
3775 missing. NULL_TREE is returned on no expressions. The parentheses
3776 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
3777 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3778 indicates whether or not all of the expressions in the list were
3779 constant. */
a723baf1
MM
3780
3781static tree
39703eb9
MM
3782cp_parser_parenthesized_expression_list (cp_parser* parser,
3783 bool is_attribute_list,
3784 bool *non_constant_p)
a723baf1
MM
3785{
3786 tree expression_list = NULL_TREE;
7efa3e22 3787 tree identifier = NULL_TREE;
39703eb9
MM
3788
3789 /* Assume all the expressions will be constant. */
3790 if (non_constant_p)
3791 *non_constant_p = false;
3792
7efa3e22
NS
3793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3794 return error_mark_node;
3795
a723baf1 3796 /* Consume expressions until there are no more. */
7efa3e22
NS
3797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3798 while (true)
3799 {
3800 tree expr;
3801
3802 /* At the beginning of attribute lists, check to see if the
3803 next token is an identifier. */
3804 if (is_attribute_list
3805 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3806 {
3807 cp_token *token;
3808
3809 /* Consume the identifier. */
3810 token = cp_lexer_consume_token (parser->lexer);
3811 /* Save the identifier. */
3812 identifier = token->value;
3813 }
3814 else
3815 {
3816 /* Parse the next assignment-expression. */
39703eb9
MM
3817 if (non_constant_p)
3818 {
3819 bool expr_non_constant_p;
3820 expr = (cp_parser_constant_expression
3821 (parser, /*allow_non_constant_p=*/true,
3822 &expr_non_constant_p));
3823 if (expr_non_constant_p)
3824 *non_constant_p = true;
3825 }
3826 else
3827 expr = cp_parser_assignment_expression (parser);
a723baf1 3828
7efa3e22
NS
3829 /* Add it to the list. We add error_mark_node
3830 expressions to the list, so that we can still tell if
3831 the correct form for a parenthesized expression-list
3832 is found. That gives better errors. */
3833 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 3834
7efa3e22
NS
3835 if (expr == error_mark_node)
3836 goto skip_comma;
3837 }
a723baf1 3838
7efa3e22
NS
3839 /* After the first item, attribute lists look the same as
3840 expression lists. */
3841 is_attribute_list = false;
3842
3843 get_comma:;
3844 /* If the next token isn't a `,', then we are done. */
3845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3846 break;
3847
3848 /* Otherwise, consume the `,' and keep going. */
3849 cp_lexer_consume_token (parser->lexer);
3850 }
3851
3852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3853 {
3854 int ending;
3855
3856 skip_comma:;
3857 /* We try and resync to an unnested comma, as that will give the
3858 user better diagnostics. */
3859 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3860 if (ending < 0)
3861 goto get_comma;
3862 if (!ending)
3863 return error_mark_node;
a723baf1
MM
3864 }
3865
3866 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
3867 expression_list = nreverse (expression_list);
3868 if (identifier)
3869 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3870
3871 return expression_list;
a723baf1
MM
3872}
3873
3874/* Parse a pseudo-destructor-name.
3875
3876 pseudo-destructor-name:
3877 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3878 :: [opt] nested-name-specifier template template-id :: ~ type-name
3879 :: [opt] nested-name-specifier [opt] ~ type-name
3880
3881 If either of the first two productions is used, sets *SCOPE to the
3882 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3883 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3884 or ERROR_MARK_NODE if no type-name is present. */
3885
3886static void
94edc4ab
NN
3887cp_parser_pseudo_destructor_name (cp_parser* parser,
3888 tree* scope,
3889 tree* type)
a723baf1
MM
3890{
3891 bool nested_name_specifier_p;
3892
3893 /* Look for the optional `::' operator. */
3894 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3895 /* Look for the optional nested-name-specifier. */
3896 nested_name_specifier_p
3897 = (cp_parser_nested_name_specifier_opt (parser,
3898 /*typename_keyword_p=*/false,
3899 /*check_dependency_p=*/true,
3900 /*type_p=*/false)
3901 != NULL_TREE);
3902 /* Now, if we saw a nested-name-specifier, we might be doing the
3903 second production. */
3904 if (nested_name_specifier_p
3905 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3906 {
3907 /* Consume the `template' keyword. */
3908 cp_lexer_consume_token (parser->lexer);
3909 /* Parse the template-id. */
3910 cp_parser_template_id (parser,
3911 /*template_keyword_p=*/true,
3912 /*check_dependency_p=*/false);
3913 /* Look for the `::' token. */
3914 cp_parser_require (parser, CPP_SCOPE, "`::'");
3915 }
3916 /* If the next token is not a `~', then there might be some
9bcb9aae 3917 additional qualification. */
a723baf1
MM
3918 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3919 {
3920 /* Look for the type-name. */
3921 *scope = TREE_TYPE (cp_parser_type_name (parser));
3922 /* Look for the `::' token. */
3923 cp_parser_require (parser, CPP_SCOPE, "`::'");
3924 }
3925 else
3926 *scope = NULL_TREE;
3927
3928 /* Look for the `~'. */
3929 cp_parser_require (parser, CPP_COMPL, "`~'");
3930 /* Look for the type-name again. We are not responsible for
3931 checking that it matches the first type-name. */
3932 *type = cp_parser_type_name (parser);
3933}
3934
3935/* Parse a unary-expression.
3936
3937 unary-expression:
3938 postfix-expression
3939 ++ cast-expression
3940 -- cast-expression
3941 unary-operator cast-expression
3942 sizeof unary-expression
3943 sizeof ( type-id )
3944 new-expression
3945 delete-expression
3946
3947 GNU Extensions:
3948
3949 unary-expression:
3950 __extension__ cast-expression
3951 __alignof__ unary-expression
3952 __alignof__ ( type-id )
3953 __real__ cast-expression
3954 __imag__ cast-expression
3955 && identifier
3956
3957 ADDRESS_P is true iff the unary-expression is appearing as the
3958 operand of the `&' operator.
3959
34cd5ae7 3960 Returns a representation of the expression. */
a723baf1
MM
3961
3962static tree
3963cp_parser_unary_expression (cp_parser *parser, bool address_p)
3964{
3965 cp_token *token;
3966 enum tree_code unary_operator;
3967
3968 /* Peek at the next token. */
3969 token = cp_lexer_peek_token (parser->lexer);
3970 /* Some keywords give away the kind of expression. */
3971 if (token->type == CPP_KEYWORD)
3972 {
3973 enum rid keyword = token->keyword;
3974
3975 switch (keyword)
3976 {
3977 case RID_ALIGNOF:
a723baf1
MM
3978 case RID_SIZEOF:
3979 {
3980 tree operand;
7a18b933 3981 enum tree_code op;
a723baf1 3982
7a18b933
NS
3983 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
3984 /* Consume the token. */
a723baf1
MM
3985 cp_lexer_consume_token (parser->lexer);
3986 /* Parse the operand. */
3987 operand = cp_parser_sizeof_operand (parser, keyword);
3988
7a18b933
NS
3989 if (TYPE_P (operand))
3990 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 3991 else
7a18b933 3992 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
3993 }
3994
3995 case RID_NEW:
3996 return cp_parser_new_expression (parser);
3997
3998 case RID_DELETE:
3999 return cp_parser_delete_expression (parser);
4000
4001 case RID_EXTENSION:
4002 {
4003 /* The saved value of the PEDANTIC flag. */
4004 int saved_pedantic;
4005 tree expr;
4006
4007 /* Save away the PEDANTIC flag. */
4008 cp_parser_extension_opt (parser, &saved_pedantic);
4009 /* Parse the cast-expression. */
d6b4ea85 4010 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4011 /* Restore the PEDANTIC flag. */
4012 pedantic = saved_pedantic;
4013
4014 return expr;
4015 }
4016
4017 case RID_REALPART:
4018 case RID_IMAGPART:
4019 {
4020 tree expression;
4021
4022 /* Consume the `__real__' or `__imag__' token. */
4023 cp_lexer_consume_token (parser->lexer);
4024 /* Parse the cast-expression. */
d6b4ea85 4025 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4026 /* Create the complete representation. */
4027 return build_x_unary_op ((keyword == RID_REALPART
4028 ? REALPART_EXPR : IMAGPART_EXPR),
4029 expression);
4030 }
4031 break;
4032
4033 default:
4034 break;
4035 }
4036 }
4037
4038 /* Look for the `:: new' and `:: delete', which also signal the
4039 beginning of a new-expression, or delete-expression,
4040 respectively. If the next token is `::', then it might be one of
4041 these. */
4042 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4043 {
4044 enum rid keyword;
4045
4046 /* See if the token after the `::' is one of the keywords in
4047 which we're interested. */
4048 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4049 /* If it's `new', we have a new-expression. */
4050 if (keyword == RID_NEW)
4051 return cp_parser_new_expression (parser);
4052 /* Similarly, for `delete'. */
4053 else if (keyword == RID_DELETE)
4054 return cp_parser_delete_expression (parser);
4055 }
4056
4057 /* Look for a unary operator. */
4058 unary_operator = cp_parser_unary_operator (token);
4059 /* The `++' and `--' operators can be handled similarly, even though
4060 they are not technically unary-operators in the grammar. */
4061 if (unary_operator == ERROR_MARK)
4062 {
4063 if (token->type == CPP_PLUS_PLUS)
4064 unary_operator = PREINCREMENT_EXPR;
4065 else if (token->type == CPP_MINUS_MINUS)
4066 unary_operator = PREDECREMENT_EXPR;
4067 /* Handle the GNU address-of-label extension. */
4068 else if (cp_parser_allow_gnu_extensions_p (parser)
4069 && token->type == CPP_AND_AND)
4070 {
4071 tree identifier;
4072
4073 /* Consume the '&&' token. */
4074 cp_lexer_consume_token (parser->lexer);
4075 /* Look for the identifier. */
4076 identifier = cp_parser_identifier (parser);
4077 /* Create an expression representing the address. */
4078 return finish_label_address_expr (identifier);
4079 }
4080 }
4081 if (unary_operator != ERROR_MARK)
4082 {
4083 tree cast_expression;
4084
4085 /* Consume the operator token. */
4086 token = cp_lexer_consume_token (parser->lexer);
4087 /* Parse the cast-expression. */
4088 cast_expression
4089 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4090 /* Now, build an appropriate representation. */
4091 switch (unary_operator)
4092 {
4093 case INDIRECT_REF:
4094 return build_x_indirect_ref (cast_expression, "unary *");
4095
4096 case ADDR_EXPR:
d17811fd
MM
4097 case BIT_NOT_EXPR:
4098 return build_x_unary_op (unary_operator, cast_expression);
a723baf1 4099
14d22dd6
MM
4100 case PREINCREMENT_EXPR:
4101 case PREDECREMENT_EXPR:
4102 if (parser->constant_expression_p)
4103 {
4104 if (!parser->allow_non_constant_expression_p)
4105 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4106 ? "an increment"
4107 : "a decrement");
4108 parser->non_constant_expression_p = true;
4109 }
4110 /* Fall through. */
a723baf1
MM
4111 case CONVERT_EXPR:
4112 case NEGATE_EXPR:
4113 case TRUTH_NOT_EXPR:
a723baf1
MM
4114 return finish_unary_op_expr (unary_operator, cast_expression);
4115
a723baf1
MM
4116 default:
4117 abort ();
4118 return error_mark_node;
4119 }
4120 }
4121
4122 return cp_parser_postfix_expression (parser, address_p);
4123}
4124
4125/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4126 unary-operator, the corresponding tree code is returned. */
4127
4128static enum tree_code
94edc4ab 4129cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4130{
4131 switch (token->type)
4132 {
4133 case CPP_MULT:
4134 return INDIRECT_REF;
4135
4136 case CPP_AND:
4137 return ADDR_EXPR;
4138
4139 case CPP_PLUS:
4140 return CONVERT_EXPR;
4141
4142 case CPP_MINUS:
4143 return NEGATE_EXPR;
4144
4145 case CPP_NOT:
4146 return TRUTH_NOT_EXPR;
4147
4148 case CPP_COMPL:
4149 return BIT_NOT_EXPR;
4150
4151 default:
4152 return ERROR_MARK;
4153 }
4154}
4155
4156/* Parse a new-expression.
4157
ca099ac8 4158 new-expression:
a723baf1
MM
4159 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4160 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4161
4162 Returns a representation of the expression. */
4163
4164static tree
94edc4ab 4165cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4166{
4167 bool global_scope_p;
4168 tree placement;
4169 tree type;
4170 tree initializer;
4171
4172 /* Look for the optional `::' operator. */
4173 global_scope_p
4174 = (cp_parser_global_scope_opt (parser,
4175 /*current_scope_valid_p=*/false)
4176 != NULL_TREE);
4177 /* Look for the `new' operator. */
4178 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4179 /* There's no easy way to tell a new-placement from the
4180 `( type-id )' construct. */
4181 cp_parser_parse_tentatively (parser);
4182 /* Look for a new-placement. */
4183 placement = cp_parser_new_placement (parser);
4184 /* If that didn't work out, there's no new-placement. */
4185 if (!cp_parser_parse_definitely (parser))
4186 placement = NULL_TREE;
4187
4188 /* If the next token is a `(', then we have a parenthesized
4189 type-id. */
4190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4191 {
4192 /* Consume the `('. */
4193 cp_lexer_consume_token (parser->lexer);
4194 /* Parse the type-id. */
4195 type = cp_parser_type_id (parser);
4196 /* Look for the closing `)'. */
4197 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4198 }
4199 /* Otherwise, there must be a new-type-id. */
4200 else
4201 type = cp_parser_new_type_id (parser);
4202
4203 /* If the next token is a `(', then we have a new-initializer. */
4204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4205 initializer = cp_parser_new_initializer (parser);
4206 else
4207 initializer = NULL_TREE;
4208
4209 /* Create a representation of the new-expression. */
4210 return build_new (placement, type, initializer, global_scope_p);
4211}
4212
4213/* Parse a new-placement.
4214
4215 new-placement:
4216 ( expression-list )
4217
4218 Returns the same representation as for an expression-list. */
4219
4220static tree
94edc4ab 4221cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4222{
4223 tree expression_list;
4224
a723baf1 4225 /* Parse the expression-list. */
39703eb9
MM
4226 expression_list = (cp_parser_parenthesized_expression_list
4227 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4228
4229 return expression_list;
4230}
4231
4232/* Parse a new-type-id.
4233
4234 new-type-id:
4235 type-specifier-seq new-declarator [opt]
4236
4237 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4238 and whose TREE_VALUE is the new-declarator. */
4239
4240static tree
94edc4ab 4241cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4242{
4243 tree type_specifier_seq;
4244 tree declarator;
4245 const char *saved_message;
4246
4247 /* The type-specifier sequence must not contain type definitions.
4248 (It cannot contain declarations of new types either, but if they
4249 are not definitions we will catch that because they are not
4250 complete.) */
4251 saved_message = parser->type_definition_forbidden_message;
4252 parser->type_definition_forbidden_message
4253 = "types may not be defined in a new-type-id";
4254 /* Parse the type-specifier-seq. */
4255 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4256 /* Restore the old message. */
4257 parser->type_definition_forbidden_message = saved_message;
4258 /* Parse the new-declarator. */
4259 declarator = cp_parser_new_declarator_opt (parser);
4260
4261 return build_tree_list (type_specifier_seq, declarator);
4262}
4263
4264/* Parse an (optional) new-declarator.
4265
4266 new-declarator:
4267 ptr-operator new-declarator [opt]
4268 direct-new-declarator
4269
4270 Returns a representation of the declarator. See
4271 cp_parser_declarator for the representations used. */
4272
4273static tree
94edc4ab 4274cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4275{
4276 enum tree_code code;
4277 tree type;
4278 tree cv_qualifier_seq;
4279
4280 /* We don't know if there's a ptr-operator next, or not. */
4281 cp_parser_parse_tentatively (parser);
4282 /* Look for a ptr-operator. */
4283 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4284 /* If that worked, look for more new-declarators. */
4285 if (cp_parser_parse_definitely (parser))
4286 {
4287 tree declarator;
4288
4289 /* Parse another optional declarator. */
4290 declarator = cp_parser_new_declarator_opt (parser);
4291
4292 /* Create the representation of the declarator. */
4293 if (code == INDIRECT_REF)
4294 declarator = make_pointer_declarator (cv_qualifier_seq,
4295 declarator);
4296 else
4297 declarator = make_reference_declarator (cv_qualifier_seq,
4298 declarator);
4299
4300 /* Handle the pointer-to-member case. */
4301 if (type)
4302 declarator = build_nt (SCOPE_REF, type, declarator);
4303
4304 return declarator;
4305 }
4306
4307 /* If the next token is a `[', there is a direct-new-declarator. */
4308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4309 return cp_parser_direct_new_declarator (parser);
4310
4311 return NULL_TREE;
4312}
4313
4314/* Parse a direct-new-declarator.
4315
4316 direct-new-declarator:
4317 [ expression ]
4318 direct-new-declarator [constant-expression]
4319
4320 Returns an ARRAY_REF, following the same conventions as are
4321 documented for cp_parser_direct_declarator. */
4322
4323static tree
94edc4ab 4324cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4325{
4326 tree declarator = NULL_TREE;
4327
4328 while (true)
4329 {
4330 tree expression;
4331
4332 /* Look for the opening `['. */
4333 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4334 /* The first expression is not required to be constant. */
4335 if (!declarator)
4336 {
4337 expression = cp_parser_expression (parser);
4338 /* The standard requires that the expression have integral
4339 type. DR 74 adds enumeration types. We believe that the
4340 real intent is that these expressions be handled like the
4341 expression in a `switch' condition, which also allows
4342 classes with a single conversion to integral or
4343 enumeration type. */
4344 if (!processing_template_decl)
4345 {
4346 expression
4347 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4348 expression,
b746c5dc 4349 /*complain=*/true);
a723baf1
MM
4350 if (!expression)
4351 {
4352 error ("expression in new-declarator must have integral or enumeration type");
4353 expression = error_mark_node;
4354 }
4355 }
4356 }
4357 /* But all the other expressions must be. */
4358 else
14d22dd6
MM
4359 expression
4360 = cp_parser_constant_expression (parser,
4361 /*allow_non_constant=*/false,
4362 NULL);
a723baf1
MM
4363 /* Look for the closing `]'. */
4364 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4365
4366 /* Add this bound to the declarator. */
4367 declarator = build_nt (ARRAY_REF, declarator, expression);
4368
4369 /* If the next token is not a `[', then there are no more
4370 bounds. */
4371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4372 break;
4373 }
4374
4375 return declarator;
4376}
4377
4378/* Parse a new-initializer.
4379
4380 new-initializer:
4381 ( expression-list [opt] )
4382
34cd5ae7 4383 Returns a representation of the expression-list. If there is no
a723baf1
MM
4384 expression-list, VOID_ZERO_NODE is returned. */
4385
4386static tree
94edc4ab 4387cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4388{
4389 tree expression_list;
4390
39703eb9
MM
4391 expression_list = (cp_parser_parenthesized_expression_list
4392 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4393 if (!expression_list)
a723baf1 4394 expression_list = void_zero_node;
a723baf1
MM
4395
4396 return expression_list;
4397}
4398
4399/* Parse a delete-expression.
4400
4401 delete-expression:
4402 :: [opt] delete cast-expression
4403 :: [opt] delete [ ] cast-expression
4404
4405 Returns a representation of the expression. */
4406
4407static tree
94edc4ab 4408cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4409{
4410 bool global_scope_p;
4411 bool array_p;
4412 tree expression;
4413
4414 /* Look for the optional `::' operator. */
4415 global_scope_p
4416 = (cp_parser_global_scope_opt (parser,
4417 /*current_scope_valid_p=*/false)
4418 != NULL_TREE);
4419 /* Look for the `delete' keyword. */
4420 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4421 /* See if the array syntax is in use. */
4422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4423 {
4424 /* Consume the `[' token. */
4425 cp_lexer_consume_token (parser->lexer);
4426 /* Look for the `]' token. */
4427 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4428 /* Remember that this is the `[]' construct. */
4429 array_p = true;
4430 }
4431 else
4432 array_p = false;
4433
4434 /* Parse the cast-expression. */
d6b4ea85 4435 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4436
4437 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4438}
4439
4440/* Parse a cast-expression.
4441
4442 cast-expression:
4443 unary-expression
4444 ( type-id ) cast-expression
4445
4446 Returns a representation of the expression. */
4447
4448static tree
4449cp_parser_cast_expression (cp_parser *parser, bool address_p)
4450{
4451 /* If it's a `(', then we might be looking at a cast. */
4452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4453 {
4454 tree type = NULL_TREE;
4455 tree expr = NULL_TREE;
4456 bool compound_literal_p;
4457 const char *saved_message;
4458
4459 /* There's no way to know yet whether or not this is a cast.
4460 For example, `(int (3))' is a unary-expression, while `(int)
4461 3' is a cast. So, we resort to parsing tentatively. */
4462 cp_parser_parse_tentatively (parser);
4463 /* Types may not be defined in a cast. */
4464 saved_message = parser->type_definition_forbidden_message;
4465 parser->type_definition_forbidden_message
4466 = "types may not be defined in casts";
4467 /* Consume the `('. */
4468 cp_lexer_consume_token (parser->lexer);
4469 /* A very tricky bit is that `(struct S) { 3 }' is a
4470 compound-literal (which we permit in C++ as an extension).
4471 But, that construct is not a cast-expression -- it is a
4472 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4473 is legal; if the compound-literal were a cast-expression,
4474 you'd need an extra set of parentheses.) But, if we parse
4475 the type-id, and it happens to be a class-specifier, then we
4476 will commit to the parse at that point, because we cannot
4477 undo the action that is done when creating a new class. So,
4478 then we cannot back up and do a postfix-expression.
4479
4480 Therefore, we scan ahead to the closing `)', and check to see
4481 if the token after the `)' is a `{'. If so, we are not
4482 looking at a cast-expression.
4483
4484 Save tokens so that we can put them back. */
4485 cp_lexer_save_tokens (parser->lexer);
4486 /* Skip tokens until the next token is a closing parenthesis.
4487 If we find the closing `)', and the next token is a `{', then
4488 we are looking at a compound-literal. */
4489 compound_literal_p
7efa3e22 4490 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
a723baf1
MM
4491 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4492 /* Roll back the tokens we skipped. */
4493 cp_lexer_rollback_tokens (parser->lexer);
4494 /* If we were looking at a compound-literal, simulate an error
4495 so that the call to cp_parser_parse_definitely below will
4496 fail. */
4497 if (compound_literal_p)
4498 cp_parser_simulate_error (parser);
4499 else
4500 {
4501 /* Look for the type-id. */
4502 type = cp_parser_type_id (parser);
4503 /* Look for the closing `)'. */
4504 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4505 }
4506
4507 /* Restore the saved message. */
4508 parser->type_definition_forbidden_message = saved_message;
4509
bbaab916
NS
4510 /* If ok so far, parse the dependent expression. We cannot be
4511 sure it is a cast. Consider `(T ())'. It is a parenthesized
4512 ctor of T, but looks like a cast to function returning T
4513 without a dependent expression. */
4514 if (!cp_parser_error_occurred (parser))
d6b4ea85 4515 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4516
a723baf1
MM
4517 if (cp_parser_parse_definitely (parser))
4518 {
a723baf1
MM
4519 /* Warn about old-style casts, if so requested. */
4520 if (warn_old_style_cast
4521 && !in_system_header
4522 && !VOID_TYPE_P (type)
4523 && current_lang_name != lang_name_c)
4524 warning ("use of old-style cast");
14d22dd6
MM
4525
4526 /* Only type conversions to integral or enumeration types
4527 can be used in constant-expressions. */
4528 if (parser->constant_expression_p
4529 && !dependent_type_p (type)
4530 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4531 {
4532 if (!parser->allow_non_constant_expression_p)
4533 return (cp_parser_non_constant_expression
4534 ("a casts to a type other than an integral or "
4535 "enumeration type"));
4536 parser->non_constant_expression_p = true;
4537 }
a723baf1
MM
4538 /* Perform the cast. */
4539 expr = build_c_cast (type, expr);
bbaab916 4540 return expr;
a723baf1 4541 }
a723baf1
MM
4542 }
4543
4544 /* If we get here, then it's not a cast, so it must be a
4545 unary-expression. */
4546 return cp_parser_unary_expression (parser, address_p);
4547}
4548
4549/* Parse a pm-expression.
4550
4551 pm-expression:
4552 cast-expression
4553 pm-expression .* cast-expression
4554 pm-expression ->* cast-expression
4555
4556 Returns a representation of the expression. */
4557
4558static tree
94edc4ab 4559cp_parser_pm_expression (cp_parser* parser)
a723baf1 4560{
d6b4ea85
MM
4561 static const cp_parser_token_tree_map map = {
4562 { CPP_DEREF_STAR, MEMBER_REF },
4563 { CPP_DOT_STAR, DOTSTAR_EXPR },
4564 { CPP_EOF, ERROR_MARK }
4565 };
a723baf1 4566
d6b4ea85
MM
4567 return cp_parser_binary_expression (parser, map,
4568 cp_parser_simple_cast_expression);
a723baf1
MM
4569}
4570
4571/* Parse a multiplicative-expression.
4572
4573 mulitplicative-expression:
4574 pm-expression
4575 multiplicative-expression * pm-expression
4576 multiplicative-expression / pm-expression
4577 multiplicative-expression % pm-expression
4578
4579 Returns a representation of the expression. */
4580
4581static tree
94edc4ab 4582cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4583{
39b1af70 4584 static const cp_parser_token_tree_map map = {
a723baf1
MM
4585 { CPP_MULT, MULT_EXPR },
4586 { CPP_DIV, TRUNC_DIV_EXPR },
4587 { CPP_MOD, TRUNC_MOD_EXPR },
4588 { CPP_EOF, ERROR_MARK }
4589 };
4590
4591 return cp_parser_binary_expression (parser,
4592 map,
4593 cp_parser_pm_expression);
4594}
4595
4596/* Parse an additive-expression.
4597
4598 additive-expression:
4599 multiplicative-expression
4600 additive-expression + multiplicative-expression
4601 additive-expression - multiplicative-expression
4602
4603 Returns a representation of the expression. */
4604
4605static tree
94edc4ab 4606cp_parser_additive_expression (cp_parser* parser)
a723baf1 4607{
39b1af70 4608 static const cp_parser_token_tree_map map = {
a723baf1
MM
4609 { CPP_PLUS, PLUS_EXPR },
4610 { CPP_MINUS, MINUS_EXPR },
4611 { CPP_EOF, ERROR_MARK }
4612 };
4613
4614 return cp_parser_binary_expression (parser,
4615 map,
4616 cp_parser_multiplicative_expression);
4617}
4618
4619/* Parse a shift-expression.
4620
4621 shift-expression:
4622 additive-expression
4623 shift-expression << additive-expression
4624 shift-expression >> additive-expression
4625
4626 Returns a representation of the expression. */
4627
4628static tree
94edc4ab 4629cp_parser_shift_expression (cp_parser* parser)
a723baf1 4630{
39b1af70 4631 static const cp_parser_token_tree_map map = {
a723baf1
MM
4632 { CPP_LSHIFT, LSHIFT_EXPR },
4633 { CPP_RSHIFT, RSHIFT_EXPR },
4634 { CPP_EOF, ERROR_MARK }
4635 };
4636
4637 return cp_parser_binary_expression (parser,
4638 map,
4639 cp_parser_additive_expression);
4640}
4641
4642/* Parse a relational-expression.
4643
4644 relational-expression:
4645 shift-expression
4646 relational-expression < shift-expression
4647 relational-expression > shift-expression
4648 relational-expression <= shift-expression
4649 relational-expression >= shift-expression
4650
4651 GNU Extension:
4652
4653 relational-expression:
4654 relational-expression <? shift-expression
4655 relational-expression >? shift-expression
4656
4657 Returns a representation of the expression. */
4658
4659static tree
94edc4ab 4660cp_parser_relational_expression (cp_parser* parser)
a723baf1 4661{
39b1af70 4662 static const cp_parser_token_tree_map map = {
a723baf1
MM
4663 { CPP_LESS, LT_EXPR },
4664 { CPP_GREATER, GT_EXPR },
4665 { CPP_LESS_EQ, LE_EXPR },
4666 { CPP_GREATER_EQ, GE_EXPR },
4667 { CPP_MIN, MIN_EXPR },
4668 { CPP_MAX, MAX_EXPR },
4669 { CPP_EOF, ERROR_MARK }
4670 };
4671
4672 return cp_parser_binary_expression (parser,
4673 map,
4674 cp_parser_shift_expression);
4675}
4676
4677/* Parse an equality-expression.
4678
4679 equality-expression:
4680 relational-expression
4681 equality-expression == relational-expression
4682 equality-expression != relational-expression
4683
4684 Returns a representation of the expression. */
4685
4686static tree
94edc4ab 4687cp_parser_equality_expression (cp_parser* parser)
a723baf1 4688{
39b1af70 4689 static const cp_parser_token_tree_map map = {
a723baf1
MM
4690 { CPP_EQ_EQ, EQ_EXPR },
4691 { CPP_NOT_EQ, NE_EXPR },
4692 { CPP_EOF, ERROR_MARK }
4693 };
4694
4695 return cp_parser_binary_expression (parser,
4696 map,
4697 cp_parser_relational_expression);
4698}
4699
4700/* Parse an and-expression.
4701
4702 and-expression:
4703 equality-expression
4704 and-expression & equality-expression
4705
4706 Returns a representation of the expression. */
4707
4708static tree
94edc4ab 4709cp_parser_and_expression (cp_parser* parser)
a723baf1 4710{
39b1af70 4711 static const cp_parser_token_tree_map map = {
a723baf1
MM
4712 { CPP_AND, BIT_AND_EXPR },
4713 { CPP_EOF, ERROR_MARK }
4714 };
4715
4716 return cp_parser_binary_expression (parser,
4717 map,
4718 cp_parser_equality_expression);
4719}
4720
4721/* Parse an exclusive-or-expression.
4722
4723 exclusive-or-expression:
4724 and-expression
4725 exclusive-or-expression ^ and-expression
4726
4727 Returns a representation of the expression. */
4728
4729static tree
94edc4ab 4730cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4731{
39b1af70 4732 static const cp_parser_token_tree_map map = {
a723baf1
MM
4733 { CPP_XOR, BIT_XOR_EXPR },
4734 { CPP_EOF, ERROR_MARK }
4735 };
4736
4737 return cp_parser_binary_expression (parser,
4738 map,
4739 cp_parser_and_expression);
4740}
4741
4742
4743/* Parse an inclusive-or-expression.
4744
4745 inclusive-or-expression:
4746 exclusive-or-expression
4747 inclusive-or-expression | exclusive-or-expression
4748
4749 Returns a representation of the expression. */
4750
4751static tree
94edc4ab 4752cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 4753{
39b1af70 4754 static const cp_parser_token_tree_map map = {
a723baf1
MM
4755 { CPP_OR, BIT_IOR_EXPR },
4756 { CPP_EOF, ERROR_MARK }
4757 };
4758
4759 return cp_parser_binary_expression (parser,
4760 map,
4761 cp_parser_exclusive_or_expression);
4762}
4763
4764/* Parse a logical-and-expression.
4765
4766 logical-and-expression:
4767 inclusive-or-expression
4768 logical-and-expression && inclusive-or-expression
4769
4770 Returns a representation of the expression. */
4771
4772static tree
94edc4ab 4773cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 4774{
39b1af70 4775 static const cp_parser_token_tree_map map = {
a723baf1
MM
4776 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4777 { CPP_EOF, ERROR_MARK }
4778 };
4779
4780 return cp_parser_binary_expression (parser,
4781 map,
4782 cp_parser_inclusive_or_expression);
4783}
4784
4785/* Parse a logical-or-expression.
4786
4787 logical-or-expression:
34cd5ae7 4788 logical-and-expression
a723baf1
MM
4789 logical-or-expression || logical-and-expression
4790
4791 Returns a representation of the expression. */
4792
4793static tree
94edc4ab 4794cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 4795{
39b1af70 4796 static const cp_parser_token_tree_map map = {
a723baf1
MM
4797 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4798 { CPP_EOF, ERROR_MARK }
4799 };
4800
4801 return cp_parser_binary_expression (parser,
4802 map,
4803 cp_parser_logical_and_expression);
4804}
4805
a723baf1
MM
4806/* Parse the `? expression : assignment-expression' part of a
4807 conditional-expression. The LOGICAL_OR_EXPR is the
4808 logical-or-expression that started the conditional-expression.
4809 Returns a representation of the entire conditional-expression.
4810
39703eb9 4811 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
4812
4813 ? expression : assignment-expression
4814
4815 GNU Extensions:
4816
4817 ? : assignment-expression */
4818
4819static tree
94edc4ab 4820cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
4821{
4822 tree expr;
4823 tree assignment_expr;
4824
4825 /* Consume the `?' token. */
4826 cp_lexer_consume_token (parser->lexer);
4827 if (cp_parser_allow_gnu_extensions_p (parser)
4828 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4829 /* Implicit true clause. */
4830 expr = NULL_TREE;
4831 else
4832 /* Parse the expression. */
4833 expr = cp_parser_expression (parser);
4834
4835 /* The next token should be a `:'. */
4836 cp_parser_require (parser, CPP_COLON, "`:'");
4837 /* Parse the assignment-expression. */
4838 assignment_expr = cp_parser_assignment_expression (parser);
4839
4840 /* Build the conditional-expression. */
4841 return build_x_conditional_expr (logical_or_expr,
4842 expr,
4843 assignment_expr);
4844}
4845
4846/* Parse an assignment-expression.
4847
4848 assignment-expression:
4849 conditional-expression
4850 logical-or-expression assignment-operator assignment_expression
4851 throw-expression
4852
4853 Returns a representation for the expression. */
4854
4855static tree
94edc4ab 4856cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
4857{
4858 tree expr;
4859
4860 /* If the next token is the `throw' keyword, then we're looking at
4861 a throw-expression. */
4862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4863 expr = cp_parser_throw_expression (parser);
4864 /* Otherwise, it must be that we are looking at a
4865 logical-or-expression. */
4866 else
4867 {
4868 /* Parse the logical-or-expression. */
4869 expr = cp_parser_logical_or_expression (parser);
4870 /* If the next token is a `?' then we're actually looking at a
4871 conditional-expression. */
4872 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4873 return cp_parser_question_colon_clause (parser, expr);
4874 else
4875 {
4876 enum tree_code assignment_operator;
4877
4878 /* If it's an assignment-operator, we're using the second
4879 production. */
4880 assignment_operator
4881 = cp_parser_assignment_operator_opt (parser);
4882 if (assignment_operator != ERROR_MARK)
4883 {
4884 tree rhs;
4885
4886 /* Parse the right-hand side of the assignment. */
4887 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
4888 /* An assignment may not appear in a
4889 constant-expression. */
4890 if (parser->constant_expression_p)
4891 {
4892 if (!parser->allow_non_constant_expression_p)
4893 return cp_parser_non_constant_expression ("an assignment");
4894 parser->non_constant_expression_p = true;
4895 }
34cd5ae7 4896 /* Build the assignment expression. */
a723baf1
MM
4897 expr = build_x_modify_expr (expr,
4898 assignment_operator,
4899 rhs);
4900 }
4901 }
4902 }
4903
4904 return expr;
4905}
4906
4907/* Parse an (optional) assignment-operator.
4908
4909 assignment-operator: one of
4910 = *= /= %= += -= >>= <<= &= ^= |=
4911
4912 GNU Extension:
4913
4914 assignment-operator: one of
4915 <?= >?=
4916
4917 If the next token is an assignment operator, the corresponding tree
4918 code is returned, and the token is consumed. For example, for
4919 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4920 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4921 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4922 operator, ERROR_MARK is returned. */
4923
4924static enum tree_code
94edc4ab 4925cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
4926{
4927 enum tree_code op;
4928 cp_token *token;
4929
4930 /* Peek at the next toen. */
4931 token = cp_lexer_peek_token (parser->lexer);
4932
4933 switch (token->type)
4934 {
4935 case CPP_EQ:
4936 op = NOP_EXPR;
4937 break;
4938
4939 case CPP_MULT_EQ:
4940 op = MULT_EXPR;
4941 break;
4942
4943 case CPP_DIV_EQ:
4944 op = TRUNC_DIV_EXPR;
4945 break;
4946
4947 case CPP_MOD_EQ:
4948 op = TRUNC_MOD_EXPR;
4949 break;
4950
4951 case CPP_PLUS_EQ:
4952 op = PLUS_EXPR;
4953 break;
4954
4955 case CPP_MINUS_EQ:
4956 op = MINUS_EXPR;
4957 break;
4958
4959 case CPP_RSHIFT_EQ:
4960 op = RSHIFT_EXPR;
4961 break;
4962
4963 case CPP_LSHIFT_EQ:
4964 op = LSHIFT_EXPR;
4965 break;
4966
4967 case CPP_AND_EQ:
4968 op = BIT_AND_EXPR;
4969 break;
4970
4971 case CPP_XOR_EQ:
4972 op = BIT_XOR_EXPR;
4973 break;
4974
4975 case CPP_OR_EQ:
4976 op = BIT_IOR_EXPR;
4977 break;
4978
4979 case CPP_MIN_EQ:
4980 op = MIN_EXPR;
4981 break;
4982
4983 case CPP_MAX_EQ:
4984 op = MAX_EXPR;
4985 break;
4986
4987 default:
4988 /* Nothing else is an assignment operator. */
4989 op = ERROR_MARK;
4990 }
4991
4992 /* If it was an assignment operator, consume it. */
4993 if (op != ERROR_MARK)
4994 cp_lexer_consume_token (parser->lexer);
4995
4996 return op;
4997}
4998
4999/* Parse an expression.
5000
5001 expression:
5002 assignment-expression
5003 expression , assignment-expression
5004
5005 Returns a representation of the expression. */
5006
5007static tree
94edc4ab 5008cp_parser_expression (cp_parser* parser)
a723baf1
MM
5009{
5010 tree expression = NULL_TREE;
a723baf1
MM
5011
5012 while (true)
5013 {
5014 tree assignment_expression;
5015
5016 /* Parse the next assignment-expression. */
5017 assignment_expression
5018 = cp_parser_assignment_expression (parser);
5019 /* If this is the first assignment-expression, we can just
5020 save it away. */
5021 if (!expression)
5022 expression = assignment_expression;
a723baf1 5023 else
d17811fd
MM
5024 expression = build_x_compound_expr (expression,
5025 assignment_expression);
a723baf1
MM
5026 /* If the next token is not a comma, then we are done with the
5027 expression. */
5028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5029 break;
5030 /* Consume the `,'. */
5031 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
5032 /* A comma operator cannot appear in a constant-expression. */
5033 if (parser->constant_expression_p)
5034 {
5035 if (!parser->allow_non_constant_expression_p)
d17811fd
MM
5036 expression
5037 = cp_parser_non_constant_expression ("a comma operator");
14d22dd6
MM
5038 parser->non_constant_expression_p = true;
5039 }
14d22dd6 5040 }
a723baf1
MM
5041
5042 return expression;
5043}
5044
5045/* Parse a constant-expression.
5046
5047 constant-expression:
14d22dd6
MM
5048 conditional-expression
5049
5050 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5051 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5052 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5053 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5054
5055static tree
14d22dd6
MM
5056cp_parser_constant_expression (cp_parser* parser,
5057 bool allow_non_constant_p,
5058 bool *non_constant_p)
a723baf1
MM
5059{
5060 bool saved_constant_expression_p;
14d22dd6
MM
5061 bool saved_allow_non_constant_expression_p;
5062 bool saved_non_constant_expression_p;
a723baf1
MM
5063 tree expression;
5064
5065 /* It might seem that we could simply parse the
5066 conditional-expression, and then check to see if it were
5067 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5068 one that the compiler can figure out is constant, possibly after
5069 doing some simplifications or optimizations. The standard has a
5070 precise definition of constant-expression, and we must honor
5071 that, even though it is somewhat more restrictive.
5072
5073 For example:
5074
5075 int i[(2, 3)];
5076
5077 is not a legal declaration, because `(2, 3)' is not a
5078 constant-expression. The `,' operator is forbidden in a
5079 constant-expression. However, GCC's constant-folding machinery
5080 will fold this operation to an INTEGER_CST for `3'. */
5081
14d22dd6 5082 /* Save the old settings. */
a723baf1 5083 saved_constant_expression_p = parser->constant_expression_p;
14d22dd6
MM
5084 saved_allow_non_constant_expression_p
5085 = parser->allow_non_constant_expression_p;
5086 saved_non_constant_expression_p = parser->non_constant_expression_p;
a723baf1
MM
5087 /* We are now parsing a constant-expression. */
5088 parser->constant_expression_p = true;
14d22dd6
MM
5089 parser->allow_non_constant_expression_p = allow_non_constant_p;
5090 parser->non_constant_expression_p = false;
39703eb9
MM
5091 /* Although the grammar says "conditional-expression", we parse an
5092 "assignment-expression", which also permits "throw-expression"
5093 and the use of assignment operators. In the case that
5094 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5095 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5096 actually essential that we look for an assignment-expression.
5097 For example, cp_parser_initializer_clauses uses this function to
5098 determine whether a particular assignment-expression is in fact
5099 constant. */
5100 expression = cp_parser_assignment_expression (parser);
14d22dd6 5101 /* Restore the old settings. */
a723baf1 5102 parser->constant_expression_p = saved_constant_expression_p;
14d22dd6
MM
5103 parser->allow_non_constant_expression_p
5104 = saved_allow_non_constant_expression_p;
5105 if (allow_non_constant_p)
5106 *non_constant_p = parser->non_constant_expression_p;
5107 parser->non_constant_expression_p = saved_non_constant_expression_p;
a723baf1
MM
5108
5109 return expression;
5110}
5111
5112/* Statements [gram.stmt.stmt] */
5113
5114/* Parse a statement.
5115
5116 statement:
5117 labeled-statement
5118 expression-statement
5119 compound-statement
5120 selection-statement
5121 iteration-statement
5122 jump-statement
5123 declaration-statement
5124 try-block */
5125
5126static void
a5bcc582 5127cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5128{
5129 tree statement;
5130 cp_token *token;
5131 int statement_line_number;
5132
5133 /* There is no statement yet. */
5134 statement = NULL_TREE;
5135 /* Peek at the next token. */
5136 token = cp_lexer_peek_token (parser->lexer);
5137 /* Remember the line number of the first token in the statement. */
82a98427 5138 statement_line_number = token->location.line;
a723baf1
MM
5139 /* If this is a keyword, then that will often determine what kind of
5140 statement we have. */
5141 if (token->type == CPP_KEYWORD)
5142 {
5143 enum rid keyword = token->keyword;
5144
5145 switch (keyword)
5146 {
5147 case RID_CASE:
5148 case RID_DEFAULT:
a5bcc582
NS
5149 statement = cp_parser_labeled_statement (parser,
5150 in_statement_expr_p);
a723baf1
MM
5151 break;
5152
5153 case RID_IF:
5154 case RID_SWITCH:
5155 statement = cp_parser_selection_statement (parser);
5156 break;
5157
5158 case RID_WHILE:
5159 case RID_DO:
5160 case RID_FOR:
5161 statement = cp_parser_iteration_statement (parser);
5162 break;
5163
5164 case RID_BREAK:
5165 case RID_CONTINUE:
5166 case RID_RETURN:
5167 case RID_GOTO:
5168 statement = cp_parser_jump_statement (parser);
5169 break;
5170
5171 case RID_TRY:
5172 statement = cp_parser_try_block (parser);
5173 break;
5174
5175 default:
5176 /* It might be a keyword like `int' that can start a
5177 declaration-statement. */
5178 break;
5179 }
5180 }
5181 else if (token->type == CPP_NAME)
5182 {
5183 /* If the next token is a `:', then we are looking at a
5184 labeled-statement. */
5185 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5186 if (token->type == CPP_COLON)
a5bcc582 5187 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5188 }
5189 /* Anything that starts with a `{' must be a compound-statement. */
5190 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5191 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5192
5193 /* Everything else must be a declaration-statement or an
5194 expression-statement. Try for the declaration-statement
5195 first, unless we are looking at a `;', in which case we know that
5196 we have an expression-statement. */
5197 if (!statement)
5198 {
5199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5200 {
5201 cp_parser_parse_tentatively (parser);
5202 /* Try to parse the declaration-statement. */
5203 cp_parser_declaration_statement (parser);
5204 /* If that worked, we're done. */
5205 if (cp_parser_parse_definitely (parser))
5206 return;
5207 }
5208 /* Look for an expression-statement instead. */
a5bcc582 5209 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5210 }
5211
5212 /* Set the line number for the statement. */
009ed910 5213 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5214 STMT_LINENO (statement) = statement_line_number;
5215}
5216
5217/* Parse a labeled-statement.
5218
5219 labeled-statement:
5220 identifier : statement
5221 case constant-expression : statement
5222 default : statement
5223
5224 Returns the new CASE_LABEL, for a `case' or `default' label. For
5225 an ordinary label, returns a LABEL_STMT. */
5226
5227static tree
a5bcc582 5228cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5229{
5230 cp_token *token;
5231 tree statement = NULL_TREE;
5232
5233 /* The next token should be an identifier. */
5234 token = cp_lexer_peek_token (parser->lexer);
5235 if (token->type != CPP_NAME
5236 && token->type != CPP_KEYWORD)
5237 {
5238 cp_parser_error (parser, "expected labeled-statement");
5239 return error_mark_node;
5240 }
5241
5242 switch (token->keyword)
5243 {
5244 case RID_CASE:
5245 {
5246 tree expr;
5247
5248 /* Consume the `case' token. */
5249 cp_lexer_consume_token (parser->lexer);
5250 /* Parse the constant-expression. */
14d22dd6 5251 expr = cp_parser_constant_expression (parser,
d17811fd 5252 /*allow_non_constant_p=*/false,
14d22dd6 5253 NULL);
a723baf1
MM
5254 /* Create the label. */
5255 statement = finish_case_label (expr, NULL_TREE);
5256 }
5257 break;
5258
5259 case RID_DEFAULT:
5260 /* Consume the `default' token. */
5261 cp_lexer_consume_token (parser->lexer);
5262 /* Create the label. */
5263 statement = finish_case_label (NULL_TREE, NULL_TREE);
5264 break;
5265
5266 default:
5267 /* Anything else must be an ordinary label. */
5268 statement = finish_label_stmt (cp_parser_identifier (parser));
5269 break;
5270 }
5271
5272 /* Require the `:' token. */
5273 cp_parser_require (parser, CPP_COLON, "`:'");
5274 /* Parse the labeled statement. */
a5bcc582 5275 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5276
5277 /* Return the label, in the case of a `case' or `default' label. */
5278 return statement;
5279}
5280
5281/* Parse an expression-statement.
5282
5283 expression-statement:
5284 expression [opt] ;
5285
5286 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5287 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5288 indicates whether this expression-statement is part of an
5289 expression statement. */
a723baf1
MM
5290
5291static tree
a5bcc582 5292cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5293{
a5bcc582 5294 tree statement = NULL_TREE;
a723baf1 5295
a5bcc582
NS
5296 /* If the next token is a ';', then there is no expression
5297 statement. */
a723baf1 5298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582
NS
5299 statement = cp_parser_expression (parser);
5300
a723baf1 5301 /* Consume the final `;'. */
e0860732 5302 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5303
a5bcc582
NS
5304 if (in_statement_expr_p
5305 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5306 {
5307 /* This is the final expression statement of a statement
5308 expression. */
5309 statement = finish_stmt_expr_expr (statement);
5310 }
5311 else if (statement)
5312 statement = finish_expr_stmt (statement);
5313 else
5314 finish_stmt ();
5315
a723baf1
MM
5316 return statement;
5317}
5318
5319/* Parse a compound-statement.
5320
5321 compound-statement:
5322 { statement-seq [opt] }
5323
5324 Returns a COMPOUND_STMT representing the statement. */
5325
5326static tree
a5bcc582 5327cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5328{
5329 tree compound_stmt;
5330
5331 /* Consume the `{'. */
5332 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5333 return error_mark_node;
5334 /* Begin the compound-statement. */
7a3397c7 5335 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5336 /* Parse an (optional) statement-seq. */
a5bcc582 5337 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5338 /* Finish the compound-statement. */
7a3397c7 5339 finish_compound_stmt (compound_stmt);
a723baf1
MM
5340 /* Consume the `}'. */
5341 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5342
5343 return compound_stmt;
5344}
5345
5346/* Parse an (optional) statement-seq.
5347
5348 statement-seq:
5349 statement
5350 statement-seq [opt] statement */
5351
5352static void
a5bcc582 5353cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5354{
5355 /* Scan statements until there aren't any more. */
5356 while (true)
5357 {
5358 /* If we're looking at a `}', then we've run out of statements. */
5359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5360 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5361 break;
5362
5363 /* Parse the statement. */
a5bcc582 5364 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5365 }
5366}
5367
5368/* Parse a selection-statement.
5369
5370 selection-statement:
5371 if ( condition ) statement
5372 if ( condition ) statement else statement
5373 switch ( condition ) statement
5374
5375 Returns the new IF_STMT or SWITCH_STMT. */
5376
5377static tree
94edc4ab 5378cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5379{
5380 cp_token *token;
5381 enum rid keyword;
5382
5383 /* Peek at the next token. */
5384 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5385
5386 /* See what kind of keyword it is. */
5387 keyword = token->keyword;
5388 switch (keyword)
5389 {
5390 case RID_IF:
5391 case RID_SWITCH:
5392 {
5393 tree statement;
5394 tree condition;
5395
5396 /* Look for the `('. */
5397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5398 {
5399 cp_parser_skip_to_end_of_statement (parser);
5400 return error_mark_node;
5401 }
5402
5403 /* Begin the selection-statement. */
5404 if (keyword == RID_IF)
5405 statement = begin_if_stmt ();
5406 else
5407 statement = begin_switch_stmt ();
5408
5409 /* Parse the condition. */
5410 condition = cp_parser_condition (parser);
5411 /* Look for the `)'. */
5412 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 5413 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
5414
5415 if (keyword == RID_IF)
5416 {
5417 tree then_stmt;
5418
5419 /* Add the condition. */
5420 finish_if_stmt_cond (condition, statement);
5421
5422 /* Parse the then-clause. */
5423 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5424 finish_then_clause (statement);
5425
5426 /* If the next token is `else', parse the else-clause. */
5427 if (cp_lexer_next_token_is_keyword (parser->lexer,
5428 RID_ELSE))
5429 {
5430 tree else_stmt;
5431
5432 /* Consume the `else' keyword. */
5433 cp_lexer_consume_token (parser->lexer);
5434 /* Parse the else-clause. */
5435 else_stmt
5436 = cp_parser_implicitly_scoped_statement (parser);
5437 finish_else_clause (statement);
5438 }
5439
5440 /* Now we're all done with the if-statement. */
5441 finish_if_stmt ();
5442 }
5443 else
5444 {
5445 tree body;
5446
5447 /* Add the condition. */
5448 finish_switch_cond (condition, statement);
5449
5450 /* Parse the body of the switch-statement. */
5451 body = cp_parser_implicitly_scoped_statement (parser);
5452
5453 /* Now we're all done with the switch-statement. */
5454 finish_switch_stmt (statement);
5455 }
5456
5457 return statement;
5458 }
5459 break;
5460
5461 default:
5462 cp_parser_error (parser, "expected selection-statement");
5463 return error_mark_node;
5464 }
5465}
5466
5467/* Parse a condition.
5468
5469 condition:
5470 expression
5471 type-specifier-seq declarator = assignment-expression
5472
5473 GNU Extension:
5474
5475 condition:
5476 type-specifier-seq declarator asm-specification [opt]
5477 attributes [opt] = assignment-expression
5478
5479 Returns the expression that should be tested. */
5480
5481static tree
94edc4ab 5482cp_parser_condition (cp_parser* parser)
a723baf1
MM
5483{
5484 tree type_specifiers;
5485 const char *saved_message;
5486
5487 /* Try the declaration first. */
5488 cp_parser_parse_tentatively (parser);
5489 /* New types are not allowed in the type-specifier-seq for a
5490 condition. */
5491 saved_message = parser->type_definition_forbidden_message;
5492 parser->type_definition_forbidden_message
5493 = "types may not be defined in conditions";
5494 /* Parse the type-specifier-seq. */
5495 type_specifiers = cp_parser_type_specifier_seq (parser);
5496 /* Restore the saved message. */
5497 parser->type_definition_forbidden_message = saved_message;
5498 /* If all is well, we might be looking at a declaration. */
5499 if (!cp_parser_error_occurred (parser))
5500 {
5501 tree decl;
5502 tree asm_specification;
5503 tree attributes;
5504 tree declarator;
5505 tree initializer = NULL_TREE;
5506
5507 /* Parse the declarator. */
62b8a44e 5508 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
5509 /*ctor_dtor_or_conv_p=*/NULL);
5510 /* Parse the attributes. */
5511 attributes = cp_parser_attributes_opt (parser);
5512 /* Parse the asm-specification. */
5513 asm_specification = cp_parser_asm_specification_opt (parser);
5514 /* If the next token is not an `=', then we might still be
5515 looking at an expression. For example:
5516
5517 if (A(a).x)
5518
5519 looks like a decl-specifier-seq and a declarator -- but then
5520 there is no `=', so this is an expression. */
5521 cp_parser_require (parser, CPP_EQ, "`='");
5522 /* If we did see an `=', then we are looking at a declaration
5523 for sure. */
5524 if (cp_parser_parse_definitely (parser))
5525 {
5526 /* Create the declaration. */
5527 decl = start_decl (declarator, type_specifiers,
5528 /*initialized_p=*/true,
5529 attributes, /*prefix_attributes=*/NULL_TREE);
5530 /* Parse the assignment-expression. */
5531 initializer = cp_parser_assignment_expression (parser);
5532
5533 /* Process the initializer. */
5534 cp_finish_decl (decl,
5535 initializer,
5536 asm_specification,
5537 LOOKUP_ONLYCONVERTING);
5538
5539 return convert_from_reference (decl);
5540 }
5541 }
5542 /* If we didn't even get past the declarator successfully, we are
5543 definitely not looking at a declaration. */
5544 else
5545 cp_parser_abort_tentative_parse (parser);
5546
5547 /* Otherwise, we are looking at an expression. */
5548 return cp_parser_expression (parser);
5549}
5550
5551/* Parse an iteration-statement.
5552
5553 iteration-statement:
5554 while ( condition ) statement
5555 do statement while ( expression ) ;
5556 for ( for-init-statement condition [opt] ; expression [opt] )
5557 statement
5558
5559 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5560
5561static tree
94edc4ab 5562cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5563{
5564 cp_token *token;
5565 enum rid keyword;
5566 tree statement;
5567
5568 /* Peek at the next token. */
5569 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5570 if (!token)
5571 return error_mark_node;
5572
5573 /* See what kind of keyword it is. */
5574 keyword = token->keyword;
5575 switch (keyword)
5576 {
5577 case RID_WHILE:
5578 {
5579 tree condition;
5580
5581 /* Begin the while-statement. */
5582 statement = begin_while_stmt ();
5583 /* Look for the `('. */
5584 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5585 /* Parse the condition. */
5586 condition = cp_parser_condition (parser);
5587 finish_while_stmt_cond (condition, statement);
5588 /* Look for the `)'. */
5589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5590 /* Parse the dependent statement. */
5591 cp_parser_already_scoped_statement (parser);
5592 /* We're done with the while-statement. */
5593 finish_while_stmt (statement);
5594 }
5595 break;
5596
5597 case RID_DO:
5598 {
5599 tree expression;
5600
5601 /* Begin the do-statement. */
5602 statement = begin_do_stmt ();
5603 /* Parse the body of the do-statement. */
5604 cp_parser_implicitly_scoped_statement (parser);
5605 finish_do_body (statement);
5606 /* Look for the `while' keyword. */
5607 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5608 /* Look for the `('. */
5609 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5610 /* Parse the expression. */
5611 expression = cp_parser_expression (parser);
5612 /* We're done with the do-statement. */
5613 finish_do_stmt (expression, statement);
5614 /* Look for the `)'. */
5615 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5616 /* Look for the `;'. */
5617 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5618 }
5619 break;
5620
5621 case RID_FOR:
5622 {
5623 tree condition = NULL_TREE;
5624 tree expression = NULL_TREE;
5625
5626 /* Begin the for-statement. */
5627 statement = begin_for_stmt ();
5628 /* Look for the `('. */
5629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5630 /* Parse the initialization. */
5631 cp_parser_for_init_statement (parser);
5632 finish_for_init_stmt (statement);
5633
5634 /* If there's a condition, process it. */
5635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5636 condition = cp_parser_condition (parser);
5637 finish_for_cond (condition, statement);
5638 /* Look for the `;'. */
5639 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5640
5641 /* If there's an expression, process it. */
5642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5643 expression = cp_parser_expression (parser);
5644 finish_for_expr (expression, statement);
5645 /* Look for the `)'. */
5646 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5647
5648 /* Parse the body of the for-statement. */
5649 cp_parser_already_scoped_statement (parser);
5650
5651 /* We're done with the for-statement. */
5652 finish_for_stmt (statement);
5653 }
5654 break;
5655
5656 default:
5657 cp_parser_error (parser, "expected iteration-statement");
5658 statement = error_mark_node;
5659 break;
5660 }
5661
5662 return statement;
5663}
5664
5665/* Parse a for-init-statement.
5666
5667 for-init-statement:
5668 expression-statement
5669 simple-declaration */
5670
5671static void
94edc4ab 5672cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5673{
5674 /* If the next token is a `;', then we have an empty
34cd5ae7 5675 expression-statement. Grammatically, this is also a
a723baf1
MM
5676 simple-declaration, but an invalid one, because it does not
5677 declare anything. Therefore, if we did not handle this case
5678 specially, we would issue an error message about an invalid
5679 declaration. */
5680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5681 {
5682 /* We're going to speculatively look for a declaration, falling back
5683 to an expression, if necessary. */
5684 cp_parser_parse_tentatively (parser);
5685 /* Parse the declaration. */
5686 cp_parser_simple_declaration (parser,
5687 /*function_definition_allowed_p=*/false);
5688 /* If the tentative parse failed, then we shall need to look for an
5689 expression-statement. */
5690 if (cp_parser_parse_definitely (parser))
5691 return;
5692 }
5693
a5bcc582 5694 cp_parser_expression_statement (parser, false);
a723baf1
MM
5695}
5696
5697/* Parse a jump-statement.
5698
5699 jump-statement:
5700 break ;
5701 continue ;
5702 return expression [opt] ;
5703 goto identifier ;
5704
5705 GNU extension:
5706
5707 jump-statement:
5708 goto * expression ;
5709
5710 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5711 GOTO_STMT. */
5712
5713static tree
94edc4ab 5714cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
5715{
5716 tree statement = error_mark_node;
5717 cp_token *token;
5718 enum rid keyword;
5719
5720 /* Peek at the next token. */
5721 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5722 if (!token)
5723 return error_mark_node;
5724
5725 /* See what kind of keyword it is. */
5726 keyword = token->keyword;
5727 switch (keyword)
5728 {
5729 case RID_BREAK:
5730 statement = finish_break_stmt ();
5731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5732 break;
5733
5734 case RID_CONTINUE:
5735 statement = finish_continue_stmt ();
5736 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5737 break;
5738
5739 case RID_RETURN:
5740 {
5741 tree expr;
5742
5743 /* If the next token is a `;', then there is no
5744 expression. */
5745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5746 expr = cp_parser_expression (parser);
5747 else
5748 expr = NULL_TREE;
5749 /* Build the return-statement. */
5750 statement = finish_return_stmt (expr);
5751 /* Look for the final `;'. */
5752 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5753 }
5754 break;
5755
5756 case RID_GOTO:
5757 /* Create the goto-statement. */
5758 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5759 {
5760 /* Issue a warning about this use of a GNU extension. */
5761 if (pedantic)
5762 pedwarn ("ISO C++ forbids computed gotos");
5763 /* Consume the '*' token. */
5764 cp_lexer_consume_token (parser->lexer);
5765 /* Parse the dependent expression. */
5766 finish_goto_stmt (cp_parser_expression (parser));
5767 }
5768 else
5769 finish_goto_stmt (cp_parser_identifier (parser));
5770 /* Look for the final `;'. */
5771 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5772 break;
5773
5774 default:
5775 cp_parser_error (parser, "expected jump-statement");
5776 break;
5777 }
5778
5779 return statement;
5780}
5781
5782/* Parse a declaration-statement.
5783
5784 declaration-statement:
5785 block-declaration */
5786
5787static void
94edc4ab 5788cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
5789{
5790 /* Parse the block-declaration. */
5791 cp_parser_block_declaration (parser, /*statement_p=*/true);
5792
5793 /* Finish off the statement. */
5794 finish_stmt ();
5795}
5796
5797/* Some dependent statements (like `if (cond) statement'), are
5798 implicitly in their own scope. In other words, if the statement is
5799 a single statement (as opposed to a compound-statement), it is
5800 none-the-less treated as if it were enclosed in braces. Any
5801 declarations appearing in the dependent statement are out of scope
5802 after control passes that point. This function parses a statement,
5803 but ensures that is in its own scope, even if it is not a
5804 compound-statement.
5805
5806 Returns the new statement. */
5807
5808static tree
94edc4ab 5809cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
5810{
5811 tree statement;
5812
5813 /* If the token is not a `{', then we must take special action. */
5814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5815 {
5816 /* Create a compound-statement. */
7a3397c7 5817 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5818 /* Parse the dependent-statement. */
a5bcc582 5819 cp_parser_statement (parser, false);
a723baf1 5820 /* Finish the dummy compound-statement. */
7a3397c7 5821 finish_compound_stmt (statement);
a723baf1
MM
5822 }
5823 /* Otherwise, we simply parse the statement directly. */
5824 else
a5bcc582 5825 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5826
5827 /* Return the statement. */
5828 return statement;
5829}
5830
5831/* For some dependent statements (like `while (cond) statement'), we
5832 have already created a scope. Therefore, even if the dependent
5833 statement is a compound-statement, we do not want to create another
5834 scope. */
5835
5836static void
94edc4ab 5837cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
5838{
5839 /* If the token is not a `{', then we must take special action. */
5840 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5841 {
5842 tree statement;
5843
5844 /* Create a compound-statement. */
7a3397c7 5845 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 5846 /* Parse the dependent-statement. */
a5bcc582 5847 cp_parser_statement (parser, false);
a723baf1 5848 /* Finish the dummy compound-statement. */
7a3397c7 5849 finish_compound_stmt (statement);
a723baf1
MM
5850 }
5851 /* Otherwise, we simply parse the statement directly. */
5852 else
a5bcc582 5853 cp_parser_statement (parser, false);
a723baf1
MM
5854}
5855
5856/* Declarations [gram.dcl.dcl] */
5857
5858/* Parse an optional declaration-sequence.
5859
5860 declaration-seq:
5861 declaration
5862 declaration-seq declaration */
5863
5864static void
94edc4ab 5865cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
5866{
5867 while (true)
5868 {
5869 cp_token *token;
5870
5871 token = cp_lexer_peek_token (parser->lexer);
5872
5873 if (token->type == CPP_CLOSE_BRACE
5874 || token->type == CPP_EOF)
5875 break;
5876
5877 if (token->type == CPP_SEMICOLON)
5878 {
5879 /* A declaration consisting of a single semicolon is
5880 invalid. Allow it unless we're being pedantic. */
5881 if (pedantic)
5882 pedwarn ("extra `;'");
5883 cp_lexer_consume_token (parser->lexer);
5884 continue;
5885 }
5886
c838d82f 5887 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 5888 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
5889 while (pending_lang_change > 0)
5890 {
5891 push_lang_context (lang_name_c);
5892 --pending_lang_change;
5893 }
5894 while (pending_lang_change < 0)
5895 {
5896 pop_lang_context ();
5897 ++pending_lang_change;
5898 }
5899
5900 /* Parse the declaration itself. */
a723baf1
MM
5901 cp_parser_declaration (parser);
5902 }
5903}
5904
5905/* Parse a declaration.
5906
5907 declaration:
5908 block-declaration
5909 function-definition
5910 template-declaration
5911 explicit-instantiation
5912 explicit-specialization
5913 linkage-specification
1092805d
MM
5914 namespace-definition
5915
5916 GNU extension:
5917
5918 declaration:
5919 __extension__ declaration */
a723baf1
MM
5920
5921static void
94edc4ab 5922cp_parser_declaration (cp_parser* parser)
a723baf1
MM
5923{
5924 cp_token token1;
5925 cp_token token2;
1092805d
MM
5926 int saved_pedantic;
5927
5928 /* Check for the `__extension__' keyword. */
5929 if (cp_parser_extension_opt (parser, &saved_pedantic))
5930 {
5931 /* Parse the qualified declaration. */
5932 cp_parser_declaration (parser);
5933 /* Restore the PEDANTIC flag. */
5934 pedantic = saved_pedantic;
5935
5936 return;
5937 }
a723baf1
MM
5938
5939 /* Try to figure out what kind of declaration is present. */
5940 token1 = *cp_lexer_peek_token (parser->lexer);
5941 if (token1.type != CPP_EOF)
5942 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5943
5944 /* If the next token is `extern' and the following token is a string
5945 literal, then we have a linkage specification. */
5946 if (token1.keyword == RID_EXTERN
5947 && cp_parser_is_string_literal (&token2))
5948 cp_parser_linkage_specification (parser);
5949 /* If the next token is `template', then we have either a template
5950 declaration, an explicit instantiation, or an explicit
5951 specialization. */
5952 else if (token1.keyword == RID_TEMPLATE)
5953 {
5954 /* `template <>' indicates a template specialization. */
5955 if (token2.type == CPP_LESS
5956 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5957 cp_parser_explicit_specialization (parser);
5958 /* `template <' indicates a template declaration. */
5959 else if (token2.type == CPP_LESS)
5960 cp_parser_template_declaration (parser, /*member_p=*/false);
5961 /* Anything else must be an explicit instantiation. */
5962 else
5963 cp_parser_explicit_instantiation (parser);
5964 }
5965 /* If the next token is `export', then we have a template
5966 declaration. */
5967 else if (token1.keyword == RID_EXPORT)
5968 cp_parser_template_declaration (parser, /*member_p=*/false);
5969 /* If the next token is `extern', 'static' or 'inline' and the one
5970 after that is `template', we have a GNU extended explicit
5971 instantiation directive. */
5972 else if (cp_parser_allow_gnu_extensions_p (parser)
5973 && (token1.keyword == RID_EXTERN
5974 || token1.keyword == RID_STATIC
5975 || token1.keyword == RID_INLINE)
5976 && token2.keyword == RID_TEMPLATE)
5977 cp_parser_explicit_instantiation (parser);
5978 /* If the next token is `namespace', check for a named or unnamed
5979 namespace definition. */
5980 else if (token1.keyword == RID_NAMESPACE
5981 && (/* A named namespace definition. */
5982 (token2.type == CPP_NAME
5983 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5984 == CPP_OPEN_BRACE))
5985 /* An unnamed namespace definition. */
5986 || token2.type == CPP_OPEN_BRACE))
5987 cp_parser_namespace_definition (parser);
5988 /* We must have either a block declaration or a function
5989 definition. */
5990 else
5991 /* Try to parse a block-declaration, or a function-definition. */
5992 cp_parser_block_declaration (parser, /*statement_p=*/false);
5993}
5994
5995/* Parse a block-declaration.
5996
5997 block-declaration:
5998 simple-declaration
5999 asm-definition
6000 namespace-alias-definition
6001 using-declaration
6002 using-directive
6003
6004 GNU Extension:
6005
6006 block-declaration:
6007 __extension__ block-declaration
6008 label-declaration
6009
34cd5ae7 6010 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6011 part of a declaration-statement. */
6012
6013static void
6014cp_parser_block_declaration (cp_parser *parser,
6015 bool statement_p)
6016{
6017 cp_token *token1;
6018 int saved_pedantic;
6019
6020 /* Check for the `__extension__' keyword. */
6021 if (cp_parser_extension_opt (parser, &saved_pedantic))
6022 {
6023 /* Parse the qualified declaration. */
6024 cp_parser_block_declaration (parser, statement_p);
6025 /* Restore the PEDANTIC flag. */
6026 pedantic = saved_pedantic;
6027
6028 return;
6029 }
6030
6031 /* Peek at the next token to figure out which kind of declaration is
6032 present. */
6033 token1 = cp_lexer_peek_token (parser->lexer);
6034
6035 /* If the next keyword is `asm', we have an asm-definition. */
6036 if (token1->keyword == RID_ASM)
6037 {
6038 if (statement_p)
6039 cp_parser_commit_to_tentative_parse (parser);
6040 cp_parser_asm_definition (parser);
6041 }
6042 /* If the next keyword is `namespace', we have a
6043 namespace-alias-definition. */
6044 else if (token1->keyword == RID_NAMESPACE)
6045 cp_parser_namespace_alias_definition (parser);
6046 /* If the next keyword is `using', we have either a
6047 using-declaration or a using-directive. */
6048 else if (token1->keyword == RID_USING)
6049 {
6050 cp_token *token2;
6051
6052 if (statement_p)
6053 cp_parser_commit_to_tentative_parse (parser);
6054 /* If the token after `using' is `namespace', then we have a
6055 using-directive. */
6056 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6057 if (token2->keyword == RID_NAMESPACE)
6058 cp_parser_using_directive (parser);
6059 /* Otherwise, it's a using-declaration. */
6060 else
6061 cp_parser_using_declaration (parser);
6062 }
6063 /* If the next keyword is `__label__' we have a label declaration. */
6064 else if (token1->keyword == RID_LABEL)
6065 {
6066 if (statement_p)
6067 cp_parser_commit_to_tentative_parse (parser);
6068 cp_parser_label_declaration (parser);
6069 }
6070 /* Anything else must be a simple-declaration. */
6071 else
6072 cp_parser_simple_declaration (parser, !statement_p);
6073}
6074
6075/* Parse a simple-declaration.
6076
6077 simple-declaration:
6078 decl-specifier-seq [opt] init-declarator-list [opt] ;
6079
6080 init-declarator-list:
6081 init-declarator
6082 init-declarator-list , init-declarator
6083
34cd5ae7 6084 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6085 function-definition as a simple-declaration. */
a723baf1
MM
6086
6087static void
94edc4ab
NN
6088cp_parser_simple_declaration (cp_parser* parser,
6089 bool function_definition_allowed_p)
a723baf1
MM
6090{
6091 tree decl_specifiers;
6092 tree attributes;
560ad596 6093 int declares_class_or_enum;
a723baf1
MM
6094 bool saw_declarator;
6095
6096 /* Defer access checks until we know what is being declared; the
6097 checks for names appearing in the decl-specifier-seq should be
6098 done as if we were in the scope of the thing being declared. */
8d241e0b 6099 push_deferring_access_checks (dk_deferred);
cf22909c 6100
a723baf1
MM
6101 /* Parse the decl-specifier-seq. We have to keep track of whether
6102 or not the decl-specifier-seq declares a named class or
6103 enumeration type, since that is the only case in which the
6104 init-declarator-list is allowed to be empty.
6105
6106 [dcl.dcl]
6107
6108 In a simple-declaration, the optional init-declarator-list can be
6109 omitted only when declaring a class or enumeration, that is when
6110 the decl-specifier-seq contains either a class-specifier, an
6111 elaborated-type-specifier, or an enum-specifier. */
6112 decl_specifiers
6113 = cp_parser_decl_specifier_seq (parser,
6114 CP_PARSER_FLAGS_OPTIONAL,
6115 &attributes,
6116 &declares_class_or_enum);
6117 /* We no longer need to defer access checks. */
cf22909c 6118 stop_deferring_access_checks ();
24c0ef37 6119
39703eb9
MM
6120 /* In a block scope, a valid declaration must always have a
6121 decl-specifier-seq. By not trying to parse declarators, we can
6122 resolve the declaration/expression ambiguity more quickly. */
6123 if (!function_definition_allowed_p && !decl_specifiers)
6124 {
6125 cp_parser_error (parser, "expected declaration");
6126 goto done;
6127 }
6128
8fbc5ae7
MM
6129 /* If the next two tokens are both identifiers, the code is
6130 erroneous. The usual cause of this situation is code like:
6131
6132 T t;
6133
6134 where "T" should name a type -- but does not. */
6135 if (cp_parser_diagnose_invalid_type_name (parser))
6136 {
8d241e0b 6137 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6138 looking at a declaration. */
6139 cp_parser_commit_to_tentative_parse (parser);
6140 /* Give up. */
39703eb9 6141 goto done;
8fbc5ae7
MM
6142 }
6143
a723baf1
MM
6144 /* Keep going until we hit the `;' at the end of the simple
6145 declaration. */
6146 saw_declarator = false;
6147 while (cp_lexer_next_token_is_not (parser->lexer,
6148 CPP_SEMICOLON))
6149 {
6150 cp_token *token;
6151 bool function_definition_p;
560ad596 6152 tree decl;
a723baf1
MM
6153
6154 saw_declarator = true;
6155 /* Parse the init-declarator. */
560ad596
MM
6156 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6157 function_definition_allowed_p,
6158 /*member_p=*/false,
6159 declares_class_or_enum,
6160 &function_definition_p);
1fb3244a
MM
6161 /* If an error occurred while parsing tentatively, exit quickly.
6162 (That usually happens when in the body of a function; each
6163 statement is treated as a declaration-statement until proven
6164 otherwise.) */
6165 if (cp_parser_error_occurred (parser))
39703eb9 6166 goto done;
a723baf1
MM
6167 /* Handle function definitions specially. */
6168 if (function_definition_p)
6169 {
6170 /* If the next token is a `,', then we are probably
6171 processing something like:
6172
6173 void f() {}, *p;
6174
6175 which is erroneous. */
6176 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6177 error ("mixing declarations and function-definitions is forbidden");
6178 /* Otherwise, we're done with the list of declarators. */
6179 else
24c0ef37 6180 {
cf22909c 6181 pop_deferring_access_checks ();
24c0ef37
GS
6182 return;
6183 }
a723baf1
MM
6184 }
6185 /* The next token should be either a `,' or a `;'. */
6186 token = cp_lexer_peek_token (parser->lexer);
6187 /* If it's a `,', there are more declarators to come. */
6188 if (token->type == CPP_COMMA)
6189 cp_lexer_consume_token (parser->lexer);
6190 /* If it's a `;', we are done. */
6191 else if (token->type == CPP_SEMICOLON)
6192 break;
6193 /* Anything else is an error. */
6194 else
6195 {
6196 cp_parser_error (parser, "expected `,' or `;'");
6197 /* Skip tokens until we reach the end of the statement. */
6198 cp_parser_skip_to_end_of_statement (parser);
39703eb9 6199 goto done;
a723baf1
MM
6200 }
6201 /* After the first time around, a function-definition is not
6202 allowed -- even if it was OK at first. For example:
6203
6204 int i, f() {}
6205
6206 is not valid. */
6207 function_definition_allowed_p = false;
6208 }
6209
6210 /* Issue an error message if no declarators are present, and the
6211 decl-specifier-seq does not itself declare a class or
6212 enumeration. */
6213 if (!saw_declarator)
6214 {
6215 if (cp_parser_declares_only_class_p (parser))
6216 shadow_tag (decl_specifiers);
6217 /* Perform any deferred access checks. */
cf22909c 6218 perform_deferred_access_checks ();
a723baf1
MM
6219 }
6220
6221 /* Consume the `;'. */
6222 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6223
39703eb9
MM
6224 done:
6225 pop_deferring_access_checks ();
a723baf1
MM
6226}
6227
6228/* Parse a decl-specifier-seq.
6229
6230 decl-specifier-seq:
6231 decl-specifier-seq [opt] decl-specifier
6232
6233 decl-specifier:
6234 storage-class-specifier
6235 type-specifier
6236 function-specifier
6237 friend
6238 typedef
6239
6240 GNU Extension:
6241
6242 decl-specifier-seq:
6243 decl-specifier-seq [opt] attributes
6244
6245 Returns a TREE_LIST, giving the decl-specifiers in the order they
6246 appear in the source code. The TREE_VALUE of each node is the
6247 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6248 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6249 representation of a type-specifier, see cp_parser_type_specifier.
6250
6251 If there are attributes, they will be stored in *ATTRIBUTES,
6252 represented as described above cp_parser_attributes.
6253
6254 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6255 appears, and the entity that will be a friend is not going to be a
6256 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6257 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
560ad596
MM
6258 friendship is granted might not be a class.
6259
6260 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6261 *flags:
6262
6263 1: one of the decl-specifiers is an elaborated-type-specifier
6264 2: one of the decl-specifiers is an enum-specifier or a
6265 class-specifier
6266
6267 */
a723baf1
MM
6268
6269static tree
94edc4ab
NN
6270cp_parser_decl_specifier_seq (cp_parser* parser,
6271 cp_parser_flags flags,
6272 tree* attributes,
560ad596 6273 int* declares_class_or_enum)
a723baf1
MM
6274{
6275 tree decl_specs = NULL_TREE;
6276 bool friend_p = false;
f2ce60b8
NS
6277 bool constructor_possible_p = !parser->in_declarator_p;
6278
a723baf1 6279 /* Assume no class or enumeration type is declared. */
560ad596 6280 *declares_class_or_enum = 0;
a723baf1
MM
6281
6282 /* Assume there are no attributes. */
6283 *attributes = NULL_TREE;
6284
6285 /* Keep reading specifiers until there are no more to read. */
6286 while (true)
6287 {
6288 tree decl_spec = NULL_TREE;
6289 bool constructor_p;
6290 cp_token *token;
6291
6292 /* Peek at the next token. */
6293 token = cp_lexer_peek_token (parser->lexer);
6294 /* Handle attributes. */
6295 if (token->keyword == RID_ATTRIBUTE)
6296 {
6297 /* Parse the attributes. */
6298 decl_spec = cp_parser_attributes_opt (parser);
6299 /* Add them to the list. */
6300 *attributes = chainon (*attributes, decl_spec);
6301 continue;
6302 }
6303 /* If the next token is an appropriate keyword, we can simply
6304 add it to the list. */
6305 switch (token->keyword)
6306 {
6307 case RID_FRIEND:
6308 /* decl-specifier:
6309 friend */
1918facf
SB
6310 if (friend_p)
6311 error ("duplicate `friend'");
6312 else
6313 friend_p = true;
a723baf1
MM
6314 /* The representation of the specifier is simply the
6315 appropriate TREE_IDENTIFIER node. */
6316 decl_spec = token->value;
6317 /* Consume the token. */
6318 cp_lexer_consume_token (parser->lexer);
6319 break;
6320
6321 /* function-specifier:
6322 inline
6323 virtual
6324 explicit */
6325 case RID_INLINE:
6326 case RID_VIRTUAL:
6327 case RID_EXPLICIT:
6328 decl_spec = cp_parser_function_specifier_opt (parser);
6329 break;
6330
6331 /* decl-specifier:
6332 typedef */
6333 case RID_TYPEDEF:
6334 /* The representation of the specifier is simply the
6335 appropriate TREE_IDENTIFIER node. */
6336 decl_spec = token->value;
6337 /* Consume the token. */
6338 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6339 /* A constructor declarator cannot appear in a typedef. */
6340 constructor_possible_p = false;
c006d942
MM
6341 /* The "typedef" keyword can only occur in a declaration; we
6342 may as well commit at this point. */
6343 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6344 break;
6345
6346 /* storage-class-specifier:
6347 auto
6348 register
6349 static
6350 extern
6351 mutable
6352
6353 GNU Extension:
6354 thread */
6355 case RID_AUTO:
6356 case RID_REGISTER:
6357 case RID_STATIC:
6358 case RID_EXTERN:
6359 case RID_MUTABLE:
6360 case RID_THREAD:
6361 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6362 break;
6363
6364 default:
6365 break;
6366 }
6367
6368 /* Constructors are a special case. The `S' in `S()' is not a
6369 decl-specifier; it is the beginning of the declarator. */
6370 constructor_p = (!decl_spec
2050a1bb 6371 && constructor_possible_p
a723baf1
MM
6372 && cp_parser_constructor_declarator_p (parser,
6373 friend_p));
6374
6375 /* If we don't have a DECL_SPEC yet, then we must be looking at
6376 a type-specifier. */
6377 if (!decl_spec && !constructor_p)
6378 {
560ad596 6379 int decl_spec_declares_class_or_enum;
a723baf1
MM
6380 bool is_cv_qualifier;
6381
6382 decl_spec
6383 = cp_parser_type_specifier (parser, flags,
6384 friend_p,
6385 /*is_declaration=*/true,
6386 &decl_spec_declares_class_or_enum,
6387 &is_cv_qualifier);
6388
6389 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6390
6391 /* If this type-specifier referenced a user-defined type
6392 (a typedef, class-name, etc.), then we can't allow any
6393 more such type-specifiers henceforth.
6394
6395 [dcl.spec]
6396
6397 The longest sequence of decl-specifiers that could
6398 possibly be a type name is taken as the
6399 decl-specifier-seq of a declaration. The sequence shall
6400 be self-consistent as described below.
6401
6402 [dcl.type]
6403
6404 As a general rule, at most one type-specifier is allowed
6405 in the complete decl-specifier-seq of a declaration. The
6406 only exceptions are the following:
6407
6408 -- const or volatile can be combined with any other
6409 type-specifier.
6410
6411 -- signed or unsigned can be combined with char, long,
6412 short, or int.
6413
6414 -- ..
6415
6416 Example:
6417
6418 typedef char* Pc;
6419 void g (const int Pc);
6420
6421 Here, Pc is *not* part of the decl-specifier seq; it's
6422 the declarator. Therefore, once we see a type-specifier
6423 (other than a cv-qualifier), we forbid any additional
6424 user-defined types. We *do* still allow things like `int
6425 int' to be considered a decl-specifier-seq, and issue the
6426 error message later. */
6427 if (decl_spec && !is_cv_qualifier)
6428 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6429 /* A constructor declarator cannot follow a type-specifier. */
6430 if (decl_spec)
6431 constructor_possible_p = false;
a723baf1
MM
6432 }
6433
6434 /* If we still do not have a DECL_SPEC, then there are no more
6435 decl-specifiers. */
6436 if (!decl_spec)
6437 {
6438 /* Issue an error message, unless the entire construct was
6439 optional. */
6440 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6441 {
6442 cp_parser_error (parser, "expected decl specifier");
6443 return error_mark_node;
6444 }
6445
6446 break;
6447 }
6448
6449 /* Add the DECL_SPEC to the list of specifiers. */
6450 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6451
6452 /* After we see one decl-specifier, further decl-specifiers are
6453 always optional. */
6454 flags |= CP_PARSER_FLAGS_OPTIONAL;
6455 }
6456
6457 /* We have built up the DECL_SPECS in reverse order. Return them in
6458 the correct order. */
6459 return nreverse (decl_specs);
6460}
6461
6462/* Parse an (optional) storage-class-specifier.
6463
6464 storage-class-specifier:
6465 auto
6466 register
6467 static
6468 extern
6469 mutable
6470
6471 GNU Extension:
6472
6473 storage-class-specifier:
6474 thread
6475
6476 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6477
6478static tree
94edc4ab 6479cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6480{
6481 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6482 {
6483 case RID_AUTO:
6484 case RID_REGISTER:
6485 case RID_STATIC:
6486 case RID_EXTERN:
6487 case RID_MUTABLE:
6488 case RID_THREAD:
6489 /* Consume the token. */
6490 return cp_lexer_consume_token (parser->lexer)->value;
6491
6492 default:
6493 return NULL_TREE;
6494 }
6495}
6496
6497/* Parse an (optional) function-specifier.
6498
6499 function-specifier:
6500 inline
6501 virtual
6502 explicit
6503
6504 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6505
6506static tree
94edc4ab 6507cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6508{
6509 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6510 {
6511 case RID_INLINE:
6512 case RID_VIRTUAL:
6513 case RID_EXPLICIT:
6514 /* Consume the token. */
6515 return cp_lexer_consume_token (parser->lexer)->value;
6516
6517 default:
6518 return NULL_TREE;
6519 }
6520}
6521
6522/* Parse a linkage-specification.
6523
6524 linkage-specification:
6525 extern string-literal { declaration-seq [opt] }
6526 extern string-literal declaration */
6527
6528static void
94edc4ab 6529cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6530{
6531 cp_token *token;
6532 tree linkage;
6533
6534 /* Look for the `extern' keyword. */
6535 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6536
6537 /* Peek at the next token. */
6538 token = cp_lexer_peek_token (parser->lexer);
6539 /* If it's not a string-literal, then there's a problem. */
6540 if (!cp_parser_is_string_literal (token))
6541 {
6542 cp_parser_error (parser, "expected language-name");
6543 return;
6544 }
6545 /* Consume the token. */
6546 cp_lexer_consume_token (parser->lexer);
6547
6548 /* Transform the literal into an identifier. If the literal is a
6549 wide-character string, or contains embedded NULs, then we can't
6550 handle it as the user wants. */
6551 if (token->type == CPP_WSTRING
6552 || (strlen (TREE_STRING_POINTER (token->value))
6553 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6554 {
6555 cp_parser_error (parser, "invalid linkage-specification");
6556 /* Assume C++ linkage. */
6557 linkage = get_identifier ("c++");
6558 }
6559 /* If it's a simple string constant, things are easier. */
6560 else
6561 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6562
6563 /* We're now using the new linkage. */
6564 push_lang_context (linkage);
6565
6566 /* If the next token is a `{', then we're using the first
6567 production. */
6568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6569 {
6570 /* Consume the `{' token. */
6571 cp_lexer_consume_token (parser->lexer);
6572 /* Parse the declarations. */
6573 cp_parser_declaration_seq_opt (parser);
6574 /* Look for the closing `}'. */
6575 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6576 }
6577 /* Otherwise, there's just one declaration. */
6578 else
6579 {
6580 bool saved_in_unbraced_linkage_specification_p;
6581
6582 saved_in_unbraced_linkage_specification_p
6583 = parser->in_unbraced_linkage_specification_p;
6584 parser->in_unbraced_linkage_specification_p = true;
6585 have_extern_spec = true;
6586 cp_parser_declaration (parser);
6587 have_extern_spec = false;
6588 parser->in_unbraced_linkage_specification_p
6589 = saved_in_unbraced_linkage_specification_p;
6590 }
6591
6592 /* We're done with the linkage-specification. */
6593 pop_lang_context ();
6594}
6595
6596/* Special member functions [gram.special] */
6597
6598/* Parse a conversion-function-id.
6599
6600 conversion-function-id:
6601 operator conversion-type-id
6602
6603 Returns an IDENTIFIER_NODE representing the operator. */
6604
6605static tree
94edc4ab 6606cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6607{
6608 tree type;
6609 tree saved_scope;
6610 tree saved_qualifying_scope;
6611 tree saved_object_scope;
6612
6613 /* Look for the `operator' token. */
6614 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6615 return error_mark_node;
6616 /* When we parse the conversion-type-id, the current scope will be
6617 reset. However, we need that information in able to look up the
6618 conversion function later, so we save it here. */
6619 saved_scope = parser->scope;
6620 saved_qualifying_scope = parser->qualifying_scope;
6621 saved_object_scope = parser->object_scope;
6622 /* We must enter the scope of the class so that the names of
6623 entities declared within the class are available in the
6624 conversion-type-id. For example, consider:
6625
6626 struct S {
6627 typedef int I;
6628 operator I();
6629 };
6630
6631 S::operator I() { ... }
6632
6633 In order to see that `I' is a type-name in the definition, we
6634 must be in the scope of `S'. */
6635 if (saved_scope)
6636 push_scope (saved_scope);
6637 /* Parse the conversion-type-id. */
6638 type = cp_parser_conversion_type_id (parser);
6639 /* Leave the scope of the class, if any. */
6640 if (saved_scope)
6641 pop_scope (saved_scope);
6642 /* Restore the saved scope. */
6643 parser->scope = saved_scope;
6644 parser->qualifying_scope = saved_qualifying_scope;
6645 parser->object_scope = saved_object_scope;
6646 /* If the TYPE is invalid, indicate failure. */
6647 if (type == error_mark_node)
6648 return error_mark_node;
6649 return mangle_conv_op_name_for_type (type);
6650}
6651
6652/* Parse a conversion-type-id:
6653
6654 conversion-type-id:
6655 type-specifier-seq conversion-declarator [opt]
6656
6657 Returns the TYPE specified. */
6658
6659static tree
94edc4ab 6660cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6661{
6662 tree attributes;
6663 tree type_specifiers;
6664 tree declarator;
6665
6666 /* Parse the attributes. */
6667 attributes = cp_parser_attributes_opt (parser);
6668 /* Parse the type-specifiers. */
6669 type_specifiers = cp_parser_type_specifier_seq (parser);
6670 /* If that didn't work, stop. */
6671 if (type_specifiers == error_mark_node)
6672 return error_mark_node;
6673 /* Parse the conversion-declarator. */
6674 declarator = cp_parser_conversion_declarator_opt (parser);
6675
6676 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6677 /*initialized=*/0, &attributes);
6678}
6679
6680/* Parse an (optional) conversion-declarator.
6681
6682 conversion-declarator:
6683 ptr-operator conversion-declarator [opt]
6684
6685 Returns a representation of the declarator. See
6686 cp_parser_declarator for details. */
6687
6688static tree
94edc4ab 6689cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
6690{
6691 enum tree_code code;
6692 tree class_type;
6693 tree cv_qualifier_seq;
6694
6695 /* We don't know if there's a ptr-operator next, or not. */
6696 cp_parser_parse_tentatively (parser);
6697 /* Try the ptr-operator. */
6698 code = cp_parser_ptr_operator (parser, &class_type,
6699 &cv_qualifier_seq);
6700 /* If it worked, look for more conversion-declarators. */
6701 if (cp_parser_parse_definitely (parser))
6702 {
6703 tree declarator;
6704
6705 /* Parse another optional declarator. */
6706 declarator = cp_parser_conversion_declarator_opt (parser);
6707
6708 /* Create the representation of the declarator. */
6709 if (code == INDIRECT_REF)
6710 declarator = make_pointer_declarator (cv_qualifier_seq,
6711 declarator);
6712 else
6713 declarator = make_reference_declarator (cv_qualifier_seq,
6714 declarator);
6715
6716 /* Handle the pointer-to-member case. */
6717 if (class_type)
6718 declarator = build_nt (SCOPE_REF, class_type, declarator);
6719
6720 return declarator;
6721 }
6722
6723 return NULL_TREE;
6724}
6725
6726/* Parse an (optional) ctor-initializer.
6727
6728 ctor-initializer:
6729 : mem-initializer-list
6730
6731 Returns TRUE iff the ctor-initializer was actually present. */
6732
6733static bool
94edc4ab 6734cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
6735{
6736 /* If the next token is not a `:', then there is no
6737 ctor-initializer. */
6738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6739 {
6740 /* Do default initialization of any bases and members. */
6741 if (DECL_CONSTRUCTOR_P (current_function_decl))
6742 finish_mem_initializers (NULL_TREE);
6743
6744 return false;
6745 }
6746
6747 /* Consume the `:' token. */
6748 cp_lexer_consume_token (parser->lexer);
6749 /* And the mem-initializer-list. */
6750 cp_parser_mem_initializer_list (parser);
6751
6752 return true;
6753}
6754
6755/* Parse a mem-initializer-list.
6756
6757 mem-initializer-list:
6758 mem-initializer
6759 mem-initializer , mem-initializer-list */
6760
6761static void
94edc4ab 6762cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
6763{
6764 tree mem_initializer_list = NULL_TREE;
6765
6766 /* Let the semantic analysis code know that we are starting the
6767 mem-initializer-list. */
0e136342
MM
6768 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6769 error ("only constructors take base initializers");
a723baf1
MM
6770
6771 /* Loop through the list. */
6772 while (true)
6773 {
6774 tree mem_initializer;
6775
6776 /* Parse the mem-initializer. */
6777 mem_initializer = cp_parser_mem_initializer (parser);
6778 /* Add it to the list, unless it was erroneous. */
6779 if (mem_initializer)
6780 {
6781 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6782 mem_initializer_list = mem_initializer;
6783 }
6784 /* If the next token is not a `,', we're done. */
6785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6786 break;
6787 /* Consume the `,' token. */
6788 cp_lexer_consume_token (parser->lexer);
6789 }
6790
6791 /* Perform semantic analysis. */
0e136342
MM
6792 if (DECL_CONSTRUCTOR_P (current_function_decl))
6793 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
6794}
6795
6796/* Parse a mem-initializer.
6797
6798 mem-initializer:
6799 mem-initializer-id ( expression-list [opt] )
6800
6801 GNU extension:
6802
6803 mem-initializer:
34cd5ae7 6804 ( expression-list [opt] )
a723baf1
MM
6805
6806 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6807 class) or FIELD_DECL (for a non-static data member) to initialize;
6808 the TREE_VALUE is the expression-list. */
6809
6810static tree
94edc4ab 6811cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
6812{
6813 tree mem_initializer_id;
6814 tree expression_list;
1f5a253a
NS
6815 tree member;
6816
a723baf1
MM
6817 /* Find out what is being initialized. */
6818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6819 {
6820 pedwarn ("anachronistic old-style base class initializer");
6821 mem_initializer_id = NULL_TREE;
6822 }
6823 else
6824 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
6825 member = expand_member_init (mem_initializer_id);
6826 if (member && !DECL_P (member))
6827 in_base_initializer = 1;
7efa3e22 6828
39703eb9
MM
6829 expression_list
6830 = cp_parser_parenthesized_expression_list (parser, false,
6831 /*non_constant_p=*/NULL);
7efa3e22 6832 if (!expression_list)
a723baf1 6833 expression_list = void_type_node;
a723baf1 6834
1f5a253a
NS
6835 in_base_initializer = 0;
6836
6837 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
6838}
6839
6840/* Parse a mem-initializer-id.
6841
6842 mem-initializer-id:
6843 :: [opt] nested-name-specifier [opt] class-name
6844 identifier
6845
6846 Returns a TYPE indicating the class to be initializer for the first
6847 production. Returns an IDENTIFIER_NODE indicating the data member
6848 to be initialized for the second production. */
6849
6850static tree
94edc4ab 6851cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
6852{
6853 bool global_scope_p;
6854 bool nested_name_specifier_p;
6855 tree id;
6856
6857 /* Look for the optional `::' operator. */
6858 global_scope_p
6859 = (cp_parser_global_scope_opt (parser,
6860 /*current_scope_valid_p=*/false)
6861 != NULL_TREE);
6862 /* Look for the optional nested-name-specifier. The simplest way to
6863 implement:
6864
6865 [temp.res]
6866
6867 The keyword `typename' is not permitted in a base-specifier or
6868 mem-initializer; in these contexts a qualified name that
6869 depends on a template-parameter is implicitly assumed to be a
6870 type name.
6871
6872 is to assume that we have seen the `typename' keyword at this
6873 point. */
6874 nested_name_specifier_p
6875 = (cp_parser_nested_name_specifier_opt (parser,
6876 /*typename_keyword_p=*/true,
6877 /*check_dependency_p=*/true,
6878 /*type_p=*/true)
6879 != NULL_TREE);
6880 /* If there is a `::' operator or a nested-name-specifier, then we
6881 are definitely looking for a class-name. */
6882 if (global_scope_p || nested_name_specifier_p)
6883 return cp_parser_class_name (parser,
6884 /*typename_keyword_p=*/true,
6885 /*template_keyword_p=*/false,
6886 /*type_p=*/false,
a723baf1
MM
6887 /*check_dependency_p=*/true,
6888 /*class_head_p=*/false);
6889 /* Otherwise, we could also be looking for an ordinary identifier. */
6890 cp_parser_parse_tentatively (parser);
6891 /* Try a class-name. */
6892 id = cp_parser_class_name (parser,
6893 /*typename_keyword_p=*/true,
6894 /*template_keyword_p=*/false,
6895 /*type_p=*/false,
a723baf1
MM
6896 /*check_dependency_p=*/true,
6897 /*class_head_p=*/false);
6898 /* If we found one, we're done. */
6899 if (cp_parser_parse_definitely (parser))
6900 return id;
6901 /* Otherwise, look for an ordinary identifier. */
6902 return cp_parser_identifier (parser);
6903}
6904
6905/* Overloading [gram.over] */
6906
6907/* Parse an operator-function-id.
6908
6909 operator-function-id:
6910 operator operator
6911
6912 Returns an IDENTIFIER_NODE for the operator which is a
6913 human-readable spelling of the identifier, e.g., `operator +'. */
6914
6915static tree
94edc4ab 6916cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
6917{
6918 /* Look for the `operator' keyword. */
6919 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6920 return error_mark_node;
6921 /* And then the name of the operator itself. */
6922 return cp_parser_operator (parser);
6923}
6924
6925/* Parse an operator.
6926
6927 operator:
6928 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6929 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6930 || ++ -- , ->* -> () []
6931
6932 GNU Extensions:
6933
6934 operator:
6935 <? >? <?= >?=
6936
6937 Returns an IDENTIFIER_NODE for the operator which is a
6938 human-readable spelling of the identifier, e.g., `operator +'. */
6939
6940static tree
94edc4ab 6941cp_parser_operator (cp_parser* parser)
a723baf1
MM
6942{
6943 tree id = NULL_TREE;
6944 cp_token *token;
6945
6946 /* Peek at the next token. */
6947 token = cp_lexer_peek_token (parser->lexer);
6948 /* Figure out which operator we have. */
6949 switch (token->type)
6950 {
6951 case CPP_KEYWORD:
6952 {
6953 enum tree_code op;
6954
6955 /* The keyword should be either `new' or `delete'. */
6956 if (token->keyword == RID_NEW)
6957 op = NEW_EXPR;
6958 else if (token->keyword == RID_DELETE)
6959 op = DELETE_EXPR;
6960 else
6961 break;
6962
6963 /* Consume the `new' or `delete' token. */
6964 cp_lexer_consume_token (parser->lexer);
6965
6966 /* Peek at the next token. */
6967 token = cp_lexer_peek_token (parser->lexer);
6968 /* If it's a `[' token then this is the array variant of the
6969 operator. */
6970 if (token->type == CPP_OPEN_SQUARE)
6971 {
6972 /* Consume the `[' token. */
6973 cp_lexer_consume_token (parser->lexer);
6974 /* Look for the `]' token. */
6975 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6976 id = ansi_opname (op == NEW_EXPR
6977 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6978 }
6979 /* Otherwise, we have the non-array variant. */
6980 else
6981 id = ansi_opname (op);
6982
6983 return id;
6984 }
6985
6986 case CPP_PLUS:
6987 id = ansi_opname (PLUS_EXPR);
6988 break;
6989
6990 case CPP_MINUS:
6991 id = ansi_opname (MINUS_EXPR);
6992 break;
6993
6994 case CPP_MULT:
6995 id = ansi_opname (MULT_EXPR);
6996 break;
6997
6998 case CPP_DIV:
6999 id = ansi_opname (TRUNC_DIV_EXPR);
7000 break;
7001
7002 case CPP_MOD:
7003 id = ansi_opname (TRUNC_MOD_EXPR);
7004 break;
7005
7006 case CPP_XOR:
7007 id = ansi_opname (BIT_XOR_EXPR);
7008 break;
7009
7010 case CPP_AND:
7011 id = ansi_opname (BIT_AND_EXPR);
7012 break;
7013
7014 case CPP_OR:
7015 id = ansi_opname (BIT_IOR_EXPR);
7016 break;
7017
7018 case CPP_COMPL:
7019 id = ansi_opname (BIT_NOT_EXPR);
7020 break;
7021
7022 case CPP_NOT:
7023 id = ansi_opname (TRUTH_NOT_EXPR);
7024 break;
7025
7026 case CPP_EQ:
7027 id = ansi_assopname (NOP_EXPR);
7028 break;
7029
7030 case CPP_LESS:
7031 id = ansi_opname (LT_EXPR);
7032 break;
7033
7034 case CPP_GREATER:
7035 id = ansi_opname (GT_EXPR);
7036 break;
7037
7038 case CPP_PLUS_EQ:
7039 id = ansi_assopname (PLUS_EXPR);
7040 break;
7041
7042 case CPP_MINUS_EQ:
7043 id = ansi_assopname (MINUS_EXPR);
7044 break;
7045
7046 case CPP_MULT_EQ:
7047 id = ansi_assopname (MULT_EXPR);
7048 break;
7049
7050 case CPP_DIV_EQ:
7051 id = ansi_assopname (TRUNC_DIV_EXPR);
7052 break;
7053
7054 case CPP_MOD_EQ:
7055 id = ansi_assopname (TRUNC_MOD_EXPR);
7056 break;
7057
7058 case CPP_XOR_EQ:
7059 id = ansi_assopname (BIT_XOR_EXPR);
7060 break;
7061
7062 case CPP_AND_EQ:
7063 id = ansi_assopname (BIT_AND_EXPR);
7064 break;
7065
7066 case CPP_OR_EQ:
7067 id = ansi_assopname (BIT_IOR_EXPR);
7068 break;
7069
7070 case CPP_LSHIFT:
7071 id = ansi_opname (LSHIFT_EXPR);
7072 break;
7073
7074 case CPP_RSHIFT:
7075 id = ansi_opname (RSHIFT_EXPR);
7076 break;
7077
7078 case CPP_LSHIFT_EQ:
7079 id = ansi_assopname (LSHIFT_EXPR);
7080 break;
7081
7082 case CPP_RSHIFT_EQ:
7083 id = ansi_assopname (RSHIFT_EXPR);
7084 break;
7085
7086 case CPP_EQ_EQ:
7087 id = ansi_opname (EQ_EXPR);
7088 break;
7089
7090 case CPP_NOT_EQ:
7091 id = ansi_opname (NE_EXPR);
7092 break;
7093
7094 case CPP_LESS_EQ:
7095 id = ansi_opname (LE_EXPR);
7096 break;
7097
7098 case CPP_GREATER_EQ:
7099 id = ansi_opname (GE_EXPR);
7100 break;
7101
7102 case CPP_AND_AND:
7103 id = ansi_opname (TRUTH_ANDIF_EXPR);
7104 break;
7105
7106 case CPP_OR_OR:
7107 id = ansi_opname (TRUTH_ORIF_EXPR);
7108 break;
7109
7110 case CPP_PLUS_PLUS:
7111 id = ansi_opname (POSTINCREMENT_EXPR);
7112 break;
7113
7114 case CPP_MINUS_MINUS:
7115 id = ansi_opname (PREDECREMENT_EXPR);
7116 break;
7117
7118 case CPP_COMMA:
7119 id = ansi_opname (COMPOUND_EXPR);
7120 break;
7121
7122 case CPP_DEREF_STAR:
7123 id = ansi_opname (MEMBER_REF);
7124 break;
7125
7126 case CPP_DEREF:
7127 id = ansi_opname (COMPONENT_REF);
7128 break;
7129
7130 case CPP_OPEN_PAREN:
7131 /* Consume the `('. */
7132 cp_lexer_consume_token (parser->lexer);
7133 /* Look for the matching `)'. */
7134 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7135 return ansi_opname (CALL_EXPR);
7136
7137 case CPP_OPEN_SQUARE:
7138 /* Consume the `['. */
7139 cp_lexer_consume_token (parser->lexer);
7140 /* Look for the matching `]'. */
7141 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7142 return ansi_opname (ARRAY_REF);
7143
7144 /* Extensions. */
7145 case CPP_MIN:
7146 id = ansi_opname (MIN_EXPR);
7147 break;
7148
7149 case CPP_MAX:
7150 id = ansi_opname (MAX_EXPR);
7151 break;
7152
7153 case CPP_MIN_EQ:
7154 id = ansi_assopname (MIN_EXPR);
7155 break;
7156
7157 case CPP_MAX_EQ:
7158 id = ansi_assopname (MAX_EXPR);
7159 break;
7160
7161 default:
7162 /* Anything else is an error. */
7163 break;
7164 }
7165
7166 /* If we have selected an identifier, we need to consume the
7167 operator token. */
7168 if (id)
7169 cp_lexer_consume_token (parser->lexer);
7170 /* Otherwise, no valid operator name was present. */
7171 else
7172 {
7173 cp_parser_error (parser, "expected operator");
7174 id = error_mark_node;
7175 }
7176
7177 return id;
7178}
7179
7180/* Parse a template-declaration.
7181
7182 template-declaration:
7183 export [opt] template < template-parameter-list > declaration
7184
7185 If MEMBER_P is TRUE, this template-declaration occurs within a
7186 class-specifier.
7187
7188 The grammar rule given by the standard isn't correct. What
7189 is really meant is:
7190
7191 template-declaration:
7192 export [opt] template-parameter-list-seq
7193 decl-specifier-seq [opt] init-declarator [opt] ;
7194 export [opt] template-parameter-list-seq
7195 function-definition
7196
7197 template-parameter-list-seq:
7198 template-parameter-list-seq [opt]
7199 template < template-parameter-list > */
7200
7201static void
94edc4ab 7202cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7203{
7204 /* Check for `export'. */
7205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7206 {
7207 /* Consume the `export' token. */
7208 cp_lexer_consume_token (parser->lexer);
7209 /* Warn that we do not support `export'. */
7210 warning ("keyword `export' not implemented, and will be ignored");
7211 }
7212
7213 cp_parser_template_declaration_after_export (parser, member_p);
7214}
7215
7216/* Parse a template-parameter-list.
7217
7218 template-parameter-list:
7219 template-parameter
7220 template-parameter-list , template-parameter
7221
7222 Returns a TREE_LIST. Each node represents a template parameter.
7223 The nodes are connected via their TREE_CHAINs. */
7224
7225static tree
94edc4ab 7226cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7227{
7228 tree parameter_list = NULL_TREE;
7229
7230 while (true)
7231 {
7232 tree parameter;
7233 cp_token *token;
7234
7235 /* Parse the template-parameter. */
7236 parameter = cp_parser_template_parameter (parser);
7237 /* Add it to the list. */
7238 parameter_list = process_template_parm (parameter_list,
7239 parameter);
7240
7241 /* Peek at the next token. */
7242 token = cp_lexer_peek_token (parser->lexer);
7243 /* If it's not a `,', we're done. */
7244 if (token->type != CPP_COMMA)
7245 break;
7246 /* Otherwise, consume the `,' token. */
7247 cp_lexer_consume_token (parser->lexer);
7248 }
7249
7250 return parameter_list;
7251}
7252
7253/* Parse a template-parameter.
7254
7255 template-parameter:
7256 type-parameter
7257 parameter-declaration
7258
7259 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7260 TREE_PURPOSE is the default value, if any. */
7261
7262static tree
94edc4ab 7263cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7264{
7265 cp_token *token;
7266
7267 /* Peek at the next token. */
7268 token = cp_lexer_peek_token (parser->lexer);
7269 /* If it is `class' or `template', we have a type-parameter. */
7270 if (token->keyword == RID_TEMPLATE)
7271 return cp_parser_type_parameter (parser);
7272 /* If it is `class' or `typename' we do not know yet whether it is a
7273 type parameter or a non-type parameter. Consider:
7274
7275 template <typename T, typename T::X X> ...
7276
7277 or:
7278
7279 template <class C, class D*> ...
7280
7281 Here, the first parameter is a type parameter, and the second is
7282 a non-type parameter. We can tell by looking at the token after
7283 the identifier -- if it is a `,', `=', or `>' then we have a type
7284 parameter. */
7285 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7286 {
7287 /* Peek at the token after `class' or `typename'. */
7288 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7289 /* If it's an identifier, skip it. */
7290 if (token->type == CPP_NAME)
7291 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7292 /* Now, see if the token looks like the end of a template
7293 parameter. */
7294 if (token->type == CPP_COMMA
7295 || token->type == CPP_EQ
7296 || token->type == CPP_GREATER)
7297 return cp_parser_type_parameter (parser);
7298 }
7299
7300 /* Otherwise, it is a non-type parameter.
7301
7302 [temp.param]
7303
7304 When parsing a default template-argument for a non-type
7305 template-parameter, the first non-nested `>' is taken as the end
7306 of the template parameter-list rather than a greater-than
7307 operator. */
7308 return
ec194454 7309 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7310}
7311
7312/* Parse a type-parameter.
7313
7314 type-parameter:
7315 class identifier [opt]
7316 class identifier [opt] = type-id
7317 typename identifier [opt]
7318 typename identifier [opt] = type-id
7319 template < template-parameter-list > class identifier [opt]
7320 template < template-parameter-list > class identifier [opt]
7321 = id-expression
7322
7323 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7324 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7325 the declaration of the parameter. */
7326
7327static tree
94edc4ab 7328cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7329{
7330 cp_token *token;
7331 tree parameter;
7332
7333 /* Look for a keyword to tell us what kind of parameter this is. */
7334 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7335 "`class', `typename', or `template'");
a723baf1
MM
7336 if (!token)
7337 return error_mark_node;
7338
7339 switch (token->keyword)
7340 {
7341 case RID_CLASS:
7342 case RID_TYPENAME:
7343 {
7344 tree identifier;
7345 tree default_argument;
7346
7347 /* If the next token is an identifier, then it names the
7348 parameter. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7350 identifier = cp_parser_identifier (parser);
7351 else
7352 identifier = NULL_TREE;
7353
7354 /* Create the parameter. */
7355 parameter = finish_template_type_parm (class_type_node, identifier);
7356
7357 /* If the next token is an `=', we have a default argument. */
7358 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7359 {
7360 /* Consume the `=' token. */
7361 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7362 /* Parse the default-argument. */
a723baf1
MM
7363 default_argument = cp_parser_type_id (parser);
7364 }
7365 else
7366 default_argument = NULL_TREE;
7367
7368 /* Create the combined representation of the parameter and the
7369 default argument. */
c67d36d0 7370 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7371 }
7372 break;
7373
7374 case RID_TEMPLATE:
7375 {
7376 tree parameter_list;
7377 tree identifier;
7378 tree default_argument;
7379
7380 /* Look for the `<'. */
7381 cp_parser_require (parser, CPP_LESS, "`<'");
7382 /* Parse the template-parameter-list. */
7383 begin_template_parm_list ();
7384 parameter_list
7385 = cp_parser_template_parameter_list (parser);
7386 parameter_list = end_template_parm_list (parameter_list);
7387 /* Look for the `>'. */
7388 cp_parser_require (parser, CPP_GREATER, "`>'");
7389 /* Look for the `class' keyword. */
7390 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7391 /* If the next token is an `=', then there is a
7392 default-argument. If the next token is a `>', we are at
7393 the end of the parameter-list. If the next token is a `,',
7394 then we are at the end of this parameter. */
7395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7396 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7397 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7398 identifier = cp_parser_identifier (parser);
7399 else
7400 identifier = NULL_TREE;
7401 /* Create the template parameter. */
7402 parameter = finish_template_template_parm (class_type_node,
7403 identifier);
7404
7405 /* If the next token is an `=', then there is a
7406 default-argument. */
7407 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7408 {
7409 /* Consume the `='. */
7410 cp_lexer_consume_token (parser->lexer);
7411 /* Parse the id-expression. */
7412 default_argument
7413 = cp_parser_id_expression (parser,
7414 /*template_keyword_p=*/false,
7415 /*check_dependency_p=*/true,
f3c2dfc6
MM
7416 /*template_p=*/NULL,
7417 /*declarator_p=*/false);
a723baf1
MM
7418 /* Look up the name. */
7419 default_argument
7420 = cp_parser_lookup_name_simple (parser, default_argument);
7421 /* See if the default argument is valid. */
7422 default_argument
7423 = check_template_template_default_arg (default_argument);
7424 }
7425 else
7426 default_argument = NULL_TREE;
7427
7428 /* Create the combined representation of the parameter and the
7429 default argument. */
c67d36d0 7430 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7431 }
7432 break;
7433
7434 default:
7435 /* Anything else is an error. */
7436 cp_parser_error (parser,
7437 "expected `class', `typename', or `template'");
7438 parameter = error_mark_node;
7439 }
7440
7441 return parameter;
7442}
7443
7444/* Parse a template-id.
7445
7446 template-id:
7447 template-name < template-argument-list [opt] >
7448
7449 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7450 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7451 returned. Otherwise, if the template-name names a function, or set
7452 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7453 names a class, returns a TYPE_DECL for the specialization.
7454
7455 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7456 uninstantiated templates. */
7457
7458static tree
7459cp_parser_template_id (cp_parser *parser,
7460 bool template_keyword_p,
7461 bool check_dependency_p)
7462{
7463 tree template;
7464 tree arguments;
7465 tree saved_scope;
7466 tree saved_qualifying_scope;
7467 tree saved_object_scope;
7468 tree template_id;
7469 bool saved_greater_than_is_operator_p;
7470 ptrdiff_t start_of_id;
7471 tree access_check = NULL_TREE;
2050a1bb 7472 cp_token *next_token;
a723baf1
MM
7473
7474 /* If the next token corresponds to a template-id, there is no need
7475 to reparse it. */
2050a1bb
MM
7476 next_token = cp_lexer_peek_token (parser->lexer);
7477 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7478 {
7479 tree value;
7480 tree check;
7481
7482 /* Get the stored value. */
7483 value = cp_lexer_consume_token (parser->lexer)->value;
7484 /* Perform any access checks that were deferred. */
7485 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7486 perform_or_defer_access_check (TREE_PURPOSE (check),
7487 TREE_VALUE (check));
a723baf1
MM
7488 /* Return the stored value. */
7489 return TREE_VALUE (value);
7490 }
7491
2050a1bb
MM
7492 /* Avoid performing name lookup if there is no possibility of
7493 finding a template-id. */
7494 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7495 || (next_token->type == CPP_NAME
7496 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7497 {
7498 cp_parser_error (parser, "expected template-id");
7499 return error_mark_node;
7500 }
7501
a723baf1
MM
7502 /* Remember where the template-id starts. */
7503 if (cp_parser_parsing_tentatively (parser)
7504 && !cp_parser_committed_to_tentative_parse (parser))
7505 {
2050a1bb 7506 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7507 start_of_id = cp_lexer_token_difference (parser->lexer,
7508 parser->lexer->first_token,
7509 next_token);
a723baf1
MM
7510 }
7511 else
7512 start_of_id = -1;
7513
8d241e0b 7514 push_deferring_access_checks (dk_deferred);
cf22909c 7515
a723baf1
MM
7516 /* Parse the template-name. */
7517 template = cp_parser_template_name (parser, template_keyword_p,
7518 check_dependency_p);
7519 if (template == error_mark_node)
cf22909c
KL
7520 {
7521 pop_deferring_access_checks ();
7522 return error_mark_node;
7523 }
a723baf1
MM
7524
7525 /* Look for the `<' that starts the template-argument-list. */
7526 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7527 {
7528 pop_deferring_access_checks ();
7529 return error_mark_node;
7530 }
a723baf1
MM
7531
7532 /* [temp.names]
7533
7534 When parsing a template-id, the first non-nested `>' is taken as
7535 the end of the template-argument-list rather than a greater-than
7536 operator. */
7537 saved_greater_than_is_operator_p
7538 = parser->greater_than_is_operator_p;
7539 parser->greater_than_is_operator_p = false;
7540 /* Parsing the argument list may modify SCOPE, so we save it
7541 here. */
7542 saved_scope = parser->scope;
7543 saved_qualifying_scope = parser->qualifying_scope;
7544 saved_object_scope = parser->object_scope;
7545 /* Parse the template-argument-list itself. */
7546 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7547 arguments = NULL_TREE;
7548 else
7549 arguments = cp_parser_template_argument_list (parser);
7550 /* Look for the `>' that ends the template-argument-list. */
7551 cp_parser_require (parser, CPP_GREATER, "`>'");
7552 /* The `>' token might be a greater-than operator again now. */
7553 parser->greater_than_is_operator_p
7554 = saved_greater_than_is_operator_p;
7555 /* Restore the SAVED_SCOPE. */
7556 parser->scope = saved_scope;
7557 parser->qualifying_scope = saved_qualifying_scope;
7558 parser->object_scope = saved_object_scope;
7559
7560 /* Build a representation of the specialization. */
7561 if (TREE_CODE (template) == IDENTIFIER_NODE)
7562 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7563 else if (DECL_CLASS_TEMPLATE_P (template)
7564 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7565 template_id
7566 = finish_template_type (template, arguments,
7567 cp_lexer_next_token_is (parser->lexer,
7568 CPP_SCOPE));
7569 else
7570 {
7571 /* If it's not a class-template or a template-template, it should be
7572 a function-template. */
7573 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7574 || TREE_CODE (template) == OVERLOAD
7575 || BASELINK_P (template)),
7576 20010716);
7577
7578 template_id = lookup_template_function (template, arguments);
7579 }
7580
cf22909c
KL
7581 /* Retrieve any deferred checks. Do not pop this access checks yet
7582 so the memory will not be reclaimed during token replacing below. */
7583 access_check = get_deferred_access_checks ();
7584
a723baf1
MM
7585 /* If parsing tentatively, replace the sequence of tokens that makes
7586 up the template-id with a CPP_TEMPLATE_ID token. That way,
7587 should we re-parse the token stream, we will not have to repeat
7588 the effort required to do the parse, nor will we issue duplicate
7589 error messages about problems during instantiation of the
7590 template. */
7591 if (start_of_id >= 0)
7592 {
7593 cp_token *token;
a723baf1
MM
7594
7595 /* Find the token that corresponds to the start of the
7596 template-id. */
7597 token = cp_lexer_advance_token (parser->lexer,
7598 parser->lexer->first_token,
7599 start_of_id);
7600
a723baf1
MM
7601 /* Reset the contents of the START_OF_ID token. */
7602 token->type = CPP_TEMPLATE_ID;
7603 token->value = build_tree_list (access_check, template_id);
7604 token->keyword = RID_MAX;
7605 /* Purge all subsequent tokens. */
7606 cp_lexer_purge_tokens_after (parser->lexer, token);
7607 }
7608
cf22909c 7609 pop_deferring_access_checks ();
a723baf1
MM
7610 return template_id;
7611}
7612
7613/* Parse a template-name.
7614
7615 template-name:
7616 identifier
7617
7618 The standard should actually say:
7619
7620 template-name:
7621 identifier
7622 operator-function-id
7623 conversion-function-id
7624
7625 A defect report has been filed about this issue.
7626
7627 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7628 `template' keyword, in a construction like:
7629
7630 T::template f<3>()
7631
7632 In that case `f' is taken to be a template-name, even though there
7633 is no way of knowing for sure.
7634
7635 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7636 name refers to a set of overloaded functions, at least one of which
7637 is a template, or an IDENTIFIER_NODE with the name of the template,
7638 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7639 names are looked up inside uninstantiated templates. */
7640
7641static tree
94edc4ab
NN
7642cp_parser_template_name (cp_parser* parser,
7643 bool template_keyword_p,
7644 bool check_dependency_p)
a723baf1
MM
7645{
7646 tree identifier;
7647 tree decl;
7648 tree fns;
7649
7650 /* If the next token is `operator', then we have either an
7651 operator-function-id or a conversion-function-id. */
7652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7653 {
7654 /* We don't know whether we're looking at an
7655 operator-function-id or a conversion-function-id. */
7656 cp_parser_parse_tentatively (parser);
7657 /* Try an operator-function-id. */
7658 identifier = cp_parser_operator_function_id (parser);
7659 /* If that didn't work, try a conversion-function-id. */
7660 if (!cp_parser_parse_definitely (parser))
7661 identifier = cp_parser_conversion_function_id (parser);
7662 }
7663 /* Look for the identifier. */
7664 else
7665 identifier = cp_parser_identifier (parser);
7666
7667 /* If we didn't find an identifier, we don't have a template-id. */
7668 if (identifier == error_mark_node)
7669 return error_mark_node;
7670
7671 /* If the name immediately followed the `template' keyword, then it
7672 is a template-name. However, if the next token is not `<', then
7673 we do not treat it as a template-name, since it is not being used
7674 as part of a template-id. This enables us to handle constructs
7675 like:
7676
7677 template <typename T> struct S { S(); };
7678 template <typename T> S<T>::S();
7679
7680 correctly. We would treat `S' as a template -- if it were `S<T>'
7681 -- but we do not if there is no `<'. */
7682 if (template_keyword_p && processing_template_decl
7683 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7684 return identifier;
7685
7686 /* Look up the name. */
7687 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 7688 /*is_type=*/false,
eea9800f 7689 /*is_namespace=*/false,
a723baf1
MM
7690 check_dependency_p);
7691 decl = maybe_get_template_decl_from_type_decl (decl);
7692
7693 /* If DECL is a template, then the name was a template-name. */
7694 if (TREE_CODE (decl) == TEMPLATE_DECL)
7695 ;
7696 else
7697 {
7698 /* The standard does not explicitly indicate whether a name that
7699 names a set of overloaded declarations, some of which are
7700 templates, is a template-name. However, such a name should
7701 be a template-name; otherwise, there is no way to form a
7702 template-id for the overloaded templates. */
7703 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7704 if (TREE_CODE (fns) == OVERLOAD)
7705 {
7706 tree fn;
7707
7708 for (fn = fns; fn; fn = OVL_NEXT (fn))
7709 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7710 break;
7711 }
7712 else
7713 {
7714 /* Otherwise, the name does not name a template. */
7715 cp_parser_error (parser, "expected template-name");
7716 return error_mark_node;
7717 }
7718 }
7719
7720 /* If DECL is dependent, and refers to a function, then just return
7721 its name; we will look it up again during template instantiation. */
7722 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7723 {
7724 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 7725 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
7726 return identifier;
7727 }
7728
7729 return decl;
7730}
7731
7732/* Parse a template-argument-list.
7733
7734 template-argument-list:
7735 template-argument
7736 template-argument-list , template-argument
7737
bf12d54d 7738 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
7739
7740static tree
94edc4ab 7741cp_parser_template_argument_list (cp_parser* parser)
a723baf1 7742{
bf12d54d
NS
7743 tree fixed_args[10];
7744 unsigned n_args = 0;
7745 unsigned alloced = 10;
7746 tree *arg_ary = fixed_args;
7747 tree vec;
a723baf1 7748
bf12d54d 7749 do
a723baf1
MM
7750 {
7751 tree argument;
7752
bf12d54d
NS
7753 if (n_args)
7754 /* Consume the comma. */
7755 cp_lexer_consume_token (parser->lexer);
7756
a723baf1
MM
7757 /* Parse the template-argument. */
7758 argument = cp_parser_template_argument (parser);
bf12d54d
NS
7759 if (n_args == alloced)
7760 {
7761 alloced *= 2;
7762
7763 if (arg_ary == fixed_args)
7764 {
7765 arg_ary = xmalloc (sizeof (tree) * alloced);
7766 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7767 }
7768 else
7769 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7770 }
7771 arg_ary[n_args++] = argument;
a723baf1 7772 }
bf12d54d
NS
7773 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7774
7775 vec = make_tree_vec (n_args);
a723baf1 7776
bf12d54d
NS
7777 while (n_args--)
7778 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7779
7780 if (arg_ary != fixed_args)
7781 free (arg_ary);
7782 return vec;
a723baf1
MM
7783}
7784
7785/* Parse a template-argument.
7786
7787 template-argument:
7788 assignment-expression
7789 type-id
7790 id-expression
7791
7792 The representation is that of an assignment-expression, type-id, or
7793 id-expression -- except that the qualified id-expression is
7794 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
7795 OVERLOAD.
7796
7797 Although the standard says "assignment-expression", it forbids
7798 throw-expressions or assignments in the template argument.
7799 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
7800
7801static tree
94edc4ab 7802cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
7803{
7804 tree argument;
7805 bool template_p;
d17811fd
MM
7806 bool address_p;
7807 cp_token *token;
b3445994 7808 cp_id_kind idk;
d17811fd 7809 tree qualifying_class;
a723baf1
MM
7810
7811 /* There's really no way to know what we're looking at, so we just
7812 try each alternative in order.
7813
7814 [temp.arg]
7815
7816 In a template-argument, an ambiguity between a type-id and an
7817 expression is resolved to a type-id, regardless of the form of
7818 the corresponding template-parameter.
7819
7820 Therefore, we try a type-id first. */
7821 cp_parser_parse_tentatively (parser);
a723baf1
MM
7822 argument = cp_parser_type_id (parser);
7823 /* If the next token isn't a `,' or a `>', then this argument wasn't
7824 really finished. */
d17811fd 7825 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7826 cp_parser_error (parser, "expected template-argument");
7827 /* If that worked, we're done. */
7828 if (cp_parser_parse_definitely (parser))
7829 return argument;
7830 /* We're still not sure what the argument will be. */
7831 cp_parser_parse_tentatively (parser);
7832 /* Try a template. */
7833 argument = cp_parser_id_expression (parser,
7834 /*template_keyword_p=*/false,
7835 /*check_dependency_p=*/true,
f3c2dfc6
MM
7836 &template_p,
7837 /*declarator_p=*/false);
a723baf1
MM
7838 /* If the next token isn't a `,' or a `>', then this argument wasn't
7839 really finished. */
d17811fd 7840 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7841 cp_parser_error (parser, "expected template-argument");
7842 if (!cp_parser_error_occurred (parser))
7843 {
7844 /* Figure out what is being referred to. */
7845 argument = cp_parser_lookup_name_simple (parser, argument);
7846 if (template_p)
7847 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7848 TREE_OPERAND (argument, 1),
78757caa 7849 tf_error);
a723baf1
MM
7850 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7851 cp_parser_error (parser, "expected template-name");
7852 }
7853 if (cp_parser_parse_definitely (parser))
7854 return argument;
d17811fd
MM
7855 /* It must be a non-type argument. There permitted cases are given
7856 in [temp.arg.nontype]:
7857
7858 -- an integral constant-expression of integral or enumeration
7859 type; or
7860
7861 -- the name of a non-type template-parameter; or
7862
7863 -- the name of an object or function with external linkage...
7864
7865 -- the address of an object or function with external linkage...
7866
7867 -- a pointer to member... */
7868 /* Look for a non-type template parameter. */
7869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7870 {
7871 cp_parser_parse_tentatively (parser);
7872 argument = cp_parser_primary_expression (parser,
7873 &idk,
7874 &qualifying_class);
7875 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7876 || !cp_parser_next_token_ends_template_argument_p (parser))
7877 cp_parser_simulate_error (parser);
7878 if (cp_parser_parse_definitely (parser))
7879 return argument;
7880 }
7881 /* If the next token is "&", the argument must be the address of an
7882 object or function with external linkage. */
7883 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7884 if (address_p)
7885 cp_lexer_consume_token (parser->lexer);
7886 /* See if we might have an id-expression. */
7887 token = cp_lexer_peek_token (parser->lexer);
7888 if (token->type == CPP_NAME
7889 || token->keyword == RID_OPERATOR
7890 || token->type == CPP_SCOPE
7891 || token->type == CPP_TEMPLATE_ID
7892 || token->type == CPP_NESTED_NAME_SPECIFIER)
7893 {
7894 cp_parser_parse_tentatively (parser);
7895 argument = cp_parser_primary_expression (parser,
7896 &idk,
7897 &qualifying_class);
7898 if (cp_parser_error_occurred (parser)
7899 || !cp_parser_next_token_ends_template_argument_p (parser))
7900 cp_parser_abort_tentative_parse (parser);
7901 else
7902 {
7903 if (qualifying_class)
7904 argument = finish_qualified_id_expr (qualifying_class,
7905 argument,
7906 /*done=*/true,
7907 address_p);
7908 if (TREE_CODE (argument) == VAR_DECL)
7909 {
7910 /* A variable without external linkage might still be a
7911 valid constant-expression, so no error is issued here
7912 if the external-linkage check fails. */
7913 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7914 cp_parser_simulate_error (parser);
7915 }
7916 else if (is_overloaded_fn (argument))
7917 /* All overloaded functions are allowed; if the external
7918 linkage test does not pass, an error will be issued
7919 later. */
7920 ;
7921 else if (address_p
7922 && (TREE_CODE (argument) == OFFSET_REF
7923 || TREE_CODE (argument) == SCOPE_REF))
7924 /* A pointer-to-member. */
7925 ;
7926 else
7927 cp_parser_simulate_error (parser);
7928
7929 if (cp_parser_parse_definitely (parser))
7930 {
7931 if (address_p)
7932 argument = build_x_unary_op (ADDR_EXPR, argument);
7933 return argument;
7934 }
7935 }
7936 }
7937 /* If the argument started with "&", there are no other valid
7938 alternatives at this point. */
7939 if (address_p)
7940 {
7941 cp_parser_error (parser, "invalid non-type template argument");
7942 return error_mark_node;
7943 }
7944 /* The argument must be a constant-expression. */
7945 argument = cp_parser_constant_expression (parser,
7946 /*allow_non_constant_p=*/false,
7947 /*non_constant_p=*/NULL);
7948 /* If it's non-dependent, simplify it. */
7949 return cp_parser_fold_non_dependent_expr (argument);
a723baf1
MM
7950}
7951
7952/* Parse an explicit-instantiation.
7953
7954 explicit-instantiation:
7955 template declaration
7956
7957 Although the standard says `declaration', what it really means is:
7958
7959 explicit-instantiation:
7960 template decl-specifier-seq [opt] declarator [opt] ;
7961
7962 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7963 supposed to be allowed. A defect report has been filed about this
7964 issue.
7965
7966 GNU Extension:
7967
7968 explicit-instantiation:
7969 storage-class-specifier template
7970 decl-specifier-seq [opt] declarator [opt] ;
7971 function-specifier template
7972 decl-specifier-seq [opt] declarator [opt] ; */
7973
7974static void
94edc4ab 7975cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 7976{
560ad596 7977 int declares_class_or_enum;
a723baf1
MM
7978 tree decl_specifiers;
7979 tree attributes;
7980 tree extension_specifier = NULL_TREE;
7981
7982 /* Look for an (optional) storage-class-specifier or
7983 function-specifier. */
7984 if (cp_parser_allow_gnu_extensions_p (parser))
7985 {
7986 extension_specifier
7987 = cp_parser_storage_class_specifier_opt (parser);
7988 if (!extension_specifier)
7989 extension_specifier = cp_parser_function_specifier_opt (parser);
7990 }
7991
7992 /* Look for the `template' keyword. */
7993 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7994 /* Let the front end know that we are processing an explicit
7995 instantiation. */
7996 begin_explicit_instantiation ();
7997 /* [temp.explicit] says that we are supposed to ignore access
7998 control while processing explicit instantiation directives. */
78757caa 7999 push_deferring_access_checks (dk_no_check);
a723baf1
MM
8000 /* Parse a decl-specifier-seq. */
8001 decl_specifiers
8002 = cp_parser_decl_specifier_seq (parser,
8003 CP_PARSER_FLAGS_OPTIONAL,
8004 &attributes,
8005 &declares_class_or_enum);
8006 /* If there was exactly one decl-specifier, and it declared a class,
8007 and there's no declarator, then we have an explicit type
8008 instantiation. */
8009 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8010 {
8011 tree type;
8012
8013 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8014 /* Turn access control back on for names used during
8015 template instantiation. */
8016 pop_deferring_access_checks ();
a723baf1
MM
8017 if (type)
8018 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8019 }
8020 else
8021 {
8022 tree declarator;
8023 tree decl;
8024
8025 /* Parse the declarator. */
8026 declarator
62b8a44e 8027 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1 8028 /*ctor_dtor_or_conv_p=*/NULL);
560ad596
MM
8029 cp_parser_check_for_definition_in_return_type (declarator,
8030 declares_class_or_enum);
a723baf1
MM
8031 decl = grokdeclarator (declarator, decl_specifiers,
8032 NORMAL, 0, NULL);
b7fc8b57
KL
8033 /* Turn access control back on for names used during
8034 template instantiation. */
8035 pop_deferring_access_checks ();
a723baf1
MM
8036 /* Do the explicit instantiation. */
8037 do_decl_instantiation (decl, extension_specifier);
8038 }
8039 /* We're done with the instantiation. */
8040 end_explicit_instantiation ();
a723baf1 8041
e0860732 8042 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8043}
8044
8045/* Parse an explicit-specialization.
8046
8047 explicit-specialization:
8048 template < > declaration
8049
8050 Although the standard says `declaration', what it really means is:
8051
8052 explicit-specialization:
8053 template <> decl-specifier [opt] init-declarator [opt] ;
8054 template <> function-definition
8055 template <> explicit-specialization
8056 template <> template-declaration */
8057
8058static void
94edc4ab 8059cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8060{
8061 /* Look for the `template' keyword. */
8062 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8063 /* Look for the `<'. */
8064 cp_parser_require (parser, CPP_LESS, "`<'");
8065 /* Look for the `>'. */
8066 cp_parser_require (parser, CPP_GREATER, "`>'");
8067 /* We have processed another parameter list. */
8068 ++parser->num_template_parameter_lists;
8069 /* Let the front end know that we are beginning a specialization. */
8070 begin_specialization ();
8071
8072 /* If the next keyword is `template', we need to figure out whether
8073 or not we're looking a template-declaration. */
8074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8075 {
8076 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8077 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8078 cp_parser_template_declaration_after_export (parser,
8079 /*member_p=*/false);
8080 else
8081 cp_parser_explicit_specialization (parser);
8082 }
8083 else
8084 /* Parse the dependent declaration. */
8085 cp_parser_single_declaration (parser,
8086 /*member_p=*/false,
8087 /*friend_p=*/NULL);
8088
8089 /* We're done with the specialization. */
8090 end_specialization ();
8091 /* We're done with this parameter list. */
8092 --parser->num_template_parameter_lists;
8093}
8094
8095/* Parse a type-specifier.
8096
8097 type-specifier:
8098 simple-type-specifier
8099 class-specifier
8100 enum-specifier
8101 elaborated-type-specifier
8102 cv-qualifier
8103
8104 GNU Extension:
8105
8106 type-specifier:
8107 __complex__
8108
8109 Returns a representation of the type-specifier. If the
8110 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8111 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8112 For a class-specifier, enum-specifier, or elaborated-type-specifier
8113 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8114
8115 If IS_FRIEND is TRUE then this type-specifier is being declared a
8116 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8117 appearing in a decl-specifier-seq.
8118
8119 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8120 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8121 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8122 if a type is declared; 2 if it is defined. Otherwise, it is set to
8123 zero.
a723baf1
MM
8124
8125 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8126 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8127 is set to FALSE. */
8128
8129static tree
94edc4ab
NN
8130cp_parser_type_specifier (cp_parser* parser,
8131 cp_parser_flags flags,
8132 bool is_friend,
8133 bool is_declaration,
560ad596 8134 int* declares_class_or_enum,
94edc4ab 8135 bool* is_cv_qualifier)
a723baf1
MM
8136{
8137 tree type_spec = NULL_TREE;
8138 cp_token *token;
8139 enum rid keyword;
8140
8141 /* Assume this type-specifier does not declare a new type. */
8142 if (declares_class_or_enum)
8143 *declares_class_or_enum = false;
8144 /* And that it does not specify a cv-qualifier. */
8145 if (is_cv_qualifier)
8146 *is_cv_qualifier = false;
8147 /* Peek at the next token. */
8148 token = cp_lexer_peek_token (parser->lexer);
8149
8150 /* If we're looking at a keyword, we can use that to guide the
8151 production we choose. */
8152 keyword = token->keyword;
8153 switch (keyword)
8154 {
8155 /* Any of these indicate either a class-specifier, or an
8156 elaborated-type-specifier. */
8157 case RID_CLASS:
8158 case RID_STRUCT:
8159 case RID_UNION:
8160 case RID_ENUM:
8161 /* Parse tentatively so that we can back up if we don't find a
8162 class-specifier or enum-specifier. */
8163 cp_parser_parse_tentatively (parser);
8164 /* Look for the class-specifier or enum-specifier. */
8165 if (keyword == RID_ENUM)
8166 type_spec = cp_parser_enum_specifier (parser);
8167 else
8168 type_spec = cp_parser_class_specifier (parser);
8169
8170 /* If that worked, we're done. */
8171 if (cp_parser_parse_definitely (parser))
8172 {
8173 if (declares_class_or_enum)
560ad596 8174 *declares_class_or_enum = 2;
a723baf1
MM
8175 return type_spec;
8176 }
8177
8178 /* Fall through. */
8179
8180 case RID_TYPENAME:
8181 /* Look for an elaborated-type-specifier. */
8182 type_spec = cp_parser_elaborated_type_specifier (parser,
8183 is_friend,
8184 is_declaration);
8185 /* We're declaring a class or enum -- unless we're using
8186 `typename'. */
8187 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8188 *declares_class_or_enum = 1;
a723baf1
MM
8189 return type_spec;
8190
8191 case RID_CONST:
8192 case RID_VOLATILE:
8193 case RID_RESTRICT:
8194 type_spec = cp_parser_cv_qualifier_opt (parser);
8195 /* Even though we call a routine that looks for an optional
8196 qualifier, we know that there should be one. */
8197 my_friendly_assert (type_spec != NULL, 20000328);
8198 /* This type-specifier was a cv-qualified. */
8199 if (is_cv_qualifier)
8200 *is_cv_qualifier = true;
8201
8202 return type_spec;
8203
8204 case RID_COMPLEX:
8205 /* The `__complex__' keyword is a GNU extension. */
8206 return cp_lexer_consume_token (parser->lexer)->value;
8207
8208 default:
8209 break;
8210 }
8211
8212 /* If we do not already have a type-specifier, assume we are looking
8213 at a simple-type-specifier. */
4b0d3cbe
MM
8214 type_spec = cp_parser_simple_type_specifier (parser, flags,
8215 /*identifier_p=*/true);
a723baf1
MM
8216
8217 /* If we didn't find a type-specifier, and a type-specifier was not
8218 optional in this context, issue an error message. */
8219 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8220 {
8221 cp_parser_error (parser, "expected type specifier");
8222 return error_mark_node;
8223 }
8224
8225 return type_spec;
8226}
8227
8228/* Parse a simple-type-specifier.
8229
8230 simple-type-specifier:
8231 :: [opt] nested-name-specifier [opt] type-name
8232 :: [opt] nested-name-specifier template template-id
8233 char
8234 wchar_t
8235 bool
8236 short
8237 int
8238 long
8239 signed
8240 unsigned
8241 float
8242 double
8243 void
8244
8245 GNU Extension:
8246
8247 simple-type-specifier:
8248 __typeof__ unary-expression
8249 __typeof__ ( type-id )
8250
8251 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8252 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8253 For the first two productions, and if IDENTIFIER_P is false, the
8254 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8255
8256static tree
4b0d3cbe
MM
8257cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8258 bool identifier_p)
a723baf1
MM
8259{
8260 tree type = NULL_TREE;
8261 cp_token *token;
8262
8263 /* Peek at the next token. */
8264 token = cp_lexer_peek_token (parser->lexer);
8265
8266 /* If we're looking at a keyword, things are easy. */
8267 switch (token->keyword)
8268 {
8269 case RID_CHAR:
4b0d3cbe
MM
8270 type = char_type_node;
8271 break;
a723baf1 8272 case RID_WCHAR:
4b0d3cbe
MM
8273 type = wchar_type_node;
8274 break;
a723baf1 8275 case RID_BOOL:
4b0d3cbe
MM
8276 type = boolean_type_node;
8277 break;
a723baf1 8278 case RID_SHORT:
4b0d3cbe
MM
8279 type = short_integer_type_node;
8280 break;
a723baf1 8281 case RID_INT:
4b0d3cbe
MM
8282 type = integer_type_node;
8283 break;
a723baf1 8284 case RID_LONG:
4b0d3cbe
MM
8285 type = long_integer_type_node;
8286 break;
a723baf1 8287 case RID_SIGNED:
4b0d3cbe
MM
8288 type = integer_type_node;
8289 break;
a723baf1 8290 case RID_UNSIGNED:
4b0d3cbe
MM
8291 type = unsigned_type_node;
8292 break;
a723baf1 8293 case RID_FLOAT:
4b0d3cbe
MM
8294 type = float_type_node;
8295 break;
a723baf1 8296 case RID_DOUBLE:
4b0d3cbe
MM
8297 type = double_type_node;
8298 break;
a723baf1 8299 case RID_VOID:
4b0d3cbe
MM
8300 type = void_type_node;
8301 break;
a723baf1
MM
8302
8303 case RID_TYPEOF:
8304 {
8305 tree operand;
8306
8307 /* Consume the `typeof' token. */
8308 cp_lexer_consume_token (parser->lexer);
8309 /* Parse the operand to `typeof' */
8310 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8311 /* If it is not already a TYPE, take its type. */
8312 if (!TYPE_P (operand))
8313 operand = finish_typeof (operand);
8314
8315 return operand;
8316 }
8317
8318 default:
8319 break;
8320 }
8321
4b0d3cbe
MM
8322 /* If the type-specifier was for a built-in type, we're done. */
8323 if (type)
8324 {
8325 tree id;
8326
8327 /* Consume the token. */
8328 id = cp_lexer_consume_token (parser->lexer)->value;
8329 return identifier_p ? id : TYPE_NAME (type);
8330 }
8331
a723baf1
MM
8332 /* The type-specifier must be a user-defined type. */
8333 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8334 {
8335 /* Don't gobble tokens or issue error messages if this is an
8336 optional type-specifier. */
8337 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8338 cp_parser_parse_tentatively (parser);
8339
8340 /* Look for the optional `::' operator. */
8341 cp_parser_global_scope_opt (parser,
8342 /*current_scope_valid_p=*/false);
8343 /* Look for the nested-name specifier. */
8344 cp_parser_nested_name_specifier_opt (parser,
8345 /*typename_keyword_p=*/false,
8346 /*check_dependency_p=*/true,
8347 /*type_p=*/false);
8348 /* If we have seen a nested-name-specifier, and the next token
8349 is `template', then we are using the template-id production. */
8350 if (parser->scope
8351 && cp_parser_optional_template_keyword (parser))
8352 {
8353 /* Look for the template-id. */
8354 type = cp_parser_template_id (parser,
8355 /*template_keyword_p=*/true,
8356 /*check_dependency_p=*/true);
8357 /* If the template-id did not name a type, we are out of
8358 luck. */
8359 if (TREE_CODE (type) != TYPE_DECL)
8360 {
8361 cp_parser_error (parser, "expected template-id for type");
8362 type = NULL_TREE;
8363 }
8364 }
8365 /* Otherwise, look for a type-name. */
8366 else
8367 {
8368 type = cp_parser_type_name (parser);
8369 if (type == error_mark_node)
8370 type = NULL_TREE;
8371 }
8372
8373 /* If it didn't work out, we don't have a TYPE. */
8374 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8375 && !cp_parser_parse_definitely (parser))
8376 type = NULL_TREE;
8377 }
8378
8379 /* If we didn't get a type-name, issue an error message. */
8380 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8381 {
8382 cp_parser_error (parser, "expected type-name");
8383 return error_mark_node;
8384 }
8385
8386 return type;
8387}
8388
8389/* Parse a type-name.
8390
8391 type-name:
8392 class-name
8393 enum-name
8394 typedef-name
8395
8396 enum-name:
8397 identifier
8398
8399 typedef-name:
8400 identifier
8401
8402 Returns a TYPE_DECL for the the type. */
8403
8404static tree
94edc4ab 8405cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8406{
8407 tree type_decl;
8408 tree identifier;
8409
8410 /* We can't know yet whether it is a class-name or not. */
8411 cp_parser_parse_tentatively (parser);
8412 /* Try a class-name. */
8413 type_decl = cp_parser_class_name (parser,
8414 /*typename_keyword_p=*/false,
8415 /*template_keyword_p=*/false,
8416 /*type_p=*/false,
a723baf1
MM
8417 /*check_dependency_p=*/true,
8418 /*class_head_p=*/false);
8419 /* If it's not a class-name, keep looking. */
8420 if (!cp_parser_parse_definitely (parser))
8421 {
8422 /* It must be a typedef-name or an enum-name. */
8423 identifier = cp_parser_identifier (parser);
8424 if (identifier == error_mark_node)
8425 return error_mark_node;
8426
8427 /* Look up the type-name. */
8428 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8429 /* Issue an error if we did not find a type-name. */
8430 if (TREE_CODE (type_decl) != TYPE_DECL)
8431 {
8432 cp_parser_error (parser, "expected type-name");
8433 type_decl = error_mark_node;
8434 }
8435 /* Remember that the name was used in the definition of the
8436 current class so that we can check later to see if the
8437 meaning would have been different after the class was
8438 entirely defined. */
8439 else if (type_decl != error_mark_node
8440 && !parser->scope)
8441 maybe_note_name_used_in_class (identifier, type_decl);
8442 }
8443
8444 return type_decl;
8445}
8446
8447
8448/* Parse an elaborated-type-specifier. Note that the grammar given
8449 here incorporates the resolution to DR68.
8450
8451 elaborated-type-specifier:
8452 class-key :: [opt] nested-name-specifier [opt] identifier
8453 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8454 enum :: [opt] nested-name-specifier [opt] identifier
8455 typename :: [opt] nested-name-specifier identifier
8456 typename :: [opt] nested-name-specifier template [opt]
8457 template-id
8458
8459 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8460 declared `friend'. If IS_DECLARATION is TRUE, then this
8461 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8462 something is being declared.
8463
8464 Returns the TYPE specified. */
8465
8466static tree
94edc4ab
NN
8467cp_parser_elaborated_type_specifier (cp_parser* parser,
8468 bool is_friend,
8469 bool is_declaration)
a723baf1
MM
8470{
8471 enum tag_types tag_type;
8472 tree identifier;
8473 tree type = NULL_TREE;
8474
8475 /* See if we're looking at the `enum' keyword. */
8476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8477 {
8478 /* Consume the `enum' token. */
8479 cp_lexer_consume_token (parser->lexer);
8480 /* Remember that it's an enumeration type. */
8481 tag_type = enum_type;
8482 }
8483 /* Or, it might be `typename'. */
8484 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8485 RID_TYPENAME))
8486 {
8487 /* Consume the `typename' token. */
8488 cp_lexer_consume_token (parser->lexer);
8489 /* Remember that it's a `typename' type. */
8490 tag_type = typename_type;
8491 /* The `typename' keyword is only allowed in templates. */
8492 if (!processing_template_decl)
8493 pedwarn ("using `typename' outside of template");
8494 }
8495 /* Otherwise it must be a class-key. */
8496 else
8497 {
8498 tag_type = cp_parser_class_key (parser);
8499 if (tag_type == none_type)
8500 return error_mark_node;
8501 }
8502
8503 /* Look for the `::' operator. */
8504 cp_parser_global_scope_opt (parser,
8505 /*current_scope_valid_p=*/false);
8506 /* Look for the nested-name-specifier. */
8507 if (tag_type == typename_type)
8fa1ad0e
MM
8508 {
8509 if (cp_parser_nested_name_specifier (parser,
8510 /*typename_keyword_p=*/true,
8511 /*check_dependency_p=*/true,
8512 /*type_p=*/true)
8513 == error_mark_node)
8514 return error_mark_node;
8515 }
a723baf1
MM
8516 else
8517 /* Even though `typename' is not present, the proposed resolution
8518 to Core Issue 180 says that in `class A<T>::B', `B' should be
8519 considered a type-name, even if `A<T>' is dependent. */
8520 cp_parser_nested_name_specifier_opt (parser,
8521 /*typename_keyword_p=*/true,
8522 /*check_dependency_p=*/true,
8523 /*type_p=*/true);
8524 /* For everything but enumeration types, consider a template-id. */
8525 if (tag_type != enum_type)
8526 {
8527 bool template_p = false;
8528 tree decl;
8529
8530 /* Allow the `template' keyword. */
8531 template_p = cp_parser_optional_template_keyword (parser);
8532 /* If we didn't see `template', we don't know if there's a
8533 template-id or not. */
8534 if (!template_p)
8535 cp_parser_parse_tentatively (parser);
8536 /* Parse the template-id. */
8537 decl = cp_parser_template_id (parser, template_p,
8538 /*check_dependency_p=*/true);
8539 /* If we didn't find a template-id, look for an ordinary
8540 identifier. */
8541 if (!template_p && !cp_parser_parse_definitely (parser))
8542 ;
8543 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8544 in effect, then we must assume that, upon instantiation, the
8545 template will correspond to a class. */
8546 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8547 && tag_type == typename_type)
8548 type = make_typename_type (parser->scope, decl,
8549 /*complain=*/1);
8550 else
8551 type = TREE_TYPE (decl);
8552 }
8553
8554 /* For an enumeration type, consider only a plain identifier. */
8555 if (!type)
8556 {
8557 identifier = cp_parser_identifier (parser);
8558
8559 if (identifier == error_mark_node)
eb5abb39
NS
8560 {
8561 parser->scope = NULL_TREE;
8562 return error_mark_node;
8563 }
a723baf1
MM
8564
8565 /* For a `typename', we needn't call xref_tag. */
8566 if (tag_type == typename_type)
8567 return make_typename_type (parser->scope, identifier,
8568 /*complain=*/1);
8569 /* Look up a qualified name in the usual way. */
8570 if (parser->scope)
8571 {
8572 tree decl;
8573
8574 /* In an elaborated-type-specifier, names are assumed to name
8575 types, so we set IS_TYPE to TRUE when calling
8576 cp_parser_lookup_name. */
8577 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8578 /*is_type=*/true,
eea9800f 8579 /*is_namespace=*/false,
a723baf1 8580 /*check_dependency=*/true);
710b73e6
KL
8581
8582 /* If we are parsing friend declaration, DECL may be a
8583 TEMPLATE_DECL tree node here. However, we need to check
8584 whether this TEMPLATE_DECL results in valid code. Consider
8585 the following example:
8586
8587 namespace N {
8588 template <class T> class C {};
8589 }
8590 class X {
8591 template <class T> friend class N::C; // #1, valid code
8592 };
8593 template <class T> class Y {
8594 friend class N::C; // #2, invalid code
8595 };
8596
8597 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8598 name lookup of `N::C'. We see that friend declaration must
8599 be template for the code to be valid. Note that
8600 processing_template_decl does not work here since it is
8601 always 1 for the above two cases. */
8602
a723baf1 8603 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8604 (decl, /*tag_name_p=*/is_friend
8605 && parser->num_template_parameter_lists));
a723baf1
MM
8606
8607 if (TREE_CODE (decl) != TYPE_DECL)
8608 {
8609 error ("expected type-name");
8610 return error_mark_node;
8611 }
560ad596
MM
8612
8613 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8614 check_elaborated_type_specifier
4b0d3cbe 8615 (tag_type, decl,
560ad596
MM
8616 (parser->num_template_parameter_lists
8617 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
8618
8619 type = TREE_TYPE (decl);
8620 }
8621 else
8622 {
8623 /* An elaborated-type-specifier sometimes introduces a new type and
8624 sometimes names an existing type. Normally, the rule is that it
8625 introduces a new type only if there is not an existing type of
8626 the same name already in scope. For example, given:
8627
8628 struct S {};
8629 void f() { struct S s; }
8630
8631 the `struct S' in the body of `f' is the same `struct S' as in
8632 the global scope; the existing definition is used. However, if
8633 there were no global declaration, this would introduce a new
8634 local class named `S'.
8635
8636 An exception to this rule applies to the following code:
8637
8638 namespace N { struct S; }
8639
8640 Here, the elaborated-type-specifier names a new type
8641 unconditionally; even if there is already an `S' in the
8642 containing scope this declaration names a new type.
8643 This exception only applies if the elaborated-type-specifier
8644 forms the complete declaration:
8645
8646 [class.name]
8647
8648 A declaration consisting solely of `class-key identifier ;' is
8649 either a redeclaration of the name in the current scope or a
8650 forward declaration of the identifier as a class name. It
8651 introduces the name into the current scope.
8652
8653 We are in this situation precisely when the next token is a `;'.
8654
8655 An exception to the exception is that a `friend' declaration does
8656 *not* name a new type; i.e., given:
8657
8658 struct S { friend struct T; };
8659
8660 `T' is not a new type in the scope of `S'.
8661
8662 Also, `new struct S' or `sizeof (struct S)' never results in the
8663 definition of a new type; a new type can only be declared in a
9bcb9aae 8664 declaration context. */
a723baf1
MM
8665
8666 type = xref_tag (tag_type, identifier,
8667 /*attributes=*/NULL_TREE,
8668 (is_friend
8669 || !is_declaration
8670 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
8671 CPP_SEMICOLON)),
8672 parser->num_template_parameter_lists);
a723baf1
MM
8673 }
8674 }
8675 if (tag_type != enum_type)
8676 cp_parser_check_class_key (tag_type, type);
8677 return type;
8678}
8679
8680/* Parse an enum-specifier.
8681
8682 enum-specifier:
8683 enum identifier [opt] { enumerator-list [opt] }
8684
8685 Returns an ENUM_TYPE representing the enumeration. */
8686
8687static tree
94edc4ab 8688cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
8689{
8690 cp_token *token;
8691 tree identifier = NULL_TREE;
8692 tree type;
8693
8694 /* Look for the `enum' keyword. */
8695 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8696 return error_mark_node;
8697 /* Peek at the next token. */
8698 token = cp_lexer_peek_token (parser->lexer);
8699
8700 /* See if it is an identifier. */
8701 if (token->type == CPP_NAME)
8702 identifier = cp_parser_identifier (parser);
8703
8704 /* Look for the `{'. */
8705 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8706 return error_mark_node;
8707
8708 /* At this point, we're going ahead with the enum-specifier, even
8709 if some other problem occurs. */
8710 cp_parser_commit_to_tentative_parse (parser);
8711
8712 /* Issue an error message if type-definitions are forbidden here. */
8713 cp_parser_check_type_definition (parser);
8714
8715 /* Create the new type. */
8716 type = start_enum (identifier ? identifier : make_anon_name ());
8717
8718 /* Peek at the next token. */
8719 token = cp_lexer_peek_token (parser->lexer);
8720 /* If it's not a `}', then there are some enumerators. */
8721 if (token->type != CPP_CLOSE_BRACE)
8722 cp_parser_enumerator_list (parser, type);
8723 /* Look for the `}'. */
8724 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8725
8726 /* Finish up the enumeration. */
8727 finish_enum (type);
8728
8729 return type;
8730}
8731
8732/* Parse an enumerator-list. The enumerators all have the indicated
8733 TYPE.
8734
8735 enumerator-list:
8736 enumerator-definition
8737 enumerator-list , enumerator-definition */
8738
8739static void
94edc4ab 8740cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
8741{
8742 while (true)
8743 {
8744 cp_token *token;
8745
8746 /* Parse an enumerator-definition. */
8747 cp_parser_enumerator_definition (parser, type);
8748 /* Peek at the next token. */
8749 token = cp_lexer_peek_token (parser->lexer);
8750 /* If it's not a `,', then we've reached the end of the
8751 list. */
8752 if (token->type != CPP_COMMA)
8753 break;
8754 /* Otherwise, consume the `,' and keep going. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* If the next token is a `}', there is a trailing comma. */
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8758 {
8759 if (pedantic && !in_system_header)
8760 pedwarn ("comma at end of enumerator list");
8761 break;
8762 }
8763 }
8764}
8765
8766/* Parse an enumerator-definition. The enumerator has the indicated
8767 TYPE.
8768
8769 enumerator-definition:
8770 enumerator
8771 enumerator = constant-expression
8772
8773 enumerator:
8774 identifier */
8775
8776static void
94edc4ab 8777cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
8778{
8779 cp_token *token;
8780 tree identifier;
8781 tree value;
8782
8783 /* Look for the identifier. */
8784 identifier = cp_parser_identifier (parser);
8785 if (identifier == error_mark_node)
8786 return;
8787
8788 /* Peek at the next token. */
8789 token = cp_lexer_peek_token (parser->lexer);
8790 /* If it's an `=', then there's an explicit value. */
8791 if (token->type == CPP_EQ)
8792 {
8793 /* Consume the `=' token. */
8794 cp_lexer_consume_token (parser->lexer);
8795 /* Parse the value. */
14d22dd6 8796 value = cp_parser_constant_expression (parser,
d17811fd 8797 /*allow_non_constant_p=*/false,
14d22dd6 8798 NULL);
a723baf1
MM
8799 }
8800 else
8801 value = NULL_TREE;
8802
8803 /* Create the enumerator. */
8804 build_enumerator (identifier, value, type);
8805}
8806
8807/* Parse a namespace-name.
8808
8809 namespace-name:
8810 original-namespace-name
8811 namespace-alias
8812
8813 Returns the NAMESPACE_DECL for the namespace. */
8814
8815static tree
94edc4ab 8816cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
8817{
8818 tree identifier;
8819 tree namespace_decl;
8820
8821 /* Get the name of the namespace. */
8822 identifier = cp_parser_identifier (parser);
8823 if (identifier == error_mark_node)
8824 return error_mark_node;
8825
eea9800f
MM
8826 /* Look up the identifier in the currently active scope. Look only
8827 for namespaces, due to:
8828
8829 [basic.lookup.udir]
8830
8831 When looking up a namespace-name in a using-directive or alias
8832 definition, only namespace names are considered.
8833
8834 And:
8835
8836 [basic.lookup.qual]
8837
8838 During the lookup of a name preceding the :: scope resolution
8839 operator, object, function, and enumerator names are ignored.
8840
8841 (Note that cp_parser_class_or_namespace_name only calls this
8842 function if the token after the name is the scope resolution
8843 operator.) */
8844 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f
MM
8845 /*is_type=*/false,
8846 /*is_namespace=*/true,
8847 /*check_dependency=*/true);
a723baf1
MM
8848 /* If it's not a namespace, issue an error. */
8849 if (namespace_decl == error_mark_node
8850 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8851 {
8852 cp_parser_error (parser, "expected namespace-name");
8853 namespace_decl = error_mark_node;
8854 }
8855
8856 return namespace_decl;
8857}
8858
8859/* Parse a namespace-definition.
8860
8861 namespace-definition:
8862 named-namespace-definition
8863 unnamed-namespace-definition
8864
8865 named-namespace-definition:
8866 original-namespace-definition
8867 extension-namespace-definition
8868
8869 original-namespace-definition:
8870 namespace identifier { namespace-body }
8871
8872 extension-namespace-definition:
8873 namespace original-namespace-name { namespace-body }
8874
8875 unnamed-namespace-definition:
8876 namespace { namespace-body } */
8877
8878static void
94edc4ab 8879cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
8880{
8881 tree identifier;
8882
8883 /* Look for the `namespace' keyword. */
8884 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8885
8886 /* Get the name of the namespace. We do not attempt to distinguish
8887 between an original-namespace-definition and an
8888 extension-namespace-definition at this point. The semantic
8889 analysis routines are responsible for that. */
8890 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8891 identifier = cp_parser_identifier (parser);
8892 else
8893 identifier = NULL_TREE;
8894
8895 /* Look for the `{' to start the namespace. */
8896 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8897 /* Start the namespace. */
8898 push_namespace (identifier);
8899 /* Parse the body of the namespace. */
8900 cp_parser_namespace_body (parser);
8901 /* Finish the namespace. */
8902 pop_namespace ();
8903 /* Look for the final `}'. */
8904 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8905}
8906
8907/* Parse a namespace-body.
8908
8909 namespace-body:
8910 declaration-seq [opt] */
8911
8912static void
94edc4ab 8913cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
8914{
8915 cp_parser_declaration_seq_opt (parser);
8916}
8917
8918/* Parse a namespace-alias-definition.
8919
8920 namespace-alias-definition:
8921 namespace identifier = qualified-namespace-specifier ; */
8922
8923static void
94edc4ab 8924cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
8925{
8926 tree identifier;
8927 tree namespace_specifier;
8928
8929 /* Look for the `namespace' keyword. */
8930 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8931 /* Look for the identifier. */
8932 identifier = cp_parser_identifier (parser);
8933 if (identifier == error_mark_node)
8934 return;
8935 /* Look for the `=' token. */
8936 cp_parser_require (parser, CPP_EQ, "`='");
8937 /* Look for the qualified-namespace-specifier. */
8938 namespace_specifier
8939 = cp_parser_qualified_namespace_specifier (parser);
8940 /* Look for the `;' token. */
8941 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8942
8943 /* Register the alias in the symbol table. */
8944 do_namespace_alias (identifier, namespace_specifier);
8945}
8946
8947/* Parse a qualified-namespace-specifier.
8948
8949 qualified-namespace-specifier:
8950 :: [opt] nested-name-specifier [opt] namespace-name
8951
8952 Returns a NAMESPACE_DECL corresponding to the specified
8953 namespace. */
8954
8955static tree
94edc4ab 8956cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
8957{
8958 /* Look for the optional `::'. */
8959 cp_parser_global_scope_opt (parser,
8960 /*current_scope_valid_p=*/false);
8961
8962 /* Look for the optional nested-name-specifier. */
8963 cp_parser_nested_name_specifier_opt (parser,
8964 /*typename_keyword_p=*/false,
8965 /*check_dependency_p=*/true,
8966 /*type_p=*/false);
8967
8968 return cp_parser_namespace_name (parser);
8969}
8970
8971/* Parse a using-declaration.
8972
8973 using-declaration:
8974 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8975 using :: unqualified-id ; */
8976
8977static void
94edc4ab 8978cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
8979{
8980 cp_token *token;
8981 bool typename_p = false;
8982 bool global_scope_p;
8983 tree decl;
8984 tree identifier;
8985 tree scope;
8986
8987 /* Look for the `using' keyword. */
8988 cp_parser_require_keyword (parser, RID_USING, "`using'");
8989
8990 /* Peek at the next token. */
8991 token = cp_lexer_peek_token (parser->lexer);
8992 /* See if it's `typename'. */
8993 if (token->keyword == RID_TYPENAME)
8994 {
8995 /* Remember that we've seen it. */
8996 typename_p = true;
8997 /* Consume the `typename' token. */
8998 cp_lexer_consume_token (parser->lexer);
8999 }
9000
9001 /* Look for the optional global scope qualification. */
9002 global_scope_p
9003 = (cp_parser_global_scope_opt (parser,
9004 /*current_scope_valid_p=*/false)
9005 != NULL_TREE);
9006
9007 /* If we saw `typename', or didn't see `::', then there must be a
9008 nested-name-specifier present. */
9009 if (typename_p || !global_scope_p)
9010 cp_parser_nested_name_specifier (parser, typename_p,
9011 /*check_dependency_p=*/true,
9012 /*type_p=*/false);
9013 /* Otherwise, we could be in either of the two productions. In that
9014 case, treat the nested-name-specifier as optional. */
9015 else
9016 cp_parser_nested_name_specifier_opt (parser,
9017 /*typename_keyword_p=*/false,
9018 /*check_dependency_p=*/true,
9019 /*type_p=*/false);
9020
9021 /* Parse the unqualified-id. */
9022 identifier = cp_parser_unqualified_id (parser,
9023 /*template_keyword_p=*/false,
f3c2dfc6
MM
9024 /*check_dependency_p=*/true,
9025 /*declarator_p=*/true);
a723baf1
MM
9026
9027 /* The function we call to handle a using-declaration is different
9028 depending on what scope we are in. */
f3c2dfc6
MM
9029 if (identifier == error_mark_node)
9030 ;
9031 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9032 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9033 /* [namespace.udecl]
9034
9035 A using declaration shall not name a template-id. */
9036 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9037 else
9038 {
f3c2dfc6
MM
9039 scope = current_scope ();
9040 if (scope && TYPE_P (scope))
4eb6d609 9041 {
f3c2dfc6
MM
9042 /* Create the USING_DECL. */
9043 decl = do_class_using_decl (build_nt (SCOPE_REF,
9044 parser->scope,
9045 identifier));
9046 /* Add it to the list of members in this class. */
9047 finish_member_declaration (decl);
4eb6d609 9048 }
a723baf1 9049 else
f3c2dfc6
MM
9050 {
9051 decl = cp_parser_lookup_name_simple (parser, identifier);
9052 if (decl == error_mark_node)
9053 {
9054 if (parser->scope && parser->scope != global_namespace)
9055 error ("`%D::%D' has not been declared",
9056 parser->scope, identifier);
9057 else
9058 error ("`::%D' has not been declared", identifier);
9059 }
9060 else if (scope)
9061 do_local_using_decl (decl);
9062 else
9063 do_toplevel_using_decl (decl);
9064 }
a723baf1
MM
9065 }
9066
9067 /* Look for the final `;'. */
9068 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9069}
9070
9071/* Parse a using-directive.
9072
9073 using-directive:
9074 using namespace :: [opt] nested-name-specifier [opt]
9075 namespace-name ; */
9076
9077static void
94edc4ab 9078cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9079{
9080 tree namespace_decl;
9081
9082 /* Look for the `using' keyword. */
9083 cp_parser_require_keyword (parser, RID_USING, "`using'");
9084 /* And the `namespace' keyword. */
9085 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9086 /* Look for the optional `::' operator. */
9087 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9088 /* And the optional nested-name-specifier. */
a723baf1
MM
9089 cp_parser_nested_name_specifier_opt (parser,
9090 /*typename_keyword_p=*/false,
9091 /*check_dependency_p=*/true,
9092 /*type_p=*/false);
9093 /* Get the namespace being used. */
9094 namespace_decl = cp_parser_namespace_name (parser);
9095 /* Update the symbol table. */
9096 do_using_directive (namespace_decl);
9097 /* Look for the final `;'. */
9098 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9099}
9100
9101/* Parse an asm-definition.
9102
9103 asm-definition:
9104 asm ( string-literal ) ;
9105
9106 GNU Extension:
9107
9108 asm-definition:
9109 asm volatile [opt] ( string-literal ) ;
9110 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9111 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9112 : asm-operand-list [opt] ) ;
9113 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9114 : asm-operand-list [opt]
9115 : asm-operand-list [opt] ) ; */
9116
9117static void
94edc4ab 9118cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9119{
9120 cp_token *token;
9121 tree string;
9122 tree outputs = NULL_TREE;
9123 tree inputs = NULL_TREE;
9124 tree clobbers = NULL_TREE;
9125 tree asm_stmt;
9126 bool volatile_p = false;
9127 bool extended_p = false;
9128
9129 /* Look for the `asm' keyword. */
9130 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9131 /* See if the next token is `volatile'. */
9132 if (cp_parser_allow_gnu_extensions_p (parser)
9133 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9134 {
9135 /* Remember that we saw the `volatile' keyword. */
9136 volatile_p = true;
9137 /* Consume the token. */
9138 cp_lexer_consume_token (parser->lexer);
9139 }
9140 /* Look for the opening `('. */
9141 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9142 /* Look for the string. */
9143 token = cp_parser_require (parser, CPP_STRING, "asm body");
9144 if (!token)
9145 return;
9146 string = token->value;
9147 /* If we're allowing GNU extensions, check for the extended assembly
9148 syntax. Unfortunately, the `:' tokens need not be separated by
9149 a space in C, and so, for compatibility, we tolerate that here
9150 too. Doing that means that we have to treat the `::' operator as
9151 two `:' tokens. */
9152 if (cp_parser_allow_gnu_extensions_p (parser)
9153 && at_function_scope_p ()
9154 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9155 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9156 {
9157 bool inputs_p = false;
9158 bool clobbers_p = false;
9159
9160 /* The extended syntax was used. */
9161 extended_p = true;
9162
9163 /* Look for outputs. */
9164 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9165 {
9166 /* Consume the `:'. */
9167 cp_lexer_consume_token (parser->lexer);
9168 /* Parse the output-operands. */
9169 if (cp_lexer_next_token_is_not (parser->lexer,
9170 CPP_COLON)
9171 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9172 CPP_SCOPE)
9173 && cp_lexer_next_token_is_not (parser->lexer,
9174 CPP_CLOSE_PAREN))
a723baf1
MM
9175 outputs = cp_parser_asm_operand_list (parser);
9176 }
9177 /* If the next token is `::', there are no outputs, and the
9178 next token is the beginning of the inputs. */
9179 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9180 {
9181 /* Consume the `::' token. */
9182 cp_lexer_consume_token (parser->lexer);
9183 /* The inputs are coming next. */
9184 inputs_p = true;
9185 }
9186
9187 /* Look for inputs. */
9188 if (inputs_p
9189 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9190 {
9191 if (!inputs_p)
9192 /* Consume the `:'. */
9193 cp_lexer_consume_token (parser->lexer);
9194 /* Parse the output-operands. */
9195 if (cp_lexer_next_token_is_not (parser->lexer,
9196 CPP_COLON)
9197 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9198 CPP_SCOPE)
9199 && cp_lexer_next_token_is_not (parser->lexer,
9200 CPP_CLOSE_PAREN))
a723baf1
MM
9201 inputs = cp_parser_asm_operand_list (parser);
9202 }
9203 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9204 /* The clobbers are coming next. */
9205 clobbers_p = true;
9206
9207 /* Look for clobbers. */
9208 if (clobbers_p
9209 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9210 {
9211 if (!clobbers_p)
9212 /* Consume the `:'. */
9213 cp_lexer_consume_token (parser->lexer);
9214 /* Parse the clobbers. */
8caf4c38
MM
9215 if (cp_lexer_next_token_is_not (parser->lexer,
9216 CPP_CLOSE_PAREN))
9217 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9218 }
9219 }
9220 /* Look for the closing `)'. */
9221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 9222 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
9223 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9224
9225 /* Create the ASM_STMT. */
9226 if (at_function_scope_p ())
9227 {
9228 asm_stmt =
9229 finish_asm_stmt (volatile_p
9230 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9231 string, outputs, inputs, clobbers);
9232 /* If the extended syntax was not used, mark the ASM_STMT. */
9233 if (!extended_p)
9234 ASM_INPUT_P (asm_stmt) = 1;
9235 }
9236 else
9237 assemble_asm (string);
9238}
9239
9240/* Declarators [gram.dcl.decl] */
9241
9242/* Parse an init-declarator.
9243
9244 init-declarator:
9245 declarator initializer [opt]
9246
9247 GNU Extension:
9248
9249 init-declarator:
9250 declarator asm-specification [opt] attributes [opt] initializer [opt]
9251
9252 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9253 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9254 then this declarator appears in a class scope. The new DECL created
9255 by this declarator is returned.
a723baf1
MM
9256
9257 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9258 for a function-definition here as well. If the declarator is a
9259 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9260 be TRUE upon return. By that point, the function-definition will
9261 have been completely parsed.
9262
9263 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9264 is FALSE. */
9265
9266static tree
94edc4ab
NN
9267cp_parser_init_declarator (cp_parser* parser,
9268 tree decl_specifiers,
9269 tree prefix_attributes,
9270 bool function_definition_allowed_p,
9271 bool member_p,
560ad596 9272 int declares_class_or_enum,
94edc4ab 9273 bool* function_definition_p)
a723baf1
MM
9274{
9275 cp_token *token;
9276 tree declarator;
9277 tree attributes;
9278 tree asm_specification;
9279 tree initializer;
9280 tree decl = NULL_TREE;
9281 tree scope;
a723baf1
MM
9282 bool is_initialized;
9283 bool is_parenthesized_init;
39703eb9 9284 bool is_non_constant_init;
7efa3e22 9285 int ctor_dtor_or_conv_p;
a723baf1
MM
9286 bool friend_p;
9287
9288 /* Assume that this is not the declarator for a function
9289 definition. */
9290 if (function_definition_p)
9291 *function_definition_p = false;
9292
9293 /* Defer access checks while parsing the declarator; we cannot know
9294 what names are accessible until we know what is being
9295 declared. */
cf22909c
KL
9296 resume_deferring_access_checks ();
9297
a723baf1
MM
9298 /* Parse the declarator. */
9299 declarator
62b8a44e 9300 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9301 &ctor_dtor_or_conv_p);
9302 /* Gather up the deferred checks. */
cf22909c 9303 stop_deferring_access_checks ();
24c0ef37 9304
a723baf1
MM
9305 /* If the DECLARATOR was erroneous, there's no need to go
9306 further. */
9307 if (declarator == error_mark_node)
cf22909c 9308 return error_mark_node;
a723baf1 9309
560ad596
MM
9310 cp_parser_check_for_definition_in_return_type (declarator,
9311 declares_class_or_enum);
9312
a723baf1
MM
9313 /* Figure out what scope the entity declared by the DECLARATOR is
9314 located in. `grokdeclarator' sometimes changes the scope, so
9315 we compute it now. */
9316 scope = get_scope_of_declarator (declarator);
9317
9318 /* If we're allowing GNU extensions, look for an asm-specification
9319 and attributes. */
9320 if (cp_parser_allow_gnu_extensions_p (parser))
9321 {
9322 /* Look for an asm-specification. */
9323 asm_specification = cp_parser_asm_specification_opt (parser);
9324 /* And attributes. */
9325 attributes = cp_parser_attributes_opt (parser);
9326 }
9327 else
9328 {
9329 asm_specification = NULL_TREE;
9330 attributes = NULL_TREE;
9331 }
9332
9333 /* Peek at the next token. */
9334 token = cp_lexer_peek_token (parser->lexer);
9335 /* Check to see if the token indicates the start of a
9336 function-definition. */
9337 if (cp_parser_token_starts_function_definition_p (token))
9338 {
9339 if (!function_definition_allowed_p)
9340 {
9341 /* If a function-definition should not appear here, issue an
9342 error message. */
9343 cp_parser_error (parser,
9344 "a function-definition is not allowed here");
9345 return error_mark_node;
9346 }
9347 else
9348 {
a723baf1
MM
9349 /* Neither attributes nor an asm-specification are allowed
9350 on a function-definition. */
9351 if (asm_specification)
9352 error ("an asm-specification is not allowed on a function-definition");
9353 if (attributes)
9354 error ("attributes are not allowed on a function-definition");
9355 /* This is a function-definition. */
9356 *function_definition_p = true;
9357
a723baf1
MM
9358 /* Parse the function definition. */
9359 decl = (cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 9360 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9361
a723baf1
MM
9362 return decl;
9363 }
9364 }
9365
9366 /* [dcl.dcl]
9367
9368 Only in function declarations for constructors, destructors, and
9369 type conversions can the decl-specifier-seq be omitted.
9370
9371 We explicitly postpone this check past the point where we handle
9372 function-definitions because we tolerate function-definitions
9373 that are missing their return types in some modes. */
7efa3e22 9374 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9375 {
9376 cp_parser_error (parser,
9377 "expected constructor, destructor, or type conversion");
9378 return error_mark_node;
9379 }
9380
9381 /* An `=' or an `(' indicates an initializer. */
9382 is_initialized = (token->type == CPP_EQ
9383 || token->type == CPP_OPEN_PAREN);
9384 /* If the init-declarator isn't initialized and isn't followed by a
9385 `,' or `;', it's not a valid init-declarator. */
9386 if (!is_initialized
9387 && token->type != CPP_COMMA
9388 && token->type != CPP_SEMICOLON)
9389 {
9390 cp_parser_error (parser, "expected init-declarator");
9391 return error_mark_node;
9392 }
9393
9394 /* Because start_decl has side-effects, we should only call it if we
9395 know we're going ahead. By this point, we know that we cannot
9396 possibly be looking at any other construct. */
9397 cp_parser_commit_to_tentative_parse (parser);
9398
9399 /* Check to see whether or not this declaration is a friend. */
9400 friend_p = cp_parser_friend_p (decl_specifiers);
9401
9402 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 9403 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 9404 return error_mark_node;
a723baf1
MM
9405
9406 /* Enter the newly declared entry in the symbol table. If we're
9407 processing a declaration in a class-specifier, we wait until
9408 after processing the initializer. */
9409 if (!member_p)
9410 {
9411 if (parser->in_unbraced_linkage_specification_p)
9412 {
9413 decl_specifiers = tree_cons (error_mark_node,
9414 get_identifier ("extern"),
9415 decl_specifiers);
9416 have_extern_spec = false;
9417 }
ee3071ef
NS
9418 decl = start_decl (declarator, decl_specifiers,
9419 is_initialized, attributes, prefix_attributes);
a723baf1
MM
9420 }
9421
9422 /* Enter the SCOPE. That way unqualified names appearing in the
9423 initializer will be looked up in SCOPE. */
9424 if (scope)
9425 push_scope (scope);
9426
9427 /* Perform deferred access control checks, now that we know in which
9428 SCOPE the declared entity resides. */
9429 if (!member_p && decl)
9430 {
9431 tree saved_current_function_decl = NULL_TREE;
9432
9433 /* If the entity being declared is a function, pretend that we
9434 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9435 things that would not otherwise be accessible. */
a723baf1
MM
9436 if (TREE_CODE (decl) == FUNCTION_DECL)
9437 {
9438 saved_current_function_decl = current_function_decl;
9439 current_function_decl = decl;
9440 }
9441
cf22909c
KL
9442 /* Perform the access control checks for the declarator and the
9443 the decl-specifiers. */
9444 perform_deferred_access_checks ();
a723baf1
MM
9445
9446 /* Restore the saved value. */
9447 if (TREE_CODE (decl) == FUNCTION_DECL)
9448 current_function_decl = saved_current_function_decl;
9449 }
9450
9451 /* Parse the initializer. */
9452 if (is_initialized)
39703eb9
MM
9453 initializer = cp_parser_initializer (parser,
9454 &is_parenthesized_init,
9455 &is_non_constant_init);
a723baf1
MM
9456 else
9457 {
9458 initializer = NULL_TREE;
9459 is_parenthesized_init = false;
39703eb9 9460 is_non_constant_init = true;
a723baf1
MM
9461 }
9462
9463 /* The old parser allows attributes to appear after a parenthesized
9464 initializer. Mark Mitchell proposed removing this functionality
9465 on the GCC mailing lists on 2002-08-13. This parser accepts the
9466 attributes -- but ignores them. */
9467 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9468 if (cp_parser_attributes_opt (parser))
9469 warning ("attributes after parenthesized initializer ignored");
9470
9471 /* Leave the SCOPE, now that we have processed the initializer. It
9472 is important to do this before calling cp_finish_decl because it
9473 makes decisions about whether to create DECL_STMTs or not based
9474 on the current scope. */
9475 if (scope)
9476 pop_scope (scope);
9477
9478 /* For an in-class declaration, use `grokfield' to create the
9479 declaration. */
9480 if (member_p)
8db1028e
NS
9481 {
9482 decl = grokfield (declarator, decl_specifiers,
9483 initializer, /*asmspec=*/NULL_TREE,
a723baf1 9484 /*attributes=*/NULL_TREE);
8db1028e
NS
9485 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9486 cp_parser_save_default_args (parser, decl);
9487 }
9488
a723baf1
MM
9489 /* Finish processing the declaration. But, skip friend
9490 declarations. */
9491 if (!friend_p && decl)
9492 cp_finish_decl (decl,
9493 initializer,
9494 asm_specification,
9495 /* If the initializer is in parentheses, then this is
9496 a direct-initialization, which means that an
9497 `explicit' constructor is OK. Otherwise, an
9498 `explicit' constructor cannot be used. */
9499 ((is_parenthesized_init || !is_initialized)
9500 ? 0 : LOOKUP_ONLYCONVERTING));
9501
39703eb9
MM
9502 /* Remember whether or not variables were initialized by
9503 constant-expressions. */
9504 if (decl && TREE_CODE (decl) == VAR_DECL
9505 && is_initialized && !is_non_constant_init)
9506 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9507
a723baf1
MM
9508 return decl;
9509}
9510
9511/* Parse a declarator.
9512
9513 declarator:
9514 direct-declarator
9515 ptr-operator declarator
9516
9517 abstract-declarator:
9518 ptr-operator abstract-declarator [opt]
9519 direct-abstract-declarator
9520
9521 GNU Extensions:
9522
9523 declarator:
9524 attributes [opt] direct-declarator
9525 attributes [opt] ptr-operator declarator
9526
9527 abstract-declarator:
9528 attributes [opt] ptr-operator abstract-declarator [opt]
9529 attributes [opt] direct-abstract-declarator
9530
9531 Returns a representation of the declarator. If the declarator has
9532 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 9533 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
9534 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9535 used. The first operand is the TYPE for `X'. The second operand
9536 is an INDIRECT_REF whose operand is the sub-declarator.
9537
34cd5ae7 9538 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
9539
9540 (It would be better to define a structure type to represent
9541 declarators, rather than abusing `tree' nodes to represent
9542 declarators. That would be much clearer and save some memory.
9543 There is no reason for declarators to be garbage-collected, for
9544 example; they are created during parser and no longer needed after
9545 `grokdeclarator' has been called.)
9546
9547 For a ptr-operator that has the optional cv-qualifier-seq,
9548 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9549 node.
9550
7efa3e22
NS
9551 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9552 detect constructor, destructor or conversion operators. It is set
9553 to -1 if the declarator is a name, and +1 if it is a
9554 function. Otherwise it is set to zero. Usually you just want to
9555 test for >0, but internally the negative value is used.
9556
a723baf1
MM
9557 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9558 a decl-specifier-seq unless it declares a constructor, destructor,
9559 or conversion. It might seem that we could check this condition in
9560 semantic analysis, rather than parsing, but that makes it difficult
9561 to handle something like `f()'. We want to notice that there are
9562 no decl-specifiers, and therefore realize that this is an
9563 expression, not a declaration.) */
9564
9565static tree
94edc4ab
NN
9566cp_parser_declarator (cp_parser* parser,
9567 cp_parser_declarator_kind dcl_kind,
7efa3e22 9568 int* ctor_dtor_or_conv_p)
a723baf1
MM
9569{
9570 cp_token *token;
9571 tree declarator;
9572 enum tree_code code;
9573 tree cv_qualifier_seq;
9574 tree class_type;
9575 tree attributes = NULL_TREE;
9576
9577 /* Assume this is not a constructor, destructor, or type-conversion
9578 operator. */
9579 if (ctor_dtor_or_conv_p)
7efa3e22 9580 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
9581
9582 if (cp_parser_allow_gnu_extensions_p (parser))
9583 attributes = cp_parser_attributes_opt (parser);
9584
9585 /* Peek at the next token. */
9586 token = cp_lexer_peek_token (parser->lexer);
9587
9588 /* Check for the ptr-operator production. */
9589 cp_parser_parse_tentatively (parser);
9590 /* Parse the ptr-operator. */
9591 code = cp_parser_ptr_operator (parser,
9592 &class_type,
9593 &cv_qualifier_seq);
9594 /* If that worked, then we have a ptr-operator. */
9595 if (cp_parser_parse_definitely (parser))
9596 {
9597 /* The dependent declarator is optional if we are parsing an
9598 abstract-declarator. */
62b8a44e 9599 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9600 cp_parser_parse_tentatively (parser);
9601
9602 /* Parse the dependent declarator. */
62b8a44e 9603 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
9604 /*ctor_dtor_or_conv_p=*/NULL);
9605
9606 /* If we are parsing an abstract-declarator, we must handle the
9607 case where the dependent declarator is absent. */
62b8a44e
NS
9608 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9609 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9610 declarator = NULL_TREE;
9611
9612 /* Build the representation of the ptr-operator. */
9613 if (code == INDIRECT_REF)
9614 declarator = make_pointer_declarator (cv_qualifier_seq,
9615 declarator);
9616 else
9617 declarator = make_reference_declarator (cv_qualifier_seq,
9618 declarator);
9619 /* Handle the pointer-to-member case. */
9620 if (class_type)
9621 declarator = build_nt (SCOPE_REF, class_type, declarator);
9622 }
9623 /* Everything else is a direct-declarator. */
9624 else
7efa3e22 9625 declarator = cp_parser_direct_declarator (parser, dcl_kind,
a723baf1
MM
9626 ctor_dtor_or_conv_p);
9627
9628 if (attributes && declarator != error_mark_node)
9629 declarator = tree_cons (attributes, declarator, NULL_TREE);
9630
9631 return declarator;
9632}
9633
9634/* Parse a direct-declarator or direct-abstract-declarator.
9635
9636 direct-declarator:
9637 declarator-id
9638 direct-declarator ( parameter-declaration-clause )
9639 cv-qualifier-seq [opt]
9640 exception-specification [opt]
9641 direct-declarator [ constant-expression [opt] ]
9642 ( declarator )
9643
9644 direct-abstract-declarator:
9645 direct-abstract-declarator [opt]
9646 ( parameter-declaration-clause )
9647 cv-qualifier-seq [opt]
9648 exception-specification [opt]
9649 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9650 ( abstract-declarator )
9651
62b8a44e
NS
9652 Returns a representation of the declarator. DCL_KIND is
9653 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9654 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9655 we are parsing a direct-declarator. It is
9656 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9657 of ambiguity we prefer an abstract declarator, as per
9658 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
9659 cp_parser_declarator.
9660
9661 For the declarator-id production, the representation is as for an
9662 id-expression, except that a qualified name is represented as a
9663 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9664 see the documentation of the FUNCTION_DECLARATOR_* macros for
9665 information about how to find the various declarator components.
9666 An array-declarator is represented as an ARRAY_REF. The
9667 direct-declarator is the first operand; the constant-expression
9668 indicating the size of the array is the second operand. */
9669
9670static tree
94edc4ab
NN
9671cp_parser_direct_declarator (cp_parser* parser,
9672 cp_parser_declarator_kind dcl_kind,
7efa3e22 9673 int* ctor_dtor_or_conv_p)
a723baf1
MM
9674{
9675 cp_token *token;
62b8a44e 9676 tree declarator = NULL_TREE;
a723baf1
MM
9677 tree scope = NULL_TREE;
9678 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9679 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
9680 bool first = true;
9681
9682 while (true)
a723baf1 9683 {
62b8a44e
NS
9684 /* Peek at the next token. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686 if (token->type == CPP_OPEN_PAREN)
a723baf1 9687 {
62b8a44e
NS
9688 /* This is either a parameter-declaration-clause, or a
9689 parenthesized declarator. When we know we are parsing a
34cd5ae7 9690 named declarator, it must be a parenthesized declarator
62b8a44e
NS
9691 if FIRST is true. For instance, `(int)' is a
9692 parameter-declaration-clause, with an omitted
9693 direct-abstract-declarator. But `((*))', is a
9694 parenthesized abstract declarator. Finally, when T is a
9695 template parameter `(T)' is a
34cd5ae7 9696 parameter-declaration-clause, and not a parenthesized
62b8a44e 9697 named declarator.
a723baf1 9698
62b8a44e
NS
9699 We first try and parse a parameter-declaration-clause,
9700 and then try a nested declarator (if FIRST is true).
a723baf1 9701
62b8a44e
NS
9702 It is not an error for it not to be a
9703 parameter-declaration-clause, even when FIRST is
9704 false. Consider,
9705
9706 int i (int);
9707 int i (3);
9708
9709 The first is the declaration of a function while the
9710 second is a the definition of a variable, including its
9711 initializer.
9712
9713 Having seen only the parenthesis, we cannot know which of
9714 these two alternatives should be selected. Even more
9715 complex are examples like:
9716
9717 int i (int (a));
9718 int i (int (3));
9719
9720 The former is a function-declaration; the latter is a
9721 variable initialization.
9722
34cd5ae7 9723 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
9724 that fails, we back out and return. */
9725
9726 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 9727 {
62b8a44e 9728 tree params;
4047b164 9729 unsigned saved_num_template_parameter_lists;
62b8a44e
NS
9730
9731 cp_parser_parse_tentatively (parser);
a723baf1 9732
62b8a44e
NS
9733 /* Consume the `('. */
9734 cp_lexer_consume_token (parser->lexer);
9735 if (first)
9736 {
9737 /* If this is going to be an abstract declarator, we're
9738 in a declarator and we can't have default args. */
9739 parser->default_arg_ok_p = false;
9740 parser->in_declarator_p = true;
9741 }
9742
4047b164
KL
9743 /* Inside the function parameter list, surrounding
9744 template-parameter-lists do not apply. */
9745 saved_num_template_parameter_lists
9746 = parser->num_template_parameter_lists;
9747 parser->num_template_parameter_lists = 0;
9748
62b8a44e
NS
9749 /* Parse the parameter-declaration-clause. */
9750 params = cp_parser_parameter_declaration_clause (parser);
9751
4047b164
KL
9752 parser->num_template_parameter_lists
9753 = saved_num_template_parameter_lists;
9754
62b8a44e 9755 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 9756 exception-specification. */
62b8a44e
NS
9757 if (cp_parser_parse_definitely (parser))
9758 {
9759 tree cv_qualifiers;
9760 tree exception_specification;
7efa3e22
NS
9761
9762 if (ctor_dtor_or_conv_p)
9763 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
9764 first = false;
9765 /* Consume the `)'. */
9766 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9767
9768 /* Parse the cv-qualifier-seq. */
9769 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9770 /* And the exception-specification. */
9771 exception_specification
9772 = cp_parser_exception_specification_opt (parser);
9773
9774 /* Create the function-declarator. */
9775 declarator = make_call_declarator (declarator,
9776 params,
9777 cv_qualifiers,
9778 exception_specification);
9779 /* Any subsequent parameter lists are to do with
9780 return type, so are not those of the declared
9781 function. */
9782 parser->default_arg_ok_p = false;
9783
9784 /* Repeat the main loop. */
9785 continue;
9786 }
9787 }
9788
9789 /* If this is the first, we can try a parenthesized
9790 declarator. */
9791 if (first)
a723baf1 9792 {
a723baf1 9793 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
9794 parser->in_declarator_p = saved_in_declarator_p;
9795
9796 /* Consume the `('. */
9797 cp_lexer_consume_token (parser->lexer);
9798 /* Parse the nested declarator. */
9799 declarator
9800 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9801 first = false;
9802 /* Expect a `)'. */
9803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9804 declarator = error_mark_node;
9805 if (declarator == error_mark_node)
9806 break;
9807
9808 goto handle_declarator;
a723baf1 9809 }
9bcb9aae 9810 /* Otherwise, we must be done. */
62b8a44e
NS
9811 else
9812 break;
a723baf1 9813 }
62b8a44e
NS
9814 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9815 && token->type == CPP_OPEN_SQUARE)
a723baf1 9816 {
62b8a44e 9817 /* Parse an array-declarator. */
a723baf1
MM
9818 tree bounds;
9819
7efa3e22
NS
9820 if (ctor_dtor_or_conv_p)
9821 *ctor_dtor_or_conv_p = 0;
9822
62b8a44e
NS
9823 first = false;
9824 parser->default_arg_ok_p = false;
9825 parser->in_declarator_p = true;
a723baf1
MM
9826 /* Consume the `['. */
9827 cp_lexer_consume_token (parser->lexer);
9828 /* Peek at the next token. */
9829 token = cp_lexer_peek_token (parser->lexer);
9830 /* If the next token is `]', then there is no
9831 constant-expression. */
9832 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
9833 {
9834 bool non_constant_p;
9835
9836 bounds
9837 = cp_parser_constant_expression (parser,
9838 /*allow_non_constant=*/true,
9839 &non_constant_p);
d17811fd
MM
9840 if (!non_constant_p)
9841 bounds = cp_parser_fold_non_dependent_expr (bounds);
14d22dd6 9842 }
a723baf1
MM
9843 else
9844 bounds = NULL_TREE;
9845 /* Look for the closing `]'. */
62b8a44e
NS
9846 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9847 {
9848 declarator = error_mark_node;
9849 break;
9850 }
a723baf1
MM
9851
9852 declarator = build_nt (ARRAY_REF, declarator, bounds);
9853 }
62b8a44e 9854 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 9855 {
62b8a44e
NS
9856 /* Parse a declarator_id */
9857 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9858 cp_parser_parse_tentatively (parser);
9859 declarator = cp_parser_declarator_id (parser);
712becab
NS
9860 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9861 {
9862 if (!cp_parser_parse_definitely (parser))
9863 declarator = error_mark_node;
9864 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9865 {
9866 cp_parser_error (parser, "expected unqualified-id");
9867 declarator = error_mark_node;
9868 }
9869 }
9870
62b8a44e
NS
9871 if (declarator == error_mark_node)
9872 break;
a723baf1 9873
62b8a44e
NS
9874 if (TREE_CODE (declarator) == SCOPE_REF)
9875 {
9876 tree scope = TREE_OPERAND (declarator, 0);
712becab 9877
62b8a44e
NS
9878 /* In the declaration of a member of a template class
9879 outside of the class itself, the SCOPE will sometimes
9880 be a TYPENAME_TYPE. For example, given:
9881
9882 template <typename T>
9883 int S<T>::R::i = 3;
9884
9885 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9886 this context, we must resolve S<T>::R to an ordinary
9887 type, rather than a typename type.
9888
9889 The reason we normally avoid resolving TYPENAME_TYPEs
9890 is that a specialization of `S' might render
9891 `S<T>::R' not a type. However, if `S' is
9892 specialized, then this `i' will not be used, so there
9893 is no harm in resolving the types here. */
9894 if (TREE_CODE (scope) == TYPENAME_TYPE)
9895 {
14d22dd6
MM
9896 tree type;
9897
62b8a44e 9898 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
9899 type = resolve_typename_type (scope,
9900 /*only_current_p=*/false);
62b8a44e 9901 /* If that failed, the declarator is invalid. */
14d22dd6
MM
9902 if (type != error_mark_node)
9903 scope = type;
62b8a44e
NS
9904 /* Build a new DECLARATOR. */
9905 declarator = build_nt (SCOPE_REF,
9906 scope,
9907 TREE_OPERAND (declarator, 1));
9908 }
9909 }
9910
9911 /* Check to see whether the declarator-id names a constructor,
9912 destructor, or conversion. */
9913 if (declarator && ctor_dtor_or_conv_p
9914 && ((TREE_CODE (declarator) == SCOPE_REF
9915 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9916 || (TREE_CODE (declarator) != SCOPE_REF
9917 && at_class_scope_p ())))
a723baf1 9918 {
62b8a44e
NS
9919 tree unqualified_name;
9920 tree class_type;
9921
9922 /* Get the unqualified part of the name. */
9923 if (TREE_CODE (declarator) == SCOPE_REF)
9924 {
9925 class_type = TREE_OPERAND (declarator, 0);
9926 unqualified_name = TREE_OPERAND (declarator, 1);
9927 }
9928 else
9929 {
9930 class_type = current_class_type;
9931 unqualified_name = declarator;
9932 }
9933
9934 /* See if it names ctor, dtor or conv. */
9935 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9936 || IDENTIFIER_TYPENAME_P (unqualified_name)
9937 || constructor_name_p (unqualified_name, class_type))
7efa3e22 9938 *ctor_dtor_or_conv_p = -1;
a723baf1 9939 }
62b8a44e
NS
9940
9941 handle_declarator:;
9942 scope = get_scope_of_declarator (declarator);
9943 if (scope)
9944 /* Any names that appear after the declarator-id for a member
9945 are looked up in the containing scope. */
9946 push_scope (scope);
9947 parser->in_declarator_p = true;
9948 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9949 || (declarator
9950 && (TREE_CODE (declarator) == SCOPE_REF
9951 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9952 /* Default args are only allowed on function
9953 declarations. */
9954 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 9955 else
62b8a44e
NS
9956 parser->default_arg_ok_p = false;
9957
9958 first = false;
a723baf1 9959 }
62b8a44e 9960 /* We're done. */
a723baf1
MM
9961 else
9962 break;
a723baf1
MM
9963 }
9964
9965 /* For an abstract declarator, we might wind up with nothing at this
9966 point. That's an error; the declarator is not optional. */
9967 if (!declarator)
9968 cp_parser_error (parser, "expected declarator");
9969
9970 /* If we entered a scope, we must exit it now. */
9971 if (scope)
9972 pop_scope (scope);
9973
9974 parser->default_arg_ok_p = saved_default_arg_ok_p;
9975 parser->in_declarator_p = saved_in_declarator_p;
9976
9977 return declarator;
9978}
9979
9980/* Parse a ptr-operator.
9981
9982 ptr-operator:
9983 * cv-qualifier-seq [opt]
9984 &
9985 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9986
9987 GNU Extension:
9988
9989 ptr-operator:
9990 & cv-qualifier-seq [opt]
9991
9992 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9993 used. Returns ADDR_EXPR if a reference was used. In the
9994 case of a pointer-to-member, *TYPE is filled in with the
9995 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9996 with the cv-qualifier-seq, or NULL_TREE, if there are no
9997 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9998
9999static enum tree_code
94edc4ab
NN
10000cp_parser_ptr_operator (cp_parser* parser,
10001 tree* type,
10002 tree* cv_qualifier_seq)
a723baf1
MM
10003{
10004 enum tree_code code = ERROR_MARK;
10005 cp_token *token;
10006
10007 /* Assume that it's not a pointer-to-member. */
10008 *type = NULL_TREE;
10009 /* And that there are no cv-qualifiers. */
10010 *cv_qualifier_seq = NULL_TREE;
10011
10012 /* Peek at the next token. */
10013 token = cp_lexer_peek_token (parser->lexer);
10014 /* If it's a `*' or `&' we have a pointer or reference. */
10015 if (token->type == CPP_MULT || token->type == CPP_AND)
10016 {
10017 /* Remember which ptr-operator we were processing. */
10018 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10019
10020 /* Consume the `*' or `&'. */
10021 cp_lexer_consume_token (parser->lexer);
10022
10023 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10024 `&', if we are allowing GNU extensions. (The only qualifier
10025 that can legally appear after `&' is `restrict', but that is
10026 enforced during semantic analysis. */
10027 if (code == INDIRECT_REF
10028 || cp_parser_allow_gnu_extensions_p (parser))
10029 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10030 }
10031 else
10032 {
10033 /* Try the pointer-to-member case. */
10034 cp_parser_parse_tentatively (parser);
10035 /* Look for the optional `::' operator. */
10036 cp_parser_global_scope_opt (parser,
10037 /*current_scope_valid_p=*/false);
10038 /* Look for the nested-name specifier. */
10039 cp_parser_nested_name_specifier (parser,
10040 /*typename_keyword_p=*/false,
10041 /*check_dependency_p=*/true,
10042 /*type_p=*/false);
10043 /* If we found it, and the next token is a `*', then we are
10044 indeed looking at a pointer-to-member operator. */
10045 if (!cp_parser_error_occurred (parser)
10046 && cp_parser_require (parser, CPP_MULT, "`*'"))
10047 {
10048 /* The type of which the member is a member is given by the
10049 current SCOPE. */
10050 *type = parser->scope;
10051 /* The next name will not be qualified. */
10052 parser->scope = NULL_TREE;
10053 parser->qualifying_scope = NULL_TREE;
10054 parser->object_scope = NULL_TREE;
10055 /* Indicate that the `*' operator was used. */
10056 code = INDIRECT_REF;
10057 /* Look for the optional cv-qualifier-seq. */
10058 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10059 }
10060 /* If that didn't work we don't have a ptr-operator. */
10061 if (!cp_parser_parse_definitely (parser))
10062 cp_parser_error (parser, "expected ptr-operator");
10063 }
10064
10065 return code;
10066}
10067
10068/* Parse an (optional) cv-qualifier-seq.
10069
10070 cv-qualifier-seq:
10071 cv-qualifier cv-qualifier-seq [opt]
10072
10073 Returns a TREE_LIST. The TREE_VALUE of each node is the
10074 representation of a cv-qualifier. */
10075
10076static tree
94edc4ab 10077cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10078{
10079 tree cv_qualifiers = NULL_TREE;
10080
10081 while (true)
10082 {
10083 tree cv_qualifier;
10084
10085 /* Look for the next cv-qualifier. */
10086 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10087 /* If we didn't find one, we're done. */
10088 if (!cv_qualifier)
10089 break;
10090
10091 /* Add this cv-qualifier to the list. */
10092 cv_qualifiers
10093 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10094 }
10095
10096 /* We built up the list in reverse order. */
10097 return nreverse (cv_qualifiers);
10098}
10099
10100/* Parse an (optional) cv-qualifier.
10101
10102 cv-qualifier:
10103 const
10104 volatile
10105
10106 GNU Extension:
10107
10108 cv-qualifier:
10109 __restrict__ */
10110
10111static tree
94edc4ab 10112cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10113{
10114 cp_token *token;
10115 tree cv_qualifier = NULL_TREE;
10116
10117 /* Peek at the next token. */
10118 token = cp_lexer_peek_token (parser->lexer);
10119 /* See if it's a cv-qualifier. */
10120 switch (token->keyword)
10121 {
10122 case RID_CONST:
10123 case RID_VOLATILE:
10124 case RID_RESTRICT:
10125 /* Save the value of the token. */
10126 cv_qualifier = token->value;
10127 /* Consume the token. */
10128 cp_lexer_consume_token (parser->lexer);
10129 break;
10130
10131 default:
10132 break;
10133 }
10134
10135 return cv_qualifier;
10136}
10137
10138/* Parse a declarator-id.
10139
10140 declarator-id:
10141 id-expression
10142 :: [opt] nested-name-specifier [opt] type-name
10143
10144 In the `id-expression' case, the value returned is as for
10145 cp_parser_id_expression if the id-expression was an unqualified-id.
10146 If the id-expression was a qualified-id, then a SCOPE_REF is
10147 returned. The first operand is the scope (either a NAMESPACE_DECL
10148 or TREE_TYPE), but the second is still just a representation of an
10149 unqualified-id. */
10150
10151static tree
94edc4ab 10152cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10153{
10154 tree id_expression;
10155
10156 /* The expression must be an id-expression. Assume that qualified
10157 names are the names of types so that:
10158
10159 template <class T>
10160 int S<T>::R::i = 3;
10161
10162 will work; we must treat `S<T>::R' as the name of a type.
10163 Similarly, assume that qualified names are templates, where
10164 required, so that:
10165
10166 template <class T>
10167 int S<T>::R<T>::i = 3;
10168
10169 will work, too. */
10170 id_expression = cp_parser_id_expression (parser,
10171 /*template_keyword_p=*/false,
10172 /*check_dependency_p=*/false,
f3c2dfc6
MM
10173 /*template_p=*/NULL,
10174 /*declarator_p=*/true);
a723baf1
MM
10175 /* If the name was qualified, create a SCOPE_REF to represent
10176 that. */
10177 if (parser->scope)
ec20aa6c
MM
10178 {
10179 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10180 parser->scope = NULL_TREE;
10181 }
a723baf1
MM
10182
10183 return id_expression;
10184}
10185
10186/* Parse a type-id.
10187
10188 type-id:
10189 type-specifier-seq abstract-declarator [opt]
10190
10191 Returns the TYPE specified. */
10192
10193static tree
94edc4ab 10194cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10195{
10196 tree type_specifier_seq;
10197 tree abstract_declarator;
10198
10199 /* Parse the type-specifier-seq. */
10200 type_specifier_seq
10201 = cp_parser_type_specifier_seq (parser);
10202 if (type_specifier_seq == error_mark_node)
10203 return error_mark_node;
10204
10205 /* There might or might not be an abstract declarator. */
10206 cp_parser_parse_tentatively (parser);
10207 /* Look for the declarator. */
10208 abstract_declarator
62b8a44e 10209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10210 /* Check to see if there really was a declarator. */
10211 if (!cp_parser_parse_definitely (parser))
10212 abstract_declarator = NULL_TREE;
10213
10214 return groktypename (build_tree_list (type_specifier_seq,
10215 abstract_declarator));
10216}
10217
10218/* Parse a type-specifier-seq.
10219
10220 type-specifier-seq:
10221 type-specifier type-specifier-seq [opt]
10222
10223 GNU extension:
10224
10225 type-specifier-seq:
10226 attributes type-specifier-seq [opt]
10227
10228 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10229 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10230
10231static tree
94edc4ab 10232cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10233{
10234 bool seen_type_specifier = false;
10235 tree type_specifier_seq = NULL_TREE;
10236
10237 /* Parse the type-specifiers and attributes. */
10238 while (true)
10239 {
10240 tree type_specifier;
10241
10242 /* Check for attributes first. */
10243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10244 {
10245 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10246 NULL_TREE,
10247 type_specifier_seq);
10248 continue;
10249 }
10250
10251 /* After the first type-specifier, others are optional. */
10252 if (seen_type_specifier)
10253 cp_parser_parse_tentatively (parser);
10254 /* Look for the type-specifier. */
10255 type_specifier = cp_parser_type_specifier (parser,
10256 CP_PARSER_FLAGS_NONE,
10257 /*is_friend=*/false,
10258 /*is_declaration=*/false,
10259 NULL,
10260 NULL);
10261 /* If the first type-specifier could not be found, this is not a
10262 type-specifier-seq at all. */
10263 if (!seen_type_specifier && type_specifier == error_mark_node)
10264 return error_mark_node;
10265 /* If subsequent type-specifiers could not be found, the
10266 type-specifier-seq is complete. */
10267 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10268 break;
10269
10270 /* Add the new type-specifier to the list. */
10271 type_specifier_seq
10272 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10273 seen_type_specifier = true;
10274 }
10275
10276 /* We built up the list in reverse order. */
10277 return nreverse (type_specifier_seq);
10278}
10279
10280/* Parse a parameter-declaration-clause.
10281
10282 parameter-declaration-clause:
10283 parameter-declaration-list [opt] ... [opt]
10284 parameter-declaration-list , ...
10285
10286 Returns a representation for the parameter declarations. Each node
10287 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10288 representation.) If the parameter-declaration-clause ends with an
10289 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10290 list. A return value of NULL_TREE indicates a
10291 parameter-declaration-clause consisting only of an ellipsis. */
10292
10293static tree
94edc4ab 10294cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10295{
10296 tree parameters;
10297 cp_token *token;
10298 bool ellipsis_p;
10299
10300 /* Peek at the next token. */
10301 token = cp_lexer_peek_token (parser->lexer);
10302 /* Check for trivial parameter-declaration-clauses. */
10303 if (token->type == CPP_ELLIPSIS)
10304 {
10305 /* Consume the `...' token. */
10306 cp_lexer_consume_token (parser->lexer);
10307 return NULL_TREE;
10308 }
10309 else if (token->type == CPP_CLOSE_PAREN)
10310 /* There are no parameters. */
c73aecdf
DE
10311 {
10312#ifndef NO_IMPLICIT_EXTERN_C
10313 if (in_system_header && current_class_type == NULL
10314 && current_lang_name == lang_name_c)
10315 return NULL_TREE;
10316 else
10317#endif
10318 return void_list_node;
10319 }
a723baf1
MM
10320 /* Check for `(void)', too, which is a special case. */
10321 else if (token->keyword == RID_VOID
10322 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10323 == CPP_CLOSE_PAREN))
10324 {
10325 /* Consume the `void' token. */
10326 cp_lexer_consume_token (parser->lexer);
10327 /* There are no parameters. */
10328 return void_list_node;
10329 }
10330
10331 /* Parse the parameter-declaration-list. */
10332 parameters = cp_parser_parameter_declaration_list (parser);
10333 /* If a parse error occurred while parsing the
10334 parameter-declaration-list, then the entire
10335 parameter-declaration-clause is erroneous. */
10336 if (parameters == error_mark_node)
10337 return error_mark_node;
10338
10339 /* Peek at the next token. */
10340 token = cp_lexer_peek_token (parser->lexer);
10341 /* If it's a `,', the clause should terminate with an ellipsis. */
10342 if (token->type == CPP_COMMA)
10343 {
10344 /* Consume the `,'. */
10345 cp_lexer_consume_token (parser->lexer);
10346 /* Expect an ellipsis. */
10347 ellipsis_p
10348 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10349 }
10350 /* It might also be `...' if the optional trailing `,' was
10351 omitted. */
10352 else if (token->type == CPP_ELLIPSIS)
10353 {
10354 /* Consume the `...' token. */
10355 cp_lexer_consume_token (parser->lexer);
10356 /* And remember that we saw it. */
10357 ellipsis_p = true;
10358 }
10359 else
10360 ellipsis_p = false;
10361
10362 /* Finish the parameter list. */
10363 return finish_parmlist (parameters, ellipsis_p);
10364}
10365
10366/* Parse a parameter-declaration-list.
10367
10368 parameter-declaration-list:
10369 parameter-declaration
10370 parameter-declaration-list , parameter-declaration
10371
10372 Returns a representation of the parameter-declaration-list, as for
10373 cp_parser_parameter_declaration_clause. However, the
10374 `void_list_node' is never appended to the list. */
10375
10376static tree
94edc4ab 10377cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10378{
10379 tree parameters = NULL_TREE;
10380
10381 /* Look for more parameters. */
10382 while (true)
10383 {
10384 tree parameter;
10385 /* Parse the parameter. */
10386 parameter
ec194454
MM
10387 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10388
34cd5ae7 10389 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10390 then the entire parameter-declaration-list is erroneous. */
10391 if (parameter == error_mark_node)
10392 {
10393 parameters = error_mark_node;
10394 break;
10395 }
10396 /* Add the new parameter to the list. */
10397 TREE_CHAIN (parameter) = parameters;
10398 parameters = parameter;
10399
10400 /* Peek at the next token. */
10401 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10402 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10403 /* The parameter-declaration-list is complete. */
10404 break;
10405 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10406 {
10407 cp_token *token;
10408
10409 /* Peek at the next token. */
10410 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10411 /* If it's an ellipsis, then the list is complete. */
10412 if (token->type == CPP_ELLIPSIS)
10413 break;
10414 /* Otherwise, there must be more parameters. Consume the
10415 `,'. */
10416 cp_lexer_consume_token (parser->lexer);
10417 }
10418 else
10419 {
10420 cp_parser_error (parser, "expected `,' or `...'");
10421 break;
10422 }
10423 }
10424
10425 /* We built up the list in reverse order; straighten it out now. */
10426 return nreverse (parameters);
10427}
10428
10429/* Parse a parameter declaration.
10430
10431 parameter-declaration:
10432 decl-specifier-seq declarator
10433 decl-specifier-seq declarator = assignment-expression
10434 decl-specifier-seq abstract-declarator [opt]
10435 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10436
ec194454
MM
10437 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10438 declares a template parameter. (In that case, a non-nested `>'
10439 token encountered during the parsing of the assignment-expression
10440 is not interpreted as a greater-than operator.)
a723baf1
MM
10441
10442 Returns a TREE_LIST representing the parameter-declaration. The
10443 TREE_VALUE is a representation of the decl-specifier-seq and
10444 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10445 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10446 TREE_VALUE represents the declarator. */
10447
10448static tree
ec194454
MM
10449cp_parser_parameter_declaration (cp_parser *parser,
10450 bool template_parm_p)
a723baf1 10451{
560ad596 10452 int declares_class_or_enum;
ec194454 10453 bool greater_than_is_operator_p;
a723baf1
MM
10454 tree decl_specifiers;
10455 tree attributes;
10456 tree declarator;
10457 tree default_argument;
10458 tree parameter;
10459 cp_token *token;
10460 const char *saved_message;
10461
ec194454
MM
10462 /* In a template parameter, `>' is not an operator.
10463
10464 [temp.param]
10465
10466 When parsing a default template-argument for a non-type
10467 template-parameter, the first non-nested `>' is taken as the end
10468 of the template parameter-list rather than a greater-than
10469 operator. */
10470 greater_than_is_operator_p = !template_parm_p;
10471
a723baf1
MM
10472 /* Type definitions may not appear in parameter types. */
10473 saved_message = parser->type_definition_forbidden_message;
10474 parser->type_definition_forbidden_message
10475 = "types may not be defined in parameter types";
10476
10477 /* Parse the declaration-specifiers. */
10478 decl_specifiers
10479 = cp_parser_decl_specifier_seq (parser,
10480 CP_PARSER_FLAGS_NONE,
10481 &attributes,
10482 &declares_class_or_enum);
10483 /* If an error occurred, there's no reason to attempt to parse the
10484 rest of the declaration. */
10485 if (cp_parser_error_occurred (parser))
10486 {
10487 parser->type_definition_forbidden_message = saved_message;
10488 return error_mark_node;
10489 }
10490
10491 /* Peek at the next token. */
10492 token = cp_lexer_peek_token (parser->lexer);
10493 /* If the next token is a `)', `,', `=', `>', or `...', then there
10494 is no declarator. */
10495 if (token->type == CPP_CLOSE_PAREN
10496 || token->type == CPP_COMMA
10497 || token->type == CPP_EQ
10498 || token->type == CPP_ELLIPSIS
10499 || token->type == CPP_GREATER)
10500 declarator = NULL_TREE;
10501 /* Otherwise, there should be a declarator. */
10502 else
10503 {
10504 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10505 parser->default_arg_ok_p = false;
10506
a723baf1 10507 declarator = cp_parser_declarator (parser,
62b8a44e 10508 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10509 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10510 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10511 /* After the declarator, allow more attributes. */
10512 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10513 }
10514
62b8a44e 10515 /* The restriction on defining new types applies only to the type
a723baf1
MM
10516 of the parameter, not to the default argument. */
10517 parser->type_definition_forbidden_message = saved_message;
10518
10519 /* If the next token is `=', then process a default argument. */
10520 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10521 {
10522 bool saved_greater_than_is_operator_p;
10523 /* Consume the `='. */
10524 cp_lexer_consume_token (parser->lexer);
10525
10526 /* If we are defining a class, then the tokens that make up the
10527 default argument must be saved and processed later. */
ec194454
MM
10528 if (!template_parm_p && at_class_scope_p ()
10529 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10530 {
10531 unsigned depth = 0;
10532
10533 /* Create a DEFAULT_ARG to represented the unparsed default
10534 argument. */
10535 default_argument = make_node (DEFAULT_ARG);
10536 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10537
10538 /* Add tokens until we have processed the entire default
10539 argument. */
10540 while (true)
10541 {
10542 bool done = false;
10543 cp_token *token;
10544
10545 /* Peek at the next token. */
10546 token = cp_lexer_peek_token (parser->lexer);
10547 /* What we do depends on what token we have. */
10548 switch (token->type)
10549 {
10550 /* In valid code, a default argument must be
10551 immediately followed by a `,' `)', or `...'. */
10552 case CPP_COMMA:
10553 case CPP_CLOSE_PAREN:
10554 case CPP_ELLIPSIS:
10555 /* If we run into a non-nested `;', `}', or `]',
10556 then the code is invalid -- but the default
10557 argument is certainly over. */
10558 case CPP_SEMICOLON:
10559 case CPP_CLOSE_BRACE:
10560 case CPP_CLOSE_SQUARE:
10561 if (depth == 0)
10562 done = true;
10563 /* Update DEPTH, if necessary. */
10564 else if (token->type == CPP_CLOSE_PAREN
10565 || token->type == CPP_CLOSE_BRACE
10566 || token->type == CPP_CLOSE_SQUARE)
10567 --depth;
10568 break;
10569
10570 case CPP_OPEN_PAREN:
10571 case CPP_OPEN_SQUARE:
10572 case CPP_OPEN_BRACE:
10573 ++depth;
10574 break;
10575
10576 case CPP_GREATER:
10577 /* If we see a non-nested `>', and `>' is not an
10578 operator, then it marks the end of the default
10579 argument. */
10580 if (!depth && !greater_than_is_operator_p)
10581 done = true;
10582 break;
10583
10584 /* If we run out of tokens, issue an error message. */
10585 case CPP_EOF:
10586 error ("file ends in default argument");
10587 done = true;
10588 break;
10589
10590 case CPP_NAME:
10591 case CPP_SCOPE:
10592 /* In these cases, we should look for template-ids.
10593 For example, if the default argument is
10594 `X<int, double>()', we need to do name lookup to
10595 figure out whether or not `X' is a template; if
34cd5ae7 10596 so, the `,' does not end the default argument.
a723baf1
MM
10597
10598 That is not yet done. */
10599 break;
10600
10601 default:
10602 break;
10603 }
10604
10605 /* If we've reached the end, stop. */
10606 if (done)
10607 break;
10608
10609 /* Add the token to the token block. */
10610 token = cp_lexer_consume_token (parser->lexer);
10611 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10612 token);
10613 }
10614 }
10615 /* Outside of a class definition, we can just parse the
10616 assignment-expression. */
10617 else
10618 {
10619 bool saved_local_variables_forbidden_p;
10620
10621 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10622 set correctly. */
10623 saved_greater_than_is_operator_p
10624 = parser->greater_than_is_operator_p;
10625 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10626 /* Local variable names (and the `this' keyword) may not
10627 appear in a default argument. */
10628 saved_local_variables_forbidden_p
10629 = parser->local_variables_forbidden_p;
10630 parser->local_variables_forbidden_p = true;
10631 /* Parse the assignment-expression. */
10632 default_argument = cp_parser_assignment_expression (parser);
10633 /* Restore saved state. */
10634 parser->greater_than_is_operator_p
10635 = saved_greater_than_is_operator_p;
10636 parser->local_variables_forbidden_p
10637 = saved_local_variables_forbidden_p;
10638 }
10639 if (!parser->default_arg_ok_p)
10640 {
c67d36d0
NS
10641 if (!flag_pedantic_errors)
10642 warning ("deprecated use of default argument for parameter of non-function");
10643 else
10644 {
10645 error ("default arguments are only permitted for function parameters");
10646 default_argument = NULL_TREE;
10647 }
a723baf1
MM
10648 }
10649 }
10650 else
10651 default_argument = NULL_TREE;
10652
10653 /* Create the representation of the parameter. */
10654 if (attributes)
10655 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10656 parameter = build_tree_list (default_argument,
10657 build_tree_list (decl_specifiers,
10658 declarator));
10659
10660 return parameter;
10661}
10662
10663/* Parse a function-definition.
10664
10665 function-definition:
10666 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10667 function-body
10668 decl-specifier-seq [opt] declarator function-try-block
10669
10670 GNU Extension:
10671
10672 function-definition:
10673 __extension__ function-definition
10674
10675 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10676 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10677 be a `friend'. */
10678
10679static tree
94edc4ab 10680cp_parser_function_definition (cp_parser* parser, bool* friend_p)
a723baf1
MM
10681{
10682 tree decl_specifiers;
10683 tree attributes;
10684 tree declarator;
10685 tree fn;
a723baf1 10686 cp_token *token;
560ad596 10687 int declares_class_or_enum;
a723baf1
MM
10688 bool member_p;
10689 /* The saved value of the PEDANTIC flag. */
10690 int saved_pedantic;
10691
10692 /* Any pending qualification must be cleared by our caller. It is
10693 more robust to force the callers to clear PARSER->SCOPE than to
10694 do it here since if the qualification is in effect here, it might
10695 also end up in effect elsewhere that it is not intended. */
10696 my_friendly_assert (!parser->scope, 20010821);
10697
10698 /* Handle `__extension__'. */
10699 if (cp_parser_extension_opt (parser, &saved_pedantic))
10700 {
10701 /* Parse the function-definition. */
10702 fn = cp_parser_function_definition (parser, friend_p);
10703 /* Restore the PEDANTIC flag. */
10704 pedantic = saved_pedantic;
10705
10706 return fn;
10707 }
10708
10709 /* Check to see if this definition appears in a class-specifier. */
10710 member_p = (at_class_scope_p ()
10711 && TYPE_BEING_DEFINED (current_class_type));
10712 /* Defer access checks in the decl-specifier-seq until we know what
10713 function is being defined. There is no need to do this for the
10714 definition of member functions; we cannot be defining a member
10715 from another class. */
8d241e0b 10716 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
cf22909c 10717
a723baf1
MM
10718 /* Parse the decl-specifier-seq. */
10719 decl_specifiers
10720 = cp_parser_decl_specifier_seq (parser,
10721 CP_PARSER_FLAGS_OPTIONAL,
10722 &attributes,
10723 &declares_class_or_enum);
10724 /* Figure out whether this declaration is a `friend'. */
10725 if (friend_p)
10726 *friend_p = cp_parser_friend_p (decl_specifiers);
10727
10728 /* Parse the declarator. */
62b8a44e 10729 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
10730 /*ctor_dtor_or_conv_p=*/NULL);
10731
10732 /* Gather up any access checks that occurred. */
cf22909c 10733 stop_deferring_access_checks ();
a723baf1
MM
10734
10735 /* If something has already gone wrong, we may as well stop now. */
10736 if (declarator == error_mark_node)
10737 {
10738 /* Skip to the end of the function, or if this wasn't anything
10739 like a function-definition, to a `;' in the hopes of finding
10740 a sensible place from which to continue parsing. */
10741 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10742 pop_deferring_access_checks ();
a723baf1
MM
10743 return error_mark_node;
10744 }
10745
10746 /* The next character should be a `{' (for a simple function
10747 definition), a `:' (for a ctor-initializer), or `try' (for a
10748 function-try block). */
10749 token = cp_lexer_peek_token (parser->lexer);
10750 if (!cp_parser_token_starts_function_definition_p (token))
10751 {
10752 /* Issue the error-message. */
10753 cp_parser_error (parser, "expected function-definition");
10754 /* Skip to the next `;'. */
10755 cp_parser_skip_to_end_of_block_or_statement (parser);
10756
cf22909c 10757 pop_deferring_access_checks ();
a723baf1
MM
10758 return error_mark_node;
10759 }
10760
560ad596
MM
10761 cp_parser_check_for_definition_in_return_type (declarator,
10762 declares_class_or_enum);
10763
a723baf1
MM
10764 /* If we are in a class scope, then we must handle
10765 function-definitions specially. In particular, we save away the
10766 tokens that make up the function body, and parse them again
10767 later, in order to handle code like:
10768
10769 struct S {
10770 int f () { return i; }
10771 int i;
10772 };
10773
10774 Here, we cannot parse the body of `f' until after we have seen
10775 the declaration of `i'. */
10776 if (member_p)
10777 {
10778 cp_token_cache *cache;
10779
10780 /* Create the function-declaration. */
10781 fn = start_method (decl_specifiers, declarator, attributes);
10782 /* If something went badly wrong, bail out now. */
10783 if (fn == error_mark_node)
10784 {
10785 /* If there's a function-body, skip it. */
10786 if (cp_parser_token_starts_function_definition_p
10787 (cp_lexer_peek_token (parser->lexer)))
10788 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10789 pop_deferring_access_checks ();
a723baf1
MM
10790 return error_mark_node;
10791 }
10792
8db1028e
NS
10793 /* Remember it, if there default args to post process. */
10794 cp_parser_save_default_args (parser, fn);
10795
a723baf1
MM
10796 /* Create a token cache. */
10797 cache = cp_token_cache_new ();
10798 /* Save away the tokens that make up the body of the
10799 function. */
10800 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10801 /* Handle function try blocks. */
10802 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10803 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10804
10805 /* Save away the inline definition; we will process it when the
10806 class is complete. */
10807 DECL_PENDING_INLINE_INFO (fn) = cache;
10808 DECL_PENDING_INLINE_P (fn) = 1;
10809
649fc72d
NS
10810 /* We need to know that this was defined in the class, so that
10811 friend templates are handled correctly. */
10812 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10813
a723baf1
MM
10814 /* We're done with the inline definition. */
10815 finish_method (fn);
10816
10817 /* Add FN to the queue of functions to be parsed later. */
10818 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 10819 = tree_cons (NULL_TREE, fn,
a723baf1
MM
10820 TREE_VALUE (parser->unparsed_functions_queues));
10821
cf22909c 10822 pop_deferring_access_checks ();
a723baf1
MM
10823 return fn;
10824 }
10825
10826 /* Check that the number of template-parameter-lists is OK. */
10827 if (!cp_parser_check_declarator_template_parameters (parser,
10828 declarator))
10829 {
10830 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10831 pop_deferring_access_checks ();
a723baf1
MM
10832 return error_mark_node;
10833 }
10834
cf22909c
KL
10835 fn = cp_parser_function_definition_from_specifiers_and_declarator
10836 (parser, decl_specifiers, attributes, declarator);
10837 pop_deferring_access_checks ();
10838 return fn;
a723baf1
MM
10839}
10840
10841/* Parse a function-body.
10842
10843 function-body:
10844 compound_statement */
10845
10846static void
10847cp_parser_function_body (cp_parser *parser)
10848{
a5bcc582 10849 cp_parser_compound_statement (parser, false);
a723baf1
MM
10850}
10851
10852/* Parse a ctor-initializer-opt followed by a function-body. Return
10853 true if a ctor-initializer was present. */
10854
10855static bool
10856cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10857{
10858 tree body;
10859 bool ctor_initializer_p;
10860
10861 /* Begin the function body. */
10862 body = begin_function_body ();
10863 /* Parse the optional ctor-initializer. */
10864 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10865 /* Parse the function-body. */
10866 cp_parser_function_body (parser);
10867 /* Finish the function body. */
10868 finish_function_body (body);
10869
10870 return ctor_initializer_p;
10871}
10872
10873/* Parse an initializer.
10874
10875 initializer:
10876 = initializer-clause
10877 ( expression-list )
10878
10879 Returns a expression representing the initializer. If no
10880 initializer is present, NULL_TREE is returned.
10881
10882 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10883 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
10884 set to FALSE if there is no initializer present. If there is an
10885 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10886 is set to true; otherwise it is set to false. */
a723baf1
MM
10887
10888static tree
39703eb9
MM
10889cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10890 bool* non_constant_p)
a723baf1
MM
10891{
10892 cp_token *token;
10893 tree init;
10894
10895 /* Peek at the next token. */
10896 token = cp_lexer_peek_token (parser->lexer);
10897
10898 /* Let our caller know whether or not this initializer was
10899 parenthesized. */
10900 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
10901 /* Assume that the initializer is constant. */
10902 *non_constant_p = false;
a723baf1
MM
10903
10904 if (token->type == CPP_EQ)
10905 {
10906 /* Consume the `='. */
10907 cp_lexer_consume_token (parser->lexer);
10908 /* Parse the initializer-clause. */
39703eb9 10909 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
10910 }
10911 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
10912 init = cp_parser_parenthesized_expression_list (parser, false,
10913 non_constant_p);
a723baf1
MM
10914 else
10915 {
10916 /* Anything else is an error. */
10917 cp_parser_error (parser, "expected initializer");
10918 init = error_mark_node;
10919 }
10920
10921 return init;
10922}
10923
10924/* Parse an initializer-clause.
10925
10926 initializer-clause:
10927 assignment-expression
10928 { initializer-list , [opt] }
10929 { }
10930
10931 Returns an expression representing the initializer.
10932
10933 If the `assignment-expression' production is used the value
34cd5ae7 10934 returned is simply a representation for the expression.
a723baf1
MM
10935
10936 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10937 the elements of the initializer-list (or NULL_TREE, if the last
10938 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10939 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
10940 trailing `,' was provided. NON_CONSTANT_P is as for
10941 cp_parser_initializer. */
a723baf1
MM
10942
10943static tree
39703eb9 10944cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
10945{
10946 tree initializer;
10947
10948 /* If it is not a `{', then we are looking at an
10949 assignment-expression. */
10950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
10951 initializer
10952 = cp_parser_constant_expression (parser,
10953 /*allow_non_constant_p=*/true,
10954 non_constant_p);
a723baf1
MM
10955 else
10956 {
10957 /* Consume the `{' token. */
10958 cp_lexer_consume_token (parser->lexer);
10959 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10960 initializer = make_node (CONSTRUCTOR);
10961 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10962 necessary, but check_initializer depends upon it, for
10963 now. */
10964 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10965 /* If it's not a `}', then there is a non-trivial initializer. */
10966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10967 {
10968 /* Parse the initializer list. */
10969 CONSTRUCTOR_ELTS (initializer)
39703eb9 10970 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
10971 /* A trailing `,' token is allowed. */
10972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10973 cp_lexer_consume_token (parser->lexer);
10974 }
a723baf1
MM
10975 /* Now, there should be a trailing `}'. */
10976 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10977 }
10978
10979 return initializer;
10980}
10981
10982/* Parse an initializer-list.
10983
10984 initializer-list:
10985 initializer-clause
10986 initializer-list , initializer-clause
10987
10988 GNU Extension:
10989
10990 initializer-list:
10991 identifier : initializer-clause
10992 initializer-list, identifier : initializer-clause
10993
10994 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10995 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
10996 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10997 as for cp_parser_initializer. */
a723baf1
MM
10998
10999static tree
39703eb9 11000cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11001{
11002 tree initializers = NULL_TREE;
11003
39703eb9
MM
11004 /* Assume all of the expressions are constant. */
11005 *non_constant_p = false;
11006
a723baf1
MM
11007 /* Parse the rest of the list. */
11008 while (true)
11009 {
11010 cp_token *token;
11011 tree identifier;
11012 tree initializer;
39703eb9
MM
11013 bool clause_non_constant_p;
11014
a723baf1
MM
11015 /* If the next token is an identifier and the following one is a
11016 colon, we are looking at the GNU designated-initializer
11017 syntax. */
11018 if (cp_parser_allow_gnu_extensions_p (parser)
11019 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11020 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11021 {
11022 /* Consume the identifier. */
11023 identifier = cp_lexer_consume_token (parser->lexer)->value;
11024 /* Consume the `:'. */
11025 cp_lexer_consume_token (parser->lexer);
11026 }
11027 else
11028 identifier = NULL_TREE;
11029
11030 /* Parse the initializer. */
39703eb9
MM
11031 initializer = cp_parser_initializer_clause (parser,
11032 &clause_non_constant_p);
11033 /* If any clause is non-constant, so is the entire initializer. */
11034 if (clause_non_constant_p)
11035 *non_constant_p = true;
a723baf1
MM
11036 /* Add it to the list. */
11037 initializers = tree_cons (identifier, initializer, initializers);
11038
11039 /* If the next token is not a comma, we have reached the end of
11040 the list. */
11041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11042 break;
11043
11044 /* Peek at the next token. */
11045 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11046 /* If the next token is a `}', then we're still done. An
11047 initializer-clause can have a trailing `,' after the
11048 initializer-list and before the closing `}'. */
11049 if (token->type == CPP_CLOSE_BRACE)
11050 break;
11051
11052 /* Consume the `,' token. */
11053 cp_lexer_consume_token (parser->lexer);
11054 }
11055
11056 /* The initializers were built up in reverse order, so we need to
11057 reverse them now. */
11058 return nreverse (initializers);
11059}
11060
11061/* Classes [gram.class] */
11062
11063/* Parse a class-name.
11064
11065 class-name:
11066 identifier
11067 template-id
11068
11069 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11070 to indicate that names looked up in dependent types should be
11071 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11072 keyword has been used to indicate that the name that appears next
11073 is a template. TYPE_P is true iff the next name should be treated
11074 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11075 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11076 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11077 being defined in a class-head.
a723baf1
MM
11078
11079 Returns the TYPE_DECL representing the class. */
11080
11081static tree
11082cp_parser_class_name (cp_parser *parser,
11083 bool typename_keyword_p,
11084 bool template_keyword_p,
11085 bool type_p,
a723baf1
MM
11086 bool check_dependency_p,
11087 bool class_head_p)
11088{
11089 tree decl;
11090 tree scope;
11091 bool typename_p;
e5976695
MM
11092 cp_token *token;
11093
11094 /* All class-names start with an identifier. */
11095 token = cp_lexer_peek_token (parser->lexer);
11096 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11097 {
11098 cp_parser_error (parser, "expected class-name");
11099 return error_mark_node;
11100 }
11101
a723baf1
MM
11102 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11103 to a template-id, so we save it here. */
11104 scope = parser->scope;
3adee96c
KL
11105 if (scope == error_mark_node)
11106 return error_mark_node;
11107
a723baf1
MM
11108 /* Any name names a type if we're following the `typename' keyword
11109 in a qualified name where the enclosing scope is type-dependent. */
11110 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11111 && dependent_type_p (scope));
e5976695
MM
11112 /* Handle the common case (an identifier, but not a template-id)
11113 efficiently. */
11114 if (token->type == CPP_NAME
11115 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11116 {
a723baf1
MM
11117 tree identifier;
11118
11119 /* Look for the identifier. */
11120 identifier = cp_parser_identifier (parser);
11121 /* If the next token isn't an identifier, we are certainly not
11122 looking at a class-name. */
11123 if (identifier == error_mark_node)
11124 decl = error_mark_node;
11125 /* If we know this is a type-name, there's no need to look it
11126 up. */
11127 else if (typename_p)
11128 decl = identifier;
11129 else
11130 {
11131 /* If the next token is a `::', then the name must be a type
11132 name.
11133
11134 [basic.lookup.qual]
11135
11136 During the lookup for a name preceding the :: scope
11137 resolution operator, object, function, and enumerator
11138 names are ignored. */
11139 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11140 type_p = true;
11141 /* Look up the name. */
11142 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11143 type_p,
eea9800f 11144 /*is_namespace=*/false,
a723baf1
MM
11145 check_dependency_p);
11146 }
11147 }
e5976695
MM
11148 else
11149 {
11150 /* Try a template-id. */
11151 decl = cp_parser_template_id (parser, template_keyword_p,
11152 check_dependency_p);
11153 if (decl == error_mark_node)
11154 return error_mark_node;
11155 }
a723baf1
MM
11156
11157 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11158
11159 /* If this is a typename, create a TYPENAME_TYPE. */
11160 if (typename_p && decl != error_mark_node)
11161 decl = TYPE_NAME (make_typename_type (scope, decl,
11162 /*complain=*/1));
11163
11164 /* Check to see that it is really the name of a class. */
11165 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11166 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11167 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11168 /* Situations like this:
11169
11170 template <typename T> struct A {
11171 typename T::template X<int>::I i;
11172 };
11173
11174 are problematic. Is `T::template X<int>' a class-name? The
11175 standard does not seem to be definitive, but there is no other
11176 valid interpretation of the following `::'. Therefore, those
11177 names are considered class-names. */
78757caa 11178 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11179 else if (decl == error_mark_node
11180 || TREE_CODE (decl) != TYPE_DECL
11181 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11182 {
11183 cp_parser_error (parser, "expected class-name");
11184 return error_mark_node;
11185 }
11186
11187 return decl;
11188}
11189
11190/* Parse a class-specifier.
11191
11192 class-specifier:
11193 class-head { member-specification [opt] }
11194
11195 Returns the TREE_TYPE representing the class. */
11196
11197static tree
94edc4ab 11198cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11199{
11200 cp_token *token;
11201 tree type;
11202 tree attributes = NULL_TREE;
11203 int has_trailing_semicolon;
11204 bool nested_name_specifier_p;
a723baf1
MM
11205 unsigned saved_num_template_parameter_lists;
11206
8d241e0b 11207 push_deferring_access_checks (dk_no_deferred);
cf22909c 11208
a723baf1
MM
11209 /* Parse the class-head. */
11210 type = cp_parser_class_head (parser,
cf22909c 11211 &nested_name_specifier_p);
a723baf1
MM
11212 /* If the class-head was a semantic disaster, skip the entire body
11213 of the class. */
11214 if (!type)
11215 {
11216 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11217 pop_deferring_access_checks ();
a723baf1
MM
11218 return error_mark_node;
11219 }
cf22909c 11220
a723baf1
MM
11221 /* Look for the `{'. */
11222 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11223 {
11224 pop_deferring_access_checks ();
11225 return error_mark_node;
11226 }
11227
a723baf1
MM
11228 /* Issue an error message if type-definitions are forbidden here. */
11229 cp_parser_check_type_definition (parser);
11230 /* Remember that we are defining one more class. */
11231 ++parser->num_classes_being_defined;
11232 /* Inside the class, surrounding template-parameter-lists do not
11233 apply. */
11234 saved_num_template_parameter_lists
11235 = parser->num_template_parameter_lists;
11236 parser->num_template_parameter_lists = 0;
78757caa 11237
a723baf1
MM
11238 /* Start the class. */
11239 type = begin_class_definition (type);
11240 if (type == error_mark_node)
9bcb9aae 11241 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11242 cp_parser_skip_to_closing_brace (parser);
11243 else
11244 /* Parse the member-specification. */
11245 cp_parser_member_specification_opt (parser);
11246 /* Look for the trailing `}'. */
11247 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11248 /* We get better error messages by noticing a common problem: a
11249 missing trailing `;'. */
11250 token = cp_lexer_peek_token (parser->lexer);
11251 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11252 /* Look for attributes to apply to this class. */
11253 if (cp_parser_allow_gnu_extensions_p (parser))
11254 attributes = cp_parser_attributes_opt (parser);
560ad596
MM
11255 /* If we got any attributes in class_head, xref_tag will stick them in
11256 TREE_TYPE of the type. Grab them now. */
11257 if (type != error_mark_node)
11258 {
11259 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11260 TYPE_ATTRIBUTES (type) = NULL_TREE;
11261 type = finish_struct (type, attributes);
11262 }
11263 if (nested_name_specifier_p)
11264 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11265 /* If this class is not itself within the scope of another class,
11266 then we need to parse the bodies of all of the queued function
11267 definitions. Note that the queued functions defined in a class
11268 are not always processed immediately following the
11269 class-specifier for that class. Consider:
11270
11271 struct A {
11272 struct B { void f() { sizeof (A); } };
11273 };
11274
11275 If `f' were processed before the processing of `A' were
11276 completed, there would be no way to compute the size of `A'.
11277 Note that the nesting we are interested in here is lexical --
11278 not the semantic nesting given by TYPE_CONTEXT. In particular,
11279 for:
11280
11281 struct A { struct B; };
11282 struct A::B { void f() { } };
11283
11284 there is no need to delay the parsing of `A::B::f'. */
11285 if (--parser->num_classes_being_defined == 0)
11286 {
8218bd34
MM
11287 tree queue_entry;
11288 tree fn;
a723baf1 11289
8218bd34
MM
11290 /* In a first pass, parse default arguments to the functions.
11291 Then, in a second pass, parse the bodies of the functions.
11292 This two-phased approach handles cases like:
11293
11294 struct S {
11295 void f() { g(); }
11296 void g(int i = 3);
11297 };
11298
11299 */
8db1028e
NS
11300 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11301 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11302 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11303 TREE_PURPOSE (parser->unparsed_functions_queues)
11304 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11305 {
11306 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11307 /* Make sure that any template parameters are in scope. */
11308 maybe_begin_member_template_processing (fn);
11309 /* If there are default arguments that have not yet been processed,
11310 take care of them now. */
11311 cp_parser_late_parsing_default_args (parser, fn);
11312 /* Remove any template parameters from the symbol table. */
11313 maybe_end_member_template_processing ();
11314 }
11315 /* Now parse the body of the functions. */
8db1028e
NS
11316 for (TREE_VALUE (parser->unparsed_functions_queues)
11317 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11318 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11319 TREE_VALUE (parser->unparsed_functions_queues)
11320 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11321 {
a723baf1 11322 /* Figure out which function we need to process. */
a723baf1
MM
11323 fn = TREE_VALUE (queue_entry);
11324
11325 /* Parse the function. */
11326 cp_parser_late_parsing_for_member (parser, fn);
a723baf1
MM
11327 }
11328
a723baf1
MM
11329 }
11330
11331 /* Put back any saved access checks. */
cf22909c 11332 pop_deferring_access_checks ();
a723baf1
MM
11333
11334 /* Restore the count of active template-parameter-lists. */
11335 parser->num_template_parameter_lists
11336 = saved_num_template_parameter_lists;
11337
11338 return type;
11339}
11340
11341/* Parse a class-head.
11342
11343 class-head:
11344 class-key identifier [opt] base-clause [opt]
11345 class-key nested-name-specifier identifier base-clause [opt]
11346 class-key nested-name-specifier [opt] template-id
11347 base-clause [opt]
11348
11349 GNU Extensions:
11350 class-key attributes identifier [opt] base-clause [opt]
11351 class-key attributes nested-name-specifier identifier base-clause [opt]
11352 class-key attributes nested-name-specifier [opt] template-id
11353 base-clause [opt]
11354
11355 Returns the TYPE of the indicated class. Sets
11356 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11357 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11358
11359 Returns NULL_TREE if the class-head is syntactically valid, but
11360 semantically invalid in a way that means we should skip the entire
11361 body of the class. */
11362
11363static tree
94edc4ab
NN
11364cp_parser_class_head (cp_parser* parser,
11365 bool* nested_name_specifier_p)
a723baf1
MM
11366{
11367 cp_token *token;
11368 tree nested_name_specifier;
11369 enum tag_types class_key;
11370 tree id = NULL_TREE;
11371 tree type = NULL_TREE;
11372 tree attributes;
11373 bool template_id_p = false;
11374 bool qualified_p = false;
11375 bool invalid_nested_name_p = false;
11376 unsigned num_templates;
11377
11378 /* Assume no nested-name-specifier will be present. */
11379 *nested_name_specifier_p = false;
11380 /* Assume no template parameter lists will be used in defining the
11381 type. */
11382 num_templates = 0;
11383
11384 /* Look for the class-key. */
11385 class_key = cp_parser_class_key (parser);
11386 if (class_key == none_type)
11387 return error_mark_node;
11388
11389 /* Parse the attributes. */
11390 attributes = cp_parser_attributes_opt (parser);
11391
11392 /* If the next token is `::', that is invalid -- but sometimes
11393 people do try to write:
11394
11395 struct ::S {};
11396
11397 Handle this gracefully by accepting the extra qualifier, and then
11398 issuing an error about it later if this really is a
2050a1bb 11399 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11400 specifier, remain silent. */
11401 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11402 qualified_p = true;
11403
8d241e0b
KL
11404 push_deferring_access_checks (dk_no_check);
11405
a723baf1
MM
11406 /* Determine the name of the class. Begin by looking for an
11407 optional nested-name-specifier. */
11408 nested_name_specifier
11409 = cp_parser_nested_name_specifier_opt (parser,
11410 /*typename_keyword_p=*/false,
66d418e6 11411 /*check_dependency_p=*/false,
a723baf1
MM
11412 /*type_p=*/false);
11413 /* If there was a nested-name-specifier, then there *must* be an
11414 identifier. */
11415 if (nested_name_specifier)
11416 {
11417 /* Although the grammar says `identifier', it really means
11418 `class-name' or `template-name'. You are only allowed to
11419 define a class that has already been declared with this
11420 syntax.
11421
11422 The proposed resolution for Core Issue 180 says that whever
11423 you see `class T::X' you should treat `X' as a type-name.
11424
11425 It is OK to define an inaccessible class; for example:
11426
11427 class A { class B; };
11428 class A::B {};
11429
a723baf1
MM
11430 We do not know if we will see a class-name, or a
11431 template-name. We look for a class-name first, in case the
11432 class-name is a template-id; if we looked for the
11433 template-name first we would stop after the template-name. */
11434 cp_parser_parse_tentatively (parser);
11435 type = cp_parser_class_name (parser,
11436 /*typename_keyword_p=*/false,
11437 /*template_keyword_p=*/false,
11438 /*type_p=*/true,
a723baf1
MM
11439 /*check_dependency_p=*/false,
11440 /*class_head_p=*/true);
11441 /* If that didn't work, ignore the nested-name-specifier. */
11442 if (!cp_parser_parse_definitely (parser))
11443 {
11444 invalid_nested_name_p = true;
11445 id = cp_parser_identifier (parser);
11446 if (id == error_mark_node)
11447 id = NULL_TREE;
11448 }
11449 /* If we could not find a corresponding TYPE, treat this
11450 declaration like an unqualified declaration. */
11451 if (type == error_mark_node)
11452 nested_name_specifier = NULL_TREE;
11453 /* Otherwise, count the number of templates used in TYPE and its
11454 containing scopes. */
11455 else
11456 {
11457 tree scope;
11458
11459 for (scope = TREE_TYPE (type);
11460 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11461 scope = (TYPE_P (scope)
11462 ? TYPE_CONTEXT (scope)
11463 : DECL_CONTEXT (scope)))
11464 if (TYPE_P (scope)
11465 && CLASS_TYPE_P (scope)
11466 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11467 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11468 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11469 ++num_templates;
11470 }
11471 }
11472 /* Otherwise, the identifier is optional. */
11473 else
11474 {
11475 /* We don't know whether what comes next is a template-id,
11476 an identifier, or nothing at all. */
11477 cp_parser_parse_tentatively (parser);
11478 /* Check for a template-id. */
11479 id = cp_parser_template_id (parser,
11480 /*template_keyword_p=*/false,
11481 /*check_dependency_p=*/true);
11482 /* If that didn't work, it could still be an identifier. */
11483 if (!cp_parser_parse_definitely (parser))
11484 {
11485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11486 id = cp_parser_identifier (parser);
11487 else
11488 id = NULL_TREE;
11489 }
11490 else
11491 {
11492 template_id_p = true;
11493 ++num_templates;
11494 }
11495 }
11496
8d241e0b
KL
11497 pop_deferring_access_checks ();
11498
a723baf1
MM
11499 /* If it's not a `:' or a `{' then we can't really be looking at a
11500 class-head, since a class-head only appears as part of a
11501 class-specifier. We have to detect this situation before calling
11502 xref_tag, since that has irreversible side-effects. */
11503 if (!cp_parser_next_token_starts_class_definition_p (parser))
11504 {
11505 cp_parser_error (parser, "expected `{' or `:'");
11506 return error_mark_node;
11507 }
11508
11509 /* At this point, we're going ahead with the class-specifier, even
11510 if some other problem occurs. */
11511 cp_parser_commit_to_tentative_parse (parser);
11512 /* Issue the error about the overly-qualified name now. */
11513 if (qualified_p)
11514 cp_parser_error (parser,
11515 "global qualification of class name is invalid");
11516 else if (invalid_nested_name_p)
11517 cp_parser_error (parser,
11518 "qualified name does not name a class");
11519 /* Make sure that the right number of template parameters were
11520 present. */
11521 if (!cp_parser_check_template_parameters (parser, num_templates))
11522 /* If something went wrong, there is no point in even trying to
11523 process the class-definition. */
11524 return NULL_TREE;
11525
a723baf1
MM
11526 /* Look up the type. */
11527 if (template_id_p)
11528 {
11529 type = TREE_TYPE (id);
11530 maybe_process_partial_specialization (type);
11531 }
11532 else if (!nested_name_specifier)
11533 {
11534 /* If the class was unnamed, create a dummy name. */
11535 if (!id)
11536 id = make_anon_name ();
cbd63935
KL
11537 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11538 parser->num_template_parameter_lists);
a723baf1
MM
11539 }
11540 else
11541 {
a723baf1 11542 tree class_type;
089d6ea7 11543 tree scope;
a723baf1
MM
11544
11545 /* Given:
11546
11547 template <typename T> struct S { struct T };
14d22dd6 11548 template <typename T> struct S<T>::T { };
a723baf1
MM
11549
11550 we will get a TYPENAME_TYPE when processing the definition of
11551 `S::T'. We need to resolve it to the actual type before we
11552 try to define it. */
11553 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11554 {
14d22dd6
MM
11555 class_type = resolve_typename_type (TREE_TYPE (type),
11556 /*only_current_p=*/false);
11557 if (class_type != error_mark_node)
11558 type = TYPE_NAME (class_type);
11559 else
11560 {
11561 cp_parser_error (parser, "could not resolve typename type");
11562 type = error_mark_node;
11563 }
a723baf1
MM
11564 }
11565
089d6ea7
MM
11566 /* Figure out in what scope the declaration is being placed. */
11567 scope = current_scope ();
11568 if (!scope)
11569 scope = current_namespace;
11570 /* If that scope does not contain the scope in which the
11571 class was originally declared, the program is invalid. */
11572 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11573 {
0e136342 11574 error ("declaration of `%D' in `%D' which does not "
089d6ea7
MM
11575 "enclose `%D'", type, scope, nested_name_specifier);
11576 return NULL_TREE;
11577 }
560ad596 11578 /* [dcl.meaning]
089d6ea7 11579
560ad596
MM
11580 A declarator-id shall not be qualified exception of the
11581 definition of a ... nested class outside of its class
11582 ... [or] a the definition or explicit instantiation of a
11583 class member of a namespace outside of its namespace. */
11584 if (scope == CP_DECL_CONTEXT (type))
a723baf1 11585 {
560ad596
MM
11586 pedwarn ("extra qualification ignored");
11587 nested_name_specifier = NULL_TREE;
a723baf1 11588 }
560ad596
MM
11589
11590 maybe_process_partial_specialization (TREE_TYPE (type));
11591 class_type = current_class_type;
11592 /* Enter the scope indicated by the nested-name-specifier. */
11593 if (nested_name_specifier)
11594 push_scope (nested_name_specifier);
11595 /* Get the canonical version of this type. */
11596 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11597 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11598 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11599 type = push_template_decl (type);
11600 type = TREE_TYPE (type);
11601 if (nested_name_specifier)
11602 *nested_name_specifier_p = true;
a723baf1
MM
11603 }
11604 /* Indicate whether this class was declared as a `class' or as a
11605 `struct'. */
11606 if (TREE_CODE (type) == RECORD_TYPE)
11607 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11608 cp_parser_check_class_key (class_key, type);
11609
11610 /* Enter the scope containing the class; the names of base classes
11611 should be looked up in that context. For example, given:
11612
11613 struct A { struct B {}; struct C; };
11614 struct A::C : B {};
11615
11616 is valid. */
11617 if (nested_name_specifier)
11618 push_scope (nested_name_specifier);
11619 /* Now, look for the base-clause. */
11620 token = cp_lexer_peek_token (parser->lexer);
11621 if (token->type == CPP_COLON)
11622 {
11623 tree bases;
11624
11625 /* Get the list of base-classes. */
11626 bases = cp_parser_base_clause (parser);
11627 /* Process them. */
11628 xref_basetypes (type, bases);
11629 }
11630 /* Leave the scope given by the nested-name-specifier. We will
11631 enter the class scope itself while processing the members. */
11632 if (nested_name_specifier)
11633 pop_scope (nested_name_specifier);
11634
11635 return type;
11636}
11637
11638/* Parse a class-key.
11639
11640 class-key:
11641 class
11642 struct
11643 union
11644
11645 Returns the kind of class-key specified, or none_type to indicate
11646 error. */
11647
11648static enum tag_types
94edc4ab 11649cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11650{
11651 cp_token *token;
11652 enum tag_types tag_type;
11653
11654 /* Look for the class-key. */
11655 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11656 if (!token)
11657 return none_type;
11658
11659 /* Check to see if the TOKEN is a class-key. */
11660 tag_type = cp_parser_token_is_class_key (token);
11661 if (!tag_type)
11662 cp_parser_error (parser, "expected class-key");
11663 return tag_type;
11664}
11665
11666/* Parse an (optional) member-specification.
11667
11668 member-specification:
11669 member-declaration member-specification [opt]
11670 access-specifier : member-specification [opt] */
11671
11672static void
94edc4ab 11673cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11674{
11675 while (true)
11676 {
11677 cp_token *token;
11678 enum rid keyword;
11679
11680 /* Peek at the next token. */
11681 token = cp_lexer_peek_token (parser->lexer);
11682 /* If it's a `}', or EOF then we've seen all the members. */
11683 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11684 break;
11685
11686 /* See if this token is a keyword. */
11687 keyword = token->keyword;
11688 switch (keyword)
11689 {
11690 case RID_PUBLIC:
11691 case RID_PROTECTED:
11692 case RID_PRIVATE:
11693 /* Consume the access-specifier. */
11694 cp_lexer_consume_token (parser->lexer);
11695 /* Remember which access-specifier is active. */
11696 current_access_specifier = token->value;
11697 /* Look for the `:'. */
11698 cp_parser_require (parser, CPP_COLON, "`:'");
11699 break;
11700
11701 default:
11702 /* Otherwise, the next construction must be a
11703 member-declaration. */
11704 cp_parser_member_declaration (parser);
a723baf1
MM
11705 }
11706 }
11707}
11708
11709/* Parse a member-declaration.
11710
11711 member-declaration:
11712 decl-specifier-seq [opt] member-declarator-list [opt] ;
11713 function-definition ; [opt]
11714 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11715 using-declaration
11716 template-declaration
11717
11718 member-declarator-list:
11719 member-declarator
11720 member-declarator-list , member-declarator
11721
11722 member-declarator:
11723 declarator pure-specifier [opt]
11724 declarator constant-initializer [opt]
11725 identifier [opt] : constant-expression
11726
11727 GNU Extensions:
11728
11729 member-declaration:
11730 __extension__ member-declaration
11731
11732 member-declarator:
11733 declarator attributes [opt] pure-specifier [opt]
11734 declarator attributes [opt] constant-initializer [opt]
11735 identifier [opt] attributes [opt] : constant-expression */
11736
11737static void
94edc4ab 11738cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
11739{
11740 tree decl_specifiers;
11741 tree prefix_attributes;
11742 tree decl;
560ad596 11743 int declares_class_or_enum;
a723baf1
MM
11744 bool friend_p;
11745 cp_token *token;
11746 int saved_pedantic;
11747
11748 /* Check for the `__extension__' keyword. */
11749 if (cp_parser_extension_opt (parser, &saved_pedantic))
11750 {
11751 /* Recurse. */
11752 cp_parser_member_declaration (parser);
11753 /* Restore the old value of the PEDANTIC flag. */
11754 pedantic = saved_pedantic;
11755
11756 return;
11757 }
11758
11759 /* Check for a template-declaration. */
11760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11761 {
11762 /* Parse the template-declaration. */
11763 cp_parser_template_declaration (parser, /*member_p=*/true);
11764
11765 return;
11766 }
11767
11768 /* Check for a using-declaration. */
11769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11770 {
11771 /* Parse the using-declaration. */
11772 cp_parser_using_declaration (parser);
11773
11774 return;
11775 }
11776
11777 /* We can't tell whether we're looking at a declaration or a
11778 function-definition. */
11779 cp_parser_parse_tentatively (parser);
11780
11781 /* Parse the decl-specifier-seq. */
11782 decl_specifiers
11783 = cp_parser_decl_specifier_seq (parser,
11784 CP_PARSER_FLAGS_OPTIONAL,
11785 &prefix_attributes,
11786 &declares_class_or_enum);
8fbc5ae7
MM
11787 /* Check for an invalid type-name. */
11788 if (cp_parser_diagnose_invalid_type_name (parser))
11789 return;
a723baf1
MM
11790 /* If there is no declarator, then the decl-specifier-seq should
11791 specify a type. */
11792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11793 {
11794 /* If there was no decl-specifier-seq, and the next token is a
11795 `;', then we have something like:
11796
11797 struct S { ; };
11798
11799 [class.mem]
11800
11801 Each member-declaration shall declare at least one member
11802 name of the class. */
11803 if (!decl_specifiers)
11804 {
11805 if (pedantic)
11806 pedwarn ("extra semicolon");
11807 }
11808 else
11809 {
11810 tree type;
11811
11812 /* See if this declaration is a friend. */
11813 friend_p = cp_parser_friend_p (decl_specifiers);
11814 /* If there were decl-specifiers, check to see if there was
11815 a class-declaration. */
11816 type = check_tag_decl (decl_specifiers);
11817 /* Nested classes have already been added to the class, but
11818 a `friend' needs to be explicitly registered. */
11819 if (friend_p)
11820 {
11821 /* If the `friend' keyword was present, the friend must
11822 be introduced with a class-key. */
11823 if (!declares_class_or_enum)
11824 error ("a class-key must be used when declaring a friend");
11825 /* In this case:
11826
11827 template <typename T> struct A {
11828 friend struct A<T>::B;
11829 };
11830
11831 A<T>::B will be represented by a TYPENAME_TYPE, and
11832 therefore not recognized by check_tag_decl. */
11833 if (!type)
11834 {
11835 tree specifier;
11836
11837 for (specifier = decl_specifiers;
11838 specifier;
11839 specifier = TREE_CHAIN (specifier))
11840 {
11841 tree s = TREE_VALUE (specifier);
11842
c003e212
GDR
11843 if (TREE_CODE (s) == IDENTIFIER_NODE)
11844 get_global_value_if_present (s, &type);
a723baf1
MM
11845 if (TREE_CODE (s) == TYPE_DECL)
11846 s = TREE_TYPE (s);
11847 if (TYPE_P (s))
11848 {
11849 type = s;
11850 break;
11851 }
11852 }
11853 }
11854 if (!type)
11855 error ("friend declaration does not name a class or "
11856 "function");
11857 else
19db77ce
KL
11858 make_friend_class (current_class_type, type,
11859 /*complain=*/true);
a723baf1
MM
11860 }
11861 /* If there is no TYPE, an error message will already have
11862 been issued. */
11863 else if (!type)
11864 ;
11865 /* An anonymous aggregate has to be handled specially; such
11866 a declaration really declares a data member (with a
11867 particular type), as opposed to a nested class. */
11868 else if (ANON_AGGR_TYPE_P (type))
11869 {
11870 /* Remove constructors and such from TYPE, now that we
34cd5ae7 11871 know it is an anonymous aggregate. */
a723baf1
MM
11872 fixup_anonymous_aggr (type);
11873 /* And make the corresponding data member. */
11874 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11875 /* Add it to the class. */
11876 finish_member_declaration (decl);
11877 }
37d407a1
KL
11878 else
11879 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
11880 }
11881 }
11882 else
11883 {
11884 /* See if these declarations will be friends. */
11885 friend_p = cp_parser_friend_p (decl_specifiers);
11886
11887 /* Keep going until we hit the `;' at the end of the
11888 declaration. */
11889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11890 {
11891 tree attributes = NULL_TREE;
11892 tree first_attribute;
11893
11894 /* Peek at the next token. */
11895 token = cp_lexer_peek_token (parser->lexer);
11896
11897 /* Check for a bitfield declaration. */
11898 if (token->type == CPP_COLON
11899 || (token->type == CPP_NAME
11900 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11901 == CPP_COLON))
11902 {
11903 tree identifier;
11904 tree width;
11905
11906 /* Get the name of the bitfield. Note that we cannot just
11907 check TOKEN here because it may have been invalidated by
11908 the call to cp_lexer_peek_nth_token above. */
11909 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11910 identifier = cp_parser_identifier (parser);
11911 else
11912 identifier = NULL_TREE;
11913
11914 /* Consume the `:' token. */
11915 cp_lexer_consume_token (parser->lexer);
11916 /* Get the width of the bitfield. */
14d22dd6
MM
11917 width
11918 = cp_parser_constant_expression (parser,
11919 /*allow_non_constant=*/false,
11920 NULL);
a723baf1
MM
11921
11922 /* Look for attributes that apply to the bitfield. */
11923 attributes = cp_parser_attributes_opt (parser);
11924 /* Remember which attributes are prefix attributes and
11925 which are not. */
11926 first_attribute = attributes;
11927 /* Combine the attributes. */
11928 attributes = chainon (prefix_attributes, attributes);
11929
11930 /* Create the bitfield declaration. */
11931 decl = grokbitfield (identifier,
11932 decl_specifiers,
11933 width);
11934 /* Apply the attributes. */
11935 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11936 }
11937 else
11938 {
11939 tree declarator;
11940 tree initializer;
11941 tree asm_specification;
7efa3e22 11942 int ctor_dtor_or_conv_p;
a723baf1
MM
11943
11944 /* Parse the declarator. */
11945 declarator
62b8a44e 11946 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
11947 &ctor_dtor_or_conv_p);
11948
11949 /* If something went wrong parsing the declarator, make sure
11950 that we at least consume some tokens. */
11951 if (declarator == error_mark_node)
11952 {
11953 /* Skip to the end of the statement. */
11954 cp_parser_skip_to_end_of_statement (parser);
11955 break;
11956 }
11957
560ad596
MM
11958 cp_parser_check_for_definition_in_return_type
11959 (declarator, declares_class_or_enum);
11960
a723baf1
MM
11961 /* Look for an asm-specification. */
11962 asm_specification = cp_parser_asm_specification_opt (parser);
11963 /* Look for attributes that apply to the declaration. */
11964 attributes = cp_parser_attributes_opt (parser);
11965 /* Remember which attributes are prefix attributes and
11966 which are not. */
11967 first_attribute = attributes;
11968 /* Combine the attributes. */
11969 attributes = chainon (prefix_attributes, attributes);
11970
11971 /* If it's an `=', then we have a constant-initializer or a
11972 pure-specifier. It is not correct to parse the
11973 initializer before registering the member declaration
11974 since the member declaration should be in scope while
11975 its initializer is processed. However, the rest of the
11976 front end does not yet provide an interface that allows
11977 us to handle this correctly. */
11978 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11979 {
11980 /* In [class.mem]:
11981
11982 A pure-specifier shall be used only in the declaration of
11983 a virtual function.
11984
11985 A member-declarator can contain a constant-initializer
11986 only if it declares a static member of integral or
11987 enumeration type.
11988
11989 Therefore, if the DECLARATOR is for a function, we look
11990 for a pure-specifier; otherwise, we look for a
11991 constant-initializer. When we call `grokfield', it will
11992 perform more stringent semantics checks. */
11993 if (TREE_CODE (declarator) == CALL_EXPR)
11994 initializer = cp_parser_pure_specifier (parser);
11995 else
11996 {
11997 /* This declaration cannot be a function
11998 definition. */
11999 cp_parser_commit_to_tentative_parse (parser);
12000 /* Parse the initializer. */
12001 initializer = cp_parser_constant_initializer (parser);
12002 }
12003 }
12004 /* Otherwise, there is no initializer. */
12005 else
12006 initializer = NULL_TREE;
12007
12008 /* See if we are probably looking at a function
12009 definition. We are certainly not looking at at a
12010 member-declarator. Calling `grokfield' has
12011 side-effects, so we must not do it unless we are sure
12012 that we are looking at a member-declarator. */
12013 if (cp_parser_token_starts_function_definition_p
12014 (cp_lexer_peek_token (parser->lexer)))
12015 decl = error_mark_node;
12016 else
39703eb9
MM
12017 {
12018 /* Create the declaration. */
ee3071ef
NS
12019 decl = grokfield (declarator, decl_specifiers,
12020 initializer, asm_specification,
39703eb9
MM
12021 attributes);
12022 /* Any initialization must have been from a
12023 constant-expression. */
12024 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12025 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12026 }
a723baf1
MM
12027 }
12028
12029 /* Reset PREFIX_ATTRIBUTES. */
12030 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12031 attributes = TREE_CHAIN (attributes);
12032 if (attributes)
12033 TREE_CHAIN (attributes) = NULL_TREE;
12034
12035 /* If there is any qualification still in effect, clear it
12036 now; we will be starting fresh with the next declarator. */
12037 parser->scope = NULL_TREE;
12038 parser->qualifying_scope = NULL_TREE;
12039 parser->object_scope = NULL_TREE;
12040 /* If it's a `,', then there are more declarators. */
12041 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12042 cp_lexer_consume_token (parser->lexer);
12043 /* If the next token isn't a `;', then we have a parse error. */
12044 else if (cp_lexer_next_token_is_not (parser->lexer,
12045 CPP_SEMICOLON))
12046 {
12047 cp_parser_error (parser, "expected `;'");
12048 /* Skip tokens until we find a `;' */
12049 cp_parser_skip_to_end_of_statement (parser);
12050
12051 break;
12052 }
12053
12054 if (decl)
12055 {
12056 /* Add DECL to the list of members. */
12057 if (!friend_p)
12058 finish_member_declaration (decl);
12059
a723baf1 12060 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12061 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12062 }
12063 }
12064 }
12065
12066 /* If everything went well, look for the `;'. */
12067 if (cp_parser_parse_definitely (parser))
12068 {
12069 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12070 return;
12071 }
12072
12073 /* Parse the function-definition. */
12074 decl = cp_parser_function_definition (parser, &friend_p);
12075 /* If the member was not a friend, declare it here. */
12076 if (!friend_p)
12077 finish_member_declaration (decl);
12078 /* Peek at the next token. */
12079 token = cp_lexer_peek_token (parser->lexer);
12080 /* If the next token is a semicolon, consume it. */
12081 if (token->type == CPP_SEMICOLON)
12082 cp_lexer_consume_token (parser->lexer);
12083}
12084
12085/* Parse a pure-specifier.
12086
12087 pure-specifier:
12088 = 0
12089
12090 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12091 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12092
12093static tree
94edc4ab 12094cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12095{
12096 cp_token *token;
12097
12098 /* Look for the `=' token. */
12099 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12100 return error_mark_node;
12101 /* Look for the `0' token. */
12102 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12103 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12104 to get information from the lexer about how the number was
12105 spelled in order to fix this problem. */
12106 if (!token || !integer_zerop (token->value))
12107 return error_mark_node;
12108
12109 return integer_zero_node;
12110}
12111
12112/* Parse a constant-initializer.
12113
12114 constant-initializer:
12115 = constant-expression
12116
12117 Returns a representation of the constant-expression. */
12118
12119static tree
94edc4ab 12120cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12121{
12122 /* Look for the `=' token. */
12123 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12124 return error_mark_node;
12125
12126 /* It is invalid to write:
12127
12128 struct S { static const int i = { 7 }; };
12129
12130 */
12131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12132 {
12133 cp_parser_error (parser,
12134 "a brace-enclosed initializer is not allowed here");
12135 /* Consume the opening brace. */
12136 cp_lexer_consume_token (parser->lexer);
12137 /* Skip the initializer. */
12138 cp_parser_skip_to_closing_brace (parser);
12139 /* Look for the trailing `}'. */
12140 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12141
12142 return error_mark_node;
12143 }
12144
14d22dd6
MM
12145 return cp_parser_constant_expression (parser,
12146 /*allow_non_constant=*/false,
12147 NULL);
a723baf1
MM
12148}
12149
12150/* Derived classes [gram.class.derived] */
12151
12152/* Parse a base-clause.
12153
12154 base-clause:
12155 : base-specifier-list
12156
12157 base-specifier-list:
12158 base-specifier
12159 base-specifier-list , base-specifier
12160
12161 Returns a TREE_LIST representing the base-classes, in the order in
12162 which they were declared. The representation of each node is as
12163 described by cp_parser_base_specifier.
12164
12165 In the case that no bases are specified, this function will return
12166 NULL_TREE, not ERROR_MARK_NODE. */
12167
12168static tree
94edc4ab 12169cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12170{
12171 tree bases = NULL_TREE;
12172
12173 /* Look for the `:' that begins the list. */
12174 cp_parser_require (parser, CPP_COLON, "`:'");
12175
12176 /* Scan the base-specifier-list. */
12177 while (true)
12178 {
12179 cp_token *token;
12180 tree base;
12181
12182 /* Look for the base-specifier. */
12183 base = cp_parser_base_specifier (parser);
12184 /* Add BASE to the front of the list. */
12185 if (base != error_mark_node)
12186 {
12187 TREE_CHAIN (base) = bases;
12188 bases = base;
12189 }
12190 /* Peek at the next token. */
12191 token = cp_lexer_peek_token (parser->lexer);
12192 /* If it's not a comma, then the list is complete. */
12193 if (token->type != CPP_COMMA)
12194 break;
12195 /* Consume the `,'. */
12196 cp_lexer_consume_token (parser->lexer);
12197 }
12198
12199 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12200 base class had a qualified name. However, the next name that
12201 appears is certainly not qualified. */
12202 parser->scope = NULL_TREE;
12203 parser->qualifying_scope = NULL_TREE;
12204 parser->object_scope = NULL_TREE;
12205
12206 return nreverse (bases);
12207}
12208
12209/* Parse a base-specifier.
12210
12211 base-specifier:
12212 :: [opt] nested-name-specifier [opt] class-name
12213 virtual access-specifier [opt] :: [opt] nested-name-specifier
12214 [opt] class-name
12215 access-specifier virtual [opt] :: [opt] nested-name-specifier
12216 [opt] class-name
12217
12218 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12219 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12220 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12221 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12222
12223static tree
94edc4ab 12224cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12225{
12226 cp_token *token;
12227 bool done = false;
12228 bool virtual_p = false;
12229 bool duplicate_virtual_error_issued_p = false;
12230 bool duplicate_access_error_issued_p = false;
bbaab916 12231 bool class_scope_p, template_p;
dbbf88d1 12232 tree access = access_default_node;
a723baf1
MM
12233 tree type;
12234
12235 /* Process the optional `virtual' and `access-specifier'. */
12236 while (!done)
12237 {
12238 /* Peek at the next token. */
12239 token = cp_lexer_peek_token (parser->lexer);
12240 /* Process `virtual'. */
12241 switch (token->keyword)
12242 {
12243 case RID_VIRTUAL:
12244 /* If `virtual' appears more than once, issue an error. */
12245 if (virtual_p && !duplicate_virtual_error_issued_p)
12246 {
12247 cp_parser_error (parser,
12248 "`virtual' specified more than once in base-specified");
12249 duplicate_virtual_error_issued_p = true;
12250 }
12251
12252 virtual_p = true;
12253
12254 /* Consume the `virtual' token. */
12255 cp_lexer_consume_token (parser->lexer);
12256
12257 break;
12258
12259 case RID_PUBLIC:
12260 case RID_PROTECTED:
12261 case RID_PRIVATE:
12262 /* If more than one access specifier appears, issue an
12263 error. */
dbbf88d1
NS
12264 if (access != access_default_node
12265 && !duplicate_access_error_issued_p)
a723baf1
MM
12266 {
12267 cp_parser_error (parser,
12268 "more than one access specifier in base-specified");
12269 duplicate_access_error_issued_p = true;
12270 }
12271
dbbf88d1 12272 access = ridpointers[(int) token->keyword];
a723baf1
MM
12273
12274 /* Consume the access-specifier. */
12275 cp_lexer_consume_token (parser->lexer);
12276
12277 break;
12278
12279 default:
12280 done = true;
12281 break;
12282 }
12283 }
12284
a723baf1
MM
12285 /* Look for the optional `::' operator. */
12286 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12287 /* Look for the nested-name-specifier. The simplest way to
12288 implement:
12289
12290 [temp.res]
12291
12292 The keyword `typename' is not permitted in a base-specifier or
12293 mem-initializer; in these contexts a qualified name that
12294 depends on a template-parameter is implicitly assumed to be a
12295 type name.
12296
12297 is to pretend that we have seen the `typename' keyword at this
12298 point. */
12299 cp_parser_nested_name_specifier_opt (parser,
12300 /*typename_keyword_p=*/true,
12301 /*check_dependency_p=*/true,
12302 /*type_p=*/true);
12303 /* If the base class is given by a qualified name, assume that names
12304 we see are type names or templates, as appropriate. */
12305 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12306 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12307
a723baf1
MM
12308 /* Finally, look for the class-name. */
12309 type = cp_parser_class_name (parser,
12310 class_scope_p,
bbaab916 12311 template_p,
a723baf1 12312 /*type_p=*/true,
a723baf1
MM
12313 /*check_dependency_p=*/true,
12314 /*class_head_p=*/false);
12315
12316 if (type == error_mark_node)
12317 return error_mark_node;
12318
dbbf88d1 12319 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12320}
12321
12322/* Exception handling [gram.exception] */
12323
12324/* Parse an (optional) exception-specification.
12325
12326 exception-specification:
12327 throw ( type-id-list [opt] )
12328
12329 Returns a TREE_LIST representing the exception-specification. The
12330 TREE_VALUE of each node is a type. */
12331
12332static tree
94edc4ab 12333cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12334{
12335 cp_token *token;
12336 tree type_id_list;
12337
12338 /* Peek at the next token. */
12339 token = cp_lexer_peek_token (parser->lexer);
12340 /* If it's not `throw', then there's no exception-specification. */
12341 if (!cp_parser_is_keyword (token, RID_THROW))
12342 return NULL_TREE;
12343
12344 /* Consume the `throw'. */
12345 cp_lexer_consume_token (parser->lexer);
12346
12347 /* Look for the `('. */
12348 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12349
12350 /* Peek at the next token. */
12351 token = cp_lexer_peek_token (parser->lexer);
12352 /* If it's not a `)', then there is a type-id-list. */
12353 if (token->type != CPP_CLOSE_PAREN)
12354 {
12355 const char *saved_message;
12356
12357 /* Types may not be defined in an exception-specification. */
12358 saved_message = parser->type_definition_forbidden_message;
12359 parser->type_definition_forbidden_message
12360 = "types may not be defined in an exception-specification";
12361 /* Parse the type-id-list. */
12362 type_id_list = cp_parser_type_id_list (parser);
12363 /* Restore the saved message. */
12364 parser->type_definition_forbidden_message = saved_message;
12365 }
12366 else
12367 type_id_list = empty_except_spec;
12368
12369 /* Look for the `)'. */
12370 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12371
12372 return type_id_list;
12373}
12374
12375/* Parse an (optional) type-id-list.
12376
12377 type-id-list:
12378 type-id
12379 type-id-list , type-id
12380
12381 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12382 in the order that the types were presented. */
12383
12384static tree
94edc4ab 12385cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12386{
12387 tree types = NULL_TREE;
12388
12389 while (true)
12390 {
12391 cp_token *token;
12392 tree type;
12393
12394 /* Get the next type-id. */
12395 type = cp_parser_type_id (parser);
12396 /* Add it to the list. */
12397 types = add_exception_specifier (types, type, /*complain=*/1);
12398 /* Peek at the next token. */
12399 token = cp_lexer_peek_token (parser->lexer);
12400 /* If it is not a `,', we are done. */
12401 if (token->type != CPP_COMMA)
12402 break;
12403 /* Consume the `,'. */
12404 cp_lexer_consume_token (parser->lexer);
12405 }
12406
12407 return nreverse (types);
12408}
12409
12410/* Parse a try-block.
12411
12412 try-block:
12413 try compound-statement handler-seq */
12414
12415static tree
94edc4ab 12416cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12417{
12418 tree try_block;
12419
12420 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12421 try_block = begin_try_block ();
a5bcc582 12422 cp_parser_compound_statement (parser, false);
a723baf1
MM
12423 finish_try_block (try_block);
12424 cp_parser_handler_seq (parser);
12425 finish_handler_sequence (try_block);
12426
12427 return try_block;
12428}
12429
12430/* Parse a function-try-block.
12431
12432 function-try-block:
12433 try ctor-initializer [opt] function-body handler-seq */
12434
12435static bool
94edc4ab 12436cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12437{
12438 tree try_block;
12439 bool ctor_initializer_p;
12440
12441 /* Look for the `try' keyword. */
12442 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12443 return false;
12444 /* Let the rest of the front-end know where we are. */
12445 try_block = begin_function_try_block ();
12446 /* Parse the function-body. */
12447 ctor_initializer_p
12448 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12449 /* We're done with the `try' part. */
12450 finish_function_try_block (try_block);
12451 /* Parse the handlers. */
12452 cp_parser_handler_seq (parser);
12453 /* We're done with the handlers. */
12454 finish_function_handler_sequence (try_block);
12455
12456 return ctor_initializer_p;
12457}
12458
12459/* Parse a handler-seq.
12460
12461 handler-seq:
12462 handler handler-seq [opt] */
12463
12464static void
94edc4ab 12465cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12466{
12467 while (true)
12468 {
12469 cp_token *token;
12470
12471 /* Parse the handler. */
12472 cp_parser_handler (parser);
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12475 /* If it's not `catch' then there are no more handlers. */
12476 if (!cp_parser_is_keyword (token, RID_CATCH))
12477 break;
12478 }
12479}
12480
12481/* Parse a handler.
12482
12483 handler:
12484 catch ( exception-declaration ) compound-statement */
12485
12486static void
94edc4ab 12487cp_parser_handler (cp_parser* parser)
a723baf1
MM
12488{
12489 tree handler;
12490 tree declaration;
12491
12492 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12493 handler = begin_handler ();
12494 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12495 declaration = cp_parser_exception_declaration (parser);
12496 finish_handler_parms (declaration, handler);
12497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 12498 cp_parser_compound_statement (parser, false);
a723baf1
MM
12499 finish_handler (handler);
12500}
12501
12502/* Parse an exception-declaration.
12503
12504 exception-declaration:
12505 type-specifier-seq declarator
12506 type-specifier-seq abstract-declarator
12507 type-specifier-seq
12508 ...
12509
12510 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12511 ellipsis variant is used. */
12512
12513static tree
94edc4ab 12514cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12515{
12516 tree type_specifiers;
12517 tree declarator;
12518 const char *saved_message;
12519
12520 /* If it's an ellipsis, it's easy to handle. */
12521 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12522 {
12523 /* Consume the `...' token. */
12524 cp_lexer_consume_token (parser->lexer);
12525 return NULL_TREE;
12526 }
12527
12528 /* Types may not be defined in exception-declarations. */
12529 saved_message = parser->type_definition_forbidden_message;
12530 parser->type_definition_forbidden_message
12531 = "types may not be defined in exception-declarations";
12532
12533 /* Parse the type-specifier-seq. */
12534 type_specifiers = cp_parser_type_specifier_seq (parser);
12535 /* If it's a `)', then there is no declarator. */
12536 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12537 declarator = NULL_TREE;
12538 else
62b8a44e
NS
12539 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12540 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
12541
12542 /* Restore the saved message. */
12543 parser->type_definition_forbidden_message = saved_message;
12544
12545 return start_handler_parms (type_specifiers, declarator);
12546}
12547
12548/* Parse a throw-expression.
12549
12550 throw-expression:
34cd5ae7 12551 throw assignment-expression [opt]
a723baf1
MM
12552
12553 Returns a THROW_EXPR representing the throw-expression. */
12554
12555static tree
94edc4ab 12556cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12557{
12558 tree expression;
12559
12560 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12561 /* We can't be sure if there is an assignment-expression or not. */
12562 cp_parser_parse_tentatively (parser);
12563 /* Try it. */
12564 expression = cp_parser_assignment_expression (parser);
12565 /* If it didn't work, this is just a rethrow. */
12566 if (!cp_parser_parse_definitely (parser))
12567 expression = NULL_TREE;
12568
12569 return build_throw (expression);
12570}
12571
12572/* GNU Extensions */
12573
12574/* Parse an (optional) asm-specification.
12575
12576 asm-specification:
12577 asm ( string-literal )
12578
12579 If the asm-specification is present, returns a STRING_CST
12580 corresponding to the string-literal. Otherwise, returns
12581 NULL_TREE. */
12582
12583static tree
94edc4ab 12584cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12585{
12586 cp_token *token;
12587 tree asm_specification;
12588
12589 /* Peek at the next token. */
12590 token = cp_lexer_peek_token (parser->lexer);
12591 /* If the next token isn't the `asm' keyword, then there's no
12592 asm-specification. */
12593 if (!cp_parser_is_keyword (token, RID_ASM))
12594 return NULL_TREE;
12595
12596 /* Consume the `asm' token. */
12597 cp_lexer_consume_token (parser->lexer);
12598 /* Look for the `('. */
12599 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12600
12601 /* Look for the string-literal. */
12602 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12603 if (token)
12604 asm_specification = token->value;
12605 else
12606 asm_specification = NULL_TREE;
12607
12608 /* Look for the `)'. */
12609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12610
12611 return asm_specification;
12612}
12613
12614/* Parse an asm-operand-list.
12615
12616 asm-operand-list:
12617 asm-operand
12618 asm-operand-list , asm-operand
12619
12620 asm-operand:
12621 string-literal ( expression )
12622 [ string-literal ] string-literal ( expression )
12623
12624 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12625 each node is the expression. The TREE_PURPOSE is itself a
12626 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12627 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12628 is a STRING_CST for the string literal before the parenthesis. */
12629
12630static tree
94edc4ab 12631cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12632{
12633 tree asm_operands = NULL_TREE;
12634
12635 while (true)
12636 {
12637 tree string_literal;
12638 tree expression;
12639 tree name;
12640 cp_token *token;
12641
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12643 {
12644 /* Consume the `[' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* Read the operand name. */
12647 name = cp_parser_identifier (parser);
12648 if (name != error_mark_node)
12649 name = build_string (IDENTIFIER_LENGTH (name),
12650 IDENTIFIER_POINTER (name));
12651 /* Look for the closing `]'. */
12652 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12653 }
12654 else
12655 name = NULL_TREE;
12656 /* Look for the string-literal. */
12657 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12658 string_literal = token ? token->value : error_mark_node;
12659 /* Look for the `('. */
12660 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12661 /* Parse the expression. */
12662 expression = cp_parser_expression (parser);
12663 /* Look for the `)'. */
12664 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12665 /* Add this operand to the list. */
12666 asm_operands = tree_cons (build_tree_list (name, string_literal),
12667 expression,
12668 asm_operands);
12669 /* If the next token is not a `,', there are no more
12670 operands. */
12671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12672 break;
12673 /* Consume the `,'. */
12674 cp_lexer_consume_token (parser->lexer);
12675 }
12676
12677 return nreverse (asm_operands);
12678}
12679
12680/* Parse an asm-clobber-list.
12681
12682 asm-clobber-list:
12683 string-literal
12684 asm-clobber-list , string-literal
12685
12686 Returns a TREE_LIST, indicating the clobbers in the order that they
12687 appeared. The TREE_VALUE of each node is a STRING_CST. */
12688
12689static tree
94edc4ab 12690cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
12691{
12692 tree clobbers = NULL_TREE;
12693
12694 while (true)
12695 {
12696 cp_token *token;
12697 tree string_literal;
12698
12699 /* Look for the string literal. */
12700 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12701 string_literal = token ? token->value : error_mark_node;
12702 /* Add it to the list. */
12703 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12704 /* If the next token is not a `,', then the list is
12705 complete. */
12706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12707 break;
12708 /* Consume the `,' token. */
12709 cp_lexer_consume_token (parser->lexer);
12710 }
12711
12712 return clobbers;
12713}
12714
12715/* Parse an (optional) series of attributes.
12716
12717 attributes:
12718 attributes attribute
12719
12720 attribute:
12721 __attribute__ (( attribute-list [opt] ))
12722
12723 The return value is as for cp_parser_attribute_list. */
12724
12725static tree
94edc4ab 12726cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
12727{
12728 tree attributes = NULL_TREE;
12729
12730 while (true)
12731 {
12732 cp_token *token;
12733 tree attribute_list;
12734
12735 /* Peek at the next token. */
12736 token = cp_lexer_peek_token (parser->lexer);
12737 /* If it's not `__attribute__', then we're done. */
12738 if (token->keyword != RID_ATTRIBUTE)
12739 break;
12740
12741 /* Consume the `__attribute__' keyword. */
12742 cp_lexer_consume_token (parser->lexer);
12743 /* Look for the two `(' tokens. */
12744 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12745 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12746
12747 /* Peek at the next token. */
12748 token = cp_lexer_peek_token (parser->lexer);
12749 if (token->type != CPP_CLOSE_PAREN)
12750 /* Parse the attribute-list. */
12751 attribute_list = cp_parser_attribute_list (parser);
12752 else
12753 /* If the next token is a `)', then there is no attribute
12754 list. */
12755 attribute_list = NULL;
12756
12757 /* Look for the two `)' tokens. */
12758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12759 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12760
12761 /* Add these new attributes to the list. */
12762 attributes = chainon (attributes, attribute_list);
12763 }
12764
12765 return attributes;
12766}
12767
12768/* Parse an attribute-list.
12769
12770 attribute-list:
12771 attribute
12772 attribute-list , attribute
12773
12774 attribute:
12775 identifier
12776 identifier ( identifier )
12777 identifier ( identifier , expression-list )
12778 identifier ( expression-list )
12779
12780 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12781 TREE_PURPOSE of each node is the identifier indicating which
12782 attribute is in use. The TREE_VALUE represents the arguments, if
12783 any. */
12784
12785static tree
94edc4ab 12786cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
12787{
12788 tree attribute_list = NULL_TREE;
12789
12790 while (true)
12791 {
12792 cp_token *token;
12793 tree identifier;
12794 tree attribute;
12795
12796 /* Look for the identifier. We also allow keywords here; for
12797 example `__attribute__ ((const))' is legal. */
12798 token = cp_lexer_peek_token (parser->lexer);
12799 if (token->type != CPP_NAME
12800 && token->type != CPP_KEYWORD)
12801 return error_mark_node;
12802 /* Consume the token. */
12803 token = cp_lexer_consume_token (parser->lexer);
12804
12805 /* Save away the identifier that indicates which attribute this is. */
12806 identifier = token->value;
12807 attribute = build_tree_list (identifier, NULL_TREE);
12808
12809 /* Peek at the next token. */
12810 token = cp_lexer_peek_token (parser->lexer);
12811 /* If it's an `(', then parse the attribute arguments. */
12812 if (token->type == CPP_OPEN_PAREN)
12813 {
12814 tree arguments;
a723baf1 12815
39703eb9
MM
12816 arguments = (cp_parser_parenthesized_expression_list
12817 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
12818 /* Save the identifier and arguments away. */
12819 TREE_VALUE (attribute) = arguments;
a723baf1
MM
12820 }
12821
12822 /* Add this attribute to the list. */
12823 TREE_CHAIN (attribute) = attribute_list;
12824 attribute_list = attribute;
12825
12826 /* Now, look for more attributes. */
12827 token = cp_lexer_peek_token (parser->lexer);
12828 /* If the next token isn't a `,', we're done. */
12829 if (token->type != CPP_COMMA)
12830 break;
12831
cd0be382 12832 /* Consume the comma and keep going. */
a723baf1
MM
12833 cp_lexer_consume_token (parser->lexer);
12834 }
12835
12836 /* We built up the list in reverse order. */
12837 return nreverse (attribute_list);
12838}
12839
12840/* Parse an optional `__extension__' keyword. Returns TRUE if it is
12841 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12842 current value of the PEDANTIC flag, regardless of whether or not
12843 the `__extension__' keyword is present. The caller is responsible
12844 for restoring the value of the PEDANTIC flag. */
12845
12846static bool
94edc4ab 12847cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
12848{
12849 /* Save the old value of the PEDANTIC flag. */
12850 *saved_pedantic = pedantic;
12851
12852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12853 {
12854 /* Consume the `__extension__' token. */
12855 cp_lexer_consume_token (parser->lexer);
12856 /* We're not being pedantic while the `__extension__' keyword is
12857 in effect. */
12858 pedantic = 0;
12859
12860 return true;
12861 }
12862
12863 return false;
12864}
12865
12866/* Parse a label declaration.
12867
12868 label-declaration:
12869 __label__ label-declarator-seq ;
12870
12871 label-declarator-seq:
12872 identifier , label-declarator-seq
12873 identifier */
12874
12875static void
94edc4ab 12876cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
12877{
12878 /* Look for the `__label__' keyword. */
12879 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12880
12881 while (true)
12882 {
12883 tree identifier;
12884
12885 /* Look for an identifier. */
12886 identifier = cp_parser_identifier (parser);
12887 /* Declare it as a lobel. */
12888 finish_label_decl (identifier);
12889 /* If the next token is a `;', stop. */
12890 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12891 break;
12892 /* Look for the `,' separating the label declarations. */
12893 cp_parser_require (parser, CPP_COMMA, "`,'");
12894 }
12895
12896 /* Look for the final `;'. */
12897 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12898}
12899
12900/* Support Functions */
12901
12902/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12903 NAME should have one of the representations used for an
12904 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12905 is returned. If PARSER->SCOPE is a dependent type, then a
12906 SCOPE_REF is returned.
12907
12908 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12909 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12910 was formed. Abstractly, such entities should not be passed to this
12911 function, because they do not need to be looked up, but it is
12912 simpler to check for this special case here, rather than at the
12913 call-sites.
12914
12915 In cases not explicitly covered above, this function returns a
12916 DECL, OVERLOAD, or baselink representing the result of the lookup.
12917 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12918 is returned.
12919
a723baf1
MM
12920 If IS_TYPE is TRUE, bindings that do not refer to types are
12921 ignored.
12922
eea9800f
MM
12923 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12924 are ignored.
12925
a723baf1
MM
12926 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12927 types. */
12928
12929static tree
8d241e0b 12930cp_parser_lookup_name (cp_parser *parser, tree name,
eea9800f 12931 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
12932{
12933 tree decl;
12934 tree object_type = parser->context->object_type;
12935
12936 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12937 no longer valid. Note that if we are parsing tentatively, and
12938 the parse fails, OBJECT_TYPE will be automatically restored. */
12939 parser->context->object_type = NULL_TREE;
12940
12941 if (name == error_mark_node)
12942 return error_mark_node;
12943
12944 /* A template-id has already been resolved; there is no lookup to
12945 do. */
12946 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12947 return name;
12948 if (BASELINK_P (name))
12949 {
12950 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12951 == TEMPLATE_ID_EXPR),
12952 20020909);
12953 return name;
12954 }
12955
12956 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12957 it should already have been checked to make sure that the name
12958 used matches the type being destroyed. */
12959 if (TREE_CODE (name) == BIT_NOT_EXPR)
12960 {
12961 tree type;
12962
12963 /* Figure out to which type this destructor applies. */
12964 if (parser->scope)
12965 type = parser->scope;
12966 else if (object_type)
12967 type = object_type;
12968 else
12969 type = current_class_type;
12970 /* If that's not a class type, there is no destructor. */
12971 if (!type || !CLASS_TYPE_P (type))
12972 return error_mark_node;
12973 /* If it was a class type, return the destructor. */
12974 return CLASSTYPE_DESTRUCTORS (type);
12975 }
12976
12977 /* By this point, the NAME should be an ordinary identifier. If
12978 the id-expression was a qualified name, the qualifying scope is
12979 stored in PARSER->SCOPE at this point. */
12980 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12981 20000619);
12982
12983 /* Perform the lookup. */
12984 if (parser->scope)
12985 {
1fb3244a 12986 bool dependent_p;
a723baf1
MM
12987
12988 if (parser->scope == error_mark_node)
12989 return error_mark_node;
12990
12991 /* If the SCOPE is dependent, the lookup must be deferred until
12992 the template is instantiated -- unless we are explicitly
12993 looking up names in uninstantiated templates. Even then, we
12994 cannot look up the name if the scope is not a class type; it
12995 might, for example, be a template type parameter. */
1fb3244a
MM
12996 dependent_p = (TYPE_P (parser->scope)
12997 && !(parser->in_declarator_p
12998 && currently_open_class (parser->scope))
12999 && dependent_type_p (parser->scope));
a723baf1 13000 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13001 && dependent_p)
a723baf1
MM
13002 {
13003 if (!is_type)
13004 decl = build_nt (SCOPE_REF, parser->scope, name);
13005 else
13006 /* The resolution to Core Issue 180 says that `struct A::B'
13007 should be considered a type-name, even if `A' is
13008 dependent. */
13009 decl = TYPE_NAME (make_typename_type (parser->scope,
13010 name,
13011 /*complain=*/1));
13012 }
13013 else
13014 {
13015 /* If PARSER->SCOPE is a dependent type, then it must be a
13016 class type, and we must not be checking dependencies;
13017 otherwise, we would have processed this lookup above. So
13018 that PARSER->SCOPE is not considered a dependent base by
13019 lookup_member, we must enter the scope here. */
1fb3244a 13020 if (dependent_p)
a723baf1
MM
13021 push_scope (parser->scope);
13022 /* If the PARSER->SCOPE is a a template specialization, it
13023 may be instantiated during name lookup. In that case,
13024 errors may be issued. Even if we rollback the current
13025 tentative parse, those errors are valid. */
5e08432e
MM
13026 decl = lookup_qualified_name (parser->scope, name, is_type,
13027 /*complain=*/true);
1fb3244a 13028 if (dependent_p)
a723baf1
MM
13029 pop_scope (parser->scope);
13030 }
13031 parser->qualifying_scope = parser->scope;
13032 parser->object_scope = NULL_TREE;
13033 }
13034 else if (object_type)
13035 {
13036 tree object_decl = NULL_TREE;
13037 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13038 OBJECT_TYPE is not a class. */
13039 if (CLASS_TYPE_P (object_type))
13040 /* If the OBJECT_TYPE is a template specialization, it may
13041 be instantiated during name lookup. In that case, errors
13042 may be issued. Even if we rollback the current tentative
13043 parse, those errors are valid. */
13044 object_decl = lookup_member (object_type,
13045 name,
13046 /*protect=*/0, is_type);
13047 /* Look it up in the enclosing context, too. */
13048 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13049 is_namespace,
a723baf1
MM
13050 /*flags=*/0);
13051 parser->object_scope = object_type;
13052 parser->qualifying_scope = NULL_TREE;
13053 if (object_decl)
13054 decl = object_decl;
13055 }
13056 else
13057 {
13058 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13059 is_namespace,
a723baf1
MM
13060 /*flags=*/0);
13061 parser->qualifying_scope = NULL_TREE;
13062 parser->object_scope = NULL_TREE;
13063 }
13064
13065 /* If the lookup failed, let our caller know. */
13066 if (!decl
13067 || decl == error_mark_node
13068 || (TREE_CODE (decl) == FUNCTION_DECL
13069 && DECL_ANTICIPATED (decl)))
13070 return error_mark_node;
13071
13072 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13073 if (TREE_CODE (decl) == TREE_LIST)
13074 {
13075 /* The error message we have to print is too complicated for
13076 cp_parser_error, so we incorporate its actions directly. */
e5976695 13077 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13078 {
13079 error ("reference to `%D' is ambiguous", name);
13080 print_candidates (decl);
13081 }
13082 return error_mark_node;
13083 }
13084
13085 my_friendly_assert (DECL_P (decl)
13086 || TREE_CODE (decl) == OVERLOAD
13087 || TREE_CODE (decl) == SCOPE_REF
13088 || BASELINK_P (decl),
13089 20000619);
13090
13091 /* If we have resolved the name of a member declaration, check to
13092 see if the declaration is accessible. When the name resolves to
34cd5ae7 13093 set of overloaded functions, accessibility is checked when
a723baf1
MM
13094 overload resolution is done.
13095
13096 During an explicit instantiation, access is not checked at all,
13097 as per [temp.explicit]. */
8d241e0b 13098 if (DECL_P (decl))
ee76b931 13099 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13100
13101 return decl;
13102}
13103
13104/* Like cp_parser_lookup_name, but for use in the typical case where
13105 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13106 TRUE. */
13107
13108static tree
94edc4ab 13109cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
13110{
13111 return cp_parser_lookup_name (parser, name,
eea9800f
MM
13112 /*is_type=*/false,
13113 /*is_namespace=*/false,
a723baf1
MM
13114 /*check_dependency=*/true);
13115}
13116
a723baf1
MM
13117/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13118 the current context, return the TYPE_DECL. If TAG_NAME_P is
13119 true, the DECL indicates the class being defined in a class-head,
13120 or declared in an elaborated-type-specifier.
13121
13122 Otherwise, return DECL. */
13123
13124static tree
13125cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13126{
710b73e6
KL
13127 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13128 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
13129
13130 struct A {
13131 template <typename T> struct B;
13132 };
13133
13134 template <typename T> struct A::B {};
13135
13136 Similarly, in a elaborated-type-specifier:
13137
13138 namespace N { struct X{}; }
13139
13140 struct A {
13141 template <typename T> friend struct N::X;
13142 };
13143
710b73e6
KL
13144 However, if the DECL refers to a class type, and we are in
13145 the scope of the class, then the name lookup automatically
13146 finds the TYPE_DECL created by build_self_reference rather
13147 than a TEMPLATE_DECL. For example, in:
13148
13149 template <class T> struct S {
13150 S s;
13151 };
13152
13153 there is no need to handle such case. */
13154
13155 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13156 return DECL_TEMPLATE_RESULT (decl);
13157
13158 return decl;
13159}
13160
13161/* If too many, or too few, template-parameter lists apply to the
13162 declarator, issue an error message. Returns TRUE if all went well,
13163 and FALSE otherwise. */
13164
13165static bool
94edc4ab
NN
13166cp_parser_check_declarator_template_parameters (cp_parser* parser,
13167 tree declarator)
a723baf1
MM
13168{
13169 unsigned num_templates;
13170
13171 /* We haven't seen any classes that involve template parameters yet. */
13172 num_templates = 0;
13173
13174 switch (TREE_CODE (declarator))
13175 {
13176 case CALL_EXPR:
13177 case ARRAY_REF:
13178 case INDIRECT_REF:
13179 case ADDR_EXPR:
13180 {
13181 tree main_declarator = TREE_OPERAND (declarator, 0);
13182 return
13183 cp_parser_check_declarator_template_parameters (parser,
13184 main_declarator);
13185 }
13186
13187 case SCOPE_REF:
13188 {
13189 tree scope;
13190 tree member;
13191
13192 scope = TREE_OPERAND (declarator, 0);
13193 member = TREE_OPERAND (declarator, 1);
13194
13195 /* If this is a pointer-to-member, then we are not interested
13196 in the SCOPE, because it does not qualify the thing that is
13197 being declared. */
13198 if (TREE_CODE (member) == INDIRECT_REF)
13199 return (cp_parser_check_declarator_template_parameters
13200 (parser, member));
13201
13202 while (scope && CLASS_TYPE_P (scope))
13203 {
13204 /* You're supposed to have one `template <...>'
13205 for every template class, but you don't need one
13206 for a full specialization. For example:
13207
13208 template <class T> struct S{};
13209 template <> struct S<int> { void f(); };
13210 void S<int>::f () {}
13211
13212 is correct; there shouldn't be a `template <>' for
13213 the definition of `S<int>::f'. */
13214 if (CLASSTYPE_TEMPLATE_INFO (scope)
13215 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13216 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13217 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13218 ++num_templates;
13219
13220 scope = TYPE_CONTEXT (scope);
13221 }
13222 }
13223
13224 /* Fall through. */
13225
13226 default:
13227 /* If the DECLARATOR has the form `X<y>' then it uses one
13228 additional level of template parameters. */
13229 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13230 ++num_templates;
13231
13232 return cp_parser_check_template_parameters (parser,
13233 num_templates);
13234 }
13235}
13236
13237/* NUM_TEMPLATES were used in the current declaration. If that is
13238 invalid, return FALSE and issue an error messages. Otherwise,
13239 return TRUE. */
13240
13241static bool
94edc4ab
NN
13242cp_parser_check_template_parameters (cp_parser* parser,
13243 unsigned num_templates)
a723baf1
MM
13244{
13245 /* If there are more template classes than parameter lists, we have
13246 something like:
13247
13248 template <class T> void S<T>::R<T>::f (); */
13249 if (parser->num_template_parameter_lists < num_templates)
13250 {
13251 error ("too few template-parameter-lists");
13252 return false;
13253 }
13254 /* If there are the same number of template classes and parameter
13255 lists, that's OK. */
13256 if (parser->num_template_parameter_lists == num_templates)
13257 return true;
13258 /* If there are more, but only one more, then we are referring to a
13259 member template. That's OK too. */
13260 if (parser->num_template_parameter_lists == num_templates + 1)
13261 return true;
13262 /* Otherwise, there are too many template parameter lists. We have
13263 something like:
13264
13265 template <class T> template <class U> void S::f(); */
13266 error ("too many template-parameter-lists");
13267 return false;
13268}
13269
13270/* Parse a binary-expression of the general form:
13271
13272 binary-expression:
13273 <expr>
13274 binary-expression <token> <expr>
13275
13276 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13277 to parser the <expr>s. If the first production is used, then the
13278 value returned by FN is returned directly. Otherwise, a node with
13279 the indicated EXPR_TYPE is returned, with operands corresponding to
13280 the two sub-expressions. */
13281
13282static tree
94edc4ab
NN
13283cp_parser_binary_expression (cp_parser* parser,
13284 const cp_parser_token_tree_map token_tree_map,
13285 cp_parser_expression_fn fn)
a723baf1
MM
13286{
13287 tree lhs;
13288
13289 /* Parse the first expression. */
13290 lhs = (*fn) (parser);
13291 /* Now, look for more expressions. */
13292 while (true)
13293 {
13294 cp_token *token;
39b1af70 13295 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13296 tree rhs;
13297
13298 /* Peek at the next token. */
13299 token = cp_lexer_peek_token (parser->lexer);
13300 /* If the token is `>', and that's not an operator at the
13301 moment, then we're done. */
13302 if (token->type == CPP_GREATER
13303 && !parser->greater_than_is_operator_p)
13304 break;
34cd5ae7 13305 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13306 tree representation. */
13307 for (map_node = token_tree_map;
13308 map_node->token_type != CPP_EOF;
13309 ++map_node)
13310 if (map_node->token_type == token->type)
13311 {
13312 /* Consume the operator token. */
13313 cp_lexer_consume_token (parser->lexer);
13314 /* Parse the right-hand side of the expression. */
13315 rhs = (*fn) (parser);
13316 /* Build the binary tree node. */
13317 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13318 break;
13319 }
13320
13321 /* If the token wasn't one of the ones we want, we're done. */
13322 if (map_node->token_type == CPP_EOF)
13323 break;
13324 }
13325
13326 return lhs;
13327}
13328
13329/* Parse an optional `::' token indicating that the following name is
13330 from the global namespace. If so, PARSER->SCOPE is set to the
13331 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13332 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13333 Returns the new value of PARSER->SCOPE, if the `::' token is
13334 present, and NULL_TREE otherwise. */
13335
13336static tree
94edc4ab 13337cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13338{
13339 cp_token *token;
13340
13341 /* Peek at the next token. */
13342 token = cp_lexer_peek_token (parser->lexer);
13343 /* If we're looking at a `::' token then we're starting from the
13344 global namespace, not our current location. */
13345 if (token->type == CPP_SCOPE)
13346 {
13347 /* Consume the `::' token. */
13348 cp_lexer_consume_token (parser->lexer);
13349 /* Set the SCOPE so that we know where to start the lookup. */
13350 parser->scope = global_namespace;
13351 parser->qualifying_scope = global_namespace;
13352 parser->object_scope = NULL_TREE;
13353
13354 return parser->scope;
13355 }
13356 else if (!current_scope_valid_p)
13357 {
13358 parser->scope = NULL_TREE;
13359 parser->qualifying_scope = NULL_TREE;
13360 parser->object_scope = NULL_TREE;
13361 }
13362
13363 return NULL_TREE;
13364}
13365
13366/* Returns TRUE if the upcoming token sequence is the start of a
13367 constructor declarator. If FRIEND_P is true, the declarator is
13368 preceded by the `friend' specifier. */
13369
13370static bool
13371cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13372{
13373 bool constructor_p;
13374 tree type_decl = NULL_TREE;
13375 bool nested_name_p;
2050a1bb
MM
13376 cp_token *next_token;
13377
13378 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13379 try to avoid doing lots of work if at all possible. It's not
13380 valid declare a constructor at function scope. */
13381 if (at_function_scope_p ())
13382 return false;
13383 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13384 next_token = cp_lexer_peek_token (parser->lexer);
13385 if (next_token->type != CPP_NAME
13386 && next_token->type != CPP_SCOPE
13387 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13388 && next_token->type != CPP_TEMPLATE_ID)
13389 return false;
a723baf1
MM
13390
13391 /* Parse tentatively; we are going to roll back all of the tokens
13392 consumed here. */
13393 cp_parser_parse_tentatively (parser);
13394 /* Assume that we are looking at a constructor declarator. */
13395 constructor_p = true;
8d241e0b 13396
a723baf1
MM
13397 /* Look for the optional `::' operator. */
13398 cp_parser_global_scope_opt (parser,
13399 /*current_scope_valid_p=*/false);
13400 /* Look for the nested-name-specifier. */
13401 nested_name_p
13402 = (cp_parser_nested_name_specifier_opt (parser,
13403 /*typename_keyword_p=*/false,
13404 /*check_dependency_p=*/false,
13405 /*type_p=*/false)
13406 != NULL_TREE);
13407 /* Outside of a class-specifier, there must be a
13408 nested-name-specifier. */
13409 if (!nested_name_p &&
13410 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13411 || friend_p))
13412 constructor_p = false;
13413 /* If we still think that this might be a constructor-declarator,
13414 look for a class-name. */
13415 if (constructor_p)
13416 {
13417 /* If we have:
13418
8fbc5ae7 13419 template <typename T> struct S { S(); };
a723baf1
MM
13420 template <typename T> S<T>::S ();
13421
13422 we must recognize that the nested `S' names a class.
13423 Similarly, for:
13424
13425 template <typename T> S<T>::S<T> ();
13426
13427 we must recognize that the nested `S' names a template. */
13428 type_decl = cp_parser_class_name (parser,
13429 /*typename_keyword_p=*/false,
13430 /*template_keyword_p=*/false,
13431 /*type_p=*/false,
a723baf1
MM
13432 /*check_dependency_p=*/false,
13433 /*class_head_p=*/false);
13434 /* If there was no class-name, then this is not a constructor. */
13435 constructor_p = !cp_parser_error_occurred (parser);
13436 }
8d241e0b 13437
a723baf1
MM
13438 /* If we're still considering a constructor, we have to see a `(',
13439 to begin the parameter-declaration-clause, followed by either a
13440 `)', an `...', or a decl-specifier. We need to check for a
13441 type-specifier to avoid being fooled into thinking that:
13442
13443 S::S (f) (int);
13444
13445 is a constructor. (It is actually a function named `f' that
13446 takes one parameter (of type `int') and returns a value of type
13447 `S::S'. */
13448 if (constructor_p
13449 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13450 {
13451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13452 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13453 && !cp_parser_storage_class_specifier_opt (parser))
13454 {
5dae1114 13455 tree type;
4047b164 13456 unsigned saved_num_template_parameter_lists;
5dae1114
MM
13457
13458 /* Names appearing in the type-specifier should be looked up
13459 in the scope of the class. */
13460 if (current_class_type)
13461 type = NULL_TREE;
a723baf1
MM
13462 else
13463 {
5dae1114
MM
13464 type = TREE_TYPE (type_decl);
13465 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13466 {
13467 type = resolve_typename_type (type,
13468 /*only_current_p=*/false);
13469 if (type == error_mark_node)
13470 {
13471 cp_parser_abort_tentative_parse (parser);
13472 return false;
13473 }
13474 }
5dae1114 13475 push_scope (type);
a723baf1 13476 }
4047b164
KL
13477
13478 /* Inside the constructor parameter list, surrounding
13479 template-parameter-lists do not apply. */
13480 saved_num_template_parameter_lists
13481 = parser->num_template_parameter_lists;
13482 parser->num_template_parameter_lists = 0;
13483
5dae1114
MM
13484 /* Look for the type-specifier. */
13485 cp_parser_type_specifier (parser,
13486 CP_PARSER_FLAGS_NONE,
13487 /*is_friend=*/false,
13488 /*is_declarator=*/true,
13489 /*declares_class_or_enum=*/NULL,
13490 /*is_cv_qualifier=*/NULL);
4047b164
KL
13491
13492 parser->num_template_parameter_lists
13493 = saved_num_template_parameter_lists;
13494
5dae1114
MM
13495 /* Leave the scope of the class. */
13496 if (type)
13497 pop_scope (type);
13498
13499 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13500 }
13501 }
13502 else
13503 constructor_p = false;
13504 /* We did not really want to consume any tokens. */
13505 cp_parser_abort_tentative_parse (parser);
13506
13507 return constructor_p;
13508}
13509
13510/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13511 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13512 they must be performed once we are in the scope of the function.
13513
13514 Returns the function defined. */
13515
13516static tree
13517cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13518 (cp_parser* parser,
13519 tree decl_specifiers,
13520 tree attributes,
13521 tree declarator)
a723baf1
MM
13522{
13523 tree fn;
13524 bool success_p;
13525
13526 /* Begin the function-definition. */
13527 success_p = begin_function_definition (decl_specifiers,
13528 attributes,
13529 declarator);
13530
13531 /* If there were names looked up in the decl-specifier-seq that we
13532 did not check, check them now. We must wait until we are in the
13533 scope of the function to perform the checks, since the function
13534 might be a friend. */
cf22909c 13535 perform_deferred_access_checks ();
a723baf1
MM
13536
13537 if (!success_p)
13538 {
13539 /* If begin_function_definition didn't like the definition, skip
13540 the entire function. */
13541 error ("invalid function declaration");
13542 cp_parser_skip_to_end_of_block_or_statement (parser);
13543 fn = error_mark_node;
13544 }
13545 else
13546 fn = cp_parser_function_definition_after_declarator (parser,
13547 /*inline_p=*/false);
13548
13549 return fn;
13550}
13551
13552/* Parse the part of a function-definition that follows the
13553 declarator. INLINE_P is TRUE iff this function is an inline
13554 function defined with a class-specifier.
13555
13556 Returns the function defined. */
13557
13558static tree
94edc4ab
NN
13559cp_parser_function_definition_after_declarator (cp_parser* parser,
13560 bool inline_p)
a723baf1
MM
13561{
13562 tree fn;
13563 bool ctor_initializer_p = false;
13564 bool saved_in_unbraced_linkage_specification_p;
13565 unsigned saved_num_template_parameter_lists;
13566
13567 /* If the next token is `return', then the code may be trying to
13568 make use of the "named return value" extension that G++ used to
13569 support. */
13570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13571 {
13572 /* Consume the `return' keyword. */
13573 cp_lexer_consume_token (parser->lexer);
13574 /* Look for the identifier that indicates what value is to be
13575 returned. */
13576 cp_parser_identifier (parser);
13577 /* Issue an error message. */
13578 error ("named return values are no longer supported");
13579 /* Skip tokens until we reach the start of the function body. */
13580 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13581 cp_lexer_consume_token (parser->lexer);
13582 }
13583 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13584 anything declared inside `f'. */
13585 saved_in_unbraced_linkage_specification_p
13586 = parser->in_unbraced_linkage_specification_p;
13587 parser->in_unbraced_linkage_specification_p = false;
13588 /* Inside the function, surrounding template-parameter-lists do not
13589 apply. */
13590 saved_num_template_parameter_lists
13591 = parser->num_template_parameter_lists;
13592 parser->num_template_parameter_lists = 0;
13593 /* If the next token is `try', then we are looking at a
13594 function-try-block. */
13595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13596 ctor_initializer_p = cp_parser_function_try_block (parser);
13597 /* A function-try-block includes the function-body, so we only do
13598 this next part if we're not processing a function-try-block. */
13599 else
13600 ctor_initializer_p
13601 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13602
13603 /* Finish the function. */
13604 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13605 (inline_p ? 2 : 0));
13606 /* Generate code for it, if necessary. */
8cd2462c 13607 expand_or_defer_fn (fn);
a723baf1
MM
13608 /* Restore the saved values. */
13609 parser->in_unbraced_linkage_specification_p
13610 = saved_in_unbraced_linkage_specification_p;
13611 parser->num_template_parameter_lists
13612 = saved_num_template_parameter_lists;
13613
13614 return fn;
13615}
13616
13617/* Parse a template-declaration, assuming that the `export' (and
13618 `extern') keywords, if present, has already been scanned. MEMBER_P
13619 is as for cp_parser_template_declaration. */
13620
13621static void
94edc4ab 13622cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13623{
13624 tree decl = NULL_TREE;
13625 tree parameter_list;
13626 bool friend_p = false;
13627
13628 /* Look for the `template' keyword. */
13629 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13630 return;
13631
13632 /* And the `<'. */
13633 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13634 return;
13635
13636 /* Parse the template parameters. */
13637 begin_template_parm_list ();
13638 /* If the next token is `>', then we have an invalid
13639 specialization. Rather than complain about an invalid template
13640 parameter, issue an error message here. */
13641 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13642 {
13643 cp_parser_error (parser, "invalid explicit specialization");
13644 parameter_list = NULL_TREE;
13645 }
13646 else
13647 parameter_list = cp_parser_template_parameter_list (parser);
13648 parameter_list = end_template_parm_list (parameter_list);
13649 /* Look for the `>'. */
13650 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13651 /* We just processed one more parameter list. */
13652 ++parser->num_template_parameter_lists;
13653 /* If the next token is `template', there are more template
13654 parameters. */
13655 if (cp_lexer_next_token_is_keyword (parser->lexer,
13656 RID_TEMPLATE))
13657 cp_parser_template_declaration_after_export (parser, member_p);
13658 else
13659 {
13660 decl = cp_parser_single_declaration (parser,
13661 member_p,
13662 &friend_p);
13663
13664 /* If this is a member template declaration, let the front
13665 end know. */
13666 if (member_p && !friend_p && decl)
37d407a1
KL
13667 {
13668 if (TREE_CODE (decl) == TYPE_DECL)
13669 cp_parser_check_access_in_redeclaration (decl);
13670
13671 decl = finish_member_template_decl (decl);
13672 }
a723baf1 13673 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
13674 make_friend_class (current_class_type, TREE_TYPE (decl),
13675 /*complain=*/true);
a723baf1
MM
13676 }
13677 /* We are done with the current parameter list. */
13678 --parser->num_template_parameter_lists;
13679
13680 /* Finish up. */
13681 finish_template_decl (parameter_list);
13682
13683 /* Register member declarations. */
13684 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13685 finish_member_declaration (decl);
13686
13687 /* If DECL is a function template, we must return to parse it later.
13688 (Even though there is no definition, there might be default
13689 arguments that need handling.) */
13690 if (member_p && decl
13691 && (TREE_CODE (decl) == FUNCTION_DECL
13692 || DECL_FUNCTION_TEMPLATE_P (decl)))
13693 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 13694 = tree_cons (NULL_TREE, decl,
a723baf1
MM
13695 TREE_VALUE (parser->unparsed_functions_queues));
13696}
13697
13698/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13699 `function-definition' sequence. MEMBER_P is true, this declaration
13700 appears in a class scope.
13701
13702 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13703 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13704
13705static tree
94edc4ab
NN
13706cp_parser_single_declaration (cp_parser* parser,
13707 bool member_p,
13708 bool* friend_p)
a723baf1 13709{
560ad596 13710 int declares_class_or_enum;
a723baf1
MM
13711 tree decl = NULL_TREE;
13712 tree decl_specifiers;
13713 tree attributes;
a723baf1
MM
13714
13715 /* Parse the dependent declaration. We don't know yet
13716 whether it will be a function-definition. */
13717 cp_parser_parse_tentatively (parser);
13718 /* Defer access checks until we know what is being declared. */
8d241e0b 13719 push_deferring_access_checks (dk_deferred);
cf22909c 13720
a723baf1
MM
13721 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13722 alternative. */
13723 decl_specifiers
13724 = cp_parser_decl_specifier_seq (parser,
13725 CP_PARSER_FLAGS_OPTIONAL,
13726 &attributes,
13727 &declares_class_or_enum);
13728 /* Gather up the access checks that occurred the
13729 decl-specifier-seq. */
cf22909c
KL
13730 stop_deferring_access_checks ();
13731
a723baf1
MM
13732 /* Check for the declaration of a template class. */
13733 if (declares_class_or_enum)
13734 {
13735 if (cp_parser_declares_only_class_p (parser))
13736 {
13737 decl = shadow_tag (decl_specifiers);
13738 if (decl)
13739 decl = TYPE_NAME (decl);
13740 else
13741 decl = error_mark_node;
13742 }
13743 }
13744 else
13745 decl = NULL_TREE;
13746 /* If it's not a template class, try for a template function. If
13747 the next token is a `;', then this declaration does not declare
13748 anything. But, if there were errors in the decl-specifiers, then
13749 the error might well have come from an attempted class-specifier.
13750 In that case, there's no need to warn about a missing declarator. */
13751 if (!decl
13752 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13753 || !value_member (error_mark_node, decl_specifiers)))
13754 decl = cp_parser_init_declarator (parser,
13755 decl_specifiers,
13756 attributes,
a723baf1
MM
13757 /*function_definition_allowed_p=*/false,
13758 member_p,
560ad596 13759 declares_class_or_enum,
a723baf1 13760 /*function_definition_p=*/NULL);
cf22909c
KL
13761
13762 pop_deferring_access_checks ();
13763
a723baf1
MM
13764 /* Clear any current qualification; whatever comes next is the start
13765 of something new. */
13766 parser->scope = NULL_TREE;
13767 parser->qualifying_scope = NULL_TREE;
13768 parser->object_scope = NULL_TREE;
13769 /* Look for a trailing `;' after the declaration. */
8a6393df 13770 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
a723baf1
MM
13771 && cp_parser_committed_to_tentative_parse (parser))
13772 cp_parser_skip_to_end_of_block_or_statement (parser);
13773 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13774 if (cp_parser_parse_definitely (parser))
13775 {
13776 if (friend_p)
13777 *friend_p = cp_parser_friend_p (decl_specifiers);
13778 }
13779 /* Otherwise, try a function-definition. */
13780 else
13781 decl = cp_parser_function_definition (parser, friend_p);
13782
13783 return decl;
13784}
13785
d6b4ea85
MM
13786/* Parse a cast-expression that is not the operand of a unary "&". */
13787
13788static tree
13789cp_parser_simple_cast_expression (cp_parser *parser)
13790{
13791 return cp_parser_cast_expression (parser, /*address_p=*/false);
13792}
13793
a723baf1
MM
13794/* Parse a functional cast to TYPE. Returns an expression
13795 representing the cast. */
13796
13797static tree
94edc4ab 13798cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
13799{
13800 tree expression_list;
13801
39703eb9
MM
13802 expression_list
13803 = cp_parser_parenthesized_expression_list (parser, false,
13804 /*non_constant_p=*/NULL);
a723baf1
MM
13805
13806 return build_functional_cast (type, expression_list);
13807}
13808
13809/* MEMBER_FUNCTION is a member function, or a friend. If default
13810 arguments, or the body of the function have not yet been parsed,
13811 parse them now. */
13812
13813static void
94edc4ab 13814cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
13815{
13816 cp_lexer *saved_lexer;
13817
13818 /* If this member is a template, get the underlying
13819 FUNCTION_DECL. */
13820 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13821 member_function = DECL_TEMPLATE_RESULT (member_function);
13822
13823 /* There should not be any class definitions in progress at this
13824 point; the bodies of members are only parsed outside of all class
13825 definitions. */
13826 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13827 /* While we're parsing the member functions we might encounter more
13828 classes. We want to handle them right away, but we don't want
13829 them getting mixed up with functions that are currently in the
13830 queue. */
13831 parser->unparsed_functions_queues
13832 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13833
13834 /* Make sure that any template parameters are in scope. */
13835 maybe_begin_member_template_processing (member_function);
13836
a723baf1
MM
13837 /* If the body of the function has not yet been parsed, parse it
13838 now. */
13839 if (DECL_PENDING_INLINE_P (member_function))
13840 {
13841 tree function_scope;
13842 cp_token_cache *tokens;
13843
13844 /* The function is no longer pending; we are processing it. */
13845 tokens = DECL_PENDING_INLINE_INFO (member_function);
13846 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13847 DECL_PENDING_INLINE_P (member_function) = 0;
13848 /* If this was an inline function in a local class, enter the scope
13849 of the containing function. */
13850 function_scope = decl_function_context (member_function);
13851 if (function_scope)
13852 push_function_context_to (function_scope);
13853
13854 /* Save away the current lexer. */
13855 saved_lexer = parser->lexer;
13856 /* Make a new lexer to feed us the tokens saved for this function. */
13857 parser->lexer = cp_lexer_new_from_tokens (tokens);
13858 parser->lexer->next = saved_lexer;
13859
13860 /* Set the current source position to be the location of the first
13861 token in the saved inline body. */
3466b292 13862 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13863
13864 /* Let the front end know that we going to be defining this
13865 function. */
13866 start_function (NULL_TREE, member_function, NULL_TREE,
13867 SF_PRE_PARSED | SF_INCLASS_INLINE);
13868
13869 /* Now, parse the body of the function. */
13870 cp_parser_function_definition_after_declarator (parser,
13871 /*inline_p=*/true);
13872
13873 /* Leave the scope of the containing function. */
13874 if (function_scope)
13875 pop_function_context_from (function_scope);
13876 /* Restore the lexer. */
13877 parser->lexer = saved_lexer;
13878 }
13879
13880 /* Remove any template parameters from the symbol table. */
13881 maybe_end_member_template_processing ();
13882
13883 /* Restore the queue. */
13884 parser->unparsed_functions_queues
13885 = TREE_CHAIN (parser->unparsed_functions_queues);
13886}
13887
cd0be382 13888/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
13889 functions queue. */
13890
13891static void
13892cp_parser_save_default_args (cp_parser* parser, tree decl)
13893{
13894 tree probe;
13895
13896 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13897 probe;
13898 probe = TREE_CHAIN (probe))
13899 if (TREE_PURPOSE (probe))
13900 {
13901 TREE_PURPOSE (parser->unparsed_functions_queues)
13902 = tree_cons (NULL_TREE, decl,
13903 TREE_PURPOSE (parser->unparsed_functions_queues));
13904 break;
13905 }
13906 return;
13907}
13908
8218bd34
MM
13909/* FN is a FUNCTION_DECL which may contains a parameter with an
13910 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
13911
13912static void
8218bd34 13913cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
13914{
13915 cp_lexer *saved_lexer;
13916 cp_token_cache *tokens;
13917 bool saved_local_variables_forbidden_p;
13918 tree parameters;
8218bd34 13919
b92bc2a0
NS
13920 /* While we're parsing the default args, we might (due to the
13921 statement expression extension) encounter more classes. We want
13922 to handle them right away, but we don't want them getting mixed
13923 up with default args that are currently in the queue. */
13924 parser->unparsed_functions_queues
13925 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13926
8218bd34 13927 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
13928 parameters;
13929 parameters = TREE_CHAIN (parameters))
13930 {
13931 if (!TREE_PURPOSE (parameters)
13932 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13933 continue;
13934
13935 /* Save away the current lexer. */
13936 saved_lexer = parser->lexer;
13937 /* Create a new one, using the tokens we have saved. */
13938 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13939 parser->lexer = cp_lexer_new_from_tokens (tokens);
13940
13941 /* Set the current source position to be the location of the
13942 first token in the default argument. */
3466b292 13943 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13944
13945 /* Local variable names (and the `this' keyword) may not appear
13946 in a default argument. */
13947 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13948 parser->local_variables_forbidden_p = true;
13949 /* Parse the assignment-expression. */
8218bd34 13950 if (DECL_CONTEXT (fn))
14d22dd6 13951 push_nested_class (DECL_CONTEXT (fn));
a723baf1 13952 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 13953 if (DECL_CONTEXT (fn))
e5976695 13954 pop_nested_class ();
a723baf1
MM
13955
13956 /* Restore saved state. */
13957 parser->lexer = saved_lexer;
13958 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13959 }
b92bc2a0
NS
13960
13961 /* Restore the queue. */
13962 parser->unparsed_functions_queues
13963 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
13964}
13965
13966/* Parse the operand of `sizeof' (or a similar operator). Returns
13967 either a TYPE or an expression, depending on the form of the
13968 input. The KEYWORD indicates which kind of expression we have
13969 encountered. */
13970
13971static tree
94edc4ab 13972cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
13973{
13974 static const char *format;
13975 tree expr = NULL_TREE;
13976 const char *saved_message;
13977 bool saved_constant_expression_p;
13978
13979 /* Initialize FORMAT the first time we get here. */
13980 if (!format)
13981 format = "types may not be defined in `%s' expressions";
13982
13983 /* Types cannot be defined in a `sizeof' expression. Save away the
13984 old message. */
13985 saved_message = parser->type_definition_forbidden_message;
13986 /* And create the new one. */
13987 parser->type_definition_forbidden_message
c68b0a84
KG
13988 = xmalloc (strlen (format)
13989 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13990 + 1 /* `\0' */);
a723baf1
MM
13991 sprintf ((char *) parser->type_definition_forbidden_message,
13992 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13993
13994 /* The restrictions on constant-expressions do not apply inside
13995 sizeof expressions. */
13996 saved_constant_expression_p = parser->constant_expression_p;
13997 parser->constant_expression_p = false;
13998
3beb3abf
MM
13999 /* Do not actually evaluate the expression. */
14000 ++skip_evaluation;
a723baf1
MM
14001 /* If it's a `(', then we might be looking at the type-id
14002 construction. */
14003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14004 {
14005 tree type;
14006
14007 /* We can't be sure yet whether we're looking at a type-id or an
14008 expression. */
14009 cp_parser_parse_tentatively (parser);
14010 /* Consume the `('. */
14011 cp_lexer_consume_token (parser->lexer);
14012 /* Parse the type-id. */
14013 type = cp_parser_type_id (parser);
14014 /* Now, look for the trailing `)'. */
14015 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14016 /* If all went well, then we're done. */
14017 if (cp_parser_parse_definitely (parser))
14018 {
14019 /* Build a list of decl-specifiers; right now, we have only
14020 a single type-specifier. */
14021 type = build_tree_list (NULL_TREE,
14022 type);
14023
14024 /* Call grokdeclarator to figure out what type this is. */
14025 expr = grokdeclarator (NULL_TREE,
14026 type,
14027 TYPENAME,
14028 /*initialized=*/0,
14029 /*attrlist=*/NULL);
14030 }
14031 }
14032
14033 /* If the type-id production did not work out, then we must be
14034 looking at the unary-expression production. */
14035 if (!expr)
14036 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14037 /* Go back to evaluating expressions. */
14038 --skip_evaluation;
a723baf1
MM
14039
14040 /* Free the message we created. */
14041 free ((char *) parser->type_definition_forbidden_message);
14042 /* And restore the old one. */
14043 parser->type_definition_forbidden_message = saved_message;
14044 parser->constant_expression_p = saved_constant_expression_p;
14045
14046 return expr;
14047}
14048
14049/* If the current declaration has no declarator, return true. */
14050
14051static bool
14052cp_parser_declares_only_class_p (cp_parser *parser)
14053{
14054 /* If the next token is a `;' or a `,' then there is no
14055 declarator. */
14056 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14057 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14058}
14059
d17811fd
MM
14060/* Simplify EXPR if it is a non-dependent expression. Returns the
14061 (possibly simplified) expression. */
14062
14063static tree
14064cp_parser_fold_non_dependent_expr (tree expr)
14065{
14066 /* If we're in a template, but EXPR isn't value dependent, simplify
14067 it. We're supposed to treat:
14068
14069 template <typename T> void f(T[1 + 1]);
14070 template <typename T> void f(T[2]);
14071
14072 as two declarations of the same function, for example. */
14073 if (processing_template_decl
14074 && !type_dependent_expression_p (expr)
14075 && !value_dependent_expression_p (expr))
14076 {
14077 HOST_WIDE_INT saved_processing_template_decl;
14078
14079 saved_processing_template_decl = processing_template_decl;
14080 processing_template_decl = 0;
14081 expr = tsubst_copy_and_build (expr,
14082 /*args=*/NULL_TREE,
14083 tf_error,
b3445994
MM
14084 /*in_decl=*/NULL_TREE,
14085 /*function_p=*/false);
d17811fd
MM
14086 processing_template_decl = saved_processing_template_decl;
14087 }
14088 return expr;
14089}
14090
a723baf1
MM
14091/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14092 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14093
14094static bool
94edc4ab 14095cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14096{
14097 while (decl_specifiers)
14098 {
14099 /* See if this decl-specifier is `friend'. */
14100 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14101 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14102 return true;
14103
14104 /* Go on to the next decl-specifier. */
14105 decl_specifiers = TREE_CHAIN (decl_specifiers);
14106 }
14107
14108 return false;
14109}
14110
14111/* If the next token is of the indicated TYPE, consume it. Otherwise,
14112 issue an error message indicating that TOKEN_DESC was expected.
14113
14114 Returns the token consumed, if the token had the appropriate type.
14115 Otherwise, returns NULL. */
14116
14117static cp_token *
94edc4ab
NN
14118cp_parser_require (cp_parser* parser,
14119 enum cpp_ttype type,
14120 const char* token_desc)
a723baf1
MM
14121{
14122 if (cp_lexer_next_token_is (parser->lexer, type))
14123 return cp_lexer_consume_token (parser->lexer);
14124 else
14125 {
e5976695
MM
14126 /* Output the MESSAGE -- unless we're parsing tentatively. */
14127 if (!cp_parser_simulate_error (parser))
14128 error ("expected %s", token_desc);
a723baf1
MM
14129 return NULL;
14130 }
14131}
14132
14133/* Like cp_parser_require, except that tokens will be skipped until
14134 the desired token is found. An error message is still produced if
14135 the next token is not as expected. */
14136
14137static void
94edc4ab
NN
14138cp_parser_skip_until_found (cp_parser* parser,
14139 enum cpp_ttype type,
14140 const char* token_desc)
a723baf1
MM
14141{
14142 cp_token *token;
14143 unsigned nesting_depth = 0;
14144
14145 if (cp_parser_require (parser, type, token_desc))
14146 return;
14147
14148 /* Skip tokens until the desired token is found. */
14149 while (true)
14150 {
14151 /* Peek at the next token. */
14152 token = cp_lexer_peek_token (parser->lexer);
14153 /* If we've reached the token we want, consume it and
14154 stop. */
14155 if (token->type == type && !nesting_depth)
14156 {
14157 cp_lexer_consume_token (parser->lexer);
14158 return;
14159 }
14160 /* If we've run out of tokens, stop. */
14161 if (token->type == CPP_EOF)
14162 return;
14163 if (token->type == CPP_OPEN_BRACE
14164 || token->type == CPP_OPEN_PAREN
14165 || token->type == CPP_OPEN_SQUARE)
14166 ++nesting_depth;
14167 else if (token->type == CPP_CLOSE_BRACE
14168 || token->type == CPP_CLOSE_PAREN
14169 || token->type == CPP_CLOSE_SQUARE)
14170 {
14171 if (nesting_depth-- == 0)
14172 return;
14173 }
14174 /* Consume this token. */
14175 cp_lexer_consume_token (parser->lexer);
14176 }
14177}
14178
14179/* If the next token is the indicated keyword, consume it. Otherwise,
14180 issue an error message indicating that TOKEN_DESC was expected.
14181
14182 Returns the token consumed, if the token had the appropriate type.
14183 Otherwise, returns NULL. */
14184
14185static cp_token *
94edc4ab
NN
14186cp_parser_require_keyword (cp_parser* parser,
14187 enum rid keyword,
14188 const char* token_desc)
a723baf1
MM
14189{
14190 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14191
14192 if (token && token->keyword != keyword)
14193 {
14194 dyn_string_t error_msg;
14195
14196 /* Format the error message. */
14197 error_msg = dyn_string_new (0);
14198 dyn_string_append_cstr (error_msg, "expected ");
14199 dyn_string_append_cstr (error_msg, token_desc);
14200 cp_parser_error (parser, error_msg->s);
14201 dyn_string_delete (error_msg);
14202 return NULL;
14203 }
14204
14205 return token;
14206}
14207
14208/* Returns TRUE iff TOKEN is a token that can begin the body of a
14209 function-definition. */
14210
14211static bool
94edc4ab 14212cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14213{
14214 return (/* An ordinary function-body begins with an `{'. */
14215 token->type == CPP_OPEN_BRACE
14216 /* A ctor-initializer begins with a `:'. */
14217 || token->type == CPP_COLON
14218 /* A function-try-block begins with `try'. */
14219 || token->keyword == RID_TRY
14220 /* The named return value extension begins with `return'. */
14221 || token->keyword == RID_RETURN);
14222}
14223
14224/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14225 definition. */
14226
14227static bool
14228cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14229{
14230 cp_token *token;
14231
14232 token = cp_lexer_peek_token (parser->lexer);
14233 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14234}
14235
d17811fd
MM
14236/* Returns TRUE iff the next token is the "," or ">" ending a
14237 template-argument. */
14238
14239static bool
14240cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14241{
14242 cp_token *token;
14243
14244 token = cp_lexer_peek_token (parser->lexer);
14245 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14246}
14247
a723baf1
MM
14248/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14249 or none_type otherwise. */
14250
14251static enum tag_types
94edc4ab 14252cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14253{
14254 switch (token->keyword)
14255 {
14256 case RID_CLASS:
14257 return class_type;
14258 case RID_STRUCT:
14259 return record_type;
14260 case RID_UNION:
14261 return union_type;
14262
14263 default:
14264 return none_type;
14265 }
14266}
14267
14268/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14269
14270static void
14271cp_parser_check_class_key (enum tag_types class_key, tree type)
14272{
14273 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14274 pedwarn ("`%s' tag used in naming `%#T'",
14275 class_key == union_type ? "union"
14276 : class_key == record_type ? "struct" : "class",
14277 type);
14278}
14279
cd0be382 14280/* Issue an error message if DECL is redeclared with different
37d407a1
KL
14281 access than its original declaration [class.access.spec/3].
14282 This applies to nested classes and nested class templates.
14283 [class.mem/1]. */
14284
14285static void cp_parser_check_access_in_redeclaration (tree decl)
14286{
14287 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14288 return;
14289
14290 if ((TREE_PRIVATE (decl)
14291 != (current_access_specifier == access_private_node))
14292 || (TREE_PROTECTED (decl)
14293 != (current_access_specifier == access_protected_node)))
14294 error ("%D redeclared with different access", decl);
14295}
14296
a723baf1
MM
14297/* Look for the `template' keyword, as a syntactic disambiguator.
14298 Return TRUE iff it is present, in which case it will be
14299 consumed. */
14300
14301static bool
14302cp_parser_optional_template_keyword (cp_parser *parser)
14303{
14304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14305 {
14306 /* The `template' keyword can only be used within templates;
14307 outside templates the parser can always figure out what is a
14308 template and what is not. */
14309 if (!processing_template_decl)
14310 {
14311 error ("`template' (as a disambiguator) is only allowed "
14312 "within templates");
14313 /* If this part of the token stream is rescanned, the same
14314 error message would be generated. So, we purge the token
14315 from the stream. */
14316 cp_lexer_purge_token (parser->lexer);
14317 return false;
14318 }
14319 else
14320 {
14321 /* Consume the `template' keyword. */
14322 cp_lexer_consume_token (parser->lexer);
14323 return true;
14324 }
14325 }
14326
14327 return false;
14328}
14329
2050a1bb
MM
14330/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14331 set PARSER->SCOPE, and perform other related actions. */
14332
14333static void
14334cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14335{
14336 tree value;
14337 tree check;
14338
14339 /* Get the stored value. */
14340 value = cp_lexer_consume_token (parser->lexer)->value;
14341 /* Perform any access checks that were deferred. */
14342 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14343 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14344 /* Set the scope from the stored value. */
14345 parser->scope = TREE_VALUE (value);
14346 parser->qualifying_scope = TREE_TYPE (value);
14347 parser->object_scope = NULL_TREE;
14348}
14349
a723baf1
MM
14350/* Add tokens to CACHE until an non-nested END token appears. */
14351
14352static void
14353cp_parser_cache_group (cp_parser *parser,
14354 cp_token_cache *cache,
14355 enum cpp_ttype end,
14356 unsigned depth)
14357{
14358 while (true)
14359 {
14360 cp_token *token;
14361
14362 /* Abort a parenthesized expression if we encounter a brace. */
14363 if ((end == CPP_CLOSE_PAREN || depth == 0)
14364 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14365 return;
14366 /* Consume the next token. */
14367 token = cp_lexer_consume_token (parser->lexer);
14368 /* If we've reached the end of the file, stop. */
14369 if (token->type == CPP_EOF)
14370 return;
14371 /* Add this token to the tokens we are saving. */
14372 cp_token_cache_push_token (cache, token);
14373 /* See if it starts a new group. */
14374 if (token->type == CPP_OPEN_BRACE)
14375 {
14376 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14377 if (depth == 0)
14378 return;
14379 }
14380 else if (token->type == CPP_OPEN_PAREN)
14381 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14382 else if (token->type == end)
14383 return;
14384 }
14385}
14386
14387/* Begin parsing tentatively. We always save tokens while parsing
14388 tentatively so that if the tentative parsing fails we can restore the
14389 tokens. */
14390
14391static void
94edc4ab 14392cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14393{
14394 /* Enter a new parsing context. */
14395 parser->context = cp_parser_context_new (parser->context);
14396 /* Begin saving tokens. */
14397 cp_lexer_save_tokens (parser->lexer);
14398 /* In order to avoid repetitive access control error messages,
14399 access checks are queued up until we are no longer parsing
14400 tentatively. */
8d241e0b 14401 push_deferring_access_checks (dk_deferred);
a723baf1
MM
14402}
14403
14404/* Commit to the currently active tentative parse. */
14405
14406static void
94edc4ab 14407cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14408{
14409 cp_parser_context *context;
14410 cp_lexer *lexer;
14411
14412 /* Mark all of the levels as committed. */
14413 lexer = parser->lexer;
14414 for (context = parser->context; context->next; context = context->next)
14415 {
14416 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14417 break;
14418 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14419 while (!cp_lexer_saving_tokens (lexer))
14420 lexer = lexer->next;
14421 cp_lexer_commit_tokens (lexer);
14422 }
14423}
14424
14425/* Abort the currently active tentative parse. All consumed tokens
14426 will be rolled back, and no diagnostics will be issued. */
14427
14428static void
94edc4ab 14429cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14430{
14431 cp_parser_simulate_error (parser);
14432 /* Now, pretend that we want to see if the construct was
14433 successfully parsed. */
14434 cp_parser_parse_definitely (parser);
14435}
14436
34cd5ae7 14437/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
14438 token stream. Otherwise, commit to the tokens we have consumed.
14439 Returns true if no error occurred; false otherwise. */
14440
14441static bool
94edc4ab 14442cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14443{
14444 bool error_occurred;
14445 cp_parser_context *context;
14446
34cd5ae7 14447 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
14448 destroy that information. */
14449 error_occurred = cp_parser_error_occurred (parser);
14450 /* Remove the topmost context from the stack. */
14451 context = parser->context;
14452 parser->context = context->next;
14453 /* If no parse errors occurred, commit to the tentative parse. */
14454 if (!error_occurred)
14455 {
14456 /* Commit to the tokens read tentatively, unless that was
14457 already done. */
14458 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14459 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14460
14461 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14462 }
14463 /* Otherwise, if errors occurred, roll back our state so that things
14464 are just as they were before we began the tentative parse. */
14465 else
cf22909c
KL
14466 {
14467 cp_lexer_rollback_tokens (parser->lexer);
14468 pop_deferring_access_checks ();
14469 }
e5976695
MM
14470 /* Add the context to the front of the free list. */
14471 context->next = cp_parser_context_free_list;
14472 cp_parser_context_free_list = context;
14473
14474 return !error_occurred;
a723baf1
MM
14475}
14476
a723baf1
MM
14477/* Returns true if we are parsing tentatively -- but have decided that
14478 we will stick with this tentative parse, even if errors occur. */
14479
14480static bool
94edc4ab 14481cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14482{
14483 return (cp_parser_parsing_tentatively (parser)
14484 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14485}
14486
4de8668e 14487/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
14488 tentative parse. */
14489
14490static bool
94edc4ab 14491cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14492{
14493 return (cp_parser_parsing_tentatively (parser)
14494 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14495}
14496
4de8668e 14497/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
14498
14499static bool
94edc4ab 14500cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14501{
14502 return parser->allow_gnu_extensions_p;
14503}
14504
14505\f
14506
14507/* The parser. */
14508
14509static GTY (()) cp_parser *the_parser;
14510
14511/* External interface. */
14512
d1bd0ded 14513/* Parse one entire translation unit. */
a723baf1 14514
d1bd0ded
GK
14515void
14516c_parse_file (void)
a723baf1
MM
14517{
14518 bool error_occurred;
14519
14520 the_parser = cp_parser_new ();
78757caa
KL
14521 push_deferring_access_checks (flag_access_control
14522 ? dk_no_deferred : dk_no_check);
a723baf1
MM
14523 error_occurred = cp_parser_translation_unit (the_parser);
14524 the_parser = NULL;
a723baf1
MM
14525}
14526
14527/* Clean up after parsing the entire translation unit. */
14528
14529void
94edc4ab 14530free_parser_stacks (void)
a723baf1
MM
14531{
14532 /* Nothing to do. */
14533}
14534
14535/* This variable must be provided by every front end. */
14536
14537int yydebug;
14538
14539#include "gt-cp-parser.h"