]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
s390.c (legitimize_pic_address): Access local symbols relative to the GOT instead...
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
3beb3abf 2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
a723baf1
MM
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
f5adbb8d 5 This file is part of GCC.
a723baf1 6
f5adbb8d 7 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
f5adbb8d 12 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
f5adbb8d 18 along with GCC; see the file COPYING. If not, write to the Free
a723baf1
MM
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "dyn-string.h"
27#include "varray.h"
28#include "cpplib.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "c-pragma.h"
32#include "decl.h"
33#include "flags.h"
34#include "diagnostic.h"
a723baf1
MM
35#include "toplev.h"
36#include "output.h"
37
38\f
39/* The lexer. */
40
41/* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
58
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
61
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
65
66/* A C++ token. */
67
68typedef struct cp_token GTY (())
69{
70 /* The kind of token. */
71 enum cpp_ttype type;
72 /* The value associated with this token, if any. */
73 tree value;
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
76 enum rid keyword;
82a98427
NS
77 /* The location at which this token was found. */
78 location_t location;
a723baf1
MM
79} cp_token;
80
81/* The number of tokens in a single token block. */
82
83#define CP_TOKEN_BLOCK_NUM_TOKENS 32
84
85/* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
89 complete.)
90
91 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 92 variable-length array), is used due to constraints imposed by the
a723baf1
MM
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
95
96typedef struct cp_token_block GTY (())
97{
98 /* The tokens. */
99 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100 /* The number of tokens in this block. */
101 size_t num_tokens;
102 /* The next token block in the chain. */
103 struct cp_token_block *next;
104 /* The previous block in the chain. */
105 struct cp_token_block *prev;
106} cp_token_block;
107
108typedef struct cp_token_cache GTY (())
109{
110 /* The first block in the cache. NULL if there are no tokens in the
111 cache. */
112 cp_token_block *first;
113 /* The last block in the cache. NULL If there are no tokens in the
114 cache. */
115 cp_token_block *last;
116} cp_token_cache;
117
9bcb9aae 118/* Prototypes. */
a723baf1
MM
119
120static cp_token_cache *cp_token_cache_new
121 (void);
122static void cp_token_cache_push_token
123 (cp_token_cache *, cp_token *);
124
125/* Create a new cp_token_cache. */
126
127static cp_token_cache *
128cp_token_cache_new ()
129{
130 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
131}
132
133/* Add *TOKEN to *CACHE. */
134
135static void
136cp_token_cache_push_token (cp_token_cache *cache,
137 cp_token *token)
138{
139 cp_token_block *b = cache->last;
140
141 /* See if we need to allocate a new token block. */
142 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
143 {
144 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
145 b->prev = cache->last;
146 if (cache->last)
147 {
148 cache->last->next = b;
149 cache->last = b;
150 }
151 else
152 cache->first = cache->last = b;
153 }
154 /* Add this token to the current token block. */
155 b->tokens[b->num_tokens++] = *token;
156}
157
158/* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
160 it to the parser. */
161
162typedef struct cp_lexer GTY (())
163{
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token * GTY ((skip (""))) buffer_end;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token * GTY ((skip (""))) first_token;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token * GTY ((skip (""))) next_token;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token * GTY ((skip (""))) last_token;
180
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
185
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
190
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens;
195
196 /* The STRING_CST tokens encountered while processing the current
197 string literal. */
198 varray_type string_tokens;
199
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
202 bool main_lexer_p;
203
204 /* True if we should output debugging information. */
205 bool debugging_p;
206
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer *next;
209} cp_lexer;
210
211/* Prototypes. */
212
17211ab5 213static cp_lexer *cp_lexer_new_main
94edc4ab 214 (void);
a723baf1 215static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 216 (struct cp_token_cache *);
a723baf1 217static int cp_lexer_saving_tokens
94edc4ab 218 (const cp_lexer *);
a723baf1 219static cp_token *cp_lexer_next_token
94edc4ab
NN
220 (cp_lexer *, cp_token *);
221static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer *, cp_token *, cp_token *);
a723baf1 223static cp_token *cp_lexer_read_token
94edc4ab 224 (cp_lexer *);
a723baf1 225static void cp_lexer_maybe_grow_buffer
94edc4ab 226 (cp_lexer *);
a723baf1 227static void cp_lexer_get_preprocessor_token
94edc4ab 228 (cp_lexer *, cp_token *);
a723baf1 229static cp_token *cp_lexer_peek_token
94edc4ab 230 (cp_lexer *);
a723baf1 231static cp_token *cp_lexer_peek_nth_token
94edc4ab 232 (cp_lexer *, size_t);
f7b5ecd9 233static inline bool cp_lexer_next_token_is
94edc4ab 234 (cp_lexer *, enum cpp_ttype);
a723baf1 235static bool cp_lexer_next_token_is_not
94edc4ab 236 (cp_lexer *, enum cpp_ttype);
a723baf1 237static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
238 (cp_lexer *, enum rid);
239static cp_token *cp_lexer_consume_token
240 (cp_lexer *);
a723baf1
MM
241static void cp_lexer_purge_token
242 (cp_lexer *);
243static void cp_lexer_purge_tokens_after
244 (cp_lexer *, cp_token *);
245static void cp_lexer_save_tokens
94edc4ab 246 (cp_lexer *);
a723baf1 247static void cp_lexer_commit_tokens
94edc4ab 248 (cp_lexer *);
a723baf1 249static void cp_lexer_rollback_tokens
94edc4ab 250 (cp_lexer *);
f7b5ecd9 251static inline void cp_lexer_set_source_position_from_token
94edc4ab 252 (cp_lexer *, const cp_token *);
a723baf1 253static void cp_lexer_print_token
94edc4ab 254 (FILE *, cp_token *);
f7b5ecd9 255static inline bool cp_lexer_debugging_p
94edc4ab 256 (cp_lexer *);
a723baf1 257static void cp_lexer_start_debugging
94edc4ab 258 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 259static void cp_lexer_stop_debugging
94edc4ab 260 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
261
262/* Manifest constants. */
263
264#define CP_TOKEN_BUFFER_SIZE 5
265#define CP_SAVED_TOKENS_SIZE 5
266
267/* A token type for keywords, as opposed to ordinary identifiers. */
268#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269
270/* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
275
276/* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
282
283/* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
286
287/* Variables. */
288
289/* The stream to which debugging output should be written. */
290static FILE *cp_lexer_debug_stream;
291
17211ab5
GK
292/* Create a new main C++ lexer, the lexer that gets tokens from the
293 preprocessor. */
a723baf1
MM
294
295static cp_lexer *
17211ab5 296cp_lexer_new_main (void)
a723baf1
MM
297{
298 cp_lexer *lexer;
17211ab5
GK
299 cp_token first_token;
300
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL, &first_token);
305 cpp_get_callbacks (parse_in)->valid_pch = NULL;
a723baf1
MM
306
307 /* Allocate the memory. */
308 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
309
310 /* Create the circular buffer. */
311 lexer->buffer = ((cp_token *)
17211ab5 312 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
GK
347 /* Allocate the memory. */
348 lexer = (cp_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;
17211ab5 354 lexer->buffer = ((cp_token *) 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. */
524 new_buffer = ((cp_token *)
525 ggc_realloc (lexer->buffer,
526 2 * buffer_length * sizeof (cp_token)));
527
528 /* Because the buffer is circular, logically consecutive tokens
529 are not necessarily placed consecutively in memory.
530 Therefore, we must keep move the tokens that were before
531 FIRST_TOKEN to the second half of the newly allocated
532 buffer. */
533 num_tokens_to_copy = (lexer->first_token - old_buffer);
534 memcpy (new_buffer + buffer_length,
535 new_buffer,
536 num_tokens_to_copy * sizeof (cp_token));
537 /* Clear the rest of the buffer. We never look at this storage,
538 but the garbage collector may. */
539 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
540 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
541
542 /* Now recompute all of the buffer pointers. */
543 new_first_token
544 = new_buffer + (lexer->first_token - old_buffer);
545 if (lexer->next_token != NULL)
546 {
547 ptrdiff_t next_token_delta;
548
549 if (lexer->next_token > lexer->first_token)
550 next_token_delta = lexer->next_token - lexer->first_token;
551 else
552 next_token_delta =
553 buffer_length - (lexer->first_token - lexer->next_token);
554 lexer->next_token = new_first_token + next_token_delta;
555 }
556 lexer->last_token = new_first_token + buffer_length;
557 lexer->buffer = new_buffer;
558 lexer->buffer_end = new_buffer + buffer_length * 2;
559 lexer->first_token = new_first_token;
560 }
561}
562
563/* Store the next token from the preprocessor in *TOKEN. */
564
565static void
94edc4ab
NN
566cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
567 cp_token *token)
a723baf1
MM
568{
569 bool done;
570
571 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 572 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
573 {
574 token->type = CPP_EOF;
82a98427
NS
575 token->location.line = 0;
576 token->location.file = NULL;
a723baf1
MM
577 token->value = NULL_TREE;
578 token->keyword = RID_MAX;
579
580 return;
581 }
582
583 done = false;
584 /* Keep going until we get a token we like. */
585 while (!done)
586 {
587 /* Get a new token from the preprocessor. */
588 token->type = c_lex (&token->value);
589 /* Issue messages about tokens we cannot process. */
590 switch (token->type)
591 {
592 case CPP_ATSIGN:
593 case CPP_HASH:
594 case CPP_PASTE:
595 error ("invalid token");
596 break;
597
a723baf1
MM
598 default:
599 /* This is a good token, so we exit the loop. */
600 done = true;
601 break;
602 }
603 }
604 /* Now we've got our token. */
82a98427 605 token->location = input_location;
a723baf1
MM
606
607 /* Check to see if this token is a keyword. */
608 if (token->type == CPP_NAME
609 && C_IS_RESERVED_WORD (token->value))
610 {
611 /* Mark this token as a keyword. */
612 token->type = CPP_KEYWORD;
613 /* Record which keyword. */
614 token->keyword = C_RID_CODE (token->value);
615 /* Update the value. Some keywords are mapped to particular
616 entities, rather than simply having the value of the
617 corresponding IDENTIFIER_NODE. For example, `__const' is
618 mapped to `const'. */
619 token->value = ridpointers[token->keyword];
620 }
621 else
622 token->keyword = RID_MAX;
623}
624
625/* Return a pointer to the next token in the token stream, but do not
626 consume it. */
627
628static cp_token *
94edc4ab 629cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
630{
631 cp_token *token;
632
633 /* If there are no tokens, read one now. */
634 if (!lexer->next_token)
635 cp_lexer_read_token (lexer);
636
637 /* Provide debugging output. */
638 if (cp_lexer_debugging_p (lexer))
639 {
640 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
641 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
642 fprintf (cp_lexer_debug_stream, "\n");
643 }
644
645 token = lexer->next_token;
646 cp_lexer_set_source_position_from_token (lexer, token);
647 return token;
648}
649
650/* Return true if the next token has the indicated TYPE. */
651
652static bool
94edc4ab 653cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
654{
655 cp_token *token;
656
657 /* Peek at the next token. */
658 token = cp_lexer_peek_token (lexer);
659 /* Check to see if it has the indicated TYPE. */
660 return token->type == type;
661}
662
663/* Return true if the next token does not have the indicated TYPE. */
664
665static bool
94edc4ab 666cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
667{
668 return !cp_lexer_next_token_is (lexer, type);
669}
670
671/* Return true if the next token is the indicated KEYWORD. */
672
673static bool
94edc4ab 674cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
675{
676 cp_token *token;
677
678 /* Peek at the next token. */
679 token = cp_lexer_peek_token (lexer);
680 /* Check to see if it is the indicated keyword. */
681 return token->keyword == keyword;
682}
683
684/* Return a pointer to the Nth token in the token stream. If N is 1,
685 then this is precisely equivalent to cp_lexer_peek_token. */
686
687static cp_token *
94edc4ab 688cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
689{
690 cp_token *token;
691
692 /* N is 1-based, not zero-based. */
693 my_friendly_assert (n > 0, 20000224);
694
695 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
696 token = lexer->next_token;
697 /* If there are no tokens in the buffer, get one now. */
698 if (!token)
699 {
700 cp_lexer_read_token (lexer);
701 token = lexer->next_token;
702 }
703
704 /* Now, read tokens until we have enough. */
705 while (--n > 0)
706 {
707 /* Advance to the next token. */
708 token = cp_lexer_next_token (lexer, token);
709 /* If that's all the tokens we have, read a new one. */
710 if (token == lexer->last_token)
711 token = cp_lexer_read_token (lexer);
712 }
713
714 return token;
715}
716
717/* Consume the next token. The pointer returned is valid only until
718 another token is read. Callers should preserve copy the token
719 explicitly if they will need its value for a longer period of
720 time. */
721
722static cp_token *
94edc4ab 723cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
724{
725 cp_token *token;
726
727 /* If there are no tokens, read one now. */
728 if (!lexer->next_token)
729 cp_lexer_read_token (lexer);
730
731 /* Remember the token we'll be returning. */
732 token = lexer->next_token;
733
734 /* Increment NEXT_TOKEN. */
735 lexer->next_token = cp_lexer_next_token (lexer,
736 lexer->next_token);
737 /* Check to see if we're all out of tokens. */
738 if (lexer->next_token == lexer->last_token)
739 lexer->next_token = NULL;
740
741 /* If we're not saving tokens, then move FIRST_TOKEN too. */
742 if (!cp_lexer_saving_tokens (lexer))
743 {
744 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
745 if (!lexer->next_token)
746 lexer->first_token = NULL;
747 else
748 lexer->first_token = lexer->next_token;
749 }
750
751 /* Provide debugging output. */
752 if (cp_lexer_debugging_p (lexer))
753 {
754 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
755 cp_lexer_print_token (cp_lexer_debug_stream, token);
756 fprintf (cp_lexer_debug_stream, "\n");
757 }
758
759 return token;
760}
761
762/* Permanently remove the next token from the token stream. There
763 must be a valid next token already; this token never reads
764 additional tokens from the preprocessor. */
765
766static void
767cp_lexer_purge_token (cp_lexer *lexer)
768{
769 cp_token *token;
770 cp_token *next_token;
771
772 token = lexer->next_token;
773 while (true)
774 {
775 next_token = cp_lexer_next_token (lexer, token);
776 if (next_token == lexer->last_token)
777 break;
778 *token = *next_token;
779 token = next_token;
780 }
781
782 lexer->last_token = token;
783 /* The token purged may have been the only token remaining; if so,
784 clear NEXT_TOKEN. */
785 if (lexer->next_token == token)
786 lexer->next_token = NULL;
787}
788
789/* Permanently remove all tokens after TOKEN, up to, but not
790 including, the token that will be returned next by
791 cp_lexer_peek_token. */
792
793static void
794cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
795{
796 cp_token *peek;
797 cp_token *t1;
798 cp_token *t2;
799
800 if (lexer->next_token)
801 {
802 /* Copy the tokens that have not yet been read to the location
803 immediately following TOKEN. */
804 t1 = cp_lexer_next_token (lexer, token);
805 t2 = peek = cp_lexer_peek_token (lexer);
806 /* Move tokens into the vacant area between TOKEN and PEEK. */
807 while (t2 != lexer->last_token)
808 {
809 *t1 = *t2;
810 t1 = cp_lexer_next_token (lexer, t1);
811 t2 = cp_lexer_next_token (lexer, t2);
812 }
813 /* Now, the next available token is right after TOKEN. */
814 lexer->next_token = cp_lexer_next_token (lexer, token);
815 /* And the last token is wherever we ended up. */
816 lexer->last_token = t1;
817 }
818 else
819 {
820 /* There are no tokens in the buffer, so there is nothing to
821 copy. The last token in the buffer is TOKEN itself. */
822 lexer->last_token = cp_lexer_next_token (lexer, token);
823 }
824}
825
826/* Begin saving tokens. All tokens consumed after this point will be
827 preserved. */
828
829static void
94edc4ab 830cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
831{
832 /* Provide debugging output. */
833 if (cp_lexer_debugging_p (lexer))
834 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
835
836 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
837 restore the tokens if required. */
838 if (!lexer->next_token)
839 cp_lexer_read_token (lexer);
840
841 VARRAY_PUSH_INT (lexer->saved_tokens,
842 cp_lexer_token_difference (lexer,
843 lexer->first_token,
844 lexer->next_token));
845}
846
847/* Commit to the portion of the token stream most recently saved. */
848
849static void
94edc4ab 850cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
851{
852 /* Provide debugging output. */
853 if (cp_lexer_debugging_p (lexer))
854 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
855
856 VARRAY_POP (lexer->saved_tokens);
857}
858
859/* Return all tokens saved since the last call to cp_lexer_save_tokens
860 to the token stream. Stop saving tokens. */
861
862static void
94edc4ab 863cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
864{
865 size_t delta;
866
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer))
869 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
870
871 /* Find the token that was the NEXT_TOKEN when we started saving
872 tokens. */
873 delta = VARRAY_TOP_INT(lexer->saved_tokens);
874 /* Make it the next token again now. */
875 lexer->next_token = cp_lexer_advance_token (lexer,
876 lexer->first_token,
877 delta);
15d2cb19 878 /* It might be the case that there were no tokens when we started
a723baf1
MM
879 saving tokens, but that there are some tokens now. */
880 if (!lexer->next_token && lexer->first_token)
881 lexer->next_token = lexer->first_token;
882
883 /* Stop saving tokens. */
884 VARRAY_POP (lexer->saved_tokens);
885}
886
a723baf1
MM
887/* Print a representation of the TOKEN on the STREAM. */
888
889static void
94edc4ab 890cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
891{
892 const char *token_type = NULL;
893
894 /* Figure out what kind of token this is. */
895 switch (token->type)
896 {
897 case CPP_EQ:
898 token_type = "EQ";
899 break;
900
901 case CPP_COMMA:
902 token_type = "COMMA";
903 break;
904
905 case CPP_OPEN_PAREN:
906 token_type = "OPEN_PAREN";
907 break;
908
909 case CPP_CLOSE_PAREN:
910 token_type = "CLOSE_PAREN";
911 break;
912
913 case CPP_OPEN_BRACE:
914 token_type = "OPEN_BRACE";
915 break;
916
917 case CPP_CLOSE_BRACE:
918 token_type = "CLOSE_BRACE";
919 break;
920
921 case CPP_SEMICOLON:
922 token_type = "SEMICOLON";
923 break;
924
925 case CPP_NAME:
926 token_type = "NAME";
927 break;
928
929 case CPP_EOF:
930 token_type = "EOF";
931 break;
932
933 case CPP_KEYWORD:
934 token_type = "keyword";
935 break;
936
937 /* This is not a token that we know how to handle yet. */
938 default:
939 break;
940 }
941
942 /* If we have a name for the token, print it out. Otherwise, we
943 simply give the numeric code. */
944 if (token_type)
945 fprintf (stream, "%s", token_type);
946 else
947 fprintf (stream, "%d", token->type);
948 /* And, for an identifier, print the identifier name. */
949 if (token->type == CPP_NAME
950 /* Some keywords have a value that is not an IDENTIFIER_NODE.
951 For example, `struct' is mapped to an INTEGER_CST. */
952 || (token->type == CPP_KEYWORD
953 && TREE_CODE (token->value) == IDENTIFIER_NODE))
954 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
955}
956
a723baf1
MM
957/* Start emitting debugging information. */
958
959static void
94edc4ab 960cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
961{
962 ++lexer->debugging_p;
963}
964
965/* Stop emitting debugging information. */
966
967static void
94edc4ab 968cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
969{
970 --lexer->debugging_p;
971}
972
973\f
974/* The parser. */
975
976/* Overview
977 --------
978
979 A cp_parser parses the token stream as specified by the C++
980 grammar. Its job is purely parsing, not semantic analysis. For
981 example, the parser breaks the token stream into declarators,
982 expressions, statements, and other similar syntactic constructs.
983 It does not check that the types of the expressions on either side
984 of an assignment-statement are compatible, or that a function is
985 not declared with a parameter of type `void'.
986
987 The parser invokes routines elsewhere in the compiler to perform
988 semantic analysis and to build up the abstract syntax tree for the
989 code processed.
990
991 The parser (and the template instantiation code, which is, in a
992 way, a close relative of parsing) are the only parts of the
993 compiler that should be calling push_scope and pop_scope, or
994 related functions. The parser (and template instantiation code)
995 keeps track of what scope is presently active; everything else
996 should simply honor that. (The code that generates static
997 initializers may also need to set the scope, in order to check
998 access control correctly when emitting the initializers.)
999
1000 Methodology
1001 -----------
1002
1003 The parser is of the standard recursive-descent variety. Upcoming
1004 tokens in the token stream are examined in order to determine which
1005 production to use when parsing a non-terminal. Some C++ constructs
1006 require arbitrary look ahead to disambiguate. For example, it is
1007 impossible, in the general case, to tell whether a statement is an
1008 expression or declaration without scanning the entire statement.
1009 Therefore, the parser is capable of "parsing tentatively." When the
1010 parser is not sure what construct comes next, it enters this mode.
1011 Then, while we attempt to parse the construct, the parser queues up
1012 error messages, rather than issuing them immediately, and saves the
1013 tokens it consumes. If the construct is parsed successfully, the
1014 parser "commits", i.e., it issues any queued error messages and
1015 the tokens that were being preserved are permanently discarded.
1016 If, however, the construct is not parsed successfully, the parser
1017 rolls back its state completely so that it can resume parsing using
1018 a different alternative.
1019
1020 Future Improvements
1021 -------------------
1022
1023 The performance of the parser could probably be improved
1024 substantially. Some possible improvements include:
1025
1026 - The expression parser recurses through the various levels of
1027 precedence as specified in the grammar, rather than using an
1028 operator-precedence technique. Therefore, parsing a simple
1029 identifier requires multiple recursive calls.
1030
1031 - We could often eliminate the need to parse tentatively by
1032 looking ahead a little bit. In some places, this approach
1033 might not entirely eliminate the need to parse tentatively, but
1034 it might still speed up the average case. */
1035
1036/* Flags that are passed to some parsing functions. These values can
1037 be bitwise-ored together. */
1038
1039typedef enum cp_parser_flags
1040{
1041 /* No flags. */
1042 CP_PARSER_FLAGS_NONE = 0x0,
1043 /* The construct is optional. If it is not present, then no error
1044 should be issued. */
1045 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1046 /* When parsing a type-specifier, do not allow user-defined types. */
1047 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1048} cp_parser_flags;
1049
62b8a44e
NS
1050/* The different kinds of declarators we want to parse. */
1051
1052typedef enum cp_parser_declarator_kind
1053{
9bcb9aae 1054 /* We want an abstract declartor. */
62b8a44e
NS
1055 CP_PARSER_DECLARATOR_ABSTRACT,
1056 /* We want a named declarator. */
1057 CP_PARSER_DECLARATOR_NAMED,
712becab 1058 /* We don't mind, but the name must be an unqualified-id */
62b8a44e
NS
1059 CP_PARSER_DECLARATOR_EITHER
1060} cp_parser_declarator_kind;
1061
a723baf1
MM
1062/* A mapping from a token type to a corresponding tree node type. */
1063
1064typedef struct cp_parser_token_tree_map_node
1065{
1066 /* The token type. */
1067 enum cpp_ttype token_type;
1068 /* The corresponding tree code. */
1069 enum tree_code tree_type;
1070} cp_parser_token_tree_map_node;
1071
1072/* A complete map consists of several ordinary entries, followed by a
1073 terminator. The terminating entry has a token_type of CPP_EOF. */
1074
1075typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1076
1077/* The status of a tentative parse. */
1078
1079typedef enum cp_parser_status_kind
1080{
1081 /* No errors have occurred. */
1082 CP_PARSER_STATUS_KIND_NO_ERROR,
1083 /* An error has occurred. */
1084 CP_PARSER_STATUS_KIND_ERROR,
1085 /* We are committed to this tentative parse, whether or not an error
1086 has occurred. */
1087 CP_PARSER_STATUS_KIND_COMMITTED
1088} cp_parser_status_kind;
1089
1090/* Context that is saved and restored when parsing tentatively. */
1091
1092typedef struct cp_parser_context GTY (())
1093{
1094 /* If this is a tentative parsing context, the status of the
1095 tentative parse. */
1096 enum cp_parser_status_kind status;
1097 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1098 that are looked up in this context must be looked up both in the
1099 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1100 the context of the containing expression. */
1101 tree object_type;
a723baf1
MM
1102 /* The next parsing context in the stack. */
1103 struct cp_parser_context *next;
1104} cp_parser_context;
1105
1106/* Prototypes. */
1107
1108/* Constructors and destructors. */
1109
1110static cp_parser_context *cp_parser_context_new
94edc4ab 1111 (cp_parser_context *);
a723baf1 1112
e5976695
MM
1113/* Class variables. */
1114
92bc1323 1115static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1116
a723baf1
MM
1117/* Constructors and destructors. */
1118
1119/* Construct a new context. The context below this one on the stack
1120 is given by NEXT. */
1121
1122static cp_parser_context *
94edc4ab 1123cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1124{
1125 cp_parser_context *context;
1126
1127 /* Allocate the storage. */
e5976695
MM
1128 if (cp_parser_context_free_list != NULL)
1129 {
1130 /* Pull the first entry from the free list. */
1131 context = cp_parser_context_free_list;
1132 cp_parser_context_free_list = context->next;
1133 memset ((char *)context, 0, sizeof (*context));
1134 }
1135 else
1136 context = ((cp_parser_context *)
1137 ggc_alloc_cleared (sizeof (cp_parser_context)));
a723baf1
MM
1138 /* No errors have occurred yet in this context. */
1139 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1140 /* If this is not the bottomost context, copy information that we
1141 need from the previous context. */
1142 if (next)
1143 {
1144 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1145 expression, then we are parsing one in this context, too. */
1146 context->object_type = next->object_type;
a723baf1
MM
1147 /* Thread the stack. */
1148 context->next = next;
1149 }
1150
1151 return context;
1152}
1153
1154/* The cp_parser structure represents the C++ parser. */
1155
1156typedef struct cp_parser GTY(())
1157{
1158 /* The lexer from which we are obtaining tokens. */
1159 cp_lexer *lexer;
1160
1161 /* The scope in which names should be looked up. If NULL_TREE, then
1162 we look up names in the scope that is currently open in the
1163 source program. If non-NULL, this is either a TYPE or
1164 NAMESPACE_DECL for the scope in which we should look.
1165
1166 This value is not cleared automatically after a name is looked
1167 up, so we must be careful to clear it before starting a new look
1168 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1169 will look up `Z' in the scope of `X', rather than the current
1170 scope.) Unfortunately, it is difficult to tell when name lookup
1171 is complete, because we sometimes peek at a token, look it up,
1172 and then decide not to consume it. */
1173 tree scope;
1174
1175 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1176 last lookup took place. OBJECT_SCOPE is used if an expression
1177 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1178 respectively. QUALIFYING_SCOPE is used for an expression of the
1179 form "X::Y"; it refers to X. */
1180 tree object_scope;
1181 tree qualifying_scope;
1182
1183 /* A stack of parsing contexts. All but the bottom entry on the
1184 stack will be tentative contexts.
1185
1186 We parse tentatively in order to determine which construct is in
1187 use in some situations. For example, in order to determine
1188 whether a statement is an expression-statement or a
1189 declaration-statement we parse it tentatively as a
1190 declaration-statement. If that fails, we then reparse the same
1191 token stream as an expression-statement. */
1192 cp_parser_context *context;
1193
1194 /* True if we are parsing GNU C++. If this flag is not set, then
1195 GNU extensions are not recognized. */
1196 bool allow_gnu_extensions_p;
1197
1198 /* TRUE if the `>' token should be interpreted as the greater-than
1199 operator. FALSE if it is the end of a template-id or
1200 template-parameter-list. */
1201 bool greater_than_is_operator_p;
1202
1203 /* TRUE if default arguments are allowed within a parameter list
1204 that starts at this point. FALSE if only a gnu extension makes
1205 them permissable. */
1206 bool default_arg_ok_p;
1207
1208 /* TRUE if we are parsing an integral constant-expression. See
1209 [expr.const] for a precise definition. */
a723baf1
MM
1210 bool constant_expression_p;
1211
14d22dd6
MM
1212 /* TRUE if we are parsing an integral constant-expression -- but a
1213 non-constant expression should be permitted as well. This flag
1214 is used when parsing an array bound so that GNU variable-length
1215 arrays are tolerated. */
1216 bool allow_non_constant_expression_p;
1217
1218 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1219 been seen that makes the expression non-constant. */
1220 bool non_constant_expression_p;
1221
a723baf1
MM
1222 /* TRUE if local variable names and `this' are forbidden in the
1223 current context. */
1224 bool local_variables_forbidden_p;
1225
1226 /* TRUE if the declaration we are parsing is part of a
1227 linkage-specification of the form `extern string-literal
1228 declaration'. */
1229 bool in_unbraced_linkage_specification_p;
1230
1231 /* TRUE if we are presently parsing a declarator, after the
1232 direct-declarator. */
1233 bool in_declarator_p;
1234
1235 /* If non-NULL, then we are parsing a construct where new type
1236 definitions are not permitted. The string stored here will be
1237 issued as an error message if a type is defined. */
1238 const char *type_definition_forbidden_message;
1239
8db1028e
NS
1240 /* A list of lists. The outer list is a stack, used for member
1241 functions of local classes. At each level there are two sub-list,
1242 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1243 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1244 TREE_VALUE's. The functions are chained in reverse declaration
1245 order.
1246
1247 The TREE_PURPOSE sublist contains those functions with default
1248 arguments that need post processing, and the TREE_VALUE sublist
1249 contains those functions with definitions that need post
1250 processing.
1251
1252 These lists can only be processed once the outermost class being
9bcb9aae 1253 defined is complete. */
a723baf1
MM
1254 tree unparsed_functions_queues;
1255
1256 /* The number of classes whose definitions are currently in
1257 progress. */
1258 unsigned num_classes_being_defined;
1259
1260 /* The number of template parameter lists that apply directly to the
1261 current declaration. */
1262 unsigned num_template_parameter_lists;
1263} cp_parser;
1264
1265/* The type of a function that parses some kind of expression */
94edc4ab 1266typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1267
1268/* Prototypes. */
1269
1270/* Constructors and destructors. */
1271
1272static cp_parser *cp_parser_new
94edc4ab 1273 (void);
a723baf1
MM
1274
1275/* Routines to parse various constructs.
1276
1277 Those that return `tree' will return the error_mark_node (rather
1278 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1279 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1280 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1281 whether or not a parse error occurred, you should always use
1282 cp_parser_error_occurred. If the construct is optional (indicated
1283 either by an `_opt' in the name of the function that does the
1284 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1285 the construct is not present. */
1286
1287/* Lexical conventions [gram.lex] */
1288
1289static tree cp_parser_identifier
94edc4ab 1290 (cp_parser *);
a723baf1
MM
1291
1292/* Basic concepts [gram.basic] */
1293
1294static bool cp_parser_translation_unit
94edc4ab 1295 (cp_parser *);
a723baf1
MM
1296
1297/* Expressions [gram.expr] */
1298
1299static tree cp_parser_primary_expression
b3445994 1300 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1301static tree cp_parser_id_expression
94edc4ab 1302 (cp_parser *, bool, bool, bool *);
a723baf1 1303static tree cp_parser_unqualified_id
94edc4ab 1304 (cp_parser *, bool, bool);
a723baf1
MM
1305static tree cp_parser_nested_name_specifier_opt
1306 (cp_parser *, bool, bool, bool);
1307static tree cp_parser_nested_name_specifier
1308 (cp_parser *, bool, bool, bool);
1309static tree cp_parser_class_or_namespace_name
1310 (cp_parser *, bool, bool, bool, bool);
1311static tree cp_parser_postfix_expression
1312 (cp_parser *, bool);
7efa3e22 1313static tree cp_parser_parenthesized_expression_list
39703eb9 1314 (cp_parser *, bool, bool *);
a723baf1 1315static void cp_parser_pseudo_destructor_name
94edc4ab 1316 (cp_parser *, tree *, tree *);
a723baf1
MM
1317static tree cp_parser_unary_expression
1318 (cp_parser *, bool);
1319static enum tree_code cp_parser_unary_operator
94edc4ab 1320 (cp_token *);
a723baf1 1321static tree cp_parser_new_expression
94edc4ab 1322 (cp_parser *);
a723baf1 1323static tree cp_parser_new_placement
94edc4ab 1324 (cp_parser *);
a723baf1 1325static tree cp_parser_new_type_id
94edc4ab 1326 (cp_parser *);
a723baf1 1327static tree cp_parser_new_declarator_opt
94edc4ab 1328 (cp_parser *);
a723baf1 1329static tree cp_parser_direct_new_declarator
94edc4ab 1330 (cp_parser *);
a723baf1 1331static tree cp_parser_new_initializer
94edc4ab 1332 (cp_parser *);
a723baf1 1333static tree cp_parser_delete_expression
94edc4ab 1334 (cp_parser *);
a723baf1
MM
1335static tree cp_parser_cast_expression
1336 (cp_parser *, bool);
1337static tree cp_parser_pm_expression
94edc4ab 1338 (cp_parser *);
a723baf1 1339static tree cp_parser_multiplicative_expression
94edc4ab 1340 (cp_parser *);
a723baf1 1341static tree cp_parser_additive_expression
94edc4ab 1342 (cp_parser *);
a723baf1 1343static tree cp_parser_shift_expression
94edc4ab 1344 (cp_parser *);
a723baf1 1345static tree cp_parser_relational_expression
94edc4ab 1346 (cp_parser *);
a723baf1 1347static tree cp_parser_equality_expression
94edc4ab 1348 (cp_parser *);
a723baf1 1349static tree cp_parser_and_expression
94edc4ab 1350 (cp_parser *);
a723baf1 1351static tree cp_parser_exclusive_or_expression
94edc4ab 1352 (cp_parser *);
a723baf1 1353static tree cp_parser_inclusive_or_expression
94edc4ab 1354 (cp_parser *);
a723baf1 1355static tree cp_parser_logical_and_expression
94edc4ab 1356 (cp_parser *);
a723baf1 1357static tree cp_parser_logical_or_expression
94edc4ab 1358 (cp_parser *);
a723baf1 1359static tree cp_parser_question_colon_clause
94edc4ab 1360 (cp_parser *, tree);
a723baf1 1361static tree cp_parser_assignment_expression
94edc4ab 1362 (cp_parser *);
a723baf1 1363static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1364 (cp_parser *);
a723baf1 1365static tree cp_parser_expression
94edc4ab 1366 (cp_parser *);
a723baf1 1367static tree cp_parser_constant_expression
14d22dd6 1368 (cp_parser *, bool, bool *);
a723baf1
MM
1369
1370/* Statements [gram.stmt.stmt] */
1371
1372static void cp_parser_statement
94edc4ab 1373 (cp_parser *);
a723baf1 1374static tree cp_parser_labeled_statement
94edc4ab 1375 (cp_parser *);
a723baf1 1376static tree cp_parser_expression_statement
94edc4ab 1377 (cp_parser *);
a723baf1
MM
1378static tree cp_parser_compound_statement
1379 (cp_parser *);
1380static void cp_parser_statement_seq_opt
94edc4ab 1381 (cp_parser *);
a723baf1 1382static tree cp_parser_selection_statement
94edc4ab 1383 (cp_parser *);
a723baf1 1384static tree cp_parser_condition
94edc4ab 1385 (cp_parser *);
a723baf1 1386static tree cp_parser_iteration_statement
94edc4ab 1387 (cp_parser *);
a723baf1 1388static void cp_parser_for_init_statement
94edc4ab 1389 (cp_parser *);
a723baf1 1390static tree cp_parser_jump_statement
94edc4ab 1391 (cp_parser *);
a723baf1 1392static void cp_parser_declaration_statement
94edc4ab 1393 (cp_parser *);
a723baf1
MM
1394
1395static tree cp_parser_implicitly_scoped_statement
94edc4ab 1396 (cp_parser *);
a723baf1 1397static void cp_parser_already_scoped_statement
94edc4ab 1398 (cp_parser *);
a723baf1
MM
1399
1400/* Declarations [gram.dcl.dcl] */
1401
1402static void cp_parser_declaration_seq_opt
94edc4ab 1403 (cp_parser *);
a723baf1 1404static void cp_parser_declaration
94edc4ab 1405 (cp_parser *);
a723baf1 1406static void cp_parser_block_declaration
94edc4ab 1407 (cp_parser *, bool);
a723baf1 1408static void cp_parser_simple_declaration
94edc4ab 1409 (cp_parser *, bool);
a723baf1 1410static tree cp_parser_decl_specifier_seq
94edc4ab 1411 (cp_parser *, cp_parser_flags, tree *, bool *);
a723baf1 1412static tree cp_parser_storage_class_specifier_opt
94edc4ab 1413 (cp_parser *);
a723baf1 1414static tree cp_parser_function_specifier_opt
94edc4ab 1415 (cp_parser *);
a723baf1 1416static tree cp_parser_type_specifier
94edc4ab 1417 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
a723baf1 1418static tree cp_parser_simple_type_specifier
94edc4ab 1419 (cp_parser *, cp_parser_flags);
a723baf1 1420static tree cp_parser_type_name
94edc4ab 1421 (cp_parser *);
a723baf1 1422static tree cp_parser_elaborated_type_specifier
94edc4ab 1423 (cp_parser *, bool, bool);
a723baf1 1424static tree cp_parser_enum_specifier
94edc4ab 1425 (cp_parser *);
a723baf1 1426static void cp_parser_enumerator_list
94edc4ab 1427 (cp_parser *, tree);
a723baf1 1428static void cp_parser_enumerator_definition
94edc4ab 1429 (cp_parser *, tree);
a723baf1 1430static tree cp_parser_namespace_name
94edc4ab 1431 (cp_parser *);
a723baf1 1432static void cp_parser_namespace_definition
94edc4ab 1433 (cp_parser *);
a723baf1 1434static void cp_parser_namespace_body
94edc4ab 1435 (cp_parser *);
a723baf1 1436static tree cp_parser_qualified_namespace_specifier
94edc4ab 1437 (cp_parser *);
a723baf1 1438static void cp_parser_namespace_alias_definition
94edc4ab 1439 (cp_parser *);
a723baf1 1440static void cp_parser_using_declaration
94edc4ab 1441 (cp_parser *);
a723baf1 1442static void cp_parser_using_directive
94edc4ab 1443 (cp_parser *);
a723baf1 1444static void cp_parser_asm_definition
94edc4ab 1445 (cp_parser *);
a723baf1 1446static void cp_parser_linkage_specification
94edc4ab 1447 (cp_parser *);
a723baf1
MM
1448
1449/* Declarators [gram.dcl.decl] */
1450
1451static tree cp_parser_init_declarator
94edc4ab 1452 (cp_parser *, tree, tree, bool, bool, bool *);
a723baf1 1453static tree cp_parser_declarator
7efa3e22 1454 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1455static tree cp_parser_direct_declarator
7efa3e22 1456 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1457static enum tree_code cp_parser_ptr_operator
94edc4ab 1458 (cp_parser *, tree *, tree *);
a723baf1 1459static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_cv_qualifier_opt
94edc4ab 1462 (cp_parser *);
a723baf1 1463static tree cp_parser_declarator_id
94edc4ab 1464 (cp_parser *);
a723baf1 1465static tree cp_parser_type_id
94edc4ab 1466 (cp_parser *);
a723baf1 1467static tree cp_parser_type_specifier_seq
94edc4ab 1468 (cp_parser *);
a723baf1 1469static tree cp_parser_parameter_declaration_clause
94edc4ab 1470 (cp_parser *);
a723baf1 1471static tree cp_parser_parameter_declaration_list
94edc4ab 1472 (cp_parser *);
a723baf1 1473static tree cp_parser_parameter_declaration
94edc4ab 1474 (cp_parser *, bool);
a723baf1 1475static tree cp_parser_function_definition
94edc4ab 1476 (cp_parser *, bool *);
a723baf1
MM
1477static void cp_parser_function_body
1478 (cp_parser *);
1479static tree cp_parser_initializer
39703eb9 1480 (cp_parser *, bool *, bool *);
a723baf1 1481static tree cp_parser_initializer_clause
39703eb9 1482 (cp_parser *, bool *);
a723baf1 1483static tree cp_parser_initializer_list
39703eb9 1484 (cp_parser *, bool *);
a723baf1
MM
1485
1486static bool cp_parser_ctor_initializer_opt_and_function_body
1487 (cp_parser *);
1488
1489/* Classes [gram.class] */
1490
1491static tree cp_parser_class_name
8d241e0b 1492 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1 1493static tree cp_parser_class_specifier
94edc4ab 1494 (cp_parser *);
a723baf1 1495static tree cp_parser_class_head
94edc4ab 1496 (cp_parser *, bool *);
a723baf1 1497static enum tag_types cp_parser_class_key
94edc4ab 1498 (cp_parser *);
a723baf1 1499static void cp_parser_member_specification_opt
94edc4ab 1500 (cp_parser *);
a723baf1 1501static void cp_parser_member_declaration
94edc4ab 1502 (cp_parser *);
a723baf1 1503static tree cp_parser_pure_specifier
94edc4ab 1504 (cp_parser *);
a723baf1 1505static tree cp_parser_constant_initializer
94edc4ab 1506 (cp_parser *);
a723baf1
MM
1507
1508/* Derived classes [gram.class.derived] */
1509
1510static tree cp_parser_base_clause
94edc4ab 1511 (cp_parser *);
a723baf1 1512static tree cp_parser_base_specifier
94edc4ab 1513 (cp_parser *);
a723baf1
MM
1514
1515/* Special member functions [gram.special] */
1516
1517static tree cp_parser_conversion_function_id
94edc4ab 1518 (cp_parser *);
a723baf1 1519static tree cp_parser_conversion_type_id
94edc4ab 1520 (cp_parser *);
a723baf1 1521static tree cp_parser_conversion_declarator_opt
94edc4ab 1522 (cp_parser *);
a723baf1 1523static bool cp_parser_ctor_initializer_opt
94edc4ab 1524 (cp_parser *);
a723baf1 1525static void cp_parser_mem_initializer_list
94edc4ab 1526 (cp_parser *);
a723baf1 1527static tree cp_parser_mem_initializer
94edc4ab 1528 (cp_parser *);
a723baf1 1529static tree cp_parser_mem_initializer_id
94edc4ab 1530 (cp_parser *);
a723baf1
MM
1531
1532/* Overloading [gram.over] */
1533
1534static tree cp_parser_operator_function_id
94edc4ab 1535 (cp_parser *);
a723baf1 1536static tree cp_parser_operator
94edc4ab 1537 (cp_parser *);
a723baf1
MM
1538
1539/* Templates [gram.temp] */
1540
1541static void cp_parser_template_declaration
94edc4ab 1542 (cp_parser *, bool);
a723baf1 1543static tree cp_parser_template_parameter_list
94edc4ab 1544 (cp_parser *);
a723baf1 1545static tree cp_parser_template_parameter
94edc4ab 1546 (cp_parser *);
a723baf1 1547static tree cp_parser_type_parameter
94edc4ab 1548 (cp_parser *);
a723baf1 1549static tree cp_parser_template_id
94edc4ab 1550 (cp_parser *, bool, bool);
a723baf1 1551static tree cp_parser_template_name
94edc4ab 1552 (cp_parser *, bool, bool);
a723baf1 1553static tree cp_parser_template_argument_list
94edc4ab 1554 (cp_parser *);
a723baf1 1555static tree cp_parser_template_argument
94edc4ab 1556 (cp_parser *);
a723baf1 1557static void cp_parser_explicit_instantiation
94edc4ab 1558 (cp_parser *);
a723baf1 1559static void cp_parser_explicit_specialization
94edc4ab 1560 (cp_parser *);
a723baf1
MM
1561
1562/* Exception handling [gram.exception] */
1563
1564static tree cp_parser_try_block
94edc4ab 1565 (cp_parser *);
a723baf1 1566static bool cp_parser_function_try_block
94edc4ab 1567 (cp_parser *);
a723baf1 1568static void cp_parser_handler_seq
94edc4ab 1569 (cp_parser *);
a723baf1 1570static void cp_parser_handler
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_exception_declaration
94edc4ab 1573 (cp_parser *);
a723baf1 1574static tree cp_parser_throw_expression
94edc4ab 1575 (cp_parser *);
a723baf1 1576static tree cp_parser_exception_specification_opt
94edc4ab 1577 (cp_parser *);
a723baf1 1578static tree cp_parser_type_id_list
94edc4ab 1579 (cp_parser *);
a723baf1
MM
1580
1581/* GNU Extensions */
1582
1583static tree cp_parser_asm_specification_opt
94edc4ab 1584 (cp_parser *);
a723baf1 1585static tree cp_parser_asm_operand_list
94edc4ab 1586 (cp_parser *);
a723baf1 1587static tree cp_parser_asm_clobber_list
94edc4ab 1588 (cp_parser *);
a723baf1 1589static tree cp_parser_attributes_opt
94edc4ab 1590 (cp_parser *);
a723baf1 1591static tree cp_parser_attribute_list
94edc4ab 1592 (cp_parser *);
a723baf1 1593static bool cp_parser_extension_opt
94edc4ab 1594 (cp_parser *, int *);
a723baf1 1595static void cp_parser_label_declaration
94edc4ab 1596 (cp_parser *);
a723baf1
MM
1597
1598/* Utility Routines */
1599
1600static tree cp_parser_lookup_name
8d241e0b 1601 (cp_parser *, tree, bool, bool, bool);
a723baf1 1602static tree cp_parser_lookup_name_simple
94edc4ab 1603 (cp_parser *, tree);
a723baf1
MM
1604static tree cp_parser_maybe_treat_template_as_class
1605 (tree, bool);
1606static bool cp_parser_check_declarator_template_parameters
94edc4ab 1607 (cp_parser *, tree);
a723baf1 1608static bool cp_parser_check_template_parameters
94edc4ab 1609 (cp_parser *, unsigned);
d6b4ea85
MM
1610static tree cp_parser_simple_cast_expression
1611 (cp_parser *);
a723baf1 1612static tree cp_parser_binary_expression
94edc4ab 1613 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1614static tree cp_parser_global_scope_opt
94edc4ab 1615 (cp_parser *, bool);
a723baf1
MM
1616static bool cp_parser_constructor_declarator_p
1617 (cp_parser *, bool);
1618static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1619 (cp_parser *, tree, tree, tree);
a723baf1 1620static tree cp_parser_function_definition_after_declarator
94edc4ab 1621 (cp_parser *, bool);
a723baf1 1622static void cp_parser_template_declaration_after_export
94edc4ab 1623 (cp_parser *, bool);
a723baf1 1624static tree cp_parser_single_declaration
94edc4ab 1625 (cp_parser *, bool, bool *);
a723baf1 1626static tree cp_parser_functional_cast
94edc4ab 1627 (cp_parser *, tree);
8db1028e
NS
1628static void cp_parser_save_default_args
1629 (cp_parser *, tree);
a723baf1 1630static void cp_parser_late_parsing_for_member
94edc4ab 1631 (cp_parser *, tree);
a723baf1 1632static void cp_parser_late_parsing_default_args
8218bd34 1633 (cp_parser *, tree);
a723baf1 1634static tree cp_parser_sizeof_operand
94edc4ab 1635 (cp_parser *, enum rid);
a723baf1 1636static bool cp_parser_declares_only_class_p
94edc4ab 1637 (cp_parser *);
d17811fd
MM
1638static tree cp_parser_fold_non_dependent_expr
1639 (tree);
a723baf1 1640static bool cp_parser_friend_p
94edc4ab 1641 (tree);
a723baf1 1642static cp_token *cp_parser_require
94edc4ab 1643 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1644static cp_token *cp_parser_require_keyword
94edc4ab 1645 (cp_parser *, enum rid, const char *);
a723baf1 1646static bool cp_parser_token_starts_function_definition_p
94edc4ab 1647 (cp_token *);
a723baf1
MM
1648static bool cp_parser_next_token_starts_class_definition_p
1649 (cp_parser *);
d17811fd
MM
1650static bool cp_parser_next_token_ends_template_argument_p
1651 (cp_parser *);
a723baf1 1652static enum tag_types cp_parser_token_is_class_key
94edc4ab 1653 (cp_token *);
a723baf1
MM
1654static void cp_parser_check_class_key
1655 (enum tag_types, tree type);
1656static bool cp_parser_optional_template_keyword
1657 (cp_parser *);
2050a1bb
MM
1658static void cp_parser_pre_parsed_nested_name_specifier
1659 (cp_parser *);
a723baf1
MM
1660static void cp_parser_cache_group
1661 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1662static void cp_parser_parse_tentatively
94edc4ab 1663 (cp_parser *);
a723baf1 1664static void cp_parser_commit_to_tentative_parse
94edc4ab 1665 (cp_parser *);
a723baf1 1666static void cp_parser_abort_tentative_parse
94edc4ab 1667 (cp_parser *);
a723baf1 1668static bool cp_parser_parse_definitely
94edc4ab 1669 (cp_parser *);
f7b5ecd9 1670static inline bool cp_parser_parsing_tentatively
94edc4ab 1671 (cp_parser *);
a723baf1 1672static bool cp_parser_committed_to_tentative_parse
94edc4ab 1673 (cp_parser *);
a723baf1 1674static void cp_parser_error
94edc4ab 1675 (cp_parser *, const char *);
e5976695 1676static bool cp_parser_simulate_error
94edc4ab 1677 (cp_parser *);
a723baf1 1678static void cp_parser_check_type_definition
94edc4ab 1679 (cp_parser *);
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
14d22dd6
MM
1769/* Issue an eror message about the fact that THING appeared in a
1770 constant-expression. Returns ERROR_MARK_NODE. */
1771
1772static tree
1773cp_parser_non_constant_expression (const char *thing)
1774{
1775 error ("%s cannot appear in a constant-expression", thing);
1776 return error_mark_node;
1777}
1778
8fbc5ae7
MM
1779/* Check for a common situation where a type-name should be present,
1780 but is not, and issue a sensible error message. Returns true if an
1781 invalid type-name was detected. */
1782
1783static bool
1784cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1785{
1786 /* If the next two tokens are both identifiers, the code is
1787 erroneous. The usual cause of this situation is code like:
1788
1789 T t;
1790
1791 where "T" should name a type -- but does not. */
1792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1793 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1794 {
1795 tree name;
1796
8d241e0b 1797 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1798 looking at a declaration. */
1799 /* Consume the first identifier. */
1800 name = cp_lexer_consume_token (parser->lexer)->value;
1801 /* Issue an error message. */
1802 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1803 /* If we're in a template class, it's possible that the user was
1804 referring to a type from a base class. For example:
1805
1806 template <typename T> struct A { typedef T X; };
1807 template <typename T> struct B : public A<T> { X x; };
1808
1809 The user should have said "typename A<T>::X". */
1810 if (processing_template_decl && current_class_type)
1811 {
1812 tree b;
1813
1814 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1815 b;
1816 b = TREE_CHAIN (b))
1817 {
1818 tree base_type = BINFO_TYPE (b);
1819 if (CLASS_TYPE_P (base_type)
1fb3244a 1820 && dependent_type_p (base_type))
8fbc5ae7
MM
1821 {
1822 tree field;
1823 /* Go from a particular instantiation of the
1824 template (which will have an empty TYPE_FIELDs),
1825 to the main version. */
353b4fc0 1826 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1827 for (field = TYPE_FIELDS (base_type);
1828 field;
1829 field = TREE_CHAIN (field))
1830 if (TREE_CODE (field) == TYPE_DECL
1831 && DECL_NAME (field) == name)
1832 {
1833 error ("(perhaps `typename %T::%s' was intended)",
1834 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1835 break;
1836 }
1837 if (field)
1838 break;
1839 }
1840 }
1841 }
1842 /* Skip to the end of the declaration; there's no point in
1843 trying to process it. */
1844 cp_parser_skip_to_end_of_statement (parser);
1845
1846 return true;
1847 }
1848
1849 return false;
1850}
1851
a723baf1 1852/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
1853 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1854 are doing error recovery. Returns -1 if OR_COMMA is true and we
1855 found an unnested comma. */
a723baf1 1856
7efa3e22
NS
1857static int
1858cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1859 bool recovering, bool or_comma)
a723baf1 1860{
7efa3e22
NS
1861 unsigned paren_depth = 0;
1862 unsigned brace_depth = 0;
a723baf1 1863
7efa3e22
NS
1864 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1865 && !cp_parser_committed_to_tentative_parse (parser))
1866 return 0;
1867
a723baf1
MM
1868 while (true)
1869 {
1870 cp_token *token;
7efa3e22 1871
a723baf1
MM
1872 /* If we've run out of tokens, then there is no closing `)'. */
1873 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 1874 return 0;
a723baf1 1875
7efa3e22
NS
1876 if (recovering)
1877 {
1878 token = cp_lexer_peek_token (parser->lexer);
a723baf1 1879
7efa3e22
NS
1880 /* This matches the processing in skip_to_end_of_statement */
1881 if (token->type == CPP_SEMICOLON && !brace_depth)
1882 return 0;
1883 if (token->type == CPP_OPEN_BRACE)
1884 ++brace_depth;
1885 if (token->type == CPP_CLOSE_BRACE)
1886 {
1887 if (!brace_depth--)
1888 return 0;
1889 }
1890 if (or_comma && token->type == CPP_COMMA
1891 && !brace_depth && !paren_depth)
1892 return -1;
1893 }
1894
a723baf1
MM
1895 /* Consume the token. */
1896 token = cp_lexer_consume_token (parser->lexer);
7efa3e22
NS
1897
1898 if (!brace_depth)
1899 {
1900 /* If it is an `(', we have entered another level of nesting. */
1901 if (token->type == CPP_OPEN_PAREN)
1902 ++paren_depth;
1903 /* If it is a `)', then we might be done. */
1904 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1905 return 1;
1906 }
a723baf1
MM
1907 }
1908}
1909
1910/* Consume tokens until we reach the end of the current statement.
1911 Normally, that will be just before consuming a `;'. However, if a
1912 non-nested `}' comes first, then we stop before consuming that. */
1913
1914static void
94edc4ab 1915cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
1916{
1917 unsigned nesting_depth = 0;
1918
1919 while (true)
1920 {
1921 cp_token *token;
1922
1923 /* Peek at the next token. */
1924 token = cp_lexer_peek_token (parser->lexer);
1925 /* If we've run out of tokens, stop. */
1926 if (token->type == CPP_EOF)
1927 break;
1928 /* If the next token is a `;', we have reached the end of the
1929 statement. */
1930 if (token->type == CPP_SEMICOLON && !nesting_depth)
1931 break;
1932 /* If the next token is a non-nested `}', then we have reached
1933 the end of the current block. */
1934 if (token->type == CPP_CLOSE_BRACE)
1935 {
1936 /* If this is a non-nested `}', stop before consuming it.
1937 That way, when confronted with something like:
1938
1939 { 3 + }
1940
1941 we stop before consuming the closing `}', even though we
1942 have not yet reached a `;'. */
1943 if (nesting_depth == 0)
1944 break;
1945 /* If it is the closing `}' for a block that we have
1946 scanned, stop -- but only after consuming the token.
1947 That way given:
1948
1949 void f g () { ... }
1950 typedef int I;
1951
1952 we will stop after the body of the erroneously declared
1953 function, but before consuming the following `typedef'
1954 declaration. */
1955 if (--nesting_depth == 0)
1956 {
1957 cp_lexer_consume_token (parser->lexer);
1958 break;
1959 }
1960 }
1961 /* If it the next token is a `{', then we are entering a new
1962 block. Consume the entire block. */
1963 else if (token->type == CPP_OPEN_BRACE)
1964 ++nesting_depth;
1965 /* Consume the token. */
1966 cp_lexer_consume_token (parser->lexer);
1967 }
1968}
1969
e0860732
MM
1970/* This function is called at the end of a statement or declaration.
1971 If the next token is a semicolon, it is consumed; otherwise, error
1972 recovery is attempted. */
1973
1974static void
1975cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1976{
1977 /* Look for the trailing `;'. */
1978 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
1979 {
1980 /* If there is additional (erroneous) input, skip to the end of
1981 the statement. */
1982 cp_parser_skip_to_end_of_statement (parser);
1983 /* If the next token is now a `;', consume it. */
1984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
1985 cp_lexer_consume_token (parser->lexer);
1986 }
1987}
1988
a723baf1
MM
1989/* Skip tokens until we have consumed an entire block, or until we
1990 have consumed a non-nested `;'. */
1991
1992static void
94edc4ab 1993cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
1994{
1995 unsigned nesting_depth = 0;
1996
1997 while (true)
1998 {
1999 cp_token *token;
2000
2001 /* Peek at the next token. */
2002 token = cp_lexer_peek_token (parser->lexer);
2003 /* If we've run out of tokens, stop. */
2004 if (token->type == CPP_EOF)
2005 break;
2006 /* If the next token is a `;', we have reached the end of the
2007 statement. */
2008 if (token->type == CPP_SEMICOLON && !nesting_depth)
2009 {
2010 /* Consume the `;'. */
2011 cp_lexer_consume_token (parser->lexer);
2012 break;
2013 }
2014 /* Consume the token. */
2015 token = cp_lexer_consume_token (parser->lexer);
2016 /* If the next token is a non-nested `}', then we have reached
2017 the end of the current block. */
2018 if (token->type == CPP_CLOSE_BRACE
2019 && (nesting_depth == 0 || --nesting_depth == 0))
2020 break;
2021 /* If it the next token is a `{', then we are entering a new
2022 block. Consume the entire block. */
2023 if (token->type == CPP_OPEN_BRACE)
2024 ++nesting_depth;
2025 }
2026}
2027
2028/* Skip tokens until a non-nested closing curly brace is the next
2029 token. */
2030
2031static void
2032cp_parser_skip_to_closing_brace (cp_parser *parser)
2033{
2034 unsigned nesting_depth = 0;
2035
2036 while (true)
2037 {
2038 cp_token *token;
2039
2040 /* Peek at the next token. */
2041 token = cp_lexer_peek_token (parser->lexer);
2042 /* If we've run out of tokens, stop. */
2043 if (token->type == CPP_EOF)
2044 break;
2045 /* If the next token is a non-nested `}', then we have reached
2046 the end of the current block. */
2047 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2048 break;
2049 /* If it the next token is a `{', then we are entering a new
2050 block. Consume the entire block. */
2051 else if (token->type == CPP_OPEN_BRACE)
2052 ++nesting_depth;
2053 /* Consume the token. */
2054 cp_lexer_consume_token (parser->lexer);
2055 }
2056}
2057
2058/* Create a new C++ parser. */
2059
2060static cp_parser *
94edc4ab 2061cp_parser_new (void)
a723baf1
MM
2062{
2063 cp_parser *parser;
17211ab5
GK
2064 cp_lexer *lexer;
2065
2066 /* cp_lexer_new_main is called before calling ggc_alloc because
2067 cp_lexer_new_main might load a PCH file. */
2068 lexer = cp_lexer_new_main ();
a723baf1
MM
2069
2070 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2071 parser->lexer = lexer;
a723baf1
MM
2072 parser->context = cp_parser_context_new (NULL);
2073
2074 /* For now, we always accept GNU extensions. */
2075 parser->allow_gnu_extensions_p = 1;
2076
2077 /* The `>' token is a greater-than operator, not the end of a
2078 template-id. */
2079 parser->greater_than_is_operator_p = true;
2080
2081 parser->default_arg_ok_p = true;
2082
2083 /* We are not parsing a constant-expression. */
2084 parser->constant_expression_p = false;
14d22dd6
MM
2085 parser->allow_non_constant_expression_p = false;
2086 parser->non_constant_expression_p = false;
a723baf1
MM
2087
2088 /* Local variable names are not forbidden. */
2089 parser->local_variables_forbidden_p = false;
2090
34cd5ae7 2091 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2092 parser->in_unbraced_linkage_specification_p = false;
2093
2094 /* We are not processing a declarator. */
2095 parser->in_declarator_p = false;
2096
a723baf1
MM
2097 /* The unparsed function queue is empty. */
2098 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2099
2100 /* There are no classes being defined. */
2101 parser->num_classes_being_defined = 0;
2102
2103 /* No template parameters apply. */
2104 parser->num_template_parameter_lists = 0;
2105
2106 return parser;
2107}
2108
2109/* Lexical conventions [gram.lex] */
2110
2111/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2112 identifier. */
2113
2114static tree
94edc4ab 2115cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2116{
2117 cp_token *token;
2118
2119 /* Look for the identifier. */
2120 token = cp_parser_require (parser, CPP_NAME, "identifier");
2121 /* Return the value. */
2122 return token ? token->value : error_mark_node;
2123}
2124
2125/* Basic concepts [gram.basic] */
2126
2127/* Parse a translation-unit.
2128
2129 translation-unit:
2130 declaration-seq [opt]
2131
2132 Returns TRUE if all went well. */
2133
2134static bool
94edc4ab 2135cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2136{
2137 while (true)
2138 {
2139 cp_parser_declaration_seq_opt (parser);
2140
2141 /* If there are no tokens left then all went well. */
2142 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2143 break;
2144
2145 /* Otherwise, issue an error message. */
2146 cp_parser_error (parser, "expected declaration");
2147 return false;
2148 }
2149
2150 /* Consume the EOF token. */
2151 cp_parser_require (parser, CPP_EOF, "end-of-file");
2152
2153 /* Finish up. */
2154 finish_translation_unit ();
2155
2156 /* All went well. */
2157 return true;
2158}
2159
2160/* Expressions [gram.expr] */
2161
2162/* Parse a primary-expression.
2163
2164 primary-expression:
2165 literal
2166 this
2167 ( expression )
2168 id-expression
2169
2170 GNU Extensions:
2171
2172 primary-expression:
2173 ( compound-statement )
2174 __builtin_va_arg ( assignment-expression , type-id )
2175
2176 literal:
2177 __null
2178
2179 Returns a representation of the expression.
2180
2181 *IDK indicates what kind of id-expression (if any) was present.
2182
2183 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2184 used as the operand of a pointer-to-member. In that case,
2185 *QUALIFYING_CLASS gives the class that is used as the qualifying
2186 class in the pointer-to-member. */
2187
2188static tree
2189cp_parser_primary_expression (cp_parser *parser,
b3445994 2190 cp_id_kind *idk,
a723baf1
MM
2191 tree *qualifying_class)
2192{
2193 cp_token *token;
2194
2195 /* Assume the primary expression is not an id-expression. */
b3445994 2196 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2197 /* And that it cannot be used as pointer-to-member. */
2198 *qualifying_class = NULL_TREE;
2199
2200 /* Peek at the next token. */
2201 token = cp_lexer_peek_token (parser->lexer);
2202 switch (token->type)
2203 {
2204 /* literal:
2205 integer-literal
2206 character-literal
2207 floating-literal
2208 string-literal
2209 boolean-literal */
2210 case CPP_CHAR:
2211 case CPP_WCHAR:
2212 case CPP_STRING:
2213 case CPP_WSTRING:
2214 case CPP_NUMBER:
2215 token = cp_lexer_consume_token (parser->lexer);
2216 return token->value;
2217
2218 case CPP_OPEN_PAREN:
2219 {
2220 tree expr;
2221 bool saved_greater_than_is_operator_p;
2222
2223 /* Consume the `('. */
2224 cp_lexer_consume_token (parser->lexer);
2225 /* Within a parenthesized expression, a `>' token is always
2226 the greater-than operator. */
2227 saved_greater_than_is_operator_p
2228 = parser->greater_than_is_operator_p;
2229 parser->greater_than_is_operator_p = true;
2230 /* If we see `( { ' then we are looking at the beginning of
2231 a GNU statement-expression. */
2232 if (cp_parser_allow_gnu_extensions_p (parser)
2233 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2234 {
2235 /* Statement-expressions are not allowed by the standard. */
2236 if (pedantic)
2237 pedwarn ("ISO C++ forbids braced-groups within expressions");
2238
2239 /* And they're not allowed outside of a function-body; you
2240 cannot, for example, write:
2241
2242 int i = ({ int j = 3; j + 1; });
2243
2244 at class or namespace scope. */
2245 if (!at_function_scope_p ())
2246 error ("statement-expressions are allowed only inside functions");
2247 /* Start the statement-expression. */
2248 expr = begin_stmt_expr ();
2249 /* Parse the compound-statement. */
2250 cp_parser_compound_statement (parser);
2251 /* Finish up. */
2252 expr = finish_stmt_expr (expr);
2253 }
2254 else
2255 {
2256 /* Parse the parenthesized expression. */
2257 expr = cp_parser_expression (parser);
2258 /* Let the front end know that this expression was
2259 enclosed in parentheses. This matters in case, for
2260 example, the expression is of the form `A::B', since
2261 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2262 not. */
2263 finish_parenthesized_expr (expr);
2264 }
2265 /* The `>' token might be the end of a template-id or
2266 template-parameter-list now. */
2267 parser->greater_than_is_operator_p
2268 = saved_greater_than_is_operator_p;
2269 /* Consume the `)'. */
2270 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2271 cp_parser_skip_to_end_of_statement (parser);
2272
2273 return expr;
2274 }
2275
2276 case CPP_KEYWORD:
2277 switch (token->keyword)
2278 {
2279 /* These two are the boolean literals. */
2280 case RID_TRUE:
2281 cp_lexer_consume_token (parser->lexer);
2282 return boolean_true_node;
2283 case RID_FALSE:
2284 cp_lexer_consume_token (parser->lexer);
2285 return boolean_false_node;
2286
2287 /* The `__null' literal. */
2288 case RID_NULL:
2289 cp_lexer_consume_token (parser->lexer);
2290 return null_node;
2291
2292 /* Recognize the `this' keyword. */
2293 case RID_THIS:
2294 cp_lexer_consume_token (parser->lexer);
2295 if (parser->local_variables_forbidden_p)
2296 {
2297 error ("`this' may not be used in this context");
2298 return error_mark_node;
2299 }
14d22dd6
MM
2300 /* Pointers cannot appear in constant-expressions. */
2301 if (parser->constant_expression_p)
2302 {
2303 if (!parser->allow_non_constant_expression_p)
2304 return cp_parser_non_constant_expression ("`this'");
2305 parser->non_constant_expression_p = true;
2306 }
a723baf1
MM
2307 return finish_this_expr ();
2308
2309 /* The `operator' keyword can be the beginning of an
2310 id-expression. */
2311 case RID_OPERATOR:
2312 goto id_expression;
2313
2314 case RID_FUNCTION_NAME:
2315 case RID_PRETTY_FUNCTION_NAME:
2316 case RID_C99_FUNCTION_NAME:
2317 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2318 __func__ are the names of variables -- but they are
2319 treated specially. Therefore, they are handled here,
2320 rather than relying on the generic id-expression logic
34cd5ae7 2321 below. Grammatically, these names are id-expressions.
a723baf1
MM
2322
2323 Consume the token. */
2324 token = cp_lexer_consume_token (parser->lexer);
2325 /* Look up the name. */
2326 return finish_fname (token->value);
2327
2328 case RID_VA_ARG:
2329 {
2330 tree expression;
2331 tree type;
2332
2333 /* The `__builtin_va_arg' construct is used to handle
2334 `va_arg'. Consume the `__builtin_va_arg' token. */
2335 cp_lexer_consume_token (parser->lexer);
2336 /* Look for the opening `('. */
2337 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2338 /* Now, parse the assignment-expression. */
2339 expression = cp_parser_assignment_expression (parser);
2340 /* Look for the `,'. */
2341 cp_parser_require (parser, CPP_COMMA, "`,'");
2342 /* Parse the type-id. */
2343 type = cp_parser_type_id (parser);
2344 /* Look for the closing `)'. */
2345 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2346 /* Using `va_arg' in a constant-expression is not
2347 allowed. */
2348 if (parser->constant_expression_p)
2349 {
2350 if (!parser->allow_non_constant_expression_p)
2351 return cp_parser_non_constant_expression ("`va_arg'");
2352 parser->non_constant_expression_p = true;
2353 }
a723baf1
MM
2354 return build_x_va_arg (expression, type);
2355 }
2356
2357 default:
2358 cp_parser_error (parser, "expected primary-expression");
2359 return error_mark_node;
2360 }
a723baf1
MM
2361
2362 /* An id-expression can start with either an identifier, a
2363 `::' as the beginning of a qualified-id, or the "operator"
2364 keyword. */
2365 case CPP_NAME:
2366 case CPP_SCOPE:
2367 case CPP_TEMPLATE_ID:
2368 case CPP_NESTED_NAME_SPECIFIER:
2369 {
2370 tree id_expression;
2371 tree decl;
b3445994 2372 const char *error_msg;
a723baf1
MM
2373
2374 id_expression:
2375 /* Parse the id-expression. */
2376 id_expression
2377 = cp_parser_id_expression (parser,
2378 /*template_keyword_p=*/false,
2379 /*check_dependency_p=*/true,
2380 /*template_p=*/NULL);
2381 if (id_expression == error_mark_node)
2382 return error_mark_node;
2383 /* If we have a template-id, then no further lookup is
2384 required. If the template-id was for a template-class, we
2385 will sometimes have a TYPE_DECL at this point. */
2386 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2387 || TREE_CODE (id_expression) == TYPE_DECL)
2388 decl = id_expression;
2389 /* Look up the name. */
2390 else
2391 {
2392 decl = cp_parser_lookup_name_simple (parser, id_expression);
2393 /* If name lookup gives us a SCOPE_REF, then the
2394 qualifying scope was dependent. Just propagate the
2395 name. */
2396 if (TREE_CODE (decl) == SCOPE_REF)
2397 {
2398 if (TYPE_P (TREE_OPERAND (decl, 0)))
2399 *qualifying_class = TREE_OPERAND (decl, 0);
14d22dd6
MM
2400 /* Since this name was dependent, the expression isn't
2401 constant -- yet. No error is issued because it
2402 might be constant when things are instantiated. */
2403 if (parser->constant_expression_p)
2404 parser->non_constant_expression_p = true;
a723baf1
MM
2405 return decl;
2406 }
2407 /* Check to see if DECL is a local variable in a context
2408 where that is forbidden. */
2409 if (parser->local_variables_forbidden_p
2410 && local_variable_p (decl))
2411 {
2412 /* It might be that we only found DECL because we are
2413 trying to be generous with pre-ISO scoping rules.
2414 For example, consider:
2415
2416 int i;
2417 void g() {
2418 for (int i = 0; i < 10; ++i) {}
2419 extern void f(int j = i);
2420 }
2421
2422 Here, name look up will originally find the out
2423 of scope `i'. We need to issue a warning message,
2424 but then use the global `i'. */
2425 decl = check_for_out_of_scope_variable (decl);
2426 if (local_variable_p (decl))
2427 {
2428 error ("local variable `%D' may not appear in this context",
2429 decl);
2430 return error_mark_node;
2431 }
2432 }
c006d942 2433 }
b3445994
MM
2434
2435 decl = finish_id_expression (id_expression, decl, parser->scope,
2436 idk, qualifying_class,
2437 parser->constant_expression_p,
2438 parser->allow_non_constant_expression_p,
2439 &parser->non_constant_expression_p,
2440 &error_msg);
2441 if (error_msg)
2442 cp_parser_error (parser, error_msg);
a723baf1
MM
2443 return decl;
2444 }
2445
2446 /* Anything else is an error. */
2447 default:
2448 cp_parser_error (parser, "expected primary-expression");
2449 return error_mark_node;
2450 }
2451}
2452
2453/* Parse an id-expression.
2454
2455 id-expression:
2456 unqualified-id
2457 qualified-id
2458
2459 qualified-id:
2460 :: [opt] nested-name-specifier template [opt] unqualified-id
2461 :: identifier
2462 :: operator-function-id
2463 :: template-id
2464
2465 Return a representation of the unqualified portion of the
2466 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2467 a `::' or nested-name-specifier.
2468
2469 Often, if the id-expression was a qualified-id, the caller will
2470 want to make a SCOPE_REF to represent the qualified-id. This
2471 function does not do this in order to avoid wastefully creating
2472 SCOPE_REFs when they are not required.
2473
a723baf1
MM
2474 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2475 `template' keyword.
2476
2477 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2478 uninstantiated templates.
2479
15d2cb19 2480 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1
MM
2481 `template' keyword is used to explicitly indicate that the entity
2482 named is a template. */
2483
2484static tree
2485cp_parser_id_expression (cp_parser *parser,
2486 bool template_keyword_p,
2487 bool check_dependency_p,
2488 bool *template_p)
2489{
2490 bool global_scope_p;
2491 bool nested_name_specifier_p;
2492
2493 /* Assume the `template' keyword was not used. */
2494 if (template_p)
2495 *template_p = false;
2496
2497 /* Look for the optional `::' operator. */
2498 global_scope_p
2499 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2500 != NULL_TREE);
2501 /* Look for the optional nested-name-specifier. */
2502 nested_name_specifier_p
2503 = (cp_parser_nested_name_specifier_opt (parser,
2504 /*typename_keyword_p=*/false,
2505 check_dependency_p,
2506 /*type_p=*/false)
2507 != NULL_TREE);
2508 /* If there is a nested-name-specifier, then we are looking at
2509 the first qualified-id production. */
2510 if (nested_name_specifier_p)
2511 {
2512 tree saved_scope;
2513 tree saved_object_scope;
2514 tree saved_qualifying_scope;
2515 tree unqualified_id;
2516 bool is_template;
2517
2518 /* See if the next token is the `template' keyword. */
2519 if (!template_p)
2520 template_p = &is_template;
2521 *template_p = cp_parser_optional_template_keyword (parser);
2522 /* Name lookup we do during the processing of the
2523 unqualified-id might obliterate SCOPE. */
2524 saved_scope = parser->scope;
2525 saved_object_scope = parser->object_scope;
2526 saved_qualifying_scope = parser->qualifying_scope;
2527 /* Process the final unqualified-id. */
2528 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2529 check_dependency_p);
2530 /* Restore the SAVED_SCOPE for our caller. */
2531 parser->scope = saved_scope;
2532 parser->object_scope = saved_object_scope;
2533 parser->qualifying_scope = saved_qualifying_scope;
2534
2535 return unqualified_id;
2536 }
2537 /* Otherwise, if we are in global scope, then we are looking at one
2538 of the other qualified-id productions. */
2539 else if (global_scope_p)
2540 {
2541 cp_token *token;
2542 tree id;
2543
e5976695
MM
2544 /* Peek at the next token. */
2545 token = cp_lexer_peek_token (parser->lexer);
2546
2547 /* If it's an identifier, and the next token is not a "<", then
2548 we can avoid the template-id case. This is an optimization
2549 for this common case. */
2550 if (token->type == CPP_NAME
2551 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2552 return cp_parser_identifier (parser);
2553
a723baf1
MM
2554 cp_parser_parse_tentatively (parser);
2555 /* Try a template-id. */
2556 id = cp_parser_template_id (parser,
2557 /*template_keyword_p=*/false,
2558 /*check_dependency_p=*/true);
2559 /* If that worked, we're done. */
2560 if (cp_parser_parse_definitely (parser))
2561 return id;
2562
e5976695
MM
2563 /* Peek at the next token. (Changes in the token buffer may
2564 have invalidated the pointer obtained above.) */
a723baf1
MM
2565 token = cp_lexer_peek_token (parser->lexer);
2566
2567 switch (token->type)
2568 {
2569 case CPP_NAME:
2570 return cp_parser_identifier (parser);
2571
2572 case CPP_KEYWORD:
2573 if (token->keyword == RID_OPERATOR)
2574 return cp_parser_operator_function_id (parser);
2575 /* Fall through. */
2576
2577 default:
2578 cp_parser_error (parser, "expected id-expression");
2579 return error_mark_node;
2580 }
2581 }
2582 else
2583 return cp_parser_unqualified_id (parser, template_keyword_p,
2584 /*check_dependency_p=*/true);
2585}
2586
2587/* Parse an unqualified-id.
2588
2589 unqualified-id:
2590 identifier
2591 operator-function-id
2592 conversion-function-id
2593 ~ class-name
2594 template-id
2595
2596 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2597 keyword, in a construct like `A::template ...'.
2598
2599 Returns a representation of unqualified-id. For the `identifier'
2600 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2601 production a BIT_NOT_EXPR is returned; the operand of the
2602 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2603 other productions, see the documentation accompanying the
2604 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2605 names are looked up in uninstantiated templates. */
2606
2607static tree
94edc4ab
NN
2608cp_parser_unqualified_id (cp_parser* parser,
2609 bool template_keyword_p,
2610 bool check_dependency_p)
a723baf1
MM
2611{
2612 cp_token *token;
2613
2614 /* Peek at the next token. */
2615 token = cp_lexer_peek_token (parser->lexer);
2616
2617 switch (token->type)
2618 {
2619 case CPP_NAME:
2620 {
2621 tree id;
2622
2623 /* We don't know yet whether or not this will be a
2624 template-id. */
2625 cp_parser_parse_tentatively (parser);
2626 /* Try a template-id. */
2627 id = cp_parser_template_id (parser, template_keyword_p,
2628 check_dependency_p);
2629 /* If it worked, we're done. */
2630 if (cp_parser_parse_definitely (parser))
2631 return id;
2632 /* Otherwise, it's an ordinary identifier. */
2633 return cp_parser_identifier (parser);
2634 }
2635
2636 case CPP_TEMPLATE_ID:
2637 return cp_parser_template_id (parser, template_keyword_p,
2638 check_dependency_p);
2639
2640 case CPP_COMPL:
2641 {
2642 tree type_decl;
2643 tree qualifying_scope;
2644 tree object_scope;
2645 tree scope;
2646
2647 /* Consume the `~' token. */
2648 cp_lexer_consume_token (parser->lexer);
2649 /* Parse the class-name. The standard, as written, seems to
2650 say that:
2651
2652 template <typename T> struct S { ~S (); };
2653 template <typename T> S<T>::~S() {}
2654
2655 is invalid, since `~' must be followed by a class-name, but
2656 `S<T>' is dependent, and so not known to be a class.
2657 That's not right; we need to look in uninstantiated
2658 templates. A further complication arises from:
2659
2660 template <typename T> void f(T t) {
2661 t.T::~T();
2662 }
2663
2664 Here, it is not possible to look up `T' in the scope of `T'
2665 itself. We must look in both the current scope, and the
2666 scope of the containing complete expression.
2667
2668 Yet another issue is:
2669
2670 struct S {
2671 int S;
2672 ~S();
2673 };
2674
2675 S::~S() {}
2676
2677 The standard does not seem to say that the `S' in `~S'
2678 should refer to the type `S' and not the data member
2679 `S::S'. */
2680
2681 /* DR 244 says that we look up the name after the "~" in the
2682 same scope as we looked up the qualifying name. That idea
2683 isn't fully worked out; it's more complicated than that. */
2684 scope = parser->scope;
2685 object_scope = parser->object_scope;
2686 qualifying_scope = parser->qualifying_scope;
2687
2688 /* If the name is of the form "X::~X" it's OK. */
2689 if (scope && TYPE_P (scope)
2690 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2691 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2692 == CPP_OPEN_PAREN)
2693 && (cp_lexer_peek_token (parser->lexer)->value
2694 == TYPE_IDENTIFIER (scope)))
2695 {
2696 cp_lexer_consume_token (parser->lexer);
2697 return build_nt (BIT_NOT_EXPR, scope);
2698 }
2699
2700 /* If there was an explicit qualification (S::~T), first look
2701 in the scope given by the qualification (i.e., S). */
2702 if (scope)
2703 {
2704 cp_parser_parse_tentatively (parser);
2705 type_decl = cp_parser_class_name (parser,
2706 /*typename_keyword_p=*/false,
2707 /*template_keyword_p=*/false,
2708 /*type_p=*/false,
a723baf1
MM
2709 /*check_dependency=*/false,
2710 /*class_head_p=*/false);
2711 if (cp_parser_parse_definitely (parser))
2712 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2713 }
2714 /* In "N::S::~S", look in "N" as well. */
2715 if (scope && qualifying_scope)
2716 {
2717 cp_parser_parse_tentatively (parser);
2718 parser->scope = qualifying_scope;
2719 parser->object_scope = NULL_TREE;
2720 parser->qualifying_scope = NULL_TREE;
2721 type_decl
2722 = cp_parser_class_name (parser,
2723 /*typename_keyword_p=*/false,
2724 /*template_keyword_p=*/false,
2725 /*type_p=*/false,
a723baf1
MM
2726 /*check_dependency=*/false,
2727 /*class_head_p=*/false);
2728 if (cp_parser_parse_definitely (parser))
2729 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2730 }
2731 /* In "p->S::~T", look in the scope given by "*p" as well. */
2732 else if (object_scope)
2733 {
2734 cp_parser_parse_tentatively (parser);
2735 parser->scope = object_scope;
2736 parser->object_scope = NULL_TREE;
2737 parser->qualifying_scope = NULL_TREE;
2738 type_decl
2739 = cp_parser_class_name (parser,
2740 /*typename_keyword_p=*/false,
2741 /*template_keyword_p=*/false,
2742 /*type_p=*/false,
a723baf1
MM
2743 /*check_dependency=*/false,
2744 /*class_head_p=*/false);
2745 if (cp_parser_parse_definitely (parser))
2746 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2747 }
2748 /* Look in the surrounding context. */
2749 parser->scope = NULL_TREE;
2750 parser->object_scope = NULL_TREE;
2751 parser->qualifying_scope = NULL_TREE;
2752 type_decl
2753 = cp_parser_class_name (parser,
2754 /*typename_keyword_p=*/false,
2755 /*template_keyword_p=*/false,
2756 /*type_p=*/false,
a723baf1
MM
2757 /*check_dependency=*/false,
2758 /*class_head_p=*/false);
2759 /* If an error occurred, assume that the name of the
2760 destructor is the same as the name of the qualifying
2761 class. That allows us to keep parsing after running
2762 into ill-formed destructor names. */
2763 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2764 return build_nt (BIT_NOT_EXPR, scope);
2765 else if (type_decl == error_mark_node)
2766 return error_mark_node;
2767
2768 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2769 }
2770
2771 case CPP_KEYWORD:
2772 if (token->keyword == RID_OPERATOR)
2773 {
2774 tree id;
2775
2776 /* This could be a template-id, so we try that first. */
2777 cp_parser_parse_tentatively (parser);
2778 /* Try a template-id. */
2779 id = cp_parser_template_id (parser, template_keyword_p,
2780 /*check_dependency_p=*/true);
2781 /* If that worked, we're done. */
2782 if (cp_parser_parse_definitely (parser))
2783 return id;
2784 /* We still don't know whether we're looking at an
2785 operator-function-id or a conversion-function-id. */
2786 cp_parser_parse_tentatively (parser);
2787 /* Try an operator-function-id. */
2788 id = cp_parser_operator_function_id (parser);
2789 /* If that didn't work, try a conversion-function-id. */
2790 if (!cp_parser_parse_definitely (parser))
2791 id = cp_parser_conversion_function_id (parser);
2792
2793 return id;
2794 }
2795 /* Fall through. */
2796
2797 default:
2798 cp_parser_error (parser, "expected unqualified-id");
2799 return error_mark_node;
2800 }
2801}
2802
2803/* Parse an (optional) nested-name-specifier.
2804
2805 nested-name-specifier:
2806 class-or-namespace-name :: nested-name-specifier [opt]
2807 class-or-namespace-name :: template nested-name-specifier [opt]
2808
2809 PARSER->SCOPE should be set appropriately before this function is
2810 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2811 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2812 in name lookups.
2813
2814 Sets PARSER->SCOPE to the class (TYPE) or namespace
2815 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2816 it unchanged if there is no nested-name-specifier. Returns the new
2817 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2818
2819static tree
2820cp_parser_nested_name_specifier_opt (cp_parser *parser,
2821 bool typename_keyword_p,
2822 bool check_dependency_p,
2823 bool type_p)
2824{
2825 bool success = false;
2826 tree access_check = NULL_TREE;
2827 ptrdiff_t start;
2050a1bb 2828 cp_token* token;
a723baf1
MM
2829
2830 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
2831 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2832 false, it may have been true before, in which case something
2833 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2834 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2835 CHECK_DEPENDENCY_P is false, we have to fall through into the
2836 main loop. */
2837 if (check_dependency_p
2838 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2839 {
2840 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
2841 return parser->scope;
2842 }
2843
2844 /* Remember where the nested-name-specifier starts. */
2845 if (cp_parser_parsing_tentatively (parser)
2846 && !cp_parser_committed_to_tentative_parse (parser))
2847 {
2050a1bb 2848 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
2849 start = cp_lexer_token_difference (parser->lexer,
2850 parser->lexer->first_token,
2050a1bb 2851 token);
a723baf1
MM
2852 }
2853 else
2854 start = -1;
2855
8d241e0b 2856 push_deferring_access_checks (dk_deferred);
cf22909c 2857
a723baf1
MM
2858 while (true)
2859 {
2860 tree new_scope;
2861 tree old_scope;
2862 tree saved_qualifying_scope;
a723baf1
MM
2863 bool template_keyword_p;
2864
2050a1bb
MM
2865 /* Spot cases that cannot be the beginning of a
2866 nested-name-specifier. */
2867 token = cp_lexer_peek_token (parser->lexer);
2868
2869 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2870 the already parsed nested-name-specifier. */
2871 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2872 {
2873 /* Grab the nested-name-specifier and continue the loop. */
2874 cp_parser_pre_parsed_nested_name_specifier (parser);
2875 success = true;
2876 continue;
2877 }
2878
a723baf1
MM
2879 /* Spot cases that cannot be the beginning of a
2880 nested-name-specifier. On the second and subsequent times
2881 through the loop, we look for the `template' keyword. */
f7b5ecd9 2882 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
2883 ;
2884 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 2885 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
2886 ;
2887 else
2888 {
2889 /* If the next token is not an identifier, then it is
2890 definitely not a class-or-namespace-name. */
f7b5ecd9 2891 if (token->type != CPP_NAME)
a723baf1
MM
2892 break;
2893 /* If the following token is neither a `<' (to begin a
2894 template-id), nor a `::', then we are not looking at a
2895 nested-name-specifier. */
2896 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2897 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2898 break;
2899 }
2900
2901 /* The nested-name-specifier is optional, so we parse
2902 tentatively. */
2903 cp_parser_parse_tentatively (parser);
2904
2905 /* Look for the optional `template' keyword, if this isn't the
2906 first time through the loop. */
2907 if (success)
2908 template_keyword_p = cp_parser_optional_template_keyword (parser);
2909 else
2910 template_keyword_p = false;
2911
2912 /* Save the old scope since the name lookup we are about to do
2913 might destroy it. */
2914 old_scope = parser->scope;
2915 saved_qualifying_scope = parser->qualifying_scope;
2916 /* Parse the qualifying entity. */
2917 new_scope
2918 = cp_parser_class_or_namespace_name (parser,
2919 typename_keyword_p,
2920 template_keyword_p,
2921 check_dependency_p,
2922 type_p);
2923 /* Look for the `::' token. */
2924 cp_parser_require (parser, CPP_SCOPE, "`::'");
2925
2926 /* If we found what we wanted, we keep going; otherwise, we're
2927 done. */
2928 if (!cp_parser_parse_definitely (parser))
2929 {
2930 bool error_p = false;
2931
2932 /* Restore the OLD_SCOPE since it was valid before the
2933 failed attempt at finding the last
2934 class-or-namespace-name. */
2935 parser->scope = old_scope;
2936 parser->qualifying_scope = saved_qualifying_scope;
2937 /* If the next token is an identifier, and the one after
2938 that is a `::', then any valid interpretation would have
2939 found a class-or-namespace-name. */
2940 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2941 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2942 == CPP_SCOPE)
2943 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2944 != CPP_COMPL))
2945 {
2946 token = cp_lexer_consume_token (parser->lexer);
2947 if (!error_p)
2948 {
2949 tree decl;
2950
2951 decl = cp_parser_lookup_name_simple (parser, token->value);
2952 if (TREE_CODE (decl) == TEMPLATE_DECL)
2953 error ("`%D' used without template parameters",
2954 decl);
2955 else if (parser->scope)
2956 {
2957 if (TYPE_P (parser->scope))
2958 error ("`%T::%D' is not a class-name or "
2959 "namespace-name",
2960 parser->scope, token->value);
2961 else
2962 error ("`%D::%D' is not a class-name or "
2963 "namespace-name",
2964 parser->scope, token->value);
2965 }
2966 else
2967 error ("`%D' is not a class-name or namespace-name",
2968 token->value);
2969 parser->scope = NULL_TREE;
2970 error_p = true;
eea9800f
MM
2971 /* Treat this as a successful nested-name-specifier
2972 due to:
2973
2974 [basic.lookup.qual]
2975
2976 If the name found is not a class-name (clause
2977 _class_) or namespace-name (_namespace.def_), the
2978 program is ill-formed. */
2979 success = true;
a723baf1
MM
2980 }
2981 cp_lexer_consume_token (parser->lexer);
2982 }
2983 break;
2984 }
2985
2986 /* We've found one valid nested-name-specifier. */
2987 success = true;
2988 /* Make sure we look in the right scope the next time through
2989 the loop. */
2990 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
2991 ? TREE_TYPE (new_scope)
2992 : new_scope);
2993 /* If it is a class scope, try to complete it; we are about to
2994 be looking up names inside the class. */
8fbc5ae7
MM
2995 if (TYPE_P (parser->scope)
2996 /* Since checking types for dependency can be expensive,
2997 avoid doing it if the type is already complete. */
2998 && !COMPLETE_TYPE_P (parser->scope)
2999 /* Do not try to complete dependent types. */
1fb3244a 3000 && !dependent_type_p (parser->scope))
a723baf1
MM
3001 complete_type (parser->scope);
3002 }
3003
cf22909c
KL
3004 /* Retrieve any deferred checks. Do not pop this access checks yet
3005 so the memory will not be reclaimed during token replacing below. */
3006 access_check = get_deferred_access_checks ();
3007
a723baf1
MM
3008 /* If parsing tentatively, replace the sequence of tokens that makes
3009 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3010 token. That way, should we re-parse the token stream, we will
3011 not have to repeat the effort required to do the parse, nor will
3012 we issue duplicate error messages. */
3013 if (success && start >= 0)
3014 {
a723baf1
MM
3015 /* Find the token that corresponds to the start of the
3016 template-id. */
3017 token = cp_lexer_advance_token (parser->lexer,
3018 parser->lexer->first_token,
3019 start);
3020
a723baf1
MM
3021 /* Reset the contents of the START token. */
3022 token->type = CPP_NESTED_NAME_SPECIFIER;
3023 token->value = build_tree_list (access_check, parser->scope);
3024 TREE_TYPE (token->value) = parser->qualifying_scope;
3025 token->keyword = RID_MAX;
3026 /* Purge all subsequent tokens. */
3027 cp_lexer_purge_tokens_after (parser->lexer, token);
3028 }
3029
cf22909c 3030 pop_deferring_access_checks ();
a723baf1
MM
3031 return success ? parser->scope : NULL_TREE;
3032}
3033
3034/* Parse a nested-name-specifier. See
3035 cp_parser_nested_name_specifier_opt for details. This function
3036 behaves identically, except that it will an issue an error if no
3037 nested-name-specifier is present, and it will return
3038 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3039 is present. */
3040
3041static tree
3042cp_parser_nested_name_specifier (cp_parser *parser,
3043 bool typename_keyword_p,
3044 bool check_dependency_p,
3045 bool type_p)
3046{
3047 tree scope;
3048
3049 /* Look for the nested-name-specifier. */
3050 scope = cp_parser_nested_name_specifier_opt (parser,
3051 typename_keyword_p,
3052 check_dependency_p,
3053 type_p);
3054 /* If it was not present, issue an error message. */
3055 if (!scope)
3056 {
3057 cp_parser_error (parser, "expected nested-name-specifier");
3058 return error_mark_node;
3059 }
3060
3061 return scope;
3062}
3063
3064/* Parse a class-or-namespace-name.
3065
3066 class-or-namespace-name:
3067 class-name
3068 namespace-name
3069
3070 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3071 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3072 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3073 TYPE_P is TRUE iff the next name should be taken as a class-name,
3074 even the same name is declared to be another entity in the same
3075 scope.
3076
3077 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3078 specified by the class-or-namespace-name. If neither is found the
3079 ERROR_MARK_NODE is returned. */
a723baf1
MM
3080
3081static tree
3082cp_parser_class_or_namespace_name (cp_parser *parser,
3083 bool typename_keyword_p,
3084 bool template_keyword_p,
3085 bool check_dependency_p,
3086 bool type_p)
3087{
3088 tree saved_scope;
3089 tree saved_qualifying_scope;
3090 tree saved_object_scope;
3091 tree scope;
eea9800f 3092 bool only_class_p;
a723baf1 3093
a723baf1
MM
3094 /* Before we try to parse the class-name, we must save away the
3095 current PARSER->SCOPE since cp_parser_class_name will destroy
3096 it. */
3097 saved_scope = parser->scope;
3098 saved_qualifying_scope = parser->qualifying_scope;
3099 saved_object_scope = parser->object_scope;
eea9800f
MM
3100 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3101 there is no need to look for a namespace-name. */
bbaab916 3102 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3103 if (!only_class_p)
3104 cp_parser_parse_tentatively (parser);
a723baf1
MM
3105 scope = cp_parser_class_name (parser,
3106 typename_keyword_p,
3107 template_keyword_p,
3108 type_p,
a723baf1
MM
3109 check_dependency_p,
3110 /*class_head_p=*/false);
3111 /* If that didn't work, try for a namespace-name. */
eea9800f 3112 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3113 {
3114 /* Restore the saved scope. */
3115 parser->scope = saved_scope;
3116 parser->qualifying_scope = saved_qualifying_scope;
3117 parser->object_scope = saved_object_scope;
eea9800f
MM
3118 /* If we are not looking at an identifier followed by the scope
3119 resolution operator, then this is not part of a
3120 nested-name-specifier. (Note that this function is only used
3121 to parse the components of a nested-name-specifier.) */
3122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3123 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3124 return error_mark_node;
a723baf1
MM
3125 scope = cp_parser_namespace_name (parser);
3126 }
3127
3128 return scope;
3129}
3130
3131/* Parse a postfix-expression.
3132
3133 postfix-expression:
3134 primary-expression
3135 postfix-expression [ expression ]
3136 postfix-expression ( expression-list [opt] )
3137 simple-type-specifier ( expression-list [opt] )
3138 typename :: [opt] nested-name-specifier identifier
3139 ( expression-list [opt] )
3140 typename :: [opt] nested-name-specifier template [opt] template-id
3141 ( expression-list [opt] )
3142 postfix-expression . template [opt] id-expression
3143 postfix-expression -> template [opt] id-expression
3144 postfix-expression . pseudo-destructor-name
3145 postfix-expression -> pseudo-destructor-name
3146 postfix-expression ++
3147 postfix-expression --
3148 dynamic_cast < type-id > ( expression )
3149 static_cast < type-id > ( expression )
3150 reinterpret_cast < type-id > ( expression )
3151 const_cast < type-id > ( expression )
3152 typeid ( expression )
3153 typeid ( type-id )
3154
3155 GNU Extension:
3156
3157 postfix-expression:
3158 ( type-id ) { initializer-list , [opt] }
3159
3160 This extension is a GNU version of the C99 compound-literal
3161 construct. (The C99 grammar uses `type-name' instead of `type-id',
3162 but they are essentially the same concept.)
3163
3164 If ADDRESS_P is true, the postfix expression is the operand of the
3165 `&' operator.
3166
3167 Returns a representation of the expression. */
3168
3169static tree
3170cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3171{
3172 cp_token *token;
3173 enum rid keyword;
b3445994 3174 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3175 tree postfix_expression = NULL_TREE;
3176 /* Non-NULL only if the current postfix-expression can be used to
3177 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3178 class used to qualify the member. */
3179 tree qualifying_class = NULL_TREE;
a723baf1
MM
3180
3181 /* Peek at the next token. */
3182 token = cp_lexer_peek_token (parser->lexer);
3183 /* Some of the productions are determined by keywords. */
3184 keyword = token->keyword;
3185 switch (keyword)
3186 {
3187 case RID_DYNCAST:
3188 case RID_STATCAST:
3189 case RID_REINTCAST:
3190 case RID_CONSTCAST:
3191 {
3192 tree type;
3193 tree expression;
3194 const char *saved_message;
3195
3196 /* All of these can be handled in the same way from the point
3197 of view of parsing. Begin by consuming the token
3198 identifying the cast. */
3199 cp_lexer_consume_token (parser->lexer);
3200
3201 /* New types cannot be defined in the cast. */
3202 saved_message = parser->type_definition_forbidden_message;
3203 parser->type_definition_forbidden_message
3204 = "types may not be defined in casts";
3205
3206 /* Look for the opening `<'. */
3207 cp_parser_require (parser, CPP_LESS, "`<'");
3208 /* Parse the type to which we are casting. */
3209 type = cp_parser_type_id (parser);
3210 /* Look for the closing `>'. */
3211 cp_parser_require (parser, CPP_GREATER, "`>'");
3212 /* Restore the old message. */
3213 parser->type_definition_forbidden_message = saved_message;
3214
3215 /* And the expression which is being cast. */
3216 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3217 expression = cp_parser_expression (parser);
3218 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3219
14d22dd6
MM
3220 /* Only type conversions to integral or enumeration types
3221 can be used in constant-expressions. */
3222 if (parser->constant_expression_p
3223 && !dependent_type_p (type)
3224 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3225 {
3226 if (!parser->allow_non_constant_expression_p)
3227 return (cp_parser_non_constant_expression
3228 ("a cast to a type other than an integral or "
3229 "enumeration type"));
3230 parser->non_constant_expression_p = true;
3231 }
3232
a723baf1
MM
3233 switch (keyword)
3234 {
3235 case RID_DYNCAST:
3236 postfix_expression
3237 = build_dynamic_cast (type, expression);
3238 break;
3239 case RID_STATCAST:
3240 postfix_expression
3241 = build_static_cast (type, expression);
3242 break;
3243 case RID_REINTCAST:
3244 postfix_expression
3245 = build_reinterpret_cast (type, expression);
3246 break;
3247 case RID_CONSTCAST:
3248 postfix_expression
3249 = build_const_cast (type, expression);
3250 break;
3251 default:
3252 abort ();
3253 }
3254 }
3255 break;
3256
3257 case RID_TYPEID:
3258 {
3259 tree type;
3260 const char *saved_message;
3261
3262 /* Consume the `typeid' token. */
3263 cp_lexer_consume_token (parser->lexer);
3264 /* Look for the `(' token. */
3265 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3266 /* Types cannot be defined in a `typeid' expression. */
3267 saved_message = parser->type_definition_forbidden_message;
3268 parser->type_definition_forbidden_message
3269 = "types may not be defined in a `typeid\' expression";
3270 /* We can't be sure yet whether we're looking at a type-id or an
3271 expression. */
3272 cp_parser_parse_tentatively (parser);
3273 /* Try a type-id first. */
3274 type = cp_parser_type_id (parser);
3275 /* Look for the `)' token. Otherwise, we can't be sure that
3276 we're not looking at an expression: consider `typeid (int
3277 (3))', for example. */
3278 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3279 /* If all went well, simply lookup the type-id. */
3280 if (cp_parser_parse_definitely (parser))
3281 postfix_expression = get_typeid (type);
3282 /* Otherwise, fall back to the expression variant. */
3283 else
3284 {
3285 tree expression;
3286
3287 /* Look for an expression. */
3288 expression = cp_parser_expression (parser);
3289 /* Compute its typeid. */
3290 postfix_expression = build_typeid (expression);
3291 /* Look for the `)' token. */
3292 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3293 }
3294
3295 /* Restore the saved message. */
3296 parser->type_definition_forbidden_message = saved_message;
3297 }
3298 break;
3299
3300 case RID_TYPENAME:
3301 {
3302 bool template_p = false;
3303 tree id;
3304 tree type;
3305
3306 /* Consume the `typename' token. */
3307 cp_lexer_consume_token (parser->lexer);
3308 /* Look for the optional `::' operator. */
3309 cp_parser_global_scope_opt (parser,
3310 /*current_scope_valid_p=*/false);
3311 /* Look for the nested-name-specifier. */
3312 cp_parser_nested_name_specifier (parser,
3313 /*typename_keyword_p=*/true,
3314 /*check_dependency_p=*/true,
3315 /*type_p=*/true);
3316 /* Look for the optional `template' keyword. */
3317 template_p = cp_parser_optional_template_keyword (parser);
3318 /* We don't know whether we're looking at a template-id or an
3319 identifier. */
3320 cp_parser_parse_tentatively (parser);
3321 /* Try a template-id. */
3322 id = cp_parser_template_id (parser, template_p,
3323 /*check_dependency_p=*/true);
3324 /* If that didn't work, try an identifier. */
3325 if (!cp_parser_parse_definitely (parser))
3326 id = cp_parser_identifier (parser);
3327 /* Create a TYPENAME_TYPE to represent the type to which the
3328 functional cast is being performed. */
3329 type = make_typename_type (parser->scope, id,
3330 /*complain=*/1);
3331
3332 postfix_expression = cp_parser_functional_cast (parser, type);
3333 }
3334 break;
3335
3336 default:
3337 {
3338 tree type;
3339
3340 /* If the next thing is a simple-type-specifier, we may be
3341 looking at a functional cast. We could also be looking at
3342 an id-expression. So, we try the functional cast, and if
3343 that doesn't work we fall back to the primary-expression. */
3344 cp_parser_parse_tentatively (parser);
3345 /* Look for the simple-type-specifier. */
3346 type = cp_parser_simple_type_specifier (parser,
3347 CP_PARSER_FLAGS_NONE);
3348 /* Parse the cast itself. */
3349 if (!cp_parser_error_occurred (parser))
3350 postfix_expression
3351 = cp_parser_functional_cast (parser, type);
3352 /* If that worked, we're done. */
3353 if (cp_parser_parse_definitely (parser))
3354 break;
3355
3356 /* If the functional-cast didn't work out, try a
3357 compound-literal. */
14d22dd6
MM
3358 if (cp_parser_allow_gnu_extensions_p (parser)
3359 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3360 {
3361 tree initializer_list = NULL_TREE;
3362
3363 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3364 /* Consume the `('. */
3365 cp_lexer_consume_token (parser->lexer);
3366 /* Parse the type. */
3367 type = cp_parser_type_id (parser);
3368 /* Look for the `)'. */
3369 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3370 /* Look for the `{'. */
3371 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3372 /* If things aren't going well, there's no need to
3373 keep going. */
3374 if (!cp_parser_error_occurred (parser))
a723baf1 3375 {
39703eb9 3376 bool non_constant_p;
14d22dd6
MM
3377 /* Parse the initializer-list. */
3378 initializer_list
39703eb9 3379 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3380 /* Allow a trailing `,'. */
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3382 cp_lexer_consume_token (parser->lexer);
3383 /* Look for the final `}'. */
3384 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3385 }
3386 /* If that worked, we're definitely looking at a
3387 compound-literal expression. */
3388 if (cp_parser_parse_definitely (parser))
3389 {
3390 /* Warn the user that a compound literal is not
3391 allowed in standard C++. */
3392 if (pedantic)
3393 pedwarn ("ISO C++ forbids compound-literals");
3394 /* Form the representation of the compound-literal. */
3395 postfix_expression
3396 = finish_compound_literal (type, initializer_list);
3397 break;
3398 }
3399 }
3400
3401 /* It must be a primary-expression. */
3402 postfix_expression = cp_parser_primary_expression (parser,
3403 &idk,
3404 &qualifying_class);
3405 }
3406 break;
3407 }
3408
ee76b931
MM
3409 /* If we were avoiding committing to the processing of a
3410 qualified-id until we knew whether or not we had a
3411 pointer-to-member, we now know. */
089d6ea7 3412 if (qualifying_class)
a723baf1 3413 {
ee76b931 3414 bool done;
a723baf1 3415
ee76b931
MM
3416 /* Peek at the next token. */
3417 token = cp_lexer_peek_token (parser->lexer);
3418 done = (token->type != CPP_OPEN_SQUARE
3419 && token->type != CPP_OPEN_PAREN
3420 && token->type != CPP_DOT
3421 && token->type != CPP_DEREF
3422 && token->type != CPP_PLUS_PLUS
3423 && token->type != CPP_MINUS_MINUS);
3424
3425 postfix_expression = finish_qualified_id_expr (qualifying_class,
3426 postfix_expression,
3427 done,
3428 address_p);
3429 if (done)
3430 return postfix_expression;
a723baf1
MM
3431 }
3432
3433 /* Remember that there was a reference to this entity. */
3434 if (DECL_P (postfix_expression))
3435 mark_used (postfix_expression);
3436
3437 /* Keep looping until the postfix-expression is complete. */
3438 while (true)
3439 {
10b1d5e7
MM
3440 if (idk == CP_ID_KIND_UNQUALIFIED
3441 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3442 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3443 /* It is not a Koenig lookup function call. */
3444 postfix_expression
3445 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3446
3447 /* Peek at the next token. */
3448 token = cp_lexer_peek_token (parser->lexer);
3449
3450 switch (token->type)
3451 {
3452 case CPP_OPEN_SQUARE:
3453 /* postfix-expression [ expression ] */
3454 {
3455 tree index;
3456
3457 /* Consume the `[' token. */
3458 cp_lexer_consume_token (parser->lexer);
3459 /* Parse the index expression. */
3460 index = cp_parser_expression (parser);
3461 /* Look for the closing `]'. */
3462 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3463
3464 /* Build the ARRAY_REF. */
3465 postfix_expression
3466 = grok_array_decl (postfix_expression, index);
b3445994 3467 idk = CP_ID_KIND_NONE;
a723baf1
MM
3468 }
3469 break;
3470
3471 case CPP_OPEN_PAREN:
3472 /* postfix-expression ( expression-list [opt] ) */
3473 {
39703eb9
MM
3474 tree args = (cp_parser_parenthesized_expression_list
3475 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3476
7efa3e22
NS
3477 if (args == error_mark_node)
3478 {
3479 postfix_expression = error_mark_node;
3480 break;
3481 }
3482
14d22dd6
MM
3483 /* Function calls are not permitted in
3484 constant-expressions. */
3485 if (parser->constant_expression_p)
3486 {
3487 if (!parser->allow_non_constant_expression_p)
3488 return cp_parser_non_constant_expression ("a function call");
3489 parser->non_constant_expression_p = true;
3490 }
a723baf1 3491
b3445994 3492 if (idk == CP_ID_KIND_UNQUALIFIED
a723baf1
MM
3493 && (is_overloaded_fn (postfix_expression)
3494 || DECL_P (postfix_expression)
3495 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3496 && args)
b3445994
MM
3497 postfix_expression
3498 = perform_koenig_lookup (postfix_expression, args);
3499 else if (idk == CP_ID_KIND_UNQUALIFIED
a723baf1 3500 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
b3445994
MM
3501 postfix_expression
3502 = unqualified_fn_lookup_error (postfix_expression);
a723baf1 3503
d17811fd 3504 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3505 {
d17811fd
MM
3506 tree instance = TREE_OPERAND (postfix_expression, 0);
3507 tree fn = TREE_OPERAND (postfix_expression, 1);
3508
3509 if (processing_template_decl
3510 && (type_dependent_expression_p (instance)
3511 || (!BASELINK_P (fn)
3512 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3513 || type_dependent_expression_p (fn)
d17811fd
MM
3514 || any_type_dependent_arguments_p (args)))
3515 {
3516 postfix_expression
3517 = build_min_nt (CALL_EXPR, postfix_expression, args);
3518 break;
3519 }
3520
3521 postfix_expression
3522 = (build_new_method_call
3523 (instance, fn, args, NULL_TREE,
b3445994 3524 (idk == CP_ID_KIND_QUALIFIED
d17811fd 3525 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
a723baf1 3526 }
d17811fd
MM
3527 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3528 || TREE_CODE (postfix_expression) == MEMBER_REF
3529 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3530 postfix_expression = (build_offset_ref_call_from_tree
3531 (postfix_expression, args));
b3445994 3532 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3533 /* A call to a static class member, or a namespace-scope
3534 function. */
3535 postfix_expression
3536 = finish_call_expr (postfix_expression, args,
3537 /*disallow_virtual=*/true);
a723baf1 3538 else
2050a1bb
MM
3539 /* All other function calls. */
3540 postfix_expression
3541 = finish_call_expr (postfix_expression, args,
3542 /*disallow_virtual=*/false);
a723baf1
MM
3543
3544 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3545 idk = CP_ID_KIND_NONE;
a723baf1
MM
3546 }
3547 break;
3548
3549 case CPP_DOT:
3550 case CPP_DEREF:
3551 /* postfix-expression . template [opt] id-expression
3552 postfix-expression . pseudo-destructor-name
3553 postfix-expression -> template [opt] id-expression
3554 postfix-expression -> pseudo-destructor-name */
3555 {
3556 tree name;
3557 bool dependent_p;
3558 bool template_p;
3559 tree scope = NULL_TREE;
3560
3561 /* If this is a `->' operator, dereference the pointer. */
3562 if (token->type == CPP_DEREF)
3563 postfix_expression = build_x_arrow (postfix_expression);
3564 /* Check to see whether or not the expression is
3565 type-dependent. */
bbaab916 3566 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3567 /* The identifier following the `->' or `.' is not
3568 qualified. */
3569 parser->scope = NULL_TREE;
3570 parser->qualifying_scope = NULL_TREE;
3571 parser->object_scope = NULL_TREE;
b3445994 3572 idk = CP_ID_KIND_NONE;
a723baf1
MM
3573 /* Enter the scope corresponding to the type of the object
3574 given by the POSTFIX_EXPRESSION. */
3575 if (!dependent_p
3576 && TREE_TYPE (postfix_expression) != NULL_TREE)
3577 {
3578 scope = TREE_TYPE (postfix_expression);
3579 /* According to the standard, no expression should
3580 ever have reference type. Unfortunately, we do not
3581 currently match the standard in this respect in
3582 that our internal representation of an expression
3583 may have reference type even when the standard says
3584 it does not. Therefore, we have to manually obtain
3585 the underlying type here. */
ee76b931 3586 scope = non_reference (scope);
a723baf1
MM
3587 /* If the SCOPE is an OFFSET_TYPE, then we grab the
3588 type of the field. We get an OFFSET_TYPE for
3589 something like:
3590
3591 S::T.a ...
3592
3593 Probably, we should not get an OFFSET_TYPE here;
3594 that transformation should be made only if `&S::T'
3595 is written. */
3596 if (TREE_CODE (scope) == OFFSET_TYPE)
3597 scope = TREE_TYPE (scope);
3598 /* The type of the POSTFIX_EXPRESSION must be
3599 complete. */
3600 scope = complete_type_or_else (scope, NULL_TREE);
3601 /* Let the name lookup machinery know that we are
3602 processing a class member access expression. */
3603 parser->context->object_type = scope;
3604 /* If something went wrong, we want to be able to
3605 discern that case, as opposed to the case where
3606 there was no SCOPE due to the type of expression
3607 being dependent. */
3608 if (!scope)
3609 scope = error_mark_node;
3610 }
3611
3612 /* Consume the `.' or `->' operator. */
3613 cp_lexer_consume_token (parser->lexer);
3614 /* If the SCOPE is not a scalar type, we are looking at an
3615 ordinary class member access expression, rather than a
3616 pseudo-destructor-name. */
3617 if (!scope || !SCALAR_TYPE_P (scope))
3618 {
3619 template_p = cp_parser_optional_template_keyword (parser);
3620 /* Parse the id-expression. */
3621 name = cp_parser_id_expression (parser,
3622 template_p,
3623 /*check_dependency_p=*/true,
3624 /*template_p=*/NULL);
3625 /* In general, build a SCOPE_REF if the member name is
3626 qualified. However, if the name was not dependent
3627 and has already been resolved; there is no need to
3628 build the SCOPE_REF. For example;
3629
3630 struct X { void f(); };
3631 template <typename T> void f(T* t) { t->X::f(); }
3632
d17811fd
MM
3633 Even though "t" is dependent, "X::f" is not and has
3634 been resolved to a BASELINK; there is no need to
a723baf1 3635 include scope information. */
a6bd211d
JM
3636
3637 /* But we do need to remember that there was an explicit
3638 scope for virtual function calls. */
3639 if (parser->scope)
b3445994 3640 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3641
a723baf1
MM
3642 if (name != error_mark_node
3643 && !BASELINK_P (name)
3644 && parser->scope)
3645 {
3646 name = build_nt (SCOPE_REF, parser->scope, name);
3647 parser->scope = NULL_TREE;
3648 parser->qualifying_scope = NULL_TREE;
3649 parser->object_scope = NULL_TREE;
3650 }
3651 postfix_expression
3652 = finish_class_member_access_expr (postfix_expression, name);
3653 }
3654 /* Otherwise, try the pseudo-destructor-name production. */
3655 else
3656 {
3657 tree s;
3658 tree type;
3659
3660 /* Parse the pseudo-destructor-name. */
3661 cp_parser_pseudo_destructor_name (parser, &s, &type);
3662 /* Form the call. */
3663 postfix_expression
3664 = finish_pseudo_destructor_expr (postfix_expression,
3665 s, TREE_TYPE (type));
3666 }
3667
3668 /* We no longer need to look up names in the scope of the
3669 object on the left-hand side of the `.' or `->'
3670 operator. */
3671 parser->context->object_type = NULL_TREE;
a723baf1
MM
3672 }
3673 break;
3674
3675 case CPP_PLUS_PLUS:
3676 /* postfix-expression ++ */
3677 /* Consume the `++' token. */
3678 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3679 /* Increments may not appear in constant-expressions. */
3680 if (parser->constant_expression_p)
3681 {
3682 if (!parser->allow_non_constant_expression_p)
3683 return cp_parser_non_constant_expression ("an increment");
3684 parser->non_constant_expression_p = true;
3685 }
34cd5ae7 3686 /* Generate a representation for the complete expression. */
a723baf1
MM
3687 postfix_expression
3688 = finish_increment_expr (postfix_expression,
3689 POSTINCREMENT_EXPR);
b3445994 3690 idk = CP_ID_KIND_NONE;
a723baf1
MM
3691 break;
3692
3693 case CPP_MINUS_MINUS:
3694 /* postfix-expression -- */
3695 /* Consume the `--' token. */
3696 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
3697 /* Decrements may not appear in constant-expressions. */
3698 if (parser->constant_expression_p)
3699 {
3700 if (!parser->allow_non_constant_expression_p)
3701 return cp_parser_non_constant_expression ("a decrement");
3702 parser->non_constant_expression_p = true;
3703 }
34cd5ae7 3704 /* Generate a representation for the complete expression. */
a723baf1
MM
3705 postfix_expression
3706 = finish_increment_expr (postfix_expression,
3707 POSTDECREMENT_EXPR);
b3445994 3708 idk = CP_ID_KIND_NONE;
a723baf1
MM
3709 break;
3710
3711 default:
3712 return postfix_expression;
3713 }
3714 }
3715
3716 /* We should never get here. */
3717 abort ();
3718 return error_mark_node;
3719}
3720
7efa3e22 3721/* Parse a parenthesized expression-list.
a723baf1
MM
3722
3723 expression-list:
3724 assignment-expression
3725 expression-list, assignment-expression
3726
7efa3e22
NS
3727 attribute-list:
3728 expression-list
3729 identifier
3730 identifier, expression-list
3731
a723baf1
MM
3732 Returns a TREE_LIST. The TREE_VALUE of each node is a
3733 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
3734 is returned even if there is only a single expression in the list.
3735 error_mark_node is returned if the ( and or ) are
3736 missing. NULL_TREE is returned on no expressions. The parentheses
3737 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
3738 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3739 indicates whether or not all of the expressions in the list were
3740 constant. */
a723baf1
MM
3741
3742static tree
39703eb9
MM
3743cp_parser_parenthesized_expression_list (cp_parser* parser,
3744 bool is_attribute_list,
3745 bool *non_constant_p)
a723baf1
MM
3746{
3747 tree expression_list = NULL_TREE;
7efa3e22 3748 tree identifier = NULL_TREE;
39703eb9
MM
3749
3750 /* Assume all the expressions will be constant. */
3751 if (non_constant_p)
3752 *non_constant_p = false;
3753
7efa3e22
NS
3754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3755 return error_mark_node;
3756
a723baf1 3757 /* Consume expressions until there are no more. */
7efa3e22
NS
3758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3759 while (true)
3760 {
3761 tree expr;
3762
3763 /* At the beginning of attribute lists, check to see if the
3764 next token is an identifier. */
3765 if (is_attribute_list
3766 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3767 {
3768 cp_token *token;
3769
3770 /* Consume the identifier. */
3771 token = cp_lexer_consume_token (parser->lexer);
3772 /* Save the identifier. */
3773 identifier = token->value;
3774 }
3775 else
3776 {
3777 /* Parse the next assignment-expression. */
39703eb9
MM
3778 if (non_constant_p)
3779 {
3780 bool expr_non_constant_p;
3781 expr = (cp_parser_constant_expression
3782 (parser, /*allow_non_constant_p=*/true,
3783 &expr_non_constant_p));
3784 if (expr_non_constant_p)
3785 *non_constant_p = true;
3786 }
3787 else
3788 expr = cp_parser_assignment_expression (parser);
a723baf1 3789
7efa3e22
NS
3790 /* Add it to the list. We add error_mark_node
3791 expressions to the list, so that we can still tell if
3792 the correct form for a parenthesized expression-list
3793 is found. That gives better errors. */
3794 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 3795
7efa3e22
NS
3796 if (expr == error_mark_node)
3797 goto skip_comma;
3798 }
a723baf1 3799
7efa3e22
NS
3800 /* After the first item, attribute lists look the same as
3801 expression lists. */
3802 is_attribute_list = false;
3803
3804 get_comma:;
3805 /* If the next token isn't a `,', then we are done. */
3806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3807 break;
3808
3809 /* Otherwise, consume the `,' and keep going. */
3810 cp_lexer_consume_token (parser->lexer);
3811 }
3812
3813 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3814 {
3815 int ending;
3816
3817 skip_comma:;
3818 /* We try and resync to an unnested comma, as that will give the
3819 user better diagnostics. */
3820 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3821 if (ending < 0)
3822 goto get_comma;
3823 if (!ending)
3824 return error_mark_node;
a723baf1
MM
3825 }
3826
3827 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
3828 expression_list = nreverse (expression_list);
3829 if (identifier)
3830 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3831
3832 return expression_list;
a723baf1
MM
3833}
3834
3835/* Parse a pseudo-destructor-name.
3836
3837 pseudo-destructor-name:
3838 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3839 :: [opt] nested-name-specifier template template-id :: ~ type-name
3840 :: [opt] nested-name-specifier [opt] ~ type-name
3841
3842 If either of the first two productions is used, sets *SCOPE to the
3843 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3844 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3845 or ERROR_MARK_NODE if no type-name is present. */
3846
3847static void
94edc4ab
NN
3848cp_parser_pseudo_destructor_name (cp_parser* parser,
3849 tree* scope,
3850 tree* type)
a723baf1
MM
3851{
3852 bool nested_name_specifier_p;
3853
3854 /* Look for the optional `::' operator. */
3855 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3856 /* Look for the optional nested-name-specifier. */
3857 nested_name_specifier_p
3858 = (cp_parser_nested_name_specifier_opt (parser,
3859 /*typename_keyword_p=*/false,
3860 /*check_dependency_p=*/true,
3861 /*type_p=*/false)
3862 != NULL_TREE);
3863 /* Now, if we saw a nested-name-specifier, we might be doing the
3864 second production. */
3865 if (nested_name_specifier_p
3866 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3867 {
3868 /* Consume the `template' keyword. */
3869 cp_lexer_consume_token (parser->lexer);
3870 /* Parse the template-id. */
3871 cp_parser_template_id (parser,
3872 /*template_keyword_p=*/true,
3873 /*check_dependency_p=*/false);
3874 /* Look for the `::' token. */
3875 cp_parser_require (parser, CPP_SCOPE, "`::'");
3876 }
3877 /* If the next token is not a `~', then there might be some
9bcb9aae 3878 additional qualification. */
a723baf1
MM
3879 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3880 {
3881 /* Look for the type-name. */
3882 *scope = TREE_TYPE (cp_parser_type_name (parser));
3883 /* Look for the `::' token. */
3884 cp_parser_require (parser, CPP_SCOPE, "`::'");
3885 }
3886 else
3887 *scope = NULL_TREE;
3888
3889 /* Look for the `~'. */
3890 cp_parser_require (parser, CPP_COMPL, "`~'");
3891 /* Look for the type-name again. We are not responsible for
3892 checking that it matches the first type-name. */
3893 *type = cp_parser_type_name (parser);
3894}
3895
3896/* Parse a unary-expression.
3897
3898 unary-expression:
3899 postfix-expression
3900 ++ cast-expression
3901 -- cast-expression
3902 unary-operator cast-expression
3903 sizeof unary-expression
3904 sizeof ( type-id )
3905 new-expression
3906 delete-expression
3907
3908 GNU Extensions:
3909
3910 unary-expression:
3911 __extension__ cast-expression
3912 __alignof__ unary-expression
3913 __alignof__ ( type-id )
3914 __real__ cast-expression
3915 __imag__ cast-expression
3916 && identifier
3917
3918 ADDRESS_P is true iff the unary-expression is appearing as the
3919 operand of the `&' operator.
3920
34cd5ae7 3921 Returns a representation of the expression. */
a723baf1
MM
3922
3923static tree
3924cp_parser_unary_expression (cp_parser *parser, bool address_p)
3925{
3926 cp_token *token;
3927 enum tree_code unary_operator;
3928
3929 /* Peek at the next token. */
3930 token = cp_lexer_peek_token (parser->lexer);
3931 /* Some keywords give away the kind of expression. */
3932 if (token->type == CPP_KEYWORD)
3933 {
3934 enum rid keyword = token->keyword;
3935
3936 switch (keyword)
3937 {
3938 case RID_ALIGNOF:
3939 {
3940 /* Consume the `alignof' token. */
3941 cp_lexer_consume_token (parser->lexer);
3942 /* Parse the operand. */
3943 return finish_alignof (cp_parser_sizeof_operand
3944 (parser, keyword));
3945 }
3946
3947 case RID_SIZEOF:
3948 {
3949 tree operand;
3950
9bcb9aae 3951 /* Consume the `sizeof' token. */
a723baf1
MM
3952 cp_lexer_consume_token (parser->lexer);
3953 /* Parse the operand. */
3954 operand = cp_parser_sizeof_operand (parser, keyword);
3955
3956 /* If the type of the operand cannot be determined build a
3957 SIZEOF_EXPR. */
3958 if (TYPE_P (operand)
1fb3244a
MM
3959 ? dependent_type_p (operand)
3960 : type_dependent_expression_p (operand))
a723baf1
MM
3961 return build_min (SIZEOF_EXPR, size_type_node, operand);
3962 /* Otherwise, compute the constant value. */
3963 else
3964 return finish_sizeof (operand);
3965 }
3966
3967 case RID_NEW:
3968 return cp_parser_new_expression (parser);
3969
3970 case RID_DELETE:
3971 return cp_parser_delete_expression (parser);
3972
3973 case RID_EXTENSION:
3974 {
3975 /* The saved value of the PEDANTIC flag. */
3976 int saved_pedantic;
3977 tree expr;
3978
3979 /* Save away the PEDANTIC flag. */
3980 cp_parser_extension_opt (parser, &saved_pedantic);
3981 /* Parse the cast-expression. */
d6b4ea85 3982 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
3983 /* Restore the PEDANTIC flag. */
3984 pedantic = saved_pedantic;
3985
3986 return expr;
3987 }
3988
3989 case RID_REALPART:
3990 case RID_IMAGPART:
3991 {
3992 tree expression;
3993
3994 /* Consume the `__real__' or `__imag__' token. */
3995 cp_lexer_consume_token (parser->lexer);
3996 /* Parse the cast-expression. */
d6b4ea85 3997 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
3998 /* Create the complete representation. */
3999 return build_x_unary_op ((keyword == RID_REALPART
4000 ? REALPART_EXPR : IMAGPART_EXPR),
4001 expression);
4002 }
4003 break;
4004
4005 default:
4006 break;
4007 }
4008 }
4009
4010 /* Look for the `:: new' and `:: delete', which also signal the
4011 beginning of a new-expression, or delete-expression,
4012 respectively. If the next token is `::', then it might be one of
4013 these. */
4014 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4015 {
4016 enum rid keyword;
4017
4018 /* See if the token after the `::' is one of the keywords in
4019 which we're interested. */
4020 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4021 /* If it's `new', we have a new-expression. */
4022 if (keyword == RID_NEW)
4023 return cp_parser_new_expression (parser);
4024 /* Similarly, for `delete'. */
4025 else if (keyword == RID_DELETE)
4026 return cp_parser_delete_expression (parser);
4027 }
4028
4029 /* Look for a unary operator. */
4030 unary_operator = cp_parser_unary_operator (token);
4031 /* The `++' and `--' operators can be handled similarly, even though
4032 they are not technically unary-operators in the grammar. */
4033 if (unary_operator == ERROR_MARK)
4034 {
4035 if (token->type == CPP_PLUS_PLUS)
4036 unary_operator = PREINCREMENT_EXPR;
4037 else if (token->type == CPP_MINUS_MINUS)
4038 unary_operator = PREDECREMENT_EXPR;
4039 /* Handle the GNU address-of-label extension. */
4040 else if (cp_parser_allow_gnu_extensions_p (parser)
4041 && token->type == CPP_AND_AND)
4042 {
4043 tree identifier;
4044
4045 /* Consume the '&&' token. */
4046 cp_lexer_consume_token (parser->lexer);
4047 /* Look for the identifier. */
4048 identifier = cp_parser_identifier (parser);
4049 /* Create an expression representing the address. */
4050 return finish_label_address_expr (identifier);
4051 }
4052 }
4053 if (unary_operator != ERROR_MARK)
4054 {
4055 tree cast_expression;
4056
4057 /* Consume the operator token. */
4058 token = cp_lexer_consume_token (parser->lexer);
4059 /* Parse the cast-expression. */
4060 cast_expression
4061 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4062 /* Now, build an appropriate representation. */
4063 switch (unary_operator)
4064 {
4065 case INDIRECT_REF:
4066 return build_x_indirect_ref (cast_expression, "unary *");
4067
4068 case ADDR_EXPR:
d17811fd
MM
4069 case BIT_NOT_EXPR:
4070 return build_x_unary_op (unary_operator, cast_expression);
a723baf1 4071
14d22dd6
MM
4072 case PREINCREMENT_EXPR:
4073 case PREDECREMENT_EXPR:
4074 if (parser->constant_expression_p)
4075 {
4076 if (!parser->allow_non_constant_expression_p)
4077 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4078 ? "an increment"
4079 : "a decrement");
4080 parser->non_constant_expression_p = true;
4081 }
4082 /* Fall through. */
a723baf1
MM
4083 case CONVERT_EXPR:
4084 case NEGATE_EXPR:
4085 case TRUTH_NOT_EXPR:
a723baf1
MM
4086 return finish_unary_op_expr (unary_operator, cast_expression);
4087
a723baf1
MM
4088 default:
4089 abort ();
4090 return error_mark_node;
4091 }
4092 }
4093
4094 return cp_parser_postfix_expression (parser, address_p);
4095}
4096
4097/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4098 unary-operator, the corresponding tree code is returned. */
4099
4100static enum tree_code
94edc4ab 4101cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4102{
4103 switch (token->type)
4104 {
4105 case CPP_MULT:
4106 return INDIRECT_REF;
4107
4108 case CPP_AND:
4109 return ADDR_EXPR;
4110
4111 case CPP_PLUS:
4112 return CONVERT_EXPR;
4113
4114 case CPP_MINUS:
4115 return NEGATE_EXPR;
4116
4117 case CPP_NOT:
4118 return TRUTH_NOT_EXPR;
4119
4120 case CPP_COMPL:
4121 return BIT_NOT_EXPR;
4122
4123 default:
4124 return ERROR_MARK;
4125 }
4126}
4127
4128/* Parse a new-expression.
4129
ca099ac8 4130 new-expression:
a723baf1
MM
4131 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4132 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4133
4134 Returns a representation of the expression. */
4135
4136static tree
94edc4ab 4137cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4138{
4139 bool global_scope_p;
4140 tree placement;
4141 tree type;
4142 tree initializer;
4143
4144 /* Look for the optional `::' operator. */
4145 global_scope_p
4146 = (cp_parser_global_scope_opt (parser,
4147 /*current_scope_valid_p=*/false)
4148 != NULL_TREE);
4149 /* Look for the `new' operator. */
4150 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4151 /* There's no easy way to tell a new-placement from the
4152 `( type-id )' construct. */
4153 cp_parser_parse_tentatively (parser);
4154 /* Look for a new-placement. */
4155 placement = cp_parser_new_placement (parser);
4156 /* If that didn't work out, there's no new-placement. */
4157 if (!cp_parser_parse_definitely (parser))
4158 placement = NULL_TREE;
4159
4160 /* If the next token is a `(', then we have a parenthesized
4161 type-id. */
4162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4163 {
4164 /* Consume the `('. */
4165 cp_lexer_consume_token (parser->lexer);
4166 /* Parse the type-id. */
4167 type = cp_parser_type_id (parser);
4168 /* Look for the closing `)'. */
4169 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4170 }
4171 /* Otherwise, there must be a new-type-id. */
4172 else
4173 type = cp_parser_new_type_id (parser);
4174
4175 /* If the next token is a `(', then we have a new-initializer. */
4176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4177 initializer = cp_parser_new_initializer (parser);
4178 else
4179 initializer = NULL_TREE;
4180
4181 /* Create a representation of the new-expression. */
4182 return build_new (placement, type, initializer, global_scope_p);
4183}
4184
4185/* Parse a new-placement.
4186
4187 new-placement:
4188 ( expression-list )
4189
4190 Returns the same representation as for an expression-list. */
4191
4192static tree
94edc4ab 4193cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4194{
4195 tree expression_list;
4196
a723baf1 4197 /* Parse the expression-list. */
39703eb9
MM
4198 expression_list = (cp_parser_parenthesized_expression_list
4199 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4200
4201 return expression_list;
4202}
4203
4204/* Parse a new-type-id.
4205
4206 new-type-id:
4207 type-specifier-seq new-declarator [opt]
4208
4209 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4210 and whose TREE_VALUE is the new-declarator. */
4211
4212static tree
94edc4ab 4213cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4214{
4215 tree type_specifier_seq;
4216 tree declarator;
4217 const char *saved_message;
4218
4219 /* The type-specifier sequence must not contain type definitions.
4220 (It cannot contain declarations of new types either, but if they
4221 are not definitions we will catch that because they are not
4222 complete.) */
4223 saved_message = parser->type_definition_forbidden_message;
4224 parser->type_definition_forbidden_message
4225 = "types may not be defined in a new-type-id";
4226 /* Parse the type-specifier-seq. */
4227 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4228 /* Restore the old message. */
4229 parser->type_definition_forbidden_message = saved_message;
4230 /* Parse the new-declarator. */
4231 declarator = cp_parser_new_declarator_opt (parser);
4232
4233 return build_tree_list (type_specifier_seq, declarator);
4234}
4235
4236/* Parse an (optional) new-declarator.
4237
4238 new-declarator:
4239 ptr-operator new-declarator [opt]
4240 direct-new-declarator
4241
4242 Returns a representation of the declarator. See
4243 cp_parser_declarator for the representations used. */
4244
4245static tree
94edc4ab 4246cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4247{
4248 enum tree_code code;
4249 tree type;
4250 tree cv_qualifier_seq;
4251
4252 /* We don't know if there's a ptr-operator next, or not. */
4253 cp_parser_parse_tentatively (parser);
4254 /* Look for a ptr-operator. */
4255 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4256 /* If that worked, look for more new-declarators. */
4257 if (cp_parser_parse_definitely (parser))
4258 {
4259 tree declarator;
4260
4261 /* Parse another optional declarator. */
4262 declarator = cp_parser_new_declarator_opt (parser);
4263
4264 /* Create the representation of the declarator. */
4265 if (code == INDIRECT_REF)
4266 declarator = make_pointer_declarator (cv_qualifier_seq,
4267 declarator);
4268 else
4269 declarator = make_reference_declarator (cv_qualifier_seq,
4270 declarator);
4271
4272 /* Handle the pointer-to-member case. */
4273 if (type)
4274 declarator = build_nt (SCOPE_REF, type, declarator);
4275
4276 return declarator;
4277 }
4278
4279 /* If the next token is a `[', there is a direct-new-declarator. */
4280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4281 return cp_parser_direct_new_declarator (parser);
4282
4283 return NULL_TREE;
4284}
4285
4286/* Parse a direct-new-declarator.
4287
4288 direct-new-declarator:
4289 [ expression ]
4290 direct-new-declarator [constant-expression]
4291
4292 Returns an ARRAY_REF, following the same conventions as are
4293 documented for cp_parser_direct_declarator. */
4294
4295static tree
94edc4ab 4296cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4297{
4298 tree declarator = NULL_TREE;
4299
4300 while (true)
4301 {
4302 tree expression;
4303
4304 /* Look for the opening `['. */
4305 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4306 /* The first expression is not required to be constant. */
4307 if (!declarator)
4308 {
4309 expression = cp_parser_expression (parser);
4310 /* The standard requires that the expression have integral
4311 type. DR 74 adds enumeration types. We believe that the
4312 real intent is that these expressions be handled like the
4313 expression in a `switch' condition, which also allows
4314 classes with a single conversion to integral or
4315 enumeration type. */
4316 if (!processing_template_decl)
4317 {
4318 expression
4319 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4320 expression,
b746c5dc 4321 /*complain=*/true);
a723baf1
MM
4322 if (!expression)
4323 {
4324 error ("expression in new-declarator must have integral or enumeration type");
4325 expression = error_mark_node;
4326 }
4327 }
4328 }
4329 /* But all the other expressions must be. */
4330 else
14d22dd6
MM
4331 expression
4332 = cp_parser_constant_expression (parser,
4333 /*allow_non_constant=*/false,
4334 NULL);
a723baf1
MM
4335 /* Look for the closing `]'. */
4336 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4337
4338 /* Add this bound to the declarator. */
4339 declarator = build_nt (ARRAY_REF, declarator, expression);
4340
4341 /* If the next token is not a `[', then there are no more
4342 bounds. */
4343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4344 break;
4345 }
4346
4347 return declarator;
4348}
4349
4350/* Parse a new-initializer.
4351
4352 new-initializer:
4353 ( expression-list [opt] )
4354
34cd5ae7 4355 Returns a representation of the expression-list. If there is no
a723baf1
MM
4356 expression-list, VOID_ZERO_NODE is returned. */
4357
4358static tree
94edc4ab 4359cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4360{
4361 tree expression_list;
4362
39703eb9
MM
4363 expression_list = (cp_parser_parenthesized_expression_list
4364 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4365 if (!expression_list)
a723baf1 4366 expression_list = void_zero_node;
a723baf1
MM
4367
4368 return expression_list;
4369}
4370
4371/* Parse a delete-expression.
4372
4373 delete-expression:
4374 :: [opt] delete cast-expression
4375 :: [opt] delete [ ] cast-expression
4376
4377 Returns a representation of the expression. */
4378
4379static tree
94edc4ab 4380cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4381{
4382 bool global_scope_p;
4383 bool array_p;
4384 tree expression;
4385
4386 /* Look for the optional `::' operator. */
4387 global_scope_p
4388 = (cp_parser_global_scope_opt (parser,
4389 /*current_scope_valid_p=*/false)
4390 != NULL_TREE);
4391 /* Look for the `delete' keyword. */
4392 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4393 /* See if the array syntax is in use. */
4394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4395 {
4396 /* Consume the `[' token. */
4397 cp_lexer_consume_token (parser->lexer);
4398 /* Look for the `]' token. */
4399 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4400 /* Remember that this is the `[]' construct. */
4401 array_p = true;
4402 }
4403 else
4404 array_p = false;
4405
4406 /* Parse the cast-expression. */
d6b4ea85 4407 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4408
4409 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4410}
4411
4412/* Parse a cast-expression.
4413
4414 cast-expression:
4415 unary-expression
4416 ( type-id ) cast-expression
4417
4418 Returns a representation of the expression. */
4419
4420static tree
4421cp_parser_cast_expression (cp_parser *parser, bool address_p)
4422{
4423 /* If it's a `(', then we might be looking at a cast. */
4424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4425 {
4426 tree type = NULL_TREE;
4427 tree expr = NULL_TREE;
4428 bool compound_literal_p;
4429 const char *saved_message;
4430
4431 /* There's no way to know yet whether or not this is a cast.
4432 For example, `(int (3))' is a unary-expression, while `(int)
4433 3' is a cast. So, we resort to parsing tentatively. */
4434 cp_parser_parse_tentatively (parser);
4435 /* Types may not be defined in a cast. */
4436 saved_message = parser->type_definition_forbidden_message;
4437 parser->type_definition_forbidden_message
4438 = "types may not be defined in casts";
4439 /* Consume the `('. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* A very tricky bit is that `(struct S) { 3 }' is a
4442 compound-literal (which we permit in C++ as an extension).
4443 But, that construct is not a cast-expression -- it is a
4444 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4445 is legal; if the compound-literal were a cast-expression,
4446 you'd need an extra set of parentheses.) But, if we parse
4447 the type-id, and it happens to be a class-specifier, then we
4448 will commit to the parse at that point, because we cannot
4449 undo the action that is done when creating a new class. So,
4450 then we cannot back up and do a postfix-expression.
4451
4452 Therefore, we scan ahead to the closing `)', and check to see
4453 if the token after the `)' is a `{'. If so, we are not
4454 looking at a cast-expression.
4455
4456 Save tokens so that we can put them back. */
4457 cp_lexer_save_tokens (parser->lexer);
4458 /* Skip tokens until the next token is a closing parenthesis.
4459 If we find the closing `)', and the next token is a `{', then
4460 we are looking at a compound-literal. */
4461 compound_literal_p
7efa3e22 4462 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
a723baf1
MM
4463 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4464 /* Roll back the tokens we skipped. */
4465 cp_lexer_rollback_tokens (parser->lexer);
4466 /* If we were looking at a compound-literal, simulate an error
4467 so that the call to cp_parser_parse_definitely below will
4468 fail. */
4469 if (compound_literal_p)
4470 cp_parser_simulate_error (parser);
4471 else
4472 {
4473 /* Look for the type-id. */
4474 type = cp_parser_type_id (parser);
4475 /* Look for the closing `)'. */
4476 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4477 }
4478
4479 /* Restore the saved message. */
4480 parser->type_definition_forbidden_message = saved_message;
4481
bbaab916
NS
4482 /* If ok so far, parse the dependent expression. We cannot be
4483 sure it is a cast. Consider `(T ())'. It is a parenthesized
4484 ctor of T, but looks like a cast to function returning T
4485 without a dependent expression. */
4486 if (!cp_parser_error_occurred (parser))
d6b4ea85 4487 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4488
a723baf1
MM
4489 if (cp_parser_parse_definitely (parser))
4490 {
a723baf1
MM
4491 /* Warn about old-style casts, if so requested. */
4492 if (warn_old_style_cast
4493 && !in_system_header
4494 && !VOID_TYPE_P (type)
4495 && current_lang_name != lang_name_c)
4496 warning ("use of old-style cast");
14d22dd6
MM
4497
4498 /* Only type conversions to integral or enumeration types
4499 can be used in constant-expressions. */
4500 if (parser->constant_expression_p
4501 && !dependent_type_p (type)
4502 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4503 {
4504 if (!parser->allow_non_constant_expression_p)
4505 return (cp_parser_non_constant_expression
4506 ("a casts to a type other than an integral or "
4507 "enumeration type"));
4508 parser->non_constant_expression_p = true;
4509 }
a723baf1
MM
4510 /* Perform the cast. */
4511 expr = build_c_cast (type, expr);
bbaab916 4512 return expr;
a723baf1 4513 }
a723baf1
MM
4514 }
4515
4516 /* If we get here, then it's not a cast, so it must be a
4517 unary-expression. */
4518 return cp_parser_unary_expression (parser, address_p);
4519}
4520
4521/* Parse a pm-expression.
4522
4523 pm-expression:
4524 cast-expression
4525 pm-expression .* cast-expression
4526 pm-expression ->* cast-expression
4527
4528 Returns a representation of the expression. */
4529
4530static tree
94edc4ab 4531cp_parser_pm_expression (cp_parser* parser)
a723baf1 4532{
d6b4ea85
MM
4533 static const cp_parser_token_tree_map map = {
4534 { CPP_DEREF_STAR, MEMBER_REF },
4535 { CPP_DOT_STAR, DOTSTAR_EXPR },
4536 { CPP_EOF, ERROR_MARK }
4537 };
a723baf1 4538
d6b4ea85
MM
4539 return cp_parser_binary_expression (parser, map,
4540 cp_parser_simple_cast_expression);
a723baf1
MM
4541}
4542
4543/* Parse a multiplicative-expression.
4544
4545 mulitplicative-expression:
4546 pm-expression
4547 multiplicative-expression * pm-expression
4548 multiplicative-expression / pm-expression
4549 multiplicative-expression % pm-expression
4550
4551 Returns a representation of the expression. */
4552
4553static tree
94edc4ab 4554cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4555{
39b1af70 4556 static const cp_parser_token_tree_map map = {
a723baf1
MM
4557 { CPP_MULT, MULT_EXPR },
4558 { CPP_DIV, TRUNC_DIV_EXPR },
4559 { CPP_MOD, TRUNC_MOD_EXPR },
4560 { CPP_EOF, ERROR_MARK }
4561 };
4562
4563 return cp_parser_binary_expression (parser,
4564 map,
4565 cp_parser_pm_expression);
4566}
4567
4568/* Parse an additive-expression.
4569
4570 additive-expression:
4571 multiplicative-expression
4572 additive-expression + multiplicative-expression
4573 additive-expression - multiplicative-expression
4574
4575 Returns a representation of the expression. */
4576
4577static tree
94edc4ab 4578cp_parser_additive_expression (cp_parser* parser)
a723baf1 4579{
39b1af70 4580 static const cp_parser_token_tree_map map = {
a723baf1
MM
4581 { CPP_PLUS, PLUS_EXPR },
4582 { CPP_MINUS, MINUS_EXPR },
4583 { CPP_EOF, ERROR_MARK }
4584 };
4585
4586 return cp_parser_binary_expression (parser,
4587 map,
4588 cp_parser_multiplicative_expression);
4589}
4590
4591/* Parse a shift-expression.
4592
4593 shift-expression:
4594 additive-expression
4595 shift-expression << additive-expression
4596 shift-expression >> additive-expression
4597
4598 Returns a representation of the expression. */
4599
4600static tree
94edc4ab 4601cp_parser_shift_expression (cp_parser* parser)
a723baf1 4602{
39b1af70 4603 static const cp_parser_token_tree_map map = {
a723baf1
MM
4604 { CPP_LSHIFT, LSHIFT_EXPR },
4605 { CPP_RSHIFT, RSHIFT_EXPR },
4606 { CPP_EOF, ERROR_MARK }
4607 };
4608
4609 return cp_parser_binary_expression (parser,
4610 map,
4611 cp_parser_additive_expression);
4612}
4613
4614/* Parse a relational-expression.
4615
4616 relational-expression:
4617 shift-expression
4618 relational-expression < shift-expression
4619 relational-expression > shift-expression
4620 relational-expression <= shift-expression
4621 relational-expression >= shift-expression
4622
4623 GNU Extension:
4624
4625 relational-expression:
4626 relational-expression <? shift-expression
4627 relational-expression >? shift-expression
4628
4629 Returns a representation of the expression. */
4630
4631static tree
94edc4ab 4632cp_parser_relational_expression (cp_parser* parser)
a723baf1 4633{
39b1af70 4634 static const cp_parser_token_tree_map map = {
a723baf1
MM
4635 { CPP_LESS, LT_EXPR },
4636 { CPP_GREATER, GT_EXPR },
4637 { CPP_LESS_EQ, LE_EXPR },
4638 { CPP_GREATER_EQ, GE_EXPR },
4639 { CPP_MIN, MIN_EXPR },
4640 { CPP_MAX, MAX_EXPR },
4641 { CPP_EOF, ERROR_MARK }
4642 };
4643
4644 return cp_parser_binary_expression (parser,
4645 map,
4646 cp_parser_shift_expression);
4647}
4648
4649/* Parse an equality-expression.
4650
4651 equality-expression:
4652 relational-expression
4653 equality-expression == relational-expression
4654 equality-expression != relational-expression
4655
4656 Returns a representation of the expression. */
4657
4658static tree
94edc4ab 4659cp_parser_equality_expression (cp_parser* parser)
a723baf1 4660{
39b1af70 4661 static const cp_parser_token_tree_map map = {
a723baf1
MM
4662 { CPP_EQ_EQ, EQ_EXPR },
4663 { CPP_NOT_EQ, NE_EXPR },
4664 { CPP_EOF, ERROR_MARK }
4665 };
4666
4667 return cp_parser_binary_expression (parser,
4668 map,
4669 cp_parser_relational_expression);
4670}
4671
4672/* Parse an and-expression.
4673
4674 and-expression:
4675 equality-expression
4676 and-expression & equality-expression
4677
4678 Returns a representation of the expression. */
4679
4680static tree
94edc4ab 4681cp_parser_and_expression (cp_parser* parser)
a723baf1 4682{
39b1af70 4683 static const cp_parser_token_tree_map map = {
a723baf1
MM
4684 { CPP_AND, BIT_AND_EXPR },
4685 { CPP_EOF, ERROR_MARK }
4686 };
4687
4688 return cp_parser_binary_expression (parser,
4689 map,
4690 cp_parser_equality_expression);
4691}
4692
4693/* Parse an exclusive-or-expression.
4694
4695 exclusive-or-expression:
4696 and-expression
4697 exclusive-or-expression ^ and-expression
4698
4699 Returns a representation of the expression. */
4700
4701static tree
94edc4ab 4702cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4703{
39b1af70 4704 static const cp_parser_token_tree_map map = {
a723baf1
MM
4705 { CPP_XOR, BIT_XOR_EXPR },
4706 { CPP_EOF, ERROR_MARK }
4707 };
4708
4709 return cp_parser_binary_expression (parser,
4710 map,
4711 cp_parser_and_expression);
4712}
4713
4714
4715/* Parse an inclusive-or-expression.
4716
4717 inclusive-or-expression:
4718 exclusive-or-expression
4719 inclusive-or-expression | exclusive-or-expression
4720
4721 Returns a representation of the expression. */
4722
4723static tree
94edc4ab 4724cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 4725{
39b1af70 4726 static const cp_parser_token_tree_map map = {
a723baf1
MM
4727 { CPP_OR, BIT_IOR_EXPR },
4728 { CPP_EOF, ERROR_MARK }
4729 };
4730
4731 return cp_parser_binary_expression (parser,
4732 map,
4733 cp_parser_exclusive_or_expression);
4734}
4735
4736/* Parse a logical-and-expression.
4737
4738 logical-and-expression:
4739 inclusive-or-expression
4740 logical-and-expression && inclusive-or-expression
4741
4742 Returns a representation of the expression. */
4743
4744static tree
94edc4ab 4745cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 4746{
39b1af70 4747 static const cp_parser_token_tree_map map = {
a723baf1
MM
4748 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4749 { CPP_EOF, ERROR_MARK }
4750 };
4751
4752 return cp_parser_binary_expression (parser,
4753 map,
4754 cp_parser_inclusive_or_expression);
4755}
4756
4757/* Parse a logical-or-expression.
4758
4759 logical-or-expression:
34cd5ae7 4760 logical-and-expression
a723baf1
MM
4761 logical-or-expression || logical-and-expression
4762
4763 Returns a representation of the expression. */
4764
4765static tree
94edc4ab 4766cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 4767{
39b1af70 4768 static const cp_parser_token_tree_map map = {
a723baf1
MM
4769 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4770 { CPP_EOF, ERROR_MARK }
4771 };
4772
4773 return cp_parser_binary_expression (parser,
4774 map,
4775 cp_parser_logical_and_expression);
4776}
4777
a723baf1
MM
4778/* Parse the `? expression : assignment-expression' part of a
4779 conditional-expression. The LOGICAL_OR_EXPR is the
4780 logical-or-expression that started the conditional-expression.
4781 Returns a representation of the entire conditional-expression.
4782
39703eb9 4783 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
4784
4785 ? expression : assignment-expression
4786
4787 GNU Extensions:
4788
4789 ? : assignment-expression */
4790
4791static tree
94edc4ab 4792cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
4793{
4794 tree expr;
4795 tree assignment_expr;
4796
4797 /* Consume the `?' token. */
4798 cp_lexer_consume_token (parser->lexer);
4799 if (cp_parser_allow_gnu_extensions_p (parser)
4800 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4801 /* Implicit true clause. */
4802 expr = NULL_TREE;
4803 else
4804 /* Parse the expression. */
4805 expr = cp_parser_expression (parser);
4806
4807 /* The next token should be a `:'. */
4808 cp_parser_require (parser, CPP_COLON, "`:'");
4809 /* Parse the assignment-expression. */
4810 assignment_expr = cp_parser_assignment_expression (parser);
4811
4812 /* Build the conditional-expression. */
4813 return build_x_conditional_expr (logical_or_expr,
4814 expr,
4815 assignment_expr);
4816}
4817
4818/* Parse an assignment-expression.
4819
4820 assignment-expression:
4821 conditional-expression
4822 logical-or-expression assignment-operator assignment_expression
4823 throw-expression
4824
4825 Returns a representation for the expression. */
4826
4827static tree
94edc4ab 4828cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
4829{
4830 tree expr;
4831
4832 /* If the next token is the `throw' keyword, then we're looking at
4833 a throw-expression. */
4834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4835 expr = cp_parser_throw_expression (parser);
4836 /* Otherwise, it must be that we are looking at a
4837 logical-or-expression. */
4838 else
4839 {
4840 /* Parse the logical-or-expression. */
4841 expr = cp_parser_logical_or_expression (parser);
4842 /* If the next token is a `?' then we're actually looking at a
4843 conditional-expression. */
4844 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4845 return cp_parser_question_colon_clause (parser, expr);
4846 else
4847 {
4848 enum tree_code assignment_operator;
4849
4850 /* If it's an assignment-operator, we're using the second
4851 production. */
4852 assignment_operator
4853 = cp_parser_assignment_operator_opt (parser);
4854 if (assignment_operator != ERROR_MARK)
4855 {
4856 tree rhs;
4857
4858 /* Parse the right-hand side of the assignment. */
4859 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
4860 /* An assignment may not appear in a
4861 constant-expression. */
4862 if (parser->constant_expression_p)
4863 {
4864 if (!parser->allow_non_constant_expression_p)
4865 return cp_parser_non_constant_expression ("an assignment");
4866 parser->non_constant_expression_p = true;
4867 }
34cd5ae7 4868 /* Build the assignment expression. */
a723baf1
MM
4869 expr = build_x_modify_expr (expr,
4870 assignment_operator,
4871 rhs);
4872 }
4873 }
4874 }
4875
4876 return expr;
4877}
4878
4879/* Parse an (optional) assignment-operator.
4880
4881 assignment-operator: one of
4882 = *= /= %= += -= >>= <<= &= ^= |=
4883
4884 GNU Extension:
4885
4886 assignment-operator: one of
4887 <?= >?=
4888
4889 If the next token is an assignment operator, the corresponding tree
4890 code is returned, and the token is consumed. For example, for
4891 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4892 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4893 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4894 operator, ERROR_MARK is returned. */
4895
4896static enum tree_code
94edc4ab 4897cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
4898{
4899 enum tree_code op;
4900 cp_token *token;
4901
4902 /* Peek at the next toen. */
4903 token = cp_lexer_peek_token (parser->lexer);
4904
4905 switch (token->type)
4906 {
4907 case CPP_EQ:
4908 op = NOP_EXPR;
4909 break;
4910
4911 case CPP_MULT_EQ:
4912 op = MULT_EXPR;
4913 break;
4914
4915 case CPP_DIV_EQ:
4916 op = TRUNC_DIV_EXPR;
4917 break;
4918
4919 case CPP_MOD_EQ:
4920 op = TRUNC_MOD_EXPR;
4921 break;
4922
4923 case CPP_PLUS_EQ:
4924 op = PLUS_EXPR;
4925 break;
4926
4927 case CPP_MINUS_EQ:
4928 op = MINUS_EXPR;
4929 break;
4930
4931 case CPP_RSHIFT_EQ:
4932 op = RSHIFT_EXPR;
4933 break;
4934
4935 case CPP_LSHIFT_EQ:
4936 op = LSHIFT_EXPR;
4937 break;
4938
4939 case CPP_AND_EQ:
4940 op = BIT_AND_EXPR;
4941 break;
4942
4943 case CPP_XOR_EQ:
4944 op = BIT_XOR_EXPR;
4945 break;
4946
4947 case CPP_OR_EQ:
4948 op = BIT_IOR_EXPR;
4949 break;
4950
4951 case CPP_MIN_EQ:
4952 op = MIN_EXPR;
4953 break;
4954
4955 case CPP_MAX_EQ:
4956 op = MAX_EXPR;
4957 break;
4958
4959 default:
4960 /* Nothing else is an assignment operator. */
4961 op = ERROR_MARK;
4962 }
4963
4964 /* If it was an assignment operator, consume it. */
4965 if (op != ERROR_MARK)
4966 cp_lexer_consume_token (parser->lexer);
4967
4968 return op;
4969}
4970
4971/* Parse an expression.
4972
4973 expression:
4974 assignment-expression
4975 expression , assignment-expression
4976
4977 Returns a representation of the expression. */
4978
4979static tree
94edc4ab 4980cp_parser_expression (cp_parser* parser)
a723baf1
MM
4981{
4982 tree expression = NULL_TREE;
a723baf1
MM
4983
4984 while (true)
4985 {
4986 tree assignment_expression;
4987
4988 /* Parse the next assignment-expression. */
4989 assignment_expression
4990 = cp_parser_assignment_expression (parser);
4991 /* If this is the first assignment-expression, we can just
4992 save it away. */
4993 if (!expression)
4994 expression = assignment_expression;
a723baf1 4995 else
d17811fd
MM
4996 expression = build_x_compound_expr (expression,
4997 assignment_expression);
a723baf1
MM
4998 /* If the next token is not a comma, then we are done with the
4999 expression. */
5000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5001 break;
5002 /* Consume the `,'. */
5003 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
5004 /* A comma operator cannot appear in a constant-expression. */
5005 if (parser->constant_expression_p)
5006 {
5007 if (!parser->allow_non_constant_expression_p)
d17811fd
MM
5008 expression
5009 = cp_parser_non_constant_expression ("a comma operator");
14d22dd6
MM
5010 parser->non_constant_expression_p = true;
5011 }
14d22dd6 5012 }
a723baf1
MM
5013
5014 return expression;
5015}
5016
5017/* Parse a constant-expression.
5018
5019 constant-expression:
14d22dd6
MM
5020 conditional-expression
5021
5022 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5023 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5024 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5025 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5026
5027static tree
14d22dd6
MM
5028cp_parser_constant_expression (cp_parser* parser,
5029 bool allow_non_constant_p,
5030 bool *non_constant_p)
a723baf1
MM
5031{
5032 bool saved_constant_expression_p;
14d22dd6
MM
5033 bool saved_allow_non_constant_expression_p;
5034 bool saved_non_constant_expression_p;
a723baf1
MM
5035 tree expression;
5036
5037 /* It might seem that we could simply parse the
5038 conditional-expression, and then check to see if it were
5039 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5040 one that the compiler can figure out is constant, possibly after
5041 doing some simplifications or optimizations. The standard has a
5042 precise definition of constant-expression, and we must honor
5043 that, even though it is somewhat more restrictive.
5044
5045 For example:
5046
5047 int i[(2, 3)];
5048
5049 is not a legal declaration, because `(2, 3)' is not a
5050 constant-expression. The `,' operator is forbidden in a
5051 constant-expression. However, GCC's constant-folding machinery
5052 will fold this operation to an INTEGER_CST for `3'. */
5053
14d22dd6 5054 /* Save the old settings. */
a723baf1 5055 saved_constant_expression_p = parser->constant_expression_p;
14d22dd6
MM
5056 saved_allow_non_constant_expression_p
5057 = parser->allow_non_constant_expression_p;
5058 saved_non_constant_expression_p = parser->non_constant_expression_p;
a723baf1
MM
5059 /* We are now parsing a constant-expression. */
5060 parser->constant_expression_p = true;
14d22dd6
MM
5061 parser->allow_non_constant_expression_p = allow_non_constant_p;
5062 parser->non_constant_expression_p = false;
39703eb9
MM
5063 /* Although the grammar says "conditional-expression", we parse an
5064 "assignment-expression", which also permits "throw-expression"
5065 and the use of assignment operators. In the case that
5066 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5067 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5068 actually essential that we look for an assignment-expression.
5069 For example, cp_parser_initializer_clauses uses this function to
5070 determine whether a particular assignment-expression is in fact
5071 constant. */
5072 expression = cp_parser_assignment_expression (parser);
14d22dd6 5073 /* Restore the old settings. */
a723baf1 5074 parser->constant_expression_p = saved_constant_expression_p;
14d22dd6
MM
5075 parser->allow_non_constant_expression_p
5076 = saved_allow_non_constant_expression_p;
5077 if (allow_non_constant_p)
5078 *non_constant_p = parser->non_constant_expression_p;
5079 parser->non_constant_expression_p = saved_non_constant_expression_p;
a723baf1
MM
5080
5081 return expression;
5082}
5083
5084/* Statements [gram.stmt.stmt] */
5085
5086/* Parse a statement.
5087
5088 statement:
5089 labeled-statement
5090 expression-statement
5091 compound-statement
5092 selection-statement
5093 iteration-statement
5094 jump-statement
5095 declaration-statement
5096 try-block */
5097
5098static void
94edc4ab 5099cp_parser_statement (cp_parser* parser)
a723baf1
MM
5100{
5101 tree statement;
5102 cp_token *token;
5103 int statement_line_number;
5104
5105 /* There is no statement yet. */
5106 statement = NULL_TREE;
5107 /* Peek at the next token. */
5108 token = cp_lexer_peek_token (parser->lexer);
5109 /* Remember the line number of the first token in the statement. */
82a98427 5110 statement_line_number = token->location.line;
a723baf1
MM
5111 /* If this is a keyword, then that will often determine what kind of
5112 statement we have. */
5113 if (token->type == CPP_KEYWORD)
5114 {
5115 enum rid keyword = token->keyword;
5116
5117 switch (keyword)
5118 {
5119 case RID_CASE:
5120 case RID_DEFAULT:
5121 statement = cp_parser_labeled_statement (parser);
5122 break;
5123
5124 case RID_IF:
5125 case RID_SWITCH:
5126 statement = cp_parser_selection_statement (parser);
5127 break;
5128
5129 case RID_WHILE:
5130 case RID_DO:
5131 case RID_FOR:
5132 statement = cp_parser_iteration_statement (parser);
5133 break;
5134
5135 case RID_BREAK:
5136 case RID_CONTINUE:
5137 case RID_RETURN:
5138 case RID_GOTO:
5139 statement = cp_parser_jump_statement (parser);
5140 break;
5141
5142 case RID_TRY:
5143 statement = cp_parser_try_block (parser);
5144 break;
5145
5146 default:
5147 /* It might be a keyword like `int' that can start a
5148 declaration-statement. */
5149 break;
5150 }
5151 }
5152 else if (token->type == CPP_NAME)
5153 {
5154 /* If the next token is a `:', then we are looking at a
5155 labeled-statement. */
5156 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5157 if (token->type == CPP_COLON)
5158 statement = cp_parser_labeled_statement (parser);
5159 }
5160 /* Anything that starts with a `{' must be a compound-statement. */
5161 else if (token->type == CPP_OPEN_BRACE)
5162 statement = cp_parser_compound_statement (parser);
5163
5164 /* Everything else must be a declaration-statement or an
5165 expression-statement. Try for the declaration-statement
5166 first, unless we are looking at a `;', in which case we know that
5167 we have an expression-statement. */
5168 if (!statement)
5169 {
5170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5171 {
5172 cp_parser_parse_tentatively (parser);
5173 /* Try to parse the declaration-statement. */
5174 cp_parser_declaration_statement (parser);
5175 /* If that worked, we're done. */
5176 if (cp_parser_parse_definitely (parser))
5177 return;
5178 }
5179 /* Look for an expression-statement instead. */
5180 statement = cp_parser_expression_statement (parser);
5181 }
5182
5183 /* Set the line number for the statement. */
009ed910 5184 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5185 STMT_LINENO (statement) = statement_line_number;
5186}
5187
5188/* Parse a labeled-statement.
5189
5190 labeled-statement:
5191 identifier : statement
5192 case constant-expression : statement
5193 default : statement
5194
5195 Returns the new CASE_LABEL, for a `case' or `default' label. For
5196 an ordinary label, returns a LABEL_STMT. */
5197
5198static tree
94edc4ab 5199cp_parser_labeled_statement (cp_parser* parser)
a723baf1
MM
5200{
5201 cp_token *token;
5202 tree statement = NULL_TREE;
5203
5204 /* The next token should be an identifier. */
5205 token = cp_lexer_peek_token (parser->lexer);
5206 if (token->type != CPP_NAME
5207 && token->type != CPP_KEYWORD)
5208 {
5209 cp_parser_error (parser, "expected labeled-statement");
5210 return error_mark_node;
5211 }
5212
5213 switch (token->keyword)
5214 {
5215 case RID_CASE:
5216 {
5217 tree expr;
5218
5219 /* Consume the `case' token. */
5220 cp_lexer_consume_token (parser->lexer);
5221 /* Parse the constant-expression. */
14d22dd6 5222 expr = cp_parser_constant_expression (parser,
d17811fd 5223 /*allow_non_constant_p=*/false,
14d22dd6 5224 NULL);
a723baf1
MM
5225 /* Create the label. */
5226 statement = finish_case_label (expr, NULL_TREE);
5227 }
5228 break;
5229
5230 case RID_DEFAULT:
5231 /* Consume the `default' token. */
5232 cp_lexer_consume_token (parser->lexer);
5233 /* Create the label. */
5234 statement = finish_case_label (NULL_TREE, NULL_TREE);
5235 break;
5236
5237 default:
5238 /* Anything else must be an ordinary label. */
5239 statement = finish_label_stmt (cp_parser_identifier (parser));
5240 break;
5241 }
5242
5243 /* Require the `:' token. */
5244 cp_parser_require (parser, CPP_COLON, "`:'");
5245 /* Parse the labeled statement. */
5246 cp_parser_statement (parser);
5247
5248 /* Return the label, in the case of a `case' or `default' label. */
5249 return statement;
5250}
5251
5252/* Parse an expression-statement.
5253
5254 expression-statement:
5255 expression [opt] ;
5256
5257 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5258 statement consists of nothing more than an `;'. */
5259
5260static tree
94edc4ab 5261cp_parser_expression_statement (cp_parser* parser)
a723baf1
MM
5262{
5263 tree statement;
5264
5265 /* If the next token is not a `;', then there is an expression to parse. */
5266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5267 statement = finish_expr_stmt (cp_parser_expression (parser));
5268 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5269 else
5270 {
5271 finish_stmt ();
5272 statement = NULL_TREE;
5273 }
5274 /* Consume the final `;'. */
e0860732 5275 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
5276
5277 return statement;
5278}
5279
5280/* Parse a compound-statement.
5281
5282 compound-statement:
5283 { statement-seq [opt] }
5284
5285 Returns a COMPOUND_STMT representing the statement. */
5286
5287static tree
5288cp_parser_compound_statement (cp_parser *parser)
5289{
5290 tree compound_stmt;
5291
5292 /* Consume the `{'. */
5293 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5294 return error_mark_node;
5295 /* Begin the compound-statement. */
5296 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5297 /* Parse an (optional) statement-seq. */
5298 cp_parser_statement_seq_opt (parser);
5299 /* Finish the compound-statement. */
5300 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5301 /* Consume the `}'. */
5302 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5303
5304 return compound_stmt;
5305}
5306
5307/* Parse an (optional) statement-seq.
5308
5309 statement-seq:
5310 statement
5311 statement-seq [opt] statement */
5312
5313static void
94edc4ab 5314cp_parser_statement_seq_opt (cp_parser* parser)
a723baf1
MM
5315{
5316 /* Scan statements until there aren't any more. */
5317 while (true)
5318 {
5319 /* If we're looking at a `}', then we've run out of statements. */
5320 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5321 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5322 break;
5323
5324 /* Parse the statement. */
5325 cp_parser_statement (parser);
5326 }
5327}
5328
5329/* Parse a selection-statement.
5330
5331 selection-statement:
5332 if ( condition ) statement
5333 if ( condition ) statement else statement
5334 switch ( condition ) statement
5335
5336 Returns the new IF_STMT or SWITCH_STMT. */
5337
5338static tree
94edc4ab 5339cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5340{
5341 cp_token *token;
5342 enum rid keyword;
5343
5344 /* Peek at the next token. */
5345 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5346
5347 /* See what kind of keyword it is. */
5348 keyword = token->keyword;
5349 switch (keyword)
5350 {
5351 case RID_IF:
5352 case RID_SWITCH:
5353 {
5354 tree statement;
5355 tree condition;
5356
5357 /* Look for the `('. */
5358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5359 {
5360 cp_parser_skip_to_end_of_statement (parser);
5361 return error_mark_node;
5362 }
5363
5364 /* Begin the selection-statement. */
5365 if (keyword == RID_IF)
5366 statement = begin_if_stmt ();
5367 else
5368 statement = begin_switch_stmt ();
5369
5370 /* Parse the condition. */
5371 condition = cp_parser_condition (parser);
5372 /* Look for the `)'. */
5373 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 5374 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
5375
5376 if (keyword == RID_IF)
5377 {
5378 tree then_stmt;
5379
5380 /* Add the condition. */
5381 finish_if_stmt_cond (condition, statement);
5382
5383 /* Parse the then-clause. */
5384 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5385 finish_then_clause (statement);
5386
5387 /* If the next token is `else', parse the else-clause. */
5388 if (cp_lexer_next_token_is_keyword (parser->lexer,
5389 RID_ELSE))
5390 {
5391 tree else_stmt;
5392
5393 /* Consume the `else' keyword. */
5394 cp_lexer_consume_token (parser->lexer);
5395 /* Parse the else-clause. */
5396 else_stmt
5397 = cp_parser_implicitly_scoped_statement (parser);
5398 finish_else_clause (statement);
5399 }
5400
5401 /* Now we're all done with the if-statement. */
5402 finish_if_stmt ();
5403 }
5404 else
5405 {
5406 tree body;
5407
5408 /* Add the condition. */
5409 finish_switch_cond (condition, statement);
5410
5411 /* Parse the body of the switch-statement. */
5412 body = cp_parser_implicitly_scoped_statement (parser);
5413
5414 /* Now we're all done with the switch-statement. */
5415 finish_switch_stmt (statement);
5416 }
5417
5418 return statement;
5419 }
5420 break;
5421
5422 default:
5423 cp_parser_error (parser, "expected selection-statement");
5424 return error_mark_node;
5425 }
5426}
5427
5428/* Parse a condition.
5429
5430 condition:
5431 expression
5432 type-specifier-seq declarator = assignment-expression
5433
5434 GNU Extension:
5435
5436 condition:
5437 type-specifier-seq declarator asm-specification [opt]
5438 attributes [opt] = assignment-expression
5439
5440 Returns the expression that should be tested. */
5441
5442static tree
94edc4ab 5443cp_parser_condition (cp_parser* parser)
a723baf1
MM
5444{
5445 tree type_specifiers;
5446 const char *saved_message;
5447
5448 /* Try the declaration first. */
5449 cp_parser_parse_tentatively (parser);
5450 /* New types are not allowed in the type-specifier-seq for a
5451 condition. */
5452 saved_message = parser->type_definition_forbidden_message;
5453 parser->type_definition_forbidden_message
5454 = "types may not be defined in conditions";
5455 /* Parse the type-specifier-seq. */
5456 type_specifiers = cp_parser_type_specifier_seq (parser);
5457 /* Restore the saved message. */
5458 parser->type_definition_forbidden_message = saved_message;
5459 /* If all is well, we might be looking at a declaration. */
5460 if (!cp_parser_error_occurred (parser))
5461 {
5462 tree decl;
5463 tree asm_specification;
5464 tree attributes;
5465 tree declarator;
5466 tree initializer = NULL_TREE;
5467
5468 /* Parse the declarator. */
62b8a44e 5469 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
5470 /*ctor_dtor_or_conv_p=*/NULL);
5471 /* Parse the attributes. */
5472 attributes = cp_parser_attributes_opt (parser);
5473 /* Parse the asm-specification. */
5474 asm_specification = cp_parser_asm_specification_opt (parser);
5475 /* If the next token is not an `=', then we might still be
5476 looking at an expression. For example:
5477
5478 if (A(a).x)
5479
5480 looks like a decl-specifier-seq and a declarator -- but then
5481 there is no `=', so this is an expression. */
5482 cp_parser_require (parser, CPP_EQ, "`='");
5483 /* If we did see an `=', then we are looking at a declaration
5484 for sure. */
5485 if (cp_parser_parse_definitely (parser))
5486 {
5487 /* Create the declaration. */
5488 decl = start_decl (declarator, type_specifiers,
5489 /*initialized_p=*/true,
5490 attributes, /*prefix_attributes=*/NULL_TREE);
5491 /* Parse the assignment-expression. */
5492 initializer = cp_parser_assignment_expression (parser);
5493
5494 /* Process the initializer. */
5495 cp_finish_decl (decl,
5496 initializer,
5497 asm_specification,
5498 LOOKUP_ONLYCONVERTING);
5499
5500 return convert_from_reference (decl);
5501 }
5502 }
5503 /* If we didn't even get past the declarator successfully, we are
5504 definitely not looking at a declaration. */
5505 else
5506 cp_parser_abort_tentative_parse (parser);
5507
5508 /* Otherwise, we are looking at an expression. */
5509 return cp_parser_expression (parser);
5510}
5511
5512/* Parse an iteration-statement.
5513
5514 iteration-statement:
5515 while ( condition ) statement
5516 do statement while ( expression ) ;
5517 for ( for-init-statement condition [opt] ; expression [opt] )
5518 statement
5519
5520 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5521
5522static tree
94edc4ab 5523cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5524{
5525 cp_token *token;
5526 enum rid keyword;
5527 tree statement;
5528
5529 /* Peek at the next token. */
5530 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5531 if (!token)
5532 return error_mark_node;
5533
5534 /* See what kind of keyword it is. */
5535 keyword = token->keyword;
5536 switch (keyword)
5537 {
5538 case RID_WHILE:
5539 {
5540 tree condition;
5541
5542 /* Begin the while-statement. */
5543 statement = begin_while_stmt ();
5544 /* Look for the `('. */
5545 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5546 /* Parse the condition. */
5547 condition = cp_parser_condition (parser);
5548 finish_while_stmt_cond (condition, statement);
5549 /* Look for the `)'. */
5550 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5551 /* Parse the dependent statement. */
5552 cp_parser_already_scoped_statement (parser);
5553 /* We're done with the while-statement. */
5554 finish_while_stmt (statement);
5555 }
5556 break;
5557
5558 case RID_DO:
5559 {
5560 tree expression;
5561
5562 /* Begin the do-statement. */
5563 statement = begin_do_stmt ();
5564 /* Parse the body of the do-statement. */
5565 cp_parser_implicitly_scoped_statement (parser);
5566 finish_do_body (statement);
5567 /* Look for the `while' keyword. */
5568 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5569 /* Look for the `('. */
5570 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5571 /* Parse the expression. */
5572 expression = cp_parser_expression (parser);
5573 /* We're done with the do-statement. */
5574 finish_do_stmt (expression, statement);
5575 /* Look for the `)'. */
5576 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5577 /* Look for the `;'. */
5578 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5579 }
5580 break;
5581
5582 case RID_FOR:
5583 {
5584 tree condition = NULL_TREE;
5585 tree expression = NULL_TREE;
5586
5587 /* Begin the for-statement. */
5588 statement = begin_for_stmt ();
5589 /* Look for the `('. */
5590 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5591 /* Parse the initialization. */
5592 cp_parser_for_init_statement (parser);
5593 finish_for_init_stmt (statement);
5594
5595 /* If there's a condition, process it. */
5596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5597 condition = cp_parser_condition (parser);
5598 finish_for_cond (condition, statement);
5599 /* Look for the `;'. */
5600 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5601
5602 /* If there's an expression, process it. */
5603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5604 expression = cp_parser_expression (parser);
5605 finish_for_expr (expression, statement);
5606 /* Look for the `)'. */
5607 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5608
5609 /* Parse the body of the for-statement. */
5610 cp_parser_already_scoped_statement (parser);
5611
5612 /* We're done with the for-statement. */
5613 finish_for_stmt (statement);
5614 }
5615 break;
5616
5617 default:
5618 cp_parser_error (parser, "expected iteration-statement");
5619 statement = error_mark_node;
5620 break;
5621 }
5622
5623 return statement;
5624}
5625
5626/* Parse a for-init-statement.
5627
5628 for-init-statement:
5629 expression-statement
5630 simple-declaration */
5631
5632static void
94edc4ab 5633cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5634{
5635 /* If the next token is a `;', then we have an empty
34cd5ae7 5636 expression-statement. Grammatically, this is also a
a723baf1
MM
5637 simple-declaration, but an invalid one, because it does not
5638 declare anything. Therefore, if we did not handle this case
5639 specially, we would issue an error message about an invalid
5640 declaration. */
5641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5642 {
5643 /* We're going to speculatively look for a declaration, falling back
5644 to an expression, if necessary. */
5645 cp_parser_parse_tentatively (parser);
5646 /* Parse the declaration. */
5647 cp_parser_simple_declaration (parser,
5648 /*function_definition_allowed_p=*/false);
5649 /* If the tentative parse failed, then we shall need to look for an
5650 expression-statement. */
5651 if (cp_parser_parse_definitely (parser))
5652 return;
5653 }
5654
5655 cp_parser_expression_statement (parser);
5656}
5657
5658/* Parse a jump-statement.
5659
5660 jump-statement:
5661 break ;
5662 continue ;
5663 return expression [opt] ;
5664 goto identifier ;
5665
5666 GNU extension:
5667
5668 jump-statement:
5669 goto * expression ;
5670
5671 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5672 GOTO_STMT. */
5673
5674static tree
94edc4ab 5675cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
5676{
5677 tree statement = error_mark_node;
5678 cp_token *token;
5679 enum rid keyword;
5680
5681 /* Peek at the next token. */
5682 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5683 if (!token)
5684 return error_mark_node;
5685
5686 /* See what kind of keyword it is. */
5687 keyword = token->keyword;
5688 switch (keyword)
5689 {
5690 case RID_BREAK:
5691 statement = finish_break_stmt ();
5692 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5693 break;
5694
5695 case RID_CONTINUE:
5696 statement = finish_continue_stmt ();
5697 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5698 break;
5699
5700 case RID_RETURN:
5701 {
5702 tree expr;
5703
5704 /* If the next token is a `;', then there is no
5705 expression. */
5706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5707 expr = cp_parser_expression (parser);
5708 else
5709 expr = NULL_TREE;
5710 /* Build the return-statement. */
5711 statement = finish_return_stmt (expr);
5712 /* Look for the final `;'. */
5713 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5714 }
5715 break;
5716
5717 case RID_GOTO:
5718 /* Create the goto-statement. */
5719 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5720 {
5721 /* Issue a warning about this use of a GNU extension. */
5722 if (pedantic)
5723 pedwarn ("ISO C++ forbids computed gotos");
5724 /* Consume the '*' token. */
5725 cp_lexer_consume_token (parser->lexer);
5726 /* Parse the dependent expression. */
5727 finish_goto_stmt (cp_parser_expression (parser));
5728 }
5729 else
5730 finish_goto_stmt (cp_parser_identifier (parser));
5731 /* Look for the final `;'. */
5732 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5733 break;
5734
5735 default:
5736 cp_parser_error (parser, "expected jump-statement");
5737 break;
5738 }
5739
5740 return statement;
5741}
5742
5743/* Parse a declaration-statement.
5744
5745 declaration-statement:
5746 block-declaration */
5747
5748static void
94edc4ab 5749cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
5750{
5751 /* Parse the block-declaration. */
5752 cp_parser_block_declaration (parser, /*statement_p=*/true);
5753
5754 /* Finish off the statement. */
5755 finish_stmt ();
5756}
5757
5758/* Some dependent statements (like `if (cond) statement'), are
5759 implicitly in their own scope. In other words, if the statement is
5760 a single statement (as opposed to a compound-statement), it is
5761 none-the-less treated as if it were enclosed in braces. Any
5762 declarations appearing in the dependent statement are out of scope
5763 after control passes that point. This function parses a statement,
5764 but ensures that is in its own scope, even if it is not a
5765 compound-statement.
5766
5767 Returns the new statement. */
5768
5769static tree
94edc4ab 5770cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
5771{
5772 tree statement;
5773
5774 /* If the token is not a `{', then we must take special action. */
5775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5776 {
5777 /* Create a compound-statement. */
5778 statement = begin_compound_stmt (/*has_no_scope=*/0);
5779 /* Parse the dependent-statement. */
5780 cp_parser_statement (parser);
5781 /* Finish the dummy compound-statement. */
5782 finish_compound_stmt (/*has_no_scope=*/0, statement);
5783 }
5784 /* Otherwise, we simply parse the statement directly. */
5785 else
5786 statement = cp_parser_compound_statement (parser);
5787
5788 /* Return the statement. */
5789 return statement;
5790}
5791
5792/* For some dependent statements (like `while (cond) statement'), we
5793 have already created a scope. Therefore, even if the dependent
5794 statement is a compound-statement, we do not want to create another
5795 scope. */
5796
5797static void
94edc4ab 5798cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
5799{
5800 /* If the token is not a `{', then we must take special action. */
5801 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5802 {
5803 tree statement;
5804
5805 /* Create a compound-statement. */
5806 statement = begin_compound_stmt (/*has_no_scope=*/1);
5807 /* Parse the dependent-statement. */
5808 cp_parser_statement (parser);
5809 /* Finish the dummy compound-statement. */
5810 finish_compound_stmt (/*has_no_scope=*/1, statement);
5811 }
5812 /* Otherwise, we simply parse the statement directly. */
5813 else
5814 cp_parser_statement (parser);
5815}
5816
5817/* Declarations [gram.dcl.dcl] */
5818
5819/* Parse an optional declaration-sequence.
5820
5821 declaration-seq:
5822 declaration
5823 declaration-seq declaration */
5824
5825static void
94edc4ab 5826cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
5827{
5828 while (true)
5829 {
5830 cp_token *token;
5831
5832 token = cp_lexer_peek_token (parser->lexer);
5833
5834 if (token->type == CPP_CLOSE_BRACE
5835 || token->type == CPP_EOF)
5836 break;
5837
5838 if (token->type == CPP_SEMICOLON)
5839 {
5840 /* A declaration consisting of a single semicolon is
5841 invalid. Allow it unless we're being pedantic. */
5842 if (pedantic)
5843 pedwarn ("extra `;'");
5844 cp_lexer_consume_token (parser->lexer);
5845 continue;
5846 }
5847
c838d82f 5848 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 5849 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
5850 while (pending_lang_change > 0)
5851 {
5852 push_lang_context (lang_name_c);
5853 --pending_lang_change;
5854 }
5855 while (pending_lang_change < 0)
5856 {
5857 pop_lang_context ();
5858 ++pending_lang_change;
5859 }
5860
5861 /* Parse the declaration itself. */
a723baf1
MM
5862 cp_parser_declaration (parser);
5863 }
5864}
5865
5866/* Parse a declaration.
5867
5868 declaration:
5869 block-declaration
5870 function-definition
5871 template-declaration
5872 explicit-instantiation
5873 explicit-specialization
5874 linkage-specification
1092805d
MM
5875 namespace-definition
5876
5877 GNU extension:
5878
5879 declaration:
5880 __extension__ declaration */
a723baf1
MM
5881
5882static void
94edc4ab 5883cp_parser_declaration (cp_parser* parser)
a723baf1
MM
5884{
5885 cp_token token1;
5886 cp_token token2;
1092805d
MM
5887 int saved_pedantic;
5888
5889 /* Check for the `__extension__' keyword. */
5890 if (cp_parser_extension_opt (parser, &saved_pedantic))
5891 {
5892 /* Parse the qualified declaration. */
5893 cp_parser_declaration (parser);
5894 /* Restore the PEDANTIC flag. */
5895 pedantic = saved_pedantic;
5896
5897 return;
5898 }
a723baf1
MM
5899
5900 /* Try to figure out what kind of declaration is present. */
5901 token1 = *cp_lexer_peek_token (parser->lexer);
5902 if (token1.type != CPP_EOF)
5903 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5904
5905 /* If the next token is `extern' and the following token is a string
5906 literal, then we have a linkage specification. */
5907 if (token1.keyword == RID_EXTERN
5908 && cp_parser_is_string_literal (&token2))
5909 cp_parser_linkage_specification (parser);
5910 /* If the next token is `template', then we have either a template
5911 declaration, an explicit instantiation, or an explicit
5912 specialization. */
5913 else if (token1.keyword == RID_TEMPLATE)
5914 {
5915 /* `template <>' indicates a template specialization. */
5916 if (token2.type == CPP_LESS
5917 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5918 cp_parser_explicit_specialization (parser);
5919 /* `template <' indicates a template declaration. */
5920 else if (token2.type == CPP_LESS)
5921 cp_parser_template_declaration (parser, /*member_p=*/false);
5922 /* Anything else must be an explicit instantiation. */
5923 else
5924 cp_parser_explicit_instantiation (parser);
5925 }
5926 /* If the next token is `export', then we have a template
5927 declaration. */
5928 else if (token1.keyword == RID_EXPORT)
5929 cp_parser_template_declaration (parser, /*member_p=*/false);
5930 /* If the next token is `extern', 'static' or 'inline' and the one
5931 after that is `template', we have a GNU extended explicit
5932 instantiation directive. */
5933 else if (cp_parser_allow_gnu_extensions_p (parser)
5934 && (token1.keyword == RID_EXTERN
5935 || token1.keyword == RID_STATIC
5936 || token1.keyword == RID_INLINE)
5937 && token2.keyword == RID_TEMPLATE)
5938 cp_parser_explicit_instantiation (parser);
5939 /* If the next token is `namespace', check for a named or unnamed
5940 namespace definition. */
5941 else if (token1.keyword == RID_NAMESPACE
5942 && (/* A named namespace definition. */
5943 (token2.type == CPP_NAME
5944 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5945 == CPP_OPEN_BRACE))
5946 /* An unnamed namespace definition. */
5947 || token2.type == CPP_OPEN_BRACE))
5948 cp_parser_namespace_definition (parser);
5949 /* We must have either a block declaration or a function
5950 definition. */
5951 else
5952 /* Try to parse a block-declaration, or a function-definition. */
5953 cp_parser_block_declaration (parser, /*statement_p=*/false);
5954}
5955
5956/* Parse a block-declaration.
5957
5958 block-declaration:
5959 simple-declaration
5960 asm-definition
5961 namespace-alias-definition
5962 using-declaration
5963 using-directive
5964
5965 GNU Extension:
5966
5967 block-declaration:
5968 __extension__ block-declaration
5969 label-declaration
5970
34cd5ae7 5971 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
5972 part of a declaration-statement. */
5973
5974static void
5975cp_parser_block_declaration (cp_parser *parser,
5976 bool statement_p)
5977{
5978 cp_token *token1;
5979 int saved_pedantic;
5980
5981 /* Check for the `__extension__' keyword. */
5982 if (cp_parser_extension_opt (parser, &saved_pedantic))
5983 {
5984 /* Parse the qualified declaration. */
5985 cp_parser_block_declaration (parser, statement_p);
5986 /* Restore the PEDANTIC flag. */
5987 pedantic = saved_pedantic;
5988
5989 return;
5990 }
5991
5992 /* Peek at the next token to figure out which kind of declaration is
5993 present. */
5994 token1 = cp_lexer_peek_token (parser->lexer);
5995
5996 /* If the next keyword is `asm', we have an asm-definition. */
5997 if (token1->keyword == RID_ASM)
5998 {
5999 if (statement_p)
6000 cp_parser_commit_to_tentative_parse (parser);
6001 cp_parser_asm_definition (parser);
6002 }
6003 /* If the next keyword is `namespace', we have a
6004 namespace-alias-definition. */
6005 else if (token1->keyword == RID_NAMESPACE)
6006 cp_parser_namespace_alias_definition (parser);
6007 /* If the next keyword is `using', we have either a
6008 using-declaration or a using-directive. */
6009 else if (token1->keyword == RID_USING)
6010 {
6011 cp_token *token2;
6012
6013 if (statement_p)
6014 cp_parser_commit_to_tentative_parse (parser);
6015 /* If the token after `using' is `namespace', then we have a
6016 using-directive. */
6017 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6018 if (token2->keyword == RID_NAMESPACE)
6019 cp_parser_using_directive (parser);
6020 /* Otherwise, it's a using-declaration. */
6021 else
6022 cp_parser_using_declaration (parser);
6023 }
6024 /* If the next keyword is `__label__' we have a label declaration. */
6025 else if (token1->keyword == RID_LABEL)
6026 {
6027 if (statement_p)
6028 cp_parser_commit_to_tentative_parse (parser);
6029 cp_parser_label_declaration (parser);
6030 }
6031 /* Anything else must be a simple-declaration. */
6032 else
6033 cp_parser_simple_declaration (parser, !statement_p);
6034}
6035
6036/* Parse a simple-declaration.
6037
6038 simple-declaration:
6039 decl-specifier-seq [opt] init-declarator-list [opt] ;
6040
6041 init-declarator-list:
6042 init-declarator
6043 init-declarator-list , init-declarator
6044
34cd5ae7 6045 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6046 function-definition as a simple-declaration. */
a723baf1
MM
6047
6048static void
94edc4ab
NN
6049cp_parser_simple_declaration (cp_parser* parser,
6050 bool function_definition_allowed_p)
a723baf1
MM
6051{
6052 tree decl_specifiers;
6053 tree attributes;
a723baf1
MM
6054 bool declares_class_or_enum;
6055 bool saw_declarator;
6056
6057 /* Defer access checks until we know what is being declared; the
6058 checks for names appearing in the decl-specifier-seq should be
6059 done as if we were in the scope of the thing being declared. */
8d241e0b 6060 push_deferring_access_checks (dk_deferred);
cf22909c 6061
a723baf1
MM
6062 /* Parse the decl-specifier-seq. We have to keep track of whether
6063 or not the decl-specifier-seq declares a named class or
6064 enumeration type, since that is the only case in which the
6065 init-declarator-list is allowed to be empty.
6066
6067 [dcl.dcl]
6068
6069 In a simple-declaration, the optional init-declarator-list can be
6070 omitted only when declaring a class or enumeration, that is when
6071 the decl-specifier-seq contains either a class-specifier, an
6072 elaborated-type-specifier, or an enum-specifier. */
6073 decl_specifiers
6074 = cp_parser_decl_specifier_seq (parser,
6075 CP_PARSER_FLAGS_OPTIONAL,
6076 &attributes,
6077 &declares_class_or_enum);
6078 /* We no longer need to defer access checks. */
cf22909c 6079 stop_deferring_access_checks ();
24c0ef37 6080
39703eb9
MM
6081 /* In a block scope, a valid declaration must always have a
6082 decl-specifier-seq. By not trying to parse declarators, we can
6083 resolve the declaration/expression ambiguity more quickly. */
6084 if (!function_definition_allowed_p && !decl_specifiers)
6085 {
6086 cp_parser_error (parser, "expected declaration");
6087 goto done;
6088 }
6089
8fbc5ae7
MM
6090 /* If the next two tokens are both identifiers, the code is
6091 erroneous. The usual cause of this situation is code like:
6092
6093 T t;
6094
6095 where "T" should name a type -- but does not. */
6096 if (cp_parser_diagnose_invalid_type_name (parser))
6097 {
8d241e0b 6098 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6099 looking at a declaration. */
6100 cp_parser_commit_to_tentative_parse (parser);
6101 /* Give up. */
39703eb9 6102 goto done;
8fbc5ae7
MM
6103 }
6104
a723baf1
MM
6105 /* Keep going until we hit the `;' at the end of the simple
6106 declaration. */
6107 saw_declarator = false;
6108 while (cp_lexer_next_token_is_not (parser->lexer,
6109 CPP_SEMICOLON))
6110 {
6111 cp_token *token;
6112 bool function_definition_p;
6113
6114 saw_declarator = true;
6115 /* Parse the init-declarator. */
6116 cp_parser_init_declarator (parser, decl_specifiers, attributes,
a723baf1
MM
6117 function_definition_allowed_p,
6118 /*member_p=*/false,
6119 &function_definition_p);
1fb3244a
MM
6120 /* If an error occurred while parsing tentatively, exit quickly.
6121 (That usually happens when in the body of a function; each
6122 statement is treated as a declaration-statement until proven
6123 otherwise.) */
6124 if (cp_parser_error_occurred (parser))
39703eb9 6125 goto done;
a723baf1
MM
6126 /* Handle function definitions specially. */
6127 if (function_definition_p)
6128 {
6129 /* If the next token is a `,', then we are probably
6130 processing something like:
6131
6132 void f() {}, *p;
6133
6134 which is erroneous. */
6135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6136 error ("mixing declarations and function-definitions is forbidden");
6137 /* Otherwise, we're done with the list of declarators. */
6138 else
24c0ef37 6139 {
cf22909c 6140 pop_deferring_access_checks ();
24c0ef37
GS
6141 return;
6142 }
a723baf1
MM
6143 }
6144 /* The next token should be either a `,' or a `;'. */
6145 token = cp_lexer_peek_token (parser->lexer);
6146 /* If it's a `,', there are more declarators to come. */
6147 if (token->type == CPP_COMMA)
6148 cp_lexer_consume_token (parser->lexer);
6149 /* If it's a `;', we are done. */
6150 else if (token->type == CPP_SEMICOLON)
6151 break;
6152 /* Anything else is an error. */
6153 else
6154 {
6155 cp_parser_error (parser, "expected `,' or `;'");
6156 /* Skip tokens until we reach the end of the statement. */
6157 cp_parser_skip_to_end_of_statement (parser);
39703eb9 6158 goto done;
a723baf1
MM
6159 }
6160 /* After the first time around, a function-definition is not
6161 allowed -- even if it was OK at first. For example:
6162
6163 int i, f() {}
6164
6165 is not valid. */
6166 function_definition_allowed_p = false;
6167 }
6168
6169 /* Issue an error message if no declarators are present, and the
6170 decl-specifier-seq does not itself declare a class or
6171 enumeration. */
6172 if (!saw_declarator)
6173 {
6174 if (cp_parser_declares_only_class_p (parser))
6175 shadow_tag (decl_specifiers);
6176 /* Perform any deferred access checks. */
cf22909c 6177 perform_deferred_access_checks ();
a723baf1
MM
6178 }
6179
6180 /* Consume the `;'. */
6181 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6182
6183 /* Mark all the classes that appeared in the decl-specifier-seq as
6184 having received a `;'. */
6185 note_list_got_semicolon (decl_specifiers);
39703eb9
MM
6186
6187 done:
6188 pop_deferring_access_checks ();
a723baf1
MM
6189}
6190
6191/* Parse a decl-specifier-seq.
6192
6193 decl-specifier-seq:
6194 decl-specifier-seq [opt] decl-specifier
6195
6196 decl-specifier:
6197 storage-class-specifier
6198 type-specifier
6199 function-specifier
6200 friend
6201 typedef
6202
6203 GNU Extension:
6204
6205 decl-specifier-seq:
6206 decl-specifier-seq [opt] attributes
6207
6208 Returns a TREE_LIST, giving the decl-specifiers in the order they
6209 appear in the source code. The TREE_VALUE of each node is the
6210 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6211 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6212 representation of a type-specifier, see cp_parser_type_specifier.
6213
6214 If there are attributes, they will be stored in *ATTRIBUTES,
6215 represented as described above cp_parser_attributes.
6216
6217 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6218 appears, and the entity that will be a friend is not going to be a
6219 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6220 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6221 friendship is granted might not be a class. */
6222
6223static tree
94edc4ab
NN
6224cp_parser_decl_specifier_seq (cp_parser* parser,
6225 cp_parser_flags flags,
6226 tree* attributes,
6227 bool* declares_class_or_enum)
a723baf1
MM
6228{
6229 tree decl_specs = NULL_TREE;
6230 bool friend_p = false;
f2ce60b8
NS
6231 bool constructor_possible_p = !parser->in_declarator_p;
6232
a723baf1
MM
6233 /* Assume no class or enumeration type is declared. */
6234 *declares_class_or_enum = false;
6235
6236 /* Assume there are no attributes. */
6237 *attributes = NULL_TREE;
6238
6239 /* Keep reading specifiers until there are no more to read. */
6240 while (true)
6241 {
6242 tree decl_spec = NULL_TREE;
6243 bool constructor_p;
6244 cp_token *token;
6245
6246 /* Peek at the next token. */
6247 token = cp_lexer_peek_token (parser->lexer);
6248 /* Handle attributes. */
6249 if (token->keyword == RID_ATTRIBUTE)
6250 {
6251 /* Parse the attributes. */
6252 decl_spec = cp_parser_attributes_opt (parser);
6253 /* Add them to the list. */
6254 *attributes = chainon (*attributes, decl_spec);
6255 continue;
6256 }
6257 /* If the next token is an appropriate keyword, we can simply
6258 add it to the list. */
6259 switch (token->keyword)
6260 {
6261 case RID_FRIEND:
6262 /* decl-specifier:
6263 friend */
6264 friend_p = true;
6265 /* The representation of the specifier is simply the
6266 appropriate TREE_IDENTIFIER node. */
6267 decl_spec = token->value;
6268 /* Consume the token. */
6269 cp_lexer_consume_token (parser->lexer);
6270 break;
6271
6272 /* function-specifier:
6273 inline
6274 virtual
6275 explicit */
6276 case RID_INLINE:
6277 case RID_VIRTUAL:
6278 case RID_EXPLICIT:
6279 decl_spec = cp_parser_function_specifier_opt (parser);
6280 break;
6281
6282 /* decl-specifier:
6283 typedef */
6284 case RID_TYPEDEF:
6285 /* The representation of the specifier is simply the
6286 appropriate TREE_IDENTIFIER node. */
6287 decl_spec = token->value;
6288 /* Consume the token. */
6289 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6290 /* A constructor declarator cannot appear in a typedef. */
6291 constructor_possible_p = false;
c006d942
MM
6292 /* The "typedef" keyword can only occur in a declaration; we
6293 may as well commit at this point. */
6294 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6295 break;
6296
6297 /* storage-class-specifier:
6298 auto
6299 register
6300 static
6301 extern
6302 mutable
6303
6304 GNU Extension:
6305 thread */
6306 case RID_AUTO:
6307 case RID_REGISTER:
6308 case RID_STATIC:
6309 case RID_EXTERN:
6310 case RID_MUTABLE:
6311 case RID_THREAD:
6312 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6313 break;
6314
6315 default:
6316 break;
6317 }
6318
6319 /* Constructors are a special case. The `S' in `S()' is not a
6320 decl-specifier; it is the beginning of the declarator. */
6321 constructor_p = (!decl_spec
2050a1bb 6322 && constructor_possible_p
a723baf1
MM
6323 && cp_parser_constructor_declarator_p (parser,
6324 friend_p));
6325
6326 /* If we don't have a DECL_SPEC yet, then we must be looking at
6327 a type-specifier. */
6328 if (!decl_spec && !constructor_p)
6329 {
6330 bool decl_spec_declares_class_or_enum;
6331 bool is_cv_qualifier;
6332
6333 decl_spec
6334 = cp_parser_type_specifier (parser, flags,
6335 friend_p,
6336 /*is_declaration=*/true,
6337 &decl_spec_declares_class_or_enum,
6338 &is_cv_qualifier);
6339
6340 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6341
6342 /* If this type-specifier referenced a user-defined type
6343 (a typedef, class-name, etc.), then we can't allow any
6344 more such type-specifiers henceforth.
6345
6346 [dcl.spec]
6347
6348 The longest sequence of decl-specifiers that could
6349 possibly be a type name is taken as the
6350 decl-specifier-seq of a declaration. The sequence shall
6351 be self-consistent as described below.
6352
6353 [dcl.type]
6354
6355 As a general rule, at most one type-specifier is allowed
6356 in the complete decl-specifier-seq of a declaration. The
6357 only exceptions are the following:
6358
6359 -- const or volatile can be combined with any other
6360 type-specifier.
6361
6362 -- signed or unsigned can be combined with char, long,
6363 short, or int.
6364
6365 -- ..
6366
6367 Example:
6368
6369 typedef char* Pc;
6370 void g (const int Pc);
6371
6372 Here, Pc is *not* part of the decl-specifier seq; it's
6373 the declarator. Therefore, once we see a type-specifier
6374 (other than a cv-qualifier), we forbid any additional
6375 user-defined types. We *do* still allow things like `int
6376 int' to be considered a decl-specifier-seq, and issue the
6377 error message later. */
6378 if (decl_spec && !is_cv_qualifier)
6379 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6380 /* A constructor declarator cannot follow a type-specifier. */
6381 if (decl_spec)
6382 constructor_possible_p = false;
a723baf1
MM
6383 }
6384
6385 /* If we still do not have a DECL_SPEC, then there are no more
6386 decl-specifiers. */
6387 if (!decl_spec)
6388 {
6389 /* Issue an error message, unless the entire construct was
6390 optional. */
6391 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6392 {
6393 cp_parser_error (parser, "expected decl specifier");
6394 return error_mark_node;
6395 }
6396
6397 break;
6398 }
6399
6400 /* Add the DECL_SPEC to the list of specifiers. */
6401 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6402
6403 /* After we see one decl-specifier, further decl-specifiers are
6404 always optional. */
6405 flags |= CP_PARSER_FLAGS_OPTIONAL;
6406 }
6407
6408 /* We have built up the DECL_SPECS in reverse order. Return them in
6409 the correct order. */
6410 return nreverse (decl_specs);
6411}
6412
6413/* Parse an (optional) storage-class-specifier.
6414
6415 storage-class-specifier:
6416 auto
6417 register
6418 static
6419 extern
6420 mutable
6421
6422 GNU Extension:
6423
6424 storage-class-specifier:
6425 thread
6426
6427 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6428
6429static tree
94edc4ab 6430cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6431{
6432 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6433 {
6434 case RID_AUTO:
6435 case RID_REGISTER:
6436 case RID_STATIC:
6437 case RID_EXTERN:
6438 case RID_MUTABLE:
6439 case RID_THREAD:
6440 /* Consume the token. */
6441 return cp_lexer_consume_token (parser->lexer)->value;
6442
6443 default:
6444 return NULL_TREE;
6445 }
6446}
6447
6448/* Parse an (optional) function-specifier.
6449
6450 function-specifier:
6451 inline
6452 virtual
6453 explicit
6454
6455 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6456
6457static tree
94edc4ab 6458cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6459{
6460 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6461 {
6462 case RID_INLINE:
6463 case RID_VIRTUAL:
6464 case RID_EXPLICIT:
6465 /* Consume the token. */
6466 return cp_lexer_consume_token (parser->lexer)->value;
6467
6468 default:
6469 return NULL_TREE;
6470 }
6471}
6472
6473/* Parse a linkage-specification.
6474
6475 linkage-specification:
6476 extern string-literal { declaration-seq [opt] }
6477 extern string-literal declaration */
6478
6479static void
94edc4ab 6480cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6481{
6482 cp_token *token;
6483 tree linkage;
6484
6485 /* Look for the `extern' keyword. */
6486 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6487
6488 /* Peek at the next token. */
6489 token = cp_lexer_peek_token (parser->lexer);
6490 /* If it's not a string-literal, then there's a problem. */
6491 if (!cp_parser_is_string_literal (token))
6492 {
6493 cp_parser_error (parser, "expected language-name");
6494 return;
6495 }
6496 /* Consume the token. */
6497 cp_lexer_consume_token (parser->lexer);
6498
6499 /* Transform the literal into an identifier. If the literal is a
6500 wide-character string, or contains embedded NULs, then we can't
6501 handle it as the user wants. */
6502 if (token->type == CPP_WSTRING
6503 || (strlen (TREE_STRING_POINTER (token->value))
6504 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6505 {
6506 cp_parser_error (parser, "invalid linkage-specification");
6507 /* Assume C++ linkage. */
6508 linkage = get_identifier ("c++");
6509 }
6510 /* If it's a simple string constant, things are easier. */
6511 else
6512 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6513
6514 /* We're now using the new linkage. */
6515 push_lang_context (linkage);
6516
6517 /* If the next token is a `{', then we're using the first
6518 production. */
6519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6520 {
6521 /* Consume the `{' token. */
6522 cp_lexer_consume_token (parser->lexer);
6523 /* Parse the declarations. */
6524 cp_parser_declaration_seq_opt (parser);
6525 /* Look for the closing `}'. */
6526 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6527 }
6528 /* Otherwise, there's just one declaration. */
6529 else
6530 {
6531 bool saved_in_unbraced_linkage_specification_p;
6532
6533 saved_in_unbraced_linkage_specification_p
6534 = parser->in_unbraced_linkage_specification_p;
6535 parser->in_unbraced_linkage_specification_p = true;
6536 have_extern_spec = true;
6537 cp_parser_declaration (parser);
6538 have_extern_spec = false;
6539 parser->in_unbraced_linkage_specification_p
6540 = saved_in_unbraced_linkage_specification_p;
6541 }
6542
6543 /* We're done with the linkage-specification. */
6544 pop_lang_context ();
6545}
6546
6547/* Special member functions [gram.special] */
6548
6549/* Parse a conversion-function-id.
6550
6551 conversion-function-id:
6552 operator conversion-type-id
6553
6554 Returns an IDENTIFIER_NODE representing the operator. */
6555
6556static tree
94edc4ab 6557cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6558{
6559 tree type;
6560 tree saved_scope;
6561 tree saved_qualifying_scope;
6562 tree saved_object_scope;
6563
6564 /* Look for the `operator' token. */
6565 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6566 return error_mark_node;
6567 /* When we parse the conversion-type-id, the current scope will be
6568 reset. However, we need that information in able to look up the
6569 conversion function later, so we save it here. */
6570 saved_scope = parser->scope;
6571 saved_qualifying_scope = parser->qualifying_scope;
6572 saved_object_scope = parser->object_scope;
6573 /* We must enter the scope of the class so that the names of
6574 entities declared within the class are available in the
6575 conversion-type-id. For example, consider:
6576
6577 struct S {
6578 typedef int I;
6579 operator I();
6580 };
6581
6582 S::operator I() { ... }
6583
6584 In order to see that `I' is a type-name in the definition, we
6585 must be in the scope of `S'. */
6586 if (saved_scope)
6587 push_scope (saved_scope);
6588 /* Parse the conversion-type-id. */
6589 type = cp_parser_conversion_type_id (parser);
6590 /* Leave the scope of the class, if any. */
6591 if (saved_scope)
6592 pop_scope (saved_scope);
6593 /* Restore the saved scope. */
6594 parser->scope = saved_scope;
6595 parser->qualifying_scope = saved_qualifying_scope;
6596 parser->object_scope = saved_object_scope;
6597 /* If the TYPE is invalid, indicate failure. */
6598 if (type == error_mark_node)
6599 return error_mark_node;
6600 return mangle_conv_op_name_for_type (type);
6601}
6602
6603/* Parse a conversion-type-id:
6604
6605 conversion-type-id:
6606 type-specifier-seq conversion-declarator [opt]
6607
6608 Returns the TYPE specified. */
6609
6610static tree
94edc4ab 6611cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6612{
6613 tree attributes;
6614 tree type_specifiers;
6615 tree declarator;
6616
6617 /* Parse the attributes. */
6618 attributes = cp_parser_attributes_opt (parser);
6619 /* Parse the type-specifiers. */
6620 type_specifiers = cp_parser_type_specifier_seq (parser);
6621 /* If that didn't work, stop. */
6622 if (type_specifiers == error_mark_node)
6623 return error_mark_node;
6624 /* Parse the conversion-declarator. */
6625 declarator = cp_parser_conversion_declarator_opt (parser);
6626
6627 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6628 /*initialized=*/0, &attributes);
6629}
6630
6631/* Parse an (optional) conversion-declarator.
6632
6633 conversion-declarator:
6634 ptr-operator conversion-declarator [opt]
6635
6636 Returns a representation of the declarator. See
6637 cp_parser_declarator for details. */
6638
6639static tree
94edc4ab 6640cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
6641{
6642 enum tree_code code;
6643 tree class_type;
6644 tree cv_qualifier_seq;
6645
6646 /* We don't know if there's a ptr-operator next, or not. */
6647 cp_parser_parse_tentatively (parser);
6648 /* Try the ptr-operator. */
6649 code = cp_parser_ptr_operator (parser, &class_type,
6650 &cv_qualifier_seq);
6651 /* If it worked, look for more conversion-declarators. */
6652 if (cp_parser_parse_definitely (parser))
6653 {
6654 tree declarator;
6655
6656 /* Parse another optional declarator. */
6657 declarator = cp_parser_conversion_declarator_opt (parser);
6658
6659 /* Create the representation of the declarator. */
6660 if (code == INDIRECT_REF)
6661 declarator = make_pointer_declarator (cv_qualifier_seq,
6662 declarator);
6663 else
6664 declarator = make_reference_declarator (cv_qualifier_seq,
6665 declarator);
6666
6667 /* Handle the pointer-to-member case. */
6668 if (class_type)
6669 declarator = build_nt (SCOPE_REF, class_type, declarator);
6670
6671 return declarator;
6672 }
6673
6674 return NULL_TREE;
6675}
6676
6677/* Parse an (optional) ctor-initializer.
6678
6679 ctor-initializer:
6680 : mem-initializer-list
6681
6682 Returns TRUE iff the ctor-initializer was actually present. */
6683
6684static bool
94edc4ab 6685cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
6686{
6687 /* If the next token is not a `:', then there is no
6688 ctor-initializer. */
6689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6690 {
6691 /* Do default initialization of any bases and members. */
6692 if (DECL_CONSTRUCTOR_P (current_function_decl))
6693 finish_mem_initializers (NULL_TREE);
6694
6695 return false;
6696 }
6697
6698 /* Consume the `:' token. */
6699 cp_lexer_consume_token (parser->lexer);
6700 /* And the mem-initializer-list. */
6701 cp_parser_mem_initializer_list (parser);
6702
6703 return true;
6704}
6705
6706/* Parse a mem-initializer-list.
6707
6708 mem-initializer-list:
6709 mem-initializer
6710 mem-initializer , mem-initializer-list */
6711
6712static void
94edc4ab 6713cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
6714{
6715 tree mem_initializer_list = NULL_TREE;
6716
6717 /* Let the semantic analysis code know that we are starting the
6718 mem-initializer-list. */
0e136342
MM
6719 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6720 error ("only constructors take base initializers");
a723baf1
MM
6721
6722 /* Loop through the list. */
6723 while (true)
6724 {
6725 tree mem_initializer;
6726
6727 /* Parse the mem-initializer. */
6728 mem_initializer = cp_parser_mem_initializer (parser);
6729 /* Add it to the list, unless it was erroneous. */
6730 if (mem_initializer)
6731 {
6732 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6733 mem_initializer_list = mem_initializer;
6734 }
6735 /* If the next token is not a `,', we're done. */
6736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6737 break;
6738 /* Consume the `,' token. */
6739 cp_lexer_consume_token (parser->lexer);
6740 }
6741
6742 /* Perform semantic analysis. */
0e136342
MM
6743 if (DECL_CONSTRUCTOR_P (current_function_decl))
6744 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
6745}
6746
6747/* Parse a mem-initializer.
6748
6749 mem-initializer:
6750 mem-initializer-id ( expression-list [opt] )
6751
6752 GNU extension:
6753
6754 mem-initializer:
34cd5ae7 6755 ( expression-list [opt] )
a723baf1
MM
6756
6757 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6758 class) or FIELD_DECL (for a non-static data member) to initialize;
6759 the TREE_VALUE is the expression-list. */
6760
6761static tree
94edc4ab 6762cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
6763{
6764 tree mem_initializer_id;
6765 tree expression_list;
1f5a253a
NS
6766 tree member;
6767
a723baf1
MM
6768 /* Find out what is being initialized. */
6769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6770 {
6771 pedwarn ("anachronistic old-style base class initializer");
6772 mem_initializer_id = NULL_TREE;
6773 }
6774 else
6775 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
6776 member = expand_member_init (mem_initializer_id);
6777 if (member && !DECL_P (member))
6778 in_base_initializer = 1;
7efa3e22 6779
39703eb9
MM
6780 expression_list
6781 = cp_parser_parenthesized_expression_list (parser, false,
6782 /*non_constant_p=*/NULL);
7efa3e22 6783 if (!expression_list)
a723baf1 6784 expression_list = void_type_node;
a723baf1 6785
1f5a253a
NS
6786 in_base_initializer = 0;
6787
6788 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
6789}
6790
6791/* Parse a mem-initializer-id.
6792
6793 mem-initializer-id:
6794 :: [opt] nested-name-specifier [opt] class-name
6795 identifier
6796
6797 Returns a TYPE indicating the class to be initializer for the first
6798 production. Returns an IDENTIFIER_NODE indicating the data member
6799 to be initialized for the second production. */
6800
6801static tree
94edc4ab 6802cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
6803{
6804 bool global_scope_p;
6805 bool nested_name_specifier_p;
6806 tree id;
6807
6808 /* Look for the optional `::' operator. */
6809 global_scope_p
6810 = (cp_parser_global_scope_opt (parser,
6811 /*current_scope_valid_p=*/false)
6812 != NULL_TREE);
6813 /* Look for the optional nested-name-specifier. The simplest way to
6814 implement:
6815
6816 [temp.res]
6817
6818 The keyword `typename' is not permitted in a base-specifier or
6819 mem-initializer; in these contexts a qualified name that
6820 depends on a template-parameter is implicitly assumed to be a
6821 type name.
6822
6823 is to assume that we have seen the `typename' keyword at this
6824 point. */
6825 nested_name_specifier_p
6826 = (cp_parser_nested_name_specifier_opt (parser,
6827 /*typename_keyword_p=*/true,
6828 /*check_dependency_p=*/true,
6829 /*type_p=*/true)
6830 != NULL_TREE);
6831 /* If there is a `::' operator or a nested-name-specifier, then we
6832 are definitely looking for a class-name. */
6833 if (global_scope_p || nested_name_specifier_p)
6834 return cp_parser_class_name (parser,
6835 /*typename_keyword_p=*/true,
6836 /*template_keyword_p=*/false,
6837 /*type_p=*/false,
a723baf1
MM
6838 /*check_dependency_p=*/true,
6839 /*class_head_p=*/false);
6840 /* Otherwise, we could also be looking for an ordinary identifier. */
6841 cp_parser_parse_tentatively (parser);
6842 /* Try a class-name. */
6843 id = cp_parser_class_name (parser,
6844 /*typename_keyword_p=*/true,
6845 /*template_keyword_p=*/false,
6846 /*type_p=*/false,
a723baf1
MM
6847 /*check_dependency_p=*/true,
6848 /*class_head_p=*/false);
6849 /* If we found one, we're done. */
6850 if (cp_parser_parse_definitely (parser))
6851 return id;
6852 /* Otherwise, look for an ordinary identifier. */
6853 return cp_parser_identifier (parser);
6854}
6855
6856/* Overloading [gram.over] */
6857
6858/* Parse an operator-function-id.
6859
6860 operator-function-id:
6861 operator operator
6862
6863 Returns an IDENTIFIER_NODE for the operator which is a
6864 human-readable spelling of the identifier, e.g., `operator +'. */
6865
6866static tree
94edc4ab 6867cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
6868{
6869 /* Look for the `operator' keyword. */
6870 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6871 return error_mark_node;
6872 /* And then the name of the operator itself. */
6873 return cp_parser_operator (parser);
6874}
6875
6876/* Parse an operator.
6877
6878 operator:
6879 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6880 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6881 || ++ -- , ->* -> () []
6882
6883 GNU Extensions:
6884
6885 operator:
6886 <? >? <?= >?=
6887
6888 Returns an IDENTIFIER_NODE for the operator which is a
6889 human-readable spelling of the identifier, e.g., `operator +'. */
6890
6891static tree
94edc4ab 6892cp_parser_operator (cp_parser* parser)
a723baf1
MM
6893{
6894 tree id = NULL_TREE;
6895 cp_token *token;
6896
6897 /* Peek at the next token. */
6898 token = cp_lexer_peek_token (parser->lexer);
6899 /* Figure out which operator we have. */
6900 switch (token->type)
6901 {
6902 case CPP_KEYWORD:
6903 {
6904 enum tree_code op;
6905
6906 /* The keyword should be either `new' or `delete'. */
6907 if (token->keyword == RID_NEW)
6908 op = NEW_EXPR;
6909 else if (token->keyword == RID_DELETE)
6910 op = DELETE_EXPR;
6911 else
6912 break;
6913
6914 /* Consume the `new' or `delete' token. */
6915 cp_lexer_consume_token (parser->lexer);
6916
6917 /* Peek at the next token. */
6918 token = cp_lexer_peek_token (parser->lexer);
6919 /* If it's a `[' token then this is the array variant of the
6920 operator. */
6921 if (token->type == CPP_OPEN_SQUARE)
6922 {
6923 /* Consume the `[' token. */
6924 cp_lexer_consume_token (parser->lexer);
6925 /* Look for the `]' token. */
6926 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6927 id = ansi_opname (op == NEW_EXPR
6928 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6929 }
6930 /* Otherwise, we have the non-array variant. */
6931 else
6932 id = ansi_opname (op);
6933
6934 return id;
6935 }
6936
6937 case CPP_PLUS:
6938 id = ansi_opname (PLUS_EXPR);
6939 break;
6940
6941 case CPP_MINUS:
6942 id = ansi_opname (MINUS_EXPR);
6943 break;
6944
6945 case CPP_MULT:
6946 id = ansi_opname (MULT_EXPR);
6947 break;
6948
6949 case CPP_DIV:
6950 id = ansi_opname (TRUNC_DIV_EXPR);
6951 break;
6952
6953 case CPP_MOD:
6954 id = ansi_opname (TRUNC_MOD_EXPR);
6955 break;
6956
6957 case CPP_XOR:
6958 id = ansi_opname (BIT_XOR_EXPR);
6959 break;
6960
6961 case CPP_AND:
6962 id = ansi_opname (BIT_AND_EXPR);
6963 break;
6964
6965 case CPP_OR:
6966 id = ansi_opname (BIT_IOR_EXPR);
6967 break;
6968
6969 case CPP_COMPL:
6970 id = ansi_opname (BIT_NOT_EXPR);
6971 break;
6972
6973 case CPP_NOT:
6974 id = ansi_opname (TRUTH_NOT_EXPR);
6975 break;
6976
6977 case CPP_EQ:
6978 id = ansi_assopname (NOP_EXPR);
6979 break;
6980
6981 case CPP_LESS:
6982 id = ansi_opname (LT_EXPR);
6983 break;
6984
6985 case CPP_GREATER:
6986 id = ansi_opname (GT_EXPR);
6987 break;
6988
6989 case CPP_PLUS_EQ:
6990 id = ansi_assopname (PLUS_EXPR);
6991 break;
6992
6993 case CPP_MINUS_EQ:
6994 id = ansi_assopname (MINUS_EXPR);
6995 break;
6996
6997 case CPP_MULT_EQ:
6998 id = ansi_assopname (MULT_EXPR);
6999 break;
7000
7001 case CPP_DIV_EQ:
7002 id = ansi_assopname (TRUNC_DIV_EXPR);
7003 break;
7004
7005 case CPP_MOD_EQ:
7006 id = ansi_assopname (TRUNC_MOD_EXPR);
7007 break;
7008
7009 case CPP_XOR_EQ:
7010 id = ansi_assopname (BIT_XOR_EXPR);
7011 break;
7012
7013 case CPP_AND_EQ:
7014 id = ansi_assopname (BIT_AND_EXPR);
7015 break;
7016
7017 case CPP_OR_EQ:
7018 id = ansi_assopname (BIT_IOR_EXPR);
7019 break;
7020
7021 case CPP_LSHIFT:
7022 id = ansi_opname (LSHIFT_EXPR);
7023 break;
7024
7025 case CPP_RSHIFT:
7026 id = ansi_opname (RSHIFT_EXPR);
7027 break;
7028
7029 case CPP_LSHIFT_EQ:
7030 id = ansi_assopname (LSHIFT_EXPR);
7031 break;
7032
7033 case CPP_RSHIFT_EQ:
7034 id = ansi_assopname (RSHIFT_EXPR);
7035 break;
7036
7037 case CPP_EQ_EQ:
7038 id = ansi_opname (EQ_EXPR);
7039 break;
7040
7041 case CPP_NOT_EQ:
7042 id = ansi_opname (NE_EXPR);
7043 break;
7044
7045 case CPP_LESS_EQ:
7046 id = ansi_opname (LE_EXPR);
7047 break;
7048
7049 case CPP_GREATER_EQ:
7050 id = ansi_opname (GE_EXPR);
7051 break;
7052
7053 case CPP_AND_AND:
7054 id = ansi_opname (TRUTH_ANDIF_EXPR);
7055 break;
7056
7057 case CPP_OR_OR:
7058 id = ansi_opname (TRUTH_ORIF_EXPR);
7059 break;
7060
7061 case CPP_PLUS_PLUS:
7062 id = ansi_opname (POSTINCREMENT_EXPR);
7063 break;
7064
7065 case CPP_MINUS_MINUS:
7066 id = ansi_opname (PREDECREMENT_EXPR);
7067 break;
7068
7069 case CPP_COMMA:
7070 id = ansi_opname (COMPOUND_EXPR);
7071 break;
7072
7073 case CPP_DEREF_STAR:
7074 id = ansi_opname (MEMBER_REF);
7075 break;
7076
7077 case CPP_DEREF:
7078 id = ansi_opname (COMPONENT_REF);
7079 break;
7080
7081 case CPP_OPEN_PAREN:
7082 /* Consume the `('. */
7083 cp_lexer_consume_token (parser->lexer);
7084 /* Look for the matching `)'. */
7085 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7086 return ansi_opname (CALL_EXPR);
7087
7088 case CPP_OPEN_SQUARE:
7089 /* Consume the `['. */
7090 cp_lexer_consume_token (parser->lexer);
7091 /* Look for the matching `]'. */
7092 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7093 return ansi_opname (ARRAY_REF);
7094
7095 /* Extensions. */
7096 case CPP_MIN:
7097 id = ansi_opname (MIN_EXPR);
7098 break;
7099
7100 case CPP_MAX:
7101 id = ansi_opname (MAX_EXPR);
7102 break;
7103
7104 case CPP_MIN_EQ:
7105 id = ansi_assopname (MIN_EXPR);
7106 break;
7107
7108 case CPP_MAX_EQ:
7109 id = ansi_assopname (MAX_EXPR);
7110 break;
7111
7112 default:
7113 /* Anything else is an error. */
7114 break;
7115 }
7116
7117 /* If we have selected an identifier, we need to consume the
7118 operator token. */
7119 if (id)
7120 cp_lexer_consume_token (parser->lexer);
7121 /* Otherwise, no valid operator name was present. */
7122 else
7123 {
7124 cp_parser_error (parser, "expected operator");
7125 id = error_mark_node;
7126 }
7127
7128 return id;
7129}
7130
7131/* Parse a template-declaration.
7132
7133 template-declaration:
7134 export [opt] template < template-parameter-list > declaration
7135
7136 If MEMBER_P is TRUE, this template-declaration occurs within a
7137 class-specifier.
7138
7139 The grammar rule given by the standard isn't correct. What
7140 is really meant is:
7141
7142 template-declaration:
7143 export [opt] template-parameter-list-seq
7144 decl-specifier-seq [opt] init-declarator [opt] ;
7145 export [opt] template-parameter-list-seq
7146 function-definition
7147
7148 template-parameter-list-seq:
7149 template-parameter-list-seq [opt]
7150 template < template-parameter-list > */
7151
7152static void
94edc4ab 7153cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7154{
7155 /* Check for `export'. */
7156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7157 {
7158 /* Consume the `export' token. */
7159 cp_lexer_consume_token (parser->lexer);
7160 /* Warn that we do not support `export'. */
7161 warning ("keyword `export' not implemented, and will be ignored");
7162 }
7163
7164 cp_parser_template_declaration_after_export (parser, member_p);
7165}
7166
7167/* Parse a template-parameter-list.
7168
7169 template-parameter-list:
7170 template-parameter
7171 template-parameter-list , template-parameter
7172
7173 Returns a TREE_LIST. Each node represents a template parameter.
7174 The nodes are connected via their TREE_CHAINs. */
7175
7176static tree
94edc4ab 7177cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7178{
7179 tree parameter_list = NULL_TREE;
7180
7181 while (true)
7182 {
7183 tree parameter;
7184 cp_token *token;
7185
7186 /* Parse the template-parameter. */
7187 parameter = cp_parser_template_parameter (parser);
7188 /* Add it to the list. */
7189 parameter_list = process_template_parm (parameter_list,
7190 parameter);
7191
7192 /* Peek at the next token. */
7193 token = cp_lexer_peek_token (parser->lexer);
7194 /* If it's not a `,', we're done. */
7195 if (token->type != CPP_COMMA)
7196 break;
7197 /* Otherwise, consume the `,' token. */
7198 cp_lexer_consume_token (parser->lexer);
7199 }
7200
7201 return parameter_list;
7202}
7203
7204/* Parse a template-parameter.
7205
7206 template-parameter:
7207 type-parameter
7208 parameter-declaration
7209
7210 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7211 TREE_PURPOSE is the default value, if any. */
7212
7213static tree
94edc4ab 7214cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7215{
7216 cp_token *token;
7217
7218 /* Peek at the next token. */
7219 token = cp_lexer_peek_token (parser->lexer);
7220 /* If it is `class' or `template', we have a type-parameter. */
7221 if (token->keyword == RID_TEMPLATE)
7222 return cp_parser_type_parameter (parser);
7223 /* If it is `class' or `typename' we do not know yet whether it is a
7224 type parameter or a non-type parameter. Consider:
7225
7226 template <typename T, typename T::X X> ...
7227
7228 or:
7229
7230 template <class C, class D*> ...
7231
7232 Here, the first parameter is a type parameter, and the second is
7233 a non-type parameter. We can tell by looking at the token after
7234 the identifier -- if it is a `,', `=', or `>' then we have a type
7235 parameter. */
7236 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7237 {
7238 /* Peek at the token after `class' or `typename'. */
7239 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7240 /* If it's an identifier, skip it. */
7241 if (token->type == CPP_NAME)
7242 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7243 /* Now, see if the token looks like the end of a template
7244 parameter. */
7245 if (token->type == CPP_COMMA
7246 || token->type == CPP_EQ
7247 || token->type == CPP_GREATER)
7248 return cp_parser_type_parameter (parser);
7249 }
7250
7251 /* Otherwise, it is a non-type parameter.
7252
7253 [temp.param]
7254
7255 When parsing a default template-argument for a non-type
7256 template-parameter, the first non-nested `>' is taken as the end
7257 of the template parameter-list rather than a greater-than
7258 operator. */
7259 return
ec194454 7260 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7261}
7262
7263/* Parse a type-parameter.
7264
7265 type-parameter:
7266 class identifier [opt]
7267 class identifier [opt] = type-id
7268 typename identifier [opt]
7269 typename identifier [opt] = type-id
7270 template < template-parameter-list > class identifier [opt]
7271 template < template-parameter-list > class identifier [opt]
7272 = id-expression
7273
7274 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7275 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7276 the declaration of the parameter. */
7277
7278static tree
94edc4ab 7279cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7280{
7281 cp_token *token;
7282 tree parameter;
7283
7284 /* Look for a keyword to tell us what kind of parameter this is. */
7285 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7286 "`class', `typename', or `template'");
a723baf1
MM
7287 if (!token)
7288 return error_mark_node;
7289
7290 switch (token->keyword)
7291 {
7292 case RID_CLASS:
7293 case RID_TYPENAME:
7294 {
7295 tree identifier;
7296 tree default_argument;
7297
7298 /* If the next token is an identifier, then it names the
7299 parameter. */
7300 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7301 identifier = cp_parser_identifier (parser);
7302 else
7303 identifier = NULL_TREE;
7304
7305 /* Create the parameter. */
7306 parameter = finish_template_type_parm (class_type_node, identifier);
7307
7308 /* If the next token is an `=', we have a default argument. */
7309 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7310 {
7311 /* Consume the `=' token. */
7312 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7313 /* Parse the default-argument. */
a723baf1
MM
7314 default_argument = cp_parser_type_id (parser);
7315 }
7316 else
7317 default_argument = NULL_TREE;
7318
7319 /* Create the combined representation of the parameter and the
7320 default argument. */
7321 parameter = build_tree_list (default_argument,
7322 parameter);
7323 }
7324 break;
7325
7326 case RID_TEMPLATE:
7327 {
7328 tree parameter_list;
7329 tree identifier;
7330 tree default_argument;
7331
7332 /* Look for the `<'. */
7333 cp_parser_require (parser, CPP_LESS, "`<'");
7334 /* Parse the template-parameter-list. */
7335 begin_template_parm_list ();
7336 parameter_list
7337 = cp_parser_template_parameter_list (parser);
7338 parameter_list = end_template_parm_list (parameter_list);
7339 /* Look for the `>'. */
7340 cp_parser_require (parser, CPP_GREATER, "`>'");
7341 /* Look for the `class' keyword. */
7342 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7343 /* If the next token is an `=', then there is a
7344 default-argument. If the next token is a `>', we are at
7345 the end of the parameter-list. If the next token is a `,',
7346 then we are at the end of this parameter. */
7347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7348 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7349 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7350 identifier = cp_parser_identifier (parser);
7351 else
7352 identifier = NULL_TREE;
7353 /* Create the template parameter. */
7354 parameter = finish_template_template_parm (class_type_node,
7355 identifier);
7356
7357 /* If the next token is an `=', then there is a
7358 default-argument. */
7359 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7360 {
7361 /* Consume the `='. */
7362 cp_lexer_consume_token (parser->lexer);
7363 /* Parse the id-expression. */
7364 default_argument
7365 = cp_parser_id_expression (parser,
7366 /*template_keyword_p=*/false,
7367 /*check_dependency_p=*/true,
7368 /*template_p=*/NULL);
7369 /* Look up the name. */
7370 default_argument
7371 = cp_parser_lookup_name_simple (parser, default_argument);
7372 /* See if the default argument is valid. */
7373 default_argument
7374 = check_template_template_default_arg (default_argument);
7375 }
7376 else
7377 default_argument = NULL_TREE;
7378
7379 /* Create the combined representation of the parameter and the
7380 default argument. */
7381 parameter = build_tree_list (default_argument,
7382 parameter);
7383 }
7384 break;
7385
7386 default:
7387 /* Anything else is an error. */
7388 cp_parser_error (parser,
7389 "expected `class', `typename', or `template'");
7390 parameter = error_mark_node;
7391 }
7392
7393 return parameter;
7394}
7395
7396/* Parse a template-id.
7397
7398 template-id:
7399 template-name < template-argument-list [opt] >
7400
7401 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7402 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7403 returned. Otherwise, if the template-name names a function, or set
7404 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7405 names a class, returns a TYPE_DECL for the specialization.
7406
7407 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7408 uninstantiated templates. */
7409
7410static tree
7411cp_parser_template_id (cp_parser *parser,
7412 bool template_keyword_p,
7413 bool check_dependency_p)
7414{
7415 tree template;
7416 tree arguments;
7417 tree saved_scope;
7418 tree saved_qualifying_scope;
7419 tree saved_object_scope;
7420 tree template_id;
7421 bool saved_greater_than_is_operator_p;
7422 ptrdiff_t start_of_id;
7423 tree access_check = NULL_TREE;
2050a1bb 7424 cp_token *next_token;
a723baf1
MM
7425
7426 /* If the next token corresponds to a template-id, there is no need
7427 to reparse it. */
2050a1bb
MM
7428 next_token = cp_lexer_peek_token (parser->lexer);
7429 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7430 {
7431 tree value;
7432 tree check;
7433
7434 /* Get the stored value. */
7435 value = cp_lexer_consume_token (parser->lexer)->value;
7436 /* Perform any access checks that were deferred. */
7437 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7438 perform_or_defer_access_check (TREE_PURPOSE (check),
7439 TREE_VALUE (check));
a723baf1
MM
7440 /* Return the stored value. */
7441 return TREE_VALUE (value);
7442 }
7443
2050a1bb
MM
7444 /* Avoid performing name lookup if there is no possibility of
7445 finding a template-id. */
7446 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7447 || (next_token->type == CPP_NAME
7448 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7449 {
7450 cp_parser_error (parser, "expected template-id");
7451 return error_mark_node;
7452 }
7453
a723baf1
MM
7454 /* Remember where the template-id starts. */
7455 if (cp_parser_parsing_tentatively (parser)
7456 && !cp_parser_committed_to_tentative_parse (parser))
7457 {
2050a1bb 7458 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7459 start_of_id = cp_lexer_token_difference (parser->lexer,
7460 parser->lexer->first_token,
7461 next_token);
a723baf1
MM
7462 }
7463 else
7464 start_of_id = -1;
7465
8d241e0b 7466 push_deferring_access_checks (dk_deferred);
cf22909c 7467
a723baf1
MM
7468 /* Parse the template-name. */
7469 template = cp_parser_template_name (parser, template_keyword_p,
7470 check_dependency_p);
7471 if (template == error_mark_node)
cf22909c
KL
7472 {
7473 pop_deferring_access_checks ();
7474 return error_mark_node;
7475 }
a723baf1
MM
7476
7477 /* Look for the `<' that starts the template-argument-list. */
7478 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7479 {
7480 pop_deferring_access_checks ();
7481 return error_mark_node;
7482 }
a723baf1
MM
7483
7484 /* [temp.names]
7485
7486 When parsing a template-id, the first non-nested `>' is taken as
7487 the end of the template-argument-list rather than a greater-than
7488 operator. */
7489 saved_greater_than_is_operator_p
7490 = parser->greater_than_is_operator_p;
7491 parser->greater_than_is_operator_p = false;
7492 /* Parsing the argument list may modify SCOPE, so we save it
7493 here. */
7494 saved_scope = parser->scope;
7495 saved_qualifying_scope = parser->qualifying_scope;
7496 saved_object_scope = parser->object_scope;
7497 /* Parse the template-argument-list itself. */
7498 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7499 arguments = NULL_TREE;
7500 else
7501 arguments = cp_parser_template_argument_list (parser);
7502 /* Look for the `>' that ends the template-argument-list. */
7503 cp_parser_require (parser, CPP_GREATER, "`>'");
7504 /* The `>' token might be a greater-than operator again now. */
7505 parser->greater_than_is_operator_p
7506 = saved_greater_than_is_operator_p;
7507 /* Restore the SAVED_SCOPE. */
7508 parser->scope = saved_scope;
7509 parser->qualifying_scope = saved_qualifying_scope;
7510 parser->object_scope = saved_object_scope;
7511
7512 /* Build a representation of the specialization. */
7513 if (TREE_CODE (template) == IDENTIFIER_NODE)
7514 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7515 else if (DECL_CLASS_TEMPLATE_P (template)
7516 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7517 template_id
7518 = finish_template_type (template, arguments,
7519 cp_lexer_next_token_is (parser->lexer,
7520 CPP_SCOPE));
7521 else
7522 {
7523 /* If it's not a class-template or a template-template, it should be
7524 a function-template. */
7525 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7526 || TREE_CODE (template) == OVERLOAD
7527 || BASELINK_P (template)),
7528 20010716);
7529
7530 template_id = lookup_template_function (template, arguments);
7531 }
7532
cf22909c
KL
7533 /* Retrieve any deferred checks. Do not pop this access checks yet
7534 so the memory will not be reclaimed during token replacing below. */
7535 access_check = get_deferred_access_checks ();
7536
a723baf1
MM
7537 /* If parsing tentatively, replace the sequence of tokens that makes
7538 up the template-id with a CPP_TEMPLATE_ID token. That way,
7539 should we re-parse the token stream, we will not have to repeat
7540 the effort required to do the parse, nor will we issue duplicate
7541 error messages about problems during instantiation of the
7542 template. */
7543 if (start_of_id >= 0)
7544 {
7545 cp_token *token;
a723baf1
MM
7546
7547 /* Find the token that corresponds to the start of the
7548 template-id. */
7549 token = cp_lexer_advance_token (parser->lexer,
7550 parser->lexer->first_token,
7551 start_of_id);
7552
a723baf1
MM
7553 /* Reset the contents of the START_OF_ID token. */
7554 token->type = CPP_TEMPLATE_ID;
7555 token->value = build_tree_list (access_check, template_id);
7556 token->keyword = RID_MAX;
7557 /* Purge all subsequent tokens. */
7558 cp_lexer_purge_tokens_after (parser->lexer, token);
7559 }
7560
cf22909c 7561 pop_deferring_access_checks ();
a723baf1
MM
7562 return template_id;
7563}
7564
7565/* Parse a template-name.
7566
7567 template-name:
7568 identifier
7569
7570 The standard should actually say:
7571
7572 template-name:
7573 identifier
7574 operator-function-id
7575 conversion-function-id
7576
7577 A defect report has been filed about this issue.
7578
7579 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7580 `template' keyword, in a construction like:
7581
7582 T::template f<3>()
7583
7584 In that case `f' is taken to be a template-name, even though there
7585 is no way of knowing for sure.
7586
7587 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7588 name refers to a set of overloaded functions, at least one of which
7589 is a template, or an IDENTIFIER_NODE with the name of the template,
7590 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7591 names are looked up inside uninstantiated templates. */
7592
7593static tree
94edc4ab
NN
7594cp_parser_template_name (cp_parser* parser,
7595 bool template_keyword_p,
7596 bool check_dependency_p)
a723baf1
MM
7597{
7598 tree identifier;
7599 tree decl;
7600 tree fns;
7601
7602 /* If the next token is `operator', then we have either an
7603 operator-function-id or a conversion-function-id. */
7604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7605 {
7606 /* We don't know whether we're looking at an
7607 operator-function-id or a conversion-function-id. */
7608 cp_parser_parse_tentatively (parser);
7609 /* Try an operator-function-id. */
7610 identifier = cp_parser_operator_function_id (parser);
7611 /* If that didn't work, try a conversion-function-id. */
7612 if (!cp_parser_parse_definitely (parser))
7613 identifier = cp_parser_conversion_function_id (parser);
7614 }
7615 /* Look for the identifier. */
7616 else
7617 identifier = cp_parser_identifier (parser);
7618
7619 /* If we didn't find an identifier, we don't have a template-id. */
7620 if (identifier == error_mark_node)
7621 return error_mark_node;
7622
7623 /* If the name immediately followed the `template' keyword, then it
7624 is a template-name. However, if the next token is not `<', then
7625 we do not treat it as a template-name, since it is not being used
7626 as part of a template-id. This enables us to handle constructs
7627 like:
7628
7629 template <typename T> struct S { S(); };
7630 template <typename T> S<T>::S();
7631
7632 correctly. We would treat `S' as a template -- if it were `S<T>'
7633 -- but we do not if there is no `<'. */
7634 if (template_keyword_p && processing_template_decl
7635 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7636 return identifier;
7637
7638 /* Look up the name. */
7639 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 7640 /*is_type=*/false,
eea9800f 7641 /*is_namespace=*/false,
a723baf1
MM
7642 check_dependency_p);
7643 decl = maybe_get_template_decl_from_type_decl (decl);
7644
7645 /* If DECL is a template, then the name was a template-name. */
7646 if (TREE_CODE (decl) == TEMPLATE_DECL)
7647 ;
7648 else
7649 {
7650 /* The standard does not explicitly indicate whether a name that
7651 names a set of overloaded declarations, some of which are
7652 templates, is a template-name. However, such a name should
7653 be a template-name; otherwise, there is no way to form a
7654 template-id for the overloaded templates. */
7655 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7656 if (TREE_CODE (fns) == OVERLOAD)
7657 {
7658 tree fn;
7659
7660 for (fn = fns; fn; fn = OVL_NEXT (fn))
7661 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7662 break;
7663 }
7664 else
7665 {
7666 /* Otherwise, the name does not name a template. */
7667 cp_parser_error (parser, "expected template-name");
7668 return error_mark_node;
7669 }
7670 }
7671
7672 /* If DECL is dependent, and refers to a function, then just return
7673 its name; we will look it up again during template instantiation. */
7674 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7675 {
7676 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 7677 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
7678 return identifier;
7679 }
7680
7681 return decl;
7682}
7683
7684/* Parse a template-argument-list.
7685
7686 template-argument-list:
7687 template-argument
7688 template-argument-list , template-argument
7689
7690 Returns a TREE_LIST representing the arguments, in the order they
7691 appeared. The TREE_VALUE of each node is a representation of the
7692 argument. */
7693
7694static tree
94edc4ab 7695cp_parser_template_argument_list (cp_parser* parser)
a723baf1
MM
7696{
7697 tree arguments = NULL_TREE;
7698
7699 while (true)
7700 {
7701 tree argument;
7702
7703 /* Parse the template-argument. */
7704 argument = cp_parser_template_argument (parser);
7705 /* Add it to the list. */
7706 arguments = tree_cons (NULL_TREE, argument, arguments);
7707 /* If it is not a `,', then there are no more arguments. */
7708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7709 break;
7710 /* Otherwise, consume the ','. */
7711 cp_lexer_consume_token (parser->lexer);
7712 }
7713
7714 /* We built up the arguments in reverse order. */
7715 return nreverse (arguments);
7716}
7717
7718/* Parse a template-argument.
7719
7720 template-argument:
7721 assignment-expression
7722 type-id
7723 id-expression
7724
7725 The representation is that of an assignment-expression, type-id, or
7726 id-expression -- except that the qualified id-expression is
7727 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
7728 OVERLOAD.
7729
7730 Although the standard says "assignment-expression", it forbids
7731 throw-expressions or assignments in the template argument.
7732 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
7733
7734static tree
94edc4ab 7735cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
7736{
7737 tree argument;
7738 bool template_p;
d17811fd
MM
7739 bool address_p;
7740 cp_token *token;
b3445994 7741 cp_id_kind idk;
d17811fd 7742 tree qualifying_class;
a723baf1
MM
7743
7744 /* There's really no way to know what we're looking at, so we just
7745 try each alternative in order.
7746
7747 [temp.arg]
7748
7749 In a template-argument, an ambiguity between a type-id and an
7750 expression is resolved to a type-id, regardless of the form of
7751 the corresponding template-parameter.
7752
7753 Therefore, we try a type-id first. */
7754 cp_parser_parse_tentatively (parser);
a723baf1
MM
7755 argument = cp_parser_type_id (parser);
7756 /* If the next token isn't a `,' or a `>', then this argument wasn't
7757 really finished. */
d17811fd 7758 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7759 cp_parser_error (parser, "expected template-argument");
7760 /* If that worked, we're done. */
7761 if (cp_parser_parse_definitely (parser))
7762 return argument;
7763 /* We're still not sure what the argument will be. */
7764 cp_parser_parse_tentatively (parser);
7765 /* Try a template. */
7766 argument = cp_parser_id_expression (parser,
7767 /*template_keyword_p=*/false,
7768 /*check_dependency_p=*/true,
7769 &template_p);
7770 /* If the next token isn't a `,' or a `>', then this argument wasn't
7771 really finished. */
d17811fd 7772 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
7773 cp_parser_error (parser, "expected template-argument");
7774 if (!cp_parser_error_occurred (parser))
7775 {
7776 /* Figure out what is being referred to. */
7777 argument = cp_parser_lookup_name_simple (parser, argument);
7778 if (template_p)
7779 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7780 TREE_OPERAND (argument, 1),
78757caa 7781 tf_error);
a723baf1
MM
7782 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7783 cp_parser_error (parser, "expected template-name");
7784 }
7785 if (cp_parser_parse_definitely (parser))
7786 return argument;
d17811fd
MM
7787 /* It must be a non-type argument. There permitted cases are given
7788 in [temp.arg.nontype]:
7789
7790 -- an integral constant-expression of integral or enumeration
7791 type; or
7792
7793 -- the name of a non-type template-parameter; or
7794
7795 -- the name of an object or function with external linkage...
7796
7797 -- the address of an object or function with external linkage...
7798
7799 -- a pointer to member... */
7800 /* Look for a non-type template parameter. */
7801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7802 {
7803 cp_parser_parse_tentatively (parser);
7804 argument = cp_parser_primary_expression (parser,
7805 &idk,
7806 &qualifying_class);
7807 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7808 || !cp_parser_next_token_ends_template_argument_p (parser))
7809 cp_parser_simulate_error (parser);
7810 if (cp_parser_parse_definitely (parser))
7811 return argument;
7812 }
7813 /* If the next token is "&", the argument must be the address of an
7814 object or function with external linkage. */
7815 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7816 if (address_p)
7817 cp_lexer_consume_token (parser->lexer);
7818 /* See if we might have an id-expression. */
7819 token = cp_lexer_peek_token (parser->lexer);
7820 if (token->type == CPP_NAME
7821 || token->keyword == RID_OPERATOR
7822 || token->type == CPP_SCOPE
7823 || token->type == CPP_TEMPLATE_ID
7824 || token->type == CPP_NESTED_NAME_SPECIFIER)
7825 {
7826 cp_parser_parse_tentatively (parser);
7827 argument = cp_parser_primary_expression (parser,
7828 &idk,
7829 &qualifying_class);
7830 if (cp_parser_error_occurred (parser)
7831 || !cp_parser_next_token_ends_template_argument_p (parser))
7832 cp_parser_abort_tentative_parse (parser);
7833 else
7834 {
7835 if (qualifying_class)
7836 argument = finish_qualified_id_expr (qualifying_class,
7837 argument,
7838 /*done=*/true,
7839 address_p);
7840 if (TREE_CODE (argument) == VAR_DECL)
7841 {
7842 /* A variable without external linkage might still be a
7843 valid constant-expression, so no error is issued here
7844 if the external-linkage check fails. */
7845 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7846 cp_parser_simulate_error (parser);
7847 }
7848 else if (is_overloaded_fn (argument))
7849 /* All overloaded functions are allowed; if the external
7850 linkage test does not pass, an error will be issued
7851 later. */
7852 ;
7853 else if (address_p
7854 && (TREE_CODE (argument) == OFFSET_REF
7855 || TREE_CODE (argument) == SCOPE_REF))
7856 /* A pointer-to-member. */
7857 ;
7858 else
7859 cp_parser_simulate_error (parser);
7860
7861 if (cp_parser_parse_definitely (parser))
7862 {
7863 if (address_p)
7864 argument = build_x_unary_op (ADDR_EXPR, argument);
7865 return argument;
7866 }
7867 }
7868 }
7869 /* If the argument started with "&", there are no other valid
7870 alternatives at this point. */
7871 if (address_p)
7872 {
7873 cp_parser_error (parser, "invalid non-type template argument");
7874 return error_mark_node;
7875 }
7876 /* The argument must be a constant-expression. */
7877 argument = cp_parser_constant_expression (parser,
7878 /*allow_non_constant_p=*/false,
7879 /*non_constant_p=*/NULL);
7880 /* If it's non-dependent, simplify it. */
7881 return cp_parser_fold_non_dependent_expr (argument);
a723baf1
MM
7882}
7883
7884/* Parse an explicit-instantiation.
7885
7886 explicit-instantiation:
7887 template declaration
7888
7889 Although the standard says `declaration', what it really means is:
7890
7891 explicit-instantiation:
7892 template decl-specifier-seq [opt] declarator [opt] ;
7893
7894 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7895 supposed to be allowed. A defect report has been filed about this
7896 issue.
7897
7898 GNU Extension:
7899
7900 explicit-instantiation:
7901 storage-class-specifier template
7902 decl-specifier-seq [opt] declarator [opt] ;
7903 function-specifier template
7904 decl-specifier-seq [opt] declarator [opt] ; */
7905
7906static void
94edc4ab 7907cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1
MM
7908{
7909 bool declares_class_or_enum;
7910 tree decl_specifiers;
7911 tree attributes;
7912 tree extension_specifier = NULL_TREE;
7913
7914 /* Look for an (optional) storage-class-specifier or
7915 function-specifier. */
7916 if (cp_parser_allow_gnu_extensions_p (parser))
7917 {
7918 extension_specifier
7919 = cp_parser_storage_class_specifier_opt (parser);
7920 if (!extension_specifier)
7921 extension_specifier = cp_parser_function_specifier_opt (parser);
7922 }
7923
7924 /* Look for the `template' keyword. */
7925 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7926 /* Let the front end know that we are processing an explicit
7927 instantiation. */
7928 begin_explicit_instantiation ();
7929 /* [temp.explicit] says that we are supposed to ignore access
7930 control while processing explicit instantiation directives. */
78757caa 7931 push_deferring_access_checks (dk_no_check);
a723baf1
MM
7932 /* Parse a decl-specifier-seq. */
7933 decl_specifiers
7934 = cp_parser_decl_specifier_seq (parser,
7935 CP_PARSER_FLAGS_OPTIONAL,
7936 &attributes,
7937 &declares_class_or_enum);
7938 /* If there was exactly one decl-specifier, and it declared a class,
7939 and there's no declarator, then we have an explicit type
7940 instantiation. */
7941 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
7942 {
7943 tree type;
7944
7945 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
7946 /* Turn access control back on for names used during
7947 template instantiation. */
7948 pop_deferring_access_checks ();
a723baf1
MM
7949 if (type)
7950 do_type_instantiation (type, extension_specifier, /*complain=*/1);
7951 }
7952 else
7953 {
7954 tree declarator;
7955 tree decl;
7956
7957 /* Parse the declarator. */
7958 declarator
62b8a44e 7959 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
7960 /*ctor_dtor_or_conv_p=*/NULL);
7961 decl = grokdeclarator (declarator, decl_specifiers,
7962 NORMAL, 0, NULL);
b7fc8b57
KL
7963 /* Turn access control back on for names used during
7964 template instantiation. */
7965 pop_deferring_access_checks ();
a723baf1
MM
7966 /* Do the explicit instantiation. */
7967 do_decl_instantiation (decl, extension_specifier);
7968 }
7969 /* We're done with the instantiation. */
7970 end_explicit_instantiation ();
a723baf1 7971
e0860732 7972 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
7973}
7974
7975/* Parse an explicit-specialization.
7976
7977 explicit-specialization:
7978 template < > declaration
7979
7980 Although the standard says `declaration', what it really means is:
7981
7982 explicit-specialization:
7983 template <> decl-specifier [opt] init-declarator [opt] ;
7984 template <> function-definition
7985 template <> explicit-specialization
7986 template <> template-declaration */
7987
7988static void
94edc4ab 7989cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
7990{
7991 /* Look for the `template' keyword. */
7992 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7993 /* Look for the `<'. */
7994 cp_parser_require (parser, CPP_LESS, "`<'");
7995 /* Look for the `>'. */
7996 cp_parser_require (parser, CPP_GREATER, "`>'");
7997 /* We have processed another parameter list. */
7998 ++parser->num_template_parameter_lists;
7999 /* Let the front end know that we are beginning a specialization. */
8000 begin_specialization ();
8001
8002 /* If the next keyword is `template', we need to figure out whether
8003 or not we're looking a template-declaration. */
8004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8005 {
8006 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8007 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8008 cp_parser_template_declaration_after_export (parser,
8009 /*member_p=*/false);
8010 else
8011 cp_parser_explicit_specialization (parser);
8012 }
8013 else
8014 /* Parse the dependent declaration. */
8015 cp_parser_single_declaration (parser,
8016 /*member_p=*/false,
8017 /*friend_p=*/NULL);
8018
8019 /* We're done with the specialization. */
8020 end_specialization ();
8021 /* We're done with this parameter list. */
8022 --parser->num_template_parameter_lists;
8023}
8024
8025/* Parse a type-specifier.
8026
8027 type-specifier:
8028 simple-type-specifier
8029 class-specifier
8030 enum-specifier
8031 elaborated-type-specifier
8032 cv-qualifier
8033
8034 GNU Extension:
8035
8036 type-specifier:
8037 __complex__
8038
8039 Returns a representation of the type-specifier. If the
8040 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8041 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8042 For a class-specifier, enum-specifier, or elaborated-type-specifier
8043 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8044
8045 If IS_FRIEND is TRUE then this type-specifier is being declared a
8046 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8047 appearing in a decl-specifier-seq.
8048
8049 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8050 class-specifier, enum-specifier, or elaborated-type-specifier, then
8051 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8052 FALSE.
8053
8054 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8055 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8056 is set to FALSE. */
8057
8058static tree
94edc4ab
NN
8059cp_parser_type_specifier (cp_parser* parser,
8060 cp_parser_flags flags,
8061 bool is_friend,
8062 bool is_declaration,
8063 bool* declares_class_or_enum,
8064 bool* is_cv_qualifier)
a723baf1
MM
8065{
8066 tree type_spec = NULL_TREE;
8067 cp_token *token;
8068 enum rid keyword;
8069
8070 /* Assume this type-specifier does not declare a new type. */
8071 if (declares_class_or_enum)
8072 *declares_class_or_enum = false;
8073 /* And that it does not specify a cv-qualifier. */
8074 if (is_cv_qualifier)
8075 *is_cv_qualifier = false;
8076 /* Peek at the next token. */
8077 token = cp_lexer_peek_token (parser->lexer);
8078
8079 /* If we're looking at a keyword, we can use that to guide the
8080 production we choose. */
8081 keyword = token->keyword;
8082 switch (keyword)
8083 {
8084 /* Any of these indicate either a class-specifier, or an
8085 elaborated-type-specifier. */
8086 case RID_CLASS:
8087 case RID_STRUCT:
8088 case RID_UNION:
8089 case RID_ENUM:
8090 /* Parse tentatively so that we can back up if we don't find a
8091 class-specifier or enum-specifier. */
8092 cp_parser_parse_tentatively (parser);
8093 /* Look for the class-specifier or enum-specifier. */
8094 if (keyword == RID_ENUM)
8095 type_spec = cp_parser_enum_specifier (parser);
8096 else
8097 type_spec = cp_parser_class_specifier (parser);
8098
8099 /* If that worked, we're done. */
8100 if (cp_parser_parse_definitely (parser))
8101 {
8102 if (declares_class_or_enum)
8103 *declares_class_or_enum = true;
8104 return type_spec;
8105 }
8106
8107 /* Fall through. */
8108
8109 case RID_TYPENAME:
8110 /* Look for an elaborated-type-specifier. */
8111 type_spec = cp_parser_elaborated_type_specifier (parser,
8112 is_friend,
8113 is_declaration);
8114 /* We're declaring a class or enum -- unless we're using
8115 `typename'. */
8116 if (declares_class_or_enum && keyword != RID_TYPENAME)
8117 *declares_class_or_enum = true;
8118 return type_spec;
8119
8120 case RID_CONST:
8121 case RID_VOLATILE:
8122 case RID_RESTRICT:
8123 type_spec = cp_parser_cv_qualifier_opt (parser);
8124 /* Even though we call a routine that looks for an optional
8125 qualifier, we know that there should be one. */
8126 my_friendly_assert (type_spec != NULL, 20000328);
8127 /* This type-specifier was a cv-qualified. */
8128 if (is_cv_qualifier)
8129 *is_cv_qualifier = true;
8130
8131 return type_spec;
8132
8133 case RID_COMPLEX:
8134 /* The `__complex__' keyword is a GNU extension. */
8135 return cp_lexer_consume_token (parser->lexer)->value;
8136
8137 default:
8138 break;
8139 }
8140
8141 /* If we do not already have a type-specifier, assume we are looking
8142 at a simple-type-specifier. */
8143 type_spec = cp_parser_simple_type_specifier (parser, flags);
8144
8145 /* If we didn't find a type-specifier, and a type-specifier was not
8146 optional in this context, issue an error message. */
8147 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8148 {
8149 cp_parser_error (parser, "expected type specifier");
8150 return error_mark_node;
8151 }
8152
8153 return type_spec;
8154}
8155
8156/* Parse a simple-type-specifier.
8157
8158 simple-type-specifier:
8159 :: [opt] nested-name-specifier [opt] type-name
8160 :: [opt] nested-name-specifier template template-id
8161 char
8162 wchar_t
8163 bool
8164 short
8165 int
8166 long
8167 signed
8168 unsigned
8169 float
8170 double
8171 void
8172
8173 GNU Extension:
8174
8175 simple-type-specifier:
8176 __typeof__ unary-expression
8177 __typeof__ ( type-id )
8178
8179 For the various keywords, the value returned is simply the
8180 TREE_IDENTIFIER representing the keyword. For the first two
8181 productions, the value returned is the indicated TYPE_DECL. */
8182
8183static tree
94edc4ab 8184cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
a723baf1
MM
8185{
8186 tree type = NULL_TREE;
8187 cp_token *token;
8188
8189 /* Peek at the next token. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191
8192 /* If we're looking at a keyword, things are easy. */
8193 switch (token->keyword)
8194 {
8195 case RID_CHAR:
8196 case RID_WCHAR:
8197 case RID_BOOL:
8198 case RID_SHORT:
8199 case RID_INT:
8200 case RID_LONG:
8201 case RID_SIGNED:
8202 case RID_UNSIGNED:
8203 case RID_FLOAT:
8204 case RID_DOUBLE:
8205 case RID_VOID:
8206 /* Consume the token. */
8207 return cp_lexer_consume_token (parser->lexer)->value;
8208
8209 case RID_TYPEOF:
8210 {
8211 tree operand;
8212
8213 /* Consume the `typeof' token. */
8214 cp_lexer_consume_token (parser->lexer);
8215 /* Parse the operand to `typeof' */
8216 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8217 /* If it is not already a TYPE, take its type. */
8218 if (!TYPE_P (operand))
8219 operand = finish_typeof (operand);
8220
8221 return operand;
8222 }
8223
8224 default:
8225 break;
8226 }
8227
8228 /* The type-specifier must be a user-defined type. */
8229 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8230 {
8231 /* Don't gobble tokens or issue error messages if this is an
8232 optional type-specifier. */
8233 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8234 cp_parser_parse_tentatively (parser);
8235
8236 /* Look for the optional `::' operator. */
8237 cp_parser_global_scope_opt (parser,
8238 /*current_scope_valid_p=*/false);
8239 /* Look for the nested-name specifier. */
8240 cp_parser_nested_name_specifier_opt (parser,
8241 /*typename_keyword_p=*/false,
8242 /*check_dependency_p=*/true,
8243 /*type_p=*/false);
8244 /* If we have seen a nested-name-specifier, and the next token
8245 is `template', then we are using the template-id production. */
8246 if (parser->scope
8247 && cp_parser_optional_template_keyword (parser))
8248 {
8249 /* Look for the template-id. */
8250 type = cp_parser_template_id (parser,
8251 /*template_keyword_p=*/true,
8252 /*check_dependency_p=*/true);
8253 /* If the template-id did not name a type, we are out of
8254 luck. */
8255 if (TREE_CODE (type) != TYPE_DECL)
8256 {
8257 cp_parser_error (parser, "expected template-id for type");
8258 type = NULL_TREE;
8259 }
8260 }
8261 /* Otherwise, look for a type-name. */
8262 else
8263 {
8264 type = cp_parser_type_name (parser);
8265 if (type == error_mark_node)
8266 type = NULL_TREE;
8267 }
8268
8269 /* If it didn't work out, we don't have a TYPE. */
8270 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8271 && !cp_parser_parse_definitely (parser))
8272 type = NULL_TREE;
8273 }
8274
8275 /* If we didn't get a type-name, issue an error message. */
8276 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8277 {
8278 cp_parser_error (parser, "expected type-name");
8279 return error_mark_node;
8280 }
8281
8282 return type;
8283}
8284
8285/* Parse a type-name.
8286
8287 type-name:
8288 class-name
8289 enum-name
8290 typedef-name
8291
8292 enum-name:
8293 identifier
8294
8295 typedef-name:
8296 identifier
8297
8298 Returns a TYPE_DECL for the the type. */
8299
8300static tree
94edc4ab 8301cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8302{
8303 tree type_decl;
8304 tree identifier;
8305
8306 /* We can't know yet whether it is a class-name or not. */
8307 cp_parser_parse_tentatively (parser);
8308 /* Try a class-name. */
8309 type_decl = cp_parser_class_name (parser,
8310 /*typename_keyword_p=*/false,
8311 /*template_keyword_p=*/false,
8312 /*type_p=*/false,
a723baf1
MM
8313 /*check_dependency_p=*/true,
8314 /*class_head_p=*/false);
8315 /* If it's not a class-name, keep looking. */
8316 if (!cp_parser_parse_definitely (parser))
8317 {
8318 /* It must be a typedef-name or an enum-name. */
8319 identifier = cp_parser_identifier (parser);
8320 if (identifier == error_mark_node)
8321 return error_mark_node;
8322
8323 /* Look up the type-name. */
8324 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8325 /* Issue an error if we did not find a type-name. */
8326 if (TREE_CODE (type_decl) != TYPE_DECL)
8327 {
8328 cp_parser_error (parser, "expected type-name");
8329 type_decl = error_mark_node;
8330 }
8331 /* Remember that the name was used in the definition of the
8332 current class so that we can check later to see if the
8333 meaning would have been different after the class was
8334 entirely defined. */
8335 else if (type_decl != error_mark_node
8336 && !parser->scope)
8337 maybe_note_name_used_in_class (identifier, type_decl);
8338 }
8339
8340 return type_decl;
8341}
8342
8343
8344/* Parse an elaborated-type-specifier. Note that the grammar given
8345 here incorporates the resolution to DR68.
8346
8347 elaborated-type-specifier:
8348 class-key :: [opt] nested-name-specifier [opt] identifier
8349 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8350 enum :: [opt] nested-name-specifier [opt] identifier
8351 typename :: [opt] nested-name-specifier identifier
8352 typename :: [opt] nested-name-specifier template [opt]
8353 template-id
8354
8355 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8356 declared `friend'. If IS_DECLARATION is TRUE, then this
8357 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8358 something is being declared.
8359
8360 Returns the TYPE specified. */
8361
8362static tree
94edc4ab
NN
8363cp_parser_elaborated_type_specifier (cp_parser* parser,
8364 bool is_friend,
8365 bool is_declaration)
a723baf1
MM
8366{
8367 enum tag_types tag_type;
8368 tree identifier;
8369 tree type = NULL_TREE;
8370
8371 /* See if we're looking at the `enum' keyword. */
8372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8373 {
8374 /* Consume the `enum' token. */
8375 cp_lexer_consume_token (parser->lexer);
8376 /* Remember that it's an enumeration type. */
8377 tag_type = enum_type;
8378 }
8379 /* Or, it might be `typename'. */
8380 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8381 RID_TYPENAME))
8382 {
8383 /* Consume the `typename' token. */
8384 cp_lexer_consume_token (parser->lexer);
8385 /* Remember that it's a `typename' type. */
8386 tag_type = typename_type;
8387 /* The `typename' keyword is only allowed in templates. */
8388 if (!processing_template_decl)
8389 pedwarn ("using `typename' outside of template");
8390 }
8391 /* Otherwise it must be a class-key. */
8392 else
8393 {
8394 tag_type = cp_parser_class_key (parser);
8395 if (tag_type == none_type)
8396 return error_mark_node;
8397 }
8398
8399 /* Look for the `::' operator. */
8400 cp_parser_global_scope_opt (parser,
8401 /*current_scope_valid_p=*/false);
8402 /* Look for the nested-name-specifier. */
8403 if (tag_type == typename_type)
8fa1ad0e
MM
8404 {
8405 if (cp_parser_nested_name_specifier (parser,
8406 /*typename_keyword_p=*/true,
8407 /*check_dependency_p=*/true,
8408 /*type_p=*/true)
8409 == error_mark_node)
8410 return error_mark_node;
8411 }
a723baf1
MM
8412 else
8413 /* Even though `typename' is not present, the proposed resolution
8414 to Core Issue 180 says that in `class A<T>::B', `B' should be
8415 considered a type-name, even if `A<T>' is dependent. */
8416 cp_parser_nested_name_specifier_opt (parser,
8417 /*typename_keyword_p=*/true,
8418 /*check_dependency_p=*/true,
8419 /*type_p=*/true);
8420 /* For everything but enumeration types, consider a template-id. */
8421 if (tag_type != enum_type)
8422 {
8423 bool template_p = false;
8424 tree decl;
8425
8426 /* Allow the `template' keyword. */
8427 template_p = cp_parser_optional_template_keyword (parser);
8428 /* If we didn't see `template', we don't know if there's a
8429 template-id or not. */
8430 if (!template_p)
8431 cp_parser_parse_tentatively (parser);
8432 /* Parse the template-id. */
8433 decl = cp_parser_template_id (parser, template_p,
8434 /*check_dependency_p=*/true);
8435 /* If we didn't find a template-id, look for an ordinary
8436 identifier. */
8437 if (!template_p && !cp_parser_parse_definitely (parser))
8438 ;
8439 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8440 in effect, then we must assume that, upon instantiation, the
8441 template will correspond to a class. */
8442 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8443 && tag_type == typename_type)
8444 type = make_typename_type (parser->scope, decl,
8445 /*complain=*/1);
8446 else
8447 type = TREE_TYPE (decl);
8448 }
8449
8450 /* For an enumeration type, consider only a plain identifier. */
8451 if (!type)
8452 {
8453 identifier = cp_parser_identifier (parser);
8454
8455 if (identifier == error_mark_node)
8456 return error_mark_node;
8457
8458 /* For a `typename', we needn't call xref_tag. */
8459 if (tag_type == typename_type)
8460 return make_typename_type (parser->scope, identifier,
8461 /*complain=*/1);
8462 /* Look up a qualified name in the usual way. */
8463 if (parser->scope)
8464 {
8465 tree decl;
8466
8467 /* In an elaborated-type-specifier, names are assumed to name
8468 types, so we set IS_TYPE to TRUE when calling
8469 cp_parser_lookup_name. */
8470 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8471 /*is_type=*/true,
eea9800f 8472 /*is_namespace=*/false,
a723baf1 8473 /*check_dependency=*/true);
710b73e6
KL
8474
8475 /* If we are parsing friend declaration, DECL may be a
8476 TEMPLATE_DECL tree node here. However, we need to check
8477 whether this TEMPLATE_DECL results in valid code. Consider
8478 the following example:
8479
8480 namespace N {
8481 template <class T> class C {};
8482 }
8483 class X {
8484 template <class T> friend class N::C; // #1, valid code
8485 };
8486 template <class T> class Y {
8487 friend class N::C; // #2, invalid code
8488 };
8489
8490 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8491 name lookup of `N::C'. We see that friend declaration must
8492 be template for the code to be valid. Note that
8493 processing_template_decl does not work here since it is
8494 always 1 for the above two cases. */
8495
a723baf1 8496 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8497 (decl, /*tag_name_p=*/is_friend
8498 && parser->num_template_parameter_lists));
a723baf1
MM
8499
8500 if (TREE_CODE (decl) != TYPE_DECL)
8501 {
8502 error ("expected type-name");
8503 return error_mark_node;
8504 }
8505 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8506 && tag_type != enum_type)
8507 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8508 tag_type == record_type ? "struct" : "class");
8509 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8510 && tag_type == enum_type)
8511 error ("`%T' referred to as enum", TREE_TYPE (decl));
8512
8513 type = TREE_TYPE (decl);
8514 }
8515 else
8516 {
8517 /* An elaborated-type-specifier sometimes introduces a new type and
8518 sometimes names an existing type. Normally, the rule is that it
8519 introduces a new type only if there is not an existing type of
8520 the same name already in scope. For example, given:
8521
8522 struct S {};
8523 void f() { struct S s; }
8524
8525 the `struct S' in the body of `f' is the same `struct S' as in
8526 the global scope; the existing definition is used. However, if
8527 there were no global declaration, this would introduce a new
8528 local class named `S'.
8529
8530 An exception to this rule applies to the following code:
8531
8532 namespace N { struct S; }
8533
8534 Here, the elaborated-type-specifier names a new type
8535 unconditionally; even if there is already an `S' in the
8536 containing scope this declaration names a new type.
8537 This exception only applies if the elaborated-type-specifier
8538 forms the complete declaration:
8539
8540 [class.name]
8541
8542 A declaration consisting solely of `class-key identifier ;' is
8543 either a redeclaration of the name in the current scope or a
8544 forward declaration of the identifier as a class name. It
8545 introduces the name into the current scope.
8546
8547 We are in this situation precisely when the next token is a `;'.
8548
8549 An exception to the exception is that a `friend' declaration does
8550 *not* name a new type; i.e., given:
8551
8552 struct S { friend struct T; };
8553
8554 `T' is not a new type in the scope of `S'.
8555
8556 Also, `new struct S' or `sizeof (struct S)' never results in the
8557 definition of a new type; a new type can only be declared in a
9bcb9aae 8558 declaration context. */
a723baf1
MM
8559
8560 type = xref_tag (tag_type, identifier,
8561 /*attributes=*/NULL_TREE,
8562 (is_friend
8563 || !is_declaration
8564 || cp_lexer_next_token_is_not (parser->lexer,
8565 CPP_SEMICOLON)));
8566 }
8567 }
8568 if (tag_type != enum_type)
8569 cp_parser_check_class_key (tag_type, type);
8570 return type;
8571}
8572
8573/* Parse an enum-specifier.
8574
8575 enum-specifier:
8576 enum identifier [opt] { enumerator-list [opt] }
8577
8578 Returns an ENUM_TYPE representing the enumeration. */
8579
8580static tree
94edc4ab 8581cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
8582{
8583 cp_token *token;
8584 tree identifier = NULL_TREE;
8585 tree type;
8586
8587 /* Look for the `enum' keyword. */
8588 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8589 return error_mark_node;
8590 /* Peek at the next token. */
8591 token = cp_lexer_peek_token (parser->lexer);
8592
8593 /* See if it is an identifier. */
8594 if (token->type == CPP_NAME)
8595 identifier = cp_parser_identifier (parser);
8596
8597 /* Look for the `{'. */
8598 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8599 return error_mark_node;
8600
8601 /* At this point, we're going ahead with the enum-specifier, even
8602 if some other problem occurs. */
8603 cp_parser_commit_to_tentative_parse (parser);
8604
8605 /* Issue an error message if type-definitions are forbidden here. */
8606 cp_parser_check_type_definition (parser);
8607
8608 /* Create the new type. */
8609 type = start_enum (identifier ? identifier : make_anon_name ());
8610
8611 /* Peek at the next token. */
8612 token = cp_lexer_peek_token (parser->lexer);
8613 /* If it's not a `}', then there are some enumerators. */
8614 if (token->type != CPP_CLOSE_BRACE)
8615 cp_parser_enumerator_list (parser, type);
8616 /* Look for the `}'. */
8617 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8618
8619 /* Finish up the enumeration. */
8620 finish_enum (type);
8621
8622 return type;
8623}
8624
8625/* Parse an enumerator-list. The enumerators all have the indicated
8626 TYPE.
8627
8628 enumerator-list:
8629 enumerator-definition
8630 enumerator-list , enumerator-definition */
8631
8632static void
94edc4ab 8633cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
8634{
8635 while (true)
8636 {
8637 cp_token *token;
8638
8639 /* Parse an enumerator-definition. */
8640 cp_parser_enumerator_definition (parser, type);
8641 /* Peek at the next token. */
8642 token = cp_lexer_peek_token (parser->lexer);
8643 /* If it's not a `,', then we've reached the end of the
8644 list. */
8645 if (token->type != CPP_COMMA)
8646 break;
8647 /* Otherwise, consume the `,' and keep going. */
8648 cp_lexer_consume_token (parser->lexer);
8649 /* If the next token is a `}', there is a trailing comma. */
8650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8651 {
8652 if (pedantic && !in_system_header)
8653 pedwarn ("comma at end of enumerator list");
8654 break;
8655 }
8656 }
8657}
8658
8659/* Parse an enumerator-definition. The enumerator has the indicated
8660 TYPE.
8661
8662 enumerator-definition:
8663 enumerator
8664 enumerator = constant-expression
8665
8666 enumerator:
8667 identifier */
8668
8669static void
94edc4ab 8670cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
8671{
8672 cp_token *token;
8673 tree identifier;
8674 tree value;
8675
8676 /* Look for the identifier. */
8677 identifier = cp_parser_identifier (parser);
8678 if (identifier == error_mark_node)
8679 return;
8680
8681 /* Peek at the next token. */
8682 token = cp_lexer_peek_token (parser->lexer);
8683 /* If it's an `=', then there's an explicit value. */
8684 if (token->type == CPP_EQ)
8685 {
8686 /* Consume the `=' token. */
8687 cp_lexer_consume_token (parser->lexer);
8688 /* Parse the value. */
14d22dd6 8689 value = cp_parser_constant_expression (parser,
d17811fd 8690 /*allow_non_constant_p=*/false,
14d22dd6 8691 NULL);
a723baf1
MM
8692 }
8693 else
8694 value = NULL_TREE;
8695
8696 /* Create the enumerator. */
8697 build_enumerator (identifier, value, type);
8698}
8699
8700/* Parse a namespace-name.
8701
8702 namespace-name:
8703 original-namespace-name
8704 namespace-alias
8705
8706 Returns the NAMESPACE_DECL for the namespace. */
8707
8708static tree
94edc4ab 8709cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
8710{
8711 tree identifier;
8712 tree namespace_decl;
8713
8714 /* Get the name of the namespace. */
8715 identifier = cp_parser_identifier (parser);
8716 if (identifier == error_mark_node)
8717 return error_mark_node;
8718
eea9800f
MM
8719 /* Look up the identifier in the currently active scope. Look only
8720 for namespaces, due to:
8721
8722 [basic.lookup.udir]
8723
8724 When looking up a namespace-name in a using-directive or alias
8725 definition, only namespace names are considered.
8726
8727 And:
8728
8729 [basic.lookup.qual]
8730
8731 During the lookup of a name preceding the :: scope resolution
8732 operator, object, function, and enumerator names are ignored.
8733
8734 (Note that cp_parser_class_or_namespace_name only calls this
8735 function if the token after the name is the scope resolution
8736 operator.) */
8737 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f
MM
8738 /*is_type=*/false,
8739 /*is_namespace=*/true,
8740 /*check_dependency=*/true);
a723baf1
MM
8741 /* If it's not a namespace, issue an error. */
8742 if (namespace_decl == error_mark_node
8743 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8744 {
8745 cp_parser_error (parser, "expected namespace-name");
8746 namespace_decl = error_mark_node;
8747 }
8748
8749 return namespace_decl;
8750}
8751
8752/* Parse a namespace-definition.
8753
8754 namespace-definition:
8755 named-namespace-definition
8756 unnamed-namespace-definition
8757
8758 named-namespace-definition:
8759 original-namespace-definition
8760 extension-namespace-definition
8761
8762 original-namespace-definition:
8763 namespace identifier { namespace-body }
8764
8765 extension-namespace-definition:
8766 namespace original-namespace-name { namespace-body }
8767
8768 unnamed-namespace-definition:
8769 namespace { namespace-body } */
8770
8771static void
94edc4ab 8772cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
8773{
8774 tree identifier;
8775
8776 /* Look for the `namespace' keyword. */
8777 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8778
8779 /* Get the name of the namespace. We do not attempt to distinguish
8780 between an original-namespace-definition and an
8781 extension-namespace-definition at this point. The semantic
8782 analysis routines are responsible for that. */
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8784 identifier = cp_parser_identifier (parser);
8785 else
8786 identifier = NULL_TREE;
8787
8788 /* Look for the `{' to start the namespace. */
8789 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8790 /* Start the namespace. */
8791 push_namespace (identifier);
8792 /* Parse the body of the namespace. */
8793 cp_parser_namespace_body (parser);
8794 /* Finish the namespace. */
8795 pop_namespace ();
8796 /* Look for the final `}'. */
8797 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8798}
8799
8800/* Parse a namespace-body.
8801
8802 namespace-body:
8803 declaration-seq [opt] */
8804
8805static void
94edc4ab 8806cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
8807{
8808 cp_parser_declaration_seq_opt (parser);
8809}
8810
8811/* Parse a namespace-alias-definition.
8812
8813 namespace-alias-definition:
8814 namespace identifier = qualified-namespace-specifier ; */
8815
8816static void
94edc4ab 8817cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
8818{
8819 tree identifier;
8820 tree namespace_specifier;
8821
8822 /* Look for the `namespace' keyword. */
8823 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8824 /* Look for the identifier. */
8825 identifier = cp_parser_identifier (parser);
8826 if (identifier == error_mark_node)
8827 return;
8828 /* Look for the `=' token. */
8829 cp_parser_require (parser, CPP_EQ, "`='");
8830 /* Look for the qualified-namespace-specifier. */
8831 namespace_specifier
8832 = cp_parser_qualified_namespace_specifier (parser);
8833 /* Look for the `;' token. */
8834 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8835
8836 /* Register the alias in the symbol table. */
8837 do_namespace_alias (identifier, namespace_specifier);
8838}
8839
8840/* Parse a qualified-namespace-specifier.
8841
8842 qualified-namespace-specifier:
8843 :: [opt] nested-name-specifier [opt] namespace-name
8844
8845 Returns a NAMESPACE_DECL corresponding to the specified
8846 namespace. */
8847
8848static tree
94edc4ab 8849cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
8850{
8851 /* Look for the optional `::'. */
8852 cp_parser_global_scope_opt (parser,
8853 /*current_scope_valid_p=*/false);
8854
8855 /* Look for the optional nested-name-specifier. */
8856 cp_parser_nested_name_specifier_opt (parser,
8857 /*typename_keyword_p=*/false,
8858 /*check_dependency_p=*/true,
8859 /*type_p=*/false);
8860
8861 return cp_parser_namespace_name (parser);
8862}
8863
8864/* Parse a using-declaration.
8865
8866 using-declaration:
8867 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8868 using :: unqualified-id ; */
8869
8870static void
94edc4ab 8871cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
8872{
8873 cp_token *token;
8874 bool typename_p = false;
8875 bool global_scope_p;
8876 tree decl;
8877 tree identifier;
8878 tree scope;
8879
8880 /* Look for the `using' keyword. */
8881 cp_parser_require_keyword (parser, RID_USING, "`using'");
8882
8883 /* Peek at the next token. */
8884 token = cp_lexer_peek_token (parser->lexer);
8885 /* See if it's `typename'. */
8886 if (token->keyword == RID_TYPENAME)
8887 {
8888 /* Remember that we've seen it. */
8889 typename_p = true;
8890 /* Consume the `typename' token. */
8891 cp_lexer_consume_token (parser->lexer);
8892 }
8893
8894 /* Look for the optional global scope qualification. */
8895 global_scope_p
8896 = (cp_parser_global_scope_opt (parser,
8897 /*current_scope_valid_p=*/false)
8898 != NULL_TREE);
8899
8900 /* If we saw `typename', or didn't see `::', then there must be a
8901 nested-name-specifier present. */
8902 if (typename_p || !global_scope_p)
8903 cp_parser_nested_name_specifier (parser, typename_p,
8904 /*check_dependency_p=*/true,
8905 /*type_p=*/false);
8906 /* Otherwise, we could be in either of the two productions. In that
8907 case, treat the nested-name-specifier as optional. */
8908 else
8909 cp_parser_nested_name_specifier_opt (parser,
8910 /*typename_keyword_p=*/false,
8911 /*check_dependency_p=*/true,
8912 /*type_p=*/false);
8913
8914 /* Parse the unqualified-id. */
8915 identifier = cp_parser_unqualified_id (parser,
8916 /*template_keyword_p=*/false,
8917 /*check_dependency_p=*/true);
8918
8919 /* The function we call to handle a using-declaration is different
8920 depending on what scope we are in. */
8921 scope = current_scope ();
8922 if (scope && TYPE_P (scope))
8923 {
8924 /* Create the USING_DECL. */
8925 decl = do_class_using_decl (build_nt (SCOPE_REF,
8926 parser->scope,
8927 identifier));
8928 /* Add it to the list of members in this class. */
8929 finish_member_declaration (decl);
8930 }
8931 else
8932 {
8933 decl = cp_parser_lookup_name_simple (parser, identifier);
4eb6d609
MM
8934 if (decl == error_mark_node)
8935 {
8936 if (parser->scope && parser->scope != global_namespace)
8937 error ("`%D::%D' has not been declared",
8938 parser->scope, identifier);
8939 else
8940 error ("`::%D' has not been declared", identifier);
8941 }
8942 else if (scope)
a723baf1
MM
8943 do_local_using_decl (decl);
8944 else
8945 do_toplevel_using_decl (decl);
8946 }
8947
8948 /* Look for the final `;'. */
8949 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8950}
8951
8952/* Parse a using-directive.
8953
8954 using-directive:
8955 using namespace :: [opt] nested-name-specifier [opt]
8956 namespace-name ; */
8957
8958static void
94edc4ab 8959cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
8960{
8961 tree namespace_decl;
8962
8963 /* Look for the `using' keyword. */
8964 cp_parser_require_keyword (parser, RID_USING, "`using'");
8965 /* And the `namespace' keyword. */
8966 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8967 /* Look for the optional `::' operator. */
8968 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 8969 /* And the optional nested-name-specifier. */
a723baf1
MM
8970 cp_parser_nested_name_specifier_opt (parser,
8971 /*typename_keyword_p=*/false,
8972 /*check_dependency_p=*/true,
8973 /*type_p=*/false);
8974 /* Get the namespace being used. */
8975 namespace_decl = cp_parser_namespace_name (parser);
8976 /* Update the symbol table. */
8977 do_using_directive (namespace_decl);
8978 /* Look for the final `;'. */
8979 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8980}
8981
8982/* Parse an asm-definition.
8983
8984 asm-definition:
8985 asm ( string-literal ) ;
8986
8987 GNU Extension:
8988
8989 asm-definition:
8990 asm volatile [opt] ( string-literal ) ;
8991 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
8992 asm volatile [opt] ( string-literal : asm-operand-list [opt]
8993 : asm-operand-list [opt] ) ;
8994 asm volatile [opt] ( string-literal : asm-operand-list [opt]
8995 : asm-operand-list [opt]
8996 : asm-operand-list [opt] ) ; */
8997
8998static void
94edc4ab 8999cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9000{
9001 cp_token *token;
9002 tree string;
9003 tree outputs = NULL_TREE;
9004 tree inputs = NULL_TREE;
9005 tree clobbers = NULL_TREE;
9006 tree asm_stmt;
9007 bool volatile_p = false;
9008 bool extended_p = false;
9009
9010 /* Look for the `asm' keyword. */
9011 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9012 /* See if the next token is `volatile'. */
9013 if (cp_parser_allow_gnu_extensions_p (parser)
9014 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9015 {
9016 /* Remember that we saw the `volatile' keyword. */
9017 volatile_p = true;
9018 /* Consume the token. */
9019 cp_lexer_consume_token (parser->lexer);
9020 }
9021 /* Look for the opening `('. */
9022 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9023 /* Look for the string. */
9024 token = cp_parser_require (parser, CPP_STRING, "asm body");
9025 if (!token)
9026 return;
9027 string = token->value;
9028 /* If we're allowing GNU extensions, check for the extended assembly
9029 syntax. Unfortunately, the `:' tokens need not be separated by
9030 a space in C, and so, for compatibility, we tolerate that here
9031 too. Doing that means that we have to treat the `::' operator as
9032 two `:' tokens. */
9033 if (cp_parser_allow_gnu_extensions_p (parser)
9034 && at_function_scope_p ()
9035 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9036 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9037 {
9038 bool inputs_p = false;
9039 bool clobbers_p = false;
9040
9041 /* The extended syntax was used. */
9042 extended_p = true;
9043
9044 /* Look for outputs. */
9045 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9046 {
9047 /* Consume the `:'. */
9048 cp_lexer_consume_token (parser->lexer);
9049 /* Parse the output-operands. */
9050 if (cp_lexer_next_token_is_not (parser->lexer,
9051 CPP_COLON)
9052 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9053 CPP_SCOPE)
9054 && cp_lexer_next_token_is_not (parser->lexer,
9055 CPP_CLOSE_PAREN))
a723baf1
MM
9056 outputs = cp_parser_asm_operand_list (parser);
9057 }
9058 /* If the next token is `::', there are no outputs, and the
9059 next token is the beginning of the inputs. */
9060 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9061 {
9062 /* Consume the `::' token. */
9063 cp_lexer_consume_token (parser->lexer);
9064 /* The inputs are coming next. */
9065 inputs_p = true;
9066 }
9067
9068 /* Look for inputs. */
9069 if (inputs_p
9070 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9071 {
9072 if (!inputs_p)
9073 /* Consume the `:'. */
9074 cp_lexer_consume_token (parser->lexer);
9075 /* Parse the output-operands. */
9076 if (cp_lexer_next_token_is_not (parser->lexer,
9077 CPP_COLON)
9078 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9079 CPP_SCOPE)
9080 && cp_lexer_next_token_is_not (parser->lexer,
9081 CPP_CLOSE_PAREN))
a723baf1
MM
9082 inputs = cp_parser_asm_operand_list (parser);
9083 }
9084 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9085 /* The clobbers are coming next. */
9086 clobbers_p = true;
9087
9088 /* Look for clobbers. */
9089 if (clobbers_p
9090 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9091 {
9092 if (!clobbers_p)
9093 /* Consume the `:'. */
9094 cp_lexer_consume_token (parser->lexer);
9095 /* Parse the clobbers. */
8caf4c38
MM
9096 if (cp_lexer_next_token_is_not (parser->lexer,
9097 CPP_CLOSE_PAREN))
9098 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9099 }
9100 }
9101 /* Look for the closing `)'. */
9102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7efa3e22 9103 cp_parser_skip_to_closing_parenthesis (parser, true, false);
a723baf1
MM
9104 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9105
9106 /* Create the ASM_STMT. */
9107 if (at_function_scope_p ())
9108 {
9109 asm_stmt =
9110 finish_asm_stmt (volatile_p
9111 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9112 string, outputs, inputs, clobbers);
9113 /* If the extended syntax was not used, mark the ASM_STMT. */
9114 if (!extended_p)
9115 ASM_INPUT_P (asm_stmt) = 1;
9116 }
9117 else
9118 assemble_asm (string);
9119}
9120
9121/* Declarators [gram.dcl.decl] */
9122
9123/* Parse an init-declarator.
9124
9125 init-declarator:
9126 declarator initializer [opt]
9127
9128 GNU Extension:
9129
9130 init-declarator:
9131 declarator asm-specification [opt] attributes [opt] initializer [opt]
9132
9133 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9134 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9135 then this declarator appears in a class scope. The new DECL created
9136 by this declarator is returned.
a723baf1
MM
9137
9138 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9139 for a function-definition here as well. If the declarator is a
9140 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9141 be TRUE upon return. By that point, the function-definition will
9142 have been completely parsed.
9143
9144 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9145 is FALSE. */
9146
9147static tree
94edc4ab
NN
9148cp_parser_init_declarator (cp_parser* parser,
9149 tree decl_specifiers,
9150 tree prefix_attributes,
9151 bool function_definition_allowed_p,
9152 bool member_p,
9153 bool* function_definition_p)
a723baf1
MM
9154{
9155 cp_token *token;
9156 tree declarator;
9157 tree attributes;
9158 tree asm_specification;
9159 tree initializer;
9160 tree decl = NULL_TREE;
9161 tree scope;
a723baf1
MM
9162 bool is_initialized;
9163 bool is_parenthesized_init;
39703eb9 9164 bool is_non_constant_init;
7efa3e22 9165 int ctor_dtor_or_conv_p;
a723baf1
MM
9166 bool friend_p;
9167
9168 /* Assume that this is not the declarator for a function
9169 definition. */
9170 if (function_definition_p)
9171 *function_definition_p = false;
9172
9173 /* Defer access checks while parsing the declarator; we cannot know
9174 what names are accessible until we know what is being
9175 declared. */
cf22909c
KL
9176 resume_deferring_access_checks ();
9177
a723baf1
MM
9178 /* Parse the declarator. */
9179 declarator
62b8a44e 9180 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9181 &ctor_dtor_or_conv_p);
9182 /* Gather up the deferred checks. */
cf22909c 9183 stop_deferring_access_checks ();
24c0ef37 9184
a723baf1
MM
9185 /* If the DECLARATOR was erroneous, there's no need to go
9186 further. */
9187 if (declarator == error_mark_node)
cf22909c 9188 return error_mark_node;
a723baf1
MM
9189
9190 /* Figure out what scope the entity declared by the DECLARATOR is
9191 located in. `grokdeclarator' sometimes changes the scope, so
9192 we compute it now. */
9193 scope = get_scope_of_declarator (declarator);
9194
9195 /* If we're allowing GNU extensions, look for an asm-specification
9196 and attributes. */
9197 if (cp_parser_allow_gnu_extensions_p (parser))
9198 {
9199 /* Look for an asm-specification. */
9200 asm_specification = cp_parser_asm_specification_opt (parser);
9201 /* And attributes. */
9202 attributes = cp_parser_attributes_opt (parser);
9203 }
9204 else
9205 {
9206 asm_specification = NULL_TREE;
9207 attributes = NULL_TREE;
9208 }
9209
9210 /* Peek at the next token. */
9211 token = cp_lexer_peek_token (parser->lexer);
9212 /* Check to see if the token indicates the start of a
9213 function-definition. */
9214 if (cp_parser_token_starts_function_definition_p (token))
9215 {
9216 if (!function_definition_allowed_p)
9217 {
9218 /* If a function-definition should not appear here, issue an
9219 error message. */
9220 cp_parser_error (parser,
9221 "a function-definition is not allowed here");
9222 return error_mark_node;
9223 }
9224 else
9225 {
a723baf1
MM
9226 /* Neither attributes nor an asm-specification are allowed
9227 on a function-definition. */
9228 if (asm_specification)
9229 error ("an asm-specification is not allowed on a function-definition");
9230 if (attributes)
9231 error ("attributes are not allowed on a function-definition");
9232 /* This is a function-definition. */
9233 *function_definition_p = true;
9234
a723baf1
MM
9235 /* Parse the function definition. */
9236 decl = (cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 9237 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9238
a723baf1
MM
9239 return decl;
9240 }
9241 }
9242
9243 /* [dcl.dcl]
9244
9245 Only in function declarations for constructors, destructors, and
9246 type conversions can the decl-specifier-seq be omitted.
9247
9248 We explicitly postpone this check past the point where we handle
9249 function-definitions because we tolerate function-definitions
9250 that are missing their return types in some modes. */
7efa3e22 9251 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9252 {
9253 cp_parser_error (parser,
9254 "expected constructor, destructor, or type conversion");
9255 return error_mark_node;
9256 }
9257
9258 /* An `=' or an `(' indicates an initializer. */
9259 is_initialized = (token->type == CPP_EQ
9260 || token->type == CPP_OPEN_PAREN);
9261 /* If the init-declarator isn't initialized and isn't followed by a
9262 `,' or `;', it's not a valid init-declarator. */
9263 if (!is_initialized
9264 && token->type != CPP_COMMA
9265 && token->type != CPP_SEMICOLON)
9266 {
9267 cp_parser_error (parser, "expected init-declarator");
9268 return error_mark_node;
9269 }
9270
9271 /* Because start_decl has side-effects, we should only call it if we
9272 know we're going ahead. By this point, we know that we cannot
9273 possibly be looking at any other construct. */
9274 cp_parser_commit_to_tentative_parse (parser);
9275
9276 /* Check to see whether or not this declaration is a friend. */
9277 friend_p = cp_parser_friend_p (decl_specifiers);
9278
9279 /* Check that the number of template-parameter-lists is OK. */
9280 if (!cp_parser_check_declarator_template_parameters (parser,
9281 declarator))
cf22909c 9282 return error_mark_node;
a723baf1
MM
9283
9284 /* Enter the newly declared entry in the symbol table. If we're
9285 processing a declaration in a class-specifier, we wait until
9286 after processing the initializer. */
9287 if (!member_p)
9288 {
9289 if (parser->in_unbraced_linkage_specification_p)
9290 {
9291 decl_specifiers = tree_cons (error_mark_node,
9292 get_identifier ("extern"),
9293 decl_specifiers);
9294 have_extern_spec = false;
9295 }
9296 decl = start_decl (declarator,
9297 decl_specifiers,
9298 is_initialized,
9299 attributes,
9300 prefix_attributes);
9301 }
9302
9303 /* Enter the SCOPE. That way unqualified names appearing in the
9304 initializer will be looked up in SCOPE. */
9305 if (scope)
9306 push_scope (scope);
9307
9308 /* Perform deferred access control checks, now that we know in which
9309 SCOPE the declared entity resides. */
9310 if (!member_p && decl)
9311 {
9312 tree saved_current_function_decl = NULL_TREE;
9313
9314 /* If the entity being declared is a function, pretend that we
9315 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9316 things that would not otherwise be accessible. */
a723baf1
MM
9317 if (TREE_CODE (decl) == FUNCTION_DECL)
9318 {
9319 saved_current_function_decl = current_function_decl;
9320 current_function_decl = decl;
9321 }
9322
cf22909c
KL
9323 /* Perform the access control checks for the declarator and the
9324 the decl-specifiers. */
9325 perform_deferred_access_checks ();
a723baf1
MM
9326
9327 /* Restore the saved value. */
9328 if (TREE_CODE (decl) == FUNCTION_DECL)
9329 current_function_decl = saved_current_function_decl;
9330 }
9331
9332 /* Parse the initializer. */
9333 if (is_initialized)
39703eb9
MM
9334 initializer = cp_parser_initializer (parser,
9335 &is_parenthesized_init,
9336 &is_non_constant_init);
a723baf1
MM
9337 else
9338 {
9339 initializer = NULL_TREE;
9340 is_parenthesized_init = false;
39703eb9 9341 is_non_constant_init = true;
a723baf1
MM
9342 }
9343
9344 /* The old parser allows attributes to appear after a parenthesized
9345 initializer. Mark Mitchell proposed removing this functionality
9346 on the GCC mailing lists on 2002-08-13. This parser accepts the
9347 attributes -- but ignores them. */
9348 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9349 if (cp_parser_attributes_opt (parser))
9350 warning ("attributes after parenthesized initializer ignored");
9351
9352 /* Leave the SCOPE, now that we have processed the initializer. It
9353 is important to do this before calling cp_finish_decl because it
9354 makes decisions about whether to create DECL_STMTs or not based
9355 on the current scope. */
9356 if (scope)
9357 pop_scope (scope);
9358
9359 /* For an in-class declaration, use `grokfield' to create the
9360 declaration. */
9361 if (member_p)
8db1028e
NS
9362 {
9363 decl = grokfield (declarator, decl_specifiers,
9364 initializer, /*asmspec=*/NULL_TREE,
a723baf1 9365 /*attributes=*/NULL_TREE);
8db1028e
NS
9366 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9367 cp_parser_save_default_args (parser, decl);
9368 }
9369
a723baf1
MM
9370 /* Finish processing the declaration. But, skip friend
9371 declarations. */
9372 if (!friend_p && decl)
9373 cp_finish_decl (decl,
9374 initializer,
9375 asm_specification,
9376 /* If the initializer is in parentheses, then this is
9377 a direct-initialization, which means that an
9378 `explicit' constructor is OK. Otherwise, an
9379 `explicit' constructor cannot be used. */
9380 ((is_parenthesized_init || !is_initialized)
9381 ? 0 : LOOKUP_ONLYCONVERTING));
9382
39703eb9
MM
9383 /* Remember whether or not variables were initialized by
9384 constant-expressions. */
9385 if (decl && TREE_CODE (decl) == VAR_DECL
9386 && is_initialized && !is_non_constant_init)
9387 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9388
a723baf1
MM
9389 return decl;
9390}
9391
9392/* Parse a declarator.
9393
9394 declarator:
9395 direct-declarator
9396 ptr-operator declarator
9397
9398 abstract-declarator:
9399 ptr-operator abstract-declarator [opt]
9400 direct-abstract-declarator
9401
9402 GNU Extensions:
9403
9404 declarator:
9405 attributes [opt] direct-declarator
9406 attributes [opt] ptr-operator declarator
9407
9408 abstract-declarator:
9409 attributes [opt] ptr-operator abstract-declarator [opt]
9410 attributes [opt] direct-abstract-declarator
9411
9412 Returns a representation of the declarator. If the declarator has
9413 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 9414 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
9415 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9416 used. The first operand is the TYPE for `X'. The second operand
9417 is an INDIRECT_REF whose operand is the sub-declarator.
9418
34cd5ae7 9419 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
9420
9421 (It would be better to define a structure type to represent
9422 declarators, rather than abusing `tree' nodes to represent
9423 declarators. That would be much clearer and save some memory.
9424 There is no reason for declarators to be garbage-collected, for
9425 example; they are created during parser and no longer needed after
9426 `grokdeclarator' has been called.)
9427
9428 For a ptr-operator that has the optional cv-qualifier-seq,
9429 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9430 node.
9431
7efa3e22
NS
9432 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9433 detect constructor, destructor or conversion operators. It is set
9434 to -1 if the declarator is a name, and +1 if it is a
9435 function. Otherwise it is set to zero. Usually you just want to
9436 test for >0, but internally the negative value is used.
9437
a723baf1
MM
9438 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9439 a decl-specifier-seq unless it declares a constructor, destructor,
9440 or conversion. It might seem that we could check this condition in
9441 semantic analysis, rather than parsing, but that makes it difficult
9442 to handle something like `f()'. We want to notice that there are
9443 no decl-specifiers, and therefore realize that this is an
9444 expression, not a declaration.) */
9445
9446static tree
94edc4ab
NN
9447cp_parser_declarator (cp_parser* parser,
9448 cp_parser_declarator_kind dcl_kind,
7efa3e22 9449 int* ctor_dtor_or_conv_p)
a723baf1
MM
9450{
9451 cp_token *token;
9452 tree declarator;
9453 enum tree_code code;
9454 tree cv_qualifier_seq;
9455 tree class_type;
9456 tree attributes = NULL_TREE;
9457
9458 /* Assume this is not a constructor, destructor, or type-conversion
9459 operator. */
9460 if (ctor_dtor_or_conv_p)
7efa3e22 9461 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
9462
9463 if (cp_parser_allow_gnu_extensions_p (parser))
9464 attributes = cp_parser_attributes_opt (parser);
9465
9466 /* Peek at the next token. */
9467 token = cp_lexer_peek_token (parser->lexer);
9468
9469 /* Check for the ptr-operator production. */
9470 cp_parser_parse_tentatively (parser);
9471 /* Parse the ptr-operator. */
9472 code = cp_parser_ptr_operator (parser,
9473 &class_type,
9474 &cv_qualifier_seq);
9475 /* If that worked, then we have a ptr-operator. */
9476 if (cp_parser_parse_definitely (parser))
9477 {
9478 /* The dependent declarator is optional if we are parsing an
9479 abstract-declarator. */
62b8a44e 9480 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9481 cp_parser_parse_tentatively (parser);
9482
9483 /* Parse the dependent declarator. */
62b8a44e 9484 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
9485 /*ctor_dtor_or_conv_p=*/NULL);
9486
9487 /* If we are parsing an abstract-declarator, we must handle the
9488 case where the dependent declarator is absent. */
62b8a44e
NS
9489 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9490 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9491 declarator = NULL_TREE;
9492
9493 /* Build the representation of the ptr-operator. */
9494 if (code == INDIRECT_REF)
9495 declarator = make_pointer_declarator (cv_qualifier_seq,
9496 declarator);
9497 else
9498 declarator = make_reference_declarator (cv_qualifier_seq,
9499 declarator);
9500 /* Handle the pointer-to-member case. */
9501 if (class_type)
9502 declarator = build_nt (SCOPE_REF, class_type, declarator);
9503 }
9504 /* Everything else is a direct-declarator. */
9505 else
7efa3e22 9506 declarator = cp_parser_direct_declarator (parser, dcl_kind,
a723baf1
MM
9507 ctor_dtor_or_conv_p);
9508
9509 if (attributes && declarator != error_mark_node)
9510 declarator = tree_cons (attributes, declarator, NULL_TREE);
9511
9512 return declarator;
9513}
9514
9515/* Parse a direct-declarator or direct-abstract-declarator.
9516
9517 direct-declarator:
9518 declarator-id
9519 direct-declarator ( parameter-declaration-clause )
9520 cv-qualifier-seq [opt]
9521 exception-specification [opt]
9522 direct-declarator [ constant-expression [opt] ]
9523 ( declarator )
9524
9525 direct-abstract-declarator:
9526 direct-abstract-declarator [opt]
9527 ( parameter-declaration-clause )
9528 cv-qualifier-seq [opt]
9529 exception-specification [opt]
9530 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9531 ( abstract-declarator )
9532
62b8a44e
NS
9533 Returns a representation of the declarator. DCL_KIND is
9534 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9535 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9536 we are parsing a direct-declarator. It is
9537 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9538 of ambiguity we prefer an abstract declarator, as per
9539 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
9540 cp_parser_declarator.
9541
9542 For the declarator-id production, the representation is as for an
9543 id-expression, except that a qualified name is represented as a
9544 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9545 see the documentation of the FUNCTION_DECLARATOR_* macros for
9546 information about how to find the various declarator components.
9547 An array-declarator is represented as an ARRAY_REF. The
9548 direct-declarator is the first operand; the constant-expression
9549 indicating the size of the array is the second operand. */
9550
9551static tree
94edc4ab
NN
9552cp_parser_direct_declarator (cp_parser* parser,
9553 cp_parser_declarator_kind dcl_kind,
7efa3e22 9554 int* ctor_dtor_or_conv_p)
a723baf1
MM
9555{
9556 cp_token *token;
62b8a44e 9557 tree declarator = NULL_TREE;
a723baf1
MM
9558 tree scope = NULL_TREE;
9559 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9560 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
9561 bool first = true;
9562
9563 while (true)
a723baf1 9564 {
62b8a44e
NS
9565 /* Peek at the next token. */
9566 token = cp_lexer_peek_token (parser->lexer);
9567 if (token->type == CPP_OPEN_PAREN)
a723baf1 9568 {
62b8a44e
NS
9569 /* This is either a parameter-declaration-clause, or a
9570 parenthesized declarator. When we know we are parsing a
34cd5ae7 9571 named declarator, it must be a parenthesized declarator
62b8a44e
NS
9572 if FIRST is true. For instance, `(int)' is a
9573 parameter-declaration-clause, with an omitted
9574 direct-abstract-declarator. But `((*))', is a
9575 parenthesized abstract declarator. Finally, when T is a
9576 template parameter `(T)' is a
34cd5ae7 9577 parameter-declaration-clause, and not a parenthesized
62b8a44e 9578 named declarator.
a723baf1 9579
62b8a44e
NS
9580 We first try and parse a parameter-declaration-clause,
9581 and then try a nested declarator (if FIRST is true).
a723baf1 9582
62b8a44e
NS
9583 It is not an error for it not to be a
9584 parameter-declaration-clause, even when FIRST is
9585 false. Consider,
9586
9587 int i (int);
9588 int i (3);
9589
9590 The first is the declaration of a function while the
9591 second is a the definition of a variable, including its
9592 initializer.
9593
9594 Having seen only the parenthesis, we cannot know which of
9595 these two alternatives should be selected. Even more
9596 complex are examples like:
9597
9598 int i (int (a));
9599 int i (int (3));
9600
9601 The former is a function-declaration; the latter is a
9602 variable initialization.
9603
34cd5ae7 9604 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
9605 that fails, we back out and return. */
9606
9607 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 9608 {
62b8a44e
NS
9609 tree params;
9610
9611 cp_parser_parse_tentatively (parser);
a723baf1 9612
62b8a44e
NS
9613 /* Consume the `('. */
9614 cp_lexer_consume_token (parser->lexer);
9615 if (first)
9616 {
9617 /* If this is going to be an abstract declarator, we're
9618 in a declarator and we can't have default args. */
9619 parser->default_arg_ok_p = false;
9620 parser->in_declarator_p = true;
9621 }
9622
9623 /* Parse the parameter-declaration-clause. */
9624 params = cp_parser_parameter_declaration_clause (parser);
9625
9626 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 9627 exception-specification. */
62b8a44e
NS
9628 if (cp_parser_parse_definitely (parser))
9629 {
9630 tree cv_qualifiers;
9631 tree exception_specification;
7efa3e22
NS
9632
9633 if (ctor_dtor_or_conv_p)
9634 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
9635 first = false;
9636 /* Consume the `)'. */
9637 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9638
9639 /* Parse the cv-qualifier-seq. */
9640 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9641 /* And the exception-specification. */
9642 exception_specification
9643 = cp_parser_exception_specification_opt (parser);
9644
9645 /* Create the function-declarator. */
9646 declarator = make_call_declarator (declarator,
9647 params,
9648 cv_qualifiers,
9649 exception_specification);
9650 /* Any subsequent parameter lists are to do with
9651 return type, so are not those of the declared
9652 function. */
9653 parser->default_arg_ok_p = false;
9654
9655 /* Repeat the main loop. */
9656 continue;
9657 }
9658 }
9659
9660 /* If this is the first, we can try a parenthesized
9661 declarator. */
9662 if (first)
a723baf1 9663 {
a723baf1 9664 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
9665 parser->in_declarator_p = saved_in_declarator_p;
9666
9667 /* Consume the `('. */
9668 cp_lexer_consume_token (parser->lexer);
9669 /* Parse the nested declarator. */
9670 declarator
9671 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9672 first = false;
9673 /* Expect a `)'. */
9674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9675 declarator = error_mark_node;
9676 if (declarator == error_mark_node)
9677 break;
9678
9679 goto handle_declarator;
a723baf1 9680 }
9bcb9aae 9681 /* Otherwise, we must be done. */
62b8a44e
NS
9682 else
9683 break;
a723baf1 9684 }
62b8a44e
NS
9685 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9686 && token->type == CPP_OPEN_SQUARE)
a723baf1 9687 {
62b8a44e 9688 /* Parse an array-declarator. */
a723baf1
MM
9689 tree bounds;
9690
7efa3e22
NS
9691 if (ctor_dtor_or_conv_p)
9692 *ctor_dtor_or_conv_p = 0;
9693
62b8a44e
NS
9694 first = false;
9695 parser->default_arg_ok_p = false;
9696 parser->in_declarator_p = true;
a723baf1
MM
9697 /* Consume the `['. */
9698 cp_lexer_consume_token (parser->lexer);
9699 /* Peek at the next token. */
9700 token = cp_lexer_peek_token (parser->lexer);
9701 /* If the next token is `]', then there is no
9702 constant-expression. */
9703 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
9704 {
9705 bool non_constant_p;
9706
9707 bounds
9708 = cp_parser_constant_expression (parser,
9709 /*allow_non_constant=*/true,
9710 &non_constant_p);
d17811fd
MM
9711 if (!non_constant_p)
9712 bounds = cp_parser_fold_non_dependent_expr (bounds);
14d22dd6 9713 }
a723baf1
MM
9714 else
9715 bounds = NULL_TREE;
9716 /* Look for the closing `]'. */
62b8a44e
NS
9717 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9718 {
9719 declarator = error_mark_node;
9720 break;
9721 }
a723baf1
MM
9722
9723 declarator = build_nt (ARRAY_REF, declarator, bounds);
9724 }
62b8a44e 9725 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 9726 {
62b8a44e
NS
9727 /* Parse a declarator_id */
9728 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9729 cp_parser_parse_tentatively (parser);
9730 declarator = cp_parser_declarator_id (parser);
712becab
NS
9731 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9732 {
9733 if (!cp_parser_parse_definitely (parser))
9734 declarator = error_mark_node;
9735 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9736 {
9737 cp_parser_error (parser, "expected unqualified-id");
9738 declarator = error_mark_node;
9739 }
9740 }
9741
62b8a44e
NS
9742 if (declarator == error_mark_node)
9743 break;
a723baf1 9744
62b8a44e
NS
9745 if (TREE_CODE (declarator) == SCOPE_REF)
9746 {
9747 tree scope = TREE_OPERAND (declarator, 0);
712becab 9748
62b8a44e
NS
9749 /* In the declaration of a member of a template class
9750 outside of the class itself, the SCOPE will sometimes
9751 be a TYPENAME_TYPE. For example, given:
9752
9753 template <typename T>
9754 int S<T>::R::i = 3;
9755
9756 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9757 this context, we must resolve S<T>::R to an ordinary
9758 type, rather than a typename type.
9759
9760 The reason we normally avoid resolving TYPENAME_TYPEs
9761 is that a specialization of `S' might render
9762 `S<T>::R' not a type. However, if `S' is
9763 specialized, then this `i' will not be used, so there
9764 is no harm in resolving the types here. */
9765 if (TREE_CODE (scope) == TYPENAME_TYPE)
9766 {
14d22dd6
MM
9767 tree type;
9768
62b8a44e 9769 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
9770 type = resolve_typename_type (scope,
9771 /*only_current_p=*/false);
62b8a44e 9772 /* If that failed, the declarator is invalid. */
14d22dd6
MM
9773 if (type != error_mark_node)
9774 scope = type;
62b8a44e
NS
9775 /* Build a new DECLARATOR. */
9776 declarator = build_nt (SCOPE_REF,
9777 scope,
9778 TREE_OPERAND (declarator, 1));
9779 }
9780 }
9781
9782 /* Check to see whether the declarator-id names a constructor,
9783 destructor, or conversion. */
9784 if (declarator && ctor_dtor_or_conv_p
9785 && ((TREE_CODE (declarator) == SCOPE_REF
9786 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9787 || (TREE_CODE (declarator) != SCOPE_REF
9788 && at_class_scope_p ())))
a723baf1 9789 {
62b8a44e
NS
9790 tree unqualified_name;
9791 tree class_type;
9792
9793 /* Get the unqualified part of the name. */
9794 if (TREE_CODE (declarator) == SCOPE_REF)
9795 {
9796 class_type = TREE_OPERAND (declarator, 0);
9797 unqualified_name = TREE_OPERAND (declarator, 1);
9798 }
9799 else
9800 {
9801 class_type = current_class_type;
9802 unqualified_name = declarator;
9803 }
9804
9805 /* See if it names ctor, dtor or conv. */
9806 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9807 || IDENTIFIER_TYPENAME_P (unqualified_name)
9808 || constructor_name_p (unqualified_name, class_type))
7efa3e22 9809 *ctor_dtor_or_conv_p = -1;
a723baf1 9810 }
62b8a44e
NS
9811
9812 handle_declarator:;
9813 scope = get_scope_of_declarator (declarator);
9814 if (scope)
9815 /* Any names that appear after the declarator-id for a member
9816 are looked up in the containing scope. */
9817 push_scope (scope);
9818 parser->in_declarator_p = true;
9819 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9820 || (declarator
9821 && (TREE_CODE (declarator) == SCOPE_REF
9822 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9823 /* Default args are only allowed on function
9824 declarations. */
9825 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 9826 else
62b8a44e
NS
9827 parser->default_arg_ok_p = false;
9828
9829 first = false;
a723baf1 9830 }
62b8a44e 9831 /* We're done. */
a723baf1
MM
9832 else
9833 break;
a723baf1
MM
9834 }
9835
9836 /* For an abstract declarator, we might wind up with nothing at this
9837 point. That's an error; the declarator is not optional. */
9838 if (!declarator)
9839 cp_parser_error (parser, "expected declarator");
9840
9841 /* If we entered a scope, we must exit it now. */
9842 if (scope)
9843 pop_scope (scope);
9844
9845 parser->default_arg_ok_p = saved_default_arg_ok_p;
9846 parser->in_declarator_p = saved_in_declarator_p;
9847
9848 return declarator;
9849}
9850
9851/* Parse a ptr-operator.
9852
9853 ptr-operator:
9854 * cv-qualifier-seq [opt]
9855 &
9856 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9857
9858 GNU Extension:
9859
9860 ptr-operator:
9861 & cv-qualifier-seq [opt]
9862
9863 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9864 used. Returns ADDR_EXPR if a reference was used. In the
9865 case of a pointer-to-member, *TYPE is filled in with the
9866 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9867 with the cv-qualifier-seq, or NULL_TREE, if there are no
9868 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9869
9870static enum tree_code
94edc4ab
NN
9871cp_parser_ptr_operator (cp_parser* parser,
9872 tree* type,
9873 tree* cv_qualifier_seq)
a723baf1
MM
9874{
9875 enum tree_code code = ERROR_MARK;
9876 cp_token *token;
9877
9878 /* Assume that it's not a pointer-to-member. */
9879 *type = NULL_TREE;
9880 /* And that there are no cv-qualifiers. */
9881 *cv_qualifier_seq = NULL_TREE;
9882
9883 /* Peek at the next token. */
9884 token = cp_lexer_peek_token (parser->lexer);
9885 /* If it's a `*' or `&' we have a pointer or reference. */
9886 if (token->type == CPP_MULT || token->type == CPP_AND)
9887 {
9888 /* Remember which ptr-operator we were processing. */
9889 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
9890
9891 /* Consume the `*' or `&'. */
9892 cp_lexer_consume_token (parser->lexer);
9893
9894 /* A `*' can be followed by a cv-qualifier-seq, and so can a
9895 `&', if we are allowing GNU extensions. (The only qualifier
9896 that can legally appear after `&' is `restrict', but that is
9897 enforced during semantic analysis. */
9898 if (code == INDIRECT_REF
9899 || cp_parser_allow_gnu_extensions_p (parser))
9900 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9901 }
9902 else
9903 {
9904 /* Try the pointer-to-member case. */
9905 cp_parser_parse_tentatively (parser);
9906 /* Look for the optional `::' operator. */
9907 cp_parser_global_scope_opt (parser,
9908 /*current_scope_valid_p=*/false);
9909 /* Look for the nested-name specifier. */
9910 cp_parser_nested_name_specifier (parser,
9911 /*typename_keyword_p=*/false,
9912 /*check_dependency_p=*/true,
9913 /*type_p=*/false);
9914 /* If we found it, and the next token is a `*', then we are
9915 indeed looking at a pointer-to-member operator. */
9916 if (!cp_parser_error_occurred (parser)
9917 && cp_parser_require (parser, CPP_MULT, "`*'"))
9918 {
9919 /* The type of which the member is a member is given by the
9920 current SCOPE. */
9921 *type = parser->scope;
9922 /* The next name will not be qualified. */
9923 parser->scope = NULL_TREE;
9924 parser->qualifying_scope = NULL_TREE;
9925 parser->object_scope = NULL_TREE;
9926 /* Indicate that the `*' operator was used. */
9927 code = INDIRECT_REF;
9928 /* Look for the optional cv-qualifier-seq. */
9929 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
9930 }
9931 /* If that didn't work we don't have a ptr-operator. */
9932 if (!cp_parser_parse_definitely (parser))
9933 cp_parser_error (parser, "expected ptr-operator");
9934 }
9935
9936 return code;
9937}
9938
9939/* Parse an (optional) cv-qualifier-seq.
9940
9941 cv-qualifier-seq:
9942 cv-qualifier cv-qualifier-seq [opt]
9943
9944 Returns a TREE_LIST. The TREE_VALUE of each node is the
9945 representation of a cv-qualifier. */
9946
9947static tree
94edc4ab 9948cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
9949{
9950 tree cv_qualifiers = NULL_TREE;
9951
9952 while (true)
9953 {
9954 tree cv_qualifier;
9955
9956 /* Look for the next cv-qualifier. */
9957 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
9958 /* If we didn't find one, we're done. */
9959 if (!cv_qualifier)
9960 break;
9961
9962 /* Add this cv-qualifier to the list. */
9963 cv_qualifiers
9964 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
9965 }
9966
9967 /* We built up the list in reverse order. */
9968 return nreverse (cv_qualifiers);
9969}
9970
9971/* Parse an (optional) cv-qualifier.
9972
9973 cv-qualifier:
9974 const
9975 volatile
9976
9977 GNU Extension:
9978
9979 cv-qualifier:
9980 __restrict__ */
9981
9982static tree
94edc4ab 9983cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
9984{
9985 cp_token *token;
9986 tree cv_qualifier = NULL_TREE;
9987
9988 /* Peek at the next token. */
9989 token = cp_lexer_peek_token (parser->lexer);
9990 /* See if it's a cv-qualifier. */
9991 switch (token->keyword)
9992 {
9993 case RID_CONST:
9994 case RID_VOLATILE:
9995 case RID_RESTRICT:
9996 /* Save the value of the token. */
9997 cv_qualifier = token->value;
9998 /* Consume the token. */
9999 cp_lexer_consume_token (parser->lexer);
10000 break;
10001
10002 default:
10003 break;
10004 }
10005
10006 return cv_qualifier;
10007}
10008
10009/* Parse a declarator-id.
10010
10011 declarator-id:
10012 id-expression
10013 :: [opt] nested-name-specifier [opt] type-name
10014
10015 In the `id-expression' case, the value returned is as for
10016 cp_parser_id_expression if the id-expression was an unqualified-id.
10017 If the id-expression was a qualified-id, then a SCOPE_REF is
10018 returned. The first operand is the scope (either a NAMESPACE_DECL
10019 or TREE_TYPE), but the second is still just a representation of an
10020 unqualified-id. */
10021
10022static tree
94edc4ab 10023cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10024{
10025 tree id_expression;
10026
10027 /* The expression must be an id-expression. Assume that qualified
10028 names are the names of types so that:
10029
10030 template <class T>
10031 int S<T>::R::i = 3;
10032
10033 will work; we must treat `S<T>::R' as the name of a type.
10034 Similarly, assume that qualified names are templates, where
10035 required, so that:
10036
10037 template <class T>
10038 int S<T>::R<T>::i = 3;
10039
10040 will work, too. */
10041 id_expression = cp_parser_id_expression (parser,
10042 /*template_keyword_p=*/false,
10043 /*check_dependency_p=*/false,
10044 /*template_p=*/NULL);
10045 /* If the name was qualified, create a SCOPE_REF to represent
10046 that. */
10047 if (parser->scope)
ec20aa6c
MM
10048 {
10049 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10050 parser->scope = NULL_TREE;
10051 }
a723baf1
MM
10052
10053 return id_expression;
10054}
10055
10056/* Parse a type-id.
10057
10058 type-id:
10059 type-specifier-seq abstract-declarator [opt]
10060
10061 Returns the TYPE specified. */
10062
10063static tree
94edc4ab 10064cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10065{
10066 tree type_specifier_seq;
10067 tree abstract_declarator;
10068
10069 /* Parse the type-specifier-seq. */
10070 type_specifier_seq
10071 = cp_parser_type_specifier_seq (parser);
10072 if (type_specifier_seq == error_mark_node)
10073 return error_mark_node;
10074
10075 /* There might or might not be an abstract declarator. */
10076 cp_parser_parse_tentatively (parser);
10077 /* Look for the declarator. */
10078 abstract_declarator
62b8a44e 10079 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10080 /* Check to see if there really was a declarator. */
10081 if (!cp_parser_parse_definitely (parser))
10082 abstract_declarator = NULL_TREE;
10083
10084 return groktypename (build_tree_list (type_specifier_seq,
10085 abstract_declarator));
10086}
10087
10088/* Parse a type-specifier-seq.
10089
10090 type-specifier-seq:
10091 type-specifier type-specifier-seq [opt]
10092
10093 GNU extension:
10094
10095 type-specifier-seq:
10096 attributes type-specifier-seq [opt]
10097
10098 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10099 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10100
10101static tree
94edc4ab 10102cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10103{
10104 bool seen_type_specifier = false;
10105 tree type_specifier_seq = NULL_TREE;
10106
10107 /* Parse the type-specifiers and attributes. */
10108 while (true)
10109 {
10110 tree type_specifier;
10111
10112 /* Check for attributes first. */
10113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10114 {
10115 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10116 NULL_TREE,
10117 type_specifier_seq);
10118 continue;
10119 }
10120
10121 /* After the first type-specifier, others are optional. */
10122 if (seen_type_specifier)
10123 cp_parser_parse_tentatively (parser);
10124 /* Look for the type-specifier. */
10125 type_specifier = cp_parser_type_specifier (parser,
10126 CP_PARSER_FLAGS_NONE,
10127 /*is_friend=*/false,
10128 /*is_declaration=*/false,
10129 NULL,
10130 NULL);
10131 /* If the first type-specifier could not be found, this is not a
10132 type-specifier-seq at all. */
10133 if (!seen_type_specifier && type_specifier == error_mark_node)
10134 return error_mark_node;
10135 /* If subsequent type-specifiers could not be found, the
10136 type-specifier-seq is complete. */
10137 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10138 break;
10139
10140 /* Add the new type-specifier to the list. */
10141 type_specifier_seq
10142 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10143 seen_type_specifier = true;
10144 }
10145
10146 /* We built up the list in reverse order. */
10147 return nreverse (type_specifier_seq);
10148}
10149
10150/* Parse a parameter-declaration-clause.
10151
10152 parameter-declaration-clause:
10153 parameter-declaration-list [opt] ... [opt]
10154 parameter-declaration-list , ...
10155
10156 Returns a representation for the parameter declarations. Each node
10157 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10158 representation.) If the parameter-declaration-clause ends with an
10159 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10160 list. A return value of NULL_TREE indicates a
10161 parameter-declaration-clause consisting only of an ellipsis. */
10162
10163static tree
94edc4ab 10164cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10165{
10166 tree parameters;
10167 cp_token *token;
10168 bool ellipsis_p;
10169
10170 /* Peek at the next token. */
10171 token = cp_lexer_peek_token (parser->lexer);
10172 /* Check for trivial parameter-declaration-clauses. */
10173 if (token->type == CPP_ELLIPSIS)
10174 {
10175 /* Consume the `...' token. */
10176 cp_lexer_consume_token (parser->lexer);
10177 return NULL_TREE;
10178 }
10179 else if (token->type == CPP_CLOSE_PAREN)
10180 /* There are no parameters. */
c73aecdf
DE
10181 {
10182#ifndef NO_IMPLICIT_EXTERN_C
10183 if (in_system_header && current_class_type == NULL
10184 && current_lang_name == lang_name_c)
10185 return NULL_TREE;
10186 else
10187#endif
10188 return void_list_node;
10189 }
a723baf1
MM
10190 /* Check for `(void)', too, which is a special case. */
10191 else if (token->keyword == RID_VOID
10192 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10193 == CPP_CLOSE_PAREN))
10194 {
10195 /* Consume the `void' token. */
10196 cp_lexer_consume_token (parser->lexer);
10197 /* There are no parameters. */
10198 return void_list_node;
10199 }
10200
10201 /* Parse the parameter-declaration-list. */
10202 parameters = cp_parser_parameter_declaration_list (parser);
10203 /* If a parse error occurred while parsing the
10204 parameter-declaration-list, then the entire
10205 parameter-declaration-clause is erroneous. */
10206 if (parameters == error_mark_node)
10207 return error_mark_node;
10208
10209 /* Peek at the next token. */
10210 token = cp_lexer_peek_token (parser->lexer);
10211 /* If it's a `,', the clause should terminate with an ellipsis. */
10212 if (token->type == CPP_COMMA)
10213 {
10214 /* Consume the `,'. */
10215 cp_lexer_consume_token (parser->lexer);
10216 /* Expect an ellipsis. */
10217 ellipsis_p
10218 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10219 }
10220 /* It might also be `...' if the optional trailing `,' was
10221 omitted. */
10222 else if (token->type == CPP_ELLIPSIS)
10223 {
10224 /* Consume the `...' token. */
10225 cp_lexer_consume_token (parser->lexer);
10226 /* And remember that we saw it. */
10227 ellipsis_p = true;
10228 }
10229 else
10230 ellipsis_p = false;
10231
10232 /* Finish the parameter list. */
10233 return finish_parmlist (parameters, ellipsis_p);
10234}
10235
10236/* Parse a parameter-declaration-list.
10237
10238 parameter-declaration-list:
10239 parameter-declaration
10240 parameter-declaration-list , parameter-declaration
10241
10242 Returns a representation of the parameter-declaration-list, as for
10243 cp_parser_parameter_declaration_clause. However, the
10244 `void_list_node' is never appended to the list. */
10245
10246static tree
94edc4ab 10247cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10248{
10249 tree parameters = NULL_TREE;
10250
10251 /* Look for more parameters. */
10252 while (true)
10253 {
10254 tree parameter;
10255 /* Parse the parameter. */
10256 parameter
ec194454
MM
10257 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10258
34cd5ae7 10259 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10260 then the entire parameter-declaration-list is erroneous. */
10261 if (parameter == error_mark_node)
10262 {
10263 parameters = error_mark_node;
10264 break;
10265 }
10266 /* Add the new parameter to the list. */
10267 TREE_CHAIN (parameter) = parameters;
10268 parameters = parameter;
10269
10270 /* Peek at the next token. */
10271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10272 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10273 /* The parameter-declaration-list is complete. */
10274 break;
10275 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10276 {
10277 cp_token *token;
10278
10279 /* Peek at the next token. */
10280 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10281 /* If it's an ellipsis, then the list is complete. */
10282 if (token->type == CPP_ELLIPSIS)
10283 break;
10284 /* Otherwise, there must be more parameters. Consume the
10285 `,'. */
10286 cp_lexer_consume_token (parser->lexer);
10287 }
10288 else
10289 {
10290 cp_parser_error (parser, "expected `,' or `...'");
10291 break;
10292 }
10293 }
10294
10295 /* We built up the list in reverse order; straighten it out now. */
10296 return nreverse (parameters);
10297}
10298
10299/* Parse a parameter declaration.
10300
10301 parameter-declaration:
10302 decl-specifier-seq declarator
10303 decl-specifier-seq declarator = assignment-expression
10304 decl-specifier-seq abstract-declarator [opt]
10305 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10306
ec194454
MM
10307 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10308 declares a template parameter. (In that case, a non-nested `>'
10309 token encountered during the parsing of the assignment-expression
10310 is not interpreted as a greater-than operator.)
a723baf1
MM
10311
10312 Returns a TREE_LIST representing the parameter-declaration. The
10313 TREE_VALUE is a representation of the decl-specifier-seq and
10314 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10315 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10316 TREE_VALUE represents the declarator. */
10317
10318static tree
ec194454
MM
10319cp_parser_parameter_declaration (cp_parser *parser,
10320 bool template_parm_p)
a723baf1
MM
10321{
10322 bool declares_class_or_enum;
ec194454 10323 bool greater_than_is_operator_p;
a723baf1
MM
10324 tree decl_specifiers;
10325 tree attributes;
10326 tree declarator;
10327 tree default_argument;
10328 tree parameter;
10329 cp_token *token;
10330 const char *saved_message;
10331
ec194454
MM
10332 /* In a template parameter, `>' is not an operator.
10333
10334 [temp.param]
10335
10336 When parsing a default template-argument for a non-type
10337 template-parameter, the first non-nested `>' is taken as the end
10338 of the template parameter-list rather than a greater-than
10339 operator. */
10340 greater_than_is_operator_p = !template_parm_p;
10341
a723baf1
MM
10342 /* Type definitions may not appear in parameter types. */
10343 saved_message = parser->type_definition_forbidden_message;
10344 parser->type_definition_forbidden_message
10345 = "types may not be defined in parameter types";
10346
10347 /* Parse the declaration-specifiers. */
10348 decl_specifiers
10349 = cp_parser_decl_specifier_seq (parser,
10350 CP_PARSER_FLAGS_NONE,
10351 &attributes,
10352 &declares_class_or_enum);
10353 /* If an error occurred, there's no reason to attempt to parse the
10354 rest of the declaration. */
10355 if (cp_parser_error_occurred (parser))
10356 {
10357 parser->type_definition_forbidden_message = saved_message;
10358 return error_mark_node;
10359 }
10360
10361 /* Peek at the next token. */
10362 token = cp_lexer_peek_token (parser->lexer);
10363 /* If the next token is a `)', `,', `=', `>', or `...', then there
10364 is no declarator. */
10365 if (token->type == CPP_CLOSE_PAREN
10366 || token->type == CPP_COMMA
10367 || token->type == CPP_EQ
10368 || token->type == CPP_ELLIPSIS
10369 || token->type == CPP_GREATER)
10370 declarator = NULL_TREE;
10371 /* Otherwise, there should be a declarator. */
10372 else
10373 {
10374 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10375 parser->default_arg_ok_p = false;
10376
a723baf1 10377 declarator = cp_parser_declarator (parser,
62b8a44e 10378 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10379 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10380 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10381 /* After the declarator, allow more attributes. */
10382 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10383 }
10384
62b8a44e 10385 /* The restriction on defining new types applies only to the type
a723baf1
MM
10386 of the parameter, not to the default argument. */
10387 parser->type_definition_forbidden_message = saved_message;
10388
10389 /* If the next token is `=', then process a default argument. */
10390 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10391 {
10392 bool saved_greater_than_is_operator_p;
10393 /* Consume the `='. */
10394 cp_lexer_consume_token (parser->lexer);
10395
10396 /* If we are defining a class, then the tokens that make up the
10397 default argument must be saved and processed later. */
ec194454
MM
10398 if (!template_parm_p && at_class_scope_p ()
10399 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10400 {
10401 unsigned depth = 0;
10402
10403 /* Create a DEFAULT_ARG to represented the unparsed default
10404 argument. */
10405 default_argument = make_node (DEFAULT_ARG);
10406 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10407
10408 /* Add tokens until we have processed the entire default
10409 argument. */
10410 while (true)
10411 {
10412 bool done = false;
10413 cp_token *token;
10414
10415 /* Peek at the next token. */
10416 token = cp_lexer_peek_token (parser->lexer);
10417 /* What we do depends on what token we have. */
10418 switch (token->type)
10419 {
10420 /* In valid code, a default argument must be
10421 immediately followed by a `,' `)', or `...'. */
10422 case CPP_COMMA:
10423 case CPP_CLOSE_PAREN:
10424 case CPP_ELLIPSIS:
10425 /* If we run into a non-nested `;', `}', or `]',
10426 then the code is invalid -- but the default
10427 argument is certainly over. */
10428 case CPP_SEMICOLON:
10429 case CPP_CLOSE_BRACE:
10430 case CPP_CLOSE_SQUARE:
10431 if (depth == 0)
10432 done = true;
10433 /* Update DEPTH, if necessary. */
10434 else if (token->type == CPP_CLOSE_PAREN
10435 || token->type == CPP_CLOSE_BRACE
10436 || token->type == CPP_CLOSE_SQUARE)
10437 --depth;
10438 break;
10439
10440 case CPP_OPEN_PAREN:
10441 case CPP_OPEN_SQUARE:
10442 case CPP_OPEN_BRACE:
10443 ++depth;
10444 break;
10445
10446 case CPP_GREATER:
10447 /* If we see a non-nested `>', and `>' is not an
10448 operator, then it marks the end of the default
10449 argument. */
10450 if (!depth && !greater_than_is_operator_p)
10451 done = true;
10452 break;
10453
10454 /* If we run out of tokens, issue an error message. */
10455 case CPP_EOF:
10456 error ("file ends in default argument");
10457 done = true;
10458 break;
10459
10460 case CPP_NAME:
10461 case CPP_SCOPE:
10462 /* In these cases, we should look for template-ids.
10463 For example, if the default argument is
10464 `X<int, double>()', we need to do name lookup to
10465 figure out whether or not `X' is a template; if
34cd5ae7 10466 so, the `,' does not end the default argument.
a723baf1
MM
10467
10468 That is not yet done. */
10469 break;
10470
10471 default:
10472 break;
10473 }
10474
10475 /* If we've reached the end, stop. */
10476 if (done)
10477 break;
10478
10479 /* Add the token to the token block. */
10480 token = cp_lexer_consume_token (parser->lexer);
10481 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10482 token);
10483 }
10484 }
10485 /* Outside of a class definition, we can just parse the
10486 assignment-expression. */
10487 else
10488 {
10489 bool saved_local_variables_forbidden_p;
10490
10491 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10492 set correctly. */
10493 saved_greater_than_is_operator_p
10494 = parser->greater_than_is_operator_p;
10495 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10496 /* Local variable names (and the `this' keyword) may not
10497 appear in a default argument. */
10498 saved_local_variables_forbidden_p
10499 = parser->local_variables_forbidden_p;
10500 parser->local_variables_forbidden_p = true;
10501 /* Parse the assignment-expression. */
10502 default_argument = cp_parser_assignment_expression (parser);
10503 /* Restore saved state. */
10504 parser->greater_than_is_operator_p
10505 = saved_greater_than_is_operator_p;
10506 parser->local_variables_forbidden_p
10507 = saved_local_variables_forbidden_p;
10508 }
10509 if (!parser->default_arg_ok_p)
10510 {
10511 pedwarn ("default arguments are only permitted on functions");
10512 if (flag_pedantic_errors)
10513 default_argument = NULL_TREE;
10514 }
10515 }
10516 else
10517 default_argument = NULL_TREE;
10518
10519 /* Create the representation of the parameter. */
10520 if (attributes)
10521 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10522 parameter = build_tree_list (default_argument,
10523 build_tree_list (decl_specifiers,
10524 declarator));
10525
10526 return parameter;
10527}
10528
10529/* Parse a function-definition.
10530
10531 function-definition:
10532 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10533 function-body
10534 decl-specifier-seq [opt] declarator function-try-block
10535
10536 GNU Extension:
10537
10538 function-definition:
10539 __extension__ function-definition
10540
10541 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10542 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10543 be a `friend'. */
10544
10545static tree
94edc4ab 10546cp_parser_function_definition (cp_parser* parser, bool* friend_p)
a723baf1
MM
10547{
10548 tree decl_specifiers;
10549 tree attributes;
10550 tree declarator;
10551 tree fn;
a723baf1
MM
10552 cp_token *token;
10553 bool declares_class_or_enum;
10554 bool member_p;
10555 /* The saved value of the PEDANTIC flag. */
10556 int saved_pedantic;
10557
10558 /* Any pending qualification must be cleared by our caller. It is
10559 more robust to force the callers to clear PARSER->SCOPE than to
10560 do it here since if the qualification is in effect here, it might
10561 also end up in effect elsewhere that it is not intended. */
10562 my_friendly_assert (!parser->scope, 20010821);
10563
10564 /* Handle `__extension__'. */
10565 if (cp_parser_extension_opt (parser, &saved_pedantic))
10566 {
10567 /* Parse the function-definition. */
10568 fn = cp_parser_function_definition (parser, friend_p);
10569 /* Restore the PEDANTIC flag. */
10570 pedantic = saved_pedantic;
10571
10572 return fn;
10573 }
10574
10575 /* Check to see if this definition appears in a class-specifier. */
10576 member_p = (at_class_scope_p ()
10577 && TYPE_BEING_DEFINED (current_class_type));
10578 /* Defer access checks in the decl-specifier-seq until we know what
10579 function is being defined. There is no need to do this for the
10580 definition of member functions; we cannot be defining a member
10581 from another class. */
8d241e0b 10582 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
cf22909c 10583
a723baf1
MM
10584 /* Parse the decl-specifier-seq. */
10585 decl_specifiers
10586 = cp_parser_decl_specifier_seq (parser,
10587 CP_PARSER_FLAGS_OPTIONAL,
10588 &attributes,
10589 &declares_class_or_enum);
10590 /* Figure out whether this declaration is a `friend'. */
10591 if (friend_p)
10592 *friend_p = cp_parser_friend_p (decl_specifiers);
10593
10594 /* Parse the declarator. */
62b8a44e 10595 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
10596 /*ctor_dtor_or_conv_p=*/NULL);
10597
10598 /* Gather up any access checks that occurred. */
cf22909c 10599 stop_deferring_access_checks ();
a723baf1
MM
10600
10601 /* If something has already gone wrong, we may as well stop now. */
10602 if (declarator == error_mark_node)
10603 {
10604 /* Skip to the end of the function, or if this wasn't anything
10605 like a function-definition, to a `;' in the hopes of finding
10606 a sensible place from which to continue parsing. */
10607 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10608 pop_deferring_access_checks ();
a723baf1
MM
10609 return error_mark_node;
10610 }
10611
10612 /* The next character should be a `{' (for a simple function
10613 definition), a `:' (for a ctor-initializer), or `try' (for a
10614 function-try block). */
10615 token = cp_lexer_peek_token (parser->lexer);
10616 if (!cp_parser_token_starts_function_definition_p (token))
10617 {
10618 /* Issue the error-message. */
10619 cp_parser_error (parser, "expected function-definition");
10620 /* Skip to the next `;'. */
10621 cp_parser_skip_to_end_of_block_or_statement (parser);
10622
cf22909c 10623 pop_deferring_access_checks ();
a723baf1
MM
10624 return error_mark_node;
10625 }
10626
10627 /* If we are in a class scope, then we must handle
10628 function-definitions specially. In particular, we save away the
10629 tokens that make up the function body, and parse them again
10630 later, in order to handle code like:
10631
10632 struct S {
10633 int f () { return i; }
10634 int i;
10635 };
10636
10637 Here, we cannot parse the body of `f' until after we have seen
10638 the declaration of `i'. */
10639 if (member_p)
10640 {
10641 cp_token_cache *cache;
10642
10643 /* Create the function-declaration. */
10644 fn = start_method (decl_specifiers, declarator, attributes);
10645 /* If something went badly wrong, bail out now. */
10646 if (fn == error_mark_node)
10647 {
10648 /* If there's a function-body, skip it. */
10649 if (cp_parser_token_starts_function_definition_p
10650 (cp_lexer_peek_token (parser->lexer)))
10651 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10652 pop_deferring_access_checks ();
a723baf1
MM
10653 return error_mark_node;
10654 }
10655
8db1028e
NS
10656 /* Remember it, if there default args to post process. */
10657 cp_parser_save_default_args (parser, fn);
10658
a723baf1
MM
10659 /* Create a token cache. */
10660 cache = cp_token_cache_new ();
10661 /* Save away the tokens that make up the body of the
10662 function. */
10663 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10664 /* Handle function try blocks. */
10665 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10666 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10667
10668 /* Save away the inline definition; we will process it when the
10669 class is complete. */
10670 DECL_PENDING_INLINE_INFO (fn) = cache;
10671 DECL_PENDING_INLINE_P (fn) = 1;
10672
649fc72d
NS
10673 /* We need to know that this was defined in the class, so that
10674 friend templates are handled correctly. */
10675 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10676
a723baf1
MM
10677 /* We're done with the inline definition. */
10678 finish_method (fn);
10679
10680 /* Add FN to the queue of functions to be parsed later. */
10681 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 10682 = tree_cons (NULL_TREE, fn,
a723baf1
MM
10683 TREE_VALUE (parser->unparsed_functions_queues));
10684
cf22909c 10685 pop_deferring_access_checks ();
a723baf1
MM
10686 return fn;
10687 }
10688
10689 /* Check that the number of template-parameter-lists is OK. */
10690 if (!cp_parser_check_declarator_template_parameters (parser,
10691 declarator))
10692 {
10693 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10694 pop_deferring_access_checks ();
a723baf1
MM
10695 return error_mark_node;
10696 }
10697
cf22909c
KL
10698 fn = cp_parser_function_definition_from_specifiers_and_declarator
10699 (parser, decl_specifiers, attributes, declarator);
10700 pop_deferring_access_checks ();
10701 return fn;
a723baf1
MM
10702}
10703
10704/* Parse a function-body.
10705
10706 function-body:
10707 compound_statement */
10708
10709static void
10710cp_parser_function_body (cp_parser *parser)
10711{
10712 cp_parser_compound_statement (parser);
10713}
10714
10715/* Parse a ctor-initializer-opt followed by a function-body. Return
10716 true if a ctor-initializer was present. */
10717
10718static bool
10719cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10720{
10721 tree body;
10722 bool ctor_initializer_p;
10723
10724 /* Begin the function body. */
10725 body = begin_function_body ();
10726 /* Parse the optional ctor-initializer. */
10727 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10728 /* Parse the function-body. */
10729 cp_parser_function_body (parser);
10730 /* Finish the function body. */
10731 finish_function_body (body);
10732
10733 return ctor_initializer_p;
10734}
10735
10736/* Parse an initializer.
10737
10738 initializer:
10739 = initializer-clause
10740 ( expression-list )
10741
10742 Returns a expression representing the initializer. If no
10743 initializer is present, NULL_TREE is returned.
10744
10745 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10746 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
10747 set to FALSE if there is no initializer present. If there is an
10748 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10749 is set to true; otherwise it is set to false. */
a723baf1
MM
10750
10751static tree
39703eb9
MM
10752cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10753 bool* non_constant_p)
a723baf1
MM
10754{
10755 cp_token *token;
10756 tree init;
10757
10758 /* Peek at the next token. */
10759 token = cp_lexer_peek_token (parser->lexer);
10760
10761 /* Let our caller know whether or not this initializer was
10762 parenthesized. */
10763 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
10764 /* Assume that the initializer is constant. */
10765 *non_constant_p = false;
a723baf1
MM
10766
10767 if (token->type == CPP_EQ)
10768 {
10769 /* Consume the `='. */
10770 cp_lexer_consume_token (parser->lexer);
10771 /* Parse the initializer-clause. */
39703eb9 10772 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
10773 }
10774 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
10775 init = cp_parser_parenthesized_expression_list (parser, false,
10776 non_constant_p);
a723baf1
MM
10777 else
10778 {
10779 /* Anything else is an error. */
10780 cp_parser_error (parser, "expected initializer");
10781 init = error_mark_node;
10782 }
10783
10784 return init;
10785}
10786
10787/* Parse an initializer-clause.
10788
10789 initializer-clause:
10790 assignment-expression
10791 { initializer-list , [opt] }
10792 { }
10793
10794 Returns an expression representing the initializer.
10795
10796 If the `assignment-expression' production is used the value
34cd5ae7 10797 returned is simply a representation for the expression.
a723baf1
MM
10798
10799 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10800 the elements of the initializer-list (or NULL_TREE, if the last
10801 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10802 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
10803 trailing `,' was provided. NON_CONSTANT_P is as for
10804 cp_parser_initializer. */
a723baf1
MM
10805
10806static tree
39703eb9 10807cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
10808{
10809 tree initializer;
10810
10811 /* If it is not a `{', then we are looking at an
10812 assignment-expression. */
10813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
10814 initializer
10815 = cp_parser_constant_expression (parser,
10816 /*allow_non_constant_p=*/true,
10817 non_constant_p);
a723baf1
MM
10818 else
10819 {
10820 /* Consume the `{' token. */
10821 cp_lexer_consume_token (parser->lexer);
10822 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10823 initializer = make_node (CONSTRUCTOR);
10824 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10825 necessary, but check_initializer depends upon it, for
10826 now. */
10827 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10828 /* If it's not a `}', then there is a non-trivial initializer. */
10829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10830 {
10831 /* Parse the initializer list. */
10832 CONSTRUCTOR_ELTS (initializer)
39703eb9 10833 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
10834 /* A trailing `,' token is allowed. */
10835 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10836 cp_lexer_consume_token (parser->lexer);
10837 }
a723baf1
MM
10838 /* Now, there should be a trailing `}'. */
10839 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10840 }
10841
10842 return initializer;
10843}
10844
10845/* Parse an initializer-list.
10846
10847 initializer-list:
10848 initializer-clause
10849 initializer-list , initializer-clause
10850
10851 GNU Extension:
10852
10853 initializer-list:
10854 identifier : initializer-clause
10855 initializer-list, identifier : initializer-clause
10856
10857 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10858 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
10859 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10860 as for cp_parser_initializer. */
a723baf1
MM
10861
10862static tree
39703eb9 10863cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
10864{
10865 tree initializers = NULL_TREE;
10866
39703eb9
MM
10867 /* Assume all of the expressions are constant. */
10868 *non_constant_p = false;
10869
a723baf1
MM
10870 /* Parse the rest of the list. */
10871 while (true)
10872 {
10873 cp_token *token;
10874 tree identifier;
10875 tree initializer;
39703eb9
MM
10876 bool clause_non_constant_p;
10877
a723baf1
MM
10878 /* If the next token is an identifier and the following one is a
10879 colon, we are looking at the GNU designated-initializer
10880 syntax. */
10881 if (cp_parser_allow_gnu_extensions_p (parser)
10882 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10883 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
10884 {
10885 /* Consume the identifier. */
10886 identifier = cp_lexer_consume_token (parser->lexer)->value;
10887 /* Consume the `:'. */
10888 cp_lexer_consume_token (parser->lexer);
10889 }
10890 else
10891 identifier = NULL_TREE;
10892
10893 /* Parse the initializer. */
39703eb9
MM
10894 initializer = cp_parser_initializer_clause (parser,
10895 &clause_non_constant_p);
10896 /* If any clause is non-constant, so is the entire initializer. */
10897 if (clause_non_constant_p)
10898 *non_constant_p = true;
a723baf1
MM
10899 /* Add it to the list. */
10900 initializers = tree_cons (identifier, initializer, initializers);
10901
10902 /* If the next token is not a comma, we have reached the end of
10903 the list. */
10904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10905 break;
10906
10907 /* Peek at the next token. */
10908 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10909 /* If the next token is a `}', then we're still done. An
10910 initializer-clause can have a trailing `,' after the
10911 initializer-list and before the closing `}'. */
10912 if (token->type == CPP_CLOSE_BRACE)
10913 break;
10914
10915 /* Consume the `,' token. */
10916 cp_lexer_consume_token (parser->lexer);
10917 }
10918
10919 /* The initializers were built up in reverse order, so we need to
10920 reverse them now. */
10921 return nreverse (initializers);
10922}
10923
10924/* Classes [gram.class] */
10925
10926/* Parse a class-name.
10927
10928 class-name:
10929 identifier
10930 template-id
10931
10932 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
10933 to indicate that names looked up in dependent types should be
10934 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
10935 keyword has been used to indicate that the name that appears next
10936 is a template. TYPE_P is true iff the next name should be treated
10937 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
10938 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10939 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
10940 being defined in a class-head.
a723baf1
MM
10941
10942 Returns the TYPE_DECL representing the class. */
10943
10944static tree
10945cp_parser_class_name (cp_parser *parser,
10946 bool typename_keyword_p,
10947 bool template_keyword_p,
10948 bool type_p,
a723baf1
MM
10949 bool check_dependency_p,
10950 bool class_head_p)
10951{
10952 tree decl;
10953 tree scope;
10954 bool typename_p;
e5976695
MM
10955 cp_token *token;
10956
10957 /* All class-names start with an identifier. */
10958 token = cp_lexer_peek_token (parser->lexer);
10959 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
10960 {
10961 cp_parser_error (parser, "expected class-name");
10962 return error_mark_node;
10963 }
10964
a723baf1
MM
10965 /* PARSER->SCOPE can be cleared when parsing the template-arguments
10966 to a template-id, so we save it here. */
10967 scope = parser->scope;
10968 /* Any name names a type if we're following the `typename' keyword
10969 in a qualified name where the enclosing scope is type-dependent. */
10970 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 10971 && dependent_type_p (scope));
e5976695
MM
10972 /* Handle the common case (an identifier, but not a template-id)
10973 efficiently. */
10974 if (token->type == CPP_NAME
10975 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 10976 {
a723baf1
MM
10977 tree identifier;
10978
10979 /* Look for the identifier. */
10980 identifier = cp_parser_identifier (parser);
10981 /* If the next token isn't an identifier, we are certainly not
10982 looking at a class-name. */
10983 if (identifier == error_mark_node)
10984 decl = error_mark_node;
10985 /* If we know this is a type-name, there's no need to look it
10986 up. */
10987 else if (typename_p)
10988 decl = identifier;
10989 else
10990 {
10991 /* If the next token is a `::', then the name must be a type
10992 name.
10993
10994 [basic.lookup.qual]
10995
10996 During the lookup for a name preceding the :: scope
10997 resolution operator, object, function, and enumerator
10998 names are ignored. */
10999 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11000 type_p = true;
11001 /* Look up the name. */
11002 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11003 type_p,
eea9800f 11004 /*is_namespace=*/false,
a723baf1
MM
11005 check_dependency_p);
11006 }
11007 }
e5976695
MM
11008 else
11009 {
11010 /* Try a template-id. */
11011 decl = cp_parser_template_id (parser, template_keyword_p,
11012 check_dependency_p);
11013 if (decl == error_mark_node)
11014 return error_mark_node;
11015 }
a723baf1
MM
11016
11017 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11018
11019 /* If this is a typename, create a TYPENAME_TYPE. */
11020 if (typename_p && decl != error_mark_node)
11021 decl = TYPE_NAME (make_typename_type (scope, decl,
11022 /*complain=*/1));
11023
11024 /* Check to see that it is really the name of a class. */
11025 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11026 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11027 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11028 /* Situations like this:
11029
11030 template <typename T> struct A {
11031 typename T::template X<int>::I i;
11032 };
11033
11034 are problematic. Is `T::template X<int>' a class-name? The
11035 standard does not seem to be definitive, but there is no other
11036 valid interpretation of the following `::'. Therefore, those
11037 names are considered class-names. */
78757caa 11038 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11039 else if (decl == error_mark_node
11040 || TREE_CODE (decl) != TYPE_DECL
11041 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11042 {
11043 cp_parser_error (parser, "expected class-name");
11044 return error_mark_node;
11045 }
11046
11047 return decl;
11048}
11049
11050/* Parse a class-specifier.
11051
11052 class-specifier:
11053 class-head { member-specification [opt] }
11054
11055 Returns the TREE_TYPE representing the class. */
11056
11057static tree
94edc4ab 11058cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11059{
11060 cp_token *token;
11061 tree type;
11062 tree attributes = NULL_TREE;
11063 int has_trailing_semicolon;
11064 bool nested_name_specifier_p;
a723baf1
MM
11065 unsigned saved_num_template_parameter_lists;
11066
8d241e0b 11067 push_deferring_access_checks (dk_no_deferred);
cf22909c 11068
a723baf1
MM
11069 /* Parse the class-head. */
11070 type = cp_parser_class_head (parser,
cf22909c 11071 &nested_name_specifier_p);
a723baf1
MM
11072 /* If the class-head was a semantic disaster, skip the entire body
11073 of the class. */
11074 if (!type)
11075 {
11076 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11077 pop_deferring_access_checks ();
a723baf1
MM
11078 return error_mark_node;
11079 }
cf22909c 11080
a723baf1
MM
11081 /* Look for the `{'. */
11082 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11083 {
11084 pop_deferring_access_checks ();
11085 return error_mark_node;
11086 }
11087
a723baf1
MM
11088 /* Issue an error message if type-definitions are forbidden here. */
11089 cp_parser_check_type_definition (parser);
11090 /* Remember that we are defining one more class. */
11091 ++parser->num_classes_being_defined;
11092 /* Inside the class, surrounding template-parameter-lists do not
11093 apply. */
11094 saved_num_template_parameter_lists
11095 = parser->num_template_parameter_lists;
11096 parser->num_template_parameter_lists = 0;
78757caa 11097
a723baf1
MM
11098 /* Start the class. */
11099 type = begin_class_definition (type);
11100 if (type == error_mark_node)
9bcb9aae 11101 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11102 cp_parser_skip_to_closing_brace (parser);
11103 else
11104 /* Parse the member-specification. */
11105 cp_parser_member_specification_opt (parser);
11106 /* Look for the trailing `}'. */
11107 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11108 /* We get better error messages by noticing a common problem: a
11109 missing trailing `;'. */
11110 token = cp_lexer_peek_token (parser->lexer);
11111 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11112 /* Look for attributes to apply to this class. */
11113 if (cp_parser_allow_gnu_extensions_p (parser))
11114 attributes = cp_parser_attributes_opt (parser);
11115 /* Finish the class definition. */
11116 type = finish_class_definition (type,
11117 attributes,
11118 has_trailing_semicolon,
11119 nested_name_specifier_p);
11120 /* If this class is not itself within the scope of another class,
11121 then we need to parse the bodies of all of the queued function
11122 definitions. Note that the queued functions defined in a class
11123 are not always processed immediately following the
11124 class-specifier for that class. Consider:
11125
11126 struct A {
11127 struct B { void f() { sizeof (A); } };
11128 };
11129
11130 If `f' were processed before the processing of `A' were
11131 completed, there would be no way to compute the size of `A'.
11132 Note that the nesting we are interested in here is lexical --
11133 not the semantic nesting given by TYPE_CONTEXT. In particular,
11134 for:
11135
11136 struct A { struct B; };
11137 struct A::B { void f() { } };
11138
11139 there is no need to delay the parsing of `A::B::f'. */
11140 if (--parser->num_classes_being_defined == 0)
11141 {
8218bd34
MM
11142 tree queue_entry;
11143 tree fn;
a723baf1 11144
8218bd34
MM
11145 /* In a first pass, parse default arguments to the functions.
11146 Then, in a second pass, parse the bodies of the functions.
11147 This two-phased approach handles cases like:
11148
11149 struct S {
11150 void f() { g(); }
11151 void g(int i = 3);
11152 };
11153
11154 */
8db1028e
NS
11155 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11156 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11157 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11158 TREE_PURPOSE (parser->unparsed_functions_queues)
11159 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11160 {
11161 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11162 /* Make sure that any template parameters are in scope. */
11163 maybe_begin_member_template_processing (fn);
11164 /* If there are default arguments that have not yet been processed,
11165 take care of them now. */
11166 cp_parser_late_parsing_default_args (parser, fn);
11167 /* Remove any template parameters from the symbol table. */
11168 maybe_end_member_template_processing ();
11169 }
11170 /* Now parse the body of the functions. */
8db1028e
NS
11171 for (TREE_VALUE (parser->unparsed_functions_queues)
11172 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11173 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11174 TREE_VALUE (parser->unparsed_functions_queues)
11175 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11176 {
a723baf1 11177 /* Figure out which function we need to process. */
a723baf1
MM
11178 fn = TREE_VALUE (queue_entry);
11179
11180 /* Parse the function. */
11181 cp_parser_late_parsing_for_member (parser, fn);
a723baf1
MM
11182 }
11183
a723baf1
MM
11184 }
11185
11186 /* Put back any saved access checks. */
cf22909c 11187 pop_deferring_access_checks ();
a723baf1
MM
11188
11189 /* Restore the count of active template-parameter-lists. */
11190 parser->num_template_parameter_lists
11191 = saved_num_template_parameter_lists;
11192
11193 return type;
11194}
11195
11196/* Parse a class-head.
11197
11198 class-head:
11199 class-key identifier [opt] base-clause [opt]
11200 class-key nested-name-specifier identifier base-clause [opt]
11201 class-key nested-name-specifier [opt] template-id
11202 base-clause [opt]
11203
11204 GNU Extensions:
11205 class-key attributes identifier [opt] base-clause [opt]
11206 class-key attributes nested-name-specifier identifier base-clause [opt]
11207 class-key attributes nested-name-specifier [opt] template-id
11208 base-clause [opt]
11209
11210 Returns the TYPE of the indicated class. Sets
11211 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11212 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11213
11214 Returns NULL_TREE if the class-head is syntactically valid, but
11215 semantically invalid in a way that means we should skip the entire
11216 body of the class. */
11217
11218static tree
94edc4ab
NN
11219cp_parser_class_head (cp_parser* parser,
11220 bool* nested_name_specifier_p)
a723baf1
MM
11221{
11222 cp_token *token;
11223 tree nested_name_specifier;
11224 enum tag_types class_key;
11225 tree id = NULL_TREE;
11226 tree type = NULL_TREE;
11227 tree attributes;
11228 bool template_id_p = false;
11229 bool qualified_p = false;
11230 bool invalid_nested_name_p = false;
11231 unsigned num_templates;
11232
11233 /* Assume no nested-name-specifier will be present. */
11234 *nested_name_specifier_p = false;
11235 /* Assume no template parameter lists will be used in defining the
11236 type. */
11237 num_templates = 0;
11238
11239 /* Look for the class-key. */
11240 class_key = cp_parser_class_key (parser);
11241 if (class_key == none_type)
11242 return error_mark_node;
11243
11244 /* Parse the attributes. */
11245 attributes = cp_parser_attributes_opt (parser);
11246
11247 /* If the next token is `::', that is invalid -- but sometimes
11248 people do try to write:
11249
11250 struct ::S {};
11251
11252 Handle this gracefully by accepting the extra qualifier, and then
11253 issuing an error about it later if this really is a
2050a1bb 11254 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11255 specifier, remain silent. */
11256 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11257 qualified_p = true;
11258
8d241e0b
KL
11259 push_deferring_access_checks (dk_no_check);
11260
a723baf1
MM
11261 /* Determine the name of the class. Begin by looking for an
11262 optional nested-name-specifier. */
11263 nested_name_specifier
11264 = cp_parser_nested_name_specifier_opt (parser,
11265 /*typename_keyword_p=*/false,
66d418e6 11266 /*check_dependency_p=*/false,
a723baf1
MM
11267 /*type_p=*/false);
11268 /* If there was a nested-name-specifier, then there *must* be an
11269 identifier. */
11270 if (nested_name_specifier)
11271 {
11272 /* Although the grammar says `identifier', it really means
11273 `class-name' or `template-name'. You are only allowed to
11274 define a class that has already been declared with this
11275 syntax.
11276
11277 The proposed resolution for Core Issue 180 says that whever
11278 you see `class T::X' you should treat `X' as a type-name.
11279
11280 It is OK to define an inaccessible class; for example:
11281
11282 class A { class B; };
11283 class A::B {};
11284
a723baf1
MM
11285 We do not know if we will see a class-name, or a
11286 template-name. We look for a class-name first, in case the
11287 class-name is a template-id; if we looked for the
11288 template-name first we would stop after the template-name. */
11289 cp_parser_parse_tentatively (parser);
11290 type = cp_parser_class_name (parser,
11291 /*typename_keyword_p=*/false,
11292 /*template_keyword_p=*/false,
11293 /*type_p=*/true,
a723baf1
MM
11294 /*check_dependency_p=*/false,
11295 /*class_head_p=*/true);
11296 /* If that didn't work, ignore the nested-name-specifier. */
11297 if (!cp_parser_parse_definitely (parser))
11298 {
11299 invalid_nested_name_p = true;
11300 id = cp_parser_identifier (parser);
11301 if (id == error_mark_node)
11302 id = NULL_TREE;
11303 }
11304 /* If we could not find a corresponding TYPE, treat this
11305 declaration like an unqualified declaration. */
11306 if (type == error_mark_node)
11307 nested_name_specifier = NULL_TREE;
11308 /* Otherwise, count the number of templates used in TYPE and its
11309 containing scopes. */
11310 else
11311 {
11312 tree scope;
11313
11314 for (scope = TREE_TYPE (type);
11315 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11316 scope = (TYPE_P (scope)
11317 ? TYPE_CONTEXT (scope)
11318 : DECL_CONTEXT (scope)))
11319 if (TYPE_P (scope)
11320 && CLASS_TYPE_P (scope)
11321 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11322 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11323 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11324 ++num_templates;
11325 }
11326 }
11327 /* Otherwise, the identifier is optional. */
11328 else
11329 {
11330 /* We don't know whether what comes next is a template-id,
11331 an identifier, or nothing at all. */
11332 cp_parser_parse_tentatively (parser);
11333 /* Check for a template-id. */
11334 id = cp_parser_template_id (parser,
11335 /*template_keyword_p=*/false,
11336 /*check_dependency_p=*/true);
11337 /* If that didn't work, it could still be an identifier. */
11338 if (!cp_parser_parse_definitely (parser))
11339 {
11340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11341 id = cp_parser_identifier (parser);
11342 else
11343 id = NULL_TREE;
11344 }
11345 else
11346 {
11347 template_id_p = true;
11348 ++num_templates;
11349 }
11350 }
11351
8d241e0b
KL
11352 pop_deferring_access_checks ();
11353
a723baf1
MM
11354 /* If it's not a `:' or a `{' then we can't really be looking at a
11355 class-head, since a class-head only appears as part of a
11356 class-specifier. We have to detect this situation before calling
11357 xref_tag, since that has irreversible side-effects. */
11358 if (!cp_parser_next_token_starts_class_definition_p (parser))
11359 {
11360 cp_parser_error (parser, "expected `{' or `:'");
11361 return error_mark_node;
11362 }
11363
11364 /* At this point, we're going ahead with the class-specifier, even
11365 if some other problem occurs. */
11366 cp_parser_commit_to_tentative_parse (parser);
11367 /* Issue the error about the overly-qualified name now. */
11368 if (qualified_p)
11369 cp_parser_error (parser,
11370 "global qualification of class name is invalid");
11371 else if (invalid_nested_name_p)
11372 cp_parser_error (parser,
11373 "qualified name does not name a class");
11374 /* Make sure that the right number of template parameters were
11375 present. */
11376 if (!cp_parser_check_template_parameters (parser, num_templates))
11377 /* If something went wrong, there is no point in even trying to
11378 process the class-definition. */
11379 return NULL_TREE;
11380
a723baf1
MM
11381 /* Look up the type. */
11382 if (template_id_p)
11383 {
11384 type = TREE_TYPE (id);
11385 maybe_process_partial_specialization (type);
11386 }
11387 else if (!nested_name_specifier)
11388 {
11389 /* If the class was unnamed, create a dummy name. */
11390 if (!id)
11391 id = make_anon_name ();
11392 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11393 }
11394 else
11395 {
a723baf1 11396 tree class_type;
089d6ea7 11397 tree scope;
a723baf1
MM
11398
11399 /* Given:
11400
11401 template <typename T> struct S { struct T };
14d22dd6 11402 template <typename T> struct S<T>::T { };
a723baf1
MM
11403
11404 we will get a TYPENAME_TYPE when processing the definition of
11405 `S::T'. We need to resolve it to the actual type before we
11406 try to define it. */
11407 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11408 {
14d22dd6
MM
11409 class_type = resolve_typename_type (TREE_TYPE (type),
11410 /*only_current_p=*/false);
11411 if (class_type != error_mark_node)
11412 type = TYPE_NAME (class_type);
11413 else
11414 {
11415 cp_parser_error (parser, "could not resolve typename type");
11416 type = error_mark_node;
11417 }
a723baf1
MM
11418 }
11419
089d6ea7
MM
11420 /* Figure out in what scope the declaration is being placed. */
11421 scope = current_scope ();
11422 if (!scope)
11423 scope = current_namespace;
11424 /* If that scope does not contain the scope in which the
11425 class was originally declared, the program is invalid. */
11426 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11427 {
0e136342 11428 error ("declaration of `%D' in `%D' which does not "
089d6ea7
MM
11429 "enclose `%D'", type, scope, nested_name_specifier);
11430 return NULL_TREE;
11431 }
11432
a723baf1
MM
11433 maybe_process_partial_specialization (TREE_TYPE (type));
11434 class_type = current_class_type;
11435 type = TREE_TYPE (handle_class_head (class_key,
11436 nested_name_specifier,
11437 type,
089d6ea7 11438 attributes));
a723baf1
MM
11439 if (type != error_mark_node)
11440 {
11441 if (!class_type && TYPE_CONTEXT (type))
11442 *nested_name_specifier_p = true;
11443 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11444 class_type))
11445 *nested_name_specifier_p = true;
11446 }
11447 }
11448 /* Indicate whether this class was declared as a `class' or as a
11449 `struct'. */
11450 if (TREE_CODE (type) == RECORD_TYPE)
11451 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11452 cp_parser_check_class_key (class_key, type);
11453
11454 /* Enter the scope containing the class; the names of base classes
11455 should be looked up in that context. For example, given:
11456
11457 struct A { struct B {}; struct C; };
11458 struct A::C : B {};
11459
11460 is valid. */
11461 if (nested_name_specifier)
11462 push_scope (nested_name_specifier);
11463 /* Now, look for the base-clause. */
11464 token = cp_lexer_peek_token (parser->lexer);
11465 if (token->type == CPP_COLON)
11466 {
11467 tree bases;
11468
11469 /* Get the list of base-classes. */
11470 bases = cp_parser_base_clause (parser);
11471 /* Process them. */
11472 xref_basetypes (type, bases);
11473 }
11474 /* Leave the scope given by the nested-name-specifier. We will
11475 enter the class scope itself while processing the members. */
11476 if (nested_name_specifier)
11477 pop_scope (nested_name_specifier);
11478
11479 return type;
11480}
11481
11482/* Parse a class-key.
11483
11484 class-key:
11485 class
11486 struct
11487 union
11488
11489 Returns the kind of class-key specified, or none_type to indicate
11490 error. */
11491
11492static enum tag_types
94edc4ab 11493cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11494{
11495 cp_token *token;
11496 enum tag_types tag_type;
11497
11498 /* Look for the class-key. */
11499 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11500 if (!token)
11501 return none_type;
11502
11503 /* Check to see if the TOKEN is a class-key. */
11504 tag_type = cp_parser_token_is_class_key (token);
11505 if (!tag_type)
11506 cp_parser_error (parser, "expected class-key");
11507 return tag_type;
11508}
11509
11510/* Parse an (optional) member-specification.
11511
11512 member-specification:
11513 member-declaration member-specification [opt]
11514 access-specifier : member-specification [opt] */
11515
11516static void
94edc4ab 11517cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11518{
11519 while (true)
11520 {
11521 cp_token *token;
11522 enum rid keyword;
11523
11524 /* Peek at the next token. */
11525 token = cp_lexer_peek_token (parser->lexer);
11526 /* If it's a `}', or EOF then we've seen all the members. */
11527 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11528 break;
11529
11530 /* See if this token is a keyword. */
11531 keyword = token->keyword;
11532 switch (keyword)
11533 {
11534 case RID_PUBLIC:
11535 case RID_PROTECTED:
11536 case RID_PRIVATE:
11537 /* Consume the access-specifier. */
11538 cp_lexer_consume_token (parser->lexer);
11539 /* Remember which access-specifier is active. */
11540 current_access_specifier = token->value;
11541 /* Look for the `:'. */
11542 cp_parser_require (parser, CPP_COLON, "`:'");
11543 break;
11544
11545 default:
11546 /* Otherwise, the next construction must be a
11547 member-declaration. */
11548 cp_parser_member_declaration (parser);
a723baf1
MM
11549 }
11550 }
11551}
11552
11553/* Parse a member-declaration.
11554
11555 member-declaration:
11556 decl-specifier-seq [opt] member-declarator-list [opt] ;
11557 function-definition ; [opt]
11558 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11559 using-declaration
11560 template-declaration
11561
11562 member-declarator-list:
11563 member-declarator
11564 member-declarator-list , member-declarator
11565
11566 member-declarator:
11567 declarator pure-specifier [opt]
11568 declarator constant-initializer [opt]
11569 identifier [opt] : constant-expression
11570
11571 GNU Extensions:
11572
11573 member-declaration:
11574 __extension__ member-declaration
11575
11576 member-declarator:
11577 declarator attributes [opt] pure-specifier [opt]
11578 declarator attributes [opt] constant-initializer [opt]
11579 identifier [opt] attributes [opt] : constant-expression */
11580
11581static void
94edc4ab 11582cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
11583{
11584 tree decl_specifiers;
11585 tree prefix_attributes;
11586 tree decl;
11587 bool declares_class_or_enum;
11588 bool friend_p;
11589 cp_token *token;
11590 int saved_pedantic;
11591
11592 /* Check for the `__extension__' keyword. */
11593 if (cp_parser_extension_opt (parser, &saved_pedantic))
11594 {
11595 /* Recurse. */
11596 cp_parser_member_declaration (parser);
11597 /* Restore the old value of the PEDANTIC flag. */
11598 pedantic = saved_pedantic;
11599
11600 return;
11601 }
11602
11603 /* Check for a template-declaration. */
11604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11605 {
11606 /* Parse the template-declaration. */
11607 cp_parser_template_declaration (parser, /*member_p=*/true);
11608
11609 return;
11610 }
11611
11612 /* Check for a using-declaration. */
11613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11614 {
11615 /* Parse the using-declaration. */
11616 cp_parser_using_declaration (parser);
11617
11618 return;
11619 }
11620
11621 /* We can't tell whether we're looking at a declaration or a
11622 function-definition. */
11623 cp_parser_parse_tentatively (parser);
11624
11625 /* Parse the decl-specifier-seq. */
11626 decl_specifiers
11627 = cp_parser_decl_specifier_seq (parser,
11628 CP_PARSER_FLAGS_OPTIONAL,
11629 &prefix_attributes,
11630 &declares_class_or_enum);
8fbc5ae7
MM
11631 /* Check for an invalid type-name. */
11632 if (cp_parser_diagnose_invalid_type_name (parser))
11633 return;
a723baf1
MM
11634 /* If there is no declarator, then the decl-specifier-seq should
11635 specify a type. */
11636 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11637 {
11638 /* If there was no decl-specifier-seq, and the next token is a
11639 `;', then we have something like:
11640
11641 struct S { ; };
11642
11643 [class.mem]
11644
11645 Each member-declaration shall declare at least one member
11646 name of the class. */
11647 if (!decl_specifiers)
11648 {
11649 if (pedantic)
11650 pedwarn ("extra semicolon");
11651 }
11652 else
11653 {
11654 tree type;
11655
11656 /* See if this declaration is a friend. */
11657 friend_p = cp_parser_friend_p (decl_specifiers);
11658 /* If there were decl-specifiers, check to see if there was
11659 a class-declaration. */
11660 type = check_tag_decl (decl_specifiers);
11661 /* Nested classes have already been added to the class, but
11662 a `friend' needs to be explicitly registered. */
11663 if (friend_p)
11664 {
11665 /* If the `friend' keyword was present, the friend must
11666 be introduced with a class-key. */
11667 if (!declares_class_or_enum)
11668 error ("a class-key must be used when declaring a friend");
11669 /* In this case:
11670
11671 template <typename T> struct A {
11672 friend struct A<T>::B;
11673 };
11674
11675 A<T>::B will be represented by a TYPENAME_TYPE, and
11676 therefore not recognized by check_tag_decl. */
11677 if (!type)
11678 {
11679 tree specifier;
11680
11681 for (specifier = decl_specifiers;
11682 specifier;
11683 specifier = TREE_CHAIN (specifier))
11684 {
11685 tree s = TREE_VALUE (specifier);
11686
11687 if (TREE_CODE (s) == IDENTIFIER_NODE
11688 && IDENTIFIER_GLOBAL_VALUE (s))
11689 type = IDENTIFIER_GLOBAL_VALUE (s);
11690 if (TREE_CODE (s) == TYPE_DECL)
11691 s = TREE_TYPE (s);
11692 if (TYPE_P (s))
11693 {
11694 type = s;
11695 break;
11696 }
11697 }
11698 }
11699 if (!type)
11700 error ("friend declaration does not name a class or "
11701 "function");
11702 else
11703 make_friend_class (current_class_type, type);
11704 }
11705 /* If there is no TYPE, an error message will already have
11706 been issued. */
11707 else if (!type)
11708 ;
11709 /* An anonymous aggregate has to be handled specially; such
11710 a declaration really declares a data member (with a
11711 particular type), as opposed to a nested class. */
11712 else if (ANON_AGGR_TYPE_P (type))
11713 {
11714 /* Remove constructors and such from TYPE, now that we
34cd5ae7 11715 know it is an anonymous aggregate. */
a723baf1
MM
11716 fixup_anonymous_aggr (type);
11717 /* And make the corresponding data member. */
11718 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11719 /* Add it to the class. */
11720 finish_member_declaration (decl);
11721 }
11722 }
11723 }
11724 else
11725 {
11726 /* See if these declarations will be friends. */
11727 friend_p = cp_parser_friend_p (decl_specifiers);
11728
11729 /* Keep going until we hit the `;' at the end of the
11730 declaration. */
11731 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11732 {
11733 tree attributes = NULL_TREE;
11734 tree first_attribute;
11735
11736 /* Peek at the next token. */
11737 token = cp_lexer_peek_token (parser->lexer);
11738
11739 /* Check for a bitfield declaration. */
11740 if (token->type == CPP_COLON
11741 || (token->type == CPP_NAME
11742 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11743 == CPP_COLON))
11744 {
11745 tree identifier;
11746 tree width;
11747
11748 /* Get the name of the bitfield. Note that we cannot just
11749 check TOKEN here because it may have been invalidated by
11750 the call to cp_lexer_peek_nth_token above. */
11751 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11752 identifier = cp_parser_identifier (parser);
11753 else
11754 identifier = NULL_TREE;
11755
11756 /* Consume the `:' token. */
11757 cp_lexer_consume_token (parser->lexer);
11758 /* Get the width of the bitfield. */
14d22dd6
MM
11759 width
11760 = cp_parser_constant_expression (parser,
11761 /*allow_non_constant=*/false,
11762 NULL);
a723baf1
MM
11763
11764 /* Look for attributes that apply to the bitfield. */
11765 attributes = cp_parser_attributes_opt (parser);
11766 /* Remember which attributes are prefix attributes and
11767 which are not. */
11768 first_attribute = attributes;
11769 /* Combine the attributes. */
11770 attributes = chainon (prefix_attributes, attributes);
11771
11772 /* Create the bitfield declaration. */
11773 decl = grokbitfield (identifier,
11774 decl_specifiers,
11775 width);
11776 /* Apply the attributes. */
11777 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11778 }
11779 else
11780 {
11781 tree declarator;
11782 tree initializer;
11783 tree asm_specification;
7efa3e22 11784 int ctor_dtor_or_conv_p;
a723baf1
MM
11785
11786 /* Parse the declarator. */
11787 declarator
62b8a44e 11788 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
11789 &ctor_dtor_or_conv_p);
11790
11791 /* If something went wrong parsing the declarator, make sure
11792 that we at least consume some tokens. */
11793 if (declarator == error_mark_node)
11794 {
11795 /* Skip to the end of the statement. */
11796 cp_parser_skip_to_end_of_statement (parser);
11797 break;
11798 }
11799
11800 /* Look for an asm-specification. */
11801 asm_specification = cp_parser_asm_specification_opt (parser);
11802 /* Look for attributes that apply to the declaration. */
11803 attributes = cp_parser_attributes_opt (parser);
11804 /* Remember which attributes are prefix attributes and
11805 which are not. */
11806 first_attribute = attributes;
11807 /* Combine the attributes. */
11808 attributes = chainon (prefix_attributes, attributes);
11809
11810 /* If it's an `=', then we have a constant-initializer or a
11811 pure-specifier. It is not correct to parse the
11812 initializer before registering the member declaration
11813 since the member declaration should be in scope while
11814 its initializer is processed. However, the rest of the
11815 front end does not yet provide an interface that allows
11816 us to handle this correctly. */
11817 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11818 {
11819 /* In [class.mem]:
11820
11821 A pure-specifier shall be used only in the declaration of
11822 a virtual function.
11823
11824 A member-declarator can contain a constant-initializer
11825 only if it declares a static member of integral or
11826 enumeration type.
11827
11828 Therefore, if the DECLARATOR is for a function, we look
11829 for a pure-specifier; otherwise, we look for a
11830 constant-initializer. When we call `grokfield', it will
11831 perform more stringent semantics checks. */
11832 if (TREE_CODE (declarator) == CALL_EXPR)
11833 initializer = cp_parser_pure_specifier (parser);
11834 else
11835 {
11836 /* This declaration cannot be a function
11837 definition. */
11838 cp_parser_commit_to_tentative_parse (parser);
11839 /* Parse the initializer. */
11840 initializer = cp_parser_constant_initializer (parser);
11841 }
11842 }
11843 /* Otherwise, there is no initializer. */
11844 else
11845 initializer = NULL_TREE;
11846
11847 /* See if we are probably looking at a function
11848 definition. We are certainly not looking at at a
11849 member-declarator. Calling `grokfield' has
11850 side-effects, so we must not do it unless we are sure
11851 that we are looking at a member-declarator. */
11852 if (cp_parser_token_starts_function_definition_p
11853 (cp_lexer_peek_token (parser->lexer)))
11854 decl = error_mark_node;
11855 else
39703eb9
MM
11856 {
11857 /* Create the declaration. */
11858 decl = grokfield (declarator,
11859 decl_specifiers,
11860 initializer,
11861 asm_specification,
11862 attributes);
11863 /* Any initialization must have been from a
11864 constant-expression. */
11865 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
11866 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
11867 }
a723baf1
MM
11868 }
11869
11870 /* Reset PREFIX_ATTRIBUTES. */
11871 while (attributes && TREE_CHAIN (attributes) != first_attribute)
11872 attributes = TREE_CHAIN (attributes);
11873 if (attributes)
11874 TREE_CHAIN (attributes) = NULL_TREE;
11875
11876 /* If there is any qualification still in effect, clear it
11877 now; we will be starting fresh with the next declarator. */
11878 parser->scope = NULL_TREE;
11879 parser->qualifying_scope = NULL_TREE;
11880 parser->object_scope = NULL_TREE;
11881 /* If it's a `,', then there are more declarators. */
11882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11883 cp_lexer_consume_token (parser->lexer);
11884 /* If the next token isn't a `;', then we have a parse error. */
11885 else if (cp_lexer_next_token_is_not (parser->lexer,
11886 CPP_SEMICOLON))
11887 {
11888 cp_parser_error (parser, "expected `;'");
11889 /* Skip tokens until we find a `;' */
11890 cp_parser_skip_to_end_of_statement (parser);
11891
11892 break;
11893 }
11894
11895 if (decl)
11896 {
11897 /* Add DECL to the list of members. */
11898 if (!friend_p)
11899 finish_member_declaration (decl);
11900
a723baf1 11901 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 11902 cp_parser_save_default_args (parser, decl);
a723baf1
MM
11903 }
11904 }
11905 }
11906
11907 /* If everything went well, look for the `;'. */
11908 if (cp_parser_parse_definitely (parser))
11909 {
11910 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11911 return;
11912 }
11913
11914 /* Parse the function-definition. */
11915 decl = cp_parser_function_definition (parser, &friend_p);
11916 /* If the member was not a friend, declare it here. */
11917 if (!friend_p)
11918 finish_member_declaration (decl);
11919 /* Peek at the next token. */
11920 token = cp_lexer_peek_token (parser->lexer);
11921 /* If the next token is a semicolon, consume it. */
11922 if (token->type == CPP_SEMICOLON)
11923 cp_lexer_consume_token (parser->lexer);
11924}
11925
11926/* Parse a pure-specifier.
11927
11928 pure-specifier:
11929 = 0
11930
11931 Returns INTEGER_ZERO_NODE if a pure specifier is found.
11932 Otherwiser, ERROR_MARK_NODE is returned. */
11933
11934static tree
94edc4ab 11935cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
11936{
11937 cp_token *token;
11938
11939 /* Look for the `=' token. */
11940 if (!cp_parser_require (parser, CPP_EQ, "`='"))
11941 return error_mark_node;
11942 /* Look for the `0' token. */
11943 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
11944 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
11945 to get information from the lexer about how the number was
11946 spelled in order to fix this problem. */
11947 if (!token || !integer_zerop (token->value))
11948 return error_mark_node;
11949
11950 return integer_zero_node;
11951}
11952
11953/* Parse a constant-initializer.
11954
11955 constant-initializer:
11956 = constant-expression
11957
11958 Returns a representation of the constant-expression. */
11959
11960static tree
94edc4ab 11961cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
11962{
11963 /* Look for the `=' token. */
11964 if (!cp_parser_require (parser, CPP_EQ, "`='"))
11965 return error_mark_node;
11966
11967 /* It is invalid to write:
11968
11969 struct S { static const int i = { 7 }; };
11970
11971 */
11972 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11973 {
11974 cp_parser_error (parser,
11975 "a brace-enclosed initializer is not allowed here");
11976 /* Consume the opening brace. */
11977 cp_lexer_consume_token (parser->lexer);
11978 /* Skip the initializer. */
11979 cp_parser_skip_to_closing_brace (parser);
11980 /* Look for the trailing `}'. */
11981 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11982
11983 return error_mark_node;
11984 }
11985
14d22dd6
MM
11986 return cp_parser_constant_expression (parser,
11987 /*allow_non_constant=*/false,
11988 NULL);
a723baf1
MM
11989}
11990
11991/* Derived classes [gram.class.derived] */
11992
11993/* Parse a base-clause.
11994
11995 base-clause:
11996 : base-specifier-list
11997
11998 base-specifier-list:
11999 base-specifier
12000 base-specifier-list , base-specifier
12001
12002 Returns a TREE_LIST representing the base-classes, in the order in
12003 which they were declared. The representation of each node is as
12004 described by cp_parser_base_specifier.
12005
12006 In the case that no bases are specified, this function will return
12007 NULL_TREE, not ERROR_MARK_NODE. */
12008
12009static tree
94edc4ab 12010cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12011{
12012 tree bases = NULL_TREE;
12013
12014 /* Look for the `:' that begins the list. */
12015 cp_parser_require (parser, CPP_COLON, "`:'");
12016
12017 /* Scan the base-specifier-list. */
12018 while (true)
12019 {
12020 cp_token *token;
12021 tree base;
12022
12023 /* Look for the base-specifier. */
12024 base = cp_parser_base_specifier (parser);
12025 /* Add BASE to the front of the list. */
12026 if (base != error_mark_node)
12027 {
12028 TREE_CHAIN (base) = bases;
12029 bases = base;
12030 }
12031 /* Peek at the next token. */
12032 token = cp_lexer_peek_token (parser->lexer);
12033 /* If it's not a comma, then the list is complete. */
12034 if (token->type != CPP_COMMA)
12035 break;
12036 /* Consume the `,'. */
12037 cp_lexer_consume_token (parser->lexer);
12038 }
12039
12040 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12041 base class had a qualified name. However, the next name that
12042 appears is certainly not qualified. */
12043 parser->scope = NULL_TREE;
12044 parser->qualifying_scope = NULL_TREE;
12045 parser->object_scope = NULL_TREE;
12046
12047 return nreverse (bases);
12048}
12049
12050/* Parse a base-specifier.
12051
12052 base-specifier:
12053 :: [opt] nested-name-specifier [opt] class-name
12054 virtual access-specifier [opt] :: [opt] nested-name-specifier
12055 [opt] class-name
12056 access-specifier virtual [opt] :: [opt] nested-name-specifier
12057 [opt] class-name
12058
12059 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12060 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12061 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12062 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12063
12064static tree
94edc4ab 12065cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12066{
12067 cp_token *token;
12068 bool done = false;
12069 bool virtual_p = false;
12070 bool duplicate_virtual_error_issued_p = false;
12071 bool duplicate_access_error_issued_p = false;
bbaab916 12072 bool class_scope_p, template_p;
dbbf88d1 12073 tree access = access_default_node;
a723baf1
MM
12074 tree type;
12075
12076 /* Process the optional `virtual' and `access-specifier'. */
12077 while (!done)
12078 {
12079 /* Peek at the next token. */
12080 token = cp_lexer_peek_token (parser->lexer);
12081 /* Process `virtual'. */
12082 switch (token->keyword)
12083 {
12084 case RID_VIRTUAL:
12085 /* If `virtual' appears more than once, issue an error. */
12086 if (virtual_p && !duplicate_virtual_error_issued_p)
12087 {
12088 cp_parser_error (parser,
12089 "`virtual' specified more than once in base-specified");
12090 duplicate_virtual_error_issued_p = true;
12091 }
12092
12093 virtual_p = true;
12094
12095 /* Consume the `virtual' token. */
12096 cp_lexer_consume_token (parser->lexer);
12097
12098 break;
12099
12100 case RID_PUBLIC:
12101 case RID_PROTECTED:
12102 case RID_PRIVATE:
12103 /* If more than one access specifier appears, issue an
12104 error. */
dbbf88d1
NS
12105 if (access != access_default_node
12106 && !duplicate_access_error_issued_p)
a723baf1
MM
12107 {
12108 cp_parser_error (parser,
12109 "more than one access specifier in base-specified");
12110 duplicate_access_error_issued_p = true;
12111 }
12112
dbbf88d1 12113 access = ridpointers[(int) token->keyword];
a723baf1
MM
12114
12115 /* Consume the access-specifier. */
12116 cp_lexer_consume_token (parser->lexer);
12117
12118 break;
12119
12120 default:
12121 done = true;
12122 break;
12123 }
12124 }
12125
a723baf1
MM
12126 /* Look for the optional `::' operator. */
12127 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12128 /* Look for the nested-name-specifier. The simplest way to
12129 implement:
12130
12131 [temp.res]
12132
12133 The keyword `typename' is not permitted in a base-specifier or
12134 mem-initializer; in these contexts a qualified name that
12135 depends on a template-parameter is implicitly assumed to be a
12136 type name.
12137
12138 is to pretend that we have seen the `typename' keyword at this
12139 point. */
12140 cp_parser_nested_name_specifier_opt (parser,
12141 /*typename_keyword_p=*/true,
12142 /*check_dependency_p=*/true,
12143 /*type_p=*/true);
12144 /* If the base class is given by a qualified name, assume that names
12145 we see are type names or templates, as appropriate. */
12146 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12147 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12148
a723baf1
MM
12149 /* Finally, look for the class-name. */
12150 type = cp_parser_class_name (parser,
12151 class_scope_p,
bbaab916 12152 template_p,
a723baf1 12153 /*type_p=*/true,
a723baf1
MM
12154 /*check_dependency_p=*/true,
12155 /*class_head_p=*/false);
12156
12157 if (type == error_mark_node)
12158 return error_mark_node;
12159
dbbf88d1 12160 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12161}
12162
12163/* Exception handling [gram.exception] */
12164
12165/* Parse an (optional) exception-specification.
12166
12167 exception-specification:
12168 throw ( type-id-list [opt] )
12169
12170 Returns a TREE_LIST representing the exception-specification. The
12171 TREE_VALUE of each node is a type. */
12172
12173static tree
94edc4ab 12174cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12175{
12176 cp_token *token;
12177 tree type_id_list;
12178
12179 /* Peek at the next token. */
12180 token = cp_lexer_peek_token (parser->lexer);
12181 /* If it's not `throw', then there's no exception-specification. */
12182 if (!cp_parser_is_keyword (token, RID_THROW))
12183 return NULL_TREE;
12184
12185 /* Consume the `throw'. */
12186 cp_lexer_consume_token (parser->lexer);
12187
12188 /* Look for the `('. */
12189 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12190
12191 /* Peek at the next token. */
12192 token = cp_lexer_peek_token (parser->lexer);
12193 /* If it's not a `)', then there is a type-id-list. */
12194 if (token->type != CPP_CLOSE_PAREN)
12195 {
12196 const char *saved_message;
12197
12198 /* Types may not be defined in an exception-specification. */
12199 saved_message = parser->type_definition_forbidden_message;
12200 parser->type_definition_forbidden_message
12201 = "types may not be defined in an exception-specification";
12202 /* Parse the type-id-list. */
12203 type_id_list = cp_parser_type_id_list (parser);
12204 /* Restore the saved message. */
12205 parser->type_definition_forbidden_message = saved_message;
12206 }
12207 else
12208 type_id_list = empty_except_spec;
12209
12210 /* Look for the `)'. */
12211 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12212
12213 return type_id_list;
12214}
12215
12216/* Parse an (optional) type-id-list.
12217
12218 type-id-list:
12219 type-id
12220 type-id-list , type-id
12221
12222 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12223 in the order that the types were presented. */
12224
12225static tree
94edc4ab 12226cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12227{
12228 tree types = NULL_TREE;
12229
12230 while (true)
12231 {
12232 cp_token *token;
12233 tree type;
12234
12235 /* Get the next type-id. */
12236 type = cp_parser_type_id (parser);
12237 /* Add it to the list. */
12238 types = add_exception_specifier (types, type, /*complain=*/1);
12239 /* Peek at the next token. */
12240 token = cp_lexer_peek_token (parser->lexer);
12241 /* If it is not a `,', we are done. */
12242 if (token->type != CPP_COMMA)
12243 break;
12244 /* Consume the `,'. */
12245 cp_lexer_consume_token (parser->lexer);
12246 }
12247
12248 return nreverse (types);
12249}
12250
12251/* Parse a try-block.
12252
12253 try-block:
12254 try compound-statement handler-seq */
12255
12256static tree
94edc4ab 12257cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12258{
12259 tree try_block;
12260
12261 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12262 try_block = begin_try_block ();
12263 cp_parser_compound_statement (parser);
12264 finish_try_block (try_block);
12265 cp_parser_handler_seq (parser);
12266 finish_handler_sequence (try_block);
12267
12268 return try_block;
12269}
12270
12271/* Parse a function-try-block.
12272
12273 function-try-block:
12274 try ctor-initializer [opt] function-body handler-seq */
12275
12276static bool
94edc4ab 12277cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12278{
12279 tree try_block;
12280 bool ctor_initializer_p;
12281
12282 /* Look for the `try' keyword. */
12283 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12284 return false;
12285 /* Let the rest of the front-end know where we are. */
12286 try_block = begin_function_try_block ();
12287 /* Parse the function-body. */
12288 ctor_initializer_p
12289 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12290 /* We're done with the `try' part. */
12291 finish_function_try_block (try_block);
12292 /* Parse the handlers. */
12293 cp_parser_handler_seq (parser);
12294 /* We're done with the handlers. */
12295 finish_function_handler_sequence (try_block);
12296
12297 return ctor_initializer_p;
12298}
12299
12300/* Parse a handler-seq.
12301
12302 handler-seq:
12303 handler handler-seq [opt] */
12304
12305static void
94edc4ab 12306cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12307{
12308 while (true)
12309 {
12310 cp_token *token;
12311
12312 /* Parse the handler. */
12313 cp_parser_handler (parser);
12314 /* Peek at the next token. */
12315 token = cp_lexer_peek_token (parser->lexer);
12316 /* If it's not `catch' then there are no more handlers. */
12317 if (!cp_parser_is_keyword (token, RID_CATCH))
12318 break;
12319 }
12320}
12321
12322/* Parse a handler.
12323
12324 handler:
12325 catch ( exception-declaration ) compound-statement */
12326
12327static void
94edc4ab 12328cp_parser_handler (cp_parser* parser)
a723baf1
MM
12329{
12330 tree handler;
12331 tree declaration;
12332
12333 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12334 handler = begin_handler ();
12335 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12336 declaration = cp_parser_exception_declaration (parser);
12337 finish_handler_parms (declaration, handler);
12338 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12339 cp_parser_compound_statement (parser);
12340 finish_handler (handler);
12341}
12342
12343/* Parse an exception-declaration.
12344
12345 exception-declaration:
12346 type-specifier-seq declarator
12347 type-specifier-seq abstract-declarator
12348 type-specifier-seq
12349 ...
12350
12351 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12352 ellipsis variant is used. */
12353
12354static tree
94edc4ab 12355cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12356{
12357 tree type_specifiers;
12358 tree declarator;
12359 const char *saved_message;
12360
12361 /* If it's an ellipsis, it's easy to handle. */
12362 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12363 {
12364 /* Consume the `...' token. */
12365 cp_lexer_consume_token (parser->lexer);
12366 return NULL_TREE;
12367 }
12368
12369 /* Types may not be defined in exception-declarations. */
12370 saved_message = parser->type_definition_forbidden_message;
12371 parser->type_definition_forbidden_message
12372 = "types may not be defined in exception-declarations";
12373
12374 /* Parse the type-specifier-seq. */
12375 type_specifiers = cp_parser_type_specifier_seq (parser);
12376 /* If it's a `)', then there is no declarator. */
12377 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12378 declarator = NULL_TREE;
12379 else
62b8a44e
NS
12380 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12381 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
12382
12383 /* Restore the saved message. */
12384 parser->type_definition_forbidden_message = saved_message;
12385
12386 return start_handler_parms (type_specifiers, declarator);
12387}
12388
12389/* Parse a throw-expression.
12390
12391 throw-expression:
34cd5ae7 12392 throw assignment-expression [opt]
a723baf1
MM
12393
12394 Returns a THROW_EXPR representing the throw-expression. */
12395
12396static tree
94edc4ab 12397cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12398{
12399 tree expression;
12400
12401 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12402 /* We can't be sure if there is an assignment-expression or not. */
12403 cp_parser_parse_tentatively (parser);
12404 /* Try it. */
12405 expression = cp_parser_assignment_expression (parser);
12406 /* If it didn't work, this is just a rethrow. */
12407 if (!cp_parser_parse_definitely (parser))
12408 expression = NULL_TREE;
12409
12410 return build_throw (expression);
12411}
12412
12413/* GNU Extensions */
12414
12415/* Parse an (optional) asm-specification.
12416
12417 asm-specification:
12418 asm ( string-literal )
12419
12420 If the asm-specification is present, returns a STRING_CST
12421 corresponding to the string-literal. Otherwise, returns
12422 NULL_TREE. */
12423
12424static tree
94edc4ab 12425cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12426{
12427 cp_token *token;
12428 tree asm_specification;
12429
12430 /* Peek at the next token. */
12431 token = cp_lexer_peek_token (parser->lexer);
12432 /* If the next token isn't the `asm' keyword, then there's no
12433 asm-specification. */
12434 if (!cp_parser_is_keyword (token, RID_ASM))
12435 return NULL_TREE;
12436
12437 /* Consume the `asm' token. */
12438 cp_lexer_consume_token (parser->lexer);
12439 /* Look for the `('. */
12440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12441
12442 /* Look for the string-literal. */
12443 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12444 if (token)
12445 asm_specification = token->value;
12446 else
12447 asm_specification = NULL_TREE;
12448
12449 /* Look for the `)'. */
12450 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12451
12452 return asm_specification;
12453}
12454
12455/* Parse an asm-operand-list.
12456
12457 asm-operand-list:
12458 asm-operand
12459 asm-operand-list , asm-operand
12460
12461 asm-operand:
12462 string-literal ( expression )
12463 [ string-literal ] string-literal ( expression )
12464
12465 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12466 each node is the expression. The TREE_PURPOSE is itself a
12467 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12468 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12469 is a STRING_CST for the string literal before the parenthesis. */
12470
12471static tree
94edc4ab 12472cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12473{
12474 tree asm_operands = NULL_TREE;
12475
12476 while (true)
12477 {
12478 tree string_literal;
12479 tree expression;
12480 tree name;
12481 cp_token *token;
12482
12483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12484 {
12485 /* Consume the `[' token. */
12486 cp_lexer_consume_token (parser->lexer);
12487 /* Read the operand name. */
12488 name = cp_parser_identifier (parser);
12489 if (name != error_mark_node)
12490 name = build_string (IDENTIFIER_LENGTH (name),
12491 IDENTIFIER_POINTER (name));
12492 /* Look for the closing `]'. */
12493 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12494 }
12495 else
12496 name = NULL_TREE;
12497 /* Look for the string-literal. */
12498 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12499 string_literal = token ? token->value : error_mark_node;
12500 /* Look for the `('. */
12501 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12502 /* Parse the expression. */
12503 expression = cp_parser_expression (parser);
12504 /* Look for the `)'. */
12505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12506 /* Add this operand to the list. */
12507 asm_operands = tree_cons (build_tree_list (name, string_literal),
12508 expression,
12509 asm_operands);
12510 /* If the next token is not a `,', there are no more
12511 operands. */
12512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12513 break;
12514 /* Consume the `,'. */
12515 cp_lexer_consume_token (parser->lexer);
12516 }
12517
12518 return nreverse (asm_operands);
12519}
12520
12521/* Parse an asm-clobber-list.
12522
12523 asm-clobber-list:
12524 string-literal
12525 asm-clobber-list , string-literal
12526
12527 Returns a TREE_LIST, indicating the clobbers in the order that they
12528 appeared. The TREE_VALUE of each node is a STRING_CST. */
12529
12530static tree
94edc4ab 12531cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
12532{
12533 tree clobbers = NULL_TREE;
12534
12535 while (true)
12536 {
12537 cp_token *token;
12538 tree string_literal;
12539
12540 /* Look for the string literal. */
12541 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12542 string_literal = token ? token->value : error_mark_node;
12543 /* Add it to the list. */
12544 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12545 /* If the next token is not a `,', then the list is
12546 complete. */
12547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12548 break;
12549 /* Consume the `,' token. */
12550 cp_lexer_consume_token (parser->lexer);
12551 }
12552
12553 return clobbers;
12554}
12555
12556/* Parse an (optional) series of attributes.
12557
12558 attributes:
12559 attributes attribute
12560
12561 attribute:
12562 __attribute__ (( attribute-list [opt] ))
12563
12564 The return value is as for cp_parser_attribute_list. */
12565
12566static tree
94edc4ab 12567cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
12568{
12569 tree attributes = NULL_TREE;
12570
12571 while (true)
12572 {
12573 cp_token *token;
12574 tree attribute_list;
12575
12576 /* Peek at the next token. */
12577 token = cp_lexer_peek_token (parser->lexer);
12578 /* If it's not `__attribute__', then we're done. */
12579 if (token->keyword != RID_ATTRIBUTE)
12580 break;
12581
12582 /* Consume the `__attribute__' keyword. */
12583 cp_lexer_consume_token (parser->lexer);
12584 /* Look for the two `(' tokens. */
12585 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12586 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12587
12588 /* Peek at the next token. */
12589 token = cp_lexer_peek_token (parser->lexer);
12590 if (token->type != CPP_CLOSE_PAREN)
12591 /* Parse the attribute-list. */
12592 attribute_list = cp_parser_attribute_list (parser);
12593 else
12594 /* If the next token is a `)', then there is no attribute
12595 list. */
12596 attribute_list = NULL;
12597
12598 /* Look for the two `)' tokens. */
12599 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12601
12602 /* Add these new attributes to the list. */
12603 attributes = chainon (attributes, attribute_list);
12604 }
12605
12606 return attributes;
12607}
12608
12609/* Parse an attribute-list.
12610
12611 attribute-list:
12612 attribute
12613 attribute-list , attribute
12614
12615 attribute:
12616 identifier
12617 identifier ( identifier )
12618 identifier ( identifier , expression-list )
12619 identifier ( expression-list )
12620
12621 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12622 TREE_PURPOSE of each node is the identifier indicating which
12623 attribute is in use. The TREE_VALUE represents the arguments, if
12624 any. */
12625
12626static tree
94edc4ab 12627cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
12628{
12629 tree attribute_list = NULL_TREE;
12630
12631 while (true)
12632 {
12633 cp_token *token;
12634 tree identifier;
12635 tree attribute;
12636
12637 /* Look for the identifier. We also allow keywords here; for
12638 example `__attribute__ ((const))' is legal. */
12639 token = cp_lexer_peek_token (parser->lexer);
12640 if (token->type != CPP_NAME
12641 && token->type != CPP_KEYWORD)
12642 return error_mark_node;
12643 /* Consume the token. */
12644 token = cp_lexer_consume_token (parser->lexer);
12645
12646 /* Save away the identifier that indicates which attribute this is. */
12647 identifier = token->value;
12648 attribute = build_tree_list (identifier, NULL_TREE);
12649
12650 /* Peek at the next token. */
12651 token = cp_lexer_peek_token (parser->lexer);
12652 /* If it's an `(', then parse the attribute arguments. */
12653 if (token->type == CPP_OPEN_PAREN)
12654 {
12655 tree arguments;
a723baf1 12656
39703eb9
MM
12657 arguments = (cp_parser_parenthesized_expression_list
12658 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
12659 /* Save the identifier and arguments away. */
12660 TREE_VALUE (attribute) = arguments;
a723baf1
MM
12661 }
12662
12663 /* Add this attribute to the list. */
12664 TREE_CHAIN (attribute) = attribute_list;
12665 attribute_list = attribute;
12666
12667 /* Now, look for more attributes. */
12668 token = cp_lexer_peek_token (parser->lexer);
12669 /* If the next token isn't a `,', we're done. */
12670 if (token->type != CPP_COMMA)
12671 break;
12672
12673 /* Consume the commma and keep going. */
12674 cp_lexer_consume_token (parser->lexer);
12675 }
12676
12677 /* We built up the list in reverse order. */
12678 return nreverse (attribute_list);
12679}
12680
12681/* Parse an optional `__extension__' keyword. Returns TRUE if it is
12682 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12683 current value of the PEDANTIC flag, regardless of whether or not
12684 the `__extension__' keyword is present. The caller is responsible
12685 for restoring the value of the PEDANTIC flag. */
12686
12687static bool
94edc4ab 12688cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
12689{
12690 /* Save the old value of the PEDANTIC flag. */
12691 *saved_pedantic = pedantic;
12692
12693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12694 {
12695 /* Consume the `__extension__' token. */
12696 cp_lexer_consume_token (parser->lexer);
12697 /* We're not being pedantic while the `__extension__' keyword is
12698 in effect. */
12699 pedantic = 0;
12700
12701 return true;
12702 }
12703
12704 return false;
12705}
12706
12707/* Parse a label declaration.
12708
12709 label-declaration:
12710 __label__ label-declarator-seq ;
12711
12712 label-declarator-seq:
12713 identifier , label-declarator-seq
12714 identifier */
12715
12716static void
94edc4ab 12717cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
12718{
12719 /* Look for the `__label__' keyword. */
12720 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12721
12722 while (true)
12723 {
12724 tree identifier;
12725
12726 /* Look for an identifier. */
12727 identifier = cp_parser_identifier (parser);
12728 /* Declare it as a lobel. */
12729 finish_label_decl (identifier);
12730 /* If the next token is a `;', stop. */
12731 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12732 break;
12733 /* Look for the `,' separating the label declarations. */
12734 cp_parser_require (parser, CPP_COMMA, "`,'");
12735 }
12736
12737 /* Look for the final `;'. */
12738 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12739}
12740
12741/* Support Functions */
12742
12743/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12744 NAME should have one of the representations used for an
12745 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12746 is returned. If PARSER->SCOPE is a dependent type, then a
12747 SCOPE_REF is returned.
12748
12749 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12750 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12751 was formed. Abstractly, such entities should not be passed to this
12752 function, because they do not need to be looked up, but it is
12753 simpler to check for this special case here, rather than at the
12754 call-sites.
12755
12756 In cases not explicitly covered above, this function returns a
12757 DECL, OVERLOAD, or baselink representing the result of the lookup.
12758 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12759 is returned.
12760
a723baf1
MM
12761 If IS_TYPE is TRUE, bindings that do not refer to types are
12762 ignored.
12763
eea9800f
MM
12764 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12765 are ignored.
12766
a723baf1
MM
12767 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12768 types. */
12769
12770static tree
8d241e0b 12771cp_parser_lookup_name (cp_parser *parser, tree name,
eea9800f 12772 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
12773{
12774 tree decl;
12775 tree object_type = parser->context->object_type;
12776
12777 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12778 no longer valid. Note that if we are parsing tentatively, and
12779 the parse fails, OBJECT_TYPE will be automatically restored. */
12780 parser->context->object_type = NULL_TREE;
12781
12782 if (name == error_mark_node)
12783 return error_mark_node;
12784
12785 /* A template-id has already been resolved; there is no lookup to
12786 do. */
12787 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12788 return name;
12789 if (BASELINK_P (name))
12790 {
12791 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12792 == TEMPLATE_ID_EXPR),
12793 20020909);
12794 return name;
12795 }
12796
12797 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12798 it should already have been checked to make sure that the name
12799 used matches the type being destroyed. */
12800 if (TREE_CODE (name) == BIT_NOT_EXPR)
12801 {
12802 tree type;
12803
12804 /* Figure out to which type this destructor applies. */
12805 if (parser->scope)
12806 type = parser->scope;
12807 else if (object_type)
12808 type = object_type;
12809 else
12810 type = current_class_type;
12811 /* If that's not a class type, there is no destructor. */
12812 if (!type || !CLASS_TYPE_P (type))
12813 return error_mark_node;
12814 /* If it was a class type, return the destructor. */
12815 return CLASSTYPE_DESTRUCTORS (type);
12816 }
12817
12818 /* By this point, the NAME should be an ordinary identifier. If
12819 the id-expression was a qualified name, the qualifying scope is
12820 stored in PARSER->SCOPE at this point. */
12821 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12822 20000619);
12823
12824 /* Perform the lookup. */
12825 if (parser->scope)
12826 {
1fb3244a 12827 bool dependent_p;
a723baf1
MM
12828
12829 if (parser->scope == error_mark_node)
12830 return error_mark_node;
12831
12832 /* If the SCOPE is dependent, the lookup must be deferred until
12833 the template is instantiated -- unless we are explicitly
12834 looking up names in uninstantiated templates. Even then, we
12835 cannot look up the name if the scope is not a class type; it
12836 might, for example, be a template type parameter. */
1fb3244a
MM
12837 dependent_p = (TYPE_P (parser->scope)
12838 && !(parser->in_declarator_p
12839 && currently_open_class (parser->scope))
12840 && dependent_type_p (parser->scope));
a723baf1 12841 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 12842 && dependent_p)
a723baf1
MM
12843 {
12844 if (!is_type)
12845 decl = build_nt (SCOPE_REF, parser->scope, name);
12846 else
12847 /* The resolution to Core Issue 180 says that `struct A::B'
12848 should be considered a type-name, even if `A' is
12849 dependent. */
12850 decl = TYPE_NAME (make_typename_type (parser->scope,
12851 name,
12852 /*complain=*/1));
12853 }
12854 else
12855 {
12856 /* If PARSER->SCOPE is a dependent type, then it must be a
12857 class type, and we must not be checking dependencies;
12858 otherwise, we would have processed this lookup above. So
12859 that PARSER->SCOPE is not considered a dependent base by
12860 lookup_member, we must enter the scope here. */
1fb3244a 12861 if (dependent_p)
a723baf1
MM
12862 push_scope (parser->scope);
12863 /* If the PARSER->SCOPE is a a template specialization, it
12864 may be instantiated during name lookup. In that case,
12865 errors may be issued. Even if we rollback the current
12866 tentative parse, those errors are valid. */
5e08432e
MM
12867 decl = lookup_qualified_name (parser->scope, name, is_type,
12868 /*complain=*/true);
1fb3244a 12869 if (dependent_p)
a723baf1
MM
12870 pop_scope (parser->scope);
12871 }
12872 parser->qualifying_scope = parser->scope;
12873 parser->object_scope = NULL_TREE;
12874 }
12875 else if (object_type)
12876 {
12877 tree object_decl = NULL_TREE;
12878 /* Look up the name in the scope of the OBJECT_TYPE, unless the
12879 OBJECT_TYPE is not a class. */
12880 if (CLASS_TYPE_P (object_type))
12881 /* If the OBJECT_TYPE is a template specialization, it may
12882 be instantiated during name lookup. In that case, errors
12883 may be issued. Even if we rollback the current tentative
12884 parse, those errors are valid. */
12885 object_decl = lookup_member (object_type,
12886 name,
12887 /*protect=*/0, is_type);
12888 /* Look it up in the enclosing context, too. */
12889 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 12890 is_namespace,
a723baf1
MM
12891 /*flags=*/0);
12892 parser->object_scope = object_type;
12893 parser->qualifying_scope = NULL_TREE;
12894 if (object_decl)
12895 decl = object_decl;
12896 }
12897 else
12898 {
12899 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 12900 is_namespace,
a723baf1
MM
12901 /*flags=*/0);
12902 parser->qualifying_scope = NULL_TREE;
12903 parser->object_scope = NULL_TREE;
12904 }
12905
12906 /* If the lookup failed, let our caller know. */
12907 if (!decl
12908 || decl == error_mark_node
12909 || (TREE_CODE (decl) == FUNCTION_DECL
12910 && DECL_ANTICIPATED (decl)))
12911 return error_mark_node;
12912
12913 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
12914 if (TREE_CODE (decl) == TREE_LIST)
12915 {
12916 /* The error message we have to print is too complicated for
12917 cp_parser_error, so we incorporate its actions directly. */
e5976695 12918 if (!cp_parser_simulate_error (parser))
a723baf1
MM
12919 {
12920 error ("reference to `%D' is ambiguous", name);
12921 print_candidates (decl);
12922 }
12923 return error_mark_node;
12924 }
12925
12926 my_friendly_assert (DECL_P (decl)
12927 || TREE_CODE (decl) == OVERLOAD
12928 || TREE_CODE (decl) == SCOPE_REF
12929 || BASELINK_P (decl),
12930 20000619);
12931
12932 /* If we have resolved the name of a member declaration, check to
12933 see if the declaration is accessible. When the name resolves to
34cd5ae7 12934 set of overloaded functions, accessibility is checked when
a723baf1
MM
12935 overload resolution is done.
12936
12937 During an explicit instantiation, access is not checked at all,
12938 as per [temp.explicit]. */
8d241e0b 12939 if (DECL_P (decl))
ee76b931 12940 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
12941
12942 return decl;
12943}
12944
12945/* Like cp_parser_lookup_name, but for use in the typical case where
12946 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
12947 TRUE. */
12948
12949static tree
94edc4ab 12950cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
12951{
12952 return cp_parser_lookup_name (parser, name,
eea9800f
MM
12953 /*is_type=*/false,
12954 /*is_namespace=*/false,
a723baf1
MM
12955 /*check_dependency=*/true);
12956}
12957
a723baf1
MM
12958/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
12959 the current context, return the TYPE_DECL. If TAG_NAME_P is
12960 true, the DECL indicates the class being defined in a class-head,
12961 or declared in an elaborated-type-specifier.
12962
12963 Otherwise, return DECL. */
12964
12965static tree
12966cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
12967{
710b73e6
KL
12968 /* If the TEMPLATE_DECL is being declared as part of a class-head,
12969 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
12970
12971 struct A {
12972 template <typename T> struct B;
12973 };
12974
12975 template <typename T> struct A::B {};
12976
12977 Similarly, in a elaborated-type-specifier:
12978
12979 namespace N { struct X{}; }
12980
12981 struct A {
12982 template <typename T> friend struct N::X;
12983 };
12984
710b73e6
KL
12985 However, if the DECL refers to a class type, and we are in
12986 the scope of the class, then the name lookup automatically
12987 finds the TYPE_DECL created by build_self_reference rather
12988 than a TEMPLATE_DECL. For example, in:
12989
12990 template <class T> struct S {
12991 S s;
12992 };
12993
12994 there is no need to handle such case. */
12995
12996 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
12997 return DECL_TEMPLATE_RESULT (decl);
12998
12999 return decl;
13000}
13001
13002/* If too many, or too few, template-parameter lists apply to the
13003 declarator, issue an error message. Returns TRUE if all went well,
13004 and FALSE otherwise. */
13005
13006static bool
94edc4ab
NN
13007cp_parser_check_declarator_template_parameters (cp_parser* parser,
13008 tree declarator)
a723baf1
MM
13009{
13010 unsigned num_templates;
13011
13012 /* We haven't seen any classes that involve template parameters yet. */
13013 num_templates = 0;
13014
13015 switch (TREE_CODE (declarator))
13016 {
13017 case CALL_EXPR:
13018 case ARRAY_REF:
13019 case INDIRECT_REF:
13020 case ADDR_EXPR:
13021 {
13022 tree main_declarator = TREE_OPERAND (declarator, 0);
13023 return
13024 cp_parser_check_declarator_template_parameters (parser,
13025 main_declarator);
13026 }
13027
13028 case SCOPE_REF:
13029 {
13030 tree scope;
13031 tree member;
13032
13033 scope = TREE_OPERAND (declarator, 0);
13034 member = TREE_OPERAND (declarator, 1);
13035
13036 /* If this is a pointer-to-member, then we are not interested
13037 in the SCOPE, because it does not qualify the thing that is
13038 being declared. */
13039 if (TREE_CODE (member) == INDIRECT_REF)
13040 return (cp_parser_check_declarator_template_parameters
13041 (parser, member));
13042
13043 while (scope && CLASS_TYPE_P (scope))
13044 {
13045 /* You're supposed to have one `template <...>'
13046 for every template class, but you don't need one
13047 for a full specialization. For example:
13048
13049 template <class T> struct S{};
13050 template <> struct S<int> { void f(); };
13051 void S<int>::f () {}
13052
13053 is correct; there shouldn't be a `template <>' for
13054 the definition of `S<int>::f'. */
13055 if (CLASSTYPE_TEMPLATE_INFO (scope)
13056 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13057 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13058 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13059 ++num_templates;
13060
13061 scope = TYPE_CONTEXT (scope);
13062 }
13063 }
13064
13065 /* Fall through. */
13066
13067 default:
13068 /* If the DECLARATOR has the form `X<y>' then it uses one
13069 additional level of template parameters. */
13070 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13071 ++num_templates;
13072
13073 return cp_parser_check_template_parameters (parser,
13074 num_templates);
13075 }
13076}
13077
13078/* NUM_TEMPLATES were used in the current declaration. If that is
13079 invalid, return FALSE and issue an error messages. Otherwise,
13080 return TRUE. */
13081
13082static bool
94edc4ab
NN
13083cp_parser_check_template_parameters (cp_parser* parser,
13084 unsigned num_templates)
a723baf1
MM
13085{
13086 /* If there are more template classes than parameter lists, we have
13087 something like:
13088
13089 template <class T> void S<T>::R<T>::f (); */
13090 if (parser->num_template_parameter_lists < num_templates)
13091 {
13092 error ("too few template-parameter-lists");
13093 return false;
13094 }
13095 /* If there are the same number of template classes and parameter
13096 lists, that's OK. */
13097 if (parser->num_template_parameter_lists == num_templates)
13098 return true;
13099 /* If there are more, but only one more, then we are referring to a
13100 member template. That's OK too. */
13101 if (parser->num_template_parameter_lists == num_templates + 1)
13102 return true;
13103 /* Otherwise, there are too many template parameter lists. We have
13104 something like:
13105
13106 template <class T> template <class U> void S::f(); */
13107 error ("too many template-parameter-lists");
13108 return false;
13109}
13110
13111/* Parse a binary-expression of the general form:
13112
13113 binary-expression:
13114 <expr>
13115 binary-expression <token> <expr>
13116
13117 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13118 to parser the <expr>s. If the first production is used, then the
13119 value returned by FN is returned directly. Otherwise, a node with
13120 the indicated EXPR_TYPE is returned, with operands corresponding to
13121 the two sub-expressions. */
13122
13123static tree
94edc4ab
NN
13124cp_parser_binary_expression (cp_parser* parser,
13125 const cp_parser_token_tree_map token_tree_map,
13126 cp_parser_expression_fn fn)
a723baf1
MM
13127{
13128 tree lhs;
13129
13130 /* Parse the first expression. */
13131 lhs = (*fn) (parser);
13132 /* Now, look for more expressions. */
13133 while (true)
13134 {
13135 cp_token *token;
39b1af70 13136 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13137 tree rhs;
13138
13139 /* Peek at the next token. */
13140 token = cp_lexer_peek_token (parser->lexer);
13141 /* If the token is `>', and that's not an operator at the
13142 moment, then we're done. */
13143 if (token->type == CPP_GREATER
13144 && !parser->greater_than_is_operator_p)
13145 break;
34cd5ae7 13146 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13147 tree representation. */
13148 for (map_node = token_tree_map;
13149 map_node->token_type != CPP_EOF;
13150 ++map_node)
13151 if (map_node->token_type == token->type)
13152 {
13153 /* Consume the operator token. */
13154 cp_lexer_consume_token (parser->lexer);
13155 /* Parse the right-hand side of the expression. */
13156 rhs = (*fn) (parser);
13157 /* Build the binary tree node. */
13158 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13159 break;
13160 }
13161
13162 /* If the token wasn't one of the ones we want, we're done. */
13163 if (map_node->token_type == CPP_EOF)
13164 break;
13165 }
13166
13167 return lhs;
13168}
13169
13170/* Parse an optional `::' token indicating that the following name is
13171 from the global namespace. If so, PARSER->SCOPE is set to the
13172 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13173 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13174 Returns the new value of PARSER->SCOPE, if the `::' token is
13175 present, and NULL_TREE otherwise. */
13176
13177static tree
94edc4ab 13178cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13179{
13180 cp_token *token;
13181
13182 /* Peek at the next token. */
13183 token = cp_lexer_peek_token (parser->lexer);
13184 /* If we're looking at a `::' token then we're starting from the
13185 global namespace, not our current location. */
13186 if (token->type == CPP_SCOPE)
13187 {
13188 /* Consume the `::' token. */
13189 cp_lexer_consume_token (parser->lexer);
13190 /* Set the SCOPE so that we know where to start the lookup. */
13191 parser->scope = global_namespace;
13192 parser->qualifying_scope = global_namespace;
13193 parser->object_scope = NULL_TREE;
13194
13195 return parser->scope;
13196 }
13197 else if (!current_scope_valid_p)
13198 {
13199 parser->scope = NULL_TREE;
13200 parser->qualifying_scope = NULL_TREE;
13201 parser->object_scope = NULL_TREE;
13202 }
13203
13204 return NULL_TREE;
13205}
13206
13207/* Returns TRUE if the upcoming token sequence is the start of a
13208 constructor declarator. If FRIEND_P is true, the declarator is
13209 preceded by the `friend' specifier. */
13210
13211static bool
13212cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13213{
13214 bool constructor_p;
13215 tree type_decl = NULL_TREE;
13216 bool nested_name_p;
2050a1bb
MM
13217 cp_token *next_token;
13218
13219 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13220 try to avoid doing lots of work if at all possible. It's not
13221 valid declare a constructor at function scope. */
13222 if (at_function_scope_p ())
13223 return false;
13224 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13225 next_token = cp_lexer_peek_token (parser->lexer);
13226 if (next_token->type != CPP_NAME
13227 && next_token->type != CPP_SCOPE
13228 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13229 && next_token->type != CPP_TEMPLATE_ID)
13230 return false;
a723baf1
MM
13231
13232 /* Parse tentatively; we are going to roll back all of the tokens
13233 consumed here. */
13234 cp_parser_parse_tentatively (parser);
13235 /* Assume that we are looking at a constructor declarator. */
13236 constructor_p = true;
8d241e0b 13237
a723baf1
MM
13238 /* Look for the optional `::' operator. */
13239 cp_parser_global_scope_opt (parser,
13240 /*current_scope_valid_p=*/false);
13241 /* Look for the nested-name-specifier. */
13242 nested_name_p
13243 = (cp_parser_nested_name_specifier_opt (parser,
13244 /*typename_keyword_p=*/false,
13245 /*check_dependency_p=*/false,
13246 /*type_p=*/false)
13247 != NULL_TREE);
13248 /* Outside of a class-specifier, there must be a
13249 nested-name-specifier. */
13250 if (!nested_name_p &&
13251 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13252 || friend_p))
13253 constructor_p = false;
13254 /* If we still think that this might be a constructor-declarator,
13255 look for a class-name. */
13256 if (constructor_p)
13257 {
13258 /* If we have:
13259
8fbc5ae7 13260 template <typename T> struct S { S(); };
a723baf1
MM
13261 template <typename T> S<T>::S ();
13262
13263 we must recognize that the nested `S' names a class.
13264 Similarly, for:
13265
13266 template <typename T> S<T>::S<T> ();
13267
13268 we must recognize that the nested `S' names a template. */
13269 type_decl = cp_parser_class_name (parser,
13270 /*typename_keyword_p=*/false,
13271 /*template_keyword_p=*/false,
13272 /*type_p=*/false,
a723baf1
MM
13273 /*check_dependency_p=*/false,
13274 /*class_head_p=*/false);
13275 /* If there was no class-name, then this is not a constructor. */
13276 constructor_p = !cp_parser_error_occurred (parser);
13277 }
8d241e0b 13278
a723baf1
MM
13279 /* If we're still considering a constructor, we have to see a `(',
13280 to begin the parameter-declaration-clause, followed by either a
13281 `)', an `...', or a decl-specifier. We need to check for a
13282 type-specifier to avoid being fooled into thinking that:
13283
13284 S::S (f) (int);
13285
13286 is a constructor. (It is actually a function named `f' that
13287 takes one parameter (of type `int') and returns a value of type
13288 `S::S'. */
13289 if (constructor_p
13290 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13291 {
13292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13293 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13294 && !cp_parser_storage_class_specifier_opt (parser))
13295 {
5dae1114
MM
13296 tree type;
13297
13298 /* Names appearing in the type-specifier should be looked up
13299 in the scope of the class. */
13300 if (current_class_type)
13301 type = NULL_TREE;
a723baf1
MM
13302 else
13303 {
5dae1114
MM
13304 type = TREE_TYPE (type_decl);
13305 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13306 {
13307 type = resolve_typename_type (type,
13308 /*only_current_p=*/false);
13309 if (type == error_mark_node)
13310 {
13311 cp_parser_abort_tentative_parse (parser);
13312 return false;
13313 }
13314 }
5dae1114 13315 push_scope (type);
a723baf1 13316 }
5dae1114
MM
13317 /* Look for the type-specifier. */
13318 cp_parser_type_specifier (parser,
13319 CP_PARSER_FLAGS_NONE,
13320 /*is_friend=*/false,
13321 /*is_declarator=*/true,
13322 /*declares_class_or_enum=*/NULL,
13323 /*is_cv_qualifier=*/NULL);
13324 /* Leave the scope of the class. */
13325 if (type)
13326 pop_scope (type);
13327
13328 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13329 }
13330 }
13331 else
13332 constructor_p = false;
13333 /* We did not really want to consume any tokens. */
13334 cp_parser_abort_tentative_parse (parser);
13335
13336 return constructor_p;
13337}
13338
13339/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13340 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13341 they must be performed once we are in the scope of the function.
13342
13343 Returns the function defined. */
13344
13345static tree
13346cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13347 (cp_parser* parser,
13348 tree decl_specifiers,
13349 tree attributes,
13350 tree declarator)
a723baf1
MM
13351{
13352 tree fn;
13353 bool success_p;
13354
13355 /* Begin the function-definition. */
13356 success_p = begin_function_definition (decl_specifiers,
13357 attributes,
13358 declarator);
13359
13360 /* If there were names looked up in the decl-specifier-seq that we
13361 did not check, check them now. We must wait until we are in the
13362 scope of the function to perform the checks, since the function
13363 might be a friend. */
cf22909c 13364 perform_deferred_access_checks ();
a723baf1
MM
13365
13366 if (!success_p)
13367 {
13368 /* If begin_function_definition didn't like the definition, skip
13369 the entire function. */
13370 error ("invalid function declaration");
13371 cp_parser_skip_to_end_of_block_or_statement (parser);
13372 fn = error_mark_node;
13373 }
13374 else
13375 fn = cp_parser_function_definition_after_declarator (parser,
13376 /*inline_p=*/false);
13377
13378 return fn;
13379}
13380
13381/* Parse the part of a function-definition that follows the
13382 declarator. INLINE_P is TRUE iff this function is an inline
13383 function defined with a class-specifier.
13384
13385 Returns the function defined. */
13386
13387static tree
94edc4ab
NN
13388cp_parser_function_definition_after_declarator (cp_parser* parser,
13389 bool inline_p)
a723baf1
MM
13390{
13391 tree fn;
13392 bool ctor_initializer_p = false;
13393 bool saved_in_unbraced_linkage_specification_p;
13394 unsigned saved_num_template_parameter_lists;
13395
13396 /* If the next token is `return', then the code may be trying to
13397 make use of the "named return value" extension that G++ used to
13398 support. */
13399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13400 {
13401 /* Consume the `return' keyword. */
13402 cp_lexer_consume_token (parser->lexer);
13403 /* Look for the identifier that indicates what value is to be
13404 returned. */
13405 cp_parser_identifier (parser);
13406 /* Issue an error message. */
13407 error ("named return values are no longer supported");
13408 /* Skip tokens until we reach the start of the function body. */
13409 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13410 cp_lexer_consume_token (parser->lexer);
13411 }
13412 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13413 anything declared inside `f'. */
13414 saved_in_unbraced_linkage_specification_p
13415 = parser->in_unbraced_linkage_specification_p;
13416 parser->in_unbraced_linkage_specification_p = false;
13417 /* Inside the function, surrounding template-parameter-lists do not
13418 apply. */
13419 saved_num_template_parameter_lists
13420 = parser->num_template_parameter_lists;
13421 parser->num_template_parameter_lists = 0;
13422 /* If the next token is `try', then we are looking at a
13423 function-try-block. */
13424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13425 ctor_initializer_p = cp_parser_function_try_block (parser);
13426 /* A function-try-block includes the function-body, so we only do
13427 this next part if we're not processing a function-try-block. */
13428 else
13429 ctor_initializer_p
13430 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13431
13432 /* Finish the function. */
13433 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13434 (inline_p ? 2 : 0));
13435 /* Generate code for it, if necessary. */
8cd2462c 13436 expand_or_defer_fn (fn);
a723baf1
MM
13437 /* Restore the saved values. */
13438 parser->in_unbraced_linkage_specification_p
13439 = saved_in_unbraced_linkage_specification_p;
13440 parser->num_template_parameter_lists
13441 = saved_num_template_parameter_lists;
13442
13443 return fn;
13444}
13445
13446/* Parse a template-declaration, assuming that the `export' (and
13447 `extern') keywords, if present, has already been scanned. MEMBER_P
13448 is as for cp_parser_template_declaration. */
13449
13450static void
94edc4ab 13451cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13452{
13453 tree decl = NULL_TREE;
13454 tree parameter_list;
13455 bool friend_p = false;
13456
13457 /* Look for the `template' keyword. */
13458 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13459 return;
13460
13461 /* And the `<'. */
13462 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13463 return;
13464
13465 /* Parse the template parameters. */
13466 begin_template_parm_list ();
13467 /* If the next token is `>', then we have an invalid
13468 specialization. Rather than complain about an invalid template
13469 parameter, issue an error message here. */
13470 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13471 {
13472 cp_parser_error (parser, "invalid explicit specialization");
13473 parameter_list = NULL_TREE;
13474 }
13475 else
13476 parameter_list = cp_parser_template_parameter_list (parser);
13477 parameter_list = end_template_parm_list (parameter_list);
13478 /* Look for the `>'. */
13479 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13480 /* We just processed one more parameter list. */
13481 ++parser->num_template_parameter_lists;
13482 /* If the next token is `template', there are more template
13483 parameters. */
13484 if (cp_lexer_next_token_is_keyword (parser->lexer,
13485 RID_TEMPLATE))
13486 cp_parser_template_declaration_after_export (parser, member_p);
13487 else
13488 {
13489 decl = cp_parser_single_declaration (parser,
13490 member_p,
13491 &friend_p);
13492
13493 /* If this is a member template declaration, let the front
13494 end know. */
13495 if (member_p && !friend_p && decl)
13496 decl = finish_member_template_decl (decl);
13497 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13498 make_friend_class (current_class_type, TREE_TYPE (decl));
13499 }
13500 /* We are done with the current parameter list. */
13501 --parser->num_template_parameter_lists;
13502
13503 /* Finish up. */
13504 finish_template_decl (parameter_list);
13505
13506 /* Register member declarations. */
13507 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13508 finish_member_declaration (decl);
13509
13510 /* If DECL is a function template, we must return to parse it later.
13511 (Even though there is no definition, there might be default
13512 arguments that need handling.) */
13513 if (member_p && decl
13514 && (TREE_CODE (decl) == FUNCTION_DECL
13515 || DECL_FUNCTION_TEMPLATE_P (decl)))
13516 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 13517 = tree_cons (NULL_TREE, decl,
a723baf1
MM
13518 TREE_VALUE (parser->unparsed_functions_queues));
13519}
13520
13521/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13522 `function-definition' sequence. MEMBER_P is true, this declaration
13523 appears in a class scope.
13524
13525 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13526 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13527
13528static tree
94edc4ab
NN
13529cp_parser_single_declaration (cp_parser* parser,
13530 bool member_p,
13531 bool* friend_p)
a723baf1
MM
13532{
13533 bool declares_class_or_enum;
13534 tree decl = NULL_TREE;
13535 tree decl_specifiers;
13536 tree attributes;
a723baf1
MM
13537
13538 /* Parse the dependent declaration. We don't know yet
13539 whether it will be a function-definition. */
13540 cp_parser_parse_tentatively (parser);
13541 /* Defer access checks until we know what is being declared. */
8d241e0b 13542 push_deferring_access_checks (dk_deferred);
cf22909c 13543
a723baf1
MM
13544 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13545 alternative. */
13546 decl_specifiers
13547 = cp_parser_decl_specifier_seq (parser,
13548 CP_PARSER_FLAGS_OPTIONAL,
13549 &attributes,
13550 &declares_class_or_enum);
13551 /* Gather up the access checks that occurred the
13552 decl-specifier-seq. */
cf22909c
KL
13553 stop_deferring_access_checks ();
13554
a723baf1
MM
13555 /* Check for the declaration of a template class. */
13556 if (declares_class_or_enum)
13557 {
13558 if (cp_parser_declares_only_class_p (parser))
13559 {
13560 decl = shadow_tag (decl_specifiers);
13561 if (decl)
13562 decl = TYPE_NAME (decl);
13563 else
13564 decl = error_mark_node;
13565 }
13566 }
13567 else
13568 decl = NULL_TREE;
13569 /* If it's not a template class, try for a template function. If
13570 the next token is a `;', then this declaration does not declare
13571 anything. But, if there were errors in the decl-specifiers, then
13572 the error might well have come from an attempted class-specifier.
13573 In that case, there's no need to warn about a missing declarator. */
13574 if (!decl
13575 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13576 || !value_member (error_mark_node, decl_specifiers)))
13577 decl = cp_parser_init_declarator (parser,
13578 decl_specifiers,
13579 attributes,
a723baf1
MM
13580 /*function_definition_allowed_p=*/false,
13581 member_p,
13582 /*function_definition_p=*/NULL);
cf22909c
KL
13583
13584 pop_deferring_access_checks ();
13585
a723baf1
MM
13586 /* Clear any current qualification; whatever comes next is the start
13587 of something new. */
13588 parser->scope = NULL_TREE;
13589 parser->qualifying_scope = NULL_TREE;
13590 parser->object_scope = NULL_TREE;
13591 /* Look for a trailing `;' after the declaration. */
8a6393df 13592 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
a723baf1
MM
13593 && cp_parser_committed_to_tentative_parse (parser))
13594 cp_parser_skip_to_end_of_block_or_statement (parser);
13595 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13596 if (cp_parser_parse_definitely (parser))
13597 {
13598 if (friend_p)
13599 *friend_p = cp_parser_friend_p (decl_specifiers);
13600 }
13601 /* Otherwise, try a function-definition. */
13602 else
13603 decl = cp_parser_function_definition (parser, friend_p);
13604
13605 return decl;
13606}
13607
d6b4ea85
MM
13608/* Parse a cast-expression that is not the operand of a unary "&". */
13609
13610static tree
13611cp_parser_simple_cast_expression (cp_parser *parser)
13612{
13613 return cp_parser_cast_expression (parser, /*address_p=*/false);
13614}
13615
a723baf1
MM
13616/* Parse a functional cast to TYPE. Returns an expression
13617 representing the cast. */
13618
13619static tree
94edc4ab 13620cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
13621{
13622 tree expression_list;
13623
39703eb9
MM
13624 expression_list
13625 = cp_parser_parenthesized_expression_list (parser, false,
13626 /*non_constant_p=*/NULL);
a723baf1
MM
13627
13628 return build_functional_cast (type, expression_list);
13629}
13630
13631/* MEMBER_FUNCTION is a member function, or a friend. If default
13632 arguments, or the body of the function have not yet been parsed,
13633 parse them now. */
13634
13635static void
94edc4ab 13636cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
13637{
13638 cp_lexer *saved_lexer;
13639
13640 /* If this member is a template, get the underlying
13641 FUNCTION_DECL. */
13642 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13643 member_function = DECL_TEMPLATE_RESULT (member_function);
13644
13645 /* There should not be any class definitions in progress at this
13646 point; the bodies of members are only parsed outside of all class
13647 definitions. */
13648 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13649 /* While we're parsing the member functions we might encounter more
13650 classes. We want to handle them right away, but we don't want
13651 them getting mixed up with functions that are currently in the
13652 queue. */
13653 parser->unparsed_functions_queues
13654 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13655
13656 /* Make sure that any template parameters are in scope. */
13657 maybe_begin_member_template_processing (member_function);
13658
a723baf1
MM
13659 /* If the body of the function has not yet been parsed, parse it
13660 now. */
13661 if (DECL_PENDING_INLINE_P (member_function))
13662 {
13663 tree function_scope;
13664 cp_token_cache *tokens;
13665
13666 /* The function is no longer pending; we are processing it. */
13667 tokens = DECL_PENDING_INLINE_INFO (member_function);
13668 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13669 DECL_PENDING_INLINE_P (member_function) = 0;
13670 /* If this was an inline function in a local class, enter the scope
13671 of the containing function. */
13672 function_scope = decl_function_context (member_function);
13673 if (function_scope)
13674 push_function_context_to (function_scope);
13675
13676 /* Save away the current lexer. */
13677 saved_lexer = parser->lexer;
13678 /* Make a new lexer to feed us the tokens saved for this function. */
13679 parser->lexer = cp_lexer_new_from_tokens (tokens);
13680 parser->lexer->next = saved_lexer;
13681
13682 /* Set the current source position to be the location of the first
13683 token in the saved inline body. */
3466b292 13684 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13685
13686 /* Let the front end know that we going to be defining this
13687 function. */
13688 start_function (NULL_TREE, member_function, NULL_TREE,
13689 SF_PRE_PARSED | SF_INCLASS_INLINE);
13690
13691 /* Now, parse the body of the function. */
13692 cp_parser_function_definition_after_declarator (parser,
13693 /*inline_p=*/true);
13694
13695 /* Leave the scope of the containing function. */
13696 if (function_scope)
13697 pop_function_context_from (function_scope);
13698 /* Restore the lexer. */
13699 parser->lexer = saved_lexer;
13700 }
13701
13702 /* Remove any template parameters from the symbol table. */
13703 maybe_end_member_template_processing ();
13704
13705 /* Restore the queue. */
13706 parser->unparsed_functions_queues
13707 = TREE_CHAIN (parser->unparsed_functions_queues);
13708}
13709
8db1028e
NS
13710/* If DECL contains any default args, remeber it on the unparsed
13711 functions queue. */
13712
13713static void
13714cp_parser_save_default_args (cp_parser* parser, tree decl)
13715{
13716 tree probe;
13717
13718 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13719 probe;
13720 probe = TREE_CHAIN (probe))
13721 if (TREE_PURPOSE (probe))
13722 {
13723 TREE_PURPOSE (parser->unparsed_functions_queues)
13724 = tree_cons (NULL_TREE, decl,
13725 TREE_PURPOSE (parser->unparsed_functions_queues));
13726 break;
13727 }
13728 return;
13729}
13730
8218bd34
MM
13731/* FN is a FUNCTION_DECL which may contains a parameter with an
13732 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
13733
13734static void
8218bd34 13735cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
13736{
13737 cp_lexer *saved_lexer;
13738 cp_token_cache *tokens;
13739 bool saved_local_variables_forbidden_p;
13740 tree parameters;
8218bd34
MM
13741
13742 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
13743 parameters;
13744 parameters = TREE_CHAIN (parameters))
13745 {
13746 if (!TREE_PURPOSE (parameters)
13747 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13748 continue;
13749
13750 /* Save away the current lexer. */
13751 saved_lexer = parser->lexer;
13752 /* Create a new one, using the tokens we have saved. */
13753 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13754 parser->lexer = cp_lexer_new_from_tokens (tokens);
13755
13756 /* Set the current source position to be the location of the
13757 first token in the default argument. */
3466b292 13758 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
13759
13760 /* Local variable names (and the `this' keyword) may not appear
13761 in a default argument. */
13762 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13763 parser->local_variables_forbidden_p = true;
13764 /* Parse the assignment-expression. */
8218bd34 13765 if (DECL_CONTEXT (fn))
14d22dd6 13766 push_nested_class (DECL_CONTEXT (fn));
a723baf1 13767 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 13768 if (DECL_CONTEXT (fn))
e5976695 13769 pop_nested_class ();
a723baf1
MM
13770
13771 /* Restore saved state. */
13772 parser->lexer = saved_lexer;
13773 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13774 }
13775}
13776
13777/* Parse the operand of `sizeof' (or a similar operator). Returns
13778 either a TYPE or an expression, depending on the form of the
13779 input. The KEYWORD indicates which kind of expression we have
13780 encountered. */
13781
13782static tree
94edc4ab 13783cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
13784{
13785 static const char *format;
13786 tree expr = NULL_TREE;
13787 const char *saved_message;
13788 bool saved_constant_expression_p;
13789
13790 /* Initialize FORMAT the first time we get here. */
13791 if (!format)
13792 format = "types may not be defined in `%s' expressions";
13793
13794 /* Types cannot be defined in a `sizeof' expression. Save away the
13795 old message. */
13796 saved_message = parser->type_definition_forbidden_message;
13797 /* And create the new one. */
13798 parser->type_definition_forbidden_message
13799 = ((const char *)
13800 xmalloc (strlen (format)
13801 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13802 + 1 /* `\0' */));
13803 sprintf ((char *) parser->type_definition_forbidden_message,
13804 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13805
13806 /* The restrictions on constant-expressions do not apply inside
13807 sizeof expressions. */
13808 saved_constant_expression_p = parser->constant_expression_p;
13809 parser->constant_expression_p = false;
13810
3beb3abf
MM
13811 /* Do not actually evaluate the expression. */
13812 ++skip_evaluation;
a723baf1
MM
13813 /* If it's a `(', then we might be looking at the type-id
13814 construction. */
13815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13816 {
13817 tree type;
13818
13819 /* We can't be sure yet whether we're looking at a type-id or an
13820 expression. */
13821 cp_parser_parse_tentatively (parser);
13822 /* Consume the `('. */
13823 cp_lexer_consume_token (parser->lexer);
13824 /* Parse the type-id. */
13825 type = cp_parser_type_id (parser);
13826 /* Now, look for the trailing `)'. */
13827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13828 /* If all went well, then we're done. */
13829 if (cp_parser_parse_definitely (parser))
13830 {
13831 /* Build a list of decl-specifiers; right now, we have only
13832 a single type-specifier. */
13833 type = build_tree_list (NULL_TREE,
13834 type);
13835
13836 /* Call grokdeclarator to figure out what type this is. */
13837 expr = grokdeclarator (NULL_TREE,
13838 type,
13839 TYPENAME,
13840 /*initialized=*/0,
13841 /*attrlist=*/NULL);
13842 }
13843 }
13844
13845 /* If the type-id production did not work out, then we must be
13846 looking at the unary-expression production. */
13847 if (!expr)
13848 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
13849 /* Go back to evaluating expressions. */
13850 --skip_evaluation;
a723baf1
MM
13851
13852 /* Free the message we created. */
13853 free ((char *) parser->type_definition_forbidden_message);
13854 /* And restore the old one. */
13855 parser->type_definition_forbidden_message = saved_message;
13856 parser->constant_expression_p = saved_constant_expression_p;
13857
13858 return expr;
13859}
13860
13861/* If the current declaration has no declarator, return true. */
13862
13863static bool
13864cp_parser_declares_only_class_p (cp_parser *parser)
13865{
13866 /* If the next token is a `;' or a `,' then there is no
13867 declarator. */
13868 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13869 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13870}
13871
d17811fd
MM
13872/* Simplify EXPR if it is a non-dependent expression. Returns the
13873 (possibly simplified) expression. */
13874
13875static tree
13876cp_parser_fold_non_dependent_expr (tree expr)
13877{
13878 /* If we're in a template, but EXPR isn't value dependent, simplify
13879 it. We're supposed to treat:
13880
13881 template <typename T> void f(T[1 + 1]);
13882 template <typename T> void f(T[2]);
13883
13884 as two declarations of the same function, for example. */
13885 if (processing_template_decl
13886 && !type_dependent_expression_p (expr)
13887 && !value_dependent_expression_p (expr))
13888 {
13889 HOST_WIDE_INT saved_processing_template_decl;
13890
13891 saved_processing_template_decl = processing_template_decl;
13892 processing_template_decl = 0;
13893 expr = tsubst_copy_and_build (expr,
13894 /*args=*/NULL_TREE,
13895 tf_error,
b3445994
MM
13896 /*in_decl=*/NULL_TREE,
13897 /*function_p=*/false);
d17811fd
MM
13898 processing_template_decl = saved_processing_template_decl;
13899 }
13900 return expr;
13901}
13902
a723baf1
MM
13903/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
13904 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
13905
13906static bool
94edc4ab 13907cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
13908{
13909 while (decl_specifiers)
13910 {
13911 /* See if this decl-specifier is `friend'. */
13912 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
13913 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
13914 return true;
13915
13916 /* Go on to the next decl-specifier. */
13917 decl_specifiers = TREE_CHAIN (decl_specifiers);
13918 }
13919
13920 return false;
13921}
13922
13923/* If the next token is of the indicated TYPE, consume it. Otherwise,
13924 issue an error message indicating that TOKEN_DESC was expected.
13925
13926 Returns the token consumed, if the token had the appropriate type.
13927 Otherwise, returns NULL. */
13928
13929static cp_token *
94edc4ab
NN
13930cp_parser_require (cp_parser* parser,
13931 enum cpp_ttype type,
13932 const char* token_desc)
a723baf1
MM
13933{
13934 if (cp_lexer_next_token_is (parser->lexer, type))
13935 return cp_lexer_consume_token (parser->lexer);
13936 else
13937 {
e5976695
MM
13938 /* Output the MESSAGE -- unless we're parsing tentatively. */
13939 if (!cp_parser_simulate_error (parser))
13940 error ("expected %s", token_desc);
a723baf1
MM
13941 return NULL;
13942 }
13943}
13944
13945/* Like cp_parser_require, except that tokens will be skipped until
13946 the desired token is found. An error message is still produced if
13947 the next token is not as expected. */
13948
13949static void
94edc4ab
NN
13950cp_parser_skip_until_found (cp_parser* parser,
13951 enum cpp_ttype type,
13952 const char* token_desc)
a723baf1
MM
13953{
13954 cp_token *token;
13955 unsigned nesting_depth = 0;
13956
13957 if (cp_parser_require (parser, type, token_desc))
13958 return;
13959
13960 /* Skip tokens until the desired token is found. */
13961 while (true)
13962 {
13963 /* Peek at the next token. */
13964 token = cp_lexer_peek_token (parser->lexer);
13965 /* If we've reached the token we want, consume it and
13966 stop. */
13967 if (token->type == type && !nesting_depth)
13968 {
13969 cp_lexer_consume_token (parser->lexer);
13970 return;
13971 }
13972 /* If we've run out of tokens, stop. */
13973 if (token->type == CPP_EOF)
13974 return;
13975 if (token->type == CPP_OPEN_BRACE
13976 || token->type == CPP_OPEN_PAREN
13977 || token->type == CPP_OPEN_SQUARE)
13978 ++nesting_depth;
13979 else if (token->type == CPP_CLOSE_BRACE
13980 || token->type == CPP_CLOSE_PAREN
13981 || token->type == CPP_CLOSE_SQUARE)
13982 {
13983 if (nesting_depth-- == 0)
13984 return;
13985 }
13986 /* Consume this token. */
13987 cp_lexer_consume_token (parser->lexer);
13988 }
13989}
13990
13991/* If the next token is the indicated keyword, consume it. Otherwise,
13992 issue an error message indicating that TOKEN_DESC was expected.
13993
13994 Returns the token consumed, if the token had the appropriate type.
13995 Otherwise, returns NULL. */
13996
13997static cp_token *
94edc4ab
NN
13998cp_parser_require_keyword (cp_parser* parser,
13999 enum rid keyword,
14000 const char* token_desc)
a723baf1
MM
14001{
14002 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14003
14004 if (token && token->keyword != keyword)
14005 {
14006 dyn_string_t error_msg;
14007
14008 /* Format the error message. */
14009 error_msg = dyn_string_new (0);
14010 dyn_string_append_cstr (error_msg, "expected ");
14011 dyn_string_append_cstr (error_msg, token_desc);
14012 cp_parser_error (parser, error_msg->s);
14013 dyn_string_delete (error_msg);
14014 return NULL;
14015 }
14016
14017 return token;
14018}
14019
14020/* Returns TRUE iff TOKEN is a token that can begin the body of a
14021 function-definition. */
14022
14023static bool
94edc4ab 14024cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14025{
14026 return (/* An ordinary function-body begins with an `{'. */
14027 token->type == CPP_OPEN_BRACE
14028 /* A ctor-initializer begins with a `:'. */
14029 || token->type == CPP_COLON
14030 /* A function-try-block begins with `try'. */
14031 || token->keyword == RID_TRY
14032 /* The named return value extension begins with `return'. */
14033 || token->keyword == RID_RETURN);
14034}
14035
14036/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14037 definition. */
14038
14039static bool
14040cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14041{
14042 cp_token *token;
14043
14044 token = cp_lexer_peek_token (parser->lexer);
14045 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14046}
14047
d17811fd
MM
14048/* Returns TRUE iff the next token is the "," or ">" ending a
14049 template-argument. */
14050
14051static bool
14052cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14053{
14054 cp_token *token;
14055
14056 token = cp_lexer_peek_token (parser->lexer);
14057 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14058}
14059
a723baf1
MM
14060/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14061 or none_type otherwise. */
14062
14063static enum tag_types
94edc4ab 14064cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14065{
14066 switch (token->keyword)
14067 {
14068 case RID_CLASS:
14069 return class_type;
14070 case RID_STRUCT:
14071 return record_type;
14072 case RID_UNION:
14073 return union_type;
14074
14075 default:
14076 return none_type;
14077 }
14078}
14079
14080/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14081
14082static void
14083cp_parser_check_class_key (enum tag_types class_key, tree type)
14084{
14085 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14086 pedwarn ("`%s' tag used in naming `%#T'",
14087 class_key == union_type ? "union"
14088 : class_key == record_type ? "struct" : "class",
14089 type);
14090}
14091
14092/* Look for the `template' keyword, as a syntactic disambiguator.
14093 Return TRUE iff it is present, in which case it will be
14094 consumed. */
14095
14096static bool
14097cp_parser_optional_template_keyword (cp_parser *parser)
14098{
14099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14100 {
14101 /* The `template' keyword can only be used within templates;
14102 outside templates the parser can always figure out what is a
14103 template and what is not. */
14104 if (!processing_template_decl)
14105 {
14106 error ("`template' (as a disambiguator) is only allowed "
14107 "within templates");
14108 /* If this part of the token stream is rescanned, the same
14109 error message would be generated. So, we purge the token
14110 from the stream. */
14111 cp_lexer_purge_token (parser->lexer);
14112 return false;
14113 }
14114 else
14115 {
14116 /* Consume the `template' keyword. */
14117 cp_lexer_consume_token (parser->lexer);
14118 return true;
14119 }
14120 }
14121
14122 return false;
14123}
14124
2050a1bb
MM
14125/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14126 set PARSER->SCOPE, and perform other related actions. */
14127
14128static void
14129cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14130{
14131 tree value;
14132 tree check;
14133
14134 /* Get the stored value. */
14135 value = cp_lexer_consume_token (parser->lexer)->value;
14136 /* Perform any access checks that were deferred. */
14137 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14138 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14139 /* Set the scope from the stored value. */
14140 parser->scope = TREE_VALUE (value);
14141 parser->qualifying_scope = TREE_TYPE (value);
14142 parser->object_scope = NULL_TREE;
14143}
14144
a723baf1
MM
14145/* Add tokens to CACHE until an non-nested END token appears. */
14146
14147static void
14148cp_parser_cache_group (cp_parser *parser,
14149 cp_token_cache *cache,
14150 enum cpp_ttype end,
14151 unsigned depth)
14152{
14153 while (true)
14154 {
14155 cp_token *token;
14156
14157 /* Abort a parenthesized expression if we encounter a brace. */
14158 if ((end == CPP_CLOSE_PAREN || depth == 0)
14159 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14160 return;
14161 /* Consume the next token. */
14162 token = cp_lexer_consume_token (parser->lexer);
14163 /* If we've reached the end of the file, stop. */
14164 if (token->type == CPP_EOF)
14165 return;
14166 /* Add this token to the tokens we are saving. */
14167 cp_token_cache_push_token (cache, token);
14168 /* See if it starts a new group. */
14169 if (token->type == CPP_OPEN_BRACE)
14170 {
14171 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14172 if (depth == 0)
14173 return;
14174 }
14175 else if (token->type == CPP_OPEN_PAREN)
14176 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14177 else if (token->type == end)
14178 return;
14179 }
14180}
14181
14182/* Begin parsing tentatively. We always save tokens while parsing
14183 tentatively so that if the tentative parsing fails we can restore the
14184 tokens. */
14185
14186static void
94edc4ab 14187cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14188{
14189 /* Enter a new parsing context. */
14190 parser->context = cp_parser_context_new (parser->context);
14191 /* Begin saving tokens. */
14192 cp_lexer_save_tokens (parser->lexer);
14193 /* In order to avoid repetitive access control error messages,
14194 access checks are queued up until we are no longer parsing
14195 tentatively. */
8d241e0b 14196 push_deferring_access_checks (dk_deferred);
a723baf1
MM
14197}
14198
14199/* Commit to the currently active tentative parse. */
14200
14201static void
94edc4ab 14202cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14203{
14204 cp_parser_context *context;
14205 cp_lexer *lexer;
14206
14207 /* Mark all of the levels as committed. */
14208 lexer = parser->lexer;
14209 for (context = parser->context; context->next; context = context->next)
14210 {
14211 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14212 break;
14213 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14214 while (!cp_lexer_saving_tokens (lexer))
14215 lexer = lexer->next;
14216 cp_lexer_commit_tokens (lexer);
14217 }
14218}
14219
14220/* Abort the currently active tentative parse. All consumed tokens
14221 will be rolled back, and no diagnostics will be issued. */
14222
14223static void
94edc4ab 14224cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14225{
14226 cp_parser_simulate_error (parser);
14227 /* Now, pretend that we want to see if the construct was
14228 successfully parsed. */
14229 cp_parser_parse_definitely (parser);
14230}
14231
34cd5ae7 14232/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
14233 token stream. Otherwise, commit to the tokens we have consumed.
14234 Returns true if no error occurred; false otherwise. */
14235
14236static bool
94edc4ab 14237cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14238{
14239 bool error_occurred;
14240 cp_parser_context *context;
14241
34cd5ae7 14242 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
14243 destroy that information. */
14244 error_occurred = cp_parser_error_occurred (parser);
14245 /* Remove the topmost context from the stack. */
14246 context = parser->context;
14247 parser->context = context->next;
14248 /* If no parse errors occurred, commit to the tentative parse. */
14249 if (!error_occurred)
14250 {
14251 /* Commit to the tokens read tentatively, unless that was
14252 already done. */
14253 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14254 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14255
14256 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14257 }
14258 /* Otherwise, if errors occurred, roll back our state so that things
14259 are just as they were before we began the tentative parse. */
14260 else
cf22909c
KL
14261 {
14262 cp_lexer_rollback_tokens (parser->lexer);
14263 pop_deferring_access_checks ();
14264 }
e5976695
MM
14265 /* Add the context to the front of the free list. */
14266 context->next = cp_parser_context_free_list;
14267 cp_parser_context_free_list = context;
14268
14269 return !error_occurred;
a723baf1
MM
14270}
14271
a723baf1
MM
14272/* Returns true if we are parsing tentatively -- but have decided that
14273 we will stick with this tentative parse, even if errors occur. */
14274
14275static bool
94edc4ab 14276cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14277{
14278 return (cp_parser_parsing_tentatively (parser)
14279 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14280}
14281
4de8668e 14282/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
14283 tentative parse. */
14284
14285static bool
94edc4ab 14286cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14287{
14288 return (cp_parser_parsing_tentatively (parser)
14289 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14290}
14291
4de8668e 14292/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
14293
14294static bool
94edc4ab 14295cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14296{
14297 return parser->allow_gnu_extensions_p;
14298}
14299
14300\f
14301
14302/* The parser. */
14303
14304static GTY (()) cp_parser *the_parser;
14305
14306/* External interface. */
14307
d1bd0ded 14308/* Parse one entire translation unit. */
a723baf1 14309
d1bd0ded
GK
14310void
14311c_parse_file (void)
a723baf1
MM
14312{
14313 bool error_occurred;
14314
14315 the_parser = cp_parser_new ();
78757caa
KL
14316 push_deferring_access_checks (flag_access_control
14317 ? dk_no_deferred : dk_no_check);
a723baf1
MM
14318 error_occurred = cp_parser_translation_unit (the_parser);
14319 the_parser = NULL;
a723baf1
MM
14320}
14321
14322/* Clean up after parsing the entire translation unit. */
14323
14324void
94edc4ab 14325free_parser_stacks (void)
a723baf1
MM
14326{
14327 /* Nothing to do. */
14328}
14329
14330/* This variable must be provided by every front end. */
14331
14332int yydebug;
14333
14334#include "gt-cp-parser.h"