]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
Thread.java: Reordered fields...
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
b0bc6e8e 2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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;
f4abade9
GB
75 /* Token flags. */
76 unsigned char flags;
522df488
DN
77 /* The value associated with this token, if any. */
78 tree value;
82a98427
NS
79 /* The location at which this token was found. */
80 location_t location;
a723baf1
MM
81} cp_token;
82
522df488
DN
83/* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
a723baf1 85
522df488 86#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
a723baf1
MM
87
88/* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
92 complete.)
93
94 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 95 variable-length array), is used due to constraints imposed by the
a723baf1
MM
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
98
99typedef struct cp_token_block GTY (())
100{
101 /* The tokens. */
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
104 size_t num_tokens;
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
109} cp_token_block;
110
111typedef struct cp_token_cache GTY (())
112{
113 /* The first block in the cache. NULL if there are no tokens in the
114 cache. */
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
117 cache. */
118 cp_token_block *last;
119} cp_token_cache;
120
9bcb9aae 121/* Prototypes. */
a723baf1
MM
122
123static cp_token_cache *cp_token_cache_new
124 (void);
125static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
127
128/* Create a new cp_token_cache. */
129
130static cp_token_cache *
bf9d3c27 131cp_token_cache_new (void)
a723baf1 132{
c68b0a84 133 return ggc_alloc_cleared (sizeof (cp_token_cache));
a723baf1
MM
134}
135
136/* Add *TOKEN to *CACHE. */
137
138static void
139cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token *token)
141{
142 cp_token_block *b = cache->last;
143
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146 {
c68b0a84 147 b = ggc_alloc_cleared (sizeof (cp_token_block));
a723baf1
MM
148 b->prev = cache->last;
149 if (cache->last)
150 {
151 cache->last->next = b;
152 cache->last = b;
153 }
154 else
155 cache->first = cache->last = b;
156 }
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
159}
160
161/* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
163 it to the parser. */
164
165typedef struct cp_lexer GTY (())
166{
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
183
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
188
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
193
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
198
199 /* The STRING_CST tokens encountered while processing the current
200 string literal. */
201 varray_type string_tokens;
202
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
205 bool main_lexer_p;
206
207 /* True if we should output debugging information. */
208 bool debugging_p;
209
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
212} cp_lexer;
213
214/* Prototypes. */
215
17211ab5 216static cp_lexer *cp_lexer_new_main
94edc4ab 217 (void);
a723baf1 218static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 219 (struct cp_token_cache *);
a723baf1 220static int cp_lexer_saving_tokens
94edc4ab 221 (const cp_lexer *);
a723baf1 222static cp_token *cp_lexer_next_token
94edc4ab 223 (cp_lexer *, cp_token *);
a668c6ad
MM
224static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
94edc4ab
NN
226static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
a723baf1 228static cp_token *cp_lexer_read_token
94edc4ab 229 (cp_lexer *);
a723baf1 230static void cp_lexer_maybe_grow_buffer
94edc4ab 231 (cp_lexer *);
a723baf1 232static void cp_lexer_get_preprocessor_token
94edc4ab 233 (cp_lexer *, cp_token *);
a723baf1 234static cp_token *cp_lexer_peek_token
94edc4ab 235 (cp_lexer *);
a723baf1 236static cp_token *cp_lexer_peek_nth_token
94edc4ab 237 (cp_lexer *, size_t);
f7b5ecd9 238static inline bool cp_lexer_next_token_is
94edc4ab 239 (cp_lexer *, enum cpp_ttype);
a723baf1 240static bool cp_lexer_next_token_is_not
94edc4ab 241 (cp_lexer *, enum cpp_ttype);
a723baf1 242static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
243 (cp_lexer *, enum rid);
244static cp_token *cp_lexer_consume_token
245 (cp_lexer *);
a723baf1
MM
246static void cp_lexer_purge_token
247 (cp_lexer *);
248static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250static void cp_lexer_save_tokens
94edc4ab 251 (cp_lexer *);
a723baf1 252static void cp_lexer_commit_tokens
94edc4ab 253 (cp_lexer *);
a723baf1 254static void cp_lexer_rollback_tokens
94edc4ab 255 (cp_lexer *);
f7b5ecd9 256static inline void cp_lexer_set_source_position_from_token
94edc4ab 257 (cp_lexer *, const cp_token *);
a723baf1 258static void cp_lexer_print_token
94edc4ab 259 (FILE *, cp_token *);
f7b5ecd9 260static inline bool cp_lexer_debugging_p
94edc4ab 261 (cp_lexer *);
a723baf1 262static void cp_lexer_start_debugging
94edc4ab 263 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 264static void cp_lexer_stop_debugging
94edc4ab 265 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
266
267/* Manifest constants. */
268
269#define CP_TOKEN_BUFFER_SIZE 5
270#define CP_SAVED_TOKENS_SIZE 5
271
272/* A token type for keywords, as opposed to ordinary identifiers. */
273#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275/* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281/* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288/* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292/* Variables. */
293
294/* The stream to which debugging output should be written. */
295static FILE *cp_lexer_debug_stream;
296
17211ab5
GK
297/* Create a new main C++ lexer, the lexer that gets tokens from the
298 preprocessor. */
a723baf1
MM
299
300static cp_lexer *
17211ab5 301cp_lexer_new_main (void)
a723baf1
MM
302{
303 cp_lexer *lexer;
17211ab5
GK
304 cp_token first_token;
305
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 310 c_common_no_more_pch ();
a723baf1
MM
311
312 /* Allocate the memory. */
c68b0a84 313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
314
315 /* Create the circular buffer. */
c68b0a84 316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
17211ab5
GK
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
324
325 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 326 lexer->main_lexer_p = true;
a723baf1
MM
327
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
336
337 return lexer;
338}
339
340/* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
342
343static cp_lexer *
344cp_lexer_new_from_tokens (cp_token_cache *tokens)
345{
346 cp_lexer *lexer;
347 cp_token *token;
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
350
17211ab5 351 /* Allocate the memory. */
c68b0a84 352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
353
354 /* Create a new buffer, appropriately sized. */
355 num_tokens = 0;
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
c68b0a84 358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1
MM
359 lexer->buffer_end = lexer->buffer + num_tokens;
360
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
364 {
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
367 }
368
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
375
17211ab5
GK
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
378
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
387
a723baf1
MM
388 return lexer;
389}
390
4de8668e 391/* Returns nonzero if debugging information should be output. */
a723baf1 392
f7b5ecd9
MM
393static inline bool
394cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 395{
f7b5ecd9
MM
396 return lexer->debugging_p;
397}
398
399/* Set the current source position from the information stored in
400 TOKEN. */
401
402static inline void
94edc4ab
NN
403cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
f7b5ecd9
MM
405{
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
408
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
82a98427 411 input_location = token->location;
a723baf1
MM
412}
413
414/* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
416
f7b5ecd9 417static inline cp_token *
94edc4ab 418cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
419{
420 token++;
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
423 return token;
424}
425
a668c6ad
MM
426/* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
428
429static inline cp_token *
430cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431{
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
434 return token - 1;
435}
436
4de8668e 437/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
438
439static int
94edc4ab 440cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
441{
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443}
444
a723baf1
MM
445/* Return a pointer to the token that is N tokens beyond TOKEN in the
446 buffer. */
447
448static cp_token *
449cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450{
451 token += n;
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
454 return token;
455}
456
457/* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
459
460static ptrdiff_t
94edc4ab 461cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
462{
463 if (finish >= start)
464 return finish - start;
465 else
466 return ((lexer->buffer_end - lexer->buffer)
467 - (start - finish));
468}
469
470/* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
472
473static cp_token *
94edc4ab 474cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
475{
476 cp_token *token;
477
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
480
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
487
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
490
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
493
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
496
e6cc3a24
ZW
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
499 constant elements.
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
a723baf1 503 {
e6cc3a24
ZW
504 tree type;
505
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
a723baf1
MM
513 }
514
515 return token;
516}
517
518/* If the circular buffer is full, make it bigger. */
519
520static void
94edc4ab 521cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
522{
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
525 {
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
531
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
534 value. */
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
c68b0a84
KG
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
a723baf1
MM
541
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
546 buffer. */
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
549 new_buffer,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556 /* Now recompute all of the buffer pointers. */
557 new_first_token
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
560 {
561 ptrdiff_t next_token_delta;
562
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
565 else
566 next_token_delta =
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
569 }
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
574 }
575}
576
577/* Store the next token from the preprocessor in *TOKEN. */
578
579static void
94edc4ab
NN
580cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581 cp_token *token)
a723baf1
MM
582{
583 bool done;
584
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 586 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
587 {
588 token->type = CPP_EOF;
82a98427
NS
589 token->location.line = 0;
590 token->location.file = NULL;
a723baf1
MM
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
593
594 return;
595 }
596
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
600 {
601 /* Get a new token from the preprocessor. */
f4abade9 602 token->type = c_lex_with_flags (&token->value, &token->flags);
a723baf1
MM
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
605 {
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
611
a723baf1
MM
612 default:
613 /* This is a good token, so we exit the loop. */
614 done = true;
615 break;
616 }
617 }
618 /* Now we've got our token. */
82a98427 619 token->location = input_location;
a723baf1
MM
620
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
624 {
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
634 }
635 else
636 token->keyword = RID_MAX;
637}
638
639/* Return a pointer to the next token in the token stream, but do not
640 consume it. */
641
642static cp_token *
94edc4ab 643cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
644{
645 cp_token *token;
646
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
650
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
653 {
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
657 }
658
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
661 return token;
662}
663
664/* Return true if the next token has the indicated TYPE. */
665
666static bool
94edc4ab 667cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
668{
669 cp_token *token;
670
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
675}
676
677/* Return true if the next token does not have the indicated TYPE. */
678
679static bool
94edc4ab 680cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
681{
682 return !cp_lexer_next_token_is (lexer, type);
683}
684
685/* Return true if the next token is the indicated KEYWORD. */
686
687static bool
94edc4ab 688cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
689{
690 cp_token *token;
691
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
696}
697
698/* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
700
701static cp_token *
94edc4ab 702cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
703{
704 cp_token *token;
705
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
708
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
712 if (!token)
713 {
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
716 }
717
718 /* Now, read tokens until we have enough. */
719 while (--n > 0)
720 {
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
726 }
727
728 return token;
729}
730
731/* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
734 time. */
735
736static cp_token *
94edc4ab 737cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
738{
739 cp_token *token;
740
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
744
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
747
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
750 lexer->next_token);
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
754
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
757 {
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
761 else
762 lexer->first_token = lexer->next_token;
763 }
764
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
767 {
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
771 }
772
773 return token;
774}
775
776/* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
779
780static void
781cp_lexer_purge_token (cp_lexer *lexer)
782{
783 cp_token *token;
784 cp_token *next_token;
785
786 token = lexer->next_token;
787 while (true)
788 {
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
791 break;
792 *token = *next_token;
793 token = next_token;
794 }
795
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
798 clear NEXT_TOKEN. */
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
801}
802
803/* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
806
807static void
808cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809{
810 cp_token *peek;
811 cp_token *t1;
812 cp_token *t2;
813
814 if (lexer->next_token)
815 {
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
822 {
823 *t1 = *t2;
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
826 }
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
831 }
832 else
833 {
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
837 }
838}
839
840/* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
842
843static void
94edc4ab 844cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
845{
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
854
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
857 lexer->first_token,
858 lexer->next_token));
859}
860
861/* Commit to the portion of the token stream most recently saved. */
862
863static void
94edc4ab 864cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
865{
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870 VARRAY_POP (lexer->saved_tokens);
871}
872
873/* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
875
876static void
94edc4ab 877cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
878{
879 size_t delta;
880
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885 /* Find the token that was the NEXT_TOKEN when we started saving
886 tokens. */
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
890 lexer->first_token,
891 delta);
15d2cb19 892 /* It might be the case that there were no tokens when we started
a723baf1
MM
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
896
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
899}
900
a723baf1
MM
901/* Print a representation of the TOKEN on the STREAM. */
902
903static void
94edc4ab 904cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
905{
906 const char *token_type = NULL;
907
908 /* Figure out what kind of token this is. */
909 switch (token->type)
910 {
911 case CPP_EQ:
912 token_type = "EQ";
913 break;
914
915 case CPP_COMMA:
916 token_type = "COMMA";
917 break;
918
919 case CPP_OPEN_PAREN:
920 token_type = "OPEN_PAREN";
921 break;
922
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
925 break;
926
927 case CPP_OPEN_BRACE:
928 token_type = "OPEN_BRACE";
929 break;
930
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
933 break;
934
935 case CPP_SEMICOLON:
936 token_type = "SEMICOLON";
937 break;
938
939 case CPP_NAME:
940 token_type = "NAME";
941 break;
942
943 case CPP_EOF:
944 token_type = "EOF";
945 break;
946
947 case CPP_KEYWORD:
948 token_type = "keyword";
949 break;
950
951 /* This is not a token that we know how to handle yet. */
952 default:
953 break;
954 }
955
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
958 if (token_type)
959 fprintf (stream, "%s", token_type);
960 else
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969}
970
a723baf1
MM
971/* Start emitting debugging information. */
972
973static void
94edc4ab 974cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
975{
976 ++lexer->debugging_p;
977}
978
979/* Stop emitting debugging information. */
980
981static void
94edc4ab 982cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
983{
984 --lexer->debugging_p;
985}
986
987\f
988/* The parser. */
989
990/* Overview
991 --------
992
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1000
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1003 code processed.
1004
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1013
1014 Methodology
1015 -----------
1016
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1033
1034 Future Improvements
1035 -------------------
1036
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1039
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1044
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1049
1050/* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1052
1053typedef enum cp_parser_flags
1054{
1055 /* No flags. */
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062} cp_parser_flags;
1063
62b8a44e
NS
1064/* The different kinds of declarators we want to parse. */
1065
1066typedef enum cp_parser_declarator_kind
1067{
852dcbdd 1068 /* We want an abstract declarator. */
62b8a44e
NS
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
04c06002 1072 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1073 CP_PARSER_DECLARATOR_EITHER
1074} cp_parser_declarator_kind;
1075
a723baf1
MM
1076/* A mapping from a token type to a corresponding tree node type. */
1077
1078typedef struct cp_parser_token_tree_map_node
1079{
1080 /* The token type. */
df2b750f 1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
a723baf1 1082 /* The corresponding tree code. */
df2b750f 1083 ENUM_BITFIELD (tree_code) tree_type : 8;
a723baf1
MM
1084} cp_parser_token_tree_map_node;
1085
1086/* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1088
1089typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091/* The status of a tentative parse. */
1092
1093typedef enum cp_parser_status_kind
1094{
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1100 has occurred. */
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102} cp_parser_status_kind;
1103
1104/* Context that is saved and restored when parsing tentatively. */
1105
1106typedef struct cp_parser_context GTY (())
1107{
1108 /* If this is a tentative parsing context, the status of the
1109 tentative parse. */
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1115 tree object_type;
a723baf1
MM
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118} cp_parser_context;
1119
1120/* Prototypes. */
1121
1122/* Constructors and destructors. */
1123
1124static cp_parser_context *cp_parser_context_new
94edc4ab 1125 (cp_parser_context *);
a723baf1 1126
e5976695
MM
1127/* Class variables. */
1128
92bc1323 1129static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1130
a723baf1
MM
1131/* Constructors and destructors. */
1132
1133/* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1135
1136static cp_parser_context *
94edc4ab 1137cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1138{
1139 cp_parser_context *context;
1140
1141 /* Allocate the storage. */
e5976695
MM
1142 if (cp_parser_context_free_list != NULL)
1143 {
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
c68b0a84 1147 memset (context, 0, sizeof (*context));
e5976695
MM
1148 }
1149 else
c68b0a84 1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1155 if (next)
1156 {
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
a723baf1
MM
1160 /* Thread the stack. */
1161 context->next = next;
1162 }
1163
1164 return context;
1165}
1166
1167/* The cp_parser structure represents the C++ parser. */
1168
1169typedef struct cp_parser GTY(())
1170{
1171 /* The lexer from which we are obtaining tokens. */
1172 cp_lexer *lexer;
1173
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1178
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1186 tree scope;
1187
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1193 tree object_scope;
1194 tree qualifying_scope;
1195
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1198
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1206
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1210
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1215
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1218 them permissible. */
a723baf1
MM
1219 bool default_arg_ok_p;
1220
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
67c03833 1223 bool integral_constant_expression_p;
a723baf1 1224
14d22dd6
MM
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
67c03833 1229 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1230
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
67c03833 1233 bool non_integral_constant_expression_p;
14d22dd6 1234
263ee052
MM
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1236 bool in_offsetof_p;
1237
a723baf1
MM
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1241
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1246
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1250
4bb8ca28
MM
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1253
0e59b3fb
MM
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1257
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1261
4f8163b1
MM
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1266
a723baf1
MM
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1271
8db1028e
NS
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1277 order.
1278
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1282 processing.
1283
1284 These lists can only be processed once the outermost class being
9bcb9aae 1285 defined is complete. */
a723baf1
MM
1286 tree unparsed_functions_queues;
1287
1288 /* The number of classes whose definitions are currently in
1289 progress. */
1290 unsigned num_classes_being_defined;
1291
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1295} cp_parser;
1296
04c06002 1297/* The type of a function that parses some kind of expression. */
94edc4ab 1298typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1299
1300/* Prototypes. */
1301
1302/* Constructors and destructors. */
1303
1304static cp_parser *cp_parser_new
94edc4ab 1305 (void);
a723baf1
MM
1306
1307/* Routines to parse various constructs.
1308
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1312 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1318
1319/* Lexical conventions [gram.lex] */
1320
1321static tree cp_parser_identifier
94edc4ab 1322 (cp_parser *);
a723baf1
MM
1323
1324/* Basic concepts [gram.basic] */
1325
1326static bool cp_parser_translation_unit
94edc4ab 1327 (cp_parser *);
a723baf1
MM
1328
1329/* Expressions [gram.expr] */
1330
1331static tree cp_parser_primary_expression
b3445994 1332 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1333static tree cp_parser_id_expression
f3c2dfc6 1334 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1335static tree cp_parser_unqualified_id
f3c2dfc6 1336 (cp_parser *, bool, bool, bool);
a723baf1 1337static tree cp_parser_nested_name_specifier_opt
a668c6ad 1338 (cp_parser *, bool, bool, bool, bool);
a723baf1 1339static tree cp_parser_nested_name_specifier
a723baf1 1340 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1341static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1343static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
7efa3e22 1345static tree cp_parser_parenthesized_expression_list
39703eb9 1346 (cp_parser *, bool, bool *);
a723baf1 1347static void cp_parser_pseudo_destructor_name
94edc4ab 1348 (cp_parser *, tree *, tree *);
a723baf1
MM
1349static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351static enum tree_code cp_parser_unary_operator
94edc4ab 1352 (cp_token *);
a723baf1 1353static tree cp_parser_new_expression
94edc4ab 1354 (cp_parser *);
a723baf1 1355static tree cp_parser_new_placement
94edc4ab 1356 (cp_parser *);
a723baf1 1357static tree cp_parser_new_type_id
94edc4ab 1358 (cp_parser *);
a723baf1 1359static tree cp_parser_new_declarator_opt
94edc4ab 1360 (cp_parser *);
a723baf1 1361static tree cp_parser_direct_new_declarator
94edc4ab 1362 (cp_parser *);
a723baf1 1363static tree cp_parser_new_initializer
94edc4ab 1364 (cp_parser *);
a723baf1 1365static tree cp_parser_delete_expression
94edc4ab 1366 (cp_parser *);
a723baf1
MM
1367static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369static tree cp_parser_pm_expression
94edc4ab 1370 (cp_parser *);
a723baf1 1371static tree cp_parser_multiplicative_expression
94edc4ab 1372 (cp_parser *);
a723baf1 1373static tree cp_parser_additive_expression
94edc4ab 1374 (cp_parser *);
a723baf1 1375static tree cp_parser_shift_expression
94edc4ab 1376 (cp_parser *);
a723baf1 1377static tree cp_parser_relational_expression
94edc4ab 1378 (cp_parser *);
a723baf1 1379static tree cp_parser_equality_expression
94edc4ab 1380 (cp_parser *);
a723baf1 1381static tree cp_parser_and_expression
94edc4ab 1382 (cp_parser *);
a723baf1 1383static tree cp_parser_exclusive_or_expression
94edc4ab 1384 (cp_parser *);
a723baf1 1385static tree cp_parser_inclusive_or_expression
94edc4ab 1386 (cp_parser *);
a723baf1 1387static tree cp_parser_logical_and_expression
94edc4ab 1388 (cp_parser *);
a723baf1 1389static tree cp_parser_logical_or_expression
94edc4ab 1390 (cp_parser *);
a723baf1 1391static tree cp_parser_question_colon_clause
94edc4ab 1392 (cp_parser *, tree);
a723baf1 1393static tree cp_parser_assignment_expression
94edc4ab 1394 (cp_parser *);
a723baf1 1395static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1396 (cp_parser *);
a723baf1 1397static tree cp_parser_expression
94edc4ab 1398 (cp_parser *);
a723baf1 1399static tree cp_parser_constant_expression
14d22dd6 1400 (cp_parser *, bool, bool *);
a723baf1
MM
1401
1402/* Statements [gram.stmt.stmt] */
1403
1404static void cp_parser_statement
a5bcc582 1405 (cp_parser *, bool);
a723baf1 1406static tree cp_parser_labeled_statement
a5bcc582 1407 (cp_parser *, bool);
a723baf1 1408static tree cp_parser_expression_statement
a5bcc582 1409 (cp_parser *, bool);
a723baf1 1410static tree cp_parser_compound_statement
a5bcc582 1411 (cp_parser *, bool);
a723baf1 1412static void cp_parser_statement_seq_opt
a5bcc582 1413 (cp_parser *, bool);
a723baf1 1414static tree cp_parser_selection_statement
94edc4ab 1415 (cp_parser *);
a723baf1 1416static tree cp_parser_condition
94edc4ab 1417 (cp_parser *);
a723baf1 1418static tree cp_parser_iteration_statement
94edc4ab 1419 (cp_parser *);
a723baf1 1420static void cp_parser_for_init_statement
94edc4ab 1421 (cp_parser *);
a723baf1 1422static tree cp_parser_jump_statement
94edc4ab 1423 (cp_parser *);
a723baf1 1424static void cp_parser_declaration_statement
94edc4ab 1425 (cp_parser *);
a723baf1
MM
1426
1427static tree cp_parser_implicitly_scoped_statement
94edc4ab 1428 (cp_parser *);
a723baf1 1429static void cp_parser_already_scoped_statement
94edc4ab 1430 (cp_parser *);
a723baf1
MM
1431
1432/* Declarations [gram.dcl.dcl] */
1433
1434static void cp_parser_declaration_seq_opt
94edc4ab 1435 (cp_parser *);
a723baf1 1436static void cp_parser_declaration
94edc4ab 1437 (cp_parser *);
a723baf1 1438static void cp_parser_block_declaration
94edc4ab 1439 (cp_parser *, bool);
a723baf1 1440static void cp_parser_simple_declaration
94edc4ab 1441 (cp_parser *, bool);
a723baf1 1442static tree cp_parser_decl_specifier_seq
560ad596 1443 (cp_parser *, cp_parser_flags, tree *, int *);
a723baf1 1444static tree cp_parser_storage_class_specifier_opt
94edc4ab 1445 (cp_parser *);
a723baf1 1446static tree cp_parser_function_specifier_opt
94edc4ab 1447 (cp_parser *);
a723baf1 1448static tree cp_parser_type_specifier
560ad596 1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
a723baf1 1450static tree cp_parser_simple_type_specifier
4b0d3cbe 1451 (cp_parser *, cp_parser_flags, bool);
a723baf1 1452static tree cp_parser_type_name
94edc4ab 1453 (cp_parser *);
a723baf1 1454static tree cp_parser_elaborated_type_specifier
94edc4ab 1455 (cp_parser *, bool, bool);
a723baf1 1456static tree cp_parser_enum_specifier
94edc4ab 1457 (cp_parser *);
a723baf1 1458static void cp_parser_enumerator_list
94edc4ab 1459 (cp_parser *, tree);
a723baf1 1460static void cp_parser_enumerator_definition
94edc4ab 1461 (cp_parser *, tree);
a723baf1 1462static tree cp_parser_namespace_name
94edc4ab 1463 (cp_parser *);
a723baf1 1464static void cp_parser_namespace_definition
94edc4ab 1465 (cp_parser *);
a723baf1 1466static void cp_parser_namespace_body
94edc4ab 1467 (cp_parser *);
a723baf1 1468static tree cp_parser_qualified_namespace_specifier
94edc4ab 1469 (cp_parser *);
a723baf1 1470static void cp_parser_namespace_alias_definition
94edc4ab 1471 (cp_parser *);
a723baf1 1472static void cp_parser_using_declaration
94edc4ab 1473 (cp_parser *);
a723baf1 1474static void cp_parser_using_directive
94edc4ab 1475 (cp_parser *);
a723baf1 1476static void cp_parser_asm_definition
94edc4ab 1477 (cp_parser *);
a723baf1 1478static void cp_parser_linkage_specification
94edc4ab 1479 (cp_parser *);
a723baf1
MM
1480
1481/* Declarators [gram.dcl.decl] */
1482
1483static tree cp_parser_init_declarator
560ad596 1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
a723baf1 1485static tree cp_parser_declarator
4bb8ca28 1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
a723baf1 1487static tree cp_parser_direct_declarator
7efa3e22 1488 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1489static enum tree_code cp_parser_ptr_operator
94edc4ab 1490 (cp_parser *, tree *, tree *);
a723baf1 1491static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1492 (cp_parser *);
a723baf1 1493static tree cp_parser_cv_qualifier_opt
94edc4ab 1494 (cp_parser *);
a723baf1 1495static tree cp_parser_declarator_id
94edc4ab 1496 (cp_parser *);
a723baf1 1497static tree cp_parser_type_id
94edc4ab 1498 (cp_parser *);
a723baf1 1499static tree cp_parser_type_specifier_seq
94edc4ab 1500 (cp_parser *);
a723baf1 1501static tree cp_parser_parameter_declaration_clause
94edc4ab 1502 (cp_parser *);
a723baf1 1503static tree cp_parser_parameter_declaration_list
94edc4ab 1504 (cp_parser *);
a723baf1 1505static tree cp_parser_parameter_declaration
4bb8ca28 1506 (cp_parser *, bool, bool *);
a723baf1
MM
1507static void cp_parser_function_body
1508 (cp_parser *);
1509static tree cp_parser_initializer
39703eb9 1510 (cp_parser *, bool *, bool *);
a723baf1 1511static tree cp_parser_initializer_clause
39703eb9 1512 (cp_parser *, bool *);
a723baf1 1513static tree cp_parser_initializer_list
39703eb9 1514 (cp_parser *, bool *);
a723baf1
MM
1515
1516static bool cp_parser_ctor_initializer_opt_and_function_body
1517 (cp_parser *);
1518
1519/* Classes [gram.class] */
1520
1521static tree cp_parser_class_name
a668c6ad 1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
a723baf1 1523static tree cp_parser_class_specifier
94edc4ab 1524 (cp_parser *);
a723baf1 1525static tree cp_parser_class_head
94edc4ab 1526 (cp_parser *, bool *);
a723baf1 1527static enum tag_types cp_parser_class_key
94edc4ab 1528 (cp_parser *);
a723baf1 1529static void cp_parser_member_specification_opt
94edc4ab 1530 (cp_parser *);
a723baf1 1531static void cp_parser_member_declaration
94edc4ab 1532 (cp_parser *);
a723baf1 1533static tree cp_parser_pure_specifier
94edc4ab 1534 (cp_parser *);
a723baf1 1535static tree cp_parser_constant_initializer
94edc4ab 1536 (cp_parser *);
a723baf1
MM
1537
1538/* Derived classes [gram.class.derived] */
1539
1540static tree cp_parser_base_clause
94edc4ab 1541 (cp_parser *);
a723baf1 1542static tree cp_parser_base_specifier
94edc4ab 1543 (cp_parser *);
a723baf1
MM
1544
1545/* Special member functions [gram.special] */
1546
1547static tree cp_parser_conversion_function_id
94edc4ab 1548 (cp_parser *);
a723baf1 1549static tree cp_parser_conversion_type_id
94edc4ab 1550 (cp_parser *);
a723baf1 1551static tree cp_parser_conversion_declarator_opt
94edc4ab 1552 (cp_parser *);
a723baf1 1553static bool cp_parser_ctor_initializer_opt
94edc4ab 1554 (cp_parser *);
a723baf1 1555static void cp_parser_mem_initializer_list
94edc4ab 1556 (cp_parser *);
a723baf1 1557static tree cp_parser_mem_initializer
94edc4ab 1558 (cp_parser *);
a723baf1 1559static tree cp_parser_mem_initializer_id
94edc4ab 1560 (cp_parser *);
a723baf1
MM
1561
1562/* Overloading [gram.over] */
1563
1564static tree cp_parser_operator_function_id
94edc4ab 1565 (cp_parser *);
a723baf1 1566static tree cp_parser_operator
94edc4ab 1567 (cp_parser *);
a723baf1
MM
1568
1569/* Templates [gram.temp] */
1570
1571static void cp_parser_template_declaration
94edc4ab 1572 (cp_parser *, bool);
a723baf1 1573static tree cp_parser_template_parameter_list
94edc4ab 1574 (cp_parser *);
a723baf1 1575static tree cp_parser_template_parameter
94edc4ab 1576 (cp_parser *);
a723baf1 1577static tree cp_parser_type_parameter
94edc4ab 1578 (cp_parser *);
a723baf1 1579static tree cp_parser_template_id
a668c6ad 1580 (cp_parser *, bool, bool, bool);
a723baf1 1581static tree cp_parser_template_name
a668c6ad 1582 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1583static tree cp_parser_template_argument_list
94edc4ab 1584 (cp_parser *);
a723baf1 1585static tree cp_parser_template_argument
94edc4ab 1586 (cp_parser *);
a723baf1 1587static void cp_parser_explicit_instantiation
94edc4ab 1588 (cp_parser *);
a723baf1 1589static void cp_parser_explicit_specialization
94edc4ab 1590 (cp_parser *);
a723baf1
MM
1591
1592/* Exception handling [gram.exception] */
1593
1594static tree cp_parser_try_block
94edc4ab 1595 (cp_parser *);
a723baf1 1596static bool cp_parser_function_try_block
94edc4ab 1597 (cp_parser *);
a723baf1 1598static void cp_parser_handler_seq
94edc4ab 1599 (cp_parser *);
a723baf1 1600static void cp_parser_handler
94edc4ab 1601 (cp_parser *);
a723baf1 1602static tree cp_parser_exception_declaration
94edc4ab 1603 (cp_parser *);
a723baf1 1604static tree cp_parser_throw_expression
94edc4ab 1605 (cp_parser *);
a723baf1 1606static tree cp_parser_exception_specification_opt
94edc4ab 1607 (cp_parser *);
a723baf1 1608static tree cp_parser_type_id_list
94edc4ab 1609 (cp_parser *);
a723baf1
MM
1610
1611/* GNU Extensions */
1612
1613static tree cp_parser_asm_specification_opt
94edc4ab 1614 (cp_parser *);
a723baf1 1615static tree cp_parser_asm_operand_list
94edc4ab 1616 (cp_parser *);
a723baf1 1617static tree cp_parser_asm_clobber_list
94edc4ab 1618 (cp_parser *);
a723baf1 1619static tree cp_parser_attributes_opt
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_attribute_list
94edc4ab 1622 (cp_parser *);
a723baf1 1623static bool cp_parser_extension_opt
94edc4ab 1624 (cp_parser *, int *);
a723baf1 1625static void cp_parser_label_declaration
94edc4ab 1626 (cp_parser *);
a723baf1
MM
1627
1628/* Utility Routines */
1629
1630static tree cp_parser_lookup_name
b0bc6e8e 1631 (cp_parser *, tree, bool, bool, bool, bool);
a723baf1 1632static tree cp_parser_lookup_name_simple
94edc4ab 1633 (cp_parser *, tree);
a723baf1
MM
1634static tree cp_parser_maybe_treat_template_as_class
1635 (tree, bool);
1636static bool cp_parser_check_declarator_template_parameters
94edc4ab 1637 (cp_parser *, tree);
a723baf1 1638static bool cp_parser_check_template_parameters
94edc4ab 1639 (cp_parser *, unsigned);
d6b4ea85
MM
1640static tree cp_parser_simple_cast_expression
1641 (cp_parser *);
a723baf1 1642static tree cp_parser_binary_expression
94edc4ab 1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1644static tree cp_parser_global_scope_opt
94edc4ab 1645 (cp_parser *, bool);
a723baf1
MM
1646static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1649 (cp_parser *, tree, tree, tree);
a723baf1 1650static tree cp_parser_function_definition_after_declarator
94edc4ab 1651 (cp_parser *, bool);
a723baf1 1652static void cp_parser_template_declaration_after_export
94edc4ab 1653 (cp_parser *, bool);
a723baf1 1654static tree cp_parser_single_declaration
94edc4ab 1655 (cp_parser *, bool, bool *);
a723baf1 1656static tree cp_parser_functional_cast
94edc4ab 1657 (cp_parser *, tree);
4bb8ca28
MM
1658static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
ec75414f
MM
1660static tree cp_parser_enclosed_template_argument_list
1661 (cp_parser *);
8db1028e
NS
1662static void cp_parser_save_default_args
1663 (cp_parser *, tree);
a723baf1 1664static void cp_parser_late_parsing_for_member
94edc4ab 1665 (cp_parser *, tree);
a723baf1 1666static void cp_parser_late_parsing_default_args
8218bd34 1667 (cp_parser *, tree);
a723baf1 1668static tree cp_parser_sizeof_operand
94edc4ab 1669 (cp_parser *, enum rid);
a723baf1 1670static bool cp_parser_declares_only_class_p
94edc4ab 1671 (cp_parser *);
a723baf1 1672static bool cp_parser_friend_p
94edc4ab 1673 (tree);
a723baf1 1674static cp_token *cp_parser_require
94edc4ab 1675 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1676static cp_token *cp_parser_require_keyword
94edc4ab 1677 (cp_parser *, enum rid, const char *);
a723baf1 1678static bool cp_parser_token_starts_function_definition_p
94edc4ab 1679 (cp_token *);
a723baf1
MM
1680static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
d17811fd
MM
1682static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
f4abade9
GB
1684static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
a723baf1 1686static enum tag_types cp_parser_token_is_class_key
94edc4ab 1687 (cp_token *);
a723baf1
MM
1688static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
37d407a1
KL
1690static void cp_parser_check_access_in_redeclaration
1691 (tree type);
a723baf1
MM
1692static bool cp_parser_optional_template_keyword
1693 (cp_parser *);
2050a1bb
MM
1694static void cp_parser_pre_parsed_nested_name_specifier
1695 (cp_parser *);
a723baf1
MM
1696static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698static void cp_parser_parse_tentatively
94edc4ab 1699 (cp_parser *);
a723baf1 1700static void cp_parser_commit_to_tentative_parse
94edc4ab 1701 (cp_parser *);
a723baf1 1702static void cp_parser_abort_tentative_parse
94edc4ab 1703 (cp_parser *);
a723baf1 1704static bool cp_parser_parse_definitely
94edc4ab 1705 (cp_parser *);
f7b5ecd9 1706static inline bool cp_parser_parsing_tentatively
94edc4ab 1707 (cp_parser *);
a723baf1 1708static bool cp_parser_committed_to_tentative_parse
94edc4ab 1709 (cp_parser *);
a723baf1 1710static void cp_parser_error
94edc4ab 1711 (cp_parser *, const char *);
4bb8ca28
MM
1712static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
e5976695 1714static bool cp_parser_simulate_error
94edc4ab 1715 (cp_parser *);
a723baf1 1716static void cp_parser_check_type_definition
94edc4ab 1717 (cp_parser *);
560ad596
MM
1718static void cp_parser_check_for_definition_in_return_type
1719 (tree, int);
ee43dab5
MM
1720static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
67c03833 1722static tree cp_parser_non_integral_constant_expression
14d22dd6 1723 (const char *);
8fbc5ae7
MM
1724static bool cp_parser_diagnose_invalid_type_name
1725 (cp_parser *);
7efa3e22 1726static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1727 (cp_parser *, bool, bool, bool);
a723baf1 1728static void cp_parser_skip_to_end_of_statement
94edc4ab 1729 (cp_parser *);
e0860732
MM
1730static void cp_parser_consume_semicolon_at_end_of_statement
1731 (cp_parser *);
a723baf1 1732static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1733 (cp_parser *);
a723baf1
MM
1734static void cp_parser_skip_to_closing_brace
1735 (cp_parser *);
1736static void cp_parser_skip_until_found
94edc4ab 1737 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1738static bool cp_parser_error_occurred
94edc4ab 1739 (cp_parser *);
a723baf1 1740static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1741 (cp_parser *);
a723baf1 1742static bool cp_parser_is_string_literal
94edc4ab 1743 (cp_token *);
a723baf1 1744static bool cp_parser_is_keyword
94edc4ab 1745 (cp_token *, enum rid);
a723baf1 1746
4de8668e 1747/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1748
1749static inline bool
94edc4ab 1750cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1751{
1752 return parser->context->next != NULL;
1753}
1754
4de8668e 1755/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1756
1757static bool
94edc4ab 1758cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1759{
1760 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1761}
1762
4de8668e 1763/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1764
1765static bool
94edc4ab 1766cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1767{
1768 return token->keyword == keyword;
1769}
1770
a723baf1
MM
1771/* Issue the indicated error MESSAGE. */
1772
1773static void
94edc4ab 1774cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1775{
a723baf1 1776 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1777 if (!cp_parser_simulate_error (parser))
4bb8ca28
MM
1778 {
1779 cp_token *token;
1780 token = cp_lexer_peek_token (parser->lexer);
5c832178
MM
1781 c_parse_error (message,
1782 /* Because c_parser_error does not understand
1783 CPP_KEYWORD, keywords are treated like
1784 identifiers. */
1785 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1786 token->value);
4bb8ca28
MM
1787 }
1788}
1789
1790/* Issue an error about name-lookup failing. NAME is the
1791 IDENTIFIER_NODE DECL is the result of
1792 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1793 the thing that we hoped to find. */
1794
1795static void
1796cp_parser_name_lookup_error (cp_parser* parser,
1797 tree name,
1798 tree decl,
1799 const char* desired)
1800{
1801 /* If name lookup completely failed, tell the user that NAME was not
1802 declared. */
1803 if (decl == error_mark_node)
1804 {
1805 if (parser->scope && parser->scope != global_namespace)
1806 error ("`%D::%D' has not been declared",
1807 parser->scope, name);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' has not been declared", name);
1810 else
1811 error ("`%D' has not been declared", name);
1812 }
1813 else if (parser->scope && parser->scope != global_namespace)
1814 error ("`%D::%D' %s", parser->scope, name, desired);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' %s", name, desired);
1817 else
1818 error ("`%D' %s", name, desired);
a723baf1
MM
1819}
1820
1821/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1822 during this tentative parse. Returns true if the error was
1823 simulated; false if a messgae should be issued by the caller. */
a723baf1 1824
e5976695 1825static bool
94edc4ab 1826cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1827{
1828 if (cp_parser_parsing_tentatively (parser)
1829 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1830 {
1831 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1832 return true;
1833 }
1834 return false;
a723baf1
MM
1835}
1836
1837/* This function is called when a type is defined. If type
1838 definitions are forbidden at this point, an error message is
1839 issued. */
1840
1841static void
94edc4ab 1842cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1843{
1844 /* If types are forbidden here, issue a message. */
1845 if (parser->type_definition_forbidden_message)
1846 /* Use `%s' to print the string in case there are any escape
1847 characters in the message. */
1848 error ("%s", parser->type_definition_forbidden_message);
1849}
1850
560ad596
MM
1851/* This function is called when a declaration is parsed. If
1852 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853 indicates that a type was defined in the decl-specifiers for DECL,
1854 then an error is issued. */
1855
1856static void
1857cp_parser_check_for_definition_in_return_type (tree declarator,
1858 int declares_class_or_enum)
1859{
1860 /* [dcl.fct] forbids type definitions in return types.
1861 Unfortunately, it's not easy to know whether or not we are
1862 processing a return type until after the fact. */
1863 while (declarator
1864 && (TREE_CODE (declarator) == INDIRECT_REF
1865 || TREE_CODE (declarator) == ADDR_EXPR))
1866 declarator = TREE_OPERAND (declarator, 0);
1867 if (declarator
1868 && TREE_CODE (declarator) == CALL_EXPR
1869 && declares_class_or_enum & 2)
1870 error ("new types may not be defined in a return type");
1871}
1872
ee43dab5
MM
1873/* A type-specifier (TYPE) has been parsed which cannot be followed by
1874 "<" in any valid C++ program. If the next token is indeed "<",
1875 issue a message warning the user about what appears to be an
1876 invalid attempt to form a template-id. */
1877
1878static void
1879cp_parser_check_for_invalid_template_id (cp_parser* parser,
1880 tree type)
1881{
1882 ptrdiff_t start;
1883 cp_token *token;
1884
1885 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1886 {
1887 if (TYPE_P (type))
1888 error ("`%T' is not a template", type);
1889 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1891 else
1892 error ("invalid template-id");
1893 /* Remember the location of the invalid "<". */
1894 if (cp_parser_parsing_tentatively (parser)
1895 && !cp_parser_committed_to_tentative_parse (parser))
1896 {
1897 token = cp_lexer_peek_token (parser->lexer);
1898 token = cp_lexer_prev_token (parser->lexer, token);
1899 start = cp_lexer_token_difference (parser->lexer,
1900 parser->lexer->first_token,
1901 token);
1902 }
1903 else
1904 start = -1;
1905 /* Consume the "<". */
1906 cp_lexer_consume_token (parser->lexer);
1907 /* Parse the template arguments. */
1908 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1909 /* Permanently remove the invalid template arguments so that
ee43dab5
MM
1910 this error message is not issued again. */
1911 if (start >= 0)
1912 {
1913 token = cp_lexer_advance_token (parser->lexer,
1914 parser->lexer->first_token,
1915 start);
1916 cp_lexer_purge_tokens_after (parser->lexer, token);
1917 }
1918 }
1919}
1920
cd0be382 1921/* Issue an error message about the fact that THING appeared in a
14d22dd6
MM
1922 constant-expression. Returns ERROR_MARK_NODE. */
1923
1924static tree
67c03833 1925cp_parser_non_integral_constant_expression (const char *thing)
14d22dd6
MM
1926{
1927 error ("%s cannot appear in a constant-expression", thing);
1928 return error_mark_node;
1929}
1930
8fbc5ae7
MM
1931/* Check for a common situation where a type-name should be present,
1932 but is not, and issue a sensible error message. Returns true if an
1933 invalid type-name was detected. */
1934
1935static bool
1936cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1937{
1938 /* If the next two tokens are both identifiers, the code is
1939 erroneous. The usual cause of this situation is code like:
1940
1941 T t;
1942
1943 where "T" should name a type -- but does not. */
1944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1945 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1946 {
1947 tree name;
1948
8d241e0b 1949 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
1950 looking at a declaration. */
1951 /* Consume the first identifier. */
1952 name = cp_lexer_consume_token (parser->lexer)->value;
1953 /* Issue an error message. */
1954 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1955 /* If we're in a template class, it's possible that the user was
1956 referring to a type from a base class. For example:
1957
1958 template <typename T> struct A { typedef T X; };
1959 template <typename T> struct B : public A<T> { X x; };
1960
1961 The user should have said "typename A<T>::X". */
1962 if (processing_template_decl && current_class_type)
1963 {
1964 tree b;
1965
1966 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1967 b;
1968 b = TREE_CHAIN (b))
1969 {
1970 tree base_type = BINFO_TYPE (b);
1971 if (CLASS_TYPE_P (base_type)
1fb3244a 1972 && dependent_type_p (base_type))
8fbc5ae7
MM
1973 {
1974 tree field;
1975 /* Go from a particular instantiation of the
1976 template (which will have an empty TYPE_FIELDs),
1977 to the main version. */
353b4fc0 1978 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1979 for (field = TYPE_FIELDS (base_type);
1980 field;
1981 field = TREE_CHAIN (field))
1982 if (TREE_CODE (field) == TYPE_DECL
1983 && DECL_NAME (field) == name)
1984 {
1985 error ("(perhaps `typename %T::%s' was intended)",
1986 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1987 break;
1988 }
1989 if (field)
1990 break;
1991 }
1992 }
1993 }
1994 /* Skip to the end of the declaration; there's no point in
1995 trying to process it. */
1996 cp_parser_skip_to_end_of_statement (parser);
1997
1998 return true;
1999 }
2000
2001 return false;
2002}
2003
a723baf1 2004/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2005 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2006 are doing error recovery. Returns -1 if OR_COMMA is true and we
2007 found an unnested comma. */
a723baf1 2008
7efa3e22
NS
2009static int
2010cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
a668c6ad
MM
2011 bool recovering,
2012 bool or_comma,
2013 bool consume_paren)
a723baf1 2014{
7efa3e22
NS
2015 unsigned paren_depth = 0;
2016 unsigned brace_depth = 0;
a723baf1 2017
7efa3e22
NS
2018 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2019 && !cp_parser_committed_to_tentative_parse (parser))
2020 return 0;
2021
a723baf1
MM
2022 while (true)
2023 {
2024 cp_token *token;
7efa3e22 2025
a723baf1
MM
2026 /* If we've run out of tokens, then there is no closing `)'. */
2027 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7efa3e22 2028 return 0;
a723baf1 2029
a668c6ad
MM
2030 token = cp_lexer_peek_token (parser->lexer);
2031
f4f206f4 2032 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad
MM
2033 if (token->type == CPP_SEMICOLON && !brace_depth)
2034 return 0;
2035 if (token->type == CPP_OPEN_BRACE)
2036 ++brace_depth;
2037 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2038 {
a668c6ad 2039 if (!brace_depth--)
7efa3e22 2040 return 0;
7efa3e22 2041 }
a668c6ad
MM
2042 if (recovering && or_comma && token->type == CPP_COMMA
2043 && !brace_depth && !paren_depth)
2044 return -1;
7efa3e22 2045
7efa3e22
NS
2046 if (!brace_depth)
2047 {
2048 /* If it is an `(', we have entered another level of nesting. */
2049 if (token->type == CPP_OPEN_PAREN)
2050 ++paren_depth;
2051 /* If it is a `)', then we might be done. */
2052 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2053 {
2054 if (consume_paren)
2055 cp_lexer_consume_token (parser->lexer);
2056 return 1;
2057 }
7efa3e22 2058 }
a668c6ad
MM
2059
2060 /* Consume the token. */
2061 cp_lexer_consume_token (parser->lexer);
a723baf1
MM
2062 }
2063}
2064
2065/* Consume tokens until we reach the end of the current statement.
2066 Normally, that will be just before consuming a `;'. However, if a
2067 non-nested `}' comes first, then we stop before consuming that. */
2068
2069static void
94edc4ab 2070cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2071{
2072 unsigned nesting_depth = 0;
2073
2074 while (true)
2075 {
2076 cp_token *token;
2077
2078 /* Peek at the next token. */
2079 token = cp_lexer_peek_token (parser->lexer);
2080 /* If we've run out of tokens, stop. */
2081 if (token->type == CPP_EOF)
2082 break;
2083 /* If the next token is a `;', we have reached the end of the
2084 statement. */
2085 if (token->type == CPP_SEMICOLON && !nesting_depth)
2086 break;
2087 /* If the next token is a non-nested `}', then we have reached
2088 the end of the current block. */
2089 if (token->type == CPP_CLOSE_BRACE)
2090 {
2091 /* If this is a non-nested `}', stop before consuming it.
2092 That way, when confronted with something like:
2093
2094 { 3 + }
2095
2096 we stop before consuming the closing `}', even though we
2097 have not yet reached a `;'. */
2098 if (nesting_depth == 0)
2099 break;
2100 /* If it is the closing `}' for a block that we have
2101 scanned, stop -- but only after consuming the token.
2102 That way given:
2103
2104 void f g () { ... }
2105 typedef int I;
2106
2107 we will stop after the body of the erroneously declared
2108 function, but before consuming the following `typedef'
2109 declaration. */
2110 if (--nesting_depth == 0)
2111 {
2112 cp_lexer_consume_token (parser->lexer);
2113 break;
2114 }
2115 }
2116 /* If it the next token is a `{', then we are entering a new
2117 block. Consume the entire block. */
2118 else if (token->type == CPP_OPEN_BRACE)
2119 ++nesting_depth;
2120 /* Consume the token. */
2121 cp_lexer_consume_token (parser->lexer);
2122 }
2123}
2124
e0860732
MM
2125/* This function is called at the end of a statement or declaration.
2126 If the next token is a semicolon, it is consumed; otherwise, error
2127 recovery is attempted. */
2128
2129static void
2130cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2131{
2132 /* Look for the trailing `;'. */
2133 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2134 {
2135 /* If there is additional (erroneous) input, skip to the end of
2136 the statement. */
2137 cp_parser_skip_to_end_of_statement (parser);
2138 /* If the next token is now a `;', consume it. */
2139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2140 cp_lexer_consume_token (parser->lexer);
2141 }
2142}
2143
a723baf1
MM
2144/* Skip tokens until we have consumed an entire block, or until we
2145 have consumed a non-nested `;'. */
2146
2147static void
94edc4ab 2148cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2149{
2150 unsigned nesting_depth = 0;
2151
2152 while (true)
2153 {
2154 cp_token *token;
2155
2156 /* Peek at the next token. */
2157 token = cp_lexer_peek_token (parser->lexer);
2158 /* If we've run out of tokens, stop. */
2159 if (token->type == CPP_EOF)
2160 break;
2161 /* If the next token is a `;', we have reached the end of the
2162 statement. */
2163 if (token->type == CPP_SEMICOLON && !nesting_depth)
2164 {
2165 /* Consume the `;'. */
2166 cp_lexer_consume_token (parser->lexer);
2167 break;
2168 }
2169 /* Consume the token. */
2170 token = cp_lexer_consume_token (parser->lexer);
2171 /* If the next token is a non-nested `}', then we have reached
2172 the end of the current block. */
2173 if (token->type == CPP_CLOSE_BRACE
2174 && (nesting_depth == 0 || --nesting_depth == 0))
2175 break;
2176 /* If it the next token is a `{', then we are entering a new
2177 block. Consume the entire block. */
2178 if (token->type == CPP_OPEN_BRACE)
2179 ++nesting_depth;
2180 }
2181}
2182
2183/* Skip tokens until a non-nested closing curly brace is the next
2184 token. */
2185
2186static void
2187cp_parser_skip_to_closing_brace (cp_parser *parser)
2188{
2189 unsigned nesting_depth = 0;
2190
2191 while (true)
2192 {
2193 cp_token *token;
2194
2195 /* Peek at the next token. */
2196 token = cp_lexer_peek_token (parser->lexer);
2197 /* If we've run out of tokens, stop. */
2198 if (token->type == CPP_EOF)
2199 break;
2200 /* If the next token is a non-nested `}', then we have reached
2201 the end of the current block. */
2202 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2203 break;
2204 /* If it the next token is a `{', then we are entering a new
2205 block. Consume the entire block. */
2206 else if (token->type == CPP_OPEN_BRACE)
2207 ++nesting_depth;
2208 /* Consume the token. */
2209 cp_lexer_consume_token (parser->lexer);
2210 }
2211}
2212
2213/* Create a new C++ parser. */
2214
2215static cp_parser *
94edc4ab 2216cp_parser_new (void)
a723baf1
MM
2217{
2218 cp_parser *parser;
17211ab5
GK
2219 cp_lexer *lexer;
2220
2221 /* cp_lexer_new_main is called before calling ggc_alloc because
2222 cp_lexer_new_main might load a PCH file. */
2223 lexer = cp_lexer_new_main ();
a723baf1 2224
c68b0a84 2225 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2226 parser->lexer = lexer;
a723baf1
MM
2227 parser->context = cp_parser_context_new (NULL);
2228
2229 /* For now, we always accept GNU extensions. */
2230 parser->allow_gnu_extensions_p = 1;
2231
2232 /* The `>' token is a greater-than operator, not the end of a
2233 template-id. */
2234 parser->greater_than_is_operator_p = true;
2235
2236 parser->default_arg_ok_p = true;
2237
2238 /* We are not parsing a constant-expression. */
67c03833
JM
2239 parser->integral_constant_expression_p = false;
2240 parser->allow_non_integral_constant_expression_p = false;
2241 parser->non_integral_constant_expression_p = false;
a723baf1 2242
263ee052
MM
2243 /* We are not parsing offsetof. */
2244 parser->in_offsetof_p = false;
2245
a723baf1
MM
2246 /* Local variable names are not forbidden. */
2247 parser->local_variables_forbidden_p = false;
2248
34cd5ae7 2249 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2250 parser->in_unbraced_linkage_specification_p = false;
2251
2252 /* We are not processing a declarator. */
2253 parser->in_declarator_p = false;
2254
4bb8ca28
MM
2255 /* We are not processing a template-argument-list. */
2256 parser->in_template_argument_list_p = false;
2257
0e59b3fb
MM
2258 /* We are not in an iteration statement. */
2259 parser->in_iteration_statement_p = false;
2260
2261 /* We are not in a switch statement. */
2262 parser->in_switch_statement_p = false;
2263
4f8163b1
MM
2264 /* We are not parsing a type-id inside an expression. */
2265 parser->in_type_id_in_expr_p = false;
2266
a723baf1
MM
2267 /* The unparsed function queue is empty. */
2268 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2269
2270 /* There are no classes being defined. */
2271 parser->num_classes_being_defined = 0;
2272
2273 /* No template parameters apply. */
2274 parser->num_template_parameter_lists = 0;
2275
2276 return parser;
2277}
2278
2279/* Lexical conventions [gram.lex] */
2280
2281/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2282 identifier. */
2283
2284static tree
94edc4ab 2285cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2286{
2287 cp_token *token;
2288
2289 /* Look for the identifier. */
2290 token = cp_parser_require (parser, CPP_NAME, "identifier");
2291 /* Return the value. */
2292 return token ? token->value : error_mark_node;
2293}
2294
2295/* Basic concepts [gram.basic] */
2296
2297/* Parse a translation-unit.
2298
2299 translation-unit:
2300 declaration-seq [opt]
2301
2302 Returns TRUE if all went well. */
2303
2304static bool
94edc4ab 2305cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2306{
2307 while (true)
2308 {
2309 cp_parser_declaration_seq_opt (parser);
2310
2311 /* If there are no tokens left then all went well. */
2312 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2313 break;
2314
2315 /* Otherwise, issue an error message. */
2316 cp_parser_error (parser, "expected declaration");
2317 return false;
2318 }
2319
2320 /* Consume the EOF token. */
2321 cp_parser_require (parser, CPP_EOF, "end-of-file");
2322
2323 /* Finish up. */
2324 finish_translation_unit ();
2325
2326 /* All went well. */
2327 return true;
2328}
2329
2330/* Expressions [gram.expr] */
2331
2332/* Parse a primary-expression.
2333
2334 primary-expression:
2335 literal
2336 this
2337 ( expression )
2338 id-expression
2339
2340 GNU Extensions:
2341
2342 primary-expression:
2343 ( compound-statement )
2344 __builtin_va_arg ( assignment-expression , type-id )
2345
2346 literal:
2347 __null
2348
2349 Returns a representation of the expression.
2350
2351 *IDK indicates what kind of id-expression (if any) was present.
2352
2353 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2354 used as the operand of a pointer-to-member. In that case,
2355 *QUALIFYING_CLASS gives the class that is used as the qualifying
2356 class in the pointer-to-member. */
2357
2358static tree
2359cp_parser_primary_expression (cp_parser *parser,
b3445994 2360 cp_id_kind *idk,
a723baf1
MM
2361 tree *qualifying_class)
2362{
2363 cp_token *token;
2364
2365 /* Assume the primary expression is not an id-expression. */
b3445994 2366 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2367 /* And that it cannot be used as pointer-to-member. */
2368 *qualifying_class = NULL_TREE;
2369
2370 /* Peek at the next token. */
2371 token = cp_lexer_peek_token (parser->lexer);
2372 switch (token->type)
2373 {
2374 /* literal:
2375 integer-literal
2376 character-literal
2377 floating-literal
2378 string-literal
2379 boolean-literal */
2380 case CPP_CHAR:
2381 case CPP_WCHAR:
2382 case CPP_STRING:
2383 case CPP_WSTRING:
2384 case CPP_NUMBER:
2385 token = cp_lexer_consume_token (parser->lexer);
2386 return token->value;
2387
2388 case CPP_OPEN_PAREN:
2389 {
2390 tree expr;
2391 bool saved_greater_than_is_operator_p;
2392
2393 /* Consume the `('. */
2394 cp_lexer_consume_token (parser->lexer);
2395 /* Within a parenthesized expression, a `>' token is always
2396 the greater-than operator. */
2397 saved_greater_than_is_operator_p
2398 = parser->greater_than_is_operator_p;
2399 parser->greater_than_is_operator_p = true;
2400 /* If we see `( { ' then we are looking at the beginning of
2401 a GNU statement-expression. */
2402 if (cp_parser_allow_gnu_extensions_p (parser)
2403 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2404 {
2405 /* Statement-expressions are not allowed by the standard. */
2406 if (pedantic)
2407 pedwarn ("ISO C++ forbids braced-groups within expressions");
2408
2409 /* And they're not allowed outside of a function-body; you
2410 cannot, for example, write:
2411
2412 int i = ({ int j = 3; j + 1; });
2413
2414 at class or namespace scope. */
2415 if (!at_function_scope_p ())
2416 error ("statement-expressions are allowed only inside functions");
2417 /* Start the statement-expression. */
2418 expr = begin_stmt_expr ();
2419 /* Parse the compound-statement. */
a5bcc582 2420 cp_parser_compound_statement (parser, true);
a723baf1 2421 /* Finish up. */
303b7406 2422 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2423 }
2424 else
2425 {
2426 /* Parse the parenthesized expression. */
2427 expr = cp_parser_expression (parser);
2428 /* Let the front end know that this expression was
2429 enclosed in parentheses. This matters in case, for
2430 example, the expression is of the form `A::B', since
2431 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2432 not. */
2433 finish_parenthesized_expr (expr);
2434 }
2435 /* The `>' token might be the end of a template-id or
2436 template-parameter-list now. */
2437 parser->greater_than_is_operator_p
2438 = saved_greater_than_is_operator_p;
2439 /* Consume the `)'. */
2440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2441 cp_parser_skip_to_end_of_statement (parser);
2442
2443 return expr;
2444 }
2445
2446 case CPP_KEYWORD:
2447 switch (token->keyword)
2448 {
2449 /* These two are the boolean literals. */
2450 case RID_TRUE:
2451 cp_lexer_consume_token (parser->lexer);
2452 return boolean_true_node;
2453 case RID_FALSE:
2454 cp_lexer_consume_token (parser->lexer);
2455 return boolean_false_node;
2456
2457 /* The `__null' literal. */
2458 case RID_NULL:
2459 cp_lexer_consume_token (parser->lexer);
2460 return null_node;
2461
2462 /* Recognize the `this' keyword. */
2463 case RID_THIS:
2464 cp_lexer_consume_token (parser->lexer);
2465 if (parser->local_variables_forbidden_p)
2466 {
2467 error ("`this' may not be used in this context");
2468 return error_mark_node;
2469 }
14d22dd6 2470 /* Pointers cannot appear in constant-expressions. */
67c03833 2471 if (parser->integral_constant_expression_p)
14d22dd6 2472 {
67c03833
JM
2473 if (!parser->allow_non_integral_constant_expression_p)
2474 return cp_parser_non_integral_constant_expression ("`this'");
2475 parser->non_integral_constant_expression_p = true;
14d22dd6 2476 }
a723baf1
MM
2477 return finish_this_expr ();
2478
2479 /* The `operator' keyword can be the beginning of an
2480 id-expression. */
2481 case RID_OPERATOR:
2482 goto id_expression;
2483
2484 case RID_FUNCTION_NAME:
2485 case RID_PRETTY_FUNCTION_NAME:
2486 case RID_C99_FUNCTION_NAME:
2487 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2488 __func__ are the names of variables -- but they are
2489 treated specially. Therefore, they are handled here,
2490 rather than relying on the generic id-expression logic
34cd5ae7 2491 below. Grammatically, these names are id-expressions.
a723baf1
MM
2492
2493 Consume the token. */
2494 token = cp_lexer_consume_token (parser->lexer);
2495 /* Look up the name. */
2496 return finish_fname (token->value);
2497
2498 case RID_VA_ARG:
2499 {
2500 tree expression;
2501 tree type;
2502
2503 /* The `__builtin_va_arg' construct is used to handle
2504 `va_arg'. Consume the `__builtin_va_arg' token. */
2505 cp_lexer_consume_token (parser->lexer);
2506 /* Look for the opening `('. */
2507 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2508 /* Now, parse the assignment-expression. */
2509 expression = cp_parser_assignment_expression (parser);
2510 /* Look for the `,'. */
2511 cp_parser_require (parser, CPP_COMMA, "`,'");
2512 /* Parse the type-id. */
2513 type = cp_parser_type_id (parser);
2514 /* Look for the closing `)'. */
2515 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2516 /* Using `va_arg' in a constant-expression is not
2517 allowed. */
67c03833 2518 if (parser->integral_constant_expression_p)
14d22dd6 2519 {
67c03833
JM
2520 if (!parser->allow_non_integral_constant_expression_p)
2521 return cp_parser_non_integral_constant_expression ("`va_arg'");
2522 parser->non_integral_constant_expression_p = true;
14d22dd6 2523 }
a723baf1
MM
2524 return build_x_va_arg (expression, type);
2525 }
2526
263ee052
MM
2527 case RID_OFFSETOF:
2528 {
2529 tree expression;
2530 bool saved_in_offsetof_p;
2531
2532 /* Consume the "__offsetof__" token. */
2533 cp_lexer_consume_token (parser->lexer);
2534 /* Consume the opening `('. */
2535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2536 /* Parse the parenthesized (almost) constant-expression. */
2537 saved_in_offsetof_p = parser->in_offsetof_p;
2538 parser->in_offsetof_p = true;
2539 expression
2540 = cp_parser_constant_expression (parser,
2541 /*allow_non_constant_p=*/false,
2542 /*non_constant_p=*/NULL);
2543 parser->in_offsetof_p = saved_in_offsetof_p;
2544 /* Consume the closing ')'. */
2545 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2546
2547 return expression;
2548 }
2549
a723baf1
MM
2550 default:
2551 cp_parser_error (parser, "expected primary-expression");
2552 return error_mark_node;
2553 }
a723baf1
MM
2554
2555 /* An id-expression can start with either an identifier, a
2556 `::' as the beginning of a qualified-id, or the "operator"
2557 keyword. */
2558 case CPP_NAME:
2559 case CPP_SCOPE:
2560 case CPP_TEMPLATE_ID:
2561 case CPP_NESTED_NAME_SPECIFIER:
2562 {
2563 tree id_expression;
2564 tree decl;
b3445994 2565 const char *error_msg;
a723baf1
MM
2566
2567 id_expression:
2568 /* Parse the id-expression. */
2569 id_expression
2570 = cp_parser_id_expression (parser,
2571 /*template_keyword_p=*/false,
2572 /*check_dependency_p=*/true,
f3c2dfc6
MM
2573 /*template_p=*/NULL,
2574 /*declarator_p=*/false);
a723baf1
MM
2575 if (id_expression == error_mark_node)
2576 return error_mark_node;
2577 /* If we have a template-id, then no further lookup is
2578 required. If the template-id was for a template-class, we
2579 will sometimes have a TYPE_DECL at this point. */
2580 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2581 || TREE_CODE (id_expression) == TYPE_DECL)
2582 decl = id_expression;
2583 /* Look up the name. */
2584 else
2585 {
2586 decl = cp_parser_lookup_name_simple (parser, id_expression);
2587 /* If name lookup gives us a SCOPE_REF, then the
2588 qualifying scope was dependent. Just propagate the
2589 name. */
2590 if (TREE_CODE (decl) == SCOPE_REF)
2591 {
2592 if (TYPE_P (TREE_OPERAND (decl, 0)))
2593 *qualifying_class = TREE_OPERAND (decl, 0);
2594 return decl;
2595 }
2596 /* Check to see if DECL is a local variable in a context
2597 where that is forbidden. */
2598 if (parser->local_variables_forbidden_p
2599 && local_variable_p (decl))
2600 {
2601 /* It might be that we only found DECL because we are
2602 trying to be generous with pre-ISO scoping rules.
2603 For example, consider:
2604
2605 int i;
2606 void g() {
2607 for (int i = 0; i < 10; ++i) {}
2608 extern void f(int j = i);
2609 }
2610
2611 Here, name look up will originally find the out
2612 of scope `i'. We need to issue a warning message,
2613 but then use the global `i'. */
2614 decl = check_for_out_of_scope_variable (decl);
2615 if (local_variable_p (decl))
2616 {
2617 error ("local variable `%D' may not appear in this context",
2618 decl);
2619 return error_mark_node;
2620 }
2621 }
c006d942 2622 }
b3445994
MM
2623
2624 decl = finish_id_expression (id_expression, decl, parser->scope,
2625 idk, qualifying_class,
67c03833
JM
2626 parser->integral_constant_expression_p,
2627 parser->allow_non_integral_constant_expression_p,
2628 &parser->non_integral_constant_expression_p,
b3445994
MM
2629 &error_msg);
2630 if (error_msg)
2631 cp_parser_error (parser, error_msg);
a723baf1
MM
2632 return decl;
2633 }
2634
2635 /* Anything else is an error. */
2636 default:
2637 cp_parser_error (parser, "expected primary-expression");
2638 return error_mark_node;
2639 }
2640}
2641
2642/* Parse an id-expression.
2643
2644 id-expression:
2645 unqualified-id
2646 qualified-id
2647
2648 qualified-id:
2649 :: [opt] nested-name-specifier template [opt] unqualified-id
2650 :: identifier
2651 :: operator-function-id
2652 :: template-id
2653
2654 Return a representation of the unqualified portion of the
2655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2656 a `::' or nested-name-specifier.
2657
2658 Often, if the id-expression was a qualified-id, the caller will
2659 want to make a SCOPE_REF to represent the qualified-id. This
2660 function does not do this in order to avoid wastefully creating
2661 SCOPE_REFs when they are not required.
2662
a723baf1
MM
2663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2664 `template' keyword.
2665
2666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2667 uninstantiated templates.
2668
15d2cb19 2669 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2670 `template' keyword is used to explicitly indicate that the entity
f3c2dfc6
MM
2671 named is a template.
2672
2673 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2674 a declarator, rather than as part of an expression. */
a723baf1
MM
2675
2676static tree
2677cp_parser_id_expression (cp_parser *parser,
2678 bool template_keyword_p,
2679 bool check_dependency_p,
f3c2dfc6
MM
2680 bool *template_p,
2681 bool declarator_p)
a723baf1
MM
2682{
2683 bool global_scope_p;
2684 bool nested_name_specifier_p;
2685
2686 /* Assume the `template' keyword was not used. */
2687 if (template_p)
2688 *template_p = false;
2689
2690 /* Look for the optional `::' operator. */
2691 global_scope_p
2692 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2693 != NULL_TREE);
2694 /* Look for the optional nested-name-specifier. */
2695 nested_name_specifier_p
2696 = (cp_parser_nested_name_specifier_opt (parser,
2697 /*typename_keyword_p=*/false,
2698 check_dependency_p,
a668c6ad
MM
2699 /*type_p=*/false,
2700 /*is_declarator=*/false)
a723baf1
MM
2701 != NULL_TREE);
2702 /* If there is a nested-name-specifier, then we are looking at
2703 the first qualified-id production. */
2704 if (nested_name_specifier_p)
2705 {
2706 tree saved_scope;
2707 tree saved_object_scope;
2708 tree saved_qualifying_scope;
2709 tree unqualified_id;
2710 bool is_template;
2711
2712 /* See if the next token is the `template' keyword. */
2713 if (!template_p)
2714 template_p = &is_template;
2715 *template_p = cp_parser_optional_template_keyword (parser);
2716 /* Name lookup we do during the processing of the
2717 unqualified-id might obliterate SCOPE. */
2718 saved_scope = parser->scope;
2719 saved_object_scope = parser->object_scope;
2720 saved_qualifying_scope = parser->qualifying_scope;
2721 /* Process the final unqualified-id. */
2722 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2723 check_dependency_p,
2724 declarator_p);
a723baf1
MM
2725 /* Restore the SAVED_SCOPE for our caller. */
2726 parser->scope = saved_scope;
2727 parser->object_scope = saved_object_scope;
2728 parser->qualifying_scope = saved_qualifying_scope;
2729
2730 return unqualified_id;
2731 }
2732 /* Otherwise, if we are in global scope, then we are looking at one
2733 of the other qualified-id productions. */
2734 else if (global_scope_p)
2735 {
2736 cp_token *token;
2737 tree id;
2738
e5976695
MM
2739 /* Peek at the next token. */
2740 token = cp_lexer_peek_token (parser->lexer);
2741
2742 /* If it's an identifier, and the next token is not a "<", then
2743 we can avoid the template-id case. This is an optimization
2744 for this common case. */
2745 if (token->type == CPP_NAME
f4abade9
GB
2746 && !cp_parser_nth_token_starts_template_argument_list_p
2747 (parser, 2))
e5976695
MM
2748 return cp_parser_identifier (parser);
2749
a723baf1
MM
2750 cp_parser_parse_tentatively (parser);
2751 /* Try a template-id. */
2752 id = cp_parser_template_id (parser,
2753 /*template_keyword_p=*/false,
a668c6ad
MM
2754 /*check_dependency_p=*/true,
2755 declarator_p);
a723baf1
MM
2756 /* If that worked, we're done. */
2757 if (cp_parser_parse_definitely (parser))
2758 return id;
2759
e5976695
MM
2760 /* Peek at the next token. (Changes in the token buffer may
2761 have invalidated the pointer obtained above.) */
a723baf1
MM
2762 token = cp_lexer_peek_token (parser->lexer);
2763
2764 switch (token->type)
2765 {
2766 case CPP_NAME:
2767 return cp_parser_identifier (parser);
2768
2769 case CPP_KEYWORD:
2770 if (token->keyword == RID_OPERATOR)
2771 return cp_parser_operator_function_id (parser);
2772 /* Fall through. */
2773
2774 default:
2775 cp_parser_error (parser, "expected id-expression");
2776 return error_mark_node;
2777 }
2778 }
2779 else
2780 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
2781 /*check_dependency_p=*/true,
2782 declarator_p);
a723baf1
MM
2783}
2784
2785/* Parse an unqualified-id.
2786
2787 unqualified-id:
2788 identifier
2789 operator-function-id
2790 conversion-function-id
2791 ~ class-name
2792 template-id
2793
2794 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2795 keyword, in a construct like `A::template ...'.
2796
2797 Returns a representation of unqualified-id. For the `identifier'
2798 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2799 production a BIT_NOT_EXPR is returned; the operand of the
2800 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2801 other productions, see the documentation accompanying the
2802 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
2803 names are looked up in uninstantiated templates. If DECLARATOR_P
2804 is true, the unqualified-id is appearing as part of a declarator,
2805 rather than as part of an expression. */
a723baf1
MM
2806
2807static tree
94edc4ab
NN
2808cp_parser_unqualified_id (cp_parser* parser,
2809 bool template_keyword_p,
f3c2dfc6
MM
2810 bool check_dependency_p,
2811 bool declarator_p)
a723baf1
MM
2812{
2813 cp_token *token;
2814
2815 /* Peek at the next token. */
2816 token = cp_lexer_peek_token (parser->lexer);
2817
2818 switch (token->type)
2819 {
2820 case CPP_NAME:
2821 {
2822 tree id;
2823
2824 /* We don't know yet whether or not this will be a
2825 template-id. */
2826 cp_parser_parse_tentatively (parser);
2827 /* Try a template-id. */
2828 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2829 check_dependency_p,
2830 declarator_p);
a723baf1
MM
2831 /* If it worked, we're done. */
2832 if (cp_parser_parse_definitely (parser))
2833 return id;
2834 /* Otherwise, it's an ordinary identifier. */
2835 return cp_parser_identifier (parser);
2836 }
2837
2838 case CPP_TEMPLATE_ID:
2839 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2840 check_dependency_p,
2841 declarator_p);
a723baf1
MM
2842
2843 case CPP_COMPL:
2844 {
2845 tree type_decl;
2846 tree qualifying_scope;
2847 tree object_scope;
2848 tree scope;
2849
2850 /* Consume the `~' token. */
2851 cp_lexer_consume_token (parser->lexer);
2852 /* Parse the class-name. The standard, as written, seems to
2853 say that:
2854
2855 template <typename T> struct S { ~S (); };
2856 template <typename T> S<T>::~S() {}
2857
2858 is invalid, since `~' must be followed by a class-name, but
2859 `S<T>' is dependent, and so not known to be a class.
2860 That's not right; we need to look in uninstantiated
2861 templates. A further complication arises from:
2862
2863 template <typename T> void f(T t) {
2864 t.T::~T();
2865 }
2866
2867 Here, it is not possible to look up `T' in the scope of `T'
2868 itself. We must look in both the current scope, and the
2869 scope of the containing complete expression.
2870
2871 Yet another issue is:
2872
2873 struct S {
2874 int S;
2875 ~S();
2876 };
2877
2878 S::~S() {}
2879
2880 The standard does not seem to say that the `S' in `~S'
2881 should refer to the type `S' and not the data member
2882 `S::S'. */
2883
2884 /* DR 244 says that we look up the name after the "~" in the
2885 same scope as we looked up the qualifying name. That idea
2886 isn't fully worked out; it's more complicated than that. */
2887 scope = parser->scope;
2888 object_scope = parser->object_scope;
2889 qualifying_scope = parser->qualifying_scope;
2890
2891 /* If the name is of the form "X::~X" it's OK. */
2892 if (scope && TYPE_P (scope)
2893 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2894 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2895 == CPP_OPEN_PAREN)
2896 && (cp_lexer_peek_token (parser->lexer)->value
2897 == TYPE_IDENTIFIER (scope)))
2898 {
2899 cp_lexer_consume_token (parser->lexer);
2900 return build_nt (BIT_NOT_EXPR, scope);
2901 }
2902
2903 /* If there was an explicit qualification (S::~T), first look
2904 in the scope given by the qualification (i.e., S). */
2905 if (scope)
2906 {
2907 cp_parser_parse_tentatively (parser);
2908 type_decl = cp_parser_class_name (parser,
2909 /*typename_keyword_p=*/false,
2910 /*template_keyword_p=*/false,
2911 /*type_p=*/false,
a723baf1 2912 /*check_dependency=*/false,
a668c6ad
MM
2913 /*class_head_p=*/false,
2914 declarator_p);
a723baf1
MM
2915 if (cp_parser_parse_definitely (parser))
2916 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2917 }
2918 /* In "N::S::~S", look in "N" as well. */
2919 if (scope && qualifying_scope)
2920 {
2921 cp_parser_parse_tentatively (parser);
2922 parser->scope = qualifying_scope;
2923 parser->object_scope = NULL_TREE;
2924 parser->qualifying_scope = NULL_TREE;
2925 type_decl
2926 = cp_parser_class_name (parser,
2927 /*typename_keyword_p=*/false,
2928 /*template_keyword_p=*/false,
2929 /*type_p=*/false,
a723baf1 2930 /*check_dependency=*/false,
a668c6ad
MM
2931 /*class_head_p=*/false,
2932 declarator_p);
a723baf1
MM
2933 if (cp_parser_parse_definitely (parser))
2934 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2935 }
2936 /* In "p->S::~T", look in the scope given by "*p" as well. */
2937 else if (object_scope)
2938 {
2939 cp_parser_parse_tentatively (parser);
2940 parser->scope = object_scope;
2941 parser->object_scope = NULL_TREE;
2942 parser->qualifying_scope = NULL_TREE;
2943 type_decl
2944 = cp_parser_class_name (parser,
2945 /*typename_keyword_p=*/false,
2946 /*template_keyword_p=*/false,
2947 /*type_p=*/false,
a723baf1 2948 /*check_dependency=*/false,
a668c6ad
MM
2949 /*class_head_p=*/false,
2950 declarator_p);
a723baf1
MM
2951 if (cp_parser_parse_definitely (parser))
2952 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2953 }
2954 /* Look in the surrounding context. */
2955 parser->scope = NULL_TREE;
2956 parser->object_scope = NULL_TREE;
2957 parser->qualifying_scope = NULL_TREE;
2958 type_decl
2959 = cp_parser_class_name (parser,
2960 /*typename_keyword_p=*/false,
2961 /*template_keyword_p=*/false,
2962 /*type_p=*/false,
a723baf1 2963 /*check_dependency=*/false,
a668c6ad
MM
2964 /*class_head_p=*/false,
2965 declarator_p);
a723baf1
MM
2966 /* If an error occurred, assume that the name of the
2967 destructor is the same as the name of the qualifying
2968 class. That allows us to keep parsing after running
2969 into ill-formed destructor names. */
2970 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2971 return build_nt (BIT_NOT_EXPR, scope);
2972 else if (type_decl == error_mark_node)
2973 return error_mark_node;
2974
f3c2dfc6
MM
2975 /* [class.dtor]
2976
2977 A typedef-name that names a class shall not be used as the
2978 identifier in the declarator for a destructor declaration. */
2979 if (declarator_p
2980 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2981 && !DECL_SELF_REFERENCE_P (type_decl))
2982 error ("typedef-name `%D' used as destructor declarator",
2983 type_decl);
2984
a723baf1
MM
2985 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2986 }
2987
2988 case CPP_KEYWORD:
2989 if (token->keyword == RID_OPERATOR)
2990 {
2991 tree id;
2992
2993 /* This could be a template-id, so we try that first. */
2994 cp_parser_parse_tentatively (parser);
2995 /* Try a template-id. */
2996 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
2997 /*check_dependency_p=*/true,
2998 declarator_p);
a723baf1
MM
2999 /* If that worked, we're done. */
3000 if (cp_parser_parse_definitely (parser))
3001 return id;
3002 /* We still don't know whether we're looking at an
3003 operator-function-id or a conversion-function-id. */
3004 cp_parser_parse_tentatively (parser);
3005 /* Try an operator-function-id. */
3006 id = cp_parser_operator_function_id (parser);
3007 /* If that didn't work, try a conversion-function-id. */
3008 if (!cp_parser_parse_definitely (parser))
3009 id = cp_parser_conversion_function_id (parser);
3010
3011 return id;
3012 }
3013 /* Fall through. */
3014
3015 default:
3016 cp_parser_error (parser, "expected unqualified-id");
3017 return error_mark_node;
3018 }
3019}
3020
3021/* Parse an (optional) nested-name-specifier.
3022
3023 nested-name-specifier:
3024 class-or-namespace-name :: nested-name-specifier [opt]
3025 class-or-namespace-name :: template nested-name-specifier [opt]
3026
3027 PARSER->SCOPE should be set appropriately before this function is
3028 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3029 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3030 in name lookups.
3031
3032 Sets PARSER->SCOPE to the class (TYPE) or namespace
3033 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3034 it unchanged if there is no nested-name-specifier. Returns the new
a668c6ad
MM
3035 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3036
3037 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3038 part of a declaration and/or decl-specifier. */
a723baf1
MM
3039
3040static tree
3041cp_parser_nested_name_specifier_opt (cp_parser *parser,
3042 bool typename_keyword_p,
3043 bool check_dependency_p,
a668c6ad
MM
3044 bool type_p,
3045 bool is_declaration)
a723baf1
MM
3046{
3047 bool success = false;
3048 tree access_check = NULL_TREE;
3049 ptrdiff_t start;
2050a1bb 3050 cp_token* token;
a723baf1
MM
3051
3052 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3053 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3054 false, it may have been true before, in which case something
3055 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3056 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3057 CHECK_DEPENDENCY_P is false, we have to fall through into the
3058 main loop. */
3059 if (check_dependency_p
3060 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3061 {
3062 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3063 return parser->scope;
3064 }
3065
3066 /* Remember where the nested-name-specifier starts. */
3067 if (cp_parser_parsing_tentatively (parser)
3068 && !cp_parser_committed_to_tentative_parse (parser))
3069 {
2050a1bb 3070 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3071 start = cp_lexer_token_difference (parser->lexer,
3072 parser->lexer->first_token,
2050a1bb 3073 token);
a723baf1
MM
3074 }
3075 else
3076 start = -1;
3077
8d241e0b 3078 push_deferring_access_checks (dk_deferred);
cf22909c 3079
a723baf1
MM
3080 while (true)
3081 {
3082 tree new_scope;
3083 tree old_scope;
3084 tree saved_qualifying_scope;
a723baf1
MM
3085 bool template_keyword_p;
3086
2050a1bb
MM
3087 /* Spot cases that cannot be the beginning of a
3088 nested-name-specifier. */
3089 token = cp_lexer_peek_token (parser->lexer);
3090
3091 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3092 the already parsed nested-name-specifier. */
3093 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3094 {
3095 /* Grab the nested-name-specifier and continue the loop. */
3096 cp_parser_pre_parsed_nested_name_specifier (parser);
3097 success = true;
3098 continue;
3099 }
3100
a723baf1
MM
3101 /* Spot cases that cannot be the beginning of a
3102 nested-name-specifier. On the second and subsequent times
3103 through the loop, we look for the `template' keyword. */
f7b5ecd9 3104 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3105 ;
3106 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3107 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3108 ;
3109 else
3110 {
3111 /* If the next token is not an identifier, then it is
3112 definitely not a class-or-namespace-name. */
f7b5ecd9 3113 if (token->type != CPP_NAME)
a723baf1
MM
3114 break;
3115 /* If the following token is neither a `<' (to begin a
3116 template-id), nor a `::', then we are not looking at a
3117 nested-name-specifier. */
3118 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3119 if (token->type != CPP_SCOPE
3120 && !cp_parser_nth_token_starts_template_argument_list_p
3121 (parser, 2))
a723baf1
MM
3122 break;
3123 }
3124
3125 /* The nested-name-specifier is optional, so we parse
3126 tentatively. */
3127 cp_parser_parse_tentatively (parser);
3128
3129 /* Look for the optional `template' keyword, if this isn't the
3130 first time through the loop. */
3131 if (success)
3132 template_keyword_p = cp_parser_optional_template_keyword (parser);
3133 else
3134 template_keyword_p = false;
3135
3136 /* Save the old scope since the name lookup we are about to do
3137 might destroy it. */
3138 old_scope = parser->scope;
3139 saved_qualifying_scope = parser->qualifying_scope;
3140 /* Parse the qualifying entity. */
3141 new_scope
3142 = cp_parser_class_or_namespace_name (parser,
3143 typename_keyword_p,
3144 template_keyword_p,
3145 check_dependency_p,
a668c6ad
MM
3146 type_p,
3147 is_declaration);
a723baf1
MM
3148 /* Look for the `::' token. */
3149 cp_parser_require (parser, CPP_SCOPE, "`::'");
3150
3151 /* If we found what we wanted, we keep going; otherwise, we're
3152 done. */
3153 if (!cp_parser_parse_definitely (parser))
3154 {
3155 bool error_p = false;
3156
3157 /* Restore the OLD_SCOPE since it was valid before the
3158 failed attempt at finding the last
3159 class-or-namespace-name. */
3160 parser->scope = old_scope;
3161 parser->qualifying_scope = saved_qualifying_scope;
3162 /* If the next token is an identifier, and the one after
3163 that is a `::', then any valid interpretation would have
3164 found a class-or-namespace-name. */
3165 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3166 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3167 == CPP_SCOPE)
3168 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3169 != CPP_COMPL))
3170 {
3171 token = cp_lexer_consume_token (parser->lexer);
3172 if (!error_p)
3173 {
3174 tree decl;
3175
3176 decl = cp_parser_lookup_name_simple (parser, token->value);
3177 if (TREE_CODE (decl) == TEMPLATE_DECL)
3178 error ("`%D' used without template parameters",
3179 decl);
a723baf1 3180 else
4bb8ca28
MM
3181 cp_parser_name_lookup_error
3182 (parser, token->value, decl,
3183 "is not a class or namespace");
a723baf1
MM
3184 parser->scope = NULL_TREE;
3185 error_p = true;
eea9800f
MM
3186 /* Treat this as a successful nested-name-specifier
3187 due to:
3188
3189 [basic.lookup.qual]
3190
3191 If the name found is not a class-name (clause
3192 _class_) or namespace-name (_namespace.def_), the
3193 program is ill-formed. */
3194 success = true;
a723baf1
MM
3195 }
3196 cp_lexer_consume_token (parser->lexer);
3197 }
3198 break;
3199 }
3200
3201 /* We've found one valid nested-name-specifier. */
3202 success = true;
3203 /* Make sure we look in the right scope the next time through
3204 the loop. */
3205 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3206 ? TREE_TYPE (new_scope)
3207 : new_scope);
3208 /* If it is a class scope, try to complete it; we are about to
3209 be looking up names inside the class. */
8fbc5ae7
MM
3210 if (TYPE_P (parser->scope)
3211 /* Since checking types for dependency can be expensive,
3212 avoid doing it if the type is already complete. */
3213 && !COMPLETE_TYPE_P (parser->scope)
3214 /* Do not try to complete dependent types. */
1fb3244a 3215 && !dependent_type_p (parser->scope))
a723baf1
MM
3216 complete_type (parser->scope);
3217 }
3218
cf22909c
KL
3219 /* Retrieve any deferred checks. Do not pop this access checks yet
3220 so the memory will not be reclaimed during token replacing below. */
3221 access_check = get_deferred_access_checks ();
3222
a723baf1
MM
3223 /* If parsing tentatively, replace the sequence of tokens that makes
3224 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3225 token. That way, should we re-parse the token stream, we will
3226 not have to repeat the effort required to do the parse, nor will
3227 we issue duplicate error messages. */
3228 if (success && start >= 0)
3229 {
a723baf1
MM
3230 /* Find the token that corresponds to the start of the
3231 template-id. */
3232 token = cp_lexer_advance_token (parser->lexer,
3233 parser->lexer->first_token,
3234 start);
3235
a723baf1
MM
3236 /* Reset the contents of the START token. */
3237 token->type = CPP_NESTED_NAME_SPECIFIER;
3238 token->value = build_tree_list (access_check, parser->scope);
3239 TREE_TYPE (token->value) = parser->qualifying_scope;
3240 token->keyword = RID_MAX;
3241 /* Purge all subsequent tokens. */
3242 cp_lexer_purge_tokens_after (parser->lexer, token);
3243 }
3244
cf22909c 3245 pop_deferring_access_checks ();
a723baf1
MM
3246 return success ? parser->scope : NULL_TREE;
3247}
3248
3249/* Parse a nested-name-specifier. See
3250 cp_parser_nested_name_specifier_opt for details. This function
3251 behaves identically, except that it will an issue an error if no
3252 nested-name-specifier is present, and it will return
3253 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3254 is present. */
3255
3256static tree
3257cp_parser_nested_name_specifier (cp_parser *parser,
3258 bool typename_keyword_p,
3259 bool check_dependency_p,
a668c6ad
MM
3260 bool type_p,
3261 bool is_declaration)
a723baf1
MM
3262{
3263 tree scope;
3264
3265 /* Look for the nested-name-specifier. */
3266 scope = cp_parser_nested_name_specifier_opt (parser,
3267 typename_keyword_p,
3268 check_dependency_p,
a668c6ad
MM
3269 type_p,
3270 is_declaration);
a723baf1
MM
3271 /* If it was not present, issue an error message. */
3272 if (!scope)
3273 {
3274 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3275 parser->scope = NULL_TREE;
a723baf1
MM
3276 return error_mark_node;
3277 }
3278
3279 return scope;
3280}
3281
3282/* Parse a class-or-namespace-name.
3283
3284 class-or-namespace-name:
3285 class-name
3286 namespace-name
3287
3288 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3289 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3290 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3291 TYPE_P is TRUE iff the next name should be taken as a class-name,
3292 even the same name is declared to be another entity in the same
3293 scope.
3294
3295 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3296 specified by the class-or-namespace-name. If neither is found the
3297 ERROR_MARK_NODE is returned. */
a723baf1
MM
3298
3299static tree
3300cp_parser_class_or_namespace_name (cp_parser *parser,
3301 bool typename_keyword_p,
3302 bool template_keyword_p,
3303 bool check_dependency_p,
a668c6ad
MM
3304 bool type_p,
3305 bool is_declaration)
a723baf1
MM
3306{
3307 tree saved_scope;
3308 tree saved_qualifying_scope;
3309 tree saved_object_scope;
3310 tree scope;
eea9800f 3311 bool only_class_p;
a723baf1 3312
a723baf1
MM
3313 /* Before we try to parse the class-name, we must save away the
3314 current PARSER->SCOPE since cp_parser_class_name will destroy
3315 it. */
3316 saved_scope = parser->scope;
3317 saved_qualifying_scope = parser->qualifying_scope;
3318 saved_object_scope = parser->object_scope;
eea9800f
MM
3319 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3320 there is no need to look for a namespace-name. */
bbaab916 3321 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3322 if (!only_class_p)
3323 cp_parser_parse_tentatively (parser);
a723baf1
MM
3324 scope = cp_parser_class_name (parser,
3325 typename_keyword_p,
3326 template_keyword_p,
3327 type_p,
a723baf1 3328 check_dependency_p,
a668c6ad
MM
3329 /*class_head_p=*/false,
3330 is_declaration);
a723baf1 3331 /* If that didn't work, try for a namespace-name. */
eea9800f 3332 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3333 {
3334 /* Restore the saved scope. */
3335 parser->scope = saved_scope;
3336 parser->qualifying_scope = saved_qualifying_scope;
3337 parser->object_scope = saved_object_scope;
eea9800f
MM
3338 /* If we are not looking at an identifier followed by the scope
3339 resolution operator, then this is not part of a
3340 nested-name-specifier. (Note that this function is only used
3341 to parse the components of a nested-name-specifier.) */
3342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3343 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3344 return error_mark_node;
a723baf1
MM
3345 scope = cp_parser_namespace_name (parser);
3346 }
3347
3348 return scope;
3349}
3350
3351/* Parse a postfix-expression.
3352
3353 postfix-expression:
3354 primary-expression
3355 postfix-expression [ expression ]
3356 postfix-expression ( expression-list [opt] )
3357 simple-type-specifier ( expression-list [opt] )
3358 typename :: [opt] nested-name-specifier identifier
3359 ( expression-list [opt] )
3360 typename :: [opt] nested-name-specifier template [opt] template-id
3361 ( expression-list [opt] )
3362 postfix-expression . template [opt] id-expression
3363 postfix-expression -> template [opt] id-expression
3364 postfix-expression . pseudo-destructor-name
3365 postfix-expression -> pseudo-destructor-name
3366 postfix-expression ++
3367 postfix-expression --
3368 dynamic_cast < type-id > ( expression )
3369 static_cast < type-id > ( expression )
3370 reinterpret_cast < type-id > ( expression )
3371 const_cast < type-id > ( expression )
3372 typeid ( expression )
3373 typeid ( type-id )
3374
3375 GNU Extension:
3376
3377 postfix-expression:
3378 ( type-id ) { initializer-list , [opt] }
3379
3380 This extension is a GNU version of the C99 compound-literal
3381 construct. (The C99 grammar uses `type-name' instead of `type-id',
3382 but they are essentially the same concept.)
3383
3384 If ADDRESS_P is true, the postfix expression is the operand of the
3385 `&' operator.
3386
3387 Returns a representation of the expression. */
3388
3389static tree
3390cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3391{
3392 cp_token *token;
3393 enum rid keyword;
b3445994 3394 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3395 tree postfix_expression = NULL_TREE;
3396 /* Non-NULL only if the current postfix-expression can be used to
3397 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3398 class used to qualify the member. */
3399 tree qualifying_class = NULL_TREE;
a723baf1
MM
3400
3401 /* Peek at the next token. */
3402 token = cp_lexer_peek_token (parser->lexer);
3403 /* Some of the productions are determined by keywords. */
3404 keyword = token->keyword;
3405 switch (keyword)
3406 {
3407 case RID_DYNCAST:
3408 case RID_STATCAST:
3409 case RID_REINTCAST:
3410 case RID_CONSTCAST:
3411 {
3412 tree type;
3413 tree expression;
3414 const char *saved_message;
3415
3416 /* All of these can be handled in the same way from the point
3417 of view of parsing. Begin by consuming the token
3418 identifying the cast. */
3419 cp_lexer_consume_token (parser->lexer);
3420
3421 /* New types cannot be defined in the cast. */
3422 saved_message = parser->type_definition_forbidden_message;
3423 parser->type_definition_forbidden_message
3424 = "types may not be defined in casts";
3425
3426 /* Look for the opening `<'. */
3427 cp_parser_require (parser, CPP_LESS, "`<'");
3428 /* Parse the type to which we are casting. */
3429 type = cp_parser_type_id (parser);
3430 /* Look for the closing `>'. */
3431 cp_parser_require (parser, CPP_GREATER, "`>'");
3432 /* Restore the old message. */
3433 parser->type_definition_forbidden_message = saved_message;
3434
3435 /* And the expression which is being cast. */
3436 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3437 expression = cp_parser_expression (parser);
3438 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3439
14d22dd6
MM
3440 /* Only type conversions to integral or enumeration types
3441 can be used in constant-expressions. */
67c03833 3442 if (parser->integral_constant_expression_p
14d22dd6 3443 && !dependent_type_p (type)
263ee052
MM
3444 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3445 /* A cast to pointer or reference type is allowed in the
3446 implementation of "offsetof". */
3447 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
14d22dd6 3448 {
67c03833
JM
3449 if (!parser->allow_non_integral_constant_expression_p)
3450 return (cp_parser_non_integral_constant_expression
14d22dd6
MM
3451 ("a cast to a type other than an integral or "
3452 "enumeration type"));
67c03833 3453 parser->non_integral_constant_expression_p = true;
14d22dd6
MM
3454 }
3455
a723baf1
MM
3456 switch (keyword)
3457 {
3458 case RID_DYNCAST:
3459 postfix_expression
3460 = build_dynamic_cast (type, expression);
3461 break;
3462 case RID_STATCAST:
3463 postfix_expression
3464 = build_static_cast (type, expression);
3465 break;
3466 case RID_REINTCAST:
3467 postfix_expression
3468 = build_reinterpret_cast (type, expression);
3469 break;
3470 case RID_CONSTCAST:
3471 postfix_expression
3472 = build_const_cast (type, expression);
3473 break;
3474 default:
3475 abort ();
3476 }
3477 }
3478 break;
3479
3480 case RID_TYPEID:
3481 {
3482 tree type;
3483 const char *saved_message;
4f8163b1 3484 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3485
3486 /* Consume the `typeid' token. */
3487 cp_lexer_consume_token (parser->lexer);
3488 /* Look for the `(' token. */
3489 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3490 /* Types cannot be defined in a `typeid' expression. */
3491 saved_message = parser->type_definition_forbidden_message;
3492 parser->type_definition_forbidden_message
3493 = "types may not be defined in a `typeid\' expression";
3494 /* We can't be sure yet whether we're looking at a type-id or an
3495 expression. */
3496 cp_parser_parse_tentatively (parser);
3497 /* Try a type-id first. */
4f8163b1
MM
3498 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3499 parser->in_type_id_in_expr_p = true;
a723baf1 3500 type = cp_parser_type_id (parser);
4f8163b1 3501 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3502 /* Look for the `)' token. Otherwise, we can't be sure that
3503 we're not looking at an expression: consider `typeid (int
3504 (3))', for example. */
3505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3506 /* If all went well, simply lookup the type-id. */
3507 if (cp_parser_parse_definitely (parser))
3508 postfix_expression = get_typeid (type);
3509 /* Otherwise, fall back to the expression variant. */
3510 else
3511 {
3512 tree expression;
3513
3514 /* Look for an expression. */
3515 expression = cp_parser_expression (parser);
3516 /* Compute its typeid. */
3517 postfix_expression = build_typeid (expression);
3518 /* Look for the `)' token. */
3519 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3520 }
3521
3522 /* Restore the saved message. */
3523 parser->type_definition_forbidden_message = saved_message;
3524 }
3525 break;
3526
3527 case RID_TYPENAME:
3528 {
3529 bool template_p = false;
3530 tree id;
3531 tree type;
3532
3533 /* Consume the `typename' token. */
3534 cp_lexer_consume_token (parser->lexer);
3535 /* Look for the optional `::' operator. */
3536 cp_parser_global_scope_opt (parser,
3537 /*current_scope_valid_p=*/false);
3538 /* Look for the nested-name-specifier. */
3539 cp_parser_nested_name_specifier (parser,
3540 /*typename_keyword_p=*/true,
3541 /*check_dependency_p=*/true,
a668c6ad
MM
3542 /*type_p=*/true,
3543 /*is_declaration=*/true);
a723baf1
MM
3544 /* Look for the optional `template' keyword. */
3545 template_p = cp_parser_optional_template_keyword (parser);
3546 /* We don't know whether we're looking at a template-id or an
3547 identifier. */
3548 cp_parser_parse_tentatively (parser);
3549 /* Try a template-id. */
3550 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3551 /*check_dependency_p=*/true,
3552 /*is_declaration=*/true);
a723baf1
MM
3553 /* If that didn't work, try an identifier. */
3554 if (!cp_parser_parse_definitely (parser))
3555 id = cp_parser_identifier (parser);
3556 /* Create a TYPENAME_TYPE to represent the type to which the
3557 functional cast is being performed. */
3558 type = make_typename_type (parser->scope, id,
3559 /*complain=*/1);
3560
3561 postfix_expression = cp_parser_functional_cast (parser, type);
3562 }
3563 break;
3564
3565 default:
3566 {
3567 tree type;
3568
3569 /* If the next thing is a simple-type-specifier, we may be
3570 looking at a functional cast. We could also be looking at
3571 an id-expression. So, we try the functional cast, and if
3572 that doesn't work we fall back to the primary-expression. */
3573 cp_parser_parse_tentatively (parser);
3574 /* Look for the simple-type-specifier. */
3575 type = cp_parser_simple_type_specifier (parser,
4b0d3cbe
MM
3576 CP_PARSER_FLAGS_NONE,
3577 /*identifier_p=*/false);
a723baf1
MM
3578 /* Parse the cast itself. */
3579 if (!cp_parser_error_occurred (parser))
3580 postfix_expression
3581 = cp_parser_functional_cast (parser, type);
3582 /* If that worked, we're done. */
3583 if (cp_parser_parse_definitely (parser))
3584 break;
3585
3586 /* If the functional-cast didn't work out, try a
3587 compound-literal. */
14d22dd6
MM
3588 if (cp_parser_allow_gnu_extensions_p (parser)
3589 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3590 {
3591 tree initializer_list = NULL_TREE;
4f8163b1 3592 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3593
3594 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3595 /* Consume the `('. */
3596 cp_lexer_consume_token (parser->lexer);
3597 /* Parse the type. */
4f8163b1
MM
3598 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3599 parser->in_type_id_in_expr_p = true;
14d22dd6 3600 type = cp_parser_type_id (parser);
4f8163b1 3601 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3602 /* Look for the `)'. */
3603 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3604 /* Look for the `{'. */
3605 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3606 /* If things aren't going well, there's no need to
3607 keep going. */
3608 if (!cp_parser_error_occurred (parser))
a723baf1 3609 {
39703eb9 3610 bool non_constant_p;
14d22dd6
MM
3611 /* Parse the initializer-list. */
3612 initializer_list
39703eb9 3613 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3614 /* Allow a trailing `,'. */
3615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3616 cp_lexer_consume_token (parser->lexer);
3617 /* Look for the final `}'. */
3618 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3619 }
3620 /* If that worked, we're definitely looking at a
3621 compound-literal expression. */
3622 if (cp_parser_parse_definitely (parser))
3623 {
3624 /* Warn the user that a compound literal is not
3625 allowed in standard C++. */
3626 if (pedantic)
3627 pedwarn ("ISO C++ forbids compound-literals");
3628 /* Form the representation of the compound-literal. */
3629 postfix_expression
3630 = finish_compound_literal (type, initializer_list);
3631 break;
3632 }
3633 }
3634
3635 /* It must be a primary-expression. */
3636 postfix_expression = cp_parser_primary_expression (parser,
3637 &idk,
3638 &qualifying_class);
3639 }
3640 break;
3641 }
3642
ee76b931
MM
3643 /* If we were avoiding committing to the processing of a
3644 qualified-id until we knew whether or not we had a
3645 pointer-to-member, we now know. */
089d6ea7 3646 if (qualifying_class)
a723baf1 3647 {
ee76b931 3648 bool done;
a723baf1 3649
ee76b931
MM
3650 /* Peek at the next token. */
3651 token = cp_lexer_peek_token (parser->lexer);
3652 done = (token->type != CPP_OPEN_SQUARE
3653 && token->type != CPP_OPEN_PAREN
3654 && token->type != CPP_DOT
3655 && token->type != CPP_DEREF
3656 && token->type != CPP_PLUS_PLUS
3657 && token->type != CPP_MINUS_MINUS);
3658
3659 postfix_expression = finish_qualified_id_expr (qualifying_class,
3660 postfix_expression,
3661 done,
3662 address_p);
3663 if (done)
3664 return postfix_expression;
a723baf1
MM
3665 }
3666
a723baf1
MM
3667 /* Keep looping until the postfix-expression is complete. */
3668 while (true)
3669 {
10b1d5e7
MM
3670 if (idk == CP_ID_KIND_UNQUALIFIED
3671 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3672 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994
MM
3673 /* It is not a Koenig lookup function call. */
3674 postfix_expression
3675 = unqualified_name_lookup_error (postfix_expression);
a723baf1
MM
3676
3677 /* Peek at the next token. */
3678 token = cp_lexer_peek_token (parser->lexer);
3679
3680 switch (token->type)
3681 {
3682 case CPP_OPEN_SQUARE:
3683 /* postfix-expression [ expression ] */
3684 {
3685 tree index;
3686
3687 /* Consume the `[' token. */
3688 cp_lexer_consume_token (parser->lexer);
3689 /* Parse the index expression. */
3690 index = cp_parser_expression (parser);
3691 /* Look for the closing `]'. */
3692 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3693
3694 /* Build the ARRAY_REF. */
3695 postfix_expression
3696 = grok_array_decl (postfix_expression, index);
b3445994 3697 idk = CP_ID_KIND_NONE;
a5ac3982
MM
3698 /* Array references are not permitted in
3699 constant-expressions. */
67c03833 3700 if (parser->integral_constant_expression_p)
a5ac3982 3701 {
67c03833 3702 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3703 postfix_expression
67c03833
JM
3704 = cp_parser_non_integral_constant_expression ("an array reference");
3705 parser->non_integral_constant_expression_p = true;
a5ac3982 3706 }
a723baf1
MM
3707 }
3708 break;
3709
3710 case CPP_OPEN_PAREN:
3711 /* postfix-expression ( expression-list [opt] ) */
3712 {
6d80c4b9 3713 bool koenig_p;
39703eb9
MM
3714 tree args = (cp_parser_parenthesized_expression_list
3715 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3716
7efa3e22
NS
3717 if (args == error_mark_node)
3718 {
3719 postfix_expression = error_mark_node;
3720 break;
3721 }
3722
14d22dd6
MM
3723 /* Function calls are not permitted in
3724 constant-expressions. */
67c03833 3725 if (parser->integral_constant_expression_p)
14d22dd6 3726 {
67c03833 3727 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982
MM
3728 {
3729 postfix_expression
67c03833 3730 = cp_parser_non_integral_constant_expression ("a function call");
a5ac3982
MM
3731 break;
3732 }
67c03833 3733 parser->non_integral_constant_expression_p = true;
14d22dd6 3734 }
a723baf1 3735
6d80c4b9 3736 koenig_p = false;
399dedb9
NS
3737 if (idk == CP_ID_KIND_UNQUALIFIED)
3738 {
3739 if (args
3740 && (is_overloaded_fn (postfix_expression)
3741 || DECL_P (postfix_expression)
3742 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
3743 {
3744 koenig_p = true;
3745 postfix_expression
3746 = perform_koenig_lookup (postfix_expression, args);
3747 }
399dedb9
NS
3748 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3749 postfix_expression
3750 = unqualified_fn_lookup_error (postfix_expression);
3751 }
3752
d17811fd 3753 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 3754 {
d17811fd
MM
3755 tree instance = TREE_OPERAND (postfix_expression, 0);
3756 tree fn = TREE_OPERAND (postfix_expression, 1);
3757
3758 if (processing_template_decl
3759 && (type_dependent_expression_p (instance)
3760 || (!BASELINK_P (fn)
3761 && TREE_CODE (fn) != FIELD_DECL)
584672ee 3762 || type_dependent_expression_p (fn)
d17811fd
MM
3763 || any_type_dependent_arguments_p (args)))
3764 {
3765 postfix_expression
3766 = build_min_nt (CALL_EXPR, postfix_expression, args);
3767 break;
3768 }
9f880ef9
MM
3769
3770 if (BASELINK_P (fn))
3771 postfix_expression
3772 = (build_new_method_call
3773 (instance, fn, args, NULL_TREE,
3774 (idk == CP_ID_KIND_QUALIFIED
3775 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3776 else
3777 postfix_expression
3778 = finish_call_expr (postfix_expression, args,
3779 /*disallow_virtual=*/false,
3780 /*koenig_p=*/false);
a723baf1 3781 }
d17811fd
MM
3782 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3783 || TREE_CODE (postfix_expression) == MEMBER_REF
3784 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
3785 postfix_expression = (build_offset_ref_call_from_tree
3786 (postfix_expression, args));
b3445994 3787 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
3788 /* A call to a static class member, or a namespace-scope
3789 function. */
3790 postfix_expression
3791 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3792 /*disallow_virtual=*/true,
3793 koenig_p);
a723baf1 3794 else
2050a1bb
MM
3795 /* All other function calls. */
3796 postfix_expression
3797 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
3798 /*disallow_virtual=*/false,
3799 koenig_p);
a723baf1
MM
3800
3801 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 3802 idk = CP_ID_KIND_NONE;
a723baf1
MM
3803 }
3804 break;
3805
3806 case CPP_DOT:
3807 case CPP_DEREF:
3808 /* postfix-expression . template [opt] id-expression
3809 postfix-expression . pseudo-destructor-name
3810 postfix-expression -> template [opt] id-expression
3811 postfix-expression -> pseudo-destructor-name */
3812 {
3813 tree name;
3814 bool dependent_p;
3815 bool template_p;
3816 tree scope = NULL_TREE;
a5ac3982 3817 enum cpp_ttype token_type = token->type;
a723baf1
MM
3818
3819 /* If this is a `->' operator, dereference the pointer. */
3820 if (token->type == CPP_DEREF)
3821 postfix_expression = build_x_arrow (postfix_expression);
3822 /* Check to see whether or not the expression is
3823 type-dependent. */
bbaab916 3824 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
3825 /* The identifier following the `->' or `.' is not
3826 qualified. */
3827 parser->scope = NULL_TREE;
3828 parser->qualifying_scope = NULL_TREE;
3829 parser->object_scope = NULL_TREE;
b3445994 3830 idk = CP_ID_KIND_NONE;
a723baf1
MM
3831 /* Enter the scope corresponding to the type of the object
3832 given by the POSTFIX_EXPRESSION. */
3833 if (!dependent_p
3834 && TREE_TYPE (postfix_expression) != NULL_TREE)
3835 {
3836 scope = TREE_TYPE (postfix_expression);
3837 /* According to the standard, no expression should
3838 ever have reference type. Unfortunately, we do not
3839 currently match the standard in this respect in
3840 that our internal representation of an expression
3841 may have reference type even when the standard says
3842 it does not. Therefore, we have to manually obtain
3843 the underlying type here. */
ee76b931 3844 scope = non_reference (scope);
a723baf1
MM
3845 /* The type of the POSTFIX_EXPRESSION must be
3846 complete. */
3847 scope = complete_type_or_else (scope, NULL_TREE);
3848 /* Let the name lookup machinery know that we are
3849 processing a class member access expression. */
3850 parser->context->object_type = scope;
3851 /* If something went wrong, we want to be able to
3852 discern that case, as opposed to the case where
3853 there was no SCOPE due to the type of expression
3854 being dependent. */
3855 if (!scope)
3856 scope = error_mark_node;
be799b1e
MM
3857 /* If the SCOPE was erroneous, make the various
3858 semantic analysis functions exit quickly -- and
3859 without issuing additional error messages. */
3860 if (scope == error_mark_node)
3861 postfix_expression = error_mark_node;
a723baf1
MM
3862 }
3863
3864 /* Consume the `.' or `->' operator. */
3865 cp_lexer_consume_token (parser->lexer);
3866 /* If the SCOPE is not a scalar type, we are looking at an
3867 ordinary class member access expression, rather than a
3868 pseudo-destructor-name. */
3869 if (!scope || !SCALAR_TYPE_P (scope))
3870 {
3871 template_p = cp_parser_optional_template_keyword (parser);
3872 /* Parse the id-expression. */
3873 name = cp_parser_id_expression (parser,
3874 template_p,
3875 /*check_dependency_p=*/true,
f3c2dfc6
MM
3876 /*template_p=*/NULL,
3877 /*declarator_p=*/false);
a723baf1
MM
3878 /* In general, build a SCOPE_REF if the member name is
3879 qualified. However, if the name was not dependent
3880 and has already been resolved; there is no need to
3881 build the SCOPE_REF. For example;
3882
3883 struct X { void f(); };
3884 template <typename T> void f(T* t) { t->X::f(); }
3885
d17811fd
MM
3886 Even though "t" is dependent, "X::f" is not and has
3887 been resolved to a BASELINK; there is no need to
a723baf1 3888 include scope information. */
a6bd211d
JM
3889
3890 /* But we do need to remember that there was an explicit
3891 scope for virtual function calls. */
3892 if (parser->scope)
b3445994 3893 idk = CP_ID_KIND_QUALIFIED;
a6bd211d 3894
a723baf1
MM
3895 if (name != error_mark_node
3896 && !BASELINK_P (name)
3897 && parser->scope)
3898 {
3899 name = build_nt (SCOPE_REF, parser->scope, name);
3900 parser->scope = NULL_TREE;
3901 parser->qualifying_scope = NULL_TREE;
3902 parser->object_scope = NULL_TREE;
3903 }
3904 postfix_expression
3905 = finish_class_member_access_expr (postfix_expression, name);
3906 }
3907 /* Otherwise, try the pseudo-destructor-name production. */
3908 else
3909 {
90808894 3910 tree s = NULL_TREE;
a723baf1
MM
3911 tree type;
3912
3913 /* Parse the pseudo-destructor-name. */
3914 cp_parser_pseudo_destructor_name (parser, &s, &type);
3915 /* Form the call. */
3916 postfix_expression
3917 = finish_pseudo_destructor_expr (postfix_expression,
3918 s, TREE_TYPE (type));
3919 }
3920
3921 /* We no longer need to look up names in the scope of the
3922 object on the left-hand side of the `.' or `->'
3923 operator. */
3924 parser->context->object_type = NULL_TREE;
a5ac3982 3925 /* These operators may not appear in constant-expressions. */
67c03833 3926 if (parser->integral_constant_expression_p
263ee052 3927 /* The "->" operator is allowed in the implementation
643aee72
MM
3928 of "offsetof". The "." operator may appear in the
3929 name of the member. */
3930 && !parser->in_offsetof_p)
a5ac3982 3931 {
67c03833 3932 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3933 postfix_expression
67c03833 3934 = (cp_parser_non_integral_constant_expression
a5ac3982 3935 (token_type == CPP_DEREF ? "'->'" : "`.'"));
67c03833 3936 parser->non_integral_constant_expression_p = true;
a5ac3982 3937 }
a723baf1
MM
3938 }
3939 break;
3940
3941 case CPP_PLUS_PLUS:
3942 /* postfix-expression ++ */
3943 /* Consume the `++' token. */
3944 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3945 /* Generate a representation for the complete expression. */
3946 postfix_expression
3947 = finish_increment_expr (postfix_expression,
3948 POSTINCREMENT_EXPR);
14d22dd6 3949 /* Increments may not appear in constant-expressions. */
67c03833 3950 if (parser->integral_constant_expression_p)
14d22dd6 3951 {
67c03833 3952 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3953 postfix_expression
67c03833
JM
3954 = cp_parser_non_integral_constant_expression ("an increment");
3955 parser->non_integral_constant_expression_p = true;
14d22dd6 3956 }
b3445994 3957 idk = CP_ID_KIND_NONE;
a723baf1
MM
3958 break;
3959
3960 case CPP_MINUS_MINUS:
3961 /* postfix-expression -- */
3962 /* Consume the `--' token. */
3963 cp_lexer_consume_token (parser->lexer);
a5ac3982
MM
3964 /* Generate a representation for the complete expression. */
3965 postfix_expression
3966 = finish_increment_expr (postfix_expression,
3967 POSTDECREMENT_EXPR);
14d22dd6 3968 /* Decrements may not appear in constant-expressions. */
67c03833 3969 if (parser->integral_constant_expression_p)
14d22dd6 3970 {
67c03833 3971 if (!parser->allow_non_integral_constant_expression_p)
a5ac3982 3972 postfix_expression
67c03833
JM
3973 = cp_parser_non_integral_constant_expression ("a decrement");
3974 parser->non_integral_constant_expression_p = true;
14d22dd6 3975 }
b3445994 3976 idk = CP_ID_KIND_NONE;
a723baf1
MM
3977 break;
3978
3979 default:
3980 return postfix_expression;
3981 }
3982 }
3983
3984 /* We should never get here. */
3985 abort ();
3986 return error_mark_node;
3987}
3988
7efa3e22 3989/* Parse a parenthesized expression-list.
a723baf1
MM
3990
3991 expression-list:
3992 assignment-expression
3993 expression-list, assignment-expression
3994
7efa3e22
NS
3995 attribute-list:
3996 expression-list
3997 identifier
3998 identifier, expression-list
3999
a723baf1
MM
4000 Returns a TREE_LIST. The TREE_VALUE of each node is a
4001 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4002 is returned even if there is only a single expression in the list.
4003 error_mark_node is returned if the ( and or ) are
4004 missing. NULL_TREE is returned on no expressions. The parentheses
4005 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4006 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4007 indicates whether or not all of the expressions in the list were
4008 constant. */
a723baf1
MM
4009
4010static tree
39703eb9
MM
4011cp_parser_parenthesized_expression_list (cp_parser* parser,
4012 bool is_attribute_list,
4013 bool *non_constant_p)
a723baf1
MM
4014{
4015 tree expression_list = NULL_TREE;
7efa3e22 4016 tree identifier = NULL_TREE;
39703eb9
MM
4017
4018 /* Assume all the expressions will be constant. */
4019 if (non_constant_p)
4020 *non_constant_p = false;
4021
7efa3e22
NS
4022 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4023 return error_mark_node;
4024
a723baf1 4025 /* Consume expressions until there are no more. */
7efa3e22
NS
4026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4027 while (true)
4028 {
4029 tree expr;
4030
4031 /* At the beginning of attribute lists, check to see if the
4032 next token is an identifier. */
4033 if (is_attribute_list
4034 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4035 {
4036 cp_token *token;
4037
4038 /* Consume the identifier. */
4039 token = cp_lexer_consume_token (parser->lexer);
4040 /* Save the identifier. */
4041 identifier = token->value;
4042 }
4043 else
4044 {
4045 /* Parse the next assignment-expression. */
39703eb9
MM
4046 if (non_constant_p)
4047 {
4048 bool expr_non_constant_p;
4049 expr = (cp_parser_constant_expression
4050 (parser, /*allow_non_constant_p=*/true,
4051 &expr_non_constant_p));
4052 if (expr_non_constant_p)
4053 *non_constant_p = true;
4054 }
4055 else
4056 expr = cp_parser_assignment_expression (parser);
a723baf1 4057
7efa3e22
NS
4058 /* Add it to the list. We add error_mark_node
4059 expressions to the list, so that we can still tell if
4060 the correct form for a parenthesized expression-list
4061 is found. That gives better errors. */
4062 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4063
7efa3e22
NS
4064 if (expr == error_mark_node)
4065 goto skip_comma;
4066 }
a723baf1 4067
7efa3e22
NS
4068 /* After the first item, attribute lists look the same as
4069 expression lists. */
4070 is_attribute_list = false;
4071
4072 get_comma:;
4073 /* If the next token isn't a `,', then we are done. */
4074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4075 break;
4076
4077 /* Otherwise, consume the `,' and keep going. */
4078 cp_lexer_consume_token (parser->lexer);
4079 }
4080
4081 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4082 {
4083 int ending;
4084
4085 skip_comma:;
4086 /* We try and resync to an unnested comma, as that will give the
4087 user better diagnostics. */
4bb8ca28
MM
4088 ending = cp_parser_skip_to_closing_parenthesis (parser,
4089 /*recovering=*/true,
4090 /*or_comma=*/true,
a668c6ad 4091 /*consume_paren=*/true);
7efa3e22
NS
4092 if (ending < 0)
4093 goto get_comma;
4094 if (!ending)
4095 return error_mark_node;
a723baf1
MM
4096 }
4097
4098 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4099 expression_list = nreverse (expression_list);
4100 if (identifier)
4101 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4102
4103 return expression_list;
a723baf1
MM
4104}
4105
4106/* Parse a pseudo-destructor-name.
4107
4108 pseudo-destructor-name:
4109 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4110 :: [opt] nested-name-specifier template template-id :: ~ type-name
4111 :: [opt] nested-name-specifier [opt] ~ type-name
4112
4113 If either of the first two productions is used, sets *SCOPE to the
4114 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4115 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4116 or ERROR_MARK_NODE if no type-name is present. */
4117
4118static void
94edc4ab
NN
4119cp_parser_pseudo_destructor_name (cp_parser* parser,
4120 tree* scope,
4121 tree* type)
a723baf1
MM
4122{
4123 bool nested_name_specifier_p;
4124
4125 /* Look for the optional `::' operator. */
4126 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4127 /* Look for the optional nested-name-specifier. */
4128 nested_name_specifier_p
4129 = (cp_parser_nested_name_specifier_opt (parser,
4130 /*typename_keyword_p=*/false,
4131 /*check_dependency_p=*/true,
a668c6ad
MM
4132 /*type_p=*/false,
4133 /*is_declaration=*/true)
a723baf1
MM
4134 != NULL_TREE);
4135 /* Now, if we saw a nested-name-specifier, we might be doing the
4136 second production. */
4137 if (nested_name_specifier_p
4138 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4139 {
4140 /* Consume the `template' keyword. */
4141 cp_lexer_consume_token (parser->lexer);
4142 /* Parse the template-id. */
4143 cp_parser_template_id (parser,
4144 /*template_keyword_p=*/true,
a668c6ad
MM
4145 /*check_dependency_p=*/false,
4146 /*is_declaration=*/true);
a723baf1
MM
4147 /* Look for the `::' token. */
4148 cp_parser_require (parser, CPP_SCOPE, "`::'");
4149 }
4150 /* If the next token is not a `~', then there might be some
9bcb9aae 4151 additional qualification. */
a723baf1
MM
4152 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4153 {
4154 /* Look for the type-name. */
4155 *scope = TREE_TYPE (cp_parser_type_name (parser));
4156 /* Look for the `::' token. */
4157 cp_parser_require (parser, CPP_SCOPE, "`::'");
4158 }
4159 else
4160 *scope = NULL_TREE;
4161
4162 /* Look for the `~'. */
4163 cp_parser_require (parser, CPP_COMPL, "`~'");
4164 /* Look for the type-name again. We are not responsible for
4165 checking that it matches the first type-name. */
4166 *type = cp_parser_type_name (parser);
4167}
4168
4169/* Parse a unary-expression.
4170
4171 unary-expression:
4172 postfix-expression
4173 ++ cast-expression
4174 -- cast-expression
4175 unary-operator cast-expression
4176 sizeof unary-expression
4177 sizeof ( type-id )
4178 new-expression
4179 delete-expression
4180
4181 GNU Extensions:
4182
4183 unary-expression:
4184 __extension__ cast-expression
4185 __alignof__ unary-expression
4186 __alignof__ ( type-id )
4187 __real__ cast-expression
4188 __imag__ cast-expression
4189 && identifier
4190
4191 ADDRESS_P is true iff the unary-expression is appearing as the
4192 operand of the `&' operator.
4193
34cd5ae7 4194 Returns a representation of the expression. */
a723baf1
MM
4195
4196static tree
4197cp_parser_unary_expression (cp_parser *parser, bool address_p)
4198{
4199 cp_token *token;
4200 enum tree_code unary_operator;
4201
4202 /* Peek at the next token. */
4203 token = cp_lexer_peek_token (parser->lexer);
4204 /* Some keywords give away the kind of expression. */
4205 if (token->type == CPP_KEYWORD)
4206 {
4207 enum rid keyword = token->keyword;
4208
4209 switch (keyword)
4210 {
4211 case RID_ALIGNOF:
a723baf1
MM
4212 case RID_SIZEOF:
4213 {
4214 tree operand;
7a18b933 4215 enum tree_code op;
a723baf1 4216
7a18b933
NS
4217 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4218 /* Consume the token. */
a723baf1
MM
4219 cp_lexer_consume_token (parser->lexer);
4220 /* Parse the operand. */
4221 operand = cp_parser_sizeof_operand (parser, keyword);
4222
7a18b933
NS
4223 if (TYPE_P (operand))
4224 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4225 else
7a18b933 4226 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4227 }
4228
4229 case RID_NEW:
4230 return cp_parser_new_expression (parser);
4231
4232 case RID_DELETE:
4233 return cp_parser_delete_expression (parser);
4234
4235 case RID_EXTENSION:
4236 {
4237 /* The saved value of the PEDANTIC flag. */
4238 int saved_pedantic;
4239 tree expr;
4240
4241 /* Save away the PEDANTIC flag. */
4242 cp_parser_extension_opt (parser, &saved_pedantic);
4243 /* Parse the cast-expression. */
d6b4ea85 4244 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4245 /* Restore the PEDANTIC flag. */
4246 pedantic = saved_pedantic;
4247
4248 return expr;
4249 }
4250
4251 case RID_REALPART:
4252 case RID_IMAGPART:
4253 {
4254 tree expression;
4255
4256 /* Consume the `__real__' or `__imag__' token. */
4257 cp_lexer_consume_token (parser->lexer);
4258 /* Parse the cast-expression. */
d6b4ea85 4259 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4260 /* Create the complete representation. */
4261 return build_x_unary_op ((keyword == RID_REALPART
4262 ? REALPART_EXPR : IMAGPART_EXPR),
4263 expression);
4264 }
4265 break;
4266
4267 default:
4268 break;
4269 }
4270 }
4271
4272 /* Look for the `:: new' and `:: delete', which also signal the
4273 beginning of a new-expression, or delete-expression,
4274 respectively. If the next token is `::', then it might be one of
4275 these. */
4276 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4277 {
4278 enum rid keyword;
4279
4280 /* See if the token after the `::' is one of the keywords in
4281 which we're interested. */
4282 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4283 /* If it's `new', we have a new-expression. */
4284 if (keyword == RID_NEW)
4285 return cp_parser_new_expression (parser);
4286 /* Similarly, for `delete'. */
4287 else if (keyword == RID_DELETE)
4288 return cp_parser_delete_expression (parser);
4289 }
4290
4291 /* Look for a unary operator. */
4292 unary_operator = cp_parser_unary_operator (token);
4293 /* The `++' and `--' operators can be handled similarly, even though
4294 they are not technically unary-operators in the grammar. */
4295 if (unary_operator == ERROR_MARK)
4296 {
4297 if (token->type == CPP_PLUS_PLUS)
4298 unary_operator = PREINCREMENT_EXPR;
4299 else if (token->type == CPP_MINUS_MINUS)
4300 unary_operator = PREDECREMENT_EXPR;
4301 /* Handle the GNU address-of-label extension. */
4302 else if (cp_parser_allow_gnu_extensions_p (parser)
4303 && token->type == CPP_AND_AND)
4304 {
4305 tree identifier;
4306
4307 /* Consume the '&&' token. */
4308 cp_lexer_consume_token (parser->lexer);
4309 /* Look for the identifier. */
4310 identifier = cp_parser_identifier (parser);
4311 /* Create an expression representing the address. */
4312 return finish_label_address_expr (identifier);
4313 }
4314 }
4315 if (unary_operator != ERROR_MARK)
4316 {
4317 tree cast_expression;
a5ac3982
MM
4318 tree expression = error_mark_node;
4319 const char *non_constant_p = NULL;
a723baf1
MM
4320
4321 /* Consume the operator token. */
4322 token = cp_lexer_consume_token (parser->lexer);
4323 /* Parse the cast-expression. */
4324 cast_expression
4325 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4326 /* Now, build an appropriate representation. */
4327 switch (unary_operator)
4328 {
4329 case INDIRECT_REF:
a5ac3982
MM
4330 non_constant_p = "`*'";
4331 expression = build_x_indirect_ref (cast_expression, "unary *");
4332 break;
4333
a723baf1 4334 case ADDR_EXPR:
263ee052
MM
4335 /* The "&" operator is allowed in the implementation of
4336 "offsetof". */
4337 if (!parser->in_offsetof_p)
4338 non_constant_p = "`&'";
a5ac3982 4339 /* Fall through. */
d17811fd 4340 case BIT_NOT_EXPR:
a5ac3982
MM
4341 expression = build_x_unary_op (unary_operator, cast_expression);
4342 break;
4343
14d22dd6
MM
4344 case PREINCREMENT_EXPR:
4345 case PREDECREMENT_EXPR:
a5ac3982
MM
4346 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4347 ? "`++'" : "`--'");
14d22dd6 4348 /* Fall through. */
a723baf1
MM
4349 case CONVERT_EXPR:
4350 case NEGATE_EXPR:
4351 case TRUTH_NOT_EXPR:
a5ac3982
MM
4352 expression = finish_unary_op_expr (unary_operator, cast_expression);
4353 break;
a723baf1 4354
a723baf1
MM
4355 default:
4356 abort ();
a723baf1 4357 }
a5ac3982 4358
67c03833 4359 if (non_constant_p && parser->integral_constant_expression_p)
a5ac3982 4360 {
67c03833
JM
4361 if (!parser->allow_non_integral_constant_expression_p)
4362 return cp_parser_non_integral_constant_expression (non_constant_p);
4363 parser->non_integral_constant_expression_p = true;
a5ac3982
MM
4364 }
4365
4366 return expression;
a723baf1
MM
4367 }
4368
4369 return cp_parser_postfix_expression (parser, address_p);
4370}
4371
4372/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4373 unary-operator, the corresponding tree code is returned. */
4374
4375static enum tree_code
94edc4ab 4376cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4377{
4378 switch (token->type)
4379 {
4380 case CPP_MULT:
4381 return INDIRECT_REF;
4382
4383 case CPP_AND:
4384 return ADDR_EXPR;
4385
4386 case CPP_PLUS:
4387 return CONVERT_EXPR;
4388
4389 case CPP_MINUS:
4390 return NEGATE_EXPR;
4391
4392 case CPP_NOT:
4393 return TRUTH_NOT_EXPR;
4394
4395 case CPP_COMPL:
4396 return BIT_NOT_EXPR;
4397
4398 default:
4399 return ERROR_MARK;
4400 }
4401}
4402
4403/* Parse a new-expression.
4404
ca099ac8 4405 new-expression:
a723baf1
MM
4406 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4407 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4408
4409 Returns a representation of the expression. */
4410
4411static tree
94edc4ab 4412cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4413{
4414 bool global_scope_p;
4415 tree placement;
4416 tree type;
4417 tree initializer;
4418
4419 /* Look for the optional `::' operator. */
4420 global_scope_p
4421 = (cp_parser_global_scope_opt (parser,
4422 /*current_scope_valid_p=*/false)
4423 != NULL_TREE);
4424 /* Look for the `new' operator. */
4425 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4426 /* There's no easy way to tell a new-placement from the
4427 `( type-id )' construct. */
4428 cp_parser_parse_tentatively (parser);
4429 /* Look for a new-placement. */
4430 placement = cp_parser_new_placement (parser);
4431 /* If that didn't work out, there's no new-placement. */
4432 if (!cp_parser_parse_definitely (parser))
4433 placement = NULL_TREE;
4434
4435 /* If the next token is a `(', then we have a parenthesized
4436 type-id. */
4437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4438 {
4439 /* Consume the `('. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Parse the type-id. */
4442 type = cp_parser_type_id (parser);
4443 /* Look for the closing `)'. */
4444 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4445 }
4446 /* Otherwise, there must be a new-type-id. */
4447 else
4448 type = cp_parser_new_type_id (parser);
4449
4450 /* If the next token is a `(', then we have a new-initializer. */
4451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4452 initializer = cp_parser_new_initializer (parser);
4453 else
4454 initializer = NULL_TREE;
4455
4456 /* Create a representation of the new-expression. */
4457 return build_new (placement, type, initializer, global_scope_p);
4458}
4459
4460/* Parse a new-placement.
4461
4462 new-placement:
4463 ( expression-list )
4464
4465 Returns the same representation as for an expression-list. */
4466
4467static tree
94edc4ab 4468cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4469{
4470 tree expression_list;
4471
a723baf1 4472 /* Parse the expression-list. */
39703eb9
MM
4473 expression_list = (cp_parser_parenthesized_expression_list
4474 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4475
4476 return expression_list;
4477}
4478
4479/* Parse a new-type-id.
4480
4481 new-type-id:
4482 type-specifier-seq new-declarator [opt]
4483
4484 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4485 and whose TREE_VALUE is the new-declarator. */
4486
4487static tree
94edc4ab 4488cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4489{
4490 tree type_specifier_seq;
4491 tree declarator;
4492 const char *saved_message;
4493
4494 /* The type-specifier sequence must not contain type definitions.
4495 (It cannot contain declarations of new types either, but if they
4496 are not definitions we will catch that because they are not
4497 complete.) */
4498 saved_message = parser->type_definition_forbidden_message;
4499 parser->type_definition_forbidden_message
4500 = "types may not be defined in a new-type-id";
4501 /* Parse the type-specifier-seq. */
4502 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4503 /* Restore the old message. */
4504 parser->type_definition_forbidden_message = saved_message;
4505 /* Parse the new-declarator. */
4506 declarator = cp_parser_new_declarator_opt (parser);
4507
4508 return build_tree_list (type_specifier_seq, declarator);
4509}
4510
4511/* Parse an (optional) new-declarator.
4512
4513 new-declarator:
4514 ptr-operator new-declarator [opt]
4515 direct-new-declarator
4516
4517 Returns a representation of the declarator. See
4518 cp_parser_declarator for the representations used. */
4519
4520static tree
94edc4ab 4521cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4522{
4523 enum tree_code code;
4524 tree type;
4525 tree cv_qualifier_seq;
4526
4527 /* We don't know if there's a ptr-operator next, or not. */
4528 cp_parser_parse_tentatively (parser);
4529 /* Look for a ptr-operator. */
4530 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4531 /* If that worked, look for more new-declarators. */
4532 if (cp_parser_parse_definitely (parser))
4533 {
4534 tree declarator;
4535
4536 /* Parse another optional declarator. */
4537 declarator = cp_parser_new_declarator_opt (parser);
4538
4539 /* Create the representation of the declarator. */
4540 if (code == INDIRECT_REF)
4541 declarator = make_pointer_declarator (cv_qualifier_seq,
4542 declarator);
4543 else
4544 declarator = make_reference_declarator (cv_qualifier_seq,
4545 declarator);
4546
4547 /* Handle the pointer-to-member case. */
4548 if (type)
4549 declarator = build_nt (SCOPE_REF, type, declarator);
4550
4551 return declarator;
4552 }
4553
4554 /* If the next token is a `[', there is a direct-new-declarator. */
4555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4556 return cp_parser_direct_new_declarator (parser);
4557
4558 return NULL_TREE;
4559}
4560
4561/* Parse a direct-new-declarator.
4562
4563 direct-new-declarator:
4564 [ expression ]
4565 direct-new-declarator [constant-expression]
4566
4567 Returns an ARRAY_REF, following the same conventions as are
4568 documented for cp_parser_direct_declarator. */
4569
4570static tree
94edc4ab 4571cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4572{
4573 tree declarator = NULL_TREE;
4574
4575 while (true)
4576 {
4577 tree expression;
4578
4579 /* Look for the opening `['. */
4580 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4581 /* The first expression is not required to be constant. */
4582 if (!declarator)
4583 {
4584 expression = cp_parser_expression (parser);
4585 /* The standard requires that the expression have integral
4586 type. DR 74 adds enumeration types. We believe that the
4587 real intent is that these expressions be handled like the
4588 expression in a `switch' condition, which also allows
4589 classes with a single conversion to integral or
4590 enumeration type. */
4591 if (!processing_template_decl)
4592 {
4593 expression
4594 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4595 expression,
b746c5dc 4596 /*complain=*/true);
a723baf1
MM
4597 if (!expression)
4598 {
4599 error ("expression in new-declarator must have integral or enumeration type");
4600 expression = error_mark_node;
4601 }
4602 }
4603 }
4604 /* But all the other expressions must be. */
4605 else
14d22dd6
MM
4606 expression
4607 = cp_parser_constant_expression (parser,
4608 /*allow_non_constant=*/false,
4609 NULL);
a723baf1
MM
4610 /* Look for the closing `]'. */
4611 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4612
4613 /* Add this bound to the declarator. */
4614 declarator = build_nt (ARRAY_REF, declarator, expression);
4615
4616 /* If the next token is not a `[', then there are no more
4617 bounds. */
4618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4619 break;
4620 }
4621
4622 return declarator;
4623}
4624
4625/* Parse a new-initializer.
4626
4627 new-initializer:
4628 ( expression-list [opt] )
4629
34cd5ae7 4630 Returns a representation of the expression-list. If there is no
a723baf1
MM
4631 expression-list, VOID_ZERO_NODE is returned. */
4632
4633static tree
94edc4ab 4634cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4635{
4636 tree expression_list;
4637
39703eb9
MM
4638 expression_list = (cp_parser_parenthesized_expression_list
4639 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 4640 if (!expression_list)
a723baf1 4641 expression_list = void_zero_node;
a723baf1
MM
4642
4643 return expression_list;
4644}
4645
4646/* Parse a delete-expression.
4647
4648 delete-expression:
4649 :: [opt] delete cast-expression
4650 :: [opt] delete [ ] cast-expression
4651
4652 Returns a representation of the expression. */
4653
4654static tree
94edc4ab 4655cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4656{
4657 bool global_scope_p;
4658 bool array_p;
4659 tree expression;
4660
4661 /* Look for the optional `::' operator. */
4662 global_scope_p
4663 = (cp_parser_global_scope_opt (parser,
4664 /*current_scope_valid_p=*/false)
4665 != NULL_TREE);
4666 /* Look for the `delete' keyword. */
4667 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4668 /* See if the array syntax is in use. */
4669 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4670 {
4671 /* Consume the `[' token. */
4672 cp_lexer_consume_token (parser->lexer);
4673 /* Look for the `]' token. */
4674 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4675 /* Remember that this is the `[]' construct. */
4676 array_p = true;
4677 }
4678 else
4679 array_p = false;
4680
4681 /* Parse the cast-expression. */
d6b4ea85 4682 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4683
4684 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4685}
4686
4687/* Parse a cast-expression.
4688
4689 cast-expression:
4690 unary-expression
4691 ( type-id ) cast-expression
4692
4693 Returns a representation of the expression. */
4694
4695static tree
4696cp_parser_cast_expression (cp_parser *parser, bool address_p)
4697{
4698 /* If it's a `(', then we might be looking at a cast. */
4699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4700 {
4701 tree type = NULL_TREE;
4702 tree expr = NULL_TREE;
4703 bool compound_literal_p;
4704 const char *saved_message;
4705
4706 /* There's no way to know yet whether or not this is a cast.
4707 For example, `(int (3))' is a unary-expression, while `(int)
4708 3' is a cast. So, we resort to parsing tentatively. */
4709 cp_parser_parse_tentatively (parser);
4710 /* Types may not be defined in a cast. */
4711 saved_message = parser->type_definition_forbidden_message;
4712 parser->type_definition_forbidden_message
4713 = "types may not be defined in casts";
4714 /* Consume the `('. */
4715 cp_lexer_consume_token (parser->lexer);
4716 /* A very tricky bit is that `(struct S) { 3 }' is a
4717 compound-literal (which we permit in C++ as an extension).
4718 But, that construct is not a cast-expression -- it is a
4719 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4720 is legal; if the compound-literal were a cast-expression,
4721 you'd need an extra set of parentheses.) But, if we parse
4722 the type-id, and it happens to be a class-specifier, then we
4723 will commit to the parse at that point, because we cannot
4724 undo the action that is done when creating a new class. So,
4725 then we cannot back up and do a postfix-expression.
4726
4727 Therefore, we scan ahead to the closing `)', and check to see
4728 if the token after the `)' is a `{'. If so, we are not
4729 looking at a cast-expression.
4730
4731 Save tokens so that we can put them back. */
4732 cp_lexer_save_tokens (parser->lexer);
4733 /* Skip tokens until the next token is a closing parenthesis.
4734 If we find the closing `)', and the next token is a `{', then
4735 we are looking at a compound-literal. */
4736 compound_literal_p
a668c6ad
MM
4737 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4738 /*consume_paren=*/true)
a723baf1
MM
4739 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4740 /* Roll back the tokens we skipped. */
4741 cp_lexer_rollback_tokens (parser->lexer);
4742 /* If we were looking at a compound-literal, simulate an error
4743 so that the call to cp_parser_parse_definitely below will
4744 fail. */
4745 if (compound_literal_p)
4746 cp_parser_simulate_error (parser);
4747 else
4748 {
4f8163b1
MM
4749 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4750 parser->in_type_id_in_expr_p = true;
a723baf1
MM
4751 /* Look for the type-id. */
4752 type = cp_parser_type_id (parser);
4753 /* Look for the closing `)'. */
4754 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 4755 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
4756 }
4757
4758 /* Restore the saved message. */
4759 parser->type_definition_forbidden_message = saved_message;
4760
bbaab916
NS
4761 /* If ok so far, parse the dependent expression. We cannot be
4762 sure it is a cast. Consider `(T ())'. It is a parenthesized
4763 ctor of T, but looks like a cast to function returning T
4764 without a dependent expression. */
4765 if (!cp_parser_error_occurred (parser))
d6b4ea85 4766 expr = cp_parser_simple_cast_expression (parser);
bbaab916 4767
a723baf1
MM
4768 if (cp_parser_parse_definitely (parser))
4769 {
a723baf1
MM
4770 /* Warn about old-style casts, if so requested. */
4771 if (warn_old_style_cast
4772 && !in_system_header
4773 && !VOID_TYPE_P (type)
4774 && current_lang_name != lang_name_c)
4775 warning ("use of old-style cast");
14d22dd6
MM
4776
4777 /* Only type conversions to integral or enumeration types
4778 can be used in constant-expressions. */
67c03833 4779 if (parser->integral_constant_expression_p
14d22dd6
MM
4780 && !dependent_type_p (type)
4781 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4782 {
67c03833
JM
4783 if (!parser->allow_non_integral_constant_expression_p)
4784 return (cp_parser_non_integral_constant_expression
14d22dd6
MM
4785 ("a casts to a type other than an integral or "
4786 "enumeration type"));
67c03833 4787 parser->non_integral_constant_expression_p = true;
14d22dd6 4788 }
a723baf1
MM
4789 /* Perform the cast. */
4790 expr = build_c_cast (type, expr);
bbaab916 4791 return expr;
a723baf1 4792 }
a723baf1
MM
4793 }
4794
4795 /* If we get here, then it's not a cast, so it must be a
4796 unary-expression. */
4797 return cp_parser_unary_expression (parser, address_p);
4798}
4799
4800/* Parse a pm-expression.
4801
4802 pm-expression:
4803 cast-expression
4804 pm-expression .* cast-expression
4805 pm-expression ->* cast-expression
4806
4807 Returns a representation of the expression. */
4808
4809static tree
94edc4ab 4810cp_parser_pm_expression (cp_parser* parser)
a723baf1 4811{
d6b4ea85
MM
4812 static const cp_parser_token_tree_map map = {
4813 { CPP_DEREF_STAR, MEMBER_REF },
4814 { CPP_DOT_STAR, DOTSTAR_EXPR },
4815 { CPP_EOF, ERROR_MARK }
4816 };
a723baf1 4817
d6b4ea85
MM
4818 return cp_parser_binary_expression (parser, map,
4819 cp_parser_simple_cast_expression);
a723baf1
MM
4820}
4821
4822/* Parse a multiplicative-expression.
4823
4824 mulitplicative-expression:
4825 pm-expression
4826 multiplicative-expression * pm-expression
4827 multiplicative-expression / pm-expression
4828 multiplicative-expression % pm-expression
4829
4830 Returns a representation of the expression. */
4831
4832static tree
94edc4ab 4833cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4834{
39b1af70 4835 static const cp_parser_token_tree_map map = {
a723baf1
MM
4836 { CPP_MULT, MULT_EXPR },
4837 { CPP_DIV, TRUNC_DIV_EXPR },
4838 { CPP_MOD, TRUNC_MOD_EXPR },
4839 { CPP_EOF, ERROR_MARK }
4840 };
4841
4842 return cp_parser_binary_expression (parser,
4843 map,
4844 cp_parser_pm_expression);
4845}
4846
4847/* Parse an additive-expression.
4848
4849 additive-expression:
4850 multiplicative-expression
4851 additive-expression + multiplicative-expression
4852 additive-expression - multiplicative-expression
4853
4854 Returns a representation of the expression. */
4855
4856static tree
94edc4ab 4857cp_parser_additive_expression (cp_parser* parser)
a723baf1 4858{
39b1af70 4859 static const cp_parser_token_tree_map map = {
a723baf1
MM
4860 { CPP_PLUS, PLUS_EXPR },
4861 { CPP_MINUS, MINUS_EXPR },
4862 { CPP_EOF, ERROR_MARK }
4863 };
4864
4865 return cp_parser_binary_expression (parser,
4866 map,
4867 cp_parser_multiplicative_expression);
4868}
4869
4870/* Parse a shift-expression.
4871
4872 shift-expression:
4873 additive-expression
4874 shift-expression << additive-expression
4875 shift-expression >> additive-expression
4876
4877 Returns a representation of the expression. */
4878
4879static tree
94edc4ab 4880cp_parser_shift_expression (cp_parser* parser)
a723baf1 4881{
39b1af70 4882 static const cp_parser_token_tree_map map = {
a723baf1
MM
4883 { CPP_LSHIFT, LSHIFT_EXPR },
4884 { CPP_RSHIFT, RSHIFT_EXPR },
4885 { CPP_EOF, ERROR_MARK }
4886 };
4887
4888 return cp_parser_binary_expression (parser,
4889 map,
4890 cp_parser_additive_expression);
4891}
4892
4893/* Parse a relational-expression.
4894
4895 relational-expression:
4896 shift-expression
4897 relational-expression < shift-expression
4898 relational-expression > shift-expression
4899 relational-expression <= shift-expression
4900 relational-expression >= shift-expression
4901
4902 GNU Extension:
4903
4904 relational-expression:
4905 relational-expression <? shift-expression
4906 relational-expression >? shift-expression
4907
4908 Returns a representation of the expression. */
4909
4910static tree
94edc4ab 4911cp_parser_relational_expression (cp_parser* parser)
a723baf1 4912{
39b1af70 4913 static const cp_parser_token_tree_map map = {
a723baf1
MM
4914 { CPP_LESS, LT_EXPR },
4915 { CPP_GREATER, GT_EXPR },
4916 { CPP_LESS_EQ, LE_EXPR },
4917 { CPP_GREATER_EQ, GE_EXPR },
4918 { CPP_MIN, MIN_EXPR },
4919 { CPP_MAX, MAX_EXPR },
4920 { CPP_EOF, ERROR_MARK }
4921 };
4922
4923 return cp_parser_binary_expression (parser,
4924 map,
4925 cp_parser_shift_expression);
4926}
4927
4928/* Parse an equality-expression.
4929
4930 equality-expression:
4931 relational-expression
4932 equality-expression == relational-expression
4933 equality-expression != relational-expression
4934
4935 Returns a representation of the expression. */
4936
4937static tree
94edc4ab 4938cp_parser_equality_expression (cp_parser* parser)
a723baf1 4939{
39b1af70 4940 static const cp_parser_token_tree_map map = {
a723baf1
MM
4941 { CPP_EQ_EQ, EQ_EXPR },
4942 { CPP_NOT_EQ, NE_EXPR },
4943 { CPP_EOF, ERROR_MARK }
4944 };
4945
4946 return cp_parser_binary_expression (parser,
4947 map,
4948 cp_parser_relational_expression);
4949}
4950
4951/* Parse an and-expression.
4952
4953 and-expression:
4954 equality-expression
4955 and-expression & equality-expression
4956
4957 Returns a representation of the expression. */
4958
4959static tree
94edc4ab 4960cp_parser_and_expression (cp_parser* parser)
a723baf1 4961{
39b1af70 4962 static const cp_parser_token_tree_map map = {
a723baf1
MM
4963 { CPP_AND, BIT_AND_EXPR },
4964 { CPP_EOF, ERROR_MARK }
4965 };
4966
4967 return cp_parser_binary_expression (parser,
4968 map,
4969 cp_parser_equality_expression);
4970}
4971
4972/* Parse an exclusive-or-expression.
4973
4974 exclusive-or-expression:
4975 and-expression
4976 exclusive-or-expression ^ and-expression
4977
4978 Returns a representation of the expression. */
4979
4980static tree
94edc4ab 4981cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 4982{
39b1af70 4983 static const cp_parser_token_tree_map map = {
a723baf1
MM
4984 { CPP_XOR, BIT_XOR_EXPR },
4985 { CPP_EOF, ERROR_MARK }
4986 };
4987
4988 return cp_parser_binary_expression (parser,
4989 map,
4990 cp_parser_and_expression);
4991}
4992
4993
4994/* Parse an inclusive-or-expression.
4995
4996 inclusive-or-expression:
4997 exclusive-or-expression
4998 inclusive-or-expression | exclusive-or-expression
4999
5000 Returns a representation of the expression. */
5001
5002static tree
94edc4ab 5003cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 5004{
39b1af70 5005 static const cp_parser_token_tree_map map = {
a723baf1
MM
5006 { CPP_OR, BIT_IOR_EXPR },
5007 { CPP_EOF, ERROR_MARK }
5008 };
5009
5010 return cp_parser_binary_expression (parser,
5011 map,
5012 cp_parser_exclusive_or_expression);
5013}
5014
5015/* Parse a logical-and-expression.
5016
5017 logical-and-expression:
5018 inclusive-or-expression
5019 logical-and-expression && inclusive-or-expression
5020
5021 Returns a representation of the expression. */
5022
5023static tree
94edc4ab 5024cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 5025{
39b1af70 5026 static const cp_parser_token_tree_map map = {
a723baf1
MM
5027 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5028 { CPP_EOF, ERROR_MARK }
5029 };
5030
5031 return cp_parser_binary_expression (parser,
5032 map,
5033 cp_parser_inclusive_or_expression);
5034}
5035
5036/* Parse a logical-or-expression.
5037
5038 logical-or-expression:
34cd5ae7 5039 logical-and-expression
a723baf1
MM
5040 logical-or-expression || logical-and-expression
5041
5042 Returns a representation of the expression. */
5043
5044static tree
94edc4ab 5045cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5046{
39b1af70 5047 static const cp_parser_token_tree_map map = {
a723baf1
MM
5048 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5049 { CPP_EOF, ERROR_MARK }
5050 };
5051
5052 return cp_parser_binary_expression (parser,
5053 map,
5054 cp_parser_logical_and_expression);
5055}
5056
a723baf1
MM
5057/* Parse the `? expression : assignment-expression' part of a
5058 conditional-expression. The LOGICAL_OR_EXPR is the
5059 logical-or-expression that started the conditional-expression.
5060 Returns a representation of the entire conditional-expression.
5061
39703eb9 5062 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5063
5064 ? expression : assignment-expression
5065
5066 GNU Extensions:
5067
5068 ? : assignment-expression */
5069
5070static tree
94edc4ab 5071cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5072{
5073 tree expr;
5074 tree assignment_expr;
5075
5076 /* Consume the `?' token. */
5077 cp_lexer_consume_token (parser->lexer);
5078 if (cp_parser_allow_gnu_extensions_p (parser)
5079 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5080 /* Implicit true clause. */
5081 expr = NULL_TREE;
5082 else
5083 /* Parse the expression. */
5084 expr = cp_parser_expression (parser);
5085
5086 /* The next token should be a `:'. */
5087 cp_parser_require (parser, CPP_COLON, "`:'");
5088 /* Parse the assignment-expression. */
5089 assignment_expr = cp_parser_assignment_expression (parser);
5090
5091 /* Build the conditional-expression. */
5092 return build_x_conditional_expr (logical_or_expr,
5093 expr,
5094 assignment_expr);
5095}
5096
5097/* Parse an assignment-expression.
5098
5099 assignment-expression:
5100 conditional-expression
5101 logical-or-expression assignment-operator assignment_expression
5102 throw-expression
5103
5104 Returns a representation for the expression. */
5105
5106static tree
94edc4ab 5107cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5108{
5109 tree expr;
5110
5111 /* If the next token is the `throw' keyword, then we're looking at
5112 a throw-expression. */
5113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5114 expr = cp_parser_throw_expression (parser);
5115 /* Otherwise, it must be that we are looking at a
5116 logical-or-expression. */
5117 else
5118 {
5119 /* Parse the logical-or-expression. */
5120 expr = cp_parser_logical_or_expression (parser);
5121 /* If the next token is a `?' then we're actually looking at a
5122 conditional-expression. */
5123 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5124 return cp_parser_question_colon_clause (parser, expr);
5125 else
5126 {
5127 enum tree_code assignment_operator;
5128
5129 /* If it's an assignment-operator, we're using the second
5130 production. */
5131 assignment_operator
5132 = cp_parser_assignment_operator_opt (parser);
5133 if (assignment_operator != ERROR_MARK)
5134 {
5135 tree rhs;
5136
5137 /* Parse the right-hand side of the assignment. */
5138 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5139 /* An assignment may not appear in a
5140 constant-expression. */
67c03833 5141 if (parser->integral_constant_expression_p)
14d22dd6 5142 {
67c03833
JM
5143 if (!parser->allow_non_integral_constant_expression_p)
5144 return cp_parser_non_integral_constant_expression ("an assignment");
5145 parser->non_integral_constant_expression_p = true;
14d22dd6 5146 }
34cd5ae7 5147 /* Build the assignment expression. */
a723baf1
MM
5148 expr = build_x_modify_expr (expr,
5149 assignment_operator,
5150 rhs);
5151 }
5152 }
5153 }
5154
5155 return expr;
5156}
5157
5158/* Parse an (optional) assignment-operator.
5159
5160 assignment-operator: one of
5161 = *= /= %= += -= >>= <<= &= ^= |=
5162
5163 GNU Extension:
5164
5165 assignment-operator: one of
5166 <?= >?=
5167
5168 If the next token is an assignment operator, the corresponding tree
5169 code is returned, and the token is consumed. For example, for
5170 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5171 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5172 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5173 operator, ERROR_MARK is returned. */
5174
5175static enum tree_code
94edc4ab 5176cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5177{
5178 enum tree_code op;
5179 cp_token *token;
5180
5181 /* Peek at the next toen. */
5182 token = cp_lexer_peek_token (parser->lexer);
5183
5184 switch (token->type)
5185 {
5186 case CPP_EQ:
5187 op = NOP_EXPR;
5188 break;
5189
5190 case CPP_MULT_EQ:
5191 op = MULT_EXPR;
5192 break;
5193
5194 case CPP_DIV_EQ:
5195 op = TRUNC_DIV_EXPR;
5196 break;
5197
5198 case CPP_MOD_EQ:
5199 op = TRUNC_MOD_EXPR;
5200 break;
5201
5202 case CPP_PLUS_EQ:
5203 op = PLUS_EXPR;
5204 break;
5205
5206 case CPP_MINUS_EQ:
5207 op = MINUS_EXPR;
5208 break;
5209
5210 case CPP_RSHIFT_EQ:
5211 op = RSHIFT_EXPR;
5212 break;
5213
5214 case CPP_LSHIFT_EQ:
5215 op = LSHIFT_EXPR;
5216 break;
5217
5218 case CPP_AND_EQ:
5219 op = BIT_AND_EXPR;
5220 break;
5221
5222 case CPP_XOR_EQ:
5223 op = BIT_XOR_EXPR;
5224 break;
5225
5226 case CPP_OR_EQ:
5227 op = BIT_IOR_EXPR;
5228 break;
5229
5230 case CPP_MIN_EQ:
5231 op = MIN_EXPR;
5232 break;
5233
5234 case CPP_MAX_EQ:
5235 op = MAX_EXPR;
5236 break;
5237
5238 default:
5239 /* Nothing else is an assignment operator. */
5240 op = ERROR_MARK;
5241 }
5242
5243 /* If it was an assignment operator, consume it. */
5244 if (op != ERROR_MARK)
5245 cp_lexer_consume_token (parser->lexer);
5246
5247 return op;
5248}
5249
5250/* Parse an expression.
5251
5252 expression:
5253 assignment-expression
5254 expression , assignment-expression
5255
5256 Returns a representation of the expression. */
5257
5258static tree
94edc4ab 5259cp_parser_expression (cp_parser* parser)
a723baf1
MM
5260{
5261 tree expression = NULL_TREE;
a723baf1
MM
5262
5263 while (true)
5264 {
5265 tree assignment_expression;
5266
5267 /* Parse the next assignment-expression. */
5268 assignment_expression
5269 = cp_parser_assignment_expression (parser);
5270 /* If this is the first assignment-expression, we can just
5271 save it away. */
5272 if (!expression)
5273 expression = assignment_expression;
a723baf1 5274 else
d17811fd
MM
5275 expression = build_x_compound_expr (expression,
5276 assignment_expression);
a723baf1
MM
5277 /* If the next token is not a comma, then we are done with the
5278 expression. */
5279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5280 break;
5281 /* Consume the `,'. */
5282 cp_lexer_consume_token (parser->lexer);
14d22dd6 5283 /* A comma operator cannot appear in a constant-expression. */
67c03833 5284 if (parser->integral_constant_expression_p)
14d22dd6 5285 {
67c03833 5286 if (!parser->allow_non_integral_constant_expression_p)
d17811fd 5287 expression
67c03833
JM
5288 = cp_parser_non_integral_constant_expression ("a comma operator");
5289 parser->non_integral_constant_expression_p = true;
14d22dd6 5290 }
14d22dd6 5291 }
a723baf1
MM
5292
5293 return expression;
5294}
5295
5296/* Parse a constant-expression.
5297
5298 constant-expression:
14d22dd6
MM
5299 conditional-expression
5300
5301 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5302 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5303 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5304 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5305
5306static tree
14d22dd6
MM
5307cp_parser_constant_expression (cp_parser* parser,
5308 bool allow_non_constant_p,
5309 bool *non_constant_p)
a723baf1 5310{
67c03833
JM
5311 bool saved_integral_constant_expression_p;
5312 bool saved_allow_non_integral_constant_expression_p;
5313 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5314 tree expression;
5315
5316 /* It might seem that we could simply parse the
5317 conditional-expression, and then check to see if it were
5318 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5319 one that the compiler can figure out is constant, possibly after
5320 doing some simplifications or optimizations. The standard has a
5321 precise definition of constant-expression, and we must honor
5322 that, even though it is somewhat more restrictive.
5323
5324 For example:
5325
5326 int i[(2, 3)];
5327
5328 is not a legal declaration, because `(2, 3)' is not a
5329 constant-expression. The `,' operator is forbidden in a
5330 constant-expression. However, GCC's constant-folding machinery
5331 will fold this operation to an INTEGER_CST for `3'. */
5332
14d22dd6 5333 /* Save the old settings. */
67c03833
JM
5334 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5335 saved_allow_non_integral_constant_expression_p
5336 = parser->allow_non_integral_constant_expression_p;
5337 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5338 /* We are now parsing a constant-expression. */
67c03833
JM
5339 parser->integral_constant_expression_p = true;
5340 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5341 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5342 /* Although the grammar says "conditional-expression", we parse an
5343 "assignment-expression", which also permits "throw-expression"
5344 and the use of assignment operators. In the case that
5345 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5346 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5347 actually essential that we look for an assignment-expression.
5348 For example, cp_parser_initializer_clauses uses this function to
5349 determine whether a particular assignment-expression is in fact
5350 constant. */
5351 expression = cp_parser_assignment_expression (parser);
14d22dd6 5352 /* Restore the old settings. */
67c03833
JM
5353 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5354 parser->allow_non_integral_constant_expression_p
5355 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5356 if (allow_non_constant_p)
67c03833
JM
5357 *non_constant_p = parser->non_integral_constant_expression_p;
5358 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5359
5360 return expression;
5361}
5362
5363/* Statements [gram.stmt.stmt] */
5364
5365/* Parse a statement.
5366
5367 statement:
5368 labeled-statement
5369 expression-statement
5370 compound-statement
5371 selection-statement
5372 iteration-statement
5373 jump-statement
5374 declaration-statement
5375 try-block */
5376
5377static void
a5bcc582 5378cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5379{
5380 tree statement;
5381 cp_token *token;
5382 int statement_line_number;
5383
5384 /* There is no statement yet. */
5385 statement = NULL_TREE;
5386 /* Peek at the next token. */
5387 token = cp_lexer_peek_token (parser->lexer);
5388 /* Remember the line number of the first token in the statement. */
82a98427 5389 statement_line_number = token->location.line;
a723baf1
MM
5390 /* If this is a keyword, then that will often determine what kind of
5391 statement we have. */
5392 if (token->type == CPP_KEYWORD)
5393 {
5394 enum rid keyword = token->keyword;
5395
5396 switch (keyword)
5397 {
5398 case RID_CASE:
5399 case RID_DEFAULT:
a5bcc582
NS
5400 statement = cp_parser_labeled_statement (parser,
5401 in_statement_expr_p);
a723baf1
MM
5402 break;
5403
5404 case RID_IF:
5405 case RID_SWITCH:
5406 statement = cp_parser_selection_statement (parser);
5407 break;
5408
5409 case RID_WHILE:
5410 case RID_DO:
5411 case RID_FOR:
5412 statement = cp_parser_iteration_statement (parser);
5413 break;
5414
5415 case RID_BREAK:
5416 case RID_CONTINUE:
5417 case RID_RETURN:
5418 case RID_GOTO:
5419 statement = cp_parser_jump_statement (parser);
5420 break;
5421
5422 case RID_TRY:
5423 statement = cp_parser_try_block (parser);
5424 break;
5425
5426 default:
5427 /* It might be a keyword like `int' that can start a
5428 declaration-statement. */
5429 break;
5430 }
5431 }
5432 else if (token->type == CPP_NAME)
5433 {
5434 /* If the next token is a `:', then we are looking at a
5435 labeled-statement. */
5436 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5437 if (token->type == CPP_COLON)
a5bcc582 5438 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
a723baf1
MM
5439 }
5440 /* Anything that starts with a `{' must be a compound-statement. */
5441 else if (token->type == CPP_OPEN_BRACE)
a5bcc582 5442 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
5443
5444 /* Everything else must be a declaration-statement or an
5445 expression-statement. Try for the declaration-statement
5446 first, unless we are looking at a `;', in which case we know that
5447 we have an expression-statement. */
5448 if (!statement)
5449 {
5450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5451 {
5452 cp_parser_parse_tentatively (parser);
5453 /* Try to parse the declaration-statement. */
5454 cp_parser_declaration_statement (parser);
5455 /* If that worked, we're done. */
5456 if (cp_parser_parse_definitely (parser))
5457 return;
5458 }
5459 /* Look for an expression-statement instead. */
a5bcc582 5460 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
a723baf1
MM
5461 }
5462
5463 /* Set the line number for the statement. */
009ed910 5464 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
a723baf1
MM
5465 STMT_LINENO (statement) = statement_line_number;
5466}
5467
5468/* Parse a labeled-statement.
5469
5470 labeled-statement:
5471 identifier : statement
5472 case constant-expression : statement
98ce043b
MM
5473 default : statement
5474
5475 GNU Extension:
5476
5477 labeled-statement:
5478 case constant-expression ... constant-expression : statement
a723baf1
MM
5479
5480 Returns the new CASE_LABEL, for a `case' or `default' label. For
5481 an ordinary label, returns a LABEL_STMT. */
5482
5483static tree
a5bcc582 5484cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5485{
5486 cp_token *token;
0e59b3fb 5487 tree statement = error_mark_node;
a723baf1
MM
5488
5489 /* The next token should be an identifier. */
5490 token = cp_lexer_peek_token (parser->lexer);
5491 if (token->type != CPP_NAME
5492 && token->type != CPP_KEYWORD)
5493 {
5494 cp_parser_error (parser, "expected labeled-statement");
5495 return error_mark_node;
5496 }
5497
5498 switch (token->keyword)
5499 {
5500 case RID_CASE:
5501 {
98ce043b
MM
5502 tree expr, expr_hi;
5503 cp_token *ellipsis;
a723baf1
MM
5504
5505 /* Consume the `case' token. */
5506 cp_lexer_consume_token (parser->lexer);
5507 /* Parse the constant-expression. */
14d22dd6 5508 expr = cp_parser_constant_expression (parser,
d17811fd 5509 /*allow_non_constant_p=*/false,
14d22dd6 5510 NULL);
98ce043b
MM
5511
5512 ellipsis = cp_lexer_peek_token (parser->lexer);
5513 if (ellipsis->type == CPP_ELLIPSIS)
5514 {
5515 /* Consume the `...' token. */
5516 cp_lexer_consume_token (parser->lexer);
5517 expr_hi =
5518 cp_parser_constant_expression (parser,
5519 /*allow_non_constant_p=*/false,
5520 NULL);
5521 /* We don't need to emit warnings here, as the common code
5522 will do this for us. */
5523 }
5524 else
5525 expr_hi = NULL_TREE;
5526
0e59b3fb
MM
5527 if (!parser->in_switch_statement_p)
5528 error ("case label `%E' not within a switch statement", expr);
5529 else
98ce043b 5530 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
5531 }
5532 break;
5533
5534 case RID_DEFAULT:
5535 /* Consume the `default' token. */
5536 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5537 if (!parser->in_switch_statement_p)
5538 error ("case label not within a switch statement");
5539 else
5540 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5541 break;
5542
5543 default:
5544 /* Anything else must be an ordinary label. */
5545 statement = finish_label_stmt (cp_parser_identifier (parser));
5546 break;
5547 }
5548
5549 /* Require the `:' token. */
5550 cp_parser_require (parser, CPP_COLON, "`:'");
5551 /* Parse the labeled statement. */
a5bcc582 5552 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5553
5554 /* Return the label, in the case of a `case' or `default' label. */
5555 return statement;
5556}
5557
5558/* Parse an expression-statement.
5559
5560 expression-statement:
5561 expression [opt] ;
5562
5563 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5564 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5565 indicates whether this expression-statement is part of an
5566 expression statement. */
a723baf1
MM
5567
5568static tree
a5bcc582 5569cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
a723baf1 5570{
a5bcc582 5571 tree statement = NULL_TREE;
a723baf1 5572
a5bcc582 5573 /* If the next token is a ';', then there is no expression
04c06002 5574 statement. */
a723baf1 5575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582
NS
5576 statement = cp_parser_expression (parser);
5577
a723baf1 5578 /* Consume the final `;'. */
e0860732 5579 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5580
a5bcc582
NS
5581 if (in_statement_expr_p
5582 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5583 {
5584 /* This is the final expression statement of a statement
5585 expression. */
5586 statement = finish_stmt_expr_expr (statement);
5587 }
5588 else if (statement)
5589 statement = finish_expr_stmt (statement);
5590 else
5591 finish_stmt ();
5592
a723baf1
MM
5593 return statement;
5594}
5595
5596/* Parse a compound-statement.
5597
5598 compound-statement:
5599 { statement-seq [opt] }
5600
5601 Returns a COMPOUND_STMT representing the statement. */
5602
5603static tree
a5bcc582 5604cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
a723baf1
MM
5605{
5606 tree compound_stmt;
5607
5608 /* Consume the `{'. */
5609 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5610 return error_mark_node;
5611 /* Begin the compound-statement. */
7a3397c7 5612 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 5613 /* Parse an (optional) statement-seq. */
a5bcc582 5614 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
a723baf1 5615 /* Finish the compound-statement. */
7a3397c7 5616 finish_compound_stmt (compound_stmt);
a723baf1
MM
5617 /* Consume the `}'. */
5618 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5619
5620 return compound_stmt;
5621}
5622
5623/* Parse an (optional) statement-seq.
5624
5625 statement-seq:
5626 statement
5627 statement-seq [opt] statement */
5628
5629static void
a5bcc582 5630cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
a723baf1
MM
5631{
5632 /* Scan statements until there aren't any more. */
5633 while (true)
5634 {
5635 /* If we're looking at a `}', then we've run out of statements. */
5636 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5637 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5638 break;
5639
5640 /* Parse the statement. */
a5bcc582 5641 cp_parser_statement (parser, in_statement_expr_p);
a723baf1
MM
5642 }
5643}
5644
5645/* Parse a selection-statement.
5646
5647 selection-statement:
5648 if ( condition ) statement
5649 if ( condition ) statement else statement
5650 switch ( condition ) statement
5651
5652 Returns the new IF_STMT or SWITCH_STMT. */
5653
5654static tree
94edc4ab 5655cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5656{
5657 cp_token *token;
5658 enum rid keyword;
5659
5660 /* Peek at the next token. */
5661 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5662
5663 /* See what kind of keyword it is. */
5664 keyword = token->keyword;
5665 switch (keyword)
5666 {
5667 case RID_IF:
5668 case RID_SWITCH:
5669 {
5670 tree statement;
5671 tree condition;
5672
5673 /* Look for the `('. */
5674 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5675 {
5676 cp_parser_skip_to_end_of_statement (parser);
5677 return error_mark_node;
5678 }
5679
5680 /* Begin the selection-statement. */
5681 if (keyword == RID_IF)
5682 statement = begin_if_stmt ();
5683 else
5684 statement = begin_switch_stmt ();
5685
5686 /* Parse the condition. */
5687 condition = cp_parser_condition (parser);
5688 /* Look for the `)'. */
5689 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
5690 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5691 /*consume_paren=*/true);
a723baf1
MM
5692
5693 if (keyword == RID_IF)
5694 {
5695 tree then_stmt;
5696
5697 /* Add the condition. */
5698 finish_if_stmt_cond (condition, statement);
5699
5700 /* Parse the then-clause. */
5701 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5702 finish_then_clause (statement);
5703
5704 /* If the next token is `else', parse the else-clause. */
5705 if (cp_lexer_next_token_is_keyword (parser->lexer,
5706 RID_ELSE))
5707 {
5708 tree else_stmt;
5709
5710 /* Consume the `else' keyword. */
5711 cp_lexer_consume_token (parser->lexer);
5712 /* Parse the else-clause. */
5713 else_stmt
5714 = cp_parser_implicitly_scoped_statement (parser);
5715 finish_else_clause (statement);
5716 }
5717
5718 /* Now we're all done with the if-statement. */
5719 finish_if_stmt ();
5720 }
5721 else
5722 {
5723 tree body;
0e59b3fb 5724 bool in_switch_statement_p;
a723baf1
MM
5725
5726 /* Add the condition. */
5727 finish_switch_cond (condition, statement);
5728
5729 /* Parse the body of the switch-statement. */
0e59b3fb
MM
5730 in_switch_statement_p = parser->in_switch_statement_p;
5731 parser->in_switch_statement_p = true;
a723baf1 5732 body = cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5733 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
5734
5735 /* Now we're all done with the switch-statement. */
5736 finish_switch_stmt (statement);
5737 }
5738
5739 return statement;
5740 }
5741 break;
5742
5743 default:
5744 cp_parser_error (parser, "expected selection-statement");
5745 return error_mark_node;
5746 }
5747}
5748
5749/* Parse a condition.
5750
5751 condition:
5752 expression
5753 type-specifier-seq declarator = assignment-expression
5754
5755 GNU Extension:
5756
5757 condition:
5758 type-specifier-seq declarator asm-specification [opt]
5759 attributes [opt] = assignment-expression
5760
5761 Returns the expression that should be tested. */
5762
5763static tree
94edc4ab 5764cp_parser_condition (cp_parser* parser)
a723baf1
MM
5765{
5766 tree type_specifiers;
5767 const char *saved_message;
5768
5769 /* Try the declaration first. */
5770 cp_parser_parse_tentatively (parser);
5771 /* New types are not allowed in the type-specifier-seq for a
5772 condition. */
5773 saved_message = parser->type_definition_forbidden_message;
5774 parser->type_definition_forbidden_message
5775 = "types may not be defined in conditions";
5776 /* Parse the type-specifier-seq. */
5777 type_specifiers = cp_parser_type_specifier_seq (parser);
5778 /* Restore the saved message. */
5779 parser->type_definition_forbidden_message = saved_message;
5780 /* If all is well, we might be looking at a declaration. */
5781 if (!cp_parser_error_occurred (parser))
5782 {
5783 tree decl;
5784 tree asm_specification;
5785 tree attributes;
5786 tree declarator;
5787 tree initializer = NULL_TREE;
5788
5789 /* Parse the declarator. */
62b8a44e 5790 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
5791 /*ctor_dtor_or_conv_p=*/NULL,
5792 /*parenthesized_p=*/NULL);
a723baf1
MM
5793 /* Parse the attributes. */
5794 attributes = cp_parser_attributes_opt (parser);
5795 /* Parse the asm-specification. */
5796 asm_specification = cp_parser_asm_specification_opt (parser);
5797 /* If the next token is not an `=', then we might still be
5798 looking at an expression. For example:
5799
5800 if (A(a).x)
5801
5802 looks like a decl-specifier-seq and a declarator -- but then
5803 there is no `=', so this is an expression. */
5804 cp_parser_require (parser, CPP_EQ, "`='");
5805 /* If we did see an `=', then we are looking at a declaration
5806 for sure. */
5807 if (cp_parser_parse_definitely (parser))
5808 {
5809 /* Create the declaration. */
5810 decl = start_decl (declarator, type_specifiers,
5811 /*initialized_p=*/true,
5812 attributes, /*prefix_attributes=*/NULL_TREE);
5813 /* Parse the assignment-expression. */
5814 initializer = cp_parser_assignment_expression (parser);
5815
5816 /* Process the initializer. */
5817 cp_finish_decl (decl,
5818 initializer,
5819 asm_specification,
5820 LOOKUP_ONLYCONVERTING);
5821
5822 return convert_from_reference (decl);
5823 }
5824 }
5825 /* If we didn't even get past the declarator successfully, we are
5826 definitely not looking at a declaration. */
5827 else
5828 cp_parser_abort_tentative_parse (parser);
5829
5830 /* Otherwise, we are looking at an expression. */
5831 return cp_parser_expression (parser);
5832}
5833
5834/* Parse an iteration-statement.
5835
5836 iteration-statement:
5837 while ( condition ) statement
5838 do statement while ( expression ) ;
5839 for ( for-init-statement condition [opt] ; expression [opt] )
5840 statement
5841
5842 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5843
5844static tree
94edc4ab 5845cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
5846{
5847 cp_token *token;
5848 enum rid keyword;
5849 tree statement;
0e59b3fb
MM
5850 bool in_iteration_statement_p;
5851
a723baf1
MM
5852
5853 /* Peek at the next token. */
5854 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5855 if (!token)
5856 return error_mark_node;
5857
0e59b3fb
MM
5858 /* Remember whether or not we are already within an iteration
5859 statement. */
5860 in_iteration_statement_p = parser->in_iteration_statement_p;
5861
a723baf1
MM
5862 /* See what kind of keyword it is. */
5863 keyword = token->keyword;
5864 switch (keyword)
5865 {
5866 case RID_WHILE:
5867 {
5868 tree condition;
5869
5870 /* Begin the while-statement. */
5871 statement = begin_while_stmt ();
5872 /* Look for the `('. */
5873 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5874 /* Parse the condition. */
5875 condition = cp_parser_condition (parser);
5876 finish_while_stmt_cond (condition, statement);
5877 /* Look for the `)'. */
5878 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5879 /* Parse the dependent statement. */
0e59b3fb 5880 parser->in_iteration_statement_p = true;
a723baf1 5881 cp_parser_already_scoped_statement (parser);
0e59b3fb 5882 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5883 /* We're done with the while-statement. */
5884 finish_while_stmt (statement);
5885 }
5886 break;
5887
5888 case RID_DO:
5889 {
5890 tree expression;
5891
5892 /* Begin the do-statement. */
5893 statement = begin_do_stmt ();
5894 /* Parse the body of the do-statement. */
0e59b3fb 5895 parser->in_iteration_statement_p = true;
a723baf1 5896 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 5897 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5898 finish_do_body (statement);
5899 /* Look for the `while' keyword. */
5900 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5901 /* Look for the `('. */
5902 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5903 /* Parse the expression. */
5904 expression = cp_parser_expression (parser);
5905 /* We're done with the do-statement. */
5906 finish_do_stmt (expression, statement);
5907 /* Look for the `)'. */
5908 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5909 /* Look for the `;'. */
5910 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5911 }
5912 break;
5913
5914 case RID_FOR:
5915 {
5916 tree condition = NULL_TREE;
5917 tree expression = NULL_TREE;
5918
5919 /* Begin the for-statement. */
5920 statement = begin_for_stmt ();
5921 /* Look for the `('. */
5922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5923 /* Parse the initialization. */
5924 cp_parser_for_init_statement (parser);
5925 finish_for_init_stmt (statement);
5926
5927 /* If there's a condition, process it. */
5928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5929 condition = cp_parser_condition (parser);
5930 finish_for_cond (condition, statement);
5931 /* Look for the `;'. */
5932 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5933
5934 /* If there's an expression, process it. */
5935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5936 expression = cp_parser_expression (parser);
5937 finish_for_expr (expression, statement);
5938 /* Look for the `)'. */
5939 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5940
5941 /* Parse the body of the for-statement. */
0e59b3fb 5942 parser->in_iteration_statement_p = true;
a723baf1 5943 cp_parser_already_scoped_statement (parser);
0e59b3fb 5944 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
5945
5946 /* We're done with the for-statement. */
5947 finish_for_stmt (statement);
5948 }
5949 break;
5950
5951 default:
5952 cp_parser_error (parser, "expected iteration-statement");
5953 statement = error_mark_node;
5954 break;
5955 }
5956
5957 return statement;
5958}
5959
5960/* Parse a for-init-statement.
5961
5962 for-init-statement:
5963 expression-statement
5964 simple-declaration */
5965
5966static void
94edc4ab 5967cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
5968{
5969 /* If the next token is a `;', then we have an empty
34cd5ae7 5970 expression-statement. Grammatically, this is also a
a723baf1
MM
5971 simple-declaration, but an invalid one, because it does not
5972 declare anything. Therefore, if we did not handle this case
5973 specially, we would issue an error message about an invalid
5974 declaration. */
5975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5976 {
5977 /* We're going to speculatively look for a declaration, falling back
5978 to an expression, if necessary. */
5979 cp_parser_parse_tentatively (parser);
5980 /* Parse the declaration. */
5981 cp_parser_simple_declaration (parser,
5982 /*function_definition_allowed_p=*/false);
5983 /* If the tentative parse failed, then we shall need to look for an
5984 expression-statement. */
5985 if (cp_parser_parse_definitely (parser))
5986 return;
5987 }
5988
a5bcc582 5989 cp_parser_expression_statement (parser, false);
a723baf1
MM
5990}
5991
5992/* Parse a jump-statement.
5993
5994 jump-statement:
5995 break ;
5996 continue ;
5997 return expression [opt] ;
5998 goto identifier ;
5999
6000 GNU extension:
6001
6002 jump-statement:
6003 goto * expression ;
6004
6005 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6006 GOTO_STMT. */
6007
6008static tree
94edc4ab 6009cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6010{
6011 tree statement = error_mark_node;
6012 cp_token *token;
6013 enum rid keyword;
6014
6015 /* Peek at the next token. */
6016 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6017 if (!token)
6018 return error_mark_node;
6019
6020 /* See what kind of keyword it is. */
6021 keyword = token->keyword;
6022 switch (keyword)
6023 {
6024 case RID_BREAK:
0e59b3fb
MM
6025 if (!parser->in_switch_statement_p
6026 && !parser->in_iteration_statement_p)
6027 {
6028 error ("break statement not within loop or switch");
6029 statement = error_mark_node;
6030 }
6031 else
6032 statement = finish_break_stmt ();
a723baf1
MM
6033 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6034 break;
6035
6036 case RID_CONTINUE:
0e59b3fb
MM
6037 if (!parser->in_iteration_statement_p)
6038 {
6039 error ("continue statement not within a loop");
6040 statement = error_mark_node;
6041 }
6042 else
6043 statement = finish_continue_stmt ();
a723baf1
MM
6044 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6045 break;
6046
6047 case RID_RETURN:
6048 {
6049 tree expr;
6050
6051 /* If the next token is a `;', then there is no
6052 expression. */
6053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6054 expr = cp_parser_expression (parser);
6055 else
6056 expr = NULL_TREE;
6057 /* Build the return-statement. */
6058 statement = finish_return_stmt (expr);
6059 /* Look for the final `;'. */
6060 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6061 }
6062 break;
6063
6064 case RID_GOTO:
6065 /* Create the goto-statement. */
6066 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6067 {
6068 /* Issue a warning about this use of a GNU extension. */
6069 if (pedantic)
6070 pedwarn ("ISO C++ forbids computed gotos");
6071 /* Consume the '*' token. */
6072 cp_lexer_consume_token (parser->lexer);
6073 /* Parse the dependent expression. */
6074 finish_goto_stmt (cp_parser_expression (parser));
6075 }
6076 else
6077 finish_goto_stmt (cp_parser_identifier (parser));
6078 /* Look for the final `;'. */
6079 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6080 break;
6081
6082 default:
6083 cp_parser_error (parser, "expected jump-statement");
6084 break;
6085 }
6086
6087 return statement;
6088}
6089
6090/* Parse a declaration-statement.
6091
6092 declaration-statement:
6093 block-declaration */
6094
6095static void
94edc4ab 6096cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6097{
6098 /* Parse the block-declaration. */
6099 cp_parser_block_declaration (parser, /*statement_p=*/true);
6100
6101 /* Finish off the statement. */
6102 finish_stmt ();
6103}
6104
6105/* Some dependent statements (like `if (cond) statement'), are
6106 implicitly in their own scope. In other words, if the statement is
6107 a single statement (as opposed to a compound-statement), it is
6108 none-the-less treated as if it were enclosed in braces. Any
6109 declarations appearing in the dependent statement are out of scope
6110 after control passes that point. This function parses a statement,
6111 but ensures that is in its own scope, even if it is not a
6112 compound-statement.
6113
6114 Returns the new statement. */
6115
6116static tree
94edc4ab 6117cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6118{
6119 tree statement;
6120
6121 /* If the token is not a `{', then we must take special action. */
6122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6123 {
6124 /* Create a compound-statement. */
7a3397c7 6125 statement = begin_compound_stmt (/*has_no_scope=*/false);
a723baf1 6126 /* Parse the dependent-statement. */
a5bcc582 6127 cp_parser_statement (parser, false);
a723baf1 6128 /* Finish the dummy compound-statement. */
7a3397c7 6129 finish_compound_stmt (statement);
a723baf1
MM
6130 }
6131 /* Otherwise, we simply parse the statement directly. */
6132 else
a5bcc582 6133 statement = cp_parser_compound_statement (parser, false);
a723baf1
MM
6134
6135 /* Return the statement. */
6136 return statement;
6137}
6138
6139/* For some dependent statements (like `while (cond) statement'), we
6140 have already created a scope. Therefore, even if the dependent
6141 statement is a compound-statement, we do not want to create another
6142 scope. */
6143
6144static void
94edc4ab 6145cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6146{
6147 /* If the token is not a `{', then we must take special action. */
6148 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6149 {
6150 tree statement;
6151
6152 /* Create a compound-statement. */
7a3397c7 6153 statement = begin_compound_stmt (/*has_no_scope=*/true);
a723baf1 6154 /* Parse the dependent-statement. */
a5bcc582 6155 cp_parser_statement (parser, false);
a723baf1 6156 /* Finish the dummy compound-statement. */
7a3397c7 6157 finish_compound_stmt (statement);
a723baf1
MM
6158 }
6159 /* Otherwise, we simply parse the statement directly. */
6160 else
a5bcc582 6161 cp_parser_statement (parser, false);
a723baf1
MM
6162}
6163
6164/* Declarations [gram.dcl.dcl] */
6165
6166/* Parse an optional declaration-sequence.
6167
6168 declaration-seq:
6169 declaration
6170 declaration-seq declaration */
6171
6172static void
94edc4ab 6173cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6174{
6175 while (true)
6176 {
6177 cp_token *token;
6178
6179 token = cp_lexer_peek_token (parser->lexer);
6180
6181 if (token->type == CPP_CLOSE_BRACE
6182 || token->type == CPP_EOF)
6183 break;
6184
6185 if (token->type == CPP_SEMICOLON)
6186 {
6187 /* A declaration consisting of a single semicolon is
6188 invalid. Allow it unless we're being pedantic. */
499b568f 6189 if (pedantic && !in_system_header)
a723baf1
MM
6190 pedwarn ("extra `;'");
6191 cp_lexer_consume_token (parser->lexer);
6192 continue;
6193 }
6194
c838d82f 6195 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6196 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6197 while (pending_lang_change > 0)
6198 {
6199 push_lang_context (lang_name_c);
6200 --pending_lang_change;
6201 }
6202 while (pending_lang_change < 0)
6203 {
6204 pop_lang_context ();
6205 ++pending_lang_change;
6206 }
6207
6208 /* Parse the declaration itself. */
a723baf1
MM
6209 cp_parser_declaration (parser);
6210 }
6211}
6212
6213/* Parse a declaration.
6214
6215 declaration:
6216 block-declaration
6217 function-definition
6218 template-declaration
6219 explicit-instantiation
6220 explicit-specialization
6221 linkage-specification
1092805d
MM
6222 namespace-definition
6223
6224 GNU extension:
6225
6226 declaration:
6227 __extension__ declaration */
a723baf1
MM
6228
6229static void
94edc4ab 6230cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6231{
6232 cp_token token1;
6233 cp_token token2;
1092805d
MM
6234 int saved_pedantic;
6235
6236 /* Check for the `__extension__' keyword. */
6237 if (cp_parser_extension_opt (parser, &saved_pedantic))
6238 {
6239 /* Parse the qualified declaration. */
6240 cp_parser_declaration (parser);
6241 /* Restore the PEDANTIC flag. */
6242 pedantic = saved_pedantic;
6243
6244 return;
6245 }
a723baf1
MM
6246
6247 /* Try to figure out what kind of declaration is present. */
6248 token1 = *cp_lexer_peek_token (parser->lexer);
6249 if (token1.type != CPP_EOF)
6250 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6251
6252 /* If the next token is `extern' and the following token is a string
6253 literal, then we have a linkage specification. */
6254 if (token1.keyword == RID_EXTERN
6255 && cp_parser_is_string_literal (&token2))
6256 cp_parser_linkage_specification (parser);
6257 /* If the next token is `template', then we have either a template
6258 declaration, an explicit instantiation, or an explicit
6259 specialization. */
6260 else if (token1.keyword == RID_TEMPLATE)
6261 {
6262 /* `template <>' indicates a template specialization. */
6263 if (token2.type == CPP_LESS
6264 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6265 cp_parser_explicit_specialization (parser);
6266 /* `template <' indicates a template declaration. */
6267 else if (token2.type == CPP_LESS)
6268 cp_parser_template_declaration (parser, /*member_p=*/false);
6269 /* Anything else must be an explicit instantiation. */
6270 else
6271 cp_parser_explicit_instantiation (parser);
6272 }
6273 /* If the next token is `export', then we have a template
6274 declaration. */
6275 else if (token1.keyword == RID_EXPORT)
6276 cp_parser_template_declaration (parser, /*member_p=*/false);
6277 /* If the next token is `extern', 'static' or 'inline' and the one
6278 after that is `template', we have a GNU extended explicit
6279 instantiation directive. */
6280 else if (cp_parser_allow_gnu_extensions_p (parser)
6281 && (token1.keyword == RID_EXTERN
6282 || token1.keyword == RID_STATIC
6283 || token1.keyword == RID_INLINE)
6284 && token2.keyword == RID_TEMPLATE)
6285 cp_parser_explicit_instantiation (parser);
6286 /* If the next token is `namespace', check for a named or unnamed
6287 namespace definition. */
6288 else if (token1.keyword == RID_NAMESPACE
6289 && (/* A named namespace definition. */
6290 (token2.type == CPP_NAME
6291 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6292 == CPP_OPEN_BRACE))
6293 /* An unnamed namespace definition. */
6294 || token2.type == CPP_OPEN_BRACE))
6295 cp_parser_namespace_definition (parser);
6296 /* We must have either a block declaration or a function
6297 definition. */
6298 else
6299 /* Try to parse a block-declaration, or a function-definition. */
6300 cp_parser_block_declaration (parser, /*statement_p=*/false);
6301}
6302
6303/* Parse a block-declaration.
6304
6305 block-declaration:
6306 simple-declaration
6307 asm-definition
6308 namespace-alias-definition
6309 using-declaration
6310 using-directive
6311
6312 GNU Extension:
6313
6314 block-declaration:
6315 __extension__ block-declaration
6316 label-declaration
6317
34cd5ae7 6318 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6319 part of a declaration-statement. */
6320
6321static void
6322cp_parser_block_declaration (cp_parser *parser,
6323 bool statement_p)
6324{
6325 cp_token *token1;
6326 int saved_pedantic;
6327
6328 /* Check for the `__extension__' keyword. */
6329 if (cp_parser_extension_opt (parser, &saved_pedantic))
6330 {
6331 /* Parse the qualified declaration. */
6332 cp_parser_block_declaration (parser, statement_p);
6333 /* Restore the PEDANTIC flag. */
6334 pedantic = saved_pedantic;
6335
6336 return;
6337 }
6338
6339 /* Peek at the next token to figure out which kind of declaration is
6340 present. */
6341 token1 = cp_lexer_peek_token (parser->lexer);
6342
6343 /* If the next keyword is `asm', we have an asm-definition. */
6344 if (token1->keyword == RID_ASM)
6345 {
6346 if (statement_p)
6347 cp_parser_commit_to_tentative_parse (parser);
6348 cp_parser_asm_definition (parser);
6349 }
6350 /* If the next keyword is `namespace', we have a
6351 namespace-alias-definition. */
6352 else if (token1->keyword == RID_NAMESPACE)
6353 cp_parser_namespace_alias_definition (parser);
6354 /* If the next keyword is `using', we have either a
6355 using-declaration or a using-directive. */
6356 else if (token1->keyword == RID_USING)
6357 {
6358 cp_token *token2;
6359
6360 if (statement_p)
6361 cp_parser_commit_to_tentative_parse (parser);
6362 /* If the token after `using' is `namespace', then we have a
6363 using-directive. */
6364 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6365 if (token2->keyword == RID_NAMESPACE)
6366 cp_parser_using_directive (parser);
6367 /* Otherwise, it's a using-declaration. */
6368 else
6369 cp_parser_using_declaration (parser);
6370 }
6371 /* If the next keyword is `__label__' we have a label declaration. */
6372 else if (token1->keyword == RID_LABEL)
6373 {
6374 if (statement_p)
6375 cp_parser_commit_to_tentative_parse (parser);
6376 cp_parser_label_declaration (parser);
6377 }
6378 /* Anything else must be a simple-declaration. */
6379 else
6380 cp_parser_simple_declaration (parser, !statement_p);
6381}
6382
6383/* Parse a simple-declaration.
6384
6385 simple-declaration:
6386 decl-specifier-seq [opt] init-declarator-list [opt] ;
6387
6388 init-declarator-list:
6389 init-declarator
6390 init-declarator-list , init-declarator
6391
34cd5ae7 6392 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6393 function-definition as a simple-declaration. */
a723baf1
MM
6394
6395static void
94edc4ab
NN
6396cp_parser_simple_declaration (cp_parser* parser,
6397 bool function_definition_allowed_p)
a723baf1
MM
6398{
6399 tree decl_specifiers;
6400 tree attributes;
560ad596 6401 int declares_class_or_enum;
a723baf1
MM
6402 bool saw_declarator;
6403
6404 /* Defer access checks until we know what is being declared; the
6405 checks for names appearing in the decl-specifier-seq should be
6406 done as if we were in the scope of the thing being declared. */
8d241e0b 6407 push_deferring_access_checks (dk_deferred);
cf22909c 6408
a723baf1
MM
6409 /* Parse the decl-specifier-seq. We have to keep track of whether
6410 or not the decl-specifier-seq declares a named class or
6411 enumeration type, since that is the only case in which the
6412 init-declarator-list is allowed to be empty.
6413
6414 [dcl.dcl]
6415
6416 In a simple-declaration, the optional init-declarator-list can be
6417 omitted only when declaring a class or enumeration, that is when
6418 the decl-specifier-seq contains either a class-specifier, an
6419 elaborated-type-specifier, or an enum-specifier. */
6420 decl_specifiers
6421 = cp_parser_decl_specifier_seq (parser,
6422 CP_PARSER_FLAGS_OPTIONAL,
6423 &attributes,
6424 &declares_class_or_enum);
6425 /* We no longer need to defer access checks. */
cf22909c 6426 stop_deferring_access_checks ();
24c0ef37 6427
39703eb9
MM
6428 /* In a block scope, a valid declaration must always have a
6429 decl-specifier-seq. By not trying to parse declarators, we can
6430 resolve the declaration/expression ambiguity more quickly. */
6431 if (!function_definition_allowed_p && !decl_specifiers)
6432 {
6433 cp_parser_error (parser, "expected declaration");
6434 goto done;
6435 }
6436
8fbc5ae7
MM
6437 /* If the next two tokens are both identifiers, the code is
6438 erroneous. The usual cause of this situation is code like:
6439
6440 T t;
6441
6442 where "T" should name a type -- but does not. */
6443 if (cp_parser_diagnose_invalid_type_name (parser))
6444 {
8d241e0b 6445 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6446 looking at a declaration. */
6447 cp_parser_commit_to_tentative_parse (parser);
6448 /* Give up. */
39703eb9 6449 goto done;
8fbc5ae7
MM
6450 }
6451
a723baf1
MM
6452 /* Keep going until we hit the `;' at the end of the simple
6453 declaration. */
6454 saw_declarator = false;
6455 while (cp_lexer_next_token_is_not (parser->lexer,
6456 CPP_SEMICOLON))
6457 {
6458 cp_token *token;
6459 bool function_definition_p;
560ad596 6460 tree decl;
a723baf1
MM
6461
6462 saw_declarator = true;
6463 /* Parse the init-declarator. */
560ad596
MM
6464 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6465 function_definition_allowed_p,
6466 /*member_p=*/false,
6467 declares_class_or_enum,
6468 &function_definition_p);
1fb3244a
MM
6469 /* If an error occurred while parsing tentatively, exit quickly.
6470 (That usually happens when in the body of a function; each
6471 statement is treated as a declaration-statement until proven
6472 otherwise.) */
6473 if (cp_parser_error_occurred (parser))
39703eb9 6474 goto done;
a723baf1
MM
6475 /* Handle function definitions specially. */
6476 if (function_definition_p)
6477 {
6478 /* If the next token is a `,', then we are probably
6479 processing something like:
6480
6481 void f() {}, *p;
6482
6483 which is erroneous. */
6484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6485 error ("mixing declarations and function-definitions is forbidden");
6486 /* Otherwise, we're done with the list of declarators. */
6487 else
24c0ef37 6488 {
cf22909c 6489 pop_deferring_access_checks ();
24c0ef37
GS
6490 return;
6491 }
a723baf1
MM
6492 }
6493 /* The next token should be either a `,' or a `;'. */
6494 token = cp_lexer_peek_token (parser->lexer);
6495 /* If it's a `,', there are more declarators to come. */
6496 if (token->type == CPP_COMMA)
6497 cp_lexer_consume_token (parser->lexer);
6498 /* If it's a `;', we are done. */
6499 else if (token->type == CPP_SEMICOLON)
6500 break;
6501 /* Anything else is an error. */
6502 else
6503 {
6504 cp_parser_error (parser, "expected `,' or `;'");
6505 /* Skip tokens until we reach the end of the statement. */
6506 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
6507 /* If the next token is now a `;', consume it. */
6508 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6509 cp_lexer_consume_token (parser->lexer);
39703eb9 6510 goto done;
a723baf1
MM
6511 }
6512 /* After the first time around, a function-definition is not
6513 allowed -- even if it was OK at first. For example:
6514
6515 int i, f() {}
6516
6517 is not valid. */
6518 function_definition_allowed_p = false;
6519 }
6520
6521 /* Issue an error message if no declarators are present, and the
6522 decl-specifier-seq does not itself declare a class or
6523 enumeration. */
6524 if (!saw_declarator)
6525 {
6526 if (cp_parser_declares_only_class_p (parser))
6527 shadow_tag (decl_specifiers);
6528 /* Perform any deferred access checks. */
cf22909c 6529 perform_deferred_access_checks ();
a723baf1
MM
6530 }
6531
6532 /* Consume the `;'. */
6533 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6534
39703eb9
MM
6535 done:
6536 pop_deferring_access_checks ();
a723baf1
MM
6537}
6538
6539/* Parse a decl-specifier-seq.
6540
6541 decl-specifier-seq:
6542 decl-specifier-seq [opt] decl-specifier
6543
6544 decl-specifier:
6545 storage-class-specifier
6546 type-specifier
6547 function-specifier
6548 friend
6549 typedef
6550
6551 GNU Extension:
6552
6553 decl-specifier-seq:
6554 decl-specifier-seq [opt] attributes
6555
6556 Returns a TREE_LIST, giving the decl-specifiers in the order they
6557 appear in the source code. The TREE_VALUE of each node is the
6558 decl-specifier. For a keyword (such as `auto' or `friend'), the
34cd5ae7 6559 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
a723baf1
MM
6560 representation of a type-specifier, see cp_parser_type_specifier.
6561
6562 If there are attributes, they will be stored in *ATTRIBUTES,
6563 represented as described above cp_parser_attributes.
6564
6565 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6566 appears, and the entity that will be a friend is not going to be a
6567 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6568 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
560ad596
MM
6569 friendship is granted might not be a class.
6570
6571 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 6572 flags:
560ad596
MM
6573
6574 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 6575 (i.e., a type declaration)
560ad596 6576 2: one of the decl-specifiers is an enum-specifier or a
543ca912 6577 class-specifier (i.e., a type definition)
560ad596
MM
6578
6579 */
a723baf1
MM
6580
6581static tree
94edc4ab
NN
6582cp_parser_decl_specifier_seq (cp_parser* parser,
6583 cp_parser_flags flags,
6584 tree* attributes,
560ad596 6585 int* declares_class_or_enum)
a723baf1
MM
6586{
6587 tree decl_specs = NULL_TREE;
6588 bool friend_p = false;
f2ce60b8
NS
6589 bool constructor_possible_p = !parser->in_declarator_p;
6590
a723baf1 6591 /* Assume no class or enumeration type is declared. */
560ad596 6592 *declares_class_or_enum = 0;
a723baf1
MM
6593
6594 /* Assume there are no attributes. */
6595 *attributes = NULL_TREE;
6596
6597 /* Keep reading specifiers until there are no more to read. */
6598 while (true)
6599 {
6600 tree decl_spec = NULL_TREE;
6601 bool constructor_p;
6602 cp_token *token;
6603
6604 /* Peek at the next token. */
6605 token = cp_lexer_peek_token (parser->lexer);
6606 /* Handle attributes. */
6607 if (token->keyword == RID_ATTRIBUTE)
6608 {
6609 /* Parse the attributes. */
6610 decl_spec = cp_parser_attributes_opt (parser);
6611 /* Add them to the list. */
6612 *attributes = chainon (*attributes, decl_spec);
6613 continue;
6614 }
6615 /* If the next token is an appropriate keyword, we can simply
6616 add it to the list. */
6617 switch (token->keyword)
6618 {
6619 case RID_FRIEND:
6620 /* decl-specifier:
6621 friend */
1918facf
SB
6622 if (friend_p)
6623 error ("duplicate `friend'");
6624 else
6625 friend_p = true;
a723baf1
MM
6626 /* The representation of the specifier is simply the
6627 appropriate TREE_IDENTIFIER node. */
6628 decl_spec = token->value;
6629 /* Consume the token. */
6630 cp_lexer_consume_token (parser->lexer);
6631 break;
6632
6633 /* function-specifier:
6634 inline
6635 virtual
6636 explicit */
6637 case RID_INLINE:
6638 case RID_VIRTUAL:
6639 case RID_EXPLICIT:
6640 decl_spec = cp_parser_function_specifier_opt (parser);
6641 break;
6642
6643 /* decl-specifier:
6644 typedef */
6645 case RID_TYPEDEF:
6646 /* The representation of the specifier is simply the
6647 appropriate TREE_IDENTIFIER node. */
6648 decl_spec = token->value;
6649 /* Consume the token. */
6650 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6651 /* A constructor declarator cannot appear in a typedef. */
6652 constructor_possible_p = false;
c006d942
MM
6653 /* The "typedef" keyword can only occur in a declaration; we
6654 may as well commit at this point. */
6655 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6656 break;
6657
6658 /* storage-class-specifier:
6659 auto
6660 register
6661 static
6662 extern
6663 mutable
6664
6665 GNU Extension:
6666 thread */
6667 case RID_AUTO:
6668 case RID_REGISTER:
6669 case RID_STATIC:
6670 case RID_EXTERN:
6671 case RID_MUTABLE:
6672 case RID_THREAD:
6673 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6674 break;
6675
6676 default:
6677 break;
6678 }
6679
6680 /* Constructors are a special case. The `S' in `S()' is not a
6681 decl-specifier; it is the beginning of the declarator. */
6682 constructor_p = (!decl_spec
2050a1bb 6683 && constructor_possible_p
a723baf1
MM
6684 && cp_parser_constructor_declarator_p (parser,
6685 friend_p));
6686
6687 /* If we don't have a DECL_SPEC yet, then we must be looking at
6688 a type-specifier. */
6689 if (!decl_spec && !constructor_p)
6690 {
560ad596 6691 int decl_spec_declares_class_or_enum;
a723baf1
MM
6692 bool is_cv_qualifier;
6693
6694 decl_spec
6695 = cp_parser_type_specifier (parser, flags,
6696 friend_p,
6697 /*is_declaration=*/true,
6698 &decl_spec_declares_class_or_enum,
6699 &is_cv_qualifier);
6700
6701 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6702
6703 /* If this type-specifier referenced a user-defined type
6704 (a typedef, class-name, etc.), then we can't allow any
6705 more such type-specifiers henceforth.
6706
6707 [dcl.spec]
6708
6709 The longest sequence of decl-specifiers that could
6710 possibly be a type name is taken as the
6711 decl-specifier-seq of a declaration. The sequence shall
6712 be self-consistent as described below.
6713
6714 [dcl.type]
6715
6716 As a general rule, at most one type-specifier is allowed
6717 in the complete decl-specifier-seq of a declaration. The
6718 only exceptions are the following:
6719
6720 -- const or volatile can be combined with any other
6721 type-specifier.
6722
6723 -- signed or unsigned can be combined with char, long,
6724 short, or int.
6725
6726 -- ..
6727
6728 Example:
6729
6730 typedef char* Pc;
6731 void g (const int Pc);
6732
6733 Here, Pc is *not* part of the decl-specifier seq; it's
6734 the declarator. Therefore, once we see a type-specifier
6735 (other than a cv-qualifier), we forbid any additional
6736 user-defined types. We *do* still allow things like `int
6737 int' to be considered a decl-specifier-seq, and issue the
6738 error message later. */
6739 if (decl_spec && !is_cv_qualifier)
6740 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6741 /* A constructor declarator cannot follow a type-specifier. */
6742 if (decl_spec)
6743 constructor_possible_p = false;
a723baf1
MM
6744 }
6745
6746 /* If we still do not have a DECL_SPEC, then there are no more
6747 decl-specifiers. */
6748 if (!decl_spec)
6749 {
6750 /* Issue an error message, unless the entire construct was
6751 optional. */
6752 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6753 {
6754 cp_parser_error (parser, "expected decl specifier");
6755 return error_mark_node;
6756 }
6757
6758 break;
6759 }
6760
6761 /* Add the DECL_SPEC to the list of specifiers. */
e90c7b84
ILT
6762 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6763 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
a723baf1
MM
6764
6765 /* After we see one decl-specifier, further decl-specifiers are
6766 always optional. */
6767 flags |= CP_PARSER_FLAGS_OPTIONAL;
6768 }
6769
0426c4ca
SB
6770 /* Don't allow a friend specifier with a class definition. */
6771 if (friend_p && (*declares_class_or_enum & 2))
6772 error ("class definition may not be declared a friend");
6773
a723baf1
MM
6774 /* We have built up the DECL_SPECS in reverse order. Return them in
6775 the correct order. */
6776 return nreverse (decl_specs);
6777}
6778
6779/* Parse an (optional) storage-class-specifier.
6780
6781 storage-class-specifier:
6782 auto
6783 register
6784 static
6785 extern
6786 mutable
6787
6788 GNU Extension:
6789
6790 storage-class-specifier:
6791 thread
6792
6793 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6794
6795static tree
94edc4ab 6796cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6797{
6798 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6799 {
6800 case RID_AUTO:
6801 case RID_REGISTER:
6802 case RID_STATIC:
6803 case RID_EXTERN:
6804 case RID_MUTABLE:
6805 case RID_THREAD:
6806 /* Consume the token. */
6807 return cp_lexer_consume_token (parser->lexer)->value;
6808
6809 default:
6810 return NULL_TREE;
6811 }
6812}
6813
6814/* Parse an (optional) function-specifier.
6815
6816 function-specifier:
6817 inline
6818 virtual
6819 explicit
6820
6821 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6822
6823static tree
94edc4ab 6824cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6825{
6826 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6827 {
6828 case RID_INLINE:
6829 case RID_VIRTUAL:
6830 case RID_EXPLICIT:
6831 /* Consume the token. */
6832 return cp_lexer_consume_token (parser->lexer)->value;
6833
6834 default:
6835 return NULL_TREE;
6836 }
6837}
6838
6839/* Parse a linkage-specification.
6840
6841 linkage-specification:
6842 extern string-literal { declaration-seq [opt] }
6843 extern string-literal declaration */
6844
6845static void
94edc4ab 6846cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6847{
6848 cp_token *token;
6849 tree linkage;
6850
6851 /* Look for the `extern' keyword. */
6852 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6853
6854 /* Peek at the next token. */
6855 token = cp_lexer_peek_token (parser->lexer);
6856 /* If it's not a string-literal, then there's a problem. */
6857 if (!cp_parser_is_string_literal (token))
6858 {
6859 cp_parser_error (parser, "expected language-name");
6860 return;
6861 }
6862 /* Consume the token. */
6863 cp_lexer_consume_token (parser->lexer);
6864
6865 /* Transform the literal into an identifier. If the literal is a
6866 wide-character string, or contains embedded NULs, then we can't
6867 handle it as the user wants. */
6868 if (token->type == CPP_WSTRING
6869 || (strlen (TREE_STRING_POINTER (token->value))
6870 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6871 {
6872 cp_parser_error (parser, "invalid linkage-specification");
6873 /* Assume C++ linkage. */
6874 linkage = get_identifier ("c++");
6875 }
6876 /* If it's a simple string constant, things are easier. */
6877 else
6878 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6879
6880 /* We're now using the new linkage. */
6881 push_lang_context (linkage);
6882
6883 /* If the next token is a `{', then we're using the first
6884 production. */
6885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6886 {
6887 /* Consume the `{' token. */
6888 cp_lexer_consume_token (parser->lexer);
6889 /* Parse the declarations. */
6890 cp_parser_declaration_seq_opt (parser);
6891 /* Look for the closing `}'. */
6892 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6893 }
6894 /* Otherwise, there's just one declaration. */
6895 else
6896 {
6897 bool saved_in_unbraced_linkage_specification_p;
6898
6899 saved_in_unbraced_linkage_specification_p
6900 = parser->in_unbraced_linkage_specification_p;
6901 parser->in_unbraced_linkage_specification_p = true;
6902 have_extern_spec = true;
6903 cp_parser_declaration (parser);
6904 have_extern_spec = false;
6905 parser->in_unbraced_linkage_specification_p
6906 = saved_in_unbraced_linkage_specification_p;
6907 }
6908
6909 /* We're done with the linkage-specification. */
6910 pop_lang_context ();
6911}
6912
6913/* Special member functions [gram.special] */
6914
6915/* Parse a conversion-function-id.
6916
6917 conversion-function-id:
6918 operator conversion-type-id
6919
6920 Returns an IDENTIFIER_NODE representing the operator. */
6921
6922static tree
94edc4ab 6923cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
6924{
6925 tree type;
6926 tree saved_scope;
6927 tree saved_qualifying_scope;
6928 tree saved_object_scope;
6929
6930 /* Look for the `operator' token. */
6931 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6932 return error_mark_node;
6933 /* When we parse the conversion-type-id, the current scope will be
6934 reset. However, we need that information in able to look up the
6935 conversion function later, so we save it here. */
6936 saved_scope = parser->scope;
6937 saved_qualifying_scope = parser->qualifying_scope;
6938 saved_object_scope = parser->object_scope;
6939 /* We must enter the scope of the class so that the names of
6940 entities declared within the class are available in the
6941 conversion-type-id. For example, consider:
6942
6943 struct S {
6944 typedef int I;
6945 operator I();
6946 };
6947
6948 S::operator I() { ... }
6949
6950 In order to see that `I' is a type-name in the definition, we
6951 must be in the scope of `S'. */
6952 if (saved_scope)
6953 push_scope (saved_scope);
6954 /* Parse the conversion-type-id. */
6955 type = cp_parser_conversion_type_id (parser);
6956 /* Leave the scope of the class, if any. */
6957 if (saved_scope)
6958 pop_scope (saved_scope);
6959 /* Restore the saved scope. */
6960 parser->scope = saved_scope;
6961 parser->qualifying_scope = saved_qualifying_scope;
6962 parser->object_scope = saved_object_scope;
6963 /* If the TYPE is invalid, indicate failure. */
6964 if (type == error_mark_node)
6965 return error_mark_node;
6966 return mangle_conv_op_name_for_type (type);
6967}
6968
6969/* Parse a conversion-type-id:
6970
6971 conversion-type-id:
6972 type-specifier-seq conversion-declarator [opt]
6973
6974 Returns the TYPE specified. */
6975
6976static tree
94edc4ab 6977cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
6978{
6979 tree attributes;
6980 tree type_specifiers;
6981 tree declarator;
6982
6983 /* Parse the attributes. */
6984 attributes = cp_parser_attributes_opt (parser);
6985 /* Parse the type-specifiers. */
6986 type_specifiers = cp_parser_type_specifier_seq (parser);
6987 /* If that didn't work, stop. */
6988 if (type_specifiers == error_mark_node)
6989 return error_mark_node;
6990 /* Parse the conversion-declarator. */
6991 declarator = cp_parser_conversion_declarator_opt (parser);
6992
6993 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6994 /*initialized=*/0, &attributes);
6995}
6996
6997/* Parse an (optional) conversion-declarator.
6998
6999 conversion-declarator:
7000 ptr-operator conversion-declarator [opt]
7001
7002 Returns a representation of the declarator. See
7003 cp_parser_declarator for details. */
7004
7005static tree
94edc4ab 7006cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7007{
7008 enum tree_code code;
7009 tree class_type;
7010 tree cv_qualifier_seq;
7011
7012 /* We don't know if there's a ptr-operator next, or not. */
7013 cp_parser_parse_tentatively (parser);
7014 /* Try the ptr-operator. */
7015 code = cp_parser_ptr_operator (parser, &class_type,
7016 &cv_qualifier_seq);
7017 /* If it worked, look for more conversion-declarators. */
7018 if (cp_parser_parse_definitely (parser))
7019 {
7020 tree declarator;
7021
7022 /* Parse another optional declarator. */
7023 declarator = cp_parser_conversion_declarator_opt (parser);
7024
7025 /* Create the representation of the declarator. */
7026 if (code == INDIRECT_REF)
7027 declarator = make_pointer_declarator (cv_qualifier_seq,
7028 declarator);
7029 else
7030 declarator = make_reference_declarator (cv_qualifier_seq,
7031 declarator);
7032
7033 /* Handle the pointer-to-member case. */
7034 if (class_type)
7035 declarator = build_nt (SCOPE_REF, class_type, declarator);
7036
7037 return declarator;
7038 }
7039
7040 return NULL_TREE;
7041}
7042
7043/* Parse an (optional) ctor-initializer.
7044
7045 ctor-initializer:
7046 : mem-initializer-list
7047
7048 Returns TRUE iff the ctor-initializer was actually present. */
7049
7050static bool
94edc4ab 7051cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7052{
7053 /* If the next token is not a `:', then there is no
7054 ctor-initializer. */
7055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7056 {
7057 /* Do default initialization of any bases and members. */
7058 if (DECL_CONSTRUCTOR_P (current_function_decl))
7059 finish_mem_initializers (NULL_TREE);
7060
7061 return false;
7062 }
7063
7064 /* Consume the `:' token. */
7065 cp_lexer_consume_token (parser->lexer);
7066 /* And the mem-initializer-list. */
7067 cp_parser_mem_initializer_list (parser);
7068
7069 return true;
7070}
7071
7072/* Parse a mem-initializer-list.
7073
7074 mem-initializer-list:
7075 mem-initializer
7076 mem-initializer , mem-initializer-list */
7077
7078static void
94edc4ab 7079cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7080{
7081 tree mem_initializer_list = NULL_TREE;
7082
7083 /* Let the semantic analysis code know that we are starting the
7084 mem-initializer-list. */
0e136342
MM
7085 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7086 error ("only constructors take base initializers");
a723baf1
MM
7087
7088 /* Loop through the list. */
7089 while (true)
7090 {
7091 tree mem_initializer;
7092
7093 /* Parse the mem-initializer. */
7094 mem_initializer = cp_parser_mem_initializer (parser);
7095 /* Add it to the list, unless it was erroneous. */
7096 if (mem_initializer)
7097 {
7098 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7099 mem_initializer_list = mem_initializer;
7100 }
7101 /* If the next token is not a `,', we're done. */
7102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7103 break;
7104 /* Consume the `,' token. */
7105 cp_lexer_consume_token (parser->lexer);
7106 }
7107
7108 /* Perform semantic analysis. */
0e136342
MM
7109 if (DECL_CONSTRUCTOR_P (current_function_decl))
7110 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7111}
7112
7113/* Parse a mem-initializer.
7114
7115 mem-initializer:
7116 mem-initializer-id ( expression-list [opt] )
7117
7118 GNU extension:
7119
7120 mem-initializer:
34cd5ae7 7121 ( expression-list [opt] )
a723baf1
MM
7122
7123 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7124 class) or FIELD_DECL (for a non-static data member) to initialize;
7125 the TREE_VALUE is the expression-list. */
7126
7127static tree
94edc4ab 7128cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7129{
7130 tree mem_initializer_id;
7131 tree expression_list;
1f5a253a
NS
7132 tree member;
7133
a723baf1
MM
7134 /* Find out what is being initialized. */
7135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7136 {
7137 pedwarn ("anachronistic old-style base class initializer");
7138 mem_initializer_id = NULL_TREE;
7139 }
7140 else
7141 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7142 member = expand_member_init (mem_initializer_id);
7143 if (member && !DECL_P (member))
7144 in_base_initializer = 1;
7efa3e22 7145
39703eb9
MM
7146 expression_list
7147 = cp_parser_parenthesized_expression_list (parser, false,
7148 /*non_constant_p=*/NULL);
7efa3e22 7149 if (!expression_list)
a723baf1 7150 expression_list = void_type_node;
a723baf1 7151
1f5a253a
NS
7152 in_base_initializer = 0;
7153
7154 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7155}
7156
7157/* Parse a mem-initializer-id.
7158
7159 mem-initializer-id:
7160 :: [opt] nested-name-specifier [opt] class-name
7161 identifier
7162
7163 Returns a TYPE indicating the class to be initializer for the first
7164 production. Returns an IDENTIFIER_NODE indicating the data member
7165 to be initialized for the second production. */
7166
7167static tree
94edc4ab 7168cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7169{
7170 bool global_scope_p;
7171 bool nested_name_specifier_p;
7172 tree id;
7173
7174 /* Look for the optional `::' operator. */
7175 global_scope_p
7176 = (cp_parser_global_scope_opt (parser,
7177 /*current_scope_valid_p=*/false)
7178 != NULL_TREE);
7179 /* Look for the optional nested-name-specifier. The simplest way to
7180 implement:
7181
7182 [temp.res]
7183
7184 The keyword `typename' is not permitted in a base-specifier or
7185 mem-initializer; in these contexts a qualified name that
7186 depends on a template-parameter is implicitly assumed to be a
7187 type name.
7188
7189 is to assume that we have seen the `typename' keyword at this
7190 point. */
7191 nested_name_specifier_p
7192 = (cp_parser_nested_name_specifier_opt (parser,
7193 /*typename_keyword_p=*/true,
7194 /*check_dependency_p=*/true,
a668c6ad
MM
7195 /*type_p=*/true,
7196 /*is_declaration=*/true)
a723baf1
MM
7197 != NULL_TREE);
7198 /* If there is a `::' operator or a nested-name-specifier, then we
7199 are definitely looking for a class-name. */
7200 if (global_scope_p || nested_name_specifier_p)
7201 return cp_parser_class_name (parser,
7202 /*typename_keyword_p=*/true,
7203 /*template_keyword_p=*/false,
7204 /*type_p=*/false,
a723baf1 7205 /*check_dependency_p=*/true,
a668c6ad
MM
7206 /*class_head_p=*/false,
7207 /*is_declaration=*/true);
a723baf1
MM
7208 /* Otherwise, we could also be looking for an ordinary identifier. */
7209 cp_parser_parse_tentatively (parser);
7210 /* Try a class-name. */
7211 id = cp_parser_class_name (parser,
7212 /*typename_keyword_p=*/true,
7213 /*template_keyword_p=*/false,
7214 /*type_p=*/false,
a723baf1 7215 /*check_dependency_p=*/true,
a668c6ad
MM
7216 /*class_head_p=*/false,
7217 /*is_declaration=*/true);
a723baf1
MM
7218 /* If we found one, we're done. */
7219 if (cp_parser_parse_definitely (parser))
7220 return id;
7221 /* Otherwise, look for an ordinary identifier. */
7222 return cp_parser_identifier (parser);
7223}
7224
7225/* Overloading [gram.over] */
7226
7227/* Parse an operator-function-id.
7228
7229 operator-function-id:
7230 operator operator
7231
7232 Returns an IDENTIFIER_NODE for the operator which is a
7233 human-readable spelling of the identifier, e.g., `operator +'. */
7234
7235static tree
94edc4ab 7236cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7237{
7238 /* Look for the `operator' keyword. */
7239 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7240 return error_mark_node;
7241 /* And then the name of the operator itself. */
7242 return cp_parser_operator (parser);
7243}
7244
7245/* Parse an operator.
7246
7247 operator:
7248 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7249 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7250 || ++ -- , ->* -> () []
7251
7252 GNU Extensions:
7253
7254 operator:
7255 <? >? <?= >?=
7256
7257 Returns an IDENTIFIER_NODE for the operator which is a
7258 human-readable spelling of the identifier, e.g., `operator +'. */
7259
7260static tree
94edc4ab 7261cp_parser_operator (cp_parser* parser)
a723baf1
MM
7262{
7263 tree id = NULL_TREE;
7264 cp_token *token;
7265
7266 /* Peek at the next token. */
7267 token = cp_lexer_peek_token (parser->lexer);
7268 /* Figure out which operator we have. */
7269 switch (token->type)
7270 {
7271 case CPP_KEYWORD:
7272 {
7273 enum tree_code op;
7274
7275 /* The keyword should be either `new' or `delete'. */
7276 if (token->keyword == RID_NEW)
7277 op = NEW_EXPR;
7278 else if (token->keyword == RID_DELETE)
7279 op = DELETE_EXPR;
7280 else
7281 break;
7282
7283 /* Consume the `new' or `delete' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285
7286 /* Peek at the next token. */
7287 token = cp_lexer_peek_token (parser->lexer);
7288 /* If it's a `[' token then this is the array variant of the
7289 operator. */
7290 if (token->type == CPP_OPEN_SQUARE)
7291 {
7292 /* Consume the `[' token. */
7293 cp_lexer_consume_token (parser->lexer);
7294 /* Look for the `]' token. */
7295 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7296 id = ansi_opname (op == NEW_EXPR
7297 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7298 }
7299 /* Otherwise, we have the non-array variant. */
7300 else
7301 id = ansi_opname (op);
7302
7303 return id;
7304 }
7305
7306 case CPP_PLUS:
7307 id = ansi_opname (PLUS_EXPR);
7308 break;
7309
7310 case CPP_MINUS:
7311 id = ansi_opname (MINUS_EXPR);
7312 break;
7313
7314 case CPP_MULT:
7315 id = ansi_opname (MULT_EXPR);
7316 break;
7317
7318 case CPP_DIV:
7319 id = ansi_opname (TRUNC_DIV_EXPR);
7320 break;
7321
7322 case CPP_MOD:
7323 id = ansi_opname (TRUNC_MOD_EXPR);
7324 break;
7325
7326 case CPP_XOR:
7327 id = ansi_opname (BIT_XOR_EXPR);
7328 break;
7329
7330 case CPP_AND:
7331 id = ansi_opname (BIT_AND_EXPR);
7332 break;
7333
7334 case CPP_OR:
7335 id = ansi_opname (BIT_IOR_EXPR);
7336 break;
7337
7338 case CPP_COMPL:
7339 id = ansi_opname (BIT_NOT_EXPR);
7340 break;
7341
7342 case CPP_NOT:
7343 id = ansi_opname (TRUTH_NOT_EXPR);
7344 break;
7345
7346 case CPP_EQ:
7347 id = ansi_assopname (NOP_EXPR);
7348 break;
7349
7350 case CPP_LESS:
7351 id = ansi_opname (LT_EXPR);
7352 break;
7353
7354 case CPP_GREATER:
7355 id = ansi_opname (GT_EXPR);
7356 break;
7357
7358 case CPP_PLUS_EQ:
7359 id = ansi_assopname (PLUS_EXPR);
7360 break;
7361
7362 case CPP_MINUS_EQ:
7363 id = ansi_assopname (MINUS_EXPR);
7364 break;
7365
7366 case CPP_MULT_EQ:
7367 id = ansi_assopname (MULT_EXPR);
7368 break;
7369
7370 case CPP_DIV_EQ:
7371 id = ansi_assopname (TRUNC_DIV_EXPR);
7372 break;
7373
7374 case CPP_MOD_EQ:
7375 id = ansi_assopname (TRUNC_MOD_EXPR);
7376 break;
7377
7378 case CPP_XOR_EQ:
7379 id = ansi_assopname (BIT_XOR_EXPR);
7380 break;
7381
7382 case CPP_AND_EQ:
7383 id = ansi_assopname (BIT_AND_EXPR);
7384 break;
7385
7386 case CPP_OR_EQ:
7387 id = ansi_assopname (BIT_IOR_EXPR);
7388 break;
7389
7390 case CPP_LSHIFT:
7391 id = ansi_opname (LSHIFT_EXPR);
7392 break;
7393
7394 case CPP_RSHIFT:
7395 id = ansi_opname (RSHIFT_EXPR);
7396 break;
7397
7398 case CPP_LSHIFT_EQ:
7399 id = ansi_assopname (LSHIFT_EXPR);
7400 break;
7401
7402 case CPP_RSHIFT_EQ:
7403 id = ansi_assopname (RSHIFT_EXPR);
7404 break;
7405
7406 case CPP_EQ_EQ:
7407 id = ansi_opname (EQ_EXPR);
7408 break;
7409
7410 case CPP_NOT_EQ:
7411 id = ansi_opname (NE_EXPR);
7412 break;
7413
7414 case CPP_LESS_EQ:
7415 id = ansi_opname (LE_EXPR);
7416 break;
7417
7418 case CPP_GREATER_EQ:
7419 id = ansi_opname (GE_EXPR);
7420 break;
7421
7422 case CPP_AND_AND:
7423 id = ansi_opname (TRUTH_ANDIF_EXPR);
7424 break;
7425
7426 case CPP_OR_OR:
7427 id = ansi_opname (TRUTH_ORIF_EXPR);
7428 break;
7429
7430 case CPP_PLUS_PLUS:
7431 id = ansi_opname (POSTINCREMENT_EXPR);
7432 break;
7433
7434 case CPP_MINUS_MINUS:
7435 id = ansi_opname (PREDECREMENT_EXPR);
7436 break;
7437
7438 case CPP_COMMA:
7439 id = ansi_opname (COMPOUND_EXPR);
7440 break;
7441
7442 case CPP_DEREF_STAR:
7443 id = ansi_opname (MEMBER_REF);
7444 break;
7445
7446 case CPP_DEREF:
7447 id = ansi_opname (COMPONENT_REF);
7448 break;
7449
7450 case CPP_OPEN_PAREN:
7451 /* Consume the `('. */
7452 cp_lexer_consume_token (parser->lexer);
7453 /* Look for the matching `)'. */
7454 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7455 return ansi_opname (CALL_EXPR);
7456
7457 case CPP_OPEN_SQUARE:
7458 /* Consume the `['. */
7459 cp_lexer_consume_token (parser->lexer);
7460 /* Look for the matching `]'. */
7461 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7462 return ansi_opname (ARRAY_REF);
7463
7464 /* Extensions. */
7465 case CPP_MIN:
7466 id = ansi_opname (MIN_EXPR);
7467 break;
7468
7469 case CPP_MAX:
7470 id = ansi_opname (MAX_EXPR);
7471 break;
7472
7473 case CPP_MIN_EQ:
7474 id = ansi_assopname (MIN_EXPR);
7475 break;
7476
7477 case CPP_MAX_EQ:
7478 id = ansi_assopname (MAX_EXPR);
7479 break;
7480
7481 default:
7482 /* Anything else is an error. */
7483 break;
7484 }
7485
7486 /* If we have selected an identifier, we need to consume the
7487 operator token. */
7488 if (id)
7489 cp_lexer_consume_token (parser->lexer);
7490 /* Otherwise, no valid operator name was present. */
7491 else
7492 {
7493 cp_parser_error (parser, "expected operator");
7494 id = error_mark_node;
7495 }
7496
7497 return id;
7498}
7499
7500/* Parse a template-declaration.
7501
7502 template-declaration:
7503 export [opt] template < template-parameter-list > declaration
7504
7505 If MEMBER_P is TRUE, this template-declaration occurs within a
7506 class-specifier.
7507
7508 The grammar rule given by the standard isn't correct. What
7509 is really meant is:
7510
7511 template-declaration:
7512 export [opt] template-parameter-list-seq
7513 decl-specifier-seq [opt] init-declarator [opt] ;
7514 export [opt] template-parameter-list-seq
7515 function-definition
7516
7517 template-parameter-list-seq:
7518 template-parameter-list-seq [opt]
7519 template < template-parameter-list > */
7520
7521static void
94edc4ab 7522cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7523{
7524 /* Check for `export'. */
7525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7526 {
7527 /* Consume the `export' token. */
7528 cp_lexer_consume_token (parser->lexer);
7529 /* Warn that we do not support `export'. */
7530 warning ("keyword `export' not implemented, and will be ignored");
7531 }
7532
7533 cp_parser_template_declaration_after_export (parser, member_p);
7534}
7535
7536/* Parse a template-parameter-list.
7537
7538 template-parameter-list:
7539 template-parameter
7540 template-parameter-list , template-parameter
7541
7542 Returns a TREE_LIST. Each node represents a template parameter.
7543 The nodes are connected via their TREE_CHAINs. */
7544
7545static tree
94edc4ab 7546cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7547{
7548 tree parameter_list = NULL_TREE;
7549
7550 while (true)
7551 {
7552 tree parameter;
7553 cp_token *token;
7554
7555 /* Parse the template-parameter. */
7556 parameter = cp_parser_template_parameter (parser);
7557 /* Add it to the list. */
7558 parameter_list = process_template_parm (parameter_list,
7559 parameter);
7560
7561 /* Peek at the next token. */
7562 token = cp_lexer_peek_token (parser->lexer);
7563 /* If it's not a `,', we're done. */
7564 if (token->type != CPP_COMMA)
7565 break;
7566 /* Otherwise, consume the `,' token. */
7567 cp_lexer_consume_token (parser->lexer);
7568 }
7569
7570 return parameter_list;
7571}
7572
7573/* Parse a template-parameter.
7574
7575 template-parameter:
7576 type-parameter
7577 parameter-declaration
7578
7579 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7580 TREE_PURPOSE is the default value, if any. */
7581
7582static tree
94edc4ab 7583cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7584{
7585 cp_token *token;
7586
7587 /* Peek at the next token. */
7588 token = cp_lexer_peek_token (parser->lexer);
7589 /* If it is `class' or `template', we have a type-parameter. */
7590 if (token->keyword == RID_TEMPLATE)
7591 return cp_parser_type_parameter (parser);
7592 /* If it is `class' or `typename' we do not know yet whether it is a
7593 type parameter or a non-type parameter. Consider:
7594
7595 template <typename T, typename T::X X> ...
7596
7597 or:
7598
7599 template <class C, class D*> ...
7600
7601 Here, the first parameter is a type parameter, and the second is
7602 a non-type parameter. We can tell by looking at the token after
7603 the identifier -- if it is a `,', `=', or `>' then we have a type
7604 parameter. */
7605 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7606 {
7607 /* Peek at the token after `class' or `typename'. */
7608 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7609 /* If it's an identifier, skip it. */
7610 if (token->type == CPP_NAME)
7611 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7612 /* Now, see if the token looks like the end of a template
7613 parameter. */
7614 if (token->type == CPP_COMMA
7615 || token->type == CPP_EQ
7616 || token->type == CPP_GREATER)
7617 return cp_parser_type_parameter (parser);
7618 }
7619
7620 /* Otherwise, it is a non-type parameter.
7621
7622 [temp.param]
7623
7624 When parsing a default template-argument for a non-type
7625 template-parameter, the first non-nested `>' is taken as the end
7626 of the template parameter-list rather than a greater-than
7627 operator. */
7628 return
4bb8ca28
MM
7629 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7630 /*parenthesized_p=*/NULL);
a723baf1
MM
7631}
7632
7633/* Parse a type-parameter.
7634
7635 type-parameter:
7636 class identifier [opt]
7637 class identifier [opt] = type-id
7638 typename identifier [opt]
7639 typename identifier [opt] = type-id
7640 template < template-parameter-list > class identifier [opt]
7641 template < template-parameter-list > class identifier [opt]
7642 = id-expression
7643
7644 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7645 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7646 the declaration of the parameter. */
7647
7648static tree
94edc4ab 7649cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7650{
7651 cp_token *token;
7652 tree parameter;
7653
7654 /* Look for a keyword to tell us what kind of parameter this is. */
7655 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7656 "`class', `typename', or `template'");
a723baf1
MM
7657 if (!token)
7658 return error_mark_node;
7659
7660 switch (token->keyword)
7661 {
7662 case RID_CLASS:
7663 case RID_TYPENAME:
7664 {
7665 tree identifier;
7666 tree default_argument;
7667
7668 /* If the next token is an identifier, then it names the
7669 parameter. */
7670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7671 identifier = cp_parser_identifier (parser);
7672 else
7673 identifier = NULL_TREE;
7674
7675 /* Create the parameter. */
7676 parameter = finish_template_type_parm (class_type_node, identifier);
7677
7678 /* If the next token is an `=', we have a default argument. */
7679 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7680 {
7681 /* Consume the `=' token. */
7682 cp_lexer_consume_token (parser->lexer);
34cd5ae7 7683 /* Parse the default-argument. */
a723baf1
MM
7684 default_argument = cp_parser_type_id (parser);
7685 }
7686 else
7687 default_argument = NULL_TREE;
7688
7689 /* Create the combined representation of the parameter and the
7690 default argument. */
c67d36d0 7691 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7692 }
7693 break;
7694
7695 case RID_TEMPLATE:
7696 {
7697 tree parameter_list;
7698 tree identifier;
7699 tree default_argument;
7700
7701 /* Look for the `<'. */
7702 cp_parser_require (parser, CPP_LESS, "`<'");
7703 /* Parse the template-parameter-list. */
7704 begin_template_parm_list ();
7705 parameter_list
7706 = cp_parser_template_parameter_list (parser);
7707 parameter_list = end_template_parm_list (parameter_list);
7708 /* Look for the `>'. */
7709 cp_parser_require (parser, CPP_GREATER, "`>'");
7710 /* Look for the `class' keyword. */
7711 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7712 /* If the next token is an `=', then there is a
7713 default-argument. If the next token is a `>', we are at
7714 the end of the parameter-list. If the next token is a `,',
7715 then we are at the end of this parameter. */
7716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7717 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7718 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7719 identifier = cp_parser_identifier (parser);
7720 else
7721 identifier = NULL_TREE;
7722 /* Create the template parameter. */
7723 parameter = finish_template_template_parm (class_type_node,
7724 identifier);
7725
7726 /* If the next token is an `=', then there is a
7727 default-argument. */
7728 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7729 {
b0bc6e8e
KL
7730 bool is_template;
7731
a723baf1
MM
7732 /* Consume the `='. */
7733 cp_lexer_consume_token (parser->lexer);
7734 /* Parse the id-expression. */
7735 default_argument
7736 = cp_parser_id_expression (parser,
7737 /*template_keyword_p=*/false,
7738 /*check_dependency_p=*/true,
b0bc6e8e 7739 /*template_p=*/&is_template,
f3c2dfc6 7740 /*declarator_p=*/false);
a3a503a5
GB
7741 if (TREE_CODE (default_argument) == TYPE_DECL)
7742 /* If the id-expression was a template-id that refers to
7743 a template-class, we already have the declaration here,
7744 so no further lookup is needed. */
7745 ;
7746 else
7747 /* Look up the name. */
7748 default_argument
7749 = cp_parser_lookup_name (parser, default_argument,
7750 /*is_type=*/false,
7751 /*is_template=*/is_template,
7752 /*is_namespace=*/false,
7753 /*check_dependency=*/true);
a723baf1
MM
7754 /* See if the default argument is valid. */
7755 default_argument
7756 = check_template_template_default_arg (default_argument);
7757 }
7758 else
7759 default_argument = NULL_TREE;
7760
7761 /* Create the combined representation of the parameter and the
7762 default argument. */
c67d36d0 7763 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
7764 }
7765 break;
7766
7767 default:
7768 /* Anything else is an error. */
7769 cp_parser_error (parser,
7770 "expected `class', `typename', or `template'");
7771 parameter = error_mark_node;
7772 }
7773
7774 return parameter;
7775}
7776
7777/* Parse a template-id.
7778
7779 template-id:
7780 template-name < template-argument-list [opt] >
7781
7782 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7783 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7784 returned. Otherwise, if the template-name names a function, or set
7785 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7786 names a class, returns a TYPE_DECL for the specialization.
7787
7788 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7789 uninstantiated templates. */
7790
7791static tree
7792cp_parser_template_id (cp_parser *parser,
7793 bool template_keyword_p,
a668c6ad
MM
7794 bool check_dependency_p,
7795 bool is_declaration)
a723baf1
MM
7796{
7797 tree template;
7798 tree arguments;
a723baf1 7799 tree template_id;
a723baf1
MM
7800 ptrdiff_t start_of_id;
7801 tree access_check = NULL_TREE;
f4abade9 7802 cp_token *next_token, *next_token_2;
a668c6ad 7803 bool is_identifier;
a723baf1
MM
7804
7805 /* If the next token corresponds to a template-id, there is no need
7806 to reparse it. */
2050a1bb
MM
7807 next_token = cp_lexer_peek_token (parser->lexer);
7808 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7809 {
7810 tree value;
7811 tree check;
7812
7813 /* Get the stored value. */
7814 value = cp_lexer_consume_token (parser->lexer)->value;
7815 /* Perform any access checks that were deferred. */
7816 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7817 perform_or_defer_access_check (TREE_PURPOSE (check),
7818 TREE_VALUE (check));
a723baf1
MM
7819 /* Return the stored value. */
7820 return TREE_VALUE (value);
7821 }
7822
2050a1bb
MM
7823 /* Avoid performing name lookup if there is no possibility of
7824 finding a template-id. */
7825 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7826 || (next_token->type == CPP_NAME
f4abade9
GB
7827 && !cp_parser_nth_token_starts_template_argument_list_p
7828 (parser, 2)))
2050a1bb
MM
7829 {
7830 cp_parser_error (parser, "expected template-id");
7831 return error_mark_node;
7832 }
7833
a723baf1
MM
7834 /* Remember where the template-id starts. */
7835 if (cp_parser_parsing_tentatively (parser)
7836 && !cp_parser_committed_to_tentative_parse (parser))
7837 {
2050a1bb 7838 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7839 start_of_id = cp_lexer_token_difference (parser->lexer,
7840 parser->lexer->first_token,
7841 next_token);
a723baf1
MM
7842 }
7843 else
7844 start_of_id = -1;
7845
8d241e0b 7846 push_deferring_access_checks (dk_deferred);
cf22909c 7847
a723baf1 7848 /* Parse the template-name. */
a668c6ad 7849 is_identifier = false;
a723baf1 7850 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
7851 check_dependency_p,
7852 is_declaration,
7853 &is_identifier);
7854 if (template == error_mark_node || is_identifier)
cf22909c
KL
7855 {
7856 pop_deferring_access_checks ();
a668c6ad 7857 return template;
cf22909c 7858 }
a723baf1 7859
f4abade9
GB
7860 /* If we find the sequence `[:' after a template-name, it's probably
7861 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7862 parse correctly the argument list. */
7863 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7864 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7865 if (next_token->type == CPP_OPEN_SQUARE
7866 && next_token->flags & DIGRAPH
7867 && next_token_2->type == CPP_COLON
7868 && !(next_token_2->flags & PREV_WHITE))
cf22909c 7869 {
f4abade9
GB
7870 cp_parser_parse_tentatively (parser);
7871 /* Change `:' into `::'. */
7872 next_token_2->type = CPP_SCOPE;
7873 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7874 CPP_LESS. */
7875 cp_lexer_consume_token (parser->lexer);
7876 /* Parse the arguments. */
7877 arguments = cp_parser_enclosed_template_argument_list (parser);
7878 if (!cp_parser_parse_definitely (parser))
7879 {
7880 /* If we couldn't parse an argument list, then we revert our changes
7881 and return simply an error. Maybe this is not a template-id
7882 after all. */
7883 next_token_2->type = CPP_COLON;
7884 cp_parser_error (parser, "expected `<'");
7885 pop_deferring_access_checks ();
7886 return error_mark_node;
7887 }
7888 /* Otherwise, emit an error about the invalid digraph, but continue
7889 parsing because we got our argument list. */
7890 pedwarn ("`<::' cannot begin a template-argument list");
7891 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7892 "between `<' and `::'");
7893 if (!flag_permissive)
7894 {
7895 static bool hint;
7896 if (!hint)
7897 {
7898 inform ("(if you use `-fpermissive' G++ will accept your code)");
7899 hint = true;
7900 }
7901 }
7902 }
7903 else
7904 {
7905 /* Look for the `<' that starts the template-argument-list. */
7906 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7907 {
7908 pop_deferring_access_checks ();
7909 return error_mark_node;
7910 }
7911 /* Parse the arguments. */
7912 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 7913 }
a723baf1
MM
7914
7915 /* Build a representation of the specialization. */
7916 if (TREE_CODE (template) == IDENTIFIER_NODE)
7917 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7918 else if (DECL_CLASS_TEMPLATE_P (template)
7919 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7920 template_id
7921 = finish_template_type (template, arguments,
7922 cp_lexer_next_token_is (parser->lexer,
7923 CPP_SCOPE));
7924 else
7925 {
7926 /* If it's not a class-template or a template-template, it should be
7927 a function-template. */
7928 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7929 || TREE_CODE (template) == OVERLOAD
7930 || BASELINK_P (template)),
7931 20010716);
7932
7933 template_id = lookup_template_function (template, arguments);
7934 }
7935
cf22909c
KL
7936 /* Retrieve any deferred checks. Do not pop this access checks yet
7937 so the memory will not be reclaimed during token replacing below. */
7938 access_check = get_deferred_access_checks ();
7939
a723baf1
MM
7940 /* If parsing tentatively, replace the sequence of tokens that makes
7941 up the template-id with a CPP_TEMPLATE_ID token. That way,
7942 should we re-parse the token stream, we will not have to repeat
7943 the effort required to do the parse, nor will we issue duplicate
7944 error messages about problems during instantiation of the
7945 template. */
7946 if (start_of_id >= 0)
7947 {
7948 cp_token *token;
a723baf1
MM
7949
7950 /* Find the token that corresponds to the start of the
7951 template-id. */
7952 token = cp_lexer_advance_token (parser->lexer,
7953 parser->lexer->first_token,
7954 start_of_id);
7955
a723baf1
MM
7956 /* Reset the contents of the START_OF_ID token. */
7957 token->type = CPP_TEMPLATE_ID;
7958 token->value = build_tree_list (access_check, template_id);
7959 token->keyword = RID_MAX;
7960 /* Purge all subsequent tokens. */
7961 cp_lexer_purge_tokens_after (parser->lexer, token);
7962 }
7963
cf22909c 7964 pop_deferring_access_checks ();
a723baf1
MM
7965 return template_id;
7966}
7967
7968/* Parse a template-name.
7969
7970 template-name:
7971 identifier
7972
7973 The standard should actually say:
7974
7975 template-name:
7976 identifier
7977 operator-function-id
a723baf1
MM
7978
7979 A defect report has been filed about this issue.
7980
0d956474
GB
7981 A conversion-function-id cannot be a template name because they cannot
7982 be part of a template-id. In fact, looking at this code:
7983
7984 a.operator K<int>()
7985
7986 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
7987 It is impossible to call a templated conversion-function-id with an
7988 explicit argument list, since the only allowed template parameter is
7989 the type to which it is converting.
7990
a723baf1
MM
7991 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7992 `template' keyword, in a construction like:
7993
7994 T::template f<3>()
7995
7996 In that case `f' is taken to be a template-name, even though there
7997 is no way of knowing for sure.
7998
7999 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8000 name refers to a set of overloaded functions, at least one of which
8001 is a template, or an IDENTIFIER_NODE with the name of the template,
8002 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8003 names are looked up inside uninstantiated templates. */
8004
8005static tree
94edc4ab
NN
8006cp_parser_template_name (cp_parser* parser,
8007 bool template_keyword_p,
a668c6ad
MM
8008 bool check_dependency_p,
8009 bool is_declaration,
8010 bool *is_identifier)
a723baf1
MM
8011{
8012 tree identifier;
8013 tree decl;
8014 tree fns;
8015
8016 /* If the next token is `operator', then we have either an
8017 operator-function-id or a conversion-function-id. */
8018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8019 {
8020 /* We don't know whether we're looking at an
8021 operator-function-id or a conversion-function-id. */
8022 cp_parser_parse_tentatively (parser);
8023 /* Try an operator-function-id. */
8024 identifier = cp_parser_operator_function_id (parser);
8025 /* If that didn't work, try a conversion-function-id. */
8026 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8027 {
8028 cp_parser_error (parser, "expected template-name");
8029 return error_mark_node;
8030 }
a723baf1
MM
8031 }
8032 /* Look for the identifier. */
8033 else
8034 identifier = cp_parser_identifier (parser);
8035
8036 /* If we didn't find an identifier, we don't have a template-id. */
8037 if (identifier == error_mark_node)
8038 return error_mark_node;
8039
8040 /* If the name immediately followed the `template' keyword, then it
8041 is a template-name. However, if the next token is not `<', then
8042 we do not treat it as a template-name, since it is not being used
8043 as part of a template-id. This enables us to handle constructs
8044 like:
8045
8046 template <typename T> struct S { S(); };
8047 template <typename T> S<T>::S();
8048
8049 correctly. We would treat `S' as a template -- if it were `S<T>'
8050 -- but we do not if there is no `<'. */
a668c6ad
MM
8051
8052 if (processing_template_decl
f4abade9 8053 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8054 {
8055 /* In a declaration, in a dependent context, we pretend that the
8056 "template" keyword was present in order to improve error
8057 recovery. For example, given:
8058
8059 template <typename T> void f(T::X<int>);
8060
8061 we want to treat "X<int>" as a template-id. */
8062 if (is_declaration
8063 && !template_keyword_p
8064 && parser->scope && TYPE_P (parser->scope)
8065 && dependent_type_p (parser->scope))
8066 {
8067 ptrdiff_t start;
8068 cp_token* token;
8069 /* Explain what went wrong. */
8070 error ("non-template `%D' used as template", identifier);
8071 error ("(use `%T::template %D' to indicate that it is a template)",
8072 parser->scope, identifier);
8073 /* If parsing tentatively, find the location of the "<"
8074 token. */
8075 if (cp_parser_parsing_tentatively (parser)
8076 && !cp_parser_committed_to_tentative_parse (parser))
8077 {
8078 cp_parser_simulate_error (parser);
8079 token = cp_lexer_peek_token (parser->lexer);
8080 token = cp_lexer_prev_token (parser->lexer, token);
8081 start = cp_lexer_token_difference (parser->lexer,
8082 parser->lexer->first_token,
8083 token);
8084 }
8085 else
8086 start = -1;
8087 /* Parse the template arguments so that we can issue error
8088 messages about them. */
8089 cp_lexer_consume_token (parser->lexer);
8090 cp_parser_enclosed_template_argument_list (parser);
8091 /* Skip tokens until we find a good place from which to
8092 continue parsing. */
8093 cp_parser_skip_to_closing_parenthesis (parser,
8094 /*recovering=*/true,
8095 /*or_comma=*/true,
8096 /*consume_paren=*/false);
8097 /* If parsing tentatively, permanently remove the
8098 template argument list. That will prevent duplicate
8099 error messages from being issued about the missing
8100 "template" keyword. */
8101 if (start >= 0)
8102 {
8103 token = cp_lexer_advance_token (parser->lexer,
8104 parser->lexer->first_token,
8105 start);
8106 cp_lexer_purge_tokens_after (parser->lexer, token);
8107 }
8108 if (is_identifier)
8109 *is_identifier = true;
8110 return identifier;
8111 }
8112 if (template_keyword_p)
8113 return identifier;
8114 }
a723baf1
MM
8115
8116 /* Look up the name. */
8117 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8118 /*is_type=*/false,
b0bc6e8e 8119 /*is_template=*/false,
eea9800f 8120 /*is_namespace=*/false,
a723baf1
MM
8121 check_dependency_p);
8122 decl = maybe_get_template_decl_from_type_decl (decl);
8123
8124 /* If DECL is a template, then the name was a template-name. */
8125 if (TREE_CODE (decl) == TEMPLATE_DECL)
8126 ;
8127 else
8128 {
8129 /* The standard does not explicitly indicate whether a name that
8130 names a set of overloaded declarations, some of which are
8131 templates, is a template-name. However, such a name should
8132 be a template-name; otherwise, there is no way to form a
8133 template-id for the overloaded templates. */
8134 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8135 if (TREE_CODE (fns) == OVERLOAD)
8136 {
8137 tree fn;
8138
8139 for (fn = fns; fn; fn = OVL_NEXT (fn))
8140 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8141 break;
8142 }
8143 else
8144 {
8145 /* Otherwise, the name does not name a template. */
8146 cp_parser_error (parser, "expected template-name");
8147 return error_mark_node;
8148 }
8149 }
8150
8151 /* If DECL is dependent, and refers to a function, then just return
8152 its name; we will look it up again during template instantiation. */
8153 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8154 {
8155 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8156 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8157 return identifier;
8158 }
8159
8160 return decl;
8161}
8162
8163/* Parse a template-argument-list.
8164
8165 template-argument-list:
8166 template-argument
8167 template-argument-list , template-argument
8168
04c06002 8169 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8170
8171static tree
94edc4ab 8172cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8173{
bf12d54d
NS
8174 tree fixed_args[10];
8175 unsigned n_args = 0;
8176 unsigned alloced = 10;
8177 tree *arg_ary = fixed_args;
8178 tree vec;
4bb8ca28 8179 bool saved_in_template_argument_list_p;
a723baf1 8180
4bb8ca28
MM
8181 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8182 parser->in_template_argument_list_p = true;
bf12d54d 8183 do
a723baf1
MM
8184 {
8185 tree argument;
8186
bf12d54d 8187 if (n_args)
04c06002 8188 /* Consume the comma. */
bf12d54d
NS
8189 cp_lexer_consume_token (parser->lexer);
8190
a723baf1
MM
8191 /* Parse the template-argument. */
8192 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8193 if (n_args == alloced)
8194 {
8195 alloced *= 2;
8196
8197 if (arg_ary == fixed_args)
8198 {
8199 arg_ary = xmalloc (sizeof (tree) * alloced);
8200 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8201 }
8202 else
8203 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8204 }
8205 arg_ary[n_args++] = argument;
a723baf1 8206 }
bf12d54d
NS
8207 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8208
8209 vec = make_tree_vec (n_args);
a723baf1 8210
bf12d54d
NS
8211 while (n_args--)
8212 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8213
8214 if (arg_ary != fixed_args)
8215 free (arg_ary);
4bb8ca28 8216 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8217 return vec;
a723baf1
MM
8218}
8219
8220/* Parse a template-argument.
8221
8222 template-argument:
8223 assignment-expression
8224 type-id
8225 id-expression
8226
8227 The representation is that of an assignment-expression, type-id, or
8228 id-expression -- except that the qualified id-expression is
8229 evaluated, so that the value returned is either a DECL or an
d17811fd
MM
8230 OVERLOAD.
8231
8232 Although the standard says "assignment-expression", it forbids
8233 throw-expressions or assignments in the template argument.
8234 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8235
8236static tree
94edc4ab 8237cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8238{
8239 tree argument;
8240 bool template_p;
d17811fd 8241 bool address_p;
4d5297fa 8242 bool maybe_type_id = false;
d17811fd 8243 cp_token *token;
b3445994 8244 cp_id_kind idk;
d17811fd 8245 tree qualifying_class;
a723baf1
MM
8246
8247 /* There's really no way to know what we're looking at, so we just
8248 try each alternative in order.
8249
8250 [temp.arg]
8251
8252 In a template-argument, an ambiguity between a type-id and an
8253 expression is resolved to a type-id, regardless of the form of
8254 the corresponding template-parameter.
8255
8256 Therefore, we try a type-id first. */
8257 cp_parser_parse_tentatively (parser);
a723baf1 8258 argument = cp_parser_type_id (parser);
4d5297fa
GB
8259 /* If there was no error parsing the type-id but the next token is a '>>',
8260 we probably found a typo for '> >'. But there are type-id which are
8261 also valid expressions. For instance:
8262
8263 struct X { int operator >> (int); };
8264 template <int V> struct Foo {};
8265 Foo<X () >> 5> r;
8266
8267 Here 'X()' is a valid type-id of a function type, but the user just
8268 wanted to write the expression "X() >> 5". Thus, we remember that we
8269 found a valid type-id, but we still try to parse the argument as an
8270 expression to see what happens. */
8271 if (!cp_parser_error_occurred (parser)
8272 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8273 {
8274 maybe_type_id = true;
8275 cp_parser_abort_tentative_parse (parser);
8276 }
8277 else
8278 {
8279 /* If the next token isn't a `,' or a `>', then this argument wasn't
8280 really finished. This means that the argument is not a valid
8281 type-id. */
8282 if (!cp_parser_next_token_ends_template_argument_p (parser))
8283 cp_parser_error (parser, "expected template-argument");
8284 /* If that worked, we're done. */
8285 if (cp_parser_parse_definitely (parser))
8286 return argument;
8287 }
a723baf1
MM
8288 /* We're still not sure what the argument will be. */
8289 cp_parser_parse_tentatively (parser);
8290 /* Try a template. */
8291 argument = cp_parser_id_expression (parser,
8292 /*template_keyword_p=*/false,
8293 /*check_dependency_p=*/true,
f3c2dfc6
MM
8294 &template_p,
8295 /*declarator_p=*/false);
a723baf1
MM
8296 /* If the next token isn't a `,' or a `>', then this argument wasn't
8297 really finished. */
d17811fd 8298 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8299 cp_parser_error (parser, "expected template-argument");
8300 if (!cp_parser_error_occurred (parser))
8301 {
8302 /* Figure out what is being referred to. */
5b4acce1
KL
8303 argument = cp_parser_lookup_name (parser, argument,
8304 /*is_type=*/false,
8305 /*is_template=*/template_p,
8306 /*is_namespace=*/false,
8307 /*check_dependency=*/true);
8308 if (TREE_CODE (argument) != TEMPLATE_DECL
8309 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8310 cp_parser_error (parser, "expected template-name");
8311 }
8312 if (cp_parser_parse_definitely (parser))
8313 return argument;
d17811fd
MM
8314 /* It must be a non-type argument. There permitted cases are given
8315 in [temp.arg.nontype]:
8316
8317 -- an integral constant-expression of integral or enumeration
8318 type; or
8319
8320 -- the name of a non-type template-parameter; or
8321
8322 -- the name of an object or function with external linkage...
8323
8324 -- the address of an object or function with external linkage...
8325
04c06002 8326 -- a pointer to member... */
d17811fd
MM
8327 /* Look for a non-type template parameter. */
8328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8329 {
8330 cp_parser_parse_tentatively (parser);
8331 argument = cp_parser_primary_expression (parser,
8332 &idk,
8333 &qualifying_class);
8334 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8335 || !cp_parser_next_token_ends_template_argument_p (parser))
8336 cp_parser_simulate_error (parser);
8337 if (cp_parser_parse_definitely (parser))
8338 return argument;
8339 }
8340 /* If the next token is "&", the argument must be the address of an
8341 object or function with external linkage. */
8342 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8343 if (address_p)
8344 cp_lexer_consume_token (parser->lexer);
8345 /* See if we might have an id-expression. */
8346 token = cp_lexer_peek_token (parser->lexer);
8347 if (token->type == CPP_NAME
8348 || token->keyword == RID_OPERATOR
8349 || token->type == CPP_SCOPE
8350 || token->type == CPP_TEMPLATE_ID
8351 || token->type == CPP_NESTED_NAME_SPECIFIER)
8352 {
8353 cp_parser_parse_tentatively (parser);
8354 argument = cp_parser_primary_expression (parser,
8355 &idk,
8356 &qualifying_class);
8357 if (cp_parser_error_occurred (parser)
8358 || !cp_parser_next_token_ends_template_argument_p (parser))
8359 cp_parser_abort_tentative_parse (parser);
8360 else
8361 {
8362 if (qualifying_class)
8363 argument = finish_qualified_id_expr (qualifying_class,
8364 argument,
8365 /*done=*/true,
8366 address_p);
8367 if (TREE_CODE (argument) == VAR_DECL)
8368 {
8369 /* A variable without external linkage might still be a
8370 valid constant-expression, so no error is issued here
8371 if the external-linkage check fails. */
8372 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8373 cp_parser_simulate_error (parser);
8374 }
8375 else if (is_overloaded_fn (argument))
8376 /* All overloaded functions are allowed; if the external
8377 linkage test does not pass, an error will be issued
8378 later. */
8379 ;
8380 else if (address_p
8381 && (TREE_CODE (argument) == OFFSET_REF
8382 || TREE_CODE (argument) == SCOPE_REF))
8383 /* A pointer-to-member. */
8384 ;
8385 else
8386 cp_parser_simulate_error (parser);
8387
8388 if (cp_parser_parse_definitely (parser))
8389 {
8390 if (address_p)
8391 argument = build_x_unary_op (ADDR_EXPR, argument);
8392 return argument;
8393 }
8394 }
8395 }
8396 /* If the argument started with "&", there are no other valid
8397 alternatives at this point. */
8398 if (address_p)
8399 {
8400 cp_parser_error (parser, "invalid non-type template argument");
8401 return error_mark_node;
8402 }
4d5297fa
GB
8403 /* If the argument wasn't successfully parsed as a type-id followed
8404 by '>>', the argument can only be a constant expression now.
8405 Otherwise, we try parsing the constant-expression tentatively,
8406 because the argument could really be a type-id. */
8407 if (maybe_type_id)
8408 cp_parser_parse_tentatively (parser);
d17811fd
MM
8409 argument = cp_parser_constant_expression (parser,
8410 /*allow_non_constant_p=*/false,
8411 /*non_constant_p=*/NULL);
9baa27a9 8412 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
8413 if (!maybe_type_id)
8414 return argument;
8415 if (!cp_parser_next_token_ends_template_argument_p (parser))
8416 cp_parser_error (parser, "expected template-argument");
8417 if (cp_parser_parse_definitely (parser))
8418 return argument;
8419 /* We did our best to parse the argument as a non type-id, but that
8420 was the only alternative that matched (albeit with a '>' after
8421 it). We can assume it's just a typo from the user, and a
8422 diagnostic will then be issued. */
8423 return cp_parser_type_id (parser);
a723baf1
MM
8424}
8425
8426/* Parse an explicit-instantiation.
8427
8428 explicit-instantiation:
8429 template declaration
8430
8431 Although the standard says `declaration', what it really means is:
8432
8433 explicit-instantiation:
8434 template decl-specifier-seq [opt] declarator [opt] ;
8435
8436 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8437 supposed to be allowed. A defect report has been filed about this
8438 issue.
8439
8440 GNU Extension:
8441
8442 explicit-instantiation:
8443 storage-class-specifier template
8444 decl-specifier-seq [opt] declarator [opt] ;
8445 function-specifier template
8446 decl-specifier-seq [opt] declarator [opt] ; */
8447
8448static void
94edc4ab 8449cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8450{
560ad596 8451 int declares_class_or_enum;
a723baf1
MM
8452 tree decl_specifiers;
8453 tree attributes;
8454 tree extension_specifier = NULL_TREE;
8455
8456 /* Look for an (optional) storage-class-specifier or
8457 function-specifier. */
8458 if (cp_parser_allow_gnu_extensions_p (parser))
8459 {
8460 extension_specifier
8461 = cp_parser_storage_class_specifier_opt (parser);
8462 if (!extension_specifier)
8463 extension_specifier = cp_parser_function_specifier_opt (parser);
8464 }
8465
8466 /* Look for the `template' keyword. */
8467 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8468 /* Let the front end know that we are processing an explicit
8469 instantiation. */
8470 begin_explicit_instantiation ();
8471 /* [temp.explicit] says that we are supposed to ignore access
8472 control while processing explicit instantiation directives. */
78757caa 8473 push_deferring_access_checks (dk_no_check);
a723baf1
MM
8474 /* Parse a decl-specifier-seq. */
8475 decl_specifiers
8476 = cp_parser_decl_specifier_seq (parser,
8477 CP_PARSER_FLAGS_OPTIONAL,
8478 &attributes,
8479 &declares_class_or_enum);
8480 /* If there was exactly one decl-specifier, and it declared a class,
8481 and there's no declarator, then we have an explicit type
8482 instantiation. */
8483 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8484 {
8485 tree type;
8486
8487 type = check_tag_decl (decl_specifiers);
b7fc8b57
KL
8488 /* Turn access control back on for names used during
8489 template instantiation. */
8490 pop_deferring_access_checks ();
a723baf1
MM
8491 if (type)
8492 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8493 }
8494 else
8495 {
8496 tree declarator;
8497 tree decl;
8498
8499 /* Parse the declarator. */
8500 declarator
62b8a44e 8501 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
8502 /*ctor_dtor_or_conv_p=*/NULL,
8503 /*parenthesized_p=*/NULL);
560ad596
MM
8504 cp_parser_check_for_definition_in_return_type (declarator,
8505 declares_class_or_enum);
216bb6e1
MM
8506 if (declarator != error_mark_node)
8507 {
8508 decl = grokdeclarator (declarator, decl_specifiers,
8509 NORMAL, 0, NULL);
8510 /* Turn access control back on for names used during
8511 template instantiation. */
8512 pop_deferring_access_checks ();
8513 /* Do the explicit instantiation. */
8514 do_decl_instantiation (decl, extension_specifier);
8515 }
8516 else
8517 {
8518 pop_deferring_access_checks ();
8519 /* Skip the body of the explicit instantiation. */
8520 cp_parser_skip_to_end_of_statement (parser);
8521 }
a723baf1
MM
8522 }
8523 /* We're done with the instantiation. */
8524 end_explicit_instantiation ();
a723baf1 8525
e0860732 8526 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8527}
8528
8529/* Parse an explicit-specialization.
8530
8531 explicit-specialization:
8532 template < > declaration
8533
8534 Although the standard says `declaration', what it really means is:
8535
8536 explicit-specialization:
8537 template <> decl-specifier [opt] init-declarator [opt] ;
8538 template <> function-definition
8539 template <> explicit-specialization
8540 template <> template-declaration */
8541
8542static void
94edc4ab 8543cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8544{
8545 /* Look for the `template' keyword. */
8546 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8547 /* Look for the `<'. */
8548 cp_parser_require (parser, CPP_LESS, "`<'");
8549 /* Look for the `>'. */
8550 cp_parser_require (parser, CPP_GREATER, "`>'");
8551 /* We have processed another parameter list. */
8552 ++parser->num_template_parameter_lists;
8553 /* Let the front end know that we are beginning a specialization. */
8554 begin_specialization ();
8555
8556 /* If the next keyword is `template', we need to figure out whether
8557 or not we're looking a template-declaration. */
8558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8559 {
8560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8562 cp_parser_template_declaration_after_export (parser,
8563 /*member_p=*/false);
8564 else
8565 cp_parser_explicit_specialization (parser);
8566 }
8567 else
8568 /* Parse the dependent declaration. */
8569 cp_parser_single_declaration (parser,
8570 /*member_p=*/false,
8571 /*friend_p=*/NULL);
8572
8573 /* We're done with the specialization. */
8574 end_specialization ();
8575 /* We're done with this parameter list. */
8576 --parser->num_template_parameter_lists;
8577}
8578
8579/* Parse a type-specifier.
8580
8581 type-specifier:
8582 simple-type-specifier
8583 class-specifier
8584 enum-specifier
8585 elaborated-type-specifier
8586 cv-qualifier
8587
8588 GNU Extension:
8589
8590 type-specifier:
8591 __complex__
8592
8593 Returns a representation of the type-specifier. If the
8594 type-specifier is a keyword (like `int' or `const', or
34cd5ae7 8595 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
a723baf1
MM
8596 For a class-specifier, enum-specifier, or elaborated-type-specifier
8597 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8598
8599 If IS_FRIEND is TRUE then this type-specifier is being declared a
8600 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8601 appearing in a decl-specifier-seq.
8602
8603 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8604 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 8605 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
8606 if a type is declared; 2 if it is defined. Otherwise, it is set to
8607 zero.
a723baf1
MM
8608
8609 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8610 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8611 is set to FALSE. */
8612
8613static tree
94edc4ab
NN
8614cp_parser_type_specifier (cp_parser* parser,
8615 cp_parser_flags flags,
8616 bool is_friend,
8617 bool is_declaration,
560ad596 8618 int* declares_class_or_enum,
94edc4ab 8619 bool* is_cv_qualifier)
a723baf1
MM
8620{
8621 tree type_spec = NULL_TREE;
8622 cp_token *token;
8623 enum rid keyword;
8624
8625 /* Assume this type-specifier does not declare a new type. */
8626 if (declares_class_or_enum)
543ca912 8627 *declares_class_or_enum = 0;
a723baf1
MM
8628 /* And that it does not specify a cv-qualifier. */
8629 if (is_cv_qualifier)
8630 *is_cv_qualifier = false;
8631 /* Peek at the next token. */
8632 token = cp_lexer_peek_token (parser->lexer);
8633
8634 /* If we're looking at a keyword, we can use that to guide the
8635 production we choose. */
8636 keyword = token->keyword;
8637 switch (keyword)
8638 {
8639 /* Any of these indicate either a class-specifier, or an
8640 elaborated-type-specifier. */
8641 case RID_CLASS:
8642 case RID_STRUCT:
8643 case RID_UNION:
8644 case RID_ENUM:
8645 /* Parse tentatively so that we can back up if we don't find a
8646 class-specifier or enum-specifier. */
8647 cp_parser_parse_tentatively (parser);
8648 /* Look for the class-specifier or enum-specifier. */
8649 if (keyword == RID_ENUM)
8650 type_spec = cp_parser_enum_specifier (parser);
8651 else
8652 type_spec = cp_parser_class_specifier (parser);
8653
8654 /* If that worked, we're done. */
8655 if (cp_parser_parse_definitely (parser))
8656 {
8657 if (declares_class_or_enum)
560ad596 8658 *declares_class_or_enum = 2;
a723baf1
MM
8659 return type_spec;
8660 }
8661
8662 /* Fall through. */
8663
8664 case RID_TYPENAME:
8665 /* Look for an elaborated-type-specifier. */
8666 type_spec = cp_parser_elaborated_type_specifier (parser,
8667 is_friend,
8668 is_declaration);
8669 /* We're declaring a class or enum -- unless we're using
8670 `typename'. */
8671 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 8672 *declares_class_or_enum = 1;
a723baf1
MM
8673 return type_spec;
8674
8675 case RID_CONST:
8676 case RID_VOLATILE:
8677 case RID_RESTRICT:
8678 type_spec = cp_parser_cv_qualifier_opt (parser);
8679 /* Even though we call a routine that looks for an optional
8680 qualifier, we know that there should be one. */
8681 my_friendly_assert (type_spec != NULL, 20000328);
8682 /* This type-specifier was a cv-qualified. */
8683 if (is_cv_qualifier)
8684 *is_cv_qualifier = true;
8685
8686 return type_spec;
8687
8688 case RID_COMPLEX:
8689 /* The `__complex__' keyword is a GNU extension. */
8690 return cp_lexer_consume_token (parser->lexer)->value;
8691
8692 default:
8693 break;
8694 }
8695
8696 /* If we do not already have a type-specifier, assume we are looking
8697 at a simple-type-specifier. */
4b0d3cbe
MM
8698 type_spec = cp_parser_simple_type_specifier (parser, flags,
8699 /*identifier_p=*/true);
a723baf1
MM
8700
8701 /* If we didn't find a type-specifier, and a type-specifier was not
8702 optional in this context, issue an error message. */
8703 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8704 {
8705 cp_parser_error (parser, "expected type specifier");
8706 return error_mark_node;
8707 }
8708
8709 return type_spec;
8710}
8711
8712/* Parse a simple-type-specifier.
8713
8714 simple-type-specifier:
8715 :: [opt] nested-name-specifier [opt] type-name
8716 :: [opt] nested-name-specifier template template-id
8717 char
8718 wchar_t
8719 bool
8720 short
8721 int
8722 long
8723 signed
8724 unsigned
8725 float
8726 double
8727 void
8728
8729 GNU Extension:
8730
8731 simple-type-specifier:
8732 __typeof__ unary-expression
8733 __typeof__ ( type-id )
8734
8735 For the various keywords, the value returned is simply the
4b0d3cbe
MM
8736 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8737 For the first two productions, and if IDENTIFIER_P is false, the
8738 value returned is the indicated TYPE_DECL. */
a723baf1
MM
8739
8740static tree
4b0d3cbe
MM
8741cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8742 bool identifier_p)
a723baf1
MM
8743{
8744 tree type = NULL_TREE;
8745 cp_token *token;
8746
8747 /* Peek at the next token. */
8748 token = cp_lexer_peek_token (parser->lexer);
8749
8750 /* If we're looking at a keyword, things are easy. */
8751 switch (token->keyword)
8752 {
8753 case RID_CHAR:
4b0d3cbe
MM
8754 type = char_type_node;
8755 break;
a723baf1 8756 case RID_WCHAR:
4b0d3cbe
MM
8757 type = wchar_type_node;
8758 break;
a723baf1 8759 case RID_BOOL:
4b0d3cbe
MM
8760 type = boolean_type_node;
8761 break;
a723baf1 8762 case RID_SHORT:
4b0d3cbe
MM
8763 type = short_integer_type_node;
8764 break;
a723baf1 8765 case RID_INT:
4b0d3cbe
MM
8766 type = integer_type_node;
8767 break;
a723baf1 8768 case RID_LONG:
4b0d3cbe
MM
8769 type = long_integer_type_node;
8770 break;
a723baf1 8771 case RID_SIGNED:
4b0d3cbe
MM
8772 type = integer_type_node;
8773 break;
a723baf1 8774 case RID_UNSIGNED:
4b0d3cbe
MM
8775 type = unsigned_type_node;
8776 break;
a723baf1 8777 case RID_FLOAT:
4b0d3cbe
MM
8778 type = float_type_node;
8779 break;
a723baf1 8780 case RID_DOUBLE:
4b0d3cbe
MM
8781 type = double_type_node;
8782 break;
a723baf1 8783 case RID_VOID:
4b0d3cbe
MM
8784 type = void_type_node;
8785 break;
a723baf1
MM
8786
8787 case RID_TYPEOF:
8788 {
8789 tree operand;
8790
8791 /* Consume the `typeof' token. */
8792 cp_lexer_consume_token (parser->lexer);
04c06002 8793 /* Parse the operand to `typeof'. */
a723baf1
MM
8794 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8795 /* If it is not already a TYPE, take its type. */
8796 if (!TYPE_P (operand))
8797 operand = finish_typeof (operand);
8798
8799 return operand;
8800 }
8801
8802 default:
8803 break;
8804 }
8805
4b0d3cbe
MM
8806 /* If the type-specifier was for a built-in type, we're done. */
8807 if (type)
8808 {
8809 tree id;
8810
8811 /* Consume the token. */
8812 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
8813
8814 /* There is no valid C++ program where a non-template type is
8815 followed by a "<". That usually indicates that the user thought
8816 that the type was a template. */
8817 cp_parser_check_for_invalid_template_id (parser, type);
8818
4b0d3cbe
MM
8819 return identifier_p ? id : TYPE_NAME (type);
8820 }
8821
a723baf1
MM
8822 /* The type-specifier must be a user-defined type. */
8823 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8824 {
8825 /* Don't gobble tokens or issue error messages if this is an
8826 optional type-specifier. */
8827 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8828 cp_parser_parse_tentatively (parser);
8829
8830 /* Look for the optional `::' operator. */
8831 cp_parser_global_scope_opt (parser,
8832 /*current_scope_valid_p=*/false);
8833 /* Look for the nested-name specifier. */
8834 cp_parser_nested_name_specifier_opt (parser,
8835 /*typename_keyword_p=*/false,
8836 /*check_dependency_p=*/true,
a668c6ad
MM
8837 /*type_p=*/false,
8838 /*is_declaration=*/false);
a723baf1
MM
8839 /* If we have seen a nested-name-specifier, and the next token
8840 is `template', then we are using the template-id production. */
8841 if (parser->scope
8842 && cp_parser_optional_template_keyword (parser))
8843 {
8844 /* Look for the template-id. */
8845 type = cp_parser_template_id (parser,
8846 /*template_keyword_p=*/true,
a668c6ad
MM
8847 /*check_dependency_p=*/true,
8848 /*is_declaration=*/false);
a723baf1
MM
8849 /* If the template-id did not name a type, we are out of
8850 luck. */
8851 if (TREE_CODE (type) != TYPE_DECL)
8852 {
8853 cp_parser_error (parser, "expected template-id for type");
8854 type = NULL_TREE;
8855 }
8856 }
8857 /* Otherwise, look for a type-name. */
8858 else
4bb8ca28 8859 type = cp_parser_type_name (parser);
a723baf1
MM
8860 /* If it didn't work out, we don't have a TYPE. */
8861 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8862 && !cp_parser_parse_definitely (parser))
8863 type = NULL_TREE;
8864 }
8865
8866 /* If we didn't get a type-name, issue an error message. */
8867 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8868 {
8869 cp_parser_error (parser, "expected type-name");
8870 return error_mark_node;
8871 }
8872
a668c6ad
MM
8873 /* There is no valid C++ program where a non-template type is
8874 followed by a "<". That usually indicates that the user thought
8875 that the type was a template. */
4bb8ca28 8876 if (type && type != error_mark_node)
ee43dab5 8877 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 8878
a723baf1
MM
8879 return type;
8880}
8881
8882/* Parse a type-name.
8883
8884 type-name:
8885 class-name
8886 enum-name
8887 typedef-name
8888
8889 enum-name:
8890 identifier
8891
8892 typedef-name:
8893 identifier
8894
8895 Returns a TYPE_DECL for the the type. */
8896
8897static tree
94edc4ab 8898cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8899{
8900 tree type_decl;
8901 tree identifier;
8902
8903 /* We can't know yet whether it is a class-name or not. */
8904 cp_parser_parse_tentatively (parser);
8905 /* Try a class-name. */
8906 type_decl = cp_parser_class_name (parser,
8907 /*typename_keyword_p=*/false,
8908 /*template_keyword_p=*/false,
8909 /*type_p=*/false,
a723baf1 8910 /*check_dependency_p=*/true,
a668c6ad
MM
8911 /*class_head_p=*/false,
8912 /*is_declaration=*/false);
a723baf1
MM
8913 /* If it's not a class-name, keep looking. */
8914 if (!cp_parser_parse_definitely (parser))
8915 {
8916 /* It must be a typedef-name or an enum-name. */
8917 identifier = cp_parser_identifier (parser);
8918 if (identifier == error_mark_node)
8919 return error_mark_node;
8920
8921 /* Look up the type-name. */
8922 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8923 /* Issue an error if we did not find a type-name. */
8924 if (TREE_CODE (type_decl) != TYPE_DECL)
8925 {
4bb8ca28
MM
8926 if (!cp_parser_simulate_error (parser))
8927 cp_parser_name_lookup_error (parser, identifier, type_decl,
8928 "is not a type");
a723baf1
MM
8929 type_decl = error_mark_node;
8930 }
8931 /* Remember that the name was used in the definition of the
8932 current class so that we can check later to see if the
8933 meaning would have been different after the class was
8934 entirely defined. */
8935 else if (type_decl != error_mark_node
8936 && !parser->scope)
8937 maybe_note_name_used_in_class (identifier, type_decl);
8938 }
8939
8940 return type_decl;
8941}
8942
8943
8944/* Parse an elaborated-type-specifier. Note that the grammar given
8945 here incorporates the resolution to DR68.
8946
8947 elaborated-type-specifier:
8948 class-key :: [opt] nested-name-specifier [opt] identifier
8949 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8950 enum :: [opt] nested-name-specifier [opt] identifier
8951 typename :: [opt] nested-name-specifier identifier
8952 typename :: [opt] nested-name-specifier template [opt]
8953 template-id
8954
360d1b99
MM
8955 GNU extension:
8956
8957 elaborated-type-specifier:
8958 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8959 class-key attributes :: [opt] nested-name-specifier [opt]
8960 template [opt] template-id
8961 enum attributes :: [opt] nested-name-specifier [opt] identifier
8962
a723baf1
MM
8963 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8964 declared `friend'. If IS_DECLARATION is TRUE, then this
8965 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8966 something is being declared.
8967
8968 Returns the TYPE specified. */
8969
8970static tree
94edc4ab
NN
8971cp_parser_elaborated_type_specifier (cp_parser* parser,
8972 bool is_friend,
8973 bool is_declaration)
a723baf1
MM
8974{
8975 enum tag_types tag_type;
8976 tree identifier;
8977 tree type = NULL_TREE;
360d1b99 8978 tree attributes = NULL_TREE;
a723baf1
MM
8979
8980 /* See if we're looking at the `enum' keyword. */
8981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8982 {
8983 /* Consume the `enum' token. */
8984 cp_lexer_consume_token (parser->lexer);
8985 /* Remember that it's an enumeration type. */
8986 tag_type = enum_type;
360d1b99
MM
8987 /* Parse the attributes. */
8988 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
8989 }
8990 /* Or, it might be `typename'. */
8991 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8992 RID_TYPENAME))
8993 {
8994 /* Consume the `typename' token. */
8995 cp_lexer_consume_token (parser->lexer);
8996 /* Remember that it's a `typename' type. */
8997 tag_type = typename_type;
8998 /* The `typename' keyword is only allowed in templates. */
8999 if (!processing_template_decl)
9000 pedwarn ("using `typename' outside of template");
9001 }
9002 /* Otherwise it must be a class-key. */
9003 else
9004 {
9005 tag_type = cp_parser_class_key (parser);
9006 if (tag_type == none_type)
9007 return error_mark_node;
360d1b99
MM
9008 /* Parse the attributes. */
9009 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9010 }
9011
9012 /* Look for the `::' operator. */
9013 cp_parser_global_scope_opt (parser,
9014 /*current_scope_valid_p=*/false);
9015 /* Look for the nested-name-specifier. */
9016 if (tag_type == typename_type)
8fa1ad0e
MM
9017 {
9018 if (cp_parser_nested_name_specifier (parser,
9019 /*typename_keyword_p=*/true,
9020 /*check_dependency_p=*/true,
a668c6ad
MM
9021 /*type_p=*/true,
9022 is_declaration)
8fa1ad0e
MM
9023 == error_mark_node)
9024 return error_mark_node;
9025 }
a723baf1
MM
9026 else
9027 /* Even though `typename' is not present, the proposed resolution
9028 to Core Issue 180 says that in `class A<T>::B', `B' should be
9029 considered a type-name, even if `A<T>' is dependent. */
9030 cp_parser_nested_name_specifier_opt (parser,
9031 /*typename_keyword_p=*/true,
9032 /*check_dependency_p=*/true,
a668c6ad
MM
9033 /*type_p=*/true,
9034 is_declaration);
a723baf1
MM
9035 /* For everything but enumeration types, consider a template-id. */
9036 if (tag_type != enum_type)
9037 {
9038 bool template_p = false;
9039 tree decl;
9040
9041 /* Allow the `template' keyword. */
9042 template_p = cp_parser_optional_template_keyword (parser);
9043 /* If we didn't see `template', we don't know if there's a
9044 template-id or not. */
9045 if (!template_p)
9046 cp_parser_parse_tentatively (parser);
9047 /* Parse the template-id. */
9048 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9049 /*check_dependency_p=*/true,
9050 is_declaration);
a723baf1
MM
9051 /* If we didn't find a template-id, look for an ordinary
9052 identifier. */
9053 if (!template_p && !cp_parser_parse_definitely (parser))
9054 ;
9055 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9056 in effect, then we must assume that, upon instantiation, the
9057 template will correspond to a class. */
9058 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9059 && tag_type == typename_type)
9060 type = make_typename_type (parser->scope, decl,
9061 /*complain=*/1);
9062 else
9063 type = TREE_TYPE (decl);
9064 }
9065
9066 /* For an enumeration type, consider only a plain identifier. */
9067 if (!type)
9068 {
9069 identifier = cp_parser_identifier (parser);
9070
9071 if (identifier == error_mark_node)
eb5abb39
NS
9072 {
9073 parser->scope = NULL_TREE;
9074 return error_mark_node;
9075 }
a723baf1
MM
9076
9077 /* For a `typename', we needn't call xref_tag. */
9078 if (tag_type == typename_type)
9079 return make_typename_type (parser->scope, identifier,
9080 /*complain=*/1);
9081 /* Look up a qualified name in the usual way. */
9082 if (parser->scope)
9083 {
9084 tree decl;
9085
9086 /* In an elaborated-type-specifier, names are assumed to name
9087 types, so we set IS_TYPE to TRUE when calling
9088 cp_parser_lookup_name. */
9089 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 9090 /*is_type=*/true,
b0bc6e8e 9091 /*is_template=*/false,
eea9800f 9092 /*is_namespace=*/false,
a723baf1 9093 /*check_dependency=*/true);
710b73e6
KL
9094
9095 /* If we are parsing friend declaration, DECL may be a
9096 TEMPLATE_DECL tree node here. However, we need to check
9097 whether this TEMPLATE_DECL results in valid code. Consider
9098 the following example:
9099
9100 namespace N {
9101 template <class T> class C {};
9102 }
9103 class X {
9104 template <class T> friend class N::C; // #1, valid code
9105 };
9106 template <class T> class Y {
9107 friend class N::C; // #2, invalid code
9108 };
9109
9110 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9111 name lookup of `N::C'. We see that friend declaration must
9112 be template for the code to be valid. Note that
9113 processing_template_decl does not work here since it is
9114 always 1 for the above two cases. */
9115
a723baf1 9116 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9117 (decl, /*tag_name_p=*/is_friend
9118 && parser->num_template_parameter_lists));
a723baf1
MM
9119
9120 if (TREE_CODE (decl) != TYPE_DECL)
9121 {
9122 error ("expected type-name");
9123 return error_mark_node;
9124 }
560ad596
MM
9125
9126 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9127 check_elaborated_type_specifier
4b0d3cbe 9128 (tag_type, decl,
560ad596
MM
9129 (parser->num_template_parameter_lists
9130 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9131
9132 type = TREE_TYPE (decl);
9133 }
9134 else
9135 {
9136 /* An elaborated-type-specifier sometimes introduces a new type and
9137 sometimes names an existing type. Normally, the rule is that it
9138 introduces a new type only if there is not an existing type of
9139 the same name already in scope. For example, given:
9140
9141 struct S {};
9142 void f() { struct S s; }
9143
9144 the `struct S' in the body of `f' is the same `struct S' as in
9145 the global scope; the existing definition is used. However, if
9146 there were no global declaration, this would introduce a new
9147 local class named `S'.
9148
9149 An exception to this rule applies to the following code:
9150
9151 namespace N { struct S; }
9152
9153 Here, the elaborated-type-specifier names a new type
9154 unconditionally; even if there is already an `S' in the
9155 containing scope this declaration names a new type.
9156 This exception only applies if the elaborated-type-specifier
9157 forms the complete declaration:
9158
9159 [class.name]
9160
9161 A declaration consisting solely of `class-key identifier ;' is
9162 either a redeclaration of the name in the current scope or a
9163 forward declaration of the identifier as a class name. It
9164 introduces the name into the current scope.
9165
9166 We are in this situation precisely when the next token is a `;'.
9167
9168 An exception to the exception is that a `friend' declaration does
9169 *not* name a new type; i.e., given:
9170
9171 struct S { friend struct T; };
9172
9173 `T' is not a new type in the scope of `S'.
9174
9175 Also, `new struct S' or `sizeof (struct S)' never results in the
9176 definition of a new type; a new type can only be declared in a
9bcb9aae 9177 declaration context. */
a723baf1 9178
e0fed25b
DS
9179 /* Warn about attributes. They are ignored. */
9180 if (attributes)
9181 warning ("type attributes are honored only at type definition");
9182
a723baf1 9183 type = xref_tag (tag_type, identifier,
e0fed25b 9184 /*attributes=*/NULL_TREE,
a723baf1
MM
9185 (is_friend
9186 || !is_declaration
9187 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
9188 CPP_SEMICOLON)),
9189 parser->num_template_parameter_lists);
a723baf1
MM
9190 }
9191 }
9192 if (tag_type != enum_type)
9193 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9194
9195 /* A "<" cannot follow an elaborated type specifier. If that
9196 happens, the user was probably trying to form a template-id. */
9197 cp_parser_check_for_invalid_template_id (parser, type);
9198
a723baf1
MM
9199 return type;
9200}
9201
9202/* Parse an enum-specifier.
9203
9204 enum-specifier:
9205 enum identifier [opt] { enumerator-list [opt] }
9206
9207 Returns an ENUM_TYPE representing the enumeration. */
9208
9209static tree
94edc4ab 9210cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9211{
9212 cp_token *token;
9213 tree identifier = NULL_TREE;
9214 tree type;
9215
9216 /* Look for the `enum' keyword. */
9217 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9218 return error_mark_node;
9219 /* Peek at the next token. */
9220 token = cp_lexer_peek_token (parser->lexer);
9221
9222 /* See if it is an identifier. */
9223 if (token->type == CPP_NAME)
9224 identifier = cp_parser_identifier (parser);
9225
9226 /* Look for the `{'. */
9227 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9228 return error_mark_node;
9229
9230 /* At this point, we're going ahead with the enum-specifier, even
9231 if some other problem occurs. */
9232 cp_parser_commit_to_tentative_parse (parser);
9233
9234 /* Issue an error message if type-definitions are forbidden here. */
9235 cp_parser_check_type_definition (parser);
9236
9237 /* Create the new type. */
9238 type = start_enum (identifier ? identifier : make_anon_name ());
9239
9240 /* Peek at the next token. */
9241 token = cp_lexer_peek_token (parser->lexer);
9242 /* If it's not a `}', then there are some enumerators. */
9243 if (token->type != CPP_CLOSE_BRACE)
9244 cp_parser_enumerator_list (parser, type);
9245 /* Look for the `}'. */
9246 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9247
9248 /* Finish up the enumeration. */
9249 finish_enum (type);
9250
9251 return type;
9252}
9253
9254/* Parse an enumerator-list. The enumerators all have the indicated
9255 TYPE.
9256
9257 enumerator-list:
9258 enumerator-definition
9259 enumerator-list , enumerator-definition */
9260
9261static void
94edc4ab 9262cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9263{
9264 while (true)
9265 {
9266 cp_token *token;
9267
9268 /* Parse an enumerator-definition. */
9269 cp_parser_enumerator_definition (parser, type);
9270 /* Peek at the next token. */
9271 token = cp_lexer_peek_token (parser->lexer);
9272 /* If it's not a `,', then we've reached the end of the
9273 list. */
9274 if (token->type != CPP_COMMA)
9275 break;
9276 /* Otherwise, consume the `,' and keep going. */
9277 cp_lexer_consume_token (parser->lexer);
9278 /* If the next token is a `}', there is a trailing comma. */
9279 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9280 {
9281 if (pedantic && !in_system_header)
9282 pedwarn ("comma at end of enumerator list");
9283 break;
9284 }
9285 }
9286}
9287
9288/* Parse an enumerator-definition. The enumerator has the indicated
9289 TYPE.
9290
9291 enumerator-definition:
9292 enumerator
9293 enumerator = constant-expression
9294
9295 enumerator:
9296 identifier */
9297
9298static void
94edc4ab 9299cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9300{
9301 cp_token *token;
9302 tree identifier;
9303 tree value;
9304
9305 /* Look for the identifier. */
9306 identifier = cp_parser_identifier (parser);
9307 if (identifier == error_mark_node)
9308 return;
9309
9310 /* Peek at the next token. */
9311 token = cp_lexer_peek_token (parser->lexer);
9312 /* If it's an `=', then there's an explicit value. */
9313 if (token->type == CPP_EQ)
9314 {
9315 /* Consume the `=' token. */
9316 cp_lexer_consume_token (parser->lexer);
9317 /* Parse the value. */
14d22dd6 9318 value = cp_parser_constant_expression (parser,
d17811fd 9319 /*allow_non_constant_p=*/false,
14d22dd6 9320 NULL);
a723baf1
MM
9321 }
9322 else
9323 value = NULL_TREE;
9324
9325 /* Create the enumerator. */
9326 build_enumerator (identifier, value, type);
9327}
9328
9329/* Parse a namespace-name.
9330
9331 namespace-name:
9332 original-namespace-name
9333 namespace-alias
9334
9335 Returns the NAMESPACE_DECL for the namespace. */
9336
9337static tree
94edc4ab 9338cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9339{
9340 tree identifier;
9341 tree namespace_decl;
9342
9343 /* Get the name of the namespace. */
9344 identifier = cp_parser_identifier (parser);
9345 if (identifier == error_mark_node)
9346 return error_mark_node;
9347
eea9800f
MM
9348 /* Look up the identifier in the currently active scope. Look only
9349 for namespaces, due to:
9350
9351 [basic.lookup.udir]
9352
9353 When looking up a namespace-name in a using-directive or alias
9354 definition, only namespace names are considered.
9355
9356 And:
9357
9358 [basic.lookup.qual]
9359
9360 During the lookup of a name preceding the :: scope resolution
9361 operator, object, function, and enumerator names are ignored.
9362
9363 (Note that cp_parser_class_or_namespace_name only calls this
9364 function if the token after the name is the scope resolution
9365 operator.) */
9366 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f 9367 /*is_type=*/false,
b0bc6e8e 9368 /*is_template=*/false,
eea9800f
MM
9369 /*is_namespace=*/true,
9370 /*check_dependency=*/true);
a723baf1
MM
9371 /* If it's not a namespace, issue an error. */
9372 if (namespace_decl == error_mark_node
9373 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9374 {
9375 cp_parser_error (parser, "expected namespace-name");
9376 namespace_decl = error_mark_node;
9377 }
9378
9379 return namespace_decl;
9380}
9381
9382/* Parse a namespace-definition.
9383
9384 namespace-definition:
9385 named-namespace-definition
9386 unnamed-namespace-definition
9387
9388 named-namespace-definition:
9389 original-namespace-definition
9390 extension-namespace-definition
9391
9392 original-namespace-definition:
9393 namespace identifier { namespace-body }
9394
9395 extension-namespace-definition:
9396 namespace original-namespace-name { namespace-body }
9397
9398 unnamed-namespace-definition:
9399 namespace { namespace-body } */
9400
9401static void
94edc4ab 9402cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9403{
9404 tree identifier;
9405
9406 /* Look for the `namespace' keyword. */
9407 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9408
9409 /* Get the name of the namespace. We do not attempt to distinguish
9410 between an original-namespace-definition and an
9411 extension-namespace-definition at this point. The semantic
9412 analysis routines are responsible for that. */
9413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9414 identifier = cp_parser_identifier (parser);
9415 else
9416 identifier = NULL_TREE;
9417
9418 /* Look for the `{' to start the namespace. */
9419 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9420 /* Start the namespace. */
9421 push_namespace (identifier);
9422 /* Parse the body of the namespace. */
9423 cp_parser_namespace_body (parser);
9424 /* Finish the namespace. */
9425 pop_namespace ();
9426 /* Look for the final `}'. */
9427 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9428}
9429
9430/* Parse a namespace-body.
9431
9432 namespace-body:
9433 declaration-seq [opt] */
9434
9435static void
94edc4ab 9436cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9437{
9438 cp_parser_declaration_seq_opt (parser);
9439}
9440
9441/* Parse a namespace-alias-definition.
9442
9443 namespace-alias-definition:
9444 namespace identifier = qualified-namespace-specifier ; */
9445
9446static void
94edc4ab 9447cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9448{
9449 tree identifier;
9450 tree namespace_specifier;
9451
9452 /* Look for the `namespace' keyword. */
9453 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9454 /* Look for the identifier. */
9455 identifier = cp_parser_identifier (parser);
9456 if (identifier == error_mark_node)
9457 return;
9458 /* Look for the `=' token. */
9459 cp_parser_require (parser, CPP_EQ, "`='");
9460 /* Look for the qualified-namespace-specifier. */
9461 namespace_specifier
9462 = cp_parser_qualified_namespace_specifier (parser);
9463 /* Look for the `;' token. */
9464 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9465
9466 /* Register the alias in the symbol table. */
9467 do_namespace_alias (identifier, namespace_specifier);
9468}
9469
9470/* Parse a qualified-namespace-specifier.
9471
9472 qualified-namespace-specifier:
9473 :: [opt] nested-name-specifier [opt] namespace-name
9474
9475 Returns a NAMESPACE_DECL corresponding to the specified
9476 namespace. */
9477
9478static tree
94edc4ab 9479cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9480{
9481 /* Look for the optional `::'. */
9482 cp_parser_global_scope_opt (parser,
9483 /*current_scope_valid_p=*/false);
9484
9485 /* Look for the optional nested-name-specifier. */
9486 cp_parser_nested_name_specifier_opt (parser,
9487 /*typename_keyword_p=*/false,
9488 /*check_dependency_p=*/true,
a668c6ad
MM
9489 /*type_p=*/false,
9490 /*is_declaration=*/true);
a723baf1
MM
9491
9492 return cp_parser_namespace_name (parser);
9493}
9494
9495/* Parse a using-declaration.
9496
9497 using-declaration:
9498 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9499 using :: unqualified-id ; */
9500
9501static void
94edc4ab 9502cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9503{
9504 cp_token *token;
9505 bool typename_p = false;
9506 bool global_scope_p;
9507 tree decl;
9508 tree identifier;
9509 tree scope;
ed5f054f 9510 tree qscope;
a723baf1
MM
9511
9512 /* Look for the `using' keyword. */
9513 cp_parser_require_keyword (parser, RID_USING, "`using'");
9514
9515 /* Peek at the next token. */
9516 token = cp_lexer_peek_token (parser->lexer);
9517 /* See if it's `typename'. */
9518 if (token->keyword == RID_TYPENAME)
9519 {
9520 /* Remember that we've seen it. */
9521 typename_p = true;
9522 /* Consume the `typename' token. */
9523 cp_lexer_consume_token (parser->lexer);
9524 }
9525
9526 /* Look for the optional global scope qualification. */
9527 global_scope_p
9528 = (cp_parser_global_scope_opt (parser,
9529 /*current_scope_valid_p=*/false)
9530 != NULL_TREE);
9531
9532 /* If we saw `typename', or didn't see `::', then there must be a
9533 nested-name-specifier present. */
9534 if (typename_p || !global_scope_p)
ed5f054f
AO
9535 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9536 /*check_dependency_p=*/true,
9537 /*type_p=*/false,
9538 /*is_declaration=*/true);
a723baf1
MM
9539 /* Otherwise, we could be in either of the two productions. In that
9540 case, treat the nested-name-specifier as optional. */
9541 else
ed5f054f
AO
9542 qscope = cp_parser_nested_name_specifier_opt (parser,
9543 /*typename_keyword_p=*/false,
9544 /*check_dependency_p=*/true,
9545 /*type_p=*/false,
9546 /*is_declaration=*/true);
9547 if (!qscope)
9548 qscope = global_namespace;
a723baf1
MM
9549
9550 /* Parse the unqualified-id. */
9551 identifier = cp_parser_unqualified_id (parser,
9552 /*template_keyword_p=*/false,
f3c2dfc6
MM
9553 /*check_dependency_p=*/true,
9554 /*declarator_p=*/true);
a723baf1
MM
9555
9556 /* The function we call to handle a using-declaration is different
9557 depending on what scope we are in. */
f3c2dfc6
MM
9558 if (identifier == error_mark_node)
9559 ;
9560 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9561 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9562 /* [namespace.udecl]
9563
9564 A using declaration shall not name a template-id. */
9565 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
9566 else
9567 {
f3c2dfc6
MM
9568 scope = current_scope ();
9569 if (scope && TYPE_P (scope))
4eb6d609 9570 {
f3c2dfc6
MM
9571 /* Create the USING_DECL. */
9572 decl = do_class_using_decl (build_nt (SCOPE_REF,
9573 parser->scope,
9574 identifier));
9575 /* Add it to the list of members in this class. */
9576 finish_member_declaration (decl);
4eb6d609 9577 }
a723baf1 9578 else
f3c2dfc6
MM
9579 {
9580 decl = cp_parser_lookup_name_simple (parser, identifier);
9581 if (decl == error_mark_node)
4bb8ca28 9582 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6 9583 else if (scope)
ed5f054f 9584 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 9585 else
ed5f054f 9586 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 9587 }
a723baf1
MM
9588 }
9589
9590 /* Look for the final `;'. */
9591 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9592}
9593
9594/* Parse a using-directive.
9595
9596 using-directive:
9597 using namespace :: [opt] nested-name-specifier [opt]
9598 namespace-name ; */
9599
9600static void
94edc4ab 9601cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9602{
9603 tree namespace_decl;
86098eb8 9604 tree attribs;
a723baf1
MM
9605
9606 /* Look for the `using' keyword. */
9607 cp_parser_require_keyword (parser, RID_USING, "`using'");
9608 /* And the `namespace' keyword. */
9609 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9610 /* Look for the optional `::' operator. */
9611 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 9612 /* And the optional nested-name-specifier. */
a723baf1
MM
9613 cp_parser_nested_name_specifier_opt (parser,
9614 /*typename_keyword_p=*/false,
9615 /*check_dependency_p=*/true,
a668c6ad
MM
9616 /*type_p=*/false,
9617 /*is_declaration=*/true);
a723baf1
MM
9618 /* Get the namespace being used. */
9619 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
9620 /* And any specified attributes. */
9621 attribs = cp_parser_attributes_opt (parser);
a723baf1 9622 /* Update the symbol table. */
86098eb8 9623 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
9624 /* Look for the final `;'. */
9625 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9626}
9627
9628/* Parse an asm-definition.
9629
9630 asm-definition:
9631 asm ( string-literal ) ;
9632
9633 GNU Extension:
9634
9635 asm-definition:
9636 asm volatile [opt] ( string-literal ) ;
9637 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9638 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9639 : asm-operand-list [opt] ) ;
9640 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9641 : asm-operand-list [opt]
9642 : asm-operand-list [opt] ) ; */
9643
9644static void
94edc4ab 9645cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9646{
9647 cp_token *token;
9648 tree string;
9649 tree outputs = NULL_TREE;
9650 tree inputs = NULL_TREE;
9651 tree clobbers = NULL_TREE;
9652 tree asm_stmt;
9653 bool volatile_p = false;
9654 bool extended_p = false;
9655
9656 /* Look for the `asm' keyword. */
9657 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9658 /* See if the next token is `volatile'. */
9659 if (cp_parser_allow_gnu_extensions_p (parser)
9660 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9661 {
9662 /* Remember that we saw the `volatile' keyword. */
9663 volatile_p = true;
9664 /* Consume the token. */
9665 cp_lexer_consume_token (parser->lexer);
9666 }
9667 /* Look for the opening `('. */
9668 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9669 /* Look for the string. */
9670 token = cp_parser_require (parser, CPP_STRING, "asm body");
9671 if (!token)
9672 return;
9673 string = token->value;
9674 /* If we're allowing GNU extensions, check for the extended assembly
9675 syntax. Unfortunately, the `:' tokens need not be separated by
9676 a space in C, and so, for compatibility, we tolerate that here
9677 too. Doing that means that we have to treat the `::' operator as
9678 two `:' tokens. */
9679 if (cp_parser_allow_gnu_extensions_p (parser)
9680 && at_function_scope_p ()
9681 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9682 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9683 {
9684 bool inputs_p = false;
9685 bool clobbers_p = false;
9686
9687 /* The extended syntax was used. */
9688 extended_p = true;
9689
9690 /* Look for outputs. */
9691 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9692 {
9693 /* Consume the `:'. */
9694 cp_lexer_consume_token (parser->lexer);
9695 /* Parse the output-operands. */
9696 if (cp_lexer_next_token_is_not (parser->lexer,
9697 CPP_COLON)
9698 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9699 CPP_SCOPE)
9700 && cp_lexer_next_token_is_not (parser->lexer,
9701 CPP_CLOSE_PAREN))
a723baf1
MM
9702 outputs = cp_parser_asm_operand_list (parser);
9703 }
9704 /* If the next token is `::', there are no outputs, and the
9705 next token is the beginning of the inputs. */
9706 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9707 {
9708 /* Consume the `::' token. */
9709 cp_lexer_consume_token (parser->lexer);
9710 /* The inputs are coming next. */
9711 inputs_p = true;
9712 }
9713
9714 /* Look for inputs. */
9715 if (inputs_p
9716 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9717 {
9718 if (!inputs_p)
9719 /* Consume the `:'. */
9720 cp_lexer_consume_token (parser->lexer);
9721 /* Parse the output-operands. */
9722 if (cp_lexer_next_token_is_not (parser->lexer,
9723 CPP_COLON)
9724 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9725 CPP_SCOPE)
9726 && cp_lexer_next_token_is_not (parser->lexer,
9727 CPP_CLOSE_PAREN))
a723baf1
MM
9728 inputs = cp_parser_asm_operand_list (parser);
9729 }
9730 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9731 /* The clobbers are coming next. */
9732 clobbers_p = true;
9733
9734 /* Look for clobbers. */
9735 if (clobbers_p
9736 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9737 {
9738 if (!clobbers_p)
9739 /* Consume the `:'. */
9740 cp_lexer_consume_token (parser->lexer);
9741 /* Parse the clobbers. */
8caf4c38
MM
9742 if (cp_lexer_next_token_is_not (parser->lexer,
9743 CPP_CLOSE_PAREN))
9744 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9745 }
9746 }
9747 /* Look for the closing `)'. */
9748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
9749 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9750 /*consume_paren=*/true);
a723baf1
MM
9751 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9752
9753 /* Create the ASM_STMT. */
9754 if (at_function_scope_p ())
9755 {
9756 asm_stmt =
9757 finish_asm_stmt (volatile_p
9758 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9759 string, outputs, inputs, clobbers);
9760 /* If the extended syntax was not used, mark the ASM_STMT. */
9761 if (!extended_p)
9762 ASM_INPUT_P (asm_stmt) = 1;
9763 }
9764 else
9765 assemble_asm (string);
9766}
9767
9768/* Declarators [gram.dcl.decl] */
9769
9770/* Parse an init-declarator.
9771
9772 init-declarator:
9773 declarator initializer [opt]
9774
9775 GNU Extension:
9776
9777 init-declarator:
9778 declarator asm-specification [opt] attributes [opt] initializer [opt]
9779
4bb8ca28
MM
9780 function-definition:
9781 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9782 function-body
9783 decl-specifier-seq [opt] declarator function-try-block
9784
9785 GNU Extension:
9786
9787 function-definition:
9788 __extension__ function-definition
9789
a723baf1 9790 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9791 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9792 then this declarator appears in a class scope. The new DECL created
9793 by this declarator is returned.
a723baf1
MM
9794
9795 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9796 for a function-definition here as well. If the declarator is a
9797 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9798 be TRUE upon return. By that point, the function-definition will
9799 have been completely parsed.
9800
9801 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9802 is FALSE. */
9803
9804static tree
94edc4ab
NN
9805cp_parser_init_declarator (cp_parser* parser,
9806 tree decl_specifiers,
9807 tree prefix_attributes,
9808 bool function_definition_allowed_p,
9809 bool member_p,
560ad596 9810 int declares_class_or_enum,
94edc4ab 9811 bool* function_definition_p)
a723baf1
MM
9812{
9813 cp_token *token;
9814 tree declarator;
9815 tree attributes;
9816 tree asm_specification;
9817 tree initializer;
9818 tree decl = NULL_TREE;
9819 tree scope;
a723baf1
MM
9820 bool is_initialized;
9821 bool is_parenthesized_init;
39703eb9 9822 bool is_non_constant_init;
7efa3e22 9823 int ctor_dtor_or_conv_p;
a723baf1
MM
9824 bool friend_p;
9825
9826 /* Assume that this is not the declarator for a function
9827 definition. */
9828 if (function_definition_p)
9829 *function_definition_p = false;
9830
9831 /* Defer access checks while parsing the declarator; we cannot know
9832 what names are accessible until we know what is being
9833 declared. */
cf22909c
KL
9834 resume_deferring_access_checks ();
9835
a723baf1
MM
9836 /* Parse the declarator. */
9837 declarator
62b8a44e 9838 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9839 &ctor_dtor_or_conv_p,
9840 /*parenthesized_p=*/NULL);
a723baf1 9841 /* Gather up the deferred checks. */
cf22909c 9842 stop_deferring_access_checks ();
24c0ef37 9843
a723baf1
MM
9844 /* If the DECLARATOR was erroneous, there's no need to go
9845 further. */
9846 if (declarator == error_mark_node)
cf22909c 9847 return error_mark_node;
a723baf1 9848
560ad596
MM
9849 cp_parser_check_for_definition_in_return_type (declarator,
9850 declares_class_or_enum);
9851
a723baf1
MM
9852 /* Figure out what scope the entity declared by the DECLARATOR is
9853 located in. `grokdeclarator' sometimes changes the scope, so
9854 we compute it now. */
9855 scope = get_scope_of_declarator (declarator);
9856
9857 /* If we're allowing GNU extensions, look for an asm-specification
9858 and attributes. */
9859 if (cp_parser_allow_gnu_extensions_p (parser))
9860 {
9861 /* Look for an asm-specification. */
9862 asm_specification = cp_parser_asm_specification_opt (parser);
9863 /* And attributes. */
9864 attributes = cp_parser_attributes_opt (parser);
9865 }
9866 else
9867 {
9868 asm_specification = NULL_TREE;
9869 attributes = NULL_TREE;
9870 }
9871
9872 /* Peek at the next token. */
9873 token = cp_lexer_peek_token (parser->lexer);
9874 /* Check to see if the token indicates the start of a
9875 function-definition. */
9876 if (cp_parser_token_starts_function_definition_p (token))
9877 {
9878 if (!function_definition_allowed_p)
9879 {
9880 /* If a function-definition should not appear here, issue an
9881 error message. */
9882 cp_parser_error (parser,
9883 "a function-definition is not allowed here");
9884 return error_mark_node;
9885 }
9886 else
9887 {
a723baf1
MM
9888 /* Neither attributes nor an asm-specification are allowed
9889 on a function-definition. */
9890 if (asm_specification)
9891 error ("an asm-specification is not allowed on a function-definition");
9892 if (attributes)
9893 error ("attributes are not allowed on a function-definition");
9894 /* This is a function-definition. */
9895 *function_definition_p = true;
9896
a723baf1 9897 /* Parse the function definition. */
4bb8ca28
MM
9898 if (member_p)
9899 decl = cp_parser_save_member_function_body (parser,
9900 decl_specifiers,
9901 declarator,
9902 prefix_attributes);
9903 else
9904 decl
9905 = (cp_parser_function_definition_from_specifiers_and_declarator
9906 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9907
a723baf1
MM
9908 return decl;
9909 }
9910 }
9911
9912 /* [dcl.dcl]
9913
9914 Only in function declarations for constructors, destructors, and
9915 type conversions can the decl-specifier-seq be omitted.
9916
9917 We explicitly postpone this check past the point where we handle
9918 function-definitions because we tolerate function-definitions
9919 that are missing their return types in some modes. */
7efa3e22 9920 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
a723baf1
MM
9921 {
9922 cp_parser_error (parser,
9923 "expected constructor, destructor, or type conversion");
9924 return error_mark_node;
9925 }
9926
9927 /* An `=' or an `(' indicates an initializer. */
9928 is_initialized = (token->type == CPP_EQ
9929 || token->type == CPP_OPEN_PAREN);
9930 /* If the init-declarator isn't initialized and isn't followed by a
9931 `,' or `;', it's not a valid init-declarator. */
9932 if (!is_initialized
9933 && token->type != CPP_COMMA
9934 && token->type != CPP_SEMICOLON)
9935 {
9936 cp_parser_error (parser, "expected init-declarator");
9937 return error_mark_node;
9938 }
9939
9940 /* Because start_decl has side-effects, we should only call it if we
9941 know we're going ahead. By this point, we know that we cannot
9942 possibly be looking at any other construct. */
9943 cp_parser_commit_to_tentative_parse (parser);
9944
e90c7b84
ILT
9945 /* If the decl specifiers were bad, issue an error now that we're
9946 sure this was intended to be a declarator. Then continue
9947 declaring the variable(s), as int, to try to cut down on further
9948 errors. */
9949 if (decl_specifiers != NULL
9950 && TREE_VALUE (decl_specifiers) == error_mark_node)
9951 {
9952 cp_parser_error (parser, "invalid type in declaration");
9953 TREE_VALUE (decl_specifiers) = integer_type_node;
9954 }
9955
a723baf1
MM
9956 /* Check to see whether or not this declaration is a friend. */
9957 friend_p = cp_parser_friend_p (decl_specifiers);
9958
9959 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 9960 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 9961 return error_mark_node;
a723baf1
MM
9962
9963 /* Enter the newly declared entry in the symbol table. If we're
9964 processing a declaration in a class-specifier, we wait until
9965 after processing the initializer. */
9966 if (!member_p)
9967 {
9968 if (parser->in_unbraced_linkage_specification_p)
9969 {
9970 decl_specifiers = tree_cons (error_mark_node,
9971 get_identifier ("extern"),
9972 decl_specifiers);
9973 have_extern_spec = false;
9974 }
ee3071ef
NS
9975 decl = start_decl (declarator, decl_specifiers,
9976 is_initialized, attributes, prefix_attributes);
a723baf1
MM
9977 }
9978
9979 /* Enter the SCOPE. That way unqualified names appearing in the
9980 initializer will be looked up in SCOPE. */
9981 if (scope)
9982 push_scope (scope);
9983
9984 /* Perform deferred access control checks, now that we know in which
9985 SCOPE the declared entity resides. */
9986 if (!member_p && decl)
9987 {
9988 tree saved_current_function_decl = NULL_TREE;
9989
9990 /* If the entity being declared is a function, pretend that we
9991 are in its scope. If it is a `friend', it may have access to
9bcb9aae 9992 things that would not otherwise be accessible. */
a723baf1
MM
9993 if (TREE_CODE (decl) == FUNCTION_DECL)
9994 {
9995 saved_current_function_decl = current_function_decl;
9996 current_function_decl = decl;
9997 }
9998
cf22909c
KL
9999 /* Perform the access control checks for the declarator and the
10000 the decl-specifiers. */
10001 perform_deferred_access_checks ();
a723baf1
MM
10002
10003 /* Restore the saved value. */
10004 if (TREE_CODE (decl) == FUNCTION_DECL)
10005 current_function_decl = saved_current_function_decl;
10006 }
10007
10008 /* Parse the initializer. */
10009 if (is_initialized)
39703eb9
MM
10010 initializer = cp_parser_initializer (parser,
10011 &is_parenthesized_init,
10012 &is_non_constant_init);
a723baf1
MM
10013 else
10014 {
10015 initializer = NULL_TREE;
10016 is_parenthesized_init = false;
39703eb9 10017 is_non_constant_init = true;
a723baf1
MM
10018 }
10019
10020 /* The old parser allows attributes to appear after a parenthesized
10021 initializer. Mark Mitchell proposed removing this functionality
10022 on the GCC mailing lists on 2002-08-13. This parser accepts the
10023 attributes -- but ignores them. */
10024 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10025 if (cp_parser_attributes_opt (parser))
10026 warning ("attributes after parenthesized initializer ignored");
10027
10028 /* Leave the SCOPE, now that we have processed the initializer. It
10029 is important to do this before calling cp_finish_decl because it
10030 makes decisions about whether to create DECL_STMTs or not based
10031 on the current scope. */
10032 if (scope)
10033 pop_scope (scope);
10034
10035 /* For an in-class declaration, use `grokfield' to create the
10036 declaration. */
10037 if (member_p)
8db1028e
NS
10038 {
10039 decl = grokfield (declarator, decl_specifiers,
10040 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10041 /*attributes=*/NULL_TREE);
8db1028e
NS
10042 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10043 cp_parser_save_default_args (parser, decl);
10044 }
10045
a723baf1
MM
10046 /* Finish processing the declaration. But, skip friend
10047 declarations. */
10048 if (!friend_p && decl)
10049 cp_finish_decl (decl,
10050 initializer,
10051 asm_specification,
10052 /* If the initializer is in parentheses, then this is
10053 a direct-initialization, which means that an
10054 `explicit' constructor is OK. Otherwise, an
10055 `explicit' constructor cannot be used. */
10056 ((is_parenthesized_init || !is_initialized)
10057 ? 0 : LOOKUP_ONLYCONVERTING));
10058
39703eb9
MM
10059 /* Remember whether or not variables were initialized by
10060 constant-expressions. */
10061 if (decl && TREE_CODE (decl) == VAR_DECL
10062 && is_initialized && !is_non_constant_init)
10063 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10064
a723baf1
MM
10065 return decl;
10066}
10067
10068/* Parse a declarator.
10069
10070 declarator:
10071 direct-declarator
10072 ptr-operator declarator
10073
10074 abstract-declarator:
10075 ptr-operator abstract-declarator [opt]
10076 direct-abstract-declarator
10077
10078 GNU Extensions:
10079
10080 declarator:
10081 attributes [opt] direct-declarator
10082 attributes [opt] ptr-operator declarator
10083
10084 abstract-declarator:
10085 attributes [opt] ptr-operator abstract-declarator [opt]
10086 attributes [opt] direct-abstract-declarator
10087
10088 Returns a representation of the declarator. If the declarator has
10089 the form `* declarator', then an INDIRECT_REF is returned, whose
34cd5ae7 10090 only operand is the sub-declarator. Analogously, `& declarator' is
a723baf1
MM
10091 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10092 used. The first operand is the TYPE for `X'. The second operand
10093 is an INDIRECT_REF whose operand is the sub-declarator.
10094
34cd5ae7 10095 Otherwise, the representation is as for a direct-declarator.
a723baf1
MM
10096
10097 (It would be better to define a structure type to represent
10098 declarators, rather than abusing `tree' nodes to represent
10099 declarators. That would be much clearer and save some memory.
10100 There is no reason for declarators to be garbage-collected, for
10101 example; they are created during parser and no longer needed after
10102 `grokdeclarator' has been called.)
10103
10104 For a ptr-operator that has the optional cv-qualifier-seq,
10105 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10106 node.
10107
7efa3e22
NS
10108 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10109 detect constructor, destructor or conversion operators. It is set
10110 to -1 if the declarator is a name, and +1 if it is a
10111 function. Otherwise it is set to zero. Usually you just want to
10112 test for >0, but internally the negative value is used.
10113
a723baf1
MM
10114 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10115 a decl-specifier-seq unless it declares a constructor, destructor,
10116 or conversion. It might seem that we could check this condition in
10117 semantic analysis, rather than parsing, but that makes it difficult
10118 to handle something like `f()'. We want to notice that there are
10119 no decl-specifiers, and therefore realize that this is an
4bb8ca28
MM
10120 expression, not a declaration.)
10121
10122 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10123 the declarator is a direct-declarator of the form "(...)". */
a723baf1
MM
10124
10125static tree
94edc4ab
NN
10126cp_parser_declarator (cp_parser* parser,
10127 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
10128 int* ctor_dtor_or_conv_p,
10129 bool* parenthesized_p)
a723baf1
MM
10130{
10131 cp_token *token;
10132 tree declarator;
10133 enum tree_code code;
10134 tree cv_qualifier_seq;
10135 tree class_type;
10136 tree attributes = NULL_TREE;
10137
10138 /* Assume this is not a constructor, destructor, or type-conversion
10139 operator. */
10140 if (ctor_dtor_or_conv_p)
7efa3e22 10141 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10142
10143 if (cp_parser_allow_gnu_extensions_p (parser))
10144 attributes = cp_parser_attributes_opt (parser);
10145
10146 /* Peek at the next token. */
10147 token = cp_lexer_peek_token (parser->lexer);
10148
10149 /* Check for the ptr-operator production. */
10150 cp_parser_parse_tentatively (parser);
10151 /* Parse the ptr-operator. */
10152 code = cp_parser_ptr_operator (parser,
10153 &class_type,
10154 &cv_qualifier_seq);
10155 /* If that worked, then we have a ptr-operator. */
10156 if (cp_parser_parse_definitely (parser))
10157 {
4bb8ca28
MM
10158 /* If a ptr-operator was found, then this declarator was not
10159 parenthesized. */
10160 if (parenthesized_p)
10161 *parenthesized_p = true;
a723baf1
MM
10162 /* The dependent declarator is optional if we are parsing an
10163 abstract-declarator. */
62b8a44e 10164 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10165 cp_parser_parse_tentatively (parser);
10166
10167 /* Parse the dependent declarator. */
62b8a44e 10168 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
10169 /*ctor_dtor_or_conv_p=*/NULL,
10170 /*parenthesized_p=*/NULL);
a723baf1
MM
10171
10172 /* If we are parsing an abstract-declarator, we must handle the
10173 case where the dependent declarator is absent. */
62b8a44e
NS
10174 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10175 && !cp_parser_parse_definitely (parser))
a723baf1
MM
10176 declarator = NULL_TREE;
10177
10178 /* Build the representation of the ptr-operator. */
10179 if (code == INDIRECT_REF)
10180 declarator = make_pointer_declarator (cv_qualifier_seq,
10181 declarator);
10182 else
10183 declarator = make_reference_declarator (cv_qualifier_seq,
10184 declarator);
10185 /* Handle the pointer-to-member case. */
10186 if (class_type)
10187 declarator = build_nt (SCOPE_REF, class_type, declarator);
10188 }
10189 /* Everything else is a direct-declarator. */
10190 else
4bb8ca28
MM
10191 {
10192 if (parenthesized_p)
10193 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10194 CPP_OPEN_PAREN);
10195 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10196 ctor_dtor_or_conv_p);
10197 }
a723baf1
MM
10198
10199 if (attributes && declarator != error_mark_node)
10200 declarator = tree_cons (attributes, declarator, NULL_TREE);
10201
10202 return declarator;
10203}
10204
10205/* Parse a direct-declarator or direct-abstract-declarator.
10206
10207 direct-declarator:
10208 declarator-id
10209 direct-declarator ( parameter-declaration-clause )
10210 cv-qualifier-seq [opt]
10211 exception-specification [opt]
10212 direct-declarator [ constant-expression [opt] ]
10213 ( declarator )
10214
10215 direct-abstract-declarator:
10216 direct-abstract-declarator [opt]
10217 ( parameter-declaration-clause )
10218 cv-qualifier-seq [opt]
10219 exception-specification [opt]
10220 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10221 ( abstract-declarator )
10222
62b8a44e
NS
10223 Returns a representation of the declarator. DCL_KIND is
10224 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10225 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10226 we are parsing a direct-declarator. It is
10227 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10228 of ambiguity we prefer an abstract declarator, as per
10229 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10230 cp_parser_declarator.
10231
10232 For the declarator-id production, the representation is as for an
10233 id-expression, except that a qualified name is represented as a
10234 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10235 see the documentation of the FUNCTION_DECLARATOR_* macros for
10236 information about how to find the various declarator components.
10237 An array-declarator is represented as an ARRAY_REF. The
10238 direct-declarator is the first operand; the constant-expression
10239 indicating the size of the array is the second operand. */
10240
10241static tree
94edc4ab
NN
10242cp_parser_direct_declarator (cp_parser* parser,
10243 cp_parser_declarator_kind dcl_kind,
7efa3e22 10244 int* ctor_dtor_or_conv_p)
a723baf1
MM
10245{
10246 cp_token *token;
62b8a44e 10247 tree declarator = NULL_TREE;
a723baf1
MM
10248 tree scope = NULL_TREE;
10249 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10250 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
10251 bool first = true;
10252
10253 while (true)
a723baf1 10254 {
62b8a44e
NS
10255 /* Peek at the next token. */
10256 token = cp_lexer_peek_token (parser->lexer);
10257 if (token->type == CPP_OPEN_PAREN)
a723baf1 10258 {
62b8a44e
NS
10259 /* This is either a parameter-declaration-clause, or a
10260 parenthesized declarator. When we know we are parsing a
34cd5ae7 10261 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10262 if FIRST is true. For instance, `(int)' is a
10263 parameter-declaration-clause, with an omitted
10264 direct-abstract-declarator. But `((*))', is a
10265 parenthesized abstract declarator. Finally, when T is a
10266 template parameter `(T)' is a
34cd5ae7 10267 parameter-declaration-clause, and not a parenthesized
62b8a44e 10268 named declarator.
a723baf1 10269
62b8a44e
NS
10270 We first try and parse a parameter-declaration-clause,
10271 and then try a nested declarator (if FIRST is true).
a723baf1 10272
62b8a44e
NS
10273 It is not an error for it not to be a
10274 parameter-declaration-clause, even when FIRST is
10275 false. Consider,
10276
10277 int i (int);
10278 int i (3);
10279
10280 The first is the declaration of a function while the
10281 second is a the definition of a variable, including its
10282 initializer.
10283
10284 Having seen only the parenthesis, we cannot know which of
10285 these two alternatives should be selected. Even more
10286 complex are examples like:
10287
10288 int i (int (a));
10289 int i (int (3));
10290
10291 The former is a function-declaration; the latter is a
10292 variable initialization.
10293
34cd5ae7 10294 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10295 that fails, we back out and return. */
10296
10297 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10298 {
62b8a44e 10299 tree params;
4047b164 10300 unsigned saved_num_template_parameter_lists;
62b8a44e
NS
10301
10302 cp_parser_parse_tentatively (parser);
a723baf1 10303
62b8a44e
NS
10304 /* Consume the `('. */
10305 cp_lexer_consume_token (parser->lexer);
10306 if (first)
10307 {
10308 /* If this is going to be an abstract declarator, we're
10309 in a declarator and we can't have default args. */
10310 parser->default_arg_ok_p = false;
10311 parser->in_declarator_p = true;
10312 }
10313
4047b164
KL
10314 /* Inside the function parameter list, surrounding
10315 template-parameter-lists do not apply. */
10316 saved_num_template_parameter_lists
10317 = parser->num_template_parameter_lists;
10318 parser->num_template_parameter_lists = 0;
10319
62b8a44e
NS
10320 /* Parse the parameter-declaration-clause. */
10321 params = cp_parser_parameter_declaration_clause (parser);
10322
4047b164
KL
10323 parser->num_template_parameter_lists
10324 = saved_num_template_parameter_lists;
10325
62b8a44e 10326 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10327 exception-specification. */
62b8a44e
NS
10328 if (cp_parser_parse_definitely (parser))
10329 {
10330 tree cv_qualifiers;
10331 tree exception_specification;
7efa3e22
NS
10332
10333 if (ctor_dtor_or_conv_p)
10334 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10335 first = false;
10336 /* Consume the `)'. */
10337 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10338
10339 /* Parse the cv-qualifier-seq. */
10340 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10341 /* And the exception-specification. */
10342 exception_specification
10343 = cp_parser_exception_specification_opt (parser);
10344
10345 /* Create the function-declarator. */
10346 declarator = make_call_declarator (declarator,
10347 params,
10348 cv_qualifiers,
10349 exception_specification);
10350 /* Any subsequent parameter lists are to do with
10351 return type, so are not those of the declared
10352 function. */
10353 parser->default_arg_ok_p = false;
10354
10355 /* Repeat the main loop. */
10356 continue;
10357 }
10358 }
10359
10360 /* If this is the first, we can try a parenthesized
10361 declarator. */
10362 if (first)
a723baf1 10363 {
a7324e75
MM
10364 bool saved_in_type_id_in_expr_p;
10365
a723baf1 10366 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10367 parser->in_declarator_p = saved_in_declarator_p;
10368
10369 /* Consume the `('. */
10370 cp_lexer_consume_token (parser->lexer);
10371 /* Parse the nested declarator. */
a7324e75
MM
10372 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10373 parser->in_type_id_in_expr_p = true;
62b8a44e 10374 declarator
4bb8ca28
MM
10375 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10376 /*parenthesized_p=*/NULL);
a7324e75 10377 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
10378 first = false;
10379 /* Expect a `)'. */
10380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10381 declarator = error_mark_node;
10382 if (declarator == error_mark_node)
10383 break;
10384
10385 goto handle_declarator;
a723baf1 10386 }
9bcb9aae 10387 /* Otherwise, we must be done. */
62b8a44e
NS
10388 else
10389 break;
a723baf1 10390 }
62b8a44e
NS
10391 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10392 && token->type == CPP_OPEN_SQUARE)
a723baf1 10393 {
62b8a44e 10394 /* Parse an array-declarator. */
a723baf1
MM
10395 tree bounds;
10396
7efa3e22
NS
10397 if (ctor_dtor_or_conv_p)
10398 *ctor_dtor_or_conv_p = 0;
10399
62b8a44e
NS
10400 first = false;
10401 parser->default_arg_ok_p = false;
10402 parser->in_declarator_p = true;
a723baf1
MM
10403 /* Consume the `['. */
10404 cp_lexer_consume_token (parser->lexer);
10405 /* Peek at the next token. */
10406 token = cp_lexer_peek_token (parser->lexer);
10407 /* If the next token is `]', then there is no
10408 constant-expression. */
10409 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10410 {
10411 bool non_constant_p;
10412
10413 bounds
10414 = cp_parser_constant_expression (parser,
10415 /*allow_non_constant=*/true,
10416 &non_constant_p);
d17811fd 10417 if (!non_constant_p)
9baa27a9 10418 bounds = fold_non_dependent_expr (bounds);
14d22dd6 10419 }
a723baf1
MM
10420 else
10421 bounds = NULL_TREE;
10422 /* Look for the closing `]'. */
62b8a44e
NS
10423 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10424 {
10425 declarator = error_mark_node;
10426 break;
10427 }
a723baf1
MM
10428
10429 declarator = build_nt (ARRAY_REF, declarator, bounds);
10430 }
62b8a44e 10431 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10432 {
a668c6ad 10433 /* Parse a declarator-id */
62b8a44e
NS
10434 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10435 cp_parser_parse_tentatively (parser);
10436 declarator = cp_parser_declarator_id (parser);
712becab
NS
10437 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10438 {
10439 if (!cp_parser_parse_definitely (parser))
10440 declarator = error_mark_node;
10441 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10442 {
10443 cp_parser_error (parser, "expected unqualified-id");
10444 declarator = error_mark_node;
10445 }
10446 }
10447
62b8a44e
NS
10448 if (declarator == error_mark_node)
10449 break;
a723baf1 10450
d9a50301
KL
10451 if (TREE_CODE (declarator) == SCOPE_REF
10452 && !current_scope ())
62b8a44e
NS
10453 {
10454 tree scope = TREE_OPERAND (declarator, 0);
712becab 10455
62b8a44e
NS
10456 /* In the declaration of a member of a template class
10457 outside of the class itself, the SCOPE will sometimes
10458 be a TYPENAME_TYPE. For example, given:
10459
10460 template <typename T>
10461 int S<T>::R::i = 3;
10462
10463 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10464 this context, we must resolve S<T>::R to an ordinary
10465 type, rather than a typename type.
10466
10467 The reason we normally avoid resolving TYPENAME_TYPEs
10468 is that a specialization of `S' might render
10469 `S<T>::R' not a type. However, if `S' is
10470 specialized, then this `i' will not be used, so there
10471 is no harm in resolving the types here. */
10472 if (TREE_CODE (scope) == TYPENAME_TYPE)
10473 {
14d22dd6
MM
10474 tree type;
10475
62b8a44e 10476 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10477 type = resolve_typename_type (scope,
10478 /*only_current_p=*/false);
62b8a44e 10479 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10480 if (type != error_mark_node)
10481 scope = type;
62b8a44e
NS
10482 /* Build a new DECLARATOR. */
10483 declarator = build_nt (SCOPE_REF,
10484 scope,
10485 TREE_OPERAND (declarator, 1));
10486 }
10487 }
10488
10489 /* Check to see whether the declarator-id names a constructor,
10490 destructor, or conversion. */
10491 if (declarator && ctor_dtor_or_conv_p
10492 && ((TREE_CODE (declarator) == SCOPE_REF
10493 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10494 || (TREE_CODE (declarator) != SCOPE_REF
10495 && at_class_scope_p ())))
a723baf1 10496 {
62b8a44e
NS
10497 tree unqualified_name;
10498 tree class_type;
10499
10500 /* Get the unqualified part of the name. */
10501 if (TREE_CODE (declarator) == SCOPE_REF)
10502 {
10503 class_type = TREE_OPERAND (declarator, 0);
10504 unqualified_name = TREE_OPERAND (declarator, 1);
10505 }
10506 else
10507 {
10508 class_type = current_class_type;
10509 unqualified_name = declarator;
10510 }
10511
10512 /* See if it names ctor, dtor or conv. */
10513 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10514 || IDENTIFIER_TYPENAME_P (unqualified_name)
10515 || constructor_name_p (unqualified_name, class_type))
7efa3e22 10516 *ctor_dtor_or_conv_p = -1;
a723baf1 10517 }
62b8a44e
NS
10518
10519 handle_declarator:;
10520 scope = get_scope_of_declarator (declarator);
10521 if (scope)
10522 /* Any names that appear after the declarator-id for a member
10523 are looked up in the containing scope. */
10524 push_scope (scope);
10525 parser->in_declarator_p = true;
10526 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10527 || (declarator
10528 && (TREE_CODE (declarator) == SCOPE_REF
10529 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10530 /* Default args are only allowed on function
10531 declarations. */
10532 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10533 else
62b8a44e
NS
10534 parser->default_arg_ok_p = false;
10535
10536 first = false;
a723baf1 10537 }
62b8a44e 10538 /* We're done. */
a723baf1
MM
10539 else
10540 break;
a723baf1
MM
10541 }
10542
10543 /* For an abstract declarator, we might wind up with nothing at this
10544 point. That's an error; the declarator is not optional. */
10545 if (!declarator)
10546 cp_parser_error (parser, "expected declarator");
10547
10548 /* If we entered a scope, we must exit it now. */
10549 if (scope)
10550 pop_scope (scope);
10551
10552 parser->default_arg_ok_p = saved_default_arg_ok_p;
10553 parser->in_declarator_p = saved_in_declarator_p;
10554
10555 return declarator;
10556}
10557
10558/* Parse a ptr-operator.
10559
10560 ptr-operator:
10561 * cv-qualifier-seq [opt]
10562 &
10563 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10564
10565 GNU Extension:
10566
10567 ptr-operator:
10568 & cv-qualifier-seq [opt]
10569
10570 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10571 used. Returns ADDR_EXPR if a reference was used. In the
10572 case of a pointer-to-member, *TYPE is filled in with the
10573 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10574 with the cv-qualifier-seq, or NULL_TREE, if there are no
10575 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10576
10577static enum tree_code
94edc4ab
NN
10578cp_parser_ptr_operator (cp_parser* parser,
10579 tree* type,
10580 tree* cv_qualifier_seq)
a723baf1
MM
10581{
10582 enum tree_code code = ERROR_MARK;
10583 cp_token *token;
10584
10585 /* Assume that it's not a pointer-to-member. */
10586 *type = NULL_TREE;
10587 /* And that there are no cv-qualifiers. */
10588 *cv_qualifier_seq = NULL_TREE;
10589
10590 /* Peek at the next token. */
10591 token = cp_lexer_peek_token (parser->lexer);
10592 /* If it's a `*' or `&' we have a pointer or reference. */
10593 if (token->type == CPP_MULT || token->type == CPP_AND)
10594 {
10595 /* Remember which ptr-operator we were processing. */
10596 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10597
10598 /* Consume the `*' or `&'. */
10599 cp_lexer_consume_token (parser->lexer);
10600
10601 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10602 `&', if we are allowing GNU extensions. (The only qualifier
10603 that can legally appear after `&' is `restrict', but that is
10604 enforced during semantic analysis. */
10605 if (code == INDIRECT_REF
10606 || cp_parser_allow_gnu_extensions_p (parser))
10607 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10608 }
10609 else
10610 {
10611 /* Try the pointer-to-member case. */
10612 cp_parser_parse_tentatively (parser);
10613 /* Look for the optional `::' operator. */
10614 cp_parser_global_scope_opt (parser,
10615 /*current_scope_valid_p=*/false);
10616 /* Look for the nested-name specifier. */
10617 cp_parser_nested_name_specifier (parser,
10618 /*typename_keyword_p=*/false,
10619 /*check_dependency_p=*/true,
a668c6ad
MM
10620 /*type_p=*/false,
10621 /*is_declaration=*/false);
a723baf1
MM
10622 /* If we found it, and the next token is a `*', then we are
10623 indeed looking at a pointer-to-member operator. */
10624 if (!cp_parser_error_occurred (parser)
10625 && cp_parser_require (parser, CPP_MULT, "`*'"))
10626 {
10627 /* The type of which the member is a member is given by the
10628 current SCOPE. */
10629 *type = parser->scope;
10630 /* The next name will not be qualified. */
10631 parser->scope = NULL_TREE;
10632 parser->qualifying_scope = NULL_TREE;
10633 parser->object_scope = NULL_TREE;
10634 /* Indicate that the `*' operator was used. */
10635 code = INDIRECT_REF;
10636 /* Look for the optional cv-qualifier-seq. */
10637 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10638 }
10639 /* If that didn't work we don't have a ptr-operator. */
10640 if (!cp_parser_parse_definitely (parser))
10641 cp_parser_error (parser, "expected ptr-operator");
10642 }
10643
10644 return code;
10645}
10646
10647/* Parse an (optional) cv-qualifier-seq.
10648
10649 cv-qualifier-seq:
10650 cv-qualifier cv-qualifier-seq [opt]
10651
10652 Returns a TREE_LIST. The TREE_VALUE of each node is the
10653 representation of a cv-qualifier. */
10654
10655static tree
94edc4ab 10656cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10657{
10658 tree cv_qualifiers = NULL_TREE;
10659
10660 while (true)
10661 {
10662 tree cv_qualifier;
10663
10664 /* Look for the next cv-qualifier. */
10665 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10666 /* If we didn't find one, we're done. */
10667 if (!cv_qualifier)
10668 break;
10669
10670 /* Add this cv-qualifier to the list. */
10671 cv_qualifiers
10672 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10673 }
10674
10675 /* We built up the list in reverse order. */
10676 return nreverse (cv_qualifiers);
10677}
10678
10679/* Parse an (optional) cv-qualifier.
10680
10681 cv-qualifier:
10682 const
10683 volatile
10684
10685 GNU Extension:
10686
10687 cv-qualifier:
10688 __restrict__ */
10689
10690static tree
94edc4ab 10691cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10692{
10693 cp_token *token;
10694 tree cv_qualifier = NULL_TREE;
10695
10696 /* Peek at the next token. */
10697 token = cp_lexer_peek_token (parser->lexer);
10698 /* See if it's a cv-qualifier. */
10699 switch (token->keyword)
10700 {
10701 case RID_CONST:
10702 case RID_VOLATILE:
10703 case RID_RESTRICT:
10704 /* Save the value of the token. */
10705 cv_qualifier = token->value;
10706 /* Consume the token. */
10707 cp_lexer_consume_token (parser->lexer);
10708 break;
10709
10710 default:
10711 break;
10712 }
10713
10714 return cv_qualifier;
10715}
10716
10717/* Parse a declarator-id.
10718
10719 declarator-id:
10720 id-expression
10721 :: [opt] nested-name-specifier [opt] type-name
10722
10723 In the `id-expression' case, the value returned is as for
10724 cp_parser_id_expression if the id-expression was an unqualified-id.
10725 If the id-expression was a qualified-id, then a SCOPE_REF is
10726 returned. The first operand is the scope (either a NAMESPACE_DECL
10727 or TREE_TYPE), but the second is still just a representation of an
10728 unqualified-id. */
10729
10730static tree
94edc4ab 10731cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10732{
10733 tree id_expression;
10734
10735 /* The expression must be an id-expression. Assume that qualified
10736 names are the names of types so that:
10737
10738 template <class T>
10739 int S<T>::R::i = 3;
10740
10741 will work; we must treat `S<T>::R' as the name of a type.
10742 Similarly, assume that qualified names are templates, where
10743 required, so that:
10744
10745 template <class T>
10746 int S<T>::R<T>::i = 3;
10747
10748 will work, too. */
10749 id_expression = cp_parser_id_expression (parser,
10750 /*template_keyword_p=*/false,
10751 /*check_dependency_p=*/false,
f3c2dfc6
MM
10752 /*template_p=*/NULL,
10753 /*declarator_p=*/true);
a723baf1
MM
10754 /* If the name was qualified, create a SCOPE_REF to represent
10755 that. */
10756 if (parser->scope)
ec20aa6c
MM
10757 {
10758 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10759 parser->scope = NULL_TREE;
10760 }
a723baf1
MM
10761
10762 return id_expression;
10763}
10764
10765/* Parse a type-id.
10766
10767 type-id:
10768 type-specifier-seq abstract-declarator [opt]
10769
10770 Returns the TYPE specified. */
10771
10772static tree
94edc4ab 10773cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10774{
10775 tree type_specifier_seq;
10776 tree abstract_declarator;
10777
10778 /* Parse the type-specifier-seq. */
10779 type_specifier_seq
10780 = cp_parser_type_specifier_seq (parser);
10781 if (type_specifier_seq == error_mark_node)
10782 return error_mark_node;
10783
10784 /* There might or might not be an abstract declarator. */
10785 cp_parser_parse_tentatively (parser);
10786 /* Look for the declarator. */
10787 abstract_declarator
4bb8ca28
MM
10788 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10789 /*parenthesized_p=*/NULL);
a723baf1
MM
10790 /* Check to see if there really was a declarator. */
10791 if (!cp_parser_parse_definitely (parser))
10792 abstract_declarator = NULL_TREE;
10793
10794 return groktypename (build_tree_list (type_specifier_seq,
10795 abstract_declarator));
10796}
10797
10798/* Parse a type-specifier-seq.
10799
10800 type-specifier-seq:
10801 type-specifier type-specifier-seq [opt]
10802
10803 GNU extension:
10804
10805 type-specifier-seq:
10806 attributes type-specifier-seq [opt]
10807
10808 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10809 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10810
10811static tree
94edc4ab 10812cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10813{
10814 bool seen_type_specifier = false;
10815 tree type_specifier_seq = NULL_TREE;
10816
10817 /* Parse the type-specifiers and attributes. */
10818 while (true)
10819 {
10820 tree type_specifier;
10821
10822 /* Check for attributes first. */
10823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10824 {
10825 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10826 NULL_TREE,
10827 type_specifier_seq);
10828 continue;
10829 }
10830
10831 /* After the first type-specifier, others are optional. */
10832 if (seen_type_specifier)
10833 cp_parser_parse_tentatively (parser);
10834 /* Look for the type-specifier. */
10835 type_specifier = cp_parser_type_specifier (parser,
10836 CP_PARSER_FLAGS_NONE,
10837 /*is_friend=*/false,
10838 /*is_declaration=*/false,
10839 NULL,
10840 NULL);
10841 /* If the first type-specifier could not be found, this is not a
10842 type-specifier-seq at all. */
10843 if (!seen_type_specifier && type_specifier == error_mark_node)
10844 return error_mark_node;
10845 /* If subsequent type-specifiers could not be found, the
10846 type-specifier-seq is complete. */
10847 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10848 break;
10849
10850 /* Add the new type-specifier to the list. */
10851 type_specifier_seq
10852 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10853 seen_type_specifier = true;
10854 }
10855
10856 /* We built up the list in reverse order. */
10857 return nreverse (type_specifier_seq);
10858}
10859
10860/* Parse a parameter-declaration-clause.
10861
10862 parameter-declaration-clause:
10863 parameter-declaration-list [opt] ... [opt]
10864 parameter-declaration-list , ...
10865
10866 Returns a representation for the parameter declarations. Each node
10867 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10868 representation.) If the parameter-declaration-clause ends with an
10869 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10870 list. A return value of NULL_TREE indicates a
10871 parameter-declaration-clause consisting only of an ellipsis. */
10872
10873static tree
94edc4ab 10874cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10875{
10876 tree parameters;
10877 cp_token *token;
10878 bool ellipsis_p;
10879
10880 /* Peek at the next token. */
10881 token = cp_lexer_peek_token (parser->lexer);
10882 /* Check for trivial parameter-declaration-clauses. */
10883 if (token->type == CPP_ELLIPSIS)
10884 {
10885 /* Consume the `...' token. */
10886 cp_lexer_consume_token (parser->lexer);
10887 return NULL_TREE;
10888 }
10889 else if (token->type == CPP_CLOSE_PAREN)
10890 /* There are no parameters. */
c73aecdf
DE
10891 {
10892#ifndef NO_IMPLICIT_EXTERN_C
10893 if (in_system_header && current_class_type == NULL
10894 && current_lang_name == lang_name_c)
10895 return NULL_TREE;
10896 else
10897#endif
10898 return void_list_node;
10899 }
a723baf1
MM
10900 /* Check for `(void)', too, which is a special case. */
10901 else if (token->keyword == RID_VOID
10902 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10903 == CPP_CLOSE_PAREN))
10904 {
10905 /* Consume the `void' token. */
10906 cp_lexer_consume_token (parser->lexer);
10907 /* There are no parameters. */
10908 return void_list_node;
10909 }
10910
10911 /* Parse the parameter-declaration-list. */
10912 parameters = cp_parser_parameter_declaration_list (parser);
10913 /* If a parse error occurred while parsing the
10914 parameter-declaration-list, then the entire
10915 parameter-declaration-clause is erroneous. */
10916 if (parameters == error_mark_node)
10917 return error_mark_node;
10918
10919 /* Peek at the next token. */
10920 token = cp_lexer_peek_token (parser->lexer);
10921 /* If it's a `,', the clause should terminate with an ellipsis. */
10922 if (token->type == CPP_COMMA)
10923 {
10924 /* Consume the `,'. */
10925 cp_lexer_consume_token (parser->lexer);
10926 /* Expect an ellipsis. */
10927 ellipsis_p
10928 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10929 }
10930 /* It might also be `...' if the optional trailing `,' was
10931 omitted. */
10932 else if (token->type == CPP_ELLIPSIS)
10933 {
10934 /* Consume the `...' token. */
10935 cp_lexer_consume_token (parser->lexer);
10936 /* And remember that we saw it. */
10937 ellipsis_p = true;
10938 }
10939 else
10940 ellipsis_p = false;
10941
10942 /* Finish the parameter list. */
10943 return finish_parmlist (parameters, ellipsis_p);
10944}
10945
10946/* Parse a parameter-declaration-list.
10947
10948 parameter-declaration-list:
10949 parameter-declaration
10950 parameter-declaration-list , parameter-declaration
10951
10952 Returns a representation of the parameter-declaration-list, as for
10953 cp_parser_parameter_declaration_clause. However, the
10954 `void_list_node' is never appended to the list. */
10955
10956static tree
94edc4ab 10957cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10958{
10959 tree parameters = NULL_TREE;
10960
10961 /* Look for more parameters. */
10962 while (true)
10963 {
10964 tree parameter;
4bb8ca28 10965 bool parenthesized_p;
a723baf1
MM
10966 /* Parse the parameter. */
10967 parameter
4bb8ca28
MM
10968 = cp_parser_parameter_declaration (parser,
10969 /*template_parm_p=*/false,
10970 &parenthesized_p);
ec194454 10971
34cd5ae7 10972 /* If a parse error occurred parsing the parameter declaration,
a723baf1
MM
10973 then the entire parameter-declaration-list is erroneous. */
10974 if (parameter == error_mark_node)
10975 {
10976 parameters = error_mark_node;
10977 break;
10978 }
10979 /* Add the new parameter to the list. */
10980 TREE_CHAIN (parameter) = parameters;
10981 parameters = parameter;
10982
10983 /* Peek at the next token. */
10984 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10985 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10986 /* The parameter-declaration-list is complete. */
10987 break;
10988 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10989 {
10990 cp_token *token;
10991
10992 /* Peek at the next token. */
10993 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10994 /* If it's an ellipsis, then the list is complete. */
10995 if (token->type == CPP_ELLIPSIS)
10996 break;
10997 /* Otherwise, there must be more parameters. Consume the
10998 `,'. */
10999 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11000 /* When parsing something like:
11001
11002 int i(float f, double d)
11003
11004 we can tell after seeing the declaration for "f" that we
11005 are not looking at an initialization of a variable "i",
11006 but rather at the declaration of a function "i".
11007
11008 Due to the fact that the parsing of template arguments
11009 (as specified to a template-id) requires backtracking we
11010 cannot use this technique when inside a template argument
11011 list. */
11012 if (!parser->in_template_argument_list_p
11013 && cp_parser_parsing_tentatively (parser)
11014 && !cp_parser_committed_to_tentative_parse (parser)
11015 /* However, a parameter-declaration of the form
11016 "foat(f)" (which is a valid declaration of a
11017 parameter "f") can also be interpreted as an
11018 expression (the conversion of "f" to "float"). */
11019 && !parenthesized_p)
11020 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11021 }
11022 else
11023 {
11024 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
11025 if (!cp_parser_parsing_tentatively (parser)
11026 || cp_parser_committed_to_tentative_parse (parser))
11027 cp_parser_skip_to_closing_parenthesis (parser,
11028 /*recovering=*/true,
5c832178 11029 /*or_comma=*/false,
4bb8ca28 11030 /*consume_paren=*/false);
a723baf1
MM
11031 break;
11032 }
11033 }
11034
11035 /* We built up the list in reverse order; straighten it out now. */
11036 return nreverse (parameters);
11037}
11038
11039/* Parse a parameter declaration.
11040
11041 parameter-declaration:
11042 decl-specifier-seq declarator
11043 decl-specifier-seq declarator = assignment-expression
11044 decl-specifier-seq abstract-declarator [opt]
11045 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11046
ec194454
MM
11047 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11048 declares a template parameter. (In that case, a non-nested `>'
11049 token encountered during the parsing of the assignment-expression
11050 is not interpreted as a greater-than operator.)
a723baf1
MM
11051
11052 Returns a TREE_LIST representing the parameter-declaration. The
4bb8ca28
MM
11053 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11054 there is no default argument. The TREE_VALUE is a representation
11055 of the decl-specifier-seq and declarator. In particular, the
11056 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11057 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11058 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11059 the declarator is of the form "(p)". */
a723baf1
MM
11060
11061static tree
ec194454 11062cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11063 bool template_parm_p,
11064 bool *parenthesized_p)
a723baf1 11065{
560ad596 11066 int declares_class_or_enum;
ec194454 11067 bool greater_than_is_operator_p;
a723baf1
MM
11068 tree decl_specifiers;
11069 tree attributes;
11070 tree declarator;
11071 tree default_argument;
11072 tree parameter;
11073 cp_token *token;
11074 const char *saved_message;
11075
ec194454
MM
11076 /* In a template parameter, `>' is not an operator.
11077
11078 [temp.param]
11079
11080 When parsing a default template-argument for a non-type
11081 template-parameter, the first non-nested `>' is taken as the end
11082 of the template parameter-list rather than a greater-than
11083 operator. */
11084 greater_than_is_operator_p = !template_parm_p;
11085
a723baf1
MM
11086 /* Type definitions may not appear in parameter types. */
11087 saved_message = parser->type_definition_forbidden_message;
11088 parser->type_definition_forbidden_message
11089 = "types may not be defined in parameter types";
11090
11091 /* Parse the declaration-specifiers. */
11092 decl_specifiers
11093 = cp_parser_decl_specifier_seq (parser,
11094 CP_PARSER_FLAGS_NONE,
11095 &attributes,
11096 &declares_class_or_enum);
11097 /* If an error occurred, there's no reason to attempt to parse the
11098 rest of the declaration. */
11099 if (cp_parser_error_occurred (parser))
11100 {
11101 parser->type_definition_forbidden_message = saved_message;
11102 return error_mark_node;
11103 }
11104
11105 /* Peek at the next token. */
11106 token = cp_lexer_peek_token (parser->lexer);
11107 /* If the next token is a `)', `,', `=', `>', or `...', then there
11108 is no declarator. */
11109 if (token->type == CPP_CLOSE_PAREN
11110 || token->type == CPP_COMMA
11111 || token->type == CPP_EQ
11112 || token->type == CPP_ELLIPSIS
11113 || token->type == CPP_GREATER)
4bb8ca28
MM
11114 {
11115 declarator = NULL_TREE;
11116 if (parenthesized_p)
11117 *parenthesized_p = false;
11118 }
a723baf1
MM
11119 /* Otherwise, there should be a declarator. */
11120 else
11121 {
11122 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11123 parser->default_arg_ok_p = false;
11124
5c832178
MM
11125 /* After seeing a decl-specifier-seq, if the next token is not a
11126 "(", there is no possibility that the code is a valid
4f8163b1
MM
11127 expression. Therefore, if parsing tentatively, we commit at
11128 this point. */
5c832178 11129 if (!parser->in_template_argument_list_p
643aee72 11130 /* In an expression context, having seen:
4f8163b1 11131
a7324e75 11132 (int((char ...
4f8163b1
MM
11133
11134 we cannot be sure whether we are looking at a
a7324e75
MM
11135 function-type (taking a "char" as a parameter) or a cast
11136 of some object of type "char" to "int". */
4f8163b1 11137 && !parser->in_type_id_in_expr_p
5c832178
MM
11138 && cp_parser_parsing_tentatively (parser)
11139 && !cp_parser_committed_to_tentative_parse (parser)
11140 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11141 cp_parser_commit_to_tentative_parse (parser);
11142 /* Parse the declarator. */
a723baf1 11143 declarator = cp_parser_declarator (parser,
62b8a44e 11144 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
11145 /*ctor_dtor_or_conv_p=*/NULL,
11146 parenthesized_p);
a723baf1 11147 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
11148 /* After the declarator, allow more attributes. */
11149 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
11150 }
11151
62b8a44e 11152 /* The restriction on defining new types applies only to the type
a723baf1
MM
11153 of the parameter, not to the default argument. */
11154 parser->type_definition_forbidden_message = saved_message;
11155
11156 /* If the next token is `=', then process a default argument. */
11157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11158 {
11159 bool saved_greater_than_is_operator_p;
11160 /* Consume the `='. */
11161 cp_lexer_consume_token (parser->lexer);
11162
11163 /* If we are defining a class, then the tokens that make up the
11164 default argument must be saved and processed later. */
ec194454
MM
11165 if (!template_parm_p && at_class_scope_p ()
11166 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11167 {
11168 unsigned depth = 0;
11169
11170 /* Create a DEFAULT_ARG to represented the unparsed default
11171 argument. */
11172 default_argument = make_node (DEFAULT_ARG);
11173 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11174
11175 /* Add tokens until we have processed the entire default
11176 argument. */
11177 while (true)
11178 {
11179 bool done = false;
11180 cp_token *token;
11181
11182 /* Peek at the next token. */
11183 token = cp_lexer_peek_token (parser->lexer);
11184 /* What we do depends on what token we have. */
11185 switch (token->type)
11186 {
11187 /* In valid code, a default argument must be
11188 immediately followed by a `,' `)', or `...'. */
11189 case CPP_COMMA:
11190 case CPP_CLOSE_PAREN:
11191 case CPP_ELLIPSIS:
11192 /* If we run into a non-nested `;', `}', or `]',
11193 then the code is invalid -- but the default
11194 argument is certainly over. */
11195 case CPP_SEMICOLON:
11196 case CPP_CLOSE_BRACE:
11197 case CPP_CLOSE_SQUARE:
11198 if (depth == 0)
11199 done = true;
11200 /* Update DEPTH, if necessary. */
11201 else if (token->type == CPP_CLOSE_PAREN
11202 || token->type == CPP_CLOSE_BRACE
11203 || token->type == CPP_CLOSE_SQUARE)
11204 --depth;
11205 break;
11206
11207 case CPP_OPEN_PAREN:
11208 case CPP_OPEN_SQUARE:
11209 case CPP_OPEN_BRACE:
11210 ++depth;
11211 break;
11212
11213 case CPP_GREATER:
11214 /* If we see a non-nested `>', and `>' is not an
11215 operator, then it marks the end of the default
11216 argument. */
11217 if (!depth && !greater_than_is_operator_p)
11218 done = true;
11219 break;
11220
11221 /* If we run out of tokens, issue an error message. */
11222 case CPP_EOF:
11223 error ("file ends in default argument");
11224 done = true;
11225 break;
11226
11227 case CPP_NAME:
11228 case CPP_SCOPE:
11229 /* In these cases, we should look for template-ids.
11230 For example, if the default argument is
11231 `X<int, double>()', we need to do name lookup to
11232 figure out whether or not `X' is a template; if
34cd5ae7 11233 so, the `,' does not end the default argument.
a723baf1
MM
11234
11235 That is not yet done. */
11236 break;
11237
11238 default:
11239 break;
11240 }
11241
11242 /* If we've reached the end, stop. */
11243 if (done)
11244 break;
11245
11246 /* Add the token to the token block. */
11247 token = cp_lexer_consume_token (parser->lexer);
11248 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11249 token);
11250 }
11251 }
11252 /* Outside of a class definition, we can just parse the
11253 assignment-expression. */
11254 else
11255 {
11256 bool saved_local_variables_forbidden_p;
11257
11258 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11259 set correctly. */
11260 saved_greater_than_is_operator_p
11261 = parser->greater_than_is_operator_p;
11262 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11263 /* Local variable names (and the `this' keyword) may not
11264 appear in a default argument. */
11265 saved_local_variables_forbidden_p
11266 = parser->local_variables_forbidden_p;
11267 parser->local_variables_forbidden_p = true;
11268 /* Parse the assignment-expression. */
11269 default_argument = cp_parser_assignment_expression (parser);
11270 /* Restore saved state. */
11271 parser->greater_than_is_operator_p
11272 = saved_greater_than_is_operator_p;
11273 parser->local_variables_forbidden_p
11274 = saved_local_variables_forbidden_p;
11275 }
11276 if (!parser->default_arg_ok_p)
11277 {
c67d36d0
NS
11278 if (!flag_pedantic_errors)
11279 warning ("deprecated use of default argument for parameter of non-function");
11280 else
11281 {
11282 error ("default arguments are only permitted for function parameters");
11283 default_argument = NULL_TREE;
11284 }
a723baf1
MM
11285 }
11286 }
11287 else
11288 default_argument = NULL_TREE;
11289
11290 /* Create the representation of the parameter. */
11291 if (attributes)
11292 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11293 parameter = build_tree_list (default_argument,
11294 build_tree_list (decl_specifiers,
11295 declarator));
11296
11297 return parameter;
11298}
11299
a723baf1
MM
11300/* Parse a function-body.
11301
11302 function-body:
11303 compound_statement */
11304
11305static void
11306cp_parser_function_body (cp_parser *parser)
11307{
a5bcc582 11308 cp_parser_compound_statement (parser, false);
a723baf1
MM
11309}
11310
11311/* Parse a ctor-initializer-opt followed by a function-body. Return
11312 true if a ctor-initializer was present. */
11313
11314static bool
11315cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11316{
11317 tree body;
11318 bool ctor_initializer_p;
11319
11320 /* Begin the function body. */
11321 body = begin_function_body ();
11322 /* Parse the optional ctor-initializer. */
11323 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11324 /* Parse the function-body. */
11325 cp_parser_function_body (parser);
11326 /* Finish the function body. */
11327 finish_function_body (body);
11328
11329 return ctor_initializer_p;
11330}
11331
11332/* Parse an initializer.
11333
11334 initializer:
11335 = initializer-clause
11336 ( expression-list )
11337
11338 Returns a expression representing the initializer. If no
11339 initializer is present, NULL_TREE is returned.
11340
11341 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11342 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11343 set to FALSE if there is no initializer present. If there is an
11344 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11345 is set to true; otherwise it is set to false. */
a723baf1
MM
11346
11347static tree
39703eb9
MM
11348cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11349 bool* non_constant_p)
a723baf1
MM
11350{
11351 cp_token *token;
11352 tree init;
11353
11354 /* Peek at the next token. */
11355 token = cp_lexer_peek_token (parser->lexer);
11356
11357 /* Let our caller know whether or not this initializer was
11358 parenthesized. */
11359 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11360 /* Assume that the initializer is constant. */
11361 *non_constant_p = false;
a723baf1
MM
11362
11363 if (token->type == CPP_EQ)
11364 {
11365 /* Consume the `='. */
11366 cp_lexer_consume_token (parser->lexer);
11367 /* Parse the initializer-clause. */
39703eb9 11368 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11369 }
11370 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11371 init = cp_parser_parenthesized_expression_list (parser, false,
11372 non_constant_p);
a723baf1
MM
11373 else
11374 {
11375 /* Anything else is an error. */
11376 cp_parser_error (parser, "expected initializer");
11377 init = error_mark_node;
11378 }
11379
11380 return init;
11381}
11382
11383/* Parse an initializer-clause.
11384
11385 initializer-clause:
11386 assignment-expression
11387 { initializer-list , [opt] }
11388 { }
11389
11390 Returns an expression representing the initializer.
11391
11392 If the `assignment-expression' production is used the value
34cd5ae7 11393 returned is simply a representation for the expression.
a723baf1
MM
11394
11395 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11396 the elements of the initializer-list (or NULL_TREE, if the last
11397 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11398 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11399 trailing `,' was provided. NON_CONSTANT_P is as for
11400 cp_parser_initializer. */
a723baf1
MM
11401
11402static tree
39703eb9 11403cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11404{
11405 tree initializer;
11406
11407 /* If it is not a `{', then we are looking at an
11408 assignment-expression. */
11409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
39703eb9
MM
11410 initializer
11411 = cp_parser_constant_expression (parser,
11412 /*allow_non_constant_p=*/true,
11413 non_constant_p);
a723baf1
MM
11414 else
11415 {
11416 /* Consume the `{' token. */
11417 cp_lexer_consume_token (parser->lexer);
11418 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11419 initializer = make_node (CONSTRUCTOR);
11420 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11421 necessary, but check_initializer depends upon it, for
11422 now. */
11423 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11424 /* If it's not a `}', then there is a non-trivial initializer. */
11425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11426 {
11427 /* Parse the initializer list. */
11428 CONSTRUCTOR_ELTS (initializer)
39703eb9 11429 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
11430 /* A trailing `,' token is allowed. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11432 cp_lexer_consume_token (parser->lexer);
11433 }
a723baf1
MM
11434 /* Now, there should be a trailing `}'. */
11435 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11436 }
11437
11438 return initializer;
11439}
11440
11441/* Parse an initializer-list.
11442
11443 initializer-list:
11444 initializer-clause
11445 initializer-list , initializer-clause
11446
11447 GNU Extension:
11448
11449 initializer-list:
11450 identifier : initializer-clause
11451 initializer-list, identifier : initializer-clause
11452
11453 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11454 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
11455 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11456 as for cp_parser_initializer. */
a723baf1
MM
11457
11458static tree
39703eb9 11459cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11460{
11461 tree initializers = NULL_TREE;
11462
39703eb9
MM
11463 /* Assume all of the expressions are constant. */
11464 *non_constant_p = false;
11465
a723baf1
MM
11466 /* Parse the rest of the list. */
11467 while (true)
11468 {
11469 cp_token *token;
11470 tree identifier;
11471 tree initializer;
39703eb9
MM
11472 bool clause_non_constant_p;
11473
a723baf1
MM
11474 /* If the next token is an identifier and the following one is a
11475 colon, we are looking at the GNU designated-initializer
11476 syntax. */
11477 if (cp_parser_allow_gnu_extensions_p (parser)
11478 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11480 {
11481 /* Consume the identifier. */
11482 identifier = cp_lexer_consume_token (parser->lexer)->value;
11483 /* Consume the `:'. */
11484 cp_lexer_consume_token (parser->lexer);
11485 }
11486 else
11487 identifier = NULL_TREE;
11488
11489 /* Parse the initializer. */
39703eb9
MM
11490 initializer = cp_parser_initializer_clause (parser,
11491 &clause_non_constant_p);
11492 /* If any clause is non-constant, so is the entire initializer. */
11493 if (clause_non_constant_p)
11494 *non_constant_p = true;
a723baf1
MM
11495 /* Add it to the list. */
11496 initializers = tree_cons (identifier, initializer, initializers);
11497
11498 /* If the next token is not a comma, we have reached the end of
11499 the list. */
11500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11501 break;
11502
11503 /* Peek at the next token. */
11504 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11505 /* If the next token is a `}', then we're still done. An
11506 initializer-clause can have a trailing `,' after the
11507 initializer-list and before the closing `}'. */
11508 if (token->type == CPP_CLOSE_BRACE)
11509 break;
11510
11511 /* Consume the `,' token. */
11512 cp_lexer_consume_token (parser->lexer);
11513 }
11514
11515 /* The initializers were built up in reverse order, so we need to
11516 reverse them now. */
11517 return nreverse (initializers);
11518}
11519
11520/* Classes [gram.class] */
11521
11522/* Parse a class-name.
11523
11524 class-name:
11525 identifier
11526 template-id
11527
11528 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11529 to indicate that names looked up in dependent types should be
11530 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11531 keyword has been used to indicate that the name that appears next
11532 is a template. TYPE_P is true iff the next name should be treated
11533 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
11534 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11535 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11536 being defined in a class-head.
a723baf1
MM
11537
11538 Returns the TYPE_DECL representing the class. */
11539
11540static tree
11541cp_parser_class_name (cp_parser *parser,
11542 bool typename_keyword_p,
11543 bool template_keyword_p,
11544 bool type_p,
a723baf1 11545 bool check_dependency_p,
a668c6ad
MM
11546 bool class_head_p,
11547 bool is_declaration)
a723baf1
MM
11548{
11549 tree decl;
11550 tree scope;
11551 bool typename_p;
e5976695
MM
11552 cp_token *token;
11553
11554 /* All class-names start with an identifier. */
11555 token = cp_lexer_peek_token (parser->lexer);
11556 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11557 {
11558 cp_parser_error (parser, "expected class-name");
11559 return error_mark_node;
11560 }
11561
a723baf1
MM
11562 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11563 to a template-id, so we save it here. */
11564 scope = parser->scope;
3adee96c
KL
11565 if (scope == error_mark_node)
11566 return error_mark_node;
11567
a723baf1
MM
11568 /* Any name names a type if we're following the `typename' keyword
11569 in a qualified name where the enclosing scope is type-dependent. */
11570 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11571 && dependent_type_p (scope));
e5976695
MM
11572 /* Handle the common case (an identifier, but not a template-id)
11573 efficiently. */
11574 if (token->type == CPP_NAME
f4abade9 11575 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 11576 {
a723baf1
MM
11577 tree identifier;
11578
11579 /* Look for the identifier. */
11580 identifier = cp_parser_identifier (parser);
11581 /* If the next token isn't an identifier, we are certainly not
11582 looking at a class-name. */
11583 if (identifier == error_mark_node)
11584 decl = error_mark_node;
11585 /* If we know this is a type-name, there's no need to look it
11586 up. */
11587 else if (typename_p)
11588 decl = identifier;
11589 else
11590 {
11591 /* If the next token is a `::', then the name must be a type
11592 name.
11593
11594 [basic.lookup.qual]
11595
11596 During the lookup for a name preceding the :: scope
11597 resolution operator, object, function, and enumerator
11598 names are ignored. */
11599 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11600 type_p = true;
11601 /* Look up the name. */
11602 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 11603 type_p,
b0bc6e8e 11604 /*is_template=*/false,
eea9800f 11605 /*is_namespace=*/false,
a723baf1
MM
11606 check_dependency_p);
11607 }
11608 }
e5976695
MM
11609 else
11610 {
11611 /* Try a template-id. */
11612 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
11613 check_dependency_p,
11614 is_declaration);
e5976695
MM
11615 if (decl == error_mark_node)
11616 return error_mark_node;
11617 }
a723baf1
MM
11618
11619 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11620
11621 /* If this is a typename, create a TYPENAME_TYPE. */
11622 if (typename_p && decl != error_mark_node)
4bfb8bba
MM
11623 {
11624 decl = make_typename_type (scope, decl, /*complain=*/1);
11625 if (decl != error_mark_node)
11626 decl = TYPE_NAME (decl);
11627 }
a723baf1
MM
11628
11629 /* Check to see that it is really the name of a class. */
11630 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11631 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11632 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11633 /* Situations like this:
11634
11635 template <typename T> struct A {
11636 typename T::template X<int>::I i;
11637 };
11638
11639 are problematic. Is `T::template X<int>' a class-name? The
11640 standard does not seem to be definitive, but there is no other
11641 valid interpretation of the following `::'. Therefore, those
11642 names are considered class-names. */
78757caa 11643 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
11644 else if (decl == error_mark_node
11645 || TREE_CODE (decl) != TYPE_DECL
11646 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11647 {
11648 cp_parser_error (parser, "expected class-name");
11649 return error_mark_node;
11650 }
11651
11652 return decl;
11653}
11654
11655/* Parse a class-specifier.
11656
11657 class-specifier:
11658 class-head { member-specification [opt] }
11659
11660 Returns the TREE_TYPE representing the class. */
11661
11662static tree
94edc4ab 11663cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11664{
11665 cp_token *token;
11666 tree type;
11667 tree attributes = NULL_TREE;
11668 int has_trailing_semicolon;
11669 bool nested_name_specifier_p;
a723baf1
MM
11670 unsigned saved_num_template_parameter_lists;
11671
8d241e0b 11672 push_deferring_access_checks (dk_no_deferred);
cf22909c 11673
a723baf1
MM
11674 /* Parse the class-head. */
11675 type = cp_parser_class_head (parser,
cf22909c 11676 &nested_name_specifier_p);
a723baf1
MM
11677 /* If the class-head was a semantic disaster, skip the entire body
11678 of the class. */
11679 if (!type)
11680 {
11681 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11682 pop_deferring_access_checks ();
a723baf1
MM
11683 return error_mark_node;
11684 }
cf22909c 11685
a723baf1
MM
11686 /* Look for the `{'. */
11687 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11688 {
11689 pop_deferring_access_checks ();
11690 return error_mark_node;
11691 }
11692
a723baf1
MM
11693 /* Issue an error message if type-definitions are forbidden here. */
11694 cp_parser_check_type_definition (parser);
11695 /* Remember that we are defining one more class. */
11696 ++parser->num_classes_being_defined;
11697 /* Inside the class, surrounding template-parameter-lists do not
11698 apply. */
11699 saved_num_template_parameter_lists
11700 = parser->num_template_parameter_lists;
11701 parser->num_template_parameter_lists = 0;
78757caa 11702
a723baf1 11703 /* Start the class. */
eeb23c11
MM
11704 if (nested_name_specifier_p)
11705 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11706 type = begin_class_definition (type);
11707 if (type == error_mark_node)
9bcb9aae 11708 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
11709 cp_parser_skip_to_closing_brace (parser);
11710 else
11711 /* Parse the member-specification. */
11712 cp_parser_member_specification_opt (parser);
11713 /* Look for the trailing `}'. */
11714 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11715 /* We get better error messages by noticing a common problem: a
11716 missing trailing `;'. */
11717 token = cp_lexer_peek_token (parser->lexer);
11718 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11719 /* Look for attributes to apply to this class. */
11720 if (cp_parser_allow_gnu_extensions_p (parser))
11721 attributes = cp_parser_attributes_opt (parser);
560ad596
MM
11722 /* If we got any attributes in class_head, xref_tag will stick them in
11723 TREE_TYPE of the type. Grab them now. */
11724 if (type != error_mark_node)
11725 {
11726 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11727 TYPE_ATTRIBUTES (type) = NULL_TREE;
11728 type = finish_struct (type, attributes);
11729 }
11730 if (nested_name_specifier_p)
11731 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
11732 /* If this class is not itself within the scope of another class,
11733 then we need to parse the bodies of all of the queued function
11734 definitions. Note that the queued functions defined in a class
11735 are not always processed immediately following the
11736 class-specifier for that class. Consider:
11737
11738 struct A {
11739 struct B { void f() { sizeof (A); } };
11740 };
11741
11742 If `f' were processed before the processing of `A' were
11743 completed, there would be no way to compute the size of `A'.
11744 Note that the nesting we are interested in here is lexical --
11745 not the semantic nesting given by TYPE_CONTEXT. In particular,
11746 for:
11747
11748 struct A { struct B; };
11749 struct A::B { void f() { } };
11750
11751 there is no need to delay the parsing of `A::B::f'. */
11752 if (--parser->num_classes_being_defined == 0)
11753 {
8218bd34
MM
11754 tree queue_entry;
11755 tree fn;
a723baf1 11756
8218bd34
MM
11757 /* In a first pass, parse default arguments to the functions.
11758 Then, in a second pass, parse the bodies of the functions.
11759 This two-phased approach handles cases like:
11760
11761 struct S {
11762 void f() { g(); }
11763 void g(int i = 3);
11764 };
11765
11766 */
8db1028e
NS
11767 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11768 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11769 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11770 TREE_PURPOSE (parser->unparsed_functions_queues)
11771 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
11772 {
11773 fn = TREE_VALUE (queue_entry);
8218bd34
MM
11774 /* Make sure that any template parameters are in scope. */
11775 maybe_begin_member_template_processing (fn);
11776 /* If there are default arguments that have not yet been processed,
11777 take care of them now. */
11778 cp_parser_late_parsing_default_args (parser, fn);
11779 /* Remove any template parameters from the symbol table. */
11780 maybe_end_member_template_processing ();
11781 }
11782 /* Now parse the body of the functions. */
8db1028e
NS
11783 for (TREE_VALUE (parser->unparsed_functions_queues)
11784 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11785 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11786 TREE_VALUE (parser->unparsed_functions_queues)
11787 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 11788 {
a723baf1 11789 /* Figure out which function we need to process. */
a723baf1
MM
11790 fn = TREE_VALUE (queue_entry);
11791
4543ee47
ZD
11792 /* A hack to prevent garbage collection. */
11793 function_depth++;
11794
a723baf1
MM
11795 /* Parse the function. */
11796 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 11797 function_depth--;
a723baf1
MM
11798 }
11799
a723baf1
MM
11800 }
11801
11802 /* Put back any saved access checks. */
cf22909c 11803 pop_deferring_access_checks ();
a723baf1
MM
11804
11805 /* Restore the count of active template-parameter-lists. */
11806 parser->num_template_parameter_lists
11807 = saved_num_template_parameter_lists;
11808
11809 return type;
11810}
11811
11812/* Parse a class-head.
11813
11814 class-head:
11815 class-key identifier [opt] base-clause [opt]
11816 class-key nested-name-specifier identifier base-clause [opt]
11817 class-key nested-name-specifier [opt] template-id
11818 base-clause [opt]
11819
11820 GNU Extensions:
11821 class-key attributes identifier [opt] base-clause [opt]
11822 class-key attributes nested-name-specifier identifier base-clause [opt]
11823 class-key attributes nested-name-specifier [opt] template-id
11824 base-clause [opt]
11825
11826 Returns the TYPE of the indicated class. Sets
11827 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11828 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11829
11830 Returns NULL_TREE if the class-head is syntactically valid, but
11831 semantically invalid in a way that means we should skip the entire
11832 body of the class. */
11833
11834static tree
94edc4ab
NN
11835cp_parser_class_head (cp_parser* parser,
11836 bool* nested_name_specifier_p)
a723baf1
MM
11837{
11838 cp_token *token;
11839 tree nested_name_specifier;
11840 enum tag_types class_key;
11841 tree id = NULL_TREE;
11842 tree type = NULL_TREE;
11843 tree attributes;
11844 bool template_id_p = false;
11845 bool qualified_p = false;
11846 bool invalid_nested_name_p = false;
afb0918a 11847 bool invalid_explicit_specialization_p = false;
a723baf1
MM
11848 unsigned num_templates;
11849
11850 /* Assume no nested-name-specifier will be present. */
11851 *nested_name_specifier_p = false;
11852 /* Assume no template parameter lists will be used in defining the
11853 type. */
11854 num_templates = 0;
11855
11856 /* Look for the class-key. */
11857 class_key = cp_parser_class_key (parser);
11858 if (class_key == none_type)
11859 return error_mark_node;
11860
11861 /* Parse the attributes. */
11862 attributes = cp_parser_attributes_opt (parser);
11863
11864 /* If the next token is `::', that is invalid -- but sometimes
11865 people do try to write:
11866
11867 struct ::S {};
11868
11869 Handle this gracefully by accepting the extra qualifier, and then
11870 issuing an error about it later if this really is a
2050a1bb 11871 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11872 specifier, remain silent. */
11873 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11874 qualified_p = true;
11875
8d241e0b
KL
11876 push_deferring_access_checks (dk_no_check);
11877
a723baf1
MM
11878 /* Determine the name of the class. Begin by looking for an
11879 optional nested-name-specifier. */
11880 nested_name_specifier
11881 = cp_parser_nested_name_specifier_opt (parser,
11882 /*typename_keyword_p=*/false,
66d418e6 11883 /*check_dependency_p=*/false,
a668c6ad
MM
11884 /*type_p=*/false,
11885 /*is_declaration=*/false);
a723baf1
MM
11886 /* If there was a nested-name-specifier, then there *must* be an
11887 identifier. */
11888 if (nested_name_specifier)
11889 {
11890 /* Although the grammar says `identifier', it really means
11891 `class-name' or `template-name'. You are only allowed to
11892 define a class that has already been declared with this
11893 syntax.
11894
11895 The proposed resolution for Core Issue 180 says that whever
11896 you see `class T::X' you should treat `X' as a type-name.
11897
11898 It is OK to define an inaccessible class; for example:
11899
11900 class A { class B; };
11901 class A::B {};
11902
a723baf1
MM
11903 We do not know if we will see a class-name, or a
11904 template-name. We look for a class-name first, in case the
11905 class-name is a template-id; if we looked for the
11906 template-name first we would stop after the template-name. */
11907 cp_parser_parse_tentatively (parser);
11908 type = cp_parser_class_name (parser,
11909 /*typename_keyword_p=*/false,
11910 /*template_keyword_p=*/false,
11911 /*type_p=*/true,
a723baf1 11912 /*check_dependency_p=*/false,
a668c6ad
MM
11913 /*class_head_p=*/true,
11914 /*is_declaration=*/false);
a723baf1
MM
11915 /* If that didn't work, ignore the nested-name-specifier. */
11916 if (!cp_parser_parse_definitely (parser))
11917 {
11918 invalid_nested_name_p = true;
11919 id = cp_parser_identifier (parser);
11920 if (id == error_mark_node)
11921 id = NULL_TREE;
11922 }
11923 /* If we could not find a corresponding TYPE, treat this
11924 declaration like an unqualified declaration. */
11925 if (type == error_mark_node)
11926 nested_name_specifier = NULL_TREE;
11927 /* Otherwise, count the number of templates used in TYPE and its
11928 containing scopes. */
11929 else
11930 {
11931 tree scope;
11932
11933 for (scope = TREE_TYPE (type);
11934 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11935 scope = (TYPE_P (scope)
11936 ? TYPE_CONTEXT (scope)
11937 : DECL_CONTEXT (scope)))
11938 if (TYPE_P (scope)
11939 && CLASS_TYPE_P (scope)
11940 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11941 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11943 ++num_templates;
11944 }
11945 }
11946 /* Otherwise, the identifier is optional. */
11947 else
11948 {
11949 /* We don't know whether what comes next is a template-id,
11950 an identifier, or nothing at all. */
11951 cp_parser_parse_tentatively (parser);
11952 /* Check for a template-id. */
11953 id = cp_parser_template_id (parser,
11954 /*template_keyword_p=*/false,
a668c6ad
MM
11955 /*check_dependency_p=*/true,
11956 /*is_declaration=*/true);
a723baf1
MM
11957 /* If that didn't work, it could still be an identifier. */
11958 if (!cp_parser_parse_definitely (parser))
11959 {
11960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11961 id = cp_parser_identifier (parser);
11962 else
11963 id = NULL_TREE;
11964 }
11965 else
11966 {
11967 template_id_p = true;
11968 ++num_templates;
11969 }
11970 }
11971
8d241e0b
KL
11972 pop_deferring_access_checks ();
11973
ee43dab5
MM
11974 cp_parser_check_for_invalid_template_id (parser, id);
11975
a723baf1
MM
11976 /* If it's not a `:' or a `{' then we can't really be looking at a
11977 class-head, since a class-head only appears as part of a
11978 class-specifier. We have to detect this situation before calling
11979 xref_tag, since that has irreversible side-effects. */
11980 if (!cp_parser_next_token_starts_class_definition_p (parser))
11981 {
11982 cp_parser_error (parser, "expected `{' or `:'");
11983 return error_mark_node;
11984 }
11985
11986 /* At this point, we're going ahead with the class-specifier, even
11987 if some other problem occurs. */
11988 cp_parser_commit_to_tentative_parse (parser);
11989 /* Issue the error about the overly-qualified name now. */
11990 if (qualified_p)
11991 cp_parser_error (parser,
11992 "global qualification of class name is invalid");
11993 else if (invalid_nested_name_p)
11994 cp_parser_error (parser,
11995 "qualified name does not name a class");
88081599
MM
11996 else if (nested_name_specifier)
11997 {
11998 tree scope;
11999 /* Figure out in what scope the declaration is being placed. */
12000 scope = current_scope ();
12001 if (!scope)
12002 scope = current_namespace;
12003 /* If that scope does not contain the scope in which the
12004 class was originally declared, the program is invalid. */
12005 if (scope && !is_ancestor (scope, nested_name_specifier))
12006 {
12007 error ("declaration of `%D' in `%D' which does not "
12008 "enclose `%D'", type, scope, nested_name_specifier);
12009 type = NULL_TREE;
12010 goto done;
12011 }
12012 /* [dcl.meaning]
12013
12014 A declarator-id shall not be qualified exception of the
12015 definition of a ... nested class outside of its class
12016 ... [or] a the definition or explicit instantiation of a
12017 class member of a namespace outside of its namespace. */
12018 if (scope == nested_name_specifier)
12019 {
12020 pedwarn ("extra qualification ignored");
12021 nested_name_specifier = NULL_TREE;
12022 num_templates = 0;
12023 }
12024 }
afb0918a
MM
12025 /* An explicit-specialization must be preceded by "template <>". If
12026 it is not, try to recover gracefully. */
12027 if (at_namespace_scope_p ()
12028 && parser->num_template_parameter_lists == 0
eeb23c11 12029 && template_id_p)
afb0918a
MM
12030 {
12031 error ("an explicit specialization must be preceded by 'template <>'");
12032 invalid_explicit_specialization_p = true;
12033 /* Take the same action that would have been taken by
12034 cp_parser_explicit_specialization. */
12035 ++parser->num_template_parameter_lists;
12036 begin_specialization ();
12037 }
12038 /* There must be no "return" statements between this point and the
12039 end of this function; set "type "to the correct return value and
12040 use "goto done;" to return. */
a723baf1
MM
12041 /* Make sure that the right number of template parameters were
12042 present. */
12043 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12044 {
12045 /* If something went wrong, there is no point in even trying to
12046 process the class-definition. */
12047 type = NULL_TREE;
12048 goto done;
12049 }
a723baf1 12050
a723baf1
MM
12051 /* Look up the type. */
12052 if (template_id_p)
12053 {
12054 type = TREE_TYPE (id);
12055 maybe_process_partial_specialization (type);
12056 }
12057 else if (!nested_name_specifier)
12058 {
12059 /* If the class was unnamed, create a dummy name. */
12060 if (!id)
12061 id = make_anon_name ();
cbd63935
KL
12062 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
12063 parser->num_template_parameter_lists);
a723baf1
MM
12064 }
12065 else
12066 {
a723baf1
MM
12067 tree class_type;
12068
12069 /* Given:
12070
12071 template <typename T> struct S { struct T };
14d22dd6 12072 template <typename T> struct S<T>::T { };
a723baf1
MM
12073
12074 we will get a TYPENAME_TYPE when processing the definition of
12075 `S::T'. We need to resolve it to the actual type before we
12076 try to define it. */
12077 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12078 {
14d22dd6
MM
12079 class_type = resolve_typename_type (TREE_TYPE (type),
12080 /*only_current_p=*/false);
12081 if (class_type != error_mark_node)
12082 type = TYPE_NAME (class_type);
12083 else
12084 {
12085 cp_parser_error (parser, "could not resolve typename type");
12086 type = error_mark_node;
12087 }
a723baf1
MM
12088 }
12089
560ad596
MM
12090 maybe_process_partial_specialization (TREE_TYPE (type));
12091 class_type = current_class_type;
12092 /* Enter the scope indicated by the nested-name-specifier. */
12093 if (nested_name_specifier)
12094 push_scope (nested_name_specifier);
12095 /* Get the canonical version of this type. */
12096 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12097 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12098 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12099 type = push_template_decl (type);
12100 type = TREE_TYPE (type);
12101 if (nested_name_specifier)
eeb23c11
MM
12102 {
12103 *nested_name_specifier_p = true;
12104 pop_scope (nested_name_specifier);
12105 }
a723baf1
MM
12106 }
12107 /* Indicate whether this class was declared as a `class' or as a
12108 `struct'. */
12109 if (TREE_CODE (type) == RECORD_TYPE)
12110 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12111 cp_parser_check_class_key (class_key, type);
12112
12113 /* Enter the scope containing the class; the names of base classes
12114 should be looked up in that context. For example, given:
12115
12116 struct A { struct B {}; struct C; };
12117 struct A::C : B {};
12118
12119 is valid. */
12120 if (nested_name_specifier)
12121 push_scope (nested_name_specifier);
12122 /* Now, look for the base-clause. */
12123 token = cp_lexer_peek_token (parser->lexer);
12124 if (token->type == CPP_COLON)
12125 {
12126 tree bases;
12127
12128 /* Get the list of base-classes. */
12129 bases = cp_parser_base_clause (parser);
12130 /* Process them. */
12131 xref_basetypes (type, bases);
12132 }
12133 /* Leave the scope given by the nested-name-specifier. We will
12134 enter the class scope itself while processing the members. */
12135 if (nested_name_specifier)
12136 pop_scope (nested_name_specifier);
12137
afb0918a
MM
12138 done:
12139 if (invalid_explicit_specialization_p)
12140 {
12141 end_specialization ();
12142 --parser->num_template_parameter_lists;
12143 }
a723baf1
MM
12144 return type;
12145}
12146
12147/* Parse a class-key.
12148
12149 class-key:
12150 class
12151 struct
12152 union
12153
12154 Returns the kind of class-key specified, or none_type to indicate
12155 error. */
12156
12157static enum tag_types
94edc4ab 12158cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12159{
12160 cp_token *token;
12161 enum tag_types tag_type;
12162
12163 /* Look for the class-key. */
12164 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12165 if (!token)
12166 return none_type;
12167
12168 /* Check to see if the TOKEN is a class-key. */
12169 tag_type = cp_parser_token_is_class_key (token);
12170 if (!tag_type)
12171 cp_parser_error (parser, "expected class-key");
12172 return tag_type;
12173}
12174
12175/* Parse an (optional) member-specification.
12176
12177 member-specification:
12178 member-declaration member-specification [opt]
12179 access-specifier : member-specification [opt] */
12180
12181static void
94edc4ab 12182cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12183{
12184 while (true)
12185 {
12186 cp_token *token;
12187 enum rid keyword;
12188
12189 /* Peek at the next token. */
12190 token = cp_lexer_peek_token (parser->lexer);
12191 /* If it's a `}', or EOF then we've seen all the members. */
12192 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12193 break;
12194
12195 /* See if this token is a keyword. */
12196 keyword = token->keyword;
12197 switch (keyword)
12198 {
12199 case RID_PUBLIC:
12200 case RID_PROTECTED:
12201 case RID_PRIVATE:
12202 /* Consume the access-specifier. */
12203 cp_lexer_consume_token (parser->lexer);
12204 /* Remember which access-specifier is active. */
12205 current_access_specifier = token->value;
12206 /* Look for the `:'. */
12207 cp_parser_require (parser, CPP_COLON, "`:'");
12208 break;
12209
12210 default:
12211 /* Otherwise, the next construction must be a
12212 member-declaration. */
12213 cp_parser_member_declaration (parser);
a723baf1
MM
12214 }
12215 }
12216}
12217
12218/* Parse a member-declaration.
12219
12220 member-declaration:
12221 decl-specifier-seq [opt] member-declarator-list [opt] ;
12222 function-definition ; [opt]
12223 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12224 using-declaration
12225 template-declaration
12226
12227 member-declarator-list:
12228 member-declarator
12229 member-declarator-list , member-declarator
12230
12231 member-declarator:
12232 declarator pure-specifier [opt]
12233 declarator constant-initializer [opt]
12234 identifier [opt] : constant-expression
12235
12236 GNU Extensions:
12237
12238 member-declaration:
12239 __extension__ member-declaration
12240
12241 member-declarator:
12242 declarator attributes [opt] pure-specifier [opt]
12243 declarator attributes [opt] constant-initializer [opt]
12244 identifier [opt] attributes [opt] : constant-expression */
12245
12246static void
94edc4ab 12247cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
12248{
12249 tree decl_specifiers;
12250 tree prefix_attributes;
12251 tree decl;
560ad596 12252 int declares_class_or_enum;
a723baf1
MM
12253 bool friend_p;
12254 cp_token *token;
12255 int saved_pedantic;
12256
12257 /* Check for the `__extension__' keyword. */
12258 if (cp_parser_extension_opt (parser, &saved_pedantic))
12259 {
12260 /* Recurse. */
12261 cp_parser_member_declaration (parser);
12262 /* Restore the old value of the PEDANTIC flag. */
12263 pedantic = saved_pedantic;
12264
12265 return;
12266 }
12267
12268 /* Check for a template-declaration. */
12269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12270 {
12271 /* Parse the template-declaration. */
12272 cp_parser_template_declaration (parser, /*member_p=*/true);
12273
12274 return;
12275 }
12276
12277 /* Check for a using-declaration. */
12278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12279 {
12280 /* Parse the using-declaration. */
12281 cp_parser_using_declaration (parser);
12282
12283 return;
12284 }
12285
a723baf1
MM
12286 /* Parse the decl-specifier-seq. */
12287 decl_specifiers
12288 = cp_parser_decl_specifier_seq (parser,
12289 CP_PARSER_FLAGS_OPTIONAL,
12290 &prefix_attributes,
12291 &declares_class_or_enum);
8fbc5ae7
MM
12292 /* Check for an invalid type-name. */
12293 if (cp_parser_diagnose_invalid_type_name (parser))
12294 return;
a723baf1
MM
12295 /* If there is no declarator, then the decl-specifier-seq should
12296 specify a type. */
12297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12298 {
12299 /* If there was no decl-specifier-seq, and the next token is a
12300 `;', then we have something like:
12301
12302 struct S { ; };
12303
12304 [class.mem]
12305
12306 Each member-declaration shall declare at least one member
12307 name of the class. */
12308 if (!decl_specifiers)
12309 {
12310 if (pedantic)
12311 pedwarn ("extra semicolon");
12312 }
12313 else
12314 {
12315 tree type;
12316
12317 /* See if this declaration is a friend. */
12318 friend_p = cp_parser_friend_p (decl_specifiers);
12319 /* If there were decl-specifiers, check to see if there was
12320 a class-declaration. */
12321 type = check_tag_decl (decl_specifiers);
12322 /* Nested classes have already been added to the class, but
12323 a `friend' needs to be explicitly registered. */
12324 if (friend_p)
12325 {
12326 /* If the `friend' keyword was present, the friend must
12327 be introduced with a class-key. */
12328 if (!declares_class_or_enum)
12329 error ("a class-key must be used when declaring a friend");
12330 /* In this case:
12331
12332 template <typename T> struct A {
12333 friend struct A<T>::B;
12334 };
12335
12336 A<T>::B will be represented by a TYPENAME_TYPE, and
12337 therefore not recognized by check_tag_decl. */
12338 if (!type)
12339 {
12340 tree specifier;
12341
12342 for (specifier = decl_specifiers;
12343 specifier;
12344 specifier = TREE_CHAIN (specifier))
12345 {
12346 tree s = TREE_VALUE (specifier);
12347
c003e212
GDR
12348 if (TREE_CODE (s) == IDENTIFIER_NODE)
12349 get_global_value_if_present (s, &type);
a723baf1
MM
12350 if (TREE_CODE (s) == TYPE_DECL)
12351 s = TREE_TYPE (s);
12352 if (TYPE_P (s))
12353 {
12354 type = s;
12355 break;
12356 }
12357 }
12358 }
fdd09134 12359 if (!type || !TYPE_P (type))
a723baf1
MM
12360 error ("friend declaration does not name a class or "
12361 "function");
12362 else
19db77ce
KL
12363 make_friend_class (current_class_type, type,
12364 /*complain=*/true);
a723baf1
MM
12365 }
12366 /* If there is no TYPE, an error message will already have
12367 been issued. */
12368 else if (!type)
12369 ;
12370 /* An anonymous aggregate has to be handled specially; such
12371 a declaration really declares a data member (with a
12372 particular type), as opposed to a nested class. */
12373 else if (ANON_AGGR_TYPE_P (type))
12374 {
12375 /* Remove constructors and such from TYPE, now that we
34cd5ae7 12376 know it is an anonymous aggregate. */
a723baf1
MM
12377 fixup_anonymous_aggr (type);
12378 /* And make the corresponding data member. */
12379 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12380 /* Add it to the class. */
12381 finish_member_declaration (decl);
12382 }
37d407a1
KL
12383 else
12384 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
12385 }
12386 }
12387 else
12388 {
12389 /* See if these declarations will be friends. */
12390 friend_p = cp_parser_friend_p (decl_specifiers);
12391
12392 /* Keep going until we hit the `;' at the end of the
12393 declaration. */
12394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12395 {
12396 tree attributes = NULL_TREE;
12397 tree first_attribute;
12398
12399 /* Peek at the next token. */
12400 token = cp_lexer_peek_token (parser->lexer);
12401
12402 /* Check for a bitfield declaration. */
12403 if (token->type == CPP_COLON
12404 || (token->type == CPP_NAME
12405 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12406 == CPP_COLON))
12407 {
12408 tree identifier;
12409 tree width;
12410
12411 /* Get the name of the bitfield. Note that we cannot just
12412 check TOKEN here because it may have been invalidated by
12413 the call to cp_lexer_peek_nth_token above. */
12414 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12415 identifier = cp_parser_identifier (parser);
12416 else
12417 identifier = NULL_TREE;
12418
12419 /* Consume the `:' token. */
12420 cp_lexer_consume_token (parser->lexer);
12421 /* Get the width of the bitfield. */
14d22dd6
MM
12422 width
12423 = cp_parser_constant_expression (parser,
12424 /*allow_non_constant=*/false,
12425 NULL);
a723baf1
MM
12426
12427 /* Look for attributes that apply to the bitfield. */
12428 attributes = cp_parser_attributes_opt (parser);
12429 /* Remember which attributes are prefix attributes and
12430 which are not. */
12431 first_attribute = attributes;
12432 /* Combine the attributes. */
12433 attributes = chainon (prefix_attributes, attributes);
12434
12435 /* Create the bitfield declaration. */
12436 decl = grokbitfield (identifier,
12437 decl_specifiers,
12438 width);
12439 /* Apply the attributes. */
12440 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12441 }
12442 else
12443 {
12444 tree declarator;
12445 tree initializer;
12446 tree asm_specification;
7efa3e22 12447 int ctor_dtor_or_conv_p;
a723baf1
MM
12448
12449 /* Parse the declarator. */
12450 declarator
62b8a44e 12451 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
12452 &ctor_dtor_or_conv_p,
12453 /*parenthesized_p=*/NULL);
a723baf1
MM
12454
12455 /* If something went wrong parsing the declarator, make sure
12456 that we at least consume some tokens. */
12457 if (declarator == error_mark_node)
12458 {
12459 /* Skip to the end of the statement. */
12460 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
12461 /* If the next token is not a semicolon, that is
12462 probably because we just skipped over the body of
12463 a function. So, we consume a semicolon if
12464 present, but do not issue an error message if it
12465 is not present. */
12466 if (cp_lexer_next_token_is (parser->lexer,
12467 CPP_SEMICOLON))
12468 cp_lexer_consume_token (parser->lexer);
12469 return;
a723baf1
MM
12470 }
12471
560ad596
MM
12472 cp_parser_check_for_definition_in_return_type
12473 (declarator, declares_class_or_enum);
12474
a723baf1
MM
12475 /* Look for an asm-specification. */
12476 asm_specification = cp_parser_asm_specification_opt (parser);
12477 /* Look for attributes that apply to the declaration. */
12478 attributes = cp_parser_attributes_opt (parser);
12479 /* Remember which attributes are prefix attributes and
12480 which are not. */
12481 first_attribute = attributes;
12482 /* Combine the attributes. */
12483 attributes = chainon (prefix_attributes, attributes);
12484
12485 /* If it's an `=', then we have a constant-initializer or a
12486 pure-specifier. It is not correct to parse the
12487 initializer before registering the member declaration
12488 since the member declaration should be in scope while
12489 its initializer is processed. However, the rest of the
12490 front end does not yet provide an interface that allows
12491 us to handle this correctly. */
12492 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12493 {
12494 /* In [class.mem]:
12495
12496 A pure-specifier shall be used only in the declaration of
12497 a virtual function.
12498
12499 A member-declarator can contain a constant-initializer
12500 only if it declares a static member of integral or
12501 enumeration type.
12502
12503 Therefore, if the DECLARATOR is for a function, we look
12504 for a pure-specifier; otherwise, we look for a
12505 constant-initializer. When we call `grokfield', it will
12506 perform more stringent semantics checks. */
12507 if (TREE_CODE (declarator) == CALL_EXPR)
12508 initializer = cp_parser_pure_specifier (parser);
12509 else
4bb8ca28
MM
12510 /* Parse the initializer. */
12511 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
12512 }
12513 /* Otherwise, there is no initializer. */
12514 else
12515 initializer = NULL_TREE;
12516
12517 /* See if we are probably looking at a function
12518 definition. We are certainly not looking at at a
12519 member-declarator. Calling `grokfield' has
12520 side-effects, so we must not do it unless we are sure
12521 that we are looking at a member-declarator. */
12522 if (cp_parser_token_starts_function_definition_p
12523 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
12524 {
12525 /* The grammar does not allow a pure-specifier to be
12526 used when a member function is defined. (It is
12527 possible that this fact is an oversight in the
12528 standard, since a pure function may be defined
12529 outside of the class-specifier. */
12530 if (initializer)
12531 error ("pure-specifier on function-definition");
12532 decl = cp_parser_save_member_function_body (parser,
12533 decl_specifiers,
12534 declarator,
12535 attributes);
12536 /* If the member was not a friend, declare it here. */
12537 if (!friend_p)
12538 finish_member_declaration (decl);
12539 /* Peek at the next token. */
12540 token = cp_lexer_peek_token (parser->lexer);
12541 /* If the next token is a semicolon, consume it. */
12542 if (token->type == CPP_SEMICOLON)
12543 cp_lexer_consume_token (parser->lexer);
12544 return;
12545 }
a723baf1 12546 else
39703eb9
MM
12547 {
12548 /* Create the declaration. */
ee3071ef
NS
12549 decl = grokfield (declarator, decl_specifiers,
12550 initializer, asm_specification,
39703eb9
MM
12551 attributes);
12552 /* Any initialization must have been from a
12553 constant-expression. */
12554 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12555 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12556 }
a723baf1
MM
12557 }
12558
12559 /* Reset PREFIX_ATTRIBUTES. */
12560 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12561 attributes = TREE_CHAIN (attributes);
12562 if (attributes)
12563 TREE_CHAIN (attributes) = NULL_TREE;
12564
12565 /* If there is any qualification still in effect, clear it
12566 now; we will be starting fresh with the next declarator. */
12567 parser->scope = NULL_TREE;
12568 parser->qualifying_scope = NULL_TREE;
12569 parser->object_scope = NULL_TREE;
12570 /* If it's a `,', then there are more declarators. */
12571 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12572 cp_lexer_consume_token (parser->lexer);
12573 /* If the next token isn't a `;', then we have a parse error. */
12574 else if (cp_lexer_next_token_is_not (parser->lexer,
12575 CPP_SEMICOLON))
12576 {
12577 cp_parser_error (parser, "expected `;'");
04c06002 12578 /* Skip tokens until we find a `;'. */
a723baf1
MM
12579 cp_parser_skip_to_end_of_statement (parser);
12580
12581 break;
12582 }
12583
12584 if (decl)
12585 {
12586 /* Add DECL to the list of members. */
12587 if (!friend_p)
12588 finish_member_declaration (decl);
12589
a723baf1 12590 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 12591 cp_parser_save_default_args (parser, decl);
a723baf1
MM
12592 }
12593 }
12594 }
12595
4bb8ca28 12596 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
12597}
12598
12599/* Parse a pure-specifier.
12600
12601 pure-specifier:
12602 = 0
12603
12604 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 12605 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
12606
12607static tree
94edc4ab 12608cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12609{
12610 cp_token *token;
12611
12612 /* Look for the `=' token. */
12613 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12614 return error_mark_node;
12615 /* Look for the `0' token. */
12616 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12617 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12618 to get information from the lexer about how the number was
12619 spelled in order to fix this problem. */
12620 if (!token || !integer_zerop (token->value))
12621 return error_mark_node;
12622
12623 return integer_zero_node;
12624}
12625
12626/* Parse a constant-initializer.
12627
12628 constant-initializer:
12629 = constant-expression
12630
12631 Returns a representation of the constant-expression. */
12632
12633static tree
94edc4ab 12634cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12635{
12636 /* Look for the `=' token. */
12637 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12638 return error_mark_node;
12639
12640 /* It is invalid to write:
12641
12642 struct S { static const int i = { 7 }; };
12643
12644 */
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12646 {
12647 cp_parser_error (parser,
12648 "a brace-enclosed initializer is not allowed here");
12649 /* Consume the opening brace. */
12650 cp_lexer_consume_token (parser->lexer);
12651 /* Skip the initializer. */
12652 cp_parser_skip_to_closing_brace (parser);
12653 /* Look for the trailing `}'. */
12654 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12655
12656 return error_mark_node;
12657 }
12658
14d22dd6
MM
12659 return cp_parser_constant_expression (parser,
12660 /*allow_non_constant=*/false,
12661 NULL);
a723baf1
MM
12662}
12663
12664/* Derived classes [gram.class.derived] */
12665
12666/* Parse a base-clause.
12667
12668 base-clause:
12669 : base-specifier-list
12670
12671 base-specifier-list:
12672 base-specifier
12673 base-specifier-list , base-specifier
12674
12675 Returns a TREE_LIST representing the base-classes, in the order in
12676 which they were declared. The representation of each node is as
12677 described by cp_parser_base_specifier.
12678
12679 In the case that no bases are specified, this function will return
12680 NULL_TREE, not ERROR_MARK_NODE. */
12681
12682static tree
94edc4ab 12683cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12684{
12685 tree bases = NULL_TREE;
12686
12687 /* Look for the `:' that begins the list. */
12688 cp_parser_require (parser, CPP_COLON, "`:'");
12689
12690 /* Scan the base-specifier-list. */
12691 while (true)
12692 {
12693 cp_token *token;
12694 tree base;
12695
12696 /* Look for the base-specifier. */
12697 base = cp_parser_base_specifier (parser);
12698 /* Add BASE to the front of the list. */
12699 if (base != error_mark_node)
12700 {
12701 TREE_CHAIN (base) = bases;
12702 bases = base;
12703 }
12704 /* Peek at the next token. */
12705 token = cp_lexer_peek_token (parser->lexer);
12706 /* If it's not a comma, then the list is complete. */
12707 if (token->type != CPP_COMMA)
12708 break;
12709 /* Consume the `,'. */
12710 cp_lexer_consume_token (parser->lexer);
12711 }
12712
12713 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12714 base class had a qualified name. However, the next name that
12715 appears is certainly not qualified. */
12716 parser->scope = NULL_TREE;
12717 parser->qualifying_scope = NULL_TREE;
12718 parser->object_scope = NULL_TREE;
12719
12720 return nreverse (bases);
12721}
12722
12723/* Parse a base-specifier.
12724
12725 base-specifier:
12726 :: [opt] nested-name-specifier [opt] class-name
12727 virtual access-specifier [opt] :: [opt] nested-name-specifier
12728 [opt] class-name
12729 access-specifier virtual [opt] :: [opt] nested-name-specifier
12730 [opt] class-name
12731
12732 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12733 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12734 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12735 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12736
12737static tree
94edc4ab 12738cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12739{
12740 cp_token *token;
12741 bool done = false;
12742 bool virtual_p = false;
12743 bool duplicate_virtual_error_issued_p = false;
12744 bool duplicate_access_error_issued_p = false;
bbaab916 12745 bool class_scope_p, template_p;
dbbf88d1 12746 tree access = access_default_node;
a723baf1
MM
12747 tree type;
12748
12749 /* Process the optional `virtual' and `access-specifier'. */
12750 while (!done)
12751 {
12752 /* Peek at the next token. */
12753 token = cp_lexer_peek_token (parser->lexer);
12754 /* Process `virtual'. */
12755 switch (token->keyword)
12756 {
12757 case RID_VIRTUAL:
12758 /* If `virtual' appears more than once, issue an error. */
12759 if (virtual_p && !duplicate_virtual_error_issued_p)
12760 {
12761 cp_parser_error (parser,
12762 "`virtual' specified more than once in base-specified");
12763 duplicate_virtual_error_issued_p = true;
12764 }
12765
12766 virtual_p = true;
12767
12768 /* Consume the `virtual' token. */
12769 cp_lexer_consume_token (parser->lexer);
12770
12771 break;
12772
12773 case RID_PUBLIC:
12774 case RID_PROTECTED:
12775 case RID_PRIVATE:
12776 /* If more than one access specifier appears, issue an
12777 error. */
dbbf88d1
NS
12778 if (access != access_default_node
12779 && !duplicate_access_error_issued_p)
a723baf1
MM
12780 {
12781 cp_parser_error (parser,
12782 "more than one access specifier in base-specified");
12783 duplicate_access_error_issued_p = true;
12784 }
12785
dbbf88d1 12786 access = ridpointers[(int) token->keyword];
a723baf1
MM
12787
12788 /* Consume the access-specifier. */
12789 cp_lexer_consume_token (parser->lexer);
12790
12791 break;
12792
12793 default:
12794 done = true;
12795 break;
12796 }
12797 }
852dcbdd 12798 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 12799 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
12800 as base classes. */
12801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12802 {
12803 if (!processing_template_decl)
12804 error ("keyword `typename' not allowed outside of templates");
12805 else
12806 error ("keyword `typename' not allowed in this context "
12807 "(the base class is implicitly a type)");
12808 cp_lexer_consume_token (parser->lexer);
12809 }
a723baf1 12810
a723baf1
MM
12811 /* Look for the optional `::' operator. */
12812 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12813 /* Look for the nested-name-specifier. The simplest way to
12814 implement:
12815
12816 [temp.res]
12817
12818 The keyword `typename' is not permitted in a base-specifier or
12819 mem-initializer; in these contexts a qualified name that
12820 depends on a template-parameter is implicitly assumed to be a
12821 type name.
12822
12823 is to pretend that we have seen the `typename' keyword at this
12824 point. */
12825 cp_parser_nested_name_specifier_opt (parser,
12826 /*typename_keyword_p=*/true,
12827 /*check_dependency_p=*/true,
a668c6ad
MM
12828 /*type_p=*/true,
12829 /*is_declaration=*/true);
a723baf1
MM
12830 /* If the base class is given by a qualified name, assume that names
12831 we see are type names or templates, as appropriate. */
12832 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12833 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12834
a723baf1
MM
12835 /* Finally, look for the class-name. */
12836 type = cp_parser_class_name (parser,
12837 class_scope_p,
bbaab916 12838 template_p,
a723baf1 12839 /*type_p=*/true,
a723baf1 12840 /*check_dependency_p=*/true,
a668c6ad
MM
12841 /*class_head_p=*/false,
12842 /*is_declaration=*/true);
a723baf1
MM
12843
12844 if (type == error_mark_node)
12845 return error_mark_node;
12846
dbbf88d1 12847 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12848}
12849
12850/* Exception handling [gram.exception] */
12851
12852/* Parse an (optional) exception-specification.
12853
12854 exception-specification:
12855 throw ( type-id-list [opt] )
12856
12857 Returns a TREE_LIST representing the exception-specification. The
12858 TREE_VALUE of each node is a type. */
12859
12860static tree
94edc4ab 12861cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12862{
12863 cp_token *token;
12864 tree type_id_list;
12865
12866 /* Peek at the next token. */
12867 token = cp_lexer_peek_token (parser->lexer);
12868 /* If it's not `throw', then there's no exception-specification. */
12869 if (!cp_parser_is_keyword (token, RID_THROW))
12870 return NULL_TREE;
12871
12872 /* Consume the `throw'. */
12873 cp_lexer_consume_token (parser->lexer);
12874
12875 /* Look for the `('. */
12876 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12877
12878 /* Peek at the next token. */
12879 token = cp_lexer_peek_token (parser->lexer);
12880 /* If it's not a `)', then there is a type-id-list. */
12881 if (token->type != CPP_CLOSE_PAREN)
12882 {
12883 const char *saved_message;
12884
12885 /* Types may not be defined in an exception-specification. */
12886 saved_message = parser->type_definition_forbidden_message;
12887 parser->type_definition_forbidden_message
12888 = "types may not be defined in an exception-specification";
12889 /* Parse the type-id-list. */
12890 type_id_list = cp_parser_type_id_list (parser);
12891 /* Restore the saved message. */
12892 parser->type_definition_forbidden_message = saved_message;
12893 }
12894 else
12895 type_id_list = empty_except_spec;
12896
12897 /* Look for the `)'. */
12898 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12899
12900 return type_id_list;
12901}
12902
12903/* Parse an (optional) type-id-list.
12904
12905 type-id-list:
12906 type-id
12907 type-id-list , type-id
12908
12909 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12910 in the order that the types were presented. */
12911
12912static tree
94edc4ab 12913cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12914{
12915 tree types = NULL_TREE;
12916
12917 while (true)
12918 {
12919 cp_token *token;
12920 tree type;
12921
12922 /* Get the next type-id. */
12923 type = cp_parser_type_id (parser);
12924 /* Add it to the list. */
12925 types = add_exception_specifier (types, type, /*complain=*/1);
12926 /* Peek at the next token. */
12927 token = cp_lexer_peek_token (parser->lexer);
12928 /* If it is not a `,', we are done. */
12929 if (token->type != CPP_COMMA)
12930 break;
12931 /* Consume the `,'. */
12932 cp_lexer_consume_token (parser->lexer);
12933 }
12934
12935 return nreverse (types);
12936}
12937
12938/* Parse a try-block.
12939
12940 try-block:
12941 try compound-statement handler-seq */
12942
12943static tree
94edc4ab 12944cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12945{
12946 tree try_block;
12947
12948 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12949 try_block = begin_try_block ();
a5bcc582 12950 cp_parser_compound_statement (parser, false);
a723baf1
MM
12951 finish_try_block (try_block);
12952 cp_parser_handler_seq (parser);
12953 finish_handler_sequence (try_block);
12954
12955 return try_block;
12956}
12957
12958/* Parse a function-try-block.
12959
12960 function-try-block:
12961 try ctor-initializer [opt] function-body handler-seq */
12962
12963static bool
94edc4ab 12964cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12965{
12966 tree try_block;
12967 bool ctor_initializer_p;
12968
12969 /* Look for the `try' keyword. */
12970 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12971 return false;
12972 /* Let the rest of the front-end know where we are. */
12973 try_block = begin_function_try_block ();
12974 /* Parse the function-body. */
12975 ctor_initializer_p
12976 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12977 /* We're done with the `try' part. */
12978 finish_function_try_block (try_block);
12979 /* Parse the handlers. */
12980 cp_parser_handler_seq (parser);
12981 /* We're done with the handlers. */
12982 finish_function_handler_sequence (try_block);
12983
12984 return ctor_initializer_p;
12985}
12986
12987/* Parse a handler-seq.
12988
12989 handler-seq:
12990 handler handler-seq [opt] */
12991
12992static void
94edc4ab 12993cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12994{
12995 while (true)
12996 {
12997 cp_token *token;
12998
12999 /* Parse the handler. */
13000 cp_parser_handler (parser);
13001 /* Peek at the next token. */
13002 token = cp_lexer_peek_token (parser->lexer);
13003 /* If it's not `catch' then there are no more handlers. */
13004 if (!cp_parser_is_keyword (token, RID_CATCH))
13005 break;
13006 }
13007}
13008
13009/* Parse a handler.
13010
13011 handler:
13012 catch ( exception-declaration ) compound-statement */
13013
13014static void
94edc4ab 13015cp_parser_handler (cp_parser* parser)
a723baf1
MM
13016{
13017 tree handler;
13018 tree declaration;
13019
13020 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13021 handler = begin_handler ();
13022 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13023 declaration = cp_parser_exception_declaration (parser);
13024 finish_handler_parms (declaration, handler);
13025 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
a5bcc582 13026 cp_parser_compound_statement (parser, false);
a723baf1
MM
13027 finish_handler (handler);
13028}
13029
13030/* Parse an exception-declaration.
13031
13032 exception-declaration:
13033 type-specifier-seq declarator
13034 type-specifier-seq abstract-declarator
13035 type-specifier-seq
13036 ...
13037
13038 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13039 ellipsis variant is used. */
13040
13041static tree
94edc4ab 13042cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
13043{
13044 tree type_specifiers;
13045 tree declarator;
13046 const char *saved_message;
13047
13048 /* If it's an ellipsis, it's easy to handle. */
13049 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13050 {
13051 /* Consume the `...' token. */
13052 cp_lexer_consume_token (parser->lexer);
13053 return NULL_TREE;
13054 }
13055
13056 /* Types may not be defined in exception-declarations. */
13057 saved_message = parser->type_definition_forbidden_message;
13058 parser->type_definition_forbidden_message
13059 = "types may not be defined in exception-declarations";
13060
13061 /* Parse the type-specifier-seq. */
13062 type_specifiers = cp_parser_type_specifier_seq (parser);
13063 /* If it's a `)', then there is no declarator. */
13064 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13065 declarator = NULL_TREE;
13066 else
62b8a44e 13067 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
13068 /*ctor_dtor_or_conv_p=*/NULL,
13069 /*parenthesized_p=*/NULL);
a723baf1
MM
13070
13071 /* Restore the saved message. */
13072 parser->type_definition_forbidden_message = saved_message;
13073
13074 return start_handler_parms (type_specifiers, declarator);
13075}
13076
13077/* Parse a throw-expression.
13078
13079 throw-expression:
34cd5ae7 13080 throw assignment-expression [opt]
a723baf1
MM
13081
13082 Returns a THROW_EXPR representing the throw-expression. */
13083
13084static tree
94edc4ab 13085cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13086{
13087 tree expression;
89f1a6ec 13088 cp_token* token;
a723baf1
MM
13089
13090 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13091 token = cp_lexer_peek_token (parser->lexer);
13092 /* Figure out whether or not there is an assignment-expression
13093 following the "throw" keyword. */
13094 if (token->type == CPP_COMMA
13095 || token->type == CPP_SEMICOLON
13096 || token->type == CPP_CLOSE_PAREN
13097 || token->type == CPP_CLOSE_SQUARE
13098 || token->type == CPP_CLOSE_BRACE
13099 || token->type == CPP_COLON)
a723baf1 13100 expression = NULL_TREE;
89f1a6ec
MM
13101 else
13102 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13103
13104 return build_throw (expression);
13105}
13106
13107/* GNU Extensions */
13108
13109/* Parse an (optional) asm-specification.
13110
13111 asm-specification:
13112 asm ( string-literal )
13113
13114 If the asm-specification is present, returns a STRING_CST
13115 corresponding to the string-literal. Otherwise, returns
13116 NULL_TREE. */
13117
13118static tree
94edc4ab 13119cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13120{
13121 cp_token *token;
13122 tree asm_specification;
13123
13124 /* Peek at the next token. */
13125 token = cp_lexer_peek_token (parser->lexer);
13126 /* If the next token isn't the `asm' keyword, then there's no
13127 asm-specification. */
13128 if (!cp_parser_is_keyword (token, RID_ASM))
13129 return NULL_TREE;
13130
13131 /* Consume the `asm' token. */
13132 cp_lexer_consume_token (parser->lexer);
13133 /* Look for the `('. */
13134 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13135
13136 /* Look for the string-literal. */
13137 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13138 if (token)
13139 asm_specification = token->value;
13140 else
13141 asm_specification = NULL_TREE;
13142
13143 /* Look for the `)'. */
13144 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13145
13146 return asm_specification;
13147}
13148
13149/* Parse an asm-operand-list.
13150
13151 asm-operand-list:
13152 asm-operand
13153 asm-operand-list , asm-operand
13154
13155 asm-operand:
13156 string-literal ( expression )
13157 [ string-literal ] string-literal ( expression )
13158
13159 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13160 each node is the expression. The TREE_PURPOSE is itself a
13161 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13162 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13163 is a STRING_CST for the string literal before the parenthesis. */
13164
13165static tree
94edc4ab 13166cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13167{
13168 tree asm_operands = NULL_TREE;
13169
13170 while (true)
13171 {
13172 tree string_literal;
13173 tree expression;
13174 tree name;
13175 cp_token *token;
13176
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13178 {
13179 /* Consume the `[' token. */
13180 cp_lexer_consume_token (parser->lexer);
13181 /* Read the operand name. */
13182 name = cp_parser_identifier (parser);
13183 if (name != error_mark_node)
13184 name = build_string (IDENTIFIER_LENGTH (name),
13185 IDENTIFIER_POINTER (name));
13186 /* Look for the closing `]'. */
13187 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13188 }
13189 else
13190 name = NULL_TREE;
13191 /* Look for the string-literal. */
13192 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13193 string_literal = token ? token->value : error_mark_node;
13194 /* Look for the `('. */
13195 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13196 /* Parse the expression. */
13197 expression = cp_parser_expression (parser);
13198 /* Look for the `)'. */
13199 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13200 /* Add this operand to the list. */
13201 asm_operands = tree_cons (build_tree_list (name, string_literal),
13202 expression,
13203 asm_operands);
13204 /* If the next token is not a `,', there are no more
13205 operands. */
13206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13207 break;
13208 /* Consume the `,'. */
13209 cp_lexer_consume_token (parser->lexer);
13210 }
13211
13212 return nreverse (asm_operands);
13213}
13214
13215/* Parse an asm-clobber-list.
13216
13217 asm-clobber-list:
13218 string-literal
13219 asm-clobber-list , string-literal
13220
13221 Returns a TREE_LIST, indicating the clobbers in the order that they
13222 appeared. The TREE_VALUE of each node is a STRING_CST. */
13223
13224static tree
94edc4ab 13225cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13226{
13227 tree clobbers = NULL_TREE;
13228
13229 while (true)
13230 {
13231 cp_token *token;
13232 tree string_literal;
13233
13234 /* Look for the string literal. */
13235 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13236 string_literal = token ? token->value : error_mark_node;
13237 /* Add it to the list. */
13238 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13239 /* If the next token is not a `,', then the list is
13240 complete. */
13241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13242 break;
13243 /* Consume the `,' token. */
13244 cp_lexer_consume_token (parser->lexer);
13245 }
13246
13247 return clobbers;
13248}
13249
13250/* Parse an (optional) series of attributes.
13251
13252 attributes:
13253 attributes attribute
13254
13255 attribute:
13256 __attribute__ (( attribute-list [opt] ))
13257
13258 The return value is as for cp_parser_attribute_list. */
13259
13260static tree
94edc4ab 13261cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13262{
13263 tree attributes = NULL_TREE;
13264
13265 while (true)
13266 {
13267 cp_token *token;
13268 tree attribute_list;
13269
13270 /* Peek at the next token. */
13271 token = cp_lexer_peek_token (parser->lexer);
13272 /* If it's not `__attribute__', then we're done. */
13273 if (token->keyword != RID_ATTRIBUTE)
13274 break;
13275
13276 /* Consume the `__attribute__' keyword. */
13277 cp_lexer_consume_token (parser->lexer);
13278 /* Look for the two `(' tokens. */
13279 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13280 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13281
13282 /* Peek at the next token. */
13283 token = cp_lexer_peek_token (parser->lexer);
13284 if (token->type != CPP_CLOSE_PAREN)
13285 /* Parse the attribute-list. */
13286 attribute_list = cp_parser_attribute_list (parser);
13287 else
13288 /* If the next token is a `)', then there is no attribute
13289 list. */
13290 attribute_list = NULL;
13291
13292 /* Look for the two `)' tokens. */
13293 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13294 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13295
13296 /* Add these new attributes to the list. */
13297 attributes = chainon (attributes, attribute_list);
13298 }
13299
13300 return attributes;
13301}
13302
13303/* Parse an attribute-list.
13304
13305 attribute-list:
13306 attribute
13307 attribute-list , attribute
13308
13309 attribute:
13310 identifier
13311 identifier ( identifier )
13312 identifier ( identifier , expression-list )
13313 identifier ( expression-list )
13314
13315 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13316 TREE_PURPOSE of each node is the identifier indicating which
13317 attribute is in use. The TREE_VALUE represents the arguments, if
13318 any. */
13319
13320static tree
94edc4ab 13321cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13322{
13323 tree attribute_list = NULL_TREE;
13324
13325 while (true)
13326 {
13327 cp_token *token;
13328 tree identifier;
13329 tree attribute;
13330
13331 /* Look for the identifier. We also allow keywords here; for
13332 example `__attribute__ ((const))' is legal. */
13333 token = cp_lexer_peek_token (parser->lexer);
13334 if (token->type != CPP_NAME
13335 && token->type != CPP_KEYWORD)
13336 return error_mark_node;
13337 /* Consume the token. */
13338 token = cp_lexer_consume_token (parser->lexer);
13339
13340 /* Save away the identifier that indicates which attribute this is. */
13341 identifier = token->value;
13342 attribute = build_tree_list (identifier, NULL_TREE);
13343
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it's an `(', then parse the attribute arguments. */
13347 if (token->type == CPP_OPEN_PAREN)
13348 {
13349 tree arguments;
a723baf1 13350
39703eb9
MM
13351 arguments = (cp_parser_parenthesized_expression_list
13352 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13353 /* Save the identifier and arguments away. */
13354 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13355 }
13356
13357 /* Add this attribute to the list. */
13358 TREE_CHAIN (attribute) = attribute_list;
13359 attribute_list = attribute;
13360
13361 /* Now, look for more attributes. */
13362 token = cp_lexer_peek_token (parser->lexer);
13363 /* If the next token isn't a `,', we're done. */
13364 if (token->type != CPP_COMMA)
13365 break;
13366
cd0be382 13367 /* Consume the comma and keep going. */
a723baf1
MM
13368 cp_lexer_consume_token (parser->lexer);
13369 }
13370
13371 /* We built up the list in reverse order. */
13372 return nreverse (attribute_list);
13373}
13374
13375/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13376 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13377 current value of the PEDANTIC flag, regardless of whether or not
13378 the `__extension__' keyword is present. The caller is responsible
13379 for restoring the value of the PEDANTIC flag. */
13380
13381static bool
94edc4ab 13382cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13383{
13384 /* Save the old value of the PEDANTIC flag. */
13385 *saved_pedantic = pedantic;
13386
13387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13388 {
13389 /* Consume the `__extension__' token. */
13390 cp_lexer_consume_token (parser->lexer);
13391 /* We're not being pedantic while the `__extension__' keyword is
13392 in effect. */
13393 pedantic = 0;
13394
13395 return true;
13396 }
13397
13398 return false;
13399}
13400
13401/* Parse a label declaration.
13402
13403 label-declaration:
13404 __label__ label-declarator-seq ;
13405
13406 label-declarator-seq:
13407 identifier , label-declarator-seq
13408 identifier */
13409
13410static void
94edc4ab 13411cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13412{
13413 /* Look for the `__label__' keyword. */
13414 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13415
13416 while (true)
13417 {
13418 tree identifier;
13419
13420 /* Look for an identifier. */
13421 identifier = cp_parser_identifier (parser);
13422 /* Declare it as a lobel. */
13423 finish_label_decl (identifier);
13424 /* If the next token is a `;', stop. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13426 break;
13427 /* Look for the `,' separating the label declarations. */
13428 cp_parser_require (parser, CPP_COMMA, "`,'");
13429 }
13430
13431 /* Look for the final `;'. */
13432 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13433}
13434
13435/* Support Functions */
13436
13437/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13438 NAME should have one of the representations used for an
13439 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13440 is returned. If PARSER->SCOPE is a dependent type, then a
13441 SCOPE_REF is returned.
13442
13443 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13444 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13445 was formed. Abstractly, such entities should not be passed to this
13446 function, because they do not need to be looked up, but it is
13447 simpler to check for this special case here, rather than at the
13448 call-sites.
13449
13450 In cases not explicitly covered above, this function returns a
13451 DECL, OVERLOAD, or baselink representing the result of the lookup.
13452 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13453 is returned.
13454
a723baf1
MM
13455 If IS_TYPE is TRUE, bindings that do not refer to types are
13456 ignored.
13457
b0bc6e8e
KL
13458 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13459 ignored.
13460
eea9800f
MM
13461 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13462 are ignored.
13463
a723baf1
MM
13464 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13465 types. */
13466
13467static tree
8d241e0b 13468cp_parser_lookup_name (cp_parser *parser, tree name,
b0bc6e8e
KL
13469 bool is_type, bool is_template, bool is_namespace,
13470 bool check_dependency)
a723baf1
MM
13471{
13472 tree decl;
13473 tree object_type = parser->context->object_type;
13474
13475 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13476 no longer valid. Note that if we are parsing tentatively, and
13477 the parse fails, OBJECT_TYPE will be automatically restored. */
13478 parser->context->object_type = NULL_TREE;
13479
13480 if (name == error_mark_node)
13481 return error_mark_node;
13482
13483 /* A template-id has already been resolved; there is no lookup to
13484 do. */
13485 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13486 return name;
13487 if (BASELINK_P (name))
13488 {
13489 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13490 == TEMPLATE_ID_EXPR),
13491 20020909);
13492 return name;
13493 }
13494
13495 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13496 it should already have been checked to make sure that the name
13497 used matches the type being destroyed. */
13498 if (TREE_CODE (name) == BIT_NOT_EXPR)
13499 {
13500 tree type;
13501
13502 /* Figure out to which type this destructor applies. */
13503 if (parser->scope)
13504 type = parser->scope;
13505 else if (object_type)
13506 type = object_type;
13507 else
13508 type = current_class_type;
13509 /* If that's not a class type, there is no destructor. */
13510 if (!type || !CLASS_TYPE_P (type))
13511 return error_mark_node;
fd6e3cce
GB
13512 if (!CLASSTYPE_DESTRUCTORS (type))
13513 return error_mark_node;
a723baf1
MM
13514 /* If it was a class type, return the destructor. */
13515 return CLASSTYPE_DESTRUCTORS (type);
13516 }
13517
13518 /* By this point, the NAME should be an ordinary identifier. If
13519 the id-expression was a qualified name, the qualifying scope is
13520 stored in PARSER->SCOPE at this point. */
13521 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13522 20000619);
13523
13524 /* Perform the lookup. */
13525 if (parser->scope)
13526 {
1fb3244a 13527 bool dependent_p;
a723baf1
MM
13528
13529 if (parser->scope == error_mark_node)
13530 return error_mark_node;
13531
13532 /* If the SCOPE is dependent, the lookup must be deferred until
13533 the template is instantiated -- unless we are explicitly
13534 looking up names in uninstantiated templates. Even then, we
13535 cannot look up the name if the scope is not a class type; it
13536 might, for example, be a template type parameter. */
1fb3244a
MM
13537 dependent_p = (TYPE_P (parser->scope)
13538 && !(parser->in_declarator_p
13539 && currently_open_class (parser->scope))
13540 && dependent_type_p (parser->scope));
a723baf1 13541 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13542 && dependent_p)
a723baf1 13543 {
b0bc6e8e 13544 if (is_type)
a723baf1
MM
13545 /* The resolution to Core Issue 180 says that `struct A::B'
13546 should be considered a type-name, even if `A' is
13547 dependent. */
13548 decl = TYPE_NAME (make_typename_type (parser->scope,
13549 name,
13550 /*complain=*/1));
b0bc6e8e 13551 else if (is_template)
5b4acce1
KL
13552 decl = make_unbound_class_template (parser->scope,
13553 name,
13554 /*complain=*/1);
b0bc6e8e
KL
13555 else
13556 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
13557 }
13558 else
13559 {
13560 /* If PARSER->SCOPE is a dependent type, then it must be a
13561 class type, and we must not be checking dependencies;
13562 otherwise, we would have processed this lookup above. So
13563 that PARSER->SCOPE is not considered a dependent base by
13564 lookup_member, we must enter the scope here. */
1fb3244a 13565 if (dependent_p)
a723baf1
MM
13566 push_scope (parser->scope);
13567 /* If the PARSER->SCOPE is a a template specialization, it
13568 may be instantiated during name lookup. In that case,
13569 errors may be issued. Even if we rollback the current
13570 tentative parse, those errors are valid. */
5e08432e
MM
13571 decl = lookup_qualified_name (parser->scope, name, is_type,
13572 /*complain=*/true);
1fb3244a 13573 if (dependent_p)
a723baf1
MM
13574 pop_scope (parser->scope);
13575 }
13576 parser->qualifying_scope = parser->scope;
13577 parser->object_scope = NULL_TREE;
13578 }
13579 else if (object_type)
13580 {
13581 tree object_decl = NULL_TREE;
13582 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13583 OBJECT_TYPE is not a class. */
13584 if (CLASS_TYPE_P (object_type))
13585 /* If the OBJECT_TYPE is a template specialization, it may
13586 be instantiated during name lookup. In that case, errors
13587 may be issued. Even if we rollback the current tentative
13588 parse, those errors are valid. */
13589 object_decl = lookup_member (object_type,
13590 name,
13591 /*protect=*/0, is_type);
13592 /* Look it up in the enclosing context, too. */
13593 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13594 is_namespace,
a723baf1
MM
13595 /*flags=*/0);
13596 parser->object_scope = object_type;
13597 parser->qualifying_scope = NULL_TREE;
13598 if (object_decl)
13599 decl = object_decl;
13600 }
13601 else
13602 {
13603 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13604 is_namespace,
a723baf1
MM
13605 /*flags=*/0);
13606 parser->qualifying_scope = NULL_TREE;
13607 parser->object_scope = NULL_TREE;
13608 }
13609
13610 /* If the lookup failed, let our caller know. */
13611 if (!decl
13612 || decl == error_mark_node
13613 || (TREE_CODE (decl) == FUNCTION_DECL
13614 && DECL_ANTICIPATED (decl)))
13615 return error_mark_node;
13616
13617 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13618 if (TREE_CODE (decl) == TREE_LIST)
13619 {
13620 /* The error message we have to print is too complicated for
13621 cp_parser_error, so we incorporate its actions directly. */
e5976695 13622 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13623 {
13624 error ("reference to `%D' is ambiguous", name);
13625 print_candidates (decl);
13626 }
13627 return error_mark_node;
13628 }
13629
13630 my_friendly_assert (DECL_P (decl)
13631 || TREE_CODE (decl) == OVERLOAD
13632 || TREE_CODE (decl) == SCOPE_REF
5b4acce1 13633 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
a723baf1
MM
13634 || BASELINK_P (decl),
13635 20000619);
13636
13637 /* If we have resolved the name of a member declaration, check to
13638 see if the declaration is accessible. When the name resolves to
34cd5ae7 13639 set of overloaded functions, accessibility is checked when
a723baf1
MM
13640 overload resolution is done.
13641
13642 During an explicit instantiation, access is not checked at all,
13643 as per [temp.explicit]. */
8d241e0b 13644 if (DECL_P (decl))
ee76b931 13645 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
13646
13647 return decl;
13648}
13649
13650/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
13651 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13652 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
13653
13654static tree
94edc4ab 13655cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
13656{
13657 return cp_parser_lookup_name (parser, name,
eea9800f 13658 /*is_type=*/false,
b0bc6e8e 13659 /*is_template=*/false,
eea9800f 13660 /*is_namespace=*/false,
a723baf1
MM
13661 /*check_dependency=*/true);
13662}
13663
a723baf1
MM
13664/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13665 the current context, return the TYPE_DECL. If TAG_NAME_P is
13666 true, the DECL indicates the class being defined in a class-head,
13667 or declared in an elaborated-type-specifier.
13668
13669 Otherwise, return DECL. */
13670
13671static tree
13672cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13673{
710b73e6
KL
13674 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13675 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
13676
13677 struct A {
13678 template <typename T> struct B;
13679 };
13680
13681 template <typename T> struct A::B {};
13682
13683 Similarly, in a elaborated-type-specifier:
13684
13685 namespace N { struct X{}; }
13686
13687 struct A {
13688 template <typename T> friend struct N::X;
13689 };
13690
710b73e6
KL
13691 However, if the DECL refers to a class type, and we are in
13692 the scope of the class, then the name lookup automatically
13693 finds the TYPE_DECL created by build_self_reference rather
13694 than a TEMPLATE_DECL. For example, in:
13695
13696 template <class T> struct S {
13697 S s;
13698 };
13699
13700 there is no need to handle such case. */
13701
13702 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13703 return DECL_TEMPLATE_RESULT (decl);
13704
13705 return decl;
13706}
13707
13708/* If too many, or too few, template-parameter lists apply to the
13709 declarator, issue an error message. Returns TRUE if all went well,
13710 and FALSE otherwise. */
13711
13712static bool
94edc4ab
NN
13713cp_parser_check_declarator_template_parameters (cp_parser* parser,
13714 tree declarator)
a723baf1
MM
13715{
13716 unsigned num_templates;
13717
13718 /* We haven't seen any classes that involve template parameters yet. */
13719 num_templates = 0;
13720
13721 switch (TREE_CODE (declarator))
13722 {
13723 case CALL_EXPR:
13724 case ARRAY_REF:
13725 case INDIRECT_REF:
13726 case ADDR_EXPR:
13727 {
13728 tree main_declarator = TREE_OPERAND (declarator, 0);
13729 return
13730 cp_parser_check_declarator_template_parameters (parser,
13731 main_declarator);
13732 }
13733
13734 case SCOPE_REF:
13735 {
13736 tree scope;
13737 tree member;
13738
13739 scope = TREE_OPERAND (declarator, 0);
13740 member = TREE_OPERAND (declarator, 1);
13741
13742 /* If this is a pointer-to-member, then we are not interested
13743 in the SCOPE, because it does not qualify the thing that is
13744 being declared. */
13745 if (TREE_CODE (member) == INDIRECT_REF)
13746 return (cp_parser_check_declarator_template_parameters
13747 (parser, member));
13748
13749 while (scope && CLASS_TYPE_P (scope))
13750 {
13751 /* You're supposed to have one `template <...>'
13752 for every template class, but you don't need one
13753 for a full specialization. For example:
13754
13755 template <class T> struct S{};
13756 template <> struct S<int> { void f(); };
13757 void S<int>::f () {}
13758
13759 is correct; there shouldn't be a `template <>' for
13760 the definition of `S<int>::f'. */
13761 if (CLASSTYPE_TEMPLATE_INFO (scope)
13762 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13763 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13764 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13765 ++num_templates;
13766
13767 scope = TYPE_CONTEXT (scope);
13768 }
13769 }
13770
13771 /* Fall through. */
13772
13773 default:
13774 /* If the DECLARATOR has the form `X<y>' then it uses one
13775 additional level of template parameters. */
13776 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13777 ++num_templates;
13778
13779 return cp_parser_check_template_parameters (parser,
13780 num_templates);
13781 }
13782}
13783
13784/* NUM_TEMPLATES were used in the current declaration. If that is
13785 invalid, return FALSE and issue an error messages. Otherwise,
13786 return TRUE. */
13787
13788static bool
94edc4ab
NN
13789cp_parser_check_template_parameters (cp_parser* parser,
13790 unsigned num_templates)
a723baf1
MM
13791{
13792 /* If there are more template classes than parameter lists, we have
13793 something like:
13794
13795 template <class T> void S<T>::R<T>::f (); */
13796 if (parser->num_template_parameter_lists < num_templates)
13797 {
13798 error ("too few template-parameter-lists");
13799 return false;
13800 }
13801 /* If there are the same number of template classes and parameter
13802 lists, that's OK. */
13803 if (parser->num_template_parameter_lists == num_templates)
13804 return true;
13805 /* If there are more, but only one more, then we are referring to a
13806 member template. That's OK too. */
13807 if (parser->num_template_parameter_lists == num_templates + 1)
13808 return true;
13809 /* Otherwise, there are too many template parameter lists. We have
13810 something like:
13811
13812 template <class T> template <class U> void S::f(); */
13813 error ("too many template-parameter-lists");
13814 return false;
13815}
13816
13817/* Parse a binary-expression of the general form:
13818
13819 binary-expression:
13820 <expr>
13821 binary-expression <token> <expr>
13822
13823 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13824 to parser the <expr>s. If the first production is used, then the
13825 value returned by FN is returned directly. Otherwise, a node with
13826 the indicated EXPR_TYPE is returned, with operands corresponding to
13827 the two sub-expressions. */
13828
13829static tree
94edc4ab
NN
13830cp_parser_binary_expression (cp_parser* parser,
13831 const cp_parser_token_tree_map token_tree_map,
13832 cp_parser_expression_fn fn)
a723baf1
MM
13833{
13834 tree lhs;
13835
13836 /* Parse the first expression. */
13837 lhs = (*fn) (parser);
13838 /* Now, look for more expressions. */
13839 while (true)
13840 {
13841 cp_token *token;
39b1af70 13842 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13843 tree rhs;
13844
13845 /* Peek at the next token. */
13846 token = cp_lexer_peek_token (parser->lexer);
13847 /* If the token is `>', and that's not an operator at the
13848 moment, then we're done. */
13849 if (token->type == CPP_GREATER
13850 && !parser->greater_than_is_operator_p)
13851 break;
34cd5ae7 13852 /* If we find one of the tokens we want, build the corresponding
a723baf1
MM
13853 tree representation. */
13854 for (map_node = token_tree_map;
13855 map_node->token_type != CPP_EOF;
13856 ++map_node)
13857 if (map_node->token_type == token->type)
13858 {
13859 /* Consume the operator token. */
13860 cp_lexer_consume_token (parser->lexer);
13861 /* Parse the right-hand side of the expression. */
13862 rhs = (*fn) (parser);
13863 /* Build the binary tree node. */
13864 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13865 break;
13866 }
13867
13868 /* If the token wasn't one of the ones we want, we're done. */
13869 if (map_node->token_type == CPP_EOF)
13870 break;
13871 }
13872
13873 return lhs;
13874}
13875
13876/* Parse an optional `::' token indicating that the following name is
13877 from the global namespace. If so, PARSER->SCOPE is set to the
13878 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13879 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13880 Returns the new value of PARSER->SCOPE, if the `::' token is
13881 present, and NULL_TREE otherwise. */
13882
13883static tree
94edc4ab 13884cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13885{
13886 cp_token *token;
13887
13888 /* Peek at the next token. */
13889 token = cp_lexer_peek_token (parser->lexer);
13890 /* If we're looking at a `::' token then we're starting from the
13891 global namespace, not our current location. */
13892 if (token->type == CPP_SCOPE)
13893 {
13894 /* Consume the `::' token. */
13895 cp_lexer_consume_token (parser->lexer);
13896 /* Set the SCOPE so that we know where to start the lookup. */
13897 parser->scope = global_namespace;
13898 parser->qualifying_scope = global_namespace;
13899 parser->object_scope = NULL_TREE;
13900
13901 return parser->scope;
13902 }
13903 else if (!current_scope_valid_p)
13904 {
13905 parser->scope = NULL_TREE;
13906 parser->qualifying_scope = NULL_TREE;
13907 parser->object_scope = NULL_TREE;
13908 }
13909
13910 return NULL_TREE;
13911}
13912
13913/* Returns TRUE if the upcoming token sequence is the start of a
13914 constructor declarator. If FRIEND_P is true, the declarator is
13915 preceded by the `friend' specifier. */
13916
13917static bool
13918cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13919{
13920 bool constructor_p;
13921 tree type_decl = NULL_TREE;
13922 bool nested_name_p;
2050a1bb
MM
13923 cp_token *next_token;
13924
13925 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13926 try to avoid doing lots of work if at all possible. It's not
13927 valid declare a constructor at function scope. */
13928 if (at_function_scope_p ())
13929 return false;
13930 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13931 next_token = cp_lexer_peek_token (parser->lexer);
13932 if (next_token->type != CPP_NAME
13933 && next_token->type != CPP_SCOPE
13934 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13935 && next_token->type != CPP_TEMPLATE_ID)
13936 return false;
a723baf1
MM
13937
13938 /* Parse tentatively; we are going to roll back all of the tokens
13939 consumed here. */
13940 cp_parser_parse_tentatively (parser);
13941 /* Assume that we are looking at a constructor declarator. */
13942 constructor_p = true;
8d241e0b 13943
a723baf1
MM
13944 /* Look for the optional `::' operator. */
13945 cp_parser_global_scope_opt (parser,
13946 /*current_scope_valid_p=*/false);
13947 /* Look for the nested-name-specifier. */
13948 nested_name_p
13949 = (cp_parser_nested_name_specifier_opt (parser,
13950 /*typename_keyword_p=*/false,
13951 /*check_dependency_p=*/false,
a668c6ad
MM
13952 /*type_p=*/false,
13953 /*is_declaration=*/false)
a723baf1
MM
13954 != NULL_TREE);
13955 /* Outside of a class-specifier, there must be a
13956 nested-name-specifier. */
13957 if (!nested_name_p &&
13958 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13959 || friend_p))
13960 constructor_p = false;
13961 /* If we still think that this might be a constructor-declarator,
13962 look for a class-name. */
13963 if (constructor_p)
13964 {
13965 /* If we have:
13966
8fbc5ae7 13967 template <typename T> struct S { S(); };
a723baf1
MM
13968 template <typename T> S<T>::S ();
13969
13970 we must recognize that the nested `S' names a class.
13971 Similarly, for:
13972
13973 template <typename T> S<T>::S<T> ();
13974
13975 we must recognize that the nested `S' names a template. */
13976 type_decl = cp_parser_class_name (parser,
13977 /*typename_keyword_p=*/false,
13978 /*template_keyword_p=*/false,
13979 /*type_p=*/false,
a723baf1 13980 /*check_dependency_p=*/false,
a668c6ad
MM
13981 /*class_head_p=*/false,
13982 /*is_declaration=*/false);
a723baf1
MM
13983 /* If there was no class-name, then this is not a constructor. */
13984 constructor_p = !cp_parser_error_occurred (parser);
13985 }
8d241e0b 13986
a723baf1
MM
13987 /* If we're still considering a constructor, we have to see a `(',
13988 to begin the parameter-declaration-clause, followed by either a
13989 `)', an `...', or a decl-specifier. We need to check for a
13990 type-specifier to avoid being fooled into thinking that:
13991
13992 S::S (f) (int);
13993
13994 is a constructor. (It is actually a function named `f' that
13995 takes one parameter (of type `int') and returns a value of type
13996 `S::S'. */
13997 if (constructor_p
13998 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13999 {
14000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14001 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14002 && !cp_parser_storage_class_specifier_opt (parser))
14003 {
5dae1114 14004 tree type;
4047b164 14005 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14006
14007 /* Names appearing in the type-specifier should be looked up
14008 in the scope of the class. */
14009 if (current_class_type)
14010 type = NULL_TREE;
a723baf1
MM
14011 else
14012 {
5dae1114
MM
14013 type = TREE_TYPE (type_decl);
14014 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
14015 {
14016 type = resolve_typename_type (type,
14017 /*only_current_p=*/false);
14018 if (type == error_mark_node)
14019 {
14020 cp_parser_abort_tentative_parse (parser);
14021 return false;
14022 }
14023 }
5dae1114 14024 push_scope (type);
a723baf1 14025 }
4047b164
KL
14026
14027 /* Inside the constructor parameter list, surrounding
14028 template-parameter-lists do not apply. */
14029 saved_num_template_parameter_lists
14030 = parser->num_template_parameter_lists;
14031 parser->num_template_parameter_lists = 0;
14032
5dae1114
MM
14033 /* Look for the type-specifier. */
14034 cp_parser_type_specifier (parser,
14035 CP_PARSER_FLAGS_NONE,
14036 /*is_friend=*/false,
14037 /*is_declarator=*/true,
14038 /*declares_class_or_enum=*/NULL,
14039 /*is_cv_qualifier=*/NULL);
4047b164
KL
14040
14041 parser->num_template_parameter_lists
14042 = saved_num_template_parameter_lists;
14043
5dae1114
MM
14044 /* Leave the scope of the class. */
14045 if (type)
14046 pop_scope (type);
14047
14048 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14049 }
14050 }
14051 else
14052 constructor_p = false;
14053 /* We did not really want to consume any tokens. */
14054 cp_parser_abort_tentative_parse (parser);
14055
14056 return constructor_p;
14057}
14058
14059/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14060 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14061 they must be performed once we are in the scope of the function.
14062
14063 Returns the function defined. */
14064
14065static tree
14066cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
14067 (cp_parser* parser,
14068 tree decl_specifiers,
14069 tree attributes,
14070 tree declarator)
a723baf1
MM
14071{
14072 tree fn;
14073 bool success_p;
14074
14075 /* Begin the function-definition. */
14076 success_p = begin_function_definition (decl_specifiers,
14077 attributes,
14078 declarator);
14079
14080 /* If there were names looked up in the decl-specifier-seq that we
14081 did not check, check them now. We must wait until we are in the
14082 scope of the function to perform the checks, since the function
14083 might be a friend. */
cf22909c 14084 perform_deferred_access_checks ();
a723baf1
MM
14085
14086 if (!success_p)
14087 {
14088 /* If begin_function_definition didn't like the definition, skip
14089 the entire function. */
14090 error ("invalid function declaration");
14091 cp_parser_skip_to_end_of_block_or_statement (parser);
14092 fn = error_mark_node;
14093 }
14094 else
14095 fn = cp_parser_function_definition_after_declarator (parser,
14096 /*inline_p=*/false);
14097
14098 return fn;
14099}
14100
14101/* Parse the part of a function-definition that follows the
14102 declarator. INLINE_P is TRUE iff this function is an inline
14103 function defined with a class-specifier.
14104
14105 Returns the function defined. */
14106
14107static tree
94edc4ab
NN
14108cp_parser_function_definition_after_declarator (cp_parser* parser,
14109 bool inline_p)
a723baf1
MM
14110{
14111 tree fn;
14112 bool ctor_initializer_p = false;
14113 bool saved_in_unbraced_linkage_specification_p;
14114 unsigned saved_num_template_parameter_lists;
14115
14116 /* If the next token is `return', then the code may be trying to
14117 make use of the "named return value" extension that G++ used to
14118 support. */
14119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14120 {
14121 /* Consume the `return' keyword. */
14122 cp_lexer_consume_token (parser->lexer);
14123 /* Look for the identifier that indicates what value is to be
14124 returned. */
14125 cp_parser_identifier (parser);
14126 /* Issue an error message. */
14127 error ("named return values are no longer supported");
14128 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14129 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14130 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14131 cp_lexer_consume_token (parser->lexer);
14132 }
14133 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14134 anything declared inside `f'. */
14135 saved_in_unbraced_linkage_specification_p
14136 = parser->in_unbraced_linkage_specification_p;
14137 parser->in_unbraced_linkage_specification_p = false;
14138 /* Inside the function, surrounding template-parameter-lists do not
14139 apply. */
14140 saved_num_template_parameter_lists
14141 = parser->num_template_parameter_lists;
14142 parser->num_template_parameter_lists = 0;
14143 /* If the next token is `try', then we are looking at a
14144 function-try-block. */
14145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14146 ctor_initializer_p = cp_parser_function_try_block (parser);
14147 /* A function-try-block includes the function-body, so we only do
14148 this next part if we're not processing a function-try-block. */
14149 else
14150 ctor_initializer_p
14151 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14152
14153 /* Finish the function. */
14154 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14155 (inline_p ? 2 : 0));
14156 /* Generate code for it, if necessary. */
8cd2462c 14157 expand_or_defer_fn (fn);
a723baf1
MM
14158 /* Restore the saved values. */
14159 parser->in_unbraced_linkage_specification_p
14160 = saved_in_unbraced_linkage_specification_p;
14161 parser->num_template_parameter_lists
14162 = saved_num_template_parameter_lists;
14163
14164 return fn;
14165}
14166
14167/* Parse a template-declaration, assuming that the `export' (and
14168 `extern') keywords, if present, has already been scanned. MEMBER_P
14169 is as for cp_parser_template_declaration. */
14170
14171static void
94edc4ab 14172cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14173{
14174 tree decl = NULL_TREE;
14175 tree parameter_list;
14176 bool friend_p = false;
14177
14178 /* Look for the `template' keyword. */
14179 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14180 return;
14181
14182 /* And the `<'. */
14183 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14184 return;
14185
a723baf1
MM
14186 /* If the next token is `>', then we have an invalid
14187 specialization. Rather than complain about an invalid template
14188 parameter, issue an error message here. */
14189 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14190 {
14191 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14192 begin_specialization ();
a723baf1
MM
14193 parameter_list = NULL_TREE;
14194 }
14195 else
2f9afd51
KL
14196 {
14197 /* Parse the template parameters. */
14198 begin_template_parm_list ();
14199 parameter_list = cp_parser_template_parameter_list (parser);
14200 parameter_list = end_template_parm_list (parameter_list);
14201 }
14202
a723baf1
MM
14203 /* Look for the `>'. */
14204 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14205 /* We just processed one more parameter list. */
14206 ++parser->num_template_parameter_lists;
14207 /* If the next token is `template', there are more template
14208 parameters. */
14209 if (cp_lexer_next_token_is_keyword (parser->lexer,
14210 RID_TEMPLATE))
14211 cp_parser_template_declaration_after_export (parser, member_p);
14212 else
14213 {
14214 decl = cp_parser_single_declaration (parser,
14215 member_p,
14216 &friend_p);
14217
14218 /* If this is a member template declaration, let the front
14219 end know. */
14220 if (member_p && !friend_p && decl)
37d407a1
KL
14221 {
14222 if (TREE_CODE (decl) == TYPE_DECL)
14223 cp_parser_check_access_in_redeclaration (decl);
14224
14225 decl = finish_member_template_decl (decl);
14226 }
a723baf1 14227 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14228 make_friend_class (current_class_type, TREE_TYPE (decl),
14229 /*complain=*/true);
a723baf1
MM
14230 }
14231 /* We are done with the current parameter list. */
14232 --parser->num_template_parameter_lists;
14233
14234 /* Finish up. */
14235 finish_template_decl (parameter_list);
14236
14237 /* Register member declarations. */
14238 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14239 finish_member_declaration (decl);
14240
14241 /* If DECL is a function template, we must return to parse it later.
14242 (Even though there is no definition, there might be default
14243 arguments that need handling.) */
14244 if (member_p && decl
14245 && (TREE_CODE (decl) == FUNCTION_DECL
14246 || DECL_FUNCTION_TEMPLATE_P (decl)))
14247 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 14248 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14249 TREE_VALUE (parser->unparsed_functions_queues));
14250}
14251
14252/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14253 `function-definition' sequence. MEMBER_P is true, this declaration
14254 appears in a class scope.
14255
14256 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14257 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14258
14259static tree
94edc4ab
NN
14260cp_parser_single_declaration (cp_parser* parser,
14261 bool member_p,
14262 bool* friend_p)
a723baf1 14263{
560ad596 14264 int declares_class_or_enum;
a723baf1
MM
14265 tree decl = NULL_TREE;
14266 tree decl_specifiers;
14267 tree attributes;
4bb8ca28 14268 bool function_definition_p = false;
a723baf1 14269
a723baf1 14270 /* Defer access checks until we know what is being declared. */
8d241e0b 14271 push_deferring_access_checks (dk_deferred);
cf22909c 14272
a723baf1
MM
14273 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14274 alternative. */
14275 decl_specifiers
14276 = cp_parser_decl_specifier_seq (parser,
14277 CP_PARSER_FLAGS_OPTIONAL,
14278 &attributes,
14279 &declares_class_or_enum);
4bb8ca28
MM
14280 if (friend_p)
14281 *friend_p = cp_parser_friend_p (decl_specifiers);
a723baf1
MM
14282 /* Gather up the access checks that occurred the
14283 decl-specifier-seq. */
cf22909c
KL
14284 stop_deferring_access_checks ();
14285
a723baf1
MM
14286 /* Check for the declaration of a template class. */
14287 if (declares_class_or_enum)
14288 {
14289 if (cp_parser_declares_only_class_p (parser))
14290 {
14291 decl = shadow_tag (decl_specifiers);
14292 if (decl)
14293 decl = TYPE_NAME (decl);
14294 else
14295 decl = error_mark_node;
14296 }
14297 }
14298 else
14299 decl = NULL_TREE;
14300 /* If it's not a template class, try for a template function. If
14301 the next token is a `;', then this declaration does not declare
14302 anything. But, if there were errors in the decl-specifiers, then
14303 the error might well have come from an attempted class-specifier.
14304 In that case, there's no need to warn about a missing declarator. */
14305 if (!decl
14306 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14307 || !value_member (error_mark_node, decl_specifiers)))
14308 decl = cp_parser_init_declarator (parser,
14309 decl_specifiers,
14310 attributes,
4bb8ca28 14311 /*function_definition_allowed_p=*/true,
a723baf1 14312 member_p,
560ad596 14313 declares_class_or_enum,
4bb8ca28 14314 &function_definition_p);
cf22909c
KL
14315
14316 pop_deferring_access_checks ();
14317
a723baf1
MM
14318 /* Clear any current qualification; whatever comes next is the start
14319 of something new. */
14320 parser->scope = NULL_TREE;
14321 parser->qualifying_scope = NULL_TREE;
14322 parser->object_scope = NULL_TREE;
14323 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14324 if (!function_definition_p
14325 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14326 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14327
14328 return decl;
14329}
14330
d6b4ea85
MM
14331/* Parse a cast-expression that is not the operand of a unary "&". */
14332
14333static tree
14334cp_parser_simple_cast_expression (cp_parser *parser)
14335{
14336 return cp_parser_cast_expression (parser, /*address_p=*/false);
14337}
14338
a723baf1
MM
14339/* Parse a functional cast to TYPE. Returns an expression
14340 representing the cast. */
14341
14342static tree
94edc4ab 14343cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14344{
14345 tree expression_list;
14346
39703eb9
MM
14347 expression_list
14348 = cp_parser_parenthesized_expression_list (parser, false,
14349 /*non_constant_p=*/NULL);
a723baf1
MM
14350
14351 return build_functional_cast (type, expression_list);
14352}
14353
4bb8ca28
MM
14354/* Save the tokens that make up the body of a member function defined
14355 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14356 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14357 specifiers applied to the declaration. Returns the FUNCTION_DECL
14358 for the member function. */
14359
7ce27103 14360static tree
4bb8ca28
MM
14361cp_parser_save_member_function_body (cp_parser* parser,
14362 tree decl_specifiers,
14363 tree declarator,
14364 tree attributes)
14365{
14366 cp_token_cache *cache;
14367 tree fn;
14368
14369 /* Create the function-declaration. */
14370 fn = start_method (decl_specifiers, declarator, attributes);
14371 /* If something went badly wrong, bail out now. */
14372 if (fn == error_mark_node)
14373 {
14374 /* If there's a function-body, skip it. */
14375 if (cp_parser_token_starts_function_definition_p
14376 (cp_lexer_peek_token (parser->lexer)))
14377 cp_parser_skip_to_end_of_block_or_statement (parser);
14378 return error_mark_node;
14379 }
14380
14381 /* Remember it, if there default args to post process. */
14382 cp_parser_save_default_args (parser, fn);
14383
14384 /* Create a token cache. */
14385 cache = cp_token_cache_new ();
14386 /* Save away the tokens that make up the body of the
14387 function. */
14388 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14389 /* Handle function try blocks. */
14390 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14391 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14392
14393 /* Save away the inline definition; we will process it when the
14394 class is complete. */
14395 DECL_PENDING_INLINE_INFO (fn) = cache;
14396 DECL_PENDING_INLINE_P (fn) = 1;
14397
14398 /* We need to know that this was defined in the class, so that
14399 friend templates are handled correctly. */
14400 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14401
14402 /* We're done with the inline definition. */
14403 finish_method (fn);
14404
14405 /* Add FN to the queue of functions to be parsed later. */
14406 TREE_VALUE (parser->unparsed_functions_queues)
14407 = tree_cons (NULL_TREE, fn,
14408 TREE_VALUE (parser->unparsed_functions_queues));
14409
14410 return fn;
14411}
14412
ec75414f
MM
14413/* Parse a template-argument-list, as well as the trailing ">" (but
14414 not the opening ">"). See cp_parser_template_argument_list for the
14415 return value. */
14416
14417static tree
14418cp_parser_enclosed_template_argument_list (cp_parser* parser)
14419{
14420 tree arguments;
14421 tree saved_scope;
14422 tree saved_qualifying_scope;
14423 tree saved_object_scope;
14424 bool saved_greater_than_is_operator_p;
14425
14426 /* [temp.names]
14427
14428 When parsing a template-id, the first non-nested `>' is taken as
14429 the end of the template-argument-list rather than a greater-than
14430 operator. */
14431 saved_greater_than_is_operator_p
14432 = parser->greater_than_is_operator_p;
14433 parser->greater_than_is_operator_p = false;
14434 /* Parsing the argument list may modify SCOPE, so we save it
14435 here. */
14436 saved_scope = parser->scope;
14437 saved_qualifying_scope = parser->qualifying_scope;
14438 saved_object_scope = parser->object_scope;
14439 /* Parse the template-argument-list itself. */
14440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14441 arguments = NULL_TREE;
14442 else
14443 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
14444 /* Look for the `>' that ends the template-argument-list. If we find
14445 a '>>' instead, it's probably just a typo. */
14446 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14447 {
14448 if (!saved_greater_than_is_operator_p)
14449 {
14450 /* If we're in a nested template argument list, the '>>' has to be
14451 a typo for '> >'. We emit the error message, but we continue
14452 parsing and we push a '>' as next token, so that the argument
14453 list will be parsed correctly.. */
14454 cp_token* token;
14455 error ("`>>' should be `> >' within a nested template argument list");
14456 token = cp_lexer_peek_token (parser->lexer);
14457 token->type = CPP_GREATER;
14458 }
14459 else
14460 {
14461 /* If this is not a nested template argument list, the '>>' is
14462 a typo for '>'. Emit an error message and continue. */
14463 error ("spurious `>>', use `>' to terminate a template argument list");
14464 cp_lexer_consume_token (parser->lexer);
14465 }
14466 }
14467 else
14468 cp_parser_require (parser, CPP_GREATER, "`>'");
ec75414f
MM
14469 /* The `>' token might be a greater-than operator again now. */
14470 parser->greater_than_is_operator_p
14471 = saved_greater_than_is_operator_p;
14472 /* Restore the SAVED_SCOPE. */
14473 parser->scope = saved_scope;
14474 parser->qualifying_scope = saved_qualifying_scope;
14475 parser->object_scope = saved_object_scope;
14476
14477 return arguments;
14478}
14479
a723baf1
MM
14480/* MEMBER_FUNCTION is a member function, or a friend. If default
14481 arguments, or the body of the function have not yet been parsed,
14482 parse them now. */
14483
14484static void
94edc4ab 14485cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14486{
14487 cp_lexer *saved_lexer;
14488
14489 /* If this member is a template, get the underlying
14490 FUNCTION_DECL. */
14491 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14492 member_function = DECL_TEMPLATE_RESULT (member_function);
14493
14494 /* There should not be any class definitions in progress at this
14495 point; the bodies of members are only parsed outside of all class
14496 definitions. */
14497 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14498 /* While we're parsing the member functions we might encounter more
14499 classes. We want to handle them right away, but we don't want
14500 them getting mixed up with functions that are currently in the
14501 queue. */
14502 parser->unparsed_functions_queues
14503 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14504
14505 /* Make sure that any template parameters are in scope. */
14506 maybe_begin_member_template_processing (member_function);
14507
a723baf1
MM
14508 /* If the body of the function has not yet been parsed, parse it
14509 now. */
14510 if (DECL_PENDING_INLINE_P (member_function))
14511 {
14512 tree function_scope;
14513 cp_token_cache *tokens;
14514
14515 /* The function is no longer pending; we are processing it. */
14516 tokens = DECL_PENDING_INLINE_INFO (member_function);
14517 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14518 DECL_PENDING_INLINE_P (member_function) = 0;
14519 /* If this was an inline function in a local class, enter the scope
14520 of the containing function. */
14521 function_scope = decl_function_context (member_function);
14522 if (function_scope)
14523 push_function_context_to (function_scope);
14524
14525 /* Save away the current lexer. */
14526 saved_lexer = parser->lexer;
14527 /* Make a new lexer to feed us the tokens saved for this function. */
14528 parser->lexer = cp_lexer_new_from_tokens (tokens);
14529 parser->lexer->next = saved_lexer;
14530
14531 /* Set the current source position to be the location of the first
14532 token in the saved inline body. */
3466b292 14533 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14534
14535 /* Let the front end know that we going to be defining this
14536 function. */
14537 start_function (NULL_TREE, member_function, NULL_TREE,
14538 SF_PRE_PARSED | SF_INCLASS_INLINE);
14539
14540 /* Now, parse the body of the function. */
14541 cp_parser_function_definition_after_declarator (parser,
14542 /*inline_p=*/true);
14543
14544 /* Leave the scope of the containing function. */
14545 if (function_scope)
14546 pop_function_context_from (function_scope);
14547 /* Restore the lexer. */
14548 parser->lexer = saved_lexer;
14549 }
14550
14551 /* Remove any template parameters from the symbol table. */
14552 maybe_end_member_template_processing ();
14553
14554 /* Restore the queue. */
14555 parser->unparsed_functions_queues
14556 = TREE_CHAIN (parser->unparsed_functions_queues);
14557}
14558
cd0be382 14559/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
14560 functions queue. */
14561
14562static void
14563cp_parser_save_default_args (cp_parser* parser, tree decl)
14564{
14565 tree probe;
14566
14567 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14568 probe;
14569 probe = TREE_CHAIN (probe))
14570 if (TREE_PURPOSE (probe))
14571 {
14572 TREE_PURPOSE (parser->unparsed_functions_queues)
14573 = tree_cons (NULL_TREE, decl,
14574 TREE_PURPOSE (parser->unparsed_functions_queues));
14575 break;
14576 }
14577 return;
14578}
14579
8218bd34
MM
14580/* FN is a FUNCTION_DECL which may contains a parameter with an
14581 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14582
14583static void
8218bd34 14584cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14585{
14586 cp_lexer *saved_lexer;
14587 cp_token_cache *tokens;
14588 bool saved_local_variables_forbidden_p;
14589 tree parameters;
8218bd34 14590
b92bc2a0
NS
14591 /* While we're parsing the default args, we might (due to the
14592 statement expression extension) encounter more classes. We want
14593 to handle them right away, but we don't want them getting mixed
14594 up with default args that are currently in the queue. */
14595 parser->unparsed_functions_queues
14596 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14597
8218bd34 14598 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14599 parameters;
14600 parameters = TREE_CHAIN (parameters))
14601 {
14602 if (!TREE_PURPOSE (parameters)
14603 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14604 continue;
14605
14606 /* Save away the current lexer. */
14607 saved_lexer = parser->lexer;
14608 /* Create a new one, using the tokens we have saved. */
14609 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14610 parser->lexer = cp_lexer_new_from_tokens (tokens);
14611
14612 /* Set the current source position to be the location of the
14613 first token in the default argument. */
3466b292 14614 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14615
14616 /* Local variable names (and the `this' keyword) may not appear
14617 in a default argument. */
14618 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14619 parser->local_variables_forbidden_p = true;
14620 /* Parse the assignment-expression. */
f128e1f3 14621 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 14622 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14623 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 14624 if (DECL_CLASS_SCOPE_P (fn))
e5976695 14625 pop_nested_class ();
a723baf1
MM
14626
14627 /* Restore saved state. */
14628 parser->lexer = saved_lexer;
14629 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14630 }
b92bc2a0
NS
14631
14632 /* Restore the queue. */
14633 parser->unparsed_functions_queues
14634 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
14635}
14636
14637/* Parse the operand of `sizeof' (or a similar operator). Returns
14638 either a TYPE or an expression, depending on the form of the
14639 input. The KEYWORD indicates which kind of expression we have
14640 encountered. */
14641
14642static tree
94edc4ab 14643cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14644{
14645 static const char *format;
14646 tree expr = NULL_TREE;
14647 const char *saved_message;
67c03833 14648 bool saved_integral_constant_expression_p;
a723baf1
MM
14649
14650 /* Initialize FORMAT the first time we get here. */
14651 if (!format)
14652 format = "types may not be defined in `%s' expressions";
14653
14654 /* Types cannot be defined in a `sizeof' expression. Save away the
14655 old message. */
14656 saved_message = parser->type_definition_forbidden_message;
14657 /* And create the new one. */
14658 parser->type_definition_forbidden_message
c68b0a84
KG
14659 = xmalloc (strlen (format)
14660 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14661 + 1 /* `\0' */);
a723baf1
MM
14662 sprintf ((char *) parser->type_definition_forbidden_message,
14663 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14664
14665 /* The restrictions on constant-expressions do not apply inside
14666 sizeof expressions. */
67c03833
JM
14667 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14668 parser->integral_constant_expression_p = false;
a723baf1 14669
3beb3abf
MM
14670 /* Do not actually evaluate the expression. */
14671 ++skip_evaluation;
a723baf1
MM
14672 /* If it's a `(', then we might be looking at the type-id
14673 construction. */
14674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14675 {
14676 tree type;
4f8163b1 14677 bool saved_in_type_id_in_expr_p;
a723baf1
MM
14678
14679 /* We can't be sure yet whether we're looking at a type-id or an
14680 expression. */
14681 cp_parser_parse_tentatively (parser);
14682 /* Consume the `('. */
14683 cp_lexer_consume_token (parser->lexer);
14684 /* Parse the type-id. */
4f8163b1
MM
14685 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14686 parser->in_type_id_in_expr_p = true;
a723baf1 14687 type = cp_parser_type_id (parser);
4f8163b1 14688 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
14689 /* Now, look for the trailing `)'. */
14690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14691 /* If all went well, then we're done. */
14692 if (cp_parser_parse_definitely (parser))
14693 {
14694 /* Build a list of decl-specifiers; right now, we have only
14695 a single type-specifier. */
14696 type = build_tree_list (NULL_TREE,
14697 type);
14698
14699 /* Call grokdeclarator to figure out what type this is. */
14700 expr = grokdeclarator (NULL_TREE,
14701 type,
14702 TYPENAME,
14703 /*initialized=*/0,
14704 /*attrlist=*/NULL);
14705 }
14706 }
14707
14708 /* If the type-id production did not work out, then we must be
14709 looking at the unary-expression production. */
14710 if (!expr)
14711 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14712 /* Go back to evaluating expressions. */
14713 --skip_evaluation;
a723baf1
MM
14714
14715 /* Free the message we created. */
14716 free ((char *) parser->type_definition_forbidden_message);
14717 /* And restore the old one. */
14718 parser->type_definition_forbidden_message = saved_message;
67c03833 14719 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
14720
14721 return expr;
14722}
14723
14724/* If the current declaration has no declarator, return true. */
14725
14726static bool
14727cp_parser_declares_only_class_p (cp_parser *parser)
14728{
14729 /* If the next token is a `;' or a `,' then there is no
14730 declarator. */
14731 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14732 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14733}
14734
14735/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14736 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14737
14738static bool
94edc4ab 14739cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14740{
14741 while (decl_specifiers)
14742 {
14743 /* See if this decl-specifier is `friend'. */
14744 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14745 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14746 return true;
14747
14748 /* Go on to the next decl-specifier. */
14749 decl_specifiers = TREE_CHAIN (decl_specifiers);
14750 }
14751
14752 return false;
14753}
14754
14755/* If the next token is of the indicated TYPE, consume it. Otherwise,
14756 issue an error message indicating that TOKEN_DESC was expected.
14757
14758 Returns the token consumed, if the token had the appropriate type.
14759 Otherwise, returns NULL. */
14760
14761static cp_token *
94edc4ab
NN
14762cp_parser_require (cp_parser* parser,
14763 enum cpp_ttype type,
14764 const char* token_desc)
a723baf1
MM
14765{
14766 if (cp_lexer_next_token_is (parser->lexer, type))
14767 return cp_lexer_consume_token (parser->lexer);
14768 else
14769 {
e5976695
MM
14770 /* Output the MESSAGE -- unless we're parsing tentatively. */
14771 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
14772 {
14773 char *message = concat ("expected ", token_desc, NULL);
14774 cp_parser_error (parser, message);
14775 free (message);
14776 }
a723baf1
MM
14777 return NULL;
14778 }
14779}
14780
14781/* Like cp_parser_require, except that tokens will be skipped until
14782 the desired token is found. An error message is still produced if
14783 the next token is not as expected. */
14784
14785static void
94edc4ab
NN
14786cp_parser_skip_until_found (cp_parser* parser,
14787 enum cpp_ttype type,
14788 const char* token_desc)
a723baf1
MM
14789{
14790 cp_token *token;
14791 unsigned nesting_depth = 0;
14792
14793 if (cp_parser_require (parser, type, token_desc))
14794 return;
14795
14796 /* Skip tokens until the desired token is found. */
14797 while (true)
14798 {
14799 /* Peek at the next token. */
14800 token = cp_lexer_peek_token (parser->lexer);
14801 /* If we've reached the token we want, consume it and
14802 stop. */
14803 if (token->type == type && !nesting_depth)
14804 {
14805 cp_lexer_consume_token (parser->lexer);
14806 return;
14807 }
14808 /* If we've run out of tokens, stop. */
14809 if (token->type == CPP_EOF)
14810 return;
14811 if (token->type == CPP_OPEN_BRACE
14812 || token->type == CPP_OPEN_PAREN
14813 || token->type == CPP_OPEN_SQUARE)
14814 ++nesting_depth;
14815 else if (token->type == CPP_CLOSE_BRACE
14816 || token->type == CPP_CLOSE_PAREN
14817 || token->type == CPP_CLOSE_SQUARE)
14818 {
14819 if (nesting_depth-- == 0)
14820 return;
14821 }
14822 /* Consume this token. */
14823 cp_lexer_consume_token (parser->lexer);
14824 }
14825}
14826
14827/* If the next token is the indicated keyword, consume it. Otherwise,
14828 issue an error message indicating that TOKEN_DESC was expected.
14829
14830 Returns the token consumed, if the token had the appropriate type.
14831 Otherwise, returns NULL. */
14832
14833static cp_token *
94edc4ab
NN
14834cp_parser_require_keyword (cp_parser* parser,
14835 enum rid keyword,
14836 const char* token_desc)
a723baf1
MM
14837{
14838 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14839
14840 if (token && token->keyword != keyword)
14841 {
14842 dyn_string_t error_msg;
14843
14844 /* Format the error message. */
14845 error_msg = dyn_string_new (0);
14846 dyn_string_append_cstr (error_msg, "expected ");
14847 dyn_string_append_cstr (error_msg, token_desc);
14848 cp_parser_error (parser, error_msg->s);
14849 dyn_string_delete (error_msg);
14850 return NULL;
14851 }
14852
14853 return token;
14854}
14855
14856/* Returns TRUE iff TOKEN is a token that can begin the body of a
14857 function-definition. */
14858
14859static bool
94edc4ab 14860cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14861{
14862 return (/* An ordinary function-body begins with an `{'. */
14863 token->type == CPP_OPEN_BRACE
14864 /* A ctor-initializer begins with a `:'. */
14865 || token->type == CPP_COLON
14866 /* A function-try-block begins with `try'. */
14867 || token->keyword == RID_TRY
14868 /* The named return value extension begins with `return'. */
14869 || token->keyword == RID_RETURN);
14870}
14871
14872/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14873 definition. */
14874
14875static bool
14876cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14877{
14878 cp_token *token;
14879
14880 token = cp_lexer_peek_token (parser->lexer);
14881 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14882}
14883
d17811fd 14884/* Returns TRUE iff the next token is the "," or ">" ending a
4d5297fa
GB
14885 template-argument. ">>" is also accepted (after the full
14886 argument was parsed) because it's probably a typo for "> >",
14887 and there is a specific diagnostic for this. */
d17811fd
MM
14888
14889static bool
14890cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14891{
14892 cp_token *token;
14893
14894 token = cp_lexer_peek_token (parser->lexer);
4d5297fa
GB
14895 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14896 || token->type == CPP_RSHIFT);
d17811fd 14897}
f4abade9
GB
14898
14899/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
14900 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
14901
14902static bool
14903cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
14904 size_t n)
14905{
14906 cp_token *token;
14907
14908 token = cp_lexer_peek_nth_token (parser->lexer, n);
14909 if (token->type == CPP_LESS)
14910 return true;
14911 /* Check for the sequence `<::' in the original code. It would be lexed as
14912 `[:', where `[' is a digraph, and there is no whitespace before
14913 `:'. */
14914 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
14915 {
14916 cp_token *token2;
14917 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
14918 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
14919 return true;
14920 }
14921 return false;
14922}
d17811fd 14923
a723baf1
MM
14924/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14925 or none_type otherwise. */
14926
14927static enum tag_types
94edc4ab 14928cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14929{
14930 switch (token->keyword)
14931 {
14932 case RID_CLASS:
14933 return class_type;
14934 case RID_STRUCT:
14935 return record_type;
14936 case RID_UNION:
14937 return union_type;
14938
14939 default:
14940 return none_type;
14941 }
14942}
14943
14944/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14945
14946static void
14947cp_parser_check_class_key (enum tag_types class_key, tree type)
14948{
14949 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14950 pedwarn ("`%s' tag used in naming `%#T'",
14951 class_key == union_type ? "union"
14952 : class_key == record_type ? "struct" : "class",
14953 type);
14954}
14955
cd0be382 14956/* Issue an error message if DECL is redeclared with different
37d407a1
KL
14957 access than its original declaration [class.access.spec/3].
14958 This applies to nested classes and nested class templates.
14959 [class.mem/1]. */
14960
14961static void cp_parser_check_access_in_redeclaration (tree decl)
14962{
14963 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14964 return;
14965
14966 if ((TREE_PRIVATE (decl)
14967 != (current_access_specifier == access_private_node))
14968 || (TREE_PROTECTED (decl)
14969 != (current_access_specifier == access_protected_node)))
14970 error ("%D redeclared with different access", decl);
14971}
14972
a723baf1
MM
14973/* Look for the `template' keyword, as a syntactic disambiguator.
14974 Return TRUE iff it is present, in which case it will be
14975 consumed. */
14976
14977static bool
14978cp_parser_optional_template_keyword (cp_parser *parser)
14979{
14980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14981 {
14982 /* The `template' keyword can only be used within templates;
14983 outside templates the parser can always figure out what is a
14984 template and what is not. */
14985 if (!processing_template_decl)
14986 {
14987 error ("`template' (as a disambiguator) is only allowed "
14988 "within templates");
14989 /* If this part of the token stream is rescanned, the same
14990 error message would be generated. So, we purge the token
14991 from the stream. */
14992 cp_lexer_purge_token (parser->lexer);
14993 return false;
14994 }
14995 else
14996 {
14997 /* Consume the `template' keyword. */
14998 cp_lexer_consume_token (parser->lexer);
14999 return true;
15000 }
15001 }
15002
15003 return false;
15004}
15005
2050a1bb
MM
15006/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15007 set PARSER->SCOPE, and perform other related actions. */
15008
15009static void
15010cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15011{
15012 tree value;
15013 tree check;
15014
15015 /* Get the stored value. */
15016 value = cp_lexer_consume_token (parser->lexer)->value;
15017 /* Perform any access checks that were deferred. */
15018 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15019 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15020 /* Set the scope from the stored value. */
15021 parser->scope = TREE_VALUE (value);
15022 parser->qualifying_scope = TREE_TYPE (value);
15023 parser->object_scope = NULL_TREE;
15024}
15025
852dcbdd 15026/* Add tokens to CACHE until a non-nested END token appears. */
a723baf1
MM
15027
15028static void
15029cp_parser_cache_group (cp_parser *parser,
15030 cp_token_cache *cache,
15031 enum cpp_ttype end,
15032 unsigned depth)
15033{
15034 while (true)
15035 {
15036 cp_token *token;
15037
15038 /* Abort a parenthesized expression if we encounter a brace. */
15039 if ((end == CPP_CLOSE_PAREN || depth == 0)
15040 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15041 return;
a723baf1 15042 /* If we've reached the end of the file, stop. */
4bfb8bba 15043 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15044 return;
4bfb8bba
MM
15045 /* Consume the next token. */
15046 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15047 /* Add this token to the tokens we are saving. */
15048 cp_token_cache_push_token (cache, token);
15049 /* See if it starts a new group. */
15050 if (token->type == CPP_OPEN_BRACE)
15051 {
15052 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15053 if (depth == 0)
15054 return;
15055 }
15056 else if (token->type == CPP_OPEN_PAREN)
15057 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15058 else if (token->type == end)
15059 return;
15060 }
15061}
15062
15063/* Begin parsing tentatively. We always save tokens while parsing
15064 tentatively so that if the tentative parsing fails we can restore the
15065 tokens. */
15066
15067static void
94edc4ab 15068cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15069{
15070 /* Enter a new parsing context. */
15071 parser->context = cp_parser_context_new (parser->context);
15072 /* Begin saving tokens. */
15073 cp_lexer_save_tokens (parser->lexer);
15074 /* In order to avoid repetitive access control error messages,
15075 access checks are queued up until we are no longer parsing
15076 tentatively. */
8d241e0b 15077 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15078}
15079
15080/* Commit to the currently active tentative parse. */
15081
15082static void
94edc4ab 15083cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15084{
15085 cp_parser_context *context;
15086 cp_lexer *lexer;
15087
15088 /* Mark all of the levels as committed. */
15089 lexer = parser->lexer;
15090 for (context = parser->context; context->next; context = context->next)
15091 {
15092 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15093 break;
15094 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15095 while (!cp_lexer_saving_tokens (lexer))
15096 lexer = lexer->next;
15097 cp_lexer_commit_tokens (lexer);
15098 }
15099}
15100
15101/* Abort the currently active tentative parse. All consumed tokens
15102 will be rolled back, and no diagnostics will be issued. */
15103
15104static void
94edc4ab 15105cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15106{
15107 cp_parser_simulate_error (parser);
15108 /* Now, pretend that we want to see if the construct was
15109 successfully parsed. */
15110 cp_parser_parse_definitely (parser);
15111}
15112
34cd5ae7 15113/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15114 token stream. Otherwise, commit to the tokens we have consumed.
15115 Returns true if no error occurred; false otherwise. */
15116
15117static bool
94edc4ab 15118cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15119{
15120 bool error_occurred;
15121 cp_parser_context *context;
15122
34cd5ae7 15123 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15124 destroy that information. */
15125 error_occurred = cp_parser_error_occurred (parser);
15126 /* Remove the topmost context from the stack. */
15127 context = parser->context;
15128 parser->context = context->next;
15129 /* If no parse errors occurred, commit to the tentative parse. */
15130 if (!error_occurred)
15131 {
15132 /* Commit to the tokens read tentatively, unless that was
15133 already done. */
15134 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15135 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15136
15137 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15138 }
15139 /* Otherwise, if errors occurred, roll back our state so that things
15140 are just as they were before we began the tentative parse. */
15141 else
cf22909c
KL
15142 {
15143 cp_lexer_rollback_tokens (parser->lexer);
15144 pop_deferring_access_checks ();
15145 }
e5976695
MM
15146 /* Add the context to the front of the free list. */
15147 context->next = cp_parser_context_free_list;
15148 cp_parser_context_free_list = context;
15149
15150 return !error_occurred;
a723baf1
MM
15151}
15152
a723baf1
MM
15153/* Returns true if we are parsing tentatively -- but have decided that
15154 we will stick with this tentative parse, even if errors occur. */
15155
15156static bool
94edc4ab 15157cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15158{
15159 return (cp_parser_parsing_tentatively (parser)
15160 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15161}
15162
4de8668e 15163/* Returns nonzero iff an error has occurred during the most recent
a723baf1
MM
15164 tentative parse. */
15165
15166static bool
94edc4ab 15167cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15168{
15169 return (cp_parser_parsing_tentatively (parser)
15170 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15171}
15172
4de8668e 15173/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15174
15175static bool
94edc4ab 15176cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15177{
15178 return parser->allow_gnu_extensions_p;
15179}
15180
15181\f
15182
15183/* The parser. */
15184
15185static GTY (()) cp_parser *the_parser;
15186
15187/* External interface. */
15188
d1bd0ded 15189/* Parse one entire translation unit. */
a723baf1 15190
d1bd0ded
GK
15191void
15192c_parse_file (void)
a723baf1
MM
15193{
15194 bool error_occurred;
15195
15196 the_parser = cp_parser_new ();
78757caa
KL
15197 push_deferring_access_checks (flag_access_control
15198 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15199 error_occurred = cp_parser_translation_unit (the_parser);
15200 the_parser = NULL;
a723baf1
MM
15201}
15202
15203/* Clean up after parsing the entire translation unit. */
15204
15205void
94edc4ab 15206free_parser_stacks (void)
a723baf1
MM
15207{
15208 /* Nothing to do. */
15209}
15210
15211/* This variable must be provided by every front end. */
15212
15213int yydebug;
15214
15215#include "gt-cp-parser.h"