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