]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
stamp-h.in: Removed, not used anymore.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
3beb3abf 2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
a723baf1
MM
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
f5adbb8d 5 This file is part of GCC.
a723baf1 6
f5adbb8d 7 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
f5adbb8d 12 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
f5adbb8d 18 along with GCC; see the file COPYING. If not, write to the Free
a723baf1
MM
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "dyn-string.h"
27#include "varray.h"
28#include "cpplib.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "c-pragma.h"
32#include "decl.h"
33#include "flags.h"
34#include "diagnostic.h"
a723baf1
MM
35#include "toplev.h"
36#include "output.h"
37
38\f
39/* The lexer. */
40
41/* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
58
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
61
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
65
66/* A C++ token. */
67
68typedef struct cp_token GTY (())
69{
70 /* The kind of token. */
df2b750f 71 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
df2b750f 74 ENUM_BITFIELD (rid) keyword : 8;
522df488
DN
75 /* The value associated with this token, if any. */
76 tree value;
82a98427
NS
77 /* The location at which this token was found. */
78 location_t location;
a723baf1
MM
79} cp_token;
80
522df488
DN
81/* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
a723baf1 83
522df488 84#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
a723baf1
MM
85
86/* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
90 complete.)
91
92 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 93 variable-length array), is used due to constraints imposed by the
a723baf1
MM
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
96
97typedef struct cp_token_block GTY (())
98{
99 /* The tokens. */
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
102 size_t num_tokens;
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
107} cp_token_block;
108
109typedef struct cp_token_cache GTY (())
110{
111 /* The first block in the cache. NULL if there are no tokens in the
112 cache. */
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
115 cache. */
116 cp_token_block *last;
117} cp_token_cache;
118
9bcb9aae 119/* Prototypes. */
a723baf1
MM
120
121static cp_token_cache *cp_token_cache_new
122 (void);
123static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
125
126/* Create a new cp_token_cache. */
127
128static cp_token_cache *
bf9d3c27 129cp_token_cache_new (void)
a723baf1 130{
c68b0a84 131 return ggc_alloc_cleared (sizeof (cp_token_cache));
a723baf1
MM
132}
133
134/* Add *TOKEN to *CACHE. */
135
136static void
137cp_token_cache_push_token (cp_token_cache *cache,
138 cp_token *token)
139{
140 cp_token_block *b = cache->last;
141
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144 {
c68b0a84 145 b = ggc_alloc_cleared (sizeof (cp_token_block));
a723baf1
MM
146 b->prev = cache->last;
147 if (cache->last)
148 {
149 cache->last->next = b;
150 cache->last = b;
151 }
152 else
153 cache->first = cache->last = b;
154 }
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
157}
158
159/* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
161 it to the parser. */
162
163typedef struct cp_lexer GTY (())
164{
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
181
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
186
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
191
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
196
197 /* The STRING_CST tokens encountered while processing the current
198 string literal. */
199 varray_type string_tokens;
200
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
203 bool main_lexer_p;
204
205 /* True if we should output debugging information. */
206 bool debugging_p;
207
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
210} cp_lexer;
211
212/* Prototypes. */
213
17211ab5 214static cp_lexer *cp_lexer_new_main
94edc4ab 215 (void);
a723baf1 216static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 217 (struct cp_token_cache *);
a723baf1 218static int cp_lexer_saving_tokens
94edc4ab 219 (const cp_lexer *);
a723baf1 220static cp_token *cp_lexer_next_token
94edc4ab 221 (cp_lexer *, cp_token *);
a668c6ad
MM
222static cp_token *cp_lexer_prev_token
223 (cp_lexer *, cp_token *);
94edc4ab
NN
224static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer *, cp_token *, cp_token *);
a723baf1 226static cp_token *cp_lexer_read_token
94edc4ab 227 (cp_lexer *);
a723baf1 228static void cp_lexer_maybe_grow_buffer
94edc4ab 229 (cp_lexer *);
a723baf1 230static void cp_lexer_get_preprocessor_token
94edc4ab 231 (cp_lexer *, cp_token *);
a723baf1 232static cp_token *cp_lexer_peek_token
94edc4ab 233 (cp_lexer *);
a723baf1 234static cp_token *cp_lexer_peek_nth_token
94edc4ab 235 (cp_lexer *, size_t);
f7b5ecd9 236static inline bool cp_lexer_next_token_is
94edc4ab 237 (cp_lexer *, enum cpp_ttype);
a723baf1 238static bool cp_lexer_next_token_is_not
94edc4ab 239 (cp_lexer *, enum cpp_ttype);
a723baf1 240static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
241 (cp_lexer *, enum rid);
242static cp_token *cp_lexer_consume_token
243 (cp_lexer *);
a723baf1
MM
244static void cp_lexer_purge_token
245 (cp_lexer *);
246static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248static void cp_lexer_save_tokens
94edc4ab 249 (cp_lexer *);
a723baf1 250static void cp_lexer_commit_tokens
94edc4ab 251 (cp_lexer *);
a723baf1 252static void cp_lexer_rollback_tokens
94edc4ab 253 (cp_lexer *);
f7b5ecd9 254static inline void cp_lexer_set_source_position_from_token
94edc4ab 255 (cp_lexer *, const cp_token *);
a723baf1 256static void cp_lexer_print_token
94edc4ab 257 (FILE *, cp_token *);
f7b5ecd9 258static inline bool cp_lexer_debugging_p
94edc4ab 259 (cp_lexer *);
a723baf1 260static void cp_lexer_start_debugging
94edc4ab 261 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 262static void cp_lexer_stop_debugging
94edc4ab 263 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
264
265/* Manifest constants. */
266
267#define CP_TOKEN_BUFFER_SIZE 5
268#define CP_SAVED_TOKENS_SIZE 5
269
270/* A token type for keywords, as opposed to ordinary identifiers. */
271#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273/* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279/* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286/* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290/* Variables. */
291
292/* The stream to which debugging output should be written. */
293static FILE *cp_lexer_debug_stream;
294
17211ab5
GK
295/* Create a new main C++ lexer, the lexer that gets tokens from the
296 preprocessor. */
a723baf1
MM
297
298static cp_lexer *
17211ab5 299cp_lexer_new_main (void)
a723baf1
MM
300{
301 cp_lexer *lexer;
17211ab5
GK
302 cp_token first_token;
303
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 308 c_common_no_more_pch ();
a723baf1
MM
309
310 /* Allocate the memory. */
c68b0a84 311 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
312
313 /* Create the circular buffer. */
c68b0a84 314 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
17211ab5
GK
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
322
323 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 324 lexer->main_lexer_p = true;
a723baf1
MM
325
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
334
335 return lexer;
336}
337
338/* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
340
341static cp_lexer *
342cp_lexer_new_from_tokens (cp_token_cache *tokens)
343{
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
348
17211ab5 349 /* Allocate the memory. */
c68b0a84 350 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
351
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
c68b0a84 356 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1
MM
357 lexer->buffer_end = lexer->buffer + num_tokens;
358
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
362 {
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
365 }
366
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
373
17211ab5
GK
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
376
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
385
a723baf1
MM
386 return lexer;
387}
388
4de8668e 389/* Returns nonzero if debugging information should be output. */
a723baf1 390
f7b5ecd9
MM
391static inline bool
392cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 393{
f7b5ecd9
MM
394 return lexer->debugging_p;
395}
396
397/* Set the current source position from the information stored in
398 TOKEN. */
399
400static inline void
94edc4ab
NN
401cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
f7b5ecd9
MM
403{
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
406
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
82a98427 409 input_location = token->location;
a723baf1
MM
410}
411
412/* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
414
f7b5ecd9 415static inline cp_token *
94edc4ab 416cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
417{
418 token++;
419 if (token == lexer->buffer_end)
420 token = lexer->buffer;
421 return token;
422}
423
a668c6ad
MM
424/* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
426
427static inline cp_token *
428cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
429{
430 if (token == lexer->buffer)
431 token = lexer->buffer_end;
432 return token - 1;
433}
434
4de8668e 435/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
436
437static int
94edc4ab 438cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
439{
440 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
441}
442
a723baf1
MM
443/* Return a pointer to the token that is N tokens beyond TOKEN in the
444 buffer. */
445
446static cp_token *
447cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
448{
449 token += n;
450 if (token >= lexer->buffer_end)
451 token = lexer->buffer + (token - lexer->buffer_end);
452 return token;
453}
454
455/* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
457
458static ptrdiff_t
94edc4ab 459cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
460{
461 if (finish >= start)
462 return finish - start;
463 else
464 return ((lexer->buffer_end - lexer->buffer)
465 - (start - finish));
466}
467
468/* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
470
471static cp_token *
94edc4ab 472cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
473{
474 cp_token *token;
475
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
478
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
485
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
488
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
491
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
494
e6cc3a24
ZW
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
497 constant elements.
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500 && flag_const_strings)
a723baf1 501 {
e6cc3a24
ZW
502 tree type;
503
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type = TREE_TYPE (token->value);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509 /* Reset the type of the token. */
510 TREE_TYPE (token->value) = type;
a723baf1
MM
511 }
512
513 return token;
514}
515
516/* If the circular buffer is full, make it bigger. */
517
518static void
94edc4ab 519cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
520{
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
523 {
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
529
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
532 value. */
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
c68b0a84
KG
537 new_buffer = ggc_realloc (lexer->buffer,
538 2 * buffer_length * sizeof (cp_token));
a723baf1
MM
539
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
544 buffer. */
545 num_tokens_to_copy = (lexer->first_token - old_buffer);
546 memcpy (new_buffer + buffer_length,
547 new_buffer,
548 num_tokens_to_copy * sizeof (cp_token));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
552 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
553
554 /* Now recompute all of the buffer pointers. */
555 new_first_token
556 = new_buffer + (lexer->first_token - old_buffer);
557 if (lexer->next_token != NULL)
558 {
559 ptrdiff_t next_token_delta;
560
561 if (lexer->next_token > lexer->first_token)
562 next_token_delta = lexer->next_token - lexer->first_token;
563 else
564 next_token_delta =
565 buffer_length - (lexer->first_token - lexer->next_token);
566 lexer->next_token = new_first_token + next_token_delta;
567 }
568 lexer->last_token = new_first_token + buffer_length;
569 lexer->buffer = new_buffer;
570 lexer->buffer_end = new_buffer + buffer_length * 2;
571 lexer->first_token = new_first_token;
572 }
573}
574
575/* Store the next token from the preprocessor in *TOKEN. */
576
577static void
94edc4ab
NN
578cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
579 cp_token *token)
a723baf1
MM
580{
581 bool done;
582
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 584 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
585 {
586 token->type = CPP_EOF;
82a98427
NS
587 token->location.line = 0;
588 token->location.file = NULL;
a723baf1
MM
589 token->value = NULL_TREE;
590 token->keyword = RID_MAX;
591
592 return;
593 }
594
595 done = false;
596 /* Keep going until we get a token we like. */
597 while (!done)
598 {
599 /* Get a new token from the preprocessor. */
600 token->type = c_lex (&token->value);
601 /* Issue messages about tokens we cannot process. */
602 switch (token->type)
603 {
604 case CPP_ATSIGN:
605 case CPP_HASH:
606 case CPP_PASTE:
607 error ("invalid token");
608 break;
609
a723baf1
MM
610 default:
611 /* This is a good token, so we exit the loop. */
612 done = true;
613 break;
614 }
615 }
616 /* Now we've got our token. */
82a98427 617 token->location = input_location;
a723baf1
MM
618
619 /* Check to see if this token is a keyword. */
620 if (token->type == CPP_NAME
621 && C_IS_RESERVED_WORD (token->value))
622 {
623 /* Mark this token as a keyword. */
624 token->type = CPP_KEYWORD;
625 /* Record which keyword. */
626 token->keyword = C_RID_CODE (token->value);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token->value = ridpointers[token->keyword];
632 }
633 else
634 token->keyword = RID_MAX;
635}
636
637/* Return a pointer to the next token in the token stream, but do not
638 consume it. */
639
640static cp_token *
94edc4ab 641cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
642{
643 cp_token *token;
644
645 /* If there are no tokens, read one now. */
646 if (!lexer->next_token)
647 cp_lexer_read_token (lexer);
648
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
651 {
652 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654 fprintf (cp_lexer_debug_stream, "\n");
655 }
656
657 token = lexer->next_token;
658 cp_lexer_set_source_position_from_token (lexer, token);
659 return token;
660}
661
662/* Return true if the next token has the indicated TYPE. */
663
664static bool
94edc4ab 665cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
666{
667 cp_token *token;
668
669 /* Peek at the next token. */
670 token = cp_lexer_peek_token (lexer);
671 /* Check to see if it has the indicated TYPE. */
672 return token->type == type;
673}
674
675/* Return true if the next token does not have the indicated TYPE. */
676
677static bool
94edc4ab 678cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
679{
680 return !cp_lexer_next_token_is (lexer, type);
681}
682
683/* Return true if the next token is the indicated KEYWORD. */
684
685static bool
94edc4ab 686cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
687{
688 cp_token *token;
689
690 /* Peek at the next token. */
691 token = cp_lexer_peek_token (lexer);
692 /* Check to see if it is the indicated keyword. */
693 return token->keyword == keyword;
694}
695
696/* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
698
699static cp_token *
94edc4ab 700cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
701{
702 cp_token *token;
703
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n > 0, 20000224);
706
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token = lexer->next_token;
709 /* If there are no tokens in the buffer, get one now. */
710 if (!token)
711 {
712 cp_lexer_read_token (lexer);
713 token = lexer->next_token;
714 }
715
716 /* Now, read tokens until we have enough. */
717 while (--n > 0)
718 {
719 /* Advance to the next token. */
720 token = cp_lexer_next_token (lexer, token);
721 /* If that's all the tokens we have, read a new one. */
722 if (token == lexer->last_token)
723 token = cp_lexer_read_token (lexer);
724 }
725
726 return token;
727}
728
729/* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
732 time. */
733
734static cp_token *
94edc4ab 735cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
736{
737 cp_token *token;
738
739 /* If there are no tokens, read one now. */
740 if (!lexer->next_token)
741 cp_lexer_read_token (lexer);
742
743 /* Remember the token we'll be returning. */
744 token = lexer->next_token;
745
746 /* Increment NEXT_TOKEN. */
747 lexer->next_token = cp_lexer_next_token (lexer,
748 lexer->next_token);
749 /* Check to see if we're all out of tokens. */
750 if (lexer->next_token == lexer->last_token)
751 lexer->next_token = NULL;
752
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer))
755 {
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer->next_token)
758 lexer->first_token = NULL;
759 else
760 lexer->first_token = lexer->next_token;
761 }
762
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer))
765 {
766 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream, token);
768 fprintf (cp_lexer_debug_stream, "\n");
769 }
770
771 return token;
772}
773
774/* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
777
778static void
779cp_lexer_purge_token (cp_lexer *lexer)
780{
781 cp_token *token;
782 cp_token *next_token;
783
784 token = lexer->next_token;
785 while (true)
786 {
787 next_token = cp_lexer_next_token (lexer, token);
788 if (next_token == lexer->last_token)
789 break;
790 *token = *next_token;
791 token = next_token;
792 }
793
794 lexer->last_token = token;
795 /* The token purged may have been the only token remaining; if so,
796 clear NEXT_TOKEN. */
797 if (lexer->next_token == token)
798 lexer->next_token = NULL;
799}
800
801/* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
804
805static void
806cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
807{
808 cp_token *peek;
809 cp_token *t1;
810 cp_token *t2;
811
812 if (lexer->next_token)
813 {
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1 = cp_lexer_next_token (lexer, token);
817 t2 = peek = cp_lexer_peek_token (lexer);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2 != lexer->last_token)
820 {
821 *t1 = *t2;
822 t1 = cp_lexer_next_token (lexer, t1);
823 t2 = cp_lexer_next_token (lexer, t2);
824 }
825 /* Now, the next available token is right after TOKEN. */
826 lexer->next_token = cp_lexer_next_token (lexer, token);
827 /* And the last token is wherever we ended up. */
828 lexer->last_token = t1;
829 }
830 else
831 {
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer->last_token = cp_lexer_next_token (lexer, token);
835 }
836}
837
838/* Begin saving tokens. All tokens consumed after this point will be
839 preserved. */
840
841static void
94edc4ab 842cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
843{
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer))
846 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
847
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer->next_token)
851 cp_lexer_read_token (lexer);
852
853 VARRAY_PUSH_INT (lexer->saved_tokens,
854 cp_lexer_token_difference (lexer,
855 lexer->first_token,
856 lexer->next_token));
857}
858
859/* Commit to the portion of the token stream most recently saved. */
860
861static void
94edc4ab 862cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
863{
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer))
866 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
867
868 VARRAY_POP (lexer->saved_tokens);
869}
870
871/* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
873
874static void
94edc4ab 875cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
876{
877 size_t delta;
878
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer))
881 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
882
883 /* Find the token that was the NEXT_TOKEN when we started saving
884 tokens. */
885 delta = VARRAY_TOP_INT(lexer->saved_tokens);
886 /* Make it the next token again now. */
887 lexer->next_token = cp_lexer_advance_token (lexer,
888 lexer->first_token,
889 delta);
15d2cb19 890 /* It might be the case that there were no tokens when we started
a723baf1
MM
891 saving tokens, but that there are some tokens now. */
892 if (!lexer->next_token && lexer->first_token)
893 lexer->next_token = lexer->first_token;
894
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer->saved_tokens);
897}
898
a723baf1
MM
899/* Print a representation of the TOKEN on the STREAM. */
900
901static void
94edc4ab 902cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
903{
904 const char *token_type = NULL;
905
906 /* Figure out what kind of token this is. */
907 switch (token->type)
908 {
909 case CPP_EQ:
910 token_type = "EQ";
911 break;
912
913 case CPP_COMMA:
914 token_type = "COMMA";
915 break;
916
917 case CPP_OPEN_PAREN:
918 token_type = "OPEN_PAREN";
919 break;
920
921 case CPP_CLOSE_PAREN:
922 token_type = "CLOSE_PAREN";
923 break;
924
925 case CPP_OPEN_BRACE:
926 token_type = "OPEN_BRACE";
927 break;
928
929 case CPP_CLOSE_BRACE:
930 token_type = "CLOSE_BRACE";
931 break;
932
933 case CPP_SEMICOLON:
934 token_type = "SEMICOLON";
935 break;
936
937 case CPP_NAME:
938 token_type = "NAME";
939 break;
940
941 case CPP_EOF:
942 token_type = "EOF";
943 break;
944
945 case CPP_KEYWORD:
946 token_type = "keyword";
947 break;
948
949 /* This is not a token that we know how to handle yet. */
950 default:
951 break;
952 }
953
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
956 if (token_type)
957 fprintf (stream, "%s", token_type);
958 else
959 fprintf (stream, "%d", token->type);
960 /* And, for an identifier, print the identifier name. */
961 if (token->type == CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token->type == CPP_KEYWORD
965 && TREE_CODE (token->value) == IDENTIFIER_NODE))
966 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
967}
968
a723baf1
MM
969/* Start emitting debugging information. */
970
971static void
94edc4ab 972cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
973{
974 ++lexer->debugging_p;
975}
976
977/* Stop emitting debugging information. */
978
979static void
94edc4ab 980cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
981{
982 --lexer->debugging_p;
983}
984
985\f
986/* The parser. */
987
988/* Overview
989 --------
990
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
998
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1002
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1011
1012 Methodology
1013 -----------
1014
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1031
1032 Future Improvements
1033 -------------------
1034
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1037
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1042
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1047
1048/* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1050
1051typedef enum cp_parser_flags
1052{
1053 /* No flags. */
1054 CP_PARSER_FLAGS_NONE = 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1060} cp_parser_flags;
1061
62b8a44e
NS
1062/* The different kinds of declarators we want to parse. */
1063
1064typedef enum cp_parser_declarator_kind
1065{
9bcb9aae 1066 /* We want an abstract declartor. */
62b8a44e
NS
1067 CP_PARSER_DECLARATOR_ABSTRACT,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED,
04c06002 1070 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1071 CP_PARSER_DECLARATOR_EITHER
1072} cp_parser_declarator_kind;
1073
a723baf1
MM
1074/* A mapping from a token type to a corresponding tree node type. */
1075
1076typedef struct cp_parser_token_tree_map_node
1077{
1078 /* The token type. */
df2b750f 1079 ENUM_BITFIELD (cpp_ttype) token_type : 8;
a723baf1 1080 /* The corresponding tree code. */
df2b750f 1081 ENUM_BITFIELD (tree_code) tree_type : 8;
a723baf1
MM
1082} cp_parser_token_tree_map_node;
1083
1084/* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1086
1087typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1088
1089/* The status of a tentative parse. */
1090
1091typedef enum cp_parser_status_kind
1092{
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1098 has occurred. */
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100} cp_parser_status_kind;
1101
1102/* Context that is saved and restored when parsing tentatively. */
1103
1104typedef struct cp_parser_context GTY (())
1105{
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
a723baf1
MM
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116} cp_parser_context;
1117
1118/* Prototypes. */
1119
1120/* Constructors and destructors. */
1121
1122static cp_parser_context *cp_parser_context_new
94edc4ab 1123 (cp_parser_context *);
a723baf1 1124
e5976695
MM
1125/* Class variables. */
1126
92bc1323 1127static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1128
a723baf1
MM
1129/* Constructors and destructors. */
1130
1131/* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1133
1134static cp_parser_context *
94edc4ab 1135cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1136{
1137 cp_parser_context *context;
1138
1139 /* Allocate the storage. */
e5976695
MM
1140 if (cp_parser_context_free_list != NULL)
1141 {
1142 /* Pull the first entry from the free list. */
1143 context = cp_parser_context_free_list;
1144 cp_parser_context_free_list = context->next;
c68b0a84 1145 memset (context, 0, sizeof (*context));
e5976695
MM
1146 }
1147 else
c68b0a84 1148 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1149 /* No errors have occurred yet in this context. */
1150 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1153 if (next)
1154 {
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context->object_type = next->object_type;
a723baf1
MM
1158 /* Thread the stack. */
1159 context->next = next;
1160 }
1161
1162 return context;
1163}
1164
1165/* The cp_parser structure represents the C++ parser. */
1166
1167typedef struct cp_parser GTY(())
1168{
1169 /* The lexer from which we are obtaining tokens. */
1170 cp_lexer *lexer;
1171
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1176
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1184 tree scope;
1185
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1191 tree object_scope;
1192 tree qualifying_scope;
1193
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1196
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context *context;
1204
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p;
1208
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p;
1213
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1216 them permissible. */
a723baf1
MM
1217 bool default_arg_ok_p;
1218
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
a723baf1
MM
1221 bool constant_expression_p;
1222
14d22dd6
MM
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
1227 bool allow_non_constant_expression_p;
1228
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
1231 bool non_constant_expression_p;
1232
263ee052
MM
1233 /* TRUE if we are parsing the argument to "__offsetof__". */
1234 bool in_offsetof_p;
1235
a723baf1
MM
1236 /* TRUE if local variable names and `this' are forbidden in the
1237 current context. */
1238 bool local_variables_forbidden_p;
1239
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1242 declaration'. */
1243 bool in_unbraced_linkage_specification_p;
1244
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p;
1248
4bb8ca28
MM
1249 /* TRUE if we are presently parsing a template-argument-list. */
1250 bool in_template_argument_list_p;
1251
0e59b3fb
MM
1252 /* TRUE if we are presently parsing the body of an
1253 iteration-statement. */
1254 bool in_iteration_statement_p;
1255
1256 /* TRUE if we are presently parsing the body of a switch
1257 statement. */
1258 bool in_switch_statement_p;
1259
a723baf1
MM
1260 /* If non-NULL, then we are parsing a construct where new type
1261 definitions are not permitted. The string stored here will be
1262 issued as an error message if a type is defined. */
1263 const char *type_definition_forbidden_message;
1264
8db1028e
NS
1265 /* A list of lists. The outer list is a stack, used for member
1266 functions of local classes. At each level there are two sub-list,
1267 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1268 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1269 TREE_VALUE's. The functions are chained in reverse declaration
1270 order.
1271
1272 The TREE_PURPOSE sublist contains those functions with default
1273 arguments that need post processing, and the TREE_VALUE sublist
1274 contains those functions with definitions that need post
1275 processing.
1276
1277 These lists can only be processed once the outermost class being
9bcb9aae 1278 defined is complete. */
a723baf1
MM
1279 tree unparsed_functions_queues;
1280
1281 /* The number of classes whose definitions are currently in
1282 progress. */
1283 unsigned num_classes_being_defined;
1284
1285 /* The number of template parameter lists that apply directly to the
1286 current declaration. */
1287 unsigned num_template_parameter_lists;
1288} cp_parser;
1289
04c06002 1290/* The type of a function that parses some kind of expression. */
94edc4ab 1291typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1292
1293/* Prototypes. */
1294
1295/* Constructors and destructors. */
1296
1297static cp_parser *cp_parser_new
94edc4ab 1298 (void);
a723baf1
MM
1299
1300/* Routines to parse various constructs.
1301
1302 Those that return `tree' will return the error_mark_node (rather
1303 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1304 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1305 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1306 whether or not a parse error occurred, you should always use
1307 cp_parser_error_occurred. If the construct is optional (indicated
1308 either by an `_opt' in the name of the function that does the
1309 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1310 the construct is not present. */
1311
1312/* Lexical conventions [gram.lex] */
1313
1314static tree cp_parser_identifier
94edc4ab 1315 (cp_parser *);
a723baf1
MM
1316
1317/* Basic concepts [gram.basic] */
1318
1319static bool cp_parser_translation_unit
94edc4ab 1320 (cp_parser *);
a723baf1
MM
1321
1322/* Expressions [gram.expr] */
1323
1324static tree cp_parser_primary_expression
b3445994 1325 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1326static tree cp_parser_id_expression
f3c2dfc6 1327 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1328static tree cp_parser_unqualified_id
f3c2dfc6 1329 (cp_parser *, bool, bool, bool);
a723baf1 1330static tree cp_parser_nested_name_specifier_opt
a668c6ad 1331 (cp_parser *, bool, bool, bool, bool);
a723baf1 1332static tree cp_parser_nested_name_specifier
a723baf1 1333 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1334static tree cp_parser_class_or_namespace_name
1335 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1336static tree cp_parser_postfix_expression
1337 (cp_parser *, bool);
7efa3e22 1338static tree cp_parser_parenthesized_expression_list
39703eb9 1339 (cp_parser *, bool, bool *);
a723baf1 1340static void cp_parser_pseudo_destructor_name
94edc4ab 1341 (cp_parser *, tree *, tree *);
a723baf1
MM
1342static tree cp_parser_unary_expression
1343 (cp_parser *, bool);
1344static enum tree_code cp_parser_unary_operator
94edc4ab 1345 (cp_token *);
a723baf1 1346static tree cp_parser_new_expression
94edc4ab 1347 (cp_parser *);
a723baf1 1348static tree cp_parser_new_placement
94edc4ab 1349 (cp_parser *);
a723baf1 1350static tree cp_parser_new_type_id
94edc4ab 1351 (cp_parser *);
a723baf1 1352static tree cp_parser_new_declarator_opt
94edc4ab 1353 (cp_parser *);
a723baf1 1354static tree cp_parser_direct_new_declarator
94edc4ab 1355 (cp_parser *);
a723baf1 1356static tree cp_parser_new_initializer
94edc4ab 1357 (cp_parser *);
a723baf1 1358static tree cp_parser_delete_expression
94edc4ab 1359 (cp_parser *);
a723baf1
MM
1360static tree cp_parser_cast_expression
1361 (cp_parser *, bool);
1362static tree cp_parser_pm_expression
94edc4ab 1363 (cp_parser *);
a723baf1 1364static tree cp_parser_multiplicative_expression
94edc4ab 1365 (cp_parser *);
a723baf1 1366static tree cp_parser_additive_expression
94edc4ab 1367 (cp_parser *);
a723baf1 1368static tree cp_parser_shift_expression
94edc4ab 1369 (cp_parser *);
a723baf1 1370static tree cp_parser_relational_expression
94edc4ab 1371 (cp_parser *);
a723baf1 1372static tree cp_parser_equality_expression
94edc4ab 1373 (cp_parser *);
a723baf1 1374static tree cp_parser_and_expression
94edc4ab 1375 (cp_parser *);
a723baf1 1376static tree cp_parser_exclusive_or_expression
94edc4ab 1377 (cp_parser *);
a723baf1 1378static tree cp_parser_inclusive_or_expression
94edc4ab 1379 (cp_parser *);
a723baf1 1380static tree cp_parser_logical_and_expression
94edc4ab 1381 (cp_parser *);
a723baf1 1382static tree cp_parser_logical_or_expression
94edc4ab 1383 (cp_parser *);
a723baf1 1384static tree cp_parser_question_colon_clause
94edc4ab 1385 (cp_parser *, tree);
a723baf1 1386static tree cp_parser_assignment_expression
94edc4ab 1387 (cp_parser *);
a723baf1 1388static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1389 (cp_parser *);
a723baf1 1390static tree cp_parser_expression
94edc4ab 1391 (cp_parser *);
a723baf1 1392static tree cp_parser_constant_expression
14d22dd6 1393 (cp_parser *, bool, bool *);
a723baf1
MM
1394
1395/* Statements [gram.stmt.stmt] */
1396
1397static void cp_parser_statement
a5bcc582 1398 (cp_parser *, bool);
a723baf1 1399static tree cp_parser_labeled_statement
a5bcc582 1400 (cp_parser *, bool);
a723baf1 1401static tree cp_parser_expression_statement
a5bcc582 1402 (cp_parser *, bool);
a723baf1 1403static tree cp_parser_compound_statement
a5bcc582 1404 (cp_parser *, bool);
a723baf1 1405static void cp_parser_statement_seq_opt
a5bcc582 1406 (cp_parser *, bool);
a723baf1 1407static tree cp_parser_selection_statement
94edc4ab 1408 (cp_parser *);
a723baf1 1409static tree cp_parser_condition
94edc4ab 1410 (cp_parser *);
a723baf1 1411static tree cp_parser_iteration_statement
94edc4ab 1412 (cp_parser *);
a723baf1 1413static void cp_parser_for_init_statement
94edc4ab 1414 (cp_parser *);
a723baf1 1415static tree cp_parser_jump_statement
94edc4ab 1416 (cp_parser *);
a723baf1 1417static void cp_parser_declaration_statement
94edc4ab 1418 (cp_parser *);
a723baf1
MM
1419
1420static tree cp_parser_implicitly_scoped_statement
94edc4ab 1421 (cp_parser *);
a723baf1 1422static void cp_parser_already_scoped_statement
94edc4ab 1423 (cp_parser *);
a723baf1
MM
1424
1425/* Declarations [gram.dcl.dcl] */
1426
1427static void cp_parser_declaration_seq_opt
94edc4ab 1428 (cp_parser *);
a723baf1 1429static void cp_parser_declaration
94edc4ab 1430 (cp_parser *);
a723baf1 1431static void cp_parser_block_declaration
94edc4ab 1432 (cp_parser *, bool);
a723baf1 1433static void cp_parser_simple_declaration
94edc4ab 1434 (cp_parser *, bool);
a723baf1 1435static tree cp_parser_decl_specifier_seq
560ad596 1436 (cp_parser *, cp_parser_flags, tree *, int *);
a723baf1 1437static tree cp_parser_storage_class_specifier_opt
94edc4ab 1438 (cp_parser *);
a723baf1 1439static tree cp_parser_function_specifier_opt
94edc4ab 1440 (cp_parser *);
a723baf1 1441static tree cp_parser_type_specifier
560ad596 1442 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
a723baf1 1443static tree cp_parser_simple_type_specifier
4b0d3cbe 1444 (cp_parser *, cp_parser_flags, bool);
a723baf1 1445static tree cp_parser_type_name
94edc4ab 1446 (cp_parser *);
a723baf1 1447static tree cp_parser_elaborated_type_specifier
94edc4ab 1448 (cp_parser *, bool, bool);
a723baf1 1449static tree cp_parser_enum_specifier
94edc4ab 1450 (cp_parser *);
a723baf1 1451static void cp_parser_enumerator_list
94edc4ab 1452 (cp_parser *, tree);
a723baf1 1453static void cp_parser_enumerator_definition
94edc4ab 1454 (cp_parser *, tree);
a723baf1 1455static tree cp_parser_namespace_name
94edc4ab 1456 (cp_parser *);
a723baf1 1457static void cp_parser_namespace_definition
94edc4ab 1458 (cp_parser *);
a723baf1 1459static void cp_parser_namespace_body
94edc4ab 1460 (cp_parser *);
a723baf1 1461static tree cp_parser_qualified_namespace_specifier
94edc4ab 1462 (cp_parser *);
a723baf1 1463static void cp_parser_namespace_alias_definition
94edc4ab 1464 (cp_parser *);
a723baf1 1465static void cp_parser_using_declaration
94edc4ab 1466 (cp_parser *);
a723baf1 1467static void cp_parser_using_directive
94edc4ab 1468 (cp_parser *);
a723baf1 1469static void cp_parser_asm_definition
94edc4ab 1470 (cp_parser *);
a723baf1 1471static void cp_parser_linkage_specification
94edc4ab 1472 (cp_parser *);
a723baf1
MM
1473
1474/* Declarators [gram.dcl.decl] */
1475
1476static tree cp_parser_init_declarator
560ad596 1477 (cp_parser *, tree, tree, bool, bool, int, bool *);
a723baf1 1478static tree cp_parser_declarator
4bb8ca28 1479 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
a723baf1 1480static tree cp_parser_direct_declarator
7efa3e22 1481 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1482static enum tree_code cp_parser_ptr_operator
94edc4ab 1483 (cp_parser *, tree *, tree *);
a723baf1 1484static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1485 (cp_parser *);
a723baf1 1486static tree cp_parser_cv_qualifier_opt
94edc4ab 1487 (cp_parser *);
a723baf1 1488static tree cp_parser_declarator_id
94edc4ab 1489 (cp_parser *);
a723baf1 1490static tree cp_parser_type_id
94edc4ab 1491 (cp_parser *);
a723baf1 1492static tree cp_parser_type_specifier_seq
94edc4ab 1493 (cp_parser *);
a723baf1 1494static tree cp_parser_parameter_declaration_clause
94edc4ab 1495 (cp_parser *);
a723baf1 1496static tree cp_parser_parameter_declaration_list
94edc4ab 1497 (cp_parser *);
a723baf1 1498static tree cp_parser_parameter_declaration
4bb8ca28 1499 (cp_parser *, bool, bool *);
a723baf1
MM
1500static void cp_parser_function_body
1501 (cp_parser *);
1502static tree cp_parser_initializer
39703eb9 1503 (cp_parser *, bool *, bool *);
a723baf1 1504static tree cp_parser_initializer_clause
39703eb9 1505 (cp_parser *, bool *);
a723baf1 1506static tree cp_parser_initializer_list
39703eb9 1507 (cp_parser *, bool *);
a723baf1
MM
1508
1509static bool cp_parser_ctor_initializer_opt_and_function_body
1510 (cp_parser *);
1511
1512/* Classes [gram.class] */
1513
1514static tree cp_parser_class_name
a668c6ad 1515 (cp_parser *, bool, bool, bool, bool, bool, bool);
a723baf1 1516static tree cp_parser_class_specifier
94edc4ab 1517 (cp_parser *);
a723baf1 1518static tree cp_parser_class_head
94edc4ab 1519 (cp_parser *, bool *);
a723baf1 1520static enum tag_types cp_parser_class_key
94edc4ab 1521 (cp_parser *);
a723baf1 1522static void cp_parser_member_specification_opt
94edc4ab 1523 (cp_parser *);
a723baf1 1524static void cp_parser_member_declaration
94edc4ab 1525 (cp_parser *);
a723baf1 1526static tree cp_parser_pure_specifier
94edc4ab 1527 (cp_parser *);
a723baf1 1528static tree cp_parser_constant_initializer
94edc4ab 1529 (cp_parser *);
a723baf1
MM
1530
1531/* Derived classes [gram.class.derived] */
1532
1533static tree cp_parser_base_clause
94edc4ab 1534 (cp_parser *);
a723baf1 1535static tree cp_parser_base_specifier
94edc4ab 1536 (cp_parser *);
a723baf1
MM
1537
1538/* Special member functions [gram.special] */
1539
1540static tree cp_parser_conversion_function_id
94edc4ab 1541 (cp_parser *);
a723baf1 1542static tree cp_parser_conversion_type_id
94edc4ab 1543 (cp_parser *);
a723baf1 1544static tree cp_parser_conversion_declarator_opt
94edc4ab 1545 (cp_parser *);
a723baf1 1546static bool cp_parser_ctor_initializer_opt
94edc4ab 1547 (cp_parser *);
a723baf1 1548static void cp_parser_mem_initializer_list
94edc4ab 1549 (cp_parser *);
a723baf1 1550static tree cp_parser_mem_initializer
94edc4ab 1551 (cp_parser *);
a723baf1 1552static tree cp_parser_mem_initializer_id
94edc4ab 1553 (cp_parser *);
a723baf1
MM
1554
1555/* Overloading [gram.over] */
1556
1557static tree cp_parser_operator_function_id
94edc4ab 1558 (cp_parser *);
a723baf1 1559static tree cp_parser_operator
94edc4ab 1560 (cp_parser *);
a723baf1
MM
1561
1562/* Templates [gram.temp] */
1563
1564static void cp_parser_template_declaration
94edc4ab 1565 (cp_parser *, bool);
a723baf1 1566static tree cp_parser_template_parameter_list
94edc4ab 1567 (cp_parser *);
a723baf1 1568static tree cp_parser_template_parameter
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_type_parameter
94edc4ab 1571 (cp_parser *);
a723baf1 1572static tree cp_parser_template_id
a668c6ad 1573 (cp_parser *, bool, bool, bool);
a723baf1 1574static tree cp_parser_template_name
a668c6ad 1575 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1576static tree cp_parser_template_argument_list
94edc4ab 1577 (cp_parser *);
a723baf1 1578static tree cp_parser_template_argument
94edc4ab 1579 (cp_parser *);
a723baf1 1580static void cp_parser_explicit_instantiation
94edc4ab 1581 (cp_parser *);
a723baf1 1582static void cp_parser_explicit_specialization
94edc4ab 1583 (cp_parser *);
a723baf1
MM
1584
1585/* Exception handling [gram.exception] */
1586
1587static tree cp_parser_try_block
94edc4ab 1588 (cp_parser *);
a723baf1 1589static bool cp_parser_function_try_block
94edc4ab 1590 (cp_parser *);
a723baf1 1591static void cp_parser_handler_seq
94edc4ab 1592 (cp_parser *);
a723baf1 1593static void cp_parser_handler
94edc4ab 1594 (cp_parser *);
a723baf1 1595static tree cp_parser_exception_declaration
94edc4ab 1596 (cp_parser *);
a723baf1 1597static tree cp_parser_throw_expression
94edc4ab 1598 (cp_parser *);
a723baf1 1599static tree cp_parser_exception_specification_opt
94edc4ab 1600 (cp_parser *);
a723baf1 1601static tree cp_parser_type_id_list
94edc4ab 1602 (cp_parser *);
a723baf1
MM
1603
1604/* GNU Extensions */
1605
1606static tree cp_parser_asm_specification_opt
94edc4ab 1607 (cp_parser *);
a723baf1 1608static tree cp_parser_asm_operand_list
94edc4ab 1609 (cp_parser *);
a723baf1 1610static tree cp_parser_asm_clobber_list
94edc4ab 1611 (cp_parser *);
a723baf1 1612static tree cp_parser_attributes_opt
94edc4ab 1613 (cp_parser *);
a723baf1 1614static tree cp_parser_attribute_list
94edc4ab 1615 (cp_parser *);
a723baf1 1616static bool cp_parser_extension_opt
94edc4ab 1617 (cp_parser *, int *);
a723baf1 1618static void cp_parser_label_declaration
94edc4ab 1619 (cp_parser *);
a723baf1
MM
1620
1621/* Utility Routines */
1622
1623static tree cp_parser_lookup_name
8d241e0b 1624 (cp_parser *, tree, bool, bool, bool);
a723baf1 1625static tree cp_parser_lookup_name_simple
94edc4ab 1626 (cp_parser *, tree);
a723baf1
MM
1627static tree cp_parser_maybe_treat_template_as_class
1628 (tree, bool);
1629static bool cp_parser_check_declarator_template_parameters
94edc4ab 1630 (cp_parser *, tree);
a723baf1 1631static bool cp_parser_check_template_parameters
94edc4ab 1632 (cp_parser *, unsigned);
d6b4ea85
MM
1633static tree cp_parser_simple_cast_expression
1634 (cp_parser *);
a723baf1 1635static tree cp_parser_binary_expression
94edc4ab 1636 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1637static tree cp_parser_global_scope_opt
94edc4ab 1638 (cp_parser *, bool);
a723baf1
MM
1639static bool cp_parser_constructor_declarator_p
1640 (cp_parser *, bool);
1641static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1642 (cp_parser *, tree, tree, tree);
a723baf1 1643static tree cp_parser_function_definition_after_declarator
94edc4ab 1644 (cp_parser *, bool);
a723baf1 1645static void cp_parser_template_declaration_after_export
94edc4ab 1646 (cp_parser *, bool);
a723baf1 1647static tree cp_parser_single_declaration
94edc4ab 1648 (cp_parser *, bool, bool *);
a723baf1 1649static tree cp_parser_functional_cast
94edc4ab 1650 (cp_parser *, tree);
4bb8ca28
MM
1651static tree cp_parser_save_member_function_body
1652 (cp_parser *, tree, tree, tree);
ec75414f
MM
1653static tree cp_parser_enclosed_template_argument_list
1654 (cp_parser *);
8db1028e
NS
1655static void cp_parser_save_default_args
1656 (cp_parser *, tree);
a723baf1 1657static void cp_parser_late_parsing_for_member
94edc4ab 1658 (cp_parser *, tree);
a723baf1 1659static void cp_parser_late_parsing_default_args
8218bd34 1660 (cp_parser *, tree);
a723baf1 1661static tree cp_parser_sizeof_operand
94edc4ab 1662 (cp_parser *, enum rid);
a723baf1 1663static bool cp_parser_declares_only_class_p
94edc4ab 1664 (cp_parser *);
d17811fd
MM
1665static tree cp_parser_fold_non_dependent_expr
1666 (tree);
a723baf1 1667static bool cp_parser_friend_p
94edc4ab 1668 (tree);
a723baf1 1669static cp_token *cp_parser_require
94edc4ab 1670 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1671static cp_token *cp_parser_require_keyword
94edc4ab 1672 (cp_parser *, enum rid, const char *);
a723baf1 1673static bool cp_parser_token_starts_function_definition_p
94edc4ab 1674 (cp_token *);
a723baf1
MM
1675static bool cp_parser_next_token_starts_class_definition_p
1676 (cp_parser *);
d17811fd
MM
1677static bool cp_parser_next_token_ends_template_argument_p
1678 (cp_parser *);
a723baf1 1679static enum tag_types cp_parser_token_is_class_key
94edc4ab 1680 (cp_token *);
a723baf1
MM
1681static void cp_parser_check_class_key
1682 (enum tag_types, tree type);
37d407a1
KL
1683static void cp_parser_check_access_in_redeclaration
1684 (tree type);
a723baf1
MM
1685static bool cp_parser_optional_template_keyword
1686 (cp_parser *);
2050a1bb
MM
1687static void cp_parser_pre_parsed_nested_name_specifier
1688 (cp_parser *);
a723baf1
MM
1689static void cp_parser_cache_group
1690 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1691static void cp_parser_parse_tentatively
94edc4ab 1692 (cp_parser *);
a723baf1 1693static void cp_parser_commit_to_tentative_parse
94edc4ab 1694 (cp_parser *);
a723baf1 1695static void cp_parser_abort_tentative_parse
94edc4ab 1696 (cp_parser *);
a723baf1 1697static bool cp_parser_parse_definitely
94edc4ab 1698 (cp_parser *);
f7b5ecd9 1699static inline bool cp_parser_parsing_tentatively
94edc4ab 1700 (cp_parser *);
a723baf1 1701static bool cp_parser_committed_to_tentative_parse
94edc4ab 1702 (cp_parser *);
a723baf1 1703static void cp_parser_error
94edc4ab 1704 (cp_parser *, const char *);
4bb8ca28
MM
1705static void cp_parser_name_lookup_error
1706 (cp_parser *, tree, tree, const char *);
e5976695 1707static bool cp_parser_simulate_error
94edc4ab 1708 (cp_parser *);
a723baf1 1709static void cp_parser_check_type_definition
94edc4ab 1710 (cp_parser *);
560ad596
MM
1711static void cp_parser_check_for_definition_in_return_type
1712 (tree, int);
ee43dab5
MM
1713static void cp_parser_check_for_invalid_template_id
1714 (cp_parser *, tree);
14d22dd6
MM
1715static tree cp_parser_non_constant_expression
1716 (const char *);
8fbc5ae7
MM
1717static bool cp_parser_diagnose_invalid_type_name
1718 (cp_parser *);
7efa3e22 1719static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1720 (cp_parser *, bool, bool, bool);
a723baf1 1721static void cp_parser_skip_to_end_of_statement
94edc4ab 1722 (cp_parser *);
e0860732
MM
1723static void cp_parser_consume_semicolon_at_end_of_statement
1724 (cp_parser *);
a723baf1 1725static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1726 (cp_parser *);
a723baf1
MM
1727static void cp_parser_skip_to_closing_brace
1728 (cp_parser *);
1729static void cp_parser_skip_until_found
94edc4ab 1730 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1731static bool cp_parser_error_occurred
94edc4ab 1732 (cp_parser *);
a723baf1 1733static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1734 (cp_parser *);
a723baf1 1735static bool cp_parser_is_string_literal
94edc4ab 1736 (cp_token *);
a723baf1 1737static bool cp_parser_is_keyword
94edc4ab 1738 (cp_token *, enum rid);
a723baf1 1739
4de8668e 1740/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1741
1742static inline bool
94edc4ab 1743cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1744{
1745 return parser->context->next != NULL;
1746}
1747
4de8668e 1748/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1749
1750static bool
94edc4ab 1751cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1752{
1753 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1754}
1755
4de8668e 1756/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1757
1758static bool
94edc4ab 1759cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1760{
1761 return token->keyword == keyword;
1762}
1763
a723baf1
MM
1764/* Issue the indicated error MESSAGE. */
1765
1766static void
94edc4ab 1767cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1768{
a723baf1 1769 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1770 if (!cp_parser_simulate_error (parser))
4bb8ca28
MM
1771 {
1772 cp_token *token;
1773 token = cp_lexer_peek_token (parser->lexer);
1774 c_parse_error (message, token->type, token->value);
1775 }
1776}
1777
1778/* Issue an error about name-lookup failing. NAME is the
1779 IDENTIFIER_NODE DECL is the result of
1780 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1781 the thing that we hoped to find. */
1782
1783static void
1784cp_parser_name_lookup_error (cp_parser* parser,
1785 tree name,
1786 tree decl,
1787 const char* desired)
1788{
1789 /* If name lookup completely failed, tell the user that NAME was not
1790 declared. */
1791 if (decl == error_mark_node)
1792 {
1793 if (parser->scope && parser->scope != global_namespace)
1794 error ("`%D::%D' has not been declared",
1795 parser->scope, name);
1796 else if (parser->scope == global_namespace)
1797 error ("`::%D' has not been declared", name);
1798 else
1799 error ("`%D' has not been declared", name);
1800 }
1801 else if (parser->scope && parser->scope != global_namespace)
1802 error ("`%D::%D' %s", parser->scope, name, desired);
1803 else if (parser->scope == global_namespace)
1804 error ("`::%D' %s", name, desired);
1805 else
1806 error ("`%D' %s", name, desired);
a723baf1
MM
1807}
1808
1809/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1810 during this tentative parse. Returns true if the error was
1811 simulated; false if a messgae should be issued by the caller. */
a723baf1 1812
e5976695 1813static bool
94edc4ab 1814cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1815{
1816 if (cp_parser_parsing_tentatively (parser)
1817 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1818 {
1819 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1820 return true;
1821 }
1822 return false;
a723baf1
MM
1823}
1824
1825/* This function is called when a type is defined. If type
1826 definitions are forbidden at this point, an error message is
1827 issued. */
1828
1829static void
94edc4ab 1830cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1831{
1832 /* If types are forbidden here, issue a message. */
1833 if (parser->type_definition_forbidden_message)
1834 /* Use `%s' to print the string in case there are any escape
1835 characters in the message. */
1836 error ("%s", parser->type_definition_forbidden_message);
1837}
1838
560ad596
MM
1839/* This function is called when a declaration is parsed. If
1840 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1841 indicates that a type was defined in the decl-specifiers for DECL,
1842 then an error is issued. */
1843
1844static void
1845cp_parser_check_for_definition_in_return_type (tree declarator,
1846 int declares_class_or_enum)
1847{
1848 /* [dcl.fct] forbids type definitions in return types.
1849 Unfortunately, it's not easy to know whether or not we are
1850 processing a return type until after the fact. */
1851 while (declarator
1852 && (TREE_CODE (declarator) == INDIRECT_REF
1853 || TREE_CODE (declarator) == ADDR_EXPR))
1854 declarator = TREE_OPERAND (declarator, 0);
1855 if (declarator
1856 && TREE_CODE (declarator) == CALL_EXPR
1857 && declares_class_or_enum & 2)
1858 error ("new types may not be defined in a return type");
1859}
1860
ee43dab5
MM
1861/* A type-specifier (TYPE) has been parsed which cannot be followed by
1862 "<" in any valid C++ program. If the next token is indeed "<",
1863 issue a message warning the user about what appears to be an
1864 invalid attempt to form a template-id. */
1865
1866static void
1867cp_parser_check_for_invalid_template_id (cp_parser* parser,
1868 tree type)
1869{
1870 ptrdiff_t start;
1871 cp_token *token;
1872
1873 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1874 {
1875 if (TYPE_P (type))
1876 error ("`%T' is not a template", type);
1877 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1878 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1879 else
1880 error ("invalid template-id");
1881 /* Remember the location of the invalid "<". */
1882 if (cp_parser_parsing_tentatively (parser)
1883 && !cp_parser_committed_to_tentative_parse (parser))
1884 {
1885 token = cp_lexer_peek_token (parser->lexer);
1886 token = cp_lexer_prev_token (parser->lexer, token);
1887 start = cp_lexer_token_difference (parser->lexer,
1888 parser->lexer->first_token,
1889 token);
1890 }
1891 else
1892 start = -1;
1893 /* Consume the "<". */
1894 cp_lexer_consume_token (parser->lexer);
1895 /* Parse the template arguments. */
1896 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1897 /* Permanently remove the invalid template arguments so that
ee43dab5
MM
1898 this error message is not issued again. */
1899 if (start >= 0)
1900 {
1901 token = cp_lexer_advance_token (parser->lexer,
1902 parser->lexer->first_token,
1903 start);
1904 cp_lexer_purge_tokens_after (parser->lexer, token);
1905 }
1906 }
1907}
1908
cd0be382 1909/* Issue an error message about the fact that THING appeared in a
14d22dd6
MM
1910 constant-expression. Returns ERROR_MARK_NODE. */
1911
1912static tree
1913cp_parser_non_constant_expression (const char *thing)
1914{
1915 error ("%s cannot appear in a constant-expression", thing);
1916 return error_mark_node;
1917}
1918
8fbc5ae7
MM
1919/* Check for a common situation where a type-name should be present,
1920 but is not, and issue a sensible error message. Returns true if an
1921 invalid type-name was detected. */
1922
1923static bool
1924cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1925{
1926 /* If the next two tokens are both identifiers, the code is
1927 erroneous. The usual cause of this situation is code like:
1928
1929 T t;
1930
1931 where "T" should name a type -- but does not. */
1932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1933 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1934 {
1935 tree name;
1936
8d241e0b 1937 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1938 looking at a declaration. */
1939 /* Consume the first identifier. */
1940 name = cp_lexer_consume_token (parser->lexer)->value;
1941 /* Issue an error message. */
1942 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1943 /* If we're in a template class, it's possible that the user was
1944 referring to a type from a base class. For example:
1945
1946 template <typename T> struct A { typedef T X; };
1947 template <typename T> struct B : public A<T> { X x; };
1948
1949 The user should have said "typename A<T>::X". */
1950 if (processing_template_decl && current_class_type)
1951 {
1952 tree b;
1953
1954 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1955 b;
1956 b = TREE_CHAIN (b))
1957 {
1958 tree base_type = BINFO_TYPE (b);
1959 if (CLASS_TYPE_P (base_type)
1fb3244a 1960 && dependent_type_p (base_type))
8fbc5ae7
MM
1961 {
1962 tree field;
1963 /* Go from a particular instantiation of the
1964 template (which will have an empty TYPE_FIELDs),
1965 to the main version. */
353b4fc0 1966 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1967 for (field = TYPE_FIELDS (base_type);
1968 field;
1969 field = TREE_CHAIN (field))
1970 if (TREE_CODE (field) == TYPE_DECL
1971 && DECL_NAME (field) == name)
1972 {
1973 error ("(perhaps `typename %T::%s' was intended)",
1974 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1975 break;
1976 }
1977 if (field)
1978 break;
1979 }
1980 }
1981 }
1982 /* Skip to the end of the declaration; there's no point in
1983 trying to process it. */
1984 cp_parser_skip_to_end_of_statement (parser);
1985
1986 return true;
1987 }
1988
1989 return false;
1990}
1991
a723baf1 1992/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
1993 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1994 are doing error recovery. Returns -1 if OR_COMMA is true and we
1995 found an unnested comma. */
a723baf1 1996
7efa3e22
NS
1997static int
1998cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
a668c6ad
MM
1999 bool recovering,
2000 bool or_comma,
2001 bool consume_paren)
a723baf1 2002{
7efa3e22
NS
2003 unsigned paren_depth = 0;
2004 unsigned brace_depth = 0;
a723baf1 2005
7efa3e22
NS
2006 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2007 && !cp_parser_committed_to_tentative_parse (parser))
2008 return 0;
2009
a723baf1
MM
2010 while (true)
2011 {
2012 cp_token *token;
7efa3e22 2013
a723baf1
MM
2014 /* If we've run out of tokens, then there is no closing `)'. */
2015 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 2016 return 0;
a723baf1 2017
a668c6ad
MM
2018 token = cp_lexer_peek_token (parser->lexer);
2019
2020 /* This matches the processing in skip_to_end_of_statement */
2021 if (token->type == CPP_SEMICOLON && !brace_depth)
2022 return 0;
2023 if (token->type == CPP_OPEN_BRACE)
2024 ++brace_depth;
2025 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2026 {
a668c6ad 2027 if (!brace_depth--)
7efa3e22 2028 return 0;
7efa3e22 2029 }
a668c6ad
MM
2030 if (recovering && or_comma && token->type == CPP_COMMA
2031 && !brace_depth && !paren_depth)
2032 return -1;
7efa3e22 2033
7efa3e22
NS
2034 if (!brace_depth)
2035 {
2036 /* If it is an `(', we have entered another level of nesting. */
2037 if (token->type == CPP_OPEN_PAREN)
2038 ++paren_depth;
2039 /* If it is a `)', then we might be done. */
2040 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2041 {
2042 if (consume_paren)
2043 cp_lexer_consume_token (parser->lexer);
2044 return 1;
2045 }
7efa3e22 2046 }
a668c6ad
MM
2047
2048 /* Consume the token. */
2049 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2050 }
2051}
2052
2053/* Consume tokens until we reach the end of the current statement.
2054 Normally, that will be just before consuming a `;'. However, if a
2055 non-nested `}' comes first, then we stop before consuming that. */
2056
2057static void
94edc4ab 2058cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2059{
2060 unsigned nesting_depth = 0;
2061
2062 while (true)
2063 {
2064 cp_token *token;
2065
2066 /* Peek at the next token. */
2067 token = cp_lexer_peek_token (parser->lexer);
2068 /* If we've run out of tokens, stop. */
2069 if (token->type == CPP_EOF)
2070 break;
2071 /* If the next token is a `;', we have reached the end of the
2072 statement. */
2073 if (token->type == CPP_SEMICOLON && !nesting_depth)
2074 break;
2075 /* If the next token is a non-nested `}', then we have reached
2076 the end of the current block. */
2077 if (token->type == CPP_CLOSE_BRACE)
2078 {
2079 /* If this is a non-nested `}', stop before consuming it.
2080 That way, when confronted with something like:
2081
2082 { 3 + }
2083
2084 we stop before consuming the closing `}', even though we
2085 have not yet reached a `;'. */
2086 if (nesting_depth == 0)
2087 break;
2088 /* If it is the closing `}' for a block that we have
2089 scanned, stop -- but only after consuming the token.
2090 That way given:
2091
2092 void f g () { ... }
2093 typedef int I;
2094
2095 we will stop after the body of the erroneously declared
2096 function, but before consuming the following `typedef'
2097 declaration. */
2098 if (--nesting_depth == 0)
2099 {
2100 cp_lexer_consume_token (parser->lexer);
2101 break;
2102 }
2103 }
2104 /* If it the next token is a `{', then we are entering a new
2105 block. Consume the entire block. */
2106 else if (token->type == CPP_OPEN_BRACE)
2107 ++nesting_depth;
2108 /* Consume the token. */
2109 cp_lexer_consume_token (parser->lexer);
2110 }
2111}
2112
e0860732
MM
2113/* This function is called at the end of a statement or declaration.
2114 If the next token is a semicolon, it is consumed; otherwise, error
2115 recovery is attempted. */
2116
2117static void
2118cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2119{
2120 /* Look for the trailing `;'. */
2121 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2122 {
2123 /* If there is additional (erroneous) input, skip to the end of
2124 the statement. */
2125 cp_parser_skip_to_end_of_statement (parser);
2126 /* If the next token is now a `;', consume it. */
2127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2128 cp_lexer_consume_token (parser->lexer);
2129 }
2130}
2131
a723baf1
MM
2132/* Skip tokens until we have consumed an entire block, or until we
2133 have consumed a non-nested `;'. */
2134
2135static void
94edc4ab 2136cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2137{
2138 unsigned nesting_depth = 0;
2139
2140 while (true)
2141 {
2142 cp_token *token;
2143
2144 /* Peek at the next token. */
2145 token = cp_lexer_peek_token (parser->lexer);
2146 /* If we've run out of tokens, stop. */
2147 if (token->type == CPP_EOF)
2148 break;
2149 /* If the next token is a `;', we have reached the end of the
2150 statement. */
2151 if (token->type == CPP_SEMICOLON && !nesting_depth)
2152 {
2153 /* Consume the `;'. */
2154 cp_lexer_consume_token (parser->lexer);
2155 break;
2156 }
2157 /* Consume the token. */
2158 token = cp_lexer_consume_token (parser->lexer);
2159 /* If the next token is a non-nested `}', then we have reached
2160 the end of the current block. */
2161 if (token->type == CPP_CLOSE_BRACE
2162 && (nesting_depth == 0 || --nesting_depth == 0))
2163 break;
2164 /* If it the next token is a `{', then we are entering a new
2165 block. Consume the entire block. */
2166 if (token->type == CPP_OPEN_BRACE)
2167 ++nesting_depth;
2168 }
2169}
2170
2171/* Skip tokens until a non-nested closing curly brace is the next
2172 token. */
2173
2174static void
2175cp_parser_skip_to_closing_brace (cp_parser *parser)
2176{
2177 unsigned nesting_depth = 0;
2178
2179 while (true)
2180 {
2181 cp_token *token;
2182
2183 /* Peek at the next token. */
2184 token = cp_lexer_peek_token (parser->lexer);
2185 /* If we've run out of tokens, stop. */
2186 if (token->type == CPP_EOF)
2187 break;
2188 /* If the next token is a non-nested `}', then we have reached
2189 the end of the current block. */
2190 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2191 break;
2192 /* If it the next token is a `{', then we are entering a new
2193 block. Consume the entire block. */
2194 else if (token->type == CPP_OPEN_BRACE)
2195 ++nesting_depth;
2196 /* Consume the token. */
2197 cp_lexer_consume_token (parser->lexer);
2198 }
2199}
2200
2201/* Create a new C++ parser. */
2202
2203static cp_parser *
94edc4ab 2204cp_parser_new (void)
a723baf1
MM
2205{
2206 cp_parser *parser;
17211ab5
GK
2207 cp_lexer *lexer;
2208
2209 /* cp_lexer_new_main is called before calling ggc_alloc because
2210 cp_lexer_new_main might load a PCH file. */
2211 lexer = cp_lexer_new_main ();
a723baf1 2212
c68b0a84 2213 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2214 parser->lexer = lexer;
a723baf1
MM
2215 parser->context = cp_parser_context_new (NULL);
2216
2217 /* For now, we always accept GNU extensions. */
2218 parser->allow_gnu_extensions_p = 1;
2219
2220 /* The `>' token is a greater-than operator, not the end of a
2221 template-id. */
2222 parser->greater_than_is_operator_p = true;
2223
2224 parser->default_arg_ok_p = true;
2225
2226 /* We are not parsing a constant-expression. */
2227 parser->constant_expression_p = false;
14d22dd6
MM
2228 parser->allow_non_constant_expression_p = false;
2229 parser->non_constant_expression_p = false;
a723baf1 2230
263ee052
MM
2231 /* We are not parsing offsetof. */
2232 parser->in_offsetof_p = false;
2233
a723baf1
MM
2234 /* Local variable names are not forbidden. */
2235 parser->local_variables_forbidden_p = false;
2236
34cd5ae7 2237 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2238 parser->in_unbraced_linkage_specification_p = false;
2239
2240 /* We are not processing a declarator. */
2241 parser->in_declarator_p = false;
2242
4bb8ca28
MM
2243 /* We are not processing a template-argument-list. */
2244 parser->in_template_argument_list_p = false;
2245
0e59b3fb
MM
2246 /* We are not in an iteration statement. */
2247 parser->in_iteration_statement_p = false;
2248
2249 /* We are not in a switch statement. */
2250 parser->in_switch_statement_p = false;
2251
a723baf1
MM
2252 /* The unparsed function queue is empty. */
2253 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2254
2255 /* There are no classes being defined. */
2256 parser->num_classes_being_defined = 0;
2257
2258 /* No template parameters apply. */
2259 parser->num_template_parameter_lists = 0;
2260
2261 return parser;
2262}
2263
2264/* Lexical conventions [gram.lex] */
2265
2266/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2267 identifier. */
2268
2269static tree
94edc4ab 2270cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2271{
2272 cp_token *token;
2273
2274 /* Look for the identifier. */
2275 token = cp_parser_require (parser, CPP_NAME, "identifier");
2276 /* Return the value. */
2277 return token ? token->value : error_mark_node;
2278}
2279
2280/* Basic concepts [gram.basic] */
2281
2282/* Parse a translation-unit.
2283
2284 translation-unit:
2285 declaration-seq [opt]
2286
2287 Returns TRUE if all went well. */
2288
2289static bool
94edc4ab 2290cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2291{
2292 while (true)
2293 {
2294 cp_parser_declaration_seq_opt (parser);
2295
2296 /* If there are no tokens left then all went well. */
2297 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2298 break;
2299
2300 /* Otherwise, issue an error message. */
2301 cp_parser_error (parser, "expected declaration");
2302 return false;
2303 }
2304
2305 /* Consume the EOF token. */
2306 cp_parser_require (parser, CPP_EOF, "end-of-file");
2307
2308 /* Finish up. */
2309 finish_translation_unit ();
2310
2311 /* All went well. */
2312 return true;
2313}
2314
2315/* Expressions [gram.expr] */
2316
2317/* Parse a primary-expression.
2318
2319 primary-expression:
2320 literal
2321 this
2322 ( expression )
2323 id-expression
2324
2325 GNU Extensions:
2326
2327 primary-expression:
2328 ( compound-statement )
2329 __builtin_va_arg ( assignment-expression , type-id )
2330
2331 literal:
2332 __null
2333
2334 Returns a representation of the expression.
2335
2336 *IDK indicates what kind of id-expression (if any) was present.
2337
2338 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2339 used as the operand of a pointer-to-member. In that case,
2340 *QUALIFYING_CLASS gives the class that is used as the qualifying
2341 class in the pointer-to-member. */
2342
2343static tree
2344cp_parser_primary_expression (cp_parser *parser,
b3445994 2345 cp_id_kind *idk,
a723baf1
MM
2346 tree *qualifying_class)
2347{
2348 cp_token *token;
2349
2350 /* Assume the primary expression is not an id-expression. */
b3445994 2351 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2352 /* And that it cannot be used as pointer-to-member. */
2353 *qualifying_class = NULL_TREE;
2354
2355 /* Peek at the next token. */
2356 token = cp_lexer_peek_token (parser->lexer);
2357 switch (token->type)
2358 {
2359 /* literal:
2360 integer-literal
2361 character-literal
2362 floating-literal
2363 string-literal
2364 boolean-literal */
2365 case CPP_CHAR:
2366 case CPP_WCHAR:
2367 case CPP_STRING:
2368 case CPP_WSTRING:
2369 case CPP_NUMBER:
2370 token = cp_lexer_consume_token (parser->lexer);
2371 return token->value;
2372
2373 case CPP_OPEN_PAREN:
2374 {
2375 tree expr;
2376 bool saved_greater_than_is_operator_p;
2377
2378 /* Consume the `('. */
2379 cp_lexer_consume_token (parser->lexer);
2380 /* Within a parenthesized expression, a `>' token is always
2381 the greater-than operator. */
2382 saved_greater_than_is_operator_p
2383 = parser->greater_than_is_operator_p;
2384 parser->greater_than_is_operator_p = true;
2385 /* If we see `( { ' then we are looking at the beginning of
2386 a GNU statement-expression. */
2387 if (cp_parser_allow_gnu_extensions_p (parser)
2388 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2389 {
2390 /* Statement-expressions are not allowed by the standard. */
2391 if (pedantic)
2392 pedwarn ("ISO C++ forbids braced-groups within expressions");
2393
2394 /* And they're not allowed outside of a function-body; you
2395 cannot, for example, write:
2396
2397 int i = ({ int j = 3; j + 1; });
2398
2399 at class or namespace scope. */
2400 if (!at_function_scope_p ())
2401 error ("statement-expressions are allowed only inside functions");
2402 /* Start the statement-expression. */
2403 expr = begin_stmt_expr ();
2404 /* Parse the compound-statement. */
a5bcc582 2405 cp_parser_compound_statement (parser, true);
a723baf1 2406 /* Finish up. */
303b7406 2407 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2408 }
2409 else
2410 {
2411 /* Parse the parenthesized expression. */
2412 expr = cp_parser_expression (parser);
2413 /* Let the front end know that this expression was
2414 enclosed in parentheses. This matters in case, for
2415 example, the expression is of the form `A::B', since
2416 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2417 not. */
2418 finish_parenthesized_expr (expr);
2419 }
2420 /* The `>' token might be the end of a template-id or
2421 template-parameter-list now. */
2422 parser->greater_than_is_operator_p
2423 = saved_greater_than_is_operator_p;
2424 /* Consume the `)'. */
2425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2426 cp_parser_skip_to_end_of_statement (parser);
2427
2428 return expr;
2429 }
2430
2431 case CPP_KEYWORD:
2432 switch (token->keyword)
2433 {
2434 /* These two are the boolean literals. */
2435 case RID_TRUE:
2436 cp_lexer_consume_token (parser->lexer);
2437 return boolean_true_node;
2438 case RID_FALSE:
2439 cp_lexer_consume_token (parser->lexer);
2440 return boolean_false_node;
2441
2442 /* The `__null' literal. */
2443 case RID_NULL:
2444 cp_lexer_consume_token (parser->lexer);
2445 return null_node;
2446
2447 /* Recognize the `this' keyword. */
2448 case RID_THIS:
2449 cp_lexer_consume_token (parser->lexer);
2450 if (parser->local_variables_forbidden_p)
2451 {
2452 error ("`this' may not be used in this context");
2453 return error_mark_node;
2454 }
14d22dd6
MM
2455 /* Pointers cannot appear in constant-expressions. */
2456 if (parser->constant_expression_p)
2457 {
2458 if (!parser->allow_non_constant_expression_p)
2459 return cp_parser_non_constant_expression ("`this'");
2460 parser->non_constant_expression_p = true;
2461 }
a723baf1
MM
2462 return finish_this_expr ();
2463
2464 /* The `operator' keyword can be the beginning of an
2465 id-expression. */
2466 case RID_OPERATOR:
2467 goto id_expression;
2468
2469 case RID_FUNCTION_NAME:
2470 case RID_PRETTY_FUNCTION_NAME:
2471 case RID_C99_FUNCTION_NAME:
2472 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2473 __func__ are the names of variables -- but they are
2474 treated specially. Therefore, they are handled here,
2475 rather than relying on the generic id-expression logic
34cd5ae7 2476 below. Grammatically, these names are id-expressions.
a723baf1
MM
2477
2478 Consume the token. */
2479 token = cp_lexer_consume_token (parser->lexer);
2480 /* Look up the name. */
2481 return finish_fname (token->value);
2482
2483 case RID_VA_ARG:
2484 {
2485 tree expression;
2486 tree type;
2487
2488 /* The `__builtin_va_arg' construct is used to handle
2489 `va_arg'. Consume the `__builtin_va_arg' token. */
2490 cp_lexer_consume_token (parser->lexer);
2491 /* Look for the opening `('. */
2492 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2493 /* Now, parse the assignment-expression. */
2494 expression = cp_parser_assignment_expression (parser);
2495 /* Look for the `,'. */
2496 cp_parser_require (parser, CPP_COMMA, "`,'");
2497 /* Parse the type-id. */
2498 type = cp_parser_type_id (parser);
2499 /* Look for the closing `)'. */
2500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2501 /* Using `va_arg' in a constant-expression is not
2502 allowed. */
2503 if (parser->constant_expression_p)
2504 {
2505 if (!parser->allow_non_constant_expression_p)
2506 return cp_parser_non_constant_expression ("`va_arg'");
2507 parser->non_constant_expression_p = true;
2508 }
a723baf1
MM
2509 return build_x_va_arg (expression, type);
2510 }
2511
263ee052
MM
2512 case RID_OFFSETOF:
2513 {
2514 tree expression;
2515 bool saved_in_offsetof_p;
2516
2517 /* Consume the "__offsetof__" token. */
2518 cp_lexer_consume_token (parser->lexer);
2519 /* Consume the opening `('. */
2520 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2521 /* Parse the parenthesized (almost) constant-expression. */
2522 saved_in_offsetof_p = parser->in_offsetof_p;
2523 parser->in_offsetof_p = true;
2524 expression
2525 = cp_parser_constant_expression (parser,
2526 /*allow_non_constant_p=*/false,
2527 /*non_constant_p=*/NULL);
2528 parser->in_offsetof_p = saved_in_offsetof_p;
2529 /* Consume the closing ')'. */
2530 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2531
2532 return expression;
2533 }
2534
a723baf1
MM
2535 default:
2536 cp_parser_error (parser, "expected primary-expression");
2537 return error_mark_node;
2538 }
a723baf1
MM
2539
2540 /* An id-expression can start with either an identifier, a
2541 `::' as the beginning of a qualified-id, or the "operator"
2542 keyword. */
2543 case CPP_NAME:
2544 case CPP_SCOPE:
2545 case CPP_TEMPLATE_ID:
2546 case CPP_NESTED_NAME_SPECIFIER:
2547 {
2548 tree id_expression;
2549 tree decl;
b3445994 2550 const char *error_msg;
a723baf1
MM
2551
2552 id_expression:
2553 /* Parse the id-expression. */
2554 id_expression
2555 = cp_parser_id_expression (parser,
2556 /*template_keyword_p=*/false,
2557 /*check_dependency_p=*/true,
f3c2dfc6
MM
2558 /*template_p=*/NULL,
2559 /*declarator_p=*/false);
a723baf1
MM
2560 if (id_expression == error_mark_node)
2561 return error_mark_node;
2562 /* If we have a template-id, then no further lookup is
2563 required. If the template-id was for a template-class, we
2564 will sometimes have a TYPE_DECL at this point. */
2565 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2566 || TREE_CODE (id_expression) == TYPE_DECL)
2567 decl = id_expression;
2568 /* Look up the name. */
2569 else
2570 {
2571 decl = cp_parser_lookup_name_simple (parser, id_expression);
2572 /* If name lookup gives us a SCOPE_REF, then the
2573 qualifying scope was dependent. Just propagate the
2574 name. */
2575 if (TREE_CODE (decl) == SCOPE_REF)
2576 {
2577 if (TYPE_P (TREE_OPERAND (decl, 0)))
2578 *qualifying_class = TREE_OPERAND (decl, 0);
2579 return decl;
2580 }
2581 /* Check to see if DECL is a local variable in a context
2582 where that is forbidden. */
2583 if (parser->local_variables_forbidden_p
2584 && local_variable_p (decl))
2585 {
2586 /* It might be that we only found DECL because we are
2587 trying to be generous with pre-ISO scoping rules.
2588 For example, consider:
2589
2590 int i;
2591 void g() {
2592 for (int i = 0; i < 10; ++i) {}
2593 extern void f(int j = i);
2594 }
2595
2596 Here, name look up will originally find the out
2597 of scope `i'. We need to issue a warning message,
2598 but then use the global `i'. */
2599 decl = check_for_out_of_scope_variable (decl);
2600 if (local_variable_p (decl))
2601 {
2602 error ("local variable `%D' may not appear in this context",
2603 decl);
2604 return error_mark_node;
2605 }
2606 }
c006d942 2607 }
b3445994
MM
2608
2609 decl = finish_id_expression (id_expression, decl, parser->scope,
2610 idk, qualifying_class,
2611 parser->constant_expression_p,
2612 parser->allow_non_constant_expression_p,
2613 &parser->non_constant_expression_p,
2614 &error_msg);
2615 if (error_msg)
2616 cp_parser_error (parser, error_msg);
a723baf1
MM
2617 return decl;
2618 }
2619
2620 /* Anything else is an error. */
2621 default:
2622 cp_parser_error (parser, "expected primary-expression");
2623 return error_mark_node;
2624 }
2625}
2626
2627/* Parse an id-expression.
2628
2629 id-expression:
2630 unqualified-id
2631 qualified-id
2632
2633 qualified-id:
2634 :: [opt] nested-name-specifier template [opt] unqualified-id
2635 :: identifier
2636 :: operator-function-id
2637 :: template-id
2638
2639 Return a representation of the unqualified portion of the
2640 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2641 a `::' or nested-name-specifier.
2642
2643 Often, if the id-expression was a qualified-id, the caller will
2644 want to make a SCOPE_REF to represent the qualified-id. This
2645 function does not do this in order to avoid wastefully creating
2646 SCOPE_REFs when they are not required.
2647
a723baf1
MM
2648 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2649 `template' keyword.
2650
2651 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2652 uninstantiated templates.
2653
15d2cb19 2654 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2655 `template' keyword is used to explicitly indicate that the entity
f3c2dfc6
MM
2656 named is a template.
2657
2658 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2659 a declarator, rather than as part of an expression. */
a723baf1
MM
2660
2661static tree
2662cp_parser_id_expression (cp_parser *parser,
2663 bool template_keyword_p,
2664 bool check_dependency_p,
f3c2dfc6
MM
2665 bool *template_p,
2666 bool declarator_p)
a723baf1
MM
2667{
2668 bool global_scope_p;
2669 bool nested_name_specifier_p;
2670
2671 /* Assume the `template' keyword was not used. */
2672 if (template_p)
2673 *template_p = false;
2674
2675 /* Look for the optional `::' operator. */
2676 global_scope_p
2677 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2678 != NULL_TREE);
2679 /* Look for the optional nested-name-specifier. */
2680 nested_name_specifier_p
2681 = (cp_parser_nested_name_specifier_opt (parser,
2682 /*typename_keyword_p=*/false,
2683 check_dependency_p,
a668c6ad
MM
2684 /*type_p=*/false,
2685 /*is_declarator=*/false)
a723baf1
MM
2686 != NULL_TREE);
2687 /* If there is a nested-name-specifier, then we are looking at
2688 the first qualified-id production. */
2689 if (nested_name_specifier_p)
2690 {
2691 tree saved_scope;
2692 tree saved_object_scope;
2693 tree saved_qualifying_scope;
2694 tree unqualified_id;
2695 bool is_template;
2696
2697 /* See if the next token is the `template' keyword. */
2698 if (!template_p)
2699 template_p = &is_template;
2700 *template_p = cp_parser_optional_template_keyword (parser);
2701 /* Name lookup we do during the processing of the
2702 unqualified-id might obliterate SCOPE. */
2703 saved_scope = parser->scope;
2704 saved_object_scope = parser->object_scope;
2705 saved_qualifying_scope = parser->qualifying_scope;
2706 /* Process the final unqualified-id. */
2707 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2708 check_dependency_p,
2709 declarator_p);
a723baf1
MM
2710 /* Restore the SAVED_SCOPE for our caller. */
2711 parser->scope = saved_scope;
2712 parser->object_scope = saved_object_scope;
2713 parser->qualifying_scope = saved_qualifying_scope;
2714
2715 return unqualified_id;
2716 }
2717 /* Otherwise, if we are in global scope, then we are looking at one
2718 of the other qualified-id productions. */
2719 else if (global_scope_p)
2720 {
2721 cp_token *token;
2722 tree id;
2723
e5976695
MM
2724 /* Peek at the next token. */
2725 token = cp_lexer_peek_token (parser->lexer);
2726
2727 /* If it's an identifier, and the next token is not a "<", then
2728 we can avoid the template-id case. This is an optimization
2729 for this common case. */
2730 if (token->type == CPP_NAME
2731 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2732 return cp_parser_identifier (parser);
2733
a723baf1
MM
2734 cp_parser_parse_tentatively (parser);
2735 /* Try a template-id. */
2736 id = cp_parser_template_id (parser,
2737 /*template_keyword_p=*/false,
a668c6ad
MM
2738 /*check_dependency_p=*/true,
2739 declarator_p);
a723baf1
MM
2740 /* If that worked, we're done. */
2741 if (cp_parser_parse_definitely (parser))
2742 return id;
2743
e5976695
MM
2744 /* Peek at the next token. (Changes in the token buffer may
2745 have invalidated the pointer obtained above.) */
a723baf1
MM
2746 token = cp_lexer_peek_token (parser->lexer);
2747
2748 switch (token->type)
2749 {
2750 case CPP_NAME:
2751 return cp_parser_identifier (parser);
2752
2753 case CPP_KEYWORD:
2754 if (token->keyword == RID_OPERATOR)
2755 return cp_parser_operator_function_id (parser);
2756 /* Fall through. */
2757
2758 default:
2759 cp_parser_error (parser, "expected id-expression");
2760 return error_mark_node;
2761 }
2762 }
2763 else
2764 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
2765 /*check_dependency_p=*/true,
2766 declarator_p);
a723baf1
MM
2767}
2768
2769/* Parse an unqualified-id.
2770
2771 unqualified-id:
2772 identifier
2773 operator-function-id
2774 conversion-function-id
2775 ~ class-name
2776 template-id
2777
2778 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2779 keyword, in a construct like `A::template ...'.
2780
2781 Returns a representation of unqualified-id. For the `identifier'
2782 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2783 production a BIT_NOT_EXPR is returned; the operand of the
2784 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2785 other productions, see the documentation accompanying the
2786 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
2787 names are looked up in uninstantiated templates. If DECLARATOR_P
2788 is true, the unqualified-id is appearing as part of a declarator,
2789 rather than as part of an expression. */
a723baf1
MM
2790
2791static tree
94edc4ab
NN
2792cp_parser_unqualified_id (cp_parser* parser,
2793 bool template_keyword_p,
f3c2dfc6
MM
2794 bool check_dependency_p,
2795 bool declarator_p)
a723baf1
MM
2796{
2797 cp_token *token;
2798
2799 /* Peek at the next token. */
2800 token = cp_lexer_peek_token (parser->lexer);
2801
2802 switch (token->type)
2803 {
2804 case CPP_NAME:
2805 {
2806 tree id;
2807
2808 /* We don't know yet whether or not this will be a
2809 template-id. */
2810 cp_parser_parse_tentatively (parser);
2811 /* Try a template-id. */
2812 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2813 check_dependency_p,
2814 declarator_p);
a723baf1
MM
2815 /* If it worked, we're done. */
2816 if (cp_parser_parse_definitely (parser))
2817 return id;
2818 /* Otherwise, it's an ordinary identifier. */
2819 return cp_parser_identifier (parser);
2820 }
2821
2822 case CPP_TEMPLATE_ID:
2823 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2824 check_dependency_p,
2825 declarator_p);
a723baf1
MM
2826
2827 case CPP_COMPL:
2828 {
2829 tree type_decl;
2830 tree qualifying_scope;
2831 tree object_scope;
2832 tree scope;
2833
2834 /* Consume the `~' token. */
2835 cp_lexer_consume_token (parser->lexer);
2836 /* Parse the class-name. The standard, as written, seems to
2837 say that:
2838
2839 template <typename T> struct S { ~S (); };
2840 template <typename T> S<T>::~S() {}
2841
2842 is invalid, since `~' must be followed by a class-name, but
2843 `S<T>' is dependent, and so not known to be a class.
2844 That's not right; we need to look in uninstantiated
2845 templates. A further complication arises from:
2846
2847 template <typename T> void f(T t) {
2848 t.T::~T();
2849 }
2850
2851 Here, it is not possible to look up `T' in the scope of `T'
2852 itself. We must look in both the current scope, and the
2853 scope of the containing complete expression.
2854
2855 Yet another issue is:
2856
2857 struct S {
2858 int S;
2859 ~S();
2860 };
2861
2862 S::~S() {}
2863
2864 The standard does not seem to say that the `S' in `~S'
2865 should refer to the type `S' and not the data member
2866 `S::S'. */
2867
2868 /* DR 244 says that we look up the name after the "~" in the
2869 same scope as we looked up the qualifying name. That idea
2870 isn't fully worked out; it's more complicated than that. */
2871 scope = parser->scope;
2872 object_scope = parser->object_scope;
2873 qualifying_scope = parser->qualifying_scope;
2874
2875 /* If the name is of the form "X::~X" it's OK. */
2876 if (scope && TYPE_P (scope)
2877 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2878 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2879 == CPP_OPEN_PAREN)
2880 && (cp_lexer_peek_token (parser->lexer)->value
2881 == TYPE_IDENTIFIER (scope)))
2882 {
2883 cp_lexer_consume_token (parser->lexer);
2884 return build_nt (BIT_NOT_EXPR, scope);
2885 }
2886
2887 /* If there was an explicit qualification (S::~T), first look
2888 in the scope given by the qualification (i.e., S). */
2889 if (scope)
2890 {
2891 cp_parser_parse_tentatively (parser);
2892 type_decl = cp_parser_class_name (parser,
2893 /*typename_keyword_p=*/false,
2894 /*template_keyword_p=*/false,
2895 /*type_p=*/false,
a723baf1 2896 /*check_dependency=*/false,
a668c6ad
MM
2897 /*class_head_p=*/false,
2898 declarator_p);
a723baf1
MM
2899 if (cp_parser_parse_definitely (parser))
2900 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2901 }
2902 /* In "N::S::~S", look in "N" as well. */
2903 if (scope && qualifying_scope)
2904 {
2905 cp_parser_parse_tentatively (parser);
2906 parser->scope = qualifying_scope;
2907 parser->object_scope = NULL_TREE;
2908 parser->qualifying_scope = NULL_TREE;
2909 type_decl
2910 = cp_parser_class_name (parser,
2911 /*typename_keyword_p=*/false,
2912 /*template_keyword_p=*/false,
2913 /*type_p=*/false,
a723baf1 2914 /*check_dependency=*/false,
a668c6ad
MM
2915 /*class_head_p=*/false,
2916 declarator_p);
a723baf1
MM
2917 if (cp_parser_parse_definitely (parser))
2918 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2919 }
2920 /* In "p->S::~T", look in the scope given by "*p" as well. */
2921 else if (object_scope)
2922 {
2923 cp_parser_parse_tentatively (parser);
2924 parser->scope = object_scope;
2925 parser->object_scope = NULL_TREE;
2926 parser->qualifying_scope = NULL_TREE;
2927 type_decl
2928 = cp_parser_class_name (parser,
2929 /*typename_keyword_p=*/false,
2930 /*template_keyword_p=*/false,
2931 /*type_p=*/false,
a723baf1 2932 /*check_dependency=*/false,
a668c6ad
MM
2933 /*class_head_p=*/false,
2934 declarator_p);
a723baf1
MM
2935 if (cp_parser_parse_definitely (parser))
2936 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2937 }
2938 /* Look in the surrounding context. */
2939 parser->scope = NULL_TREE;
2940 parser->object_scope = NULL_TREE;
2941 parser->qualifying_scope = NULL_TREE;
2942 type_decl
2943 = cp_parser_class_name (parser,
2944 /*typename_keyword_p=*/false,
2945 /*template_keyword_p=*/false,
2946 /*type_p=*/false,
a723baf1 2947 /*check_dependency=*/false,
a668c6ad
MM
2948 /*class_head_p=*/false,
2949 declarator_p);
a723baf1
MM
2950 /* If an error occurred, assume that the name of the
2951 destructor is the same as the name of the qualifying
2952 class. That allows us to keep parsing after running
2953 into ill-formed destructor names. */
2954 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2955 return build_nt (BIT_NOT_EXPR, scope);
2956 else if (type_decl == error_mark_node)
2957 return error_mark_node;
2958
f3c2dfc6
MM
2959 /* [class.dtor]
2960
2961 A typedef-name that names a class shall not be used as the
2962 identifier in the declarator for a destructor declaration. */
2963 if (declarator_p
2964 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2965 && !DECL_SELF_REFERENCE_P (type_decl))
2966 error ("typedef-name `%D' used as destructor declarator",
2967 type_decl);
2968
a723baf1
MM
2969 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2970 }
2971
2972 case CPP_KEYWORD:
2973 if (token->keyword == RID_OPERATOR)
2974 {
2975 tree id;
2976
2977 /* This could be a template-id, so we try that first. */
2978 cp_parser_parse_tentatively (parser);
2979 /* Try a template-id. */
2980 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2981 /*check_dependency_p=*/true,
2982 declarator_p);
a723baf1
MM
2983 /* If that worked, we're done. */
2984 if (cp_parser_parse_definitely (parser))
2985 return id;
2986 /* We still don't know whether we're looking at an
2987 operator-function-id or a conversion-function-id. */
2988 cp_parser_parse_tentatively (parser);
2989 /* Try an operator-function-id. */
2990 id = cp_parser_operator_function_id (parser);
2991 /* If that didn't work, try a conversion-function-id. */
2992 if (!cp_parser_parse_definitely (parser))
2993 id = cp_parser_conversion_function_id (parser);
2994
2995 return id;
2996 }
2997 /* Fall through. */
2998
2999 default:
3000 cp_parser_error (parser, "expected unqualified-id");
3001 return error_mark_node;
3002 }
3003}
3004
3005/* Parse an (optional) nested-name-specifier.
3006
3007 nested-name-specifier:
3008 class-or-namespace-name :: nested-name-specifier [opt]
3009 class-or-namespace-name :: template nested-name-specifier [opt]
3010
3011 PARSER->SCOPE should be set appropriately before this function is
3012 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3013 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3014 in name lookups.
3015
3016 Sets PARSER->SCOPE to the class (TYPE) or namespace
3017 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3018 it unchanged if there is no nested-name-specifier. Returns the new
a668c6ad
MM
3019 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3020
3021 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3022 part of a declaration and/or decl-specifier. */
a723baf1
MM
3023
3024static tree
3025cp_parser_nested_name_specifier_opt (cp_parser *parser,
3026 bool typename_keyword_p,
3027 bool check_dependency_p,
a668c6ad
MM
3028 bool type_p,
3029 bool is_declaration)
a723baf1
MM
3030{
3031 bool success = false;
3032 tree access_check = NULL_TREE;
3033 ptrdiff_t start;
2050a1bb 3034 cp_token* token;
a723baf1
MM
3035
3036 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3037 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3038 false, it may have been true before, in which case something
3039 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3040 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3041 CHECK_DEPENDENCY_P is false, we have to fall through into the
3042 main loop. */
3043 if (check_dependency_p
3044 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3045 {
3046 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3047 return parser->scope;
3048 }
3049
3050 /* Remember where the nested-name-specifier starts. */
3051 if (cp_parser_parsing_tentatively (parser)
3052 && !cp_parser_committed_to_tentative_parse (parser))
3053 {
2050a1bb 3054 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3055 start = cp_lexer_token_difference (parser->lexer,
3056 parser->lexer->first_token,
2050a1bb 3057 token);
a723baf1
MM
3058 }
3059 else
3060 start = -1;
3061
8d241e0b 3062 push_deferring_access_checks (dk_deferred);
cf22909c 3063
a723baf1
MM
3064 while (true)
3065 {
3066 tree new_scope;
3067 tree old_scope;
3068 tree saved_qualifying_scope;
a723baf1
MM
3069 bool template_keyword_p;
3070
2050a1bb
MM
3071 /* Spot cases that cannot be the beginning of a
3072 nested-name-specifier. */
3073 token = cp_lexer_peek_token (parser->lexer);
3074
3075 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3076 the already parsed nested-name-specifier. */
3077 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3078 {
3079 /* Grab the nested-name-specifier and continue the loop. */
3080 cp_parser_pre_parsed_nested_name_specifier (parser);
3081 success = true;
3082 continue;
3083 }
3084
a723baf1
MM
3085 /* Spot cases that cannot be the beginning of a
3086 nested-name-specifier. On the second and subsequent times
3087 through the loop, we look for the `template' keyword. */
f7b5ecd9 3088 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3089 ;
3090 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3091 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3092 ;
3093 else
3094 {
3095 /* If the next token is not an identifier, then it is
3096 definitely not a class-or-namespace-name. */
f7b5ecd9 3097 if (token->type != CPP_NAME)
a723baf1
MM
3098 break;
3099 /* If the following token is neither a `<' (to begin a
3100 template-id), nor a `::', then we are not looking at a
3101 nested-name-specifier. */
3102 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3103 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3104 break;
3105 }
3106
3107 /* The nested-name-specifier is optional, so we parse
3108 tentatively. */
3109 cp_parser_parse_tentatively (parser);
3110
3111 /* Look for the optional `template' keyword, if this isn't the
3112 first time through the loop. */
3113 if (success)
3114 template_keyword_p = cp_parser_optional_template_keyword (parser);
3115 else
3116 template_keyword_p = false;
3117
3118 /* Save the old scope since the name lookup we are about to do
3119 might destroy it. */
3120 old_scope = parser->scope;
3121 saved_qualifying_scope = parser->qualifying_scope;
3122 /* Parse the qualifying entity. */
3123 new_scope
3124 = cp_parser_class_or_namespace_name (parser,
3125 typename_keyword_p,
3126 template_keyword_p,
3127 check_dependency_p,
a668c6ad
MM
3128 type_p,
3129 is_declaration);
a723baf1
MM
3130 /* Look for the `::' token. */
3131 cp_parser_require (parser, CPP_SCOPE, "`::'");
3132
3133 /* If we found what we wanted, we keep going; otherwise, we're
3134 done. */
3135 if (!cp_parser_parse_definitely (parser))
3136 {
3137 bool error_p = false;
3138
3139 /* Restore the OLD_SCOPE since it was valid before the
3140 failed attempt at finding the last
3141 class-or-namespace-name. */
3142 parser->scope = old_scope;
3143 parser->qualifying_scope = saved_qualifying_scope;
3144 /* If the next token is an identifier, and the one after
3145 that is a `::', then any valid interpretation would have
3146 found a class-or-namespace-name. */
3147 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3148 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3149 == CPP_SCOPE)
3150 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3151 != CPP_COMPL))
3152 {
3153 token = cp_lexer_consume_token (parser->lexer);
3154 if (!error_p)
3155 {
3156 tree decl;
3157
3158 decl = cp_parser_lookup_name_simple (parser, token->value);
3159 if (TREE_CODE (decl) == TEMPLATE_DECL)
3160 error ("`%D' used without template parameters",
3161 decl);
a723baf1 3162 else
4bb8ca28
MM
3163 cp_parser_name_lookup_error
3164 (parser, token->value, decl,
3165 "is not a class or namespace");
a723baf1
MM
3166 parser->scope = NULL_TREE;
3167 error_p = true;
eea9800f
MM
3168 /* Treat this as a successful nested-name-specifier
3169 due to:
3170
3171 [basic.lookup.qual]
3172
3173 If the name found is not a class-name (clause
3174 _class_) or namespace-name (_namespace.def_), the
3175 program is ill-formed. */
3176 success = true;
a723baf1
MM
3177 }
3178 cp_lexer_consume_token (parser->lexer);
3179 }
3180 break;
3181 }
3182
3183 /* We've found one valid nested-name-specifier. */
3184 success = true;
3185 /* Make sure we look in the right scope the next time through
3186 the loop. */
3187 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3188 ? TREE_TYPE (new_scope)
3189 : new_scope);
3190 /* If it is a class scope, try to complete it; we are about to
3191 be looking up names inside the class. */
8fbc5ae7
MM
3192 if (TYPE_P (parser->scope)
3193 /* Since checking types for dependency can be expensive,
3194 avoid doing it if the type is already complete. */
3195 && !COMPLETE_TYPE_P (parser->scope)
3196 /* Do not try to complete dependent types. */
1fb3244a 3197 && !dependent_type_p (parser->scope))
a723baf1
MM
3198 complete_type (parser->scope);
3199 }
3200
cf22909c
KL
3201 /* Retrieve any deferred checks. Do not pop this access checks yet
3202 so the memory will not be reclaimed during token replacing below. */
3203 access_check = get_deferred_access_checks ();
3204
a723baf1
MM
3205 /* If parsing tentatively, replace the sequence of tokens that makes
3206 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3207 token. That way, should we re-parse the token stream, we will
3208 not have to repeat the effort required to do the parse, nor will
3209 we issue duplicate error messages. */
3210 if (success && start >= 0)
3211 {
a723baf1
MM
3212 /* Find the token that corresponds to the start of the
3213 template-id. */
3214 token = cp_lexer_advance_token (parser->lexer,
3215 parser->lexer->first_token,
3216 start);
3217
a723baf1
MM
3218 /* Reset the contents of the START token. */
3219 token->type = CPP_NESTED_NAME_SPECIFIER;
3220 token->value = build_tree_list (access_check, parser->scope);
3221 TREE_TYPE (token->value) = parser->qualifying_scope;
3222 token->keyword = RID_MAX;
3223 /* Purge all subsequent tokens. */
3224 cp_lexer_purge_tokens_after (parser->lexer, token);
3225 }
3226
cf22909c 3227 pop_deferring_access_checks ();
a723baf1
MM
3228 return success ? parser->scope : NULL_TREE;
3229}
3230
3231/* Parse a nested-name-specifier. See
3232 cp_parser_nested_name_specifier_opt for details. This function
3233 behaves identically, except that it will an issue an error if no
3234 nested-name-specifier is present, and it will return
3235 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3236 is present. */
3237
3238static tree
3239cp_parser_nested_name_specifier (cp_parser *parser,
3240 bool typename_keyword_p,
3241 bool check_dependency_p,
a668c6ad
MM
3242 bool type_p,
3243 bool is_declaration)
a723baf1
MM
3244{
3245 tree scope;
3246
3247 /* Look for the nested-name-specifier. */
3248 scope = cp_parser_nested_name_specifier_opt (parser,
3249 typename_keyword_p,
3250 check_dependency_p,
a668c6ad
MM
3251 type_p,
3252 is_declaration);
a723baf1
MM
3253 /* If it was not present, issue an error message. */
3254 if (!scope)
3255 {
3256 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3257 parser->scope = NULL_TREE;
a723baf1
MM
3258 return error_mark_node;
3259 }
3260
3261 return scope;
3262}
3263
3264/* Parse a class-or-namespace-name.
3265
3266 class-or-namespace-name:
3267 class-name
3268 namespace-name
3269
3270 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3271 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3272 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3273 TYPE_P is TRUE iff the next name should be taken as a class-name,
3274 even the same name is declared to be another entity in the same
3275 scope.
3276
3277 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3278 specified by the class-or-namespace-name. If neither is found the
3279 ERROR_MARK_NODE is returned. */
a723baf1
MM
3280
3281static tree
3282cp_parser_class_or_namespace_name (cp_parser *parser,
3283 bool typename_keyword_p,
3284 bool template_keyword_p,
3285 bool check_dependency_p,
a668c6ad
MM
3286 bool type_p,
3287 bool is_declaration)
a723baf1
MM
3288{
3289 tree saved_scope;
3290 tree saved_qualifying_scope;
3291 tree saved_object_scope;
3292 tree scope;
eea9800f 3293 bool only_class_p;
a723baf1 3294
a723baf1
MM
3295 /* Before we try to parse the class-name, we must save away the
3296 current PARSER->SCOPE since cp_parser_class_name will destroy
3297 it. */
3298 saved_scope = parser->scope;
3299 saved_qualifying_scope = parser->qualifying_scope;
3300 saved_object_scope = parser->object_scope;
eea9800f
MM
3301 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3302 there is no need to look for a namespace-name. */
bbaab916 3303 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3304 if (!only_class_p)
3305 cp_parser_parse_tentatively (parser);
a723baf1
MM
3306 scope = cp_parser_class_name (parser,
3307 typename_keyword_p,
3308 template_keyword_p,
3309 type_p,
a723baf1 3310 check_dependency_p,
a668c6ad
MM
3311 /*class_head_p=*/false,
3312 is_declaration);
a723baf1 3313 /* If that didn't work, try for a namespace-name. */
eea9800f 3314 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3315 {
3316 /* Restore the saved scope. */
3317 parser->scope = saved_scope;
3318 parser->qualifying_scope = saved_qualifying_scope;
3319 parser->object_scope = saved_object_scope;
eea9800f
MM
3320 /* If we are not looking at an identifier followed by the scope
3321 resolution operator, then this is not part of a
3322 nested-name-specifier. (Note that this function is only used
3323 to parse the components of a nested-name-specifier.) */
3324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3325 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3326 return error_mark_node;
a723baf1
MM
3327 scope = cp_parser_namespace_name (parser);
3328 }
3329
3330 return scope;
3331}
3332
3333/* Parse a postfix-expression.
3334
3335 postfix-expression:
3336 primary-expression
3337 postfix-expression [ expression ]
3338 postfix-expression ( expression-list [opt] )
3339 simple-type-specifier ( expression-list [opt] )
3340 typename :: [opt] nested-name-specifier identifier
3341 ( expression-list [opt] )
3342 typename :: [opt] nested-name-specifier template [opt] template-id
3343 ( expression-list [opt] )
3344 postfix-expression . template [opt] id-expression
3345 postfix-expression -> template [opt] id-expression
3346 postfix-expression . pseudo-destructor-name
3347 postfix-expression -> pseudo-destructor-name
3348 postfix-expression ++
3349 postfix-expression --
3350 dynamic_cast < type-id > ( expression )
3351 static_cast < type-id > ( expression )
3352 reinterpret_cast < type-id > ( expression )
3353 const_cast < type-id > ( expression )
3354 typeid ( expression )
3355 typeid ( type-id )
3356
3357 GNU Extension:
3358
3359 postfix-expression:
3360 ( type-id ) { initializer-list , [opt] }
3361
3362 This extension is a GNU version of the C99 compound-literal
3363 construct. (The C99 grammar uses `type-name' instead of `type-id',
3364 but they are essentially the same concept.)
3365
3366 If ADDRESS_P is true, the postfix expression is the operand of the
3367 `&' operator.
3368
3369 Returns a representation of the expression. */
3370
3371static tree
3372cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3373{
3374 cp_token *token;
3375 enum rid keyword;
b3445994 3376 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3377 tree postfix_expression = NULL_TREE;
3378 /* Non-NULL only if the current postfix-expression can be used to
3379 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3380 class used to qualify the member. */
3381 tree qualifying_class = NULL_TREE;
a723baf1
MM
3382
3383 /* Peek at the next token. */
3384 token = cp_lexer_peek_token (parser->lexer);
3385 /* Some of the productions are determined by keywords. */
3386 keyword = token->keyword;
3387 switch (keyword)
3388 {
3389 case RID_DYNCAST:
3390 case RID_STATCAST:
3391 case RID_REINTCAST:
3392 case RID_CONSTCAST:
3393 {
3394 tree type;
3395 tree expression;
3396 const char *saved_message;
3397
3398 /* All of these can be handled in the same way from the point
3399 of view of parsing. Begin by consuming the token
3400 identifying the cast. */
3401 cp_lexer_consume_token (parser->lexer);
3402
3403 /* New types cannot be defined in the cast. */
3404 saved_message = parser->type_definition_forbidden_message;
3405 parser->type_definition_forbidden_message
3406 = "types may not be defined in casts";
3407
3408 /* Look for the opening `<'. */
3409 cp_parser_require (parser, CPP_LESS, "`<'");
3410 /* Parse the type to which we are casting. */
3411 type = cp_parser_type_id (parser);
3412 /* Look for the closing `>'. */
3413 cp_parser_require (parser, CPP_GREATER, "`>'");
3414 /* Restore the old message. */
3415 parser->type_definition_forbidden_message = saved_message;
3416
3417 /* And the expression which is being cast. */
3418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3419 expression = cp_parser_expression (parser);
3420 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3421
14d22dd6
MM
3422 /* Only type conversions to integral or enumeration types
3423 can be used in constant-expressions. */
3424 if (parser->constant_expression_p
3425 && !dependent_type_p (type)
263ee052
MM
3426 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3427 /* A cast to pointer or reference type is allowed in the
3428 implementation of "offsetof". */
3429 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
14d22dd6
MM
3430 {
3431 if (!parser->allow_non_constant_expression_p)
3432 return (cp_parser_non_constant_expression
3433 ("a cast to a type other than an integral or "
3434 "enumeration type"));
3435 parser->non_constant_expression_p = true;
3436 }
3437
a723baf1
MM
3438 switch (keyword)
3439 {
3440 case RID_DYNCAST:
3441 postfix_expression
3442 = build_dynamic_cast (type, expression);
3443 break;
3444 case RID_STATCAST:
3445 postfix_expression
3446 = build_static_cast (type, expression);
3447 break;
3448 case RID_REINTCAST:
3449 postfix_expression
3450 = build_reinterpret_cast (type, expression);
3451 break;
3452 case RID_CONSTCAST:
3453 postfix_expression
3454 = build_const_cast (type, expression);
3455 break;
3456 default:
3457 abort ();
3458 }
3459 }
3460 break;
3461
3462 case RID_TYPEID:
3463 {
3464 tree type;
3465 const char *saved_message;
3466
3467 /* Consume the `typeid' token. */
3468 cp_lexer_consume_token (parser->lexer);
3469 /* Look for the `(' token. */
3470 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3471 /* Types cannot be defined in a `typeid' expression. */
3472 saved_message = parser->type_definition_forbidden_message;
3473 parser->type_definition_forbidden_message
3474 = "types may not be defined in a `typeid\' expression";
3475 /* We can't be sure yet whether we're looking at a type-id or an
3476 expression. */
3477 cp_parser_parse_tentatively (parser);
3478 /* Try a type-id first. */
3479 type = cp_parser_type_id (parser);
3480 /* Look for the `)' token. Otherwise, we can't be sure that
3481 we're not looking at an expression: consider `typeid (int
3482 (3))', for example. */
3483 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3484 /* If all went well, simply lookup the type-id. */
3485 if (cp_parser_parse_definitely (parser))
3486 postfix_expression = get_typeid (type);
3487 /* Otherwise, fall back to the expression variant. */
3488 else
3489 {
3490 tree expression;
3491
3492 /* Look for an expression. */
3493 expression = cp_parser_expression (parser);
3494 /* Compute its typeid. */
3495 postfix_expression = build_typeid (expression);
3496 /* Look for the `)' token. */
3497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3498 }
3499
3500 /* Restore the saved message. */
3501 parser->type_definition_forbidden_message = saved_message;
3502 }
3503 break;
3504
3505 case RID_TYPENAME:
3506 {
3507 bool template_p = false;
3508 tree id;
3509 tree type;
3510
3511 /* Consume the `typename' token. */
3512 cp_lexer_consume_token (parser->lexer);
3513 /* Look for the optional `::' operator. */
3514 cp_parser_global_scope_opt (parser,
3515 /*current_scope_valid_p=*/false);
3516 /* Look for the nested-name-specifier. */
3517 cp_parser_nested_name_specifier (parser,
3518 /*typename_keyword_p=*/true,
3519 /*check_dependency_p=*/true,
a668c6ad
MM
3520 /*type_p=*/true,
3521 /*is_declaration=*/true);
a723baf1
MM
3522 /* Look for the optional `template' keyword. */
3523 template_p = cp_parser_optional_template_keyword (parser);
3524 /* We don't know whether we're looking at a template-id or an
3525 identifier. */
3526 cp_parser_parse_tentatively (parser);
3527 /* Try a template-id. */
3528 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3529 /*check_dependency_p=*/true,
3530 /*is_declaration=*/true);
a723baf1
MM
3531 /* If that didn't work, try an identifier. */
3532 if (!cp_parser_parse_definitely (parser))
3533 id = cp_parser_identifier (parser);
3534 /* Create a TYPENAME_TYPE to represent the type to which the
3535 functional cast is being performed. */
3536 type = make_typename_type (parser->scope, id,
3537 /*complain=*/1);
3538
3539 postfix_expression = cp_parser_functional_cast (parser, type);
3540 }
3541 break;
3542
3543 default:
3544 {
3545 tree type;
3546
3547 /* If the next thing is a simple-type-specifier, we may be
3548 looking at a functional cast. We could also be looking at
3549 an id-expression. So, we try the functional cast, and if
3550 that doesn't work we fall back to the primary-expression. */
3551 cp_parser_parse_tentatively (parser);
3552 /* Look for the simple-type-specifier. */
3553 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3554 CP_PARSER_FLAGS_NONE,
3555 /*identifier_p=*/false);
a723baf1
MM
3556 /* Parse the cast itself. */
3557 if (!cp_parser_error_occurred (parser))
3558 postfix_expression
3559 = cp_parser_functional_cast (parser, type);
3560 /* If that worked, we're done. */
3561 if (cp_parser_parse_definitely (parser))
3562 break;
3563
3564 /* If the functional-cast didn't work out, try a
3565 compound-literal. */
14d22dd6
MM
3566 if (cp_parser_allow_gnu_extensions_p (parser)
3567 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3568 {
3569 tree initializer_list = NULL_TREE;
3570
3571 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3572 /* Consume the `('. */
3573 cp_lexer_consume_token (parser->lexer);
3574 /* Parse the type. */
3575 type = cp_parser_type_id (parser);
3576 /* Look for the `)'. */
3577 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3578 /* Look for the `{'. */
3579 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3580 /* If things aren't going well, there's no need to
3581 keep going. */
3582 if (!cp_parser_error_occurred (parser))
a723baf1 3583 {
39703eb9 3584 bool non_constant_p;
14d22dd6
MM
3585 /* Parse the initializer-list. */
3586 initializer_list
39703eb9 3587 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3588 /* Allow a trailing `,'. */
3589 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3590 cp_lexer_consume_token (parser->lexer);
3591 /* Look for the final `}'. */
3592 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3593 }
3594 /* If that worked, we're definitely looking at a
3595 compound-literal expression. */
3596 if (cp_parser_parse_definitely (parser))
3597 {
3598 /* Warn the user that a compound literal is not
3599 allowed in standard C++. */
3600 if (pedantic)
3601 pedwarn ("ISO C++ forbids compound-literals");
3602 /* Form the representation of the compound-literal. */
3603 postfix_expression
3604 = finish_compound_literal (type, initializer_list);
3605 break;
3606 }
3607 }
3608
3609 /* It must be a primary-expression. */
3610 postfix_expression = cp_parser_primary_expression (parser,
3611 &idk,
3612 &qualifying_class);
3613 }
3614 break;
3615 }
3616
ee76b931
MM
3617 /* If we were avoiding committing to the processing of a
3618 qualified-id until we knew whether or not we had a
3619 pointer-to-member, we now know. */
089d6ea7 3620 if (qualifying_class)
a723baf1 3621 {
ee76b931 3622 bool done;
a723baf1 3623
ee76b931
MM
3624 /* Peek at the next token. */
3625 token = cp_lexer_peek_token (parser->lexer);
3626 done = (token->type != CPP_OPEN_SQUARE
3627 && token->type != CPP_OPEN_PAREN
3628 && token->type != CPP_DOT
3629 && token->type != CPP_DEREF
3630 && token->type != CPP_PLUS_PLUS
3631 && token->type != CPP_MINUS_MINUS);
3632
3633 postfix_expression = finish_qualified_id_expr (qualifying_class,
3634 postfix_expression,
3635 done,
3636 address_p);
3637 if (done)
3638 return postfix_expression;
a723baf1
MM
3639 }
3640
a723baf1
MM
3641 /* Keep looping until the postfix-expression is complete. */
3642 while (true)
3643 {
10b1d5e7
MM
3644 if (idk == CP_ID_KIND_UNQUALIFIED
3645 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3646 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3647 /* It is not a Koenig lookup function call. */
3648 postfix_expression
3649 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3650
3651 /* Peek at the next token. */
3652 token = cp_lexer_peek_token (parser->lexer);
3653
3654 switch (token->type)
3655 {
3656 case CPP_OPEN_SQUARE:
3657 /* postfix-expression [ expression ] */
3658 {
3659 tree index;
3660
3661 /* Consume the `[' token. */
3662 cp_lexer_consume_token (parser->lexer);
3663 /* Parse the index expression. */
3664 index = cp_parser_expression (parser);
3665 /* Look for the closing `]'. */
3666 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3667
3668 /* Build the ARRAY_REF. */
3669 postfix_expression
3670 = grok_array_decl (postfix_expression, index);
b3445994 3671 idk = CP_ID_KIND_NONE;
a5ac3982
MM
3672 /* Array references are not permitted in
3673 constant-expressions. */
3674 if (parser->constant_expression_p)
3675 {
3676 if (!parser->allow_non_constant_expression_p)
3677 postfix_expression
3678 = cp_parser_non_constant_expression ("an array reference");
3679 parser->non_constant_expression_p = true;
3680 }
a723baf1
MM
3681 }
3682 break;
3683
3684 case CPP_OPEN_PAREN:
3685 /* postfix-expression ( expression-list [opt] ) */
3686 {
6d80c4b9 3687 bool koenig_p;
39703eb9
MM
3688 tree args = (cp_parser_parenthesized_expression_list
3689 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3690
7efa3e22
NS
3691 if (args == error_mark_node)
3692 {
3693 postfix_expression = error_mark_node;
3694 break;
3695 }
3696
14d22dd6
MM
3697 /* Function calls are not permitted in
3698 constant-expressions. */
3699 if (parser->constant_expression_p)
3700 {
3701 if (!parser->allow_non_constant_expression_p)
a5ac3982
MM
3702 {
3703 postfix_expression
3704 = cp_parser_non_constant_expression ("a function call");
3705 break;
3706 }
14d22dd6
MM
3707 parser->non_constant_expression_p = true;
3708 }
a723baf1 3709
6d80c4b9 3710 koenig_p = false;
399dedb9
NS
3711 if (idk == CP_ID_KIND_UNQUALIFIED)
3712 {
3713 if (args
3714 && (is_overloaded_fn (postfix_expression)
3715 || DECL_P (postfix_expression)
3716 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3717 {
3718 koenig_p = true;
3719 postfix_expression
3720 = perform_koenig_lookup (postfix_expression, args);
3721 }
399dedb9
NS
3722 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3723 postfix_expression
3724 = unqualified_fn_lookup_error (postfix_expression);
3725 }
3726
d17811fd 3727 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3728 {
d17811fd
MM
3729 tree instance = TREE_OPERAND (postfix_expression, 0);
3730 tree fn = TREE_OPERAND (postfix_expression, 1);
3731
3732 if (processing_template_decl
3733 && (type_dependent_expression_p (instance)
3734 || (!BASELINK_P (fn)
3735 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3736 || type_dependent_expression_p (fn)
d17811fd
MM
3737 || any_type_dependent_arguments_p (args)))
3738 {
3739 postfix_expression
3740 = build_min_nt (CALL_EXPR, postfix_expression, args);
3741 break;
3742 }
3743
3744 postfix_expression
3745 = (build_new_method_call
3746 (instance, fn, args, NULL_TREE,
b3445994 3747 (idk == CP_ID_KIND_QUALIFIED
d17811fd 3748 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
a723baf1 3749 }
d17811fd
MM
3750 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3751 || TREE_CODE (postfix_expression) == MEMBER_REF
3752 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3753 postfix_expression = (build_offset_ref_call_from_tree
3754 (postfix_expression, args));
b3445994 3755 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3756 /* A call to a static class member, or a namespace-scope
3757 function. */
3758 postfix_expression
3759 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3760 /*disallow_virtual=*/true,
3761 koenig_p);
a723baf1 3762 else
2050a1bb
MM
3763 /* All other function calls. */
3764 postfix_expression
3765 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3766 /*disallow_virtual=*/false,
3767 koenig_p);
a723baf1
MM
3768
3769 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3770 idk = CP_ID_KIND_NONE;
a723baf1
MM
3771 }
3772 break;
3773
3774 case CPP_DOT:
3775 case CPP_DEREF:
3776 /* postfix-expression . template [opt] id-expression
3777 postfix-expression . pseudo-destructor-name
3778 postfix-expression -> template [opt] id-expression
3779 postfix-expression -> pseudo-destructor-name */
3780 {
3781 tree name;
3782 bool dependent_p;
3783 bool template_p;
3784 tree scope = NULL_TREE;
a5ac3982 3785 enum cpp_ttype token_type = token->type;
a723baf1
MM
3786
3787 /* If this is a `->' operator, dereference the pointer. */
3788 if (token->type == CPP_DEREF)
3789 postfix_expression = build_x_arrow (postfix_expression);
3790 /* Check to see whether or not the expression is
3791 type-dependent. */
bbaab916 3792 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3793 /* The identifier following the `->' or `.' is not
3794 qualified. */
3795 parser->scope = NULL_TREE;
3796 parser->qualifying_scope = NULL_TREE;
3797 parser->object_scope = NULL_TREE;
b3445994 3798 idk = CP_ID_KIND_NONE;
a723baf1
MM
3799 /* Enter the scope corresponding to the type of the object
3800 given by the POSTFIX_EXPRESSION. */
3801 if (!dependent_p
3802 && TREE_TYPE (postfix_expression) != NULL_TREE)
3803 {
3804 scope = TREE_TYPE (postfix_expression);
3805 /* According to the standard, no expression should
3806 ever have reference type. Unfortunately, we do not
3807 currently match the standard in this respect in
3808 that our internal representation of an expression
3809 may have reference type even when the standard says
3810 it does not. Therefore, we have to manually obtain
3811 the underlying type here. */
ee76b931 3812 scope = non_reference (scope);
a723baf1
MM
3813 /* The type of the POSTFIX_EXPRESSION must be
3814 complete. */
3815 scope = complete_type_or_else (scope, NULL_TREE);
3816 /* Let the name lookup machinery know that we are
3817 processing a class member access expression. */
3818 parser->context->object_type = scope;
3819 /* If something went wrong, we want to be able to
3820 discern that case, as opposed to the case where
3821 there was no SCOPE due to the type of expression
3822 being dependent. */
3823 if (!scope)
3824 scope = error_mark_node;
3825 }
3826
3827 /* Consume the `.' or `->' operator. */
3828 cp_lexer_consume_token (parser->lexer);
3829 /* If the SCOPE is not a scalar type, we are looking at an
3830 ordinary class member access expression, rather than a
3831 pseudo-destructor-name. */
3832 if (!scope || !SCALAR_TYPE_P (scope))
3833 {
3834 template_p = cp_parser_optional_template_keyword (parser);
3835 /* Parse the id-expression. */
3836 name = cp_parser_id_expression (parser,
3837 template_p,
3838 /*check_dependency_p=*/true,
f3c2dfc6
MM
3839 /*template_p=*/NULL,
3840 /*declarator_p=*/false);
a723baf1
MM
3841 /* In general, build a SCOPE_REF if the member name is
3842 qualified. However, if the name was not dependent
3843 and has already been resolved; there is no need to
3844 build the SCOPE_REF. For example;
3845
3846 struct X { void f(); };
3847 template <typename T> void f(T* t) { t->X::f(); }
3848
d17811fd
MM
3849 Even though "t" is dependent, "X::f" is not and has
3850 been resolved to a BASELINK; there is no need to
a723baf1 3851 include scope information. */
a6bd211d
JM
3852
3853 /* But we do need to remember that there was an explicit
3854 scope for virtual function calls. */
3855 if (parser->scope)
b3445994 3856 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3857
a723baf1
MM
3858 if (name != error_mark_node
3859 && !BASELINK_P (name)
3860 && parser->scope)
3861 {
3862 name = build_nt (SCOPE_REF, parser->scope, name);
3863 parser->scope = NULL_TREE;
3864 parser->qualifying_scope = NULL_TREE;
3865 parser->object_scope = NULL_TREE;
3866 }
3867 postfix_expression
3868 = finish_class_member_access_expr (postfix_expression, name);
3869 }
3870 /* Otherwise, try the pseudo-destructor-name production. */
3871 else
3872 {
90808894 3873 tree s = NULL_TREE;
a723baf1
MM
3874 tree type;
3875
3876 /* Parse the pseudo-destructor-name. */
3877 cp_parser_pseudo_destructor_name (parser, &s, &type);
3878 /* Form the call. */
3879 postfix_expression
3880 = finish_pseudo_destructor_expr (postfix_expression,
3881 s, TREE_TYPE (type));
3882 }
3883
3884 /* We no longer need to look up names in the scope of the
3885 object on the left-hand side of the `.' or `->'
3886 operator. */
3887 parser->context->object_type = NULL_TREE;
a5ac3982 3888 /* These operators may not appear in constant-expressions. */
263ee052
MM
3889 if (parser->constant_expression_p
3890 /* The "->" operator is allowed in the implementation
3891 of "offsetof". */
3892 && !(parser->in_offsetof_p && token_type == CPP_DEREF))
a5ac3982
MM
3893 {
3894 if (!parser->allow_non_constant_expression_p)
3895 postfix_expression
3896 = (cp_parser_non_constant_expression
3897 (token_type == CPP_DEREF ? "'->'" : "`.'"));
3898 parser->non_constant_expression_p = true;
3899 }
a723baf1
MM
3900 }
3901 break;
3902
3903 case CPP_PLUS_PLUS:
3904 /* postfix-expression ++ */
3905 /* Consume the `++' token. */
3906 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3907 /* Generate a representation for the complete expression. */
3908 postfix_expression
3909 = finish_increment_expr (postfix_expression,
3910 POSTINCREMENT_EXPR);
14d22dd6
MM
3911 /* Increments may not appear in constant-expressions. */
3912 if (parser->constant_expression_p)
3913 {
3914 if (!parser->allow_non_constant_expression_p)
a5ac3982
MM
3915 postfix_expression
3916 = cp_parser_non_constant_expression ("an increment");
14d22dd6
MM
3917 parser->non_constant_expression_p = true;
3918 }
b3445994 3919 idk = CP_ID_KIND_NONE;
a723baf1
MM
3920 break;
3921
3922 case CPP_MINUS_MINUS:
3923 /* postfix-expression -- */
3924 /* Consume the `--' token. */
3925 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3926 /* Generate a representation for the complete expression. */
3927 postfix_expression
3928 = finish_increment_expr (postfix_expression,
3929 POSTDECREMENT_EXPR);
14d22dd6
MM
3930 /* Decrements may not appear in constant-expressions. */
3931 if (parser->constant_expression_p)
3932 {
3933 if (!parser->allow_non_constant_expression_p)
a5ac3982
MM
3934 postfix_expression
3935 = cp_parser_non_constant_expression ("a decrement");
14d22dd6
MM
3936 parser->non_constant_expression_p = true;
3937 }
b3445994 3938 idk = CP_ID_KIND_NONE;
a723baf1
MM
3939 break;
3940
3941 default:
3942 return postfix_expression;
3943 }
3944 }
3945
3946 /* We should never get here. */
3947 abort ();
3948 return error_mark_node;
3949}
3950
7efa3e22 3951/* Parse a parenthesized expression-list.
a723baf1
MM
3952
3953 expression-list:
3954 assignment-expression
3955 expression-list, assignment-expression
3956
7efa3e22
NS
3957 attribute-list:
3958 expression-list
3959 identifier
3960 identifier, expression-list
3961
a723baf1
MM
3962 Returns a TREE_LIST. The TREE_VALUE of each node is a
3963 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
3964 is returned even if there is only a single expression in the list.
3965 error_mark_node is returned if the ( and or ) are
3966 missing. NULL_TREE is returned on no expressions. The parentheses
3967 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
3968 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3969 indicates whether or not all of the expressions in the list were
3970 constant. */
a723baf1
MM
3971
3972static tree
39703eb9
MM
3973cp_parser_parenthesized_expression_list (cp_parser* parser,
3974 bool is_attribute_list,
3975 bool *non_constant_p)
a723baf1
MM
3976{
3977 tree expression_list = NULL_TREE;
7efa3e22 3978 tree identifier = NULL_TREE;
39703eb9
MM
3979
3980 /* Assume all the expressions will be constant. */
3981 if (non_constant_p)
3982 *non_constant_p = false;
3983
7efa3e22
NS
3984 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3985 return error_mark_node;
3986
a723baf1 3987 /* Consume expressions until there are no more. */
7efa3e22
NS
3988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3989 while (true)
3990 {
3991 tree expr;
3992
3993 /* At the beginning of attribute lists, check to see if the
3994 next token is an identifier. */
3995 if (is_attribute_list
3996 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3997 {
3998 cp_token *token;
3999
4000 /* Consume the identifier. */
4001 token = cp_lexer_consume_token (parser->lexer);
4002 /* Save the identifier. */
4003 identifier = token->value;
4004 }
4005 else
4006 {
4007 /* Parse the next assignment-expression. */
39703eb9
MM
4008 if (non_constant_p)
4009 {
4010 bool expr_non_constant_p;
4011 expr = (cp_parser_constant_expression
4012 (parser, /*allow_non_constant_p=*/true,
4013 &expr_non_constant_p));
4014 if (expr_non_constant_p)
4015 *non_constant_p = true;
4016 }
4017 else
4018 expr = cp_parser_assignment_expression (parser);
a723baf1 4019
7efa3e22
NS
4020 /* Add it to the list. We add error_mark_node
4021 expressions to the list, so that we can still tell if
4022 the correct form for a parenthesized expression-list
4023 is found. That gives better errors. */
4024 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4025
7efa3e22
NS
4026 if (expr == error_mark_node)
4027 goto skip_comma;
4028 }
a723baf1 4029
7efa3e22
NS
4030 /* After the first item, attribute lists look the same as
4031 expression lists. */
4032 is_attribute_list = false;
4033
4034 get_comma:;
4035 /* If the next token isn't a `,', then we are done. */
4036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4037 break;
4038
4039 /* Otherwise, consume the `,' and keep going. */
4040 cp_lexer_consume_token (parser->lexer);
4041 }
4042
4043 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4044 {
4045 int ending;
4046
4047 skip_comma:;
4048 /* We try and resync to an unnested comma, as that will give the
4049 user better diagnostics. */
4bb8ca28
MM
4050 ending = cp_parser_skip_to_closing_parenthesis (parser,
4051 /*recovering=*/true,
4052 /*or_comma=*/true,
a668c6ad 4053 /*consume_paren=*/true);
7efa3e22
NS
4054 if (ending < 0)
4055 goto get_comma;
4056 if (!ending)
4057 return error_mark_node;
a723baf1
MM
4058 }
4059
4060 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4061 expression_list = nreverse (expression_list);
4062 if (identifier)
4063 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4064
4065 return expression_list;
a723baf1
MM
4066}
4067
4068/* Parse a pseudo-destructor-name.
4069
4070 pseudo-destructor-name:
4071 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4072 :: [opt] nested-name-specifier template template-id :: ~ type-name
4073 :: [opt] nested-name-specifier [opt] ~ type-name
4074
4075 If either of the first two productions is used, sets *SCOPE to the
4076 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4077 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4078 or ERROR_MARK_NODE if no type-name is present. */
4079
4080static void
94edc4ab
NN
4081cp_parser_pseudo_destructor_name (cp_parser* parser,
4082 tree* scope,
4083 tree* type)
a723baf1
MM
4084{
4085 bool nested_name_specifier_p;
4086
4087 /* Look for the optional `::' operator. */
4088 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4089 /* Look for the optional nested-name-specifier. */
4090 nested_name_specifier_p
4091 = (cp_parser_nested_name_specifier_opt (parser,
4092 /*typename_keyword_p=*/false,
4093 /*check_dependency_p=*/true,
a668c6ad
MM
4094 /*type_p=*/false,
4095 /*is_declaration=*/true)
a723baf1
MM
4096 != NULL_TREE);
4097 /* Now, if we saw a nested-name-specifier, we might be doing the
4098 second production. */
4099 if (nested_name_specifier_p
4100 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4101 {
4102 /* Consume the `template' keyword. */
4103 cp_lexer_consume_token (parser->lexer);
4104 /* Parse the template-id. */
4105 cp_parser_template_id (parser,
4106 /*template_keyword_p=*/true,
a668c6ad
MM
4107 /*check_dependency_p=*/false,
4108 /*is_declaration=*/true);
a723baf1
MM
4109 /* Look for the `::' token. */
4110 cp_parser_require (parser, CPP_SCOPE, "`::'");
4111 }
4112 /* If the next token is not a `~', then there might be some
9bcb9aae 4113 additional qualification. */
a723baf1
MM
4114 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4115 {
4116 /* Look for the type-name. */
4117 *scope = TREE_TYPE (cp_parser_type_name (parser));
4118 /* Look for the `::' token. */
4119 cp_parser_require (parser, CPP_SCOPE, "`::'");
4120 }
4121 else
4122 *scope = NULL_TREE;
4123
4124 /* Look for the `~'. */
4125 cp_parser_require (parser, CPP_COMPL, "`~'");
4126 /* Look for the type-name again. We are not responsible for
4127 checking that it matches the first type-name. */
4128 *type = cp_parser_type_name (parser);
4129}
4130
4131/* Parse a unary-expression.
4132
4133 unary-expression:
4134 postfix-expression
4135 ++ cast-expression
4136 -- cast-expression
4137 unary-operator cast-expression
4138 sizeof unary-expression
4139 sizeof ( type-id )
4140 new-expression
4141 delete-expression
4142
4143 GNU Extensions:
4144
4145 unary-expression:
4146 __extension__ cast-expression
4147 __alignof__ unary-expression
4148 __alignof__ ( type-id )
4149 __real__ cast-expression
4150 __imag__ cast-expression
4151 && identifier
4152
4153 ADDRESS_P is true iff the unary-expression is appearing as the
4154 operand of the `&' operator.
4155
34cd5ae7 4156 Returns a representation of the expression. */
a723baf1
MM
4157
4158static tree
4159cp_parser_unary_expression (cp_parser *parser, bool address_p)
4160{
4161 cp_token *token;
4162 enum tree_code unary_operator;
4163
4164 /* Peek at the next token. */
4165 token = cp_lexer_peek_token (parser->lexer);
4166 /* Some keywords give away the kind of expression. */
4167 if (token->type == CPP_KEYWORD)
4168 {
4169 enum rid keyword = token->keyword;
4170
4171 switch (keyword)
4172 {
4173 case RID_ALIGNOF:
a723baf1
MM
4174 case RID_SIZEOF:
4175 {
4176 tree operand;
7a18b933 4177 enum tree_code op;
a723baf1 4178
7a18b933
NS
4179 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4180 /* Consume the token. */
a723baf1
MM
4181 cp_lexer_consume_token (parser->lexer);
4182 /* Parse the operand. */
4183 operand = cp_parser_sizeof_operand (parser, keyword);
4184
7a18b933
NS
4185 if (TYPE_P (operand))
4186 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4187 else
7a18b933 4188 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4189 }
4190
4191 case RID_NEW:
4192 return cp_parser_new_expression (parser);
4193
4194 case RID_DELETE:
4195 return cp_parser_delete_expression (parser);
4196
4197 case RID_EXTENSION:
4198 {
4199 /* The saved value of the PEDANTIC flag. */
4200 int saved_pedantic;
4201 tree expr;
4202
4203 /* Save away the PEDANTIC flag. */
4204 cp_parser_extension_opt (parser, &saved_pedantic);
4205 /* Parse the cast-expression. */
d6b4ea85 4206 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4207 /* Restore the PEDANTIC flag. */
4208 pedantic = saved_pedantic;
4209
4210 return expr;
4211 }
4212
4213 case RID_REALPART:
4214 case RID_IMAGPART:
4215 {
4216 tree expression;
4217
4218 /* Consume the `__real__' or `__imag__' token. */
4219 cp_lexer_consume_token (parser->lexer);
4220 /* Parse the cast-expression. */
d6b4ea85 4221 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4222 /* Create the complete representation. */
4223 return build_x_unary_op ((keyword == RID_REALPART
4224 ? REALPART_EXPR : IMAGPART_EXPR),
4225 expression);
4226 }
4227 break;
4228
4229 default:
4230 break;
4231 }
4232 }
4233
4234 /* Look for the `:: new' and `:: delete', which also signal the
4235 beginning of a new-expression, or delete-expression,
4236 respectively. If the next token is `::', then it might be one of
4237 these. */
4238 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4239 {
4240 enum rid keyword;
4241
4242 /* See if the token after the `::' is one of the keywords in
4243 which we're interested. */
4244 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4245 /* If it's `new', we have a new-expression. */
4246 if (keyword == RID_NEW)
4247 return cp_parser_new_expression (parser);
4248 /* Similarly, for `delete'. */
4249 else if (keyword == RID_DELETE)
4250 return cp_parser_delete_expression (parser);
4251 }
4252
4253 /* Look for a unary operator. */
4254 unary_operator = cp_parser_unary_operator (token);
4255 /* The `++' and `--' operators can be handled similarly, even though
4256 they are not technically unary-operators in the grammar. */
4257 if (unary_operator == ERROR_MARK)
4258 {
4259 if (token->type == CPP_PLUS_PLUS)
4260 unary_operator = PREINCREMENT_EXPR;
4261 else if (token->type == CPP_MINUS_MINUS)
4262 unary_operator = PREDECREMENT_EXPR;
4263 /* Handle the GNU address-of-label extension. */
4264 else if (cp_parser_allow_gnu_extensions_p (parser)
4265 && token->type == CPP_AND_AND)
4266 {
4267 tree identifier;
4268
4269 /* Consume the '&&' token. */
4270 cp_lexer_consume_token (parser->lexer);
4271 /* Look for the identifier. */
4272 identifier = cp_parser_identifier (parser);
4273 /* Create an expression representing the address. */
4274 return finish_label_address_expr (identifier);
4275 }
4276 }
4277 if (unary_operator != ERROR_MARK)
4278 {
4279 tree cast_expression;
a5ac3982
MM
4280 tree expression = error_mark_node;
4281 const char *non_constant_p = NULL;
a723baf1
MM
4282
4283 /* Consume the operator token. */
4284 token = cp_lexer_consume_token (parser->lexer);
4285 /* Parse the cast-expression. */
4286 cast_expression
4287 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4288 /* Now, build an appropriate representation. */
4289 switch (unary_operator)
4290 {
4291 case INDIRECT_REF:
a5ac3982
MM
4292 non_constant_p = "`*'";
4293 expression = build_x_indirect_ref (cast_expression, "unary *");
4294 break;
4295
a723baf1 4296 case ADDR_EXPR:
263ee052
MM
4297 /* The "&" operator is allowed in the implementation of
4298 "offsetof". */
4299 if (!parser->in_offsetof_p)
4300 non_constant_p = "`&'";
a5ac3982 4301 /* Fall through. */
d17811fd 4302 case BIT_NOT_EXPR:
a5ac3982
MM
4303 expression = build_x_unary_op (unary_operator, cast_expression);
4304 break;
4305
14d22dd6
MM
4306 case PREINCREMENT_EXPR:
4307 case PREDECREMENT_EXPR:
a5ac3982
MM
4308 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4309 ? "`++'" : "`--'");
14d22dd6 4310 /* Fall through. */
a723baf1
MM
4311 case CONVERT_EXPR:
4312 case NEGATE_EXPR:
4313 case TRUTH_NOT_EXPR:
a5ac3982
MM
4314 expression = finish_unary_op_expr (unary_operator, cast_expression);
4315 break;
a723baf1 4316
a723baf1
MM
4317 default:
4318 abort ();
a723baf1 4319 }
a5ac3982
MM
4320
4321 if (non_constant_p && parser->constant_expression_p)
4322 {
4323 if (!parser->allow_non_constant_expression_p)
4324 return cp_parser_non_constant_expression (non_constant_p);
4325 parser->non_constant_expression_p = true;
4326 }
4327
4328 return expression;
a723baf1
MM
4329 }
4330
4331 return cp_parser_postfix_expression (parser, address_p);
4332}
4333
4334/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4335 unary-operator, the corresponding tree code is returned. */
4336
4337static enum tree_code
94edc4ab 4338cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4339{
4340 switch (token->type)
4341 {
4342 case CPP_MULT:
4343 return INDIRECT_REF;
4344
4345 case CPP_AND:
4346 return ADDR_EXPR;
4347
4348 case CPP_PLUS:
4349 return CONVERT_EXPR;
4350
4351 case CPP_MINUS:
4352 return NEGATE_EXPR;
4353
4354 case CPP_NOT:
4355 return TRUTH_NOT_EXPR;
4356
4357 case CPP_COMPL:
4358 return BIT_NOT_EXPR;
4359
4360 default:
4361 return ERROR_MARK;
4362 }
4363}
4364
4365/* Parse a new-expression.
4366
ca099ac8 4367 new-expression:
a723baf1
MM
4368 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4369 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4370
4371 Returns a representation of the expression. */
4372
4373static tree
94edc4ab 4374cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4375{
4376 bool global_scope_p;
4377 tree placement;
4378 tree type;
4379 tree initializer;
4380
4381 /* Look for the optional `::' operator. */
4382 global_scope_p
4383 = (cp_parser_global_scope_opt (parser,
4384 /*current_scope_valid_p=*/false)
4385 != NULL_TREE);
4386 /* Look for the `new' operator. */
4387 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4388 /* There's no easy way to tell a new-placement from the
4389 `( type-id )' construct. */
4390 cp_parser_parse_tentatively (parser);
4391 /* Look for a new-placement. */
4392 placement = cp_parser_new_placement (parser);
4393 /* If that didn't work out, there's no new-placement. */
4394 if (!cp_parser_parse_definitely (parser))
4395 placement = NULL_TREE;
4396
4397 /* If the next token is a `(', then we have a parenthesized
4398 type-id. */
4399 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4400 {
4401 /* Consume the `('. */
4402 cp_lexer_consume_token (parser->lexer);
4403 /* Parse the type-id. */
4404 type = cp_parser_type_id (parser);
4405 /* Look for the closing `)'. */
4406 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4407 }
4408 /* Otherwise, there must be a new-type-id. */
4409 else
4410 type = cp_parser_new_type_id (parser);
4411
4412 /* If the next token is a `(', then we have a new-initializer. */
4413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4414 initializer = cp_parser_new_initializer (parser);
4415 else
4416 initializer = NULL_TREE;
4417
4418 /* Create a representation of the new-expression. */
4419 return build_new (placement, type, initializer, global_scope_p);
4420}
4421
4422/* Parse a new-placement.
4423
4424 new-placement:
4425 ( expression-list )
4426
4427 Returns the same representation as for an expression-list. */
4428
4429static tree
94edc4ab 4430cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4431{
4432 tree expression_list;
4433
a723baf1 4434 /* Parse the expression-list. */
39703eb9
MM
4435 expression_list = (cp_parser_parenthesized_expression_list
4436 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4437
4438 return expression_list;
4439}
4440
4441/* Parse a new-type-id.
4442
4443 new-type-id:
4444 type-specifier-seq new-declarator [opt]
4445
4446 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4447 and whose TREE_VALUE is the new-declarator. */
4448
4449static tree
94edc4ab 4450cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4451{
4452 tree type_specifier_seq;
4453 tree declarator;
4454 const char *saved_message;
4455
4456 /* The type-specifier sequence must not contain type definitions.
4457 (It cannot contain declarations of new types either, but if they
4458 are not definitions we will catch that because they are not
4459 complete.) */
4460 saved_message = parser->type_definition_forbidden_message;
4461 parser->type_definition_forbidden_message
4462 = "types may not be defined in a new-type-id";
4463 /* Parse the type-specifier-seq. */
4464 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4465 /* Restore the old message. */
4466 parser->type_definition_forbidden_message = saved_message;
4467 /* Parse the new-declarator. */
4468 declarator = cp_parser_new_declarator_opt (parser);
4469
4470 return build_tree_list (type_specifier_seq, declarator);
4471}
4472
4473/* Parse an (optional) new-declarator.
4474
4475 new-declarator:
4476 ptr-operator new-declarator [opt]
4477 direct-new-declarator
4478
4479 Returns a representation of the declarator. See
4480 cp_parser_declarator for the representations used. */
4481
4482static tree
94edc4ab 4483cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4484{
4485 enum tree_code code;
4486 tree type;
4487 tree cv_qualifier_seq;
4488
4489 /* We don't know if there's a ptr-operator next, or not. */
4490 cp_parser_parse_tentatively (parser);
4491 /* Look for a ptr-operator. */
4492 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4493 /* If that worked, look for more new-declarators. */
4494 if (cp_parser_parse_definitely (parser))
4495 {
4496 tree declarator;
4497
4498 /* Parse another optional declarator. */
4499 declarator = cp_parser_new_declarator_opt (parser);
4500
4501 /* Create the representation of the declarator. */
4502 if (code == INDIRECT_REF)
4503 declarator = make_pointer_declarator (cv_qualifier_seq,
4504 declarator);
4505 else
4506 declarator = make_reference_declarator (cv_qualifier_seq,
4507 declarator);
4508
4509 /* Handle the pointer-to-member case. */
4510 if (type)
4511 declarator = build_nt (SCOPE_REF, type, declarator);
4512
4513 return declarator;
4514 }
4515
4516 /* If the next token is a `[', there is a direct-new-declarator. */
4517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4518 return cp_parser_direct_new_declarator (parser);
4519
4520 return NULL_TREE;
4521}
4522
4523/* Parse a direct-new-declarator.
4524
4525 direct-new-declarator:
4526 [ expression ]
4527 direct-new-declarator [constant-expression]
4528
4529 Returns an ARRAY_REF, following the same conventions as are
4530 documented for cp_parser_direct_declarator. */
4531
4532static tree
94edc4ab 4533cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4534{
4535 tree declarator = NULL_TREE;
4536
4537 while (true)
4538 {
4539 tree expression;
4540
4541 /* Look for the opening `['. */
4542 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4543 /* The first expression is not required to be constant. */
4544 if (!declarator)
4545 {
4546 expression = cp_parser_expression (parser);
4547 /* The standard requires that the expression have integral
4548 type. DR 74 adds enumeration types. We believe that the
4549 real intent is that these expressions be handled like the
4550 expression in a `switch' condition, which also allows
4551 classes with a single conversion to integral or
4552 enumeration type. */
4553 if (!processing_template_decl)
4554 {
4555 expression
4556 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4557 expression,
b746c5dc 4558 /*complain=*/true);
a723baf1
MM
4559 if (!expression)
4560 {
4561 error ("expression in new-declarator must have integral or enumeration type");
4562 expression = error_mark_node;
4563 }
4564 }
4565 }
4566 /* But all the other expressions must be. */
4567 else
14d22dd6
MM
4568 expression
4569 = cp_parser_constant_expression (parser,
4570 /*allow_non_constant=*/false,
4571 NULL);
a723baf1
MM
4572 /* Look for the closing `]'. */
4573 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4574
4575 /* Add this bound to the declarator. */
4576 declarator = build_nt (ARRAY_REF, declarator, expression);
4577
4578 /* If the next token is not a `[', then there are no more
4579 bounds. */
4580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4581 break;
4582 }
4583
4584 return declarator;
4585}
4586
4587/* Parse a new-initializer.
4588
4589 new-initializer:
4590 ( expression-list [opt] )
4591
34cd5ae7 4592 Returns a representation of the expression-list. If there is no
a723baf1
MM
4593 expression-list, VOID_ZERO_NODE is returned. */
4594
4595static tree
94edc4ab 4596cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4597{
4598 tree expression_list;
4599
39703eb9
MM
4600 expression_list = (cp_parser_parenthesized_expression_list
4601 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4602 if (!expression_list)
a723baf1 4603 expression_list = void_zero_node;
a723baf1
MM
4604
4605 return expression_list;
4606}
4607
4608/* Parse a delete-expression.
4609
4610 delete-expression:
4611 :: [opt] delete cast-expression
4612 :: [opt] delete [ ] cast-expression
4613
4614 Returns a representation of the expression. */
4615
4616static tree
94edc4ab 4617cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4618{
4619 bool global_scope_p;
4620 bool array_p;
4621 tree expression;
4622
4623 /* Look for the optional `::' operator. */
4624 global_scope_p
4625 = (cp_parser_global_scope_opt (parser,
4626 /*current_scope_valid_p=*/false)
4627 != NULL_TREE);
4628 /* Look for the `delete' keyword. */
4629 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4630 /* See if the array syntax is in use. */
4631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4632 {
4633 /* Consume the `[' token. */
4634 cp_lexer_consume_token (parser->lexer);
4635 /* Look for the `]' token. */
4636 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4637 /* Remember that this is the `[]' construct. */
4638 array_p = true;
4639 }
4640 else
4641 array_p = false;
4642
4643 /* Parse the cast-expression. */
d6b4ea85 4644 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4645
4646 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4647}
4648
4649/* Parse a cast-expression.
4650
4651 cast-expression:
4652 unary-expression
4653 ( type-id ) cast-expression
4654
4655 Returns a representation of the expression. */
4656
4657static tree
4658cp_parser_cast_expression (cp_parser *parser, bool address_p)
4659{
4660 /* If it's a `(', then we might be looking at a cast. */
4661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4662 {
4663 tree type = NULL_TREE;
4664 tree expr = NULL_TREE;
4665 bool compound_literal_p;
4666 const char *saved_message;
4667
4668 /* There's no way to know yet whether or not this is a cast.
4669 For example, `(int (3))' is a unary-expression, while `(int)
4670 3' is a cast. So, we resort to parsing tentatively. */
4671 cp_parser_parse_tentatively (parser);
4672 /* Types may not be defined in a cast. */
4673 saved_message = parser->type_definition_forbidden_message;
4674 parser->type_definition_forbidden_message
4675 = "types may not be defined in casts";
4676 /* Consume the `('. */
4677 cp_lexer_consume_token (parser->lexer);
4678 /* A very tricky bit is that `(struct S) { 3 }' is a
4679 compound-literal (which we permit in C++ as an extension).
4680 But, that construct is not a cast-expression -- it is a
4681 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4682 is legal; if the compound-literal were a cast-expression,
4683 you'd need an extra set of parentheses.) But, if we parse
4684 the type-id, and it happens to be a class-specifier, then we
4685 will commit to the parse at that point, because we cannot
4686 undo the action that is done when creating a new class. So,
4687 then we cannot back up and do a postfix-expression.
4688
4689 Therefore, we scan ahead to the closing `)', and check to see
4690 if the token after the `)' is a `{'. If so, we are not
4691 looking at a cast-expression.
4692
4693 Save tokens so that we can put them back. */
4694 cp_lexer_save_tokens (parser->lexer);
4695 /* Skip tokens until the next token is a closing parenthesis.
4696 If we find the closing `)', and the next token is a `{', then
4697 we are looking at a compound-literal. */
4698 compound_literal_p
a668c6ad
MM
4699 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4700 /*consume_paren=*/true)
a723baf1
MM
4701 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4702 /* Roll back the tokens we skipped. */
4703 cp_lexer_rollback_tokens (parser->lexer);
4704 /* If we were looking at a compound-literal, simulate an error
4705 so that the call to cp_parser_parse_definitely below will
4706 fail. */
4707 if (compound_literal_p)
4708 cp_parser_simulate_error (parser);
4709 else
4710 {
4711 /* Look for the type-id. */
4712 type = cp_parser_type_id (parser);
4713 /* Look for the closing `)'. */
4714 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4715 }
4716
4717 /* Restore the saved message. */
4718 parser->type_definition_forbidden_message = saved_message;
4719
bbaab916
NS
4720 /* If ok so far, parse the dependent expression. We cannot be
4721 sure it is a cast. Consider `(T ())'. It is a parenthesized
4722 ctor of T, but looks like a cast to function returning T
4723 without a dependent expression. */
4724 if (!cp_parser_error_occurred (parser))
d6b4ea85 4725 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4726
a723baf1
MM
4727 if (cp_parser_parse_definitely (parser))
4728 {
a723baf1
MM
4729 /* Warn about old-style casts, if so requested. */
4730 if (warn_old_style_cast
4731 && !in_system_header
4732 && !VOID_TYPE_P (type)
4733 && current_lang_name != lang_name_c)
4734 warning ("use of old-style cast");
14d22dd6
MM
4735
4736 /* Only type conversions to integral or enumeration types
4737 can be used in constant-expressions. */
4738 if (parser->constant_expression_p
4739 && !dependent_type_p (type)
4740 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4741 {
4742 if (!parser->allow_non_constant_expression_p)
4743 return (cp_parser_non_constant_expression
4744 ("a casts to a type other than an integral or "
4745 "enumeration type"));
4746 parser->non_constant_expression_p = true;
4747 }
a723baf1
MM
4748 /* Perform the cast. */
4749 expr = build_c_cast (type, expr);
bbaab916 4750 return expr;
a723baf1 4751 }
a723baf1
MM
4752 }
4753
4754 /* If we get here, then it's not a cast, so it must be a
4755 unary-expression. */
4756 return cp_parser_unary_expression (parser, address_p);
4757}
4758
4759/* Parse a pm-expression.
4760
4761 pm-expression:
4762 cast-expression
4763 pm-expression .* cast-expression
4764 pm-expression ->* cast-expression
4765
4766 Returns a representation of the expression. */
4767
4768static tree
94edc4ab 4769cp_parser_pm_expression (cp_parser* parser)
a723baf1 4770{
d6b4ea85
MM
4771 static const cp_parser_token_tree_map map = {
4772 { CPP_DEREF_STAR, MEMBER_REF },
4773 { CPP_DOT_STAR, DOTSTAR_EXPR },
4774 { CPP_EOF, ERROR_MARK }
4775 };
a723baf1 4776
d6b4ea85
MM
4777 return cp_parser_binary_expression (parser, map,
4778 cp_parser_simple_cast_expression);
a723baf1
MM
4779}
4780
4781/* Parse a multiplicative-expression.
4782
4783 mulitplicative-expression:
4784 pm-expression
4785 multiplicative-expression * pm-expression
4786 multiplicative-expression / pm-expression
4787 multiplicative-expression % pm-expression
4788
4789 Returns a representation of the expression. */
4790
4791static tree
94edc4ab 4792cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4793{
39b1af70 4794 static const cp_parser_token_tree_map map = {
a723baf1
MM
4795 { CPP_MULT, MULT_EXPR },
4796 { CPP_DIV, TRUNC_DIV_EXPR },
4797 { CPP_MOD, TRUNC_MOD_EXPR },
4798 { CPP_EOF, ERROR_MARK }
4799 };
4800
4801 return cp_parser_binary_expression (parser,
4802 map,
4803 cp_parser_pm_expression);
4804}
4805
4806/* Parse an additive-expression.
4807
4808 additive-expression:
4809 multiplicative-expression
4810 additive-expression + multiplicative-expression
4811 additive-expression - multiplicative-expression
4812
4813 Returns a representation of the expression. */
4814
4815static tree
94edc4ab 4816cp_parser_additive_expression (cp_parser* parser)
a723baf1 4817{
39b1af70 4818 static const cp_parser_token_tree_map map = {
a723baf1
MM
4819 { CPP_PLUS, PLUS_EXPR },
4820 { CPP_MINUS, MINUS_EXPR },
4821 { CPP_EOF, ERROR_MARK }
4822 };
4823
4824 return cp_parser_binary_expression (parser,
4825 map,
4826 cp_parser_multiplicative_expression);
4827}
4828
4829/* Parse a shift-expression.
4830
4831 shift-expression:
4832 additive-expression
4833 shift-expression << additive-expression
4834 shift-expression >> additive-expression
4835
4836 Returns a representation of the expression. */
4837
4838static tree
94edc4ab 4839cp_parser_shift_expression (cp_parser* parser)
a723baf1 4840{
39b1af70 4841 static const cp_parser_token_tree_map map = {
a723baf1
MM
4842 { CPP_LSHIFT, LSHIFT_EXPR },
4843 { CPP_RSHIFT, RSHIFT_EXPR },
4844 { CPP_EOF, ERROR_MARK }
4845 };
4846
4847 return cp_parser_binary_expression (parser,
4848 map,
4849 cp_parser_additive_expression);
4850}
4851
4852/* Parse a relational-expression.
4853
4854 relational-expression:
4855 shift-expression
4856 relational-expression < shift-expression
4857 relational-expression > shift-expression
4858 relational-expression <= shift-expression
4859 relational-expression >= shift-expression
4860
4861 GNU Extension:
4862
4863 relational-expression:
4864 relational-expression <? shift-expression
4865 relational-expression >? shift-expression
4866
4867 Returns a representation of the expression. */
4868
4869static tree
94edc4ab 4870cp_parser_relational_expression (cp_parser* parser)
a723baf1 4871{
39b1af70 4872 static const cp_parser_token_tree_map map = {
a723baf1
MM
4873 { CPP_LESS, LT_EXPR },
4874 { CPP_GREATER, GT_EXPR },
4875 { CPP_LESS_EQ, LE_EXPR },
4876 { CPP_GREATER_EQ, GE_EXPR },
4877 { CPP_MIN, MIN_EXPR },
4878 { CPP_MAX, MAX_EXPR },
4879 { CPP_EOF, ERROR_MARK }
4880 };
4881
4882 return cp_parser_binary_expression (parser,
4883 map,
4884 cp_parser_shift_expression);
4885}
4886
4887/* Parse an equality-expression.
4888
4889 equality-expression:
4890 relational-expression
4891 equality-expression == relational-expression
4892 equality-expression != relational-expression
4893
4894 Returns a representation of the expression. */
4895
4896static tree
94edc4ab 4897cp_parser_equality_expression (cp_parser* parser)
a723baf1 4898{
39b1af70 4899 static const cp_parser_token_tree_map map = {
a723baf1
MM
4900 { CPP_EQ_EQ, EQ_EXPR },
4901 { CPP_NOT_EQ, NE_EXPR },
4902 { CPP_EOF, ERROR_MARK }
4903 };
4904
4905 return cp_parser_binary_expression (parser,
4906 map,
4907 cp_parser_relational_expression);
4908}
4909
4910/* Parse an and-expression.
4911
4912 and-expression:
4913 equality-expression
4914 and-expression & equality-expression
4915
4916 Returns a representation of the expression. */
4917
4918static tree
94edc4ab 4919cp_parser_and_expression (cp_parser* parser)
a723baf1 4920{
39b1af70 4921 static const cp_parser_token_tree_map map = {
a723baf1
MM
4922 { CPP_AND, BIT_AND_EXPR },
4923 { CPP_EOF, ERROR_MARK }
4924 };
4925
4926 return cp_parser_binary_expression (parser,
4927 map,
4928 cp_parser_equality_expression);
4929}
4930
4931/* Parse an exclusive-or-expression.
4932
4933 exclusive-or-expression:
4934 and-expression
4935 exclusive-or-expression ^ and-expression
4936
4937 Returns a representation of the expression. */
4938
4939static tree
94edc4ab 4940cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4941{
39b1af70 4942 static const cp_parser_token_tree_map map = {
a723baf1
MM
4943 { CPP_XOR, BIT_XOR_EXPR },
4944 { CPP_EOF, ERROR_MARK }
4945 };
4946
4947 return cp_parser_binary_expression (parser,
4948 map,
4949 cp_parser_and_expression);
4950}
4951
4952
4953/* Parse an inclusive-or-expression.
4954
4955 inclusive-or-expression:
4956 exclusive-or-expression
4957 inclusive-or-expression | exclusive-or-expression
4958
4959 Returns a representation of the expression. */
4960
4961static tree
94edc4ab 4962cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 4963{
39b1af70 4964 static const cp_parser_token_tree_map map = {
a723baf1
MM
4965 { CPP_OR, BIT_IOR_EXPR },
4966 { CPP_EOF, ERROR_MARK }
4967 };
4968
4969 return cp_parser_binary_expression (parser,
4970 map,
4971 cp_parser_exclusive_or_expression);
4972}
4973
4974/* Parse a logical-and-expression.
4975
4976 logical-and-expression:
4977 inclusive-or-expression
4978 logical-and-expression && inclusive-or-expression
4979
4980 Returns a representation of the expression. */
4981
4982static tree
94edc4ab 4983cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 4984{
39b1af70 4985 static const cp_parser_token_tree_map map = {
a723baf1
MM
4986 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4987 { CPP_EOF, ERROR_MARK }
4988 };
4989
4990 return cp_parser_binary_expression (parser,
4991 map,
4992 cp_parser_inclusive_or_expression);
4993}
4994
4995/* Parse a logical-or-expression.
4996
4997 logical-or-expression:
34cd5ae7 4998 logical-and-expression
a723baf1
MM
4999 logical-or-expression || logical-and-expression
5000
5001 Returns a representation of the expression. */
5002
5003static tree
94edc4ab 5004cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5005{
39b1af70 5006 static const cp_parser_token_tree_map map = {
a723baf1
MM
5007 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5008 { CPP_EOF, ERROR_MARK }
5009 };
5010
5011 return cp_parser_binary_expression (parser,
5012 map,
5013 cp_parser_logical_and_expression);
5014}
5015
a723baf1
MM
5016/* Parse the `? expression : assignment-expression' part of a
5017 conditional-expression. The LOGICAL_OR_EXPR is the
5018 logical-or-expression that started the conditional-expression.
5019 Returns a representation of the entire conditional-expression.
5020
39703eb9 5021 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5022
5023 ? expression : assignment-expression
5024
5025 GNU Extensions:
5026
5027 ? : assignment-expression */
5028
5029static tree
94edc4ab 5030cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5031{
5032 tree expr;
5033 tree assignment_expr;
5034
5035 /* Consume the `?' token. */
5036 cp_lexer_consume_token (parser->lexer);
5037 if (cp_parser_allow_gnu_extensions_p (parser)
5038 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5039 /* Implicit true clause. */
5040 expr = NULL_TREE;
5041 else
5042 /* Parse the expression. */
5043 expr = cp_parser_expression (parser);
5044
5045 /* The next token should be a `:'. */
5046 cp_parser_require (parser, CPP_COLON, "`:'");
5047 /* Parse the assignment-expression. */
5048 assignment_expr = cp_parser_assignment_expression (parser);
5049
5050 /* Build the conditional-expression. */
5051 return build_x_conditional_expr (logical_or_expr,
5052 expr,
5053 assignment_expr);
5054}
5055
5056/* Parse an assignment-expression.
5057
5058 assignment-expression:
5059 conditional-expression
5060 logical-or-expression assignment-operator assignment_expression
5061 throw-expression
5062
5063 Returns a representation for the expression. */
5064
5065static tree
94edc4ab 5066cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5067{
5068 tree expr;
5069
5070 /* If the next token is the `throw' keyword, then we're looking at
5071 a throw-expression. */
5072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5073 expr = cp_parser_throw_expression (parser);
5074 /* Otherwise, it must be that we are looking at a
5075 logical-or-expression. */
5076 else
5077 {
5078 /* Parse the logical-or-expression. */
5079 expr = cp_parser_logical_or_expression (parser);
5080 /* If the next token is a `?' then we're actually looking at a
5081 conditional-expression. */
5082 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5083 return cp_parser_question_colon_clause (parser, expr);
5084 else
5085 {
5086 enum tree_code assignment_operator;
5087
5088 /* If it's an assignment-operator, we're using the second
5089 production. */
5090 assignment_operator
5091 = cp_parser_assignment_operator_opt (parser);
5092 if (assignment_operator != ERROR_MARK)
5093 {
5094 tree rhs;
5095
5096 /* Parse the right-hand side of the assignment. */
5097 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5098 /* An assignment may not appear in a
5099 constant-expression. */
5100 if (parser->constant_expression_p)
5101 {
5102 if (!parser->allow_non_constant_expression_p)
5103 return cp_parser_non_constant_expression ("an assignment");
5104 parser->non_constant_expression_p = true;
5105 }
34cd5ae7 5106 /* Build the assignment expression. */
a723baf1
MM
5107 expr = build_x_modify_expr (expr,
5108 assignment_operator,
5109 rhs);
5110 }
5111 }
5112 }
5113
5114 return expr;
5115}
5116
5117/* Parse an (optional) assignment-operator.
5118
5119 assignment-operator: one of
5120 = *= /= %= += -= >>= <<= &= ^= |=
5121
5122 GNU Extension:
5123
5124 assignment-operator: one of
5125 <?= >?=
5126
5127 If the next token is an assignment operator, the corresponding tree
5128 code is returned, and the token is consumed. For example, for
5129 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5130 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5131 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5132 operator, ERROR_MARK is returned. */
5133
5134static enum tree_code
94edc4ab 5135cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5136{
5137 enum tree_code op;
5138 cp_token *token;
5139
5140 /* Peek at the next toen. */
5141 token = cp_lexer_peek_token (parser->lexer);
5142
5143 switch (token->type)
5144 {
5145 case CPP_EQ:
5146 op = NOP_EXPR;
5147 break;
5148
5149 case CPP_MULT_EQ:
5150 op = MULT_EXPR;
5151 break;
5152
5153 case CPP_DIV_EQ:
5154 op = TRUNC_DIV_EXPR;
5155 break;
5156
5157 case CPP_MOD_EQ:
5158 op = TRUNC_MOD_EXPR;
5159 break;
5160
5161 case CPP_PLUS_EQ:
5162 op = PLUS_EXPR;
5163 break;
5164
5165 case CPP_MINUS_EQ:
5166 op = MINUS_EXPR;
5167 break;
5168
5169 case CPP_RSHIFT_EQ:
5170 op = RSHIFT_EXPR;
5171 break;
5172
5173 case CPP_LSHIFT_EQ:
5174 op = LSHIFT_EXPR;
5175 break;
5176
5177 case CPP_AND_EQ:
5178 op = BIT_AND_EXPR;
5179 break;
5180
5181 case CPP_XOR_EQ:
5182 op = BIT_XOR_EXPR;
5183 break;
5184
5185 case CPP_OR_EQ:
5186 op = BIT_IOR_EXPR;
5187 break;
5188
5189 case CPP_MIN_EQ:
5190 op = MIN_EXPR;
5191 break;
5192
5193 case CPP_MAX_EQ:
5194 op = MAX_EXPR;
5195 break;
5196
5197 default:
5198 /* Nothing else is an assignment operator. */
5199 op = ERROR_MARK;
5200 }
5201
5202 /* If it was an assignment operator, consume it. */
5203 if (op != ERROR_MARK)
5204 cp_lexer_consume_token (parser->lexer);
5205
5206 return op;
5207}
5208
5209/* Parse an expression.
5210
5211 expression:
5212 assignment-expression
5213 expression , assignment-expression
5214
5215 Returns a representation of the expression. */
5216
5217static tree
94edc4ab 5218cp_parser_expression (cp_parser* parser)
a723baf1
MM
5219{
5220 tree expression = NULL_TREE;
a723baf1
MM
5221
5222 while (true)
5223 {
5224 tree assignment_expression;
5225
5226 /* Parse the next assignment-expression. */
5227 assignment_expression
5228 = cp_parser_assignment_expression (parser);
5229 /* If this is the first assignment-expression, we can just
5230 save it away. */
5231 if (!expression)
5232 expression = assignment_expression;
a723baf1 5233 else
d17811fd
MM
5234 expression = build_x_compound_expr (expression,
5235 assignment_expression);
a723baf1
MM
5236 /* If the next token is not a comma, then we are done with the
5237 expression. */
5238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5239 break;
5240 /* Consume the `,'. */
5241 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
5242 /* A comma operator cannot appear in a constant-expression. */
5243 if (parser->constant_expression_p)
5244 {
5245 if (!parser->allow_non_constant_expression_p)
d17811fd
MM
5246 expression
5247 = cp_parser_non_constant_expression ("a comma operator");
14d22dd6
MM
5248 parser->non_constant_expression_p = true;
5249 }
14d22dd6 5250 }
a723baf1
MM
5251
5252 return expression;
5253}
5254
5255/* Parse a constant-expression.
5256
5257 constant-expression:
14d22dd6
MM
5258 conditional-expression
5259
5260 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5261 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5262 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5263 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5264
5265static tree
14d22dd6
MM
5266cp_parser_constant_expression (cp_parser* parser,
5267 bool allow_non_constant_p,
5268 bool *non_constant_p)
a723baf1
MM
5269{
5270 bool saved_constant_expression_p;
14d22dd6
MM
5271 bool saved_allow_non_constant_expression_p;
5272 bool saved_non_constant_expression_p;
a723baf1
MM
5273 tree expression;
5274
5275 /* It might seem that we could simply parse the
5276 conditional-expression, and then check to see if it were
5277 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5278 one that the compiler can figure out is constant, possibly after
5279 doing some simplifications or optimizations. The standard has a
5280 precise definition of constant-expression, and we must honor
5281 that, even though it is somewhat more restrictive.
5282
5283 For example:
5284
5285 int i[(2, 3)];
5286
5287 is not a legal declaration, because `(2, 3)' is not a
5288 constant-expression. The `,' operator is forbidden in a
5289 constant-expression. However, GCC's constant-folding machinery
5290 will fold this operation to an INTEGER_CST for `3'. */
5291
14d22dd6 5292 /* Save the old settings. */
a723baf1 5293 saved_constant_expression_p = parser->constant_expression_p;
14d22dd6
MM
5294 saved_allow_non_constant_expression_p
5295 = parser->allow_non_constant_expression_p;
5296 saved_non_constant_expression_p = parser->non_constant_expression_p;
a723baf1
MM
5297 /* We are now parsing a constant-expression. */
5298 parser->constant_expression_p = true;
14d22dd6
MM
5299 parser->allow_non_constant_expression_p = allow_non_constant_p;
5300 parser->non_constant_expression_p = false;
39703eb9
MM
5301 /* Although the grammar says "conditional-expression", we parse an
5302 "assignment-expression", which also permits "throw-expression"
5303 and the use of assignment operators. In the case that
5304 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5305 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5306 actually essential that we look for an assignment-expression.
5307 For example, cp_parser_initializer_clauses uses this function to
5308 determine whether a particular assignment-expression is in fact
5309 constant. */
5310 expression = cp_parser_assignment_expression (parser);
14d22dd6 5311 /* Restore the old settings. */
a723baf1 5312 parser->constant_expression_p = saved_constant_expression_p;
14d22dd6
MM
5313 parser->allow_non_constant_expression_p
5314 = saved_allow_non_constant_expression_p;
5315 if (allow_non_constant_p)
5316 *non_constant_p = parser->non_constant_expression_p;
5317 parser->non_constant_expression_p = saved_non_constant_expression_p;
a723baf1
MM
5318
5319 return expression;
5320}
5321
5322/* Statements [gram.stmt.stmt] */
5323
5324/* Parse a statement.
5325
5326 statement:
5327 labeled-statement
5328 expression-statement
5329 compound-statement
5330 selection-statement
5331 iteration-statement
5332 jump-statement
5333 declaration-statement
5334 try-block */
5335
5336static void
a5bcc582 5337cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5338{
5339 tree statement;
5340 cp_token *token;
5341 int statement_line_number;
5342
5343 /* There is no statement yet. */
5344 statement = NULL_TREE;
5345 /* Peek at the next token. */
5346 token = cp_lexer_peek_token (parser->lexer);
5347 /* Remember the line number of the first token in the statement. */
82a98427 5348 statement_line_number = token->location.line;
a723baf1
MM
5349 /* If this is a keyword, then that will often determine what kind of
5350 statement we have. */
5351 if (token->type == CPP_KEYWORD)
5352 {
5353 enum rid keyword = token->keyword;
5354
5355 switch (keyword)
5356 {
5357 case RID_CASE:
5358 case RID_DEFAULT:
a5bcc582
NS
5359 statement = cp_parser_labeled_statement (parser,
5360 in_statement_expr_p);
a723baf1
MM
5361 break;
5362
5363 case RID_IF:
5364 case RID_SWITCH:
5365 statement = cp_parser_selection_statement (parser);
5366 break;
5367
5368 case RID_WHILE:
5369 case RID_DO:
5370 case RID_FOR:
5371 statement = cp_parser_iteration_statement (parser);
5372 break;
5373
5374 case RID_BREAK:
5375 case RID_CONTINUE:
5376 case RID_RETURN:
5377 case RID_GOTO:
5378 statement = cp_parser_jump_statement (parser);
5379 break;
5380
5381 case RID_TRY:
5382 statement = cp_parser_try_block (parser);
5383 break;
5384
5385 default:
5386 /* It might be a keyword like `int' that can start a
5387 declaration-statement. */
5388 break;
5389 }
5390 }
5391 else if (token->type == CPP_NAME)
5392 {
5393 /* If the next token is a `:', then we are looking at a
5394 labeled-statement. */
5395 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5396 if (token->type == CPP_COLON)
a5bcc582 5397 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5398 }
5399 /* Anything that starts with a `{' must be a compound-statement. */
5400 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5401 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5402
5403 /* Everything else must be a declaration-statement or an
5404 expression-statement. Try for the declaration-statement
5405 first, unless we are looking at a `;', in which case we know that
5406 we have an expression-statement. */
5407 if (!statement)
5408 {
5409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5410 {
5411 cp_parser_parse_tentatively (parser);
5412 /* Try to parse the declaration-statement. */
5413 cp_parser_declaration_statement (parser);
5414 /* If that worked, we're done. */
5415 if (cp_parser_parse_definitely (parser))
5416 return;
5417 }
5418 /* Look for an expression-statement instead. */
a5bcc582 5419 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5420 }
5421
5422 /* Set the line number for the statement. */
009ed910 5423 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5424 STMT_LINENO (statement) = statement_line_number;
5425}
5426
5427/* Parse a labeled-statement.
5428
5429 labeled-statement:
5430 identifier : statement
5431 case constant-expression : statement
5432 default : statement
5433
5434 Returns the new CASE_LABEL, for a `case' or `default' label. For
5435 an ordinary label, returns a LABEL_STMT. */
5436
5437static tree
a5bcc582 5438cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5439{
5440 cp_token *token;
0e59b3fb 5441 tree statement = error_mark_node;
a723baf1
MM
5442
5443 /* The next token should be an identifier. */
5444 token = cp_lexer_peek_token (parser->lexer);
5445 if (token->type != CPP_NAME
5446 && token->type != CPP_KEYWORD)
5447 {
5448 cp_parser_error (parser, "expected labeled-statement");
5449 return error_mark_node;
5450 }
5451
5452 switch (token->keyword)
5453 {
5454 case RID_CASE:
5455 {
5456 tree expr;
5457
5458 /* Consume the `case' token. */
5459 cp_lexer_consume_token (parser->lexer);
5460 /* Parse the constant-expression. */
14d22dd6 5461 expr = cp_parser_constant_expression (parser,
d17811fd 5462 /*allow_non_constant_p=*/false,
14d22dd6 5463 NULL);
0e59b3fb
MM
5464 if (!parser->in_switch_statement_p)
5465 error ("case label `%E' not within a switch statement", expr);
5466 else
5467 statement = finish_case_label (expr, NULL_TREE);
a723baf1
MM
5468 }
5469 break;
5470
5471 case RID_DEFAULT:
5472 /* Consume the `default' token. */
5473 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5474 if (!parser->in_switch_statement_p)
5475 error ("case label not within a switch statement");
5476 else
5477 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5478 break;
5479
5480 default:
5481 /* Anything else must be an ordinary label. */
5482 statement = finish_label_stmt (cp_parser_identifier (parser));
5483 break;
5484 }
5485
5486 /* Require the `:' token. */
5487 cp_parser_require (parser, CPP_COLON, "`:'");
5488 /* Parse the labeled statement. */
a5bcc582 5489 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5490
5491 /* Return the label, in the case of a `case' or `default' label. */
5492 return statement;
5493}
5494
5495/* Parse an expression-statement.
5496
5497 expression-statement:
5498 expression [opt] ;
5499
5500 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5501 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5502 indicates whether this expression-statement is part of an
5503 expression statement. */
a723baf1
MM
5504
5505static tree
a5bcc582 5506cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5507{
a5bcc582 5508 tree statement = NULL_TREE;
a723baf1 5509
a5bcc582 5510 /* If the next token is a ';', then there is no expression
04c06002 5511 statement. */
a723baf1 5512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582
NS
5513 statement = cp_parser_expression (parser);
5514
a723baf1 5515 /* Consume the final `;'. */
e0860732 5516 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5517
a5bcc582
NS
5518 if (in_statement_expr_p
5519 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5520 {
5521 /* This is the final expression statement of a statement
5522 expression. */
5523 statement = finish_stmt_expr_expr (statement);
5524 }
5525 else if (statement)
5526 statement = finish_expr_stmt (statement);
5527 else
5528 finish_stmt ();
5529
a723baf1
MM
5530 return statement;
5531}
5532
5533/* Parse a compound-statement.
5534
5535 compound-statement:
5536 { statement-seq [opt] }
5537
5538 Returns a COMPOUND_STMT representing the statement. */
5539
5540static tree
a5bcc582 5541cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5542{
5543 tree compound_stmt;
5544
5545 /* Consume the `{'. */
5546 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5547 return error_mark_node;
5548 /* Begin the compound-statement. */
7a3397c7 5549 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5550 /* Parse an (optional) statement-seq. */
a5bcc582 5551 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5552 /* Finish the compound-statement. */
7a3397c7 5553 finish_compound_stmt (compound_stmt);
a723baf1
MM
5554 /* Consume the `}'. */
5555 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5556
5557 return compound_stmt;
5558}
5559
5560/* Parse an (optional) statement-seq.
5561
5562 statement-seq:
5563 statement
5564 statement-seq [opt] statement */
5565
5566static void
a5bcc582 5567cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5568{
5569 /* Scan statements until there aren't any more. */
5570 while (true)
5571 {
5572 /* If we're looking at a `}', then we've run out of statements. */
5573 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5574 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5575 break;
5576
5577 /* Parse the statement. */
a5bcc582 5578 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5579 }
5580}
5581
5582/* Parse a selection-statement.
5583
5584 selection-statement:
5585 if ( condition ) statement
5586 if ( condition ) statement else statement
5587 switch ( condition ) statement
5588
5589 Returns the new IF_STMT or SWITCH_STMT. */
5590
5591static tree
94edc4ab 5592cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5593{
5594 cp_token *token;
5595 enum rid keyword;
5596
5597 /* Peek at the next token. */
5598 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5599
5600 /* See what kind of keyword it is. */
5601 keyword = token->keyword;
5602 switch (keyword)
5603 {
5604 case RID_IF:
5605 case RID_SWITCH:
5606 {
5607 tree statement;
5608 tree condition;
5609
5610 /* Look for the `('. */
5611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5612 {
5613 cp_parser_skip_to_end_of_statement (parser);
5614 return error_mark_node;
5615 }
5616
5617 /* Begin the selection-statement. */
5618 if (keyword == RID_IF)
5619 statement = begin_if_stmt ();
5620 else
5621 statement = begin_switch_stmt ();
5622
5623 /* Parse the condition. */
5624 condition = cp_parser_condition (parser);
5625 /* Look for the `)'. */
5626 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
5627 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5628 /*consume_paren=*/true);
a723baf1
MM
5629
5630 if (keyword == RID_IF)
5631 {
5632 tree then_stmt;
5633
5634 /* Add the condition. */
5635 finish_if_stmt_cond (condition, statement);
5636
5637 /* Parse the then-clause. */
5638 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5639 finish_then_clause (statement);
5640
5641 /* If the next token is `else', parse the else-clause. */
5642 if (cp_lexer_next_token_is_keyword (parser->lexer,
5643 RID_ELSE))
5644 {
5645 tree else_stmt;
5646
5647 /* Consume the `else' keyword. */
5648 cp_lexer_consume_token (parser->lexer);
5649 /* Parse the else-clause. */
5650 else_stmt
5651 = cp_parser_implicitly_scoped_statement (parser);
5652 finish_else_clause (statement);
5653 }
5654
5655 /* Now we're all done with the if-statement. */
5656 finish_if_stmt ();
5657 }
5658 else
5659 {
5660 tree body;
0e59b3fb 5661 bool in_switch_statement_p;
a723baf1
MM
5662
5663 /* Add the condition. */
5664 finish_switch_cond (condition, statement);
5665
5666 /* Parse the body of the switch-statement. */
0e59b3fb
MM
5667 in_switch_statement_p = parser->in_switch_statement_p;
5668 parser->in_switch_statement_p = true;
a723baf1 5669 body = cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5670 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
5671
5672 /* Now we're all done with the switch-statement. */
5673 finish_switch_stmt (statement);
5674 }
5675
5676 return statement;
5677 }
5678 break;
5679
5680 default:
5681 cp_parser_error (parser, "expected selection-statement");
5682 return error_mark_node;
5683 }
5684}
5685
5686/* Parse a condition.
5687
5688 condition:
5689 expression
5690 type-specifier-seq declarator = assignment-expression
5691
5692 GNU Extension:
5693
5694 condition:
5695 type-specifier-seq declarator asm-specification [opt]
5696 attributes [opt] = assignment-expression
5697
5698 Returns the expression that should be tested. */
5699
5700static tree
94edc4ab 5701cp_parser_condition (cp_parser* parser)
a723baf1
MM
5702{
5703 tree type_specifiers;
5704 const char *saved_message;
5705
5706 /* Try the declaration first. */
5707 cp_parser_parse_tentatively (parser);
5708 /* New types are not allowed in the type-specifier-seq for a
5709 condition. */
5710 saved_message = parser->type_definition_forbidden_message;
5711 parser->type_definition_forbidden_message
5712 = "types may not be defined in conditions";
5713 /* Parse the type-specifier-seq. */
5714 type_specifiers = cp_parser_type_specifier_seq (parser);
5715 /* Restore the saved message. */
5716 parser->type_definition_forbidden_message = saved_message;
5717 /* If all is well, we might be looking at a declaration. */
5718 if (!cp_parser_error_occurred (parser))
5719 {
5720 tree decl;
5721 tree asm_specification;
5722 tree attributes;
5723 tree declarator;
5724 tree initializer = NULL_TREE;
5725
5726 /* Parse the declarator. */
62b8a44e 5727 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
5728 /*ctor_dtor_or_conv_p=*/NULL,
5729 /*parenthesized_p=*/NULL);
a723baf1
MM
5730 /* Parse the attributes. */
5731 attributes = cp_parser_attributes_opt (parser);
5732 /* Parse the asm-specification. */
5733 asm_specification = cp_parser_asm_specification_opt (parser);
5734 /* If the next token is not an `=', then we might still be
5735 looking at an expression. For example:
5736
5737 if (A(a).x)
5738
5739 looks like a decl-specifier-seq and a declarator -- but then
5740 there is no `=', so this is an expression. */
5741 cp_parser_require (parser, CPP_EQ, "`='");
5742 /* If we did see an `=', then we are looking at a declaration
5743 for sure. */
5744 if (cp_parser_parse_definitely (parser))
5745 {
5746 /* Create the declaration. */
5747 decl = start_decl (declarator, type_specifiers,
5748 /*initialized_p=*/true,
5749 attributes, /*prefix_attributes=*/NULL_TREE);
5750 /* Parse the assignment-expression. */
5751 initializer = cp_parser_assignment_expression (parser);
5752
5753 /* Process the initializer. */
5754 cp_finish_decl (decl,
5755 initializer,
5756 asm_specification,
5757 LOOKUP_ONLYCONVERTING);
5758
5759 return convert_from_reference (decl);
5760 }
5761 }
5762 /* If we didn't even get past the declarator successfully, we are
5763 definitely not looking at a declaration. */
5764 else
5765 cp_parser_abort_tentative_parse (parser);
5766
5767 /* Otherwise, we are looking at an expression. */
5768 return cp_parser_expression (parser);
5769}
5770
5771/* Parse an iteration-statement.
5772
5773 iteration-statement:
5774 while ( condition ) statement
5775 do statement while ( expression ) ;
5776 for ( for-init-statement condition [opt] ; expression [opt] )
5777 statement
5778
5779 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5780
5781static tree
94edc4ab 5782cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5783{
5784 cp_token *token;
5785 enum rid keyword;
5786 tree statement;
0e59b3fb
MM
5787 bool in_iteration_statement_p;
5788
a723baf1
MM
5789
5790 /* Peek at the next token. */
5791 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5792 if (!token)
5793 return error_mark_node;
5794
0e59b3fb
MM
5795 /* Remember whether or not we are already within an iteration
5796 statement. */
5797 in_iteration_statement_p = parser->in_iteration_statement_p;
5798
a723baf1
MM
5799 /* See what kind of keyword it is. */
5800 keyword = token->keyword;
5801 switch (keyword)
5802 {
5803 case RID_WHILE:
5804 {
5805 tree condition;
5806
5807 /* Begin the while-statement. */
5808 statement = begin_while_stmt ();
5809 /* Look for the `('. */
5810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5811 /* Parse the condition. */
5812 condition = cp_parser_condition (parser);
5813 finish_while_stmt_cond (condition, statement);
5814 /* Look for the `)'. */
5815 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5816 /* Parse the dependent statement. */
0e59b3fb 5817 parser->in_iteration_statement_p = true;
a723baf1 5818 cp_parser_already_scoped_statement (parser);
0e59b3fb 5819 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5820 /* We're done with the while-statement. */
5821 finish_while_stmt (statement);
5822 }
5823 break;
5824
5825 case RID_DO:
5826 {
5827 tree expression;
5828
5829 /* Begin the do-statement. */
5830 statement = begin_do_stmt ();
5831 /* Parse the body of the do-statement. */
0e59b3fb 5832 parser->in_iteration_statement_p = true;
a723baf1 5833 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5834 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5835 finish_do_body (statement);
5836 /* Look for the `while' keyword. */
5837 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5838 /* Look for the `('. */
5839 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5840 /* Parse the expression. */
5841 expression = cp_parser_expression (parser);
5842 /* We're done with the do-statement. */
5843 finish_do_stmt (expression, statement);
5844 /* Look for the `)'. */
5845 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5846 /* Look for the `;'. */
5847 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5848 }
5849 break;
5850
5851 case RID_FOR:
5852 {
5853 tree condition = NULL_TREE;
5854 tree expression = NULL_TREE;
5855
5856 /* Begin the for-statement. */
5857 statement = begin_for_stmt ();
5858 /* Look for the `('. */
5859 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5860 /* Parse the initialization. */
5861 cp_parser_for_init_statement (parser);
5862 finish_for_init_stmt (statement);
5863
5864 /* If there's a condition, process it. */
5865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5866 condition = cp_parser_condition (parser);
5867 finish_for_cond (condition, statement);
5868 /* Look for the `;'. */
5869 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5870
5871 /* If there's an expression, process it. */
5872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5873 expression = cp_parser_expression (parser);
5874 finish_for_expr (expression, statement);
5875 /* Look for the `)'. */
5876 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5877
5878 /* Parse the body of the for-statement. */
0e59b3fb 5879 parser->in_iteration_statement_p = true;
a723baf1 5880 cp_parser_already_scoped_statement (parser);
0e59b3fb 5881 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5882
5883 /* We're done with the for-statement. */
5884 finish_for_stmt (statement);
5885 }
5886 break;
5887
5888 default:
5889 cp_parser_error (parser, "expected iteration-statement");
5890 statement = error_mark_node;
5891 break;
5892 }
5893
5894 return statement;
5895}
5896
5897/* Parse a for-init-statement.
5898
5899 for-init-statement:
5900 expression-statement
5901 simple-declaration */
5902
5903static void
94edc4ab 5904cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5905{
5906 /* If the next token is a `;', then we have an empty
34cd5ae7 5907 expression-statement. Grammatically, this is also a
a723baf1
MM
5908 simple-declaration, but an invalid one, because it does not
5909 declare anything. Therefore, if we did not handle this case
5910 specially, we would issue an error message about an invalid
5911 declaration. */
5912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5913 {
5914 /* We're going to speculatively look for a declaration, falling back
5915 to an expression, if necessary. */
5916 cp_parser_parse_tentatively (parser);
5917 /* Parse the declaration. */
5918 cp_parser_simple_declaration (parser,
5919 /*function_definition_allowed_p=*/false);
5920 /* If the tentative parse failed, then we shall need to look for an
5921 expression-statement. */
5922 if (cp_parser_parse_definitely (parser))
5923 return;
5924 }
5925
a5bcc582 5926 cp_parser_expression_statement (parser, false);
a723baf1
MM
5927}
5928
5929/* Parse a jump-statement.
5930
5931 jump-statement:
5932 break ;
5933 continue ;
5934 return expression [opt] ;
5935 goto identifier ;
5936
5937 GNU extension:
5938
5939 jump-statement:
5940 goto * expression ;
5941
5942 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5943 GOTO_STMT. */
5944
5945static tree
94edc4ab 5946cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
5947{
5948 tree statement = error_mark_node;
5949 cp_token *token;
5950 enum rid keyword;
5951
5952 /* Peek at the next token. */
5953 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5954 if (!token)
5955 return error_mark_node;
5956
5957 /* See what kind of keyword it is. */
5958 keyword = token->keyword;
5959 switch (keyword)
5960 {
5961 case RID_BREAK:
0e59b3fb
MM
5962 if (!parser->in_switch_statement_p
5963 && !parser->in_iteration_statement_p)
5964 {
5965 error ("break statement not within loop or switch");
5966 statement = error_mark_node;
5967 }
5968 else
5969 statement = finish_break_stmt ();
a723baf1
MM
5970 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5971 break;
5972
5973 case RID_CONTINUE:
0e59b3fb
MM
5974 if (!parser->in_iteration_statement_p)
5975 {
5976 error ("continue statement not within a loop");
5977 statement = error_mark_node;
5978 }
5979 else
5980 statement = finish_continue_stmt ();
a723baf1
MM
5981 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5982 break;
5983
5984 case RID_RETURN:
5985 {
5986 tree expr;
5987
5988 /* If the next token is a `;', then there is no
5989 expression. */
5990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5991 expr = cp_parser_expression (parser);
5992 else
5993 expr = NULL_TREE;
5994 /* Build the return-statement. */
5995 statement = finish_return_stmt (expr);
5996 /* Look for the final `;'. */
5997 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5998 }
5999 break;
6000
6001 case RID_GOTO:
6002 /* Create the goto-statement. */
6003 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6004 {
6005 /* Issue a warning about this use of a GNU extension. */
6006 if (pedantic)
6007 pedwarn ("ISO C++ forbids computed gotos");
6008 /* Consume the '*' token. */
6009 cp_lexer_consume_token (parser->lexer);
6010 /* Parse the dependent expression. */
6011 finish_goto_stmt (cp_parser_expression (parser));
6012 }
6013 else
6014 finish_goto_stmt (cp_parser_identifier (parser));
6015 /* Look for the final `;'. */
6016 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6017 break;
6018
6019 default:
6020 cp_parser_error (parser, "expected jump-statement");
6021 break;
6022 }
6023
6024 return statement;
6025}
6026
6027/* Parse a declaration-statement.
6028
6029 declaration-statement:
6030 block-declaration */
6031
6032static void
94edc4ab 6033cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6034{
6035 /* Parse the block-declaration. */
6036 cp_parser_block_declaration (parser, /*statement_p=*/true);
6037
6038 /* Finish off the statement. */
6039 finish_stmt ();
6040}
6041
6042/* Some dependent statements (like `if (cond) statement'), are
6043 implicitly in their own scope. In other words, if the statement is
6044 a single statement (as opposed to a compound-statement), it is
6045 none-the-less treated as if it were enclosed in braces. Any
6046 declarations appearing in the dependent statement are out of scope
6047 after control passes that point. This function parses a statement,
6048 but ensures that is in its own scope, even if it is not a
6049 compound-statement.
6050
6051 Returns the new statement. */
6052
6053static tree
94edc4ab 6054cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6055{
6056 tree statement;
6057
6058 /* If the token is not a `{', then we must take special action. */
6059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6060 {
6061 /* Create a compound-statement. */
7a3397c7 6062 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 6063 /* Parse the dependent-statement. */
a5bcc582 6064 cp_parser_statement (parser, false);
a723baf1 6065 /* Finish the dummy compound-statement. */
7a3397c7 6066 finish_compound_stmt (statement);
a723baf1
MM
6067 }
6068 /* Otherwise, we simply parse the statement directly. */
6069 else
a5bcc582 6070 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
6071
6072 /* Return the statement. */
6073 return statement;
6074}
6075
6076/* For some dependent statements (like `while (cond) statement'), we
6077 have already created a scope. Therefore, even if the dependent
6078 statement is a compound-statement, we do not want to create another
6079 scope. */
6080
6081static void
94edc4ab 6082cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6083{
6084 /* If the token is not a `{', then we must take special action. */
6085 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6086 {
6087 tree statement;
6088
6089 /* Create a compound-statement. */
7a3397c7 6090 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 6091 /* Parse the dependent-statement. */
a5bcc582 6092 cp_parser_statement (parser, false);
a723baf1 6093 /* Finish the dummy compound-statement. */
7a3397c7 6094 finish_compound_stmt (statement);
a723baf1
MM
6095 }
6096 /* Otherwise, we simply parse the statement directly. */
6097 else
a5bcc582 6098 cp_parser_statement (parser, false);
a723baf1
MM
6099}
6100
6101/* Declarations [gram.dcl.dcl] */
6102
6103/* Parse an optional declaration-sequence.
6104
6105 declaration-seq:
6106 declaration
6107 declaration-seq declaration */
6108
6109static void
94edc4ab 6110cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6111{
6112 while (true)
6113 {
6114 cp_token *token;
6115
6116 token = cp_lexer_peek_token (parser->lexer);
6117
6118 if (token->type == CPP_CLOSE_BRACE
6119 || token->type == CPP_EOF)
6120 break;
6121
6122 if (token->type == CPP_SEMICOLON)
6123 {
6124 /* A declaration consisting of a single semicolon is
6125 invalid. Allow it unless we're being pedantic. */
6126 if (pedantic)
6127 pedwarn ("extra `;'");
6128 cp_lexer_consume_token (parser->lexer);
6129 continue;
6130 }
6131
c838d82f 6132 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6133 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6134 while (pending_lang_change > 0)
6135 {
6136 push_lang_context (lang_name_c);
6137 --pending_lang_change;
6138 }
6139 while (pending_lang_change < 0)
6140 {
6141 pop_lang_context ();
6142 ++pending_lang_change;
6143 }
6144
6145 /* Parse the declaration itself. */
a723baf1
MM
6146 cp_parser_declaration (parser);
6147 }
6148}
6149
6150/* Parse a declaration.
6151
6152 declaration:
6153 block-declaration
6154 function-definition
6155 template-declaration
6156 explicit-instantiation
6157 explicit-specialization
6158 linkage-specification
1092805d
MM
6159 namespace-definition
6160
6161 GNU extension:
6162
6163 declaration:
6164 __extension__ declaration */
a723baf1
MM
6165
6166static void
94edc4ab 6167cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6168{
6169 cp_token token1;
6170 cp_token token2;
1092805d
MM
6171 int saved_pedantic;
6172
6173 /* Check for the `__extension__' keyword. */
6174 if (cp_parser_extension_opt (parser, &saved_pedantic))
6175 {
6176 /* Parse the qualified declaration. */
6177 cp_parser_declaration (parser);
6178 /* Restore the PEDANTIC flag. */
6179 pedantic = saved_pedantic;
6180
6181 return;
6182 }
a723baf1
MM
6183
6184 /* Try to figure out what kind of declaration is present. */
6185 token1 = *cp_lexer_peek_token (parser->lexer);
6186 if (token1.type != CPP_EOF)
6187 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6188
6189 /* If the next token is `extern' and the following token is a string
6190 literal, then we have a linkage specification. */
6191 if (token1.keyword == RID_EXTERN
6192 && cp_parser_is_string_literal (&token2))
6193 cp_parser_linkage_specification (parser);
6194 /* If the next token is `template', then we have either a template
6195 declaration, an explicit instantiation, or an explicit
6196 specialization. */
6197 else if (token1.keyword == RID_TEMPLATE)
6198 {
6199 /* `template <>' indicates a template specialization. */
6200 if (token2.type == CPP_LESS
6201 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6202 cp_parser_explicit_specialization (parser);
6203 /* `template <' indicates a template declaration. */
6204 else if (token2.type == CPP_LESS)
6205 cp_parser_template_declaration (parser, /*member_p=*/false);
6206 /* Anything else must be an explicit instantiation. */
6207 else
6208 cp_parser_explicit_instantiation (parser);
6209 }
6210 /* If the next token is `export', then we have a template
6211 declaration. */
6212 else if (token1.keyword == RID_EXPORT)
6213 cp_parser_template_declaration (parser, /*member_p=*/false);
6214 /* If the next token is `extern', 'static' or 'inline' and the one
6215 after that is `template', we have a GNU extended explicit
6216 instantiation directive. */
6217 else if (cp_parser_allow_gnu_extensions_p (parser)
6218 && (token1.keyword == RID_EXTERN
6219 || token1.keyword == RID_STATIC
6220 || token1.keyword == RID_INLINE)
6221 && token2.keyword == RID_TEMPLATE)
6222 cp_parser_explicit_instantiation (parser);
6223 /* If the next token is `namespace', check for a named or unnamed
6224 namespace definition. */
6225 else if (token1.keyword == RID_NAMESPACE
6226 && (/* A named namespace definition. */
6227 (token2.type == CPP_NAME
6228 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6229 == CPP_OPEN_BRACE))
6230 /* An unnamed namespace definition. */
6231 || token2.type == CPP_OPEN_BRACE))
6232 cp_parser_namespace_definition (parser);
6233 /* We must have either a block declaration or a function
6234 definition. */
6235 else
6236 /* Try to parse a block-declaration, or a function-definition. */
6237 cp_parser_block_declaration (parser, /*statement_p=*/false);
6238}
6239
6240/* Parse a block-declaration.
6241
6242 block-declaration:
6243 simple-declaration
6244 asm-definition
6245 namespace-alias-definition
6246 using-declaration
6247 using-directive
6248
6249 GNU Extension:
6250
6251 block-declaration:
6252 __extension__ block-declaration
6253 label-declaration
6254
34cd5ae7 6255 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6256 part of a declaration-statement. */
6257
6258static void
6259cp_parser_block_declaration (cp_parser *parser,
6260 bool statement_p)
6261{
6262 cp_token *token1;
6263 int saved_pedantic;
6264
6265 /* Check for the `__extension__' keyword. */
6266 if (cp_parser_extension_opt (parser, &saved_pedantic))
6267 {
6268 /* Parse the qualified declaration. */
6269 cp_parser_block_declaration (parser, statement_p);
6270 /* Restore the PEDANTIC flag. */
6271 pedantic = saved_pedantic;
6272
6273 return;
6274 }
6275
6276 /* Peek at the next token to figure out which kind of declaration is
6277 present. */
6278 token1 = cp_lexer_peek_token (parser->lexer);
6279
6280 /* If the next keyword is `asm', we have an asm-definition. */
6281 if (token1->keyword == RID_ASM)
6282 {
6283 if (statement_p)
6284 cp_parser_commit_to_tentative_parse (parser);
6285 cp_parser_asm_definition (parser);
6286 }
6287 /* If the next keyword is `namespace', we have a
6288 namespace-alias-definition. */
6289 else if (token1->keyword == RID_NAMESPACE)
6290 cp_parser_namespace_alias_definition (parser);
6291 /* If the next keyword is `using', we have either a
6292 using-declaration or a using-directive. */
6293 else if (token1->keyword == RID_USING)
6294 {
6295 cp_token *token2;
6296
6297 if (statement_p)
6298 cp_parser_commit_to_tentative_parse (parser);
6299 /* If the token after `using' is `namespace', then we have a
6300 using-directive. */
6301 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6302 if (token2->keyword == RID_NAMESPACE)
6303 cp_parser_using_directive (parser);
6304 /* Otherwise, it's a using-declaration. */
6305 else
6306 cp_parser_using_declaration (parser);
6307 }
6308 /* If the next keyword is `__label__' we have a label declaration. */
6309 else if (token1->keyword == RID_LABEL)
6310 {
6311 if (statement_p)
6312 cp_parser_commit_to_tentative_parse (parser);
6313 cp_parser_label_declaration (parser);
6314 }
6315 /* Anything else must be a simple-declaration. */
6316 else
6317 cp_parser_simple_declaration (parser, !statement_p);
6318}
6319
6320/* Parse a simple-declaration.
6321
6322 simple-declaration:
6323 decl-specifier-seq [opt] init-declarator-list [opt] ;
6324
6325 init-declarator-list:
6326 init-declarator
6327 init-declarator-list , init-declarator
6328
34cd5ae7 6329 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6330 function-definition as a simple-declaration. */
a723baf1
MM
6331
6332static void
94edc4ab
NN
6333cp_parser_simple_declaration (cp_parser* parser,
6334 bool function_definition_allowed_p)
a723baf1
MM
6335{
6336 tree decl_specifiers;
6337 tree attributes;
560ad596 6338 int declares_class_or_enum;
a723baf1
MM
6339 bool saw_declarator;
6340
6341 /* Defer access checks until we know what is being declared; the
6342 checks for names appearing in the decl-specifier-seq should be
6343 done as if we were in the scope of the thing being declared. */
8d241e0b 6344 push_deferring_access_checks (dk_deferred);
cf22909c 6345
a723baf1
MM
6346 /* Parse the decl-specifier-seq. We have to keep track of whether
6347 or not the decl-specifier-seq declares a named class or
6348 enumeration type, since that is the only case in which the
6349 init-declarator-list is allowed to be empty.
6350
6351 [dcl.dcl]
6352
6353 In a simple-declaration, the optional init-declarator-list can be
6354 omitted only when declaring a class or enumeration, that is when
6355 the decl-specifier-seq contains either a class-specifier, an
6356 elaborated-type-specifier, or an enum-specifier. */
6357 decl_specifiers
6358 = cp_parser_decl_specifier_seq (parser,
6359 CP_PARSER_FLAGS_OPTIONAL,
6360 &attributes,
6361 &declares_class_or_enum);
6362 /* We no longer need to defer access checks. */
cf22909c 6363 stop_deferring_access_checks ();
24c0ef37 6364
39703eb9
MM
6365 /* In a block scope, a valid declaration must always have a
6366 decl-specifier-seq. By not trying to parse declarators, we can
6367 resolve the declaration/expression ambiguity more quickly. */
6368 if (!function_definition_allowed_p && !decl_specifiers)
6369 {
6370 cp_parser_error (parser, "expected declaration");
6371 goto done;
6372 }
6373
8fbc5ae7
MM
6374 /* If the next two tokens are both identifiers, the code is
6375 erroneous. The usual cause of this situation is code like:
6376
6377 T t;
6378
6379 where "T" should name a type -- but does not. */
6380 if (cp_parser_diagnose_invalid_type_name (parser))
6381 {
8d241e0b 6382 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6383 looking at a declaration. */
6384 cp_parser_commit_to_tentative_parse (parser);
6385 /* Give up. */
39703eb9 6386 goto done;
8fbc5ae7
MM
6387 }
6388
a723baf1
MM
6389 /* Keep going until we hit the `;' at the end of the simple
6390 declaration. */
6391 saw_declarator = false;
6392 while (cp_lexer_next_token_is_not (parser->lexer,
6393 CPP_SEMICOLON))
6394 {
6395 cp_token *token;
6396 bool function_definition_p;
560ad596 6397 tree decl;
a723baf1
MM
6398
6399 saw_declarator = true;
6400 /* Parse the init-declarator. */
560ad596
MM
6401 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6402 function_definition_allowed_p,
6403 /*member_p=*/false,
6404 declares_class_or_enum,
6405 &function_definition_p);
1fb3244a
MM
6406 /* If an error occurred while parsing tentatively, exit quickly.
6407 (That usually happens when in the body of a function; each
6408 statement is treated as a declaration-statement until proven
6409 otherwise.) */
6410 if (cp_parser_error_occurred (parser))
39703eb9 6411 goto done;
a723baf1
MM
6412 /* Handle function definitions specially. */
6413 if (function_definition_p)
6414 {
6415 /* If the next token is a `,', then we are probably
6416 processing something like:
6417
6418 void f() {}, *p;
6419
6420 which is erroneous. */
6421 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6422 error ("mixing declarations and function-definitions is forbidden");
6423 /* Otherwise, we're done with the list of declarators. */
6424 else
24c0ef37 6425 {
cf22909c 6426 pop_deferring_access_checks ();
24c0ef37
GS
6427 return;
6428 }
a723baf1
MM
6429 }
6430 /* The next token should be either a `,' or a `;'. */
6431 token = cp_lexer_peek_token (parser->lexer);
6432 /* If it's a `,', there are more declarators to come. */
6433 if (token->type == CPP_COMMA)
6434 cp_lexer_consume_token (parser->lexer);
6435 /* If it's a `;', we are done. */
6436 else if (token->type == CPP_SEMICOLON)
6437 break;
6438 /* Anything else is an error. */
6439 else
6440 {
6441 cp_parser_error (parser, "expected `,' or `;'");
6442 /* Skip tokens until we reach the end of the statement. */
6443 cp_parser_skip_to_end_of_statement (parser);
39703eb9 6444 goto done;
a723baf1
MM
6445 }
6446 /* After the first time around, a function-definition is not
6447 allowed -- even if it was OK at first. For example:
6448
6449 int i, f() {}
6450
6451 is not valid. */
6452 function_definition_allowed_p = false;
6453 }
6454
6455 /* Issue an error message if no declarators are present, and the
6456 decl-specifier-seq does not itself declare a class or
6457 enumeration. */
6458 if (!saw_declarator)
6459 {
6460 if (cp_parser_declares_only_class_p (parser))
6461 shadow_tag (decl_specifiers);
6462 /* Perform any deferred access checks. */
cf22909c 6463 perform_deferred_access_checks ();
a723baf1
MM
6464 }
6465
6466 /* Consume the `;'. */
6467 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6468
39703eb9
MM
6469 done:
6470 pop_deferring_access_checks ();
a723baf1
MM
6471}
6472
6473/* Parse a decl-specifier-seq.
6474
6475 decl-specifier-seq:
6476 decl-specifier-seq [opt] decl-specifier
6477
6478 decl-specifier:
6479 storage-class-specifier
6480 type-specifier
6481 function-specifier
6482 friend
6483 typedef
6484
6485 GNU Extension:
6486
6487 decl-specifier-seq:
6488 decl-specifier-seq [opt] attributes
6489
6490 Returns a TREE_LIST, giving the decl-specifiers in the order they
6491 appear in the source code. The TREE_VALUE of each node is the
6492 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6493 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6494 representation of a type-specifier, see cp_parser_type_specifier.
6495
6496 If there are attributes, they will be stored in *ATTRIBUTES,
6497 represented as described above cp_parser_attributes.
6498
6499 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6500 appears, and the entity that will be a friend is not going to be a
6501 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6502 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
560ad596
MM
6503 friendship is granted might not be a class.
6504
6505 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6506 *flags:
6507
6508 1: one of the decl-specifiers is an elaborated-type-specifier
6509 2: one of the decl-specifiers is an enum-specifier or a
6510 class-specifier
6511
6512 */
a723baf1
MM
6513
6514static tree
94edc4ab
NN
6515cp_parser_decl_specifier_seq (cp_parser* parser,
6516 cp_parser_flags flags,
6517 tree* attributes,
560ad596 6518 int* declares_class_or_enum)
a723baf1
MM
6519{
6520 tree decl_specs = NULL_TREE;
6521 bool friend_p = false;
f2ce60b8
NS
6522 bool constructor_possible_p = !parser->in_declarator_p;
6523
a723baf1 6524 /* Assume no class or enumeration type is declared. */
560ad596 6525 *declares_class_or_enum = 0;
a723baf1
MM
6526
6527 /* Assume there are no attributes. */
6528 *attributes = NULL_TREE;
6529
6530 /* Keep reading specifiers until there are no more to read. */
6531 while (true)
6532 {
6533 tree decl_spec = NULL_TREE;
6534 bool constructor_p;
6535 cp_token *token;
6536
6537 /* Peek at the next token. */
6538 token = cp_lexer_peek_token (parser->lexer);
6539 /* Handle attributes. */
6540 if (token->keyword == RID_ATTRIBUTE)
6541 {
6542 /* Parse the attributes. */
6543 decl_spec = cp_parser_attributes_opt (parser);
6544 /* Add them to the list. */
6545 *attributes = chainon (*attributes, decl_spec);
6546 continue;
6547 }
6548 /* If the next token is an appropriate keyword, we can simply
6549 add it to the list. */
6550 switch (token->keyword)
6551 {
6552 case RID_FRIEND:
6553 /* decl-specifier:
6554 friend */
1918facf
SB
6555 if (friend_p)
6556 error ("duplicate `friend'");
6557 else
6558 friend_p = true;
a723baf1
MM
6559 /* The representation of the specifier is simply the
6560 appropriate TREE_IDENTIFIER node. */
6561 decl_spec = token->value;
6562 /* Consume the token. */
6563 cp_lexer_consume_token (parser->lexer);
6564 break;
6565
6566 /* function-specifier:
6567 inline
6568 virtual
6569 explicit */
6570 case RID_INLINE:
6571 case RID_VIRTUAL:
6572 case RID_EXPLICIT:
6573 decl_spec = cp_parser_function_specifier_opt (parser);
6574 break;
6575
6576 /* decl-specifier:
6577 typedef */
6578 case RID_TYPEDEF:
6579 /* The representation of the specifier is simply the
6580 appropriate TREE_IDENTIFIER node. */
6581 decl_spec = token->value;
6582 /* Consume the token. */
6583 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6584 /* A constructor declarator cannot appear in a typedef. */
6585 constructor_possible_p = false;
c006d942
MM
6586 /* The "typedef" keyword can only occur in a declaration; we
6587 may as well commit at this point. */
6588 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6589 break;
6590
6591 /* storage-class-specifier:
6592 auto
6593 register
6594 static
6595 extern
6596 mutable
6597
6598 GNU Extension:
6599 thread */
6600 case RID_AUTO:
6601 case RID_REGISTER:
6602 case RID_STATIC:
6603 case RID_EXTERN:
6604 case RID_MUTABLE:
6605 case RID_THREAD:
6606 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6607 break;
6608
6609 default:
6610 break;
6611 }
6612
6613 /* Constructors are a special case. The `S' in `S()' is not a
6614 decl-specifier; it is the beginning of the declarator. */
6615 constructor_p = (!decl_spec
2050a1bb 6616 && constructor_possible_p
a723baf1
MM
6617 && cp_parser_constructor_declarator_p (parser,
6618 friend_p));
6619
6620 /* If we don't have a DECL_SPEC yet, then we must be looking at
6621 a type-specifier. */
6622 if (!decl_spec && !constructor_p)
6623 {
560ad596 6624 int decl_spec_declares_class_or_enum;
a723baf1
MM
6625 bool is_cv_qualifier;
6626
6627 decl_spec
6628 = cp_parser_type_specifier (parser, flags,
6629 friend_p,
6630 /*is_declaration=*/true,
6631 &decl_spec_declares_class_or_enum,
6632 &is_cv_qualifier);
6633
6634 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6635
6636 /* If this type-specifier referenced a user-defined type
6637 (a typedef, class-name, etc.), then we can't allow any
6638 more such type-specifiers henceforth.
6639
6640 [dcl.spec]
6641
6642 The longest sequence of decl-specifiers that could
6643 possibly be a type name is taken as the
6644 decl-specifier-seq of a declaration. The sequence shall
6645 be self-consistent as described below.
6646
6647 [dcl.type]
6648
6649 As a general rule, at most one type-specifier is allowed
6650 in the complete decl-specifier-seq of a declaration. The
6651 only exceptions are the following:
6652
6653 -- const or volatile can be combined with any other
6654 type-specifier.
6655
6656 -- signed or unsigned can be combined with char, long,
6657 short, or int.
6658
6659 -- ..
6660
6661 Example:
6662
6663 typedef char* Pc;
6664 void g (const int Pc);
6665
6666 Here, Pc is *not* part of the decl-specifier seq; it's
6667 the declarator. Therefore, once we see a type-specifier
6668 (other than a cv-qualifier), we forbid any additional
6669 user-defined types. We *do* still allow things like `int
6670 int' to be considered a decl-specifier-seq, and issue the
6671 error message later. */
6672 if (decl_spec && !is_cv_qualifier)
6673 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6674 /* A constructor declarator cannot follow a type-specifier. */
6675 if (decl_spec)
6676 constructor_possible_p = false;
a723baf1
MM
6677 }
6678
6679 /* If we still do not have a DECL_SPEC, then there are no more
6680 decl-specifiers. */
6681 if (!decl_spec)
6682 {
6683 /* Issue an error message, unless the entire construct was
6684 optional. */
6685 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6686 {
6687 cp_parser_error (parser, "expected decl specifier");
6688 return error_mark_node;
6689 }
6690
6691 break;
6692 }
6693
6694 /* Add the DECL_SPEC to the list of specifiers. */
6695 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6696
6697 /* After we see one decl-specifier, further decl-specifiers are
6698 always optional. */
6699 flags |= CP_PARSER_FLAGS_OPTIONAL;
6700 }
6701
6702 /* We have built up the DECL_SPECS in reverse order. Return them in
6703 the correct order. */
6704 return nreverse (decl_specs);
6705}
6706
6707/* Parse an (optional) storage-class-specifier.
6708
6709 storage-class-specifier:
6710 auto
6711 register
6712 static
6713 extern
6714 mutable
6715
6716 GNU Extension:
6717
6718 storage-class-specifier:
6719 thread
6720
6721 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6722
6723static tree
94edc4ab 6724cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6725{
6726 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6727 {
6728 case RID_AUTO:
6729 case RID_REGISTER:
6730 case RID_STATIC:
6731 case RID_EXTERN:
6732 case RID_MUTABLE:
6733 case RID_THREAD:
6734 /* Consume the token. */
6735 return cp_lexer_consume_token (parser->lexer)->value;
6736
6737 default:
6738 return NULL_TREE;
6739 }
6740}
6741
6742/* Parse an (optional) function-specifier.
6743
6744 function-specifier:
6745 inline
6746 virtual
6747 explicit
6748
6749 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6750
6751static tree
94edc4ab 6752cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6753{
6754 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6755 {
6756 case RID_INLINE:
6757 case RID_VIRTUAL:
6758 case RID_EXPLICIT:
6759 /* Consume the token. */
6760 return cp_lexer_consume_token (parser->lexer)->value;
6761
6762 default:
6763 return NULL_TREE;
6764 }
6765}
6766
6767/* Parse a linkage-specification.
6768
6769 linkage-specification:
6770 extern string-literal { declaration-seq [opt] }
6771 extern string-literal declaration */
6772
6773static void
94edc4ab 6774cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6775{
6776 cp_token *token;
6777 tree linkage;
6778
6779 /* Look for the `extern' keyword. */
6780 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6781
6782 /* Peek at the next token. */
6783 token = cp_lexer_peek_token (parser->lexer);
6784 /* If it's not a string-literal, then there's a problem. */
6785 if (!cp_parser_is_string_literal (token))
6786 {
6787 cp_parser_error (parser, "expected language-name");
6788 return;
6789 }
6790 /* Consume the token. */
6791 cp_lexer_consume_token (parser->lexer);
6792
6793 /* Transform the literal into an identifier. If the literal is a
6794 wide-character string, or contains embedded NULs, then we can't
6795 handle it as the user wants. */
6796 if (token->type == CPP_WSTRING
6797 || (strlen (TREE_STRING_POINTER (token->value))
6798 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6799 {
6800 cp_parser_error (parser, "invalid linkage-specification");
6801 /* Assume C++ linkage. */
6802 linkage = get_identifier ("c++");
6803 }
6804 /* If it's a simple string constant, things are easier. */
6805 else
6806 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6807
6808 /* We're now using the new linkage. */
6809 push_lang_context (linkage);
6810
6811 /* If the next token is a `{', then we're using the first
6812 production. */
6813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6814 {
6815 /* Consume the `{' token. */
6816 cp_lexer_consume_token (parser->lexer);
6817 /* Parse the declarations. */
6818 cp_parser_declaration_seq_opt (parser);
6819 /* Look for the closing `}'. */
6820 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6821 }
6822 /* Otherwise, there's just one declaration. */
6823 else
6824 {
6825 bool saved_in_unbraced_linkage_specification_p;
6826
6827 saved_in_unbraced_linkage_specification_p
6828 = parser->in_unbraced_linkage_specification_p;
6829 parser->in_unbraced_linkage_specification_p = true;
6830 have_extern_spec = true;
6831 cp_parser_declaration (parser);
6832 have_extern_spec = false;
6833 parser->in_unbraced_linkage_specification_p
6834 = saved_in_unbraced_linkage_specification_p;
6835 }
6836
6837 /* We're done with the linkage-specification. */
6838 pop_lang_context ();
6839}
6840
6841/* Special member functions [gram.special] */
6842
6843/* Parse a conversion-function-id.
6844
6845 conversion-function-id:
6846 operator conversion-type-id
6847
6848 Returns an IDENTIFIER_NODE representing the operator. */
6849
6850static tree
94edc4ab 6851cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6852{
6853 tree type;
6854 tree saved_scope;
6855 tree saved_qualifying_scope;
6856 tree saved_object_scope;
6857
6858 /* Look for the `operator' token. */
6859 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6860 return error_mark_node;
6861 /* When we parse the conversion-type-id, the current scope will be
6862 reset. However, we need that information in able to look up the
6863 conversion function later, so we save it here. */
6864 saved_scope = parser->scope;
6865 saved_qualifying_scope = parser->qualifying_scope;
6866 saved_object_scope = parser->object_scope;
6867 /* We must enter the scope of the class so that the names of
6868 entities declared within the class are available in the
6869 conversion-type-id. For example, consider:
6870
6871 struct S {
6872 typedef int I;
6873 operator I();
6874 };
6875
6876 S::operator I() { ... }
6877
6878 In order to see that `I' is a type-name in the definition, we
6879 must be in the scope of `S'. */
6880 if (saved_scope)
6881 push_scope (saved_scope);
6882 /* Parse the conversion-type-id. */
6883 type = cp_parser_conversion_type_id (parser);
6884 /* Leave the scope of the class, if any. */
6885 if (saved_scope)
6886 pop_scope (saved_scope);
6887 /* Restore the saved scope. */
6888 parser->scope = saved_scope;
6889 parser->qualifying_scope = saved_qualifying_scope;
6890 parser->object_scope = saved_object_scope;
6891 /* If the TYPE is invalid, indicate failure. */
6892 if (type == error_mark_node)
6893 return error_mark_node;
6894 return mangle_conv_op_name_for_type (type);
6895}
6896
6897/* Parse a conversion-type-id:
6898
6899 conversion-type-id:
6900 type-specifier-seq conversion-declarator [opt]
6901
6902 Returns the TYPE specified. */
6903
6904static tree
94edc4ab 6905cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6906{
6907 tree attributes;
6908 tree type_specifiers;
6909 tree declarator;
6910
6911 /* Parse the attributes. */
6912 attributes = cp_parser_attributes_opt (parser);
6913 /* Parse the type-specifiers. */
6914 type_specifiers = cp_parser_type_specifier_seq (parser);
6915 /* If that didn't work, stop. */
6916 if (type_specifiers == error_mark_node)
6917 return error_mark_node;
6918 /* Parse the conversion-declarator. */
6919 declarator = cp_parser_conversion_declarator_opt (parser);
6920
6921 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6922 /*initialized=*/0, &attributes);
6923}
6924
6925/* Parse an (optional) conversion-declarator.
6926
6927 conversion-declarator:
6928 ptr-operator conversion-declarator [opt]
6929
6930 Returns a representation of the declarator. See
6931 cp_parser_declarator for details. */
6932
6933static tree
94edc4ab 6934cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
6935{
6936 enum tree_code code;
6937 tree class_type;
6938 tree cv_qualifier_seq;
6939
6940 /* We don't know if there's a ptr-operator next, or not. */
6941 cp_parser_parse_tentatively (parser);
6942 /* Try the ptr-operator. */
6943 code = cp_parser_ptr_operator (parser, &class_type,
6944 &cv_qualifier_seq);
6945 /* If it worked, look for more conversion-declarators. */
6946 if (cp_parser_parse_definitely (parser))
6947 {
6948 tree declarator;
6949
6950 /* Parse another optional declarator. */
6951 declarator = cp_parser_conversion_declarator_opt (parser);
6952
6953 /* Create the representation of the declarator. */
6954 if (code == INDIRECT_REF)
6955 declarator = make_pointer_declarator (cv_qualifier_seq,
6956 declarator);
6957 else
6958 declarator = make_reference_declarator (cv_qualifier_seq,
6959 declarator);
6960
6961 /* Handle the pointer-to-member case. */
6962 if (class_type)
6963 declarator = build_nt (SCOPE_REF, class_type, declarator);
6964
6965 return declarator;
6966 }
6967
6968 return NULL_TREE;
6969}
6970
6971/* Parse an (optional) ctor-initializer.
6972
6973 ctor-initializer:
6974 : mem-initializer-list
6975
6976 Returns TRUE iff the ctor-initializer was actually present. */
6977
6978static bool
94edc4ab 6979cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
6980{
6981 /* If the next token is not a `:', then there is no
6982 ctor-initializer. */
6983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6984 {
6985 /* Do default initialization of any bases and members. */
6986 if (DECL_CONSTRUCTOR_P (current_function_decl))
6987 finish_mem_initializers (NULL_TREE);
6988
6989 return false;
6990 }
6991
6992 /* Consume the `:' token. */
6993 cp_lexer_consume_token (parser->lexer);
6994 /* And the mem-initializer-list. */
6995 cp_parser_mem_initializer_list (parser);
6996
6997 return true;
6998}
6999
7000/* Parse a mem-initializer-list.
7001
7002 mem-initializer-list:
7003 mem-initializer
7004 mem-initializer , mem-initializer-list */
7005
7006static void
94edc4ab 7007cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7008{
7009 tree mem_initializer_list = NULL_TREE;
7010
7011 /* Let the semantic analysis code know that we are starting the
7012 mem-initializer-list. */
0e136342
MM
7013 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7014 error ("only constructors take base initializers");
a723baf1
MM
7015
7016 /* Loop through the list. */
7017 while (true)
7018 {
7019 tree mem_initializer;
7020
7021 /* Parse the mem-initializer. */
7022 mem_initializer = cp_parser_mem_initializer (parser);
7023 /* Add it to the list, unless it was erroneous. */
7024 if (mem_initializer)
7025 {
7026 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7027 mem_initializer_list = mem_initializer;
7028 }
7029 /* If the next token is not a `,', we're done. */
7030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7031 break;
7032 /* Consume the `,' token. */
7033 cp_lexer_consume_token (parser->lexer);
7034 }
7035
7036 /* Perform semantic analysis. */
0e136342
MM
7037 if (DECL_CONSTRUCTOR_P (current_function_decl))
7038 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7039}
7040
7041/* Parse a mem-initializer.
7042
7043 mem-initializer:
7044 mem-initializer-id ( expression-list [opt] )
7045
7046 GNU extension:
7047
7048 mem-initializer:
34cd5ae7 7049 ( expression-list [opt] )
a723baf1
MM
7050
7051 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7052 class) or FIELD_DECL (for a non-static data member) to initialize;
7053 the TREE_VALUE is the expression-list. */
7054
7055static tree
94edc4ab 7056cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7057{
7058 tree mem_initializer_id;
7059 tree expression_list;
1f5a253a
NS
7060 tree member;
7061
a723baf1
MM
7062 /* Find out what is being initialized. */
7063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7064 {
7065 pedwarn ("anachronistic old-style base class initializer");
7066 mem_initializer_id = NULL_TREE;
7067 }
7068 else
7069 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7070 member = expand_member_init (mem_initializer_id);
7071 if (member && !DECL_P (member))
7072 in_base_initializer = 1;
7efa3e22 7073
39703eb9
MM
7074 expression_list
7075 = cp_parser_parenthesized_expression_list (parser, false,
7076 /*non_constant_p=*/NULL);
7efa3e22 7077 if (!expression_list)
a723baf1 7078 expression_list = void_type_node;
a723baf1 7079
1f5a253a
NS
7080 in_base_initializer = 0;
7081
7082 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7083}
7084
7085/* Parse a mem-initializer-id.
7086
7087 mem-initializer-id:
7088 :: [opt] nested-name-specifier [opt] class-name
7089 identifier
7090
7091 Returns a TYPE indicating the class to be initializer for the first
7092 production. Returns an IDENTIFIER_NODE indicating the data member
7093 to be initialized for the second production. */
7094
7095static tree
94edc4ab 7096cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7097{
7098 bool global_scope_p;
7099 bool nested_name_specifier_p;
7100 tree id;
7101
7102 /* Look for the optional `::' operator. */
7103 global_scope_p
7104 = (cp_parser_global_scope_opt (parser,
7105 /*current_scope_valid_p=*/false)
7106 != NULL_TREE);
7107 /* Look for the optional nested-name-specifier. The simplest way to
7108 implement:
7109
7110 [temp.res]
7111
7112 The keyword `typename' is not permitted in a base-specifier or
7113 mem-initializer; in these contexts a qualified name that
7114 depends on a template-parameter is implicitly assumed to be a
7115 type name.
7116
7117 is to assume that we have seen the `typename' keyword at this
7118 point. */
7119 nested_name_specifier_p
7120 = (cp_parser_nested_name_specifier_opt (parser,
7121 /*typename_keyword_p=*/true,
7122 /*check_dependency_p=*/true,
a668c6ad
MM
7123 /*type_p=*/true,
7124 /*is_declaration=*/true)
a723baf1
MM
7125 != NULL_TREE);
7126 /* If there is a `::' operator or a nested-name-specifier, then we
7127 are definitely looking for a class-name. */
7128 if (global_scope_p || nested_name_specifier_p)
7129 return cp_parser_class_name (parser,
7130 /*typename_keyword_p=*/true,
7131 /*template_keyword_p=*/false,
7132 /*type_p=*/false,
a723baf1 7133 /*check_dependency_p=*/true,
a668c6ad
MM
7134 /*class_head_p=*/false,
7135 /*is_declaration=*/true);
a723baf1
MM
7136 /* Otherwise, we could also be looking for an ordinary identifier. */
7137 cp_parser_parse_tentatively (parser);
7138 /* Try a class-name. */
7139 id = cp_parser_class_name (parser,
7140 /*typename_keyword_p=*/true,
7141 /*template_keyword_p=*/false,
7142 /*type_p=*/false,
a723baf1 7143 /*check_dependency_p=*/true,
a668c6ad
MM
7144 /*class_head_p=*/false,
7145 /*is_declaration=*/true);
a723baf1
MM
7146 /* If we found one, we're done. */
7147 if (cp_parser_parse_definitely (parser))
7148 return id;
7149 /* Otherwise, look for an ordinary identifier. */
7150 return cp_parser_identifier (parser);
7151}
7152
7153/* Overloading [gram.over] */
7154
7155/* Parse an operator-function-id.
7156
7157 operator-function-id:
7158 operator operator
7159
7160 Returns an IDENTIFIER_NODE for the operator which is a
7161 human-readable spelling of the identifier, e.g., `operator +'. */
7162
7163static tree
94edc4ab 7164cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7165{
7166 /* Look for the `operator' keyword. */
7167 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7168 return error_mark_node;
7169 /* And then the name of the operator itself. */
7170 return cp_parser_operator (parser);
7171}
7172
7173/* Parse an operator.
7174
7175 operator:
7176 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7177 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7178 || ++ -- , ->* -> () []
7179
7180 GNU Extensions:
7181
7182 operator:
7183 <? >? <?= >?=
7184
7185 Returns an IDENTIFIER_NODE for the operator which is a
7186 human-readable spelling of the identifier, e.g., `operator +'. */
7187
7188static tree
94edc4ab 7189cp_parser_operator (cp_parser* parser)
a723baf1
MM
7190{
7191 tree id = NULL_TREE;
7192 cp_token *token;
7193
7194 /* Peek at the next token. */
7195 token = cp_lexer_peek_token (parser->lexer);
7196 /* Figure out which operator we have. */
7197 switch (token->type)
7198 {
7199 case CPP_KEYWORD:
7200 {
7201 enum tree_code op;
7202
7203 /* The keyword should be either `new' or `delete'. */
7204 if (token->keyword == RID_NEW)
7205 op = NEW_EXPR;
7206 else if (token->keyword == RID_DELETE)
7207 op = DELETE_EXPR;
7208 else
7209 break;
7210
7211 /* Consume the `new' or `delete' token. */
7212 cp_lexer_consume_token (parser->lexer);
7213
7214 /* Peek at the next token. */
7215 token = cp_lexer_peek_token (parser->lexer);
7216 /* If it's a `[' token then this is the array variant of the
7217 operator. */
7218 if (token->type == CPP_OPEN_SQUARE)
7219 {
7220 /* Consume the `[' token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 /* Look for the `]' token. */
7223 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7224 id = ansi_opname (op == NEW_EXPR
7225 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7226 }
7227 /* Otherwise, we have the non-array variant. */
7228 else
7229 id = ansi_opname (op);
7230
7231 return id;
7232 }
7233
7234 case CPP_PLUS:
7235 id = ansi_opname (PLUS_EXPR);
7236 break;
7237
7238 case CPP_MINUS:
7239 id = ansi_opname (MINUS_EXPR);
7240 break;
7241
7242 case CPP_MULT:
7243 id = ansi_opname (MULT_EXPR);
7244 break;
7245
7246 case CPP_DIV:
7247 id = ansi_opname (TRUNC_DIV_EXPR);
7248 break;
7249
7250 case CPP_MOD:
7251 id = ansi_opname (TRUNC_MOD_EXPR);
7252 break;
7253
7254 case CPP_XOR:
7255 id = ansi_opname (BIT_XOR_EXPR);
7256 break;
7257
7258 case CPP_AND:
7259 id = ansi_opname (BIT_AND_EXPR);
7260 break;
7261
7262 case CPP_OR:
7263 id = ansi_opname (BIT_IOR_EXPR);
7264 break;
7265
7266 case CPP_COMPL:
7267 id = ansi_opname (BIT_NOT_EXPR);
7268 break;
7269
7270 case CPP_NOT:
7271 id = ansi_opname (TRUTH_NOT_EXPR);
7272 break;
7273
7274 case CPP_EQ:
7275 id = ansi_assopname (NOP_EXPR);
7276 break;
7277
7278 case CPP_LESS:
7279 id = ansi_opname (LT_EXPR);
7280 break;
7281
7282 case CPP_GREATER:
7283 id = ansi_opname (GT_EXPR);
7284 break;
7285
7286 case CPP_PLUS_EQ:
7287 id = ansi_assopname (PLUS_EXPR);
7288 break;
7289
7290 case CPP_MINUS_EQ:
7291 id = ansi_assopname (MINUS_EXPR);
7292 break;
7293
7294 case CPP_MULT_EQ:
7295 id = ansi_assopname (MULT_EXPR);
7296 break;
7297
7298 case CPP_DIV_EQ:
7299 id = ansi_assopname (TRUNC_DIV_EXPR);
7300 break;
7301
7302 case CPP_MOD_EQ:
7303 id = ansi_assopname (TRUNC_MOD_EXPR);
7304 break;
7305
7306 case CPP_XOR_EQ:
7307 id = ansi_assopname (BIT_XOR_EXPR);
7308 break;
7309
7310 case CPP_AND_EQ:
7311 id = ansi_assopname (BIT_AND_EXPR);
7312 break;
7313
7314 case CPP_OR_EQ:
7315 id = ansi_assopname (BIT_IOR_EXPR);
7316 break;
7317
7318 case CPP_LSHIFT:
7319 id = ansi_opname (LSHIFT_EXPR);
7320 break;
7321
7322 case CPP_RSHIFT:
7323 id = ansi_opname (RSHIFT_EXPR);
7324 break;
7325
7326 case CPP_LSHIFT_EQ:
7327 id = ansi_assopname (LSHIFT_EXPR);
7328 break;
7329
7330 case CPP_RSHIFT_EQ:
7331 id = ansi_assopname (RSHIFT_EXPR);
7332 break;
7333
7334 case CPP_EQ_EQ:
7335 id = ansi_opname (EQ_EXPR);
7336 break;
7337
7338 case CPP_NOT_EQ:
7339 id = ansi_opname (NE_EXPR);
7340 break;
7341
7342 case CPP_LESS_EQ:
7343 id = ansi_opname (LE_EXPR);
7344 break;
7345
7346 case CPP_GREATER_EQ:
7347 id = ansi_opname (GE_EXPR);
7348 break;
7349
7350 case CPP_AND_AND:
7351 id = ansi_opname (TRUTH_ANDIF_EXPR);
7352 break;
7353
7354 case CPP_OR_OR:
7355 id = ansi_opname (TRUTH_ORIF_EXPR);
7356 break;
7357
7358 case CPP_PLUS_PLUS:
7359 id = ansi_opname (POSTINCREMENT_EXPR);
7360 break;
7361
7362 case CPP_MINUS_MINUS:
7363 id = ansi_opname (PREDECREMENT_EXPR);
7364 break;
7365
7366 case CPP_COMMA:
7367 id = ansi_opname (COMPOUND_EXPR);
7368 break;
7369
7370 case CPP_DEREF_STAR:
7371 id = ansi_opname (MEMBER_REF);
7372 break;
7373
7374 case CPP_DEREF:
7375 id = ansi_opname (COMPONENT_REF);
7376 break;
7377
7378 case CPP_OPEN_PAREN:
7379 /* Consume the `('. */
7380 cp_lexer_consume_token (parser->lexer);
7381 /* Look for the matching `)'. */
7382 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7383 return ansi_opname (CALL_EXPR);
7384
7385 case CPP_OPEN_SQUARE:
7386 /* Consume the `['. */
7387 cp_lexer_consume_token (parser->lexer);
7388 /* Look for the matching `]'. */
7389 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7390 return ansi_opname (ARRAY_REF);
7391
7392 /* Extensions. */
7393 case CPP_MIN:
7394 id = ansi_opname (MIN_EXPR);
7395 break;
7396
7397 case CPP_MAX:
7398 id = ansi_opname (MAX_EXPR);
7399 break;
7400
7401 case CPP_MIN_EQ:
7402 id = ansi_assopname (MIN_EXPR);
7403 break;
7404
7405 case CPP_MAX_EQ:
7406 id = ansi_assopname (MAX_EXPR);
7407 break;
7408
7409 default:
7410 /* Anything else is an error. */
7411 break;
7412 }
7413
7414 /* If we have selected an identifier, we need to consume the
7415 operator token. */
7416 if (id)
7417 cp_lexer_consume_token (parser->lexer);
7418 /* Otherwise, no valid operator name was present. */
7419 else
7420 {
7421 cp_parser_error (parser, "expected operator");
7422 id = error_mark_node;
7423 }
7424
7425 return id;
7426}
7427
7428/* Parse a template-declaration.
7429
7430 template-declaration:
7431 export [opt] template < template-parameter-list > declaration
7432
7433 If MEMBER_P is TRUE, this template-declaration occurs within a
7434 class-specifier.
7435
7436 The grammar rule given by the standard isn't correct. What
7437 is really meant is:
7438
7439 template-declaration:
7440 export [opt] template-parameter-list-seq
7441 decl-specifier-seq [opt] init-declarator [opt] ;
7442 export [opt] template-parameter-list-seq
7443 function-definition
7444
7445 template-parameter-list-seq:
7446 template-parameter-list-seq [opt]
7447 template < template-parameter-list > */
7448
7449static void
94edc4ab 7450cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7451{
7452 /* Check for `export'. */
7453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7454 {
7455 /* Consume the `export' token. */
7456 cp_lexer_consume_token (parser->lexer);
7457 /* Warn that we do not support `export'. */
7458 warning ("keyword `export' not implemented, and will be ignored");
7459 }
7460
7461 cp_parser_template_declaration_after_export (parser, member_p);
7462}
7463
7464/* Parse a template-parameter-list.
7465
7466 template-parameter-list:
7467 template-parameter
7468 template-parameter-list , template-parameter
7469
7470 Returns a TREE_LIST. Each node represents a template parameter.
7471 The nodes are connected via their TREE_CHAINs. */
7472
7473static tree
94edc4ab 7474cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7475{
7476 tree parameter_list = NULL_TREE;
7477
7478 while (true)
7479 {
7480 tree parameter;
7481 cp_token *token;
7482
7483 /* Parse the template-parameter. */
7484 parameter = cp_parser_template_parameter (parser);
7485 /* Add it to the list. */
7486 parameter_list = process_template_parm (parameter_list,
7487 parameter);
7488
7489 /* Peek at the next token. */
7490 token = cp_lexer_peek_token (parser->lexer);
7491 /* If it's not a `,', we're done. */
7492 if (token->type != CPP_COMMA)
7493 break;
7494 /* Otherwise, consume the `,' token. */
7495 cp_lexer_consume_token (parser->lexer);
7496 }
7497
7498 return parameter_list;
7499}
7500
7501/* Parse a template-parameter.
7502
7503 template-parameter:
7504 type-parameter
7505 parameter-declaration
7506
7507 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7508 TREE_PURPOSE is the default value, if any. */
7509
7510static tree
94edc4ab 7511cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7512{
7513 cp_token *token;
7514
7515 /* Peek at the next token. */
7516 token = cp_lexer_peek_token (parser->lexer);
7517 /* If it is `class' or `template', we have a type-parameter. */
7518 if (token->keyword == RID_TEMPLATE)
7519 return cp_parser_type_parameter (parser);
7520 /* If it is `class' or `typename' we do not know yet whether it is a
7521 type parameter or a non-type parameter. Consider:
7522
7523 template <typename T, typename T::X X> ...
7524
7525 or:
7526
7527 template <class C, class D*> ...
7528
7529 Here, the first parameter is a type parameter, and the second is
7530 a non-type parameter. We can tell by looking at the token after
7531 the identifier -- if it is a `,', `=', or `>' then we have a type
7532 parameter. */
7533 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7534 {
7535 /* Peek at the token after `class' or `typename'. */
7536 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7537 /* If it's an identifier, skip it. */
7538 if (token->type == CPP_NAME)
7539 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7540 /* Now, see if the token looks like the end of a template
7541 parameter. */
7542 if (token->type == CPP_COMMA
7543 || token->type == CPP_EQ
7544 || token->type == CPP_GREATER)
7545 return cp_parser_type_parameter (parser);
7546 }
7547
7548 /* Otherwise, it is a non-type parameter.
7549
7550 [temp.param]
7551
7552 When parsing a default template-argument for a non-type
7553 template-parameter, the first non-nested `>' is taken as the end
7554 of the template parameter-list rather than a greater-than
7555 operator. */
7556 return
4bb8ca28
MM
7557 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7558 /*parenthesized_p=*/NULL);
a723baf1
MM
7559}
7560
7561/* Parse a type-parameter.
7562
7563 type-parameter:
7564 class identifier [opt]
7565 class identifier [opt] = type-id
7566 typename identifier [opt]
7567 typename identifier [opt] = type-id
7568 template < template-parameter-list > class identifier [opt]
7569 template < template-parameter-list > class identifier [opt]
7570 = id-expression
7571
7572 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7573 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7574 the declaration of the parameter. */
7575
7576static tree
94edc4ab 7577cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7578{
7579 cp_token *token;
7580 tree parameter;
7581
7582 /* Look for a keyword to tell us what kind of parameter this is. */
7583 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7584 "`class', `typename', or `template'");
a723baf1
MM
7585 if (!token)
7586 return error_mark_node;
7587
7588 switch (token->keyword)
7589 {
7590 case RID_CLASS:
7591 case RID_TYPENAME:
7592 {
7593 tree identifier;
7594 tree default_argument;
7595
7596 /* If the next token is an identifier, then it names the
7597 parameter. */
7598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7599 identifier = cp_parser_identifier (parser);
7600 else
7601 identifier = NULL_TREE;
7602
7603 /* Create the parameter. */
7604 parameter = finish_template_type_parm (class_type_node, identifier);
7605
7606 /* If the next token is an `=', we have a default argument. */
7607 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7608 {
7609 /* Consume the `=' token. */
7610 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7611 /* Parse the default-argument. */
a723baf1
MM
7612 default_argument = cp_parser_type_id (parser);
7613 }
7614 else
7615 default_argument = NULL_TREE;
7616
7617 /* Create the combined representation of the parameter and the
7618 default argument. */
c67d36d0 7619 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7620 }
7621 break;
7622
7623 case RID_TEMPLATE:
7624 {
7625 tree parameter_list;
7626 tree identifier;
7627 tree default_argument;
7628
7629 /* Look for the `<'. */
7630 cp_parser_require (parser, CPP_LESS, "`<'");
7631 /* Parse the template-parameter-list. */
7632 begin_template_parm_list ();
7633 parameter_list
7634 = cp_parser_template_parameter_list (parser);
7635 parameter_list = end_template_parm_list (parameter_list);
7636 /* Look for the `>'. */
7637 cp_parser_require (parser, CPP_GREATER, "`>'");
7638 /* Look for the `class' keyword. */
7639 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7640 /* If the next token is an `=', then there is a
7641 default-argument. If the next token is a `>', we are at
7642 the end of the parameter-list. If the next token is a `,',
7643 then we are at the end of this parameter. */
7644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7645 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7646 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7647 identifier = cp_parser_identifier (parser);
7648 else
7649 identifier = NULL_TREE;
7650 /* Create the template parameter. */
7651 parameter = finish_template_template_parm (class_type_node,
7652 identifier);
7653
7654 /* If the next token is an `=', then there is a
7655 default-argument. */
7656 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7657 {
7658 /* Consume the `='. */
7659 cp_lexer_consume_token (parser->lexer);
7660 /* Parse the id-expression. */
7661 default_argument
7662 = cp_parser_id_expression (parser,
7663 /*template_keyword_p=*/false,
7664 /*check_dependency_p=*/true,
f3c2dfc6
MM
7665 /*template_p=*/NULL,
7666 /*declarator_p=*/false);
a723baf1
MM
7667 /* Look up the name. */
7668 default_argument
7669 = cp_parser_lookup_name_simple (parser, default_argument);
7670 /* See if the default argument is valid. */
7671 default_argument
7672 = check_template_template_default_arg (default_argument);
7673 }
7674 else
7675 default_argument = NULL_TREE;
7676
7677 /* Create the combined representation of the parameter and the
7678 default argument. */
c67d36d0 7679 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7680 }
7681 break;
7682
7683 default:
7684 /* Anything else is an error. */
7685 cp_parser_error (parser,
7686 "expected `class', `typename', or `template'");
7687 parameter = error_mark_node;
7688 }
7689
7690 return parameter;
7691}
7692
7693/* Parse a template-id.
7694
7695 template-id:
7696 template-name < template-argument-list [opt] >
7697
7698 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7699 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7700 returned. Otherwise, if the template-name names a function, or set
7701 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7702 names a class, returns a TYPE_DECL for the specialization.
7703
7704 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7705 uninstantiated templates. */
7706
7707static tree
7708cp_parser_template_id (cp_parser *parser,
7709 bool template_keyword_p,
a668c6ad
MM
7710 bool check_dependency_p,
7711 bool is_declaration)
a723baf1
MM
7712{
7713 tree template;
7714 tree arguments;
a723baf1 7715 tree template_id;
a723baf1
MM
7716 ptrdiff_t start_of_id;
7717 tree access_check = NULL_TREE;
2050a1bb 7718 cp_token *next_token;
a668c6ad 7719 bool is_identifier;
a723baf1
MM
7720
7721 /* If the next token corresponds to a template-id, there is no need
7722 to reparse it. */
2050a1bb
MM
7723 next_token = cp_lexer_peek_token (parser->lexer);
7724 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7725 {
7726 tree value;
7727 tree check;
7728
7729 /* Get the stored value. */
7730 value = cp_lexer_consume_token (parser->lexer)->value;
7731 /* Perform any access checks that were deferred. */
7732 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7733 perform_or_defer_access_check (TREE_PURPOSE (check),
7734 TREE_VALUE (check));
a723baf1
MM
7735 /* Return the stored value. */
7736 return TREE_VALUE (value);
7737 }
7738
2050a1bb
MM
7739 /* Avoid performing name lookup if there is no possibility of
7740 finding a template-id. */
7741 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7742 || (next_token->type == CPP_NAME
7743 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7744 {
7745 cp_parser_error (parser, "expected template-id");
7746 return error_mark_node;
7747 }
7748
a723baf1
MM
7749 /* Remember where the template-id starts. */
7750 if (cp_parser_parsing_tentatively (parser)
7751 && !cp_parser_committed_to_tentative_parse (parser))
7752 {
2050a1bb 7753 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7754 start_of_id = cp_lexer_token_difference (parser->lexer,
7755 parser->lexer->first_token,
7756 next_token);
a723baf1
MM
7757 }
7758 else
7759 start_of_id = -1;
7760
8d241e0b 7761 push_deferring_access_checks (dk_deferred);
cf22909c 7762
a723baf1 7763 /* Parse the template-name. */
a668c6ad 7764 is_identifier = false;
a723baf1 7765 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
7766 check_dependency_p,
7767 is_declaration,
7768 &is_identifier);
7769 if (template == error_mark_node || is_identifier)
cf22909c
KL
7770 {
7771 pop_deferring_access_checks ();
a668c6ad 7772 return template;
cf22909c 7773 }
a723baf1
MM
7774
7775 /* Look for the `<' that starts the template-argument-list. */
7776 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7777 {
7778 pop_deferring_access_checks ();
7779 return error_mark_node;
7780 }
a723baf1 7781
ec75414f
MM
7782 /* Parse the arguments. */
7783 arguments = cp_parser_enclosed_template_argument_list (parser);
a723baf1
MM
7784
7785 /* Build a representation of the specialization. */
7786 if (TREE_CODE (template) == IDENTIFIER_NODE)
7787 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7788 else if (DECL_CLASS_TEMPLATE_P (template)
7789 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7790 template_id
7791 = finish_template_type (template, arguments,
7792 cp_lexer_next_token_is (parser->lexer,
7793 CPP_SCOPE));
7794 else
7795 {
7796 /* If it's not a class-template or a template-template, it should be
7797 a function-template. */
7798 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7799 || TREE_CODE (template) == OVERLOAD
7800 || BASELINK_P (template)),
7801 20010716);
7802
7803 template_id = lookup_template_function (template, arguments);
7804 }
7805
cf22909c
KL
7806 /* Retrieve any deferred checks. Do not pop this access checks yet
7807 so the memory will not be reclaimed during token replacing below. */
7808 access_check = get_deferred_access_checks ();
7809
a723baf1
MM
7810 /* If parsing tentatively, replace the sequence of tokens that makes
7811 up the template-id with a CPP_TEMPLATE_ID token. That way,
7812 should we re-parse the token stream, we will not have to repeat
7813 the effort required to do the parse, nor will we issue duplicate
7814 error messages about problems during instantiation of the
7815 template. */
7816 if (start_of_id >= 0)
7817 {
7818 cp_token *token;
a723baf1
MM
7819
7820 /* Find the token that corresponds to the start of the
7821 template-id. */
7822 token = cp_lexer_advance_token (parser->lexer,
7823 parser->lexer->first_token,
7824 start_of_id);
7825
a723baf1
MM
7826 /* Reset the contents of the START_OF_ID token. */
7827 token->type = CPP_TEMPLATE_ID;
7828 token->value = build_tree_list (access_check, template_id);
7829 token->keyword = RID_MAX;
7830 /* Purge all subsequent tokens. */
7831 cp_lexer_purge_tokens_after (parser->lexer, token);
7832 }
7833
cf22909c 7834 pop_deferring_access_checks ();
a723baf1
MM
7835 return template_id;
7836}
7837
7838/* Parse a template-name.
7839
7840 template-name:
7841 identifier
7842
7843 The standard should actually say:
7844
7845 template-name:
7846 identifier
7847 operator-function-id
7848 conversion-function-id
7849
7850 A defect report has been filed about this issue.
7851
7852 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7853 `template' keyword, in a construction like:
7854
7855 T::template f<3>()
7856
7857 In that case `f' is taken to be a template-name, even though there
7858 is no way of knowing for sure.
7859
7860 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7861 name refers to a set of overloaded functions, at least one of which
7862 is a template, or an IDENTIFIER_NODE with the name of the template,
7863 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7864 names are looked up inside uninstantiated templates. */
7865
7866static tree
94edc4ab
NN
7867cp_parser_template_name (cp_parser* parser,
7868 bool template_keyword_p,
a668c6ad
MM
7869 bool check_dependency_p,
7870 bool is_declaration,
7871 bool *is_identifier)
a723baf1
MM
7872{
7873 tree identifier;
7874 tree decl;
7875 tree fns;
7876
7877 /* If the next token is `operator', then we have either an
7878 operator-function-id or a conversion-function-id. */
7879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7880 {
7881 /* We don't know whether we're looking at an
7882 operator-function-id or a conversion-function-id. */
7883 cp_parser_parse_tentatively (parser);
7884 /* Try an operator-function-id. */
7885 identifier = cp_parser_operator_function_id (parser);
7886 /* If that didn't work, try a conversion-function-id. */
7887 if (!cp_parser_parse_definitely (parser))
7888 identifier = cp_parser_conversion_function_id (parser);
7889 }
7890 /* Look for the identifier. */
7891 else
7892 identifier = cp_parser_identifier (parser);
7893
7894 /* If we didn't find an identifier, we don't have a template-id. */
7895 if (identifier == error_mark_node)
7896 return error_mark_node;
7897
7898 /* If the name immediately followed the `template' keyword, then it
7899 is a template-name. However, if the next token is not `<', then
7900 we do not treat it as a template-name, since it is not being used
7901 as part of a template-id. This enables us to handle constructs
7902 like:
7903
7904 template <typename T> struct S { S(); };
7905 template <typename T> S<T>::S();
7906
7907 correctly. We would treat `S' as a template -- if it were `S<T>'
7908 -- but we do not if there is no `<'. */
a668c6ad
MM
7909
7910 if (processing_template_decl
a723baf1 7911 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
a668c6ad
MM
7912 {
7913 /* In a declaration, in a dependent context, we pretend that the
7914 "template" keyword was present in order to improve error
7915 recovery. For example, given:
7916
7917 template <typename T> void f(T::X<int>);
7918
7919 we want to treat "X<int>" as a template-id. */
7920 if (is_declaration
7921 && !template_keyword_p
7922 && parser->scope && TYPE_P (parser->scope)
7923 && dependent_type_p (parser->scope))
7924 {
7925 ptrdiff_t start;
7926 cp_token* token;
7927 /* Explain what went wrong. */
7928 error ("non-template `%D' used as template", identifier);
7929 error ("(use `%T::template %D' to indicate that it is a template)",
7930 parser->scope, identifier);
7931 /* If parsing tentatively, find the location of the "<"
7932 token. */
7933 if (cp_parser_parsing_tentatively (parser)
7934 && !cp_parser_committed_to_tentative_parse (parser))
7935 {
7936 cp_parser_simulate_error (parser);
7937 token = cp_lexer_peek_token (parser->lexer);
7938 token = cp_lexer_prev_token (parser->lexer, token);
7939 start = cp_lexer_token_difference (parser->lexer,
7940 parser->lexer->first_token,
7941 token);
7942 }
7943 else
7944 start = -1;
7945 /* Parse the template arguments so that we can issue error
7946 messages about them. */
7947 cp_lexer_consume_token (parser->lexer);
7948 cp_parser_enclosed_template_argument_list (parser);
7949 /* Skip tokens until we find a good place from which to
7950 continue parsing. */
7951 cp_parser_skip_to_closing_parenthesis (parser,
7952 /*recovering=*/true,
7953 /*or_comma=*/true,
7954 /*consume_paren=*/false);
7955 /* If parsing tentatively, permanently remove the
7956 template argument list. That will prevent duplicate
7957 error messages from being issued about the missing
7958 "template" keyword. */
7959 if (start >= 0)
7960 {
7961 token = cp_lexer_advance_token (parser->lexer,
7962 parser->lexer->first_token,
7963 start);
7964 cp_lexer_purge_tokens_after (parser->lexer, token);
7965 }
7966 if (is_identifier)
7967 *is_identifier = true;
7968 return identifier;
7969 }
7970 if (template_keyword_p)
7971 return identifier;
7972 }
a723baf1
MM
7973
7974 /* Look up the name. */
7975 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 7976 /*is_type=*/false,
eea9800f 7977 /*is_namespace=*/false,
a723baf1
MM
7978 check_dependency_p);
7979 decl = maybe_get_template_decl_from_type_decl (decl);
7980
7981 /* If DECL is a template, then the name was a template-name. */
7982 if (TREE_CODE (decl) == TEMPLATE_DECL)
7983 ;
7984 else
7985 {
7986 /* The standard does not explicitly indicate whether a name that
7987 names a set of overloaded declarations, some of which are
7988 templates, is a template-name. However, such a name should
7989 be a template-name; otherwise, there is no way to form a
7990 template-id for the overloaded templates. */
7991 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7992 if (TREE_CODE (fns) == OVERLOAD)
7993 {
7994 tree fn;
7995
7996 for (fn = fns; fn; fn = OVL_NEXT (fn))
7997 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7998 break;
7999 }
8000 else
8001 {
8002 /* Otherwise, the name does not name a template. */
8003 cp_parser_error (parser, "expected template-name");
8004 return error_mark_node;
8005 }
8006 }
8007
8008 /* If DECL is dependent, and refers to a function, then just return
8009 its name; we will look it up again during template instantiation. */
8010 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8011 {
8012 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8013 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8014 return identifier;
8015 }
8016
8017 return decl;
8018}
8019
8020/* Parse a template-argument-list.
8021
8022 template-argument-list:
8023 template-argument
8024 template-argument-list , template-argument
8025
04c06002 8026 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8027
8028static tree
94edc4ab 8029cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8030{
bf12d54d
NS
8031 tree fixed_args[10];
8032 unsigned n_args = 0;
8033 unsigned alloced = 10;
8034 tree *arg_ary = fixed_args;
8035 tree vec;
4bb8ca28 8036 bool saved_in_template_argument_list_p;
a723baf1 8037
4bb8ca28
MM
8038 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8039 parser->in_template_argument_list_p = true;
bf12d54d 8040 do
a723baf1
MM
8041 {
8042 tree argument;
8043
bf12d54d 8044 if (n_args)
04c06002 8045 /* Consume the comma. */
bf12d54d
NS
8046 cp_lexer_consume_token (parser->lexer);
8047
a723baf1
MM
8048 /* Parse the template-argument. */
8049 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8050 if (n_args == alloced)
8051 {
8052 alloced *= 2;
8053
8054 if (arg_ary == fixed_args)
8055 {
8056 arg_ary = xmalloc (sizeof (tree) * alloced);
8057 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8058 }
8059 else
8060 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8061 }
8062 arg_ary[n_args++] = argument;
a723baf1 8063 }
bf12d54d
NS
8064 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8065
8066 vec = make_tree_vec (n_args);
a723baf1 8067
bf12d54d
NS
8068 while (n_args--)
8069 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8070
8071 if (arg_ary != fixed_args)
8072 free (arg_ary);
4bb8ca28 8073 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8074 return vec;
a723baf1
MM
8075}
8076
8077/* Parse a template-argument.
8078
8079 template-argument:
8080 assignment-expression
8081 type-id
8082 id-expression
8083
8084 The representation is that of an assignment-expression, type-id, or
8085 id-expression -- except that the qualified id-expression is
8086 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
8087 OVERLOAD.
8088
8089 Although the standard says "assignment-expression", it forbids
8090 throw-expressions or assignments in the template argument.
8091 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8092
8093static tree
94edc4ab 8094cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8095{
8096 tree argument;
8097 bool template_p;
d17811fd
MM
8098 bool address_p;
8099 cp_token *token;
b3445994 8100 cp_id_kind idk;
d17811fd 8101 tree qualifying_class;
a723baf1
MM
8102
8103 /* There's really no way to know what we're looking at, so we just
8104 try each alternative in order.
8105
8106 [temp.arg]
8107
8108 In a template-argument, an ambiguity between a type-id and an
8109 expression is resolved to a type-id, regardless of the form of
8110 the corresponding template-parameter.
8111
8112 Therefore, we try a type-id first. */
8113 cp_parser_parse_tentatively (parser);
a723baf1
MM
8114 argument = cp_parser_type_id (parser);
8115 /* If the next token isn't a `,' or a `>', then this argument wasn't
8116 really finished. */
d17811fd 8117 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8118 cp_parser_error (parser, "expected template-argument");
8119 /* If that worked, we're done. */
8120 if (cp_parser_parse_definitely (parser))
8121 return argument;
8122 /* We're still not sure what the argument will be. */
8123 cp_parser_parse_tentatively (parser);
8124 /* Try a template. */
8125 argument = cp_parser_id_expression (parser,
8126 /*template_keyword_p=*/false,
8127 /*check_dependency_p=*/true,
f3c2dfc6
MM
8128 &template_p,
8129 /*declarator_p=*/false);
a723baf1
MM
8130 /* If the next token isn't a `,' or a `>', then this argument wasn't
8131 really finished. */
d17811fd 8132 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8133 cp_parser_error (parser, "expected template-argument");
8134 if (!cp_parser_error_occurred (parser))
8135 {
8136 /* Figure out what is being referred to. */
8137 argument = cp_parser_lookup_name_simple (parser, argument);
8138 if (template_p)
8139 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8140 TREE_OPERAND (argument, 1),
78757caa 8141 tf_error);
a723baf1
MM
8142 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8143 cp_parser_error (parser, "expected template-name");
8144 }
8145 if (cp_parser_parse_definitely (parser))
8146 return argument;
d17811fd
MM
8147 /* It must be a non-type argument. There permitted cases are given
8148 in [temp.arg.nontype]:
8149
8150 -- an integral constant-expression of integral or enumeration
8151 type; or
8152
8153 -- the name of a non-type template-parameter; or
8154
8155 -- the name of an object or function with external linkage...
8156
8157 -- the address of an object or function with external linkage...
8158
04c06002 8159 -- a pointer to member... */
d17811fd
MM
8160 /* Look for a non-type template parameter. */
8161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8162 {
8163 cp_parser_parse_tentatively (parser);
8164 argument = cp_parser_primary_expression (parser,
8165 &idk,
8166 &qualifying_class);
8167 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8168 || !cp_parser_next_token_ends_template_argument_p (parser))
8169 cp_parser_simulate_error (parser);
8170 if (cp_parser_parse_definitely (parser))
8171 return argument;
8172 }
8173 /* If the next token is "&", the argument must be the address of an
8174 object or function with external linkage. */
8175 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8176 if (address_p)
8177 cp_lexer_consume_token (parser->lexer);
8178 /* See if we might have an id-expression. */
8179 token = cp_lexer_peek_token (parser->lexer);
8180 if (token->type == CPP_NAME
8181 || token->keyword == RID_OPERATOR
8182 || token->type == CPP_SCOPE
8183 || token->type == CPP_TEMPLATE_ID
8184 || token->type == CPP_NESTED_NAME_SPECIFIER)
8185 {
8186 cp_parser_parse_tentatively (parser);
8187 argument = cp_parser_primary_expression (parser,
8188 &idk,
8189 &qualifying_class);
8190 if (cp_parser_error_occurred (parser)
8191 || !cp_parser_next_token_ends_template_argument_p (parser))
8192 cp_parser_abort_tentative_parse (parser);
8193 else
8194 {
8195 if (qualifying_class)
8196 argument = finish_qualified_id_expr (qualifying_class,
8197 argument,
8198 /*done=*/true,
8199 address_p);
8200 if (TREE_CODE (argument) == VAR_DECL)
8201 {
8202 /* A variable without external linkage might still be a
8203 valid constant-expression, so no error is issued here
8204 if the external-linkage check fails. */
8205 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8206 cp_parser_simulate_error (parser);
8207 }
8208 else if (is_overloaded_fn (argument))
8209 /* All overloaded functions are allowed; if the external
8210 linkage test does not pass, an error will be issued
8211 later. */
8212 ;
8213 else if (address_p
8214 && (TREE_CODE (argument) == OFFSET_REF
8215 || TREE_CODE (argument) == SCOPE_REF))
8216 /* A pointer-to-member. */
8217 ;
8218 else
8219 cp_parser_simulate_error (parser);
8220
8221 if (cp_parser_parse_definitely (parser))
8222 {
8223 if (address_p)
8224 argument = build_x_unary_op (ADDR_EXPR, argument);
8225 return argument;
8226 }
8227 }
8228 }
8229 /* If the argument started with "&", there are no other valid
8230 alternatives at this point. */
8231 if (address_p)
8232 {
8233 cp_parser_error (parser, "invalid non-type template argument");
8234 return error_mark_node;
8235 }
04c06002 8236 /* The argument must be a constant-expression. */
d17811fd
MM
8237 argument = cp_parser_constant_expression (parser,
8238 /*allow_non_constant_p=*/false,
8239 /*non_constant_p=*/NULL);
8240 /* If it's non-dependent, simplify it. */
8241 return cp_parser_fold_non_dependent_expr (argument);
a723baf1
MM
8242}
8243
8244/* Parse an explicit-instantiation.
8245
8246 explicit-instantiation:
8247 template declaration
8248
8249 Although the standard says `declaration', what it really means is:
8250
8251 explicit-instantiation:
8252 template decl-specifier-seq [opt] declarator [opt] ;
8253
8254 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8255 supposed to be allowed. A defect report has been filed about this
8256 issue.
8257
8258 GNU Extension:
8259
8260 explicit-instantiation:
8261 storage-class-specifier template
8262 decl-specifier-seq [opt] declarator [opt] ;
8263 function-specifier template
8264 decl-specifier-seq [opt] declarator [opt] ; */
8265
8266static void
94edc4ab 8267cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8268{
560ad596 8269 int declares_class_or_enum;
a723baf1
MM
8270 tree decl_specifiers;
8271 tree attributes;
8272 tree extension_specifier = NULL_TREE;
8273
8274 /* Look for an (optional) storage-class-specifier or
8275 function-specifier. */
8276 if (cp_parser_allow_gnu_extensions_p (parser))
8277 {
8278 extension_specifier
8279 = cp_parser_storage_class_specifier_opt (parser);
8280 if (!extension_specifier)
8281 extension_specifier = cp_parser_function_specifier_opt (parser);
8282 }
8283
8284 /* Look for the `template' keyword. */
8285 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8286 /* Let the front end know that we are processing an explicit
8287 instantiation. */
8288 begin_explicit_instantiation ();
8289 /* [temp.explicit] says that we are supposed to ignore access
8290 control while processing explicit instantiation directives. */
78757caa 8291 push_deferring_access_checks (dk_no_check);
a723baf1
MM
8292 /* Parse a decl-specifier-seq. */
8293 decl_specifiers
8294 = cp_parser_decl_specifier_seq (parser,
8295 CP_PARSER_FLAGS_OPTIONAL,
8296 &attributes,
8297 &declares_class_or_enum);
8298 /* If there was exactly one decl-specifier, and it declared a class,
8299 and there's no declarator, then we have an explicit type
8300 instantiation. */
8301 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8302 {
8303 tree type;
8304
8305 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8306 /* Turn access control back on for names used during
8307 template instantiation. */
8308 pop_deferring_access_checks ();
a723baf1
MM
8309 if (type)
8310 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8311 }
8312 else
8313 {
8314 tree declarator;
8315 tree decl;
8316
8317 /* Parse the declarator. */
8318 declarator
62b8a44e 8319 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
8320 /*ctor_dtor_or_conv_p=*/NULL,
8321 /*parenthesized_p=*/NULL);
560ad596
MM
8322 cp_parser_check_for_definition_in_return_type (declarator,
8323 declares_class_or_enum);
a723baf1
MM
8324 decl = grokdeclarator (declarator, decl_specifiers,
8325 NORMAL, 0, NULL);
b7fc8b57
KL
8326 /* Turn access control back on for names used during
8327 template instantiation. */
8328 pop_deferring_access_checks ();
a723baf1
MM
8329 /* Do the explicit instantiation. */
8330 do_decl_instantiation (decl, extension_specifier);
8331 }
8332 /* We're done with the instantiation. */
8333 end_explicit_instantiation ();
a723baf1 8334
e0860732 8335 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8336}
8337
8338/* Parse an explicit-specialization.
8339
8340 explicit-specialization:
8341 template < > declaration
8342
8343 Although the standard says `declaration', what it really means is:
8344
8345 explicit-specialization:
8346 template <> decl-specifier [opt] init-declarator [opt] ;
8347 template <> function-definition
8348 template <> explicit-specialization
8349 template <> template-declaration */
8350
8351static void
94edc4ab 8352cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8353{
8354 /* Look for the `template' keyword. */
8355 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8356 /* Look for the `<'. */
8357 cp_parser_require (parser, CPP_LESS, "`<'");
8358 /* Look for the `>'. */
8359 cp_parser_require (parser, CPP_GREATER, "`>'");
8360 /* We have processed another parameter list. */
8361 ++parser->num_template_parameter_lists;
8362 /* Let the front end know that we are beginning a specialization. */
8363 begin_specialization ();
8364
8365 /* If the next keyword is `template', we need to figure out whether
8366 or not we're looking a template-declaration. */
8367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8368 {
8369 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8370 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8371 cp_parser_template_declaration_after_export (parser,
8372 /*member_p=*/false);
8373 else
8374 cp_parser_explicit_specialization (parser);
8375 }
8376 else
8377 /* Parse the dependent declaration. */
8378 cp_parser_single_declaration (parser,
8379 /*member_p=*/false,
8380 /*friend_p=*/NULL);
8381
8382 /* We're done with the specialization. */
8383 end_specialization ();
8384 /* We're done with this parameter list. */
8385 --parser->num_template_parameter_lists;
8386}
8387
8388/* Parse a type-specifier.
8389
8390 type-specifier:
8391 simple-type-specifier
8392 class-specifier
8393 enum-specifier
8394 elaborated-type-specifier
8395 cv-qualifier
8396
8397 GNU Extension:
8398
8399 type-specifier:
8400 __complex__
8401
8402 Returns a representation of the type-specifier. If the
8403 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8404 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8405 For a class-specifier, enum-specifier, or elaborated-type-specifier
8406 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8407
8408 If IS_FRIEND is TRUE then this type-specifier is being declared a
8409 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8410 appearing in a decl-specifier-seq.
8411
8412 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8413 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8414 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8415 if a type is declared; 2 if it is defined. Otherwise, it is set to
8416 zero.
a723baf1
MM
8417
8418 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8419 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8420 is set to FALSE. */
8421
8422static tree
94edc4ab
NN
8423cp_parser_type_specifier (cp_parser* parser,
8424 cp_parser_flags flags,
8425 bool is_friend,
8426 bool is_declaration,
560ad596 8427 int* declares_class_or_enum,
94edc4ab 8428 bool* is_cv_qualifier)
a723baf1
MM
8429{
8430 tree type_spec = NULL_TREE;
8431 cp_token *token;
8432 enum rid keyword;
8433
8434 /* Assume this type-specifier does not declare a new type. */
8435 if (declares_class_or_enum)
8436 *declares_class_or_enum = false;
8437 /* And that it does not specify a cv-qualifier. */
8438 if (is_cv_qualifier)
8439 *is_cv_qualifier = false;
8440 /* Peek at the next token. */
8441 token = cp_lexer_peek_token (parser->lexer);
8442
8443 /* If we're looking at a keyword, we can use that to guide the
8444 production we choose. */
8445 keyword = token->keyword;
8446 switch (keyword)
8447 {
8448 /* Any of these indicate either a class-specifier, or an
8449 elaborated-type-specifier. */
8450 case RID_CLASS:
8451 case RID_STRUCT:
8452 case RID_UNION:
8453 case RID_ENUM:
8454 /* Parse tentatively so that we can back up if we don't find a
8455 class-specifier or enum-specifier. */
8456 cp_parser_parse_tentatively (parser);
8457 /* Look for the class-specifier or enum-specifier. */
8458 if (keyword == RID_ENUM)
8459 type_spec = cp_parser_enum_specifier (parser);
8460 else
8461 type_spec = cp_parser_class_specifier (parser);
8462
8463 /* If that worked, we're done. */
8464 if (cp_parser_parse_definitely (parser))
8465 {
8466 if (declares_class_or_enum)
560ad596 8467 *declares_class_or_enum = 2;
a723baf1
MM
8468 return type_spec;
8469 }
8470
8471 /* Fall through. */
8472
8473 case RID_TYPENAME:
8474 /* Look for an elaborated-type-specifier. */
8475 type_spec = cp_parser_elaborated_type_specifier (parser,
8476 is_friend,
8477 is_declaration);
8478 /* We're declaring a class or enum -- unless we're using
8479 `typename'. */
8480 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8481 *declares_class_or_enum = 1;
a723baf1
MM
8482 return type_spec;
8483
8484 case RID_CONST:
8485 case RID_VOLATILE:
8486 case RID_RESTRICT:
8487 type_spec = cp_parser_cv_qualifier_opt (parser);
8488 /* Even though we call a routine that looks for an optional
8489 qualifier, we know that there should be one. */
8490 my_friendly_assert (type_spec != NULL, 20000328);
8491 /* This type-specifier was a cv-qualified. */
8492 if (is_cv_qualifier)
8493 *is_cv_qualifier = true;
8494
8495 return type_spec;
8496
8497 case RID_COMPLEX:
8498 /* The `__complex__' keyword is a GNU extension. */
8499 return cp_lexer_consume_token (parser->lexer)->value;
8500
8501 default:
8502 break;
8503 }
8504
8505 /* If we do not already have a type-specifier, assume we are looking
8506 at a simple-type-specifier. */
4b0d3cbe
MM
8507 type_spec = cp_parser_simple_type_specifier (parser, flags,
8508 /*identifier_p=*/true);
a723baf1
MM
8509
8510 /* If we didn't find a type-specifier, and a type-specifier was not
8511 optional in this context, issue an error message. */
8512 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8513 {
8514 cp_parser_error (parser, "expected type specifier");
8515 return error_mark_node;
8516 }
8517
8518 return type_spec;
8519}
8520
8521/* Parse a simple-type-specifier.
8522
8523 simple-type-specifier:
8524 :: [opt] nested-name-specifier [opt] type-name
8525 :: [opt] nested-name-specifier template template-id
8526 char
8527 wchar_t
8528 bool
8529 short
8530 int
8531 long
8532 signed
8533 unsigned
8534 float
8535 double
8536 void
8537
8538 GNU Extension:
8539
8540 simple-type-specifier:
8541 __typeof__ unary-expression
8542 __typeof__ ( type-id )
8543
8544 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8545 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8546 For the first two productions, and if IDENTIFIER_P is false, the
8547 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8548
8549static tree
4b0d3cbe
MM
8550cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8551 bool identifier_p)
a723baf1
MM
8552{
8553 tree type = NULL_TREE;
8554 cp_token *token;
8555
8556 /* Peek at the next token. */
8557 token = cp_lexer_peek_token (parser->lexer);
8558
8559 /* If we're looking at a keyword, things are easy. */
8560 switch (token->keyword)
8561 {
8562 case RID_CHAR:
4b0d3cbe
MM
8563 type = char_type_node;
8564 break;
a723baf1 8565 case RID_WCHAR:
4b0d3cbe
MM
8566 type = wchar_type_node;
8567 break;
a723baf1 8568 case RID_BOOL:
4b0d3cbe
MM
8569 type = boolean_type_node;
8570 break;
a723baf1 8571 case RID_SHORT:
4b0d3cbe
MM
8572 type = short_integer_type_node;
8573 break;
a723baf1 8574 case RID_INT:
4b0d3cbe
MM
8575 type = integer_type_node;
8576 break;
a723baf1 8577 case RID_LONG:
4b0d3cbe
MM
8578 type = long_integer_type_node;
8579 break;
a723baf1 8580 case RID_SIGNED:
4b0d3cbe
MM
8581 type = integer_type_node;
8582 break;
a723baf1 8583 case RID_UNSIGNED:
4b0d3cbe
MM
8584 type = unsigned_type_node;
8585 break;
a723baf1 8586 case RID_FLOAT:
4b0d3cbe
MM
8587 type = float_type_node;
8588 break;
a723baf1 8589 case RID_DOUBLE:
4b0d3cbe
MM
8590 type = double_type_node;
8591 break;
a723baf1 8592 case RID_VOID:
4b0d3cbe
MM
8593 type = void_type_node;
8594 break;
a723baf1
MM
8595
8596 case RID_TYPEOF:
8597 {
8598 tree operand;
8599
8600 /* Consume the `typeof' token. */
8601 cp_lexer_consume_token (parser->lexer);
04c06002 8602 /* Parse the operand to `typeof'. */
a723baf1
MM
8603 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8604 /* If it is not already a TYPE, take its type. */
8605 if (!TYPE_P (operand))
8606 operand = finish_typeof (operand);
8607
8608 return operand;
8609 }
8610
8611 default:
8612 break;
8613 }
8614
4b0d3cbe
MM
8615 /* If the type-specifier was for a built-in type, we're done. */
8616 if (type)
8617 {
8618 tree id;
8619
8620 /* Consume the token. */
8621 id = cp_lexer_consume_token (parser->lexer)->value;
8622 return identifier_p ? id : TYPE_NAME (type);
8623 }
8624
a723baf1
MM
8625 /* The type-specifier must be a user-defined type. */
8626 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8627 {
8628 /* Don't gobble tokens or issue error messages if this is an
8629 optional type-specifier. */
8630 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8631 cp_parser_parse_tentatively (parser);
8632
8633 /* Look for the optional `::' operator. */
8634 cp_parser_global_scope_opt (parser,
8635 /*current_scope_valid_p=*/false);
8636 /* Look for the nested-name specifier. */
8637 cp_parser_nested_name_specifier_opt (parser,
8638 /*typename_keyword_p=*/false,
8639 /*check_dependency_p=*/true,
a668c6ad
MM
8640 /*type_p=*/false,
8641 /*is_declaration=*/false);
a723baf1
MM
8642 /* If we have seen a nested-name-specifier, and the next token
8643 is `template', then we are using the template-id production. */
8644 if (parser->scope
8645 && cp_parser_optional_template_keyword (parser))
8646 {
8647 /* Look for the template-id. */
8648 type = cp_parser_template_id (parser,
8649 /*template_keyword_p=*/true,
a668c6ad
MM
8650 /*check_dependency_p=*/true,
8651 /*is_declaration=*/false);
a723baf1
MM
8652 /* If the template-id did not name a type, we are out of
8653 luck. */
8654 if (TREE_CODE (type) != TYPE_DECL)
8655 {
8656 cp_parser_error (parser, "expected template-id for type");
8657 type = NULL_TREE;
8658 }
8659 }
8660 /* Otherwise, look for a type-name. */
8661 else
4bb8ca28 8662 type = cp_parser_type_name (parser);
a723baf1
MM
8663 /* If it didn't work out, we don't have a TYPE. */
8664 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8665 && !cp_parser_parse_definitely (parser))
8666 type = NULL_TREE;
8667 }
8668
8669 /* If we didn't get a type-name, issue an error message. */
8670 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8671 {
8672 cp_parser_error (parser, "expected type-name");
8673 return error_mark_node;
8674 }
8675
a668c6ad
MM
8676 /* There is no valid C++ program where a non-template type is
8677 followed by a "<". That usually indicates that the user thought
8678 that the type was a template. */
4bb8ca28 8679 if (type && type != error_mark_node)
ee43dab5 8680 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 8681
a723baf1
MM
8682 return type;
8683}
8684
8685/* Parse a type-name.
8686
8687 type-name:
8688 class-name
8689 enum-name
8690 typedef-name
8691
8692 enum-name:
8693 identifier
8694
8695 typedef-name:
8696 identifier
8697
8698 Returns a TYPE_DECL for the the type. */
8699
8700static tree
94edc4ab 8701cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8702{
8703 tree type_decl;
8704 tree identifier;
8705
8706 /* We can't know yet whether it is a class-name or not. */
8707 cp_parser_parse_tentatively (parser);
8708 /* Try a class-name. */
8709 type_decl = cp_parser_class_name (parser,
8710 /*typename_keyword_p=*/false,
8711 /*template_keyword_p=*/false,
8712 /*type_p=*/false,
a723baf1 8713 /*check_dependency_p=*/true,
a668c6ad
MM
8714 /*class_head_p=*/false,
8715 /*is_declaration=*/false);
a723baf1
MM
8716 /* If it's not a class-name, keep looking. */
8717 if (!cp_parser_parse_definitely (parser))
8718 {
8719 /* It must be a typedef-name or an enum-name. */
8720 identifier = cp_parser_identifier (parser);
8721 if (identifier == error_mark_node)
8722 return error_mark_node;
8723
8724 /* Look up the type-name. */
8725 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8726 /* Issue an error if we did not find a type-name. */
8727 if (TREE_CODE (type_decl) != TYPE_DECL)
8728 {
4bb8ca28
MM
8729 if (!cp_parser_simulate_error (parser))
8730 cp_parser_name_lookup_error (parser, identifier, type_decl,
8731 "is not a type");
a723baf1
MM
8732 type_decl = error_mark_node;
8733 }
8734 /* Remember that the name was used in the definition of the
8735 current class so that we can check later to see if the
8736 meaning would have been different after the class was
8737 entirely defined. */
8738 else if (type_decl != error_mark_node
8739 && !parser->scope)
8740 maybe_note_name_used_in_class (identifier, type_decl);
8741 }
8742
8743 return type_decl;
8744}
8745
8746
8747/* Parse an elaborated-type-specifier. Note that the grammar given
8748 here incorporates the resolution to DR68.
8749
8750 elaborated-type-specifier:
8751 class-key :: [opt] nested-name-specifier [opt] identifier
8752 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8753 enum :: [opt] nested-name-specifier [opt] identifier
8754 typename :: [opt] nested-name-specifier identifier
8755 typename :: [opt] nested-name-specifier template [opt]
8756 template-id
8757
360d1b99
MM
8758 GNU extension:
8759
8760 elaborated-type-specifier:
8761 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8762 class-key attributes :: [opt] nested-name-specifier [opt]
8763 template [opt] template-id
8764 enum attributes :: [opt] nested-name-specifier [opt] identifier
8765
a723baf1
MM
8766 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8767 declared `friend'. If IS_DECLARATION is TRUE, then this
8768 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8769 something is being declared.
8770
8771 Returns the TYPE specified. */
8772
8773static tree
94edc4ab
NN
8774cp_parser_elaborated_type_specifier (cp_parser* parser,
8775 bool is_friend,
8776 bool is_declaration)
a723baf1
MM
8777{
8778 enum tag_types tag_type;
8779 tree identifier;
8780 tree type = NULL_TREE;
360d1b99 8781 tree attributes = NULL_TREE;
a723baf1
MM
8782
8783 /* See if we're looking at the `enum' keyword. */
8784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8785 {
8786 /* Consume the `enum' token. */
8787 cp_lexer_consume_token (parser->lexer);
8788 /* Remember that it's an enumeration type. */
8789 tag_type = enum_type;
360d1b99
MM
8790 /* Parse the attributes. */
8791 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
8792 }
8793 /* Or, it might be `typename'. */
8794 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8795 RID_TYPENAME))
8796 {
8797 /* Consume the `typename' token. */
8798 cp_lexer_consume_token (parser->lexer);
8799 /* Remember that it's a `typename' type. */
8800 tag_type = typename_type;
8801 /* The `typename' keyword is only allowed in templates. */
8802 if (!processing_template_decl)
8803 pedwarn ("using `typename' outside of template");
8804 }
8805 /* Otherwise it must be a class-key. */
8806 else
8807 {
8808 tag_type = cp_parser_class_key (parser);
8809 if (tag_type == none_type)
8810 return error_mark_node;
360d1b99
MM
8811 /* Parse the attributes. */
8812 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
8813 }
8814
8815 /* Look for the `::' operator. */
8816 cp_parser_global_scope_opt (parser,
8817 /*current_scope_valid_p=*/false);
8818 /* Look for the nested-name-specifier. */
8819 if (tag_type == typename_type)
8fa1ad0e
MM
8820 {
8821 if (cp_parser_nested_name_specifier (parser,
8822 /*typename_keyword_p=*/true,
8823 /*check_dependency_p=*/true,
a668c6ad
MM
8824 /*type_p=*/true,
8825 is_declaration)
8fa1ad0e
MM
8826 == error_mark_node)
8827 return error_mark_node;
8828 }
a723baf1
MM
8829 else
8830 /* Even though `typename' is not present, the proposed resolution
8831 to Core Issue 180 says that in `class A<T>::B', `B' should be
8832 considered a type-name, even if `A<T>' is dependent. */
8833 cp_parser_nested_name_specifier_opt (parser,
8834 /*typename_keyword_p=*/true,
8835 /*check_dependency_p=*/true,
a668c6ad
MM
8836 /*type_p=*/true,
8837 is_declaration);
a723baf1
MM
8838 /* For everything but enumeration types, consider a template-id. */
8839 if (tag_type != enum_type)
8840 {
8841 bool template_p = false;
8842 tree decl;
8843
8844 /* Allow the `template' keyword. */
8845 template_p = cp_parser_optional_template_keyword (parser);
8846 /* If we didn't see `template', we don't know if there's a
8847 template-id or not. */
8848 if (!template_p)
8849 cp_parser_parse_tentatively (parser);
8850 /* Parse the template-id. */
8851 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
8852 /*check_dependency_p=*/true,
8853 is_declaration);
a723baf1
MM
8854 /* If we didn't find a template-id, look for an ordinary
8855 identifier. */
8856 if (!template_p && !cp_parser_parse_definitely (parser))
8857 ;
8858 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8859 in effect, then we must assume that, upon instantiation, the
8860 template will correspond to a class. */
8861 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8862 && tag_type == typename_type)
8863 type = make_typename_type (parser->scope, decl,
8864 /*complain=*/1);
8865 else
8866 type = TREE_TYPE (decl);
8867 }
8868
8869 /* For an enumeration type, consider only a plain identifier. */
8870 if (!type)
8871 {
8872 identifier = cp_parser_identifier (parser);
8873
8874 if (identifier == error_mark_node)
eb5abb39
NS
8875 {
8876 parser->scope = NULL_TREE;
8877 return error_mark_node;
8878 }
a723baf1
MM
8879
8880 /* For a `typename', we needn't call xref_tag. */
8881 if (tag_type == typename_type)
8882 return make_typename_type (parser->scope, identifier,
8883 /*complain=*/1);
8884 /* Look up a qualified name in the usual way. */
8885 if (parser->scope)
8886 {
8887 tree decl;
8888
8889 /* In an elaborated-type-specifier, names are assumed to name
8890 types, so we set IS_TYPE to TRUE when calling
8891 cp_parser_lookup_name. */
8892 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8893 /*is_type=*/true,
eea9800f 8894 /*is_namespace=*/false,
a723baf1 8895 /*check_dependency=*/true);
710b73e6
KL
8896
8897 /* If we are parsing friend declaration, DECL may be a
8898 TEMPLATE_DECL tree node here. However, we need to check
8899 whether this TEMPLATE_DECL results in valid code. Consider
8900 the following example:
8901
8902 namespace N {
8903 template <class T> class C {};
8904 }
8905 class X {
8906 template <class T> friend class N::C; // #1, valid code
8907 };
8908 template <class T> class Y {
8909 friend class N::C; // #2, invalid code
8910 };
8911
8912 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8913 name lookup of `N::C'. We see that friend declaration must
8914 be template for the code to be valid. Note that
8915 processing_template_decl does not work here since it is
8916 always 1 for the above two cases. */
8917
a723baf1 8918 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8919 (decl, /*tag_name_p=*/is_friend
8920 && parser->num_template_parameter_lists));
a723baf1
MM
8921
8922 if (TREE_CODE (decl) != TYPE_DECL)
8923 {
8924 error ("expected type-name");
8925 return error_mark_node;
8926 }
560ad596
MM
8927
8928 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8929 check_elaborated_type_specifier
4b0d3cbe 8930 (tag_type, decl,
560ad596
MM
8931 (parser->num_template_parameter_lists
8932 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
8933
8934 type = TREE_TYPE (decl);
8935 }
8936 else
8937 {
8938 /* An elaborated-type-specifier sometimes introduces a new type and
8939 sometimes names an existing type. Normally, the rule is that it
8940 introduces a new type only if there is not an existing type of
8941 the same name already in scope. For example, given:
8942
8943 struct S {};
8944 void f() { struct S s; }
8945
8946 the `struct S' in the body of `f' is the same `struct S' as in
8947 the global scope; the existing definition is used. However, if
8948 there were no global declaration, this would introduce a new
8949 local class named `S'.
8950
8951 An exception to this rule applies to the following code:
8952
8953 namespace N { struct S; }
8954
8955 Here, the elaborated-type-specifier names a new type
8956 unconditionally; even if there is already an `S' in the
8957 containing scope this declaration names a new type.
8958 This exception only applies if the elaborated-type-specifier
8959 forms the complete declaration:
8960
8961 [class.name]
8962
8963 A declaration consisting solely of `class-key identifier ;' is
8964 either a redeclaration of the name in the current scope or a
8965 forward declaration of the identifier as a class name. It
8966 introduces the name into the current scope.
8967
8968 We are in this situation precisely when the next token is a `;'.
8969
8970 An exception to the exception is that a `friend' declaration does
8971 *not* name a new type; i.e., given:
8972
8973 struct S { friend struct T; };
8974
8975 `T' is not a new type in the scope of `S'.
8976
8977 Also, `new struct S' or `sizeof (struct S)' never results in the
8978 definition of a new type; a new type can only be declared in a
9bcb9aae 8979 declaration context. */
a723baf1
MM
8980
8981 type = xref_tag (tag_type, identifier,
360d1b99 8982 attributes,
a723baf1
MM
8983 (is_friend
8984 || !is_declaration
8985 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
8986 CPP_SEMICOLON)),
8987 parser->num_template_parameter_lists);
a723baf1
MM
8988 }
8989 }
8990 if (tag_type != enum_type)
8991 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
8992
8993 /* A "<" cannot follow an elaborated type specifier. If that
8994 happens, the user was probably trying to form a template-id. */
8995 cp_parser_check_for_invalid_template_id (parser, type);
8996
a723baf1
MM
8997 return type;
8998}
8999
9000/* Parse an enum-specifier.
9001
9002 enum-specifier:
9003 enum identifier [opt] { enumerator-list [opt] }
9004
9005 Returns an ENUM_TYPE representing the enumeration. */
9006
9007static tree
94edc4ab 9008cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9009{
9010 cp_token *token;
9011 tree identifier = NULL_TREE;
9012 tree type;
9013
9014 /* Look for the `enum' keyword. */
9015 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9016 return error_mark_node;
9017 /* Peek at the next token. */
9018 token = cp_lexer_peek_token (parser->lexer);
9019
9020 /* See if it is an identifier. */
9021 if (token->type == CPP_NAME)
9022 identifier = cp_parser_identifier (parser);
9023
9024 /* Look for the `{'. */
9025 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9026 return error_mark_node;
9027
9028 /* At this point, we're going ahead with the enum-specifier, even
9029 if some other problem occurs. */
9030 cp_parser_commit_to_tentative_parse (parser);
9031
9032 /* Issue an error message if type-definitions are forbidden here. */
9033 cp_parser_check_type_definition (parser);
9034
9035 /* Create the new type. */
9036 type = start_enum (identifier ? identifier : make_anon_name ());
9037
9038 /* Peek at the next token. */
9039 token = cp_lexer_peek_token (parser->lexer);
9040 /* If it's not a `}', then there are some enumerators. */
9041 if (token->type != CPP_CLOSE_BRACE)
9042 cp_parser_enumerator_list (parser, type);
9043 /* Look for the `}'. */
9044 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9045
9046 /* Finish up the enumeration. */
9047 finish_enum (type);
9048
9049 return type;
9050}
9051
9052/* Parse an enumerator-list. The enumerators all have the indicated
9053 TYPE.
9054
9055 enumerator-list:
9056 enumerator-definition
9057 enumerator-list , enumerator-definition */
9058
9059static void
94edc4ab 9060cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9061{
9062 while (true)
9063 {
9064 cp_token *token;
9065
9066 /* Parse an enumerator-definition. */
9067 cp_parser_enumerator_definition (parser, type);
9068 /* Peek at the next token. */
9069 token = cp_lexer_peek_token (parser->lexer);
9070 /* If it's not a `,', then we've reached the end of the
9071 list. */
9072 if (token->type != CPP_COMMA)
9073 break;
9074 /* Otherwise, consume the `,' and keep going. */
9075 cp_lexer_consume_token (parser->lexer);
9076 /* If the next token is a `}', there is a trailing comma. */
9077 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9078 {
9079 if (pedantic && !in_system_header)
9080 pedwarn ("comma at end of enumerator list");
9081 break;
9082 }
9083 }
9084}
9085
9086/* Parse an enumerator-definition. The enumerator has the indicated
9087 TYPE.
9088
9089 enumerator-definition:
9090 enumerator
9091 enumerator = constant-expression
9092
9093 enumerator:
9094 identifier */
9095
9096static void
94edc4ab 9097cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9098{
9099 cp_token *token;
9100 tree identifier;
9101 tree value;
9102
9103 /* Look for the identifier. */
9104 identifier = cp_parser_identifier (parser);
9105 if (identifier == error_mark_node)
9106 return;
9107
9108 /* Peek at the next token. */
9109 token = cp_lexer_peek_token (parser->lexer);
9110 /* If it's an `=', then there's an explicit value. */
9111 if (token->type == CPP_EQ)
9112 {
9113 /* Consume the `=' token. */
9114 cp_lexer_consume_token (parser->lexer);
9115 /* Parse the value. */
14d22dd6 9116 value = cp_parser_constant_expression (parser,
d17811fd 9117 /*allow_non_constant_p=*/false,
14d22dd6 9118 NULL);
a723baf1
MM
9119 }
9120 else
9121 value = NULL_TREE;
9122
9123 /* Create the enumerator. */
9124 build_enumerator (identifier, value, type);
9125}
9126
9127/* Parse a namespace-name.
9128
9129 namespace-name:
9130 original-namespace-name
9131 namespace-alias
9132
9133 Returns the NAMESPACE_DECL for the namespace. */
9134
9135static tree
94edc4ab 9136cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9137{
9138 tree identifier;
9139 tree namespace_decl;
9140
9141 /* Get the name of the namespace. */
9142 identifier = cp_parser_identifier (parser);
9143 if (identifier == error_mark_node)
9144 return error_mark_node;
9145
eea9800f
MM
9146 /* Look up the identifier in the currently active scope. Look only
9147 for namespaces, due to:
9148
9149 [basic.lookup.udir]
9150
9151 When looking up a namespace-name in a using-directive or alias
9152 definition, only namespace names are considered.
9153
9154 And:
9155
9156 [basic.lookup.qual]
9157
9158 During the lookup of a name preceding the :: scope resolution
9159 operator, object, function, and enumerator names are ignored.
9160
9161 (Note that cp_parser_class_or_namespace_name only calls this
9162 function if the token after the name is the scope resolution
9163 operator.) */
9164 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f
MM
9165 /*is_type=*/false,
9166 /*is_namespace=*/true,
9167 /*check_dependency=*/true);
a723baf1
MM
9168 /* If it's not a namespace, issue an error. */
9169 if (namespace_decl == error_mark_node
9170 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9171 {
9172 cp_parser_error (parser, "expected namespace-name");
9173 namespace_decl = error_mark_node;
9174 }
9175
9176 return namespace_decl;
9177}
9178
9179/* Parse a namespace-definition.
9180
9181 namespace-definition:
9182 named-namespace-definition
9183 unnamed-namespace-definition
9184
9185 named-namespace-definition:
9186 original-namespace-definition
9187 extension-namespace-definition
9188
9189 original-namespace-definition:
9190 namespace identifier { namespace-body }
9191
9192 extension-namespace-definition:
9193 namespace original-namespace-name { namespace-body }
9194
9195 unnamed-namespace-definition:
9196 namespace { namespace-body } */
9197
9198static void
94edc4ab 9199cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9200{
9201 tree identifier;
9202
9203 /* Look for the `namespace' keyword. */
9204 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9205
9206 /* Get the name of the namespace. We do not attempt to distinguish
9207 between an original-namespace-definition and an
9208 extension-namespace-definition at this point. The semantic
9209 analysis routines are responsible for that. */
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9211 identifier = cp_parser_identifier (parser);
9212 else
9213 identifier = NULL_TREE;
9214
9215 /* Look for the `{' to start the namespace. */
9216 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9217 /* Start the namespace. */
9218 push_namespace (identifier);
9219 /* Parse the body of the namespace. */
9220 cp_parser_namespace_body (parser);
9221 /* Finish the namespace. */
9222 pop_namespace ();
9223 /* Look for the final `}'. */
9224 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9225}
9226
9227/* Parse a namespace-body.
9228
9229 namespace-body:
9230 declaration-seq [opt] */
9231
9232static void
94edc4ab 9233cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9234{
9235 cp_parser_declaration_seq_opt (parser);
9236}
9237
9238/* Parse a namespace-alias-definition.
9239
9240 namespace-alias-definition:
9241 namespace identifier = qualified-namespace-specifier ; */
9242
9243static void
94edc4ab 9244cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9245{
9246 tree identifier;
9247 tree namespace_specifier;
9248
9249 /* Look for the `namespace' keyword. */
9250 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9251 /* Look for the identifier. */
9252 identifier = cp_parser_identifier (parser);
9253 if (identifier == error_mark_node)
9254 return;
9255 /* Look for the `=' token. */
9256 cp_parser_require (parser, CPP_EQ, "`='");
9257 /* Look for the qualified-namespace-specifier. */
9258 namespace_specifier
9259 = cp_parser_qualified_namespace_specifier (parser);
9260 /* Look for the `;' token. */
9261 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9262
9263 /* Register the alias in the symbol table. */
9264 do_namespace_alias (identifier, namespace_specifier);
9265}
9266
9267/* Parse a qualified-namespace-specifier.
9268
9269 qualified-namespace-specifier:
9270 :: [opt] nested-name-specifier [opt] namespace-name
9271
9272 Returns a NAMESPACE_DECL corresponding to the specified
9273 namespace. */
9274
9275static tree
94edc4ab 9276cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9277{
9278 /* Look for the optional `::'. */
9279 cp_parser_global_scope_opt (parser,
9280 /*current_scope_valid_p=*/false);
9281
9282 /* Look for the optional nested-name-specifier. */
9283 cp_parser_nested_name_specifier_opt (parser,
9284 /*typename_keyword_p=*/false,
9285 /*check_dependency_p=*/true,
a668c6ad
MM
9286 /*type_p=*/false,
9287 /*is_declaration=*/true);
a723baf1
MM
9288
9289 return cp_parser_namespace_name (parser);
9290}
9291
9292/* Parse a using-declaration.
9293
9294 using-declaration:
9295 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9296 using :: unqualified-id ; */
9297
9298static void
94edc4ab 9299cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9300{
9301 cp_token *token;
9302 bool typename_p = false;
9303 bool global_scope_p;
9304 tree decl;
9305 tree identifier;
9306 tree scope;
9307
9308 /* Look for the `using' keyword. */
9309 cp_parser_require_keyword (parser, RID_USING, "`using'");
9310
9311 /* Peek at the next token. */
9312 token = cp_lexer_peek_token (parser->lexer);
9313 /* See if it's `typename'. */
9314 if (token->keyword == RID_TYPENAME)
9315 {
9316 /* Remember that we've seen it. */
9317 typename_p = true;
9318 /* Consume the `typename' token. */
9319 cp_lexer_consume_token (parser->lexer);
9320 }
9321
9322 /* Look for the optional global scope qualification. */
9323 global_scope_p
9324 = (cp_parser_global_scope_opt (parser,
9325 /*current_scope_valid_p=*/false)
9326 != NULL_TREE);
9327
9328 /* If we saw `typename', or didn't see `::', then there must be a
9329 nested-name-specifier present. */
9330 if (typename_p || !global_scope_p)
9331 cp_parser_nested_name_specifier (parser, typename_p,
9332 /*check_dependency_p=*/true,
a668c6ad
MM
9333 /*type_p=*/false,
9334 /*is_declaration=*/true);
a723baf1
MM
9335 /* Otherwise, we could be in either of the two productions. In that
9336 case, treat the nested-name-specifier as optional. */
9337 else
9338 cp_parser_nested_name_specifier_opt (parser,
9339 /*typename_keyword_p=*/false,
9340 /*check_dependency_p=*/true,
a668c6ad
MM
9341 /*type_p=*/false,
9342 /*is_declaration=*/true);
a723baf1
MM
9343
9344 /* Parse the unqualified-id. */
9345 identifier = cp_parser_unqualified_id (parser,
9346 /*template_keyword_p=*/false,
f3c2dfc6
MM
9347 /*check_dependency_p=*/true,
9348 /*declarator_p=*/true);
a723baf1
MM
9349
9350 /* The function we call to handle a using-declaration is different
9351 depending on what scope we are in. */
f3c2dfc6
MM
9352 if (identifier == error_mark_node)
9353 ;
9354 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9355 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9356 /* [namespace.udecl]
9357
9358 A using declaration shall not name a template-id. */
9359 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9360 else
9361 {
f3c2dfc6
MM
9362 scope = current_scope ();
9363 if (scope && TYPE_P (scope))
4eb6d609 9364 {
f3c2dfc6
MM
9365 /* Create the USING_DECL. */
9366 decl = do_class_using_decl (build_nt (SCOPE_REF,
9367 parser->scope,
9368 identifier));
9369 /* Add it to the list of members in this class. */
9370 finish_member_declaration (decl);
4eb6d609 9371 }
a723baf1 9372 else
f3c2dfc6
MM
9373 {
9374 decl = cp_parser_lookup_name_simple (parser, identifier);
9375 if (decl == error_mark_node)
4bb8ca28 9376 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6
MM
9377 else if (scope)
9378 do_local_using_decl (decl);
9379 else
9380 do_toplevel_using_decl (decl);
9381 }
a723baf1
MM
9382 }
9383
9384 /* Look for the final `;'. */
9385 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9386}
9387
9388/* Parse a using-directive.
9389
9390 using-directive:
9391 using namespace :: [opt] nested-name-specifier [opt]
9392 namespace-name ; */
9393
9394static void
94edc4ab 9395cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9396{
9397 tree namespace_decl;
86098eb8 9398 tree attribs;
a723baf1
MM
9399
9400 /* Look for the `using' keyword. */
9401 cp_parser_require_keyword (parser, RID_USING, "`using'");
9402 /* And the `namespace' keyword. */
9403 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9404 /* Look for the optional `::' operator. */
9405 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9406 /* And the optional nested-name-specifier. */
a723baf1
MM
9407 cp_parser_nested_name_specifier_opt (parser,
9408 /*typename_keyword_p=*/false,
9409 /*check_dependency_p=*/true,
a668c6ad
MM
9410 /*type_p=*/false,
9411 /*is_declaration=*/true);
a723baf1
MM
9412 /* Get the namespace being used. */
9413 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
9414 /* And any specified attributes. */
9415 attribs = cp_parser_attributes_opt (parser);
a723baf1 9416 /* Update the symbol table. */
86098eb8 9417 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
9418 /* Look for the final `;'. */
9419 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9420}
9421
9422/* Parse an asm-definition.
9423
9424 asm-definition:
9425 asm ( string-literal ) ;
9426
9427 GNU Extension:
9428
9429 asm-definition:
9430 asm volatile [opt] ( string-literal ) ;
9431 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9432 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9433 : asm-operand-list [opt] ) ;
9434 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9435 : asm-operand-list [opt]
9436 : asm-operand-list [opt] ) ; */
9437
9438static void
94edc4ab 9439cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9440{
9441 cp_token *token;
9442 tree string;
9443 tree outputs = NULL_TREE;
9444 tree inputs = NULL_TREE;
9445 tree clobbers = NULL_TREE;
9446 tree asm_stmt;
9447 bool volatile_p = false;
9448 bool extended_p = false;
9449
9450 /* Look for the `asm' keyword. */
9451 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9452 /* See if the next token is `volatile'. */
9453 if (cp_parser_allow_gnu_extensions_p (parser)
9454 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9455 {
9456 /* Remember that we saw the `volatile' keyword. */
9457 volatile_p = true;
9458 /* Consume the token. */
9459 cp_lexer_consume_token (parser->lexer);
9460 }
9461 /* Look for the opening `('. */
9462 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9463 /* Look for the string. */
9464 token = cp_parser_require (parser, CPP_STRING, "asm body");
9465 if (!token)
9466 return;
9467 string = token->value;
9468 /* If we're allowing GNU extensions, check for the extended assembly
9469 syntax. Unfortunately, the `:' tokens need not be separated by
9470 a space in C, and so, for compatibility, we tolerate that here
9471 too. Doing that means that we have to treat the `::' operator as
9472 two `:' tokens. */
9473 if (cp_parser_allow_gnu_extensions_p (parser)
9474 && at_function_scope_p ()
9475 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9476 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9477 {
9478 bool inputs_p = false;
9479 bool clobbers_p = false;
9480
9481 /* The extended syntax was used. */
9482 extended_p = true;
9483
9484 /* Look for outputs. */
9485 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9486 {
9487 /* Consume the `:'. */
9488 cp_lexer_consume_token (parser->lexer);
9489 /* Parse the output-operands. */
9490 if (cp_lexer_next_token_is_not (parser->lexer,
9491 CPP_COLON)
9492 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9493 CPP_SCOPE)
9494 && cp_lexer_next_token_is_not (parser->lexer,
9495 CPP_CLOSE_PAREN))
a723baf1
MM
9496 outputs = cp_parser_asm_operand_list (parser);
9497 }
9498 /* If the next token is `::', there are no outputs, and the
9499 next token is the beginning of the inputs. */
9500 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9501 {
9502 /* Consume the `::' token. */
9503 cp_lexer_consume_token (parser->lexer);
9504 /* The inputs are coming next. */
9505 inputs_p = true;
9506 }
9507
9508 /* Look for inputs. */
9509 if (inputs_p
9510 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9511 {
9512 if (!inputs_p)
9513 /* Consume the `:'. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* Parse the output-operands. */
9516 if (cp_lexer_next_token_is_not (parser->lexer,
9517 CPP_COLON)
9518 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9519 CPP_SCOPE)
9520 && cp_lexer_next_token_is_not (parser->lexer,
9521 CPP_CLOSE_PAREN))
a723baf1
MM
9522 inputs = cp_parser_asm_operand_list (parser);
9523 }
9524 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9525 /* The clobbers are coming next. */
9526 clobbers_p = true;
9527
9528 /* Look for clobbers. */
9529 if (clobbers_p
9530 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9531 {
9532 if (!clobbers_p)
9533 /* Consume the `:'. */
9534 cp_lexer_consume_token (parser->lexer);
9535 /* Parse the clobbers. */
8caf4c38
MM
9536 if (cp_lexer_next_token_is_not (parser->lexer,
9537 CPP_CLOSE_PAREN))
9538 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9539 }
9540 }
9541 /* Look for the closing `)'. */
9542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
9543 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9544 /*consume_paren=*/true);
a723baf1
MM
9545 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9546
9547 /* Create the ASM_STMT. */
9548 if (at_function_scope_p ())
9549 {
9550 asm_stmt =
9551 finish_asm_stmt (volatile_p
9552 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9553 string, outputs, inputs, clobbers);
9554 /* If the extended syntax was not used, mark the ASM_STMT. */
9555 if (!extended_p)
9556 ASM_INPUT_P (asm_stmt) = 1;
9557 }
9558 else
9559 assemble_asm (string);
9560}
9561
9562/* Declarators [gram.dcl.decl] */
9563
9564/* Parse an init-declarator.
9565
9566 init-declarator:
9567 declarator initializer [opt]
9568
9569 GNU Extension:
9570
9571 init-declarator:
9572 declarator asm-specification [opt] attributes [opt] initializer [opt]
9573
4bb8ca28
MM
9574 function-definition:
9575 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9576 function-body
9577 decl-specifier-seq [opt] declarator function-try-block
9578
9579 GNU Extension:
9580
9581 function-definition:
9582 __extension__ function-definition
9583
a723baf1 9584 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9585 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9586 then this declarator appears in a class scope. The new DECL created
9587 by this declarator is returned.
a723baf1
MM
9588
9589 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9590 for a function-definition here as well. If the declarator is a
9591 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9592 be TRUE upon return. By that point, the function-definition will
9593 have been completely parsed.
9594
9595 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9596 is FALSE. */
9597
9598static tree
94edc4ab
NN
9599cp_parser_init_declarator (cp_parser* parser,
9600 tree decl_specifiers,
9601 tree prefix_attributes,
9602 bool function_definition_allowed_p,
9603 bool member_p,
560ad596 9604 int declares_class_or_enum,
94edc4ab 9605 bool* function_definition_p)
a723baf1
MM
9606{
9607 cp_token *token;
9608 tree declarator;
9609 tree attributes;
9610 tree asm_specification;
9611 tree initializer;
9612 tree decl = NULL_TREE;
9613 tree scope;
a723baf1
MM
9614 bool is_initialized;
9615 bool is_parenthesized_init;
39703eb9 9616 bool is_non_constant_init;
7efa3e22 9617 int ctor_dtor_or_conv_p;
a723baf1
MM
9618 bool friend_p;
9619
9620 /* Assume that this is not the declarator for a function
9621 definition. */
9622 if (function_definition_p)
9623 *function_definition_p = false;
9624
9625 /* Defer access checks while parsing the declarator; we cannot know
9626 what names are accessible until we know what is being
9627 declared. */
cf22909c
KL
9628 resume_deferring_access_checks ();
9629
a723baf1
MM
9630 /* Parse the declarator. */
9631 declarator
62b8a44e 9632 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9633 &ctor_dtor_or_conv_p,
9634 /*parenthesized_p=*/NULL);
a723baf1 9635 /* Gather up the deferred checks. */
cf22909c 9636 stop_deferring_access_checks ();
24c0ef37 9637
a723baf1
MM
9638 /* If the DECLARATOR was erroneous, there's no need to go
9639 further. */
9640 if (declarator == error_mark_node)
cf22909c 9641 return error_mark_node;
a723baf1 9642
560ad596
MM
9643 cp_parser_check_for_definition_in_return_type (declarator,
9644 declares_class_or_enum);
9645
a723baf1
MM
9646 /* Figure out what scope the entity declared by the DECLARATOR is
9647 located in. `grokdeclarator' sometimes changes the scope, so
9648 we compute it now. */
9649 scope = get_scope_of_declarator (declarator);
9650
9651 /* If we're allowing GNU extensions, look for an asm-specification
9652 and attributes. */
9653 if (cp_parser_allow_gnu_extensions_p (parser))
9654 {
9655 /* Look for an asm-specification. */
9656 asm_specification = cp_parser_asm_specification_opt (parser);
9657 /* And attributes. */
9658 attributes = cp_parser_attributes_opt (parser);
9659 }
9660 else
9661 {
9662 asm_specification = NULL_TREE;
9663 attributes = NULL_TREE;
9664 }
9665
9666 /* Peek at the next token. */
9667 token = cp_lexer_peek_token (parser->lexer);
9668 /* Check to see if the token indicates the start of a
9669 function-definition. */
9670 if (cp_parser_token_starts_function_definition_p (token))
9671 {
9672 if (!function_definition_allowed_p)
9673 {
9674 /* If a function-definition should not appear here, issue an
9675 error message. */
9676 cp_parser_error (parser,
9677 "a function-definition is not allowed here");
9678 return error_mark_node;
9679 }
9680 else
9681 {
a723baf1
MM
9682 /* Neither attributes nor an asm-specification are allowed
9683 on a function-definition. */
9684 if (asm_specification)
9685 error ("an asm-specification is not allowed on a function-definition");
9686 if (attributes)
9687 error ("attributes are not allowed on a function-definition");
9688 /* This is a function-definition. */
9689 *function_definition_p = true;
9690
a723baf1 9691 /* Parse the function definition. */
4bb8ca28
MM
9692 if (member_p)
9693 decl = cp_parser_save_member_function_body (parser,
9694 decl_specifiers,
9695 declarator,
9696 prefix_attributes);
9697 else
9698 decl
9699 = (cp_parser_function_definition_from_specifiers_and_declarator
9700 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9701
a723baf1
MM
9702 return decl;
9703 }
9704 }
9705
9706 /* [dcl.dcl]
9707
9708 Only in function declarations for constructors, destructors, and
9709 type conversions can the decl-specifier-seq be omitted.
9710
9711 We explicitly postpone this check past the point where we handle
9712 function-definitions because we tolerate function-definitions
9713 that are missing their return types in some modes. */
7efa3e22 9714 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9715 {
9716 cp_parser_error (parser,
9717 "expected constructor, destructor, or type conversion");
9718 return error_mark_node;
9719 }
9720
9721 /* An `=' or an `(' indicates an initializer. */
9722 is_initialized = (token->type == CPP_EQ
9723 || token->type == CPP_OPEN_PAREN);
9724 /* If the init-declarator isn't initialized and isn't followed by a
9725 `,' or `;', it's not a valid init-declarator. */
9726 if (!is_initialized
9727 && token->type != CPP_COMMA
9728 && token->type != CPP_SEMICOLON)
9729 {
9730 cp_parser_error (parser, "expected init-declarator");
9731 return error_mark_node;
9732 }
9733
9734 /* Because start_decl has side-effects, we should only call it if we
9735 know we're going ahead. By this point, we know that we cannot
9736 possibly be looking at any other construct. */
9737 cp_parser_commit_to_tentative_parse (parser);
9738
9739 /* Check to see whether or not this declaration is a friend. */
9740 friend_p = cp_parser_friend_p (decl_specifiers);
9741
9742 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 9743 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 9744 return error_mark_node;
a723baf1
MM
9745
9746 /* Enter the newly declared entry in the symbol table. If we're
9747 processing a declaration in a class-specifier, we wait until
9748 after processing the initializer. */
9749 if (!member_p)
9750 {
9751 if (parser->in_unbraced_linkage_specification_p)
9752 {
9753 decl_specifiers = tree_cons (error_mark_node,
9754 get_identifier ("extern"),
9755 decl_specifiers);
9756 have_extern_spec = false;
9757 }
ee3071ef
NS
9758 decl = start_decl (declarator, decl_specifiers,
9759 is_initialized, attributes, prefix_attributes);
a723baf1
MM
9760 }
9761
9762 /* Enter the SCOPE. That way unqualified names appearing in the
9763 initializer will be looked up in SCOPE. */
9764 if (scope)
9765 push_scope (scope);
9766
9767 /* Perform deferred access control checks, now that we know in which
9768 SCOPE the declared entity resides. */
9769 if (!member_p && decl)
9770 {
9771 tree saved_current_function_decl = NULL_TREE;
9772
9773 /* If the entity being declared is a function, pretend that we
9774 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9775 things that would not otherwise be accessible. */
a723baf1
MM
9776 if (TREE_CODE (decl) == FUNCTION_DECL)
9777 {
9778 saved_current_function_decl = current_function_decl;
9779 current_function_decl = decl;
9780 }
9781
cf22909c
KL
9782 /* Perform the access control checks for the declarator and the
9783 the decl-specifiers. */
9784 perform_deferred_access_checks ();
a723baf1
MM
9785
9786 /* Restore the saved value. */
9787 if (TREE_CODE (decl) == FUNCTION_DECL)
9788 current_function_decl = saved_current_function_decl;
9789 }
9790
9791 /* Parse the initializer. */
9792 if (is_initialized)
39703eb9
MM
9793 initializer = cp_parser_initializer (parser,
9794 &is_parenthesized_init,
9795 &is_non_constant_init);
a723baf1
MM
9796 else
9797 {
9798 initializer = NULL_TREE;
9799 is_parenthesized_init = false;
39703eb9 9800 is_non_constant_init = true;
a723baf1
MM
9801 }
9802
9803 /* The old parser allows attributes to appear after a parenthesized
9804 initializer. Mark Mitchell proposed removing this functionality
9805 on the GCC mailing lists on 2002-08-13. This parser accepts the
9806 attributes -- but ignores them. */
9807 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9808 if (cp_parser_attributes_opt (parser))
9809 warning ("attributes after parenthesized initializer ignored");
9810
9811 /* Leave the SCOPE, now that we have processed the initializer. It
9812 is important to do this before calling cp_finish_decl because it
9813 makes decisions about whether to create DECL_STMTs or not based
9814 on the current scope. */
9815 if (scope)
9816 pop_scope (scope);
9817
9818 /* For an in-class declaration, use `grokfield' to create the
9819 declaration. */
9820 if (member_p)
8db1028e
NS
9821 {
9822 decl = grokfield (declarator, decl_specifiers,
9823 initializer, /*asmspec=*/NULL_TREE,
a723baf1 9824 /*attributes=*/NULL_TREE);
8db1028e
NS
9825 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9826 cp_parser_save_default_args (parser, decl);
9827 }
9828
a723baf1
MM
9829 /* Finish processing the declaration. But, skip friend
9830 declarations. */
9831 if (!friend_p && decl)
9832 cp_finish_decl (decl,
9833 initializer,
9834 asm_specification,
9835 /* If the initializer is in parentheses, then this is
9836 a direct-initialization, which means that an
9837 `explicit' constructor is OK. Otherwise, an
9838 `explicit' constructor cannot be used. */
9839 ((is_parenthesized_init || !is_initialized)
9840 ? 0 : LOOKUP_ONLYCONVERTING));
9841
39703eb9
MM
9842 /* Remember whether or not variables were initialized by
9843 constant-expressions. */
9844 if (decl && TREE_CODE (decl) == VAR_DECL
9845 && is_initialized && !is_non_constant_init)
9846 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9847
a723baf1
MM
9848 return decl;
9849}
9850
9851/* Parse a declarator.
9852
9853 declarator:
9854 direct-declarator
9855 ptr-operator declarator
9856
9857 abstract-declarator:
9858 ptr-operator abstract-declarator [opt]
9859 direct-abstract-declarator
9860
9861 GNU Extensions:
9862
9863 declarator:
9864 attributes [opt] direct-declarator
9865 attributes [opt] ptr-operator declarator
9866
9867 abstract-declarator:
9868 attributes [opt] ptr-operator abstract-declarator [opt]
9869 attributes [opt] direct-abstract-declarator
9870
9871 Returns a representation of the declarator. If the declarator has
9872 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 9873 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
9874 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9875 used. The first operand is the TYPE for `X'. The second operand
9876 is an INDIRECT_REF whose operand is the sub-declarator.
9877
34cd5ae7 9878 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
9879
9880 (It would be better to define a structure type to represent
9881 declarators, rather than abusing `tree' nodes to represent
9882 declarators. That would be much clearer and save some memory.
9883 There is no reason for declarators to be garbage-collected, for
9884 example; they are created during parser and no longer needed after
9885 `grokdeclarator' has been called.)
9886
9887 For a ptr-operator that has the optional cv-qualifier-seq,
9888 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9889 node.
9890
7efa3e22
NS
9891 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9892 detect constructor, destructor or conversion operators. It is set
9893 to -1 if the declarator is a name, and +1 if it is a
9894 function. Otherwise it is set to zero. Usually you just want to
9895 test for >0, but internally the negative value is used.
9896
a723baf1
MM
9897 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9898 a decl-specifier-seq unless it declares a constructor, destructor,
9899 or conversion. It might seem that we could check this condition in
9900 semantic analysis, rather than parsing, but that makes it difficult
9901 to handle something like `f()'. We want to notice that there are
9902 no decl-specifiers, and therefore realize that this is an
4bb8ca28
MM
9903 expression, not a declaration.)
9904
9905 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9906 the declarator is a direct-declarator of the form "(...)". */
a723baf1
MM
9907
9908static tree
94edc4ab
NN
9909cp_parser_declarator (cp_parser* parser,
9910 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
9911 int* ctor_dtor_or_conv_p,
9912 bool* parenthesized_p)
a723baf1
MM
9913{
9914 cp_token *token;
9915 tree declarator;
9916 enum tree_code code;
9917 tree cv_qualifier_seq;
9918 tree class_type;
9919 tree attributes = NULL_TREE;
9920
9921 /* Assume this is not a constructor, destructor, or type-conversion
9922 operator. */
9923 if (ctor_dtor_or_conv_p)
7efa3e22 9924 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
9925
9926 if (cp_parser_allow_gnu_extensions_p (parser))
9927 attributes = cp_parser_attributes_opt (parser);
9928
9929 /* Peek at the next token. */
9930 token = cp_lexer_peek_token (parser->lexer);
9931
9932 /* Check for the ptr-operator production. */
9933 cp_parser_parse_tentatively (parser);
9934 /* Parse the ptr-operator. */
9935 code = cp_parser_ptr_operator (parser,
9936 &class_type,
9937 &cv_qualifier_seq);
9938 /* If that worked, then we have a ptr-operator. */
9939 if (cp_parser_parse_definitely (parser))
9940 {
4bb8ca28
MM
9941 /* If a ptr-operator was found, then this declarator was not
9942 parenthesized. */
9943 if (parenthesized_p)
9944 *parenthesized_p = true;
a723baf1
MM
9945 /* The dependent declarator is optional if we are parsing an
9946 abstract-declarator. */
62b8a44e 9947 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9948 cp_parser_parse_tentatively (parser);
9949
9950 /* Parse the dependent declarator. */
62b8a44e 9951 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
9952 /*ctor_dtor_or_conv_p=*/NULL,
9953 /*parenthesized_p=*/NULL);
a723baf1
MM
9954
9955 /* If we are parsing an abstract-declarator, we must handle the
9956 case where the dependent declarator is absent. */
62b8a44e
NS
9957 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9958 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9959 declarator = NULL_TREE;
9960
9961 /* Build the representation of the ptr-operator. */
9962 if (code == INDIRECT_REF)
9963 declarator = make_pointer_declarator (cv_qualifier_seq,
9964 declarator);
9965 else
9966 declarator = make_reference_declarator (cv_qualifier_seq,
9967 declarator);
9968 /* Handle the pointer-to-member case. */
9969 if (class_type)
9970 declarator = build_nt (SCOPE_REF, class_type, declarator);
9971 }
9972 /* Everything else is a direct-declarator. */
9973 else
4bb8ca28
MM
9974 {
9975 if (parenthesized_p)
9976 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
9977 CPP_OPEN_PAREN);
9978 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9979 ctor_dtor_or_conv_p);
9980 }
a723baf1
MM
9981
9982 if (attributes && declarator != error_mark_node)
9983 declarator = tree_cons (attributes, declarator, NULL_TREE);
9984
9985 return declarator;
9986}
9987
9988/* Parse a direct-declarator or direct-abstract-declarator.
9989
9990 direct-declarator:
9991 declarator-id
9992 direct-declarator ( parameter-declaration-clause )
9993 cv-qualifier-seq [opt]
9994 exception-specification [opt]
9995 direct-declarator [ constant-expression [opt] ]
9996 ( declarator )
9997
9998 direct-abstract-declarator:
9999 direct-abstract-declarator [opt]
10000 ( parameter-declaration-clause )
10001 cv-qualifier-seq [opt]
10002 exception-specification [opt]
10003 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10004 ( abstract-declarator )
10005
62b8a44e
NS
10006 Returns a representation of the declarator. DCL_KIND is
10007 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10008 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10009 we are parsing a direct-declarator. It is
10010 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10011 of ambiguity we prefer an abstract declarator, as per
10012 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10013 cp_parser_declarator.
10014
10015 For the declarator-id production, the representation is as for an
10016 id-expression, except that a qualified name is represented as a
10017 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10018 see the documentation of the FUNCTION_DECLARATOR_* macros for
10019 information about how to find the various declarator components.
10020 An array-declarator is represented as an ARRAY_REF. The
10021 direct-declarator is the first operand; the constant-expression
10022 indicating the size of the array is the second operand. */
10023
10024static tree
94edc4ab
NN
10025cp_parser_direct_declarator (cp_parser* parser,
10026 cp_parser_declarator_kind dcl_kind,
7efa3e22 10027 int* ctor_dtor_or_conv_p)
a723baf1
MM
10028{
10029 cp_token *token;
62b8a44e 10030 tree declarator = NULL_TREE;
a723baf1
MM
10031 tree scope = NULL_TREE;
10032 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10033 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
10034 bool first = true;
10035
10036 while (true)
a723baf1 10037 {
62b8a44e
NS
10038 /* Peek at the next token. */
10039 token = cp_lexer_peek_token (parser->lexer);
10040 if (token->type == CPP_OPEN_PAREN)
a723baf1 10041 {
62b8a44e
NS
10042 /* This is either a parameter-declaration-clause, or a
10043 parenthesized declarator. When we know we are parsing a
34cd5ae7 10044 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10045 if FIRST is true. For instance, `(int)' is a
10046 parameter-declaration-clause, with an omitted
10047 direct-abstract-declarator. But `((*))', is a
10048 parenthesized abstract declarator. Finally, when T is a
10049 template parameter `(T)' is a
34cd5ae7 10050 parameter-declaration-clause, and not a parenthesized
62b8a44e 10051 named declarator.
a723baf1 10052
62b8a44e
NS
10053 We first try and parse a parameter-declaration-clause,
10054 and then try a nested declarator (if FIRST is true).
a723baf1 10055
62b8a44e
NS
10056 It is not an error for it not to be a
10057 parameter-declaration-clause, even when FIRST is
10058 false. Consider,
10059
10060 int i (int);
10061 int i (3);
10062
10063 The first is the declaration of a function while the
10064 second is a the definition of a variable, including its
10065 initializer.
10066
10067 Having seen only the parenthesis, we cannot know which of
10068 these two alternatives should be selected. Even more
10069 complex are examples like:
10070
10071 int i (int (a));
10072 int i (int (3));
10073
10074 The former is a function-declaration; the latter is a
10075 variable initialization.
10076
34cd5ae7 10077 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10078 that fails, we back out and return. */
10079
10080 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10081 {
62b8a44e 10082 tree params;
4047b164 10083 unsigned saved_num_template_parameter_lists;
62b8a44e
NS
10084
10085 cp_parser_parse_tentatively (parser);
a723baf1 10086
62b8a44e
NS
10087 /* Consume the `('. */
10088 cp_lexer_consume_token (parser->lexer);
10089 if (first)
10090 {
10091 /* If this is going to be an abstract declarator, we're
10092 in a declarator and we can't have default args. */
10093 parser->default_arg_ok_p = false;
10094 parser->in_declarator_p = true;
10095 }
10096
4047b164
KL
10097 /* Inside the function parameter list, surrounding
10098 template-parameter-lists do not apply. */
10099 saved_num_template_parameter_lists
10100 = parser->num_template_parameter_lists;
10101 parser->num_template_parameter_lists = 0;
10102
62b8a44e
NS
10103 /* Parse the parameter-declaration-clause. */
10104 params = cp_parser_parameter_declaration_clause (parser);
10105
4047b164
KL
10106 parser->num_template_parameter_lists
10107 = saved_num_template_parameter_lists;
10108
62b8a44e 10109 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10110 exception-specification. */
62b8a44e
NS
10111 if (cp_parser_parse_definitely (parser))
10112 {
10113 tree cv_qualifiers;
10114 tree exception_specification;
7efa3e22
NS
10115
10116 if (ctor_dtor_or_conv_p)
10117 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10118 first = false;
10119 /* Consume the `)'. */
10120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10121
10122 /* Parse the cv-qualifier-seq. */
10123 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10124 /* And the exception-specification. */
10125 exception_specification
10126 = cp_parser_exception_specification_opt (parser);
10127
10128 /* Create the function-declarator. */
10129 declarator = make_call_declarator (declarator,
10130 params,
10131 cv_qualifiers,
10132 exception_specification);
10133 /* Any subsequent parameter lists are to do with
10134 return type, so are not those of the declared
10135 function. */
10136 parser->default_arg_ok_p = false;
10137
10138 /* Repeat the main loop. */
10139 continue;
10140 }
10141 }
10142
10143 /* If this is the first, we can try a parenthesized
10144 declarator. */
10145 if (first)
a723baf1 10146 {
a723baf1 10147 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10148 parser->in_declarator_p = saved_in_declarator_p;
10149
10150 /* Consume the `('. */
10151 cp_lexer_consume_token (parser->lexer);
10152 /* Parse the nested declarator. */
10153 declarator
4bb8ca28
MM
10154 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10155 /*parenthesized_p=*/NULL);
62b8a44e
NS
10156 first = false;
10157 /* Expect a `)'. */
10158 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10159 declarator = error_mark_node;
10160 if (declarator == error_mark_node)
10161 break;
10162
10163 goto handle_declarator;
a723baf1 10164 }
9bcb9aae 10165 /* Otherwise, we must be done. */
62b8a44e
NS
10166 else
10167 break;
a723baf1 10168 }
62b8a44e
NS
10169 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10170 && token->type == CPP_OPEN_SQUARE)
a723baf1 10171 {
62b8a44e 10172 /* Parse an array-declarator. */
a723baf1
MM
10173 tree bounds;
10174
7efa3e22
NS
10175 if (ctor_dtor_or_conv_p)
10176 *ctor_dtor_or_conv_p = 0;
10177
62b8a44e
NS
10178 first = false;
10179 parser->default_arg_ok_p = false;
10180 parser->in_declarator_p = true;
a723baf1
MM
10181 /* Consume the `['. */
10182 cp_lexer_consume_token (parser->lexer);
10183 /* Peek at the next token. */
10184 token = cp_lexer_peek_token (parser->lexer);
10185 /* If the next token is `]', then there is no
10186 constant-expression. */
10187 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10188 {
10189 bool non_constant_p;
10190
10191 bounds
10192 = cp_parser_constant_expression (parser,
10193 /*allow_non_constant=*/true,
10194 &non_constant_p);
d17811fd
MM
10195 if (!non_constant_p)
10196 bounds = cp_parser_fold_non_dependent_expr (bounds);
14d22dd6 10197 }
a723baf1
MM
10198 else
10199 bounds = NULL_TREE;
10200 /* Look for the closing `]'. */
62b8a44e
NS
10201 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10202 {
10203 declarator = error_mark_node;
10204 break;
10205 }
a723baf1
MM
10206
10207 declarator = build_nt (ARRAY_REF, declarator, bounds);
10208 }
62b8a44e 10209 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10210 {
a668c6ad 10211 /* Parse a declarator-id */
62b8a44e
NS
10212 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10213 cp_parser_parse_tentatively (parser);
10214 declarator = cp_parser_declarator_id (parser);
712becab
NS
10215 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10216 {
10217 if (!cp_parser_parse_definitely (parser))
10218 declarator = error_mark_node;
10219 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10220 {
10221 cp_parser_error (parser, "expected unqualified-id");
10222 declarator = error_mark_node;
10223 }
10224 }
10225
62b8a44e
NS
10226 if (declarator == error_mark_node)
10227 break;
a723baf1 10228
d9a50301
KL
10229 if (TREE_CODE (declarator) == SCOPE_REF
10230 && !current_scope ())
62b8a44e
NS
10231 {
10232 tree scope = TREE_OPERAND (declarator, 0);
712becab 10233
62b8a44e
NS
10234 /* In the declaration of a member of a template class
10235 outside of the class itself, the SCOPE will sometimes
10236 be a TYPENAME_TYPE. For example, given:
10237
10238 template <typename T>
10239 int S<T>::R::i = 3;
10240
10241 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10242 this context, we must resolve S<T>::R to an ordinary
10243 type, rather than a typename type.
10244
10245 The reason we normally avoid resolving TYPENAME_TYPEs
10246 is that a specialization of `S' might render
10247 `S<T>::R' not a type. However, if `S' is
10248 specialized, then this `i' will not be used, so there
10249 is no harm in resolving the types here. */
10250 if (TREE_CODE (scope) == TYPENAME_TYPE)
10251 {
14d22dd6
MM
10252 tree type;
10253
62b8a44e 10254 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10255 type = resolve_typename_type (scope,
10256 /*only_current_p=*/false);
62b8a44e 10257 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10258 if (type != error_mark_node)
10259 scope = type;
62b8a44e
NS
10260 /* Build a new DECLARATOR. */
10261 declarator = build_nt (SCOPE_REF,
10262 scope,
10263 TREE_OPERAND (declarator, 1));
10264 }
10265 }
10266
10267 /* Check to see whether the declarator-id names a constructor,
10268 destructor, or conversion. */
10269 if (declarator && ctor_dtor_or_conv_p
10270 && ((TREE_CODE (declarator) == SCOPE_REF
10271 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10272 || (TREE_CODE (declarator) != SCOPE_REF
10273 && at_class_scope_p ())))
a723baf1 10274 {
62b8a44e
NS
10275 tree unqualified_name;
10276 tree class_type;
10277
10278 /* Get the unqualified part of the name. */
10279 if (TREE_CODE (declarator) == SCOPE_REF)
10280 {
10281 class_type = TREE_OPERAND (declarator, 0);
10282 unqualified_name = TREE_OPERAND (declarator, 1);
10283 }
10284 else
10285 {
10286 class_type = current_class_type;
10287 unqualified_name = declarator;
10288 }
10289
10290 /* See if it names ctor, dtor or conv. */
10291 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10292 || IDENTIFIER_TYPENAME_P (unqualified_name)
10293 || constructor_name_p (unqualified_name, class_type))
7efa3e22 10294 *ctor_dtor_or_conv_p = -1;
a723baf1 10295 }
62b8a44e
NS
10296
10297 handle_declarator:;
10298 scope = get_scope_of_declarator (declarator);
10299 if (scope)
10300 /* Any names that appear after the declarator-id for a member
10301 are looked up in the containing scope. */
10302 push_scope (scope);
10303 parser->in_declarator_p = true;
10304 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10305 || (declarator
10306 && (TREE_CODE (declarator) == SCOPE_REF
10307 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10308 /* Default args are only allowed on function
10309 declarations. */
10310 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10311 else
62b8a44e
NS
10312 parser->default_arg_ok_p = false;
10313
10314 first = false;
a723baf1 10315 }
62b8a44e 10316 /* We're done. */
a723baf1
MM
10317 else
10318 break;
a723baf1
MM
10319 }
10320
10321 /* For an abstract declarator, we might wind up with nothing at this
10322 point. That's an error; the declarator is not optional. */
10323 if (!declarator)
10324 cp_parser_error (parser, "expected declarator");
10325
10326 /* If we entered a scope, we must exit it now. */
10327 if (scope)
10328 pop_scope (scope);
10329
10330 parser->default_arg_ok_p = saved_default_arg_ok_p;
10331 parser->in_declarator_p = saved_in_declarator_p;
10332
10333 return declarator;
10334}
10335
10336/* Parse a ptr-operator.
10337
10338 ptr-operator:
10339 * cv-qualifier-seq [opt]
10340 &
10341 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10342
10343 GNU Extension:
10344
10345 ptr-operator:
10346 & cv-qualifier-seq [opt]
10347
10348 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10349 used. Returns ADDR_EXPR if a reference was used. In the
10350 case of a pointer-to-member, *TYPE is filled in with the
10351 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10352 with the cv-qualifier-seq, or NULL_TREE, if there are no
10353 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10354
10355static enum tree_code
94edc4ab
NN
10356cp_parser_ptr_operator (cp_parser* parser,
10357 tree* type,
10358 tree* cv_qualifier_seq)
a723baf1
MM
10359{
10360 enum tree_code code = ERROR_MARK;
10361 cp_token *token;
10362
10363 /* Assume that it's not a pointer-to-member. */
10364 *type = NULL_TREE;
10365 /* And that there are no cv-qualifiers. */
10366 *cv_qualifier_seq = NULL_TREE;
10367
10368 /* Peek at the next token. */
10369 token = cp_lexer_peek_token (parser->lexer);
10370 /* If it's a `*' or `&' we have a pointer or reference. */
10371 if (token->type == CPP_MULT || token->type == CPP_AND)
10372 {
10373 /* Remember which ptr-operator we were processing. */
10374 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10375
10376 /* Consume the `*' or `&'. */
10377 cp_lexer_consume_token (parser->lexer);
10378
10379 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10380 `&', if we are allowing GNU extensions. (The only qualifier
10381 that can legally appear after `&' is `restrict', but that is
10382 enforced during semantic analysis. */
10383 if (code == INDIRECT_REF
10384 || cp_parser_allow_gnu_extensions_p (parser))
10385 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10386 }
10387 else
10388 {
10389 /* Try the pointer-to-member case. */
10390 cp_parser_parse_tentatively (parser);
10391 /* Look for the optional `::' operator. */
10392 cp_parser_global_scope_opt (parser,
10393 /*current_scope_valid_p=*/false);
10394 /* Look for the nested-name specifier. */
10395 cp_parser_nested_name_specifier (parser,
10396 /*typename_keyword_p=*/false,
10397 /*check_dependency_p=*/true,
a668c6ad
MM
10398 /*type_p=*/false,
10399 /*is_declaration=*/false);
a723baf1
MM
10400 /* If we found it, and the next token is a `*', then we are
10401 indeed looking at a pointer-to-member operator. */
10402 if (!cp_parser_error_occurred (parser)
10403 && cp_parser_require (parser, CPP_MULT, "`*'"))
10404 {
10405 /* The type of which the member is a member is given by the
10406 current SCOPE. */
10407 *type = parser->scope;
10408 /* The next name will not be qualified. */
10409 parser->scope = NULL_TREE;
10410 parser->qualifying_scope = NULL_TREE;
10411 parser->object_scope = NULL_TREE;
10412 /* Indicate that the `*' operator was used. */
10413 code = INDIRECT_REF;
10414 /* Look for the optional cv-qualifier-seq. */
10415 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10416 }
10417 /* If that didn't work we don't have a ptr-operator. */
10418 if (!cp_parser_parse_definitely (parser))
10419 cp_parser_error (parser, "expected ptr-operator");
10420 }
10421
10422 return code;
10423}
10424
10425/* Parse an (optional) cv-qualifier-seq.
10426
10427 cv-qualifier-seq:
10428 cv-qualifier cv-qualifier-seq [opt]
10429
10430 Returns a TREE_LIST. The TREE_VALUE of each node is the
10431 representation of a cv-qualifier. */
10432
10433static tree
94edc4ab 10434cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10435{
10436 tree cv_qualifiers = NULL_TREE;
10437
10438 while (true)
10439 {
10440 tree cv_qualifier;
10441
10442 /* Look for the next cv-qualifier. */
10443 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10444 /* If we didn't find one, we're done. */
10445 if (!cv_qualifier)
10446 break;
10447
10448 /* Add this cv-qualifier to the list. */
10449 cv_qualifiers
10450 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10451 }
10452
10453 /* We built up the list in reverse order. */
10454 return nreverse (cv_qualifiers);
10455}
10456
10457/* Parse an (optional) cv-qualifier.
10458
10459 cv-qualifier:
10460 const
10461 volatile
10462
10463 GNU Extension:
10464
10465 cv-qualifier:
10466 __restrict__ */
10467
10468static tree
94edc4ab 10469cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10470{
10471 cp_token *token;
10472 tree cv_qualifier = NULL_TREE;
10473
10474 /* Peek at the next token. */
10475 token = cp_lexer_peek_token (parser->lexer);
10476 /* See if it's a cv-qualifier. */
10477 switch (token->keyword)
10478 {
10479 case RID_CONST:
10480 case RID_VOLATILE:
10481 case RID_RESTRICT:
10482 /* Save the value of the token. */
10483 cv_qualifier = token->value;
10484 /* Consume the token. */
10485 cp_lexer_consume_token (parser->lexer);
10486 break;
10487
10488 default:
10489 break;
10490 }
10491
10492 return cv_qualifier;
10493}
10494
10495/* Parse a declarator-id.
10496
10497 declarator-id:
10498 id-expression
10499 :: [opt] nested-name-specifier [opt] type-name
10500
10501 In the `id-expression' case, the value returned is as for
10502 cp_parser_id_expression if the id-expression was an unqualified-id.
10503 If the id-expression was a qualified-id, then a SCOPE_REF is
10504 returned. The first operand is the scope (either a NAMESPACE_DECL
10505 or TREE_TYPE), but the second is still just a representation of an
10506 unqualified-id. */
10507
10508static tree
94edc4ab 10509cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10510{
10511 tree id_expression;
10512
10513 /* The expression must be an id-expression. Assume that qualified
10514 names are the names of types so that:
10515
10516 template <class T>
10517 int S<T>::R::i = 3;
10518
10519 will work; we must treat `S<T>::R' as the name of a type.
10520 Similarly, assume that qualified names are templates, where
10521 required, so that:
10522
10523 template <class T>
10524 int S<T>::R<T>::i = 3;
10525
10526 will work, too. */
10527 id_expression = cp_parser_id_expression (parser,
10528 /*template_keyword_p=*/false,
10529 /*check_dependency_p=*/false,
f3c2dfc6
MM
10530 /*template_p=*/NULL,
10531 /*declarator_p=*/true);
a723baf1
MM
10532 /* If the name was qualified, create a SCOPE_REF to represent
10533 that. */
10534 if (parser->scope)
ec20aa6c
MM
10535 {
10536 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10537 parser->scope = NULL_TREE;
10538 }
a723baf1
MM
10539
10540 return id_expression;
10541}
10542
10543/* Parse a type-id.
10544
10545 type-id:
10546 type-specifier-seq abstract-declarator [opt]
10547
10548 Returns the TYPE specified. */
10549
10550static tree
94edc4ab 10551cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10552{
10553 tree type_specifier_seq;
10554 tree abstract_declarator;
10555
10556 /* Parse the type-specifier-seq. */
10557 type_specifier_seq
10558 = cp_parser_type_specifier_seq (parser);
10559 if (type_specifier_seq == error_mark_node)
10560 return error_mark_node;
10561
10562 /* There might or might not be an abstract declarator. */
10563 cp_parser_parse_tentatively (parser);
10564 /* Look for the declarator. */
10565 abstract_declarator
4bb8ca28
MM
10566 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10567 /*parenthesized_p=*/NULL);
a723baf1
MM
10568 /* Check to see if there really was a declarator. */
10569 if (!cp_parser_parse_definitely (parser))
10570 abstract_declarator = NULL_TREE;
10571
10572 return groktypename (build_tree_list (type_specifier_seq,
10573 abstract_declarator));
10574}
10575
10576/* Parse a type-specifier-seq.
10577
10578 type-specifier-seq:
10579 type-specifier type-specifier-seq [opt]
10580
10581 GNU extension:
10582
10583 type-specifier-seq:
10584 attributes type-specifier-seq [opt]
10585
10586 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10587 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10588
10589static tree
94edc4ab 10590cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10591{
10592 bool seen_type_specifier = false;
10593 tree type_specifier_seq = NULL_TREE;
10594
10595 /* Parse the type-specifiers and attributes. */
10596 while (true)
10597 {
10598 tree type_specifier;
10599
10600 /* Check for attributes first. */
10601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10602 {
10603 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10604 NULL_TREE,
10605 type_specifier_seq);
10606 continue;
10607 }
10608
10609 /* After the first type-specifier, others are optional. */
10610 if (seen_type_specifier)
10611 cp_parser_parse_tentatively (parser);
10612 /* Look for the type-specifier. */
10613 type_specifier = cp_parser_type_specifier (parser,
10614 CP_PARSER_FLAGS_NONE,
10615 /*is_friend=*/false,
10616 /*is_declaration=*/false,
10617 NULL,
10618 NULL);
10619 /* If the first type-specifier could not be found, this is not a
10620 type-specifier-seq at all. */
10621 if (!seen_type_specifier && type_specifier == error_mark_node)
10622 return error_mark_node;
10623 /* If subsequent type-specifiers could not be found, the
10624 type-specifier-seq is complete. */
10625 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10626 break;
10627
10628 /* Add the new type-specifier to the list. */
10629 type_specifier_seq
10630 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10631 seen_type_specifier = true;
10632 }
10633
10634 /* We built up the list in reverse order. */
10635 return nreverse (type_specifier_seq);
10636}
10637
10638/* Parse a parameter-declaration-clause.
10639
10640 parameter-declaration-clause:
10641 parameter-declaration-list [opt] ... [opt]
10642 parameter-declaration-list , ...
10643
10644 Returns a representation for the parameter declarations. Each node
10645 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10646 representation.) If the parameter-declaration-clause ends with an
10647 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10648 list. A return value of NULL_TREE indicates a
10649 parameter-declaration-clause consisting only of an ellipsis. */
10650
10651static tree
94edc4ab 10652cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10653{
10654 tree parameters;
10655 cp_token *token;
10656 bool ellipsis_p;
10657
10658 /* Peek at the next token. */
10659 token = cp_lexer_peek_token (parser->lexer);
10660 /* Check for trivial parameter-declaration-clauses. */
10661 if (token->type == CPP_ELLIPSIS)
10662 {
10663 /* Consume the `...' token. */
10664 cp_lexer_consume_token (parser->lexer);
10665 return NULL_TREE;
10666 }
10667 else if (token->type == CPP_CLOSE_PAREN)
10668 /* There are no parameters. */
c73aecdf
DE
10669 {
10670#ifndef NO_IMPLICIT_EXTERN_C
10671 if (in_system_header && current_class_type == NULL
10672 && current_lang_name == lang_name_c)
10673 return NULL_TREE;
10674 else
10675#endif
10676 return void_list_node;
10677 }
a723baf1
MM
10678 /* Check for `(void)', too, which is a special case. */
10679 else if (token->keyword == RID_VOID
10680 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10681 == CPP_CLOSE_PAREN))
10682 {
10683 /* Consume the `void' token. */
10684 cp_lexer_consume_token (parser->lexer);
10685 /* There are no parameters. */
10686 return void_list_node;
10687 }
10688
10689 /* Parse the parameter-declaration-list. */
10690 parameters = cp_parser_parameter_declaration_list (parser);
10691 /* If a parse error occurred while parsing the
10692 parameter-declaration-list, then the entire
10693 parameter-declaration-clause is erroneous. */
10694 if (parameters == error_mark_node)
10695 return error_mark_node;
10696
10697 /* Peek at the next token. */
10698 token = cp_lexer_peek_token (parser->lexer);
10699 /* If it's a `,', the clause should terminate with an ellipsis. */
10700 if (token->type == CPP_COMMA)
10701 {
10702 /* Consume the `,'. */
10703 cp_lexer_consume_token (parser->lexer);
10704 /* Expect an ellipsis. */
10705 ellipsis_p
10706 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10707 }
10708 /* It might also be `...' if the optional trailing `,' was
10709 omitted. */
10710 else if (token->type == CPP_ELLIPSIS)
10711 {
10712 /* Consume the `...' token. */
10713 cp_lexer_consume_token (parser->lexer);
10714 /* And remember that we saw it. */
10715 ellipsis_p = true;
10716 }
10717 else
10718 ellipsis_p = false;
10719
10720 /* Finish the parameter list. */
10721 return finish_parmlist (parameters, ellipsis_p);
10722}
10723
10724/* Parse a parameter-declaration-list.
10725
10726 parameter-declaration-list:
10727 parameter-declaration
10728 parameter-declaration-list , parameter-declaration
10729
10730 Returns a representation of the parameter-declaration-list, as for
10731 cp_parser_parameter_declaration_clause. However, the
10732 `void_list_node' is never appended to the list. */
10733
10734static tree
94edc4ab 10735cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10736{
10737 tree parameters = NULL_TREE;
10738
10739 /* Look for more parameters. */
10740 while (true)
10741 {
10742 tree parameter;
4bb8ca28 10743 bool parenthesized_p;
a723baf1
MM
10744 /* Parse the parameter. */
10745 parameter
4bb8ca28
MM
10746 = cp_parser_parameter_declaration (parser,
10747 /*template_parm_p=*/false,
10748 &parenthesized_p);
ec194454 10749
34cd5ae7 10750 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10751 then the entire parameter-declaration-list is erroneous. */
10752 if (parameter == error_mark_node)
10753 {
10754 parameters = error_mark_node;
10755 break;
10756 }
10757 /* Add the new parameter to the list. */
10758 TREE_CHAIN (parameter) = parameters;
10759 parameters = parameter;
10760
10761 /* Peek at the next token. */
10762 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10763 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10764 /* The parameter-declaration-list is complete. */
10765 break;
10766 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10767 {
10768 cp_token *token;
10769
10770 /* Peek at the next token. */
10771 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10772 /* If it's an ellipsis, then the list is complete. */
10773 if (token->type == CPP_ELLIPSIS)
10774 break;
10775 /* Otherwise, there must be more parameters. Consume the
10776 `,'. */
10777 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
10778 /* When parsing something like:
10779
10780 int i(float f, double d)
10781
10782 we can tell after seeing the declaration for "f" that we
10783 are not looking at an initialization of a variable "i",
10784 but rather at the declaration of a function "i".
10785
10786 Due to the fact that the parsing of template arguments
10787 (as specified to a template-id) requires backtracking we
10788 cannot use this technique when inside a template argument
10789 list. */
10790 if (!parser->in_template_argument_list_p
10791 && cp_parser_parsing_tentatively (parser)
10792 && !cp_parser_committed_to_tentative_parse (parser)
10793 /* However, a parameter-declaration of the form
10794 "foat(f)" (which is a valid declaration of a
10795 parameter "f") can also be interpreted as an
10796 expression (the conversion of "f" to "float"). */
10797 && !parenthesized_p)
10798 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
10799 }
10800 else
10801 {
10802 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
10803 if (!cp_parser_parsing_tentatively (parser)
10804 || cp_parser_committed_to_tentative_parse (parser))
10805 cp_parser_skip_to_closing_parenthesis (parser,
10806 /*recovering=*/true,
10807 /*or_comma=*/true,
10808 /*consume_paren=*/false);
a723baf1
MM
10809 break;
10810 }
10811 }
10812
10813 /* We built up the list in reverse order; straighten it out now. */
10814 return nreverse (parameters);
10815}
10816
10817/* Parse a parameter declaration.
10818
10819 parameter-declaration:
10820 decl-specifier-seq declarator
10821 decl-specifier-seq declarator = assignment-expression
10822 decl-specifier-seq abstract-declarator [opt]
10823 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10824
ec194454
MM
10825 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10826 declares a template parameter. (In that case, a non-nested `>'
10827 token encountered during the parsing of the assignment-expression
10828 is not interpreted as a greater-than operator.)
a723baf1
MM
10829
10830 Returns a TREE_LIST representing the parameter-declaration. The
4bb8ca28
MM
10831 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10832 there is no default argument. The TREE_VALUE is a representation
10833 of the decl-specifier-seq and declarator. In particular, the
10834 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10835 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10836 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10837 the declarator is of the form "(p)". */
a723baf1
MM
10838
10839static tree
ec194454 10840cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
10841 bool template_parm_p,
10842 bool *parenthesized_p)
a723baf1 10843{
560ad596 10844 int declares_class_or_enum;
ec194454 10845 bool greater_than_is_operator_p;
a723baf1
MM
10846 tree decl_specifiers;
10847 tree attributes;
10848 tree declarator;
10849 tree default_argument;
10850 tree parameter;
10851 cp_token *token;
10852 const char *saved_message;
10853
ec194454
MM
10854 /* In a template parameter, `>' is not an operator.
10855
10856 [temp.param]
10857
10858 When parsing a default template-argument for a non-type
10859 template-parameter, the first non-nested `>' is taken as the end
10860 of the template parameter-list rather than a greater-than
10861 operator. */
10862 greater_than_is_operator_p = !template_parm_p;
10863
a723baf1
MM
10864 /* Type definitions may not appear in parameter types. */
10865 saved_message = parser->type_definition_forbidden_message;
10866 parser->type_definition_forbidden_message
10867 = "types may not be defined in parameter types";
10868
10869 /* Parse the declaration-specifiers. */
10870 decl_specifiers
10871 = cp_parser_decl_specifier_seq (parser,
10872 CP_PARSER_FLAGS_NONE,
10873 &attributes,
10874 &declares_class_or_enum);
10875 /* If an error occurred, there's no reason to attempt to parse the
10876 rest of the declaration. */
10877 if (cp_parser_error_occurred (parser))
10878 {
10879 parser->type_definition_forbidden_message = saved_message;
10880 return error_mark_node;
10881 }
10882
10883 /* Peek at the next token. */
10884 token = cp_lexer_peek_token (parser->lexer);
10885 /* If the next token is a `)', `,', `=', `>', or `...', then there
10886 is no declarator. */
10887 if (token->type == CPP_CLOSE_PAREN
10888 || token->type == CPP_COMMA
10889 || token->type == CPP_EQ
10890 || token->type == CPP_ELLIPSIS
10891 || token->type == CPP_GREATER)
4bb8ca28
MM
10892 {
10893 declarator = NULL_TREE;
10894 if (parenthesized_p)
10895 *parenthesized_p = false;
10896 }
a723baf1
MM
10897 /* Otherwise, there should be a declarator. */
10898 else
10899 {
10900 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10901 parser->default_arg_ok_p = false;
10902
a723baf1 10903 declarator = cp_parser_declarator (parser,
62b8a44e 10904 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
10905 /*ctor_dtor_or_conv_p=*/NULL,
10906 parenthesized_p);
a723baf1 10907 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10908 /* After the declarator, allow more attributes. */
10909 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10910 }
10911
62b8a44e 10912 /* The restriction on defining new types applies only to the type
a723baf1
MM
10913 of the parameter, not to the default argument. */
10914 parser->type_definition_forbidden_message = saved_message;
10915
10916 /* If the next token is `=', then process a default argument. */
10917 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10918 {
10919 bool saved_greater_than_is_operator_p;
10920 /* Consume the `='. */
10921 cp_lexer_consume_token (parser->lexer);
10922
10923 /* If we are defining a class, then the tokens that make up the
10924 default argument must be saved and processed later. */
ec194454
MM
10925 if (!template_parm_p && at_class_scope_p ()
10926 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10927 {
10928 unsigned depth = 0;
10929
10930 /* Create a DEFAULT_ARG to represented the unparsed default
10931 argument. */
10932 default_argument = make_node (DEFAULT_ARG);
10933 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10934
10935 /* Add tokens until we have processed the entire default
10936 argument. */
10937 while (true)
10938 {
10939 bool done = false;
10940 cp_token *token;
10941
10942 /* Peek at the next token. */
10943 token = cp_lexer_peek_token (parser->lexer);
10944 /* What we do depends on what token we have. */
10945 switch (token->type)
10946 {
10947 /* In valid code, a default argument must be
10948 immediately followed by a `,' `)', or `...'. */
10949 case CPP_COMMA:
10950 case CPP_CLOSE_PAREN:
10951 case CPP_ELLIPSIS:
10952 /* If we run into a non-nested `;', `}', or `]',
10953 then the code is invalid -- but the default
10954 argument is certainly over. */
10955 case CPP_SEMICOLON:
10956 case CPP_CLOSE_BRACE:
10957 case CPP_CLOSE_SQUARE:
10958 if (depth == 0)
10959 done = true;
10960 /* Update DEPTH, if necessary. */
10961 else if (token->type == CPP_CLOSE_PAREN
10962 || token->type == CPP_CLOSE_BRACE
10963 || token->type == CPP_CLOSE_SQUARE)
10964 --depth;
10965 break;
10966
10967 case CPP_OPEN_PAREN:
10968 case CPP_OPEN_SQUARE:
10969 case CPP_OPEN_BRACE:
10970 ++depth;
10971 break;
10972
10973 case CPP_GREATER:
10974 /* If we see a non-nested `>', and `>' is not an
10975 operator, then it marks the end of the default
10976 argument. */
10977 if (!depth && !greater_than_is_operator_p)
10978 done = true;
10979 break;
10980
10981 /* If we run out of tokens, issue an error message. */
10982 case CPP_EOF:
10983 error ("file ends in default argument");
10984 done = true;
10985 break;
10986
10987 case CPP_NAME:
10988 case CPP_SCOPE:
10989 /* In these cases, we should look for template-ids.
10990 For example, if the default argument is
10991 `X<int, double>()', we need to do name lookup to
10992 figure out whether or not `X' is a template; if
34cd5ae7 10993 so, the `,' does not end the default argument.
a723baf1
MM
10994
10995 That is not yet done. */
10996 break;
10997
10998 default:
10999 break;
11000 }
11001
11002 /* If we've reached the end, stop. */
11003 if (done)
11004 break;
11005
11006 /* Add the token to the token block. */
11007 token = cp_lexer_consume_token (parser->lexer);
11008 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11009 token);
11010 }
11011 }
11012 /* Outside of a class definition, we can just parse the
11013 assignment-expression. */
11014 else
11015 {
11016 bool saved_local_variables_forbidden_p;
11017
11018 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11019 set correctly. */
11020 saved_greater_than_is_operator_p
11021 = parser->greater_than_is_operator_p;
11022 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11023 /* Local variable names (and the `this' keyword) may not
11024 appear in a default argument. */
11025 saved_local_variables_forbidden_p
11026 = parser->local_variables_forbidden_p;
11027 parser->local_variables_forbidden_p = true;
11028 /* Parse the assignment-expression. */
11029 default_argument = cp_parser_assignment_expression (parser);
11030 /* Restore saved state. */
11031 parser->greater_than_is_operator_p
11032 = saved_greater_than_is_operator_p;
11033 parser->local_variables_forbidden_p
11034 = saved_local_variables_forbidden_p;
11035 }
11036 if (!parser->default_arg_ok_p)
11037 {
c67d36d0
NS
11038 if (!flag_pedantic_errors)
11039 warning ("deprecated use of default argument for parameter of non-function");
11040 else
11041 {
11042 error ("default arguments are only permitted for function parameters");
11043 default_argument = NULL_TREE;
11044 }
a723baf1
MM
11045 }
11046 }
11047 else
11048 default_argument = NULL_TREE;
11049
11050 /* Create the representation of the parameter. */
11051 if (attributes)
11052 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11053 parameter = build_tree_list (default_argument,
11054 build_tree_list (decl_specifiers,
11055 declarator));
11056
11057 return parameter;
11058}
11059
a723baf1
MM
11060/* Parse a function-body.
11061
11062 function-body:
11063 compound_statement */
11064
11065static void
11066cp_parser_function_body (cp_parser *parser)
11067{
a5bcc582 11068 cp_parser_compound_statement (parser, false);
a723baf1
MM
11069}
11070
11071/* Parse a ctor-initializer-opt followed by a function-body. Return
11072 true if a ctor-initializer was present. */
11073
11074static bool
11075cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11076{
11077 tree body;
11078 bool ctor_initializer_p;
11079
11080 /* Begin the function body. */
11081 body = begin_function_body ();
11082 /* Parse the optional ctor-initializer. */
11083 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11084 /* Parse the function-body. */
11085 cp_parser_function_body (parser);
11086 /* Finish the function body. */
11087 finish_function_body (body);
11088
11089 return ctor_initializer_p;
11090}
11091
11092/* Parse an initializer.
11093
11094 initializer:
11095 = initializer-clause
11096 ( expression-list )
11097
11098 Returns a expression representing the initializer. If no
11099 initializer is present, NULL_TREE is returned.
11100
11101 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11102 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11103 set to FALSE if there is no initializer present. If there is an
11104 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11105 is set to true; otherwise it is set to false. */
a723baf1
MM
11106
11107static tree
39703eb9
MM
11108cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11109 bool* non_constant_p)
a723baf1
MM
11110{
11111 cp_token *token;
11112 tree init;
11113
11114 /* Peek at the next token. */
11115 token = cp_lexer_peek_token (parser->lexer);
11116
11117 /* Let our caller know whether or not this initializer was
11118 parenthesized. */
11119 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11120 /* Assume that the initializer is constant. */
11121 *non_constant_p = false;
a723baf1
MM
11122
11123 if (token->type == CPP_EQ)
11124 {
11125 /* Consume the `='. */
11126 cp_lexer_consume_token (parser->lexer);
11127 /* Parse the initializer-clause. */
39703eb9 11128 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11129 }
11130 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11131 init = cp_parser_parenthesized_expression_list (parser, false,
11132 non_constant_p);
a723baf1
MM
11133 else
11134 {
11135 /* Anything else is an error. */
11136 cp_parser_error (parser, "expected initializer");
11137 init = error_mark_node;
11138 }
11139
11140 return init;
11141}
11142
11143/* Parse an initializer-clause.
11144
11145 initializer-clause:
11146 assignment-expression
11147 { initializer-list , [opt] }
11148 { }
11149
11150 Returns an expression representing the initializer.
11151
11152 If the `assignment-expression' production is used the value
34cd5ae7 11153 returned is simply a representation for the expression.
a723baf1
MM
11154
11155 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11156 the elements of the initializer-list (or NULL_TREE, if the last
11157 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11158 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11159 trailing `,' was provided. NON_CONSTANT_P is as for
11160 cp_parser_initializer. */
a723baf1
MM
11161
11162static tree
39703eb9 11163cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11164{
11165 tree initializer;
11166
11167 /* If it is not a `{', then we are looking at an
11168 assignment-expression. */
11169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
11170 initializer
11171 = cp_parser_constant_expression (parser,
11172 /*allow_non_constant_p=*/true,
11173 non_constant_p);
a723baf1
MM
11174 else
11175 {
11176 /* Consume the `{' token. */
11177 cp_lexer_consume_token (parser->lexer);
11178 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11179 initializer = make_node (CONSTRUCTOR);
11180 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11181 necessary, but check_initializer depends upon it, for
11182 now. */
11183 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11184 /* If it's not a `}', then there is a non-trivial initializer. */
11185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11186 {
11187 /* Parse the initializer list. */
11188 CONSTRUCTOR_ELTS (initializer)
39703eb9 11189 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
11190 /* A trailing `,' token is allowed. */
11191 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11192 cp_lexer_consume_token (parser->lexer);
11193 }
a723baf1
MM
11194 /* Now, there should be a trailing `}'. */
11195 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11196 }
11197
11198 return initializer;
11199}
11200
11201/* Parse an initializer-list.
11202
11203 initializer-list:
11204 initializer-clause
11205 initializer-list , initializer-clause
11206
11207 GNU Extension:
11208
11209 initializer-list:
11210 identifier : initializer-clause
11211 initializer-list, identifier : initializer-clause
11212
11213 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11214 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
11215 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11216 as for cp_parser_initializer. */
a723baf1
MM
11217
11218static tree
39703eb9 11219cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11220{
11221 tree initializers = NULL_TREE;
11222
39703eb9
MM
11223 /* Assume all of the expressions are constant. */
11224 *non_constant_p = false;
11225
a723baf1
MM
11226 /* Parse the rest of the list. */
11227 while (true)
11228 {
11229 cp_token *token;
11230 tree identifier;
11231 tree initializer;
39703eb9
MM
11232 bool clause_non_constant_p;
11233
a723baf1
MM
11234 /* If the next token is an identifier and the following one is a
11235 colon, we are looking at the GNU designated-initializer
11236 syntax. */
11237 if (cp_parser_allow_gnu_extensions_p (parser)
11238 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11239 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11240 {
11241 /* Consume the identifier. */
11242 identifier = cp_lexer_consume_token (parser->lexer)->value;
11243 /* Consume the `:'. */
11244 cp_lexer_consume_token (parser->lexer);
11245 }
11246 else
11247 identifier = NULL_TREE;
11248
11249 /* Parse the initializer. */
39703eb9
MM
11250 initializer = cp_parser_initializer_clause (parser,
11251 &clause_non_constant_p);
11252 /* If any clause is non-constant, so is the entire initializer. */
11253 if (clause_non_constant_p)
11254 *non_constant_p = true;
a723baf1
MM
11255 /* Add it to the list. */
11256 initializers = tree_cons (identifier, initializer, initializers);
11257
11258 /* If the next token is not a comma, we have reached the end of
11259 the list. */
11260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11261 break;
11262
11263 /* Peek at the next token. */
11264 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11265 /* If the next token is a `}', then we're still done. An
11266 initializer-clause can have a trailing `,' after the
11267 initializer-list and before the closing `}'. */
11268 if (token->type == CPP_CLOSE_BRACE)
11269 break;
11270
11271 /* Consume the `,' token. */
11272 cp_lexer_consume_token (parser->lexer);
11273 }
11274
11275 /* The initializers were built up in reverse order, so we need to
11276 reverse them now. */
11277 return nreverse (initializers);
11278}
11279
11280/* Classes [gram.class] */
11281
11282/* Parse a class-name.
11283
11284 class-name:
11285 identifier
11286 template-id
11287
11288 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11289 to indicate that names looked up in dependent types should be
11290 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11291 keyword has been used to indicate that the name that appears next
11292 is a template. TYPE_P is true iff the next name should be treated
11293 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11294 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11295 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11296 being defined in a class-head.
a723baf1
MM
11297
11298 Returns the TYPE_DECL representing the class. */
11299
11300static tree
11301cp_parser_class_name (cp_parser *parser,
11302 bool typename_keyword_p,
11303 bool template_keyword_p,
11304 bool type_p,
a723baf1 11305 bool check_dependency_p,
a668c6ad
MM
11306 bool class_head_p,
11307 bool is_declaration)
a723baf1
MM
11308{
11309 tree decl;
11310 tree scope;
11311 bool typename_p;
e5976695
MM
11312 cp_token *token;
11313
11314 /* All class-names start with an identifier. */
11315 token = cp_lexer_peek_token (parser->lexer);
11316 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11317 {
11318 cp_parser_error (parser, "expected class-name");
11319 return error_mark_node;
11320 }
11321
a723baf1
MM
11322 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11323 to a template-id, so we save it here. */
11324 scope = parser->scope;
3adee96c
KL
11325 if (scope == error_mark_node)
11326 return error_mark_node;
11327
a723baf1
MM
11328 /* Any name names a type if we're following the `typename' keyword
11329 in a qualified name where the enclosing scope is type-dependent. */
11330 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11331 && dependent_type_p (scope));
e5976695
MM
11332 /* Handle the common case (an identifier, but not a template-id)
11333 efficiently. */
11334 if (token->type == CPP_NAME
11335 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11336 {
a723baf1
MM
11337 tree identifier;
11338
11339 /* Look for the identifier. */
11340 identifier = cp_parser_identifier (parser);
11341 /* If the next token isn't an identifier, we are certainly not
11342 looking at a class-name. */
11343 if (identifier == error_mark_node)
11344 decl = error_mark_node;
11345 /* If we know this is a type-name, there's no need to look it
11346 up. */
11347 else if (typename_p)
11348 decl = identifier;
11349 else
11350 {
11351 /* If the next token is a `::', then the name must be a type
11352 name.
11353
11354 [basic.lookup.qual]
11355
11356 During the lookup for a name preceding the :: scope
11357 resolution operator, object, function, and enumerator
11358 names are ignored. */
11359 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11360 type_p = true;
11361 /* Look up the name. */
11362 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11363 type_p,
eea9800f 11364 /*is_namespace=*/false,
a723baf1
MM
11365 check_dependency_p);
11366 }
11367 }
e5976695
MM
11368 else
11369 {
11370 /* Try a template-id. */
11371 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
11372 check_dependency_p,
11373 is_declaration);
e5976695
MM
11374 if (decl == error_mark_node)
11375 return error_mark_node;
11376 }
a723baf1
MM
11377
11378 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11379
11380 /* If this is a typename, create a TYPENAME_TYPE. */
11381 if (typename_p && decl != error_mark_node)
11382 decl = TYPE_NAME (make_typename_type (scope, decl,
11383 /*complain=*/1));
11384
11385 /* Check to see that it is really the name of a class. */
11386 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11387 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11388 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11389 /* Situations like this:
11390
11391 template <typename T> struct A {
11392 typename T::template X<int>::I i;
11393 };
11394
11395 are problematic. Is `T::template X<int>' a class-name? The
11396 standard does not seem to be definitive, but there is no other
11397 valid interpretation of the following `::'. Therefore, those
11398 names are considered class-names. */
78757caa 11399 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11400 else if (decl == error_mark_node
11401 || TREE_CODE (decl) != TYPE_DECL
11402 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11403 {
11404 cp_parser_error (parser, "expected class-name");
11405 return error_mark_node;
11406 }
11407
11408 return decl;
11409}
11410
11411/* Parse a class-specifier.
11412
11413 class-specifier:
11414 class-head { member-specification [opt] }
11415
11416 Returns the TREE_TYPE representing the class. */
11417
11418static tree
94edc4ab 11419cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11420{
11421 cp_token *token;
11422 tree type;
11423 tree attributes = NULL_TREE;
11424 int has_trailing_semicolon;
11425 bool nested_name_specifier_p;
a723baf1
MM
11426 unsigned saved_num_template_parameter_lists;
11427
8d241e0b 11428 push_deferring_access_checks (dk_no_deferred);
cf22909c 11429
a723baf1
MM
11430 /* Parse the class-head. */
11431 type = cp_parser_class_head (parser,
cf22909c 11432 &nested_name_specifier_p);
a723baf1
MM
11433 /* If the class-head was a semantic disaster, skip the entire body
11434 of the class. */
11435 if (!type)
11436 {
11437 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11438 pop_deferring_access_checks ();
a723baf1
MM
11439 return error_mark_node;
11440 }
cf22909c 11441
a723baf1
MM
11442 /* Look for the `{'. */
11443 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11444 {
11445 pop_deferring_access_checks ();
11446 return error_mark_node;
11447 }
11448
a723baf1
MM
11449 /* Issue an error message if type-definitions are forbidden here. */
11450 cp_parser_check_type_definition (parser);
11451 /* Remember that we are defining one more class. */
11452 ++parser->num_classes_being_defined;
11453 /* Inside the class, surrounding template-parameter-lists do not
11454 apply. */
11455 saved_num_template_parameter_lists
11456 = parser->num_template_parameter_lists;
11457 parser->num_template_parameter_lists = 0;
78757caa 11458
a723baf1 11459 /* Start the class. */
eeb23c11
MM
11460 if (nested_name_specifier_p)
11461 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11462 type = begin_class_definition (type);
11463 if (type == error_mark_node)
9bcb9aae 11464 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11465 cp_parser_skip_to_closing_brace (parser);
11466 else
11467 /* Parse the member-specification. */
11468 cp_parser_member_specification_opt (parser);
11469 /* Look for the trailing `}'. */
11470 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11471 /* We get better error messages by noticing a common problem: a
11472 missing trailing `;'. */
11473 token = cp_lexer_peek_token (parser->lexer);
11474 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11475 /* Look for attributes to apply to this class. */
11476 if (cp_parser_allow_gnu_extensions_p (parser))
11477 attributes = cp_parser_attributes_opt (parser);
560ad596
MM
11478 /* If we got any attributes in class_head, xref_tag will stick them in
11479 TREE_TYPE of the type. Grab them now. */
11480 if (type != error_mark_node)
11481 {
11482 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11483 TYPE_ATTRIBUTES (type) = NULL_TREE;
11484 type = finish_struct (type, attributes);
11485 }
11486 if (nested_name_specifier_p)
11487 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11488 /* If this class is not itself within the scope of another class,
11489 then we need to parse the bodies of all of the queued function
11490 definitions. Note that the queued functions defined in a class
11491 are not always processed immediately following the
11492 class-specifier for that class. Consider:
11493
11494 struct A {
11495 struct B { void f() { sizeof (A); } };
11496 };
11497
11498 If `f' were processed before the processing of `A' were
11499 completed, there would be no way to compute the size of `A'.
11500 Note that the nesting we are interested in here is lexical --
11501 not the semantic nesting given by TYPE_CONTEXT. In particular,
11502 for:
11503
11504 struct A { struct B; };
11505 struct A::B { void f() { } };
11506
11507 there is no need to delay the parsing of `A::B::f'. */
11508 if (--parser->num_classes_being_defined == 0)
11509 {
8218bd34
MM
11510 tree queue_entry;
11511 tree fn;
a723baf1 11512
8218bd34
MM
11513 /* In a first pass, parse default arguments to the functions.
11514 Then, in a second pass, parse the bodies of the functions.
11515 This two-phased approach handles cases like:
11516
11517 struct S {
11518 void f() { g(); }
11519 void g(int i = 3);
11520 };
11521
11522 */
8db1028e
NS
11523 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11524 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11525 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11526 TREE_PURPOSE (parser->unparsed_functions_queues)
11527 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11528 {
11529 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11530 /* Make sure that any template parameters are in scope. */
11531 maybe_begin_member_template_processing (fn);
11532 /* If there are default arguments that have not yet been processed,
11533 take care of them now. */
11534 cp_parser_late_parsing_default_args (parser, fn);
11535 /* Remove any template parameters from the symbol table. */
11536 maybe_end_member_template_processing ();
11537 }
11538 /* Now parse the body of the functions. */
8db1028e
NS
11539 for (TREE_VALUE (parser->unparsed_functions_queues)
11540 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11541 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11542 TREE_VALUE (parser->unparsed_functions_queues)
11543 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11544 {
a723baf1 11545 /* Figure out which function we need to process. */
a723baf1
MM
11546 fn = TREE_VALUE (queue_entry);
11547
11548 /* Parse the function. */
11549 cp_parser_late_parsing_for_member (parser, fn);
a723baf1
MM
11550 }
11551
a723baf1
MM
11552 }
11553
11554 /* Put back any saved access checks. */
cf22909c 11555 pop_deferring_access_checks ();
a723baf1
MM
11556
11557 /* Restore the count of active template-parameter-lists. */
11558 parser->num_template_parameter_lists
11559 = saved_num_template_parameter_lists;
11560
11561 return type;
11562}
11563
11564/* Parse a class-head.
11565
11566 class-head:
11567 class-key identifier [opt] base-clause [opt]
11568 class-key nested-name-specifier identifier base-clause [opt]
11569 class-key nested-name-specifier [opt] template-id
11570 base-clause [opt]
11571
11572 GNU Extensions:
11573 class-key attributes identifier [opt] base-clause [opt]
11574 class-key attributes nested-name-specifier identifier base-clause [opt]
11575 class-key attributes nested-name-specifier [opt] template-id
11576 base-clause [opt]
11577
11578 Returns the TYPE of the indicated class. Sets
11579 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11580 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11581
11582 Returns NULL_TREE if the class-head is syntactically valid, but
11583 semantically invalid in a way that means we should skip the entire
11584 body of the class. */
11585
11586static tree
94edc4ab
NN
11587cp_parser_class_head (cp_parser* parser,
11588 bool* nested_name_specifier_p)
a723baf1
MM
11589{
11590 cp_token *token;
11591 tree nested_name_specifier;
11592 enum tag_types class_key;
11593 tree id = NULL_TREE;
11594 tree type = NULL_TREE;
11595 tree attributes;
11596 bool template_id_p = false;
11597 bool qualified_p = false;
11598 bool invalid_nested_name_p = false;
afb0918a 11599 bool invalid_explicit_specialization_p = false;
a723baf1
MM
11600 unsigned num_templates;
11601
11602 /* Assume no nested-name-specifier will be present. */
11603 *nested_name_specifier_p = false;
11604 /* Assume no template parameter lists will be used in defining the
11605 type. */
11606 num_templates = 0;
11607
11608 /* Look for the class-key. */
11609 class_key = cp_parser_class_key (parser);
11610 if (class_key == none_type)
11611 return error_mark_node;
11612
11613 /* Parse the attributes. */
11614 attributes = cp_parser_attributes_opt (parser);
11615
11616 /* If the next token is `::', that is invalid -- but sometimes
11617 people do try to write:
11618
11619 struct ::S {};
11620
11621 Handle this gracefully by accepting the extra qualifier, and then
11622 issuing an error about it later if this really is a
2050a1bb 11623 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11624 specifier, remain silent. */
11625 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11626 qualified_p = true;
11627
8d241e0b
KL
11628 push_deferring_access_checks (dk_no_check);
11629
a723baf1
MM
11630 /* Determine the name of the class. Begin by looking for an
11631 optional nested-name-specifier. */
11632 nested_name_specifier
11633 = cp_parser_nested_name_specifier_opt (parser,
11634 /*typename_keyword_p=*/false,
66d418e6 11635 /*check_dependency_p=*/false,
a668c6ad
MM
11636 /*type_p=*/false,
11637 /*is_declaration=*/false);
a723baf1
MM
11638 /* If there was a nested-name-specifier, then there *must* be an
11639 identifier. */
11640 if (nested_name_specifier)
11641 {
11642 /* Although the grammar says `identifier', it really means
11643 `class-name' or `template-name'. You are only allowed to
11644 define a class that has already been declared with this
11645 syntax.
11646
11647 The proposed resolution for Core Issue 180 says that whever
11648 you see `class T::X' you should treat `X' as a type-name.
11649
11650 It is OK to define an inaccessible class; for example:
11651
11652 class A { class B; };
11653 class A::B {};
11654
a723baf1
MM
11655 We do not know if we will see a class-name, or a
11656 template-name. We look for a class-name first, in case the
11657 class-name is a template-id; if we looked for the
11658 template-name first we would stop after the template-name. */
11659 cp_parser_parse_tentatively (parser);
11660 type = cp_parser_class_name (parser,
11661 /*typename_keyword_p=*/false,
11662 /*template_keyword_p=*/false,
11663 /*type_p=*/true,
a723baf1 11664 /*check_dependency_p=*/false,
a668c6ad
MM
11665 /*class_head_p=*/true,
11666 /*is_declaration=*/false);
a723baf1
MM
11667 /* If that didn't work, ignore the nested-name-specifier. */
11668 if (!cp_parser_parse_definitely (parser))
11669 {
11670 invalid_nested_name_p = true;
11671 id = cp_parser_identifier (parser);
11672 if (id == error_mark_node)
11673 id = NULL_TREE;
11674 }
11675 /* If we could not find a corresponding TYPE, treat this
11676 declaration like an unqualified declaration. */
11677 if (type == error_mark_node)
11678 nested_name_specifier = NULL_TREE;
11679 /* Otherwise, count the number of templates used in TYPE and its
11680 containing scopes. */
11681 else
11682 {
11683 tree scope;
11684
11685 for (scope = TREE_TYPE (type);
11686 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11687 scope = (TYPE_P (scope)
11688 ? TYPE_CONTEXT (scope)
11689 : DECL_CONTEXT (scope)))
11690 if (TYPE_P (scope)
11691 && CLASS_TYPE_P (scope)
11692 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11693 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11694 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11695 ++num_templates;
11696 }
11697 }
11698 /* Otherwise, the identifier is optional. */
11699 else
11700 {
11701 /* We don't know whether what comes next is a template-id,
11702 an identifier, or nothing at all. */
11703 cp_parser_parse_tentatively (parser);
11704 /* Check for a template-id. */
11705 id = cp_parser_template_id (parser,
11706 /*template_keyword_p=*/false,
a668c6ad
MM
11707 /*check_dependency_p=*/true,
11708 /*is_declaration=*/true);
a723baf1
MM
11709 /* If that didn't work, it could still be an identifier. */
11710 if (!cp_parser_parse_definitely (parser))
11711 {
11712 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11713 id = cp_parser_identifier (parser);
11714 else
11715 id = NULL_TREE;
11716 }
11717 else
11718 {
11719 template_id_p = true;
11720 ++num_templates;
11721 }
11722 }
11723
8d241e0b
KL
11724 pop_deferring_access_checks ();
11725
ee43dab5
MM
11726 cp_parser_check_for_invalid_template_id (parser, id);
11727
a723baf1
MM
11728 /* If it's not a `:' or a `{' then we can't really be looking at a
11729 class-head, since a class-head only appears as part of a
11730 class-specifier. We have to detect this situation before calling
11731 xref_tag, since that has irreversible side-effects. */
11732 if (!cp_parser_next_token_starts_class_definition_p (parser))
11733 {
11734 cp_parser_error (parser, "expected `{' or `:'");
11735 return error_mark_node;
11736 }
11737
11738 /* At this point, we're going ahead with the class-specifier, even
11739 if some other problem occurs. */
11740 cp_parser_commit_to_tentative_parse (parser);
11741 /* Issue the error about the overly-qualified name now. */
11742 if (qualified_p)
11743 cp_parser_error (parser,
11744 "global qualification of class name is invalid");
11745 else if (invalid_nested_name_p)
11746 cp_parser_error (parser,
11747 "qualified name does not name a class");
afb0918a
MM
11748 /* An explicit-specialization must be preceded by "template <>". If
11749 it is not, try to recover gracefully. */
11750 if (at_namespace_scope_p ()
11751 && parser->num_template_parameter_lists == 0
eeb23c11 11752 && template_id_p)
afb0918a
MM
11753 {
11754 error ("an explicit specialization must be preceded by 'template <>'");
11755 invalid_explicit_specialization_p = true;
11756 /* Take the same action that would have been taken by
11757 cp_parser_explicit_specialization. */
11758 ++parser->num_template_parameter_lists;
11759 begin_specialization ();
11760 }
11761 /* There must be no "return" statements between this point and the
11762 end of this function; set "type "to the correct return value and
11763 use "goto done;" to return. */
a723baf1
MM
11764 /* Make sure that the right number of template parameters were
11765 present. */
11766 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
11767 {
11768 /* If something went wrong, there is no point in even trying to
11769 process the class-definition. */
11770 type = NULL_TREE;
11771 goto done;
11772 }
a723baf1 11773
a723baf1
MM
11774 /* Look up the type. */
11775 if (template_id_p)
11776 {
11777 type = TREE_TYPE (id);
11778 maybe_process_partial_specialization (type);
11779 }
11780 else if (!nested_name_specifier)
11781 {
11782 /* If the class was unnamed, create a dummy name. */
11783 if (!id)
11784 id = make_anon_name ();
cbd63935
KL
11785 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11786 parser->num_template_parameter_lists);
a723baf1
MM
11787 }
11788 else
11789 {
a723baf1 11790 tree class_type;
089d6ea7 11791 tree scope;
a723baf1
MM
11792
11793 /* Given:
11794
11795 template <typename T> struct S { struct T };
14d22dd6 11796 template <typename T> struct S<T>::T { };
a723baf1
MM
11797
11798 we will get a TYPENAME_TYPE when processing the definition of
11799 `S::T'. We need to resolve it to the actual type before we
11800 try to define it. */
11801 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11802 {
14d22dd6
MM
11803 class_type = resolve_typename_type (TREE_TYPE (type),
11804 /*only_current_p=*/false);
11805 if (class_type != error_mark_node)
11806 type = TYPE_NAME (class_type);
11807 else
11808 {
11809 cp_parser_error (parser, "could not resolve typename type");
11810 type = error_mark_node;
11811 }
a723baf1
MM
11812 }
11813
089d6ea7
MM
11814 /* Figure out in what scope the declaration is being placed. */
11815 scope = current_scope ();
11816 if (!scope)
11817 scope = current_namespace;
11818 /* If that scope does not contain the scope in which the
11819 class was originally declared, the program is invalid. */
11820 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11821 {
0e136342 11822 error ("declaration of `%D' in `%D' which does not "
089d6ea7 11823 "enclose `%D'", type, scope, nested_name_specifier);
afb0918a
MM
11824 type = NULL_TREE;
11825 goto done;
089d6ea7 11826 }
560ad596 11827 /* [dcl.meaning]
089d6ea7 11828
560ad596
MM
11829 A declarator-id shall not be qualified exception of the
11830 definition of a ... nested class outside of its class
11831 ... [or] a the definition or explicit instantiation of a
11832 class member of a namespace outside of its namespace. */
11833 if (scope == CP_DECL_CONTEXT (type))
a723baf1 11834 {
560ad596
MM
11835 pedwarn ("extra qualification ignored");
11836 nested_name_specifier = NULL_TREE;
a723baf1 11837 }
560ad596
MM
11838
11839 maybe_process_partial_specialization (TREE_TYPE (type));
11840 class_type = current_class_type;
11841 /* Enter the scope indicated by the nested-name-specifier. */
11842 if (nested_name_specifier)
11843 push_scope (nested_name_specifier);
11844 /* Get the canonical version of this type. */
11845 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11846 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11847 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11848 type = push_template_decl (type);
11849 type = TREE_TYPE (type);
11850 if (nested_name_specifier)
eeb23c11
MM
11851 {
11852 *nested_name_specifier_p = true;
11853 pop_scope (nested_name_specifier);
11854 }
a723baf1
MM
11855 }
11856 /* Indicate whether this class was declared as a `class' or as a
11857 `struct'. */
11858 if (TREE_CODE (type) == RECORD_TYPE)
11859 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11860 cp_parser_check_class_key (class_key, type);
11861
11862 /* Enter the scope containing the class; the names of base classes
11863 should be looked up in that context. For example, given:
11864
11865 struct A { struct B {}; struct C; };
11866 struct A::C : B {};
11867
11868 is valid. */
11869 if (nested_name_specifier)
11870 push_scope (nested_name_specifier);
11871 /* Now, look for the base-clause. */
11872 token = cp_lexer_peek_token (parser->lexer);
11873 if (token->type == CPP_COLON)
11874 {
11875 tree bases;
11876
11877 /* Get the list of base-classes. */
11878 bases = cp_parser_base_clause (parser);
11879 /* Process them. */
11880 xref_basetypes (type, bases);
11881 }
11882 /* Leave the scope given by the nested-name-specifier. We will
11883 enter the class scope itself while processing the members. */
11884 if (nested_name_specifier)
11885 pop_scope (nested_name_specifier);
11886
afb0918a
MM
11887 done:
11888 if (invalid_explicit_specialization_p)
11889 {
11890 end_specialization ();
11891 --parser->num_template_parameter_lists;
11892 }
a723baf1
MM
11893 return type;
11894}
11895
11896/* Parse a class-key.
11897
11898 class-key:
11899 class
11900 struct
11901 union
11902
11903 Returns the kind of class-key specified, or none_type to indicate
11904 error. */
11905
11906static enum tag_types
94edc4ab 11907cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11908{
11909 cp_token *token;
11910 enum tag_types tag_type;
11911
11912 /* Look for the class-key. */
11913 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11914 if (!token)
11915 return none_type;
11916
11917 /* Check to see if the TOKEN is a class-key. */
11918 tag_type = cp_parser_token_is_class_key (token);
11919 if (!tag_type)
11920 cp_parser_error (parser, "expected class-key");
11921 return tag_type;
11922}
11923
11924/* Parse an (optional) member-specification.
11925
11926 member-specification:
11927 member-declaration member-specification [opt]
11928 access-specifier : member-specification [opt] */
11929
11930static void
94edc4ab 11931cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11932{
11933 while (true)
11934 {
11935 cp_token *token;
11936 enum rid keyword;
11937
11938 /* Peek at the next token. */
11939 token = cp_lexer_peek_token (parser->lexer);
11940 /* If it's a `}', or EOF then we've seen all the members. */
11941 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11942 break;
11943
11944 /* See if this token is a keyword. */
11945 keyword = token->keyword;
11946 switch (keyword)
11947 {
11948 case RID_PUBLIC:
11949 case RID_PROTECTED:
11950 case RID_PRIVATE:
11951 /* Consume the access-specifier. */
11952 cp_lexer_consume_token (parser->lexer);
11953 /* Remember which access-specifier is active. */
11954 current_access_specifier = token->value;
11955 /* Look for the `:'. */
11956 cp_parser_require (parser, CPP_COLON, "`:'");
11957 break;
11958
11959 default:
11960 /* Otherwise, the next construction must be a
11961 member-declaration. */
11962 cp_parser_member_declaration (parser);
a723baf1
MM
11963 }
11964 }
11965}
11966
11967/* Parse a member-declaration.
11968
11969 member-declaration:
11970 decl-specifier-seq [opt] member-declarator-list [opt] ;
11971 function-definition ; [opt]
11972 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11973 using-declaration
11974 template-declaration
11975
11976 member-declarator-list:
11977 member-declarator
11978 member-declarator-list , member-declarator
11979
11980 member-declarator:
11981 declarator pure-specifier [opt]
11982 declarator constant-initializer [opt]
11983 identifier [opt] : constant-expression
11984
11985 GNU Extensions:
11986
11987 member-declaration:
11988 __extension__ member-declaration
11989
11990 member-declarator:
11991 declarator attributes [opt] pure-specifier [opt]
11992 declarator attributes [opt] constant-initializer [opt]
11993 identifier [opt] attributes [opt] : constant-expression */
11994
11995static void
94edc4ab 11996cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
11997{
11998 tree decl_specifiers;
11999 tree prefix_attributes;
12000 tree decl;
560ad596 12001 int declares_class_or_enum;
a723baf1
MM
12002 bool friend_p;
12003 cp_token *token;
12004 int saved_pedantic;
12005
12006 /* Check for the `__extension__' keyword. */
12007 if (cp_parser_extension_opt (parser, &saved_pedantic))
12008 {
12009 /* Recurse. */
12010 cp_parser_member_declaration (parser);
12011 /* Restore the old value of the PEDANTIC flag. */
12012 pedantic = saved_pedantic;
12013
12014 return;
12015 }
12016
12017 /* Check for a template-declaration. */
12018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12019 {
12020 /* Parse the template-declaration. */
12021 cp_parser_template_declaration (parser, /*member_p=*/true);
12022
12023 return;
12024 }
12025
12026 /* Check for a using-declaration. */
12027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12028 {
12029 /* Parse the using-declaration. */
12030 cp_parser_using_declaration (parser);
12031
12032 return;
12033 }
12034
a723baf1
MM
12035 /* Parse the decl-specifier-seq. */
12036 decl_specifiers
12037 = cp_parser_decl_specifier_seq (parser,
12038 CP_PARSER_FLAGS_OPTIONAL,
12039 &prefix_attributes,
12040 &declares_class_or_enum);
8fbc5ae7
MM
12041 /* Check for an invalid type-name. */
12042 if (cp_parser_diagnose_invalid_type_name (parser))
12043 return;
a723baf1
MM
12044 /* If there is no declarator, then the decl-specifier-seq should
12045 specify a type. */
12046 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12047 {
12048 /* If there was no decl-specifier-seq, and the next token is a
12049 `;', then we have something like:
12050
12051 struct S { ; };
12052
12053 [class.mem]
12054
12055 Each member-declaration shall declare at least one member
12056 name of the class. */
12057 if (!decl_specifiers)
12058 {
12059 if (pedantic)
12060 pedwarn ("extra semicolon");
12061 }
12062 else
12063 {
12064 tree type;
12065
12066 /* See if this declaration is a friend. */
12067 friend_p = cp_parser_friend_p (decl_specifiers);
12068 /* If there were decl-specifiers, check to see if there was
12069 a class-declaration. */
12070 type = check_tag_decl (decl_specifiers);
12071 /* Nested classes have already been added to the class, but
12072 a `friend' needs to be explicitly registered. */
12073 if (friend_p)
12074 {
12075 /* If the `friend' keyword was present, the friend must
12076 be introduced with a class-key. */
12077 if (!declares_class_or_enum)
12078 error ("a class-key must be used when declaring a friend");
12079 /* In this case:
12080
12081 template <typename T> struct A {
12082 friend struct A<T>::B;
12083 };
12084
12085 A<T>::B will be represented by a TYPENAME_TYPE, and
12086 therefore not recognized by check_tag_decl. */
12087 if (!type)
12088 {
12089 tree specifier;
12090
12091 for (specifier = decl_specifiers;
12092 specifier;
12093 specifier = TREE_CHAIN (specifier))
12094 {
12095 tree s = TREE_VALUE (specifier);
12096
c003e212
GDR
12097 if (TREE_CODE (s) == IDENTIFIER_NODE)
12098 get_global_value_if_present (s, &type);
a723baf1
MM
12099 if (TREE_CODE (s) == TYPE_DECL)
12100 s = TREE_TYPE (s);
12101 if (TYPE_P (s))
12102 {
12103 type = s;
12104 break;
12105 }
12106 }
12107 }
12108 if (!type)
12109 error ("friend declaration does not name a class or "
12110 "function");
12111 else
19db77ce
KL
12112 make_friend_class (current_class_type, type,
12113 /*complain=*/true);
a723baf1
MM
12114 }
12115 /* If there is no TYPE, an error message will already have
12116 been issued. */
12117 else if (!type)
12118 ;
12119 /* An anonymous aggregate has to be handled specially; such
12120 a declaration really declares a data member (with a
12121 particular type), as opposed to a nested class. */
12122 else if (ANON_AGGR_TYPE_P (type))
12123 {
12124 /* Remove constructors and such from TYPE, now that we
34cd5ae7 12125 know it is an anonymous aggregate. */
a723baf1
MM
12126 fixup_anonymous_aggr (type);
12127 /* And make the corresponding data member. */
12128 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12129 /* Add it to the class. */
12130 finish_member_declaration (decl);
12131 }
37d407a1
KL
12132 else
12133 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
12134 }
12135 }
12136 else
12137 {
12138 /* See if these declarations will be friends. */
12139 friend_p = cp_parser_friend_p (decl_specifiers);
12140
12141 /* Keep going until we hit the `;' at the end of the
12142 declaration. */
12143 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12144 {
12145 tree attributes = NULL_TREE;
12146 tree first_attribute;
12147
12148 /* Peek at the next token. */
12149 token = cp_lexer_peek_token (parser->lexer);
12150
12151 /* Check for a bitfield declaration. */
12152 if (token->type == CPP_COLON
12153 || (token->type == CPP_NAME
12154 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12155 == CPP_COLON))
12156 {
12157 tree identifier;
12158 tree width;
12159
12160 /* Get the name of the bitfield. Note that we cannot just
12161 check TOKEN here because it may have been invalidated by
12162 the call to cp_lexer_peek_nth_token above. */
12163 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12164 identifier = cp_parser_identifier (parser);
12165 else
12166 identifier = NULL_TREE;
12167
12168 /* Consume the `:' token. */
12169 cp_lexer_consume_token (parser->lexer);
12170 /* Get the width of the bitfield. */
14d22dd6
MM
12171 width
12172 = cp_parser_constant_expression (parser,
12173 /*allow_non_constant=*/false,
12174 NULL);
a723baf1
MM
12175
12176 /* Look for attributes that apply to the bitfield. */
12177 attributes = cp_parser_attributes_opt (parser);
12178 /* Remember which attributes are prefix attributes and
12179 which are not. */
12180 first_attribute = attributes;
12181 /* Combine the attributes. */
12182 attributes = chainon (prefix_attributes, attributes);
12183
12184 /* Create the bitfield declaration. */
12185 decl = grokbitfield (identifier,
12186 decl_specifiers,
12187 width);
12188 /* Apply the attributes. */
12189 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12190 }
12191 else
12192 {
12193 tree declarator;
12194 tree initializer;
12195 tree asm_specification;
7efa3e22 12196 int ctor_dtor_or_conv_p;
a723baf1
MM
12197
12198 /* Parse the declarator. */
12199 declarator
62b8a44e 12200 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
12201 &ctor_dtor_or_conv_p,
12202 /*parenthesized_p=*/NULL);
a723baf1
MM
12203
12204 /* If something went wrong parsing the declarator, make sure
12205 that we at least consume some tokens. */
12206 if (declarator == error_mark_node)
12207 {
12208 /* Skip to the end of the statement. */
12209 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
12210 /* If the next token is not a semicolon, that is
12211 probably because we just skipped over the body of
12212 a function. So, we consume a semicolon if
12213 present, but do not issue an error message if it
12214 is not present. */
12215 if (cp_lexer_next_token_is (parser->lexer,
12216 CPP_SEMICOLON))
12217 cp_lexer_consume_token (parser->lexer);
12218 return;
a723baf1
MM
12219 }
12220
560ad596
MM
12221 cp_parser_check_for_definition_in_return_type
12222 (declarator, declares_class_or_enum);
12223
a723baf1
MM
12224 /* Look for an asm-specification. */
12225 asm_specification = cp_parser_asm_specification_opt (parser);
12226 /* Look for attributes that apply to the declaration. */
12227 attributes = cp_parser_attributes_opt (parser);
12228 /* Remember which attributes are prefix attributes and
12229 which are not. */
12230 first_attribute = attributes;
12231 /* Combine the attributes. */
12232 attributes = chainon (prefix_attributes, attributes);
12233
12234 /* If it's an `=', then we have a constant-initializer or a
12235 pure-specifier. It is not correct to parse the
12236 initializer before registering the member declaration
12237 since the member declaration should be in scope while
12238 its initializer is processed. However, the rest of the
12239 front end does not yet provide an interface that allows
12240 us to handle this correctly. */
12241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12242 {
12243 /* In [class.mem]:
12244
12245 A pure-specifier shall be used only in the declaration of
12246 a virtual function.
12247
12248 A member-declarator can contain a constant-initializer
12249 only if it declares a static member of integral or
12250 enumeration type.
12251
12252 Therefore, if the DECLARATOR is for a function, we look
12253 for a pure-specifier; otherwise, we look for a
12254 constant-initializer. When we call `grokfield', it will
12255 perform more stringent semantics checks. */
12256 if (TREE_CODE (declarator) == CALL_EXPR)
12257 initializer = cp_parser_pure_specifier (parser);
12258 else
4bb8ca28
MM
12259 /* Parse the initializer. */
12260 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
12261 }
12262 /* Otherwise, there is no initializer. */
12263 else
12264 initializer = NULL_TREE;
12265
12266 /* See if we are probably looking at a function
12267 definition. We are certainly not looking at at a
12268 member-declarator. Calling `grokfield' has
12269 side-effects, so we must not do it unless we are sure
12270 that we are looking at a member-declarator. */
12271 if (cp_parser_token_starts_function_definition_p
12272 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
12273 {
12274 /* The grammar does not allow a pure-specifier to be
12275 used when a member function is defined. (It is
12276 possible that this fact is an oversight in the
12277 standard, since a pure function may be defined
12278 outside of the class-specifier. */
12279 if (initializer)
12280 error ("pure-specifier on function-definition");
12281 decl = cp_parser_save_member_function_body (parser,
12282 decl_specifiers,
12283 declarator,
12284 attributes);
12285 /* If the member was not a friend, declare it here. */
12286 if (!friend_p)
12287 finish_member_declaration (decl);
12288 /* Peek at the next token. */
12289 token = cp_lexer_peek_token (parser->lexer);
12290 /* If the next token is a semicolon, consume it. */
12291 if (token->type == CPP_SEMICOLON)
12292 cp_lexer_consume_token (parser->lexer);
12293 return;
12294 }
a723baf1 12295 else
39703eb9
MM
12296 {
12297 /* Create the declaration. */
ee3071ef
NS
12298 decl = grokfield (declarator, decl_specifiers,
12299 initializer, asm_specification,
39703eb9
MM
12300 attributes);
12301 /* Any initialization must have been from a
12302 constant-expression. */
12303 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12304 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12305 }
a723baf1
MM
12306 }
12307
12308 /* Reset PREFIX_ATTRIBUTES. */
12309 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12310 attributes = TREE_CHAIN (attributes);
12311 if (attributes)
12312 TREE_CHAIN (attributes) = NULL_TREE;
12313
12314 /* If there is any qualification still in effect, clear it
12315 now; we will be starting fresh with the next declarator. */
12316 parser->scope = NULL_TREE;
12317 parser->qualifying_scope = NULL_TREE;
12318 parser->object_scope = NULL_TREE;
12319 /* If it's a `,', then there are more declarators. */
12320 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12321 cp_lexer_consume_token (parser->lexer);
12322 /* If the next token isn't a `;', then we have a parse error. */
12323 else if (cp_lexer_next_token_is_not (parser->lexer,
12324 CPP_SEMICOLON))
12325 {
12326 cp_parser_error (parser, "expected `;'");
04c06002 12327 /* Skip tokens until we find a `;'. */
a723baf1
MM
12328 cp_parser_skip_to_end_of_statement (parser);
12329
12330 break;
12331 }
12332
12333 if (decl)
12334 {
12335 /* Add DECL to the list of members. */
12336 if (!friend_p)
12337 finish_member_declaration (decl);
12338
a723baf1 12339 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12340 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12341 }
12342 }
12343 }
12344
4bb8ca28 12345 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
12346}
12347
12348/* Parse a pure-specifier.
12349
12350 pure-specifier:
12351 = 0
12352
12353 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12354 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12355
12356static tree
94edc4ab 12357cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12358{
12359 cp_token *token;
12360
12361 /* Look for the `=' token. */
12362 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12363 return error_mark_node;
12364 /* Look for the `0' token. */
12365 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12366 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12367 to get information from the lexer about how the number was
12368 spelled in order to fix this problem. */
12369 if (!token || !integer_zerop (token->value))
12370 return error_mark_node;
12371
12372 return integer_zero_node;
12373}
12374
12375/* Parse a constant-initializer.
12376
12377 constant-initializer:
12378 = constant-expression
12379
12380 Returns a representation of the constant-expression. */
12381
12382static tree
94edc4ab 12383cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12384{
12385 /* Look for the `=' token. */
12386 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12387 return error_mark_node;
12388
12389 /* It is invalid to write:
12390
12391 struct S { static const int i = { 7 }; };
12392
12393 */
12394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12395 {
12396 cp_parser_error (parser,
12397 "a brace-enclosed initializer is not allowed here");
12398 /* Consume the opening brace. */
12399 cp_lexer_consume_token (parser->lexer);
12400 /* Skip the initializer. */
12401 cp_parser_skip_to_closing_brace (parser);
12402 /* Look for the trailing `}'. */
12403 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12404
12405 return error_mark_node;
12406 }
12407
14d22dd6
MM
12408 return cp_parser_constant_expression (parser,
12409 /*allow_non_constant=*/false,
12410 NULL);
a723baf1
MM
12411}
12412
12413/* Derived classes [gram.class.derived] */
12414
12415/* Parse a base-clause.
12416
12417 base-clause:
12418 : base-specifier-list
12419
12420 base-specifier-list:
12421 base-specifier
12422 base-specifier-list , base-specifier
12423
12424 Returns a TREE_LIST representing the base-classes, in the order in
12425 which they were declared. The representation of each node is as
12426 described by cp_parser_base_specifier.
12427
12428 In the case that no bases are specified, this function will return
12429 NULL_TREE, not ERROR_MARK_NODE. */
12430
12431static tree
94edc4ab 12432cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12433{
12434 tree bases = NULL_TREE;
12435
12436 /* Look for the `:' that begins the list. */
12437 cp_parser_require (parser, CPP_COLON, "`:'");
12438
12439 /* Scan the base-specifier-list. */
12440 while (true)
12441 {
12442 cp_token *token;
12443 tree base;
12444
12445 /* Look for the base-specifier. */
12446 base = cp_parser_base_specifier (parser);
12447 /* Add BASE to the front of the list. */
12448 if (base != error_mark_node)
12449 {
12450 TREE_CHAIN (base) = bases;
12451 bases = base;
12452 }
12453 /* Peek at the next token. */
12454 token = cp_lexer_peek_token (parser->lexer);
12455 /* If it's not a comma, then the list is complete. */
12456 if (token->type != CPP_COMMA)
12457 break;
12458 /* Consume the `,'. */
12459 cp_lexer_consume_token (parser->lexer);
12460 }
12461
12462 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12463 base class had a qualified name. However, the next name that
12464 appears is certainly not qualified. */
12465 parser->scope = NULL_TREE;
12466 parser->qualifying_scope = NULL_TREE;
12467 parser->object_scope = NULL_TREE;
12468
12469 return nreverse (bases);
12470}
12471
12472/* Parse a base-specifier.
12473
12474 base-specifier:
12475 :: [opt] nested-name-specifier [opt] class-name
12476 virtual access-specifier [opt] :: [opt] nested-name-specifier
12477 [opt] class-name
12478 access-specifier virtual [opt] :: [opt] nested-name-specifier
12479 [opt] class-name
12480
12481 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12482 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12483 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12484 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12485
12486static tree
94edc4ab 12487cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12488{
12489 cp_token *token;
12490 bool done = false;
12491 bool virtual_p = false;
12492 bool duplicate_virtual_error_issued_p = false;
12493 bool duplicate_access_error_issued_p = false;
bbaab916 12494 bool class_scope_p, template_p;
dbbf88d1 12495 tree access = access_default_node;
a723baf1
MM
12496 tree type;
12497
12498 /* Process the optional `virtual' and `access-specifier'. */
12499 while (!done)
12500 {
12501 /* Peek at the next token. */
12502 token = cp_lexer_peek_token (parser->lexer);
12503 /* Process `virtual'. */
12504 switch (token->keyword)
12505 {
12506 case RID_VIRTUAL:
12507 /* If `virtual' appears more than once, issue an error. */
12508 if (virtual_p && !duplicate_virtual_error_issued_p)
12509 {
12510 cp_parser_error (parser,
12511 "`virtual' specified more than once in base-specified");
12512 duplicate_virtual_error_issued_p = true;
12513 }
12514
12515 virtual_p = true;
12516
12517 /* Consume the `virtual' token. */
12518 cp_lexer_consume_token (parser->lexer);
12519
12520 break;
12521
12522 case RID_PUBLIC:
12523 case RID_PROTECTED:
12524 case RID_PRIVATE:
12525 /* If more than one access specifier appears, issue an
12526 error. */
dbbf88d1
NS
12527 if (access != access_default_node
12528 && !duplicate_access_error_issued_p)
a723baf1
MM
12529 {
12530 cp_parser_error (parser,
12531 "more than one access specifier in base-specified");
12532 duplicate_access_error_issued_p = true;
12533 }
12534
dbbf88d1 12535 access = ridpointers[(int) token->keyword];
a723baf1
MM
12536
12537 /* Consume the access-specifier. */
12538 cp_lexer_consume_token (parser->lexer);
12539
12540 break;
12541
12542 default:
12543 done = true;
12544 break;
12545 }
12546 }
12547
a723baf1
MM
12548 /* Look for the optional `::' operator. */
12549 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12550 /* Look for the nested-name-specifier. The simplest way to
12551 implement:
12552
12553 [temp.res]
12554
12555 The keyword `typename' is not permitted in a base-specifier or
12556 mem-initializer; in these contexts a qualified name that
12557 depends on a template-parameter is implicitly assumed to be a
12558 type name.
12559
12560 is to pretend that we have seen the `typename' keyword at this
12561 point. */
12562 cp_parser_nested_name_specifier_opt (parser,
12563 /*typename_keyword_p=*/true,
12564 /*check_dependency_p=*/true,
a668c6ad
MM
12565 /*type_p=*/true,
12566 /*is_declaration=*/true);
a723baf1
MM
12567 /* If the base class is given by a qualified name, assume that names
12568 we see are type names or templates, as appropriate. */
12569 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12570 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12571
a723baf1
MM
12572 /* Finally, look for the class-name. */
12573 type = cp_parser_class_name (parser,
12574 class_scope_p,
bbaab916 12575 template_p,
a723baf1 12576 /*type_p=*/true,
a723baf1 12577 /*check_dependency_p=*/true,
a668c6ad
MM
12578 /*class_head_p=*/false,
12579 /*is_declaration=*/true);
a723baf1
MM
12580
12581 if (type == error_mark_node)
12582 return error_mark_node;
12583
dbbf88d1 12584 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12585}
12586
12587/* Exception handling [gram.exception] */
12588
12589/* Parse an (optional) exception-specification.
12590
12591 exception-specification:
12592 throw ( type-id-list [opt] )
12593
12594 Returns a TREE_LIST representing the exception-specification. The
12595 TREE_VALUE of each node is a type. */
12596
12597static tree
94edc4ab 12598cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12599{
12600 cp_token *token;
12601 tree type_id_list;
12602
12603 /* Peek at the next token. */
12604 token = cp_lexer_peek_token (parser->lexer);
12605 /* If it's not `throw', then there's no exception-specification. */
12606 if (!cp_parser_is_keyword (token, RID_THROW))
12607 return NULL_TREE;
12608
12609 /* Consume the `throw'. */
12610 cp_lexer_consume_token (parser->lexer);
12611
12612 /* Look for the `('. */
12613 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12614
12615 /* Peek at the next token. */
12616 token = cp_lexer_peek_token (parser->lexer);
12617 /* If it's not a `)', then there is a type-id-list. */
12618 if (token->type != CPP_CLOSE_PAREN)
12619 {
12620 const char *saved_message;
12621
12622 /* Types may not be defined in an exception-specification. */
12623 saved_message = parser->type_definition_forbidden_message;
12624 parser->type_definition_forbidden_message
12625 = "types may not be defined in an exception-specification";
12626 /* Parse the type-id-list. */
12627 type_id_list = cp_parser_type_id_list (parser);
12628 /* Restore the saved message. */
12629 parser->type_definition_forbidden_message = saved_message;
12630 }
12631 else
12632 type_id_list = empty_except_spec;
12633
12634 /* Look for the `)'. */
12635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12636
12637 return type_id_list;
12638}
12639
12640/* Parse an (optional) type-id-list.
12641
12642 type-id-list:
12643 type-id
12644 type-id-list , type-id
12645
12646 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12647 in the order that the types were presented. */
12648
12649static tree
94edc4ab 12650cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12651{
12652 tree types = NULL_TREE;
12653
12654 while (true)
12655 {
12656 cp_token *token;
12657 tree type;
12658
12659 /* Get the next type-id. */
12660 type = cp_parser_type_id (parser);
12661 /* Add it to the list. */
12662 types = add_exception_specifier (types, type, /*complain=*/1);
12663 /* Peek at the next token. */
12664 token = cp_lexer_peek_token (parser->lexer);
12665 /* If it is not a `,', we are done. */
12666 if (token->type != CPP_COMMA)
12667 break;
12668 /* Consume the `,'. */
12669 cp_lexer_consume_token (parser->lexer);
12670 }
12671
12672 return nreverse (types);
12673}
12674
12675/* Parse a try-block.
12676
12677 try-block:
12678 try compound-statement handler-seq */
12679
12680static tree
94edc4ab 12681cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12682{
12683 tree try_block;
12684
12685 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12686 try_block = begin_try_block ();
a5bcc582 12687 cp_parser_compound_statement (parser, false);
a723baf1
MM
12688 finish_try_block (try_block);
12689 cp_parser_handler_seq (parser);
12690 finish_handler_sequence (try_block);
12691
12692 return try_block;
12693}
12694
12695/* Parse a function-try-block.
12696
12697 function-try-block:
12698 try ctor-initializer [opt] function-body handler-seq */
12699
12700static bool
94edc4ab 12701cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12702{
12703 tree try_block;
12704 bool ctor_initializer_p;
12705
12706 /* Look for the `try' keyword. */
12707 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12708 return false;
12709 /* Let the rest of the front-end know where we are. */
12710 try_block = begin_function_try_block ();
12711 /* Parse the function-body. */
12712 ctor_initializer_p
12713 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12714 /* We're done with the `try' part. */
12715 finish_function_try_block (try_block);
12716 /* Parse the handlers. */
12717 cp_parser_handler_seq (parser);
12718 /* We're done with the handlers. */
12719 finish_function_handler_sequence (try_block);
12720
12721 return ctor_initializer_p;
12722}
12723
12724/* Parse a handler-seq.
12725
12726 handler-seq:
12727 handler handler-seq [opt] */
12728
12729static void
94edc4ab 12730cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12731{
12732 while (true)
12733 {
12734 cp_token *token;
12735
12736 /* Parse the handler. */
12737 cp_parser_handler (parser);
12738 /* Peek at the next token. */
12739 token = cp_lexer_peek_token (parser->lexer);
12740 /* If it's not `catch' then there are no more handlers. */
12741 if (!cp_parser_is_keyword (token, RID_CATCH))
12742 break;
12743 }
12744}
12745
12746/* Parse a handler.
12747
12748 handler:
12749 catch ( exception-declaration ) compound-statement */
12750
12751static void
94edc4ab 12752cp_parser_handler (cp_parser* parser)
a723baf1
MM
12753{
12754 tree handler;
12755 tree declaration;
12756
12757 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12758 handler = begin_handler ();
12759 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12760 declaration = cp_parser_exception_declaration (parser);
12761 finish_handler_parms (declaration, handler);
12762 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 12763 cp_parser_compound_statement (parser, false);
a723baf1
MM
12764 finish_handler (handler);
12765}
12766
12767/* Parse an exception-declaration.
12768
12769 exception-declaration:
12770 type-specifier-seq declarator
12771 type-specifier-seq abstract-declarator
12772 type-specifier-seq
12773 ...
12774
12775 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12776 ellipsis variant is used. */
12777
12778static tree
94edc4ab 12779cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12780{
12781 tree type_specifiers;
12782 tree declarator;
12783 const char *saved_message;
12784
12785 /* If it's an ellipsis, it's easy to handle. */
12786 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12787 {
12788 /* Consume the `...' token. */
12789 cp_lexer_consume_token (parser->lexer);
12790 return NULL_TREE;
12791 }
12792
12793 /* Types may not be defined in exception-declarations. */
12794 saved_message = parser->type_definition_forbidden_message;
12795 parser->type_definition_forbidden_message
12796 = "types may not be defined in exception-declarations";
12797
12798 /* Parse the type-specifier-seq. */
12799 type_specifiers = cp_parser_type_specifier_seq (parser);
12800 /* If it's a `)', then there is no declarator. */
12801 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12802 declarator = NULL_TREE;
12803 else
62b8a44e 12804 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
12805 /*ctor_dtor_or_conv_p=*/NULL,
12806 /*parenthesized_p=*/NULL);
a723baf1
MM
12807
12808 /* Restore the saved message. */
12809 parser->type_definition_forbidden_message = saved_message;
12810
12811 return start_handler_parms (type_specifiers, declarator);
12812}
12813
12814/* Parse a throw-expression.
12815
12816 throw-expression:
34cd5ae7 12817 throw assignment-expression [opt]
a723baf1
MM
12818
12819 Returns a THROW_EXPR representing the throw-expression. */
12820
12821static tree
94edc4ab 12822cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12823{
12824 tree expression;
89f1a6ec 12825 cp_token* token;
a723baf1
MM
12826
12827 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
12828 token = cp_lexer_peek_token (parser->lexer);
12829 /* Figure out whether or not there is an assignment-expression
12830 following the "throw" keyword. */
12831 if (token->type == CPP_COMMA
12832 || token->type == CPP_SEMICOLON
12833 || token->type == CPP_CLOSE_PAREN
12834 || token->type == CPP_CLOSE_SQUARE
12835 || token->type == CPP_CLOSE_BRACE
12836 || token->type == CPP_COLON)
a723baf1 12837 expression = NULL_TREE;
89f1a6ec
MM
12838 else
12839 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
12840
12841 return build_throw (expression);
12842}
12843
12844/* GNU Extensions */
12845
12846/* Parse an (optional) asm-specification.
12847
12848 asm-specification:
12849 asm ( string-literal )
12850
12851 If the asm-specification is present, returns a STRING_CST
12852 corresponding to the string-literal. Otherwise, returns
12853 NULL_TREE. */
12854
12855static tree
94edc4ab 12856cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12857{
12858 cp_token *token;
12859 tree asm_specification;
12860
12861 /* Peek at the next token. */
12862 token = cp_lexer_peek_token (parser->lexer);
12863 /* If the next token isn't the `asm' keyword, then there's no
12864 asm-specification. */
12865 if (!cp_parser_is_keyword (token, RID_ASM))
12866 return NULL_TREE;
12867
12868 /* Consume the `asm' token. */
12869 cp_lexer_consume_token (parser->lexer);
12870 /* Look for the `('. */
12871 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12872
12873 /* Look for the string-literal. */
12874 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12875 if (token)
12876 asm_specification = token->value;
12877 else
12878 asm_specification = NULL_TREE;
12879
12880 /* Look for the `)'. */
12881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12882
12883 return asm_specification;
12884}
12885
12886/* Parse an asm-operand-list.
12887
12888 asm-operand-list:
12889 asm-operand
12890 asm-operand-list , asm-operand
12891
12892 asm-operand:
12893 string-literal ( expression )
12894 [ string-literal ] string-literal ( expression )
12895
12896 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12897 each node is the expression. The TREE_PURPOSE is itself a
12898 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12899 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12900 is a STRING_CST for the string literal before the parenthesis. */
12901
12902static tree
94edc4ab 12903cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12904{
12905 tree asm_operands = NULL_TREE;
12906
12907 while (true)
12908 {
12909 tree string_literal;
12910 tree expression;
12911 tree name;
12912 cp_token *token;
12913
12914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12915 {
12916 /* Consume the `[' token. */
12917 cp_lexer_consume_token (parser->lexer);
12918 /* Read the operand name. */
12919 name = cp_parser_identifier (parser);
12920 if (name != error_mark_node)
12921 name = build_string (IDENTIFIER_LENGTH (name),
12922 IDENTIFIER_POINTER (name));
12923 /* Look for the closing `]'. */
12924 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12925 }
12926 else
12927 name = NULL_TREE;
12928 /* Look for the string-literal. */
12929 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12930 string_literal = token ? token->value : error_mark_node;
12931 /* Look for the `('. */
12932 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12933 /* Parse the expression. */
12934 expression = cp_parser_expression (parser);
12935 /* Look for the `)'. */
12936 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12937 /* Add this operand to the list. */
12938 asm_operands = tree_cons (build_tree_list (name, string_literal),
12939 expression,
12940 asm_operands);
12941 /* If the next token is not a `,', there are no more
12942 operands. */
12943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12944 break;
12945 /* Consume the `,'. */
12946 cp_lexer_consume_token (parser->lexer);
12947 }
12948
12949 return nreverse (asm_operands);
12950}
12951
12952/* Parse an asm-clobber-list.
12953
12954 asm-clobber-list:
12955 string-literal
12956 asm-clobber-list , string-literal
12957
12958 Returns a TREE_LIST, indicating the clobbers in the order that they
12959 appeared. The TREE_VALUE of each node is a STRING_CST. */
12960
12961static tree
94edc4ab 12962cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
12963{
12964 tree clobbers = NULL_TREE;
12965
12966 while (true)
12967 {
12968 cp_token *token;
12969 tree string_literal;
12970
12971 /* Look for the string literal. */
12972 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12973 string_literal = token ? token->value : error_mark_node;
12974 /* Add it to the list. */
12975 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12976 /* If the next token is not a `,', then the list is
12977 complete. */
12978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12979 break;
12980 /* Consume the `,' token. */
12981 cp_lexer_consume_token (parser->lexer);
12982 }
12983
12984 return clobbers;
12985}
12986
12987/* Parse an (optional) series of attributes.
12988
12989 attributes:
12990 attributes attribute
12991
12992 attribute:
12993 __attribute__ (( attribute-list [opt] ))
12994
12995 The return value is as for cp_parser_attribute_list. */
12996
12997static tree
94edc4ab 12998cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
12999{
13000 tree attributes = NULL_TREE;
13001
13002 while (true)
13003 {
13004 cp_token *token;
13005 tree attribute_list;
13006
13007 /* Peek at the next token. */
13008 token = cp_lexer_peek_token (parser->lexer);
13009 /* If it's not `__attribute__', then we're done. */
13010 if (token->keyword != RID_ATTRIBUTE)
13011 break;
13012
13013 /* Consume the `__attribute__' keyword. */
13014 cp_lexer_consume_token (parser->lexer);
13015 /* Look for the two `(' tokens. */
13016 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13017 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13018
13019 /* Peek at the next token. */
13020 token = cp_lexer_peek_token (parser->lexer);
13021 if (token->type != CPP_CLOSE_PAREN)
13022 /* Parse the attribute-list. */
13023 attribute_list = cp_parser_attribute_list (parser);
13024 else
13025 /* If the next token is a `)', then there is no attribute
13026 list. */
13027 attribute_list = NULL;
13028
13029 /* Look for the two `)' tokens. */
13030 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13031 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13032
13033 /* Add these new attributes to the list. */
13034 attributes = chainon (attributes, attribute_list);
13035 }
13036
13037 return attributes;
13038}
13039
13040/* Parse an attribute-list.
13041
13042 attribute-list:
13043 attribute
13044 attribute-list , attribute
13045
13046 attribute:
13047 identifier
13048 identifier ( identifier )
13049 identifier ( identifier , expression-list )
13050 identifier ( expression-list )
13051
13052 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13053 TREE_PURPOSE of each node is the identifier indicating which
13054 attribute is in use. The TREE_VALUE represents the arguments, if
13055 any. */
13056
13057static tree
94edc4ab 13058cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13059{
13060 tree attribute_list = NULL_TREE;
13061
13062 while (true)
13063 {
13064 cp_token *token;
13065 tree identifier;
13066 tree attribute;
13067
13068 /* Look for the identifier. We also allow keywords here; for
13069 example `__attribute__ ((const))' is legal. */
13070 token = cp_lexer_peek_token (parser->lexer);
13071 if (token->type != CPP_NAME
13072 && token->type != CPP_KEYWORD)
13073 return error_mark_node;
13074 /* Consume the token. */
13075 token = cp_lexer_consume_token (parser->lexer);
13076
13077 /* Save away the identifier that indicates which attribute this is. */
13078 identifier = token->value;
13079 attribute = build_tree_list (identifier, NULL_TREE);
13080
13081 /* Peek at the next token. */
13082 token = cp_lexer_peek_token (parser->lexer);
13083 /* If it's an `(', then parse the attribute arguments. */
13084 if (token->type == CPP_OPEN_PAREN)
13085 {
13086 tree arguments;
a723baf1 13087
39703eb9
MM
13088 arguments = (cp_parser_parenthesized_expression_list
13089 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13090 /* Save the identifier and arguments away. */
13091 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13092 }
13093
13094 /* Add this attribute to the list. */
13095 TREE_CHAIN (attribute) = attribute_list;
13096 attribute_list = attribute;
13097
13098 /* Now, look for more attributes. */
13099 token = cp_lexer_peek_token (parser->lexer);
13100 /* If the next token isn't a `,', we're done. */
13101 if (token->type != CPP_COMMA)
13102 break;
13103
cd0be382 13104 /* Consume the comma and keep going. */
a723baf1
MM
13105 cp_lexer_consume_token (parser->lexer);
13106 }
13107
13108 /* We built up the list in reverse order. */
13109 return nreverse (attribute_list);
13110}
13111
13112/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13113 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13114 current value of the PEDANTIC flag, regardless of whether or not
13115 the `__extension__' keyword is present. The caller is responsible
13116 for restoring the value of the PEDANTIC flag. */
13117
13118static bool
94edc4ab 13119cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13120{
13121 /* Save the old value of the PEDANTIC flag. */
13122 *saved_pedantic = pedantic;
13123
13124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13125 {
13126 /* Consume the `__extension__' token. */
13127 cp_lexer_consume_token (parser->lexer);
13128 /* We're not being pedantic while the `__extension__' keyword is
13129 in effect. */
13130 pedantic = 0;
13131
13132 return true;
13133 }
13134
13135 return false;
13136}
13137
13138/* Parse a label declaration.
13139
13140 label-declaration:
13141 __label__ label-declarator-seq ;
13142
13143 label-declarator-seq:
13144 identifier , label-declarator-seq
13145 identifier */
13146
13147static void
94edc4ab 13148cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13149{
13150 /* Look for the `__label__' keyword. */
13151 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13152
13153 while (true)
13154 {
13155 tree identifier;
13156
13157 /* Look for an identifier. */
13158 identifier = cp_parser_identifier (parser);
13159 /* Declare it as a lobel. */
13160 finish_label_decl (identifier);
13161 /* If the next token is a `;', stop. */
13162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13163 break;
13164 /* Look for the `,' separating the label declarations. */
13165 cp_parser_require (parser, CPP_COMMA, "`,'");
13166 }
13167
13168 /* Look for the final `;'. */
13169 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13170}
13171
13172/* Support Functions */
13173
13174/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13175 NAME should have one of the representations used for an
13176 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13177 is returned. If PARSER->SCOPE is a dependent type, then a
13178 SCOPE_REF is returned.
13179
13180 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13181 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13182 was formed. Abstractly, such entities should not be passed to this
13183 function, because they do not need to be looked up, but it is
13184 simpler to check for this special case here, rather than at the
13185 call-sites.
13186
13187 In cases not explicitly covered above, this function returns a
13188 DECL, OVERLOAD, or baselink representing the result of the lookup.
13189 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13190 is returned.
13191
a723baf1
MM
13192 If IS_TYPE is TRUE, bindings that do not refer to types are
13193 ignored.
13194
eea9800f
MM
13195 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13196 are ignored.
13197
a723baf1
MM
13198 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13199 types. */
13200
13201static tree
8d241e0b 13202cp_parser_lookup_name (cp_parser *parser, tree name,
eea9800f 13203 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
13204{
13205 tree decl;
13206 tree object_type = parser->context->object_type;
13207
13208 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13209 no longer valid. Note that if we are parsing tentatively, and
13210 the parse fails, OBJECT_TYPE will be automatically restored. */
13211 parser->context->object_type = NULL_TREE;
13212
13213 if (name == error_mark_node)
13214 return error_mark_node;
13215
13216 /* A template-id has already been resolved; there is no lookup to
13217 do. */
13218 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13219 return name;
13220 if (BASELINK_P (name))
13221 {
13222 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13223 == TEMPLATE_ID_EXPR),
13224 20020909);
13225 return name;
13226 }
13227
13228 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13229 it should already have been checked to make sure that the name
13230 used matches the type being destroyed. */
13231 if (TREE_CODE (name) == BIT_NOT_EXPR)
13232 {
13233 tree type;
13234
13235 /* Figure out to which type this destructor applies. */
13236 if (parser->scope)
13237 type = parser->scope;
13238 else if (object_type)
13239 type = object_type;
13240 else
13241 type = current_class_type;
13242 /* If that's not a class type, there is no destructor. */
13243 if (!type || !CLASS_TYPE_P (type))
13244 return error_mark_node;
13245 /* If it was a class type, return the destructor. */
13246 return CLASSTYPE_DESTRUCTORS (type);
13247 }
13248
13249 /* By this point, the NAME should be an ordinary identifier. If
13250 the id-expression was a qualified name, the qualifying scope is
13251 stored in PARSER->SCOPE at this point. */
13252 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13253 20000619);
13254
13255 /* Perform the lookup. */
13256 if (parser->scope)
13257 {
1fb3244a 13258 bool dependent_p;
a723baf1
MM
13259
13260 if (parser->scope == error_mark_node)
13261 return error_mark_node;
13262
13263 /* If the SCOPE is dependent, the lookup must be deferred until
13264 the template is instantiated -- unless we are explicitly
13265 looking up names in uninstantiated templates. Even then, we
13266 cannot look up the name if the scope is not a class type; it
13267 might, for example, be a template type parameter. */
1fb3244a
MM
13268 dependent_p = (TYPE_P (parser->scope)
13269 && !(parser->in_declarator_p
13270 && currently_open_class (parser->scope))
13271 && dependent_type_p (parser->scope));
a723baf1 13272 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13273 && dependent_p)
a723baf1
MM
13274 {
13275 if (!is_type)
13276 decl = build_nt (SCOPE_REF, parser->scope, name);
13277 else
13278 /* The resolution to Core Issue 180 says that `struct A::B'
13279 should be considered a type-name, even if `A' is
13280 dependent. */
13281 decl = TYPE_NAME (make_typename_type (parser->scope,
13282 name,
13283 /*complain=*/1));
13284 }
13285 else
13286 {
13287 /* If PARSER->SCOPE is a dependent type, then it must be a
13288 class type, and we must not be checking dependencies;
13289 otherwise, we would have processed this lookup above. So
13290 that PARSER->SCOPE is not considered a dependent base by
13291 lookup_member, we must enter the scope here. */
1fb3244a 13292 if (dependent_p)
a723baf1
MM
13293 push_scope (parser->scope);
13294 /* If the PARSER->SCOPE is a a template specialization, it
13295 may be instantiated during name lookup. In that case,
13296 errors may be issued. Even if we rollback the current
13297 tentative parse, those errors are valid. */
5e08432e
MM
13298 decl = lookup_qualified_name (parser->scope, name, is_type,
13299 /*complain=*/true);
1fb3244a 13300 if (dependent_p)
a723baf1
MM
13301 pop_scope (parser->scope);
13302 }
13303 parser->qualifying_scope = parser->scope;
13304 parser->object_scope = NULL_TREE;
13305 }
13306 else if (object_type)
13307 {
13308 tree object_decl = NULL_TREE;
13309 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13310 OBJECT_TYPE is not a class. */
13311 if (CLASS_TYPE_P (object_type))
13312 /* If the OBJECT_TYPE is a template specialization, it may
13313 be instantiated during name lookup. In that case, errors
13314 may be issued. Even if we rollback the current tentative
13315 parse, those errors are valid. */
13316 object_decl = lookup_member (object_type,
13317 name,
13318 /*protect=*/0, is_type);
13319 /* Look it up in the enclosing context, too. */
13320 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13321 is_namespace,
a723baf1
MM
13322 /*flags=*/0);
13323 parser->object_scope = object_type;
13324 parser->qualifying_scope = NULL_TREE;
13325 if (object_decl)
13326 decl = object_decl;
13327 }
13328 else
13329 {
13330 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13331 is_namespace,
a723baf1
MM
13332 /*flags=*/0);
13333 parser->qualifying_scope = NULL_TREE;
13334 parser->object_scope = NULL_TREE;
13335 }
13336
13337 /* If the lookup failed, let our caller know. */
13338 if (!decl
13339 || decl == error_mark_node
13340 || (TREE_CODE (decl) == FUNCTION_DECL
13341 && DECL_ANTICIPATED (decl)))
13342 return error_mark_node;
13343
13344 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13345 if (TREE_CODE (decl) == TREE_LIST)
13346 {
13347 /* The error message we have to print is too complicated for
13348 cp_parser_error, so we incorporate its actions directly. */
e5976695 13349 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13350 {
13351 error ("reference to `%D' is ambiguous", name);
13352 print_candidates (decl);
13353 }
13354 return error_mark_node;
13355 }
13356
13357 my_friendly_assert (DECL_P (decl)
13358 || TREE_CODE (decl) == OVERLOAD
13359 || TREE_CODE (decl) == SCOPE_REF
13360 || BASELINK_P (decl),
13361 20000619);
13362
13363 /* If we have resolved the name of a member declaration, check to
13364 see if the declaration is accessible. When the name resolves to
34cd5ae7 13365 set of overloaded functions, accessibility is checked when
a723baf1
MM
13366 overload resolution is done.
13367
13368 During an explicit instantiation, access is not checked at all,
13369 as per [temp.explicit]. */
8d241e0b 13370 if (DECL_P (decl))
ee76b931 13371 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13372
13373 return decl;
13374}
13375
13376/* Like cp_parser_lookup_name, but for use in the typical case where
13377 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13378 TRUE. */
13379
13380static tree
94edc4ab 13381cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
13382{
13383 return cp_parser_lookup_name (parser, name,
eea9800f
MM
13384 /*is_type=*/false,
13385 /*is_namespace=*/false,
a723baf1
MM
13386 /*check_dependency=*/true);
13387}
13388
a723baf1
MM
13389/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13390 the current context, return the TYPE_DECL. If TAG_NAME_P is
13391 true, the DECL indicates the class being defined in a class-head,
13392 or declared in an elaborated-type-specifier.
13393
13394 Otherwise, return DECL. */
13395
13396static tree
13397cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13398{
710b73e6
KL
13399 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13400 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
13401
13402 struct A {
13403 template <typename T> struct B;
13404 };
13405
13406 template <typename T> struct A::B {};
13407
13408 Similarly, in a elaborated-type-specifier:
13409
13410 namespace N { struct X{}; }
13411
13412 struct A {
13413 template <typename T> friend struct N::X;
13414 };
13415
710b73e6
KL
13416 However, if the DECL refers to a class type, and we are in
13417 the scope of the class, then the name lookup automatically
13418 finds the TYPE_DECL created by build_self_reference rather
13419 than a TEMPLATE_DECL. For example, in:
13420
13421 template <class T> struct S {
13422 S s;
13423 };
13424
13425 there is no need to handle such case. */
13426
13427 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13428 return DECL_TEMPLATE_RESULT (decl);
13429
13430 return decl;
13431}
13432
13433/* If too many, or too few, template-parameter lists apply to the
13434 declarator, issue an error message. Returns TRUE if all went well,
13435 and FALSE otherwise. */
13436
13437static bool
94edc4ab
NN
13438cp_parser_check_declarator_template_parameters (cp_parser* parser,
13439 tree declarator)
a723baf1
MM
13440{
13441 unsigned num_templates;
13442
13443 /* We haven't seen any classes that involve template parameters yet. */
13444 num_templates = 0;
13445
13446 switch (TREE_CODE (declarator))
13447 {
13448 case CALL_EXPR:
13449 case ARRAY_REF:
13450 case INDIRECT_REF:
13451 case ADDR_EXPR:
13452 {
13453 tree main_declarator = TREE_OPERAND (declarator, 0);
13454 return
13455 cp_parser_check_declarator_template_parameters (parser,
13456 main_declarator);
13457 }
13458
13459 case SCOPE_REF:
13460 {
13461 tree scope;
13462 tree member;
13463
13464 scope = TREE_OPERAND (declarator, 0);
13465 member = TREE_OPERAND (declarator, 1);
13466
13467 /* If this is a pointer-to-member, then we are not interested
13468 in the SCOPE, because it does not qualify the thing that is
13469 being declared. */
13470 if (TREE_CODE (member) == INDIRECT_REF)
13471 return (cp_parser_check_declarator_template_parameters
13472 (parser, member));
13473
13474 while (scope && CLASS_TYPE_P (scope))
13475 {
13476 /* You're supposed to have one `template <...>'
13477 for every template class, but you don't need one
13478 for a full specialization. For example:
13479
13480 template <class T> struct S{};
13481 template <> struct S<int> { void f(); };
13482 void S<int>::f () {}
13483
13484 is correct; there shouldn't be a `template <>' for
13485 the definition of `S<int>::f'. */
13486 if (CLASSTYPE_TEMPLATE_INFO (scope)
13487 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13488 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13489 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13490 ++num_templates;
13491
13492 scope = TYPE_CONTEXT (scope);
13493 }
13494 }
13495
13496 /* Fall through. */
13497
13498 default:
13499 /* If the DECLARATOR has the form `X<y>' then it uses one
13500 additional level of template parameters. */
13501 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13502 ++num_templates;
13503
13504 return cp_parser_check_template_parameters (parser,
13505 num_templates);
13506 }
13507}
13508
13509/* NUM_TEMPLATES were used in the current declaration. If that is
13510 invalid, return FALSE and issue an error messages. Otherwise,
13511 return TRUE. */
13512
13513static bool
94edc4ab
NN
13514cp_parser_check_template_parameters (cp_parser* parser,
13515 unsigned num_templates)
a723baf1
MM
13516{
13517 /* If there are more template classes than parameter lists, we have
13518 something like:
13519
13520 template <class T> void S<T>::R<T>::f (); */
13521 if (parser->num_template_parameter_lists < num_templates)
13522 {
13523 error ("too few template-parameter-lists");
13524 return false;
13525 }
13526 /* If there are the same number of template classes and parameter
13527 lists, that's OK. */
13528 if (parser->num_template_parameter_lists == num_templates)
13529 return true;
13530 /* If there are more, but only one more, then we are referring to a
13531 member template. That's OK too. */
13532 if (parser->num_template_parameter_lists == num_templates + 1)
13533 return true;
13534 /* Otherwise, there are too many template parameter lists. We have
13535 something like:
13536
13537 template <class T> template <class U> void S::f(); */
13538 error ("too many template-parameter-lists");
13539 return false;
13540}
13541
13542/* Parse a binary-expression of the general form:
13543
13544 binary-expression:
13545 <expr>
13546 binary-expression <token> <expr>
13547
13548 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13549 to parser the <expr>s. If the first production is used, then the
13550 value returned by FN is returned directly. Otherwise, a node with
13551 the indicated EXPR_TYPE is returned, with operands corresponding to
13552 the two sub-expressions. */
13553
13554static tree
94edc4ab
NN
13555cp_parser_binary_expression (cp_parser* parser,
13556 const cp_parser_token_tree_map token_tree_map,
13557 cp_parser_expression_fn fn)
a723baf1
MM
13558{
13559 tree lhs;
13560
13561 /* Parse the first expression. */
13562 lhs = (*fn) (parser);
13563 /* Now, look for more expressions. */
13564 while (true)
13565 {
13566 cp_token *token;
39b1af70 13567 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13568 tree rhs;
13569
13570 /* Peek at the next token. */
13571 token = cp_lexer_peek_token (parser->lexer);
13572 /* If the token is `>', and that's not an operator at the
13573 moment, then we're done. */
13574 if (token->type == CPP_GREATER
13575 && !parser->greater_than_is_operator_p)
13576 break;
34cd5ae7 13577 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13578 tree representation. */
13579 for (map_node = token_tree_map;
13580 map_node->token_type != CPP_EOF;
13581 ++map_node)
13582 if (map_node->token_type == token->type)
13583 {
13584 /* Consume the operator token. */
13585 cp_lexer_consume_token (parser->lexer);
13586 /* Parse the right-hand side of the expression. */
13587 rhs = (*fn) (parser);
13588 /* Build the binary tree node. */
13589 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13590 break;
13591 }
13592
13593 /* If the token wasn't one of the ones we want, we're done. */
13594 if (map_node->token_type == CPP_EOF)
13595 break;
13596 }
13597
13598 return lhs;
13599}
13600
13601/* Parse an optional `::' token indicating that the following name is
13602 from the global namespace. If so, PARSER->SCOPE is set to the
13603 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13604 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13605 Returns the new value of PARSER->SCOPE, if the `::' token is
13606 present, and NULL_TREE otherwise. */
13607
13608static tree
94edc4ab 13609cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13610{
13611 cp_token *token;
13612
13613 /* Peek at the next token. */
13614 token = cp_lexer_peek_token (parser->lexer);
13615 /* If we're looking at a `::' token then we're starting from the
13616 global namespace, not our current location. */
13617 if (token->type == CPP_SCOPE)
13618 {
13619 /* Consume the `::' token. */
13620 cp_lexer_consume_token (parser->lexer);
13621 /* Set the SCOPE so that we know where to start the lookup. */
13622 parser->scope = global_namespace;
13623 parser->qualifying_scope = global_namespace;
13624 parser->object_scope = NULL_TREE;
13625
13626 return parser->scope;
13627 }
13628 else if (!current_scope_valid_p)
13629 {
13630 parser->scope = NULL_TREE;
13631 parser->qualifying_scope = NULL_TREE;
13632 parser->object_scope = NULL_TREE;
13633 }
13634
13635 return NULL_TREE;
13636}
13637
13638/* Returns TRUE if the upcoming token sequence is the start of a
13639 constructor declarator. If FRIEND_P is true, the declarator is
13640 preceded by the `friend' specifier. */
13641
13642static bool
13643cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13644{
13645 bool constructor_p;
13646 tree type_decl = NULL_TREE;
13647 bool nested_name_p;
2050a1bb
MM
13648 cp_token *next_token;
13649
13650 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13651 try to avoid doing lots of work if at all possible. It's not
13652 valid declare a constructor at function scope. */
13653 if (at_function_scope_p ())
13654 return false;
13655 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13656 next_token = cp_lexer_peek_token (parser->lexer);
13657 if (next_token->type != CPP_NAME
13658 && next_token->type != CPP_SCOPE
13659 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13660 && next_token->type != CPP_TEMPLATE_ID)
13661 return false;
a723baf1
MM
13662
13663 /* Parse tentatively; we are going to roll back all of the tokens
13664 consumed here. */
13665 cp_parser_parse_tentatively (parser);
13666 /* Assume that we are looking at a constructor declarator. */
13667 constructor_p = true;
8d241e0b 13668
a723baf1
MM
13669 /* Look for the optional `::' operator. */
13670 cp_parser_global_scope_opt (parser,
13671 /*current_scope_valid_p=*/false);
13672 /* Look for the nested-name-specifier. */
13673 nested_name_p
13674 = (cp_parser_nested_name_specifier_opt (parser,
13675 /*typename_keyword_p=*/false,
13676 /*check_dependency_p=*/false,
a668c6ad
MM
13677 /*type_p=*/false,
13678 /*is_declaration=*/false)
a723baf1
MM
13679 != NULL_TREE);
13680 /* Outside of a class-specifier, there must be a
13681 nested-name-specifier. */
13682 if (!nested_name_p &&
13683 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13684 || friend_p))
13685 constructor_p = false;
13686 /* If we still think that this might be a constructor-declarator,
13687 look for a class-name. */
13688 if (constructor_p)
13689 {
13690 /* If we have:
13691
8fbc5ae7 13692 template <typename T> struct S { S(); };
a723baf1
MM
13693 template <typename T> S<T>::S ();
13694
13695 we must recognize that the nested `S' names a class.
13696 Similarly, for:
13697
13698 template <typename T> S<T>::S<T> ();
13699
13700 we must recognize that the nested `S' names a template. */
13701 type_decl = cp_parser_class_name (parser,
13702 /*typename_keyword_p=*/false,
13703 /*template_keyword_p=*/false,
13704 /*type_p=*/false,
a723baf1 13705 /*check_dependency_p=*/false,
a668c6ad
MM
13706 /*class_head_p=*/false,
13707 /*is_declaration=*/false);
a723baf1
MM
13708 /* If there was no class-name, then this is not a constructor. */
13709 constructor_p = !cp_parser_error_occurred (parser);
13710 }
8d241e0b 13711
a723baf1
MM
13712 /* If we're still considering a constructor, we have to see a `(',
13713 to begin the parameter-declaration-clause, followed by either a
13714 `)', an `...', or a decl-specifier. We need to check for a
13715 type-specifier to avoid being fooled into thinking that:
13716
13717 S::S (f) (int);
13718
13719 is a constructor. (It is actually a function named `f' that
13720 takes one parameter (of type `int') and returns a value of type
13721 `S::S'. */
13722 if (constructor_p
13723 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13724 {
13725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13726 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13727 && !cp_parser_storage_class_specifier_opt (parser))
13728 {
5dae1114 13729 tree type;
4047b164 13730 unsigned saved_num_template_parameter_lists;
5dae1114
MM
13731
13732 /* Names appearing in the type-specifier should be looked up
13733 in the scope of the class. */
13734 if (current_class_type)
13735 type = NULL_TREE;
a723baf1
MM
13736 else
13737 {
5dae1114
MM
13738 type = TREE_TYPE (type_decl);
13739 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13740 {
13741 type = resolve_typename_type (type,
13742 /*only_current_p=*/false);
13743 if (type == error_mark_node)
13744 {
13745 cp_parser_abort_tentative_parse (parser);
13746 return false;
13747 }
13748 }
5dae1114 13749 push_scope (type);
a723baf1 13750 }
4047b164
KL
13751
13752 /* Inside the constructor parameter list, surrounding
13753 template-parameter-lists do not apply. */
13754 saved_num_template_parameter_lists
13755 = parser->num_template_parameter_lists;
13756 parser->num_template_parameter_lists = 0;
13757
5dae1114
MM
13758 /* Look for the type-specifier. */
13759 cp_parser_type_specifier (parser,
13760 CP_PARSER_FLAGS_NONE,
13761 /*is_friend=*/false,
13762 /*is_declarator=*/true,
13763 /*declares_class_or_enum=*/NULL,
13764 /*is_cv_qualifier=*/NULL);
4047b164
KL
13765
13766 parser->num_template_parameter_lists
13767 = saved_num_template_parameter_lists;
13768
5dae1114
MM
13769 /* Leave the scope of the class. */
13770 if (type)
13771 pop_scope (type);
13772
13773 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13774 }
13775 }
13776 else
13777 constructor_p = false;
13778 /* We did not really want to consume any tokens. */
13779 cp_parser_abort_tentative_parse (parser);
13780
13781 return constructor_p;
13782}
13783
13784/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13785 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13786 they must be performed once we are in the scope of the function.
13787
13788 Returns the function defined. */
13789
13790static tree
13791cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13792 (cp_parser* parser,
13793 tree decl_specifiers,
13794 tree attributes,
13795 tree declarator)
a723baf1
MM
13796{
13797 tree fn;
13798 bool success_p;
13799
13800 /* Begin the function-definition. */
13801 success_p = begin_function_definition (decl_specifiers,
13802 attributes,
13803 declarator);
13804
13805 /* If there were names looked up in the decl-specifier-seq that we
13806 did not check, check them now. We must wait until we are in the
13807 scope of the function to perform the checks, since the function
13808 might be a friend. */
cf22909c 13809 perform_deferred_access_checks ();
a723baf1
MM
13810
13811 if (!success_p)
13812 {
13813 /* If begin_function_definition didn't like the definition, skip
13814 the entire function. */
13815 error ("invalid function declaration");
13816 cp_parser_skip_to_end_of_block_or_statement (parser);
13817 fn = error_mark_node;
13818 }
13819 else
13820 fn = cp_parser_function_definition_after_declarator (parser,
13821 /*inline_p=*/false);
13822
13823 return fn;
13824}
13825
13826/* Parse the part of a function-definition that follows the
13827 declarator. INLINE_P is TRUE iff this function is an inline
13828 function defined with a class-specifier.
13829
13830 Returns the function defined. */
13831
13832static tree
94edc4ab
NN
13833cp_parser_function_definition_after_declarator (cp_parser* parser,
13834 bool inline_p)
a723baf1
MM
13835{
13836 tree fn;
13837 bool ctor_initializer_p = false;
13838 bool saved_in_unbraced_linkage_specification_p;
13839 unsigned saved_num_template_parameter_lists;
13840
13841 /* If the next token is `return', then the code may be trying to
13842 make use of the "named return value" extension that G++ used to
13843 support. */
13844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13845 {
13846 /* Consume the `return' keyword. */
13847 cp_lexer_consume_token (parser->lexer);
13848 /* Look for the identifier that indicates what value is to be
13849 returned. */
13850 cp_parser_identifier (parser);
13851 /* Issue an error message. */
13852 error ("named return values are no longer supported");
13853 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
13854 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13855 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
13856 cp_lexer_consume_token (parser->lexer);
13857 }
13858 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13859 anything declared inside `f'. */
13860 saved_in_unbraced_linkage_specification_p
13861 = parser->in_unbraced_linkage_specification_p;
13862 parser->in_unbraced_linkage_specification_p = false;
13863 /* Inside the function, surrounding template-parameter-lists do not
13864 apply. */
13865 saved_num_template_parameter_lists
13866 = parser->num_template_parameter_lists;
13867 parser->num_template_parameter_lists = 0;
13868 /* If the next token is `try', then we are looking at a
13869 function-try-block. */
13870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13871 ctor_initializer_p = cp_parser_function_try_block (parser);
13872 /* A function-try-block includes the function-body, so we only do
13873 this next part if we're not processing a function-try-block. */
13874 else
13875 ctor_initializer_p
13876 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13877
13878 /* Finish the function. */
13879 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13880 (inline_p ? 2 : 0));
13881 /* Generate code for it, if necessary. */
8cd2462c 13882 expand_or_defer_fn (fn);
a723baf1
MM
13883 /* Restore the saved values. */
13884 parser->in_unbraced_linkage_specification_p
13885 = saved_in_unbraced_linkage_specification_p;
13886 parser->num_template_parameter_lists
13887 = saved_num_template_parameter_lists;
13888
13889 return fn;
13890}
13891
13892/* Parse a template-declaration, assuming that the `export' (and
13893 `extern') keywords, if present, has already been scanned. MEMBER_P
13894 is as for cp_parser_template_declaration. */
13895
13896static void
94edc4ab 13897cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13898{
13899 tree decl = NULL_TREE;
13900 tree parameter_list;
13901 bool friend_p = false;
13902
13903 /* Look for the `template' keyword. */
13904 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13905 return;
13906
13907 /* And the `<'. */
13908 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13909 return;
13910
13911 /* Parse the template parameters. */
13912 begin_template_parm_list ();
13913 /* If the next token is `>', then we have an invalid
13914 specialization. Rather than complain about an invalid template
13915 parameter, issue an error message here. */
13916 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13917 {
13918 cp_parser_error (parser, "invalid explicit specialization");
13919 parameter_list = NULL_TREE;
13920 }
13921 else
13922 parameter_list = cp_parser_template_parameter_list (parser);
13923 parameter_list = end_template_parm_list (parameter_list);
13924 /* Look for the `>'. */
13925 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13926 /* We just processed one more parameter list. */
13927 ++parser->num_template_parameter_lists;
13928 /* If the next token is `template', there are more template
13929 parameters. */
13930 if (cp_lexer_next_token_is_keyword (parser->lexer,
13931 RID_TEMPLATE))
13932 cp_parser_template_declaration_after_export (parser, member_p);
13933 else
13934 {
13935 decl = cp_parser_single_declaration (parser,
13936 member_p,
13937 &friend_p);
13938
13939 /* If this is a member template declaration, let the front
13940 end know. */
13941 if (member_p && !friend_p && decl)
37d407a1
KL
13942 {
13943 if (TREE_CODE (decl) == TYPE_DECL)
13944 cp_parser_check_access_in_redeclaration (decl);
13945
13946 decl = finish_member_template_decl (decl);
13947 }
a723baf1 13948 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
13949 make_friend_class (current_class_type, TREE_TYPE (decl),
13950 /*complain=*/true);
a723baf1
MM
13951 }
13952 /* We are done with the current parameter list. */
13953 --parser->num_template_parameter_lists;
13954
13955 /* Finish up. */
13956 finish_template_decl (parameter_list);
13957
13958 /* Register member declarations. */
13959 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13960 finish_member_declaration (decl);
13961
13962 /* If DECL is a function template, we must return to parse it later.
13963 (Even though there is no definition, there might be default
13964 arguments that need handling.) */
13965 if (member_p && decl
13966 && (TREE_CODE (decl) == FUNCTION_DECL
13967 || DECL_FUNCTION_TEMPLATE_P (decl)))
13968 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 13969 = tree_cons (NULL_TREE, decl,
a723baf1
MM
13970 TREE_VALUE (parser->unparsed_functions_queues));
13971}
13972
13973/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13974 `function-definition' sequence. MEMBER_P is true, this declaration
13975 appears in a class scope.
13976
13977 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13978 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13979
13980static tree
94edc4ab
NN
13981cp_parser_single_declaration (cp_parser* parser,
13982 bool member_p,
13983 bool* friend_p)
a723baf1 13984{
560ad596 13985 int declares_class_or_enum;
a723baf1
MM
13986 tree decl = NULL_TREE;
13987 tree decl_specifiers;
13988 tree attributes;
4bb8ca28 13989 bool function_definition_p = false;
a723baf1 13990
a723baf1 13991 /* Defer access checks until we know what is being declared. */
8d241e0b 13992 push_deferring_access_checks (dk_deferred);
cf22909c 13993
a723baf1
MM
13994 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13995 alternative. */
13996 decl_specifiers
13997 = cp_parser_decl_specifier_seq (parser,
13998 CP_PARSER_FLAGS_OPTIONAL,
13999 &attributes,
14000 &declares_class_or_enum);
4bb8ca28
MM
14001 if (friend_p)
14002 *friend_p = cp_parser_friend_p (decl_specifiers);
a723baf1
MM
14003 /* Gather up the access checks that occurred the
14004 decl-specifier-seq. */
cf22909c
KL
14005 stop_deferring_access_checks ();
14006
a723baf1
MM
14007 /* Check for the declaration of a template class. */
14008 if (declares_class_or_enum)
14009 {
14010 if (cp_parser_declares_only_class_p (parser))
14011 {
14012 decl = shadow_tag (decl_specifiers);
14013 if (decl)
14014 decl = TYPE_NAME (decl);
14015 else
14016 decl = error_mark_node;
14017 }
14018 }
14019 else
14020 decl = NULL_TREE;
14021 /* If it's not a template class, try for a template function. If
14022 the next token is a `;', then this declaration does not declare
14023 anything. But, if there were errors in the decl-specifiers, then
14024 the error might well have come from an attempted class-specifier.
14025 In that case, there's no need to warn about a missing declarator. */
14026 if (!decl
14027 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14028 || !value_member (error_mark_node, decl_specifiers)))
14029 decl = cp_parser_init_declarator (parser,
14030 decl_specifiers,
14031 attributes,
4bb8ca28 14032 /*function_definition_allowed_p=*/true,
a723baf1 14033 member_p,
560ad596 14034 declares_class_or_enum,
4bb8ca28 14035 &function_definition_p);
cf22909c
KL
14036
14037 pop_deferring_access_checks ();
14038
a723baf1
MM
14039 /* Clear any current qualification; whatever comes next is the start
14040 of something new. */
14041 parser->scope = NULL_TREE;
14042 parser->qualifying_scope = NULL_TREE;
14043 parser->object_scope = NULL_TREE;
14044 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14045 if (!function_definition_p
14046 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14047 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14048
14049 return decl;
14050}
14051
d6b4ea85
MM
14052/* Parse a cast-expression that is not the operand of a unary "&". */
14053
14054static tree
14055cp_parser_simple_cast_expression (cp_parser *parser)
14056{
14057 return cp_parser_cast_expression (parser, /*address_p=*/false);
14058}
14059
a723baf1
MM
14060/* Parse a functional cast to TYPE. Returns an expression
14061 representing the cast. */
14062
14063static tree
94edc4ab 14064cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14065{
14066 tree expression_list;
14067
39703eb9
MM
14068 expression_list
14069 = cp_parser_parenthesized_expression_list (parser, false,
14070 /*non_constant_p=*/NULL);
a723baf1
MM
14071
14072 return build_functional_cast (type, expression_list);
14073}
14074
4bb8ca28
MM
14075/* Save the tokens that make up the body of a member function defined
14076 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14077 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14078 specifiers applied to the declaration. Returns the FUNCTION_DECL
14079 for the member function. */
14080
14081tree
14082cp_parser_save_member_function_body (cp_parser* parser,
14083 tree decl_specifiers,
14084 tree declarator,
14085 tree attributes)
14086{
14087 cp_token_cache *cache;
14088 tree fn;
14089
14090 /* Create the function-declaration. */
14091 fn = start_method (decl_specifiers, declarator, attributes);
14092 /* If something went badly wrong, bail out now. */
14093 if (fn == error_mark_node)
14094 {
14095 /* If there's a function-body, skip it. */
14096 if (cp_parser_token_starts_function_definition_p
14097 (cp_lexer_peek_token (parser->lexer)))
14098 cp_parser_skip_to_end_of_block_or_statement (parser);
14099 return error_mark_node;
14100 }
14101
14102 /* Remember it, if there default args to post process. */
14103 cp_parser_save_default_args (parser, fn);
14104
14105 /* Create a token cache. */
14106 cache = cp_token_cache_new ();
14107 /* Save away the tokens that make up the body of the
14108 function. */
14109 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14110 /* Handle function try blocks. */
14111 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14112 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14113
14114 /* Save away the inline definition; we will process it when the
14115 class is complete. */
14116 DECL_PENDING_INLINE_INFO (fn) = cache;
14117 DECL_PENDING_INLINE_P (fn) = 1;
14118
14119 /* We need to know that this was defined in the class, so that
14120 friend templates are handled correctly. */
14121 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14122
14123 /* We're done with the inline definition. */
14124 finish_method (fn);
14125
14126 /* Add FN to the queue of functions to be parsed later. */
14127 TREE_VALUE (parser->unparsed_functions_queues)
14128 = tree_cons (NULL_TREE, fn,
14129 TREE_VALUE (parser->unparsed_functions_queues));
14130
14131 return fn;
14132}
14133
ec75414f
MM
14134/* Parse a template-argument-list, as well as the trailing ">" (but
14135 not the opening ">"). See cp_parser_template_argument_list for the
14136 return value. */
14137
14138static tree
14139cp_parser_enclosed_template_argument_list (cp_parser* parser)
14140{
14141 tree arguments;
14142 tree saved_scope;
14143 tree saved_qualifying_scope;
14144 tree saved_object_scope;
14145 bool saved_greater_than_is_operator_p;
14146
14147 /* [temp.names]
14148
14149 When parsing a template-id, the first non-nested `>' is taken as
14150 the end of the template-argument-list rather than a greater-than
14151 operator. */
14152 saved_greater_than_is_operator_p
14153 = parser->greater_than_is_operator_p;
14154 parser->greater_than_is_operator_p = false;
14155 /* Parsing the argument list may modify SCOPE, so we save it
14156 here. */
14157 saved_scope = parser->scope;
14158 saved_qualifying_scope = parser->qualifying_scope;
14159 saved_object_scope = parser->object_scope;
14160 /* Parse the template-argument-list itself. */
14161 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14162 arguments = NULL_TREE;
14163 else
14164 arguments = cp_parser_template_argument_list (parser);
14165 /* Look for the `>' that ends the template-argument-list. */
14166 cp_parser_require (parser, CPP_GREATER, "`>'");
14167 /* The `>' token might be a greater-than operator again now. */
14168 parser->greater_than_is_operator_p
14169 = saved_greater_than_is_operator_p;
14170 /* Restore the SAVED_SCOPE. */
14171 parser->scope = saved_scope;
14172 parser->qualifying_scope = saved_qualifying_scope;
14173 parser->object_scope = saved_object_scope;
14174
14175 return arguments;
14176}
14177
a723baf1
MM
14178/* MEMBER_FUNCTION is a member function, or a friend. If default
14179 arguments, or the body of the function have not yet been parsed,
14180 parse them now. */
14181
14182static void
94edc4ab 14183cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14184{
14185 cp_lexer *saved_lexer;
14186
14187 /* If this member is a template, get the underlying
14188 FUNCTION_DECL. */
14189 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14190 member_function = DECL_TEMPLATE_RESULT (member_function);
14191
14192 /* There should not be any class definitions in progress at this
14193 point; the bodies of members are only parsed outside of all class
14194 definitions. */
14195 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14196 /* While we're parsing the member functions we might encounter more
14197 classes. We want to handle them right away, but we don't want
14198 them getting mixed up with functions that are currently in the
14199 queue. */
14200 parser->unparsed_functions_queues
14201 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14202
14203 /* Make sure that any template parameters are in scope. */
14204 maybe_begin_member_template_processing (member_function);
14205
a723baf1
MM
14206 /* If the body of the function has not yet been parsed, parse it
14207 now. */
14208 if (DECL_PENDING_INLINE_P (member_function))
14209 {
14210 tree function_scope;
14211 cp_token_cache *tokens;
14212
14213 /* The function is no longer pending; we are processing it. */
14214 tokens = DECL_PENDING_INLINE_INFO (member_function);
14215 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14216 DECL_PENDING_INLINE_P (member_function) = 0;
14217 /* If this was an inline function in a local class, enter the scope
14218 of the containing function. */
14219 function_scope = decl_function_context (member_function);
14220 if (function_scope)
14221 push_function_context_to (function_scope);
14222
14223 /* Save away the current lexer. */
14224 saved_lexer = parser->lexer;
14225 /* Make a new lexer to feed us the tokens saved for this function. */
14226 parser->lexer = cp_lexer_new_from_tokens (tokens);
14227 parser->lexer->next = saved_lexer;
14228
14229 /* Set the current source position to be the location of the first
14230 token in the saved inline body. */
3466b292 14231 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14232
14233 /* Let the front end know that we going to be defining this
14234 function. */
14235 start_function (NULL_TREE, member_function, NULL_TREE,
14236 SF_PRE_PARSED | SF_INCLASS_INLINE);
14237
14238 /* Now, parse the body of the function. */
14239 cp_parser_function_definition_after_declarator (parser,
14240 /*inline_p=*/true);
14241
14242 /* Leave the scope of the containing function. */
14243 if (function_scope)
14244 pop_function_context_from (function_scope);
14245 /* Restore the lexer. */
14246 parser->lexer = saved_lexer;
14247 }
14248
14249 /* Remove any template parameters from the symbol table. */
14250 maybe_end_member_template_processing ();
14251
14252 /* Restore the queue. */
14253 parser->unparsed_functions_queues
14254 = TREE_CHAIN (parser->unparsed_functions_queues);
14255}
14256
cd0be382 14257/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
14258 functions queue. */
14259
14260static void
14261cp_parser_save_default_args (cp_parser* parser, tree decl)
14262{
14263 tree probe;
14264
14265 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14266 probe;
14267 probe = TREE_CHAIN (probe))
14268 if (TREE_PURPOSE (probe))
14269 {
14270 TREE_PURPOSE (parser->unparsed_functions_queues)
14271 = tree_cons (NULL_TREE, decl,
14272 TREE_PURPOSE (parser->unparsed_functions_queues));
14273 break;
14274 }
14275 return;
14276}
14277
8218bd34
MM
14278/* FN is a FUNCTION_DECL which may contains a parameter with an
14279 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14280
14281static void
8218bd34 14282cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14283{
14284 cp_lexer *saved_lexer;
14285 cp_token_cache *tokens;
14286 bool saved_local_variables_forbidden_p;
14287 tree parameters;
8218bd34 14288
b92bc2a0
NS
14289 /* While we're parsing the default args, we might (due to the
14290 statement expression extension) encounter more classes. We want
14291 to handle them right away, but we don't want them getting mixed
14292 up with default args that are currently in the queue. */
14293 parser->unparsed_functions_queues
14294 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14295
8218bd34 14296 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14297 parameters;
14298 parameters = TREE_CHAIN (parameters))
14299 {
14300 if (!TREE_PURPOSE (parameters)
14301 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14302 continue;
14303
14304 /* Save away the current lexer. */
14305 saved_lexer = parser->lexer;
14306 /* Create a new one, using the tokens we have saved. */
14307 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14308 parser->lexer = cp_lexer_new_from_tokens (tokens);
14309
14310 /* Set the current source position to be the location of the
14311 first token in the default argument. */
3466b292 14312 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14313
14314 /* Local variable names (and the `this' keyword) may not appear
14315 in a default argument. */
14316 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14317 parser->local_variables_forbidden_p = true;
14318 /* Parse the assignment-expression. */
f128e1f3 14319 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 14320 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14321 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 14322 if (DECL_CLASS_SCOPE_P (fn))
e5976695 14323 pop_nested_class ();
a723baf1
MM
14324
14325 /* Restore saved state. */
14326 parser->lexer = saved_lexer;
14327 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14328 }
b92bc2a0
NS
14329
14330 /* Restore the queue. */
14331 parser->unparsed_functions_queues
14332 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
14333}
14334
14335/* Parse the operand of `sizeof' (or a similar operator). Returns
14336 either a TYPE or an expression, depending on the form of the
14337 input. The KEYWORD indicates which kind of expression we have
14338 encountered. */
14339
14340static tree
94edc4ab 14341cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14342{
14343 static const char *format;
14344 tree expr = NULL_TREE;
14345 const char *saved_message;
14346 bool saved_constant_expression_p;
14347
14348 /* Initialize FORMAT the first time we get here. */
14349 if (!format)
14350 format = "types may not be defined in `%s' expressions";
14351
14352 /* Types cannot be defined in a `sizeof' expression. Save away the
14353 old message. */
14354 saved_message = parser->type_definition_forbidden_message;
14355 /* And create the new one. */
14356 parser->type_definition_forbidden_message
c68b0a84
KG
14357 = xmalloc (strlen (format)
14358 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14359 + 1 /* `\0' */);
a723baf1
MM
14360 sprintf ((char *) parser->type_definition_forbidden_message,
14361 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14362
14363 /* The restrictions on constant-expressions do not apply inside
14364 sizeof expressions. */
14365 saved_constant_expression_p = parser->constant_expression_p;
14366 parser->constant_expression_p = false;
14367
3beb3abf
MM
14368 /* Do not actually evaluate the expression. */
14369 ++skip_evaluation;
a723baf1
MM
14370 /* If it's a `(', then we might be looking at the type-id
14371 construction. */
14372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14373 {
14374 tree type;
14375
14376 /* We can't be sure yet whether we're looking at a type-id or an
14377 expression. */
14378 cp_parser_parse_tentatively (parser);
14379 /* Consume the `('. */
14380 cp_lexer_consume_token (parser->lexer);
14381 /* Parse the type-id. */
14382 type = cp_parser_type_id (parser);
14383 /* Now, look for the trailing `)'. */
14384 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14385 /* If all went well, then we're done. */
14386 if (cp_parser_parse_definitely (parser))
14387 {
14388 /* Build a list of decl-specifiers; right now, we have only
14389 a single type-specifier. */
14390 type = build_tree_list (NULL_TREE,
14391 type);
14392
14393 /* Call grokdeclarator to figure out what type this is. */
14394 expr = grokdeclarator (NULL_TREE,
14395 type,
14396 TYPENAME,
14397 /*initialized=*/0,
14398 /*attrlist=*/NULL);
14399 }
14400 }
14401
14402 /* If the type-id production did not work out, then we must be
14403 looking at the unary-expression production. */
14404 if (!expr)
14405 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14406 /* Go back to evaluating expressions. */
14407 --skip_evaluation;
a723baf1
MM
14408
14409 /* Free the message we created. */
14410 free ((char *) parser->type_definition_forbidden_message);
14411 /* And restore the old one. */
14412 parser->type_definition_forbidden_message = saved_message;
14413 parser->constant_expression_p = saved_constant_expression_p;
14414
14415 return expr;
14416}
14417
14418/* If the current declaration has no declarator, return true. */
14419
14420static bool
14421cp_parser_declares_only_class_p (cp_parser *parser)
14422{
14423 /* If the next token is a `;' or a `,' then there is no
14424 declarator. */
14425 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14426 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14427}
14428
d17811fd
MM
14429/* Simplify EXPR if it is a non-dependent expression. Returns the
14430 (possibly simplified) expression. */
14431
14432static tree
14433cp_parser_fold_non_dependent_expr (tree expr)
14434{
14435 /* If we're in a template, but EXPR isn't value dependent, simplify
14436 it. We're supposed to treat:
14437
14438 template <typename T> void f(T[1 + 1]);
14439 template <typename T> void f(T[2]);
14440
14441 as two declarations of the same function, for example. */
14442 if (processing_template_decl
14443 && !type_dependent_expression_p (expr)
14444 && !value_dependent_expression_p (expr))
14445 {
14446 HOST_WIDE_INT saved_processing_template_decl;
14447
14448 saved_processing_template_decl = processing_template_decl;
14449 processing_template_decl = 0;
14450 expr = tsubst_copy_and_build (expr,
14451 /*args=*/NULL_TREE,
14452 tf_error,
b3445994
MM
14453 /*in_decl=*/NULL_TREE,
14454 /*function_p=*/false);
d17811fd
MM
14455 processing_template_decl = saved_processing_template_decl;
14456 }
14457 return expr;
14458}
14459
a723baf1
MM
14460/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14461 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14462
14463static bool
94edc4ab 14464cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14465{
14466 while (decl_specifiers)
14467 {
14468 /* See if this decl-specifier is `friend'. */
14469 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14470 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14471 return true;
14472
14473 /* Go on to the next decl-specifier. */
14474 decl_specifiers = TREE_CHAIN (decl_specifiers);
14475 }
14476
14477 return false;
14478}
14479
14480/* If the next token is of the indicated TYPE, consume it. Otherwise,
14481 issue an error message indicating that TOKEN_DESC was expected.
14482
14483 Returns the token consumed, if the token had the appropriate type.
14484 Otherwise, returns NULL. */
14485
14486static cp_token *
94edc4ab
NN
14487cp_parser_require (cp_parser* parser,
14488 enum cpp_ttype type,
14489 const char* token_desc)
a723baf1
MM
14490{
14491 if (cp_lexer_next_token_is (parser->lexer, type))
14492 return cp_lexer_consume_token (parser->lexer);
14493 else
14494 {
e5976695
MM
14495 /* Output the MESSAGE -- unless we're parsing tentatively. */
14496 if (!cp_parser_simulate_error (parser))
14497 error ("expected %s", token_desc);
a723baf1
MM
14498 return NULL;
14499 }
14500}
14501
14502/* Like cp_parser_require, except that tokens will be skipped until
14503 the desired token is found. An error message is still produced if
14504 the next token is not as expected. */
14505
14506static void
94edc4ab
NN
14507cp_parser_skip_until_found (cp_parser* parser,
14508 enum cpp_ttype type,
14509 const char* token_desc)
a723baf1
MM
14510{
14511 cp_token *token;
14512 unsigned nesting_depth = 0;
14513
14514 if (cp_parser_require (parser, type, token_desc))
14515 return;
14516
14517 /* Skip tokens until the desired token is found. */
14518 while (true)
14519 {
14520 /* Peek at the next token. */
14521 token = cp_lexer_peek_token (parser->lexer);
14522 /* If we've reached the token we want, consume it and
14523 stop. */
14524 if (token->type == type && !nesting_depth)
14525 {
14526 cp_lexer_consume_token (parser->lexer);
14527 return;
14528 }
14529 /* If we've run out of tokens, stop. */
14530 if (token->type == CPP_EOF)
14531 return;
14532 if (token->type == CPP_OPEN_BRACE
14533 || token->type == CPP_OPEN_PAREN
14534 || token->type == CPP_OPEN_SQUARE)
14535 ++nesting_depth;
14536 else if (token->type == CPP_CLOSE_BRACE
14537 || token->type == CPP_CLOSE_PAREN
14538 || token->type == CPP_CLOSE_SQUARE)
14539 {
14540 if (nesting_depth-- == 0)
14541 return;
14542 }
14543 /* Consume this token. */
14544 cp_lexer_consume_token (parser->lexer);
14545 }
14546}
14547
14548/* If the next token is the indicated keyword, consume it. Otherwise,
14549 issue an error message indicating that TOKEN_DESC was expected.
14550
14551 Returns the token consumed, if the token had the appropriate type.
14552 Otherwise, returns NULL. */
14553
14554static cp_token *
94edc4ab
NN
14555cp_parser_require_keyword (cp_parser* parser,
14556 enum rid keyword,
14557 const char* token_desc)
a723baf1
MM
14558{
14559 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14560
14561 if (token && token->keyword != keyword)
14562 {
14563 dyn_string_t error_msg;
14564
14565 /* Format the error message. */
14566 error_msg = dyn_string_new (0);
14567 dyn_string_append_cstr (error_msg, "expected ");
14568 dyn_string_append_cstr (error_msg, token_desc);
14569 cp_parser_error (parser, error_msg->s);
14570 dyn_string_delete (error_msg);
14571 return NULL;
14572 }
14573
14574 return token;
14575}
14576
14577/* Returns TRUE iff TOKEN is a token that can begin the body of a
14578 function-definition. */
14579
14580static bool
94edc4ab 14581cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14582{
14583 return (/* An ordinary function-body begins with an `{'. */
14584 token->type == CPP_OPEN_BRACE
14585 /* A ctor-initializer begins with a `:'. */
14586 || token->type == CPP_COLON
14587 /* A function-try-block begins with `try'. */
14588 || token->keyword == RID_TRY
14589 /* The named return value extension begins with `return'. */
14590 || token->keyword == RID_RETURN);
14591}
14592
14593/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14594 definition. */
14595
14596static bool
14597cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14598{
14599 cp_token *token;
14600
14601 token = cp_lexer_peek_token (parser->lexer);
14602 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14603}
14604
d17811fd
MM
14605/* Returns TRUE iff the next token is the "," or ">" ending a
14606 template-argument. */
14607
14608static bool
14609cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14610{
14611 cp_token *token;
14612
14613 token = cp_lexer_peek_token (parser->lexer);
14614 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14615}
14616
a723baf1
MM
14617/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14618 or none_type otherwise. */
14619
14620static enum tag_types
94edc4ab 14621cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14622{
14623 switch (token->keyword)
14624 {
14625 case RID_CLASS:
14626 return class_type;
14627 case RID_STRUCT:
14628 return record_type;
14629 case RID_UNION:
14630 return union_type;
14631
14632 default:
14633 return none_type;
14634 }
14635}
14636
14637/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14638
14639static void
14640cp_parser_check_class_key (enum tag_types class_key, tree type)
14641{
14642 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14643 pedwarn ("`%s' tag used in naming `%#T'",
14644 class_key == union_type ? "union"
14645 : class_key == record_type ? "struct" : "class",
14646 type);
14647}
14648
cd0be382 14649/* Issue an error message if DECL is redeclared with different
37d407a1
KL
14650 access than its original declaration [class.access.spec/3].
14651 This applies to nested classes and nested class templates.
14652 [class.mem/1]. */
14653
14654static void cp_parser_check_access_in_redeclaration (tree decl)
14655{
14656 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14657 return;
14658
14659 if ((TREE_PRIVATE (decl)
14660 != (current_access_specifier == access_private_node))
14661 || (TREE_PROTECTED (decl)
14662 != (current_access_specifier == access_protected_node)))
14663 error ("%D redeclared with different access", decl);
14664}
14665
a723baf1
MM
14666/* Look for the `template' keyword, as a syntactic disambiguator.
14667 Return TRUE iff it is present, in which case it will be
14668 consumed. */
14669
14670static bool
14671cp_parser_optional_template_keyword (cp_parser *parser)
14672{
14673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14674 {
14675 /* The `template' keyword can only be used within templates;
14676 outside templates the parser can always figure out what is a
14677 template and what is not. */
14678 if (!processing_template_decl)
14679 {
14680 error ("`template' (as a disambiguator) is only allowed "
14681 "within templates");
14682 /* If this part of the token stream is rescanned, the same
14683 error message would be generated. So, we purge the token
14684 from the stream. */
14685 cp_lexer_purge_token (parser->lexer);
14686 return false;
14687 }
14688 else
14689 {
14690 /* Consume the `template' keyword. */
14691 cp_lexer_consume_token (parser->lexer);
14692 return true;
14693 }
14694 }
14695
14696 return false;
14697}
14698
2050a1bb
MM
14699/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14700 set PARSER->SCOPE, and perform other related actions. */
14701
14702static void
14703cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14704{
14705 tree value;
14706 tree check;
14707
14708 /* Get the stored value. */
14709 value = cp_lexer_consume_token (parser->lexer)->value;
14710 /* Perform any access checks that were deferred. */
14711 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14712 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14713 /* Set the scope from the stored value. */
14714 parser->scope = TREE_VALUE (value);
14715 parser->qualifying_scope = TREE_TYPE (value);
14716 parser->object_scope = NULL_TREE;
14717}
14718
a723baf1
MM
14719/* Add tokens to CACHE until an non-nested END token appears. */
14720
14721static void
14722cp_parser_cache_group (cp_parser *parser,
14723 cp_token_cache *cache,
14724 enum cpp_ttype end,
14725 unsigned depth)
14726{
14727 while (true)
14728 {
14729 cp_token *token;
14730
14731 /* Abort a parenthesized expression if we encounter a brace. */
14732 if ((end == CPP_CLOSE_PAREN || depth == 0)
14733 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14734 return;
14735 /* Consume the next token. */
14736 token = cp_lexer_consume_token (parser->lexer);
14737 /* If we've reached the end of the file, stop. */
14738 if (token->type == CPP_EOF)
14739 return;
14740 /* Add this token to the tokens we are saving. */
14741 cp_token_cache_push_token (cache, token);
14742 /* See if it starts a new group. */
14743 if (token->type == CPP_OPEN_BRACE)
14744 {
14745 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14746 if (depth == 0)
14747 return;
14748 }
14749 else if (token->type == CPP_OPEN_PAREN)
14750 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14751 else if (token->type == end)
14752 return;
14753 }
14754}
14755
14756/* Begin parsing tentatively. We always save tokens while parsing
14757 tentatively so that if the tentative parsing fails we can restore the
14758 tokens. */
14759
14760static void
94edc4ab 14761cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14762{
14763 /* Enter a new parsing context. */
14764 parser->context = cp_parser_context_new (parser->context);
14765 /* Begin saving tokens. */
14766 cp_lexer_save_tokens (parser->lexer);
14767 /* In order to avoid repetitive access control error messages,
14768 access checks are queued up until we are no longer parsing
14769 tentatively. */
8d241e0b 14770 push_deferring_access_checks (dk_deferred);
a723baf1
MM
14771}
14772
14773/* Commit to the currently active tentative parse. */
14774
14775static void
94edc4ab 14776cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14777{
14778 cp_parser_context *context;
14779 cp_lexer *lexer;
14780
14781 /* Mark all of the levels as committed. */
14782 lexer = parser->lexer;
14783 for (context = parser->context; context->next; context = context->next)
14784 {
14785 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14786 break;
14787 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14788 while (!cp_lexer_saving_tokens (lexer))
14789 lexer = lexer->next;
14790 cp_lexer_commit_tokens (lexer);
14791 }
14792}
14793
14794/* Abort the currently active tentative parse. All consumed tokens
14795 will be rolled back, and no diagnostics will be issued. */
14796
14797static void
94edc4ab 14798cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14799{
14800 cp_parser_simulate_error (parser);
14801 /* Now, pretend that we want to see if the construct was
14802 successfully parsed. */
14803 cp_parser_parse_definitely (parser);
14804}
14805
34cd5ae7 14806/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
14807 token stream. Otherwise, commit to the tokens we have consumed.
14808 Returns true if no error occurred; false otherwise. */
14809
14810static bool
94edc4ab 14811cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14812{
14813 bool error_occurred;
14814 cp_parser_context *context;
14815
34cd5ae7 14816 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
14817 destroy that information. */
14818 error_occurred = cp_parser_error_occurred (parser);
14819 /* Remove the topmost context from the stack. */
14820 context = parser->context;
14821 parser->context = context->next;
14822 /* If no parse errors occurred, commit to the tentative parse. */
14823 if (!error_occurred)
14824 {
14825 /* Commit to the tokens read tentatively, unless that was
14826 already done. */
14827 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14828 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14829
14830 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14831 }
14832 /* Otherwise, if errors occurred, roll back our state so that things
14833 are just as they were before we began the tentative parse. */
14834 else
cf22909c
KL
14835 {
14836 cp_lexer_rollback_tokens (parser->lexer);
14837 pop_deferring_access_checks ();
14838 }
e5976695
MM
14839 /* Add the context to the front of the free list. */
14840 context->next = cp_parser_context_free_list;
14841 cp_parser_context_free_list = context;
14842
14843 return !error_occurred;
a723baf1
MM
14844}
14845
a723baf1
MM
14846/* Returns true if we are parsing tentatively -- but have decided that
14847 we will stick with this tentative parse, even if errors occur. */
14848
14849static bool
94edc4ab 14850cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14851{
14852 return (cp_parser_parsing_tentatively (parser)
14853 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14854}
14855
4de8668e 14856/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
14857 tentative parse. */
14858
14859static bool
94edc4ab 14860cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14861{
14862 return (cp_parser_parsing_tentatively (parser)
14863 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14864}
14865
4de8668e 14866/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
14867
14868static bool
94edc4ab 14869cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14870{
14871 return parser->allow_gnu_extensions_p;
14872}
14873
14874\f
14875
14876/* The parser. */
14877
14878static GTY (()) cp_parser *the_parser;
14879
14880/* External interface. */
14881
d1bd0ded 14882/* Parse one entire translation unit. */
a723baf1 14883
d1bd0ded
GK
14884void
14885c_parse_file (void)
a723baf1
MM
14886{
14887 bool error_occurred;
14888
14889 the_parser = cp_parser_new ();
78757caa
KL
14890 push_deferring_access_checks (flag_access_control
14891 ? dk_no_deferred : dk_no_check);
a723baf1
MM
14892 error_occurred = cp_parser_translation_unit (the_parser);
14893 the_parser = NULL;
a723baf1
MM
14894}
14895
14896/* Clean up after parsing the entire translation unit. */
14897
14898void
94edc4ab 14899free_parser_stacks (void)
a723baf1
MM
14900{
14901 /* Nothing to do. */
14902}
14903
14904/* This variable must be provided by every front end. */
14905
14906int yydebug;
14907
14908#include "gt-cp-parser.h"