]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
encoding.h: Wrap the functions with extern "C" for C++ mode.
[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"
62d1db17 37#include "target.h"
a723baf1
MM
38
39\f
40/* The lexer. */
41
42/* Overview
43 --------
44
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
46 look-ahead.
47
48 Methodology
49 -----------
50
51 We use a circular buffer to store incoming tokens.
52
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
59
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
21526606 62
a723baf1
MM
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
65 circular buffer. */
66
67/* A C++ token. */
68
69typedef struct cp_token GTY (())
70{
71 /* The kind of token. */
df2b750f 72 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
df2b750f 75 ENUM_BITFIELD (rid) keyword : 8;
f4abade9
GB
76 /* Token flags. */
77 unsigned char flags;
522df488
DN
78 /* The value associated with this token, if any. */
79 tree value;
82a98427
NS
80 /* The location at which this token was found. */
81 location_t location;
a723baf1
MM
82} cp_token;
83
522df488
DN
84/* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
a723baf1 86
522df488 87#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
a723baf1
MM
88
89/* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
21526606 93 complete.)
a723baf1
MM
94
95 This somewhat ungainly data structure (as opposed to, say, a
34cd5ae7 96 variable-length array), is used due to constraints imposed by the
a723baf1
MM
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
99
100typedef struct cp_token_block GTY (())
101{
102 /* The tokens. */
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
105 size_t num_tokens;
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
110} cp_token_block;
111
112typedef struct cp_token_cache GTY (())
113{
114 /* The first block in the cache. NULL if there are no tokens in the
115 cache. */
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
118 cache. */
119 cp_token_block *last;
120} cp_token_cache;
121
9bcb9aae 122/* Prototypes. */
a723baf1 123
21526606 124static cp_token_cache *cp_token_cache_new
a723baf1
MM
125 (void);
126static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
128
129/* Create a new cp_token_cache. */
130
131static cp_token_cache *
bf9d3c27 132cp_token_cache_new (void)
a723baf1 133{
c68b0a84 134 return ggc_alloc_cleared (sizeof (cp_token_cache));
a723baf1
MM
135}
136
137/* Add *TOKEN to *CACHE. */
138
139static void
140cp_token_cache_push_token (cp_token_cache *cache,
141 cp_token *token)
142{
143 cp_token_block *b = cache->last;
144
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 {
c68b0a84 148 b = ggc_alloc_cleared (sizeof (cp_token_block));
a723baf1
MM
149 b->prev = cache->last;
150 if (cache->last)
151 {
152 cache->last->next = b;
153 cache->last = b;
154 }
155 else
156 cache->first = cache->last = b;
157 }
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
160}
161
162/* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
164 it to the parser. */
165
166typedef struct cp_lexer GTY (())
167{
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
1431042e 171 cp_token * GTY ((skip)) buffer_end;
a723baf1 172 /* The first valid token in the buffer, or NULL if none. */
1431042e 173 cp_token * GTY ((skip)) first_token;
a723baf1
MM
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
1431042e 176 cp_token * GTY ((skip)) next_token;
a723baf1
MM
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
1431042e 183 cp_token * GTY ((skip)) last_token;
a723baf1
MM
184
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
189
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
194
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
199
200 /* The STRING_CST tokens encountered while processing the current
201 string literal. */
202 varray_type string_tokens;
203
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
206 bool main_lexer_p;
207
208 /* True if we should output debugging information. */
209 bool debugging_p;
210
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
213} cp_lexer;
214
215/* Prototypes. */
216
17211ab5 217static cp_lexer *cp_lexer_new_main
94edc4ab 218 (void);
a723baf1 219static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 220 (struct cp_token_cache *);
a723baf1 221static int cp_lexer_saving_tokens
94edc4ab 222 (const cp_lexer *);
a723baf1 223static cp_token *cp_lexer_next_token
94edc4ab 224 (cp_lexer *, cp_token *);
a668c6ad
MM
225static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
21526606 227static ptrdiff_t cp_lexer_token_difference
94edc4ab 228 (cp_lexer *, cp_token *, cp_token *);
a723baf1 229static cp_token *cp_lexer_read_token
94edc4ab 230 (cp_lexer *);
a723baf1 231static void cp_lexer_maybe_grow_buffer
94edc4ab 232 (cp_lexer *);
a723baf1 233static void cp_lexer_get_preprocessor_token
94edc4ab 234 (cp_lexer *, cp_token *);
a723baf1 235static cp_token *cp_lexer_peek_token
94edc4ab 236 (cp_lexer *);
a723baf1 237static cp_token *cp_lexer_peek_nth_token
94edc4ab 238 (cp_lexer *, size_t);
f7b5ecd9 239static inline bool cp_lexer_next_token_is
94edc4ab 240 (cp_lexer *, enum cpp_ttype);
a723baf1 241static bool cp_lexer_next_token_is_not
94edc4ab 242 (cp_lexer *, enum cpp_ttype);
a723baf1 243static bool cp_lexer_next_token_is_keyword
94edc4ab 244 (cp_lexer *, enum rid);
21526606 245static cp_token *cp_lexer_consume_token
94edc4ab 246 (cp_lexer *);
a723baf1
MM
247static void cp_lexer_purge_token
248 (cp_lexer *);
249static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251static void cp_lexer_save_tokens
94edc4ab 252 (cp_lexer *);
a723baf1 253static void cp_lexer_commit_tokens
94edc4ab 254 (cp_lexer *);
a723baf1 255static void cp_lexer_rollback_tokens
94edc4ab 256 (cp_lexer *);
21526606 257static inline void cp_lexer_set_source_position_from_token
94edc4ab 258 (cp_lexer *, const cp_token *);
a723baf1 259static void cp_lexer_print_token
94edc4ab 260 (FILE *, cp_token *);
21526606 261static inline bool cp_lexer_debugging_p
94edc4ab 262 (cp_lexer *);
a723baf1 263static void cp_lexer_start_debugging
94edc4ab 264 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 265static void cp_lexer_stop_debugging
94edc4ab 266 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
267
268/* Manifest constants. */
269
270#define CP_TOKEN_BUFFER_SIZE 5
271#define CP_SAVED_TOKENS_SIZE 5
272
273/* A token type for keywords, as opposed to ordinary identifiers. */
274#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275
276/* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281
282/* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288
289/* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292
293/* Variables. */
294
295/* The stream to which debugging output should be written. */
296static FILE *cp_lexer_debug_stream;
297
17211ab5
GK
298/* Create a new main C++ lexer, the lexer that gets tokens from the
299 preprocessor. */
a723baf1
MM
300
301static cp_lexer *
17211ab5 302cp_lexer_new_main (void)
a723baf1
MM
303{
304 cp_lexer *lexer;
17211ab5
GK
305 cp_token first_token;
306
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 311 c_common_no_more_pch ();
a723baf1
MM
312
313 /* Allocate the memory. */
c68b0a84 314 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
315
316 /* Create the circular buffer. */
c68b0a84 317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
a723baf1
MM
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319
17211ab5
GK
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
325
326 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 327 lexer->main_lexer_p = true;
a723baf1
MM
328
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
21526606 331
a723baf1
MM
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
337
338 return lexer;
339}
340
341/* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
343
344static cp_lexer *
345cp_lexer_new_from_tokens (cp_token_cache *tokens)
346{
347 cp_lexer *lexer;
348 cp_token *token;
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
351
17211ab5 352 /* Allocate the memory. */
c68b0a84 353 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
354
355 /* Create a new buffer, appropriately sized. */
356 num_tokens = 0;
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
c68b0a84 359 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
a723baf1 360 lexer->buffer_end = lexer->buffer + num_tokens;
21526606 361
a723baf1
MM
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
365 {
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
368 }
369
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
376
17211ab5
GK
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
379
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
21526606 382
17211ab5
GK
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
388
a723baf1
MM
389 return lexer;
390}
391
4de8668e 392/* Returns nonzero if debugging information should be output. */
a723baf1 393
f7b5ecd9
MM
394static inline bool
395cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 396{
f7b5ecd9
MM
397 return lexer->debugging_p;
398}
399
400/* Set the current source position from the information stored in
401 TOKEN. */
402
403static inline void
94edc4ab
NN
404cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
f7b5ecd9
MM
406{
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
409
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
82a98427 412 input_location = token->location;
a723baf1
MM
413}
414
415/* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
417
f7b5ecd9 418static inline cp_token *
94edc4ab 419cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
420{
421 token++;
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
424 return token;
425}
426
a668c6ad
MM
427/* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
429
430static inline cp_token *
431cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432{
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
435 return token - 1;
436}
437
4de8668e 438/* nonzero if we are presently saving tokens. */
f7b5ecd9
MM
439
440static int
94edc4ab 441cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
442{
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
444}
445
a723baf1
MM
446/* Return a pointer to the token that is N tokens beyond TOKEN in the
447 buffer. */
448
449static cp_token *
450cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
451{
452 token += n;
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
455 return token;
456}
457
458/* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
460
461static ptrdiff_t
94edc4ab 462cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
463{
464 if (finish >= start)
465 return finish - start;
466 else
467 return ((lexer->buffer_end - lexer->buffer)
468 - (start - finish));
469}
470
471/* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
473
474static cp_token *
94edc4ab 475cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
476{
477 cp_token *token;
478
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
481
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
488
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
491
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
494
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
497
e6cc3a24
ZW
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
500 constant elements.
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
a723baf1 504 {
0173bb6f
AO
505 if (c_lex_string_translate)
506 {
507 tree value = token->value;
508 tree type;
e6cc3a24 509
0173bb6f
AO
510 /* We might as well go ahead and release the chained
511 translated string such that we can reuse its memory. */
512 if (TREE_CHAIN (value))
513 value = TREE_CHAIN (token->value);
514
515 /* Get the current type. It will be an ARRAY_TYPE. */
516 type = TREE_TYPE (value);
517 /* Use build_cplus_array_type to rebuild the array, thereby
518 getting the right type. */
519 type = build_cplus_array_type (TREE_TYPE (type),
520 TYPE_DOMAIN (type));
521 /* Reset the type of the token. */
522 TREE_TYPE (value) = type;
523 }
a723baf1
MM
524 }
525
526 return token;
527}
528
529/* If the circular buffer is full, make it bigger. */
530
531static void
94edc4ab 532cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
533{
534 /* If the buffer is full, enlarge it. */
535 if (lexer->last_token == lexer->first_token)
536 {
537 cp_token *new_buffer;
538 cp_token *old_buffer;
539 cp_token *new_first_token;
540 ptrdiff_t buffer_length;
541 size_t num_tokens_to_copy;
542
543 /* Remember the current buffer pointer. It will become invalid,
544 but we will need to do pointer arithmetic involving this
545 value. */
546 old_buffer = lexer->buffer;
547 /* Compute the current buffer size. */
548 buffer_length = lexer->buffer_end - lexer->buffer;
549 /* Allocate a buffer twice as big. */
21526606 550 new_buffer = ggc_realloc (lexer->buffer,
c68b0a84 551 2 * buffer_length * sizeof (cp_token));
21526606 552
a723baf1
MM
553 /* Because the buffer is circular, logically consecutive tokens
554 are not necessarily placed consecutively in memory.
555 Therefore, we must keep move the tokens that were before
556 FIRST_TOKEN to the second half of the newly allocated
557 buffer. */
558 num_tokens_to_copy = (lexer->first_token - old_buffer);
559 memcpy (new_buffer + buffer_length,
560 new_buffer,
561 num_tokens_to_copy * sizeof (cp_token));
562 /* Clear the rest of the buffer. We never look at this storage,
563 but the garbage collector may. */
21526606 564 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
a723baf1
MM
565 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
566
567 /* Now recompute all of the buffer pointers. */
21526606 568 new_first_token
a723baf1
MM
569 = new_buffer + (lexer->first_token - old_buffer);
570 if (lexer->next_token != NULL)
571 {
572 ptrdiff_t next_token_delta;
573
574 if (lexer->next_token > lexer->first_token)
575 next_token_delta = lexer->next_token - lexer->first_token;
576 else
21526606 577 next_token_delta =
a723baf1
MM
578 buffer_length - (lexer->first_token - lexer->next_token);
579 lexer->next_token = new_first_token + next_token_delta;
580 }
581 lexer->last_token = new_first_token + buffer_length;
582 lexer->buffer = new_buffer;
583 lexer->buffer_end = new_buffer + buffer_length * 2;
584 lexer->first_token = new_first_token;
585 }
586}
587
588/* Store the next token from the preprocessor in *TOKEN. */
589
21526606 590static void
94edc4ab
NN
591cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
592 cp_token *token)
a723baf1
MM
593{
594 bool done;
595
596 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 597 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
598 {
599 token->type = CPP_EOF;
82a98427
NS
600 token->location.line = 0;
601 token->location.file = NULL;
a723baf1
MM
602 token->value = NULL_TREE;
603 token->keyword = RID_MAX;
604
605 return;
606 }
607
608 done = false;
609 /* Keep going until we get a token we like. */
610 while (!done)
611 {
612 /* Get a new token from the preprocessor. */
f4abade9 613 token->type = c_lex_with_flags (&token->value, &token->flags);
a723baf1
MM
614 /* Issue messages about tokens we cannot process. */
615 switch (token->type)
616 {
617 case CPP_ATSIGN:
618 case CPP_HASH:
619 case CPP_PASTE:
620 error ("invalid token");
621 break;
622
a723baf1
MM
623 default:
624 /* This is a good token, so we exit the loop. */
625 done = true;
626 break;
627 }
628 }
629 /* Now we've got our token. */
82a98427 630 token->location = input_location;
a723baf1
MM
631
632 /* Check to see if this token is a keyword. */
21526606 633 if (token->type == CPP_NAME
a723baf1
MM
634 && C_IS_RESERVED_WORD (token->value))
635 {
636 /* Mark this token as a keyword. */
637 token->type = CPP_KEYWORD;
638 /* Record which keyword. */
639 token->keyword = C_RID_CODE (token->value);
640 /* Update the value. Some keywords are mapped to particular
641 entities, rather than simply having the value of the
642 corresponding IDENTIFIER_NODE. For example, `__const' is
643 mapped to `const'. */
644 token->value = ridpointers[token->keyword];
645 }
646 else
647 token->keyword = RID_MAX;
648}
649
650/* Return a pointer to the next token in the token stream, but do not
651 consume it. */
652
653static cp_token *
94edc4ab 654cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
655{
656 cp_token *token;
657
658 /* If there are no tokens, read one now. */
659 if (!lexer->next_token)
660 cp_lexer_read_token (lexer);
661
662 /* Provide debugging output. */
663 if (cp_lexer_debugging_p (lexer))
664 {
665 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
666 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
667 fprintf (cp_lexer_debug_stream, "\n");
668 }
669
670 token = lexer->next_token;
671 cp_lexer_set_source_position_from_token (lexer, token);
672 return token;
673}
674
675/* Return true if the next token has the indicated TYPE. */
676
677static bool
94edc4ab 678cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
679{
680 cp_token *token;
681
682 /* Peek at the next token. */
683 token = cp_lexer_peek_token (lexer);
684 /* Check to see if it has the indicated TYPE. */
685 return token->type == type;
686}
687
688/* Return true if the next token does not have the indicated TYPE. */
689
690static bool
94edc4ab 691cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
692{
693 return !cp_lexer_next_token_is (lexer, type);
694}
695
696/* Return true if the next token is the indicated KEYWORD. */
697
698static bool
94edc4ab 699cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
700{
701 cp_token *token;
702
703 /* Peek at the next token. */
704 token = cp_lexer_peek_token (lexer);
705 /* Check to see if it is the indicated keyword. */
706 return token->keyword == keyword;
707}
708
709/* Return a pointer to the Nth token in the token stream. If N is 1,
710 then this is precisely equivalent to cp_lexer_peek_token. */
711
712static cp_token *
94edc4ab 713cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
714{
715 cp_token *token;
716
717 /* N is 1-based, not zero-based. */
718 my_friendly_assert (n > 0, 20000224);
719
720 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
721 token = lexer->next_token;
722 /* If there are no tokens in the buffer, get one now. */
723 if (!token)
724 {
725 cp_lexer_read_token (lexer);
726 token = lexer->next_token;
727 }
728
729 /* Now, read tokens until we have enough. */
730 while (--n > 0)
731 {
732 /* Advance to the next token. */
733 token = cp_lexer_next_token (lexer, token);
734 /* If that's all the tokens we have, read a new one. */
735 if (token == lexer->last_token)
736 token = cp_lexer_read_token (lexer);
737 }
738
739 return token;
740}
741
742/* Consume the next token. The pointer returned is valid only until
743 another token is read. Callers should preserve copy the token
744 explicitly if they will need its value for a longer period of
745 time. */
746
747static cp_token *
94edc4ab 748cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
749{
750 cp_token *token;
751
752 /* If there are no tokens, read one now. */
753 if (!lexer->next_token)
754 cp_lexer_read_token (lexer);
755
756 /* Remember the token we'll be returning. */
757 token = lexer->next_token;
758
759 /* Increment NEXT_TOKEN. */
21526606 760 lexer->next_token = cp_lexer_next_token (lexer,
a723baf1
MM
761 lexer->next_token);
762 /* Check to see if we're all out of tokens. */
763 if (lexer->next_token == lexer->last_token)
764 lexer->next_token = NULL;
765
766 /* If we're not saving tokens, then move FIRST_TOKEN too. */
767 if (!cp_lexer_saving_tokens (lexer))
768 {
769 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
770 if (!lexer->next_token)
771 lexer->first_token = NULL;
772 else
773 lexer->first_token = lexer->next_token;
774 }
775
776 /* Provide debugging output. */
777 if (cp_lexer_debugging_p (lexer))
778 {
779 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
780 cp_lexer_print_token (cp_lexer_debug_stream, token);
781 fprintf (cp_lexer_debug_stream, "\n");
782 }
783
784 return token;
785}
786
787/* Permanently remove the next token from the token stream. There
788 must be a valid next token already; this token never reads
789 additional tokens from the preprocessor. */
790
791static void
792cp_lexer_purge_token (cp_lexer *lexer)
793{
794 cp_token *token;
795 cp_token *next_token;
796
797 token = lexer->next_token;
21526606 798 while (true)
a723baf1
MM
799 {
800 next_token = cp_lexer_next_token (lexer, token);
801 if (next_token == lexer->last_token)
802 break;
803 *token = *next_token;
804 token = next_token;
805 }
806
807 lexer->last_token = token;
808 /* The token purged may have been the only token remaining; if so,
809 clear NEXT_TOKEN. */
810 if (lexer->next_token == token)
811 lexer->next_token = NULL;
812}
813
814/* Permanently remove all tokens after TOKEN, up to, but not
815 including, the token that will be returned next by
816 cp_lexer_peek_token. */
817
818static void
819cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
820{
821 cp_token *peek;
822 cp_token *t1;
823 cp_token *t2;
824
825 if (lexer->next_token)
826 {
827 /* Copy the tokens that have not yet been read to the location
828 immediately following TOKEN. */
829 t1 = cp_lexer_next_token (lexer, token);
830 t2 = peek = cp_lexer_peek_token (lexer);
831 /* Move tokens into the vacant area between TOKEN and PEEK. */
832 while (t2 != lexer->last_token)
833 {
834 *t1 = *t2;
835 t1 = cp_lexer_next_token (lexer, t1);
836 t2 = cp_lexer_next_token (lexer, t2);
837 }
838 /* Now, the next available token is right after TOKEN. */
839 lexer->next_token = cp_lexer_next_token (lexer, token);
840 /* And the last token is wherever we ended up. */
841 lexer->last_token = t1;
842 }
843 else
844 {
845 /* There are no tokens in the buffer, so there is nothing to
846 copy. The last token in the buffer is TOKEN itself. */
847 lexer->last_token = cp_lexer_next_token (lexer, token);
848 }
849}
850
851/* Begin saving tokens. All tokens consumed after this point will be
852 preserved. */
853
854static void
94edc4ab 855cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
856{
857 /* Provide debugging output. */
858 if (cp_lexer_debugging_p (lexer))
859 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
860
861 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
862 restore the tokens if required. */
863 if (!lexer->next_token)
864 cp_lexer_read_token (lexer);
865
866 VARRAY_PUSH_INT (lexer->saved_tokens,
867 cp_lexer_token_difference (lexer,
868 lexer->first_token,
869 lexer->next_token));
870}
871
872/* Commit to the portion of the token stream most recently saved. */
873
874static void
94edc4ab 875cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
876{
877 /* Provide debugging output. */
878 if (cp_lexer_debugging_p (lexer))
879 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
880
881 VARRAY_POP (lexer->saved_tokens);
882}
883
884/* Return all tokens saved since the last call to cp_lexer_save_tokens
885 to the token stream. Stop saving tokens. */
886
887static void
94edc4ab 888cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
889{
890 size_t delta;
891
892 /* Provide debugging output. */
893 if (cp_lexer_debugging_p (lexer))
894 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
895
896 /* Find the token that was the NEXT_TOKEN when we started saving
897 tokens. */
898 delta = VARRAY_TOP_INT(lexer->saved_tokens);
899 /* Make it the next token again now. */
900 lexer->next_token = cp_lexer_advance_token (lexer,
21526606 901 lexer->first_token,
a723baf1 902 delta);
15d2cb19 903 /* It might be the case that there were no tokens when we started
a723baf1
MM
904 saving tokens, but that there are some tokens now. */
905 if (!lexer->next_token && lexer->first_token)
906 lexer->next_token = lexer->first_token;
907
908 /* Stop saving tokens. */
909 VARRAY_POP (lexer->saved_tokens);
910}
911
a723baf1
MM
912/* Print a representation of the TOKEN on the STREAM. */
913
914static void
94edc4ab 915cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
916{
917 const char *token_type = NULL;
918
919 /* Figure out what kind of token this is. */
920 switch (token->type)
921 {
922 case CPP_EQ:
923 token_type = "EQ";
924 break;
925
926 case CPP_COMMA:
927 token_type = "COMMA";
928 break;
929
930 case CPP_OPEN_PAREN:
931 token_type = "OPEN_PAREN";
932 break;
933
934 case CPP_CLOSE_PAREN:
935 token_type = "CLOSE_PAREN";
936 break;
937
938 case CPP_OPEN_BRACE:
939 token_type = "OPEN_BRACE";
940 break;
941
942 case CPP_CLOSE_BRACE:
943 token_type = "CLOSE_BRACE";
944 break;
945
946 case CPP_SEMICOLON:
947 token_type = "SEMICOLON";
948 break;
949
950 case CPP_NAME:
951 token_type = "NAME";
952 break;
953
954 case CPP_EOF:
955 token_type = "EOF";
956 break;
957
958 case CPP_KEYWORD:
959 token_type = "keyword";
960 break;
961
962 /* This is not a token that we know how to handle yet. */
963 default:
964 break;
965 }
966
967 /* If we have a name for the token, print it out. Otherwise, we
968 simply give the numeric code. */
969 if (token_type)
970 fprintf (stream, "%s", token_type);
971 else
972 fprintf (stream, "%d", token->type);
973 /* And, for an identifier, print the identifier name. */
21526606 974 if (token->type == CPP_NAME
a723baf1
MM
975 /* Some keywords have a value that is not an IDENTIFIER_NODE.
976 For example, `struct' is mapped to an INTEGER_CST. */
21526606 977 || (token->type == CPP_KEYWORD
a723baf1
MM
978 && TREE_CODE (token->value) == IDENTIFIER_NODE))
979 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
980}
981
a723baf1
MM
982/* Start emitting debugging information. */
983
984static void
94edc4ab 985cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
986{
987 ++lexer->debugging_p;
988}
21526606 989
a723baf1
MM
990/* Stop emitting debugging information. */
991
992static void
94edc4ab 993cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
994{
995 --lexer->debugging_p;
996}
997
998\f
62d1db17
MM
999/* Decl-specifiers. */
1000
1001static void clear_decl_specs
1002 (cp_decl_specifier_seq *);
1003
1004/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1005
1006static void
1007clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1008{
1009 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1010}
1011
058b15c1
MM
1012/* Declarators. */
1013
1014/* Nothing other than the parser should be creating declarators;
1015 declarators are a semi-syntactic representation of C++ entities.
1016 Other parts of the front end that need to create entities (like
1017 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1018
1019static cp_declarator *make_id_declarator
1020 (tree);
1021static cp_declarator *make_call_declarator
1022 (cp_declarator *, cp_parameter_declarator *, tree, tree);
1023static cp_declarator *make_array_declarator
1024 (cp_declarator *, tree);
1025static cp_declarator *make_pointer_declarator
1026 (tree, cp_declarator *);
1027static cp_declarator *make_reference_declarator
1028 (tree, cp_declarator *);
1029static cp_parameter_declarator *make_parameter_declarator
62d1db17 1030 (cp_decl_specifier_seq *, cp_declarator *, tree);
058b15c1
MM
1031static cp_declarator *make_ptrmem_declarator
1032 (tree, tree, cp_declarator *);
1033
1034cp_declarator *cp_error_declarator;
1035
1036/* The obstack on which declarators and related data structures are
1037 allocated. */
1038static struct obstack declarator_obstack;
1039
1040/* Alloc BYTES from the declarator memory pool. */
1041
1042static inline void *
1043alloc_declarator (size_t bytes)
1044{
1045 return obstack_alloc (&declarator_obstack, bytes);
1046}
1047
1048/* Allocate a declarator of the indicated KIND. Clear fields that are
1049 common to all declarators. */
1050
1051static cp_declarator *
1052make_declarator (cp_declarator_kind kind)
1053{
1054 cp_declarator *declarator;
1055
1056 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1057 declarator->kind = kind;
1058 declarator->attributes = NULL_TREE;
1059 declarator->declarator = NULL;
1060
1061 return declarator;
1062}
1063
1064/* Make a declarator for a generalized identifier. */
1065
1066cp_declarator *
1067make_id_declarator (tree id)
1068{
1069 cp_declarator *declarator;
1070
1071 declarator = make_declarator (cdk_id);
1072 declarator->u.id.name = id;
1073 declarator->u.id.sfk = sfk_none;
1074
1075 return declarator;
1076}
1077
1078/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1079 of modifiers such as const or volatile to apply to the pointer
1080 type, represented as identifiers. */
1081
1082cp_declarator *
1083make_pointer_declarator (tree cv_qualifiers, cp_declarator *target)
1084{
1085 cp_declarator *declarator;
1086
1087 declarator = make_declarator (cdk_pointer);
1088 declarator->declarator = target;
1089 declarator->u.pointer.qualifiers = cv_qualifiers;
1090 declarator->u.pointer.class_type = NULL_TREE;
1091
1092 return declarator;
1093}
1094
1095/* Like make_pointer_declarator -- but for references. */
1096
1097cp_declarator *
1098make_reference_declarator (tree cv_qualifiers, cp_declarator *target)
1099{
1100 cp_declarator *declarator;
1101
1102 declarator = make_declarator (cdk_reference);
1103 declarator->declarator = target;
1104 declarator->u.pointer.qualifiers = cv_qualifiers;
1105 declarator->u.pointer.class_type = NULL_TREE;
1106
1107 return declarator;
1108}
1109
1110/* Like make_pointer_declarator -- but for a pointer to a non-static
1111 member of CLASS_TYPE. */
1112
1113cp_declarator *
1114make_ptrmem_declarator (tree cv_qualifiers, tree class_type,
1115 cp_declarator *pointee)
1116{
1117 cp_declarator *declarator;
1118
1119 declarator = make_declarator (cdk_ptrmem);
1120 declarator->declarator = pointee;
1121 declarator->u.pointer.qualifiers = cv_qualifiers;
1122 declarator->u.pointer.class_type = class_type;
1123
1124 return declarator;
1125}
1126
1127/* Make a declarator for the function given by TARGET, with the
1128 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1129 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1130 indicates what exceptions can be thrown. */
1131
1132cp_declarator *
1133make_call_declarator (cp_declarator *target,
1134 cp_parameter_declarator *parms,
1135 tree cv_qualifiers,
1136 tree exception_specification)
1137{
1138 cp_declarator *declarator;
1139
1140 declarator = make_declarator (cdk_function);
1141 declarator->declarator = target;
1142 declarator->u.function.parameters = parms;
1143 declarator->u.function.qualifiers = cv_qualifiers;
1144 declarator->u.function.exception_specification = exception_specification;
1145
1146 return declarator;
1147}
1148
1149/* Make a declarator for an array of BOUNDS elements, each of which is
1150 defined by ELEMENT. */
1151
1152cp_declarator *
1153make_array_declarator (cp_declarator *element, tree bounds)
1154{
1155 cp_declarator *declarator;
1156
1157 declarator = make_declarator (cdk_array);
1158 declarator->declarator = element;
1159 declarator->u.array.bounds = bounds;
1160
1161 return declarator;
1162}
1163
1164cp_parameter_declarator *no_parameters;
1165
1166/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1167 DECLARATOR and DEFAULT_ARGUMENT. */
1168
1169cp_parameter_declarator *
62d1db17 1170make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
058b15c1
MM
1171 cp_declarator *declarator,
1172 tree default_argument)
1173{
1174 cp_parameter_declarator *parameter;
1175
1176 parameter = ((cp_parameter_declarator *)
1177 alloc_declarator (sizeof (cp_parameter_declarator)));
1178 parameter->next = NULL;
62d1db17
MM
1179 if (decl_specifiers)
1180 parameter->decl_specifiers = *decl_specifiers;
1181 else
1182 clear_decl_specs (&parameter->decl_specifiers);
058b15c1
MM
1183 parameter->declarator = declarator;
1184 parameter->default_argument = default_argument;
1185 parameter->ellipsis_p = false;
1186
1187 return parameter;
1188}
1189
a723baf1
MM
1190/* The parser. */
1191
1192/* Overview
1193 --------
1194
1195 A cp_parser parses the token stream as specified by the C++
1196 grammar. Its job is purely parsing, not semantic analysis. For
1197 example, the parser breaks the token stream into declarators,
1198 expressions, statements, and other similar syntactic constructs.
1199 It does not check that the types of the expressions on either side
1200 of an assignment-statement are compatible, or that a function is
1201 not declared with a parameter of type `void'.
1202
1203 The parser invokes routines elsewhere in the compiler to perform
1204 semantic analysis and to build up the abstract syntax tree for the
21526606 1205 code processed.
a723baf1
MM
1206
1207 The parser (and the template instantiation code, which is, in a
1208 way, a close relative of parsing) are the only parts of the
1209 compiler that should be calling push_scope and pop_scope, or
1210 related functions. The parser (and template instantiation code)
1211 keeps track of what scope is presently active; everything else
1212 should simply honor that. (The code that generates static
1213 initializers may also need to set the scope, in order to check
1214 access control correctly when emitting the initializers.)
1215
1216 Methodology
1217 -----------
21526606 1218
a723baf1
MM
1219 The parser is of the standard recursive-descent variety. Upcoming
1220 tokens in the token stream are examined in order to determine which
1221 production to use when parsing a non-terminal. Some C++ constructs
1222 require arbitrary look ahead to disambiguate. For example, it is
1223 impossible, in the general case, to tell whether a statement is an
1224 expression or declaration without scanning the entire statement.
1225 Therefore, the parser is capable of "parsing tentatively." When the
1226 parser is not sure what construct comes next, it enters this mode.
1227 Then, while we attempt to parse the construct, the parser queues up
1228 error messages, rather than issuing them immediately, and saves the
1229 tokens it consumes. If the construct is parsed successfully, the
1230 parser "commits", i.e., it issues any queued error messages and
1231 the tokens that were being preserved are permanently discarded.
1232 If, however, the construct is not parsed successfully, the parser
1233 rolls back its state completely so that it can resume parsing using
1234 a different alternative.
1235
1236 Future Improvements
1237 -------------------
21526606 1238
a723baf1
MM
1239 The performance of the parser could probably be improved
1240 substantially. Some possible improvements include:
1241
1242 - The expression parser recurses through the various levels of
1243 precedence as specified in the grammar, rather than using an
1244 operator-precedence technique. Therefore, parsing a simple
1245 identifier requires multiple recursive calls.
1246
1247 - We could often eliminate the need to parse tentatively by
1248 looking ahead a little bit. In some places, this approach
1249 might not entirely eliminate the need to parse tentatively, but
1250 it might still speed up the average case. */
1251
1252/* Flags that are passed to some parsing functions. These values can
1253 be bitwise-ored together. */
1254
1255typedef enum cp_parser_flags
1256{
1257 /* No flags. */
1258 CP_PARSER_FLAGS_NONE = 0x0,
1259 /* The construct is optional. If it is not present, then no error
1260 should be issued. */
1261 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1262 /* When parsing a type-specifier, do not allow user-defined types. */
1263 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1264} cp_parser_flags;
1265
62b8a44e
NS
1266/* The different kinds of declarators we want to parse. */
1267
1268typedef enum cp_parser_declarator_kind
1269{
852dcbdd 1270 /* We want an abstract declarator. */
62b8a44e
NS
1271 CP_PARSER_DECLARATOR_ABSTRACT,
1272 /* We want a named declarator. */
1273 CP_PARSER_DECLARATOR_NAMED,
04c06002 1274 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
1275 CP_PARSER_DECLARATOR_EITHER
1276} cp_parser_declarator_kind;
1277
a723baf1
MM
1278/* A mapping from a token type to a corresponding tree node type. */
1279
1280typedef struct cp_parser_token_tree_map_node
1281{
1282 /* The token type. */
df2b750f 1283 ENUM_BITFIELD (cpp_ttype) token_type : 8;
a723baf1 1284 /* The corresponding tree code. */
df2b750f 1285 ENUM_BITFIELD (tree_code) tree_type : 8;
a723baf1
MM
1286} cp_parser_token_tree_map_node;
1287
1288/* A complete map consists of several ordinary entries, followed by a
1289 terminator. The terminating entry has a token_type of CPP_EOF. */
1290
1291typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1292
1293/* The status of a tentative parse. */
1294
1295typedef enum cp_parser_status_kind
1296{
1297 /* No errors have occurred. */
1298 CP_PARSER_STATUS_KIND_NO_ERROR,
1299 /* An error has occurred. */
1300 CP_PARSER_STATUS_KIND_ERROR,
1301 /* We are committed to this tentative parse, whether or not an error
1302 has occurred. */
1303 CP_PARSER_STATUS_KIND_COMMITTED
1304} cp_parser_status_kind;
1305
1306/* Context that is saved and restored when parsing tentatively. */
1307
1308typedef struct cp_parser_context GTY (())
1309{
1310 /* If this is a tentative parsing context, the status of the
1311 tentative parse. */
1312 enum cp_parser_status_kind status;
1313 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1314 that are looked up in this context must be looked up both in the
1315 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1316 the context of the containing expression. */
1317 tree object_type;
a723baf1
MM
1318 /* The next parsing context in the stack. */
1319 struct cp_parser_context *next;
1320} cp_parser_context;
1321
1322/* Prototypes. */
1323
1324/* Constructors and destructors. */
1325
1326static cp_parser_context *cp_parser_context_new
94edc4ab 1327 (cp_parser_context *);
a723baf1 1328
e5976695
MM
1329/* Class variables. */
1330
1431042e 1331static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
e5976695 1332
a723baf1
MM
1333/* Constructors and destructors. */
1334
1335/* Construct a new context. The context below this one on the stack
1336 is given by NEXT. */
1337
1338static cp_parser_context *
94edc4ab 1339cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1340{
1341 cp_parser_context *context;
1342
1343 /* Allocate the storage. */
e5976695
MM
1344 if (cp_parser_context_free_list != NULL)
1345 {
1346 /* Pull the first entry from the free list. */
1347 context = cp_parser_context_free_list;
1348 cp_parser_context_free_list = context->next;
c68b0a84 1349 memset (context, 0, sizeof (*context));
e5976695
MM
1350 }
1351 else
c68b0a84 1352 context = ggc_alloc_cleared (sizeof (cp_parser_context));
a723baf1
MM
1353 /* No errors have occurred yet in this context. */
1354 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1355 /* If this is not the bottomost context, copy information that we
1356 need from the previous context. */
1357 if (next)
1358 {
1359 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1360 expression, then we are parsing one in this context, too. */
1361 context->object_type = next->object_type;
a723baf1
MM
1362 /* Thread the stack. */
1363 context->next = next;
1364 }
1365
1366 return context;
1367}
1368
1369/* The cp_parser structure represents the C++ parser. */
1370
1371typedef struct cp_parser GTY(())
1372{
1373 /* The lexer from which we are obtaining tokens. */
1374 cp_lexer *lexer;
1375
1376 /* The scope in which names should be looked up. If NULL_TREE, then
1377 we look up names in the scope that is currently open in the
1378 source program. If non-NULL, this is either a TYPE or
21526606 1379 NAMESPACE_DECL for the scope in which we should look.
a723baf1
MM
1380
1381 This value is not cleared automatically after a name is looked
1382 up, so we must be careful to clear it before starting a new look
1383 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1384 will look up `Z' in the scope of `X', rather than the current
1385 scope.) Unfortunately, it is difficult to tell when name lookup
1386 is complete, because we sometimes peek at a token, look it up,
1387 and then decide not to consume it. */
1388 tree scope;
1389
1390 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1391 last lookup took place. OBJECT_SCOPE is used if an expression
1392 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1393 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1394 form "X::Y"; it refers to X. */
1395 tree object_scope;
1396 tree qualifying_scope;
1397
1398 /* A stack of parsing contexts. All but the bottom entry on the
1399 stack will be tentative contexts.
1400
1401 We parse tentatively in order to determine which construct is in
1402 use in some situations. For example, in order to determine
1403 whether a statement is an expression-statement or a
1404 declaration-statement we parse it tentatively as a
1405 declaration-statement. If that fails, we then reparse the same
1406 token stream as an expression-statement. */
1407 cp_parser_context *context;
1408
1409 /* True if we are parsing GNU C++. If this flag is not set, then
1410 GNU extensions are not recognized. */
1411 bool allow_gnu_extensions_p;
1412
1413 /* TRUE if the `>' token should be interpreted as the greater-than
1414 operator. FALSE if it is the end of a template-id or
1415 template-parameter-list. */
1416 bool greater_than_is_operator_p;
1417
1418 /* TRUE if default arguments are allowed within a parameter list
1419 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1420 them permissible. */
a723baf1 1421 bool default_arg_ok_p;
21526606 1422
a723baf1
MM
1423 /* TRUE if we are parsing an integral constant-expression. See
1424 [expr.const] for a precise definition. */
67c03833 1425 bool integral_constant_expression_p;
a723baf1 1426
14d22dd6
MM
1427 /* TRUE if we are parsing an integral constant-expression -- but a
1428 non-constant expression should be permitted as well. This flag
1429 is used when parsing an array bound so that GNU variable-length
1430 arrays are tolerated. */
67c03833 1431 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1432
1433 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1434 been seen that makes the expression non-constant. */
67c03833 1435 bool non_integral_constant_expression_p;
14d22dd6 1436
a723baf1
MM
1437 /* TRUE if local variable names and `this' are forbidden in the
1438 current context. */
1439 bool local_variables_forbidden_p;
1440
1441 /* TRUE if the declaration we are parsing is part of a
1442 linkage-specification of the form `extern string-literal
1443 declaration'. */
1444 bool in_unbraced_linkage_specification_p;
1445
1446 /* TRUE if we are presently parsing a declarator, after the
1447 direct-declarator. */
1448 bool in_declarator_p;
1449
4bb8ca28
MM
1450 /* TRUE if we are presently parsing a template-argument-list. */
1451 bool in_template_argument_list_p;
1452
0e59b3fb
MM
1453 /* TRUE if we are presently parsing the body of an
1454 iteration-statement. */
1455 bool in_iteration_statement_p;
1456
1457 /* TRUE if we are presently parsing the body of a switch
1458 statement. */
1459 bool in_switch_statement_p;
1460
4f8163b1
MM
1461 /* TRUE if we are parsing a type-id in an expression context. In
1462 such a situation, both "type (expr)" and "type (type)" are valid
1463 alternatives. */
1464 bool in_type_id_in_expr_p;
1465
a723baf1
MM
1466 /* If non-NULL, then we are parsing a construct where new type
1467 definitions are not permitted. The string stored here will be
1468 issued as an error message if a type is defined. */
1469 const char *type_definition_forbidden_message;
1470
8db1028e
NS
1471 /* A list of lists. The outer list is a stack, used for member
1472 functions of local classes. At each level there are two sub-list,
1473 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1474 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1475 TREE_VALUE's. The functions are chained in reverse declaration
1476 order.
1477
1478 The TREE_PURPOSE sublist contains those functions with default
1479 arguments that need post processing, and the TREE_VALUE sublist
1480 contains those functions with definitions that need post
1481 processing.
1482
1483 These lists can only be processed once the outermost class being
9bcb9aae 1484 defined is complete. */
a723baf1
MM
1485 tree unparsed_functions_queues;
1486
1487 /* The number of classes whose definitions are currently in
1488 progress. */
1489 unsigned num_classes_being_defined;
1490
1491 /* The number of template parameter lists that apply directly to the
1492 current declaration. */
1493 unsigned num_template_parameter_lists;
1494} cp_parser;
1495
04c06002 1496/* The type of a function that parses some kind of expression. */
94edc4ab 1497typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1498
1499/* Prototypes. */
1500
1501/* Constructors and destructors. */
1502
1503static cp_parser *cp_parser_new
94edc4ab 1504 (void);
a723baf1 1505
21526606 1506/* Routines to parse various constructs.
a723baf1
MM
1507
1508 Those that return `tree' will return the error_mark_node (rather
1509 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1510 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1511 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1512 whether or not a parse error occurred, you should always use
1513 cp_parser_error_occurred. If the construct is optional (indicated
1514 either by an `_opt' in the name of the function that does the
1515 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1516 the construct is not present. */
1517
1518/* Lexical conventions [gram.lex] */
1519
1520static tree cp_parser_identifier
94edc4ab 1521 (cp_parser *);
a723baf1
MM
1522
1523/* Basic concepts [gram.basic] */
1524
1525static bool cp_parser_translation_unit
94edc4ab 1526 (cp_parser *);
a723baf1
MM
1527
1528/* Expressions [gram.expr] */
1529
1530static tree cp_parser_primary_expression
b3445994 1531 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1532static tree cp_parser_id_expression
f3c2dfc6 1533 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1534static tree cp_parser_unqualified_id
f3c2dfc6 1535 (cp_parser *, bool, bool, bool);
a723baf1 1536static tree cp_parser_nested_name_specifier_opt
a668c6ad 1537 (cp_parser *, bool, bool, bool, bool);
a723baf1 1538static tree cp_parser_nested_name_specifier
a723baf1 1539 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1540static tree cp_parser_class_or_namespace_name
1541 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1542static tree cp_parser_postfix_expression
1543 (cp_parser *, bool);
7a3ea201
RH
1544static tree cp_parser_postfix_open_square_expression
1545 (cp_parser *, tree, bool);
1546static tree cp_parser_postfix_dot_deref_expression
1547 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1548static tree cp_parser_parenthesized_expression_list
39703eb9 1549 (cp_parser *, bool, bool *);
a723baf1 1550static void cp_parser_pseudo_destructor_name
94edc4ab 1551 (cp_parser *, tree *, tree *);
a723baf1
MM
1552static tree cp_parser_unary_expression
1553 (cp_parser *, bool);
1554static enum tree_code cp_parser_unary_operator
94edc4ab 1555 (cp_token *);
a723baf1 1556static tree cp_parser_new_expression
94edc4ab 1557 (cp_parser *);
a723baf1 1558static tree cp_parser_new_placement
94edc4ab 1559 (cp_parser *);
a723baf1 1560static tree cp_parser_new_type_id
058b15c1
MM
1561 (cp_parser *, tree *);
1562static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1563 (cp_parser *);
058b15c1 1564static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1565 (cp_parser *);
a723baf1 1566static tree cp_parser_new_initializer
94edc4ab 1567 (cp_parser *);
a723baf1 1568static tree cp_parser_delete_expression
94edc4ab 1569 (cp_parser *);
21526606 1570static tree cp_parser_cast_expression
a723baf1
MM
1571 (cp_parser *, bool);
1572static tree cp_parser_pm_expression
94edc4ab 1573 (cp_parser *);
a723baf1 1574static tree cp_parser_multiplicative_expression
94edc4ab 1575 (cp_parser *);
a723baf1 1576static tree cp_parser_additive_expression
94edc4ab 1577 (cp_parser *);
a723baf1 1578static tree cp_parser_shift_expression
94edc4ab 1579 (cp_parser *);
a723baf1 1580static tree cp_parser_relational_expression
94edc4ab 1581 (cp_parser *);
a723baf1 1582static tree cp_parser_equality_expression
94edc4ab 1583 (cp_parser *);
a723baf1 1584static tree cp_parser_and_expression
94edc4ab 1585 (cp_parser *);
a723baf1 1586static tree cp_parser_exclusive_or_expression
94edc4ab 1587 (cp_parser *);
a723baf1 1588static tree cp_parser_inclusive_or_expression
94edc4ab 1589 (cp_parser *);
a723baf1 1590static tree cp_parser_logical_and_expression
94edc4ab 1591 (cp_parser *);
21526606 1592static tree cp_parser_logical_or_expression
94edc4ab 1593 (cp_parser *);
a723baf1 1594static tree cp_parser_question_colon_clause
94edc4ab 1595 (cp_parser *, tree);
a723baf1 1596static tree cp_parser_assignment_expression
94edc4ab 1597 (cp_parser *);
a723baf1 1598static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1599 (cp_parser *);
a723baf1 1600static tree cp_parser_expression
94edc4ab 1601 (cp_parser *);
a723baf1 1602static tree cp_parser_constant_expression
14d22dd6 1603 (cp_parser *, bool, bool *);
7a3ea201
RH
1604static tree cp_parser_builtin_offsetof
1605 (cp_parser *);
a723baf1
MM
1606
1607/* Statements [gram.stmt.stmt] */
1608
1609static void cp_parser_statement
325c3691 1610 (cp_parser *, tree);
a723baf1 1611static tree cp_parser_labeled_statement
325c3691 1612 (cp_parser *, tree);
a723baf1 1613static tree cp_parser_expression_statement
325c3691 1614 (cp_parser *, tree);
a723baf1 1615static tree cp_parser_compound_statement
325c3691 1616 (cp_parser *, tree, bool);
a723baf1 1617static void cp_parser_statement_seq_opt
325c3691 1618 (cp_parser *, tree);
a723baf1 1619static tree cp_parser_selection_statement
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_condition
94edc4ab 1622 (cp_parser *);
a723baf1 1623static tree cp_parser_iteration_statement
94edc4ab 1624 (cp_parser *);
a723baf1 1625static void cp_parser_for_init_statement
94edc4ab 1626 (cp_parser *);
a723baf1 1627static tree cp_parser_jump_statement
94edc4ab 1628 (cp_parser *);
a723baf1 1629static void cp_parser_declaration_statement
94edc4ab 1630 (cp_parser *);
a723baf1
MM
1631
1632static tree cp_parser_implicitly_scoped_statement
94edc4ab 1633 (cp_parser *);
a723baf1 1634static void cp_parser_already_scoped_statement
94edc4ab 1635 (cp_parser *);
a723baf1
MM
1636
1637/* Declarations [gram.dcl.dcl] */
1638
1639static void cp_parser_declaration_seq_opt
94edc4ab 1640 (cp_parser *);
a723baf1 1641static void cp_parser_declaration
94edc4ab 1642 (cp_parser *);
a723baf1 1643static void cp_parser_block_declaration
94edc4ab 1644 (cp_parser *, bool);
a723baf1 1645static void cp_parser_simple_declaration
94edc4ab 1646 (cp_parser *, bool);
62d1db17
MM
1647static void cp_parser_decl_specifier_seq
1648 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1649static tree cp_parser_storage_class_specifier_opt
94edc4ab 1650 (cp_parser *);
a723baf1 1651static tree cp_parser_function_specifier_opt
62d1db17 1652 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1653static tree cp_parser_type_specifier
62d1db17
MM
1654 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1655 int *, bool *);
a723baf1 1656static tree cp_parser_simple_type_specifier
62d1db17 1657 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1658static tree cp_parser_type_name
94edc4ab 1659 (cp_parser *);
a723baf1 1660static tree cp_parser_elaborated_type_specifier
94edc4ab 1661 (cp_parser *, bool, bool);
a723baf1 1662static tree cp_parser_enum_specifier
94edc4ab 1663 (cp_parser *);
a723baf1 1664static void cp_parser_enumerator_list
94edc4ab 1665 (cp_parser *, tree);
21526606 1666static void cp_parser_enumerator_definition
94edc4ab 1667 (cp_parser *, tree);
a723baf1 1668static tree cp_parser_namespace_name
94edc4ab 1669 (cp_parser *);
a723baf1 1670static void cp_parser_namespace_definition
94edc4ab 1671 (cp_parser *);
a723baf1 1672static void cp_parser_namespace_body
94edc4ab 1673 (cp_parser *);
a723baf1 1674static tree cp_parser_qualified_namespace_specifier
94edc4ab 1675 (cp_parser *);
a723baf1 1676static void cp_parser_namespace_alias_definition
94edc4ab 1677 (cp_parser *);
a723baf1 1678static void cp_parser_using_declaration
94edc4ab 1679 (cp_parser *);
a723baf1 1680static void cp_parser_using_directive
94edc4ab 1681 (cp_parser *);
a723baf1 1682static void cp_parser_asm_definition
94edc4ab 1683 (cp_parser *);
a723baf1 1684static void cp_parser_linkage_specification
94edc4ab 1685 (cp_parser *);
a723baf1
MM
1686
1687/* Declarators [gram.dcl.decl] */
1688
1689static tree cp_parser_init_declarator
62d1db17 1690 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
058b15c1 1691static cp_declarator *cp_parser_declarator
4bb8ca28 1692 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
058b15c1 1693static cp_declarator *cp_parser_direct_declarator
7efa3e22 1694 (cp_parser *, cp_parser_declarator_kind, int *);
a723baf1 1695static enum tree_code cp_parser_ptr_operator
94edc4ab 1696 (cp_parser *, tree *, tree *);
a723baf1 1697static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1698 (cp_parser *);
a723baf1 1699static tree cp_parser_cv_qualifier_opt
94edc4ab 1700 (cp_parser *);
a723baf1 1701static tree cp_parser_declarator_id
94edc4ab 1702 (cp_parser *);
a723baf1 1703static tree cp_parser_type_id
94edc4ab 1704 (cp_parser *);
62d1db17
MM
1705static void cp_parser_type_specifier_seq
1706 (cp_parser *, cp_decl_specifier_seq *);
058b15c1 1707static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1708 (cp_parser *);
058b15c1
MM
1709static cp_parameter_declarator *cp_parser_parameter_declaration_list
1710 (cp_parser *, bool *);
1711static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1712 (cp_parser *, bool, bool *);
a723baf1
MM
1713static void cp_parser_function_body
1714 (cp_parser *);
1715static tree cp_parser_initializer
39703eb9 1716 (cp_parser *, bool *, bool *);
a723baf1 1717static tree cp_parser_initializer_clause
39703eb9 1718 (cp_parser *, bool *);
a723baf1 1719static tree cp_parser_initializer_list
39703eb9 1720 (cp_parser *, bool *);
a723baf1
MM
1721
1722static bool cp_parser_ctor_initializer_opt_and_function_body
1723 (cp_parser *);
1724
1725/* Classes [gram.class] */
1726
1727static tree cp_parser_class_name
a668c6ad 1728 (cp_parser *, bool, bool, bool, bool, bool, bool);
a723baf1 1729static tree cp_parser_class_specifier
94edc4ab 1730 (cp_parser *);
a723baf1 1731static tree cp_parser_class_head
38b305d0 1732 (cp_parser *, bool *, tree *);
a723baf1 1733static enum tag_types cp_parser_class_key
94edc4ab 1734 (cp_parser *);
a723baf1 1735static void cp_parser_member_specification_opt
94edc4ab 1736 (cp_parser *);
a723baf1 1737static void cp_parser_member_declaration
94edc4ab 1738 (cp_parser *);
a723baf1 1739static tree cp_parser_pure_specifier
94edc4ab 1740 (cp_parser *);
a723baf1 1741static tree cp_parser_constant_initializer
94edc4ab 1742 (cp_parser *);
a723baf1
MM
1743
1744/* Derived classes [gram.class.derived] */
1745
1746static tree cp_parser_base_clause
94edc4ab 1747 (cp_parser *);
a723baf1 1748static tree cp_parser_base_specifier
94edc4ab 1749 (cp_parser *);
a723baf1
MM
1750
1751/* Special member functions [gram.special] */
1752
1753static tree cp_parser_conversion_function_id
94edc4ab 1754 (cp_parser *);
a723baf1 1755static tree cp_parser_conversion_type_id
94edc4ab 1756 (cp_parser *);
058b15c1 1757static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1758 (cp_parser *);
a723baf1 1759static bool cp_parser_ctor_initializer_opt
94edc4ab 1760 (cp_parser *);
a723baf1 1761static void cp_parser_mem_initializer_list
94edc4ab 1762 (cp_parser *);
a723baf1 1763static tree cp_parser_mem_initializer
94edc4ab 1764 (cp_parser *);
a723baf1 1765static tree cp_parser_mem_initializer_id
94edc4ab 1766 (cp_parser *);
a723baf1
MM
1767
1768/* Overloading [gram.over] */
1769
1770static tree cp_parser_operator_function_id
94edc4ab 1771 (cp_parser *);
a723baf1 1772static tree cp_parser_operator
94edc4ab 1773 (cp_parser *);
a723baf1
MM
1774
1775/* Templates [gram.temp] */
1776
1777static void cp_parser_template_declaration
94edc4ab 1778 (cp_parser *, bool);
a723baf1 1779static tree cp_parser_template_parameter_list
94edc4ab 1780 (cp_parser *);
a723baf1 1781static tree cp_parser_template_parameter
058b15c1 1782 (cp_parser *, bool *);
a723baf1 1783static tree cp_parser_type_parameter
94edc4ab 1784 (cp_parser *);
a723baf1 1785static tree cp_parser_template_id
a668c6ad 1786 (cp_parser *, bool, bool, bool);
a723baf1 1787static tree cp_parser_template_name
a668c6ad 1788 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1789static tree cp_parser_template_argument_list
94edc4ab 1790 (cp_parser *);
a723baf1 1791static tree cp_parser_template_argument
94edc4ab 1792 (cp_parser *);
a723baf1 1793static void cp_parser_explicit_instantiation
94edc4ab 1794 (cp_parser *);
a723baf1 1795static void cp_parser_explicit_specialization
94edc4ab 1796 (cp_parser *);
a723baf1
MM
1797
1798/* Exception handling [gram.exception] */
1799
21526606 1800static tree cp_parser_try_block
94edc4ab 1801 (cp_parser *);
a723baf1 1802static bool cp_parser_function_try_block
94edc4ab 1803 (cp_parser *);
a723baf1 1804static void cp_parser_handler_seq
94edc4ab 1805 (cp_parser *);
a723baf1 1806static void cp_parser_handler
94edc4ab 1807 (cp_parser *);
a723baf1 1808static tree cp_parser_exception_declaration
94edc4ab 1809 (cp_parser *);
a723baf1 1810static tree cp_parser_throw_expression
94edc4ab 1811 (cp_parser *);
a723baf1 1812static tree cp_parser_exception_specification_opt
94edc4ab 1813 (cp_parser *);
a723baf1 1814static tree cp_parser_type_id_list
94edc4ab 1815 (cp_parser *);
a723baf1
MM
1816
1817/* GNU Extensions */
1818
1819static tree cp_parser_asm_specification_opt
94edc4ab 1820 (cp_parser *);
a723baf1 1821static tree cp_parser_asm_operand_list
94edc4ab 1822 (cp_parser *);
a723baf1 1823static tree cp_parser_asm_clobber_list
94edc4ab 1824 (cp_parser *);
a723baf1 1825static tree cp_parser_attributes_opt
94edc4ab 1826 (cp_parser *);
a723baf1 1827static tree cp_parser_attribute_list
94edc4ab 1828 (cp_parser *);
a723baf1 1829static bool cp_parser_extension_opt
94edc4ab 1830 (cp_parser *, int *);
a723baf1 1831static void cp_parser_label_declaration
94edc4ab 1832 (cp_parser *);
a723baf1
MM
1833
1834/* Utility Routines */
1835
1836static tree cp_parser_lookup_name
b0bc6e8e 1837 (cp_parser *, tree, bool, bool, bool, bool);
a723baf1 1838static tree cp_parser_lookup_name_simple
94edc4ab 1839 (cp_parser *, tree);
a723baf1
MM
1840static tree cp_parser_maybe_treat_template_as_class
1841 (tree, bool);
1842static bool cp_parser_check_declarator_template_parameters
058b15c1 1843 (cp_parser *, cp_declarator *);
a723baf1 1844static bool cp_parser_check_template_parameters
94edc4ab 1845 (cp_parser *, unsigned);
d6b4ea85
MM
1846static tree cp_parser_simple_cast_expression
1847 (cp_parser *);
a723baf1 1848static tree cp_parser_binary_expression
94edc4ab 1849 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1850static tree cp_parser_global_scope_opt
94edc4ab 1851 (cp_parser *, bool);
a723baf1
MM
1852static bool cp_parser_constructor_declarator_p
1853 (cp_parser *, bool);
1854static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1855 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1856static tree cp_parser_function_definition_after_declarator
94edc4ab 1857 (cp_parser *, bool);
a723baf1 1858static void cp_parser_template_declaration_after_export
94edc4ab 1859 (cp_parser *, bool);
a723baf1 1860static tree cp_parser_single_declaration
94edc4ab 1861 (cp_parser *, bool, bool *);
a723baf1 1862static tree cp_parser_functional_cast
94edc4ab 1863 (cp_parser *, tree);
4bb8ca28 1864static tree cp_parser_save_member_function_body
62d1db17 1865 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
ec75414f
MM
1866static tree cp_parser_enclosed_template_argument_list
1867 (cp_parser *);
8db1028e
NS
1868static void cp_parser_save_default_args
1869 (cp_parser *, tree);
a723baf1 1870static void cp_parser_late_parsing_for_member
94edc4ab 1871 (cp_parser *, tree);
a723baf1 1872static void cp_parser_late_parsing_default_args
8218bd34 1873 (cp_parser *, tree);
a723baf1 1874static tree cp_parser_sizeof_operand
94edc4ab 1875 (cp_parser *, enum rid);
a723baf1 1876static bool cp_parser_declares_only_class_p
94edc4ab 1877 (cp_parser *);
62d1db17
MM
1878static void cp_parser_set_storage_class
1879 (cp_decl_specifier_seq *, cp_storage_class);
1880static void cp_parser_set_decl_spec_type
1881 (cp_decl_specifier_seq *, tree, bool);
a723baf1 1882static bool cp_parser_friend_p
62d1db17 1883 (const cp_decl_specifier_seq *);
a723baf1 1884static cp_token *cp_parser_require
94edc4ab 1885 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1886static cp_token *cp_parser_require_keyword
94edc4ab 1887 (cp_parser *, enum rid, const char *);
21526606 1888static bool cp_parser_token_starts_function_definition_p
94edc4ab 1889 (cp_token *);
a723baf1
MM
1890static bool cp_parser_next_token_starts_class_definition_p
1891 (cp_parser *);
d17811fd
MM
1892static bool cp_parser_next_token_ends_template_argument_p
1893 (cp_parser *);
f4abade9
GB
1894static bool cp_parser_nth_token_starts_template_argument_list_p
1895 (cp_parser *, size_t);
a723baf1 1896static enum tag_types cp_parser_token_is_class_key
94edc4ab 1897 (cp_token *);
a723baf1
MM
1898static void cp_parser_check_class_key
1899 (enum tag_types, tree type);
37d407a1
KL
1900static void cp_parser_check_access_in_redeclaration
1901 (tree type);
a723baf1
MM
1902static bool cp_parser_optional_template_keyword
1903 (cp_parser *);
21526606 1904static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1905 (cp_parser *);
a723baf1
MM
1906static void cp_parser_cache_group
1907 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
21526606 1908static void cp_parser_parse_tentatively
94edc4ab 1909 (cp_parser *);
a723baf1 1910static void cp_parser_commit_to_tentative_parse
94edc4ab 1911 (cp_parser *);
a723baf1 1912static void cp_parser_abort_tentative_parse
94edc4ab 1913 (cp_parser *);
a723baf1 1914static bool cp_parser_parse_definitely
94edc4ab 1915 (cp_parser *);
f7b5ecd9 1916static inline bool cp_parser_parsing_tentatively
94edc4ab 1917 (cp_parser *);
a723baf1 1918static bool cp_parser_committed_to_tentative_parse
94edc4ab 1919 (cp_parser *);
a723baf1 1920static void cp_parser_error
94edc4ab 1921 (cp_parser *, const char *);
4bb8ca28
MM
1922static void cp_parser_name_lookup_error
1923 (cp_parser *, tree, tree, const char *);
e5976695 1924static bool cp_parser_simulate_error
94edc4ab 1925 (cp_parser *);
a723baf1 1926static void cp_parser_check_type_definition
94edc4ab 1927 (cp_parser *);
560ad596 1928static void cp_parser_check_for_definition_in_return_type
058b15c1 1929 (cp_declarator *, int);
ee43dab5
MM
1930static void cp_parser_check_for_invalid_template_id
1931 (cp_parser *, tree);
625cbf93
MM
1932static bool cp_parser_non_integral_constant_expression
1933 (cp_parser *, const char *);
2097b5f2
GB
1934static void cp_parser_diagnose_invalid_type_name
1935 (cp_parser *, tree, tree);
1936static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1937 (cp_parser *);
7efa3e22 1938static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1939 (cp_parser *, bool, bool, bool);
a723baf1 1940static void cp_parser_skip_to_end_of_statement
94edc4ab 1941 (cp_parser *);
e0860732
MM
1942static void cp_parser_consume_semicolon_at_end_of_statement
1943 (cp_parser *);
a723baf1 1944static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1945 (cp_parser *);
a723baf1
MM
1946static void cp_parser_skip_to_closing_brace
1947 (cp_parser *);
1948static void cp_parser_skip_until_found
94edc4ab 1949 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1950static bool cp_parser_error_occurred
94edc4ab 1951 (cp_parser *);
a723baf1 1952static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1953 (cp_parser *);
a723baf1 1954static bool cp_parser_is_string_literal
94edc4ab 1955 (cp_token *);
21526606 1956static bool cp_parser_is_keyword
94edc4ab 1957 (cp_token *, enum rid);
2097b5f2
GB
1958static tree cp_parser_make_typename_type
1959 (cp_parser *, tree, tree);
a723baf1 1960
4de8668e 1961/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1962
1963static inline bool
94edc4ab 1964cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1965{
1966 return parser->context->next != NULL;
1967}
1968
4de8668e 1969/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1970
1971static bool
94edc4ab 1972cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1973{
1974 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1975}
1976
4de8668e 1977/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1978
1979static bool
94edc4ab 1980cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1981{
1982 return token->keyword == keyword;
1983}
1984
a723baf1
MM
1985/* Issue the indicated error MESSAGE. */
1986
1987static void
94edc4ab 1988cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1989{
a723baf1 1990 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1991 if (!cp_parser_simulate_error (parser))
4bb8ca28
MM
1992 {
1993 cp_token *token;
1994 token = cp_lexer_peek_token (parser->lexer);
21526606 1995 c_parse_error (message,
5c832178
MM
1996 /* Because c_parser_error does not understand
1997 CPP_KEYWORD, keywords are treated like
1998 identifiers. */
21526606 1999 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 2000 token->value);
4bb8ca28
MM
2001 }
2002}
2003
2004/* Issue an error about name-lookup failing. NAME is the
2005 IDENTIFIER_NODE DECL is the result of
2006 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2007 the thing that we hoped to find. */
2008
2009static void
2010cp_parser_name_lookup_error (cp_parser* parser,
2011 tree name,
2012 tree decl,
2013 const char* desired)
2014{
2015 /* If name lookup completely failed, tell the user that NAME was not
2016 declared. */
2017 if (decl == error_mark_node)
2018 {
2019 if (parser->scope && parser->scope != global_namespace)
21526606 2020 error ("`%D::%D' has not been declared",
4bb8ca28
MM
2021 parser->scope, name);
2022 else if (parser->scope == global_namespace)
2023 error ("`::%D' has not been declared", name);
2024 else
2025 error ("`%D' has not been declared", name);
2026 }
2027 else if (parser->scope && parser->scope != global_namespace)
2028 error ("`%D::%D' %s", parser->scope, name, desired);
2029 else if (parser->scope == global_namespace)
2030 error ("`::%D' %s", name, desired);
2031 else
2032 error ("`%D' %s", name, desired);
a723baf1
MM
2033}
2034
2035/* If we are parsing tentatively, remember that an error has occurred
e5976695 2036 during this tentative parse. Returns true if the error was
77077b39 2037 simulated; false if a message should be issued by the caller. */
a723baf1 2038
e5976695 2039static bool
94edc4ab 2040cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
2041{
2042 if (cp_parser_parsing_tentatively (parser)
2043 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
2044 {
2045 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2046 return true;
2047 }
2048 return false;
a723baf1
MM
2049}
2050
2051/* This function is called when a type is defined. If type
2052 definitions are forbidden at this point, an error message is
2053 issued. */
2054
2055static void
94edc4ab 2056cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
2057{
2058 /* If types are forbidden here, issue a message. */
2059 if (parser->type_definition_forbidden_message)
2060 /* Use `%s' to print the string in case there are any escape
2061 characters in the message. */
2062 error ("%s", parser->type_definition_forbidden_message);
2063}
2064
560ad596
MM
2065/* This function is called when a declaration is parsed. If
2066 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2067 indicates that a type was defined in the decl-specifiers for DECL,
2068 then an error is issued. */
2069
2070static void
058b15c1 2071cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
560ad596
MM
2072 int declares_class_or_enum)
2073{
2074 /* [dcl.fct] forbids type definitions in return types.
2075 Unfortunately, it's not easy to know whether or not we are
2076 processing a return type until after the fact. */
2077 while (declarator
058b15c1
MM
2078 && (declarator->kind == cdk_pointer
2079 || declarator->kind == cdk_reference
2080 || declarator->kind == cdk_ptrmem))
2081 declarator = declarator->declarator;
560ad596 2082 if (declarator
058b15c1 2083 && declarator->kind == cdk_function
560ad596
MM
2084 && declares_class_or_enum & 2)
2085 error ("new types may not be defined in a return type");
2086}
2087
ee43dab5
MM
2088/* A type-specifier (TYPE) has been parsed which cannot be followed by
2089 "<" in any valid C++ program. If the next token is indeed "<",
2090 issue a message warning the user about what appears to be an
2091 invalid attempt to form a template-id. */
2092
2093static void
21526606 2094cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
2095 tree type)
2096{
2097 ptrdiff_t start;
2098 cp_token *token;
2099
2100 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2101 {
2102 if (TYPE_P (type))
2103 error ("`%T' is not a template", type);
2104 else if (TREE_CODE (type) == IDENTIFIER_NODE)
4460cef2 2105 error ("`%E' is not a template", type);
ee43dab5
MM
2106 else
2107 error ("invalid template-id");
2108 /* Remember the location of the invalid "<". */
2109 if (cp_parser_parsing_tentatively (parser)
2110 && !cp_parser_committed_to_tentative_parse (parser))
2111 {
2112 token = cp_lexer_peek_token (parser->lexer);
2113 token = cp_lexer_prev_token (parser->lexer, token);
2114 start = cp_lexer_token_difference (parser->lexer,
2115 parser->lexer->first_token,
2116 token);
2117 }
2118 else
2119 start = -1;
2120 /* Consume the "<". */
2121 cp_lexer_consume_token (parser->lexer);
2122 /* Parse the template arguments. */
2123 cp_parser_enclosed_template_argument_list (parser);
da1d7781 2124 /* Permanently remove the invalid template arguments so that
ee43dab5
MM
2125 this error message is not issued again. */
2126 if (start >= 0)
2127 {
2128 token = cp_lexer_advance_token (parser->lexer,
2129 parser->lexer->first_token,
2130 start);
2131 cp_lexer_purge_tokens_after (parser->lexer, token);
2132 }
2133 }
2134}
2135
625cbf93
MM
2136/* If parsing an integral constant-expression, issue an error message
2137 about the fact that THING appeared and return true. Otherwise,
2138 return false, marking the current expression as non-constant. */
14d22dd6 2139
625cbf93
MM
2140static bool
2141cp_parser_non_integral_constant_expression (cp_parser *parser,
2142 const char *thing)
14d22dd6 2143{
625cbf93
MM
2144 if (parser->integral_constant_expression_p)
2145 {
2146 if (!parser->allow_non_integral_constant_expression_p)
2147 {
2148 error ("%s cannot appear in a constant-expression", thing);
2149 return true;
2150 }
2151 parser->non_integral_constant_expression_p = true;
2152 }
2153 return false;
14d22dd6
MM
2154}
2155
2097b5f2 2156/* Emit a diagnostic for an invalid type name. Consider also if it is
21526606 2157 qualified or not and the result of a lookup, to provide a better
2097b5f2 2158 message. */
8fbc5ae7 2159
2097b5f2
GB
2160static void
2161cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2162{
2163 tree decl, old_scope;
2097b5f2
GB
2164 /* Try to lookup the identifier. */
2165 old_scope = parser->scope;
2166 parser->scope = scope;
2167 decl = cp_parser_lookup_name_simple (parser, id);
2168 parser->scope = old_scope;
2169 /* If the lookup found a template-name, it means that the user forgot
2170 to specify an argument list. Emit an useful error message. */
2171 if (TREE_CODE (decl) == TEMPLATE_DECL)
6c0cc713
GB
2172 error ("invalid use of template-name `%E' without an argument list",
2173 decl);
2097b5f2 2174 else if (!parser->scope)
8fbc5ae7 2175 {
8fbc5ae7 2176 /* Issue an error message. */
2097b5f2 2177 error ("`%E' does not name a type", id);
8fbc5ae7
MM
2178 /* If we're in a template class, it's possible that the user was
2179 referring to a type from a base class. For example:
2180
2181 template <typename T> struct A { typedef T X; };
2182 template <typename T> struct B : public A<T> { X x; };
2183
2184 The user should have said "typename A<T>::X". */
2185 if (processing_template_decl && current_class_type)
2186 {
2187 tree b;
2188
2189 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2190 b;
2191 b = TREE_CHAIN (b))
2192 {
2193 tree base_type = BINFO_TYPE (b);
21526606 2194 if (CLASS_TYPE_P (base_type)
1fb3244a 2195 && dependent_type_p (base_type))
8fbc5ae7
MM
2196 {
2197 tree field;
2198 /* Go from a particular instantiation of the
2199 template (which will have an empty TYPE_FIELDs),
2200 to the main version. */
353b4fc0 2201 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
2202 for (field = TYPE_FIELDS (base_type);
2203 field;
2204 field = TREE_CHAIN (field))
2205 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 2206 && DECL_NAME (field) == id)
8fbc5ae7 2207 {
2097b5f2
GB
2208 inform ("(perhaps `typename %T::%E' was intended)",
2209 BINFO_TYPE (b), id);
8fbc5ae7
MM
2210 break;
2211 }
2212 if (field)
2213 break;
2214 }
2215 }
2216 }
8fbc5ae7 2217 }
2097b5f2
GB
2218 /* Here we diagnose qualified-ids where the scope is actually correct,
2219 but the identifier does not resolve to a valid type name. */
21526606 2220 else
2097b5f2
GB
2221 {
2222 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21526606 2223 error ("`%E' in namespace `%E' does not name a type",
2097b5f2
GB
2224 id, parser->scope);
2225 else if (TYPE_P (parser->scope))
21526606 2226 error ("`%E' in class `%T' does not name a type",
2097b5f2
GB
2227 id, parser->scope);
2228 else
2229 abort();
2230 }
2231}
8fbc5ae7 2232
2097b5f2
GB
2233/* Check for a common situation where a type-name should be present,
2234 but is not, and issue a sensible error message. Returns true if an
2235 invalid type-name was detected.
21526606 2236
2097b5f2 2237 The situation handled by this function are variable declarations of the
21526606
EC
2238 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2239 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2240 does not. We try to emit the best possible error message depending on
2241 how exactly the id-expression looks like.
2242*/
2243
2244static bool
2245cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2246{
2247 tree id;
2248
2249 cp_parser_parse_tentatively (parser);
21526606 2250 id = cp_parser_id_expression (parser,
2097b5f2
GB
2251 /*template_keyword_p=*/false,
2252 /*check_dependency_p=*/true,
2253 /*template_p=*/NULL,
2254 /*declarator_p=*/true);
2255 /* After the id-expression, there should be a plain identifier,
2256 otherwise this is not a simple variable declaration. Also, if
2257 the scope is dependent, we cannot do much. */
2258 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2259 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2260 && dependent_type_p (parser->scope)))
2261 {
2262 cp_parser_abort_tentative_parse (parser);
2263 return false;
2264 }
2265 if (!cp_parser_parse_definitely (parser))
2266 return false;
2267
2268 /* If we got here, this cannot be a valid variable declaration, thus
2269 the cp_parser_id_expression must have resolved to a plain identifier
2270 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2271 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2272 /* Emit a diagnostic for the invalid type. */
2273 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2274 /* Skip to the end of the declaration; there's no point in
2275 trying to process it. */
2276 cp_parser_skip_to_end_of_block_or_statement (parser);
2277 return true;
8fbc5ae7
MM
2278}
2279
21526606 2280/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2281 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2282 are doing error recovery. Returns -1 if OR_COMMA is true and we
2283 found an unnested comma. */
a723baf1 2284
7efa3e22
NS
2285static int
2286cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2287 bool recovering,
a668c6ad
MM
2288 bool or_comma,
2289 bool consume_paren)
a723baf1 2290{
7efa3e22
NS
2291 unsigned paren_depth = 0;
2292 unsigned brace_depth = 0;
0173bb6f
AO
2293 int saved_c_lex_string_translate = c_lex_string_translate;
2294 int result;
a723baf1 2295
7efa3e22
NS
2296 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2297 && !cp_parser_committed_to_tentative_parse (parser))
2298 return 0;
21526606 2299
0173bb6f
AO
2300 if (! recovering)
2301 /* If we're looking ahead, keep both translated and untranslated
2302 strings. */
2303 c_lex_string_translate = -1;
2304
a723baf1
MM
2305 while (true)
2306 {
2307 cp_token *token;
21526606 2308
a723baf1
MM
2309 /* If we've run out of tokens, then there is no closing `)'. */
2310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0173bb6f
AO
2311 {
2312 result = 0;
2313 break;
2314 }
a723baf1 2315
a668c6ad 2316 token = cp_lexer_peek_token (parser->lexer);
21526606 2317
f4f206f4 2318 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad 2319 if (token->type == CPP_SEMICOLON && !brace_depth)
0173bb6f
AO
2320 {
2321 result = 0;
2322 break;
2323 }
a668c6ad
MM
2324 if (token->type == CPP_OPEN_BRACE)
2325 ++brace_depth;
2326 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2327 {
a668c6ad 2328 if (!brace_depth--)
0173bb6f
AO
2329 {
2330 result = 0;
2331 break;
2332 }
7efa3e22 2333 }
a668c6ad
MM
2334 if (recovering && or_comma && token->type == CPP_COMMA
2335 && !brace_depth && !paren_depth)
0173bb6f
AO
2336 {
2337 result = -1;
2338 break;
2339 }
21526606 2340
7efa3e22
NS
2341 if (!brace_depth)
2342 {
2343 /* If it is an `(', we have entered another level of nesting. */
2344 if (token->type == CPP_OPEN_PAREN)
2345 ++paren_depth;
2346 /* If it is a `)', then we might be done. */
2347 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2348 {
2349 if (consume_paren)
2350 cp_lexer_consume_token (parser->lexer);
0173bb6f
AO
2351 {
2352 result = 1;
2353 break;
2354 }
a668c6ad 2355 }
7efa3e22 2356 }
21526606 2357
a668c6ad
MM
2358 /* Consume the token. */
2359 cp_lexer_consume_token (parser->lexer);
a723baf1 2360 }
0173bb6f
AO
2361
2362 c_lex_string_translate = saved_c_lex_string_translate;
2363 return result;
a723baf1
MM
2364}
2365
2366/* Consume tokens until we reach the end of the current statement.
2367 Normally, that will be just before consuming a `;'. However, if a
2368 non-nested `}' comes first, then we stop before consuming that. */
2369
2370static void
94edc4ab 2371cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2372{
2373 unsigned nesting_depth = 0;
2374
2375 while (true)
2376 {
2377 cp_token *token;
2378
2379 /* Peek at the next token. */
2380 token = cp_lexer_peek_token (parser->lexer);
2381 /* If we've run out of tokens, stop. */
2382 if (token->type == CPP_EOF)
2383 break;
2384 /* If the next token is a `;', we have reached the end of the
2385 statement. */
2386 if (token->type == CPP_SEMICOLON && !nesting_depth)
2387 break;
2388 /* If the next token is a non-nested `}', then we have reached
2389 the end of the current block. */
2390 if (token->type == CPP_CLOSE_BRACE)
2391 {
2392 /* If this is a non-nested `}', stop before consuming it.
2393 That way, when confronted with something like:
2394
21526606 2395 { 3 + }
a723baf1
MM
2396
2397 we stop before consuming the closing `}', even though we
2398 have not yet reached a `;'. */
2399 if (nesting_depth == 0)
2400 break;
2401 /* If it is the closing `}' for a block that we have
2402 scanned, stop -- but only after consuming the token.
2403 That way given:
2404
2405 void f g () { ... }
2406 typedef int I;
2407
2408 we will stop after the body of the erroneously declared
2409 function, but before consuming the following `typedef'
2410 declaration. */
2411 if (--nesting_depth == 0)
2412 {
2413 cp_lexer_consume_token (parser->lexer);
2414 break;
2415 }
2416 }
2417 /* If it the next token is a `{', then we are entering a new
2418 block. Consume the entire block. */
2419 else if (token->type == CPP_OPEN_BRACE)
2420 ++nesting_depth;
2421 /* Consume the token. */
2422 cp_lexer_consume_token (parser->lexer);
2423 }
2424}
2425
e0860732
MM
2426/* This function is called at the end of a statement or declaration.
2427 If the next token is a semicolon, it is consumed; otherwise, error
2428 recovery is attempted. */
2429
2430static void
2431cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2432{
2433 /* Look for the trailing `;'. */
2434 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2435 {
2436 /* If there is additional (erroneous) input, skip to the end of
2437 the statement. */
2438 cp_parser_skip_to_end_of_statement (parser);
2439 /* If the next token is now a `;', consume it. */
2440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2441 cp_lexer_consume_token (parser->lexer);
2442 }
2443}
2444
a723baf1
MM
2445/* Skip tokens until we have consumed an entire block, or until we
2446 have consumed a non-nested `;'. */
2447
2448static void
94edc4ab 2449cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2450{
2451 unsigned nesting_depth = 0;
2452
2453 while (true)
2454 {
2455 cp_token *token;
2456
2457 /* Peek at the next token. */
2458 token = cp_lexer_peek_token (parser->lexer);
2459 /* If we've run out of tokens, stop. */
2460 if (token->type == CPP_EOF)
2461 break;
2462 /* If the next token is a `;', we have reached the end of the
2463 statement. */
2464 if (token->type == CPP_SEMICOLON && !nesting_depth)
2465 {
2466 /* Consume the `;'. */
2467 cp_lexer_consume_token (parser->lexer);
2468 break;
2469 }
2470 /* Consume the token. */
2471 token = cp_lexer_consume_token (parser->lexer);
2472 /* If the next token is a non-nested `}', then we have reached
2473 the end of the current block. */
21526606 2474 if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
2475 && (nesting_depth == 0 || --nesting_depth == 0))
2476 break;
2477 /* If it the next token is a `{', then we are entering a new
2478 block. Consume the entire block. */
2479 if (token->type == CPP_OPEN_BRACE)
2480 ++nesting_depth;
2481 }
2482}
2483
2484/* Skip tokens until a non-nested closing curly brace is the next
2485 token. */
2486
2487static void
2488cp_parser_skip_to_closing_brace (cp_parser *parser)
2489{
2490 unsigned nesting_depth = 0;
2491
2492 while (true)
2493 {
2494 cp_token *token;
2495
2496 /* Peek at the next token. */
2497 token = cp_lexer_peek_token (parser->lexer);
2498 /* If we've run out of tokens, stop. */
2499 if (token->type == CPP_EOF)
2500 break;
2501 /* If the next token is a non-nested `}', then we have reached
2502 the end of the current block. */
2503 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2504 break;
2505 /* If it the next token is a `{', then we are entering a new
2506 block. Consume the entire block. */
2507 else if (token->type == CPP_OPEN_BRACE)
2508 ++nesting_depth;
2509 /* Consume the token. */
2510 cp_lexer_consume_token (parser->lexer);
2511 }
2512}
2513
2097b5f2
GB
2514/* This is a simple wrapper around make_typename_type. When the id is
2515 an unresolved identifier node, we can provide a superior diagnostic
2516 using cp_parser_diagnose_invalid_type_name. */
2517
2518static tree
2519cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2520{
2521 tree result;
2522 if (TREE_CODE (id) == IDENTIFIER_NODE)
2523 {
2524 result = make_typename_type (scope, id, /*complain=*/0);
2525 if (result == error_mark_node)
2526 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2527 return result;
2528 }
2529 return make_typename_type (scope, id, tf_error);
2097b5f2
GB
2530}
2531
2532
a723baf1
MM
2533/* Create a new C++ parser. */
2534
2535static cp_parser *
94edc4ab 2536cp_parser_new (void)
a723baf1
MM
2537{
2538 cp_parser *parser;
17211ab5
GK
2539 cp_lexer *lexer;
2540
2541 /* cp_lexer_new_main is called before calling ggc_alloc because
2542 cp_lexer_new_main might load a PCH file. */
2543 lexer = cp_lexer_new_main ();
a723baf1 2544
c68b0a84 2545 parser = ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2546 parser->lexer = lexer;
a723baf1
MM
2547 parser->context = cp_parser_context_new (NULL);
2548
2549 /* For now, we always accept GNU extensions. */
2550 parser->allow_gnu_extensions_p = 1;
2551
2552 /* The `>' token is a greater-than operator, not the end of a
2553 template-id. */
2554 parser->greater_than_is_operator_p = true;
2555
2556 parser->default_arg_ok_p = true;
21526606 2557
a723baf1 2558 /* We are not parsing a constant-expression. */
67c03833
JM
2559 parser->integral_constant_expression_p = false;
2560 parser->allow_non_integral_constant_expression_p = false;
2561 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2562
2563 /* Local variable names are not forbidden. */
2564 parser->local_variables_forbidden_p = false;
2565
34cd5ae7 2566 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2567 parser->in_unbraced_linkage_specification_p = false;
2568
2569 /* We are not processing a declarator. */
2570 parser->in_declarator_p = false;
2571
4bb8ca28
MM
2572 /* We are not processing a template-argument-list. */
2573 parser->in_template_argument_list_p = false;
2574
0e59b3fb
MM
2575 /* We are not in an iteration statement. */
2576 parser->in_iteration_statement_p = false;
2577
2578 /* We are not in a switch statement. */
2579 parser->in_switch_statement_p = false;
2580
4f8163b1
MM
2581 /* We are not parsing a type-id inside an expression. */
2582 parser->in_type_id_in_expr_p = false;
2583
a723baf1
MM
2584 /* The unparsed function queue is empty. */
2585 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2586
2587 /* There are no classes being defined. */
2588 parser->num_classes_being_defined = 0;
2589
2590 /* No template parameters apply. */
2591 parser->num_template_parameter_lists = 0;
2592
2593 return parser;
2594}
2595
2596/* Lexical conventions [gram.lex] */
2597
2598/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2599 identifier. */
2600
21526606 2601static tree
94edc4ab 2602cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2603{
2604 cp_token *token;
2605
2606 /* Look for the identifier. */
2607 token = cp_parser_require (parser, CPP_NAME, "identifier");
2608 /* Return the value. */
2609 return token ? token->value : error_mark_node;
2610}
2611
2612/* Basic concepts [gram.basic] */
2613
2614/* Parse a translation-unit.
2615
2616 translation-unit:
21526606 2617 declaration-seq [opt]
a723baf1
MM
2618
2619 Returns TRUE if all went well. */
2620
2621static bool
94edc4ab 2622cp_parser_translation_unit (cp_parser* parser)
a723baf1 2623{
058b15c1
MM
2624 /* The address of the first non-permanent object on the declarator
2625 obstack. */
2626 static void *declarator_obstack_base;
2627
2628 bool success;
2629
2630 /* Create the declarator obstack, if necessary. */
2631 if (!cp_error_declarator)
2632 {
2633 gcc_obstack_init (&declarator_obstack);
2634 /* Create the error declarator. */
2635 cp_error_declarator = make_declarator (cdk_error);
2636 /* Create the empty parameter list. */
62d1db17 2637 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2638 /* Remember where the base of the declarator obstack lies. */
2639 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2640 }
2641
a723baf1
MM
2642 while (true)
2643 {
2644 cp_parser_declaration_seq_opt (parser);
2645
2646 /* If there are no tokens left then all went well. */
2647 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
058b15c1
MM
2648 {
2649 /* Consume the EOF token. */
2650 cp_parser_require (parser, CPP_EOF, "end-of-file");
2651
2652 /* Finish up. */
2653 finish_translation_unit ();
21526606 2654
058b15c1
MM
2655 success = true;
2656 break;
2657 }
2658 else
2659 {
2660 cp_parser_error (parser, "expected declaration");
2661 success = false;
2662 break;
2663 }
a723baf1
MM
2664 }
2665
058b15c1
MM
2666 /* Make sure the declarator obstack was fully cleaned up. */
2667 my_friendly_assert (obstack_next_free (&declarator_obstack) ==
2668 declarator_obstack_base,
2669 20040621);
a723baf1
MM
2670
2671 /* All went well. */
058b15c1 2672 return success;
a723baf1
MM
2673}
2674
2675/* Expressions [gram.expr] */
2676
2677/* Parse a primary-expression.
2678
2679 primary-expression:
2680 literal
2681 this
2682 ( expression )
2683 id-expression
2684
2685 GNU Extensions:
2686
2687 primary-expression:
2688 ( compound-statement )
2689 __builtin_va_arg ( assignment-expression , type-id )
2690
2691 literal:
2692 __null
2693
21526606 2694 Returns a representation of the expression.
a723baf1 2695
21526606 2696 *IDK indicates what kind of id-expression (if any) was present.
a723baf1
MM
2697
2698 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2699 used as the operand of a pointer-to-member. In that case,
2700 *QUALIFYING_CLASS gives the class that is used as the qualifying
2701 class in the pointer-to-member. */
2702
2703static tree
21526606 2704cp_parser_primary_expression (cp_parser *parser,
b3445994 2705 cp_id_kind *idk,
a723baf1
MM
2706 tree *qualifying_class)
2707{
2708 cp_token *token;
2709
2710 /* Assume the primary expression is not an id-expression. */
b3445994 2711 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2712 /* And that it cannot be used as pointer-to-member. */
2713 *qualifying_class = NULL_TREE;
2714
2715 /* Peek at the next token. */
2716 token = cp_lexer_peek_token (parser->lexer);
2717 switch (token->type)
2718 {
2719 /* literal:
2720 integer-literal
2721 character-literal
2722 floating-literal
2723 string-literal
2724 boolean-literal */
2725 case CPP_CHAR:
2726 case CPP_WCHAR:
a723baf1
MM
2727 case CPP_NUMBER:
2728 token = cp_lexer_consume_token (parser->lexer);
2729 return token->value;
2730
0173bb6f
AO
2731 case CPP_STRING:
2732 case CPP_WSTRING:
2733 token = cp_lexer_consume_token (parser->lexer);
2734 if (TREE_CHAIN (token->value))
2735 return TREE_CHAIN (token->value);
2736 else
2737 return token->value;
2738
a723baf1
MM
2739 case CPP_OPEN_PAREN:
2740 {
2741 tree expr;
2742 bool saved_greater_than_is_operator_p;
2743
2744 /* Consume the `('. */
2745 cp_lexer_consume_token (parser->lexer);
2746 /* Within a parenthesized expression, a `>' token is always
2747 the greater-than operator. */
21526606 2748 saved_greater_than_is_operator_p
a723baf1
MM
2749 = parser->greater_than_is_operator_p;
2750 parser->greater_than_is_operator_p = true;
2751 /* If we see `( { ' then we are looking at the beginning of
2752 a GNU statement-expression. */
2753 if (cp_parser_allow_gnu_extensions_p (parser)
2754 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2755 {
2756 /* Statement-expressions are not allowed by the standard. */
2757 if (pedantic)
21526606
EC
2758 pedwarn ("ISO C++ forbids braced-groups within expressions");
2759
a723baf1
MM
2760 /* And they're not allowed outside of a function-body; you
2761 cannot, for example, write:
21526606 2762
a723baf1 2763 int i = ({ int j = 3; j + 1; });
21526606 2764
a723baf1
MM
2765 at class or namespace scope. */
2766 if (!at_function_scope_p ())
2767 error ("statement-expressions are allowed only inside functions");
2768 /* Start the statement-expression. */
2769 expr = begin_stmt_expr ();
2770 /* Parse the compound-statement. */
325c3691 2771 cp_parser_compound_statement (parser, expr, false);
a723baf1 2772 /* Finish up. */
303b7406 2773 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2774 }
2775 else
2776 {
2777 /* Parse the parenthesized expression. */
2778 expr = cp_parser_expression (parser);
2779 /* Let the front end know that this expression was
2780 enclosed in parentheses. This matters in case, for
2781 example, the expression is of the form `A::B', since
2782 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2783 not. */
2784 finish_parenthesized_expr (expr);
2785 }
2786 /* The `>' token might be the end of a template-id or
2787 template-parameter-list now. */
21526606 2788 parser->greater_than_is_operator_p
a723baf1
MM
2789 = saved_greater_than_is_operator_p;
2790 /* Consume the `)'. */
2791 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792 cp_parser_skip_to_end_of_statement (parser);
2793
2794 return expr;
2795 }
2796
2797 case CPP_KEYWORD:
2798 switch (token->keyword)
2799 {
2800 /* These two are the boolean literals. */
2801 case RID_TRUE:
2802 cp_lexer_consume_token (parser->lexer);
2803 return boolean_true_node;
2804 case RID_FALSE:
2805 cp_lexer_consume_token (parser->lexer);
2806 return boolean_false_node;
21526606 2807
a723baf1
MM
2808 /* The `__null' literal. */
2809 case RID_NULL:
2810 cp_lexer_consume_token (parser->lexer);
2811 return null_node;
2812
2813 /* Recognize the `this' keyword. */
2814 case RID_THIS:
2815 cp_lexer_consume_token (parser->lexer);
2816 if (parser->local_variables_forbidden_p)
2817 {
2818 error ("`this' may not be used in this context");
2819 return error_mark_node;
2820 }
14d22dd6 2821 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2822 if (cp_parser_non_integral_constant_expression (parser,
2823 "`this'"))
2824 return error_mark_node;
a723baf1
MM
2825 return finish_this_expr ();
2826
2827 /* The `operator' keyword can be the beginning of an
2828 id-expression. */
2829 case RID_OPERATOR:
2830 goto id_expression;
2831
2832 case RID_FUNCTION_NAME:
2833 case RID_PRETTY_FUNCTION_NAME:
2834 case RID_C99_FUNCTION_NAME:
2835 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836 __func__ are the names of variables -- but they are
2837 treated specially. Therefore, they are handled here,
2838 rather than relying on the generic id-expression logic
21526606 2839 below. Grammatically, these names are id-expressions.
a723baf1
MM
2840
2841 Consume the token. */
2842 token = cp_lexer_consume_token (parser->lexer);
2843 /* Look up the name. */
2844 return finish_fname (token->value);
2845
2846 case RID_VA_ARG:
2847 {
2848 tree expression;
2849 tree type;
2850
2851 /* The `__builtin_va_arg' construct is used to handle
2852 `va_arg'. Consume the `__builtin_va_arg' token. */
2853 cp_lexer_consume_token (parser->lexer);
2854 /* Look for the opening `('. */
2855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856 /* Now, parse the assignment-expression. */
2857 expression = cp_parser_assignment_expression (parser);
2858 /* Look for the `,'. */
2859 cp_parser_require (parser, CPP_COMMA, "`,'");
2860 /* Parse the type-id. */
2861 type = cp_parser_type_id (parser);
2862 /* Look for the closing `)'. */
2863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2864 /* Using `va_arg' in a constant-expression is not
2865 allowed. */
625cbf93
MM
2866 if (cp_parser_non_integral_constant_expression (parser,
2867 "`va_arg'"))
2868 return error_mark_node;
a723baf1
MM
2869 return build_x_va_arg (expression, type);
2870 }
2871
263ee052 2872 case RID_OFFSETOF:
7a3ea201 2873 return cp_parser_builtin_offsetof (parser);
263ee052 2874
a723baf1
MM
2875 default:
2876 cp_parser_error (parser, "expected primary-expression");
2877 return error_mark_node;
2878 }
a723baf1
MM
2879
2880 /* An id-expression can start with either an identifier, a
2881 `::' as the beginning of a qualified-id, or the "operator"
2882 keyword. */
2883 case CPP_NAME:
2884 case CPP_SCOPE:
2885 case CPP_TEMPLATE_ID:
2886 case CPP_NESTED_NAME_SPECIFIER:
2887 {
2888 tree id_expression;
2889 tree decl;
b3445994 2890 const char *error_msg;
a723baf1
MM
2891
2892 id_expression:
2893 /* Parse the id-expression. */
21526606
EC
2894 id_expression
2895 = cp_parser_id_expression (parser,
a723baf1
MM
2896 /*template_keyword_p=*/false,
2897 /*check_dependency_p=*/true,
f3c2dfc6
MM
2898 /*template_p=*/NULL,
2899 /*declarator_p=*/false);
a723baf1
MM
2900 if (id_expression == error_mark_node)
2901 return error_mark_node;
2902 /* If we have a template-id, then no further lookup is
2903 required. If the template-id was for a template-class, we
2904 will sometimes have a TYPE_DECL at this point. */
2905 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906 || TREE_CODE (id_expression) == TYPE_DECL)
2907 decl = id_expression;
2908 /* Look up the name. */
21526606 2909 else
a723baf1
MM
2910 {
2911 decl = cp_parser_lookup_name_simple (parser, id_expression);
2912 /* If name lookup gives us a SCOPE_REF, then the
2913 qualifying scope was dependent. Just propagate the
2914 name. */
2915 if (TREE_CODE (decl) == SCOPE_REF)
2916 {
2917 if (TYPE_P (TREE_OPERAND (decl, 0)))
2918 *qualifying_class = TREE_OPERAND (decl, 0);
2919 return decl;
2920 }
2921 /* Check to see if DECL is a local variable in a context
2922 where that is forbidden. */
2923 if (parser->local_variables_forbidden_p
2924 && local_variable_p (decl))
2925 {
2926 /* It might be that we only found DECL because we are
2927 trying to be generous with pre-ISO scoping rules.
2928 For example, consider:
2929
2930 int i;
2931 void g() {
2932 for (int i = 0; i < 10; ++i) {}
2933 extern void f(int j = i);
2934 }
2935
21526606 2936 Here, name look up will originally find the out
a723baf1
MM
2937 of scope `i'. We need to issue a warning message,
2938 but then use the global `i'. */
2939 decl = check_for_out_of_scope_variable (decl);
2940 if (local_variable_p (decl))
2941 {
2942 error ("local variable `%D' may not appear in this context",
2943 decl);
2944 return error_mark_node;
2945 }
2946 }
c006d942 2947 }
21526606
EC
2948
2949 decl = finish_id_expression (id_expression, decl, parser->scope,
b3445994 2950 idk, qualifying_class,
67c03833
JM
2951 parser->integral_constant_expression_p,
2952 parser->allow_non_integral_constant_expression_p,
2953 &parser->non_integral_constant_expression_p,
b3445994
MM
2954 &error_msg);
2955 if (error_msg)
2956 cp_parser_error (parser, error_msg);
a723baf1
MM
2957 return decl;
2958 }
2959
2960 /* Anything else is an error. */
2961 default:
2962 cp_parser_error (parser, "expected primary-expression");
2963 return error_mark_node;
2964 }
2965}
2966
2967/* Parse an id-expression.
2968
2969 id-expression:
2970 unqualified-id
2971 qualified-id
2972
2973 qualified-id:
2974 :: [opt] nested-name-specifier template [opt] unqualified-id
2975 :: identifier
2976 :: operator-function-id
2977 :: template-id
2978
2979 Return a representation of the unqualified portion of the
2980 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2981 a `::' or nested-name-specifier.
2982
2983 Often, if the id-expression was a qualified-id, the caller will
2984 want to make a SCOPE_REF to represent the qualified-id. This
2985 function does not do this in order to avoid wastefully creating
2986 SCOPE_REFs when they are not required.
2987
a723baf1
MM
2988 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2989 `template' keyword.
2990
2991 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 2992 uninstantiated templates.
a723baf1 2993
15d2cb19 2994 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2995 `template' keyword is used to explicitly indicate that the entity
21526606 2996 named is a template.
f3c2dfc6
MM
2997
2998 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2999 a declarator, rather than as part of an expression. */
a723baf1
MM
3000
3001static tree
3002cp_parser_id_expression (cp_parser *parser,
3003 bool template_keyword_p,
3004 bool check_dependency_p,
f3c2dfc6
MM
3005 bool *template_p,
3006 bool declarator_p)
a723baf1
MM
3007{
3008 bool global_scope_p;
3009 bool nested_name_specifier_p;
3010
3011 /* Assume the `template' keyword was not used. */
3012 if (template_p)
3013 *template_p = false;
3014
3015 /* Look for the optional `::' operator. */
21526606
EC
3016 global_scope_p
3017 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
3018 != NULL_TREE);
3019 /* Look for the optional nested-name-specifier. */
21526606 3020 nested_name_specifier_p
a723baf1
MM
3021 = (cp_parser_nested_name_specifier_opt (parser,
3022 /*typename_keyword_p=*/false,
3023 check_dependency_p,
a668c6ad
MM
3024 /*type_p=*/false,
3025 /*is_declarator=*/false)
a723baf1
MM
3026 != NULL_TREE);
3027 /* If there is a nested-name-specifier, then we are looking at
3028 the first qualified-id production. */
3029 if (nested_name_specifier_p)
3030 {
3031 tree saved_scope;
3032 tree saved_object_scope;
3033 tree saved_qualifying_scope;
3034 tree unqualified_id;
3035 bool is_template;
3036
3037 /* See if the next token is the `template' keyword. */
3038 if (!template_p)
3039 template_p = &is_template;
3040 *template_p = cp_parser_optional_template_keyword (parser);
3041 /* Name lookup we do during the processing of the
3042 unqualified-id might obliterate SCOPE. */
3043 saved_scope = parser->scope;
3044 saved_object_scope = parser->object_scope;
3045 saved_qualifying_scope = parser->qualifying_scope;
3046 /* Process the final unqualified-id. */
3047 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
3048 check_dependency_p,
3049 declarator_p);
a723baf1
MM
3050 /* Restore the SAVED_SCOPE for our caller. */
3051 parser->scope = saved_scope;
3052 parser->object_scope = saved_object_scope;
3053 parser->qualifying_scope = saved_qualifying_scope;
3054
3055 return unqualified_id;
3056 }
3057 /* Otherwise, if we are in global scope, then we are looking at one
3058 of the other qualified-id productions. */
3059 else if (global_scope_p)
3060 {
3061 cp_token *token;
3062 tree id;
3063
e5976695
MM
3064 /* Peek at the next token. */
3065 token = cp_lexer_peek_token (parser->lexer);
3066
3067 /* If it's an identifier, and the next token is not a "<", then
3068 we can avoid the template-id case. This is an optimization
3069 for this common case. */
21526606
EC
3070 if (token->type == CPP_NAME
3071 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3072 (parser, 2))
e5976695
MM
3073 return cp_parser_identifier (parser);
3074
a723baf1
MM
3075 cp_parser_parse_tentatively (parser);
3076 /* Try a template-id. */
21526606 3077 id = cp_parser_template_id (parser,
a723baf1 3078 /*template_keyword_p=*/false,
a668c6ad
MM
3079 /*check_dependency_p=*/true,
3080 declarator_p);
a723baf1
MM
3081 /* If that worked, we're done. */
3082 if (cp_parser_parse_definitely (parser))
3083 return id;
3084
e5976695
MM
3085 /* Peek at the next token. (Changes in the token buffer may
3086 have invalidated the pointer obtained above.) */
a723baf1
MM
3087 token = cp_lexer_peek_token (parser->lexer);
3088
3089 switch (token->type)
3090 {
3091 case CPP_NAME:
3092 return cp_parser_identifier (parser);
3093
3094 case CPP_KEYWORD:
3095 if (token->keyword == RID_OPERATOR)
3096 return cp_parser_operator_function_id (parser);
3097 /* Fall through. */
21526606 3098
a723baf1
MM
3099 default:
3100 cp_parser_error (parser, "expected id-expression");
3101 return error_mark_node;
3102 }
3103 }
3104 else
3105 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
3106 /*check_dependency_p=*/true,
3107 declarator_p);
a723baf1
MM
3108}
3109
3110/* Parse an unqualified-id.
3111
3112 unqualified-id:
3113 identifier
3114 operator-function-id
3115 conversion-function-id
3116 ~ class-name
3117 template-id
3118
3119 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3120 keyword, in a construct like `A::template ...'.
3121
3122 Returns a representation of unqualified-id. For the `identifier'
3123 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3124 production a BIT_NOT_EXPR is returned; the operand of the
3125 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3126 other productions, see the documentation accompanying the
3127 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3128 names are looked up in uninstantiated templates. If DECLARATOR_P
3129 is true, the unqualified-id is appearing as part of a declarator,
3130 rather than as part of an expression. */
a723baf1
MM
3131
3132static tree
21526606 3133cp_parser_unqualified_id (cp_parser* parser,
94edc4ab 3134 bool template_keyword_p,
f3c2dfc6
MM
3135 bool check_dependency_p,
3136 bool declarator_p)
a723baf1
MM
3137{
3138 cp_token *token;
3139
3140 /* Peek at the next token. */
3141 token = cp_lexer_peek_token (parser->lexer);
21526606 3142
a723baf1
MM
3143 switch (token->type)
3144 {
3145 case CPP_NAME:
3146 {
3147 tree id;
3148
3149 /* We don't know yet whether or not this will be a
3150 template-id. */
3151 cp_parser_parse_tentatively (parser);
3152 /* Try a template-id. */
3153 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3154 check_dependency_p,
3155 declarator_p);
a723baf1
MM
3156 /* If it worked, we're done. */
3157 if (cp_parser_parse_definitely (parser))
3158 return id;
3159 /* Otherwise, it's an ordinary identifier. */
3160 return cp_parser_identifier (parser);
3161 }
3162
3163 case CPP_TEMPLATE_ID:
3164 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3165 check_dependency_p,
3166 declarator_p);
a723baf1
MM
3167
3168 case CPP_COMPL:
3169 {
3170 tree type_decl;
3171 tree qualifying_scope;
3172 tree object_scope;
3173 tree scope;
3174
3175 /* Consume the `~' token. */
3176 cp_lexer_consume_token (parser->lexer);
3177 /* Parse the class-name. The standard, as written, seems to
3178 say that:
3179
3180 template <typename T> struct S { ~S (); };
3181 template <typename T> S<T>::~S() {}
3182
3183 is invalid, since `~' must be followed by a class-name, but
3184 `S<T>' is dependent, and so not known to be a class.
3185 That's not right; we need to look in uninstantiated
3186 templates. A further complication arises from:
3187
3188 template <typename T> void f(T t) {
3189 t.T::~T();
21526606 3190 }
a723baf1
MM
3191
3192 Here, it is not possible to look up `T' in the scope of `T'
3193 itself. We must look in both the current scope, and the
21526606 3194 scope of the containing complete expression.
a723baf1
MM
3195
3196 Yet another issue is:
3197
3198 struct S {
3199 int S;
3200 ~S();
3201 };
3202
3203 S::~S() {}
3204
3205 The standard does not seem to say that the `S' in `~S'
3206 should refer to the type `S' and not the data member
3207 `S::S'. */
3208
3209 /* DR 244 says that we look up the name after the "~" in the
3210 same scope as we looked up the qualifying name. That idea
3211 isn't fully worked out; it's more complicated than that. */
3212 scope = parser->scope;
3213 object_scope = parser->object_scope;
3214 qualifying_scope = parser->qualifying_scope;
3215
3216 /* If the name is of the form "X::~X" it's OK. */
3217 if (scope && TYPE_P (scope)
3218 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3219 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3220 == CPP_OPEN_PAREN)
21526606 3221 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3222 == TYPE_IDENTIFIER (scope)))
3223 {
3224 cp_lexer_consume_token (parser->lexer);
3225 return build_nt (BIT_NOT_EXPR, scope);
3226 }
3227
3228 /* If there was an explicit qualification (S::~T), first look
3229 in the scope given by the qualification (i.e., S). */
3230 if (scope)
3231 {
3232 cp_parser_parse_tentatively (parser);
21526606 3233 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3234 /*typename_keyword_p=*/false,
3235 /*template_keyword_p=*/false,
3236 /*type_p=*/false,
a723baf1 3237 /*check_dependency=*/false,
a668c6ad
MM
3238 /*class_head_p=*/false,
3239 declarator_p);
a723baf1
MM
3240 if (cp_parser_parse_definitely (parser))
3241 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3242 }
3243 /* In "N::S::~S", look in "N" as well. */
3244 if (scope && qualifying_scope)
3245 {
3246 cp_parser_parse_tentatively (parser);
3247 parser->scope = qualifying_scope;
3248 parser->object_scope = NULL_TREE;
3249 parser->qualifying_scope = NULL_TREE;
21526606
EC
3250 type_decl
3251 = cp_parser_class_name (parser,
a723baf1
MM
3252 /*typename_keyword_p=*/false,
3253 /*template_keyword_p=*/false,
3254 /*type_p=*/false,
a723baf1 3255 /*check_dependency=*/false,
a668c6ad
MM
3256 /*class_head_p=*/false,
3257 declarator_p);
a723baf1
MM
3258 if (cp_parser_parse_definitely (parser))
3259 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3260 }
3261 /* In "p->S::~T", look in the scope given by "*p" as well. */
3262 else if (object_scope)
3263 {
3264 cp_parser_parse_tentatively (parser);
3265 parser->scope = object_scope;
3266 parser->object_scope = NULL_TREE;
3267 parser->qualifying_scope = NULL_TREE;
21526606
EC
3268 type_decl
3269 = cp_parser_class_name (parser,
a723baf1
MM
3270 /*typename_keyword_p=*/false,
3271 /*template_keyword_p=*/false,
3272 /*type_p=*/false,
a723baf1 3273 /*check_dependency=*/false,
a668c6ad
MM
3274 /*class_head_p=*/false,
3275 declarator_p);
a723baf1
MM
3276 if (cp_parser_parse_definitely (parser))
3277 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3278 }
3279 /* Look in the surrounding context. */
3280 parser->scope = NULL_TREE;
3281 parser->object_scope = NULL_TREE;
3282 parser->qualifying_scope = NULL_TREE;
21526606
EC
3283 type_decl
3284 = cp_parser_class_name (parser,
a723baf1
MM
3285 /*typename_keyword_p=*/false,
3286 /*template_keyword_p=*/false,
3287 /*type_p=*/false,
a723baf1 3288 /*check_dependency=*/false,
a668c6ad
MM
3289 /*class_head_p=*/false,
3290 declarator_p);
a723baf1
MM
3291 /* If an error occurred, assume that the name of the
3292 destructor is the same as the name of the qualifying
3293 class. That allows us to keep parsing after running
3294 into ill-formed destructor names. */
3295 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3296 return build_nt (BIT_NOT_EXPR, scope);
3297 else if (type_decl == error_mark_node)
3298 return error_mark_node;
3299
f3c2dfc6
MM
3300 /* [class.dtor]
3301
3302 A typedef-name that names a class shall not be used as the
3303 identifier in the declarator for a destructor declaration. */
21526606 3304 if (declarator_p
f3c2dfc6
MM
3305 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3306 && !DECL_SELF_REFERENCE_P (type_decl))
3307 error ("typedef-name `%D' used as destructor declarator",
3308 type_decl);
3309
a723baf1
MM
3310 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3311 }
3312
3313 case CPP_KEYWORD:
3314 if (token->keyword == RID_OPERATOR)
3315 {
3316 tree id;
3317
3318 /* This could be a template-id, so we try that first. */
3319 cp_parser_parse_tentatively (parser);
3320 /* Try a template-id. */
3321 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3322 /*check_dependency_p=*/true,
3323 declarator_p);
a723baf1
MM
3324 /* If that worked, we're done. */
3325 if (cp_parser_parse_definitely (parser))
3326 return id;
3327 /* We still don't know whether we're looking at an
3328 operator-function-id or a conversion-function-id. */
3329 cp_parser_parse_tentatively (parser);
3330 /* Try an operator-function-id. */
3331 id = cp_parser_operator_function_id (parser);
3332 /* If that didn't work, try a conversion-function-id. */
3333 if (!cp_parser_parse_definitely (parser))
3334 id = cp_parser_conversion_function_id (parser);
3335
3336 return id;
3337 }
3338 /* Fall through. */
3339
3340 default:
3341 cp_parser_error (parser, "expected unqualified-id");
3342 return error_mark_node;
3343 }
3344}
3345
3346/* Parse an (optional) nested-name-specifier.
3347
3348 nested-name-specifier:
3349 class-or-namespace-name :: nested-name-specifier [opt]
3350 class-or-namespace-name :: template nested-name-specifier [opt]
3351
3352 PARSER->SCOPE should be set appropriately before this function is
3353 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3354 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3355 in name lookups.
3356
3357 Sets PARSER->SCOPE to the class (TYPE) or namespace
3358 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3359 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3360 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3361
3362 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3363 part of a declaration and/or decl-specifier. */
a723baf1
MM
3364
3365static tree
21526606
EC
3366cp_parser_nested_name_specifier_opt (cp_parser *parser,
3367 bool typename_keyword_p,
a723baf1 3368 bool check_dependency_p,
a668c6ad
MM
3369 bool type_p,
3370 bool is_declaration)
a723baf1
MM
3371{
3372 bool success = false;
3373 tree access_check = NULL_TREE;
3374 ptrdiff_t start;
2050a1bb 3375 cp_token* token;
a723baf1
MM
3376
3377 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3378 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3379 false, it may have been true before, in which case something
2050a1bb
MM
3380 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3381 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3382 CHECK_DEPENDENCY_P is false, we have to fall through into the
3383 main loop. */
3384 if (check_dependency_p
3385 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3386 {
3387 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3388 return parser->scope;
3389 }
3390
3391 /* Remember where the nested-name-specifier starts. */
3392 if (cp_parser_parsing_tentatively (parser)
3393 && !cp_parser_committed_to_tentative_parse (parser))
3394 {
2050a1bb 3395 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3396 start = cp_lexer_token_difference (parser->lexer,
3397 parser->lexer->first_token,
2050a1bb 3398 token);
a723baf1
MM
3399 }
3400 else
3401 start = -1;
3402
8d241e0b 3403 push_deferring_access_checks (dk_deferred);
cf22909c 3404
a723baf1
MM
3405 while (true)
3406 {
3407 tree new_scope;
3408 tree old_scope;
3409 tree saved_qualifying_scope;
a723baf1
MM
3410 bool template_keyword_p;
3411
2050a1bb
MM
3412 /* Spot cases that cannot be the beginning of a
3413 nested-name-specifier. */
3414 token = cp_lexer_peek_token (parser->lexer);
3415
3416 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3417 the already parsed nested-name-specifier. */
3418 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3419 {
3420 /* Grab the nested-name-specifier and continue the loop. */
3421 cp_parser_pre_parsed_nested_name_specifier (parser);
3422 success = true;
3423 continue;
3424 }
3425
a723baf1
MM
3426 /* Spot cases that cannot be the beginning of a
3427 nested-name-specifier. On the second and subsequent times
3428 through the loop, we look for the `template' keyword. */
f7b5ecd9 3429 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3430 ;
3431 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3432 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3433 ;
3434 else
3435 {
3436 /* If the next token is not an identifier, then it is
3437 definitely not a class-or-namespace-name. */
f7b5ecd9 3438 if (token->type != CPP_NAME)
a723baf1
MM
3439 break;
3440 /* If the following token is neither a `<' (to begin a
3441 template-id), nor a `::', then we are not looking at a
3442 nested-name-specifier. */
3443 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3444 if (token->type != CPP_SCOPE
3445 && !cp_parser_nth_token_starts_template_argument_list_p
3446 (parser, 2))
a723baf1
MM
3447 break;
3448 }
3449
3450 /* The nested-name-specifier is optional, so we parse
3451 tentatively. */
3452 cp_parser_parse_tentatively (parser);
3453
3454 /* Look for the optional `template' keyword, if this isn't the
3455 first time through the loop. */
3456 if (success)
3457 template_keyword_p = cp_parser_optional_template_keyword (parser);
3458 else
3459 template_keyword_p = false;
3460
3461 /* Save the old scope since the name lookup we are about to do
3462 might destroy it. */
3463 old_scope = parser->scope;
3464 saved_qualifying_scope = parser->qualifying_scope;
3465 /* Parse the qualifying entity. */
21526606 3466 new_scope
a723baf1
MM
3467 = cp_parser_class_or_namespace_name (parser,
3468 typename_keyword_p,
3469 template_keyword_p,
3470 check_dependency_p,
a668c6ad
MM
3471 type_p,
3472 is_declaration);
a723baf1
MM
3473 /* Look for the `::' token. */
3474 cp_parser_require (parser, CPP_SCOPE, "`::'");
3475
3476 /* If we found what we wanted, we keep going; otherwise, we're
3477 done. */
3478 if (!cp_parser_parse_definitely (parser))
3479 {
3480 bool error_p = false;
3481
3482 /* Restore the OLD_SCOPE since it was valid before the
3483 failed attempt at finding the last
3484 class-or-namespace-name. */
3485 parser->scope = old_scope;
3486 parser->qualifying_scope = saved_qualifying_scope;
3487 /* If the next token is an identifier, and the one after
3488 that is a `::', then any valid interpretation would have
3489 found a class-or-namespace-name. */
3490 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3491 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3492 == CPP_SCOPE)
21526606 3493 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3494 != CPP_COMPL))
3495 {
3496 token = cp_lexer_consume_token (parser->lexer);
21526606 3497 if (!error_p)
a723baf1
MM
3498 {
3499 tree decl;
3500
3501 decl = cp_parser_lookup_name_simple (parser, token->value);
3502 if (TREE_CODE (decl) == TEMPLATE_DECL)
3503 error ("`%D' used without template parameters",
3504 decl);
a723baf1 3505 else
21526606
EC
3506 cp_parser_name_lookup_error
3507 (parser, token->value, decl,
4bb8ca28 3508 "is not a class or namespace");
a723baf1
MM
3509 parser->scope = NULL_TREE;
3510 error_p = true;
eea9800f
MM
3511 /* Treat this as a successful nested-name-specifier
3512 due to:
3513
3514 [basic.lookup.qual]
3515
3516 If the name found is not a class-name (clause
3517 _class_) or namespace-name (_namespace.def_), the
3518 program is ill-formed. */
3519 success = true;
a723baf1
MM
3520 }
3521 cp_lexer_consume_token (parser->lexer);
3522 }
3523 break;
3524 }
3525
3526 /* We've found one valid nested-name-specifier. */
3527 success = true;
3528 /* Make sure we look in the right scope the next time through
3529 the loop. */
21526606 3530 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3531 ? TREE_TYPE (new_scope)
3532 : new_scope);
3533 /* If it is a class scope, try to complete it; we are about to
3534 be looking up names inside the class. */
8fbc5ae7
MM
3535 if (TYPE_P (parser->scope)
3536 /* Since checking types for dependency can be expensive,
3537 avoid doing it if the type is already complete. */
3538 && !COMPLETE_TYPE_P (parser->scope)
3539 /* Do not try to complete dependent types. */
1fb3244a 3540 && !dependent_type_p (parser->scope))
a723baf1
MM
3541 complete_type (parser->scope);
3542 }
3543
cf22909c
KL
3544 /* Retrieve any deferred checks. Do not pop this access checks yet
3545 so the memory will not be reclaimed during token replacing below. */
3546 access_check = get_deferred_access_checks ();
3547
a723baf1
MM
3548 /* If parsing tentatively, replace the sequence of tokens that makes
3549 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3550 token. That way, should we re-parse the token stream, we will
3551 not have to repeat the effort required to do the parse, nor will
3552 we issue duplicate error messages. */
3553 if (success && start >= 0)
3554 {
a723baf1
MM
3555 /* Find the token that corresponds to the start of the
3556 template-id. */
21526606 3557 token = cp_lexer_advance_token (parser->lexer,
a723baf1
MM
3558 parser->lexer->first_token,
3559 start);
3560
a723baf1
MM
3561 /* Reset the contents of the START token. */
3562 token->type = CPP_NESTED_NAME_SPECIFIER;
3563 token->value = build_tree_list (access_check, parser->scope);
3564 TREE_TYPE (token->value) = parser->qualifying_scope;
3565 token->keyword = RID_MAX;
3566 /* Purge all subsequent tokens. */
3567 cp_lexer_purge_tokens_after (parser->lexer, token);
3568 }
3569
cf22909c 3570 pop_deferring_access_checks ();
a723baf1
MM
3571 return success ? parser->scope : NULL_TREE;
3572}
3573
3574/* Parse a nested-name-specifier. See
3575 cp_parser_nested_name_specifier_opt for details. This function
3576 behaves identically, except that it will an issue an error if no
3577 nested-name-specifier is present, and it will return
3578 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3579 is present. */
3580
3581static tree
21526606
EC
3582cp_parser_nested_name_specifier (cp_parser *parser,
3583 bool typename_keyword_p,
a723baf1 3584 bool check_dependency_p,
a668c6ad
MM
3585 bool type_p,
3586 bool is_declaration)
a723baf1
MM
3587{
3588 tree scope;
3589
3590 /* Look for the nested-name-specifier. */
3591 scope = cp_parser_nested_name_specifier_opt (parser,
3592 typename_keyword_p,
3593 check_dependency_p,
a668c6ad
MM
3594 type_p,
3595 is_declaration);
a723baf1
MM
3596 /* If it was not present, issue an error message. */
3597 if (!scope)
3598 {
3599 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3600 parser->scope = NULL_TREE;
a723baf1
MM
3601 return error_mark_node;
3602 }
3603
3604 return scope;
3605}
3606
3607/* Parse a class-or-namespace-name.
3608
3609 class-or-namespace-name:
3610 class-name
3611 namespace-name
3612
3613 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3614 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3615 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3616 TYPE_P is TRUE iff the next name should be taken as a class-name,
3617 even the same name is declared to be another entity in the same
3618 scope.
3619
3620 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3621 specified by the class-or-namespace-name. If neither is found the
3622 ERROR_MARK_NODE is returned. */
a723baf1
MM
3623
3624static tree
21526606 3625cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3626 bool typename_keyword_p,
3627 bool template_keyword_p,
3628 bool check_dependency_p,
a668c6ad
MM
3629 bool type_p,
3630 bool is_declaration)
a723baf1
MM
3631{
3632 tree saved_scope;
3633 tree saved_qualifying_scope;
3634 tree saved_object_scope;
3635 tree scope;
eea9800f 3636 bool only_class_p;
a723baf1 3637
a723baf1
MM
3638 /* Before we try to parse the class-name, we must save away the
3639 current PARSER->SCOPE since cp_parser_class_name will destroy
3640 it. */
3641 saved_scope = parser->scope;
3642 saved_qualifying_scope = parser->qualifying_scope;
3643 saved_object_scope = parser->object_scope;
eea9800f
MM
3644 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3645 there is no need to look for a namespace-name. */
bbaab916 3646 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3647 if (!only_class_p)
3648 cp_parser_parse_tentatively (parser);
21526606 3649 scope = cp_parser_class_name (parser,
a723baf1
MM
3650 typename_keyword_p,
3651 template_keyword_p,
3652 type_p,
a723baf1 3653 check_dependency_p,
a668c6ad
MM
3654 /*class_head_p=*/false,
3655 is_declaration);
a723baf1 3656 /* If that didn't work, try for a namespace-name. */
eea9800f 3657 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3658 {
3659 /* Restore the saved scope. */
3660 parser->scope = saved_scope;
3661 parser->qualifying_scope = saved_qualifying_scope;
3662 parser->object_scope = saved_object_scope;
eea9800f
MM
3663 /* If we are not looking at an identifier followed by the scope
3664 resolution operator, then this is not part of a
3665 nested-name-specifier. (Note that this function is only used
3666 to parse the components of a nested-name-specifier.) */
3667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3668 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3669 return error_mark_node;
a723baf1
MM
3670 scope = cp_parser_namespace_name (parser);
3671 }
3672
3673 return scope;
3674}
3675
3676/* Parse a postfix-expression.
3677
3678 postfix-expression:
3679 primary-expression
3680 postfix-expression [ expression ]
3681 postfix-expression ( expression-list [opt] )
3682 simple-type-specifier ( expression-list [opt] )
21526606 3683 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3684 ( expression-list [opt] )
3685 typename :: [opt] nested-name-specifier template [opt] template-id
3686 ( expression-list [opt] )
3687 postfix-expression . template [opt] id-expression
3688 postfix-expression -> template [opt] id-expression
3689 postfix-expression . pseudo-destructor-name
3690 postfix-expression -> pseudo-destructor-name
3691 postfix-expression ++
3692 postfix-expression --
3693 dynamic_cast < type-id > ( expression )
3694 static_cast < type-id > ( expression )
3695 reinterpret_cast < type-id > ( expression )
3696 const_cast < type-id > ( expression )
3697 typeid ( expression )
3698 typeid ( type-id )
3699
3700 GNU Extension:
21526606 3701
a723baf1
MM
3702 postfix-expression:
3703 ( type-id ) { initializer-list , [opt] }
3704
3705 This extension is a GNU version of the C99 compound-literal
3706 construct. (The C99 grammar uses `type-name' instead of `type-id',
3707 but they are essentially the same concept.)
3708
3709 If ADDRESS_P is true, the postfix expression is the operand of the
3710 `&' operator.
3711
3712 Returns a representation of the expression. */
3713
3714static tree
3715cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3716{
3717 cp_token *token;
3718 enum rid keyword;
b3445994 3719 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3720 tree postfix_expression = NULL_TREE;
3721 /* Non-NULL only if the current postfix-expression can be used to
3722 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3723 class used to qualify the member. */
3724 tree qualifying_class = NULL_TREE;
a723baf1
MM
3725
3726 /* Peek at the next token. */
3727 token = cp_lexer_peek_token (parser->lexer);
3728 /* Some of the productions are determined by keywords. */
3729 keyword = token->keyword;
3730 switch (keyword)
3731 {
3732 case RID_DYNCAST:
3733 case RID_STATCAST:
3734 case RID_REINTCAST:
3735 case RID_CONSTCAST:
3736 {
3737 tree type;
3738 tree expression;
3739 const char *saved_message;
3740
3741 /* All of these can be handled in the same way from the point
3742 of view of parsing. Begin by consuming the token
3743 identifying the cast. */
3744 cp_lexer_consume_token (parser->lexer);
21526606 3745
a723baf1
MM
3746 /* New types cannot be defined in the cast. */
3747 saved_message = parser->type_definition_forbidden_message;
3748 parser->type_definition_forbidden_message
3749 = "types may not be defined in casts";
3750
3751 /* Look for the opening `<'. */
3752 cp_parser_require (parser, CPP_LESS, "`<'");
3753 /* Parse the type to which we are casting. */
3754 type = cp_parser_type_id (parser);
3755 /* Look for the closing `>'. */
3756 cp_parser_require (parser, CPP_GREATER, "`>'");
3757 /* Restore the old message. */
3758 parser->type_definition_forbidden_message = saved_message;
3759
3760 /* And the expression which is being cast. */
3761 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3762 expression = cp_parser_expression (parser);
3763 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3764
14d22dd6
MM
3765 /* Only type conversions to integral or enumeration types
3766 can be used in constant-expressions. */
67c03833 3767 if (parser->integral_constant_expression_p
14d22dd6 3768 && !dependent_type_p (type)
263ee052 3769 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
625cbf93
MM
3770 && (cp_parser_non_integral_constant_expression
3771 (parser,
3772 "a cast to a type other than an integral or "
3773 "enumeration type")))
3774 return error_mark_node;
14d22dd6 3775
a723baf1
MM
3776 switch (keyword)
3777 {
3778 case RID_DYNCAST:
3779 postfix_expression
3780 = build_dynamic_cast (type, expression);
3781 break;
3782 case RID_STATCAST:
3783 postfix_expression
3784 = build_static_cast (type, expression);
3785 break;
3786 case RID_REINTCAST:
3787 postfix_expression
3788 = build_reinterpret_cast (type, expression);
3789 break;
3790 case RID_CONSTCAST:
3791 postfix_expression
3792 = build_const_cast (type, expression);
3793 break;
3794 default:
3795 abort ();
3796 }
3797 }
3798 break;
3799
3800 case RID_TYPEID:
3801 {
3802 tree type;
3803 const char *saved_message;
4f8163b1 3804 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3805
3806 /* Consume the `typeid' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Look for the `(' token. */
3809 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3810 /* Types cannot be defined in a `typeid' expression. */
3811 saved_message = parser->type_definition_forbidden_message;
3812 parser->type_definition_forbidden_message
3813 = "types may not be defined in a `typeid\' expression";
3814 /* We can't be sure yet whether we're looking at a type-id or an
3815 expression. */
3816 cp_parser_parse_tentatively (parser);
3817 /* Try a type-id first. */
4f8163b1
MM
3818 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3819 parser->in_type_id_in_expr_p = true;
a723baf1 3820 type = cp_parser_type_id (parser);
4f8163b1 3821 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3822 /* Look for the `)' token. Otherwise, we can't be sure that
3823 we're not looking at an expression: consider `typeid (int
3824 (3))', for example. */
3825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3826 /* If all went well, simply lookup the type-id. */
3827 if (cp_parser_parse_definitely (parser))
3828 postfix_expression = get_typeid (type);
3829 /* Otherwise, fall back to the expression variant. */
3830 else
3831 {
3832 tree expression;
3833
3834 /* Look for an expression. */
3835 expression = cp_parser_expression (parser);
3836 /* Compute its typeid. */
3837 postfix_expression = build_typeid (expression);
3838 /* Look for the `)' token. */
3839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3840 }
4424e0da
GB
3841 /* `typeid' may not appear in an integral constant expression. */
3842 if (cp_parser_non_integral_constant_expression(parser,
3843 "`typeid' operator"))
3844 return error_mark_node;
a723baf1
MM
3845 /* Restore the saved message. */
3846 parser->type_definition_forbidden_message = saved_message;
3847 }
3848 break;
21526606 3849
a723baf1
MM
3850 case RID_TYPENAME:
3851 {
3852 bool template_p = false;
3853 tree id;
3854 tree type;
3855
3856 /* Consume the `typename' token. */
3857 cp_lexer_consume_token (parser->lexer);
3858 /* Look for the optional `::' operator. */
21526606 3859 cp_parser_global_scope_opt (parser,
a723baf1
MM
3860 /*current_scope_valid_p=*/false);
3861 /* Look for the nested-name-specifier. */
3862 cp_parser_nested_name_specifier (parser,
3863 /*typename_keyword_p=*/true,
3864 /*check_dependency_p=*/true,
a668c6ad
MM
3865 /*type_p=*/true,
3866 /*is_declaration=*/true);
a723baf1
MM
3867 /* Look for the optional `template' keyword. */
3868 template_p = cp_parser_optional_template_keyword (parser);
3869 /* We don't know whether we're looking at a template-id or an
3870 identifier. */
3871 cp_parser_parse_tentatively (parser);
3872 /* Try a template-id. */
3873 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3874 /*check_dependency_p=*/true,
3875 /*is_declaration=*/true);
a723baf1
MM
3876 /* If that didn't work, try an identifier. */
3877 if (!cp_parser_parse_definitely (parser))
3878 id = cp_parser_identifier (parser);
26bcf8fc
MM
3879 /* If we look up a template-id in a non-dependent qualifying
3880 scope, there's no need to create a dependent type. */
3881 if (TREE_CODE (id) == TYPE_DECL
3882 && !dependent_type_p (parser->scope))
3883 type = TREE_TYPE (id);
a723baf1
MM
3884 /* Create a TYPENAME_TYPE to represent the type to which the
3885 functional cast is being performed. */
26bcf8fc
MM
3886 else
3887 type = make_typename_type (parser->scope, id,
3888 /*complain=*/1);
a723baf1
MM
3889
3890 postfix_expression = cp_parser_functional_cast (parser, type);
3891 }
3892 break;
3893
3894 default:
3895 {
3896 tree type;
3897
3898 /* If the next thing is a simple-type-specifier, we may be
3899 looking at a functional cast. We could also be looking at
3900 an id-expression. So, we try the functional cast, and if
3901 that doesn't work we fall back to the primary-expression. */
3902 cp_parser_parse_tentatively (parser);
3903 /* Look for the simple-type-specifier. */
21526606 3904 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
3905 /*decl_specs=*/NULL,
3906 CP_PARSER_FLAGS_NONE);
a723baf1
MM
3907 /* Parse the cast itself. */
3908 if (!cp_parser_error_occurred (parser))
21526606 3909 postfix_expression
a723baf1
MM
3910 = cp_parser_functional_cast (parser, type);
3911 /* If that worked, we're done. */
3912 if (cp_parser_parse_definitely (parser))
3913 break;
3914
3915 /* If the functional-cast didn't work out, try a
3916 compound-literal. */
14d22dd6
MM
3917 if (cp_parser_allow_gnu_extensions_p (parser)
3918 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3919 {
3920 tree initializer_list = NULL_TREE;
4f8163b1 3921 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3922
3923 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3924 /* Consume the `('. */
3925 cp_lexer_consume_token (parser->lexer);
3926 /* Parse the type. */
4f8163b1
MM
3927 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3928 parser->in_type_id_in_expr_p = true;
14d22dd6 3929 type = cp_parser_type_id (parser);
4f8163b1 3930 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3931 /* Look for the `)'. */
3932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3933 /* Look for the `{'. */
3934 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3935 /* If things aren't going well, there's no need to
3936 keep going. */
3937 if (!cp_parser_error_occurred (parser))
a723baf1 3938 {
39703eb9 3939 bool non_constant_p;
14d22dd6 3940 /* Parse the initializer-list. */
21526606 3941 initializer_list
39703eb9 3942 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3943 /* Allow a trailing `,'. */
3944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3945 cp_lexer_consume_token (parser->lexer);
3946 /* Look for the final `}'. */
3947 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3948 }
3949 /* If that worked, we're definitely looking at a
3950 compound-literal expression. */
3951 if (cp_parser_parse_definitely (parser))
3952 {
3953 /* Warn the user that a compound literal is not
3954 allowed in standard C++. */
3955 if (pedantic)
3956 pedwarn ("ISO C++ forbids compound-literals");
3957 /* Form the representation of the compound-literal. */
21526606 3958 postfix_expression
a723baf1
MM
3959 = finish_compound_literal (type, initializer_list);
3960 break;
3961 }
3962 }
3963
3964 /* It must be a primary-expression. */
21526606 3965 postfix_expression = cp_parser_primary_expression (parser,
a723baf1
MM
3966 &idk,
3967 &qualifying_class);
3968 }
3969 break;
3970 }
3971
ee76b931
MM
3972 /* If we were avoiding committing to the processing of a
3973 qualified-id until we knew whether or not we had a
3974 pointer-to-member, we now know. */
089d6ea7 3975 if (qualifying_class)
a723baf1 3976 {
ee76b931 3977 bool done;
a723baf1 3978
ee76b931
MM
3979 /* Peek at the next token. */
3980 token = cp_lexer_peek_token (parser->lexer);
3981 done = (token->type != CPP_OPEN_SQUARE
3982 && token->type != CPP_OPEN_PAREN
3983 && token->type != CPP_DOT
3984 && token->type != CPP_DEREF
3985 && token->type != CPP_PLUS_PLUS
3986 && token->type != CPP_MINUS_MINUS);
3987
3988 postfix_expression = finish_qualified_id_expr (qualifying_class,
3989 postfix_expression,
3990 done,
3991 address_p);
3992 if (done)
3993 return postfix_expression;
a723baf1
MM
3994 }
3995
a723baf1
MM
3996 /* Keep looping until the postfix-expression is complete. */
3997 while (true)
3998 {
10b1d5e7
MM
3999 if (idk == CP_ID_KIND_UNQUALIFIED
4000 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 4001 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 4002 /* It is not a Koenig lookup function call. */
21526606 4003 postfix_expression
b3445994 4004 = unqualified_name_lookup_error (postfix_expression);
21526606 4005
a723baf1
MM
4006 /* Peek at the next token. */
4007 token = cp_lexer_peek_token (parser->lexer);
4008
4009 switch (token->type)
4010 {
4011 case CPP_OPEN_SQUARE:
7a3ea201
RH
4012 postfix_expression
4013 = cp_parser_postfix_open_square_expression (parser,
4014 postfix_expression,
4015 false);
4016 idk = CP_ID_KIND_NONE;
a723baf1
MM
4017 break;
4018
4019 case CPP_OPEN_PAREN:
4020 /* postfix-expression ( expression-list [opt] ) */
4021 {
6d80c4b9 4022 bool koenig_p;
21526606 4023 tree args = (cp_parser_parenthesized_expression_list
39703eb9 4024 (parser, false, /*non_constant_p=*/NULL));
a723baf1 4025
7efa3e22
NS
4026 if (args == error_mark_node)
4027 {
4028 postfix_expression = error_mark_node;
4029 break;
4030 }
21526606 4031
14d22dd6
MM
4032 /* Function calls are not permitted in
4033 constant-expressions. */
625cbf93
MM
4034 if (cp_parser_non_integral_constant_expression (parser,
4035 "a function call"))
14d22dd6 4036 {
625cbf93
MM
4037 postfix_expression = error_mark_node;
4038 break;
14d22dd6 4039 }
a723baf1 4040
6d80c4b9 4041 koenig_p = false;
399dedb9
NS
4042 if (idk == CP_ID_KIND_UNQUALIFIED)
4043 {
676e33ca
MM
4044 /* We do not perform argument-dependent lookup if
4045 normal lookup finds a non-function, in accordance
4046 with the expected resolution of DR 218. */
399dedb9
NS
4047 if (args
4048 && (is_overloaded_fn (postfix_expression)
399dedb9 4049 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
6d80c4b9
MM
4050 {
4051 koenig_p = true;
21526606 4052 postfix_expression
6d80c4b9
MM
4053 = perform_koenig_lookup (postfix_expression, args);
4054 }
399dedb9
NS
4055 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4056 postfix_expression
4057 = unqualified_fn_lookup_error (postfix_expression);
4058 }
21526606 4059
d17811fd 4060 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4061 {
d17811fd
MM
4062 tree instance = TREE_OPERAND (postfix_expression, 0);
4063 tree fn = TREE_OPERAND (postfix_expression, 1);
4064
4065 if (processing_template_decl
4066 && (type_dependent_expression_p (instance)
4067 || (!BASELINK_P (fn)
4068 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4069 || type_dependent_expression_p (fn)
d17811fd
MM
4070 || any_type_dependent_arguments_p (args)))
4071 {
4072 postfix_expression
6de9cd9a
DN
4073 = build_min_nt (CALL_EXPR, postfix_expression,
4074 args, NULL_TREE);
d17811fd
MM
4075 break;
4076 }
9f880ef9
MM
4077
4078 if (BASELINK_P (fn))
4079 postfix_expression
21526606
EC
4080 = (build_new_method_call
4081 (instance, fn, args, NULL_TREE,
4082 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4083 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4084 else
4085 postfix_expression
4086 = finish_call_expr (postfix_expression, args,
4087 /*disallow_virtual=*/false,
4088 /*koenig_p=*/false);
a723baf1 4089 }
d17811fd
MM
4090 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4091 || TREE_CODE (postfix_expression) == MEMBER_REF
4092 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4093 postfix_expression = (build_offset_ref_call_from_tree
4094 (postfix_expression, args));
b3445994 4095 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4096 /* A call to a static class member, or a namespace-scope
4097 function. */
4098 postfix_expression
4099 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4100 /*disallow_virtual=*/true,
4101 koenig_p);
a723baf1 4102 else
2050a1bb 4103 /* All other function calls. */
21526606
EC
4104 postfix_expression
4105 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4106 /*disallow_virtual=*/false,
4107 koenig_p);
a723baf1
MM
4108
4109 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4110 idk = CP_ID_KIND_NONE;
a723baf1
MM
4111 }
4112 break;
21526606 4113
a723baf1
MM
4114 case CPP_DOT:
4115 case CPP_DEREF:
21526606
EC
4116 /* postfix-expression . template [opt] id-expression
4117 postfix-expression . pseudo-destructor-name
a723baf1
MM
4118 postfix-expression -> template [opt] id-expression
4119 postfix-expression -> pseudo-destructor-name */
7a3ea201
RH
4120
4121 /* Consume the `.' or `->' operator. */
4122 cp_lexer_consume_token (parser->lexer);
a723baf1 4123
7a3ea201
RH
4124 postfix_expression
4125 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4126 postfix_expression,
4127 false, &idk);
a723baf1
MM
4128 break;
4129
4130 case CPP_PLUS_PLUS:
4131 /* postfix-expression ++ */
4132 /* Consume the `++' token. */
4133 cp_lexer_consume_token (parser->lexer);
a5ac3982 4134 /* Generate a representation for the complete expression. */
21526606
EC
4135 postfix_expression
4136 = finish_increment_expr (postfix_expression,
a5ac3982 4137 POSTINCREMENT_EXPR);
14d22dd6 4138 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4139 if (cp_parser_non_integral_constant_expression (parser,
4140 "an increment"))
4141 postfix_expression = error_mark_node;
b3445994 4142 idk = CP_ID_KIND_NONE;
a723baf1
MM
4143 break;
4144
4145 case CPP_MINUS_MINUS:
4146 /* postfix-expression -- */
4147 /* Consume the `--' token. */
4148 cp_lexer_consume_token (parser->lexer);
a5ac3982 4149 /* Generate a representation for the complete expression. */
21526606
EC
4150 postfix_expression
4151 = finish_increment_expr (postfix_expression,
a5ac3982 4152 POSTDECREMENT_EXPR);
14d22dd6 4153 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4154 if (cp_parser_non_integral_constant_expression (parser,
4155 "a decrement"))
4156 postfix_expression = error_mark_node;
b3445994 4157 idk = CP_ID_KIND_NONE;
a723baf1
MM
4158 break;
4159
4160 default:
4161 return postfix_expression;
4162 }
4163 }
4164
4165 /* We should never get here. */
4166 abort ();
4167 return error_mark_node;
4168}
4169
7a3ea201
RH
4170/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4171 by cp_parser_builtin_offsetof. We're looking for
4172
4173 postfix-expression [ expression ]
4174
4175 FOR_OFFSETOF is set if we're being called in that context, which
4176 changes how we deal with integer constant expressions. */
4177
4178static tree
4179cp_parser_postfix_open_square_expression (cp_parser *parser,
4180 tree postfix_expression,
4181 bool for_offsetof)
4182{
4183 tree index;
4184
4185 /* Consume the `[' token. */
4186 cp_lexer_consume_token (parser->lexer);
4187
4188 /* Parse the index expression. */
4189 /* ??? For offsetof, there is a question of what to allow here. If
4190 offsetof is not being used in an integral constant expression context,
4191 then we *could* get the right answer by computing the value at runtime.
4192 If we are in an integral constant expression context, then we might
4193 could accept any constant expression; hard to say without analysis.
4194 Rather than open the barn door too wide right away, allow only integer
4195 constant expresions here. */
4196 if (for_offsetof)
4197 index = cp_parser_constant_expression (parser, false, NULL);
4198 else
4199 index = cp_parser_expression (parser);
4200
4201 /* Look for the closing `]'. */
4202 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4203
4204 /* Build the ARRAY_REF. */
4205 postfix_expression = grok_array_decl (postfix_expression, index);
4206
4207 /* When not doing offsetof, array references are not permitted in
4208 constant-expressions. */
4209 if (!for_offsetof
4210 && (cp_parser_non_integral_constant_expression
4211 (parser, "an array reference")))
4212 postfix_expression = error_mark_node;
4213
4214 return postfix_expression;
4215}
4216
4217/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4218 by cp_parser_builtin_offsetof. We're looking for
4219
4220 postfix-expression . template [opt] id-expression
4221 postfix-expression . pseudo-destructor-name
4222 postfix-expression -> template [opt] id-expression
4223 postfix-expression -> pseudo-destructor-name
4224
4225 FOR_OFFSETOF is set if we're being called in that context. That sorta
4226 limits what of the above we'll actually accept, but nevermind.
4227 TOKEN_TYPE is the "." or "->" token, which will already have been
4228 removed from the stream. */
4229
4230static tree
4231cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4232 enum cpp_ttype token_type,
4233 tree postfix_expression,
4234 bool for_offsetof, cp_id_kind *idk)
4235{
4236 tree name;
4237 bool dependent_p;
4238 bool template_p;
4239 tree scope = NULL_TREE;
4240
4241 /* If this is a `->' operator, dereference the pointer. */
4242 if (token_type == CPP_DEREF)
4243 postfix_expression = build_x_arrow (postfix_expression);
4244 /* Check to see whether or not the expression is type-dependent. */
4245 dependent_p = type_dependent_expression_p (postfix_expression);
4246 /* The identifier following the `->' or `.' is not qualified. */
4247 parser->scope = NULL_TREE;
4248 parser->qualifying_scope = NULL_TREE;
4249 parser->object_scope = NULL_TREE;
4250 *idk = CP_ID_KIND_NONE;
4251 /* Enter the scope corresponding to the type of the object
4252 given by the POSTFIX_EXPRESSION. */
4253 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4254 {
4255 scope = TREE_TYPE (postfix_expression);
4256 /* According to the standard, no expression should ever have
4257 reference type. Unfortunately, we do not currently match
4258 the standard in this respect in that our internal representation
4259 of an expression may have reference type even when the standard
4260 says it does not. Therefore, we have to manually obtain the
4261 underlying type here. */
4262 scope = non_reference (scope);
4263 /* The type of the POSTFIX_EXPRESSION must be complete. */
4264 scope = complete_type_or_else (scope, NULL_TREE);
4265 /* Let the name lookup machinery know that we are processing a
4266 class member access expression. */
4267 parser->context->object_type = scope;
4268 /* If something went wrong, we want to be able to discern that case,
4269 as opposed to the case where there was no SCOPE due to the type
4270 of expression being dependent. */
4271 if (!scope)
4272 scope = error_mark_node;
4273 /* If the SCOPE was erroneous, make the various semantic analysis
4274 functions exit quickly -- and without issuing additional error
4275 messages. */
4276 if (scope == error_mark_node)
4277 postfix_expression = error_mark_node;
4278 }
4279
4280 /* If the SCOPE is not a scalar type, we are looking at an
4281 ordinary class member access expression, rather than a
4282 pseudo-destructor-name. */
4283 if (!scope || !SCALAR_TYPE_P (scope))
4284 {
4285 template_p = cp_parser_optional_template_keyword (parser);
4286 /* Parse the id-expression. */
4287 name = cp_parser_id_expression (parser, template_p,
4288 /*check_dependency_p=*/true,
4289 /*template_p=*/NULL,
4290 /*declarator_p=*/false);
4291 /* In general, build a SCOPE_REF if the member name is qualified.
4292 However, if the name was not dependent and has already been
4293 resolved; there is no need to build the SCOPE_REF. For example;
4294
4295 struct X { void f(); };
4296 template <typename T> void f(T* t) { t->X::f(); }
4297
4298 Even though "t" is dependent, "X::f" is not and has been resolved
4299 to a BASELINK; there is no need to include scope information. */
4300
4301 /* But we do need to remember that there was an explicit scope for
4302 virtual function calls. */
4303 if (parser->scope)
4304 *idk = CP_ID_KIND_QUALIFIED;
4305
4306 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4307 {
4308 name = build_nt (SCOPE_REF, parser->scope, name);
4309 parser->scope = NULL_TREE;
4310 parser->qualifying_scope = NULL_TREE;
4311 parser->object_scope = NULL_TREE;
4312 }
4313 if (scope && name && BASELINK_P (name))
4314 adjust_result_of_qualified_name_lookup
4315 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4316 postfix_expression
4317 = finish_class_member_access_expr (postfix_expression, name);
4318 }
4319 /* Otherwise, try the pseudo-destructor-name production. */
4320 else
4321 {
4322 tree s = NULL_TREE;
4323 tree type;
4324
4325 /* Parse the pseudo-destructor-name. */
4326 cp_parser_pseudo_destructor_name (parser, &s, &type);
4327 /* Form the call. */
4328 postfix_expression
4329 = finish_pseudo_destructor_expr (postfix_expression,
4330 s, TREE_TYPE (type));
4331 }
4332
4333 /* We no longer need to look up names in the scope of the object on
4334 the left-hand side of the `.' or `->' operator. */
4335 parser->context->object_type = NULL_TREE;
4336
4337 /* Outside of offsetof, these operators may not appear in
4338 constant-expressions. */
4339 if (!for_offsetof
4340 && (cp_parser_non_integral_constant_expression
4341 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4342 postfix_expression = error_mark_node;
4343
4344 return postfix_expression;
4345}
4346
7efa3e22 4347/* Parse a parenthesized expression-list.
a723baf1
MM
4348
4349 expression-list:
4350 assignment-expression
4351 expression-list, assignment-expression
4352
7efa3e22
NS
4353 attribute-list:
4354 expression-list
4355 identifier
4356 identifier, expression-list
4357
a723baf1
MM
4358 Returns a TREE_LIST. The TREE_VALUE of each node is a
4359 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4360 is returned even if there is only a single expression in the list.
4361 error_mark_node is returned if the ( and or ) are
4362 missing. NULL_TREE is returned on no expressions. The parentheses
4363 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4364 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4365 indicates whether or not all of the expressions in the list were
4366 constant. */
a723baf1
MM
4367
4368static tree
21526606 4369cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9
MM
4370 bool is_attribute_list,
4371 bool *non_constant_p)
a723baf1
MM
4372{
4373 tree expression_list = NULL_TREE;
7efa3e22 4374 tree identifier = NULL_TREE;
39703eb9
MM
4375
4376 /* Assume all the expressions will be constant. */
4377 if (non_constant_p)
4378 *non_constant_p = false;
4379
7efa3e22
NS
4380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4381 return error_mark_node;
21526606 4382
a723baf1 4383 /* Consume expressions until there are no more. */
7efa3e22
NS
4384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4385 while (true)
4386 {
4387 tree expr;
21526606 4388
7efa3e22
NS
4389 /* At the beginning of attribute lists, check to see if the
4390 next token is an identifier. */
4391 if (is_attribute_list
4392 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4393 {
4394 cp_token *token;
21526606 4395
7efa3e22
NS
4396 /* Consume the identifier. */
4397 token = cp_lexer_consume_token (parser->lexer);
4398 /* Save the identifier. */
4399 identifier = token->value;
4400 }
4401 else
4402 {
4403 /* Parse the next assignment-expression. */
39703eb9
MM
4404 if (non_constant_p)
4405 {
4406 bool expr_non_constant_p;
21526606 4407 expr = (cp_parser_constant_expression
39703eb9
MM
4408 (parser, /*allow_non_constant_p=*/true,
4409 &expr_non_constant_p));
4410 if (expr_non_constant_p)
4411 *non_constant_p = true;
4412 }
4413 else
4414 expr = cp_parser_assignment_expression (parser);
a723baf1 4415
7efa3e22
NS
4416 /* Add it to the list. We add error_mark_node
4417 expressions to the list, so that we can still tell if
4418 the correct form for a parenthesized expression-list
4419 is found. That gives better errors. */
4420 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4421
7efa3e22
NS
4422 if (expr == error_mark_node)
4423 goto skip_comma;
4424 }
a723baf1 4425
7efa3e22
NS
4426 /* After the first item, attribute lists look the same as
4427 expression lists. */
4428 is_attribute_list = false;
21526606 4429
7efa3e22
NS
4430 get_comma:;
4431 /* If the next token isn't a `,', then we are done. */
4432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4433 break;
4434
4435 /* Otherwise, consume the `,' and keep going. */
4436 cp_lexer_consume_token (parser->lexer);
4437 }
21526606 4438
7efa3e22
NS
4439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4440 {
4441 int ending;
21526606 4442
7efa3e22
NS
4443 skip_comma:;
4444 /* We try and resync to an unnested comma, as that will give the
4445 user better diagnostics. */
21526606
EC
4446 ending = cp_parser_skip_to_closing_parenthesis (parser,
4447 /*recovering=*/true,
4bb8ca28 4448 /*or_comma=*/true,
a668c6ad 4449 /*consume_paren=*/true);
7efa3e22
NS
4450 if (ending < 0)
4451 goto get_comma;
4452 if (!ending)
4453 return error_mark_node;
a723baf1
MM
4454 }
4455
4456 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4457 expression_list = nreverse (expression_list);
4458 if (identifier)
4459 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4460
7efa3e22 4461 return expression_list;
a723baf1
MM
4462}
4463
4464/* Parse a pseudo-destructor-name.
4465
4466 pseudo-destructor-name:
4467 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4468 :: [opt] nested-name-specifier template template-id :: ~ type-name
4469 :: [opt] nested-name-specifier [opt] ~ type-name
4470
4471 If either of the first two productions is used, sets *SCOPE to the
4472 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4473 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4474 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4475
4476static void
21526606
EC
4477cp_parser_pseudo_destructor_name (cp_parser* parser,
4478 tree* scope,
94edc4ab 4479 tree* type)
a723baf1
MM
4480{
4481 bool nested_name_specifier_p;
4482
4483 /* Look for the optional `::' operator. */
4484 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4485 /* Look for the optional nested-name-specifier. */
21526606 4486 nested_name_specifier_p
a723baf1
MM
4487 = (cp_parser_nested_name_specifier_opt (parser,
4488 /*typename_keyword_p=*/false,
4489 /*check_dependency_p=*/true,
a668c6ad 4490 /*type_p=*/false,
21526606 4491 /*is_declaration=*/true)
a723baf1
MM
4492 != NULL_TREE);
4493 /* Now, if we saw a nested-name-specifier, we might be doing the
4494 second production. */
21526606 4495 if (nested_name_specifier_p
a723baf1
MM
4496 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4497 {
4498 /* Consume the `template' keyword. */
4499 cp_lexer_consume_token (parser->lexer);
4500 /* Parse the template-id. */
21526606 4501 cp_parser_template_id (parser,
a723baf1 4502 /*template_keyword_p=*/true,
a668c6ad
MM
4503 /*check_dependency_p=*/false,
4504 /*is_declaration=*/true);
a723baf1
MM
4505 /* Look for the `::' token. */
4506 cp_parser_require (parser, CPP_SCOPE, "`::'");
4507 }
4508 /* If the next token is not a `~', then there might be some
9bcb9aae 4509 additional qualification. */
a723baf1
MM
4510 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4511 {
4512 /* Look for the type-name. */
4513 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462
ILT
4514
4515 /* If we didn't get an aggregate type, or we don't have ::~,
4516 then something has gone wrong. Since the only caller of this
4517 function is looking for something after `.' or `->' after a
4518 scalar type, most likely the program is trying to get a
4519 member of a non-aggregate type. */
4520 if (*scope == error_mark_node
4521 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4522 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4523 {
4524 cp_parser_error (parser, "request for member of non-aggregate type");
4525 *type = error_mark_node;
4526 return;
4527 }
4528
a723baf1
MM
4529 /* Look for the `::' token. */
4530 cp_parser_require (parser, CPP_SCOPE, "`::'");
4531 }
4532 else
4533 *scope = NULL_TREE;
4534
4535 /* Look for the `~'. */
4536 cp_parser_require (parser, CPP_COMPL, "`~'");
4537 /* Look for the type-name again. We are not responsible for
4538 checking that it matches the first type-name. */
4539 *type = cp_parser_type_name (parser);
4540}
4541
4542/* Parse a unary-expression.
4543
4544 unary-expression:
4545 postfix-expression
4546 ++ cast-expression
4547 -- cast-expression
4548 unary-operator cast-expression
4549 sizeof unary-expression
4550 sizeof ( type-id )
4551 new-expression
4552 delete-expression
4553
4554 GNU Extensions:
4555
4556 unary-expression:
4557 __extension__ cast-expression
4558 __alignof__ unary-expression
4559 __alignof__ ( type-id )
4560 __real__ cast-expression
4561 __imag__ cast-expression
4562 && identifier
4563
4564 ADDRESS_P is true iff the unary-expression is appearing as the
4565 operand of the `&' operator.
4566
34cd5ae7 4567 Returns a representation of the expression. */
a723baf1
MM
4568
4569static tree
4570cp_parser_unary_expression (cp_parser *parser, bool address_p)
4571{
4572 cp_token *token;
4573 enum tree_code unary_operator;
4574
4575 /* Peek at the next token. */
4576 token = cp_lexer_peek_token (parser->lexer);
4577 /* Some keywords give away the kind of expression. */
4578 if (token->type == CPP_KEYWORD)
4579 {
4580 enum rid keyword = token->keyword;
4581
4582 switch (keyword)
4583 {
4584 case RID_ALIGNOF:
a723baf1
MM
4585 case RID_SIZEOF:
4586 {
4587 tree operand;
7a18b933 4588 enum tree_code op;
21526606 4589
7a18b933
NS
4590 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4591 /* Consume the token. */
a723baf1
MM
4592 cp_lexer_consume_token (parser->lexer);
4593 /* Parse the operand. */
4594 operand = cp_parser_sizeof_operand (parser, keyword);
4595
7a18b933
NS
4596 if (TYPE_P (operand))
4597 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4598 else
7a18b933 4599 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4600 }
4601
4602 case RID_NEW:
4603 return cp_parser_new_expression (parser);
4604
4605 case RID_DELETE:
4606 return cp_parser_delete_expression (parser);
21526606 4607
a723baf1
MM
4608 case RID_EXTENSION:
4609 {
4610 /* The saved value of the PEDANTIC flag. */
4611 int saved_pedantic;
4612 tree expr;
4613
4614 /* Save away the PEDANTIC flag. */
4615 cp_parser_extension_opt (parser, &saved_pedantic);
4616 /* Parse the cast-expression. */
d6b4ea85 4617 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4618 /* Restore the PEDANTIC flag. */
4619 pedantic = saved_pedantic;
4620
4621 return expr;
4622 }
4623
4624 case RID_REALPART:
4625 case RID_IMAGPART:
4626 {
4627 tree expression;
4628
4629 /* Consume the `__real__' or `__imag__' token. */
4630 cp_lexer_consume_token (parser->lexer);
4631 /* Parse the cast-expression. */
d6b4ea85 4632 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4633 /* Create the complete representation. */
4634 return build_x_unary_op ((keyword == RID_REALPART
4635 ? REALPART_EXPR : IMAGPART_EXPR),
4636 expression);
4637 }
4638 break;
4639
4640 default:
4641 break;
4642 }
4643 }
4644
4645 /* Look for the `:: new' and `:: delete', which also signal the
4646 beginning of a new-expression, or delete-expression,
4647 respectively. If the next token is `::', then it might be one of
4648 these. */
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4650 {
4651 enum rid keyword;
4652
4653 /* See if the token after the `::' is one of the keywords in
4654 which we're interested. */
4655 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4656 /* If it's `new', we have a new-expression. */
4657 if (keyword == RID_NEW)
4658 return cp_parser_new_expression (parser);
4659 /* Similarly, for `delete'. */
4660 else if (keyword == RID_DELETE)
4661 return cp_parser_delete_expression (parser);
4662 }
4663
4664 /* Look for a unary operator. */
4665 unary_operator = cp_parser_unary_operator (token);
4666 /* The `++' and `--' operators can be handled similarly, even though
4667 they are not technically unary-operators in the grammar. */
4668 if (unary_operator == ERROR_MARK)
4669 {
4670 if (token->type == CPP_PLUS_PLUS)
4671 unary_operator = PREINCREMENT_EXPR;
4672 else if (token->type == CPP_MINUS_MINUS)
4673 unary_operator = PREDECREMENT_EXPR;
4674 /* Handle the GNU address-of-label extension. */
4675 else if (cp_parser_allow_gnu_extensions_p (parser)
4676 && token->type == CPP_AND_AND)
4677 {
4678 tree identifier;
4679
4680 /* Consume the '&&' token. */
4681 cp_lexer_consume_token (parser->lexer);
4682 /* Look for the identifier. */
4683 identifier = cp_parser_identifier (parser);
4684 /* Create an expression representing the address. */
4685 return finish_label_address_expr (identifier);
4686 }
4687 }
4688 if (unary_operator != ERROR_MARK)
4689 {
4690 tree cast_expression;
a5ac3982
MM
4691 tree expression = error_mark_node;
4692 const char *non_constant_p = NULL;
a723baf1
MM
4693
4694 /* Consume the operator token. */
4695 token = cp_lexer_consume_token (parser->lexer);
4696 /* Parse the cast-expression. */
21526606 4697 cast_expression
a723baf1
MM
4698 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4699 /* Now, build an appropriate representation. */
4700 switch (unary_operator)
4701 {
4702 case INDIRECT_REF:
a5ac3982
MM
4703 non_constant_p = "`*'";
4704 expression = build_x_indirect_ref (cast_expression, "unary *");
4705 break;
4706
a723baf1 4707 case ADDR_EXPR:
7a3ea201 4708 non_constant_p = "`&'";
a5ac3982 4709 /* Fall through. */
d17811fd 4710 case BIT_NOT_EXPR:
a5ac3982
MM
4711 expression = build_x_unary_op (unary_operator, cast_expression);
4712 break;
4713
14d22dd6
MM
4714 case PREINCREMENT_EXPR:
4715 case PREDECREMENT_EXPR:
a5ac3982
MM
4716 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4717 ? "`++'" : "`--'");
14d22dd6 4718 /* Fall through. */
a723baf1
MM
4719 case CONVERT_EXPR:
4720 case NEGATE_EXPR:
4721 case TRUTH_NOT_EXPR:
a5ac3982
MM
4722 expression = finish_unary_op_expr (unary_operator, cast_expression);
4723 break;
a723baf1 4724
a723baf1
MM
4725 default:
4726 abort ();
a723baf1 4727 }
a5ac3982 4728
625cbf93
MM
4729 if (non_constant_p
4730 && cp_parser_non_integral_constant_expression (parser,
4731 non_constant_p))
4732 expression = error_mark_node;
a5ac3982
MM
4733
4734 return expression;
a723baf1
MM
4735 }
4736
4737 return cp_parser_postfix_expression (parser, address_p);
4738}
4739
4740/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4741 unary-operator, the corresponding tree code is returned. */
4742
4743static enum tree_code
94edc4ab 4744cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4745{
4746 switch (token->type)
4747 {
4748 case CPP_MULT:
4749 return INDIRECT_REF;
4750
4751 case CPP_AND:
4752 return ADDR_EXPR;
4753
4754 case CPP_PLUS:
4755 return CONVERT_EXPR;
4756
4757 case CPP_MINUS:
4758 return NEGATE_EXPR;
4759
4760 case CPP_NOT:
4761 return TRUTH_NOT_EXPR;
21526606 4762
a723baf1
MM
4763 case CPP_COMPL:
4764 return BIT_NOT_EXPR;
4765
4766 default:
4767 return ERROR_MARK;
4768 }
4769}
4770
4771/* Parse a new-expression.
4772
ca099ac8 4773 new-expression:
a723baf1
MM
4774 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4775 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4776
4777 Returns a representation of the expression. */
4778
4779static tree
94edc4ab 4780cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4781{
4782 bool global_scope_p;
4783 tree placement;
4784 tree type;
4785 tree initializer;
058b15c1 4786 tree nelts;
a723baf1
MM
4787
4788 /* Look for the optional `::' operator. */
21526606 4789 global_scope_p
a723baf1
MM
4790 = (cp_parser_global_scope_opt (parser,
4791 /*current_scope_valid_p=*/false)
4792 != NULL_TREE);
4793 /* Look for the `new' operator. */
4794 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4795 /* There's no easy way to tell a new-placement from the
4796 `( type-id )' construct. */
4797 cp_parser_parse_tentatively (parser);
4798 /* Look for a new-placement. */
4799 placement = cp_parser_new_placement (parser);
4800 /* If that didn't work out, there's no new-placement. */
4801 if (!cp_parser_parse_definitely (parser))
4802 placement = NULL_TREE;
4803
4804 /* If the next token is a `(', then we have a parenthesized
4805 type-id. */
4806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4807 {
4808 /* Consume the `('. */
4809 cp_lexer_consume_token (parser->lexer);
4810 /* Parse the type-id. */
4811 type = cp_parser_type_id (parser);
4812 /* Look for the closing `)'. */
4813 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
063e900f
GB
4814 /* There should not be a direct-new-declarator in this production,
4815 but GCC used to allowed this, so we check and emit a sensible error
4816 message for this case. */
4817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4818 {
4819 error ("array bound forbidden after parenthesized type-id");
4820 inform ("try removing the parentheses around the type-id");
063e900f
GB
4821 cp_parser_direct_new_declarator (parser);
4822 }
058b15c1 4823 nelts = integer_one_node;
a723baf1
MM
4824 }
4825 /* Otherwise, there must be a new-type-id. */
4826 else
058b15c1 4827 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
4828
4829 /* If the next token is a `(', then we have a new-initializer. */
4830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4831 initializer = cp_parser_new_initializer (parser);
4832 else
4833 initializer = NULL_TREE;
4834
625cbf93
MM
4835 /* A new-expression may not appear in an integral constant
4836 expression. */
4837 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4838 return error_mark_node;
4839
a723baf1 4840 /* Create a representation of the new-expression. */
058b15c1 4841 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
4842}
4843
4844/* Parse a new-placement.
4845
4846 new-placement:
4847 ( expression-list )
4848
4849 Returns the same representation as for an expression-list. */
4850
4851static tree
94edc4ab 4852cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4853{
4854 tree expression_list;
4855
a723baf1 4856 /* Parse the expression-list. */
21526606 4857 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4858 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4859
4860 return expression_list;
4861}
4862
4863/* Parse a new-type-id.
4864
4865 new-type-id:
4866 type-specifier-seq new-declarator [opt]
4867
058b15c1
MM
4868 Returns the TYPE allocated. If the new-type-id indicates an array
4869 type, *NELTS is set to the number of elements in the last array
4870 bound; the TYPE will not include the last array bound. */
a723baf1
MM
4871
4872static tree
058b15c1 4873cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 4874{
62d1db17 4875 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
4876 cp_declarator *new_declarator;
4877 cp_declarator *declarator;
4878 cp_declarator *outer_declarator;
a723baf1 4879 const char *saved_message;
058b15c1 4880 tree type;
a723baf1
MM
4881
4882 /* The type-specifier sequence must not contain type definitions.
4883 (It cannot contain declarations of new types either, but if they
4884 are not definitions we will catch that because they are not
4885 complete.) */
4886 saved_message = parser->type_definition_forbidden_message;
4887 parser->type_definition_forbidden_message
4888 = "types may not be defined in a new-type-id";
4889 /* Parse the type-specifier-seq. */
62d1db17 4890 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
a723baf1
MM
4891 /* Restore the old message. */
4892 parser->type_definition_forbidden_message = saved_message;
4893 /* Parse the new-declarator. */
058b15c1
MM
4894 new_declarator = cp_parser_new_declarator_opt (parser);
4895
4896 /* Determine the number of elements in the last array dimension, if
4897 any. */
4898 *nelts = NULL_TREE;
4899 /* Skip down to the last array dimension. */
4900 declarator = new_declarator;
4901 outer_declarator = NULL;
4902 while (declarator && (declarator->kind == cdk_pointer
4903 || declarator->kind == cdk_ptrmem))
4904 {
4905 outer_declarator = declarator;
4906 declarator = declarator->declarator;
4907 }
4908 while (declarator
4909 && declarator->kind == cdk_array
4910 && declarator->declarator
4911 && declarator->declarator->kind == cdk_array)
4912 {
4913 outer_declarator = declarator;
4914 declarator = declarator->declarator;
4915 }
4916
4917 if (declarator && declarator->kind == cdk_array)
4918 {
4919 *nelts = declarator->u.array.bounds;
4920 if (*nelts == error_mark_node)
4921 *nelts = integer_one_node;
4922 else if (!processing_template_decl)
4923 {
4924 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4925 false))
4926 pedwarn ("size in array new must have integral type");
4927 *nelts = save_expr (cp_convert (sizetype, *nelts));
4928 if (*nelts == integer_zero_node)
4929 warning ("zero size array reserves no space");
4930 }
4931 if (outer_declarator)
4932 outer_declarator->declarator = declarator->declarator;
4933 else
4934 new_declarator = NULL;
4935 }
a723baf1 4936
62d1db17 4937 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
4938 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4939 {
4940 *nelts = array_type_nelts_top (type);
4941 type = TREE_TYPE (type);
4942 }
4943 return type;
a723baf1
MM
4944}
4945
4946/* Parse an (optional) new-declarator.
4947
4948 new-declarator:
4949 ptr-operator new-declarator [opt]
4950 direct-new-declarator
4951
058b15c1 4952 Returns the declarator. */
a723baf1 4953
058b15c1 4954static cp_declarator *
94edc4ab 4955cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4956{
4957 enum tree_code code;
4958 tree type;
4959 tree cv_qualifier_seq;
4960
4961 /* We don't know if there's a ptr-operator next, or not. */
4962 cp_parser_parse_tentatively (parser);
4963 /* Look for a ptr-operator. */
4964 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4965 /* If that worked, look for more new-declarators. */
4966 if (cp_parser_parse_definitely (parser))
4967 {
058b15c1 4968 cp_declarator *declarator;
a723baf1
MM
4969
4970 /* Parse another optional declarator. */
4971 declarator = cp_parser_new_declarator_opt (parser);
4972
4973 /* Create the representation of the declarator. */
058b15c1
MM
4974 if (type)
4975 declarator = make_ptrmem_declarator (cv_qualifier_seq,
4976 type,
4977 declarator);
4978 else if (code == INDIRECT_REF)
a723baf1
MM
4979 declarator = make_pointer_declarator (cv_qualifier_seq,
4980 declarator);
4981 else
4982 declarator = make_reference_declarator (cv_qualifier_seq,
4983 declarator);
4984
a723baf1
MM
4985 return declarator;
4986 }
4987
4988 /* If the next token is a `[', there is a direct-new-declarator. */
4989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4990 return cp_parser_direct_new_declarator (parser);
4991
058b15c1 4992 return NULL;
a723baf1
MM
4993}
4994
4995/* Parse a direct-new-declarator.
4996
4997 direct-new-declarator:
4998 [ expression ]
21526606 4999 direct-new-declarator [constant-expression]
a723baf1 5000
058b15c1 5001 */
a723baf1 5002
058b15c1 5003static cp_declarator *
94edc4ab 5004cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 5005{
058b15c1 5006 cp_declarator *declarator = NULL;
a723baf1
MM
5007
5008 while (true)
5009 {
5010 tree expression;
5011
5012 /* Look for the opening `['. */
5013 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5014 /* The first expression is not required to be constant. */
5015 if (!declarator)
5016 {
5017 expression = cp_parser_expression (parser);
5018 /* The standard requires that the expression have integral
5019 type. DR 74 adds enumeration types. We believe that the
5020 real intent is that these expressions be handled like the
5021 expression in a `switch' condition, which also allows
5022 classes with a single conversion to integral or
5023 enumeration type. */
5024 if (!processing_template_decl)
5025 {
21526606 5026 expression
a723baf1
MM
5027 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5028 expression,
b746c5dc 5029 /*complain=*/true);
a723baf1
MM
5030 if (!expression)
5031 {
5032 error ("expression in new-declarator must have integral or enumeration type");
5033 expression = error_mark_node;
5034 }
5035 }
5036 }
5037 /* But all the other expressions must be. */
5038 else
21526606
EC
5039 expression
5040 = cp_parser_constant_expression (parser,
14d22dd6
MM
5041 /*allow_non_constant=*/false,
5042 NULL);
a723baf1
MM
5043 /* Look for the closing `]'. */
5044 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5045
5046 /* Add this bound to the declarator. */
058b15c1 5047 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5048
5049 /* If the next token is not a `[', then there are no more
5050 bounds. */
5051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5052 break;
5053 }
5054
5055 return declarator;
5056}
5057
5058/* Parse a new-initializer.
5059
5060 new-initializer:
5061 ( expression-list [opt] )
5062
34cd5ae7 5063 Returns a representation of the expression-list. If there is no
a723baf1
MM
5064 expression-list, VOID_ZERO_NODE is returned. */
5065
5066static tree
94edc4ab 5067cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5068{
5069 tree expression_list;
5070
21526606 5071 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 5072 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 5073 if (!expression_list)
a723baf1 5074 expression_list = void_zero_node;
a723baf1
MM
5075
5076 return expression_list;
5077}
5078
5079/* Parse a delete-expression.
5080
5081 delete-expression:
5082 :: [opt] delete cast-expression
5083 :: [opt] delete [ ] cast-expression
5084
5085 Returns a representation of the expression. */
5086
5087static tree
94edc4ab 5088cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5089{
5090 bool global_scope_p;
5091 bool array_p;
5092 tree expression;
5093
5094 /* Look for the optional `::' operator. */
5095 global_scope_p
5096 = (cp_parser_global_scope_opt (parser,
5097 /*current_scope_valid_p=*/false)
5098 != NULL_TREE);
5099 /* Look for the `delete' keyword. */
5100 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5101 /* See if the array syntax is in use. */
5102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5103 {
5104 /* Consume the `[' token. */
5105 cp_lexer_consume_token (parser->lexer);
5106 /* Look for the `]' token. */
5107 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5108 /* Remember that this is the `[]' construct. */
5109 array_p = true;
5110 }
5111 else
5112 array_p = false;
5113
5114 /* Parse the cast-expression. */
d6b4ea85 5115 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5116
625cbf93
MM
5117 /* A delete-expression may not appear in an integral constant
5118 expression. */
5119 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5120 return error_mark_node;
5121
a723baf1
MM
5122 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5123}
5124
5125/* Parse a cast-expression.
5126
5127 cast-expression:
5128 unary-expression
5129 ( type-id ) cast-expression
5130
5131 Returns a representation of the expression. */
5132
5133static tree
5134cp_parser_cast_expression (cp_parser *parser, bool address_p)
5135{
5136 /* If it's a `(', then we might be looking at a cast. */
5137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5138 {
5139 tree type = NULL_TREE;
5140 tree expr = NULL_TREE;
5141 bool compound_literal_p;
5142 const char *saved_message;
5143
5144 /* There's no way to know yet whether or not this is a cast.
5145 For example, `(int (3))' is a unary-expression, while `(int)
5146 3' is a cast. So, we resort to parsing tentatively. */
5147 cp_parser_parse_tentatively (parser);
5148 /* Types may not be defined in a cast. */
5149 saved_message = parser->type_definition_forbidden_message;
5150 parser->type_definition_forbidden_message
5151 = "types may not be defined in casts";
5152 /* Consume the `('. */
5153 cp_lexer_consume_token (parser->lexer);
5154 /* A very tricky bit is that `(struct S) { 3 }' is a
5155 compound-literal (which we permit in C++ as an extension).
5156 But, that construct is not a cast-expression -- it is a
5157 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5158 is legal; if the compound-literal were a cast-expression,
5159 you'd need an extra set of parentheses.) But, if we parse
5160 the type-id, and it happens to be a class-specifier, then we
5161 will commit to the parse at that point, because we cannot
5162 undo the action that is done when creating a new class. So,
21526606 5163 then we cannot back up and do a postfix-expression.
a723baf1
MM
5164
5165 Therefore, we scan ahead to the closing `)', and check to see
5166 if the token after the `)' is a `{'. If so, we are not
21526606 5167 looking at a cast-expression.
a723baf1
MM
5168
5169 Save tokens so that we can put them back. */
5170 cp_lexer_save_tokens (parser->lexer);
5171 /* Skip tokens until the next token is a closing parenthesis.
5172 If we find the closing `)', and the next token is a `{', then
5173 we are looking at a compound-literal. */
21526606 5174 compound_literal_p
a668c6ad
MM
5175 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5176 /*consume_paren=*/true)
a723baf1
MM
5177 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5178 /* Roll back the tokens we skipped. */
5179 cp_lexer_rollback_tokens (parser->lexer);
5180 /* If we were looking at a compound-literal, simulate an error
5181 so that the call to cp_parser_parse_definitely below will
5182 fail. */
5183 if (compound_literal_p)
5184 cp_parser_simulate_error (parser);
5185 else
5186 {
4f8163b1
MM
5187 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5188 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5189 /* Look for the type-id. */
5190 type = cp_parser_type_id (parser);
5191 /* Look for the closing `)'. */
5192 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5193 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5194 }
5195
5196 /* Restore the saved message. */
5197 parser->type_definition_forbidden_message = saved_message;
5198
bbaab916
NS
5199 /* If ok so far, parse the dependent expression. We cannot be
5200 sure it is a cast. Consider `(T ())'. It is a parenthesized
5201 ctor of T, but looks like a cast to function returning T
5202 without a dependent expression. */
5203 if (!cp_parser_error_occurred (parser))
d6b4ea85 5204 expr = cp_parser_simple_cast_expression (parser);
bbaab916 5205
a723baf1
MM
5206 if (cp_parser_parse_definitely (parser))
5207 {
a723baf1 5208 /* Warn about old-style casts, if so requested. */
21526606
EC
5209 if (warn_old_style_cast
5210 && !in_system_header
5211 && !VOID_TYPE_P (type)
a723baf1
MM
5212 && current_lang_name != lang_name_c)
5213 warning ("use of old-style cast");
14d22dd6
MM
5214
5215 /* Only type conversions to integral or enumeration types
5216 can be used in constant-expressions. */
67c03833 5217 if (parser->integral_constant_expression_p
14d22dd6 5218 && !dependent_type_p (type)
625cbf93
MM
5219 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5220 && (cp_parser_non_integral_constant_expression
5221 (parser,
5222 "a cast to a type other than an integral or "
5223 "enumeration type")))
5224 return error_mark_node;
5225
a723baf1
MM
5226 /* Perform the cast. */
5227 expr = build_c_cast (type, expr);
bbaab916 5228 return expr;
a723baf1 5229 }
a723baf1
MM
5230 }
5231
5232 /* If we get here, then it's not a cast, so it must be a
5233 unary-expression. */
5234 return cp_parser_unary_expression (parser, address_p);
5235}
5236
5237/* Parse a pm-expression.
5238
5239 pm-expression:
5240 cast-expression
5241 pm-expression .* cast-expression
5242 pm-expression ->* cast-expression
5243
5244 Returns a representation of the expression. */
5245
5246static tree
94edc4ab 5247cp_parser_pm_expression (cp_parser* parser)
a723baf1 5248{
d6b4ea85
MM
5249 static const cp_parser_token_tree_map map = {
5250 { CPP_DEREF_STAR, MEMBER_REF },
5251 { CPP_DOT_STAR, DOTSTAR_EXPR },
5252 { CPP_EOF, ERROR_MARK }
5253 };
a723baf1 5254
21526606 5255 return cp_parser_binary_expression (parser, map,
d6b4ea85 5256 cp_parser_simple_cast_expression);
a723baf1
MM
5257}
5258
5259/* Parse a multiplicative-expression.
5260
77077b39 5261 multiplicative-expression:
a723baf1
MM
5262 pm-expression
5263 multiplicative-expression * pm-expression
5264 multiplicative-expression / pm-expression
5265 multiplicative-expression % pm-expression
5266
5267 Returns a representation of the expression. */
5268
5269static tree
94edc4ab 5270cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 5271{
39b1af70 5272 static const cp_parser_token_tree_map map = {
a723baf1
MM
5273 { CPP_MULT, MULT_EXPR },
5274 { CPP_DIV, TRUNC_DIV_EXPR },
5275 { CPP_MOD, TRUNC_MOD_EXPR },
5276 { CPP_EOF, ERROR_MARK }
5277 };
5278
5279 return cp_parser_binary_expression (parser,
5280 map,
5281 cp_parser_pm_expression);
5282}
5283
5284/* Parse an additive-expression.
5285
5286 additive-expression:
5287 multiplicative-expression
5288 additive-expression + multiplicative-expression
5289 additive-expression - multiplicative-expression
5290
5291 Returns a representation of the expression. */
5292
5293static tree
94edc4ab 5294cp_parser_additive_expression (cp_parser* parser)
a723baf1 5295{
39b1af70 5296 static const cp_parser_token_tree_map map = {
a723baf1
MM
5297 { CPP_PLUS, PLUS_EXPR },
5298 { CPP_MINUS, MINUS_EXPR },
5299 { CPP_EOF, ERROR_MARK }
5300 };
5301
5302 return cp_parser_binary_expression (parser,
5303 map,
5304 cp_parser_multiplicative_expression);
5305}
5306
5307/* Parse a shift-expression.
5308
5309 shift-expression:
5310 additive-expression
5311 shift-expression << additive-expression
5312 shift-expression >> additive-expression
5313
5314 Returns a representation of the expression. */
5315
5316static tree
94edc4ab 5317cp_parser_shift_expression (cp_parser* parser)
a723baf1 5318{
39b1af70 5319 static const cp_parser_token_tree_map map = {
a723baf1
MM
5320 { CPP_LSHIFT, LSHIFT_EXPR },
5321 { CPP_RSHIFT, RSHIFT_EXPR },
5322 { CPP_EOF, ERROR_MARK }
5323 };
5324
5325 return cp_parser_binary_expression (parser,
5326 map,
5327 cp_parser_additive_expression);
5328}
5329
5330/* Parse a relational-expression.
5331
5332 relational-expression:
5333 shift-expression
5334 relational-expression < shift-expression
5335 relational-expression > shift-expression
5336 relational-expression <= shift-expression
5337 relational-expression >= shift-expression
5338
5339 GNU Extension:
5340
5341 relational-expression:
5342 relational-expression <? shift-expression
5343 relational-expression >? shift-expression
5344
5345 Returns a representation of the expression. */
5346
5347static tree
94edc4ab 5348cp_parser_relational_expression (cp_parser* parser)
a723baf1 5349{
39b1af70 5350 static const cp_parser_token_tree_map map = {
a723baf1
MM
5351 { CPP_LESS, LT_EXPR },
5352 { CPP_GREATER, GT_EXPR },
5353 { CPP_LESS_EQ, LE_EXPR },
5354 { CPP_GREATER_EQ, GE_EXPR },
5355 { CPP_MIN, MIN_EXPR },
5356 { CPP_MAX, MAX_EXPR },
5357 { CPP_EOF, ERROR_MARK }
5358 };
5359
5360 return cp_parser_binary_expression (parser,
5361 map,
5362 cp_parser_shift_expression);
5363}
5364
5365/* Parse an equality-expression.
5366
5367 equality-expression:
5368 relational-expression
5369 equality-expression == relational-expression
5370 equality-expression != relational-expression
5371
5372 Returns a representation of the expression. */
5373
5374static tree
94edc4ab 5375cp_parser_equality_expression (cp_parser* parser)
a723baf1 5376{
39b1af70 5377 static const cp_parser_token_tree_map map = {
a723baf1
MM
5378 { CPP_EQ_EQ, EQ_EXPR },
5379 { CPP_NOT_EQ, NE_EXPR },
5380 { CPP_EOF, ERROR_MARK }
5381 };
5382
5383 return cp_parser_binary_expression (parser,
5384 map,
5385 cp_parser_relational_expression);
5386}
5387
5388/* Parse an and-expression.
5389
5390 and-expression:
5391 equality-expression
5392 and-expression & equality-expression
5393
5394 Returns a representation of the expression. */
5395
5396static tree
94edc4ab 5397cp_parser_and_expression (cp_parser* parser)
a723baf1 5398{
39b1af70 5399 static const cp_parser_token_tree_map map = {
a723baf1
MM
5400 { CPP_AND, BIT_AND_EXPR },
5401 { CPP_EOF, ERROR_MARK }
5402 };
5403
5404 return cp_parser_binary_expression (parser,
5405 map,
5406 cp_parser_equality_expression);
5407}
5408
5409/* Parse an exclusive-or-expression.
5410
5411 exclusive-or-expression:
5412 and-expression
5413 exclusive-or-expression ^ and-expression
5414
5415 Returns a representation of the expression. */
5416
5417static tree
94edc4ab 5418cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 5419{
39b1af70 5420 static const cp_parser_token_tree_map map = {
a723baf1
MM
5421 { CPP_XOR, BIT_XOR_EXPR },
5422 { CPP_EOF, ERROR_MARK }
5423 };
5424
5425 return cp_parser_binary_expression (parser,
5426 map,
5427 cp_parser_and_expression);
5428}
5429
5430
5431/* Parse an inclusive-or-expression.
5432
5433 inclusive-or-expression:
5434 exclusive-or-expression
5435 inclusive-or-expression | exclusive-or-expression
5436
5437 Returns a representation of the expression. */
5438
5439static tree
94edc4ab 5440cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 5441{
39b1af70 5442 static const cp_parser_token_tree_map map = {
a723baf1
MM
5443 { CPP_OR, BIT_IOR_EXPR },
5444 { CPP_EOF, ERROR_MARK }
5445 };
5446
5447 return cp_parser_binary_expression (parser,
5448 map,
5449 cp_parser_exclusive_or_expression);
5450}
5451
5452/* Parse a logical-and-expression.
5453
5454 logical-and-expression:
5455 inclusive-or-expression
5456 logical-and-expression && inclusive-or-expression
5457
5458 Returns a representation of the expression. */
5459
5460static tree
94edc4ab 5461cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 5462{
39b1af70 5463 static const cp_parser_token_tree_map map = {
a723baf1
MM
5464 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5465 { CPP_EOF, ERROR_MARK }
5466 };
5467
5468 return cp_parser_binary_expression (parser,
5469 map,
5470 cp_parser_inclusive_or_expression);
5471}
5472
5473/* Parse a logical-or-expression.
5474
5475 logical-or-expression:
34cd5ae7 5476 logical-and-expression
a723baf1
MM
5477 logical-or-expression || logical-and-expression
5478
5479 Returns a representation of the expression. */
5480
5481static tree
94edc4ab 5482cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5483{
39b1af70 5484 static const cp_parser_token_tree_map map = {
a723baf1
MM
5485 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5486 { CPP_EOF, ERROR_MARK }
5487 };
5488
5489 return cp_parser_binary_expression (parser,
5490 map,
5491 cp_parser_logical_and_expression);
5492}
5493
a723baf1
MM
5494/* Parse the `? expression : assignment-expression' part of a
5495 conditional-expression. The LOGICAL_OR_EXPR is the
5496 logical-or-expression that started the conditional-expression.
5497 Returns a representation of the entire conditional-expression.
5498
39703eb9 5499 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5500
5501 ? expression : assignment-expression
21526606 5502
a723baf1 5503 GNU Extensions:
21526606 5504
a723baf1
MM
5505 ? : assignment-expression */
5506
5507static tree
94edc4ab 5508cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5509{
5510 tree expr;
5511 tree assignment_expr;
5512
5513 /* Consume the `?' token. */
5514 cp_lexer_consume_token (parser->lexer);
5515 if (cp_parser_allow_gnu_extensions_p (parser)
5516 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5517 /* Implicit true clause. */
5518 expr = NULL_TREE;
5519 else
5520 /* Parse the expression. */
5521 expr = cp_parser_expression (parser);
21526606 5522
a723baf1
MM
5523 /* The next token should be a `:'. */
5524 cp_parser_require (parser, CPP_COLON, "`:'");
5525 /* Parse the assignment-expression. */
5526 assignment_expr = cp_parser_assignment_expression (parser);
5527
5528 /* Build the conditional-expression. */
5529 return build_x_conditional_expr (logical_or_expr,
5530 expr,
5531 assignment_expr);
5532}
5533
5534/* Parse an assignment-expression.
5535
5536 assignment-expression:
5537 conditional-expression
5538 logical-or-expression assignment-operator assignment_expression
5539 throw-expression
5540
5541 Returns a representation for the expression. */
5542
5543static tree
94edc4ab 5544cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5545{
5546 tree expr;
5547
5548 /* If the next token is the `throw' keyword, then we're looking at
5549 a throw-expression. */
5550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5551 expr = cp_parser_throw_expression (parser);
5552 /* Otherwise, it must be that we are looking at a
5553 logical-or-expression. */
5554 else
5555 {
5556 /* Parse the logical-or-expression. */
5557 expr = cp_parser_logical_or_expression (parser);
5558 /* If the next token is a `?' then we're actually looking at a
5559 conditional-expression. */
5560 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5561 return cp_parser_question_colon_clause (parser, expr);
21526606 5562 else
a723baf1
MM
5563 {
5564 enum tree_code assignment_operator;
5565
5566 /* If it's an assignment-operator, we're using the second
5567 production. */
21526606 5568 assignment_operator
a723baf1
MM
5569 = cp_parser_assignment_operator_opt (parser);
5570 if (assignment_operator != ERROR_MARK)
5571 {
5572 tree rhs;
5573
5574 /* Parse the right-hand side of the assignment. */
5575 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5576 /* An assignment may not appear in a
5577 constant-expression. */
625cbf93
MM
5578 if (cp_parser_non_integral_constant_expression (parser,
5579 "an assignment"))
5580 return error_mark_node;
34cd5ae7 5581 /* Build the assignment expression. */
21526606
EC
5582 expr = build_x_modify_expr (expr,
5583 assignment_operator,
a723baf1
MM
5584 rhs);
5585 }
5586 }
5587 }
5588
5589 return expr;
5590}
5591
5592/* Parse an (optional) assignment-operator.
5593
21526606
EC
5594 assignment-operator: one of
5595 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5596
5597 GNU Extension:
21526606 5598
a723baf1
MM
5599 assignment-operator: one of
5600 <?= >?=
5601
5602 If the next token is an assignment operator, the corresponding tree
5603 code is returned, and the token is consumed. For example, for
5604 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5605 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5606 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5607 operator, ERROR_MARK is returned. */
5608
5609static enum tree_code
94edc4ab 5610cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5611{
5612 enum tree_code op;
5613 cp_token *token;
5614
5615 /* Peek at the next toen. */
5616 token = cp_lexer_peek_token (parser->lexer);
5617
5618 switch (token->type)
5619 {
5620 case CPP_EQ:
5621 op = NOP_EXPR;
5622 break;
5623
5624 case CPP_MULT_EQ:
5625 op = MULT_EXPR;
5626 break;
5627
5628 case CPP_DIV_EQ:
5629 op = TRUNC_DIV_EXPR;
5630 break;
5631
5632 case CPP_MOD_EQ:
5633 op = TRUNC_MOD_EXPR;
5634 break;
5635
5636 case CPP_PLUS_EQ:
5637 op = PLUS_EXPR;
5638 break;
5639
5640 case CPP_MINUS_EQ:
5641 op = MINUS_EXPR;
5642 break;
5643
5644 case CPP_RSHIFT_EQ:
5645 op = RSHIFT_EXPR;
5646 break;
5647
5648 case CPP_LSHIFT_EQ:
5649 op = LSHIFT_EXPR;
5650 break;
5651
5652 case CPP_AND_EQ:
5653 op = BIT_AND_EXPR;
5654 break;
5655
5656 case CPP_XOR_EQ:
5657 op = BIT_XOR_EXPR;
5658 break;
5659
5660 case CPP_OR_EQ:
5661 op = BIT_IOR_EXPR;
5662 break;
5663
5664 case CPP_MIN_EQ:
5665 op = MIN_EXPR;
5666 break;
5667
5668 case CPP_MAX_EQ:
5669 op = MAX_EXPR;
5670 break;
5671
21526606 5672 default:
a723baf1
MM
5673 /* Nothing else is an assignment operator. */
5674 op = ERROR_MARK;
5675 }
5676
5677 /* If it was an assignment operator, consume it. */
5678 if (op != ERROR_MARK)
5679 cp_lexer_consume_token (parser->lexer);
5680
5681 return op;
5682}
5683
5684/* Parse an expression.
5685
5686 expression:
5687 assignment-expression
5688 expression , assignment-expression
5689
5690 Returns a representation of the expression. */
5691
5692static tree
94edc4ab 5693cp_parser_expression (cp_parser* parser)
a723baf1
MM
5694{
5695 tree expression = NULL_TREE;
a723baf1
MM
5696
5697 while (true)
5698 {
5699 tree assignment_expression;
5700
5701 /* Parse the next assignment-expression. */
21526606 5702 assignment_expression
a723baf1
MM
5703 = cp_parser_assignment_expression (parser);
5704 /* If this is the first assignment-expression, we can just
5705 save it away. */
5706 if (!expression)
5707 expression = assignment_expression;
a723baf1 5708 else
d17811fd
MM
5709 expression = build_x_compound_expr (expression,
5710 assignment_expression);
a723baf1
MM
5711 /* If the next token is not a comma, then we are done with the
5712 expression. */
5713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5714 break;
5715 /* Consume the `,'. */
5716 cp_lexer_consume_token (parser->lexer);
14d22dd6 5717 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5718 if (cp_parser_non_integral_constant_expression (parser,
5719 "a comma operator"))
5720 expression = error_mark_node;
14d22dd6 5721 }
a723baf1
MM
5722
5723 return expression;
5724}
5725
21526606 5726/* Parse a constant-expression.
a723baf1
MM
5727
5728 constant-expression:
21526606 5729 conditional-expression
14d22dd6
MM
5730
5731 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5732 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5733 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5734 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5735
5736static tree
21526606 5737cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5738 bool allow_non_constant_p,
5739 bool *non_constant_p)
a723baf1 5740{
67c03833
JM
5741 bool saved_integral_constant_expression_p;
5742 bool saved_allow_non_integral_constant_expression_p;
5743 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5744 tree expression;
5745
5746 /* It might seem that we could simply parse the
5747 conditional-expression, and then check to see if it were
5748 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5749 one that the compiler can figure out is constant, possibly after
5750 doing some simplifications or optimizations. The standard has a
5751 precise definition of constant-expression, and we must honor
5752 that, even though it is somewhat more restrictive.
5753
5754 For example:
5755
5756 int i[(2, 3)];
5757
5758 is not a legal declaration, because `(2, 3)' is not a
5759 constant-expression. The `,' operator is forbidden in a
5760 constant-expression. However, GCC's constant-folding machinery
5761 will fold this operation to an INTEGER_CST for `3'. */
5762
14d22dd6 5763 /* Save the old settings. */
67c03833 5764 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5765 saved_allow_non_integral_constant_expression_p
67c03833
JM
5766 = parser->allow_non_integral_constant_expression_p;
5767 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5768 /* We are now parsing a constant-expression. */
67c03833
JM
5769 parser->integral_constant_expression_p = true;
5770 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5771 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5772 /* Although the grammar says "conditional-expression", we parse an
5773 "assignment-expression", which also permits "throw-expression"
5774 and the use of assignment operators. In the case that
5775 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5776 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5777 actually essential that we look for an assignment-expression.
5778 For example, cp_parser_initializer_clauses uses this function to
5779 determine whether a particular assignment-expression is in fact
5780 constant. */
5781 expression = cp_parser_assignment_expression (parser);
14d22dd6 5782 /* Restore the old settings. */
67c03833 5783 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
21526606 5784 parser->allow_non_integral_constant_expression_p
67c03833 5785 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5786 if (allow_non_constant_p)
67c03833
JM
5787 *non_constant_p = parser->non_integral_constant_expression_p;
5788 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5789
5790 return expression;
5791}
5792
7a3ea201
RH
5793/* Parse __builtin_offsetof.
5794
5795 offsetof-expression:
5796 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5797
5798 offsetof-member-designator:
5799 id-expression
5800 | offsetof-member-designator "." id-expression
5801 | offsetof-member-designator "[" expression "]"
5802*/
5803
5804static tree
5805cp_parser_builtin_offsetof (cp_parser *parser)
5806{
5807 int save_ice_p, save_non_ice_p;
5808 tree type, expr;
5809 cp_id_kind dummy;
5810
5811 /* We're about to accept non-integral-constant things, but will
5812 definitely yield an integral constant expression. Save and
5813 restore these values around our local parsing. */
5814 save_ice_p = parser->integral_constant_expression_p;
5815 save_non_ice_p = parser->non_integral_constant_expression_p;
5816
5817 /* Consume the "__builtin_offsetof" token. */
5818 cp_lexer_consume_token (parser->lexer);
5819 /* Consume the opening `('. */
5820 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5821 /* Parse the type-id. */
5822 type = cp_parser_type_id (parser);
5823 /* Look for the `,'. */
5824 cp_parser_require (parser, CPP_COMMA, "`,'");
5825
5826 /* Build the (type *)null that begins the traditional offsetof macro. */
5827 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5828
5829 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5830 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5831 true, &dummy);
5832 while (true)
5833 {
5834 cp_token *token = cp_lexer_peek_token (parser->lexer);
5835 switch (token->type)
5836 {
5837 case CPP_OPEN_SQUARE:
5838 /* offsetof-member-designator "[" expression "]" */
5839 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5840 break;
5841
5842 case CPP_DOT:
5843 /* offsetof-member-designator "." identifier */
5844 cp_lexer_consume_token (parser->lexer);
5845 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5846 true, &dummy);
5847 break;
5848
5849 case CPP_CLOSE_PAREN:
5850 /* Consume the ")" token. */
5851 cp_lexer_consume_token (parser->lexer);
5852 goto success;
5853
5854 default:
5855 /* Error. We know the following require will fail, but
5856 that gives the proper error message. */
5857 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5858 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5859 expr = error_mark_node;
5860 goto failure;
5861 }
5862 }
5863
5864 success:
5865 /* We've finished the parsing, now finish with the semantics. At present
5866 we're just mirroring the traditional macro implementation. Better
5867 would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5868 here rather than in build_x_unary_op. */
5869 expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5870 expr = build_x_unary_op (ADDR_EXPR, expr);
5871 expr = build_reinterpret_cast (size_type_node, expr);
5872
5873 failure:
5874 parser->integral_constant_expression_p = save_ice_p;
5875 parser->non_integral_constant_expression_p = save_non_ice_p;
5876
5877 return expr;
5878}
5879
a723baf1
MM
5880/* Statements [gram.stmt.stmt] */
5881
21526606 5882/* Parse a statement.
a723baf1
MM
5883
5884 statement:
5885 labeled-statement
5886 expression-statement
5887 compound-statement
5888 selection-statement
5889 iteration-statement
5890 jump-statement
5891 declaration-statement
5892 try-block */
5893
5894static void
325c3691 5895cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5896{
5897 tree statement;
5898 cp_token *token;
6de9cd9a 5899 location_t statement_locus;
a723baf1
MM
5900
5901 /* There is no statement yet. */
5902 statement = NULL_TREE;
5903 /* Peek at the next token. */
5904 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a
DN
5905 /* Remember the location of the first token in the statement. */
5906 statement_locus = token->location;
a723baf1
MM
5907 /* If this is a keyword, then that will often determine what kind of
5908 statement we have. */
5909 if (token->type == CPP_KEYWORD)
5910 {
5911 enum rid keyword = token->keyword;
5912
5913 switch (keyword)
5914 {
5915 case RID_CASE:
5916 case RID_DEFAULT:
a5bcc582 5917 statement = cp_parser_labeled_statement (parser,
325c3691 5918 in_statement_expr);
a723baf1
MM
5919 break;
5920
5921 case RID_IF:
5922 case RID_SWITCH:
5923 statement = cp_parser_selection_statement (parser);
5924 break;
5925
5926 case RID_WHILE:
5927 case RID_DO:
5928 case RID_FOR:
5929 statement = cp_parser_iteration_statement (parser);
5930 break;
5931
5932 case RID_BREAK:
5933 case RID_CONTINUE:
5934 case RID_RETURN:
5935 case RID_GOTO:
5936 statement = cp_parser_jump_statement (parser);
5937 break;
5938
5939 case RID_TRY:
5940 statement = cp_parser_try_block (parser);
5941 break;
5942
5943 default:
5944 /* It might be a keyword like `int' that can start a
5945 declaration-statement. */
5946 break;
5947 }
5948 }
5949 else if (token->type == CPP_NAME)
5950 {
5951 /* If the next token is a `:', then we are looking at a
5952 labeled-statement. */
5953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5954 if (token->type == CPP_COLON)
325c3691 5955 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
5956 }
5957 /* Anything that starts with a `{' must be a compound-statement. */
5958 else if (token->type == CPP_OPEN_BRACE)
325c3691 5959 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
5960
5961 /* Everything else must be a declaration-statement or an
21526606 5962 expression-statement. Try for the declaration-statement
a723baf1
MM
5963 first, unless we are looking at a `;', in which case we know that
5964 we have an expression-statement. */
5965 if (!statement)
5966 {
5967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5968 {
5969 cp_parser_parse_tentatively (parser);
5970 /* Try to parse the declaration-statement. */
5971 cp_parser_declaration_statement (parser);
5972 /* If that worked, we're done. */
5973 if (cp_parser_parse_definitely (parser))
5974 return;
5975 }
5976 /* Look for an expression-statement instead. */
325c3691 5977 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
5978 }
5979
5980 /* Set the line number for the statement. */
009ed910 5981 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6de9cd9a
DN
5982 {
5983 SET_EXPR_LOCUS (statement, NULL);
5984 annotate_with_locus (statement, statement_locus);
5985 }
a723baf1
MM
5986}
5987
5988/* Parse a labeled-statement.
5989
5990 labeled-statement:
5991 identifier : statement
5992 case constant-expression : statement
98ce043b
MM
5993 default : statement
5994
5995 GNU Extension:
21526606 5996
98ce043b
MM
5997 labeled-statement:
5998 case constant-expression ... constant-expression : statement
a723baf1 5999
8c161995
RH
6000 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6001 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
6002
6003static tree
325c3691 6004cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6005{
6006 cp_token *token;
0e59b3fb 6007 tree statement = error_mark_node;
a723baf1
MM
6008
6009 /* The next token should be an identifier. */
6010 token = cp_lexer_peek_token (parser->lexer);
6011 if (token->type != CPP_NAME
6012 && token->type != CPP_KEYWORD)
6013 {
6014 cp_parser_error (parser, "expected labeled-statement");
6015 return error_mark_node;
6016 }
6017
6018 switch (token->keyword)
6019 {
6020 case RID_CASE:
6021 {
98ce043b
MM
6022 tree expr, expr_hi;
6023 cp_token *ellipsis;
a723baf1
MM
6024
6025 /* Consume the `case' token. */
6026 cp_lexer_consume_token (parser->lexer);
6027 /* Parse the constant-expression. */
21526606 6028 expr = cp_parser_constant_expression (parser,
d17811fd 6029 /*allow_non_constant_p=*/false,
14d22dd6 6030 NULL);
98ce043b
MM
6031
6032 ellipsis = cp_lexer_peek_token (parser->lexer);
6033 if (ellipsis->type == CPP_ELLIPSIS)
6034 {
6035 /* Consume the `...' token. */
6036 cp_lexer_consume_token (parser->lexer);
6037 expr_hi =
6038 cp_parser_constant_expression (parser,
6039 /*allow_non_constant_p=*/false,
6040 NULL);
6041 /* We don't need to emit warnings here, as the common code
6042 will do this for us. */
6043 }
6044 else
6045 expr_hi = NULL_TREE;
6046
0e59b3fb
MM
6047 if (!parser->in_switch_statement_p)
6048 error ("case label `%E' not within a switch statement", expr);
6049 else
98ce043b 6050 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
6051 }
6052 break;
6053
6054 case RID_DEFAULT:
6055 /* Consume the `default' token. */
6056 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
6057 if (!parser->in_switch_statement_p)
6058 error ("case label not within a switch statement");
6059 else
6060 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
6061 break;
6062
6063 default:
6064 /* Anything else must be an ordinary label. */
6065 statement = finish_label_stmt (cp_parser_identifier (parser));
6066 break;
6067 }
6068
6069 /* Require the `:' token. */
6070 cp_parser_require (parser, CPP_COLON, "`:'");
6071 /* Parse the labeled statement. */
325c3691 6072 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6073
6074 /* Return the label, in the case of a `case' or `default' label. */
6075 return statement;
6076}
6077
6078/* Parse an expression-statement.
6079
6080 expression-statement:
6081 expression [opt] ;
6082
6083 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
6084 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6085 indicates whether this expression-statement is part of an
6086 expression statement. */
a723baf1
MM
6087
6088static tree
325c3691 6089cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 6090{
a5bcc582 6091 tree statement = NULL_TREE;
a723baf1 6092
a5bcc582 6093 /* If the next token is a ';', then there is no expression
04c06002 6094 statement. */
a723baf1 6095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582 6096 statement = cp_parser_expression (parser);
21526606 6097
a723baf1 6098 /* Consume the final `;'. */
e0860732 6099 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 6100
325c3691 6101 if (in_statement_expr
a5bcc582
NS
6102 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6103 {
6104 /* This is the final expression statement of a statement
6105 expression. */
325c3691 6106 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
6107 }
6108 else if (statement)
6109 statement = finish_expr_stmt (statement);
6110 else
6111 finish_stmt ();
21526606 6112
a723baf1
MM
6113 return statement;
6114}
6115
6116/* Parse a compound-statement.
6117
6118 compound-statement:
6119 { statement-seq [opt] }
21526606 6120
5882f0f3 6121 Returns a tree representing the statement. */
a723baf1
MM
6122
6123static tree
325c3691
RH
6124cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6125 bool in_try)
a723baf1
MM
6126{
6127 tree compound_stmt;
6128
6129 /* Consume the `{'. */
6130 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6131 return error_mark_node;
6132 /* Begin the compound-statement. */
325c3691 6133 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6134 /* Parse an (optional) statement-seq. */
325c3691 6135 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6136 /* Finish the compound-statement. */
7a3397c7 6137 finish_compound_stmt (compound_stmt);
a723baf1
MM
6138 /* Consume the `}'. */
6139 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6140
6141 return compound_stmt;
6142}
6143
6144/* Parse an (optional) statement-seq.
6145
6146 statement-seq:
6147 statement
6148 statement-seq [opt] statement */
6149
6150static void
325c3691 6151cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6152{
6153 /* Scan statements until there aren't any more. */
6154 while (true)
6155 {
6156 /* If we're looking at a `}', then we've run out of statements. */
6157 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6158 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6159 break;
6160
6161 /* Parse the statement. */
325c3691 6162 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6163 }
6164}
6165
6166/* Parse a selection-statement.
6167
6168 selection-statement:
6169 if ( condition ) statement
6170 if ( condition ) statement else statement
21526606 6171 switch ( condition ) statement
a723baf1
MM
6172
6173 Returns the new IF_STMT or SWITCH_STMT. */
6174
6175static tree
94edc4ab 6176cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6177{
6178 cp_token *token;
6179 enum rid keyword;
6180
6181 /* Peek at the next token. */
6182 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6183
6184 /* See what kind of keyword it is. */
6185 keyword = token->keyword;
6186 switch (keyword)
6187 {
6188 case RID_IF:
6189 case RID_SWITCH:
6190 {
6191 tree statement;
6192 tree condition;
6193
6194 /* Look for the `('. */
6195 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6196 {
6197 cp_parser_skip_to_end_of_statement (parser);
6198 return error_mark_node;
6199 }
6200
6201 /* Begin the selection-statement. */
6202 if (keyword == RID_IF)
6203 statement = begin_if_stmt ();
6204 else
6205 statement = begin_switch_stmt ();
6206
6207 /* Parse the condition. */
6208 condition = cp_parser_condition (parser);
6209 /* Look for the `)'. */
6210 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6211 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6212 /*consume_paren=*/true);
a723baf1
MM
6213
6214 if (keyword == RID_IF)
6215 {
a723baf1
MM
6216 /* Add the condition. */
6217 finish_if_stmt_cond (condition, statement);
6218
6219 /* Parse the then-clause. */
325c3691 6220 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6221 finish_then_clause (statement);
6222
6223 /* If the next token is `else', parse the else-clause. */
6224 if (cp_lexer_next_token_is_keyword (parser->lexer,
6225 RID_ELSE))
6226 {
a723baf1
MM
6227 /* Consume the `else' keyword. */
6228 cp_lexer_consume_token (parser->lexer);
325c3691 6229 begin_else_clause (statement);
a723baf1 6230 /* Parse the else-clause. */
325c3691 6231 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6232 finish_else_clause (statement);
6233 }
6234
6235 /* Now we're all done with the if-statement. */
325c3691 6236 finish_if_stmt (statement);
a723baf1
MM
6237 }
6238 else
6239 {
0e59b3fb 6240 bool in_switch_statement_p;
a723baf1
MM
6241
6242 /* Add the condition. */
6243 finish_switch_cond (condition, statement);
6244
6245 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6246 in_switch_statement_p = parser->in_switch_statement_p;
6247 parser->in_switch_statement_p = true;
325c3691 6248 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6249 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6250
6251 /* Now we're all done with the switch-statement. */
6252 finish_switch_stmt (statement);
6253 }
6254
6255 return statement;
6256 }
6257 break;
6258
6259 default:
6260 cp_parser_error (parser, "expected selection-statement");
6261 return error_mark_node;
6262 }
6263}
6264
21526606 6265/* Parse a condition.
a723baf1
MM
6266
6267 condition:
6268 expression
21526606 6269 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6270
6271 GNU Extension:
21526606 6272
a723baf1 6273 condition:
21526606 6274 type-specifier-seq declarator asm-specification [opt]
a723baf1 6275 attributes [opt] = assignment-expression
21526606 6276
a723baf1
MM
6277 Returns the expression that should be tested. */
6278
6279static tree
94edc4ab 6280cp_parser_condition (cp_parser* parser)
a723baf1 6281{
62d1db17 6282 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6283 const char *saved_message;
6284
6285 /* Try the declaration first. */
6286 cp_parser_parse_tentatively (parser);
6287 /* New types are not allowed in the type-specifier-seq for a
6288 condition. */
6289 saved_message = parser->type_definition_forbidden_message;
6290 parser->type_definition_forbidden_message
6291 = "types may not be defined in conditions";
6292 /* Parse the type-specifier-seq. */
62d1db17 6293 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
6294 /* Restore the saved message. */
6295 parser->type_definition_forbidden_message = saved_message;
6296 /* If all is well, we might be looking at a declaration. */
6297 if (!cp_parser_error_occurred (parser))
6298 {
6299 tree decl;
6300 tree asm_specification;
6301 tree attributes;
058b15c1 6302 cp_declarator *declarator;
a723baf1 6303 tree initializer = NULL_TREE;
21526606 6304
a723baf1 6305 /* Parse the declarator. */
62b8a44e 6306 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
6307 /*ctor_dtor_or_conv_p=*/NULL,
6308 /*parenthesized_p=*/NULL);
a723baf1
MM
6309 /* Parse the attributes. */
6310 attributes = cp_parser_attributes_opt (parser);
6311 /* Parse the asm-specification. */
6312 asm_specification = cp_parser_asm_specification_opt (parser);
6313 /* If the next token is not an `=', then we might still be
6314 looking at an expression. For example:
21526606 6315
a723baf1 6316 if (A(a).x)
21526606 6317
a723baf1
MM
6318 looks like a decl-specifier-seq and a declarator -- but then
6319 there is no `=', so this is an expression. */
6320 cp_parser_require (parser, CPP_EQ, "`='");
6321 /* If we did see an `=', then we are looking at a declaration
6322 for sure. */
6323 if (cp_parser_parse_definitely (parser))
6324 {
6325 /* Create the declaration. */
62d1db17 6326 decl = start_decl (declarator, &type_specifiers,
a723baf1
MM
6327 /*initialized_p=*/true,
6328 attributes, /*prefix_attributes=*/NULL_TREE);
6329 /* Parse the assignment-expression. */
6330 initializer = cp_parser_assignment_expression (parser);
21526606 6331
a723baf1 6332 /* Process the initializer. */
21526606
EC
6333 cp_finish_decl (decl,
6334 initializer,
6335 asm_specification,
a723baf1 6336 LOOKUP_ONLYCONVERTING);
21526606 6337
a723baf1
MM
6338 return convert_from_reference (decl);
6339 }
6340 }
6341 /* If we didn't even get past the declarator successfully, we are
6342 definitely not looking at a declaration. */
6343 else
6344 cp_parser_abort_tentative_parse (parser);
6345
6346 /* Otherwise, we are looking at an expression. */
6347 return cp_parser_expression (parser);
6348}
6349
6350/* Parse an iteration-statement.
6351
6352 iteration-statement:
6353 while ( condition ) statement
6354 do statement while ( expression ) ;
6355 for ( for-init-statement condition [opt] ; expression [opt] )
6356 statement
6357
6358 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6359
6360static tree
94edc4ab 6361cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6362{
6363 cp_token *token;
6364 enum rid keyword;
6365 tree statement;
0e59b3fb
MM
6366 bool in_iteration_statement_p;
6367
a723baf1
MM
6368
6369 /* Peek at the next token. */
6370 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6371 if (!token)
6372 return error_mark_node;
6373
0e59b3fb 6374 /* Remember whether or not we are already within an iteration
21526606 6375 statement. */
0e59b3fb
MM
6376 in_iteration_statement_p = parser->in_iteration_statement_p;
6377
a723baf1
MM
6378 /* See what kind of keyword it is. */
6379 keyword = token->keyword;
6380 switch (keyword)
6381 {
6382 case RID_WHILE:
6383 {
6384 tree condition;
6385
6386 /* Begin the while-statement. */
6387 statement = begin_while_stmt ();
6388 /* Look for the `('. */
6389 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6390 /* Parse the condition. */
6391 condition = cp_parser_condition (parser);
6392 finish_while_stmt_cond (condition, statement);
6393 /* Look for the `)'. */
6394 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6395 /* Parse the dependent statement. */
0e59b3fb 6396 parser->in_iteration_statement_p = true;
a723baf1 6397 cp_parser_already_scoped_statement (parser);
0e59b3fb 6398 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6399 /* We're done with the while-statement. */
6400 finish_while_stmt (statement);
6401 }
6402 break;
6403
6404 case RID_DO:
6405 {
6406 tree expression;
6407
6408 /* Begin the do-statement. */
6409 statement = begin_do_stmt ();
6410 /* Parse the body of the do-statement. */
0e59b3fb 6411 parser->in_iteration_statement_p = true;
a723baf1 6412 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6413 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6414 finish_do_body (statement);
6415 /* Look for the `while' keyword. */
6416 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6417 /* Look for the `('. */
6418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419 /* Parse the expression. */
6420 expression = cp_parser_expression (parser);
6421 /* We're done with the do-statement. */
6422 finish_do_stmt (expression, statement);
6423 /* Look for the `)'. */
6424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6425 /* Look for the `;'. */
6426 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6427 }
6428 break;
6429
6430 case RID_FOR:
6431 {
6432 tree condition = NULL_TREE;
6433 tree expression = NULL_TREE;
6434
6435 /* Begin the for-statement. */
6436 statement = begin_for_stmt ();
6437 /* Look for the `('. */
6438 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6439 /* Parse the initialization. */
6440 cp_parser_for_init_statement (parser);
6441 finish_for_init_stmt (statement);
6442
6443 /* If there's a condition, process it. */
6444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6445 condition = cp_parser_condition (parser);
6446 finish_for_cond (condition, statement);
6447 /* Look for the `;'. */
6448 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6449
6450 /* If there's an expression, process it. */
6451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6452 expression = cp_parser_expression (parser);
6453 finish_for_expr (expression, statement);
6454 /* Look for the `)'. */
d5a10cf0
MM
6455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6456
a723baf1 6457 /* Parse the body of the for-statement. */
0e59b3fb 6458 parser->in_iteration_statement_p = true;
a723baf1 6459 cp_parser_already_scoped_statement (parser);
0e59b3fb 6460 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6461
6462 /* We're done with the for-statement. */
6463 finish_for_stmt (statement);
6464 }
6465 break;
6466
6467 default:
6468 cp_parser_error (parser, "expected iteration-statement");
6469 statement = error_mark_node;
6470 break;
6471 }
6472
6473 return statement;
6474}
6475
6476/* Parse a for-init-statement.
6477
6478 for-init-statement:
6479 expression-statement
6480 simple-declaration */
6481
6482static void
94edc4ab 6483cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6484{
6485 /* If the next token is a `;', then we have an empty
34cd5ae7 6486 expression-statement. Grammatically, this is also a
a723baf1
MM
6487 simple-declaration, but an invalid one, because it does not
6488 declare anything. Therefore, if we did not handle this case
6489 specially, we would issue an error message about an invalid
6490 declaration. */
6491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6492 {
6493 /* We're going to speculatively look for a declaration, falling back
6494 to an expression, if necessary. */
6495 cp_parser_parse_tentatively (parser);
6496 /* Parse the declaration. */
6497 cp_parser_simple_declaration (parser,
6498 /*function_definition_allowed_p=*/false);
6499 /* If the tentative parse failed, then we shall need to look for an
6500 expression-statement. */
6501 if (cp_parser_parse_definitely (parser))
6502 return;
6503 }
6504
a5bcc582 6505 cp_parser_expression_statement (parser, false);
a723baf1
MM
6506}
6507
6508/* Parse a jump-statement.
6509
6510 jump-statement:
6511 break ;
6512 continue ;
6513 return expression [opt] ;
21526606 6514 goto identifier ;
a723baf1
MM
6515
6516 GNU extension:
6517
6518 jump-statement:
6519 goto * expression ;
6520
5088b058 6521 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6522
6523static tree
94edc4ab 6524cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6525{
6526 tree statement = error_mark_node;
6527 cp_token *token;
6528 enum rid keyword;
6529
6530 /* Peek at the next token. */
6531 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6532 if (!token)
6533 return error_mark_node;
6534
6535 /* See what kind of keyword it is. */
6536 keyword = token->keyword;
6537 switch (keyword)
6538 {
6539 case RID_BREAK:
0e59b3fb
MM
6540 if (!parser->in_switch_statement_p
6541 && !parser->in_iteration_statement_p)
6542 {
6543 error ("break statement not within loop or switch");
6544 statement = error_mark_node;
6545 }
6546 else
6547 statement = finish_break_stmt ();
a723baf1
MM
6548 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6549 break;
6550
6551 case RID_CONTINUE:
0e59b3fb
MM
6552 if (!parser->in_iteration_statement_p)
6553 {
6554 error ("continue statement not within a loop");
6555 statement = error_mark_node;
6556 }
6557 else
6558 statement = finish_continue_stmt ();
a723baf1
MM
6559 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6560 break;
6561
6562 case RID_RETURN:
6563 {
6564 tree expr;
6565
21526606 6566 /* If the next token is a `;', then there is no
a723baf1
MM
6567 expression. */
6568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6569 expr = cp_parser_expression (parser);
6570 else
6571 expr = NULL_TREE;
6572 /* Build the return-statement. */
6573 statement = finish_return_stmt (expr);
6574 /* Look for the final `;'. */
6575 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6576 }
6577 break;
6578
6579 case RID_GOTO:
6580 /* Create the goto-statement. */
6581 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6582 {
6583 /* Issue a warning about this use of a GNU extension. */
6584 if (pedantic)
6585 pedwarn ("ISO C++ forbids computed gotos");
6586 /* Consume the '*' token. */
6587 cp_lexer_consume_token (parser->lexer);
6588 /* Parse the dependent expression. */
6589 finish_goto_stmt (cp_parser_expression (parser));
6590 }
6591 else
6592 finish_goto_stmt (cp_parser_identifier (parser));
6593 /* Look for the final `;'. */
6594 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6595 break;
6596
6597 default:
6598 cp_parser_error (parser, "expected jump-statement");
6599 break;
6600 }
6601
6602 return statement;
6603}
6604
6605/* Parse a declaration-statement.
6606
6607 declaration-statement:
6608 block-declaration */
6609
6610static void
94edc4ab 6611cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6612{
058b15c1
MM
6613 void *p;
6614
6615 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6616 p = obstack_alloc (&declarator_obstack, 0);
6617
6618 /* Parse the block-declaration. */
a723baf1
MM
6619 cp_parser_block_declaration (parser, /*statement_p=*/true);
6620
058b15c1
MM
6621 /* Free any declarators allocated. */
6622 obstack_free (&declarator_obstack, p);
6623
a723baf1
MM
6624 /* Finish off the statement. */
6625 finish_stmt ();
6626}
6627
6628/* Some dependent statements (like `if (cond) statement'), are
6629 implicitly in their own scope. In other words, if the statement is
6630 a single statement (as opposed to a compound-statement), it is
6631 none-the-less treated as if it were enclosed in braces. Any
6632 declarations appearing in the dependent statement are out of scope
6633 after control passes that point. This function parses a statement,
6634 but ensures that is in its own scope, even if it is not a
21526606 6635 compound-statement.
a723baf1
MM
6636
6637 Returns the new statement. */
6638
6639static tree
94edc4ab 6640cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6641{
6642 tree statement;
6643
6644 /* If the token is not a `{', then we must take special action. */
6645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6646 {
6647 /* Create a compound-statement. */
325c3691 6648 statement = begin_compound_stmt (0);
a723baf1 6649 /* Parse the dependent-statement. */
a5bcc582 6650 cp_parser_statement (parser, false);
a723baf1 6651 /* Finish the dummy compound-statement. */
7a3397c7 6652 finish_compound_stmt (statement);
a723baf1
MM
6653 }
6654 /* Otherwise, we simply parse the statement directly. */
6655 else
325c3691 6656 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6657
6658 /* Return the statement. */
6659 return statement;
6660}
6661
6662/* For some dependent statements (like `while (cond) statement'), we
6663 have already created a scope. Therefore, even if the dependent
6664 statement is a compound-statement, we do not want to create another
6665 scope. */
6666
6667static void
94edc4ab 6668cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6669{
325c3691
RH
6670 /* If the token is a `{', then we must take special action. */
6671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6672 cp_parser_statement (parser, false);
6673 else
a723baf1 6674 {
325c3691
RH
6675 /* Avoid calling cp_parser_compound_statement, so that we
6676 don't create a new scope. Do everything else by hand. */
6677 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6678 cp_parser_statement_seq_opt (parser, false);
6679 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6680 }
a723baf1
MM
6681}
6682
6683/* Declarations [gram.dcl.dcl] */
6684
6685/* Parse an optional declaration-sequence.
6686
6687 declaration-seq:
6688 declaration
6689 declaration-seq declaration */
6690
6691static void
94edc4ab 6692cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6693{
6694 while (true)
6695 {
6696 cp_token *token;
6697
6698 token = cp_lexer_peek_token (parser->lexer);
6699
6700 if (token->type == CPP_CLOSE_BRACE
6701 || token->type == CPP_EOF)
6702 break;
6703
21526606 6704 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6705 {
6706 /* A declaration consisting of a single semicolon is
6707 invalid. Allow it unless we're being pedantic. */
499b568f 6708 if (pedantic && !in_system_header)
a723baf1
MM
6709 pedwarn ("extra `;'");
6710 cp_lexer_consume_token (parser->lexer);
6711 continue;
6712 }
6713
c838d82f 6714 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
34cd5ae7 6715 parser to enter or exit implicit `extern "C"' blocks. */
c838d82f
MM
6716 while (pending_lang_change > 0)
6717 {
6718 push_lang_context (lang_name_c);
6719 --pending_lang_change;
6720 }
6721 while (pending_lang_change < 0)
6722 {
6723 pop_lang_context ();
6724 ++pending_lang_change;
6725 }
6726
6727 /* Parse the declaration itself. */
a723baf1
MM
6728 cp_parser_declaration (parser);
6729 }
6730}
6731
6732/* Parse a declaration.
6733
6734 declaration:
6735 block-declaration
6736 function-definition
6737 template-declaration
6738 explicit-instantiation
6739 explicit-specialization
6740 linkage-specification
21526606 6741 namespace-definition
1092805d
MM
6742
6743 GNU extension:
6744
6745 declaration:
6746 __extension__ declaration */
a723baf1
MM
6747
6748static void
94edc4ab 6749cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6750{
6751 cp_token token1;
6752 cp_token token2;
1092805d 6753 int saved_pedantic;
058b15c1 6754 void *p;
1092805d 6755
21526606
EC
6756 /* Set this here since we can be called after
6757 pushing the linkage specification. */
0173bb6f 6758 c_lex_string_translate = 1;
21526606 6759
1092805d
MM
6760 /* Check for the `__extension__' keyword. */
6761 if (cp_parser_extension_opt (parser, &saved_pedantic))
6762 {
6763 /* Parse the qualified declaration. */
6764 cp_parser_declaration (parser);
6765 /* Restore the PEDANTIC flag. */
6766 pedantic = saved_pedantic;
6767
6768 return;
6769 }
a723baf1
MM
6770
6771 /* Try to figure out what kind of declaration is present. */
6772 token1 = *cp_lexer_peek_token (parser->lexer);
21526606
EC
6773
6774 /* Don't translate the CPP_STRING in extern "C". */
6775 if (token1.keyword == RID_EXTERN)
0173bb6f 6776 c_lex_string_translate = 0;
21526606 6777
a723baf1
MM
6778 if (token1.type != CPP_EOF)
6779 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6780
0173bb6f 6781 c_lex_string_translate = 1;
77a705e4 6782
058b15c1
MM
6783 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6784 p = obstack_alloc (&declarator_obstack, 0);
6785
a723baf1
MM
6786 /* If the next token is `extern' and the following token is a string
6787 literal, then we have a linkage specification. */
6788 if (token1.keyword == RID_EXTERN
6789 && cp_parser_is_string_literal (&token2))
6790 cp_parser_linkage_specification (parser);
6791 /* If the next token is `template', then we have either a template
6792 declaration, an explicit instantiation, or an explicit
6793 specialization. */
6794 else if (token1.keyword == RID_TEMPLATE)
6795 {
6796 /* `template <>' indicates a template specialization. */
6797 if (token2.type == CPP_LESS
6798 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6799 cp_parser_explicit_specialization (parser);
6800 /* `template <' indicates a template declaration. */
6801 else if (token2.type == CPP_LESS)
6802 cp_parser_template_declaration (parser, /*member_p=*/false);
6803 /* Anything else must be an explicit instantiation. */
6804 else
6805 cp_parser_explicit_instantiation (parser);
6806 }
6807 /* If the next token is `export', then we have a template
6808 declaration. */
6809 else if (token1.keyword == RID_EXPORT)
6810 cp_parser_template_declaration (parser, /*member_p=*/false);
6811 /* If the next token is `extern', 'static' or 'inline' and the one
6812 after that is `template', we have a GNU extended explicit
6813 instantiation directive. */
6814 else if (cp_parser_allow_gnu_extensions_p (parser)
6815 && (token1.keyword == RID_EXTERN
6816 || token1.keyword == RID_STATIC
6817 || token1.keyword == RID_INLINE)
6818 && token2.keyword == RID_TEMPLATE)
6819 cp_parser_explicit_instantiation (parser);
6820 /* If the next token is `namespace', check for a named or unnamed
6821 namespace definition. */
6822 else if (token1.keyword == RID_NAMESPACE
6823 && (/* A named namespace definition. */
6824 (token2.type == CPP_NAME
21526606 6825 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6826 == CPP_OPEN_BRACE))
6827 /* An unnamed namespace definition. */
6828 || token2.type == CPP_OPEN_BRACE))
6829 cp_parser_namespace_definition (parser);
6830 /* We must have either a block declaration or a function
6831 definition. */
6832 else
6833 /* Try to parse a block-declaration, or a function-definition. */
6834 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6835
6836 /* Free any declarators allocated. */
6837 obstack_free (&declarator_obstack, p);
a723baf1
MM
6838}
6839
21526606 6840/* Parse a block-declaration.
a723baf1
MM
6841
6842 block-declaration:
6843 simple-declaration
6844 asm-definition
6845 namespace-alias-definition
6846 using-declaration
21526606 6847 using-directive
a723baf1
MM
6848
6849 GNU Extension:
6850
6851 block-declaration:
21526606 6852 __extension__ block-declaration
a723baf1
MM
6853 label-declaration
6854
34cd5ae7 6855 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6856 part of a declaration-statement. */
6857
6858static void
21526606 6859cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6860 bool statement_p)
6861{
6862 cp_token *token1;
6863 int saved_pedantic;
6864
6865 /* Check for the `__extension__' keyword. */
6866 if (cp_parser_extension_opt (parser, &saved_pedantic))
6867 {
6868 /* Parse the qualified declaration. */
6869 cp_parser_block_declaration (parser, statement_p);
6870 /* Restore the PEDANTIC flag. */
6871 pedantic = saved_pedantic;
6872
6873 return;
6874 }
6875
6876 /* Peek at the next token to figure out which kind of declaration is
6877 present. */
6878 token1 = cp_lexer_peek_token (parser->lexer);
6879
6880 /* If the next keyword is `asm', we have an asm-definition. */
6881 if (token1->keyword == RID_ASM)
6882 {
6883 if (statement_p)
6884 cp_parser_commit_to_tentative_parse (parser);
6885 cp_parser_asm_definition (parser);
6886 }
6887 /* If the next keyword is `namespace', we have a
6888 namespace-alias-definition. */
6889 else if (token1->keyword == RID_NAMESPACE)
6890 cp_parser_namespace_alias_definition (parser);
6891 /* If the next keyword is `using', we have either a
6892 using-declaration or a using-directive. */
6893 else if (token1->keyword == RID_USING)
6894 {
6895 cp_token *token2;
6896
6897 if (statement_p)
6898 cp_parser_commit_to_tentative_parse (parser);
6899 /* If the token after `using' is `namespace', then we have a
6900 using-directive. */
6901 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6902 if (token2->keyword == RID_NAMESPACE)
6903 cp_parser_using_directive (parser);
6904 /* Otherwise, it's a using-declaration. */
6905 else
6906 cp_parser_using_declaration (parser);
6907 }
6908 /* If the next keyword is `__label__' we have a label declaration. */
6909 else if (token1->keyword == RID_LABEL)
6910 {
6911 if (statement_p)
6912 cp_parser_commit_to_tentative_parse (parser);
6913 cp_parser_label_declaration (parser);
6914 }
6915 /* Anything else must be a simple-declaration. */
6916 else
6917 cp_parser_simple_declaration (parser, !statement_p);
6918}
6919
6920/* Parse a simple-declaration.
6921
6922 simple-declaration:
21526606 6923 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6924
6925 init-declarator-list:
6926 init-declarator
21526606 6927 init-declarator-list , init-declarator
a723baf1 6928
34cd5ae7 6929 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6930 function-definition as a simple-declaration. */
a723baf1
MM
6931
6932static void
21526606 6933cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6934 bool function_definition_allowed_p)
a723baf1 6935{
62d1db17 6936 cp_decl_specifier_seq decl_specifiers;
560ad596 6937 int declares_class_or_enum;
a723baf1
MM
6938 bool saw_declarator;
6939
6940 /* Defer access checks until we know what is being declared; the
6941 checks for names appearing in the decl-specifier-seq should be
6942 done as if we were in the scope of the thing being declared. */
8d241e0b 6943 push_deferring_access_checks (dk_deferred);
cf22909c 6944
a723baf1
MM
6945 /* Parse the decl-specifier-seq. We have to keep track of whether
6946 or not the decl-specifier-seq declares a named class or
6947 enumeration type, since that is the only case in which the
21526606 6948 init-declarator-list is allowed to be empty.
a723baf1
MM
6949
6950 [dcl.dcl]
6951
6952 In a simple-declaration, the optional init-declarator-list can be
6953 omitted only when declaring a class or enumeration, that is when
6954 the decl-specifier-seq contains either a class-specifier, an
6955 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
6956 cp_parser_decl_specifier_seq (parser,
6957 CP_PARSER_FLAGS_OPTIONAL,
6958 &decl_specifiers,
6959 &declares_class_or_enum);
a723baf1 6960 /* We no longer need to defer access checks. */
cf22909c 6961 stop_deferring_access_checks ();
24c0ef37 6962
39703eb9
MM
6963 /* In a block scope, a valid declaration must always have a
6964 decl-specifier-seq. By not trying to parse declarators, we can
6965 resolve the declaration/expression ambiguity more quickly. */
62d1db17
MM
6966 if (!function_definition_allowed_p
6967 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
6968 {
6969 cp_parser_error (parser, "expected declaration");
6970 goto done;
6971 }
6972
8fbc5ae7
MM
6973 /* If the next two tokens are both identifiers, the code is
6974 erroneous. The usual cause of this situation is code like:
6975
6976 T t;
6977
6978 where "T" should name a type -- but does not. */
2097b5f2 6979 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6980 {
8d241e0b 6981 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6982 looking at a declaration. */
6983 cp_parser_commit_to_tentative_parse (parser);
6984 /* Give up. */
39703eb9 6985 goto done;
8fbc5ae7
MM
6986 }
6987
a723baf1
MM
6988 /* Keep going until we hit the `;' at the end of the simple
6989 declaration. */
6990 saw_declarator = false;
21526606 6991 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6992 CPP_SEMICOLON))
6993 {
6994 cp_token *token;
6995 bool function_definition_p;
560ad596 6996 tree decl;
a723baf1
MM
6997
6998 saw_declarator = true;
6999 /* Parse the init-declarator. */
62d1db17 7000 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
7001 function_definition_allowed_p,
7002 /*member_p=*/false,
7003 declares_class_or_enum,
7004 &function_definition_p);
1fb3244a
MM
7005 /* If an error occurred while parsing tentatively, exit quickly.
7006 (That usually happens when in the body of a function; each
7007 statement is treated as a declaration-statement until proven
7008 otherwise.) */
7009 if (cp_parser_error_occurred (parser))
39703eb9 7010 goto done;
a723baf1
MM
7011 /* Handle function definitions specially. */
7012 if (function_definition_p)
7013 {
7014 /* If the next token is a `,', then we are probably
7015 processing something like:
7016
7017 void f() {}, *p;
7018
7019 which is erroneous. */
7020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7021 error ("mixing declarations and function-definitions is forbidden");
7022 /* Otherwise, we're done with the list of declarators. */
7023 else
24c0ef37 7024 {
cf22909c 7025 pop_deferring_access_checks ();
24c0ef37
GS
7026 return;
7027 }
a723baf1
MM
7028 }
7029 /* The next token should be either a `,' or a `;'. */
7030 token = cp_lexer_peek_token (parser->lexer);
7031 /* If it's a `,', there are more declarators to come. */
7032 if (token->type == CPP_COMMA)
7033 cp_lexer_consume_token (parser->lexer);
7034 /* If it's a `;', we are done. */
7035 else if (token->type == CPP_SEMICOLON)
7036 break;
7037 /* Anything else is an error. */
7038 else
7039 {
7040 cp_parser_error (parser, "expected `,' or `;'");
7041 /* Skip tokens until we reach the end of the statement. */
7042 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
7043 /* If the next token is now a `;', consume it. */
7044 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7045 cp_lexer_consume_token (parser->lexer);
39703eb9 7046 goto done;
a723baf1
MM
7047 }
7048 /* After the first time around, a function-definition is not
7049 allowed -- even if it was OK at first. For example:
7050
7051 int i, f() {}
7052
7053 is not valid. */
7054 function_definition_allowed_p = false;
7055 }
7056
7057 /* Issue an error message if no declarators are present, and the
7058 decl-specifier-seq does not itself declare a class or
7059 enumeration. */
7060 if (!saw_declarator)
7061 {
7062 if (cp_parser_declares_only_class_p (parser))
62d1db17 7063 shadow_tag (&decl_specifiers);
a723baf1 7064 /* Perform any deferred access checks. */
cf22909c 7065 perform_deferred_access_checks ();
a723baf1
MM
7066 }
7067
7068 /* Consume the `;'. */
7069 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7070
39703eb9
MM
7071 done:
7072 pop_deferring_access_checks ();
a723baf1
MM
7073}
7074
7075/* Parse a decl-specifier-seq.
7076
7077 decl-specifier-seq:
7078 decl-specifier-seq [opt] decl-specifier
7079
7080 decl-specifier:
7081 storage-class-specifier
7082 type-specifier
7083 function-specifier
7084 friend
21526606 7085 typedef
a723baf1
MM
7086
7087 GNU Extension:
7088
15077df5
MM
7089 decl-specifier:
7090 attributes
a723baf1 7091
62d1db17 7092 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1
MM
7093
7094 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7095 appears, and the entity that will be a friend is not going to be a
7096 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
7097 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
21526606 7098 friendship is granted might not be a class.
560ad596
MM
7099
7100 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7101 flags:
560ad596
MM
7102
7103 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 7104 (i.e., a type declaration)
560ad596 7105 2: one of the decl-specifiers is an enum-specifier or a
543ca912 7106 class-specifier (i.e., a type definition)
62d1db17 7107
560ad596 7108 */
a723baf1 7109
62d1db17 7110static void
21526606 7111cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7112 cp_parser_flags flags,
7113 cp_decl_specifier_seq *decl_specs,
560ad596 7114 int* declares_class_or_enum)
a723baf1 7115{
f2ce60b8 7116 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7117
62d1db17
MM
7118 /* Clear DECL_SPECS. */
7119 clear_decl_specs (decl_specs);
7120
a723baf1 7121 /* Assume no class or enumeration type is declared. */
560ad596 7122 *declares_class_or_enum = 0;
a723baf1 7123
a723baf1
MM
7124 /* Keep reading specifiers until there are no more to read. */
7125 while (true)
7126 {
a723baf1 7127 bool constructor_p;
62d1db17 7128 bool found_decl_spec;
a723baf1
MM
7129 cp_token *token;
7130
7131 /* Peek at the next token. */
7132 token = cp_lexer_peek_token (parser->lexer);
7133 /* Handle attributes. */
7134 if (token->keyword == RID_ATTRIBUTE)
7135 {
7136 /* Parse the attributes. */
62d1db17
MM
7137 decl_specs->attributes
7138 = chainon (decl_specs->attributes,
7139 cp_parser_attributes_opt (parser));
a723baf1
MM
7140 continue;
7141 }
62d1db17
MM
7142 /* Assume we will find a decl-specifier keyword. */
7143 found_decl_spec = true;
a723baf1
MM
7144 /* If the next token is an appropriate keyword, we can simply
7145 add it to the list. */
7146 switch (token->keyword)
7147 {
a723baf1
MM
7148 /* decl-specifier:
7149 friend */
62d1db17
MM
7150 case RID_FRIEND:
7151 if (decl_specs->specs[(int) ds_friend]++)
1918facf 7152 error ("duplicate `friend'");
a723baf1
MM
7153 /* Consume the token. */
7154 cp_lexer_consume_token (parser->lexer);
7155 break;
7156
7157 /* function-specifier:
7158 inline
7159 virtual
7160 explicit */
7161 case RID_INLINE:
7162 case RID_VIRTUAL:
7163 case RID_EXPLICIT:
62d1db17 7164 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7165 break;
21526606 7166
a723baf1
MM
7167 /* decl-specifier:
7168 typedef */
7169 case RID_TYPEDEF:
62d1db17 7170 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7171 /* Consume the token. */
7172 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7173 /* A constructor declarator cannot appear in a typedef. */
7174 constructor_possible_p = false;
c006d942
MM
7175 /* The "typedef" keyword can only occur in a declaration; we
7176 may as well commit at this point. */
7177 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7178 break;
7179
7180 /* storage-class-specifier:
7181 auto
7182 register
7183 static
7184 extern
21526606 7185 mutable
a723baf1
MM
7186
7187 GNU Extension:
7188 thread */
7189 case RID_AUTO:
62d1db17
MM
7190 /* Consume the token. */
7191 cp_lexer_consume_token (parser->lexer);
7192 cp_parser_set_storage_class (decl_specs, sc_auto);
7193 break;
a723baf1 7194 case RID_REGISTER:
62d1db17
MM
7195 /* Consume the token. */
7196 cp_lexer_consume_token (parser->lexer);
7197 cp_parser_set_storage_class (decl_specs, sc_register);
7198 break;
a723baf1 7199 case RID_STATIC:
62d1db17
MM
7200 /* Consume the token. */
7201 cp_lexer_consume_token (parser->lexer);
7202 if (decl_specs->specs[(int) ds_thread])
7203 error ("`__thread' before `static'");
7204 else
7205 cp_parser_set_storage_class (decl_specs, sc_static);
7206 break;
a723baf1 7207 case RID_EXTERN:
62d1db17
MM
7208 /* Consume the token. */
7209 cp_lexer_consume_token (parser->lexer);
7210 if (decl_specs->specs[(int) ds_thread])
7211 error ("`__thread' before `extern'");
7212 else
7213 cp_parser_set_storage_class (decl_specs, sc_extern);
7214 break;
a723baf1 7215 case RID_MUTABLE:
62d1db17
MM
7216 /* Consume the token. */
7217 cp_lexer_consume_token (parser->lexer);
7218 cp_parser_set_storage_class (decl_specs, sc_mutable);
7219 break;
a723baf1 7220 case RID_THREAD:
62d1db17
MM
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser->lexer);
7223 ++decl_specs->specs[(int) ds_thread];
a723baf1 7224 break;
21526606 7225
a723baf1 7226 default:
62d1db17
MM
7227 /* We did not yet find a decl-specifier yet. */
7228 found_decl_spec = false;
a723baf1
MM
7229 break;
7230 }
7231
7232 /* Constructors are a special case. The `S' in `S()' is not a
7233 decl-specifier; it is the beginning of the declarator. */
62d1db17
MM
7234 constructor_p
7235 = (!found_decl_spec
7236 && constructor_possible_p
7237 && (cp_parser_constructor_declarator_p
7238 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7239
7240 /* If we don't have a DECL_SPEC yet, then we must be looking at
7241 a type-specifier. */
62d1db17 7242 if (!found_decl_spec && !constructor_p)
a723baf1 7243 {
560ad596 7244 int decl_spec_declares_class_or_enum;
a723baf1 7245 bool is_cv_qualifier;
62d1db17 7246 tree type_spec;
a723baf1 7247
62d1db17 7248 type_spec
a723baf1 7249 = cp_parser_type_specifier (parser, flags,
62d1db17 7250 decl_specs,
a723baf1
MM
7251 /*is_declaration=*/true,
7252 &decl_spec_declares_class_or_enum,
7253 &is_cv_qualifier);
7254
7255 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7256
7257 /* If this type-specifier referenced a user-defined type
7258 (a typedef, class-name, etc.), then we can't allow any
7259 more such type-specifiers henceforth.
7260
7261 [dcl.spec]
7262
7263 The longest sequence of decl-specifiers that could
7264 possibly be a type name is taken as the
7265 decl-specifier-seq of a declaration. The sequence shall
7266 be self-consistent as described below.
7267
7268 [dcl.type]
7269
7270 As a general rule, at most one type-specifier is allowed
7271 in the complete decl-specifier-seq of a declaration. The
7272 only exceptions are the following:
7273
7274 -- const or volatile can be combined with any other
21526606 7275 type-specifier.
a723baf1
MM
7276
7277 -- signed or unsigned can be combined with char, long,
7278 short, or int.
7279
7280 -- ..
7281
7282 Example:
7283
7284 typedef char* Pc;
7285 void g (const int Pc);
7286
7287 Here, Pc is *not* part of the decl-specifier seq; it's
7288 the declarator. Therefore, once we see a type-specifier
7289 (other than a cv-qualifier), we forbid any additional
7290 user-defined types. We *do* still allow things like `int
7291 int' to be considered a decl-specifier-seq, and issue the
7292 error message later. */
62d1db17 7293 if (type_spec && !is_cv_qualifier)
a723baf1 7294 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7295 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7296 if (type_spec)
a723baf1 7297 {
62d1db17
MM
7298 constructor_possible_p = false;
7299 found_decl_spec = true;
a723baf1 7300 }
a723baf1
MM
7301 }
7302
62d1db17
MM
7303 /* If we still do not have a DECL_SPEC, then there are no more
7304 decl-specifiers. */
7305 if (!found_decl_spec)
7306 break;
a723baf1 7307
62d1db17 7308 decl_specs->any_specifiers_p = true;
a723baf1
MM
7309 /* After we see one decl-specifier, further decl-specifiers are
7310 always optional. */
7311 flags |= CP_PARSER_FLAGS_OPTIONAL;
7312 }
7313
0426c4ca 7314 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7315 if (decl_specs->specs[(int) ds_friend] != 0
7316 && (*declares_class_or_enum & 2))
0426c4ca 7317 error ("class definition may not be declared a friend");
a723baf1
MM
7318}
7319
21526606 7320/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7321
7322 storage-class-specifier:
7323 auto
7324 register
7325 static
7326 extern
21526606 7327 mutable
a723baf1
MM
7328
7329 GNU Extension:
7330
7331 storage-class-specifier:
7332 thread
7333
7334 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7335
a723baf1 7336static tree
94edc4ab 7337cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7338{
7339 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7340 {
7341 case RID_AUTO:
7342 case RID_REGISTER:
7343 case RID_STATIC:
7344 case RID_EXTERN:
7345 case RID_MUTABLE:
7346 case RID_THREAD:
7347 /* Consume the token. */
7348 return cp_lexer_consume_token (parser->lexer)->value;
7349
7350 default:
7351 return NULL_TREE;
7352 }
7353}
7354
21526606 7355/* Parse an (optional) function-specifier.
a723baf1
MM
7356
7357 function-specifier:
7358 inline
7359 virtual
7360 explicit
7361
62d1db17
MM
7362 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7363 Updates DECL_SPECS, if it is non-NULL. */
21526606 7364
a723baf1 7365static tree
62d1db17
MM
7366cp_parser_function_specifier_opt (cp_parser* parser,
7367 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7368{
7369 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7370 {
7371 case RID_INLINE:
62d1db17
MM
7372 if (decl_specs)
7373 ++decl_specs->specs[(int) ds_inline];
7374 break;
7375
a723baf1 7376 case RID_VIRTUAL:
62d1db17
MM
7377 if (decl_specs)
7378 ++decl_specs->specs[(int) ds_virtual];
7379 break;
7380
a723baf1 7381 case RID_EXPLICIT:
62d1db17
MM
7382 if (decl_specs)
7383 ++decl_specs->specs[(int) ds_explicit];
7384 break;
a723baf1
MM
7385
7386 default:
7387 return NULL_TREE;
7388 }
62d1db17
MM
7389
7390 /* Consume the token. */
7391 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7392}
7393
7394/* Parse a linkage-specification.
7395
7396 linkage-specification:
7397 extern string-literal { declaration-seq [opt] }
7398 extern string-literal declaration */
7399
7400static void
94edc4ab 7401cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
7402{
7403 cp_token *token;
7404 tree linkage;
7405
7406 /* Look for the `extern' keyword. */
7407 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7408
7409 /* Peek at the next token. */
7410 token = cp_lexer_peek_token (parser->lexer);
7411 /* If it's not a string-literal, then there's a problem. */
7412 if (!cp_parser_is_string_literal (token))
7413 {
7414 cp_parser_error (parser, "expected language-name");
7415 return;
7416 }
7417 /* Consume the token. */
7418 cp_lexer_consume_token (parser->lexer);
7419
7420 /* Transform the literal into an identifier. If the literal is a
7421 wide-character string, or contains embedded NULs, then we can't
7422 handle it as the user wants. */
7423 if (token->type == CPP_WSTRING
7424 || (strlen (TREE_STRING_POINTER (token->value))
7425 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7426 {
7427 cp_parser_error (parser, "invalid linkage-specification");
7428 /* Assume C++ linkage. */
7429 linkage = get_identifier ("c++");
7430 }
0173bb6f
AO
7431 /* If the string is chained to another string, take the latter,
7432 that's the untranslated string. */
7433 else if (TREE_CHAIN (token->value))
7434 linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
a723baf1
MM
7435 /* If it's a simple string constant, things are easier. */
7436 else
7437 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7438
7439 /* We're now using the new linkage. */
7440 push_lang_context (linkage);
7441
7442 /* If the next token is a `{', then we're using the first
7443 production. */
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7445 {
7446 /* Consume the `{' token. */
7447 cp_lexer_consume_token (parser->lexer);
7448 /* Parse the declarations. */
7449 cp_parser_declaration_seq_opt (parser);
7450 /* Look for the closing `}'. */
7451 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7452 }
7453 /* Otherwise, there's just one declaration. */
7454 else
7455 {
7456 bool saved_in_unbraced_linkage_specification_p;
7457
21526606 7458 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7459 = parser->in_unbraced_linkage_specification_p;
7460 parser->in_unbraced_linkage_specification_p = true;
7461 have_extern_spec = true;
7462 cp_parser_declaration (parser);
7463 have_extern_spec = false;
21526606 7464 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7465 = saved_in_unbraced_linkage_specification_p;
7466 }
7467
7468 /* We're done with the linkage-specification. */
7469 pop_lang_context ();
7470}
7471
7472/* Special member functions [gram.special] */
7473
7474/* Parse a conversion-function-id.
7475
7476 conversion-function-id:
21526606 7477 operator conversion-type-id
a723baf1
MM
7478
7479 Returns an IDENTIFIER_NODE representing the operator. */
7480
21526606 7481static tree
94edc4ab 7482cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7483{
7484 tree type;
7485 tree saved_scope;
7486 tree saved_qualifying_scope;
7487 tree saved_object_scope;
91b004e5 7488 bool pop_p = false;
a723baf1
MM
7489
7490 /* Look for the `operator' token. */
7491 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7492 return error_mark_node;
7493 /* When we parse the conversion-type-id, the current scope will be
7494 reset. However, we need that information in able to look up the
7495 conversion function later, so we save it here. */
7496 saved_scope = parser->scope;
7497 saved_qualifying_scope = parser->qualifying_scope;
7498 saved_object_scope = parser->object_scope;
7499 /* We must enter the scope of the class so that the names of
7500 entities declared within the class are available in the
7501 conversion-type-id. For example, consider:
7502
21526606 7503 struct S {
a723baf1
MM
7504 typedef int I;
7505 operator I();
7506 };
7507
7508 S::operator I() { ... }
7509
7510 In order to see that `I' is a type-name in the definition, we
7511 must be in the scope of `S'. */
7512 if (saved_scope)
91b004e5 7513 pop_p = push_scope (saved_scope);
a723baf1
MM
7514 /* Parse the conversion-type-id. */
7515 type = cp_parser_conversion_type_id (parser);
7516 /* Leave the scope of the class, if any. */
91b004e5 7517 if (pop_p)
a723baf1
MM
7518 pop_scope (saved_scope);
7519 /* Restore the saved scope. */
7520 parser->scope = saved_scope;
7521 parser->qualifying_scope = saved_qualifying_scope;
7522 parser->object_scope = saved_object_scope;
7523 /* If the TYPE is invalid, indicate failure. */
7524 if (type == error_mark_node)
7525 return error_mark_node;
7526 return mangle_conv_op_name_for_type (type);
7527}
7528
7529/* Parse a conversion-type-id:
7530
7531 conversion-type-id:
7532 type-specifier-seq conversion-declarator [opt]
7533
7534 Returns the TYPE specified. */
7535
7536static tree
94edc4ab 7537cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7538{
7539 tree attributes;
62d1db17 7540 cp_decl_specifier_seq type_specifiers;
058b15c1 7541 cp_declarator *declarator;
a723baf1
MM
7542
7543 /* Parse the attributes. */
7544 attributes = cp_parser_attributes_opt (parser);
7545 /* Parse the type-specifiers. */
62d1db17 7546 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1 7547 /* If that didn't work, stop. */
62d1db17 7548 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7549 return error_mark_node;
7550 /* Parse the conversion-declarator. */
7551 declarator = cp_parser_conversion_declarator_opt (parser);
7552
62d1db17 7553 return grokdeclarator (declarator, &type_specifiers, TYPENAME,
a723baf1
MM
7554 /*initialized=*/0, &attributes);
7555}
7556
7557/* Parse an (optional) conversion-declarator.
7558
7559 conversion-declarator:
21526606 7560 ptr-operator conversion-declarator [opt]
a723baf1 7561
058b15c1 7562 */
a723baf1 7563
058b15c1 7564static cp_declarator *
94edc4ab 7565cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7566{
7567 enum tree_code code;
7568 tree class_type;
7569 tree cv_qualifier_seq;
7570
7571 /* We don't know if there's a ptr-operator next, or not. */
7572 cp_parser_parse_tentatively (parser);
7573 /* Try the ptr-operator. */
21526606 7574 code = cp_parser_ptr_operator (parser, &class_type,
a723baf1
MM
7575 &cv_qualifier_seq);
7576 /* If it worked, look for more conversion-declarators. */
7577 if (cp_parser_parse_definitely (parser))
7578 {
058b15c1
MM
7579 cp_declarator *declarator;
7580
7581 /* Parse another optional declarator. */
7582 declarator = cp_parser_conversion_declarator_opt (parser);
7583
7584 /* Create the representation of the declarator. */
7585 if (class_type)
7586 declarator = make_ptrmem_declarator (cv_qualifier_seq,
7587 class_type,
a723baf1 7588 declarator);
058b15c1
MM
7589 else if (code == INDIRECT_REF)
7590 declarator = make_pointer_declarator (cv_qualifier_seq,
7591 declarator);
7592 else
7593 declarator = make_reference_declarator (cv_qualifier_seq,
a723baf1 7594 declarator);
058b15c1
MM
7595
7596 return declarator;
a723baf1
MM
7597 }
7598
058b15c1 7599 return NULL;
a723baf1
MM
7600}
7601
7602/* Parse an (optional) ctor-initializer.
7603
7604 ctor-initializer:
21526606 7605 : mem-initializer-list
a723baf1
MM
7606
7607 Returns TRUE iff the ctor-initializer was actually present. */
7608
7609static bool
94edc4ab 7610cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7611{
7612 /* If the next token is not a `:', then there is no
7613 ctor-initializer. */
7614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7615 {
7616 /* Do default initialization of any bases and members. */
7617 if (DECL_CONSTRUCTOR_P (current_function_decl))
7618 finish_mem_initializers (NULL_TREE);
7619
7620 return false;
7621 }
7622
7623 /* Consume the `:' token. */
7624 cp_lexer_consume_token (parser->lexer);
7625 /* And the mem-initializer-list. */
7626 cp_parser_mem_initializer_list (parser);
7627
7628 return true;
7629}
7630
7631/* Parse a mem-initializer-list.
7632
7633 mem-initializer-list:
7634 mem-initializer
7635 mem-initializer , mem-initializer-list */
7636
7637static void
94edc4ab 7638cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7639{
7640 tree mem_initializer_list = NULL_TREE;
7641
7642 /* Let the semantic analysis code know that we are starting the
7643 mem-initializer-list. */
0e136342
MM
7644 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7645 error ("only constructors take base initializers");
a723baf1
MM
7646
7647 /* Loop through the list. */
7648 while (true)
7649 {
7650 tree mem_initializer;
7651
7652 /* Parse the mem-initializer. */
7653 mem_initializer = cp_parser_mem_initializer (parser);
7654 /* Add it to the list, unless it was erroneous. */
7655 if (mem_initializer)
7656 {
7657 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7658 mem_initializer_list = mem_initializer;
7659 }
7660 /* If the next token is not a `,', we're done. */
7661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7662 break;
7663 /* Consume the `,' token. */
7664 cp_lexer_consume_token (parser->lexer);
7665 }
7666
7667 /* Perform semantic analysis. */
0e136342
MM
7668 if (DECL_CONSTRUCTOR_P (current_function_decl))
7669 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7670}
7671
7672/* Parse a mem-initializer.
7673
7674 mem-initializer:
21526606 7675 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7676
7677 GNU extension:
21526606 7678
a723baf1 7679 mem-initializer:
34cd5ae7 7680 ( expression-list [opt] )
a723baf1
MM
7681
7682 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7683 class) or FIELD_DECL (for a non-static data member) to initialize;
7684 the TREE_VALUE is the expression-list. */
7685
7686static tree
94edc4ab 7687cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7688{
7689 tree mem_initializer_id;
7690 tree expression_list;
1f5a253a 7691 tree member;
21526606 7692
a723baf1
MM
7693 /* Find out what is being initialized. */
7694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7695 {
7696 pedwarn ("anachronistic old-style base class initializer");
7697 mem_initializer_id = NULL_TREE;
7698 }
7699 else
7700 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7701 member = expand_member_init (mem_initializer_id);
7702 if (member && !DECL_P (member))
7703 in_base_initializer = 1;
7efa3e22 7704
21526606 7705 expression_list
39703eb9
MM
7706 = cp_parser_parenthesized_expression_list (parser, false,
7707 /*non_constant_p=*/NULL);
7efa3e22 7708 if (!expression_list)
a723baf1 7709 expression_list = void_type_node;
a723baf1 7710
1f5a253a 7711 in_base_initializer = 0;
21526606 7712
1f5a253a 7713 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7714}
7715
7716/* Parse a mem-initializer-id.
7717
7718 mem-initializer-id:
7719 :: [opt] nested-name-specifier [opt] class-name
21526606 7720 identifier
a723baf1
MM
7721
7722 Returns a TYPE indicating the class to be initializer for the first
7723 production. Returns an IDENTIFIER_NODE indicating the data member
7724 to be initialized for the second production. */
7725
7726static tree
94edc4ab 7727cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7728{
7729 bool global_scope_p;
7730 bool nested_name_specifier_p;
8a83a693 7731 bool template_p = false;
a723baf1
MM
7732 tree id;
7733
8a83a693
GB
7734 /* `typename' is not allowed in this context ([temp.res]). */
7735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7736 {
7737 error ("keyword `typename' not allowed in this context (a qualified "
7738 "member initializer is implicitly a type)");
7739 cp_lexer_consume_token (parser->lexer);
7740 }
a723baf1 7741 /* Look for the optional `::' operator. */
21526606
EC
7742 global_scope_p
7743 = (cp_parser_global_scope_opt (parser,
7744 /*current_scope_valid_p=*/false)
a723baf1
MM
7745 != NULL_TREE);
7746 /* Look for the optional nested-name-specifier. The simplest way to
7747 implement:
7748
7749 [temp.res]
7750
7751 The keyword `typename' is not permitted in a base-specifier or
7752 mem-initializer; in these contexts a qualified name that
7753 depends on a template-parameter is implicitly assumed to be a
7754 type name.
7755
7756 is to assume that we have seen the `typename' keyword at this
7757 point. */
21526606 7758 nested_name_specifier_p
a723baf1
MM
7759 = (cp_parser_nested_name_specifier_opt (parser,
7760 /*typename_keyword_p=*/true,
7761 /*check_dependency_p=*/true,
a668c6ad
MM
7762 /*type_p=*/true,
7763 /*is_declaration=*/true)
a723baf1 7764 != NULL_TREE);
8a83a693
GB
7765 if (nested_name_specifier_p)
7766 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7767 /* If there is a `::' operator or a nested-name-specifier, then we
7768 are definitely looking for a class-name. */
7769 if (global_scope_p || nested_name_specifier_p)
7770 return cp_parser_class_name (parser,
7771 /*typename_keyword_p=*/true,
8a83a693 7772 /*template_keyword_p=*/template_p,
a723baf1 7773 /*type_p=*/false,
a723baf1 7774 /*check_dependency_p=*/true,
a668c6ad
MM
7775 /*class_head_p=*/false,
7776 /*is_declaration=*/true);
a723baf1
MM
7777 /* Otherwise, we could also be looking for an ordinary identifier. */
7778 cp_parser_parse_tentatively (parser);
7779 /* Try a class-name. */
21526606 7780 id = cp_parser_class_name (parser,
a723baf1
MM
7781 /*typename_keyword_p=*/true,
7782 /*template_keyword_p=*/false,
7783 /*type_p=*/false,
a723baf1 7784 /*check_dependency_p=*/true,
a668c6ad
MM
7785 /*class_head_p=*/false,
7786 /*is_declaration=*/true);
a723baf1
MM
7787 /* If we found one, we're done. */
7788 if (cp_parser_parse_definitely (parser))
7789 return id;
7790 /* Otherwise, look for an ordinary identifier. */
7791 return cp_parser_identifier (parser);
7792}
7793
7794/* Overloading [gram.over] */
7795
7796/* Parse an operator-function-id.
7797
7798 operator-function-id:
21526606 7799 operator operator
a723baf1
MM
7800
7801 Returns an IDENTIFIER_NODE for the operator which is a
7802 human-readable spelling of the identifier, e.g., `operator +'. */
7803
21526606 7804static tree
94edc4ab 7805cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7806{
7807 /* Look for the `operator' keyword. */
7808 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7809 return error_mark_node;
7810 /* And then the name of the operator itself. */
7811 return cp_parser_operator (parser);
7812}
7813
7814/* Parse an operator.
7815
7816 operator:
7817 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7818 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7819 || ++ -- , ->* -> () []
7820
7821 GNU Extensions:
21526606 7822
a723baf1
MM
7823 operator:
7824 <? >? <?= >?=
7825
7826 Returns an IDENTIFIER_NODE for the operator which is a
7827 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7828
a723baf1 7829static tree
94edc4ab 7830cp_parser_operator (cp_parser* parser)
a723baf1
MM
7831{
7832 tree id = NULL_TREE;
7833 cp_token *token;
7834
7835 /* Peek at the next token. */
7836 token = cp_lexer_peek_token (parser->lexer);
7837 /* Figure out which operator we have. */
7838 switch (token->type)
7839 {
7840 case CPP_KEYWORD:
7841 {
7842 enum tree_code op;
7843
7844 /* The keyword should be either `new' or `delete'. */
7845 if (token->keyword == RID_NEW)
7846 op = NEW_EXPR;
7847 else if (token->keyword == RID_DELETE)
7848 op = DELETE_EXPR;
7849 else
7850 break;
7851
7852 /* Consume the `new' or `delete' token. */
7853 cp_lexer_consume_token (parser->lexer);
7854
7855 /* Peek at the next token. */
7856 token = cp_lexer_peek_token (parser->lexer);
7857 /* If it's a `[' token then this is the array variant of the
7858 operator. */
7859 if (token->type == CPP_OPEN_SQUARE)
7860 {
7861 /* Consume the `[' token. */
7862 cp_lexer_consume_token (parser->lexer);
7863 /* Look for the `]' token. */
7864 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7865 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7866 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7867 }
7868 /* Otherwise, we have the non-array variant. */
7869 else
7870 id = ansi_opname (op);
7871
7872 return id;
7873 }
7874
7875 case CPP_PLUS:
7876 id = ansi_opname (PLUS_EXPR);
7877 break;
7878
7879 case CPP_MINUS:
7880 id = ansi_opname (MINUS_EXPR);
7881 break;
7882
7883 case CPP_MULT:
7884 id = ansi_opname (MULT_EXPR);
7885 break;
7886
7887 case CPP_DIV:
7888 id = ansi_opname (TRUNC_DIV_EXPR);
7889 break;
7890
7891 case CPP_MOD:
7892 id = ansi_opname (TRUNC_MOD_EXPR);
7893 break;
7894
7895 case CPP_XOR:
7896 id = ansi_opname (BIT_XOR_EXPR);
7897 break;
7898
7899 case CPP_AND:
7900 id = ansi_opname (BIT_AND_EXPR);
7901 break;
7902
7903 case CPP_OR:
7904 id = ansi_opname (BIT_IOR_EXPR);
7905 break;
7906
7907 case CPP_COMPL:
7908 id = ansi_opname (BIT_NOT_EXPR);
7909 break;
21526606 7910
a723baf1
MM
7911 case CPP_NOT:
7912 id = ansi_opname (TRUTH_NOT_EXPR);
7913 break;
7914
7915 case CPP_EQ:
7916 id = ansi_assopname (NOP_EXPR);
7917 break;
7918
7919 case CPP_LESS:
7920 id = ansi_opname (LT_EXPR);
7921 break;
7922
7923 case CPP_GREATER:
7924 id = ansi_opname (GT_EXPR);
7925 break;
7926
7927 case CPP_PLUS_EQ:
7928 id = ansi_assopname (PLUS_EXPR);
7929 break;
7930
7931 case CPP_MINUS_EQ:
7932 id = ansi_assopname (MINUS_EXPR);
7933 break;
7934
7935 case CPP_MULT_EQ:
7936 id = ansi_assopname (MULT_EXPR);
7937 break;
7938
7939 case CPP_DIV_EQ:
7940 id = ansi_assopname (TRUNC_DIV_EXPR);
7941 break;
7942
7943 case CPP_MOD_EQ:
7944 id = ansi_assopname (TRUNC_MOD_EXPR);
7945 break;
7946
7947 case CPP_XOR_EQ:
7948 id = ansi_assopname (BIT_XOR_EXPR);
7949 break;
7950
7951 case CPP_AND_EQ:
7952 id = ansi_assopname (BIT_AND_EXPR);
7953 break;
7954
7955 case CPP_OR_EQ:
7956 id = ansi_assopname (BIT_IOR_EXPR);
7957 break;
7958
7959 case CPP_LSHIFT:
7960 id = ansi_opname (LSHIFT_EXPR);
7961 break;
7962
7963 case CPP_RSHIFT:
7964 id = ansi_opname (RSHIFT_EXPR);
7965 break;
7966
7967 case CPP_LSHIFT_EQ:
7968 id = ansi_assopname (LSHIFT_EXPR);
7969 break;
7970
7971 case CPP_RSHIFT_EQ:
7972 id = ansi_assopname (RSHIFT_EXPR);
7973 break;
7974
7975 case CPP_EQ_EQ:
7976 id = ansi_opname (EQ_EXPR);
7977 break;
7978
7979 case CPP_NOT_EQ:
7980 id = ansi_opname (NE_EXPR);
7981 break;
7982
7983 case CPP_LESS_EQ:
7984 id = ansi_opname (LE_EXPR);
7985 break;
7986
7987 case CPP_GREATER_EQ:
7988 id = ansi_opname (GE_EXPR);
7989 break;
7990
7991 case CPP_AND_AND:
7992 id = ansi_opname (TRUTH_ANDIF_EXPR);
7993 break;
7994
7995 case CPP_OR_OR:
7996 id = ansi_opname (TRUTH_ORIF_EXPR);
7997 break;
21526606 7998
a723baf1
MM
7999 case CPP_PLUS_PLUS:
8000 id = ansi_opname (POSTINCREMENT_EXPR);
8001 break;
8002
8003 case CPP_MINUS_MINUS:
8004 id = ansi_opname (PREDECREMENT_EXPR);
8005 break;
8006
8007 case CPP_COMMA:
8008 id = ansi_opname (COMPOUND_EXPR);
8009 break;
8010
8011 case CPP_DEREF_STAR:
8012 id = ansi_opname (MEMBER_REF);
8013 break;
8014
8015 case CPP_DEREF:
8016 id = ansi_opname (COMPONENT_REF);
8017 break;
8018
8019 case CPP_OPEN_PAREN:
8020 /* Consume the `('. */
8021 cp_lexer_consume_token (parser->lexer);
8022 /* Look for the matching `)'. */
8023 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8024 return ansi_opname (CALL_EXPR);
8025
8026 case CPP_OPEN_SQUARE:
8027 /* Consume the `['. */
8028 cp_lexer_consume_token (parser->lexer);
8029 /* Look for the matching `]'. */
8030 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8031 return ansi_opname (ARRAY_REF);
8032
8033 /* Extensions. */
8034 case CPP_MIN:
8035 id = ansi_opname (MIN_EXPR);
8036 break;
8037
8038 case CPP_MAX:
8039 id = ansi_opname (MAX_EXPR);
8040 break;
8041
8042 case CPP_MIN_EQ:
8043 id = ansi_assopname (MIN_EXPR);
8044 break;
8045
8046 case CPP_MAX_EQ:
8047 id = ansi_assopname (MAX_EXPR);
8048 break;
8049
8050 default:
8051 /* Anything else is an error. */
8052 break;
8053 }
8054
8055 /* If we have selected an identifier, we need to consume the
8056 operator token. */
8057 if (id)
8058 cp_lexer_consume_token (parser->lexer);
8059 /* Otherwise, no valid operator name was present. */
8060 else
8061 {
8062 cp_parser_error (parser, "expected operator");
8063 id = error_mark_node;
8064 }
8065
8066 return id;
8067}
8068
8069/* Parse a template-declaration.
8070
8071 template-declaration:
21526606 8072 export [opt] template < template-parameter-list > declaration
a723baf1
MM
8073
8074 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 8075 class-specifier.
a723baf1
MM
8076
8077 The grammar rule given by the standard isn't correct. What
8078 is really meant is:
8079
8080 template-declaration:
21526606 8081 export [opt] template-parameter-list-seq
a723baf1 8082 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 8083 export [opt] template-parameter-list-seq
a723baf1
MM
8084 function-definition
8085
8086 template-parameter-list-seq:
8087 template-parameter-list-seq [opt]
8088 template < template-parameter-list > */
8089
8090static void
94edc4ab 8091cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
8092{
8093 /* Check for `export'. */
8094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8095 {
8096 /* Consume the `export' token. */
8097 cp_lexer_consume_token (parser->lexer);
8098 /* Warn that we do not support `export'. */
8099 warning ("keyword `export' not implemented, and will be ignored");
8100 }
8101
8102 cp_parser_template_declaration_after_export (parser, member_p);
8103}
8104
8105/* Parse a template-parameter-list.
8106
8107 template-parameter-list:
8108 template-parameter
8109 template-parameter-list , template-parameter
8110
8111 Returns a TREE_LIST. Each node represents a template parameter.
8112 The nodes are connected via their TREE_CHAINs. */
8113
8114static tree
94edc4ab 8115cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8116{
8117 tree parameter_list = NULL_TREE;
8118
8119 while (true)
8120 {
8121 tree parameter;
8122 cp_token *token;
058b15c1 8123 bool is_non_type;
a723baf1
MM
8124
8125 /* Parse the template-parameter. */
058b15c1 8126 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1
MM
8127 /* Add it to the list. */
8128 parameter_list = process_template_parm (parameter_list,
058b15c1
MM
8129 parameter,
8130 is_non_type);
a723baf1
MM
8131 /* Peek at the next token. */
8132 token = cp_lexer_peek_token (parser->lexer);
8133 /* If it's not a `,', we're done. */
8134 if (token->type != CPP_COMMA)
8135 break;
8136 /* Otherwise, consume the `,' token. */
8137 cp_lexer_consume_token (parser->lexer);
8138 }
8139
8140 return parameter_list;
8141}
8142
8143/* Parse a template-parameter.
8144
8145 template-parameter:
8146 type-parameter
8147 parameter-declaration
8148
8149 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
058b15c1
MM
8150 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8151 true iff this parameter is a non-type parameter. */
a723baf1
MM
8152
8153static tree
058b15c1 8154cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8155{
8156 cp_token *token;
62d1db17 8157 cp_parameter_declarator *parameter_declarator;
a723baf1 8158
058b15c1
MM
8159 /* Assume it is a type parameter or a template parameter. */
8160 *is_non_type = false;
a723baf1
MM
8161 /* Peek at the next token. */
8162 token = cp_lexer_peek_token (parser->lexer);
8163 /* If it is `class' or `template', we have a type-parameter. */
8164 if (token->keyword == RID_TEMPLATE)
8165 return cp_parser_type_parameter (parser);
8166 /* If it is `class' or `typename' we do not know yet whether it is a
8167 type parameter or a non-type parameter. Consider:
8168
8169 template <typename T, typename T::X X> ...
8170
8171 or:
21526606 8172
a723baf1
MM
8173 template <class C, class D*> ...
8174
8175 Here, the first parameter is a type parameter, and the second is
8176 a non-type parameter. We can tell by looking at the token after
8177 the identifier -- if it is a `,', `=', or `>' then we have a type
8178 parameter. */
8179 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8180 {
8181 /* Peek at the token after `class' or `typename'. */
8182 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8183 /* If it's an identifier, skip it. */
8184 if (token->type == CPP_NAME)
8185 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8186 /* Now, see if the token looks like the end of a template
8187 parameter. */
21526606 8188 if (token->type == CPP_COMMA
a723baf1
MM
8189 || token->type == CPP_EQ
8190 || token->type == CPP_GREATER)
8191 return cp_parser_type_parameter (parser);
8192 }
8193
21526606 8194 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8195
8196 [temp.param]
8197
8198 When parsing a default template-argument for a non-type
8199 template-parameter, the first non-nested `>' is taken as the end
8200 of the template parameter-list rather than a greater-than
8201 operator. */
058b15c1
MM
8202 *is_non_type = true;
8203 parameter_declarator
8204 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8205 /*parenthesized_p=*/NULL);
8206 return (build_tree_list
8207 (parameter_declarator->default_argument,
8208 grokdeclarator (parameter_declarator->declarator,
62d1db17 8209 &parameter_declarator->decl_specifiers,
058b15c1
MM
8210 PARM, /*initialized=*/0,
8211 /*attrlist=*/NULL)));
a723baf1
MM
8212}
8213
8214/* Parse a type-parameter.
8215
8216 type-parameter:
8217 class identifier [opt]
8218 class identifier [opt] = type-id
8219 typename identifier [opt]
8220 typename identifier [opt] = type-id
8221 template < template-parameter-list > class identifier [opt]
21526606
EC
8222 template < template-parameter-list > class identifier [opt]
8223 = id-expression
a723baf1
MM
8224
8225 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8226 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8227 the declaration of the parameter. */
8228
8229static tree
94edc4ab 8230cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8231{
8232 cp_token *token;
8233 tree parameter;
8234
8235 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8236 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8237 "`class', `typename', or `template'");
a723baf1
MM
8238 if (!token)
8239 return error_mark_node;
8240
8241 switch (token->keyword)
8242 {
8243 case RID_CLASS:
8244 case RID_TYPENAME:
8245 {
8246 tree identifier;
8247 tree default_argument;
8248
8249 /* If the next token is an identifier, then it names the
8250 parameter. */
8251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8252 identifier = cp_parser_identifier (parser);
8253 else
8254 identifier = NULL_TREE;
8255
8256 /* Create the parameter. */
8257 parameter = finish_template_type_parm (class_type_node, identifier);
8258
8259 /* If the next token is an `=', we have a default argument. */
8260 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8261 {
8262 /* Consume the `=' token. */
8263 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8264 /* Parse the default-argument. */
a723baf1
MM
8265 default_argument = cp_parser_type_id (parser);
8266 }
8267 else
8268 default_argument = NULL_TREE;
8269
8270 /* Create the combined representation of the parameter and the
8271 default argument. */
c67d36d0 8272 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8273 }
8274 break;
8275
8276 case RID_TEMPLATE:
8277 {
8278 tree parameter_list;
8279 tree identifier;
8280 tree default_argument;
8281
8282 /* Look for the `<'. */
8283 cp_parser_require (parser, CPP_LESS, "`<'");
8284 /* Parse the template-parameter-list. */
8285 begin_template_parm_list ();
21526606 8286 parameter_list
a723baf1
MM
8287 = cp_parser_template_parameter_list (parser);
8288 parameter_list = end_template_parm_list (parameter_list);
8289 /* Look for the `>'. */
8290 cp_parser_require (parser, CPP_GREATER, "`>'");
8291 /* Look for the `class' keyword. */
8292 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8293 /* If the next token is an `=', then there is a
8294 default-argument. If the next token is a `>', we are at
8295 the end of the parameter-list. If the next token is a `,',
8296 then we are at the end of this parameter. */
8297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8298 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8299 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8300 identifier = cp_parser_identifier (parser);
8301 else
8302 identifier = NULL_TREE;
8303 /* Create the template parameter. */
8304 parameter = finish_template_template_parm (class_type_node,
8305 identifier);
21526606 8306
a723baf1
MM
8307 /* If the next token is an `=', then there is a
8308 default-argument. */
8309 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8310 {
b0bc6e8e
KL
8311 bool is_template;
8312
a723baf1
MM
8313 /* Consume the `='. */
8314 cp_lexer_consume_token (parser->lexer);
8315 /* Parse the id-expression. */
21526606 8316 default_argument
a723baf1
MM
8317 = cp_parser_id_expression (parser,
8318 /*template_keyword_p=*/false,
8319 /*check_dependency_p=*/true,
b0bc6e8e 8320 /*template_p=*/&is_template,
f3c2dfc6 8321 /*declarator_p=*/false);
a3a503a5
GB
8322 if (TREE_CODE (default_argument) == TYPE_DECL)
8323 /* If the id-expression was a template-id that refers to
8324 a template-class, we already have the declaration here,
8325 so no further lookup is needed. */
8326 ;
8327 else
8328 /* Look up the name. */
21526606 8329 default_argument
a3a503a5
GB
8330 = cp_parser_lookup_name (parser, default_argument,
8331 /*is_type=*/false,
8332 /*is_template=*/is_template,
8333 /*is_namespace=*/false,
8334 /*check_dependency=*/true);
a723baf1
MM
8335 /* See if the default argument is valid. */
8336 default_argument
8337 = check_template_template_default_arg (default_argument);
8338 }
8339 else
8340 default_argument = NULL_TREE;
8341
8342 /* Create the combined representation of the parameter and the
8343 default argument. */
c67d36d0 8344 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8345 }
8346 break;
8347
8348 default:
8349 /* Anything else is an error. */
8350 cp_parser_error (parser,
8351 "expected `class', `typename', or `template'");
8352 parameter = error_mark_node;
8353 }
21526606 8354
a723baf1
MM
8355 return parameter;
8356}
8357
8358/* Parse a template-id.
8359
8360 template-id:
8361 template-name < template-argument-list [opt] >
8362
8363 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8364 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8365 returned. Otherwise, if the template-name names a function, or set
8366 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8367 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8368
8369 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8370 uninstantiated templates. */
8371
8372static tree
21526606
EC
8373cp_parser_template_id (cp_parser *parser,
8374 bool template_keyword_p,
a668c6ad
MM
8375 bool check_dependency_p,
8376 bool is_declaration)
a723baf1
MM
8377{
8378 tree template;
8379 tree arguments;
a723baf1 8380 tree template_id;
a723baf1
MM
8381 ptrdiff_t start_of_id;
8382 tree access_check = NULL_TREE;
f4abade9 8383 cp_token *next_token, *next_token_2;
a668c6ad 8384 bool is_identifier;
a723baf1
MM
8385
8386 /* If the next token corresponds to a template-id, there is no need
8387 to reparse it. */
2050a1bb
MM
8388 next_token = cp_lexer_peek_token (parser->lexer);
8389 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8390 {
8391 tree value;
8392 tree check;
8393
8394 /* Get the stored value. */
8395 value = cp_lexer_consume_token (parser->lexer)->value;
8396 /* Perform any access checks that were deferred. */
8397 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8398 perform_or_defer_access_check (TREE_PURPOSE (check),
8399 TREE_VALUE (check));
a723baf1
MM
8400 /* Return the stored value. */
8401 return TREE_VALUE (value);
8402 }
8403
2050a1bb
MM
8404 /* Avoid performing name lookup if there is no possibility of
8405 finding a template-id. */
8406 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8407 || (next_token->type == CPP_NAME
21526606 8408 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8409 (parser, 2)))
2050a1bb
MM
8410 {
8411 cp_parser_error (parser, "expected template-id");
8412 return error_mark_node;
8413 }
8414
a723baf1
MM
8415 /* Remember where the template-id starts. */
8416 if (cp_parser_parsing_tentatively (parser)
8417 && !cp_parser_committed_to_tentative_parse (parser))
8418 {
2050a1bb 8419 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
8420 start_of_id = cp_lexer_token_difference (parser->lexer,
8421 parser->lexer->first_token,
8422 next_token);
a723baf1
MM
8423 }
8424 else
8425 start_of_id = -1;
8426
8d241e0b 8427 push_deferring_access_checks (dk_deferred);
cf22909c 8428
a723baf1 8429 /* Parse the template-name. */
a668c6ad 8430 is_identifier = false;
a723baf1 8431 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8432 check_dependency_p,
8433 is_declaration,
8434 &is_identifier);
8435 if (template == error_mark_node || is_identifier)
cf22909c
KL
8436 {
8437 pop_deferring_access_checks ();
a668c6ad 8438 return template;
cf22909c 8439 }
a723baf1 8440
21526606 8441 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8442 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8443 parse correctly the argument list. */
8444 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8445 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8446 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8447 && next_token->flags & DIGRAPH
21526606 8448 && next_token_2->type == CPP_COLON
f4abade9 8449 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8450 {
f4abade9
GB
8451 cp_parser_parse_tentatively (parser);
8452 /* Change `:' into `::'. */
8453 next_token_2->type = CPP_SCOPE;
8454 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8455 CPP_LESS. */
8456 cp_lexer_consume_token (parser->lexer);
8457 /* Parse the arguments. */
8458 arguments = cp_parser_enclosed_template_argument_list (parser);
8459 if (!cp_parser_parse_definitely (parser))
8460 {
8461 /* If we couldn't parse an argument list, then we revert our changes
8462 and return simply an error. Maybe this is not a template-id
8463 after all. */
8464 next_token_2->type = CPP_COLON;
8465 cp_parser_error (parser, "expected `<'");
8466 pop_deferring_access_checks ();
8467 return error_mark_node;
8468 }
8469 /* Otherwise, emit an error about the invalid digraph, but continue
8470 parsing because we got our argument list. */
8471 pedwarn ("`<::' cannot begin a template-argument list");
8472 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8473 "between `<' and `::'");
8474 if (!flag_permissive)
8475 {
8476 static bool hint;
8477 if (!hint)
8478 {
8479 inform ("(if you use `-fpermissive' G++ will accept your code)");
8480 hint = true;
8481 }
8482 }
8483 }
8484 else
8485 {
8486 /* Look for the `<' that starts the template-argument-list. */
8487 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8488 {
8489 pop_deferring_access_checks ();
8490 return error_mark_node;
8491 }
8492 /* Parse the arguments. */
8493 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8494 }
a723baf1
MM
8495
8496 /* Build a representation of the specialization. */
8497 if (TREE_CODE (template) == IDENTIFIER_NODE)
8498 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8499 else if (DECL_CLASS_TEMPLATE_P (template)
8500 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8501 template_id
8502 = finish_template_type (template, arguments,
8503 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8504 CPP_SCOPE));
8505 else
8506 {
8507 /* If it's not a class-template or a template-template, it should be
8508 a function-template. */
8509 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8510 || TREE_CODE (template) == OVERLOAD
8511 || BASELINK_P (template)),
8512 20010716);
21526606 8513
a723baf1
MM
8514 template_id = lookup_template_function (template, arguments);
8515 }
21526606 8516
cf22909c
KL
8517 /* Retrieve any deferred checks. Do not pop this access checks yet
8518 so the memory will not be reclaimed during token replacing below. */
8519 access_check = get_deferred_access_checks ();
8520
a723baf1
MM
8521 /* If parsing tentatively, replace the sequence of tokens that makes
8522 up the template-id with a CPP_TEMPLATE_ID token. That way,
8523 should we re-parse the token stream, we will not have to repeat
8524 the effort required to do the parse, nor will we issue duplicate
8525 error messages about problems during instantiation of the
8526 template. */
8527 if (start_of_id >= 0)
8528 {
8529 cp_token *token;
a723baf1
MM
8530
8531 /* Find the token that corresponds to the start of the
8532 template-id. */
21526606 8533 token = cp_lexer_advance_token (parser->lexer,
a723baf1
MM
8534 parser->lexer->first_token,
8535 start_of_id);
8536
a723baf1
MM
8537 /* Reset the contents of the START_OF_ID token. */
8538 token->type = CPP_TEMPLATE_ID;
8539 token->value = build_tree_list (access_check, template_id);
8540 token->keyword = RID_MAX;
8541 /* Purge all subsequent tokens. */
8542 cp_lexer_purge_tokens_after (parser->lexer, token);
8543 }
8544
cf22909c 8545 pop_deferring_access_checks ();
a723baf1
MM
8546 return template_id;
8547}
8548
8549/* Parse a template-name.
8550
8551 template-name:
8552 identifier
21526606 8553
a723baf1
MM
8554 The standard should actually say:
8555
8556 template-name:
8557 identifier
8558 operator-function-id
a723baf1
MM
8559
8560 A defect report has been filed about this issue.
8561
0d956474
GB
8562 A conversion-function-id cannot be a template name because they cannot
8563 be part of a template-id. In fact, looking at this code:
8564
8565 a.operator K<int>()
8566
8567 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8568 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8569 explicit argument list, since the only allowed template parameter is
8570 the type to which it is converting.
8571
a723baf1
MM
8572 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8573 `template' keyword, in a construction like:
8574
8575 T::template f<3>()
8576
8577 In that case `f' is taken to be a template-name, even though there
8578 is no way of knowing for sure.
8579
8580 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8581 name refers to a set of overloaded functions, at least one of which
8582 is a template, or an IDENTIFIER_NODE with the name of the template,
8583 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8584 names are looked up inside uninstantiated templates. */
8585
8586static tree
21526606
EC
8587cp_parser_template_name (cp_parser* parser,
8588 bool template_keyword_p,
a668c6ad
MM
8589 bool check_dependency_p,
8590 bool is_declaration,
8591 bool *is_identifier)
a723baf1
MM
8592{
8593 tree identifier;
8594 tree decl;
8595 tree fns;
8596
8597 /* If the next token is `operator', then we have either an
8598 operator-function-id or a conversion-function-id. */
8599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8600 {
8601 /* We don't know whether we're looking at an
8602 operator-function-id or a conversion-function-id. */
8603 cp_parser_parse_tentatively (parser);
8604 /* Try an operator-function-id. */
8605 identifier = cp_parser_operator_function_id (parser);
8606 /* If that didn't work, try a conversion-function-id. */
8607 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8608 {
8609 cp_parser_error (parser, "expected template-name");
8610 return error_mark_node;
8611 }
a723baf1
MM
8612 }
8613 /* Look for the identifier. */
8614 else
8615 identifier = cp_parser_identifier (parser);
21526606 8616
a723baf1
MM
8617 /* If we didn't find an identifier, we don't have a template-id. */
8618 if (identifier == error_mark_node)
8619 return error_mark_node;
8620
8621 /* If the name immediately followed the `template' keyword, then it
8622 is a template-name. However, if the next token is not `<', then
8623 we do not treat it as a template-name, since it is not being used
8624 as part of a template-id. This enables us to handle constructs
8625 like:
8626
8627 template <typename T> struct S { S(); };
8628 template <typename T> S<T>::S();
8629
8630 correctly. We would treat `S' as a template -- if it were `S<T>'
8631 -- but we do not if there is no `<'. */
a668c6ad
MM
8632
8633 if (processing_template_decl
f4abade9 8634 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8635 {
8636 /* In a declaration, in a dependent context, we pretend that the
8637 "template" keyword was present in order to improve error
8638 recovery. For example, given:
21526606 8639
a668c6ad 8640 template <typename T> void f(T::X<int>);
21526606 8641
a668c6ad 8642 we want to treat "X<int>" as a template-id. */
21526606
EC
8643 if (is_declaration
8644 && !template_keyword_p
a668c6ad 8645 && parser->scope && TYPE_P (parser->scope)
4e0f4df5
GB
8646 && dependent_type_p (parser->scope)
8647 /* Do not do this for dtors (or ctors), since they never
8648 need the template keyword before their name. */
8649 && !constructor_name_p (identifier, parser->scope))
a668c6ad
MM
8650 {
8651 ptrdiff_t start;
8652 cp_token* token;
8653 /* Explain what went wrong. */
8654 error ("non-template `%D' used as template", identifier);
4e0f4df5
GB
8655 inform ("use `%T::template %D' to indicate that it is a template",
8656 parser->scope, identifier);
a668c6ad
MM
8657 /* If parsing tentatively, find the location of the "<"
8658 token. */
8659 if (cp_parser_parsing_tentatively (parser)
8660 && !cp_parser_committed_to_tentative_parse (parser))
8661 {
8662 cp_parser_simulate_error (parser);
8663 token = cp_lexer_peek_token (parser->lexer);
8664 token = cp_lexer_prev_token (parser->lexer, token);
8665 start = cp_lexer_token_difference (parser->lexer,
8666 parser->lexer->first_token,
8667 token);
8668 }
8669 else
8670 start = -1;
8671 /* Parse the template arguments so that we can issue error
8672 messages about them. */
8673 cp_lexer_consume_token (parser->lexer);
8674 cp_parser_enclosed_template_argument_list (parser);
8675 /* Skip tokens until we find a good place from which to
8676 continue parsing. */
8677 cp_parser_skip_to_closing_parenthesis (parser,
8678 /*recovering=*/true,
8679 /*or_comma=*/true,
8680 /*consume_paren=*/false);
8681 /* If parsing tentatively, permanently remove the
8682 template argument list. That will prevent duplicate
8683 error messages from being issued about the missing
8684 "template" keyword. */
8685 if (start >= 0)
8686 {
8687 token = cp_lexer_advance_token (parser->lexer,
8688 parser->lexer->first_token,
8689 start);
8690 cp_lexer_purge_tokens_after (parser->lexer, token);
8691 }
8692 if (is_identifier)
8693 *is_identifier = true;
8694 return identifier;
8695 }
9d363a56
MM
8696
8697 /* If the "template" keyword is present, then there is generally
8698 no point in doing name-lookup, so we just return IDENTIFIER.
8699 But, if the qualifying scope is non-dependent then we can
8700 (and must) do name-lookup normally. */
8701 if (template_keyword_p
8702 && (!parser->scope
8703 || (TYPE_P (parser->scope)
8704 && dependent_type_p (parser->scope))))
a668c6ad
MM
8705 return identifier;
8706 }
a723baf1
MM
8707
8708 /* Look up the name. */
8709 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 8710 /*is_type=*/false,
b0bc6e8e 8711 /*is_template=*/false,
eea9800f 8712 /*is_namespace=*/false,
a723baf1
MM
8713 check_dependency_p);
8714 decl = maybe_get_template_decl_from_type_decl (decl);
8715
8716 /* If DECL is a template, then the name was a template-name. */
8717 if (TREE_CODE (decl) == TEMPLATE_DECL)
8718 ;
21526606 8719 else
a723baf1
MM
8720 {
8721 /* The standard does not explicitly indicate whether a name that
8722 names a set of overloaded declarations, some of which are
8723 templates, is a template-name. However, such a name should
8724 be a template-name; otherwise, there is no way to form a
8725 template-id for the overloaded templates. */
8726 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8727 if (TREE_CODE (fns) == OVERLOAD)
8728 {
8729 tree fn;
21526606 8730
a723baf1
MM
8731 for (fn = fns; fn; fn = OVL_NEXT (fn))
8732 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8733 break;
8734 }
8735 else
8736 {
8737 /* Otherwise, the name does not name a template. */
8738 cp_parser_error (parser, "expected template-name");
8739 return error_mark_node;
8740 }
8741 }
8742
8743 /* If DECL is dependent, and refers to a function, then just return
8744 its name; we will look it up again during template instantiation. */
8745 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8746 {
8747 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8748 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8749 return identifier;
8750 }
8751
8752 return decl;
8753}
8754
8755/* Parse a template-argument-list.
8756
8757 template-argument-list:
8758 template-argument
8759 template-argument-list , template-argument
8760
04c06002 8761 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8762
8763static tree
94edc4ab 8764cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8765{
bf12d54d
NS
8766 tree fixed_args[10];
8767 unsigned n_args = 0;
8768 unsigned alloced = 10;
8769 tree *arg_ary = fixed_args;
8770 tree vec;
4bb8ca28 8771 bool saved_in_template_argument_list_p;
a723baf1 8772
4bb8ca28
MM
8773 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8774 parser->in_template_argument_list_p = true;
bf12d54d 8775 do
a723baf1
MM
8776 {
8777 tree argument;
8778
bf12d54d 8779 if (n_args)
04c06002 8780 /* Consume the comma. */
bf12d54d 8781 cp_lexer_consume_token (parser->lexer);
21526606 8782
a723baf1
MM
8783 /* Parse the template-argument. */
8784 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8785 if (n_args == alloced)
8786 {
8787 alloced *= 2;
21526606 8788
bf12d54d
NS
8789 if (arg_ary == fixed_args)
8790 {
8791 arg_ary = xmalloc (sizeof (tree) * alloced);
8792 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8793 }
8794 else
8795 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8796 }
8797 arg_ary[n_args++] = argument;
a723baf1 8798 }
bf12d54d
NS
8799 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8800
8801 vec = make_tree_vec (n_args);
a723baf1 8802
bf12d54d
NS
8803 while (n_args--)
8804 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8805
bf12d54d
NS
8806 if (arg_ary != fixed_args)
8807 free (arg_ary);
4bb8ca28 8808 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8809 return vec;
a723baf1
MM
8810}
8811
8812/* Parse a template-argument.
8813
8814 template-argument:
8815 assignment-expression
8816 type-id
8817 id-expression
8818
8819 The representation is that of an assignment-expression, type-id, or
8820 id-expression -- except that the qualified id-expression is
8821 evaluated, so that the value returned is either a DECL or an
21526606 8822 OVERLOAD.
d17811fd
MM
8823
8824 Although the standard says "assignment-expression", it forbids
8825 throw-expressions or assignments in the template argument.
8826 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8827
8828static tree
94edc4ab 8829cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8830{
8831 tree argument;
8832 bool template_p;
d17811fd 8833 bool address_p;
4d5297fa 8834 bool maybe_type_id = false;
d17811fd 8835 cp_token *token;
b3445994 8836 cp_id_kind idk;
d17811fd 8837 tree qualifying_class;
a723baf1
MM
8838
8839 /* There's really no way to know what we're looking at, so we just
21526606 8840 try each alternative in order.
a723baf1
MM
8841
8842 [temp.arg]
8843
8844 In a template-argument, an ambiguity between a type-id and an
8845 expression is resolved to a type-id, regardless of the form of
21526606 8846 the corresponding template-parameter.
a723baf1
MM
8847
8848 Therefore, we try a type-id first. */
8849 cp_parser_parse_tentatively (parser);
a723baf1 8850 argument = cp_parser_type_id (parser);
4d5297fa 8851 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8852 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8853 also valid expressions. For instance:
8854
8855 struct X { int operator >> (int); };
8856 template <int V> struct Foo {};
8857 Foo<X () >> 5> r;
8858
8859 Here 'X()' is a valid type-id of a function type, but the user just
8860 wanted to write the expression "X() >> 5". Thus, we remember that we
8861 found a valid type-id, but we still try to parse the argument as an
8862 expression to see what happens. */
8863 if (!cp_parser_error_occurred (parser)
8864 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8865 {
8866 maybe_type_id = true;
8867 cp_parser_abort_tentative_parse (parser);
8868 }
8869 else
8870 {
8871 /* If the next token isn't a `,' or a `>', then this argument wasn't
8872 really finished. This means that the argument is not a valid
8873 type-id. */
8874 if (!cp_parser_next_token_ends_template_argument_p (parser))
8875 cp_parser_error (parser, "expected template-argument");
8876 /* If that worked, we're done. */
8877 if (cp_parser_parse_definitely (parser))
8878 return argument;
8879 }
a723baf1
MM
8880 /* We're still not sure what the argument will be. */
8881 cp_parser_parse_tentatively (parser);
8882 /* Try a template. */
21526606 8883 argument = cp_parser_id_expression (parser,
a723baf1
MM
8884 /*template_keyword_p=*/false,
8885 /*check_dependency_p=*/true,
f3c2dfc6
MM
8886 &template_p,
8887 /*declarator_p=*/false);
a723baf1
MM
8888 /* If the next token isn't a `,' or a `>', then this argument wasn't
8889 really finished. */
d17811fd 8890 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8891 cp_parser_error (parser, "expected template-argument");
8892 if (!cp_parser_error_occurred (parser))
8893 {
f746161e
MM
8894 /* Figure out what is being referred to. If the id-expression
8895 was for a class template specialization, then we will have a
8896 TYPE_DECL at this point. There is no need to do name lookup
8897 at this point in that case. */
8898 if (TREE_CODE (argument) != TYPE_DECL)
8899 argument = cp_parser_lookup_name (parser, argument,
8900 /*is_type=*/false,
8901 /*is_template=*/template_p,
8902 /*is_namespace=*/false,
8903 /*check_dependency=*/true);
5b4acce1
KL
8904 if (TREE_CODE (argument) != TEMPLATE_DECL
8905 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8906 cp_parser_error (parser, "expected template-name");
8907 }
8908 if (cp_parser_parse_definitely (parser))
8909 return argument;
d17811fd
MM
8910 /* It must be a non-type argument. There permitted cases are given
8911 in [temp.arg.nontype]:
8912
8913 -- an integral constant-expression of integral or enumeration
8914 type; or
8915
8916 -- the name of a non-type template-parameter; or
8917
8918 -- the name of an object or function with external linkage...
8919
8920 -- the address of an object or function with external linkage...
8921
04c06002 8922 -- a pointer to member... */
d17811fd
MM
8923 /* Look for a non-type template parameter. */
8924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8925 {
8926 cp_parser_parse_tentatively (parser);
8927 argument = cp_parser_primary_expression (parser,
8928 &idk,
8929 &qualifying_class);
8930 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8931 || !cp_parser_next_token_ends_template_argument_p (parser))
8932 cp_parser_simulate_error (parser);
8933 if (cp_parser_parse_definitely (parser))
8934 return argument;
8935 }
8936 /* If the next token is "&", the argument must be the address of an
8937 object or function with external linkage. */
8938 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8939 if (address_p)
8940 cp_lexer_consume_token (parser->lexer);
8941 /* See if we might have an id-expression. */
8942 token = cp_lexer_peek_token (parser->lexer);
8943 if (token->type == CPP_NAME
8944 || token->keyword == RID_OPERATOR
8945 || token->type == CPP_SCOPE
8946 || token->type == CPP_TEMPLATE_ID
8947 || token->type == CPP_NESTED_NAME_SPECIFIER)
8948 {
8949 cp_parser_parse_tentatively (parser);
8950 argument = cp_parser_primary_expression (parser,
8951 &idk,
8952 &qualifying_class);
8953 if (cp_parser_error_occurred (parser)
8954 || !cp_parser_next_token_ends_template_argument_p (parser))
8955 cp_parser_abort_tentative_parse (parser);
8956 else
8957 {
8958 if (qualifying_class)
8959 argument = finish_qualified_id_expr (qualifying_class,
8960 argument,
8961 /*done=*/true,
8962 address_p);
8963 if (TREE_CODE (argument) == VAR_DECL)
8964 {
8965 /* A variable without external linkage might still be a
8966 valid constant-expression, so no error is issued here
8967 if the external-linkage check fails. */
8968 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8969 cp_parser_simulate_error (parser);
8970 }
8971 else if (is_overloaded_fn (argument))
8972 /* All overloaded functions are allowed; if the external
8973 linkage test does not pass, an error will be issued
8974 later. */
8975 ;
8976 else if (address_p
21526606 8977 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8978 || TREE_CODE (argument) == SCOPE_REF))
8979 /* A pointer-to-member. */
8980 ;
8981 else
8982 cp_parser_simulate_error (parser);
8983
8984 if (cp_parser_parse_definitely (parser))
8985 {
8986 if (address_p)
8987 argument = build_x_unary_op (ADDR_EXPR, argument);
8988 return argument;
8989 }
8990 }
8991 }
8992 /* If the argument started with "&", there are no other valid
8993 alternatives at this point. */
8994 if (address_p)
8995 {
8996 cp_parser_error (parser, "invalid non-type template argument");
8997 return error_mark_node;
8998 }
4d5297fa 8999 /* If the argument wasn't successfully parsed as a type-id followed
21526606 9000 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
9001 Otherwise, we try parsing the constant-expression tentatively,
9002 because the argument could really be a type-id. */
9003 if (maybe_type_id)
9004 cp_parser_parse_tentatively (parser);
21526606 9005 argument = cp_parser_constant_expression (parser,
d17811fd
MM
9006 /*allow_non_constant_p=*/false,
9007 /*non_constant_p=*/NULL);
9baa27a9 9008 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
9009 if (!maybe_type_id)
9010 return argument;
9011 if (!cp_parser_next_token_ends_template_argument_p (parser))
9012 cp_parser_error (parser, "expected template-argument");
9013 if (cp_parser_parse_definitely (parser))
9014 return argument;
9015 /* We did our best to parse the argument as a non type-id, but that
9016 was the only alternative that matched (albeit with a '>' after
21526606 9017 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
9018 diagnostic will then be issued. */
9019 return cp_parser_type_id (parser);
a723baf1
MM
9020}
9021
9022/* Parse an explicit-instantiation.
9023
9024 explicit-instantiation:
21526606 9025 template declaration
a723baf1
MM
9026
9027 Although the standard says `declaration', what it really means is:
9028
9029 explicit-instantiation:
21526606 9030 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
9031
9032 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9033 supposed to be allowed. A defect report has been filed about this
21526606 9034 issue.
a723baf1
MM
9035
9036 GNU Extension:
21526606 9037
a723baf1 9038 explicit-instantiation:
21526606 9039 storage-class-specifier template
a723baf1 9040 decl-specifier-seq [opt] declarator [opt] ;
21526606 9041 function-specifier template
a723baf1
MM
9042 decl-specifier-seq [opt] declarator [opt] ; */
9043
9044static void
94edc4ab 9045cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 9046{
560ad596 9047 int declares_class_or_enum;
62d1db17 9048 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
9049 tree extension_specifier = NULL_TREE;
9050
9051 /* Look for an (optional) storage-class-specifier or
9052 function-specifier. */
9053 if (cp_parser_allow_gnu_extensions_p (parser))
9054 {
21526606 9055 extension_specifier
a723baf1
MM
9056 = cp_parser_storage_class_specifier_opt (parser);
9057 if (!extension_specifier)
62d1db17
MM
9058 extension_specifier
9059 = cp_parser_function_specifier_opt (parser,
9060 /*decl_specs=*/NULL);
a723baf1
MM
9061 }
9062
9063 /* Look for the `template' keyword. */
9064 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9065 /* Let the front end know that we are processing an explicit
9066 instantiation. */
9067 begin_explicit_instantiation ();
9068 /* [temp.explicit] says that we are supposed to ignore access
9069 control while processing explicit instantiation directives. */
78757caa 9070 push_deferring_access_checks (dk_no_check);
a723baf1 9071 /* Parse a decl-specifier-seq. */
62d1db17
MM
9072 cp_parser_decl_specifier_seq (parser,
9073 CP_PARSER_FLAGS_OPTIONAL,
9074 &decl_specifiers,
9075 &declares_class_or_enum);
a723baf1
MM
9076 /* If there was exactly one decl-specifier, and it declared a class,
9077 and there's no declarator, then we have an explicit type
9078 instantiation. */
9079 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9080 {
9081 tree type;
9082
62d1db17 9083 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
9084 /* Turn access control back on for names used during
9085 template instantiation. */
9086 pop_deferring_access_checks ();
a723baf1
MM
9087 if (type)
9088 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9089 }
9090 else
9091 {
058b15c1 9092 cp_declarator *declarator;
a723baf1
MM
9093 tree decl;
9094
9095 /* Parse the declarator. */
21526606 9096 declarator
62b8a44e 9097 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
9098 /*ctor_dtor_or_conv_p=*/NULL,
9099 /*parenthesized_p=*/NULL);
21526606 9100 cp_parser_check_for_definition_in_return_type (declarator,
560ad596 9101 declares_class_or_enum);
058b15c1 9102 if (declarator != cp_error_declarator)
216bb6e1 9103 {
62d1db17 9104 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
9105 NORMAL, 0, NULL);
9106 /* Turn access control back on for names used during
9107 template instantiation. */
9108 pop_deferring_access_checks ();
9109 /* Do the explicit instantiation. */
9110 do_decl_instantiation (decl, extension_specifier);
9111 }
9112 else
9113 {
9114 pop_deferring_access_checks ();
9115 /* Skip the body of the explicit instantiation. */
9116 cp_parser_skip_to_end_of_statement (parser);
9117 }
a723baf1
MM
9118 }
9119 /* We're done with the instantiation. */
9120 end_explicit_instantiation ();
a723baf1 9121
e0860732 9122 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9123}
9124
9125/* Parse an explicit-specialization.
9126
9127 explicit-specialization:
21526606 9128 template < > declaration
a723baf1
MM
9129
9130 Although the standard says `declaration', what it really means is:
9131
9132 explicit-specialization:
9133 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9134 template <> function-definition
a723baf1
MM
9135 template <> explicit-specialization
9136 template <> template-declaration */
9137
9138static void
94edc4ab 9139cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9140{
9141 /* Look for the `template' keyword. */
9142 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9143 /* Look for the `<'. */
9144 cp_parser_require (parser, CPP_LESS, "`<'");
9145 /* Look for the `>'. */
9146 cp_parser_require (parser, CPP_GREATER, "`>'");
9147 /* We have processed another parameter list. */
9148 ++parser->num_template_parameter_lists;
9149 /* Let the front end know that we are beginning a specialization. */
9150 begin_specialization ();
9151
9152 /* If the next keyword is `template', we need to figure out whether
9153 or not we're looking a template-declaration. */
9154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9155 {
9156 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9157 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9158 cp_parser_template_declaration_after_export (parser,
9159 /*member_p=*/false);
9160 else
9161 cp_parser_explicit_specialization (parser);
9162 }
9163 else
9164 /* Parse the dependent declaration. */
21526606 9165 cp_parser_single_declaration (parser,
a723baf1
MM
9166 /*member_p=*/false,
9167 /*friend_p=*/NULL);
9168
9169 /* We're done with the specialization. */
9170 end_specialization ();
9171 /* We're done with this parameter list. */
9172 --parser->num_template_parameter_lists;
9173}
9174
9175/* Parse a type-specifier.
9176
9177 type-specifier:
9178 simple-type-specifier
9179 class-specifier
9180 enum-specifier
9181 elaborated-type-specifier
9182 cv-qualifier
9183
9184 GNU Extension:
9185
9186 type-specifier:
9187 __complex__
9188
62d1db17
MM
9189 Returns a representation of the type-specifier. For a
9190 class-specifier, enum-specifier, or elaborated-type-specifier, a
9191 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1
MM
9192
9193 If IS_FRIEND is TRUE then this type-specifier is being declared a
9194 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
9195 appearing in a decl-specifier-seq.
9196
9197 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9198 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9199 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9200 if a type is declared; 2 if it is defined. Otherwise, it is set to
9201 zero.
a723baf1
MM
9202
9203 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9204 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9205 is set to FALSE. */
9206
9207static tree
21526606
EC
9208cp_parser_type_specifier (cp_parser* parser,
9209 cp_parser_flags flags,
62d1db17 9210 cp_decl_specifier_seq *decl_specs,
94edc4ab 9211 bool is_declaration,
560ad596 9212 int* declares_class_or_enum,
94edc4ab 9213 bool* is_cv_qualifier)
a723baf1
MM
9214{
9215 tree type_spec = NULL_TREE;
9216 cp_token *token;
9217 enum rid keyword;
62d1db17 9218 cp_decl_spec ds = ds_last;
a723baf1
MM
9219
9220 /* Assume this type-specifier does not declare a new type. */
9221 if (declares_class_or_enum)
543ca912 9222 *declares_class_or_enum = 0;
a723baf1
MM
9223 /* And that it does not specify a cv-qualifier. */
9224 if (is_cv_qualifier)
9225 *is_cv_qualifier = false;
9226 /* Peek at the next token. */
9227 token = cp_lexer_peek_token (parser->lexer);
9228
9229 /* If we're looking at a keyword, we can use that to guide the
9230 production we choose. */
9231 keyword = token->keyword;
9232 switch (keyword)
9233 {
9234 /* Any of these indicate either a class-specifier, or an
9235 elaborated-type-specifier. */
9236 case RID_CLASS:
9237 case RID_STRUCT:
9238 case RID_UNION:
9239 case RID_ENUM:
9240 /* Parse tentatively so that we can back up if we don't find a
9241 class-specifier or enum-specifier. */
9242 cp_parser_parse_tentatively (parser);
9243 /* Look for the class-specifier or enum-specifier. */
9244 if (keyword == RID_ENUM)
9245 type_spec = cp_parser_enum_specifier (parser);
9246 else
9247 type_spec = cp_parser_class_specifier (parser);
9248
9249 /* If that worked, we're done. */
9250 if (cp_parser_parse_definitely (parser))
9251 {
9252 if (declares_class_or_enum)
560ad596 9253 *declares_class_or_enum = 2;
62d1db17
MM
9254 if (decl_specs)
9255 cp_parser_set_decl_spec_type (decl_specs,
9256 type_spec,
9257 /*user_defined_p=*/true);
a723baf1
MM
9258 return type_spec;
9259 }
9260
9261 /* Fall through. */
9262
9263 case RID_TYPENAME:
9264 /* Look for an elaborated-type-specifier. */
62d1db17
MM
9265 type_spec
9266 = (cp_parser_elaborated_type_specifier
9267 (parser,
9268 decl_specs && decl_specs->specs[(int) ds_friend],
9269 is_declaration));
a723baf1
MM
9270 /* We're declaring a class or enum -- unless we're using
9271 `typename'. */
9272 if (declares_class_or_enum && keyword != RID_TYPENAME)
560ad596 9273 *declares_class_or_enum = 1;
62d1db17
MM
9274 if (decl_specs)
9275 cp_parser_set_decl_spec_type (decl_specs,
9276 type_spec,
9277 /*user_defined_p=*/true);
a723baf1
MM
9278 return type_spec;
9279
9280 case RID_CONST:
62d1db17
MM
9281 ds = ds_const;
9282 if (is_cv_qualifier)
9283 *is_cv_qualifier = true;
9284 break;
9285
a723baf1 9286 case RID_VOLATILE:
62d1db17 9287 ds = ds_volatile;
a723baf1
MM
9288 if (is_cv_qualifier)
9289 *is_cv_qualifier = true;
62d1db17 9290 break;
a723baf1 9291
62d1db17
MM
9292 case RID_RESTRICT:
9293 ds = ds_restrict;
9294 if (is_cv_qualifier)
9295 *is_cv_qualifier = true;
9296 break;
a723baf1
MM
9297
9298 case RID_COMPLEX:
9299 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9300 ds = ds_complex;
9301 break;
a723baf1
MM
9302
9303 default:
9304 break;
9305 }
9306
62d1db17
MM
9307 /* Handle simple keywords. */
9308 if (ds != ds_last)
9309 {
9310 if (decl_specs)
9311 {
9312 ++decl_specs->specs[(int)ds];
9313 decl_specs->any_specifiers_p = true;
9314 }
9315 return cp_lexer_consume_token (parser->lexer)->value;
9316 }
9317
a723baf1
MM
9318 /* If we do not already have a type-specifier, assume we are looking
9319 at a simple-type-specifier. */
62d1db17
MM
9320 type_spec = cp_parser_simple_type_specifier (parser,
9321 decl_specs,
9322 flags);
a723baf1
MM
9323
9324 /* If we didn't find a type-specifier, and a type-specifier was not
9325 optional in this context, issue an error message. */
9326 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9327 {
9328 cp_parser_error (parser, "expected type specifier");
9329 return error_mark_node;
9330 }
9331
9332 return type_spec;
9333}
9334
9335/* Parse a simple-type-specifier.
9336
9337 simple-type-specifier:
9338 :: [opt] nested-name-specifier [opt] type-name
9339 :: [opt] nested-name-specifier template template-id
9340 char
9341 wchar_t
9342 bool
9343 short
9344 int
9345 long
9346 signed
9347 unsigned
9348 float
9349 double
21526606 9350 void
a723baf1
MM
9351
9352 GNU Extension:
9353
9354 simple-type-specifier:
9355 __typeof__ unary-expression
9356 __typeof__ ( type-id )
9357
62d1db17
MM
9358 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9359 appropriately updated. */
a723baf1
MM
9360
9361static tree
62d1db17
MM
9362cp_parser_simple_type_specifier (cp_parser* parser,
9363 cp_decl_specifier_seq *decl_specs,
9364 cp_parser_flags flags)
a723baf1
MM
9365{
9366 tree type = NULL_TREE;
9367 cp_token *token;
9368
9369 /* Peek at the next token. */
9370 token = cp_lexer_peek_token (parser->lexer);
9371
9372 /* If we're looking at a keyword, things are easy. */
9373 switch (token->keyword)
9374 {
9375 case RID_CHAR:
62d1db17
MM
9376 if (decl_specs)
9377 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9378 type = char_type_node;
9379 break;
a723baf1 9380 case RID_WCHAR:
4b0d3cbe
MM
9381 type = wchar_type_node;
9382 break;
a723baf1 9383 case RID_BOOL:
4b0d3cbe
MM
9384 type = boolean_type_node;
9385 break;
a723baf1 9386 case RID_SHORT:
62d1db17
MM
9387 if (decl_specs)
9388 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9389 type = short_integer_type_node;
9390 break;
a723baf1 9391 case RID_INT:
62d1db17
MM
9392 if (decl_specs)
9393 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9394 type = integer_type_node;
9395 break;
a723baf1 9396 case RID_LONG:
62d1db17
MM
9397 if (decl_specs)
9398 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9399 type = long_integer_type_node;
9400 break;
a723baf1 9401 case RID_SIGNED:
62d1db17
MM
9402 if (decl_specs)
9403 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9404 type = integer_type_node;
9405 break;
a723baf1 9406 case RID_UNSIGNED:
62d1db17
MM
9407 if (decl_specs)
9408 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9409 type = unsigned_type_node;
9410 break;
a723baf1 9411 case RID_FLOAT:
4b0d3cbe
MM
9412 type = float_type_node;
9413 break;
a723baf1 9414 case RID_DOUBLE:
4b0d3cbe
MM
9415 type = double_type_node;
9416 break;
a723baf1 9417 case RID_VOID:
4b0d3cbe
MM
9418 type = void_type_node;
9419 break;
a723baf1
MM
9420
9421 case RID_TYPEOF:
62d1db17
MM
9422 /* Consume the `typeof' token. */
9423 cp_lexer_consume_token (parser->lexer);
9424 /* Parse the operand to `typeof'. */
9425 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9426 /* If it is not already a TYPE, take its type. */
9427 if (!TYPE_P (type))
9428 type = finish_typeof (type);
9429
9430 if (decl_specs)
9431 cp_parser_set_decl_spec_type (decl_specs, type,
9432 /*user_defined_p=*/true);
9433
9434 return type;
a723baf1
MM
9435
9436 default:
9437 break;
9438 }
9439
4b0d3cbe
MM
9440 /* If the type-specifier was for a built-in type, we're done. */
9441 if (type)
9442 {
9443 tree id;
9444
62d1db17
MM
9445 /* Record the type. */
9446 if (decl_specs
9447 && (token->keyword != RID_SIGNED
9448 && token->keyword != RID_UNSIGNED
9449 && token->keyword != RID_SHORT
9450 && token->keyword != RID_LONG))
9451 cp_parser_set_decl_spec_type (decl_specs,
9452 type,
9453 /*user_defined=*/false);
9454 if (decl_specs)
9455 decl_specs->any_specifiers_p = true;
9456
4b0d3cbe
MM
9457 /* Consume the token. */
9458 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9459
9460 /* There is no valid C++ program where a non-template type is
9461 followed by a "<". That usually indicates that the user thought
9462 that the type was a template. */
9463 cp_parser_check_for_invalid_template_id (parser, type);
9464
62d1db17 9465 return TYPE_NAME (type);
4b0d3cbe
MM
9466 }
9467
a723baf1 9468 /* The type-specifier must be a user-defined type. */
21526606 9469 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9470 {
0c1a1ecd
MM
9471 bool qualified_p;
9472
a723baf1
MM
9473 /* Don't gobble tokens or issue error messages if this is an
9474 optional type-specifier. */
9475 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9476 cp_parser_parse_tentatively (parser);
9477
9478 /* Look for the optional `::' operator. */
9479 cp_parser_global_scope_opt (parser,
9480 /*current_scope_valid_p=*/false);
9481 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9482 qualified_p
9483 = (cp_parser_nested_name_specifier_opt (parser,
9484 /*typename_keyword_p=*/false,
9485 /*check_dependency_p=*/true,
9486 /*type_p=*/false,
6661a85f
EB
9487 /*is_declaration=*/false)
9488 != NULL_TREE);
a723baf1
MM
9489 /* If we have seen a nested-name-specifier, and the next token
9490 is `template', then we are using the template-id production. */
21526606 9491 if (parser->scope
a723baf1
MM
9492 && cp_parser_optional_template_keyword (parser))
9493 {
9494 /* Look for the template-id. */
21526606 9495 type = cp_parser_template_id (parser,
a723baf1 9496 /*template_keyword_p=*/true,
a668c6ad
MM
9497 /*check_dependency_p=*/true,
9498 /*is_declaration=*/false);
a723baf1
MM
9499 /* If the template-id did not name a type, we are out of
9500 luck. */
9501 if (TREE_CODE (type) != TYPE_DECL)
9502 {
9503 cp_parser_error (parser, "expected template-id for type");
9504 type = NULL_TREE;
9505 }
9506 }
9507 /* Otherwise, look for a type-name. */
9508 else
4bb8ca28 9509 type = cp_parser_type_name (parser);
0c1a1ecd
MM
9510 /* Keep track of all name-lookups performed in class scopes. */
9511 if (type
9512 && !qualified_p
9513 && TREE_CODE (type) == TYPE_DECL
9514 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9515 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9516 /* If it didn't work out, we don't have a TYPE. */
21526606 9517 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9518 && !cp_parser_parse_definitely (parser))
9519 type = NULL_TREE;
62d1db17
MM
9520 if (type && decl_specs)
9521 cp_parser_set_decl_spec_type (decl_specs, type,
9522 /*user_defined=*/true);
a723baf1
MM
9523 }
9524
9525 /* If we didn't get a type-name, issue an error message. */
9526 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9527 {
9528 cp_parser_error (parser, "expected type-name");
9529 return error_mark_node;
9530 }
9531
a668c6ad
MM
9532 /* There is no valid C++ program where a non-template type is
9533 followed by a "<". That usually indicates that the user thought
9534 that the type was a template. */
4bb8ca28 9535 if (type && type != error_mark_node)
ee43dab5 9536 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 9537
a723baf1
MM
9538 return type;
9539}
9540
9541/* Parse a type-name.
9542
9543 type-name:
9544 class-name
9545 enum-name
21526606 9546 typedef-name
a723baf1
MM
9547
9548 enum-name:
9549 identifier
9550
9551 typedef-name:
21526606 9552 identifier
a723baf1
MM
9553
9554 Returns a TYPE_DECL for the the type. */
9555
9556static tree
94edc4ab 9557cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9558{
9559 tree type_decl;
9560 tree identifier;
9561
9562 /* We can't know yet whether it is a class-name or not. */
9563 cp_parser_parse_tentatively (parser);
9564 /* Try a class-name. */
21526606 9565 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9566 /*typename_keyword_p=*/false,
9567 /*template_keyword_p=*/false,
9568 /*type_p=*/false,
a723baf1 9569 /*check_dependency_p=*/true,
a668c6ad
MM
9570 /*class_head_p=*/false,
9571 /*is_declaration=*/false);
a723baf1
MM
9572 /* If it's not a class-name, keep looking. */
9573 if (!cp_parser_parse_definitely (parser))
9574 {
9575 /* It must be a typedef-name or an enum-name. */
9576 identifier = cp_parser_identifier (parser);
9577 if (identifier == error_mark_node)
9578 return error_mark_node;
21526606 9579
a723baf1
MM
9580 /* Look up the type-name. */
9581 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9582 /* Issue an error if we did not find a type-name. */
9583 if (TREE_CODE (type_decl) != TYPE_DECL)
9584 {
4bb8ca28 9585 if (!cp_parser_simulate_error (parser))
21526606 9586 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9587 "is not a type");
a723baf1
MM
9588 type_decl = error_mark_node;
9589 }
9590 /* Remember that the name was used in the definition of the
9591 current class so that we can check later to see if the
9592 meaning would have been different after the class was
9593 entirely defined. */
9594 else if (type_decl != error_mark_node
9595 && !parser->scope)
9596 maybe_note_name_used_in_class (identifier, type_decl);
9597 }
21526606 9598
a723baf1
MM
9599 return type_decl;
9600}
9601
9602
9603/* Parse an elaborated-type-specifier. Note that the grammar given
9604 here incorporates the resolution to DR68.
9605
9606 elaborated-type-specifier:
9607 class-key :: [opt] nested-name-specifier [opt] identifier
9608 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9609 enum :: [opt] nested-name-specifier [opt] identifier
9610 typename :: [opt] nested-name-specifier identifier
21526606
EC
9611 typename :: [opt] nested-name-specifier template [opt]
9612 template-id
a723baf1 9613
360d1b99
MM
9614 GNU extension:
9615
9616 elaborated-type-specifier:
9617 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9618 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9619 template [opt] template-id
9620 enum attributes :: [opt] nested-name-specifier [opt] identifier
9621
a723baf1
MM
9622 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9623 declared `friend'. If IS_DECLARATION is TRUE, then this
9624 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9625 something is being declared.
9626
9627 Returns the TYPE specified. */
9628
9629static tree
21526606
EC
9630cp_parser_elaborated_type_specifier (cp_parser* parser,
9631 bool is_friend,
94edc4ab 9632 bool is_declaration)
a723baf1
MM
9633{
9634 enum tag_types tag_type;
9635 tree identifier;
9636 tree type = NULL_TREE;
360d1b99 9637 tree attributes = NULL_TREE;
a723baf1
MM
9638
9639 /* See if we're looking at the `enum' keyword. */
9640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9641 {
9642 /* Consume the `enum' token. */
9643 cp_lexer_consume_token (parser->lexer);
9644 /* Remember that it's an enumeration type. */
9645 tag_type = enum_type;
360d1b99
MM
9646 /* Parse the attributes. */
9647 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9648 }
9649 /* Or, it might be `typename'. */
9650 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9651 RID_TYPENAME))
9652 {
9653 /* Consume the `typename' token. */
9654 cp_lexer_consume_token (parser->lexer);
9655 /* Remember that it's a `typename' type. */
9656 tag_type = typename_type;
9657 /* The `typename' keyword is only allowed in templates. */
9658 if (!processing_template_decl)
9659 pedwarn ("using `typename' outside of template");
9660 }
9661 /* Otherwise it must be a class-key. */
9662 else
9663 {
9664 tag_type = cp_parser_class_key (parser);
9665 if (tag_type == none_type)
9666 return error_mark_node;
360d1b99
MM
9667 /* Parse the attributes. */
9668 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9669 }
9670
9671 /* Look for the `::' operator. */
21526606 9672 cp_parser_global_scope_opt (parser,
a723baf1
MM
9673 /*current_scope_valid_p=*/false);
9674 /* Look for the nested-name-specifier. */
9675 if (tag_type == typename_type)
8fa1ad0e
MM
9676 {
9677 if (cp_parser_nested_name_specifier (parser,
9678 /*typename_keyword_p=*/true,
9679 /*check_dependency_p=*/true,
a668c6ad 9680 /*type_p=*/true,
21526606 9681 is_declaration)
8fa1ad0e
MM
9682 == error_mark_node)
9683 return error_mark_node;
9684 }
a723baf1
MM
9685 else
9686 /* Even though `typename' is not present, the proposed resolution
9687 to Core Issue 180 says that in `class A<T>::B', `B' should be
9688 considered a type-name, even if `A<T>' is dependent. */
9689 cp_parser_nested_name_specifier_opt (parser,
9690 /*typename_keyword_p=*/true,
9691 /*check_dependency_p=*/true,
a668c6ad
MM
9692 /*type_p=*/true,
9693 is_declaration);
a723baf1
MM
9694 /* For everything but enumeration types, consider a template-id. */
9695 if (tag_type != enum_type)
9696 {
9697 bool template_p = false;
9698 tree decl;
9699
9700 /* Allow the `template' keyword. */
9701 template_p = cp_parser_optional_template_keyword (parser);
9702 /* If we didn't see `template', we don't know if there's a
9703 template-id or not. */
9704 if (!template_p)
9705 cp_parser_parse_tentatively (parser);
9706 /* Parse the template-id. */
9707 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9708 /*check_dependency_p=*/true,
9709 is_declaration);
a723baf1
MM
9710 /* If we didn't find a template-id, look for an ordinary
9711 identifier. */
9712 if (!template_p && !cp_parser_parse_definitely (parser))
9713 ;
9714 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9715 in effect, then we must assume that, upon instantiation, the
9716 template will correspond to a class. */
9717 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9718 && tag_type == typename_type)
9719 type = make_typename_type (parser->scope, decl,
9720 /*complain=*/1);
21526606 9721 else
a723baf1
MM
9722 type = TREE_TYPE (decl);
9723 }
9724
9725 /* For an enumeration type, consider only a plain identifier. */
9726 if (!type)
9727 {
9728 identifier = cp_parser_identifier (parser);
9729
9730 if (identifier == error_mark_node)
eb5abb39
NS
9731 {
9732 parser->scope = NULL_TREE;
9733 return error_mark_node;
9734 }
a723baf1
MM
9735
9736 /* For a `typename', we needn't call xref_tag. */
9737 if (tag_type == typename_type)
21526606 9738 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9739 identifier);
a723baf1
MM
9740 /* Look up a qualified name in the usual way. */
9741 if (parser->scope)
9742 {
9743 tree decl;
9744
9745 /* In an elaborated-type-specifier, names are assumed to name
9746 types, so we set IS_TYPE to TRUE when calling
9747 cp_parser_lookup_name. */
21526606 9748 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 9749 /*is_type=*/true,
b0bc6e8e 9750 /*is_template=*/false,
eea9800f 9751 /*is_namespace=*/false,
a723baf1 9752 /*check_dependency=*/true);
710b73e6
KL
9753
9754 /* If we are parsing friend declaration, DECL may be a
9755 TEMPLATE_DECL tree node here. However, we need to check
9756 whether this TEMPLATE_DECL results in valid code. Consider
9757 the following example:
9758
9759 namespace N {
9760 template <class T> class C {};
9761 }
9762 class X {
9763 template <class T> friend class N::C; // #1, valid code
9764 };
9765 template <class T> class Y {
9766 friend class N::C; // #2, invalid code
9767 };
9768
9769 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9770 name lookup of `N::C'. We see that friend declaration must
9771 be template for the code to be valid. Note that
9772 processing_template_decl does not work here since it is
9773 always 1 for the above two cases. */
9774
21526606 9775 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9776 (decl, /*tag_name_p=*/is_friend
9777 && parser->num_template_parameter_lists));
a723baf1
MM
9778
9779 if (TREE_CODE (decl) != TYPE_DECL)
9780 {
9781 error ("expected type-name");
9782 return error_mark_node;
9783 }
560ad596
MM
9784
9785 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9786 check_elaborated_type_specifier
4b0d3cbe 9787 (tag_type, decl,
560ad596
MM
9788 (parser->num_template_parameter_lists
9789 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9790
9791 type = TREE_TYPE (decl);
9792 }
21526606 9793 else
a723baf1
MM
9794 {
9795 /* An elaborated-type-specifier sometimes introduces a new type and
9796 sometimes names an existing type. Normally, the rule is that it
9797 introduces a new type only if there is not an existing type of
9798 the same name already in scope. For example, given:
9799
9800 struct S {};
9801 void f() { struct S s; }
9802
9803 the `struct S' in the body of `f' is the same `struct S' as in
9804 the global scope; the existing definition is used. However, if
21526606 9805 there were no global declaration, this would introduce a new
a723baf1
MM
9806 local class named `S'.
9807
9808 An exception to this rule applies to the following code:
9809
9810 namespace N { struct S; }
9811
9812 Here, the elaborated-type-specifier names a new type
9813 unconditionally; even if there is already an `S' in the
9814 containing scope this declaration names a new type.
9815 This exception only applies if the elaborated-type-specifier
9816 forms the complete declaration:
9817
21526606 9818 [class.name]
a723baf1
MM
9819
9820 A declaration consisting solely of `class-key identifier ;' is
9821 either a redeclaration of the name in the current scope or a
9822 forward declaration of the identifier as a class name. It
9823 introduces the name into the current scope.
9824
9825 We are in this situation precisely when the next token is a `;'.
9826
9827 An exception to the exception is that a `friend' declaration does
9828 *not* name a new type; i.e., given:
9829
9830 struct S { friend struct T; };
9831
21526606 9832 `T' is not a new type in the scope of `S'.
a723baf1
MM
9833
9834 Also, `new struct S' or `sizeof (struct S)' never results in the
9835 definition of a new type; a new type can only be declared in a
9bcb9aae 9836 declaration context. */
a723baf1 9837
e0fed25b
DS
9838 /* Warn about attributes. They are ignored. */
9839 if (attributes)
9840 warning ("type attributes are honored only at type definition");
9841
21526606 9842 type = xref_tag (tag_type, identifier,
21526606 9843 (is_friend
a723baf1 9844 || !is_declaration
21526606 9845 || cp_lexer_next_token_is_not (parser->lexer,
cbd63935
KL
9846 CPP_SEMICOLON)),
9847 parser->num_template_parameter_lists);
a723baf1
MM
9848 }
9849 }
9850 if (tag_type != enum_type)
9851 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9852
9853 /* A "<" cannot follow an elaborated type specifier. If that
9854 happens, the user was probably trying to form a template-id. */
9855 cp_parser_check_for_invalid_template_id (parser, type);
9856
a723baf1
MM
9857 return type;
9858}
9859
9860/* Parse an enum-specifier.
9861
9862 enum-specifier:
9863 enum identifier [opt] { enumerator-list [opt] }
9864
9865 Returns an ENUM_TYPE representing the enumeration. */
9866
9867static tree
94edc4ab 9868cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
9869{
9870 cp_token *token;
9871 tree identifier = NULL_TREE;
9872 tree type;
9873
9874 /* Look for the `enum' keyword. */
9875 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9876 return error_mark_node;
9877 /* Peek at the next token. */
9878 token = cp_lexer_peek_token (parser->lexer);
9879
9880 /* See if it is an identifier. */
9881 if (token->type == CPP_NAME)
9882 identifier = cp_parser_identifier (parser);
9883
9884 /* Look for the `{'. */
9885 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9886 return error_mark_node;
9887
9888 /* At this point, we're going ahead with the enum-specifier, even
9889 if some other problem occurs. */
9890 cp_parser_commit_to_tentative_parse (parser);
9891
9892 /* Issue an error message if type-definitions are forbidden here. */
9893 cp_parser_check_type_definition (parser);
9894
9895 /* Create the new type. */
9896 type = start_enum (identifier ? identifier : make_anon_name ());
9897
9898 /* Peek at the next token. */
9899 token = cp_lexer_peek_token (parser->lexer);
9900 /* If it's not a `}', then there are some enumerators. */
9901 if (token->type != CPP_CLOSE_BRACE)
9902 cp_parser_enumerator_list (parser, type);
9903 /* Look for the `}'. */
9904 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9905
9906 /* Finish up the enumeration. */
9907 finish_enum (type);
9908
9909 return type;
9910}
9911
9912/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9913 TYPE.
a723baf1
MM
9914
9915 enumerator-list:
9916 enumerator-definition
9917 enumerator-list , enumerator-definition */
9918
9919static void
94edc4ab 9920cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9921{
9922 while (true)
9923 {
9924 cp_token *token;
9925
9926 /* Parse an enumerator-definition. */
9927 cp_parser_enumerator_definition (parser, type);
9928 /* Peek at the next token. */
9929 token = cp_lexer_peek_token (parser->lexer);
21526606 9930 /* If it's not a `,', then we've reached the end of the
a723baf1
MM
9931 list. */
9932 if (token->type != CPP_COMMA)
9933 break;
9934 /* Otherwise, consume the `,' and keep going. */
9935 cp_lexer_consume_token (parser->lexer);
9936 /* If the next token is a `}', there is a trailing comma. */
9937 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9938 {
9939 if (pedantic && !in_system_header)
9940 pedwarn ("comma at end of enumerator list");
9941 break;
9942 }
9943 }
9944}
9945
9946/* Parse an enumerator-definition. The enumerator has the indicated
9947 TYPE.
9948
9949 enumerator-definition:
9950 enumerator
9951 enumerator = constant-expression
21526606 9952
a723baf1
MM
9953 enumerator:
9954 identifier */
9955
9956static void
94edc4ab 9957cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9958{
9959 cp_token *token;
9960 tree identifier;
9961 tree value;
9962
9963 /* Look for the identifier. */
9964 identifier = cp_parser_identifier (parser);
9965 if (identifier == error_mark_node)
9966 return;
21526606 9967
a723baf1
MM
9968 /* Peek at the next token. */
9969 token = cp_lexer_peek_token (parser->lexer);
9970 /* If it's an `=', then there's an explicit value. */
9971 if (token->type == CPP_EQ)
9972 {
9973 /* Consume the `=' token. */
9974 cp_lexer_consume_token (parser->lexer);
9975 /* Parse the value. */
21526606 9976 value = cp_parser_constant_expression (parser,
d17811fd 9977 /*allow_non_constant_p=*/false,
14d22dd6 9978 NULL);
a723baf1
MM
9979 }
9980 else
9981 value = NULL_TREE;
9982
9983 /* Create the enumerator. */
9984 build_enumerator (identifier, value, type);
9985}
9986
9987/* Parse a namespace-name.
9988
9989 namespace-name:
9990 original-namespace-name
9991 namespace-alias
9992
9993 Returns the NAMESPACE_DECL for the namespace. */
9994
9995static tree
94edc4ab 9996cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9997{
9998 tree identifier;
9999 tree namespace_decl;
10000
10001 /* Get the name of the namespace. */
10002 identifier = cp_parser_identifier (parser);
10003 if (identifier == error_mark_node)
10004 return error_mark_node;
10005
eea9800f
MM
10006 /* Look up the identifier in the currently active scope. Look only
10007 for namespaces, due to:
10008
10009 [basic.lookup.udir]
10010
10011 When looking up a namespace-name in a using-directive or alias
21526606 10012 definition, only namespace names are considered.
eea9800f
MM
10013
10014 And:
10015
10016 [basic.lookup.qual]
10017
10018 During the lookup of a name preceding the :: scope resolution
21526606 10019 operator, object, function, and enumerator names are ignored.
eea9800f
MM
10020
10021 (Note that cp_parser_class_or_namespace_name only calls this
10022 function if the token after the name is the scope resolution
10023 operator.) */
10024 namespace_decl = cp_parser_lookup_name (parser, identifier,
eea9800f 10025 /*is_type=*/false,
b0bc6e8e 10026 /*is_template=*/false,
eea9800f
MM
10027 /*is_namespace=*/true,
10028 /*check_dependency=*/true);
a723baf1
MM
10029 /* If it's not a namespace, issue an error. */
10030 if (namespace_decl == error_mark_node
10031 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10032 {
10033 cp_parser_error (parser, "expected namespace-name");
10034 namespace_decl = error_mark_node;
10035 }
21526606 10036
a723baf1
MM
10037 return namespace_decl;
10038}
10039
10040/* Parse a namespace-definition.
10041
10042 namespace-definition:
10043 named-namespace-definition
21526606 10044 unnamed-namespace-definition
a723baf1
MM
10045
10046 named-namespace-definition:
10047 original-namespace-definition
10048 extension-namespace-definition
10049
10050 original-namespace-definition:
10051 namespace identifier { namespace-body }
21526606 10052
a723baf1
MM
10053 extension-namespace-definition:
10054 namespace original-namespace-name { namespace-body }
21526606 10055
a723baf1
MM
10056 unnamed-namespace-definition:
10057 namespace { namespace-body } */
10058
10059static void
94edc4ab 10060cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
10061{
10062 tree identifier;
10063
10064 /* Look for the `namespace' keyword. */
10065 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10066
10067 /* Get the name of the namespace. We do not attempt to distinguish
10068 between an original-namespace-definition and an
10069 extension-namespace-definition at this point. The semantic
10070 analysis routines are responsible for that. */
10071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10072 identifier = cp_parser_identifier (parser);
10073 else
10074 identifier = NULL_TREE;
10075
10076 /* Look for the `{' to start the namespace. */
10077 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10078 /* Start the namespace. */
10079 push_namespace (identifier);
10080 /* Parse the body of the namespace. */
10081 cp_parser_namespace_body (parser);
10082 /* Finish the namespace. */
10083 pop_namespace ();
10084 /* Look for the final `}'. */
10085 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10086}
10087
10088/* Parse a namespace-body.
10089
10090 namespace-body:
10091 declaration-seq [opt] */
10092
10093static void
94edc4ab 10094cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10095{
10096 cp_parser_declaration_seq_opt (parser);
10097}
10098
10099/* Parse a namespace-alias-definition.
10100
10101 namespace-alias-definition:
10102 namespace identifier = qualified-namespace-specifier ; */
10103
10104static void
94edc4ab 10105cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10106{
10107 tree identifier;
10108 tree namespace_specifier;
10109
10110 /* Look for the `namespace' keyword. */
10111 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10112 /* Look for the identifier. */
10113 identifier = cp_parser_identifier (parser);
10114 if (identifier == error_mark_node)
10115 return;
10116 /* Look for the `=' token. */
10117 cp_parser_require (parser, CPP_EQ, "`='");
10118 /* Look for the qualified-namespace-specifier. */
21526606 10119 namespace_specifier
a723baf1
MM
10120 = cp_parser_qualified_namespace_specifier (parser);
10121 /* Look for the `;' token. */
10122 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10123
10124 /* Register the alias in the symbol table. */
10125 do_namespace_alias (identifier, namespace_specifier);
10126}
10127
10128/* Parse a qualified-namespace-specifier.
10129
10130 qualified-namespace-specifier:
10131 :: [opt] nested-name-specifier [opt] namespace-name
10132
10133 Returns a NAMESPACE_DECL corresponding to the specified
10134 namespace. */
10135
10136static tree
94edc4ab 10137cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10138{
10139 /* Look for the optional `::'. */
21526606 10140 cp_parser_global_scope_opt (parser,
a723baf1
MM
10141 /*current_scope_valid_p=*/false);
10142
10143 /* Look for the optional nested-name-specifier. */
10144 cp_parser_nested_name_specifier_opt (parser,
10145 /*typename_keyword_p=*/false,
10146 /*check_dependency_p=*/true,
a668c6ad
MM
10147 /*type_p=*/false,
10148 /*is_declaration=*/true);
a723baf1
MM
10149
10150 return cp_parser_namespace_name (parser);
10151}
10152
10153/* Parse a using-declaration.
10154
10155 using-declaration:
10156 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10157 using :: unqualified-id ; */
10158
10159static void
94edc4ab 10160cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10161{
10162 cp_token *token;
10163 bool typename_p = false;
10164 bool global_scope_p;
10165 tree decl;
10166 tree identifier;
10167 tree scope;
ed5f054f 10168 tree qscope;
a723baf1
MM
10169
10170 /* Look for the `using' keyword. */
10171 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10172
a723baf1
MM
10173 /* Peek at the next token. */
10174 token = cp_lexer_peek_token (parser->lexer);
10175 /* See if it's `typename'. */
10176 if (token->keyword == RID_TYPENAME)
10177 {
10178 /* Remember that we've seen it. */
10179 typename_p = true;
10180 /* Consume the `typename' token. */
10181 cp_lexer_consume_token (parser->lexer);
10182 }
10183
10184 /* Look for the optional global scope qualification. */
21526606 10185 global_scope_p
a723baf1 10186 = (cp_parser_global_scope_opt (parser,
21526606 10187 /*current_scope_valid_p=*/false)
a723baf1
MM
10188 != NULL_TREE);
10189
10190 /* If we saw `typename', or didn't see `::', then there must be a
10191 nested-name-specifier present. */
10192 if (typename_p || !global_scope_p)
21526606 10193 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10194 /*check_dependency_p=*/true,
10195 /*type_p=*/false,
10196 /*is_declaration=*/true);
a723baf1
MM
10197 /* Otherwise, we could be in either of the two productions. In that
10198 case, treat the nested-name-specifier as optional. */
10199 else
ed5f054f
AO
10200 qscope = cp_parser_nested_name_specifier_opt (parser,
10201 /*typename_keyword_p=*/false,
10202 /*check_dependency_p=*/true,
10203 /*type_p=*/false,
10204 /*is_declaration=*/true);
10205 if (!qscope)
10206 qscope = global_namespace;
a723baf1
MM
10207
10208 /* Parse the unqualified-id. */
21526606 10209 identifier = cp_parser_unqualified_id (parser,
a723baf1 10210 /*template_keyword_p=*/false,
f3c2dfc6
MM
10211 /*check_dependency_p=*/true,
10212 /*declarator_p=*/true);
a723baf1
MM
10213
10214 /* The function we call to handle a using-declaration is different
10215 depending on what scope we are in. */
f3c2dfc6
MM
10216 if (identifier == error_mark_node)
10217 ;
10218 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10219 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10220 /* [namespace.udecl]
10221
10222 A using declaration shall not name a template-id. */
10223 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10224 else
10225 {
f3c2dfc6
MM
10226 scope = current_scope ();
10227 if (scope && TYPE_P (scope))
4eb6d609 10228 {
f3c2dfc6
MM
10229 /* Create the USING_DECL. */
10230 decl = do_class_using_decl (build_nt (SCOPE_REF,
10231 parser->scope,
10232 identifier));
10233 /* Add it to the list of members in this class. */
10234 finish_member_declaration (decl);
4eb6d609 10235 }
a723baf1 10236 else
f3c2dfc6
MM
10237 {
10238 decl = cp_parser_lookup_name_simple (parser, identifier);
10239 if (decl == error_mark_node)
4bb8ca28 10240 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
f3c2dfc6 10241 else if (scope)
ed5f054f 10242 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10243 else
ed5f054f 10244 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10245 }
a723baf1
MM
10246 }
10247
10248 /* Look for the final `;'. */
10249 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10250}
10251
21526606
EC
10252/* Parse a using-directive.
10253
a723baf1
MM
10254 using-directive:
10255 using namespace :: [opt] nested-name-specifier [opt]
10256 namespace-name ; */
10257
10258static void
94edc4ab 10259cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10260{
10261 tree namespace_decl;
86098eb8 10262 tree attribs;
a723baf1
MM
10263
10264 /* Look for the `using' keyword. */
10265 cp_parser_require_keyword (parser, RID_USING, "`using'");
10266 /* And the `namespace' keyword. */
10267 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10268 /* Look for the optional `::' operator. */
10269 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10270 /* And the optional nested-name-specifier. */
a723baf1
MM
10271 cp_parser_nested_name_specifier_opt (parser,
10272 /*typename_keyword_p=*/false,
10273 /*check_dependency_p=*/true,
a668c6ad
MM
10274 /*type_p=*/false,
10275 /*is_declaration=*/true);
a723baf1
MM
10276 /* Get the namespace being used. */
10277 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10278 /* And any specified attributes. */
10279 attribs = cp_parser_attributes_opt (parser);
a723baf1 10280 /* Update the symbol table. */
86098eb8 10281 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10282 /* Look for the final `;'. */
10283 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10284}
10285
10286/* Parse an asm-definition.
10287
10288 asm-definition:
21526606 10289 asm ( string-literal ) ;
a723baf1
MM
10290
10291 GNU Extension:
10292
10293 asm-definition:
10294 asm volatile [opt] ( string-literal ) ;
10295 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10296 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10297 : asm-operand-list [opt] ) ;
21526606
EC
10298 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10299 : asm-operand-list [opt]
a723baf1
MM
10300 : asm-operand-list [opt] ) ; */
10301
10302static void
94edc4ab 10303cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
10304{
10305 cp_token *token;
10306 tree string;
10307 tree outputs = NULL_TREE;
10308 tree inputs = NULL_TREE;
10309 tree clobbers = NULL_TREE;
10310 tree asm_stmt;
10311 bool volatile_p = false;
10312 bool extended_p = false;
10313
10314 /* Look for the `asm' keyword. */
10315 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10316 /* See if the next token is `volatile'. */
10317 if (cp_parser_allow_gnu_extensions_p (parser)
10318 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10319 {
10320 /* Remember that we saw the `volatile' keyword. */
10321 volatile_p = true;
10322 /* Consume the token. */
10323 cp_lexer_consume_token (parser->lexer);
10324 }
10325 /* Look for the opening `('. */
10326 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10327 /* Look for the string. */
0173bb6f 10328 c_lex_string_translate = 0;
a723baf1
MM
10329 token = cp_parser_require (parser, CPP_STRING, "asm body");
10330 if (!token)
21526606 10331 goto finish;
a723baf1
MM
10332 string = token->value;
10333 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10334 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10335 a space in C, and so, for compatibility, we tolerate that here
10336 too. Doing that means that we have to treat the `::' operator as
10337 two `:' tokens. */
10338 if (cp_parser_allow_gnu_extensions_p (parser)
10339 && at_function_scope_p ()
10340 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10341 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10342 {
10343 bool inputs_p = false;
10344 bool clobbers_p = false;
10345
10346 /* The extended syntax was used. */
10347 extended_p = true;
10348
10349 /* Look for outputs. */
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10351 {
10352 /* Consume the `:'. */
10353 cp_lexer_consume_token (parser->lexer);
10354 /* Parse the output-operands. */
21526606 10355 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10356 CPP_COLON)
10357 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10358 CPP_SCOPE)
10359 && cp_lexer_next_token_is_not (parser->lexer,
10360 CPP_CLOSE_PAREN))
a723baf1
MM
10361 outputs = cp_parser_asm_operand_list (parser);
10362 }
10363 /* If the next token is `::', there are no outputs, and the
10364 next token is the beginning of the inputs. */
10365 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10366 {
10367 /* Consume the `::' token. */
10368 cp_lexer_consume_token (parser->lexer);
10369 /* The inputs are coming next. */
10370 inputs_p = true;
10371 }
10372
10373 /* Look for inputs. */
10374 if (inputs_p
10375 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10376 {
10377 if (!inputs_p)
10378 /* Consume the `:'. */
10379 cp_lexer_consume_token (parser->lexer);
10380 /* Parse the output-operands. */
21526606 10381 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10382 CPP_COLON)
10383 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10384 CPP_SCOPE)
10385 && cp_lexer_next_token_is_not (parser->lexer,
10386 CPP_CLOSE_PAREN))
a723baf1
MM
10387 inputs = cp_parser_asm_operand_list (parser);
10388 }
10389 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10390 /* The clobbers are coming next. */
10391 clobbers_p = true;
10392
10393 /* Look for clobbers. */
21526606 10394 if (clobbers_p
a723baf1
MM
10395 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10396 {
10397 if (!clobbers_p)
10398 /* Consume the `:'. */
10399 cp_lexer_consume_token (parser->lexer);
10400 /* Parse the clobbers. */
8caf4c38
MM
10401 if (cp_lexer_next_token_is_not (parser->lexer,
10402 CPP_CLOSE_PAREN))
10403 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10404 }
10405 }
10406 /* Look for the closing `)'. */
10407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10408 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10409 /*consume_paren=*/true);
a723baf1
MM
10410 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10411
e130a54b 10412 /* Create the ASM_EXPR. */
a723baf1
MM
10413 if (at_function_scope_p ())
10414 {
6de9cd9a
DN
10415 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10416 inputs, clobbers);
e130a54b 10417 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1
MM
10418 if (!extended_p)
10419 ASM_INPUT_P (asm_stmt) = 1;
10420 }
10421 else
10422 assemble_asm (string);
21526606
EC
10423
10424 finish:
0173bb6f 10425 c_lex_string_translate = 1;
a723baf1
MM
10426}
10427
10428/* Declarators [gram.dcl.decl] */
10429
10430/* Parse an init-declarator.
10431
10432 init-declarator:
10433 declarator initializer [opt]
10434
10435 GNU Extension:
10436
10437 init-declarator:
10438 declarator asm-specification [opt] attributes [opt] initializer [opt]
10439
4bb8ca28
MM
10440 function-definition:
10441 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10442 function-body
10443 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10444
10445 GNU Extension:
10446
10447 function-definition:
21526606 10448 __extension__ function-definition
4bb8ca28 10449
a723baf1 10450 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10451 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10452 then this declarator appears in a class scope. The new DECL created
10453 by this declarator is returned.
a723baf1
MM
10454
10455 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10456 for a function-definition here as well. If the declarator is a
10457 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10458 be TRUE upon return. By that point, the function-definition will
10459 have been completely parsed.
10460
10461 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10462 is FALSE. */
10463
10464static tree
21526606 10465cp_parser_init_declarator (cp_parser* parser,
62d1db17 10466 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10467 bool function_definition_allowed_p,
10468 bool member_p,
560ad596 10469 int declares_class_or_enum,
94edc4ab 10470 bool* function_definition_p)
a723baf1
MM
10471{
10472 cp_token *token;
058b15c1 10473 cp_declarator *declarator;
62d1db17 10474 tree prefix_attributes;
a723baf1
MM
10475 tree attributes;
10476 tree asm_specification;
10477 tree initializer;
10478 tree decl = NULL_TREE;
10479 tree scope;
a723baf1
MM
10480 bool is_initialized;
10481 bool is_parenthesized_init;
39703eb9 10482 bool is_non_constant_init;
7efa3e22 10483 int ctor_dtor_or_conv_p;
a723baf1 10484 bool friend_p;
91b004e5 10485 bool pop_p = false;
a723baf1 10486
62d1db17
MM
10487 /* Gather the attributes that were provided with the
10488 decl-specifiers. */
10489 prefix_attributes = decl_specifiers->attributes;
10490 decl_specifiers->attributes = NULL_TREE;
10491
a723baf1
MM
10492 /* Assume that this is not the declarator for a function
10493 definition. */
10494 if (function_definition_p)
10495 *function_definition_p = false;
10496
10497 /* Defer access checks while parsing the declarator; we cannot know
21526606 10498 what names are accessible until we know what is being
a723baf1 10499 declared. */
cf22909c
KL
10500 resume_deferring_access_checks ();
10501
a723baf1 10502 /* Parse the declarator. */
21526606 10503 declarator
62b8a44e 10504 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
10505 &ctor_dtor_or_conv_p,
10506 /*parenthesized_p=*/NULL);
a723baf1 10507 /* Gather up the deferred checks. */
cf22909c 10508 stop_deferring_access_checks ();
24c0ef37 10509
a723baf1
MM
10510 /* If the DECLARATOR was erroneous, there's no need to go
10511 further. */
058b15c1 10512 if (declarator == cp_error_declarator)
cf22909c 10513 return error_mark_node;
a723baf1 10514
560ad596
MM
10515 cp_parser_check_for_definition_in_return_type (declarator,
10516 declares_class_or_enum);
10517
a723baf1
MM
10518 /* Figure out what scope the entity declared by the DECLARATOR is
10519 located in. `grokdeclarator' sometimes changes the scope, so
10520 we compute it now. */
10521 scope = get_scope_of_declarator (declarator);
10522
10523 /* If we're allowing GNU extensions, look for an asm-specification
10524 and attributes. */
10525 if (cp_parser_allow_gnu_extensions_p (parser))
10526 {
10527 /* Look for an asm-specification. */
10528 asm_specification = cp_parser_asm_specification_opt (parser);
10529 /* And attributes. */
10530 attributes = cp_parser_attributes_opt (parser);
10531 }
10532 else
10533 {
10534 asm_specification = NULL_TREE;
10535 attributes = NULL_TREE;
10536 }
10537
10538 /* Peek at the next token. */
10539 token = cp_lexer_peek_token (parser->lexer);
10540 /* Check to see if the token indicates the start of a
10541 function-definition. */
10542 if (cp_parser_token_starts_function_definition_p (token))
10543 {
10544 if (!function_definition_allowed_p)
10545 {
10546 /* If a function-definition should not appear here, issue an
10547 error message. */
10548 cp_parser_error (parser,
10549 "a function-definition is not allowed here");
10550 return error_mark_node;
10551 }
10552 else
10553 {
a723baf1
MM
10554 /* Neither attributes nor an asm-specification are allowed
10555 on a function-definition. */
10556 if (asm_specification)
10557 error ("an asm-specification is not allowed on a function-definition");
10558 if (attributes)
10559 error ("attributes are not allowed on a function-definition");
10560 /* This is a function-definition. */
10561 *function_definition_p = true;
10562
a723baf1 10563 /* Parse the function definition. */
4bb8ca28
MM
10564 if (member_p)
10565 decl = cp_parser_save_member_function_body (parser,
10566 decl_specifiers,
10567 declarator,
10568 prefix_attributes);
10569 else
21526606 10570 decl
4bb8ca28
MM
10571 = (cp_parser_function_definition_from_specifiers_and_declarator
10572 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10573
a723baf1
MM
10574 return decl;
10575 }
10576 }
10577
10578 /* [dcl.dcl]
10579
10580 Only in function declarations for constructors, destructors, and
21526606 10581 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10582
10583 We explicitly postpone this check past the point where we handle
10584 function-definitions because we tolerate function-definitions
10585 that are missing their return types in some modes. */
62d1db17 10586 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10587 {
21526606 10588 cp_parser_error (parser,
a723baf1
MM
10589 "expected constructor, destructor, or type conversion");
10590 return error_mark_node;
10591 }
10592
10593 /* An `=' or an `(' indicates an initializer. */
21526606 10594 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10595 || token->type == CPP_OPEN_PAREN);
10596 /* If the init-declarator isn't initialized and isn't followed by a
10597 `,' or `;', it's not a valid init-declarator. */
21526606 10598 if (!is_initialized
a723baf1
MM
10599 && token->type != CPP_COMMA
10600 && token->type != CPP_SEMICOLON)
10601 {
10602 cp_parser_error (parser, "expected init-declarator");
10603 return error_mark_node;
10604 }
10605
10606 /* Because start_decl has side-effects, we should only call it if we
10607 know we're going ahead. By this point, we know that we cannot
10608 possibly be looking at any other construct. */
10609 cp_parser_commit_to_tentative_parse (parser);
10610
e90c7b84
ILT
10611 /* If the decl specifiers were bad, issue an error now that we're
10612 sure this was intended to be a declarator. Then continue
10613 declaring the variable(s), as int, to try to cut down on further
10614 errors. */
62d1db17
MM
10615 if (decl_specifiers->any_specifiers_p
10616 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10617 {
10618 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10619 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10620 }
10621
a723baf1
MM
10622 /* Check to see whether or not this declaration is a friend. */
10623 friend_p = cp_parser_friend_p (decl_specifiers);
10624
10625 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10626 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10627 return error_mark_node;
a723baf1
MM
10628
10629 /* Enter the newly declared entry in the symbol table. If we're
10630 processing a declaration in a class-specifier, we wait until
10631 after processing the initializer. */
10632 if (!member_p)
10633 {
10634 if (parser->in_unbraced_linkage_specification_p)
10635 {
62d1db17 10636 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10637 have_extern_spec = false;
10638 }
ee3071ef
NS
10639 decl = start_decl (declarator, decl_specifiers,
10640 is_initialized, attributes, prefix_attributes);
a723baf1
MM
10641 }
10642
10643 /* Enter the SCOPE. That way unqualified names appearing in the
10644 initializer will be looked up in SCOPE. */
10645 if (scope)
91b004e5 10646 pop_p = push_scope (scope);
a723baf1
MM
10647
10648 /* Perform deferred access control checks, now that we know in which
10649 SCOPE the declared entity resides. */
21526606 10650 if (!member_p && decl)
a723baf1
MM
10651 {
10652 tree saved_current_function_decl = NULL_TREE;
10653
10654 /* If the entity being declared is a function, pretend that we
10655 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10656 things that would not otherwise be accessible. */
a723baf1
MM
10657 if (TREE_CODE (decl) == FUNCTION_DECL)
10658 {
10659 saved_current_function_decl = current_function_decl;
10660 current_function_decl = decl;
10661 }
21526606 10662
cf22909c
KL
10663 /* Perform the access control checks for the declarator and the
10664 the decl-specifiers. */
10665 perform_deferred_access_checks ();
a723baf1
MM
10666
10667 /* Restore the saved value. */
10668 if (TREE_CODE (decl) == FUNCTION_DECL)
10669 current_function_decl = saved_current_function_decl;
10670 }
10671
10672 /* Parse the initializer. */
10673 if (is_initialized)
21526606 10674 initializer = cp_parser_initializer (parser,
39703eb9
MM
10675 &is_parenthesized_init,
10676 &is_non_constant_init);
a723baf1
MM
10677 else
10678 {
10679 initializer = NULL_TREE;
10680 is_parenthesized_init = false;
39703eb9 10681 is_non_constant_init = true;
a723baf1
MM
10682 }
10683
10684 /* The old parser allows attributes to appear after a parenthesized
10685 initializer. Mark Mitchell proposed removing this functionality
10686 on the GCC mailing lists on 2002-08-13. This parser accepts the
10687 attributes -- but ignores them. */
10688 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10689 if (cp_parser_attributes_opt (parser))
10690 warning ("attributes after parenthesized initializer ignored");
10691
10692 /* Leave the SCOPE, now that we have processed the initializer. It
10693 is important to do this before calling cp_finish_decl because it
10694 makes decisions about whether to create DECL_STMTs or not based
10695 on the current scope. */
91b004e5 10696 if (pop_p)
a723baf1
MM
10697 pop_scope (scope);
10698
10699 /* For an in-class declaration, use `grokfield' to create the
10700 declaration. */
10701 if (member_p)
8db1028e
NS
10702 {
10703 decl = grokfield (declarator, decl_specifiers,
10704 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10705 /*attributes=*/NULL_TREE);
8db1028e
NS
10706 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10707 cp_parser_save_default_args (parser, decl);
10708 }
21526606 10709
a723baf1
MM
10710 /* Finish processing the declaration. But, skip friend
10711 declarations. */
10712 if (!friend_p && decl)
21526606
EC
10713 cp_finish_decl (decl,
10714 initializer,
a723baf1
MM
10715 asm_specification,
10716 /* If the initializer is in parentheses, then this is
10717 a direct-initialization, which means that an
10718 `explicit' constructor is OK. Otherwise, an
10719 `explicit' constructor cannot be used. */
10720 ((is_parenthesized_init || !is_initialized)
10721 ? 0 : LOOKUP_ONLYCONVERTING));
10722
39703eb9
MM
10723 /* Remember whether or not variables were initialized by
10724 constant-expressions. */
21526606 10725 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10726 && is_initialized && !is_non_constant_init)
10727 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10728
a723baf1
MM
10729 return decl;
10730}
10731
10732/* Parse a declarator.
21526606 10733
a723baf1
MM
10734 declarator:
10735 direct-declarator
21526606 10736 ptr-operator declarator
a723baf1
MM
10737
10738 abstract-declarator:
10739 ptr-operator abstract-declarator [opt]
10740 direct-abstract-declarator
10741
10742 GNU Extensions:
10743
10744 declarator:
10745 attributes [opt] direct-declarator
21526606 10746 attributes [opt] ptr-operator declarator
a723baf1
MM
10747
10748 abstract-declarator:
10749 attributes [opt] ptr-operator abstract-declarator [opt]
10750 attributes [opt] direct-abstract-declarator
21526606 10751
7efa3e22
NS
10752 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10753 detect constructor, destructor or conversion operators. It is set
10754 to -1 if the declarator is a name, and +1 if it is a
10755 function. Otherwise it is set to zero. Usually you just want to
10756 test for >0, but internally the negative value is used.
21526606 10757
a723baf1
MM
10758 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10759 a decl-specifier-seq unless it declares a constructor, destructor,
10760 or conversion. It might seem that we could check this condition in
10761 semantic analysis, rather than parsing, but that makes it difficult
10762 to handle something like `f()'. We want to notice that there are
10763 no decl-specifiers, and therefore realize that this is an
21526606
EC
10764 expression, not a declaration.)
10765
4bb8ca28
MM
10766 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10767 the declarator is a direct-declarator of the form "(...)". */
a723baf1 10768
058b15c1 10769static cp_declarator *
21526606
EC
10770cp_parser_declarator (cp_parser* parser,
10771 cp_parser_declarator_kind dcl_kind,
4bb8ca28
MM
10772 int* ctor_dtor_or_conv_p,
10773 bool* parenthesized_p)
a723baf1
MM
10774{
10775 cp_token *token;
058b15c1 10776 cp_declarator *declarator;
a723baf1
MM
10777 enum tree_code code;
10778 tree cv_qualifier_seq;
10779 tree class_type;
10780 tree attributes = NULL_TREE;
10781
10782 /* Assume this is not a constructor, destructor, or type-conversion
10783 operator. */
10784 if (ctor_dtor_or_conv_p)
7efa3e22 10785 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10786
10787 if (cp_parser_allow_gnu_extensions_p (parser))
10788 attributes = cp_parser_attributes_opt (parser);
21526606 10789
a723baf1
MM
10790 /* Peek at the next token. */
10791 token = cp_lexer_peek_token (parser->lexer);
21526606 10792
a723baf1
MM
10793 /* Check for the ptr-operator production. */
10794 cp_parser_parse_tentatively (parser);
10795 /* Parse the ptr-operator. */
21526606
EC
10796 code = cp_parser_ptr_operator (parser,
10797 &class_type,
a723baf1
MM
10798 &cv_qualifier_seq);
10799 /* If that worked, then we have a ptr-operator. */
10800 if (cp_parser_parse_definitely (parser))
10801 {
4bb8ca28
MM
10802 /* If a ptr-operator was found, then this declarator was not
10803 parenthesized. */
10804 if (parenthesized_p)
10805 *parenthesized_p = true;
a723baf1
MM
10806 /* The dependent declarator is optional if we are parsing an
10807 abstract-declarator. */
62b8a44e 10808 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10809 cp_parser_parse_tentatively (parser);
10810
10811 /* Parse the dependent declarator. */
62b8a44e 10812 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28
MM
10813 /*ctor_dtor_or_conv_p=*/NULL,
10814 /*parenthesized_p=*/NULL);
a723baf1
MM
10815
10816 /* If we are parsing an abstract-declarator, we must handle the
10817 case where the dependent declarator is absent. */
62b8a44e
NS
10818 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10819 && !cp_parser_parse_definitely (parser))
058b15c1 10820 declarator = NULL;
21526606 10821
a723baf1 10822 /* Build the representation of the ptr-operator. */
058b15c1
MM
10823 if (class_type)
10824 declarator = make_ptrmem_declarator (cv_qualifier_seq,
10825 class_type,
10826 declarator);
10827 else if (code == INDIRECT_REF)
21526606 10828 declarator = make_pointer_declarator (cv_qualifier_seq,
a723baf1
MM
10829 declarator);
10830 else
10831 declarator = make_reference_declarator (cv_qualifier_seq,
10832 declarator);
a723baf1
MM
10833 }
10834 /* Everything else is a direct-declarator. */
10835 else
4bb8ca28
MM
10836 {
10837 if (parenthesized_p)
10838 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10839 CPP_OPEN_PAREN);
10840 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10841 ctor_dtor_or_conv_p);
10842 }
a723baf1 10843
058b15c1
MM
10844 if (attributes && declarator != cp_error_declarator)
10845 declarator->attributes = attributes;
21526606 10846
a723baf1
MM
10847 return declarator;
10848}
10849
10850/* Parse a direct-declarator or direct-abstract-declarator.
10851
10852 direct-declarator:
10853 declarator-id
10854 direct-declarator ( parameter-declaration-clause )
21526606 10855 cv-qualifier-seq [opt]
a723baf1
MM
10856 exception-specification [opt]
10857 direct-declarator [ constant-expression [opt] ]
21526606 10858 ( declarator )
a723baf1
MM
10859
10860 direct-abstract-declarator:
10861 direct-abstract-declarator [opt]
21526606 10862 ( parameter-declaration-clause )
a723baf1
MM
10863 cv-qualifier-seq [opt]
10864 exception-specification [opt]
10865 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10866 ( abstract-declarator )
10867
62b8a44e
NS
10868 Returns a representation of the declarator. DCL_KIND is
10869 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10870 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10871 we are parsing a direct-declarator. It is
10872 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10873 of ambiguity we prefer an abstract declarator, as per
10874 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
058b15c1 10875 cp_parser_declarator. */
a723baf1 10876
058b15c1 10877static cp_declarator *
94edc4ab
NN
10878cp_parser_direct_declarator (cp_parser* parser,
10879 cp_parser_declarator_kind dcl_kind,
7efa3e22 10880 int* ctor_dtor_or_conv_p)
a723baf1
MM
10881{
10882 cp_token *token;
058b15c1 10883 cp_declarator *declarator = NULL;
a723baf1
MM
10884 tree scope = NULL_TREE;
10885 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10886 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10887 bool first = true;
91b004e5 10888 bool pop_p = false;
21526606 10889
62b8a44e 10890 while (true)
a723baf1 10891 {
62b8a44e
NS
10892 /* Peek at the next token. */
10893 token = cp_lexer_peek_token (parser->lexer);
10894 if (token->type == CPP_OPEN_PAREN)
a723baf1 10895 {
62b8a44e
NS
10896 /* This is either a parameter-declaration-clause, or a
10897 parenthesized declarator. When we know we are parsing a
34cd5ae7 10898 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10899 if FIRST is true. For instance, `(int)' is a
10900 parameter-declaration-clause, with an omitted
10901 direct-abstract-declarator. But `((*))', is a
10902 parenthesized abstract declarator. Finally, when T is a
10903 template parameter `(T)' is a
34cd5ae7 10904 parameter-declaration-clause, and not a parenthesized
62b8a44e 10905 named declarator.
21526606 10906
62b8a44e
NS
10907 We first try and parse a parameter-declaration-clause,
10908 and then try a nested declarator (if FIRST is true).
a723baf1 10909
62b8a44e
NS
10910 It is not an error for it not to be a
10911 parameter-declaration-clause, even when FIRST is
10912 false. Consider,
10913
10914 int i (int);
10915 int i (3);
10916
10917 The first is the declaration of a function while the
10918 second is a the definition of a variable, including its
10919 initializer.
10920
10921 Having seen only the parenthesis, we cannot know which of
10922 these two alternatives should be selected. Even more
10923 complex are examples like:
10924
10925 int i (int (a));
10926 int i (int (3));
10927
10928 The former is a function-declaration; the latter is a
21526606 10929 variable initialization.
62b8a44e 10930
34cd5ae7 10931 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10932 that fails, we back out and return. */
10933
10934 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10935 {
058b15c1 10936 cp_parameter_declarator *params;
4047b164 10937 unsigned saved_num_template_parameter_lists;
21526606 10938
62b8a44e 10939 cp_parser_parse_tentatively (parser);
a723baf1 10940
62b8a44e
NS
10941 /* Consume the `('. */
10942 cp_lexer_consume_token (parser->lexer);
10943 if (first)
10944 {
10945 /* If this is going to be an abstract declarator, we're
10946 in a declarator and we can't have default args. */
10947 parser->default_arg_ok_p = false;
10948 parser->in_declarator_p = true;
10949 }
21526606 10950
4047b164
KL
10951 /* Inside the function parameter list, surrounding
10952 template-parameter-lists do not apply. */
10953 saved_num_template_parameter_lists
10954 = parser->num_template_parameter_lists;
10955 parser->num_template_parameter_lists = 0;
10956
62b8a44e
NS
10957 /* Parse the parameter-declaration-clause. */
10958 params = cp_parser_parameter_declaration_clause (parser);
10959
4047b164
KL
10960 parser->num_template_parameter_lists
10961 = saved_num_template_parameter_lists;
10962
62b8a44e 10963 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10964 exception-specification. */
62b8a44e
NS
10965 if (cp_parser_parse_definitely (parser))
10966 {
10967 tree cv_qualifiers;
10968 tree exception_specification;
7efa3e22
NS
10969
10970 if (ctor_dtor_or_conv_p)
10971 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10972 first = false;
10973 /* Consume the `)'. */
10974 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10975
10976 /* Parse the cv-qualifier-seq. */
10977 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10978 /* And the exception-specification. */
21526606 10979 exception_specification
62b8a44e
NS
10980 = cp_parser_exception_specification_opt (parser);
10981
10982 /* Create the function-declarator. */
10983 declarator = make_call_declarator (declarator,
10984 params,
10985 cv_qualifiers,
10986 exception_specification);
10987 /* Any subsequent parameter lists are to do with
10988 return type, so are not those of the declared
10989 function. */
10990 parser->default_arg_ok_p = false;
21526606 10991
62b8a44e
NS
10992 /* Repeat the main loop. */
10993 continue;
10994 }
10995 }
21526606 10996
62b8a44e
NS
10997 /* If this is the first, we can try a parenthesized
10998 declarator. */
10999 if (first)
a723baf1 11000 {
a7324e75
MM
11001 bool saved_in_type_id_in_expr_p;
11002
a723baf1 11003 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 11004 parser->in_declarator_p = saved_in_declarator_p;
21526606 11005
62b8a44e
NS
11006 /* Consume the `('. */
11007 cp_lexer_consume_token (parser->lexer);
11008 /* Parse the nested declarator. */
a7324e75
MM
11009 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11010 parser->in_type_id_in_expr_p = true;
21526606 11011 declarator
4bb8ca28
MM
11012 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11013 /*parenthesized_p=*/NULL);
a7324e75 11014 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
11015 first = false;
11016 /* Expect a `)'. */
11017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
11018 declarator = cp_error_declarator;
11019 if (declarator == cp_error_declarator)
62b8a44e 11020 break;
21526606 11021
62b8a44e 11022 goto handle_declarator;
a723baf1 11023 }
9bcb9aae 11024 /* Otherwise, we must be done. */
62b8a44e
NS
11025 else
11026 break;
a723baf1 11027 }
62b8a44e
NS
11028 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11029 && token->type == CPP_OPEN_SQUARE)
a723baf1 11030 {
62b8a44e 11031 /* Parse an array-declarator. */
a723baf1
MM
11032 tree bounds;
11033
7efa3e22
NS
11034 if (ctor_dtor_or_conv_p)
11035 *ctor_dtor_or_conv_p = 0;
21526606 11036
62b8a44e
NS
11037 first = false;
11038 parser->default_arg_ok_p = false;
11039 parser->in_declarator_p = true;
a723baf1
MM
11040 /* Consume the `['. */
11041 cp_lexer_consume_token (parser->lexer);
11042 /* Peek at the next token. */
11043 token = cp_lexer_peek_token (parser->lexer);
11044 /* If the next token is `]', then there is no
11045 constant-expression. */
11046 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
11047 {
11048 bool non_constant_p;
11049
21526606 11050 bounds
14d22dd6
MM
11051 = cp_parser_constant_expression (parser,
11052 /*allow_non_constant=*/true,
11053 &non_constant_p);
d17811fd 11054 if (!non_constant_p)
9baa27a9 11055 bounds = fold_non_dependent_expr (bounds);
14d22dd6 11056 }
a723baf1
MM
11057 else
11058 bounds = NULL_TREE;
11059 /* Look for the closing `]'. */
62b8a44e
NS
11060 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11061 {
058b15c1 11062 declarator = cp_error_declarator;
62b8a44e
NS
11063 break;
11064 }
a723baf1 11065
058b15c1 11066 declarator = make_array_declarator (declarator, bounds);
a723baf1 11067 }
62b8a44e 11068 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11069 {
058b15c1
MM
11070 tree id;
11071
a668c6ad 11072 /* Parse a declarator-id */
62b8a44e
NS
11073 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11074 cp_parser_parse_tentatively (parser);
058b15c1 11075 id = cp_parser_declarator_id (parser);
712becab
NS
11076 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11077 {
11078 if (!cp_parser_parse_definitely (parser))
058b15c1
MM
11079 id = error_mark_node;
11080 else if (TREE_CODE (id) != IDENTIFIER_NODE)
712becab
NS
11081 {
11082 cp_parser_error (parser, "expected unqualified-id");
058b15c1 11083 id = error_mark_node;
712becab
NS
11084 }
11085 }
21526606 11086
058b15c1
MM
11087 if (id == error_mark_node)
11088 {
11089 declarator = cp_error_declarator;
11090 break;
11091 }
21526606 11092
058b15c1 11093 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
62b8a44e 11094 {
058b15c1 11095 tree scope = TREE_OPERAND (id, 0);
712becab 11096
62b8a44e
NS
11097 /* In the declaration of a member of a template class
11098 outside of the class itself, the SCOPE will sometimes
11099 be a TYPENAME_TYPE. For example, given:
21526606 11100
62b8a44e
NS
11101 template <typename T>
11102 int S<T>::R::i = 3;
21526606 11103
62b8a44e
NS
11104 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11105 this context, we must resolve S<T>::R to an ordinary
11106 type, rather than a typename type.
21526606 11107
62b8a44e
NS
11108 The reason we normally avoid resolving TYPENAME_TYPEs
11109 is that a specialization of `S' might render
11110 `S<T>::R' not a type. However, if `S' is
11111 specialized, then this `i' will not be used, so there
11112 is no harm in resolving the types here. */
11113 if (TREE_CODE (scope) == TYPENAME_TYPE)
11114 {
14d22dd6
MM
11115 tree type;
11116
62b8a44e 11117 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
11118 type = resolve_typename_type (scope,
11119 /*only_current_p=*/false);
62b8a44e 11120 /* If that failed, the declarator is invalid. */
109e0040
MM
11121 if (type == error_mark_node)
11122 error ("`%T::%D' is not a type",
11123 TYPE_CONTEXT (scope),
11124 TYPE_IDENTIFIER (scope));
62b8a44e 11125 /* Build a new DECLARATOR. */
058b15c1 11126 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
62b8a44e
NS
11127 }
11128 }
21526606 11129
058b15c1
MM
11130 declarator = make_id_declarator (id);
11131 if (id)
a723baf1 11132 {
62b8a44e 11133 tree class_type;
058b15c1 11134 tree unqualified_name;
62b8a44e 11135
058b15c1
MM
11136 if (TREE_CODE (id) == SCOPE_REF
11137 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
62b8a44e 11138 {
058b15c1
MM
11139 class_type = TREE_OPERAND (id, 0);
11140 unqualified_name = TREE_OPERAND (id, 1);
62b8a44e
NS
11141 }
11142 else
11143 {
11144 class_type = current_class_type;
058b15c1 11145 unqualified_name = id;
62b8a44e
NS
11146 }
11147
058b15c1
MM
11148 if (class_type)
11149 {
11150 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11151 declarator->u.id.sfk = sfk_destructor;
11152 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11153 declarator->u.id.sfk = sfk_conversion;
11154 else if (constructor_name_p (unqualified_name,
11155 class_type)
11156 || (TREE_CODE (unqualified_name) == TYPE_DECL
11157 && same_type_p (TREE_TYPE (unqualified_name),
11158 class_type)))
11159 declarator->u.id.sfk = sfk_constructor;
11160
11161 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11162 *ctor_dtor_or_conv_p = -1;
11163 if (TREE_CODE (id) == SCOPE_REF
11164 && TREE_CODE (unqualified_name) == TYPE_DECL
11165 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11166 {
11167 error ("invalid use of constructor as a template");
11168 inform ("use `%T::%D' instead of `%T::%T' to name the "
11169 "constructor in a qualified name", class_type,
11170 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11171 class_type, class_type);
11172 }
11173 }
a723baf1 11174 }
62b8a44e
NS
11175
11176 handle_declarator:;
11177 scope = get_scope_of_declarator (declarator);
11178 if (scope)
91b004e5
MM
11179 /* Any names that appear after the declarator-id for a
11180 member are looked up in the containing scope. */
11181 pop_p = push_scope (scope);
62b8a44e
NS
11182 parser->in_declarator_p = true;
11183 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11184 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11185 /* Default args are only allowed on function
11186 declarations. */
11187 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11188 else
62b8a44e
NS
11189 parser->default_arg_ok_p = false;
11190
11191 first = false;
a723baf1 11192 }
62b8a44e 11193 /* We're done. */
a723baf1
MM
11194 else
11195 break;
a723baf1
MM
11196 }
11197
11198 /* For an abstract declarator, we might wind up with nothing at this
11199 point. That's an error; the declarator is not optional. */
11200 if (!declarator)
11201 cp_parser_error (parser, "expected declarator");
11202
11203 /* If we entered a scope, we must exit it now. */
91b004e5 11204 if (pop_p)
a723baf1
MM
11205 pop_scope (scope);
11206
11207 parser->default_arg_ok_p = saved_default_arg_ok_p;
11208 parser->in_declarator_p = saved_in_declarator_p;
21526606 11209
a723baf1
MM
11210 return declarator;
11211}
11212
21526606 11213/* Parse a ptr-operator.
a723baf1
MM
11214
11215 ptr-operator:
11216 * cv-qualifier-seq [opt]
11217 &
11218 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11219
11220 GNU Extension:
11221
11222 ptr-operator:
11223 & cv-qualifier-seq [opt]
11224
11225 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
11226 used. Returns ADDR_EXPR if a reference was used. In the
21526606 11227 case of a pointer-to-member, *TYPE is filled in with the
a723baf1
MM
11228 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
11229 with the cv-qualifier-seq, or NULL_TREE, if there are no
11230 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
21526606 11231
a723baf1 11232static enum tree_code
21526606
EC
11233cp_parser_ptr_operator (cp_parser* parser,
11234 tree* type,
94edc4ab 11235 tree* cv_qualifier_seq)
a723baf1
MM
11236{
11237 enum tree_code code = ERROR_MARK;
11238 cp_token *token;
11239
11240 /* Assume that it's not a pointer-to-member. */
11241 *type = NULL_TREE;
11242 /* And that there are no cv-qualifiers. */
11243 *cv_qualifier_seq = NULL_TREE;
11244
11245 /* Peek at the next token. */
11246 token = cp_lexer_peek_token (parser->lexer);
11247 /* If it's a `*' or `&' we have a pointer or reference. */
11248 if (token->type == CPP_MULT || token->type == CPP_AND)
11249 {
11250 /* Remember which ptr-operator we were processing. */
11251 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11252
11253 /* Consume the `*' or `&'. */
11254 cp_lexer_consume_token (parser->lexer);
11255
11256 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11257 `&', if we are allowing GNU extensions. (The only qualifier
11258 that can legally appear after `&' is `restrict', but that is
11259 enforced during semantic analysis. */
21526606 11260 if (code == INDIRECT_REF
a723baf1
MM
11261 || cp_parser_allow_gnu_extensions_p (parser))
11262 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11263 }
11264 else
11265 {
11266 /* Try the pointer-to-member case. */
11267 cp_parser_parse_tentatively (parser);
11268 /* Look for the optional `::' operator. */
11269 cp_parser_global_scope_opt (parser,
11270 /*current_scope_valid_p=*/false);
11271 /* Look for the nested-name specifier. */
11272 cp_parser_nested_name_specifier (parser,
11273 /*typename_keyword_p=*/false,
11274 /*check_dependency_p=*/true,
a668c6ad
MM
11275 /*type_p=*/false,
11276 /*is_declaration=*/false);
a723baf1
MM
11277 /* If we found it, and the next token is a `*', then we are
11278 indeed looking at a pointer-to-member operator. */
11279 if (!cp_parser_error_occurred (parser)
11280 && cp_parser_require (parser, CPP_MULT, "`*'"))
11281 {
11282 /* The type of which the member is a member is given by the
11283 current SCOPE. */
11284 *type = parser->scope;
11285 /* The next name will not be qualified. */
11286 parser->scope = NULL_TREE;
11287 parser->qualifying_scope = NULL_TREE;
11288 parser->object_scope = NULL_TREE;
11289 /* Indicate that the `*' operator was used. */
11290 code = INDIRECT_REF;
11291 /* Look for the optional cv-qualifier-seq. */
11292 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11293 }
11294 /* If that didn't work we don't have a ptr-operator. */
11295 if (!cp_parser_parse_definitely (parser))
11296 cp_parser_error (parser, "expected ptr-operator");
11297 }
11298
11299 return code;
11300}
11301
11302/* Parse an (optional) cv-qualifier-seq.
11303
11304 cv-qualifier-seq:
21526606 11305 cv-qualifier cv-qualifier-seq [opt]
a723baf1
MM
11306
11307 Returns a TREE_LIST. The TREE_VALUE of each node is the
11308 representation of a cv-qualifier. */
11309
11310static tree
94edc4ab 11311cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
11312{
11313 tree cv_qualifiers = NULL_TREE;
21526606 11314
a723baf1
MM
11315 while (true)
11316 {
11317 tree cv_qualifier;
11318
11319 /* Look for the next cv-qualifier. */
11320 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
11321 /* If we didn't find one, we're done. */
11322 if (!cv_qualifier)
11323 break;
11324
11325 /* Add this cv-qualifier to the list. */
21526606 11326 cv_qualifiers
a723baf1
MM
11327 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
11328 }
11329
11330 /* We built up the list in reverse order. */
11331 return nreverse (cv_qualifiers);
11332}
11333
11334/* Parse an (optional) cv-qualifier.
11335
11336 cv-qualifier:
11337 const
21526606 11338 volatile
a723baf1
MM
11339
11340 GNU Extension:
11341
11342 cv-qualifier:
11343 __restrict__ */
11344
11345static tree
94edc4ab 11346cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
11347{
11348 cp_token *token;
11349 tree cv_qualifier = NULL_TREE;
11350
11351 /* Peek at the next token. */
11352 token = cp_lexer_peek_token (parser->lexer);
11353 /* See if it's a cv-qualifier. */
11354 switch (token->keyword)
11355 {
11356 case RID_CONST:
11357 case RID_VOLATILE:
11358 case RID_RESTRICT:
11359 /* Save the value of the token. */
11360 cv_qualifier = token->value;
11361 /* Consume the token. */
11362 cp_lexer_consume_token (parser->lexer);
11363 break;
11364
11365 default:
11366 break;
11367 }
11368
11369 return cv_qualifier;
11370}
11371
11372/* Parse a declarator-id.
11373
11374 declarator-id:
11375 id-expression
21526606 11376 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11377
11378 In the `id-expression' case, the value returned is as for
11379 cp_parser_id_expression if the id-expression was an unqualified-id.
11380 If the id-expression was a qualified-id, then a SCOPE_REF is
11381 returned. The first operand is the scope (either a NAMESPACE_DECL
11382 or TREE_TYPE), but the second is still just a representation of an
11383 unqualified-id. */
11384
11385static tree
94edc4ab 11386cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
11387{
11388 tree id_expression;
11389
11390 /* The expression must be an id-expression. Assume that qualified
11391 names are the names of types so that:
11392
11393 template <class T>
11394 int S<T>::R::i = 3;
11395
11396 will work; we must treat `S<T>::R' as the name of a type.
11397 Similarly, assume that qualified names are templates, where
11398 required, so that:
11399
11400 template <class T>
11401 int S<T>::R<T>::i = 3;
11402
11403 will work, too. */
11404 id_expression = cp_parser_id_expression (parser,
11405 /*template_keyword_p=*/false,
11406 /*check_dependency_p=*/false,
f3c2dfc6
MM
11407 /*template_p=*/NULL,
11408 /*declarator_p=*/true);
21526606 11409 /* If the name was qualified, create a SCOPE_REF to represent
a723baf1
MM
11410 that. */
11411 if (parser->scope)
ec20aa6c
MM
11412 {
11413 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11414 parser->scope = NULL_TREE;
11415 }
a723baf1
MM
11416
11417 return id_expression;
11418}
11419
11420/* Parse a type-id.
11421
11422 type-id:
11423 type-specifier-seq abstract-declarator [opt]
11424
11425 Returns the TYPE specified. */
11426
11427static tree
94edc4ab 11428cp_parser_type_id (cp_parser* parser)
a723baf1 11429{
62d1db17 11430 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11431 cp_declarator *abstract_declarator;
a723baf1
MM
11432
11433 /* Parse the type-specifier-seq. */
62d1db17
MM
11434 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11435 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11436 return error_mark_node;
11437
11438 /* There might or might not be an abstract declarator. */
11439 cp_parser_parse_tentatively (parser);
11440 /* Look for the declarator. */
21526606 11441 abstract_declarator
4bb8ca28
MM
11442 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11443 /*parenthesized_p=*/NULL);
a723baf1
MM
11444 /* Check to see if there really was a declarator. */
11445 if (!cp_parser_parse_definitely (parser))
058b15c1 11446 abstract_declarator = NULL;
a723baf1 11447
62d1db17 11448 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11449}
11450
11451/* Parse a type-specifier-seq.
11452
11453 type-specifier-seq:
11454 type-specifier type-specifier-seq [opt]
11455
11456 GNU extension:
11457
11458 type-specifier-seq:
11459 attributes type-specifier-seq [opt]
11460
62d1db17 11461 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11462
62d1db17
MM
11463static void
11464cp_parser_type_specifier_seq (cp_parser* parser,
11465 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11466{
11467 bool seen_type_specifier = false;
62d1db17
MM
11468
11469 /* Clear the TYPE_SPECIFIER_SEQ. */
11470 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11471
11472 /* Parse the type-specifiers and attributes. */
11473 while (true)
11474 {
11475 tree type_specifier;
11476
11477 /* Check for attributes first. */
11478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11479 {
62d1db17
MM
11480 type_specifier_seq->attributes =
11481 chainon (type_specifier_seq->attributes,
11482 cp_parser_attributes_opt (parser));
a723baf1
MM
11483 continue;
11484 }
11485
a723baf1 11486 /* Look for the type-specifier. */
21526606 11487 type_specifier = cp_parser_type_specifier (parser,
62d1db17
MM
11488 CP_PARSER_FLAGS_OPTIONAL,
11489 type_specifier_seq,
a723baf1
MM
11490 /*is_declaration=*/false,
11491 NULL,
11492 NULL);
11493 /* If the first type-specifier could not be found, this is not a
11494 type-specifier-seq at all. */
62d1db17
MM
11495 if (!seen_type_specifier && !type_specifier)
11496 {
11497 cp_parser_error (parser, "expected type-specifier");
11498 type_specifier_seq->type = error_mark_node;
11499 return;
11500 }
a723baf1
MM
11501 /* If subsequent type-specifiers could not be found, the
11502 type-specifier-seq is complete. */
62d1db17 11503 else if (seen_type_specifier && !type_specifier)
a723baf1
MM
11504 break;
11505
a723baf1
MM
11506 seen_type_specifier = true;
11507 }
11508
62d1db17 11509 return;
a723baf1
MM
11510}
11511
11512/* Parse a parameter-declaration-clause.
11513
11514 parameter-declaration-clause:
11515 parameter-declaration-list [opt] ... [opt]
11516 parameter-declaration-list , ...
11517
058b15c1
MM
11518 Returns a representation for the parameter declarations. A return
11519 value of NULL indicates a parameter-declaration-clause consisting
11520 only of an ellipsis. */
a723baf1 11521
058b15c1 11522static cp_parameter_declarator *
94edc4ab 11523cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11524{
058b15c1 11525 cp_parameter_declarator *parameters;
a723baf1
MM
11526 cp_token *token;
11527 bool ellipsis_p;
058b15c1 11528 bool is_error;
a723baf1
MM
11529
11530 /* Peek at the next token. */
11531 token = cp_lexer_peek_token (parser->lexer);
11532 /* Check for trivial parameter-declaration-clauses. */
11533 if (token->type == CPP_ELLIPSIS)
11534 {
11535 /* Consume the `...' token. */
11536 cp_lexer_consume_token (parser->lexer);
058b15c1 11537 return NULL;
a723baf1
MM
11538 }
11539 else if (token->type == CPP_CLOSE_PAREN)
11540 /* There are no parameters. */
c73aecdf
DE
11541 {
11542#ifndef NO_IMPLICIT_EXTERN_C
11543 if (in_system_header && current_class_type == NULL
11544 && current_lang_name == lang_name_c)
058b15c1 11545 return NULL;
c73aecdf
DE
11546 else
11547#endif
058b15c1 11548 return no_parameters;
c73aecdf 11549 }
a723baf1
MM
11550 /* Check for `(void)', too, which is a special case. */
11551 else if (token->keyword == RID_VOID
21526606 11552 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11553 == CPP_CLOSE_PAREN))
11554 {
11555 /* Consume the `void' token. */
11556 cp_lexer_consume_token (parser->lexer);
11557 /* There are no parameters. */
058b15c1 11558 return no_parameters;
a723baf1 11559 }
21526606 11560
a723baf1 11561 /* Parse the parameter-declaration-list. */
058b15c1 11562 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11563 /* If a parse error occurred while parsing the
11564 parameter-declaration-list, then the entire
11565 parameter-declaration-clause is erroneous. */
058b15c1
MM
11566 if (is_error)
11567 return NULL;
a723baf1
MM
11568
11569 /* Peek at the next token. */
11570 token = cp_lexer_peek_token (parser->lexer);
11571 /* If it's a `,', the clause should terminate with an ellipsis. */
11572 if (token->type == CPP_COMMA)
11573 {
11574 /* Consume the `,'. */
11575 cp_lexer_consume_token (parser->lexer);
11576 /* Expect an ellipsis. */
21526606 11577 ellipsis_p
a723baf1
MM
11578 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11579 }
21526606 11580 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11581 omitted. */
11582 else if (token->type == CPP_ELLIPSIS)
11583 {
11584 /* Consume the `...' token. */
11585 cp_lexer_consume_token (parser->lexer);
11586 /* And remember that we saw it. */
11587 ellipsis_p = true;
11588 }
11589 else
11590 ellipsis_p = false;
11591
11592 /* Finish the parameter list. */
058b15c1
MM
11593 if (parameters && ellipsis_p)
11594 parameters->ellipsis_p = true;
11595
11596 return parameters;
a723baf1
MM
11597}
11598
11599/* Parse a parameter-declaration-list.
11600
11601 parameter-declaration-list:
11602 parameter-declaration
11603 parameter-declaration-list , parameter-declaration
11604
11605 Returns a representation of the parameter-declaration-list, as for
11606 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11607 `void_list_node' is never appended to the list. Upon return,
11608 *IS_ERROR will be true iff an error occurred. */
a723baf1 11609
058b15c1
MM
11610static cp_parameter_declarator *
11611cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11612{
058b15c1
MM
11613 cp_parameter_declarator *parameters = NULL;
11614 cp_parameter_declarator **tail = &parameters;
11615
11616 /* Assume all will go well. */
11617 *is_error = false;
a723baf1
MM
11618
11619 /* Look for more parameters. */
11620 while (true)
11621 {
058b15c1 11622 cp_parameter_declarator *parameter;
4bb8ca28 11623 bool parenthesized_p;
a723baf1 11624 /* Parse the parameter. */
21526606
EC
11625 parameter
11626 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11627 /*template_parm_p=*/false,
11628 &parenthesized_p);
ec194454 11629
34cd5ae7 11630 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11631 then the entire parameter-declaration-list is erroneous. */
058b15c1 11632 if (!parameter)
a723baf1 11633 {
058b15c1
MM
11634 *is_error = true;
11635 parameters = NULL;
a723baf1
MM
11636 break;
11637 }
11638 /* Add the new parameter to the list. */
058b15c1
MM
11639 *tail = parameter;
11640 tail = &parameter->next;
a723baf1
MM
11641
11642 /* Peek at the next token. */
11643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11644 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11645 /* The parameter-declaration-list is complete. */
11646 break;
11647 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11648 {
11649 cp_token *token;
11650
11651 /* Peek at the next token. */
11652 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11653 /* If it's an ellipsis, then the list is complete. */
11654 if (token->type == CPP_ELLIPSIS)
11655 break;
11656 /* Otherwise, there must be more parameters. Consume the
11657 `,'. */
11658 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11659 /* When parsing something like:
11660
11661 int i(float f, double d)
21526606 11662
4bb8ca28
MM
11663 we can tell after seeing the declaration for "f" that we
11664 are not looking at an initialization of a variable "i",
21526606 11665 but rather at the declaration of a function "i".
4bb8ca28
MM
11666
11667 Due to the fact that the parsing of template arguments
11668 (as specified to a template-id) requires backtracking we
11669 cannot use this technique when inside a template argument
11670 list. */
11671 if (!parser->in_template_argument_list_p
4d5fe289 11672 && !parser->in_type_id_in_expr_p
4bb8ca28
MM
11673 && cp_parser_parsing_tentatively (parser)
11674 && !cp_parser_committed_to_tentative_parse (parser)
11675 /* However, a parameter-declaration of the form
11676 "foat(f)" (which is a valid declaration of a
11677 parameter "f") can also be interpreted as an
11678 expression (the conversion of "f" to "float"). */
11679 && !parenthesized_p)
11680 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11681 }
11682 else
11683 {
11684 cp_parser_error (parser, "expected `,' or `...'");
4bb8ca28
MM
11685 if (!cp_parser_parsing_tentatively (parser)
11686 || cp_parser_committed_to_tentative_parse (parser))
21526606 11687 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11688 /*recovering=*/true,
5c832178 11689 /*or_comma=*/false,
4bb8ca28 11690 /*consume_paren=*/false);
a723baf1
MM
11691 break;
11692 }
11693 }
11694
058b15c1 11695 return parameters;
a723baf1
MM
11696}
11697
11698/* Parse a parameter declaration.
11699
11700 parameter-declaration:
11701 decl-specifier-seq declarator
11702 decl-specifier-seq declarator = assignment-expression
11703 decl-specifier-seq abstract-declarator [opt]
11704 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11705
ec194454
MM
11706 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11707 declares a template parameter. (In that case, a non-nested `>'
11708 token encountered during the parsing of the assignment-expression
11709 is not interpreted as a greater-than operator.)
a723baf1 11710
058b15c1
MM
11711 Returns a representation of the parameter, or NULL if an error
11712 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11713 true iff the declarator is of the form "(p)". */
a723baf1 11714
058b15c1 11715static cp_parameter_declarator *
21526606 11716cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11717 bool template_parm_p,
11718 bool *parenthesized_p)
a723baf1 11719{
560ad596 11720 int declares_class_or_enum;
ec194454 11721 bool greater_than_is_operator_p;
62d1db17 11722 cp_decl_specifier_seq decl_specifiers;
058b15c1 11723 cp_declarator *declarator;
a723baf1 11724 tree default_argument;
a723baf1
MM
11725 cp_token *token;
11726 const char *saved_message;
11727
ec194454
MM
11728 /* In a template parameter, `>' is not an operator.
11729
11730 [temp.param]
11731
11732 When parsing a default template-argument for a non-type
11733 template-parameter, the first non-nested `>' is taken as the end
11734 of the template parameter-list rather than a greater-than
11735 operator. */
11736 greater_than_is_operator_p = !template_parm_p;
11737
a723baf1
MM
11738 /* Type definitions may not appear in parameter types. */
11739 saved_message = parser->type_definition_forbidden_message;
21526606 11740 parser->type_definition_forbidden_message
a723baf1
MM
11741 = "types may not be defined in parameter types";
11742
11743 /* Parse the declaration-specifiers. */
62d1db17
MM
11744 cp_parser_decl_specifier_seq (parser,
11745 CP_PARSER_FLAGS_NONE,
11746 &decl_specifiers,
11747 &declares_class_or_enum);
a723baf1
MM
11748 /* If an error occurred, there's no reason to attempt to parse the
11749 rest of the declaration. */
11750 if (cp_parser_error_occurred (parser))
11751 {
11752 parser->type_definition_forbidden_message = saved_message;
058b15c1 11753 return NULL;
a723baf1
MM
11754 }
11755
11756 /* Peek at the next token. */
11757 token = cp_lexer_peek_token (parser->lexer);
11758 /* If the next token is a `)', `,', `=', `>', or `...', then there
11759 is no declarator. */
21526606 11760 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11761 || token->type == CPP_COMMA
11762 || token->type == CPP_EQ
11763 || token->type == CPP_ELLIPSIS
11764 || token->type == CPP_GREATER)
4bb8ca28 11765 {
058b15c1 11766 declarator = NULL;
4bb8ca28
MM
11767 if (parenthesized_p)
11768 *parenthesized_p = false;
11769 }
a723baf1
MM
11770 /* Otherwise, there should be a declarator. */
11771 else
11772 {
11773 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11774 parser->default_arg_ok_p = false;
21526606 11775
5c832178
MM
11776 /* After seeing a decl-specifier-seq, if the next token is not a
11777 "(", there is no possibility that the code is a valid
4f8163b1
MM
11778 expression. Therefore, if parsing tentatively, we commit at
11779 this point. */
5c832178 11780 if (!parser->in_template_argument_list_p
643aee72 11781 /* In an expression context, having seen:
4f8163b1 11782
a7324e75 11783 (int((char ...
4f8163b1
MM
11784
11785 we cannot be sure whether we are looking at a
a7324e75
MM
11786 function-type (taking a "char" as a parameter) or a cast
11787 of some object of type "char" to "int". */
4f8163b1 11788 && !parser->in_type_id_in_expr_p
5c832178
MM
11789 && cp_parser_parsing_tentatively (parser)
11790 && !cp_parser_committed_to_tentative_parse (parser)
11791 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11792 cp_parser_commit_to_tentative_parse (parser);
11793 /* Parse the declarator. */
a723baf1 11794 declarator = cp_parser_declarator (parser,
62b8a44e 11795 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
11796 /*ctor_dtor_or_conv_p=*/NULL,
11797 parenthesized_p);
a723baf1 11798 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 11799 /* After the declarator, allow more attributes. */
62d1db17
MM
11800 decl_specifiers.attributes
11801 = chainon (decl_specifiers.attributes,
11802 cp_parser_attributes_opt (parser));
a723baf1
MM
11803 }
11804
62b8a44e 11805 /* The restriction on defining new types applies only to the type
a723baf1
MM
11806 of the parameter, not to the default argument. */
11807 parser->type_definition_forbidden_message = saved_message;
11808
11809 /* If the next token is `=', then process a default argument. */
11810 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11811 {
11812 bool saved_greater_than_is_operator_p;
11813 /* Consume the `='. */
11814 cp_lexer_consume_token (parser->lexer);
11815
11816 /* If we are defining a class, then the tokens that make up the
11817 default argument must be saved and processed later. */
21526606 11818 if (!template_parm_p && at_class_scope_p ()
ec194454 11819 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11820 {
11821 unsigned depth = 0;
11822
11823 /* Create a DEFAULT_ARG to represented the unparsed default
11824 argument. */
11825 default_argument = make_node (DEFAULT_ARG);
11826 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11827
11828 /* Add tokens until we have processed the entire default
11829 argument. */
11830 while (true)
11831 {
11832 bool done = false;
11833 cp_token *token;
11834
11835 /* Peek at the next token. */
11836 token = cp_lexer_peek_token (parser->lexer);
11837 /* What we do depends on what token we have. */
11838 switch (token->type)
11839 {
11840 /* In valid code, a default argument must be
11841 immediately followed by a `,' `)', or `...'. */
11842 case CPP_COMMA:
11843 case CPP_CLOSE_PAREN:
11844 case CPP_ELLIPSIS:
11845 /* If we run into a non-nested `;', `}', or `]',
11846 then the code is invalid -- but the default
11847 argument is certainly over. */
11848 case CPP_SEMICOLON:
11849 case CPP_CLOSE_BRACE:
11850 case CPP_CLOSE_SQUARE:
11851 if (depth == 0)
11852 done = true;
11853 /* Update DEPTH, if necessary. */
11854 else if (token->type == CPP_CLOSE_PAREN
11855 || token->type == CPP_CLOSE_BRACE
11856 || token->type == CPP_CLOSE_SQUARE)
11857 --depth;
11858 break;
11859
11860 case CPP_OPEN_PAREN:
11861 case CPP_OPEN_SQUARE:
11862 case CPP_OPEN_BRACE:
11863 ++depth;
11864 break;
11865
11866 case CPP_GREATER:
11867 /* If we see a non-nested `>', and `>' is not an
11868 operator, then it marks the end of the default
11869 argument. */
11870 if (!depth && !greater_than_is_operator_p)
11871 done = true;
11872 break;
11873
11874 /* If we run out of tokens, issue an error message. */
11875 case CPP_EOF:
11876 error ("file ends in default argument");
11877 done = true;
11878 break;
11879
11880 case CPP_NAME:
11881 case CPP_SCOPE:
11882 /* In these cases, we should look for template-ids.
21526606 11883 For example, if the default argument is
a723baf1
MM
11884 `X<int, double>()', we need to do name lookup to
11885 figure out whether or not `X' is a template; if
34cd5ae7 11886 so, the `,' does not end the default argument.
a723baf1
MM
11887
11888 That is not yet done. */
11889 break;
11890
11891 default:
11892 break;
11893 }
11894
11895 /* If we've reached the end, stop. */
11896 if (done)
11897 break;
21526606 11898
a723baf1
MM
11899 /* Add the token to the token block. */
11900 token = cp_lexer_consume_token (parser->lexer);
11901 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11902 token);
11903 }
11904 }
11905 /* Outside of a class definition, we can just parse the
11906 assignment-expression. */
11907 else
11908 {
11909 bool saved_local_variables_forbidden_p;
11910
11911 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11912 set correctly. */
21526606 11913 saved_greater_than_is_operator_p
a723baf1
MM
11914 = parser->greater_than_is_operator_p;
11915 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11916 /* Local variable names (and the `this' keyword) may not
11917 appear in a default argument. */
21526606 11918 saved_local_variables_forbidden_p
a723baf1
MM
11919 = parser->local_variables_forbidden_p;
11920 parser->local_variables_forbidden_p = true;
11921 /* Parse the assignment-expression. */
11922 default_argument = cp_parser_assignment_expression (parser);
11923 /* Restore saved state. */
21526606 11924 parser->greater_than_is_operator_p
a723baf1 11925 = saved_greater_than_is_operator_p;
21526606
EC
11926 parser->local_variables_forbidden_p
11927 = saved_local_variables_forbidden_p;
a723baf1
MM
11928 }
11929 if (!parser->default_arg_ok_p)
11930 {
c67d36d0
NS
11931 if (!flag_pedantic_errors)
11932 warning ("deprecated use of default argument for parameter of non-function");
11933 else
11934 {
11935 error ("default arguments are only permitted for function parameters");
11936 default_argument = NULL_TREE;
11937 }
a723baf1
MM
11938 }
11939 }
11940 else
11941 default_argument = NULL_TREE;
21526606 11942
62d1db17 11943 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
11944 declarator,
11945 default_argument);
a723baf1
MM
11946}
11947
a723baf1
MM
11948/* Parse a function-body.
11949
11950 function-body:
11951 compound_statement */
11952
11953static void
11954cp_parser_function_body (cp_parser *parser)
11955{
325c3691 11956 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
11957}
11958
11959/* Parse a ctor-initializer-opt followed by a function-body. Return
11960 true if a ctor-initializer was present. */
11961
11962static bool
11963cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11964{
11965 tree body;
11966 bool ctor_initializer_p;
11967
11968 /* Begin the function body. */
11969 body = begin_function_body ();
11970 /* Parse the optional ctor-initializer. */
11971 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11972 /* Parse the function-body. */
11973 cp_parser_function_body (parser);
11974 /* Finish the function body. */
11975 finish_function_body (body);
11976
11977 return ctor_initializer_p;
11978}
11979
11980/* Parse an initializer.
11981
11982 initializer:
11983 = initializer-clause
21526606 11984 ( expression-list )
a723baf1
MM
11985
11986 Returns a expression representing the initializer. If no
21526606 11987 initializer is present, NULL_TREE is returned.
a723baf1
MM
11988
11989 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11990 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11991 set to FALSE if there is no initializer present. If there is an
11992 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11993 is set to true; otherwise it is set to false. */
a723baf1
MM
11994
11995static tree
39703eb9
MM
11996cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11997 bool* non_constant_p)
a723baf1
MM
11998{
11999 cp_token *token;
12000 tree init;
12001
12002 /* Peek at the next token. */
12003 token = cp_lexer_peek_token (parser->lexer);
12004
12005 /* Let our caller know whether or not this initializer was
12006 parenthesized. */
12007 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
12008 /* Assume that the initializer is constant. */
12009 *non_constant_p = false;
a723baf1
MM
12010
12011 if (token->type == CPP_EQ)
12012 {
12013 /* Consume the `='. */
12014 cp_lexer_consume_token (parser->lexer);
12015 /* Parse the initializer-clause. */
39703eb9 12016 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
12017 }
12018 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
12019 init = cp_parser_parenthesized_expression_list (parser, false,
12020 non_constant_p);
a723baf1
MM
12021 else
12022 {
12023 /* Anything else is an error. */
12024 cp_parser_error (parser, "expected initializer");
12025 init = error_mark_node;
12026 }
12027
12028 return init;
12029}
12030
21526606 12031/* Parse an initializer-clause.
a723baf1
MM
12032
12033 initializer-clause:
12034 assignment-expression
12035 { initializer-list , [opt] }
12036 { }
12037
21526606 12038 Returns an expression representing the initializer.
a723baf1
MM
12039
12040 If the `assignment-expression' production is used the value
21526606 12041 returned is simply a representation for the expression.
a723baf1
MM
12042
12043 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12044 the elements of the initializer-list (or NULL_TREE, if the last
12045 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12046 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
12047 trailing `,' was provided. NON_CONSTANT_P is as for
12048 cp_parser_initializer. */
a723baf1
MM
12049
12050static tree
39703eb9 12051cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12052{
12053 tree initializer;
12054
12055 /* If it is not a `{', then we are looking at an
12056 assignment-expression. */
12057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e
GB
12058 {
12059 initializer
12060 = cp_parser_constant_expression (parser,
12061 /*allow_non_constant_p=*/true,
12062 non_constant_p);
12063 if (!*non_constant_p)
12064 initializer = fold_non_dependent_expr (initializer);
12065 }
a723baf1
MM
12066 else
12067 {
12068 /* Consume the `{' token. */
12069 cp_lexer_consume_token (parser->lexer);
12070 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12071 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12072 /* If it's not a `}', then there is a non-trivial initializer. */
12073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12074 {
12075 /* Parse the initializer list. */
12076 CONSTRUCTOR_ELTS (initializer)
39703eb9 12077 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12078 /* A trailing `,' token is allowed. */
12079 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12080 cp_lexer_consume_token (parser->lexer);
12081 }
a723baf1
MM
12082 /* Now, there should be a trailing `}'. */
12083 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12084 }
12085
12086 return initializer;
12087}
12088
12089/* Parse an initializer-list.
12090
12091 initializer-list:
12092 initializer-clause
12093 initializer-list , initializer-clause
12094
12095 GNU Extension:
21526606 12096
a723baf1
MM
12097 initializer-list:
12098 identifier : initializer-clause
12099 initializer-list, identifier : initializer-clause
12100
12101 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12102 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
12103 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12104 as for cp_parser_initializer. */
a723baf1
MM
12105
12106static tree
39703eb9 12107cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12108{
12109 tree initializers = NULL_TREE;
12110
39703eb9
MM
12111 /* Assume all of the expressions are constant. */
12112 *non_constant_p = false;
12113
a723baf1
MM
12114 /* Parse the rest of the list. */
12115 while (true)
12116 {
12117 cp_token *token;
12118 tree identifier;
12119 tree initializer;
39703eb9
MM
12120 bool clause_non_constant_p;
12121
a723baf1
MM
12122 /* If the next token is an identifier and the following one is a
12123 colon, we are looking at the GNU designated-initializer
12124 syntax. */
12125 if (cp_parser_allow_gnu_extensions_p (parser)
12126 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12127 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12128 {
12129 /* Consume the identifier. */
12130 identifier = cp_lexer_consume_token (parser->lexer)->value;
12131 /* Consume the `:'. */
12132 cp_lexer_consume_token (parser->lexer);
12133 }
12134 else
12135 identifier = NULL_TREE;
12136
12137 /* Parse the initializer. */
21526606 12138 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12139 &clause_non_constant_p);
12140 /* If any clause is non-constant, so is the entire initializer. */
12141 if (clause_non_constant_p)
12142 *non_constant_p = true;
a723baf1
MM
12143 /* Add it to the list. */
12144 initializers = tree_cons (identifier, initializer, initializers);
12145
12146 /* If the next token is not a comma, we have reached the end of
12147 the list. */
12148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12149 break;
12150
12151 /* Peek at the next token. */
12152 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12153 /* If the next token is a `}', then we're still done. An
12154 initializer-clause can have a trailing `,' after the
12155 initializer-list and before the closing `}'. */
12156 if (token->type == CPP_CLOSE_BRACE)
12157 break;
12158
12159 /* Consume the `,' token. */
12160 cp_lexer_consume_token (parser->lexer);
12161 }
12162
12163 /* The initializers were built up in reverse order, so we need to
12164 reverse them now. */
12165 return nreverse (initializers);
12166}
12167
12168/* Classes [gram.class] */
12169
12170/* Parse a class-name.
12171
12172 class-name:
12173 identifier
12174 template-id
12175
12176 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12177 to indicate that names looked up in dependent types should be
12178 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12179 keyword has been used to indicate that the name that appears next
12180 is a template. TYPE_P is true iff the next name should be treated
12181 as class-name, even if it is declared to be some other kind of name
8d241e0b
KL
12182 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12183 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12184 being defined in a class-head.
a723baf1
MM
12185
12186 Returns the TYPE_DECL representing the class. */
12187
12188static tree
21526606
EC
12189cp_parser_class_name (cp_parser *parser,
12190 bool typename_keyword_p,
12191 bool template_keyword_p,
a723baf1 12192 bool type_p,
a723baf1 12193 bool check_dependency_p,
a668c6ad
MM
12194 bool class_head_p,
12195 bool is_declaration)
a723baf1
MM
12196{
12197 tree decl;
12198 tree scope;
12199 bool typename_p;
e5976695
MM
12200 cp_token *token;
12201
12202 /* All class-names start with an identifier. */
12203 token = cp_lexer_peek_token (parser->lexer);
12204 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12205 {
12206 cp_parser_error (parser, "expected class-name");
12207 return error_mark_node;
12208 }
21526606 12209
a723baf1
MM
12210 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12211 to a template-id, so we save it here. */
12212 scope = parser->scope;
3adee96c
KL
12213 if (scope == error_mark_node)
12214 return error_mark_node;
21526606 12215
a723baf1
MM
12216 /* Any name names a type if we're following the `typename' keyword
12217 in a qualified name where the enclosing scope is type-dependent. */
12218 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12219 && dependent_type_p (scope));
e5976695
MM
12220 /* Handle the common case (an identifier, but not a template-id)
12221 efficiently. */
21526606 12222 if (token->type == CPP_NAME
f4abade9 12223 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12224 {
a723baf1
MM
12225 tree identifier;
12226
12227 /* Look for the identifier. */
12228 identifier = cp_parser_identifier (parser);
12229 /* If the next token isn't an identifier, we are certainly not
12230 looking at a class-name. */
12231 if (identifier == error_mark_node)
12232 decl = error_mark_node;
12233 /* If we know this is a type-name, there's no need to look it
12234 up. */
12235 else if (typename_p)
12236 decl = identifier;
12237 else
12238 {
12239 /* If the next token is a `::', then the name must be a type
12240 name.
12241
12242 [basic.lookup.qual]
12243
12244 During the lookup for a name preceding the :: scope
12245 resolution operator, object, function, and enumerator
12246 names are ignored. */
12247 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12248 type_p = true;
12249 /* Look up the name. */
21526606 12250 decl = cp_parser_lookup_name (parser, identifier,
a723baf1 12251 type_p,
b0bc6e8e 12252 /*is_template=*/false,
eea9800f 12253 /*is_namespace=*/false,
a723baf1
MM
12254 check_dependency_p);
12255 }
12256 }
e5976695
MM
12257 else
12258 {
12259 /* Try a template-id. */
12260 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12261 check_dependency_p,
12262 is_declaration);
e5976695
MM
12263 if (decl == error_mark_node)
12264 return error_mark_node;
12265 }
a723baf1
MM
12266
12267 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12268
12269 /* If this is a typename, create a TYPENAME_TYPE. */
12270 if (typename_p && decl != error_mark_node)
4bfb8bba
MM
12271 {
12272 decl = make_typename_type (scope, decl, /*complain=*/1);
12273 if (decl != error_mark_node)
12274 decl = TYPE_NAME (decl);
12275 }
a723baf1
MM
12276
12277 /* Check to see that it is really the name of a class. */
21526606 12278 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12279 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12280 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12281 /* Situations like this:
12282
12283 template <typename T> struct A {
21526606 12284 typename T::template X<int>::I i;
a723baf1
MM
12285 };
12286
12287 are problematic. Is `T::template X<int>' a class-name? The
12288 standard does not seem to be definitive, but there is no other
12289 valid interpretation of the following `::'. Therefore, those
12290 names are considered class-names. */
78757caa 12291 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
a723baf1
MM
12292 else if (decl == error_mark_node
12293 || TREE_CODE (decl) != TYPE_DECL
12294 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12295 {
12296 cp_parser_error (parser, "expected class-name");
12297 return error_mark_node;
12298 }
12299
12300 return decl;
12301}
12302
12303/* Parse a class-specifier.
12304
12305 class-specifier:
12306 class-head { member-specification [opt] }
12307
12308 Returns the TREE_TYPE representing the class. */
12309
12310static tree
94edc4ab 12311cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12312{
12313 cp_token *token;
12314 tree type;
6de9cd9a 12315 tree attributes = NULL_TREE;
a723baf1
MM
12316 int has_trailing_semicolon;
12317 bool nested_name_specifier_p;
a723baf1 12318 unsigned saved_num_template_parameter_lists;
91b004e5 12319 bool pop_p = false;
a723baf1 12320
8d241e0b 12321 push_deferring_access_checks (dk_no_deferred);
cf22909c 12322
a723baf1
MM
12323 /* Parse the class-head. */
12324 type = cp_parser_class_head (parser,
38b305d0
JM
12325 &nested_name_specifier_p,
12326 &attributes);
a723baf1
MM
12327 /* If the class-head was a semantic disaster, skip the entire body
12328 of the class. */
12329 if (!type)
12330 {
12331 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12332 pop_deferring_access_checks ();
a723baf1
MM
12333 return error_mark_node;
12334 }
cf22909c 12335
a723baf1
MM
12336 /* Look for the `{'. */
12337 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12338 {
12339 pop_deferring_access_checks ();
12340 return error_mark_node;
12341 }
12342
a723baf1
MM
12343 /* Issue an error message if type-definitions are forbidden here. */
12344 cp_parser_check_type_definition (parser);
12345 /* Remember that we are defining one more class. */
12346 ++parser->num_classes_being_defined;
12347 /* Inside the class, surrounding template-parameter-lists do not
12348 apply. */
21526606
EC
12349 saved_num_template_parameter_lists
12350 = parser->num_template_parameter_lists;
a723baf1 12351 parser->num_template_parameter_lists = 0;
78757caa 12352
a723baf1 12353 /* Start the class. */
eeb23c11 12354 if (nested_name_specifier_p)
91b004e5 12355 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
12356 type = begin_class_definition (type);
12357 if (type == error_mark_node)
9bcb9aae 12358 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12359 cp_parser_skip_to_closing_brace (parser);
12360 else
12361 /* Parse the member-specification. */
12362 cp_parser_member_specification_opt (parser);
12363 /* Look for the trailing `}'. */
12364 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12365 /* We get better error messages by noticing a common problem: a
12366 missing trailing `;'. */
12367 token = cp_lexer_peek_token (parser->lexer);
12368 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12369 /* Look for trailing attributes to apply to this class. */
a723baf1 12370 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12371 {
38b305d0
JM
12372 tree sub_attr = cp_parser_attributes_opt (parser);
12373 attributes = chainon (attributes, sub_attr);
560ad596 12374 }
38b305d0
JM
12375 if (type != error_mark_node)
12376 type = finish_struct (type, attributes);
91b004e5 12377 if (pop_p)
560ad596 12378 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
a723baf1
MM
12379 /* If this class is not itself within the scope of another class,
12380 then we need to parse the bodies of all of the queued function
12381 definitions. Note that the queued functions defined in a class
12382 are not always processed immediately following the
12383 class-specifier for that class. Consider:
12384
12385 struct A {
12386 struct B { void f() { sizeof (A); } };
12387 };
12388
12389 If `f' were processed before the processing of `A' were
12390 completed, there would be no way to compute the size of `A'.
12391 Note that the nesting we are interested in here is lexical --
12392 not the semantic nesting given by TYPE_CONTEXT. In particular,
12393 for:
12394
12395 struct A { struct B; };
12396 struct A::B { void f() { } };
12397
12398 there is no need to delay the parsing of `A::B::f'. */
21526606 12399 if (--parser->num_classes_being_defined == 0)
a723baf1 12400 {
8218bd34
MM
12401 tree queue_entry;
12402 tree fn;
a723baf1 12403
8218bd34
MM
12404 /* In a first pass, parse default arguments to the functions.
12405 Then, in a second pass, parse the bodies of the functions.
12406 This two-phased approach handles cases like:
21526606
EC
12407
12408 struct S {
12409 void f() { g(); }
8218bd34
MM
12410 void g(int i = 3);
12411 };
12412
12413 */
8db1028e
NS
12414 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12415 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12416 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12417 TREE_PURPOSE (parser->unparsed_functions_queues)
12418 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12419 {
12420 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12421 /* Make sure that any template parameters are in scope. */
12422 maybe_begin_member_template_processing (fn);
12423 /* If there are default arguments that have not yet been processed,
12424 take care of them now. */
12425 cp_parser_late_parsing_default_args (parser, fn);
12426 /* Remove any template parameters from the symbol table. */
12427 maybe_end_member_template_processing ();
12428 }
12429 /* Now parse the body of the functions. */
8db1028e
NS
12430 for (TREE_VALUE (parser->unparsed_functions_queues)
12431 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12432 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12433 TREE_VALUE (parser->unparsed_functions_queues)
12434 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12435 {
a723baf1 12436 /* Figure out which function we need to process. */
a723baf1
MM
12437 fn = TREE_VALUE (queue_entry);
12438
4543ee47
ZD
12439 /* A hack to prevent garbage collection. */
12440 function_depth++;
12441
a723baf1
MM
12442 /* Parse the function. */
12443 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 12444 function_depth--;
a723baf1
MM
12445 }
12446
a723baf1
MM
12447 }
12448
12449 /* Put back any saved access checks. */
cf22909c 12450 pop_deferring_access_checks ();
a723baf1
MM
12451
12452 /* Restore the count of active template-parameter-lists. */
12453 parser->num_template_parameter_lists
12454 = saved_num_template_parameter_lists;
12455
12456 return type;
12457}
12458
12459/* Parse a class-head.
12460
12461 class-head:
12462 class-key identifier [opt] base-clause [opt]
12463 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12464 class-key nested-name-specifier [opt] template-id
12465 base-clause [opt]
a723baf1
MM
12466
12467 GNU Extensions:
12468 class-key attributes identifier [opt] base-clause [opt]
12469 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12470 class-key attributes nested-name-specifier [opt] template-id
12471 base-clause [opt]
a723baf1
MM
12472
12473 Returns the TYPE of the indicated class. Sets
12474 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12475 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
12476
12477 Returns NULL_TREE if the class-head is syntactically valid, but
12478 semantically invalid in a way that means we should skip the entire
12479 body of the class. */
12480
12481static tree
21526606 12482cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12483 bool* nested_name_specifier_p,
12484 tree *attributes_p)
a723baf1
MM
12485{
12486 cp_token *token;
12487 tree nested_name_specifier;
12488 enum tag_types class_key;
12489 tree id = NULL_TREE;
12490 tree type = NULL_TREE;
12491 tree attributes;
12492 bool template_id_p = false;
12493 bool qualified_p = false;
12494 bool invalid_nested_name_p = false;
afb0918a 12495 bool invalid_explicit_specialization_p = false;
91b004e5 12496 bool pop_p = false;
a723baf1
MM
12497 unsigned num_templates;
12498
12499 /* Assume no nested-name-specifier will be present. */
12500 *nested_name_specifier_p = false;
12501 /* Assume no template parameter lists will be used in defining the
12502 type. */
12503 num_templates = 0;
12504
12505 /* Look for the class-key. */
12506 class_key = cp_parser_class_key (parser);
12507 if (class_key == none_type)
12508 return error_mark_node;
12509
12510 /* Parse the attributes. */
12511 attributes = cp_parser_attributes_opt (parser);
12512
12513 /* If the next token is `::', that is invalid -- but sometimes
12514 people do try to write:
12515
21526606 12516 struct ::S {};
a723baf1
MM
12517
12518 Handle this gracefully by accepting the extra qualifier, and then
12519 issuing an error about it later if this really is a
2050a1bb 12520 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12521 specifier, remain silent. */
12522 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12523 qualified_p = true;
12524
8d241e0b
KL
12525 push_deferring_access_checks (dk_no_check);
12526
a723baf1
MM
12527 /* Determine the name of the class. Begin by looking for an
12528 optional nested-name-specifier. */
21526606 12529 nested_name_specifier
a723baf1
MM
12530 = cp_parser_nested_name_specifier_opt (parser,
12531 /*typename_keyword_p=*/false,
66d418e6 12532 /*check_dependency_p=*/false,
a668c6ad
MM
12533 /*type_p=*/false,
12534 /*is_declaration=*/false);
a723baf1
MM
12535 /* If there was a nested-name-specifier, then there *must* be an
12536 identifier. */
12537 if (nested_name_specifier)
12538 {
12539 /* Although the grammar says `identifier', it really means
12540 `class-name' or `template-name'. You are only allowed to
12541 define a class that has already been declared with this
21526606 12542 syntax.
a723baf1
MM
12543
12544 The proposed resolution for Core Issue 180 says that whever
12545 you see `class T::X' you should treat `X' as a type-name.
21526606 12546
a723baf1 12547 It is OK to define an inaccessible class; for example:
21526606 12548
a723baf1
MM
12549 class A { class B; };
12550 class A::B {};
21526606 12551
a723baf1
MM
12552 We do not know if we will see a class-name, or a
12553 template-name. We look for a class-name first, in case the
12554 class-name is a template-id; if we looked for the
12555 template-name first we would stop after the template-name. */
12556 cp_parser_parse_tentatively (parser);
12557 type = cp_parser_class_name (parser,
12558 /*typename_keyword_p=*/false,
12559 /*template_keyword_p=*/false,
12560 /*type_p=*/true,
a723baf1 12561 /*check_dependency_p=*/false,
a668c6ad
MM
12562 /*class_head_p=*/true,
12563 /*is_declaration=*/false);
a723baf1
MM
12564 /* If that didn't work, ignore the nested-name-specifier. */
12565 if (!cp_parser_parse_definitely (parser))
12566 {
12567 invalid_nested_name_p = true;
12568 id = cp_parser_identifier (parser);
12569 if (id == error_mark_node)
12570 id = NULL_TREE;
12571 }
12572 /* If we could not find a corresponding TYPE, treat this
12573 declaration like an unqualified declaration. */
12574 if (type == error_mark_node)
12575 nested_name_specifier = NULL_TREE;
12576 /* Otherwise, count the number of templates used in TYPE and its
12577 containing scopes. */
21526606 12578 else
a723baf1
MM
12579 {
12580 tree scope;
12581
21526606 12582 for (scope = TREE_TYPE (type);
a723baf1 12583 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12584 scope = (TYPE_P (scope)
a723baf1 12585 ? TYPE_CONTEXT (scope)
21526606
EC
12586 : DECL_CONTEXT (scope)))
12587 if (TYPE_P (scope)
a723baf1
MM
12588 && CLASS_TYPE_P (scope)
12589 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12590 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12591 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12592 ++num_templates;
12593 }
12594 }
12595 /* Otherwise, the identifier is optional. */
12596 else
12597 {
12598 /* We don't know whether what comes next is a template-id,
12599 an identifier, or nothing at all. */
12600 cp_parser_parse_tentatively (parser);
12601 /* Check for a template-id. */
21526606 12602 id = cp_parser_template_id (parser,
a723baf1 12603 /*template_keyword_p=*/false,
a668c6ad
MM
12604 /*check_dependency_p=*/true,
12605 /*is_declaration=*/true);
a723baf1
MM
12606 /* If that didn't work, it could still be an identifier. */
12607 if (!cp_parser_parse_definitely (parser))
12608 {
12609 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12610 id = cp_parser_identifier (parser);
12611 else
12612 id = NULL_TREE;
12613 }
12614 else
12615 {
12616 template_id_p = true;
12617 ++num_templates;
12618 }
12619 }
12620
8d241e0b
KL
12621 pop_deferring_access_checks ();
12622
15077df5
MM
12623 if (id)
12624 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12625
a723baf1
MM
12626 /* If it's not a `:' or a `{' then we can't really be looking at a
12627 class-head, since a class-head only appears as part of a
12628 class-specifier. We have to detect this situation before calling
12629 xref_tag, since that has irreversible side-effects. */
12630 if (!cp_parser_next_token_starts_class_definition_p (parser))
12631 {
12632 cp_parser_error (parser, "expected `{' or `:'");
12633 return error_mark_node;
12634 }
12635
12636 /* At this point, we're going ahead with the class-specifier, even
12637 if some other problem occurs. */
12638 cp_parser_commit_to_tentative_parse (parser);
12639 /* Issue the error about the overly-qualified name now. */
12640 if (qualified_p)
12641 cp_parser_error (parser,
12642 "global qualification of class name is invalid");
12643 else if (invalid_nested_name_p)
12644 cp_parser_error (parser,
12645 "qualified name does not name a class");
88081599
MM
12646 else if (nested_name_specifier)
12647 {
12648 tree scope;
12649 /* Figure out in what scope the declaration is being placed. */
12650 scope = current_scope ();
12651 if (!scope)
12652 scope = current_namespace;
12653 /* If that scope does not contain the scope in which the
12654 class was originally declared, the program is invalid. */
12655 if (scope && !is_ancestor (scope, nested_name_specifier))
12656 {
12657 error ("declaration of `%D' in `%D' which does not "
12658 "enclose `%D'", type, scope, nested_name_specifier);
12659 type = NULL_TREE;
12660 goto done;
12661 }
12662 /* [dcl.meaning]
12663
12664 A declarator-id shall not be qualified exception of the
12665 definition of a ... nested class outside of its class
12666 ... [or] a the definition or explicit instantiation of a
12667 class member of a namespace outside of its namespace. */
12668 if (scope == nested_name_specifier)
12669 {
12670 pedwarn ("extra qualification ignored");
12671 nested_name_specifier = NULL_TREE;
12672 num_templates = 0;
12673 }
12674 }
afb0918a
MM
12675 /* An explicit-specialization must be preceded by "template <>". If
12676 it is not, try to recover gracefully. */
21526606 12677 if (at_namespace_scope_p ()
afb0918a 12678 && parser->num_template_parameter_lists == 0
eeb23c11 12679 && template_id_p)
afb0918a
MM
12680 {
12681 error ("an explicit specialization must be preceded by 'template <>'");
12682 invalid_explicit_specialization_p = true;
12683 /* Take the same action that would have been taken by
12684 cp_parser_explicit_specialization. */
12685 ++parser->num_template_parameter_lists;
12686 begin_specialization ();
12687 }
12688 /* There must be no "return" statements between this point and the
12689 end of this function; set "type "to the correct return value and
12690 use "goto done;" to return. */
a723baf1
MM
12691 /* Make sure that the right number of template parameters were
12692 present. */
12693 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12694 {
12695 /* If something went wrong, there is no point in even trying to
12696 process the class-definition. */
12697 type = NULL_TREE;
12698 goto done;
12699 }
a723baf1 12700
a723baf1
MM
12701 /* Look up the type. */
12702 if (template_id_p)
12703 {
12704 type = TREE_TYPE (id);
12705 maybe_process_partial_specialization (type);
12706 }
12707 else if (!nested_name_specifier)
12708 {
12709 /* If the class was unnamed, create a dummy name. */
12710 if (!id)
12711 id = make_anon_name ();
38b305d0 12712 type = xref_tag (class_key, id, /*globalize=*/false,
cbd63935 12713 parser->num_template_parameter_lists);
a723baf1
MM
12714 }
12715 else
12716 {
a723baf1 12717 tree class_type;
91b004e5 12718 bool pop_p = false;
a723baf1
MM
12719
12720 /* Given:
12721
12722 template <typename T> struct S { struct T };
14d22dd6 12723 template <typename T> struct S<T>::T { };
a723baf1
MM
12724
12725 we will get a TYPENAME_TYPE when processing the definition of
12726 `S::T'. We need to resolve it to the actual type before we
12727 try to define it. */
12728 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12729 {
14d22dd6
MM
12730 class_type = resolve_typename_type (TREE_TYPE (type),
12731 /*only_current_p=*/false);
12732 if (class_type != error_mark_node)
12733 type = TYPE_NAME (class_type);
12734 else
12735 {
12736 cp_parser_error (parser, "could not resolve typename type");
12737 type = error_mark_node;
12738 }
a723baf1
MM
12739 }
12740
560ad596
MM
12741 maybe_process_partial_specialization (TREE_TYPE (type));
12742 class_type = current_class_type;
12743 /* Enter the scope indicated by the nested-name-specifier. */
12744 if (nested_name_specifier)
91b004e5 12745 pop_p = push_scope (nested_name_specifier);
560ad596
MM
12746 /* Get the canonical version of this type. */
12747 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12748 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12749 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12750 type = push_template_decl (type);
12751 type = TREE_TYPE (type);
12752 if (nested_name_specifier)
eeb23c11
MM
12753 {
12754 *nested_name_specifier_p = true;
91b004e5
MM
12755 if (pop_p)
12756 pop_scope (nested_name_specifier);
eeb23c11 12757 }
a723baf1
MM
12758 }
12759 /* Indicate whether this class was declared as a `class' or as a
12760 `struct'. */
12761 if (TREE_CODE (type) == RECORD_TYPE)
12762 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12763 cp_parser_check_class_key (class_key, type);
12764
12765 /* Enter the scope containing the class; the names of base classes
12766 should be looked up in that context. For example, given:
12767
12768 struct A { struct B {}; struct C; };
12769 struct A::C : B {};
12770
12771 is valid. */
12772 if (nested_name_specifier)
91b004e5 12773 pop_p = push_scope (nested_name_specifier);
a723baf1
MM
12774 /* Now, look for the base-clause. */
12775 token = cp_lexer_peek_token (parser->lexer);
12776 if (token->type == CPP_COLON)
12777 {
12778 tree bases;
12779
12780 /* Get the list of base-classes. */
12781 bases = cp_parser_base_clause (parser);
12782 /* Process them. */
12783 xref_basetypes (type, bases);
12784 }
12785 /* Leave the scope given by the nested-name-specifier. We will
12786 enter the class scope itself while processing the members. */
91b004e5 12787 if (pop_p)
a723baf1
MM
12788 pop_scope (nested_name_specifier);
12789
afb0918a
MM
12790 done:
12791 if (invalid_explicit_specialization_p)
12792 {
12793 end_specialization ();
12794 --parser->num_template_parameter_lists;
12795 }
38b305d0 12796 *attributes_p = attributes;
a723baf1
MM
12797 return type;
12798}
12799
12800/* Parse a class-key.
12801
12802 class-key:
12803 class
12804 struct
12805 union
12806
12807 Returns the kind of class-key specified, or none_type to indicate
12808 error. */
12809
12810static enum tag_types
94edc4ab 12811cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12812{
12813 cp_token *token;
12814 enum tag_types tag_type;
12815
12816 /* Look for the class-key. */
12817 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12818 if (!token)
12819 return none_type;
12820
12821 /* Check to see if the TOKEN is a class-key. */
12822 tag_type = cp_parser_token_is_class_key (token);
12823 if (!tag_type)
12824 cp_parser_error (parser, "expected class-key");
12825 return tag_type;
12826}
12827
12828/* Parse an (optional) member-specification.
12829
12830 member-specification:
12831 member-declaration member-specification [opt]
12832 access-specifier : member-specification [opt] */
12833
12834static void
94edc4ab 12835cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12836{
12837 while (true)
12838 {
12839 cp_token *token;
12840 enum rid keyword;
12841
12842 /* Peek at the next token. */
12843 token = cp_lexer_peek_token (parser->lexer);
12844 /* If it's a `}', or EOF then we've seen all the members. */
12845 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12846 break;
12847
12848 /* See if this token is a keyword. */
12849 keyword = token->keyword;
12850 switch (keyword)
12851 {
12852 case RID_PUBLIC:
12853 case RID_PROTECTED:
12854 case RID_PRIVATE:
12855 /* Consume the access-specifier. */
12856 cp_lexer_consume_token (parser->lexer);
12857 /* Remember which access-specifier is active. */
12858 current_access_specifier = token->value;
12859 /* Look for the `:'. */
12860 cp_parser_require (parser, CPP_COLON, "`:'");
12861 break;
12862
12863 default:
12864 /* Otherwise, the next construction must be a
12865 member-declaration. */
12866 cp_parser_member_declaration (parser);
a723baf1
MM
12867 }
12868 }
12869}
12870
21526606 12871/* Parse a member-declaration.
a723baf1
MM
12872
12873 member-declaration:
12874 decl-specifier-seq [opt] member-declarator-list [opt] ;
12875 function-definition ; [opt]
12876 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12877 using-declaration
21526606 12878 template-declaration
a723baf1
MM
12879
12880 member-declarator-list:
12881 member-declarator
12882 member-declarator-list , member-declarator
12883
12884 member-declarator:
21526606 12885 declarator pure-specifier [opt]
a723baf1 12886 declarator constant-initializer [opt]
21526606 12887 identifier [opt] : constant-expression
a723baf1
MM
12888
12889 GNU Extensions:
12890
12891 member-declaration:
12892 __extension__ member-declaration
12893
12894 member-declarator:
12895 declarator attributes [opt] pure-specifier [opt]
12896 declarator attributes [opt] constant-initializer [opt]
12897 identifier [opt] attributes [opt] : constant-expression */
12898
12899static void
94edc4ab 12900cp_parser_member_declaration (cp_parser* parser)
a723baf1 12901{
62d1db17 12902 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
12903 tree prefix_attributes;
12904 tree decl;
560ad596 12905 int declares_class_or_enum;
a723baf1
MM
12906 bool friend_p;
12907 cp_token *token;
12908 int saved_pedantic;
12909
12910 /* Check for the `__extension__' keyword. */
12911 if (cp_parser_extension_opt (parser, &saved_pedantic))
12912 {
12913 /* Recurse. */
12914 cp_parser_member_declaration (parser);
12915 /* Restore the old value of the PEDANTIC flag. */
12916 pedantic = saved_pedantic;
12917
12918 return;
12919 }
12920
12921 /* Check for a template-declaration. */
12922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12923 {
12924 /* Parse the template-declaration. */
12925 cp_parser_template_declaration (parser, /*member_p=*/true);
12926
12927 return;
12928 }
12929
12930 /* Check for a using-declaration. */
12931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12932 {
12933 /* Parse the using-declaration. */
12934 cp_parser_using_declaration (parser);
12935
12936 return;
12937 }
21526606 12938
a723baf1 12939 /* Parse the decl-specifier-seq. */
62d1db17
MM
12940 cp_parser_decl_specifier_seq (parser,
12941 CP_PARSER_FLAGS_OPTIONAL,
12942 &decl_specifiers,
12943 &declares_class_or_enum);
12944 prefix_attributes = decl_specifiers.attributes;
12945 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 12946 /* Check for an invalid type-name. */
2097b5f2 12947 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 12948 return;
a723baf1
MM
12949 /* If there is no declarator, then the decl-specifier-seq should
12950 specify a type. */
12951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12952 {
12953 /* If there was no decl-specifier-seq, and the next token is a
12954 `;', then we have something like:
12955
12956 struct S { ; };
12957
12958 [class.mem]
12959
12960 Each member-declaration shall declare at least one member
12961 name of the class. */
62d1db17 12962 if (!decl_specifiers.any_specifiers_p)
a723baf1
MM
12963 {
12964 if (pedantic)
12965 pedwarn ("extra semicolon");
12966 }
21526606 12967 else
a723baf1
MM
12968 {
12969 tree type;
21526606 12970
a723baf1 12971 /* See if this declaration is a friend. */
62d1db17 12972 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
12973 /* If there were decl-specifiers, check to see if there was
12974 a class-declaration. */
62d1db17 12975 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
12976 /* Nested classes have already been added to the class, but
12977 a `friend' needs to be explicitly registered. */
12978 if (friend_p)
12979 {
12980 /* If the `friend' keyword was present, the friend must
12981 be introduced with a class-key. */
12982 if (!declares_class_or_enum)
12983 error ("a class-key must be used when declaring a friend");
12984 /* In this case:
12985
21526606
EC
12986 template <typename T> struct A {
12987 friend struct A<T>::B;
a723baf1 12988 };
21526606 12989
a723baf1
MM
12990 A<T>::B will be represented by a TYPENAME_TYPE, and
12991 therefore not recognized by check_tag_decl. */
62d1db17
MM
12992 if (!type
12993 && decl_specifiers.type
12994 && TYPE_P (decl_specifiers.type))
12995 type = decl_specifiers.type;
fdd09134 12996 if (!type || !TYPE_P (type))
a723baf1
MM
12997 error ("friend declaration does not name a class or "
12998 "function");
12999 else
19db77ce
KL
13000 make_friend_class (current_class_type, type,
13001 /*complain=*/true);
a723baf1
MM
13002 }
13003 /* If there is no TYPE, an error message will already have
13004 been issued. */
62d1db17 13005 else if (!type || type == error_mark_node)
a723baf1
MM
13006 ;
13007 /* An anonymous aggregate has to be handled specially; such
13008 a declaration really declares a data member (with a
13009 particular type), as opposed to a nested class. */
13010 else if (ANON_AGGR_TYPE_P (type))
13011 {
13012 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13013 know it is an anonymous aggregate. */
a723baf1
MM
13014 fixup_anonymous_aggr (type);
13015 /* And make the corresponding data member. */
13016 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13017 /* Add it to the class. */
13018 finish_member_declaration (decl);
13019 }
37d407a1
KL
13020 else
13021 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13022 }
13023 }
13024 else
13025 {
13026 /* See if these declarations will be friends. */
62d1db17 13027 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13028
21526606 13029 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13030 declaration. */
13031 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13032 {
13033 tree attributes = NULL_TREE;
13034 tree first_attribute;
13035
13036 /* Peek at the next token. */
13037 token = cp_lexer_peek_token (parser->lexer);
13038
13039 /* Check for a bitfield declaration. */
13040 if (token->type == CPP_COLON
13041 || (token->type == CPP_NAME
21526606 13042 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13043 == CPP_COLON))
13044 {
13045 tree identifier;
13046 tree width;
13047
13048 /* Get the name of the bitfield. Note that we cannot just
13049 check TOKEN here because it may have been invalidated by
13050 the call to cp_lexer_peek_nth_token above. */
13051 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13052 identifier = cp_parser_identifier (parser);
13053 else
13054 identifier = NULL_TREE;
13055
13056 /* Consume the `:' token. */
13057 cp_lexer_consume_token (parser->lexer);
13058 /* Get the width of the bitfield. */
21526606 13059 width
14d22dd6
MM
13060 = cp_parser_constant_expression (parser,
13061 /*allow_non_constant=*/false,
13062 NULL);
a723baf1
MM
13063
13064 /* Look for attributes that apply to the bitfield. */
13065 attributes = cp_parser_attributes_opt (parser);
13066 /* Remember which attributes are prefix attributes and
13067 which are not. */
13068 first_attribute = attributes;
13069 /* Combine the attributes. */
13070 attributes = chainon (prefix_attributes, attributes);
13071
13072 /* Create the bitfield declaration. */
058b15c1
MM
13073 decl = grokbitfield (identifier
13074 ? make_id_declarator (identifier)
13075 : NULL,
62d1db17 13076 &decl_specifiers,
a723baf1
MM
13077 width);
13078 /* Apply the attributes. */
13079 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13080 }
13081 else
13082 {
058b15c1 13083 cp_declarator *declarator;
a723baf1
MM
13084 tree initializer;
13085 tree asm_specification;
7efa3e22 13086 int ctor_dtor_or_conv_p;
a723baf1
MM
13087
13088 /* Parse the declarator. */
21526606 13089 declarator
62b8a44e 13090 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28
MM
13091 &ctor_dtor_or_conv_p,
13092 /*parenthesized_p=*/NULL);
a723baf1
MM
13093
13094 /* If something went wrong parsing the declarator, make sure
13095 that we at least consume some tokens. */
058b15c1 13096 if (declarator == cp_error_declarator)
a723baf1
MM
13097 {
13098 /* Skip to the end of the statement. */
13099 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13100 /* If the next token is not a semicolon, that is
13101 probably because we just skipped over the body of
13102 a function. So, we consume a semicolon if
13103 present, but do not issue an error message if it
13104 is not present. */
13105 if (cp_lexer_next_token_is (parser->lexer,
13106 CPP_SEMICOLON))
13107 cp_lexer_consume_token (parser->lexer);
13108 return;
a723baf1
MM
13109 }
13110
21526606 13111 cp_parser_check_for_definition_in_return_type
560ad596
MM
13112 (declarator, declares_class_or_enum);
13113
a723baf1
MM
13114 /* Look for an asm-specification. */
13115 asm_specification = cp_parser_asm_specification_opt (parser);
13116 /* Look for attributes that apply to the declaration. */
13117 attributes = cp_parser_attributes_opt (parser);
13118 /* Remember which attributes are prefix attributes and
13119 which are not. */
13120 first_attribute = attributes;
13121 /* Combine the attributes. */
13122 attributes = chainon (prefix_attributes, attributes);
13123
13124 /* If it's an `=', then we have a constant-initializer or a
13125 pure-specifier. It is not correct to parse the
13126 initializer before registering the member declaration
13127 since the member declaration should be in scope while
13128 its initializer is processed. However, the rest of the
13129 front end does not yet provide an interface that allows
13130 us to handle this correctly. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13132 {
13133 /* In [class.mem]:
13134
13135 A pure-specifier shall be used only in the declaration of
21526606 13136 a virtual function.
a723baf1
MM
13137
13138 A member-declarator can contain a constant-initializer
13139 only if it declares a static member of integral or
21526606 13140 enumeration type.
a723baf1
MM
13141
13142 Therefore, if the DECLARATOR is for a function, we look
13143 for a pure-specifier; otherwise, we look for a
13144 constant-initializer. When we call `grokfield', it will
13145 perform more stringent semantics checks. */
058b15c1 13146 if (declarator->kind == cdk_function)
a723baf1
MM
13147 initializer = cp_parser_pure_specifier (parser);
13148 else
4bb8ca28
MM
13149 /* Parse the initializer. */
13150 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13151 }
13152 /* Otherwise, there is no initializer. */
13153 else
13154 initializer = NULL_TREE;
13155
13156 /* See if we are probably looking at a function
13157 definition. We are certainly not looking at at a
13158 member-declarator. Calling `grokfield' has
13159 side-effects, so we must not do it unless we are sure
13160 that we are looking at a member-declarator. */
21526606 13161 if (cp_parser_token_starts_function_definition_p
a723baf1 13162 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13163 {
13164 /* The grammar does not allow a pure-specifier to be
13165 used when a member function is defined. (It is
13166 possible that this fact is an oversight in the
13167 standard, since a pure function may be defined
13168 outside of the class-specifier. */
13169 if (initializer)
13170 error ("pure-specifier on function-definition");
13171 decl = cp_parser_save_member_function_body (parser,
62d1db17 13172 &decl_specifiers,
4bb8ca28
MM
13173 declarator,
13174 attributes);
13175 /* If the member was not a friend, declare it here. */
13176 if (!friend_p)
13177 finish_member_declaration (decl);
13178 /* Peek at the next token. */
13179 token = cp_lexer_peek_token (parser->lexer);
13180 /* If the next token is a semicolon, consume it. */
13181 if (token->type == CPP_SEMICOLON)
13182 cp_lexer_consume_token (parser->lexer);
13183 return;
13184 }
a723baf1 13185 else
39703eb9
MM
13186 {
13187 /* Create the declaration. */
62d1db17 13188 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13189 initializer, asm_specification,
39703eb9
MM
13190 attributes);
13191 /* Any initialization must have been from a
13192 constant-expression. */
13193 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13194 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13195 }
a723baf1
MM
13196 }
13197
13198 /* Reset PREFIX_ATTRIBUTES. */
13199 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13200 attributes = TREE_CHAIN (attributes);
13201 if (attributes)
13202 TREE_CHAIN (attributes) = NULL_TREE;
13203
13204 /* If there is any qualification still in effect, clear it
13205 now; we will be starting fresh with the next declarator. */
13206 parser->scope = NULL_TREE;
13207 parser->qualifying_scope = NULL_TREE;
13208 parser->object_scope = NULL_TREE;
13209 /* If it's a `,', then there are more declarators. */
13210 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13211 cp_lexer_consume_token (parser->lexer);
13212 /* If the next token isn't a `;', then we have a parse error. */
13213 else if (cp_lexer_next_token_is_not (parser->lexer,
13214 CPP_SEMICOLON))
13215 {
13216 cp_parser_error (parser, "expected `;'");
04c06002 13217 /* Skip tokens until we find a `;'. */
a723baf1
MM
13218 cp_parser_skip_to_end_of_statement (parser);
13219
13220 break;
13221 }
13222
13223 if (decl)
13224 {
13225 /* Add DECL to the list of members. */
13226 if (!friend_p)
13227 finish_member_declaration (decl);
13228
a723baf1 13229 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13230 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13231 }
13232 }
13233 }
13234
4bb8ca28 13235 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13236}
13237
13238/* Parse a pure-specifier.
13239
13240 pure-specifier:
13241 = 0
13242
13243 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13244 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13245
13246static tree
94edc4ab 13247cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13248{
13249 cp_token *token;
13250
13251 /* Look for the `=' token. */
13252 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13253 return error_mark_node;
13254 /* Look for the `0' token. */
13255 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13256 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13257 to get information from the lexer about how the number was
13258 spelled in order to fix this problem. */
13259 if (!token || !integer_zerop (token->value))
13260 return error_mark_node;
13261
13262 return integer_zero_node;
13263}
13264
13265/* Parse a constant-initializer.
13266
13267 constant-initializer:
13268 = constant-expression
13269
13270 Returns a representation of the constant-expression. */
13271
13272static tree
94edc4ab 13273cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13274{
13275 /* Look for the `=' token. */
13276 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13277 return error_mark_node;
13278
13279 /* It is invalid to write:
13280
13281 struct S { static const int i = { 7 }; };
13282
13283 */
13284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13285 {
13286 cp_parser_error (parser,
13287 "a brace-enclosed initializer is not allowed here");
13288 /* Consume the opening brace. */
13289 cp_lexer_consume_token (parser->lexer);
13290 /* Skip the initializer. */
13291 cp_parser_skip_to_closing_brace (parser);
13292 /* Look for the trailing `}'. */
13293 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13294
a723baf1
MM
13295 return error_mark_node;
13296 }
13297
21526606 13298 return cp_parser_constant_expression (parser,
14d22dd6
MM
13299 /*allow_non_constant=*/false,
13300 NULL);
a723baf1
MM
13301}
13302
13303/* Derived classes [gram.class.derived] */
13304
13305/* Parse a base-clause.
13306
13307 base-clause:
21526606 13308 : base-specifier-list
a723baf1
MM
13309
13310 base-specifier-list:
13311 base-specifier
13312 base-specifier-list , base-specifier
13313
13314 Returns a TREE_LIST representing the base-classes, in the order in
13315 which they were declared. The representation of each node is as
21526606 13316 described by cp_parser_base_specifier.
a723baf1
MM
13317
13318 In the case that no bases are specified, this function will return
13319 NULL_TREE, not ERROR_MARK_NODE. */
13320
13321static tree
94edc4ab 13322cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13323{
13324 tree bases = NULL_TREE;
13325
13326 /* Look for the `:' that begins the list. */
13327 cp_parser_require (parser, CPP_COLON, "`:'");
13328
13329 /* Scan the base-specifier-list. */
13330 while (true)
13331 {
13332 cp_token *token;
13333 tree base;
13334
13335 /* Look for the base-specifier. */
13336 base = cp_parser_base_specifier (parser);
13337 /* Add BASE to the front of the list. */
13338 if (base != error_mark_node)
13339 {
13340 TREE_CHAIN (base) = bases;
13341 bases = base;
13342 }
13343 /* Peek at the next token. */
13344 token = cp_lexer_peek_token (parser->lexer);
13345 /* If it's not a comma, then the list is complete. */
13346 if (token->type != CPP_COMMA)
13347 break;
13348 /* Consume the `,'. */
13349 cp_lexer_consume_token (parser->lexer);
13350 }
13351
13352 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13353 base class had a qualified name. However, the next name that
13354 appears is certainly not qualified. */
13355 parser->scope = NULL_TREE;
13356 parser->qualifying_scope = NULL_TREE;
13357 parser->object_scope = NULL_TREE;
13358
13359 return nreverse (bases);
13360}
13361
13362/* Parse a base-specifier.
13363
13364 base-specifier:
13365 :: [opt] nested-name-specifier [opt] class-name
13366 virtual access-specifier [opt] :: [opt] nested-name-specifier
13367 [opt] class-name
13368 access-specifier virtual [opt] :: [opt] nested-name-specifier
13369 [opt] class-name
13370
13371 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13372 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13373 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13374 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13375
a723baf1 13376static tree
94edc4ab 13377cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13378{
13379 cp_token *token;
13380 bool done = false;
13381 bool virtual_p = false;
13382 bool duplicate_virtual_error_issued_p = false;
13383 bool duplicate_access_error_issued_p = false;
bbaab916 13384 bool class_scope_p, template_p;
dbbf88d1 13385 tree access = access_default_node;
a723baf1
MM
13386 tree type;
13387
13388 /* Process the optional `virtual' and `access-specifier'. */
13389 while (!done)
13390 {
13391 /* Peek at the next token. */
13392 token = cp_lexer_peek_token (parser->lexer);
13393 /* Process `virtual'. */
13394 switch (token->keyword)
13395 {
13396 case RID_VIRTUAL:
13397 /* If `virtual' appears more than once, issue an error. */
13398 if (virtual_p && !duplicate_virtual_error_issued_p)
13399 {
13400 cp_parser_error (parser,
13401 "`virtual' specified more than once in base-specified");
13402 duplicate_virtual_error_issued_p = true;
13403 }
13404
13405 virtual_p = true;
13406
13407 /* Consume the `virtual' token. */
13408 cp_lexer_consume_token (parser->lexer);
13409
13410 break;
13411
13412 case RID_PUBLIC:
13413 case RID_PROTECTED:
13414 case RID_PRIVATE:
13415 /* If more than one access specifier appears, issue an
13416 error. */
dbbf88d1
NS
13417 if (access != access_default_node
13418 && !duplicate_access_error_issued_p)
a723baf1
MM
13419 {
13420 cp_parser_error (parser,
13421 "more than one access specifier in base-specified");
13422 duplicate_access_error_issued_p = true;
13423 }
13424
dbbf88d1 13425 access = ridpointers[(int) token->keyword];
a723baf1
MM
13426
13427 /* Consume the access-specifier. */
13428 cp_lexer_consume_token (parser->lexer);
13429
13430 break;
13431
13432 default:
13433 done = true;
13434 break;
13435 }
13436 }
852dcbdd 13437 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13438 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13439 as base classes. */
13440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13441 {
13442 if (!processing_template_decl)
13443 error ("keyword `typename' not allowed outside of templates");
13444 else
13445 error ("keyword `typename' not allowed in this context "
13446 "(the base class is implicitly a type)");
13447 cp_lexer_consume_token (parser->lexer);
13448 }
a723baf1 13449
a723baf1
MM
13450 /* Look for the optional `::' operator. */
13451 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13452 /* Look for the nested-name-specifier. The simplest way to
13453 implement:
13454
13455 [temp.res]
13456
13457 The keyword `typename' is not permitted in a base-specifier or
13458 mem-initializer; in these contexts a qualified name that
13459 depends on a template-parameter is implicitly assumed to be a
13460 type name.
13461
13462 is to pretend that we have seen the `typename' keyword at this
21526606 13463 point. */
a723baf1
MM
13464 cp_parser_nested_name_specifier_opt (parser,
13465 /*typename_keyword_p=*/true,
13466 /*check_dependency_p=*/true,
a668c6ad
MM
13467 /*type_p=*/true,
13468 /*is_declaration=*/true);
a723baf1
MM
13469 /* If the base class is given by a qualified name, assume that names
13470 we see are type names or templates, as appropriate. */
13471 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13472 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13473
a723baf1 13474 /* Finally, look for the class-name. */
21526606 13475 type = cp_parser_class_name (parser,
a723baf1 13476 class_scope_p,
bbaab916 13477 template_p,
a723baf1 13478 /*type_p=*/true,
a723baf1 13479 /*check_dependency_p=*/true,
a668c6ad
MM
13480 /*class_head_p=*/false,
13481 /*is_declaration=*/true);
a723baf1
MM
13482
13483 if (type == error_mark_node)
13484 return error_mark_node;
13485
dbbf88d1 13486 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13487}
13488
13489/* Exception handling [gram.exception] */
13490
13491/* Parse an (optional) exception-specification.
13492
13493 exception-specification:
13494 throw ( type-id-list [opt] )
13495
13496 Returns a TREE_LIST representing the exception-specification. The
13497 TREE_VALUE of each node is a type. */
13498
13499static tree
94edc4ab 13500cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13501{
13502 cp_token *token;
13503 tree type_id_list;
13504
13505 /* Peek at the next token. */
13506 token = cp_lexer_peek_token (parser->lexer);
13507 /* If it's not `throw', then there's no exception-specification. */
13508 if (!cp_parser_is_keyword (token, RID_THROW))
13509 return NULL_TREE;
13510
13511 /* Consume the `throw'. */
13512 cp_lexer_consume_token (parser->lexer);
13513
13514 /* Look for the `('. */
13515 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13516
13517 /* Peek at the next token. */
13518 token = cp_lexer_peek_token (parser->lexer);
13519 /* If it's not a `)', then there is a type-id-list. */
13520 if (token->type != CPP_CLOSE_PAREN)
13521 {
13522 const char *saved_message;
13523
13524 /* Types may not be defined in an exception-specification. */
13525 saved_message = parser->type_definition_forbidden_message;
13526 parser->type_definition_forbidden_message
13527 = "types may not be defined in an exception-specification";
13528 /* Parse the type-id-list. */
13529 type_id_list = cp_parser_type_id_list (parser);
13530 /* Restore the saved message. */
13531 parser->type_definition_forbidden_message = saved_message;
13532 }
13533 else
13534 type_id_list = empty_except_spec;
13535
13536 /* Look for the `)'. */
13537 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13538
13539 return type_id_list;
13540}
13541
13542/* Parse an (optional) type-id-list.
13543
13544 type-id-list:
13545 type-id
13546 type-id-list , type-id
13547
13548 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13549 in the order that the types were presented. */
13550
13551static tree
94edc4ab 13552cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13553{
13554 tree types = NULL_TREE;
13555
13556 while (true)
13557 {
13558 cp_token *token;
13559 tree type;
13560
13561 /* Get the next type-id. */
13562 type = cp_parser_type_id (parser);
13563 /* Add it to the list. */
13564 types = add_exception_specifier (types, type, /*complain=*/1);
13565 /* Peek at the next token. */
13566 token = cp_lexer_peek_token (parser->lexer);
13567 /* If it is not a `,', we are done. */
13568 if (token->type != CPP_COMMA)
13569 break;
13570 /* Consume the `,'. */
13571 cp_lexer_consume_token (parser->lexer);
13572 }
13573
13574 return nreverse (types);
13575}
13576
13577/* Parse a try-block.
13578
13579 try-block:
13580 try compound-statement handler-seq */
13581
13582static tree
94edc4ab 13583cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13584{
13585 tree try_block;
13586
13587 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13588 try_block = begin_try_block ();
325c3691 13589 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13590 finish_try_block (try_block);
13591 cp_parser_handler_seq (parser);
13592 finish_handler_sequence (try_block);
13593
13594 return try_block;
13595}
13596
13597/* Parse a function-try-block.
13598
13599 function-try-block:
13600 try ctor-initializer [opt] function-body handler-seq */
13601
13602static bool
94edc4ab 13603cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13604{
13605 tree try_block;
13606 bool ctor_initializer_p;
13607
13608 /* Look for the `try' keyword. */
13609 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13610 return false;
13611 /* Let the rest of the front-end know where we are. */
13612 try_block = begin_function_try_block ();
13613 /* Parse the function-body. */
21526606 13614 ctor_initializer_p
a723baf1
MM
13615 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13616 /* We're done with the `try' part. */
13617 finish_function_try_block (try_block);
13618 /* Parse the handlers. */
13619 cp_parser_handler_seq (parser);
13620 /* We're done with the handlers. */
13621 finish_function_handler_sequence (try_block);
13622
13623 return ctor_initializer_p;
13624}
13625
13626/* Parse a handler-seq.
13627
13628 handler-seq:
13629 handler handler-seq [opt] */
13630
13631static void
94edc4ab 13632cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13633{
13634 while (true)
13635 {
13636 cp_token *token;
13637
13638 /* Parse the handler. */
13639 cp_parser_handler (parser);
13640 /* Peek at the next token. */
13641 token = cp_lexer_peek_token (parser->lexer);
13642 /* If it's not `catch' then there are no more handlers. */
13643 if (!cp_parser_is_keyword (token, RID_CATCH))
13644 break;
13645 }
13646}
13647
13648/* Parse a handler.
13649
13650 handler:
13651 catch ( exception-declaration ) compound-statement */
13652
13653static void
94edc4ab 13654cp_parser_handler (cp_parser* parser)
a723baf1
MM
13655{
13656 tree handler;
13657 tree declaration;
13658
13659 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13660 handler = begin_handler ();
13661 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13662 declaration = cp_parser_exception_declaration (parser);
13663 finish_handler_parms (declaration, handler);
13664 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 13665 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
13666 finish_handler (handler);
13667}
13668
13669/* Parse an exception-declaration.
13670
13671 exception-declaration:
13672 type-specifier-seq declarator
13673 type-specifier-seq abstract-declarator
13674 type-specifier-seq
21526606 13675 ...
a723baf1
MM
13676
13677 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13678 ellipsis variant is used. */
13679
13680static tree
94edc4ab 13681cp_parser_exception_declaration (cp_parser* parser)
a723baf1 13682{
058b15c1 13683 tree decl;
62d1db17 13684 cp_decl_specifier_seq type_specifiers;
058b15c1 13685 cp_declarator *declarator;
a723baf1
MM
13686 const char *saved_message;
13687
13688 /* If it's an ellipsis, it's easy to handle. */
13689 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13690 {
13691 /* Consume the `...' token. */
13692 cp_lexer_consume_token (parser->lexer);
13693 return NULL_TREE;
13694 }
13695
13696 /* Types may not be defined in exception-declarations. */
13697 saved_message = parser->type_definition_forbidden_message;
13698 parser->type_definition_forbidden_message
13699 = "types may not be defined in exception-declarations";
13700
13701 /* Parse the type-specifier-seq. */
62d1db17 13702 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
13703 /* If it's a `)', then there is no declarator. */
13704 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 13705 declarator = NULL;
a723baf1 13706 else
62b8a44e 13707 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28
MM
13708 /*ctor_dtor_or_conv_p=*/NULL,
13709 /*parenthesized_p=*/NULL);
a723baf1
MM
13710
13711 /* Restore the saved message. */
13712 parser->type_definition_forbidden_message = saved_message;
13713
62d1db17 13714 if (type_specifiers.any_specifiers_p)
058b15c1 13715 {
62d1db17 13716 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
13717 if (decl == NULL_TREE)
13718 error ("invalid catch parameter");
13719 }
13720 else
13721 decl = NULL_TREE;
13722
13723 return decl;
a723baf1
MM
13724}
13725
21526606 13726/* Parse a throw-expression.
a723baf1
MM
13727
13728 throw-expression:
34cd5ae7 13729 throw assignment-expression [opt]
a723baf1
MM
13730
13731 Returns a THROW_EXPR representing the throw-expression. */
13732
13733static tree
94edc4ab 13734cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13735{
13736 tree expression;
89f1a6ec 13737 cp_token* token;
a723baf1
MM
13738
13739 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13740 token = cp_lexer_peek_token (parser->lexer);
13741 /* Figure out whether or not there is an assignment-expression
13742 following the "throw" keyword. */
13743 if (token->type == CPP_COMMA
13744 || token->type == CPP_SEMICOLON
13745 || token->type == CPP_CLOSE_PAREN
13746 || token->type == CPP_CLOSE_SQUARE
13747 || token->type == CPP_CLOSE_BRACE
13748 || token->type == CPP_COLON)
a723baf1 13749 expression = NULL_TREE;
89f1a6ec
MM
13750 else
13751 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13752
13753 return build_throw (expression);
13754}
13755
13756/* GNU Extensions */
13757
13758/* Parse an (optional) asm-specification.
13759
13760 asm-specification:
13761 asm ( string-literal )
13762
13763 If the asm-specification is present, returns a STRING_CST
13764 corresponding to the string-literal. Otherwise, returns
13765 NULL_TREE. */
13766
13767static tree
94edc4ab 13768cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13769{
13770 cp_token *token;
13771 tree asm_specification;
13772
13773 /* Peek at the next token. */
13774 token = cp_lexer_peek_token (parser->lexer);
21526606 13775 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13776 asm-specification. */
13777 if (!cp_parser_is_keyword (token, RID_ASM))
13778 return NULL_TREE;
13779
13780 /* Consume the `asm' token. */
13781 cp_lexer_consume_token (parser->lexer);
13782 /* Look for the `('. */
13783 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13784
13785 /* Look for the string-literal. */
13786 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13787 if (token)
13788 asm_specification = token->value;
13789 else
13790 asm_specification = NULL_TREE;
13791
13792 /* Look for the `)'. */
13793 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13794
13795 return asm_specification;
13796}
13797
21526606 13798/* Parse an asm-operand-list.
a723baf1
MM
13799
13800 asm-operand-list:
13801 asm-operand
13802 asm-operand-list , asm-operand
21526606 13803
a723baf1 13804 asm-operand:
21526606 13805 string-literal ( expression )
a723baf1
MM
13806 [ string-literal ] string-literal ( expression )
13807
13808 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13809 each node is the expression. The TREE_PURPOSE is itself a
13810 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13811 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13812 is a STRING_CST for the string literal before the parenthesis. */
13813
13814static tree
94edc4ab 13815cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13816{
13817 tree asm_operands = NULL_TREE;
13818
13819 while (true)
13820 {
13821 tree string_literal;
13822 tree expression;
13823 tree name;
13824 cp_token *token;
21526606 13825
21526606 13826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13827 {
13828 /* Consume the `[' token. */
13829 cp_lexer_consume_token (parser->lexer);
13830 /* Read the operand name. */
13831 name = cp_parser_identifier (parser);
21526606 13832 if (name != error_mark_node)
a723baf1
MM
13833 name = build_string (IDENTIFIER_LENGTH (name),
13834 IDENTIFIER_POINTER (name));
13835 /* Look for the closing `]'. */
13836 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13837 }
13838 else
13839 name = NULL_TREE;
13840 /* Look for the string-literal. */
13841 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13842 string_literal = token ? token->value : error_mark_node;
0173bb6f 13843 c_lex_string_translate = 1;
a723baf1
MM
13844 /* Look for the `('. */
13845 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13846 /* Parse the expression. */
13847 expression = cp_parser_expression (parser);
13848 /* Look for the `)'. */
13849 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
0173bb6f 13850 c_lex_string_translate = 0;
a723baf1
MM
13851 /* Add this operand to the list. */
13852 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13853 expression,
a723baf1 13854 asm_operands);
21526606 13855 /* If the next token is not a `,', there are no more
a723baf1
MM
13856 operands. */
13857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13858 break;
13859 /* Consume the `,'. */
13860 cp_lexer_consume_token (parser->lexer);
13861 }
13862
13863 return nreverse (asm_operands);
13864}
13865
21526606 13866/* Parse an asm-clobber-list.
a723baf1
MM
13867
13868 asm-clobber-list:
13869 string-literal
21526606 13870 asm-clobber-list , string-literal
a723baf1
MM
13871
13872 Returns a TREE_LIST, indicating the clobbers in the order that they
13873 appeared. The TREE_VALUE of each node is a STRING_CST. */
13874
13875static tree
94edc4ab 13876cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13877{
13878 tree clobbers = NULL_TREE;
13879
13880 while (true)
13881 {
13882 cp_token *token;
13883 tree string_literal;
13884
13885 /* Look for the string literal. */
13886 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13887 string_literal = token ? token->value : error_mark_node;
13888 /* Add it to the list. */
13889 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13890 /* If the next token is not a `,', then the list is
a723baf1
MM
13891 complete. */
13892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13893 break;
13894 /* Consume the `,' token. */
13895 cp_lexer_consume_token (parser->lexer);
13896 }
13897
13898 return clobbers;
13899}
13900
13901/* Parse an (optional) series of attributes.
13902
13903 attributes:
13904 attributes attribute
13905
13906 attribute:
21526606 13907 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13908
13909 The return value is as for cp_parser_attribute_list. */
21526606 13910
a723baf1 13911static tree
94edc4ab 13912cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13913{
13914 tree attributes = NULL_TREE;
13915
13916 while (true)
13917 {
13918 cp_token *token;
13919 tree attribute_list;
13920
13921 /* Peek at the next token. */
13922 token = cp_lexer_peek_token (parser->lexer);
13923 /* If it's not `__attribute__', then we're done. */
13924 if (token->keyword != RID_ATTRIBUTE)
13925 break;
13926
13927 /* Consume the `__attribute__' keyword. */
13928 cp_lexer_consume_token (parser->lexer);
13929 /* Look for the two `(' tokens. */
13930 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13931 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13932
13933 /* Peek at the next token. */
13934 token = cp_lexer_peek_token (parser->lexer);
13935 if (token->type != CPP_CLOSE_PAREN)
13936 /* Parse the attribute-list. */
13937 attribute_list = cp_parser_attribute_list (parser);
13938 else
13939 /* If the next token is a `)', then there is no attribute
13940 list. */
13941 attribute_list = NULL;
13942
13943 /* Look for the two `)' tokens. */
13944 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13946
13947 /* Add these new attributes to the list. */
13948 attributes = chainon (attributes, attribute_list);
13949 }
13950
13951 return attributes;
13952}
13953
21526606 13954/* Parse an attribute-list.
a723baf1 13955
21526606
EC
13956 attribute-list:
13957 attribute
a723baf1
MM
13958 attribute-list , attribute
13959
13960 attribute:
21526606 13961 identifier
a723baf1
MM
13962 identifier ( identifier )
13963 identifier ( identifier , expression-list )
21526606 13964 identifier ( expression-list )
a723baf1
MM
13965
13966 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13967 TREE_PURPOSE of each node is the identifier indicating which
13968 attribute is in use. The TREE_VALUE represents the arguments, if
13969 any. */
13970
13971static tree
94edc4ab 13972cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13973{
13974 tree attribute_list = NULL_TREE;
13975
0173bb6f 13976 c_lex_string_translate = 0;
a723baf1
MM
13977 while (true)
13978 {
13979 cp_token *token;
13980 tree identifier;
13981 tree attribute;
13982
13983 /* Look for the identifier. We also allow keywords here; for
13984 example `__attribute__ ((const))' is legal. */
13985 token = cp_lexer_peek_token (parser->lexer);
21526606 13986 if (token->type != CPP_NAME
a723baf1
MM
13987 && token->type != CPP_KEYWORD)
13988 return error_mark_node;
13989 /* Consume the token. */
13990 token = cp_lexer_consume_token (parser->lexer);
21526606 13991
a723baf1
MM
13992 /* Save away the identifier that indicates which attribute this is. */
13993 identifier = token->value;
13994 attribute = build_tree_list (identifier, NULL_TREE);
13995
13996 /* Peek at the next token. */
13997 token = cp_lexer_peek_token (parser->lexer);
13998 /* If it's an `(', then parse the attribute arguments. */
13999 if (token->type == CPP_OPEN_PAREN)
14000 {
14001 tree arguments;
a723baf1 14002
21526606 14003 arguments = (cp_parser_parenthesized_expression_list
39703eb9 14004 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
14005 /* Save the identifier and arguments away. */
14006 TREE_VALUE (attribute) = arguments;
a723baf1
MM
14007 }
14008
14009 /* Add this attribute to the list. */
14010 TREE_CHAIN (attribute) = attribute_list;
14011 attribute_list = attribute;
14012
14013 /* Now, look for more attributes. */
14014 token = cp_lexer_peek_token (parser->lexer);
14015 /* If the next token isn't a `,', we're done. */
14016 if (token->type != CPP_COMMA)
14017 break;
14018
cd0be382 14019 /* Consume the comma and keep going. */
a723baf1
MM
14020 cp_lexer_consume_token (parser->lexer);
14021 }
0173bb6f 14022 c_lex_string_translate = 1;
a723baf1
MM
14023
14024 /* We built up the list in reverse order. */
14025 return nreverse (attribute_list);
14026}
14027
14028/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14029 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14030 current value of the PEDANTIC flag, regardless of whether or not
14031 the `__extension__' keyword is present. The caller is responsible
14032 for restoring the value of the PEDANTIC flag. */
14033
14034static bool
94edc4ab 14035cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14036{
14037 /* Save the old value of the PEDANTIC flag. */
14038 *saved_pedantic = pedantic;
14039
14040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14041 {
14042 /* Consume the `__extension__' token. */
14043 cp_lexer_consume_token (parser->lexer);
14044 /* We're not being pedantic while the `__extension__' keyword is
14045 in effect. */
14046 pedantic = 0;
14047
14048 return true;
14049 }
14050
14051 return false;
14052}
14053
14054/* Parse a label declaration.
14055
14056 label-declaration:
14057 __label__ label-declarator-seq ;
14058
14059 label-declarator-seq:
14060 identifier , label-declarator-seq
14061 identifier */
14062
14063static void
94edc4ab 14064cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14065{
14066 /* Look for the `__label__' keyword. */
14067 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14068
14069 while (true)
14070 {
14071 tree identifier;
14072
14073 /* Look for an identifier. */
14074 identifier = cp_parser_identifier (parser);
14075 /* Declare it as a lobel. */
14076 finish_label_decl (identifier);
14077 /* If the next token is a `;', stop. */
14078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14079 break;
14080 /* Look for the `,' separating the label declarations. */
14081 cp_parser_require (parser, CPP_COMMA, "`,'");
14082 }
14083
14084 /* Look for the final `;'. */
14085 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14086}
14087
14088/* Support Functions */
14089
14090/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14091 NAME should have one of the representations used for an
14092 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14093 is returned. If PARSER->SCOPE is a dependent type, then a
14094 SCOPE_REF is returned.
14095
14096 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14097 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14098 was formed. Abstractly, such entities should not be passed to this
14099 function, because they do not need to be looked up, but it is
14100 simpler to check for this special case here, rather than at the
14101 call-sites.
14102
14103 In cases not explicitly covered above, this function returns a
14104 DECL, OVERLOAD, or baselink representing the result of the lookup.
14105 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14106 is returned.
14107
a723baf1
MM
14108 If IS_TYPE is TRUE, bindings that do not refer to types are
14109 ignored.
14110
b0bc6e8e
KL
14111 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14112 ignored.
14113
eea9800f
MM
14114 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14115 are ignored.
14116
a723baf1
MM
14117 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14118 types. */
14119
14120static tree
21526606 14121cp_parser_lookup_name (cp_parser *parser, tree name,
b0bc6e8e
KL
14122 bool is_type, bool is_template, bool is_namespace,
14123 bool check_dependency)
a723baf1
MM
14124{
14125 tree decl;
14126 tree object_type = parser->context->object_type;
14127
14128 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14129 no longer valid. Note that if we are parsing tentatively, and
14130 the parse fails, OBJECT_TYPE will be automatically restored. */
14131 parser->context->object_type = NULL_TREE;
14132
14133 if (name == error_mark_node)
14134 return error_mark_node;
14135
14136 /* A template-id has already been resolved; there is no lookup to
14137 do. */
14138 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14139 return name;
14140 if (BASELINK_P (name))
14141 {
14142 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
14143 == TEMPLATE_ID_EXPR),
14144 20020909);
14145 return name;
14146 }
14147
14148 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14149 it should already have been checked to make sure that the name
14150 used matches the type being destroyed. */
14151 if (TREE_CODE (name) == BIT_NOT_EXPR)
14152 {
14153 tree type;
14154
14155 /* Figure out to which type this destructor applies. */
14156 if (parser->scope)
14157 type = parser->scope;
14158 else if (object_type)
14159 type = object_type;
14160 else
14161 type = current_class_type;
14162 /* If that's not a class type, there is no destructor. */
14163 if (!type || !CLASS_TYPE_P (type))
14164 return error_mark_node;
fd6e3cce
GB
14165 if (!CLASSTYPE_DESTRUCTORS (type))
14166 return error_mark_node;
a723baf1
MM
14167 /* If it was a class type, return the destructor. */
14168 return CLASSTYPE_DESTRUCTORS (type);
14169 }
14170
14171 /* By this point, the NAME should be an ordinary identifier. If
14172 the id-expression was a qualified name, the qualifying scope is
14173 stored in PARSER->SCOPE at this point. */
14174 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
14175 20000619);
21526606 14176
a723baf1
MM
14177 /* Perform the lookup. */
14178 if (parser->scope)
21526606 14179 {
1fb3244a 14180 bool dependent_p;
a723baf1
MM
14181
14182 if (parser->scope == error_mark_node)
14183 return error_mark_node;
14184
14185 /* If the SCOPE is dependent, the lookup must be deferred until
14186 the template is instantiated -- unless we are explicitly
14187 looking up names in uninstantiated templates. Even then, we
14188 cannot look up the name if the scope is not a class type; it
14189 might, for example, be a template type parameter. */
1fb3244a
MM
14190 dependent_p = (TYPE_P (parser->scope)
14191 && !(parser->in_declarator_p
14192 && currently_open_class (parser->scope))
14193 && dependent_type_p (parser->scope));
a723baf1 14194 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14195 && dependent_p)
a723baf1 14196 {
b0bc6e8e 14197 if (is_type)
a723baf1
MM
14198 /* The resolution to Core Issue 180 says that `struct A::B'
14199 should be considered a type-name, even if `A' is
14200 dependent. */
14201 decl = TYPE_NAME (make_typename_type (parser->scope,
14202 name,
14203 /*complain=*/1));
b0bc6e8e 14204 else if (is_template)
5b4acce1
KL
14205 decl = make_unbound_class_template (parser->scope,
14206 name,
14207 /*complain=*/1);
b0bc6e8e
KL
14208 else
14209 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
14210 }
14211 else
14212 {
91b004e5
MM
14213 bool pop_p = false;
14214
a723baf1
MM
14215 /* If PARSER->SCOPE is a dependent type, then it must be a
14216 class type, and we must not be checking dependencies;
14217 otherwise, we would have processed this lookup above. So
14218 that PARSER->SCOPE is not considered a dependent base by
14219 lookup_member, we must enter the scope here. */
1fb3244a 14220 if (dependent_p)
91b004e5 14221 pop_p = push_scope (parser->scope);
a723baf1
MM
14222 /* If the PARSER->SCOPE is a a template specialization, it
14223 may be instantiated during name lookup. In that case,
14224 errors may be issued. Even if we rollback the current
14225 tentative parse, those errors are valid. */
5e08432e
MM
14226 decl = lookup_qualified_name (parser->scope, name, is_type,
14227 /*complain=*/true);
91b004e5 14228 if (pop_p)
a723baf1
MM
14229 pop_scope (parser->scope);
14230 }
14231 parser->qualifying_scope = parser->scope;
14232 parser->object_scope = NULL_TREE;
14233 }
14234 else if (object_type)
14235 {
14236 tree object_decl = NULL_TREE;
14237 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14238 OBJECT_TYPE is not a class. */
14239 if (CLASS_TYPE_P (object_type))
14240 /* If the OBJECT_TYPE is a template specialization, it may
14241 be instantiated during name lookup. In that case, errors
14242 may be issued. Even if we rollback the current tentative
14243 parse, those errors are valid. */
14244 object_decl = lookup_member (object_type,
14245 name,
14246 /*protect=*/0, is_type);
14247 /* Look it up in the enclosing context, too. */
21526606 14248 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 14249 is_namespace,
a723baf1
MM
14250 /*flags=*/0);
14251 parser->object_scope = object_type;
14252 parser->qualifying_scope = NULL_TREE;
14253 if (object_decl)
14254 decl = object_decl;
14255 }
14256 else
14257 {
21526606 14258 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 14259 is_namespace,
a723baf1
MM
14260 /*flags=*/0);
14261 parser->qualifying_scope = NULL_TREE;
14262 parser->object_scope = NULL_TREE;
14263 }
14264
14265 /* If the lookup failed, let our caller know. */
21526606 14266 if (!decl
a723baf1 14267 || decl == error_mark_node
21526606 14268 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
14269 && DECL_ANTICIPATED (decl)))
14270 return error_mark_node;
14271
14272 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14273 if (TREE_CODE (decl) == TREE_LIST)
14274 {
14275 /* The error message we have to print is too complicated for
14276 cp_parser_error, so we incorporate its actions directly. */
e5976695 14277 if (!cp_parser_simulate_error (parser))
a723baf1
MM
14278 {
14279 error ("reference to `%D' is ambiguous", name);
14280 print_candidates (decl);
14281 }
14282 return error_mark_node;
14283 }
14284
21526606 14285 my_friendly_assert (DECL_P (decl)
a723baf1
MM
14286 || TREE_CODE (decl) == OVERLOAD
14287 || TREE_CODE (decl) == SCOPE_REF
5b4acce1 14288 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
a723baf1
MM
14289 || BASELINK_P (decl),
14290 20000619);
14291
14292 /* If we have resolved the name of a member declaration, check to
14293 see if the declaration is accessible. When the name resolves to
34cd5ae7 14294 set of overloaded functions, accessibility is checked when
21526606 14295 overload resolution is done.
a723baf1
MM
14296
14297 During an explicit instantiation, access is not checked at all,
14298 as per [temp.explicit]. */
8d241e0b 14299 if (DECL_P (decl))
ee76b931 14300 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
a723baf1
MM
14301
14302 return decl;
14303}
14304
14305/* Like cp_parser_lookup_name, but for use in the typical case where
b0bc6e8e
KL
14306 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14307 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
a723baf1
MM
14308
14309static tree
94edc4ab 14310cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1 14311{
21526606 14312 return cp_parser_lookup_name (parser, name,
eea9800f 14313 /*is_type=*/false,
b0bc6e8e 14314 /*is_template=*/false,
eea9800f 14315 /*is_namespace=*/false,
a723baf1
MM
14316 /*check_dependency=*/true);
14317}
14318
a723baf1
MM
14319/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14320 the current context, return the TYPE_DECL. If TAG_NAME_P is
14321 true, the DECL indicates the class being defined in a class-head,
14322 or declared in an elaborated-type-specifier.
14323
14324 Otherwise, return DECL. */
14325
14326static tree
14327cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14328{
710b73e6
KL
14329 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14330 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14331
21526606 14332 struct A {
a723baf1
MM
14333 template <typename T> struct B;
14334 };
14335
21526606
EC
14336 template <typename T> struct A::B {};
14337
a723baf1
MM
14338 Similarly, in a elaborated-type-specifier:
14339
14340 namespace N { struct X{}; }
14341
14342 struct A {
14343 template <typename T> friend struct N::X;
14344 };
14345
710b73e6
KL
14346 However, if the DECL refers to a class type, and we are in
14347 the scope of the class, then the name lookup automatically
14348 finds the TYPE_DECL created by build_self_reference rather
14349 than a TEMPLATE_DECL. For example, in:
14350
14351 template <class T> struct S {
14352 S s;
14353 };
14354
14355 there is no need to handle such case. */
14356
14357 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14358 return DECL_TEMPLATE_RESULT (decl);
14359
14360 return decl;
14361}
14362
14363/* If too many, or too few, template-parameter lists apply to the
14364 declarator, issue an error message. Returns TRUE if all went well,
14365 and FALSE otherwise. */
14366
14367static bool
21526606 14368cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14369 cp_declarator *declarator)
a723baf1
MM
14370{
14371 unsigned num_templates;
14372
14373 /* We haven't seen any classes that involve template parameters yet. */
14374 num_templates = 0;
14375
058b15c1 14376 switch (declarator->kind)
a723baf1 14377 {
058b15c1
MM
14378 case cdk_id:
14379 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14380 {
14381 tree scope;
14382 tree member;
a723baf1 14383
058b15c1
MM
14384 scope = TREE_OPERAND (declarator->u.id.name, 0);
14385 member = TREE_OPERAND (declarator->u.id.name, 1);
a723baf1 14386
058b15c1
MM
14387 while (scope && CLASS_TYPE_P (scope))
14388 {
14389 /* You're supposed to have one `template <...>'
14390 for every template class, but you don't need one
14391 for a full specialization. For example:
14392
14393 template <class T> struct S{};
14394 template <> struct S<int> { void f(); };
14395 void S<int>::f () {}
14396
14397 is correct; there shouldn't be a `template <>' for
14398 the definition of `S<int>::f'. */
14399 if (CLASSTYPE_TEMPLATE_INFO (scope)
14400 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14401 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14402 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14403 ++num_templates;
14404
14405 scope = TYPE_CONTEXT (scope);
14406 }
14407 }
a723baf1 14408
a723baf1
MM
14409 /* If the DECLARATOR has the form `X<y>' then it uses one
14410 additional level of template parameters. */
058b15c1 14411 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
a723baf1
MM
14412 ++num_templates;
14413
21526606 14414 return cp_parser_check_template_parameters (parser,
a723baf1 14415 num_templates);
058b15c1
MM
14416
14417 case cdk_function:
14418 case cdk_array:
14419 case cdk_pointer:
14420 case cdk_reference:
14421 case cdk_ptrmem:
14422 return (cp_parser_check_declarator_template_parameters
14423 (parser, declarator->declarator));
14424
14425 case cdk_error:
14426 return true;
14427
14428 default:
14429 abort ();
14430 return false;
a723baf1
MM
14431 }
14432}
14433
14434/* NUM_TEMPLATES were used in the current declaration. If that is
14435 invalid, return FALSE and issue an error messages. Otherwise,
14436 return TRUE. */
14437
14438static bool
94edc4ab
NN
14439cp_parser_check_template_parameters (cp_parser* parser,
14440 unsigned num_templates)
a723baf1
MM
14441{
14442 /* If there are more template classes than parameter lists, we have
14443 something like:
21526606 14444
a723baf1
MM
14445 template <class T> void S<T>::R<T>::f (); */
14446 if (parser->num_template_parameter_lists < num_templates)
14447 {
14448 error ("too few template-parameter-lists");
14449 return false;
14450 }
14451 /* If there are the same number of template classes and parameter
14452 lists, that's OK. */
14453 if (parser->num_template_parameter_lists == num_templates)
14454 return true;
14455 /* If there are more, but only one more, then we are referring to a
14456 member template. That's OK too. */
14457 if (parser->num_template_parameter_lists == num_templates + 1)
14458 return true;
14459 /* Otherwise, there are too many template parameter lists. We have
14460 something like:
14461
14462 template <class T> template <class U> void S::f(); */
14463 error ("too many template-parameter-lists");
14464 return false;
14465}
14466
14467/* Parse a binary-expression of the general form:
14468
14469 binary-expression:
14470 <expr>
14471 binary-expression <token> <expr>
14472
14473 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14474 to parser the <expr>s. If the first production is used, then the
14475 value returned by FN is returned directly. Otherwise, a node with
14476 the indicated EXPR_TYPE is returned, with operands corresponding to
14477 the two sub-expressions. */
14478
14479static tree
21526606
EC
14480cp_parser_binary_expression (cp_parser* parser,
14481 const cp_parser_token_tree_map token_tree_map,
94edc4ab 14482 cp_parser_expression_fn fn)
a723baf1
MM
14483{
14484 tree lhs;
14485
14486 /* Parse the first expression. */
14487 lhs = (*fn) (parser);
14488 /* Now, look for more expressions. */
14489 while (true)
14490 {
14491 cp_token *token;
39b1af70 14492 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
14493 tree rhs;
14494
14495 /* Peek at the next token. */
14496 token = cp_lexer_peek_token (parser->lexer);
14497 /* If the token is `>', and that's not an operator at the
14498 moment, then we're done. */
14499 if (token->type == CPP_GREATER
14500 && !parser->greater_than_is_operator_p)
14501 break;
34cd5ae7 14502 /* If we find one of the tokens we want, build the corresponding
a723baf1 14503 tree representation. */
21526606 14504 for (map_node = token_tree_map;
a723baf1
MM
14505 map_node->token_type != CPP_EOF;
14506 ++map_node)
14507 if (map_node->token_type == token->type)
14508 {
ec835fb2
MM
14509 /* Assume that an overloaded operator will not be used. */
14510 bool overloaded_p = false;
14511
a723baf1
MM
14512 /* Consume the operator token. */
14513 cp_lexer_consume_token (parser->lexer);
14514 /* Parse the right-hand side of the expression. */
14515 rhs = (*fn) (parser);
14516 /* Build the binary tree node. */
ec835fb2
MM
14517 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14518 &overloaded_p);
14519 /* If the binary operator required the use of an
14520 overloaded operator, then this expression cannot be an
14521 integral constant-expression. An overloaded operator
14522 can be used even if both operands are otherwise
14523 permissible in an integral constant-expression if at
14524 least one of the operands is of enumeration type. */
14525 if (overloaded_p
14526 && (cp_parser_non_integral_constant_expression
14527 (parser, "calls to overloaded operators")))
14528 lhs = error_mark_node;
a723baf1
MM
14529 break;
14530 }
14531
14532 /* If the token wasn't one of the ones we want, we're done. */
14533 if (map_node->token_type == CPP_EOF)
14534 break;
14535 }
14536
14537 return lhs;
14538}
14539
14540/* Parse an optional `::' token indicating that the following name is
14541 from the global namespace. If so, PARSER->SCOPE is set to the
14542 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14543 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14544 Returns the new value of PARSER->SCOPE, if the `::' token is
14545 present, and NULL_TREE otherwise. */
14546
14547static tree
94edc4ab 14548cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14549{
14550 cp_token *token;
14551
14552 /* Peek at the next token. */
14553 token = cp_lexer_peek_token (parser->lexer);
14554 /* If we're looking at a `::' token then we're starting from the
14555 global namespace, not our current location. */
14556 if (token->type == CPP_SCOPE)
14557 {
14558 /* Consume the `::' token. */
14559 cp_lexer_consume_token (parser->lexer);
14560 /* Set the SCOPE so that we know where to start the lookup. */
14561 parser->scope = global_namespace;
14562 parser->qualifying_scope = global_namespace;
14563 parser->object_scope = NULL_TREE;
14564
14565 return parser->scope;
14566 }
14567 else if (!current_scope_valid_p)
14568 {
14569 parser->scope = NULL_TREE;
14570 parser->qualifying_scope = NULL_TREE;
14571 parser->object_scope = NULL_TREE;
14572 }
14573
14574 return NULL_TREE;
14575}
14576
14577/* Returns TRUE if the upcoming token sequence is the start of a
14578 constructor declarator. If FRIEND_P is true, the declarator is
14579 preceded by the `friend' specifier. */
14580
14581static bool
14582cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14583{
14584 bool constructor_p;
14585 tree type_decl = NULL_TREE;
14586 bool nested_name_p;
2050a1bb
MM
14587 cp_token *next_token;
14588
14589 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14590 try to avoid doing lots of work if at all possible. It's not
14591 valid declare a constructor at function scope. */
14592 if (at_function_scope_p ())
14593 return false;
14594 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14595 next_token = cp_lexer_peek_token (parser->lexer);
14596 if (next_token->type != CPP_NAME
14597 && next_token->type != CPP_SCOPE
14598 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14599 && next_token->type != CPP_TEMPLATE_ID)
14600 return false;
a723baf1
MM
14601
14602 /* Parse tentatively; we are going to roll back all of the tokens
14603 consumed here. */
14604 cp_parser_parse_tentatively (parser);
14605 /* Assume that we are looking at a constructor declarator. */
14606 constructor_p = true;
8d241e0b 14607
a723baf1
MM
14608 /* Look for the optional `::' operator. */
14609 cp_parser_global_scope_opt (parser,
14610 /*current_scope_valid_p=*/false);
14611 /* Look for the nested-name-specifier. */
21526606 14612 nested_name_p
a723baf1
MM
14613 = (cp_parser_nested_name_specifier_opt (parser,
14614 /*typename_keyword_p=*/false,
14615 /*check_dependency_p=*/false,
a668c6ad
MM
14616 /*type_p=*/false,
14617 /*is_declaration=*/false)
a723baf1
MM
14618 != NULL_TREE);
14619 /* Outside of a class-specifier, there must be a
14620 nested-name-specifier. */
21526606 14621 if (!nested_name_p &&
a723baf1
MM
14622 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14623 || friend_p))
14624 constructor_p = false;
14625 /* If we still think that this might be a constructor-declarator,
14626 look for a class-name. */
14627 if (constructor_p)
14628 {
14629 /* If we have:
14630
8fbc5ae7 14631 template <typename T> struct S { S(); };
a723baf1
MM
14632 template <typename T> S<T>::S ();
14633
14634 we must recognize that the nested `S' names a class.
14635 Similarly, for:
14636
14637 template <typename T> S<T>::S<T> ();
14638
14639 we must recognize that the nested `S' names a template. */
14640 type_decl = cp_parser_class_name (parser,
14641 /*typename_keyword_p=*/false,
14642 /*template_keyword_p=*/false,
14643 /*type_p=*/false,
a723baf1 14644 /*check_dependency_p=*/false,
a668c6ad
MM
14645 /*class_head_p=*/false,
14646 /*is_declaration=*/false);
a723baf1
MM
14647 /* If there was no class-name, then this is not a constructor. */
14648 constructor_p = !cp_parser_error_occurred (parser);
14649 }
8d241e0b 14650
a723baf1
MM
14651 /* If we're still considering a constructor, we have to see a `(',
14652 to begin the parameter-declaration-clause, followed by either a
14653 `)', an `...', or a decl-specifier. We need to check for a
14654 type-specifier to avoid being fooled into thinking that:
14655
14656 S::S (f) (int);
14657
14658 is a constructor. (It is actually a function named `f' that
14659 takes one parameter (of type `int') and returns a value of type
14660 `S::S'. */
21526606 14661 if (constructor_p
a723baf1
MM
14662 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14663 {
14664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14665 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14666 /* A parameter declaration begins with a decl-specifier,
14667 which is either the "attribute" keyword, a storage class
14668 specifier, or (usually) a type-specifier. */
14669 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14670 && !cp_parser_storage_class_specifier_opt (parser))
14671 {
5dae1114 14672 tree type;
91b004e5 14673 bool pop_p = false;
4047b164 14674 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14675
14676 /* Names appearing in the type-specifier should be looked up
14677 in the scope of the class. */
14678 if (current_class_type)
14679 type = NULL_TREE;
a723baf1
MM
14680 else
14681 {
5dae1114
MM
14682 type = TREE_TYPE (type_decl);
14683 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14684 {
21526606 14685 type = resolve_typename_type (type,
14d22dd6
MM
14686 /*only_current_p=*/false);
14687 if (type == error_mark_node)
14688 {
14689 cp_parser_abort_tentative_parse (parser);
14690 return false;
14691 }
14692 }
91b004e5 14693 pop_p = push_scope (type);
a723baf1 14694 }
4047b164
KL
14695
14696 /* Inside the constructor parameter list, surrounding
14697 template-parameter-lists do not apply. */
14698 saved_num_template_parameter_lists
14699 = parser->num_template_parameter_lists;
14700 parser->num_template_parameter_lists = 0;
14701
5dae1114
MM
14702 /* Look for the type-specifier. */
14703 cp_parser_type_specifier (parser,
14704 CP_PARSER_FLAGS_NONE,
62d1db17 14705 /*decl_specs=*/NULL,
5dae1114
MM
14706 /*is_declarator=*/true,
14707 /*declares_class_or_enum=*/NULL,
14708 /*is_cv_qualifier=*/NULL);
4047b164
KL
14709
14710 parser->num_template_parameter_lists
14711 = saved_num_template_parameter_lists;
14712
5dae1114 14713 /* Leave the scope of the class. */
91b004e5 14714 if (pop_p)
5dae1114
MM
14715 pop_scope (type);
14716
14717 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14718 }
14719 }
14720 else
14721 constructor_p = false;
14722 /* We did not really want to consume any tokens. */
14723 cp_parser_abort_tentative_parse (parser);
14724
14725 return constructor_p;
14726}
14727
14728/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14729 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14730 they must be performed once we are in the scope of the function.
14731
14732 Returns the function defined. */
14733
14734static tree
14735cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 14736 (cp_parser* parser,
62d1db17 14737 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 14738 tree attributes,
058b15c1 14739 const cp_declarator *declarator)
a723baf1
MM
14740{
14741 tree fn;
14742 bool success_p;
14743
14744 /* Begin the function-definition. */
058b15c1
MM
14745 success_p = start_function (decl_specifiers, declarator, attributes);
14746
14747 /* The things we're about to see are not directly qualified by any
14748 template headers we've seen thus far. */
14749 reset_specialization ();
a723baf1
MM
14750
14751 /* If there were names looked up in the decl-specifier-seq that we
14752 did not check, check them now. We must wait until we are in the
14753 scope of the function to perform the checks, since the function
14754 might be a friend. */
cf22909c 14755 perform_deferred_access_checks ();
a723baf1
MM
14756
14757 if (!success_p)
14758 {
058b15c1 14759 /* Skip the entire function. */
a723baf1
MM
14760 error ("invalid function declaration");
14761 cp_parser_skip_to_end_of_block_or_statement (parser);
14762 fn = error_mark_node;
14763 }
14764 else
14765 fn = cp_parser_function_definition_after_declarator (parser,
14766 /*inline_p=*/false);
14767
14768 return fn;
14769}
14770
14771/* Parse the part of a function-definition that follows the
14772 declarator. INLINE_P is TRUE iff this function is an inline
14773 function defined with a class-specifier.
14774
14775 Returns the function defined. */
14776
21526606
EC
14777static tree
14778cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14779 bool inline_p)
a723baf1
MM
14780{
14781 tree fn;
14782 bool ctor_initializer_p = false;
14783 bool saved_in_unbraced_linkage_specification_p;
14784 unsigned saved_num_template_parameter_lists;
14785
14786 /* If the next token is `return', then the code may be trying to
14787 make use of the "named return value" extension that G++ used to
14788 support. */
14789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14790 {
14791 /* Consume the `return' keyword. */
14792 cp_lexer_consume_token (parser->lexer);
14793 /* Look for the identifier that indicates what value is to be
14794 returned. */
14795 cp_parser_identifier (parser);
14796 /* Issue an error message. */
14797 error ("named return values are no longer supported");
14798 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14800 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14801 cp_lexer_consume_token (parser->lexer);
14802 }
14803 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14804 anything declared inside `f'. */
21526606 14805 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14806 = parser->in_unbraced_linkage_specification_p;
14807 parser->in_unbraced_linkage_specification_p = false;
14808 /* Inside the function, surrounding template-parameter-lists do not
14809 apply. */
21526606
EC
14810 saved_num_template_parameter_lists
14811 = parser->num_template_parameter_lists;
a723baf1
MM
14812 parser->num_template_parameter_lists = 0;
14813 /* If the next token is `try', then we are looking at a
14814 function-try-block. */
14815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14816 ctor_initializer_p = cp_parser_function_try_block (parser);
14817 /* A function-try-block includes the function-body, so we only do
14818 this next part if we're not processing a function-try-block. */
14819 else
21526606 14820 ctor_initializer_p
a723baf1
MM
14821 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14822
14823 /* Finish the function. */
21526606 14824 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14825 (inline_p ? 2 : 0));
14826 /* Generate code for it, if necessary. */
8cd2462c 14827 expand_or_defer_fn (fn);
a723baf1 14828 /* Restore the saved values. */
21526606 14829 parser->in_unbraced_linkage_specification_p
a723baf1 14830 = saved_in_unbraced_linkage_specification_p;
21526606 14831 parser->num_template_parameter_lists
a723baf1
MM
14832 = saved_num_template_parameter_lists;
14833
14834 return fn;
14835}
14836
14837/* Parse a template-declaration, assuming that the `export' (and
14838 `extern') keywords, if present, has already been scanned. MEMBER_P
14839 is as for cp_parser_template_declaration. */
14840
14841static void
94edc4ab 14842cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14843{
14844 tree decl = NULL_TREE;
14845 tree parameter_list;
14846 bool friend_p = false;
14847
14848 /* Look for the `template' keyword. */
14849 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14850 return;
21526606 14851
a723baf1
MM
14852 /* And the `<'. */
14853 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14854 return;
21526606 14855
a723baf1
MM
14856 /* If the next token is `>', then we have an invalid
14857 specialization. Rather than complain about an invalid template
14858 parameter, issue an error message here. */
14859 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14860 {
14861 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14862 begin_specialization ();
a723baf1
MM
14863 parameter_list = NULL_TREE;
14864 }
14865 else
2f9afd51
KL
14866 {
14867 /* Parse the template parameters. */
14868 begin_template_parm_list ();
14869 parameter_list = cp_parser_template_parameter_list (parser);
14870 parameter_list = end_template_parm_list (parameter_list);
14871 }
14872
a723baf1
MM
14873 /* Look for the `>'. */
14874 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14875 /* We just processed one more parameter list. */
14876 ++parser->num_template_parameter_lists;
14877 /* If the next token is `template', there are more template
14878 parameters. */
21526606 14879 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14880 RID_TEMPLATE))
14881 cp_parser_template_declaration_after_export (parser, member_p);
14882 else
14883 {
14884 decl = cp_parser_single_declaration (parser,
14885 member_p,
14886 &friend_p);
14887
14888 /* If this is a member template declaration, let the front
14889 end know. */
14890 if (member_p && !friend_p && decl)
37d407a1
KL
14891 {
14892 if (TREE_CODE (decl) == TYPE_DECL)
14893 cp_parser_check_access_in_redeclaration (decl);
14894
14895 decl = finish_member_template_decl (decl);
14896 }
a723baf1 14897 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14898 make_friend_class (current_class_type, TREE_TYPE (decl),
14899 /*complain=*/true);
a723baf1
MM
14900 }
14901 /* We are done with the current parameter list. */
14902 --parser->num_template_parameter_lists;
14903
14904 /* Finish up. */
14905 finish_template_decl (parameter_list);
14906
14907 /* Register member declarations. */
14908 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14909 finish_member_declaration (decl);
14910
14911 /* If DECL is a function template, we must return to parse it later.
14912 (Even though there is no definition, there might be default
14913 arguments that need handling.) */
21526606 14914 if (member_p && decl
a723baf1
MM
14915 && (TREE_CODE (decl) == FUNCTION_DECL
14916 || DECL_FUNCTION_TEMPLATE_P (decl)))
14917 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14918 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14919 TREE_VALUE (parser->unparsed_functions_queues));
14920}
14921
14922/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14923 `function-definition' sequence. MEMBER_P is true, this declaration
14924 appears in a class scope.
14925
14926 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14927 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14928
14929static tree
21526606 14930cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14931 bool member_p,
14932 bool* friend_p)
a723baf1 14933{
560ad596 14934 int declares_class_or_enum;
a723baf1 14935 tree decl = NULL_TREE;
62d1db17 14936 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 14937 bool function_definition_p = false;
a723baf1 14938
a723baf1 14939 /* Defer access checks until we know what is being declared. */
8d241e0b 14940 push_deferring_access_checks (dk_deferred);
cf22909c 14941
a723baf1
MM
14942 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14943 alternative. */
62d1db17
MM
14944 cp_parser_decl_specifier_seq (parser,
14945 CP_PARSER_FLAGS_OPTIONAL,
14946 &decl_specifiers,
14947 &declares_class_or_enum);
4bb8ca28 14948 if (friend_p)
62d1db17 14949 *friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
14950 /* Gather up the access checks that occurred the
14951 decl-specifier-seq. */
cf22909c
KL
14952 stop_deferring_access_checks ();
14953
a723baf1
MM
14954 /* Check for the declaration of a template class. */
14955 if (declares_class_or_enum)
14956 {
14957 if (cp_parser_declares_only_class_p (parser))
14958 {
62d1db17
MM
14959 decl = shadow_tag (&decl_specifiers);
14960 if (decl && decl != error_mark_node)
a723baf1
MM
14961 decl = TYPE_NAME (decl);
14962 else
14963 decl = error_mark_node;
14964 }
14965 }
14966 else
14967 decl = NULL_TREE;
14968 /* If it's not a template class, try for a template function. If
14969 the next token is a `;', then this declaration does not declare
14970 anything. But, if there were errors in the decl-specifiers, then
14971 the error might well have come from an attempted class-specifier.
14972 In that case, there's no need to warn about a missing declarator. */
14973 if (!decl
14974 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 14975 || decl_specifiers.type != error_mark_node))
21526606 14976 decl = cp_parser_init_declarator (parser,
62d1db17 14977 &decl_specifiers,
4bb8ca28 14978 /*function_definition_allowed_p=*/true,
a723baf1 14979 member_p,
560ad596 14980 declares_class_or_enum,
4bb8ca28 14981 &function_definition_p);
cf22909c
KL
14982
14983 pop_deferring_access_checks ();
14984
a723baf1
MM
14985 /* Clear any current qualification; whatever comes next is the start
14986 of something new. */
14987 parser->scope = NULL_TREE;
14988 parser->qualifying_scope = NULL_TREE;
14989 parser->object_scope = NULL_TREE;
14990 /* Look for a trailing `;' after the declaration. */
4bb8ca28
MM
14991 if (!function_definition_p
14992 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
a723baf1 14993 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14994
14995 return decl;
14996}
14997
d6b4ea85
MM
14998/* Parse a cast-expression that is not the operand of a unary "&". */
14999
15000static tree
15001cp_parser_simple_cast_expression (cp_parser *parser)
15002{
15003 return cp_parser_cast_expression (parser, /*address_p=*/false);
15004}
15005
a723baf1
MM
15006/* Parse a functional cast to TYPE. Returns an expression
15007 representing the cast. */
15008
15009static tree
94edc4ab 15010cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
15011{
15012 tree expression_list;
d36d5600 15013 tree cast;
a723baf1 15014
21526606 15015 expression_list
39703eb9
MM
15016 = cp_parser_parenthesized_expression_list (parser, false,
15017 /*non_constant_p=*/NULL);
a723baf1 15018
d36d5600
GB
15019 cast = build_functional_cast (type, expression_list);
15020 /* [expr.const]/1: In an integral constant expression "only type
15021 conversions to integral or enumeration type can be used". */
15022 if (cast != error_mark_node && !type_dependent_expression_p (type)
15023 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15024 {
15025 if (cp_parser_non_integral_constant_expression
15026 (parser, "a call to a constructor"))
15027 return error_mark_node;
15028 }
15029 return cast;
a723baf1
MM
15030}
15031
4bb8ca28
MM
15032/* Save the tokens that make up the body of a member function defined
15033 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15034 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15035 specifiers applied to the declaration. Returns the FUNCTION_DECL
15036 for the member function. */
15037
7ce27103 15038static tree
4bb8ca28 15039cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15040 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15041 cp_declarator *declarator,
4bb8ca28
MM
15042 tree attributes)
15043{
15044 cp_token_cache *cache;
15045 tree fn;
15046
15047 /* Create the function-declaration. */
15048 fn = start_method (decl_specifiers, declarator, attributes);
15049 /* If something went badly wrong, bail out now. */
15050 if (fn == error_mark_node)
15051 {
15052 /* If there's a function-body, skip it. */
21526606 15053 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15054 (cp_lexer_peek_token (parser->lexer)))
15055 cp_parser_skip_to_end_of_block_or_statement (parser);
15056 return error_mark_node;
15057 }
15058
15059 /* Remember it, if there default args to post process. */
15060 cp_parser_save_default_args (parser, fn);
15061
15062 /* Create a token cache. */
15063 cache = cp_token_cache_new ();
21526606 15064 /* Save away the tokens that make up the body of the
4bb8ca28
MM
15065 function. */
15066 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15067 /* Handle function try blocks. */
15068 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15069 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15070
15071 /* Save away the inline definition; we will process it when the
15072 class is complete. */
15073 DECL_PENDING_INLINE_INFO (fn) = cache;
15074 DECL_PENDING_INLINE_P (fn) = 1;
15075
15076 /* We need to know that this was defined in the class, so that
15077 friend templates are handled correctly. */
15078 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15079
15080 /* We're done with the inline definition. */
15081 finish_method (fn);
15082
15083 /* Add FN to the queue of functions to be parsed later. */
15084 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15085 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15086 TREE_VALUE (parser->unparsed_functions_queues));
15087
15088 return fn;
15089}
15090
ec75414f
MM
15091/* Parse a template-argument-list, as well as the trailing ">" (but
15092 not the opening ">"). See cp_parser_template_argument_list for the
15093 return value. */
15094
15095static tree
15096cp_parser_enclosed_template_argument_list (cp_parser* parser)
15097{
15098 tree arguments;
15099 tree saved_scope;
15100 tree saved_qualifying_scope;
15101 tree saved_object_scope;
15102 bool saved_greater_than_is_operator_p;
15103
15104 /* [temp.names]
15105
15106 When parsing a template-id, the first non-nested `>' is taken as
15107 the end of the template-argument-list rather than a greater-than
15108 operator. */
21526606 15109 saved_greater_than_is_operator_p
ec75414f
MM
15110 = parser->greater_than_is_operator_p;
15111 parser->greater_than_is_operator_p = false;
15112 /* Parsing the argument list may modify SCOPE, so we save it
15113 here. */
15114 saved_scope = parser->scope;
15115 saved_qualifying_scope = parser->qualifying_scope;
15116 saved_object_scope = parser->object_scope;
15117 /* Parse the template-argument-list itself. */
15118 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15119 arguments = NULL_TREE;
15120 else
15121 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15122 /* Look for the `>' that ends the template-argument-list. If we find
15123 a '>>' instead, it's probably just a typo. */
15124 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15125 {
15126 if (!saved_greater_than_is_operator_p)
15127 {
15128 /* If we're in a nested template argument list, the '>>' has to be
15129 a typo for '> >'. We emit the error message, but we continue
15130 parsing and we push a '>' as next token, so that the argument
15131 list will be parsed correctly.. */
15132 cp_token* token;
15133 error ("`>>' should be `> >' within a nested template argument list");
15134 token = cp_lexer_peek_token (parser->lexer);
15135 token->type = CPP_GREATER;
15136 }
15137 else
15138 {
15139 /* If this is not a nested template argument list, the '>>' is
15140 a typo for '>'. Emit an error message and continue. */
15141 error ("spurious `>>', use `>' to terminate a template argument list");
15142 cp_lexer_consume_token (parser->lexer);
15143 }
15144 }
6c0cc713
GB
15145 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15146 error ("missing `>' to terminate the template argument list");
ec75414f 15147 /* The `>' token might be a greater-than operator again now. */
21526606 15148 parser->greater_than_is_operator_p
ec75414f
MM
15149 = saved_greater_than_is_operator_p;
15150 /* Restore the SAVED_SCOPE. */
15151 parser->scope = saved_scope;
15152 parser->qualifying_scope = saved_qualifying_scope;
15153 parser->object_scope = saved_object_scope;
15154
15155 return arguments;
15156}
15157
a723baf1
MM
15158/* MEMBER_FUNCTION is a member function, or a friend. If default
15159 arguments, or the body of the function have not yet been parsed,
15160 parse them now. */
15161
15162static void
94edc4ab 15163cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
15164{
15165 cp_lexer *saved_lexer;
15166
15167 /* If this member is a template, get the underlying
15168 FUNCTION_DECL. */
15169 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15170 member_function = DECL_TEMPLATE_RESULT (member_function);
15171
15172 /* There should not be any class definitions in progress at this
15173 point; the bodies of members are only parsed outside of all class
15174 definitions. */
15175 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
15176 /* While we're parsing the member functions we might encounter more
15177 classes. We want to handle them right away, but we don't want
15178 them getting mixed up with functions that are currently in the
15179 queue. */
15180 parser->unparsed_functions_queues
15181 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15182
15183 /* Make sure that any template parameters are in scope. */
15184 maybe_begin_member_template_processing (member_function);
15185
a723baf1
MM
15186 /* If the body of the function has not yet been parsed, parse it
15187 now. */
15188 if (DECL_PENDING_INLINE_P (member_function))
15189 {
15190 tree function_scope;
15191 cp_token_cache *tokens;
15192
15193 /* The function is no longer pending; we are processing it. */
15194 tokens = DECL_PENDING_INLINE_INFO (member_function);
15195 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15196 DECL_PENDING_INLINE_P (member_function) = 0;
15197 /* If this was an inline function in a local class, enter the scope
15198 of the containing function. */
15199 function_scope = decl_function_context (member_function);
15200 if (function_scope)
15201 push_function_context_to (function_scope);
21526606 15202
a723baf1
MM
15203 /* Save away the current lexer. */
15204 saved_lexer = parser->lexer;
15205 /* Make a new lexer to feed us the tokens saved for this function. */
15206 parser->lexer = cp_lexer_new_from_tokens (tokens);
15207 parser->lexer->next = saved_lexer;
21526606 15208
a723baf1
MM
15209 /* Set the current source position to be the location of the first
15210 token in the saved inline body. */
3466b292 15211 cp_lexer_peek_token (parser->lexer);
21526606 15212
a723baf1
MM
15213 /* Let the front end know that we going to be defining this
15214 function. */
058b15c1
MM
15215 start_preparsed_function (member_function, NULL_TREE,
15216 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15217
a723baf1
MM
15218 /* Now, parse the body of the function. */
15219 cp_parser_function_definition_after_declarator (parser,
15220 /*inline_p=*/true);
21526606 15221
a723baf1
MM
15222 /* Leave the scope of the containing function. */
15223 if (function_scope)
15224 pop_function_context_from (function_scope);
15225 /* Restore the lexer. */
15226 parser->lexer = saved_lexer;
15227 }
15228
15229 /* Remove any template parameters from the symbol table. */
15230 maybe_end_member_template_processing ();
15231
15232 /* Restore the queue. */
21526606 15233 parser->unparsed_functions_queues
a723baf1
MM
15234 = TREE_CHAIN (parser->unparsed_functions_queues);
15235}
15236
cd0be382 15237/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15238 functions queue. */
15239
15240static void
15241cp_parser_save_default_args (cp_parser* parser, tree decl)
15242{
15243 tree probe;
15244
15245 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15246 probe;
15247 probe = TREE_CHAIN (probe))
15248 if (TREE_PURPOSE (probe))
15249 {
15250 TREE_PURPOSE (parser->unparsed_functions_queues)
21526606 15251 = tree_cons (NULL_TREE, decl,
8db1028e
NS
15252 TREE_PURPOSE (parser->unparsed_functions_queues));
15253 break;
15254 }
15255 return;
15256}
15257
8218bd34
MM
15258/* FN is a FUNCTION_DECL which may contains a parameter with an
15259 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
15260
15261static void
8218bd34 15262cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
15263{
15264 cp_lexer *saved_lexer;
15265 cp_token_cache *tokens;
15266 bool saved_local_variables_forbidden_p;
15267 tree parameters;
8218bd34 15268
b92bc2a0
NS
15269 /* While we're parsing the default args, we might (due to the
15270 statement expression extension) encounter more classes. We want
15271 to handle them right away, but we don't want them getting mixed
15272 up with default args that are currently in the queue. */
15273 parser->unparsed_functions_queues
15274 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15275
8218bd34 15276 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
15277 parameters;
15278 parameters = TREE_CHAIN (parameters))
15279 {
15280 if (!TREE_PURPOSE (parameters)
15281 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15282 continue;
21526606 15283
a723baf1
MM
15284 /* Save away the current lexer. */
15285 saved_lexer = parser->lexer;
15286 /* Create a new one, using the tokens we have saved. */
15287 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
15288 parser->lexer = cp_lexer_new_from_tokens (tokens);
15289
15290 /* Set the current source position to be the location of the
15291 first token in the default argument. */
3466b292 15292 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
15293
15294 /* Local variable names (and the `this' keyword) may not appear
15295 in a default argument. */
15296 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15297 parser->local_variables_forbidden_p = true;
15298 /* Parse the assignment-expression. */
f128e1f3 15299 if (DECL_CLASS_SCOPE_P (fn))
14d22dd6 15300 push_nested_class (DECL_CONTEXT (fn));
a723baf1 15301 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
f128e1f3 15302 if (DECL_CLASS_SCOPE_P (fn))
e5976695 15303 pop_nested_class ();
a723baf1 15304
676e33ca
MM
15305 /* If the token stream has not been completely used up, then
15306 there was extra junk after the end of the default
15307 argument. */
15308 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15309 cp_parser_error (parser, "expected `,'");
15310
a723baf1
MM
15311 /* Restore saved state. */
15312 parser->lexer = saved_lexer;
15313 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15314 }
b92bc2a0
NS
15315
15316 /* Restore the queue. */
21526606 15317 parser->unparsed_functions_queues
b92bc2a0 15318 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15319}
15320
15321/* Parse the operand of `sizeof' (or a similar operator). Returns
15322 either a TYPE or an expression, depending on the form of the
15323 input. The KEYWORD indicates which kind of expression we have
15324 encountered. */
15325
15326static tree
94edc4ab 15327cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15328{
15329 static const char *format;
15330 tree expr = NULL_TREE;
15331 const char *saved_message;
67c03833 15332 bool saved_integral_constant_expression_p;
a723baf1
MM
15333
15334 /* Initialize FORMAT the first time we get here. */
15335 if (!format)
15336 format = "types may not be defined in `%s' expressions";
15337
15338 /* Types cannot be defined in a `sizeof' expression. Save away the
15339 old message. */
15340 saved_message = parser->type_definition_forbidden_message;
15341 /* And create the new one. */
21526606
EC
15342 parser->type_definition_forbidden_message
15343 = xmalloc (strlen (format)
c68b0a84
KG
15344 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15345 + 1 /* `\0' */);
a723baf1
MM
15346 sprintf ((char *) parser->type_definition_forbidden_message,
15347 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15348
15349 /* The restrictions on constant-expressions do not apply inside
15350 sizeof expressions. */
67c03833
JM
15351 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15352 parser->integral_constant_expression_p = false;
a723baf1 15353
3beb3abf
MM
15354 /* Do not actually evaluate the expression. */
15355 ++skip_evaluation;
a723baf1
MM
15356 /* If it's a `(', then we might be looking at the type-id
15357 construction. */
15358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15359 {
15360 tree type;
4f8163b1 15361 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15362
15363 /* We can't be sure yet whether we're looking at a type-id or an
15364 expression. */
15365 cp_parser_parse_tentatively (parser);
15366 /* Consume the `('. */
15367 cp_lexer_consume_token (parser->lexer);
15368 /* Parse the type-id. */
4f8163b1
MM
15369 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15370 parser->in_type_id_in_expr_p = true;
a723baf1 15371 type = cp_parser_type_id (parser);
4f8163b1 15372 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
15373 /* Now, look for the trailing `)'. */
15374 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15375 /* If all went well, then we're done. */
15376 if (cp_parser_parse_definitely (parser))
15377 {
62d1db17
MM
15378 cp_decl_specifier_seq decl_specs;
15379
15380 /* Build a trivial decl-specifier-seq. */
15381 clear_decl_specs (&decl_specs);
15382 decl_specs.type = type;
a723baf1
MM
15383
15384 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15385 expr = grokdeclarator (NULL,
62d1db17 15386 &decl_specs,
a723baf1
MM
15387 TYPENAME,
15388 /*initialized=*/0,
15389 /*attrlist=*/NULL);
15390 }
15391 }
15392
15393 /* If the type-id production did not work out, then we must be
15394 looking at the unary-expression production. */
15395 if (!expr)
15396 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
15397 /* Go back to evaluating expressions. */
15398 --skip_evaluation;
a723baf1
MM
15399
15400 /* Free the message we created. */
15401 free ((char *) parser->type_definition_forbidden_message);
15402 /* And restore the old one. */
15403 parser->type_definition_forbidden_message = saved_message;
67c03833 15404 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
15405
15406 return expr;
15407}
15408
15409/* If the current declaration has no declarator, return true. */
15410
15411static bool
15412cp_parser_declares_only_class_p (cp_parser *parser)
15413{
21526606 15414 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15415 declarator. */
15416 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15417 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15418}
15419
62d1db17 15420/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15421
62d1db17
MM
15422static void
15423cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15424 cp_storage_class storage_class)
a723baf1 15425{
62d1db17
MM
15426 if (decl_specs->storage_class != sc_none)
15427 decl_specs->multiple_storage_classes_p = true;
15428 else
15429 decl_specs->storage_class = storage_class;
15430}
15431
15432/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15433 is true, the type is a user-defined type; otherwise it is a
15434 built-in type specified by a keyword. */
a723baf1 15435
62d1db17
MM
15436static void
15437cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15438 tree type_spec,
15439 bool user_defined_p)
15440{
15441 decl_specs->any_specifiers_p = true;
15442 if (decl_specs->type)
15443 {
15444 if (decl_specs->specs[(int)ds_typedef] && !user_defined_p)
15445 decl_specs->redefined_builtin_type = type_spec;
15446 else
15447 decl_specs->multiple_types_p = true;
15448 }
15449 else
15450 {
15451 decl_specs->type = type_spec;
15452 decl_specs->user_defined_type_p = user_defined_p;
a723baf1 15453 }
62d1db17 15454}
a723baf1 15455
62d1db17
MM
15456/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15457 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15458
15459static bool
15460cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15461{
15462 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15463}
15464
15465/* If the next token is of the indicated TYPE, consume it. Otherwise,
15466 issue an error message indicating that TOKEN_DESC was expected.
21526606 15467
a723baf1
MM
15468 Returns the token consumed, if the token had the appropriate type.
15469 Otherwise, returns NULL. */
15470
15471static cp_token *
94edc4ab
NN
15472cp_parser_require (cp_parser* parser,
15473 enum cpp_ttype type,
15474 const char* token_desc)
a723baf1
MM
15475{
15476 if (cp_lexer_next_token_is (parser->lexer, type))
15477 return cp_lexer_consume_token (parser->lexer);
15478 else
15479 {
e5976695
MM
15480 /* Output the MESSAGE -- unless we're parsing tentatively. */
15481 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15482 {
15483 char *message = concat ("expected ", token_desc, NULL);
15484 cp_parser_error (parser, message);
15485 free (message);
15486 }
a723baf1
MM
15487 return NULL;
15488 }
15489}
15490
15491/* Like cp_parser_require, except that tokens will be skipped until
15492 the desired token is found. An error message is still produced if
15493 the next token is not as expected. */
15494
15495static void
21526606
EC
15496cp_parser_skip_until_found (cp_parser* parser,
15497 enum cpp_ttype type,
94edc4ab 15498 const char* token_desc)
a723baf1
MM
15499{
15500 cp_token *token;
15501 unsigned nesting_depth = 0;
15502
15503 if (cp_parser_require (parser, type, token_desc))
15504 return;
15505
15506 /* Skip tokens until the desired token is found. */
15507 while (true)
15508 {
15509 /* Peek at the next token. */
15510 token = cp_lexer_peek_token (parser->lexer);
21526606 15511 /* If we've reached the token we want, consume it and
a723baf1
MM
15512 stop. */
15513 if (token->type == type && !nesting_depth)
15514 {
15515 cp_lexer_consume_token (parser->lexer);
15516 return;
15517 }
15518 /* If we've run out of tokens, stop. */
15519 if (token->type == CPP_EOF)
15520 return;
21526606 15521 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15522 || token->type == CPP_OPEN_PAREN
15523 || token->type == CPP_OPEN_SQUARE)
15524 ++nesting_depth;
21526606 15525 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15526 || token->type == CPP_CLOSE_PAREN
15527 || token->type == CPP_CLOSE_SQUARE)
15528 {
15529 if (nesting_depth-- == 0)
15530 return;
15531 }
15532 /* Consume this token. */
15533 cp_lexer_consume_token (parser->lexer);
15534 }
15535}
15536
15537/* If the next token is the indicated keyword, consume it. Otherwise,
15538 issue an error message indicating that TOKEN_DESC was expected.
21526606 15539
a723baf1
MM
15540 Returns the token consumed, if the token had the appropriate type.
15541 Otherwise, returns NULL. */
15542
15543static cp_token *
94edc4ab
NN
15544cp_parser_require_keyword (cp_parser* parser,
15545 enum rid keyword,
15546 const char* token_desc)
a723baf1
MM
15547{
15548 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15549
15550 if (token && token->keyword != keyword)
15551 {
15552 dyn_string_t error_msg;
15553
15554 /* Format the error message. */
15555 error_msg = dyn_string_new (0);
15556 dyn_string_append_cstr (error_msg, "expected ");
15557 dyn_string_append_cstr (error_msg, token_desc);
15558 cp_parser_error (parser, error_msg->s);
15559 dyn_string_delete (error_msg);
15560 return NULL;
15561 }
15562
15563 return token;
15564}
15565
15566/* Returns TRUE iff TOKEN is a token that can begin the body of a
15567 function-definition. */
15568
21526606 15569static bool
94edc4ab 15570cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15571{
15572 return (/* An ordinary function-body begins with an `{'. */
15573 token->type == CPP_OPEN_BRACE
15574 /* A ctor-initializer begins with a `:'. */
15575 || token->type == CPP_COLON
15576 /* A function-try-block begins with `try'. */
15577 || token->keyword == RID_TRY
15578 /* The named return value extension begins with `return'. */
15579 || token->keyword == RID_RETURN);
15580}
15581
15582/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15583 definition. */
15584
15585static bool
15586cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15587{
15588 cp_token *token;
15589
15590 token = cp_lexer_peek_token (parser->lexer);
15591 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15592}
15593
d17811fd 15594/* Returns TRUE iff the next token is the "," or ">" ending a
4d5297fa
GB
15595 template-argument. ">>" is also accepted (after the full
15596 argument was parsed) because it's probably a typo for "> >",
15597 and there is a specific diagnostic for this. */
d17811fd
MM
15598
15599static bool
15600cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15601{
15602 cp_token *token;
15603
15604 token = cp_lexer_peek_token (parser->lexer);
21526606 15605 return (token->type == CPP_COMMA || token->type == CPP_GREATER
4d5297fa 15606 || token->type == CPP_RSHIFT);
d17811fd 15607}
f4abade9
GB
15608
15609/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15610 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15611
15612static bool
21526606 15613cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15614 size_t n)
15615{
15616 cp_token *token;
15617
15618 token = cp_lexer_peek_nth_token (parser->lexer, n);
15619 if (token->type == CPP_LESS)
15620 return true;
15621 /* Check for the sequence `<::' in the original code. It would be lexed as
15622 `[:', where `[' is a digraph, and there is no whitespace before
15623 `:'. */
15624 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15625 {
15626 cp_token *token2;
15627 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15628 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15629 return true;
15630 }
15631 return false;
15632}
21526606 15633
a723baf1
MM
15634/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15635 or none_type otherwise. */
15636
15637static enum tag_types
94edc4ab 15638cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15639{
15640 switch (token->keyword)
15641 {
15642 case RID_CLASS:
15643 return class_type;
15644 case RID_STRUCT:
15645 return record_type;
15646 case RID_UNION:
15647 return union_type;
21526606 15648
a723baf1
MM
15649 default:
15650 return none_type;
15651 }
15652}
15653
15654/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15655
15656static void
15657cp_parser_check_class_key (enum tag_types class_key, tree type)
15658{
15659 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15660 pedwarn ("`%s' tag used in naming `%#T'",
15661 class_key == union_type ? "union"
21526606 15662 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15663 type);
15664}
21526606 15665
cd0be382 15666/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15667 access than its original declaration [class.access.spec/3].
15668 This applies to nested classes and nested class templates.
15669 [class.mem/1]. */
15670
15671static void cp_parser_check_access_in_redeclaration (tree decl)
15672{
15673 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15674 return;
15675
15676 if ((TREE_PRIVATE (decl)
15677 != (current_access_specifier == access_private_node))
15678 || (TREE_PROTECTED (decl)
15679 != (current_access_specifier == access_protected_node)))
15680 error ("%D redeclared with different access", decl);
15681}
15682
a723baf1 15683/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15684 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15685 consumed. */
15686
15687static bool
15688cp_parser_optional_template_keyword (cp_parser *parser)
15689{
15690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15691 {
15692 /* The `template' keyword can only be used within templates;
15693 outside templates the parser can always figure out what is a
15694 template and what is not. */
15695 if (!processing_template_decl)
15696 {
15697 error ("`template' (as a disambiguator) is only allowed "
15698 "within templates");
15699 /* If this part of the token stream is rescanned, the same
15700 error message would be generated. So, we purge the token
15701 from the stream. */
15702 cp_lexer_purge_token (parser->lexer);
15703 return false;
15704 }
15705 else
15706 {
15707 /* Consume the `template' keyword. */
15708 cp_lexer_consume_token (parser->lexer);
15709 return true;
15710 }
15711 }
15712
15713 return false;
15714}
15715
2050a1bb
MM
15716/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15717 set PARSER->SCOPE, and perform other related actions. */
15718
15719static void
15720cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15721{
15722 tree value;
15723 tree check;
15724
15725 /* Get the stored value. */
15726 value = cp_lexer_consume_token (parser->lexer)->value;
15727 /* Perform any access checks that were deferred. */
15728 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15729 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15730 /* Set the scope from the stored value. */
15731 parser->scope = TREE_VALUE (value);
15732 parser->qualifying_scope = TREE_TYPE (value);
15733 parser->object_scope = NULL_TREE;
15734}
15735
852dcbdd 15736/* Add tokens to CACHE until a non-nested END token appears. */
a723baf1
MM
15737
15738static void
0173bb6f
AO
15739cp_parser_cache_group_1 (cp_parser *parser,
15740 cp_token_cache *cache,
15741 enum cpp_ttype end,
15742 unsigned depth)
a723baf1
MM
15743{
15744 while (true)
15745 {
15746 cp_token *token;
15747
15748 /* Abort a parenthesized expression if we encounter a brace. */
15749 if ((end == CPP_CLOSE_PAREN || depth == 0)
15750 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15751 return;
a723baf1 15752 /* If we've reached the end of the file, stop. */
4bfb8bba 15753 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15754 return;
4bfb8bba
MM
15755 /* Consume the next token. */
15756 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15757 /* Add this token to the tokens we are saving. */
15758 cp_token_cache_push_token (cache, token);
15759 /* See if it starts a new group. */
15760 if (token->type == CPP_OPEN_BRACE)
15761 {
0173bb6f 15762 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
15763 if (depth == 0)
15764 return;
15765 }
15766 else if (token->type == CPP_OPEN_PAREN)
0173bb6f 15767 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
15768 else if (token->type == end)
15769 return;
15770 }
15771}
15772
0173bb6f
AO
15773/* Convenient interface for cp_parser_cache_group_1 that makes sure we
15774 preserve string tokens in both translated and untranslated
15775 forms. */
15776
15777static void
15778cp_parser_cache_group (cp_parser *parser,
15779 cp_token_cache *cache,
15780 enum cpp_ttype end,
15781 unsigned depth)
15782{
15783 int saved_c_lex_string_translate;
15784
15785 saved_c_lex_string_translate = c_lex_string_translate;
15786 c_lex_string_translate = -1;
15787
15788 cp_parser_cache_group_1 (parser, cache, end, depth);
15789
15790 c_lex_string_translate = saved_c_lex_string_translate;
15791}
15792
15793
a723baf1
MM
15794/* Begin parsing tentatively. We always save tokens while parsing
15795 tentatively so that if the tentative parsing fails we can restore the
15796 tokens. */
15797
15798static void
94edc4ab 15799cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15800{
15801 /* Enter a new parsing context. */
15802 parser->context = cp_parser_context_new (parser->context);
15803 /* Begin saving tokens. */
15804 cp_lexer_save_tokens (parser->lexer);
15805 /* In order to avoid repetitive access control error messages,
15806 access checks are queued up until we are no longer parsing
15807 tentatively. */
8d241e0b 15808 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15809}
15810
15811/* Commit to the currently active tentative parse. */
15812
15813static void
94edc4ab 15814cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15815{
15816 cp_parser_context *context;
15817 cp_lexer *lexer;
15818
15819 /* Mark all of the levels as committed. */
15820 lexer = parser->lexer;
15821 for (context = parser->context; context->next; context = context->next)
15822 {
15823 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15824 break;
15825 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15826 while (!cp_lexer_saving_tokens (lexer))
15827 lexer = lexer->next;
15828 cp_lexer_commit_tokens (lexer);
15829 }
15830}
15831
15832/* Abort the currently active tentative parse. All consumed tokens
15833 will be rolled back, and no diagnostics will be issued. */
15834
15835static void
94edc4ab 15836cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15837{
15838 cp_parser_simulate_error (parser);
15839 /* Now, pretend that we want to see if the construct was
15840 successfully parsed. */
15841 cp_parser_parse_definitely (parser);
15842}
15843
34cd5ae7 15844/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15845 token stream. Otherwise, commit to the tokens we have consumed.
15846 Returns true if no error occurred; false otherwise. */
15847
15848static bool
94edc4ab 15849cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15850{
15851 bool error_occurred;
15852 cp_parser_context *context;
15853
34cd5ae7 15854 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15855 destroy that information. */
15856 error_occurred = cp_parser_error_occurred (parser);
15857 /* Remove the topmost context from the stack. */
15858 context = parser->context;
15859 parser->context = context->next;
15860 /* If no parse errors occurred, commit to the tentative parse. */
15861 if (!error_occurred)
15862 {
15863 /* Commit to the tokens read tentatively, unless that was
15864 already done. */
15865 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15866 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15867
15868 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15869 }
15870 /* Otherwise, if errors occurred, roll back our state so that things
15871 are just as they were before we began the tentative parse. */
15872 else
cf22909c
KL
15873 {
15874 cp_lexer_rollback_tokens (parser->lexer);
15875 pop_deferring_access_checks ();
15876 }
e5976695
MM
15877 /* Add the context to the front of the free list. */
15878 context->next = cp_parser_context_free_list;
15879 cp_parser_context_free_list = context;
15880
15881 return !error_occurred;
a723baf1
MM
15882}
15883
a723baf1
MM
15884/* Returns true if we are parsing tentatively -- but have decided that
15885 we will stick with this tentative parse, even if errors occur. */
15886
15887static bool
94edc4ab 15888cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15889{
15890 return (cp_parser_parsing_tentatively (parser)
15891 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15892}
15893
4de8668e 15894/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15895 tentative parse. */
21526606 15896
a723baf1 15897static bool
94edc4ab 15898cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15899{
15900 return (cp_parser_parsing_tentatively (parser)
15901 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15902}
15903
4de8668e 15904/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15905
15906static bool
94edc4ab 15907cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15908{
15909 return parser->allow_gnu_extensions_p;
15910}
15911
15912\f
a723baf1
MM
15913/* The parser. */
15914
15915static GTY (()) cp_parser *the_parser;
15916
15917/* External interface. */
15918
d1bd0ded 15919/* Parse one entire translation unit. */
a723baf1 15920
d1bd0ded
GK
15921void
15922c_parse_file (void)
a723baf1
MM
15923{
15924 bool error_occurred;
f75fbaf7
ZW
15925 static bool already_called = false;
15926
15927 if (already_called)
15928 {
15929 sorry ("inter-module optimizations not implemented for C++");
15930 return;
15931 }
15932 already_called = true;
a723baf1
MM
15933
15934 the_parser = cp_parser_new ();
78757caa
KL
15935 push_deferring_access_checks (flag_access_control
15936 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15937 error_occurred = cp_parser_translation_unit (the_parser);
15938 the_parser = NULL;
a723baf1
MM
15939}
15940
a723baf1
MM
15941/* This variable must be provided by every front end. */
15942
15943int yydebug;
15944
15945#include "gt-cp-parser.h"