]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
gimplify.c (gimplify_modify_expr_rhs): Use types_compatible_p.
[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
c162c75e
MA
42/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43 and c-lex.c) and the C++ parser. */
a723baf1
MM
44
45/* A C++ token. */
46
47typedef struct cp_token GTY (())
48{
49 /* The kind of token. */
df2b750f 50 ENUM_BITFIELD (cpp_ttype) type : 8;
a723baf1
MM
51 /* If this token is a keyword, this value indicates which keyword.
52 Otherwise, this value is RID_MAX. */
df2b750f 53 ENUM_BITFIELD (rid) keyword : 8;
f4abade9
GB
54 /* Token flags. */
55 unsigned char flags;
03fd3f84 56 /* True if this token is from a system header. */
c162c75e 57 BOOL_BITFIELD in_system_header : 1;
7d381002
MA
58 /* True if this token is from a context where it is implicitly extern "C" */
59 BOOL_BITFIELD implicit_extern_c : 1;
522df488
DN
60 /* The value associated with this token, if any. */
61 tree value;
82a98427
NS
62 /* The location at which this token was found. */
63 location_t location;
a723baf1
MM
64} cp_token;
65
0c5e4866
NS
66/* We use a stack of token pointer for saving token sets. */
67typedef struct cp_token *cp_token_position;
68DEF_VEC_MALLOC_P (cp_token_position);
69
76aebc9f
NS
70static const cp_token eof_token =
71{
72 CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73#if USE_MAPPED_LOCATION
74 0
75#else
76 {0, 0}
77#endif
78};
0c5e4866 79
a723baf1
MM
80/* The cp_lexer structure represents the C++ lexer. It is responsible
81 for managing the token stream from the preprocessor and supplying
c162c75e 82 it to the parser. Tokens are never added to the cp_lexer after
03fd3f84 83 it is created. */
a723baf1
MM
84
85typedef struct cp_lexer GTY (())
86{
0c5e4866
NS
87 /* The memory allocated for the buffer. NULL if this lexer does not
88 own the token buffer. */
76aebc9f
NS
89 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90 /* If the lexer owns the buffer, this is the number of tokens in the
91 buffer. */
92 size_t buffer_length;
0c5e4866 93
c162c75e 94 /* A pointer just past the last available token. The tokens
03fd3f84 95 in this lexer are [buffer, last_token). */
0c5e4866 96 cp_token_position GTY ((skip)) last_token;
c162c75e 97
0c5e4866 98 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
a723baf1 99 no more available tokens. */
0c5e4866 100 cp_token_position GTY ((skip)) next_token;
a723baf1
MM
101
102 /* A stack indicating positions at which cp_lexer_save_tokens was
103 called. The top entry is the most recent position at which we
0c5e4866
NS
104 began saving tokens. If the stack is non-empty, we are saving
105 tokens. */
106 VEC (cp_token_position) *GTY ((skip)) saved_tokens;
a723baf1 107
a723baf1
MM
108 /* True if we should output debugging information. */
109 bool debugging_p;
110
111 /* The next lexer in a linked list of lexers. */
112 struct cp_lexer *next;
113} cp_lexer;
114
c162c75e
MA
115/* cp_token_cache is a range of tokens. There is no need to represent
116 allocate heap memory for it, since tokens are never removed from the
117 lexer's array. There is also no need for the GC to walk through
118 a cp_token_cache, since everything in here is referenced through
03fd3f84 119 a lexer. */
c162c75e
MA
120
121typedef struct cp_token_cache GTY(())
122{
03fd3f84 123 /* The beginning of the token range. */
c162c75e
MA
124 cp_token * GTY((skip)) first;
125
03fd3f84 126 /* Points immediately after the last token in the range. */
c162c75e
MA
127 cp_token * GTY ((skip)) last;
128} cp_token_cache;
129
a723baf1
MM
130/* Prototypes. */
131
17211ab5 132static cp_lexer *cp_lexer_new_main
94edc4ab 133 (void);
a723baf1 134static cp_lexer *cp_lexer_new_from_tokens
c162c75e
MA
135 (cp_token_cache *tokens);
136static void cp_lexer_destroy
137 (cp_lexer *);
a723baf1 138static int cp_lexer_saving_tokens
94edc4ab 139 (const cp_lexer *);
0c5e4866
NS
140static cp_token_position cp_lexer_token_position
141 (cp_lexer *, bool);
142static cp_token *cp_lexer_token_at
143 (cp_lexer *, cp_token_position);
a723baf1 144static void cp_lexer_get_preprocessor_token
94edc4ab 145 (cp_lexer *, cp_token *);
c162c75e
MA
146static inline cp_token *cp_lexer_peek_token
147 (cp_lexer *);
a723baf1 148static cp_token *cp_lexer_peek_nth_token
94edc4ab 149 (cp_lexer *, size_t);
f7b5ecd9 150static inline bool cp_lexer_next_token_is
94edc4ab 151 (cp_lexer *, enum cpp_ttype);
a723baf1 152static bool cp_lexer_next_token_is_not
94edc4ab 153 (cp_lexer *, enum cpp_ttype);
a723baf1 154static bool cp_lexer_next_token_is_keyword
94edc4ab 155 (cp_lexer *, enum rid);
21526606 156static cp_token *cp_lexer_consume_token
94edc4ab 157 (cp_lexer *);
a723baf1
MM
158static void cp_lexer_purge_token
159 (cp_lexer *);
160static void cp_lexer_purge_tokens_after
0c5e4866 161 (cp_lexer *, cp_token_position);
c162c75e
MA
162static void cp_lexer_handle_pragma
163 (cp_lexer *);
a723baf1 164static void cp_lexer_save_tokens
94edc4ab 165 (cp_lexer *);
a723baf1 166static void cp_lexer_commit_tokens
94edc4ab 167 (cp_lexer *);
a723baf1 168static void cp_lexer_rollback_tokens
94edc4ab 169 (cp_lexer *);
6983ea08 170#ifdef ENABLE_CHECKING
a723baf1 171static void cp_lexer_print_token
94edc4ab 172 (FILE *, cp_token *);
21526606 173static inline bool cp_lexer_debugging_p
94edc4ab 174 (cp_lexer *);
a723baf1 175static void cp_lexer_start_debugging
94edc4ab 176 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 177static void cp_lexer_stop_debugging
94edc4ab 178 (cp_lexer *) ATTRIBUTE_UNUSED;
6983ea08 179#else
2cfe82fe
ZW
180/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181 about passing NULL to functions that require non-NULL arguments
182 (fputs, fprintf). It will never be used, so all we need is a value
183 of the right type that's guaranteed not to be NULL. */
184#define cp_lexer_debug_stream stdout
185#define cp_lexer_print_token(str, tok) (void) 0
6983ea08
MA
186#define cp_lexer_debugging_p(lexer) 0
187#endif /* ENABLE_CHECKING */
a723baf1 188
c162c75e
MA
189static cp_token_cache *cp_token_cache_new
190 (cp_token *, cp_token *);
191
a723baf1 192/* Manifest constants. */
c162c75e 193#define CP_LEXER_BUFFER_SIZE 10000
0c5e4866 194#define CP_SAVED_TOKEN_STACK 5
a723baf1
MM
195
196/* A token type for keywords, as opposed to ordinary identifiers. */
197#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
198
199/* A token type for template-ids. If a template-id is processed while
200 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201 the value of the CPP_TEMPLATE_ID is whatever was returned by
202 cp_parser_template_id. */
203#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
204
205/* A token type for nested-name-specifiers. If a
206 nested-name-specifier is processed while parsing tentatively, it is
207 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209 cp_parser_nested_name_specifier_opt. */
210#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
211
212/* A token type for tokens that are not tokens at all; these are used
c162c75e 213 to represent slots in the array where there used to be a token
03fd3f84 214 that has now been deleted. */
b8b94c5b
PB
215#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
216
217/* The number of token types, including C++-specific ones. */
218#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
a723baf1
MM
219
220/* Variables. */
221
6983ea08 222#ifdef ENABLE_CHECKING
a723baf1
MM
223/* The stream to which debugging output should be written. */
224static FILE *cp_lexer_debug_stream;
6983ea08 225#endif /* ENABLE_CHECKING */
a723baf1 226
17211ab5
GK
227/* Create a new main C++ lexer, the lexer that gets tokens from the
228 preprocessor. */
a723baf1
MM
229
230static cp_lexer *
17211ab5 231cp_lexer_new_main (void)
a723baf1 232{
17211ab5 233 cp_token first_token;
76aebc9f
NS
234 cp_lexer *lexer;
235 cp_token *pos;
236 size_t alloc;
237 size_t space;
238 cp_token *buffer;
17211ab5 239
03fd3f84 240 /* Tell cpplib we want CPP_PRAGMA tokens. */
c162c75e
MA
241 cpp_get_options (parse_in)->defer_pragmas = true;
242
243 /* Tell c_lex not to merge string constants. */
244 c_lex_return_raw_strings = true;
245
17211ab5
GK
246 /* It's possible that lexing the first token will load a PCH file,
247 which is a GC collection point. So we have to grab the first
248 token before allocating any memory. */
249 cp_lexer_get_preprocessor_token (NULL, &first_token);
18c81520 250 c_common_no_more_pch ();
a723baf1
MM
251
252 /* Allocate the memory. */
99dd239f 253 lexer = GGC_CNEW (cp_lexer);
a723baf1 254
6983ea08 255#ifdef ENABLE_CHECKING
c162c75e 256 /* Initially we are not debugging. */
a723baf1 257 lexer->debugging_p = false;
6983ea08 258#endif /* ENABLE_CHECKING */
76aebc9f
NS
259 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
260
261 /* Create the buffer. */
262 alloc = CP_LEXER_BUFFER_SIZE;
263 buffer = ggc_alloc (alloc * sizeof (cp_token));
a723baf1 264
76aebc9f
NS
265 /* Put the first token in the buffer. */
266 space = alloc;
267 pos = buffer;
268 *pos = first_token;
269
03fd3f84 270 /* Get the remaining tokens from the preprocessor. */
76aebc9f 271 while (pos->type != CPP_EOF)
c162c75e 272 {
76aebc9f
NS
273 pos++;
274 if (!--space)
275 {
276 space = alloc;
277 alloc *= 2;
278 buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279 pos = buffer + space;
280 }
281 cp_lexer_get_preprocessor_token (lexer, pos);
c162c75e 282 }
76aebc9f
NS
283 lexer->buffer = buffer;
284 lexer->buffer_length = alloc - space;
285 lexer->last_token = pos;
286 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
c162c75e
MA
287
288 /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289 direct calls to c_lex. Those callers all expect c_lex to do
290 string constant concatenation. */
291 c_lex_return_raw_strings = false;
292
2cfe82fe 293 gcc_assert (lexer->next_token->type != CPP_PURGED);
a723baf1
MM
294 return lexer;
295}
296
c162c75e 297/* Create a new lexer whose token stream is primed with the tokens in
2cfe82fe 298 CACHE. When these tokens are exhausted, no new tokens will be read. */
a723baf1
MM
299
300static cp_lexer *
2cfe82fe 301cp_lexer_new_from_tokens (cp_token_cache *cache)
a723baf1 302{
2cfe82fe
ZW
303 cp_token *first = cache->first;
304 cp_token *last = cache->last;
c162c75e 305 cp_lexer *lexer = GGC_CNEW (cp_lexer);
17211ab5 306
0c5e4866 307 /* We do not own the buffer. */
76aebc9f
NS
308 lexer->buffer = NULL;
309 lexer->buffer_length = 0;
310 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
0c5e4866
NS
311 lexer->last_token = last;
312
313 lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
21526606 314
6983ea08 315#ifdef ENABLE_CHECKING
c162c75e 316 /* Initially we are not debugging. */
17211ab5 317 lexer->debugging_p = false;
c162c75e 318#endif
a723baf1 319
2cfe82fe
ZW
320 gcc_assert (lexer->next_token->type != CPP_PURGED);
321 return lexer;
c162c75e
MA
322}
323
03fd3f84 324/* Frees all resources associated with LEXER. */
c162c75e
MA
325
326static void
327cp_lexer_destroy (cp_lexer *lexer)
328{
0c5e4866
NS
329 if (lexer->buffer)
330 ggc_free (lexer->buffer);
331 VEC_free (cp_token_position, lexer->saved_tokens);
c162c75e
MA
332 ggc_free (lexer);
333}
334
4de8668e 335/* Returns nonzero if debugging information should be output. */
a723baf1 336
6983ea08
MA
337#ifdef ENABLE_CHECKING
338
f7b5ecd9
MM
339static inline bool
340cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 341{
f7b5ecd9
MM
342 return lexer->debugging_p;
343}
344
6983ea08
MA
345#endif /* ENABLE_CHECKING */
346
0c5e4866
NS
347static inline cp_token_position
348cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
a723baf1 349{
0c5e4866
NS
350 gcc_assert (!previous_p || lexer->next_token != &eof_token);
351
352 return lexer->next_token - previous_p;
a723baf1
MM
353}
354
a668c6ad 355static inline cp_token *
0c5e4866 356cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
a668c6ad 357{
0c5e4866 358 return pos;
a668c6ad
MM
359}
360
4de8668e 361/* nonzero if we are presently saving tokens. */
f7b5ecd9 362
0c5e4866 363static inline int
94edc4ab 364cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9 365{
0c5e4866 366 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
a723baf1
MM
367}
368
76aebc9f
NS
369/* Store the next token from the preprocessor in *TOKEN. Return true
370 if we reach EOF. */
a723baf1 371
21526606 372static void
94edc4ab
NN
373cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
374 cp_token *token)
a723baf1 375{
7d381002 376 static int is_extern_c = 0;
a723baf1 377
51e63e60
NS
378 /* Get a new token from the preprocessor. */
379 token->type = c_lex_with_flags (&token->value, &token->flags);
82a98427 380 token->location = input_location;
c162c75e 381 token->in_system_header = in_system_header;
a723baf1 382
7d381002
MA
383 /* On some systems, some header files are surrounded by an
384 implicit extern "C" block. Set a flag in the token if it
03fd3f84 385 comes from such a header. */
7d381002
MA
386 is_extern_c += pending_lang_change;
387 pending_lang_change = 0;
388 token->implicit_extern_c = is_extern_c > 0;
389
a723baf1 390 /* Check to see if this token is a keyword. */
21526606 391 if (token->type == CPP_NAME
a723baf1
MM
392 && C_IS_RESERVED_WORD (token->value))
393 {
394 /* Mark this token as a keyword. */
395 token->type = CPP_KEYWORD;
396 /* Record which keyword. */
397 token->keyword = C_RID_CODE (token->value);
398 /* Update the value. Some keywords are mapped to particular
399 entities, rather than simply having the value of the
400 corresponding IDENTIFIER_NODE. For example, `__const' is
401 mapped to `const'. */
402 token->value = ridpointers[token->keyword];
403 }
404 else
405 token->keyword = RID_MAX;
406}
407
03fd3f84 408/* Update the globals input_location and in_system_header from TOKEN. */
2cfe82fe
ZW
409static inline void
410cp_lexer_set_source_position_from_token (cp_token *token)
411{
412 if (token->type != CPP_EOF)
413 {
414 input_location = token->location;
415 in_system_header = token->in_system_header;
416 }
417}
418
a723baf1
MM
419/* Return a pointer to the next token in the token stream, but do not
420 consume it. */
421
c162c75e
MA
422static inline cp_token *
423cp_lexer_peek_token (cp_lexer *lexer)
a723baf1 424{
a723baf1 425 if (cp_lexer_debugging_p (lexer))
0c5e4866
NS
426 {
427 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
428 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
429 putc ('\n', cp_lexer_debug_stream);
430 }
2cfe82fe 431 return lexer->next_token;
a723baf1
MM
432}
433
434/* Return true if the next token has the indicated TYPE. */
435
2cfe82fe 436static inline bool
94edc4ab 437cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1 438{
2cfe82fe 439 return cp_lexer_peek_token (lexer)->type == type;
a723baf1
MM
440}
441
442/* Return true if the next token does not have the indicated TYPE. */
443
2cfe82fe 444static inline bool
94edc4ab 445cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
446{
447 return !cp_lexer_next_token_is (lexer, type);
448}
449
450/* Return true if the next token is the indicated KEYWORD. */
451
2cfe82fe 452static inline bool
94edc4ab 453cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
454{
455 cp_token *token;
456
457 /* Peek at the next token. */
458 token = cp_lexer_peek_token (lexer);
459 /* Check to see if it is the indicated keyword. */
460 return token->keyword == keyword;
461}
462
463/* Return a pointer to the Nth token in the token stream. If N is 1,
2cfe82fe
ZW
464 then this is precisely equivalent to cp_lexer_peek_token (except
465 that it is not inline). One would like to disallow that case, but
466 there is one case (cp_parser_nth_token_starts_template_id) where
467 the caller passes a variable for N and it might be 1. */
a723baf1
MM
468
469static cp_token *
94edc4ab 470cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
471{
472 cp_token *token;
473
474 /* N is 1-based, not zero-based. */
0c5e4866 475 gcc_assert (n > 0 && lexer->next_token != &eof_token);
a723baf1 476
2cfe82fe
ZW
477 if (cp_lexer_debugging_p (lexer))
478 fprintf (cp_lexer_debug_stream,
479 "cp_lexer: peeking ahead %ld at token: ", (long)n);
480
c162c75e 481 --n;
a723baf1 482 token = lexer->next_token;
c162c75e 483 while (n != 0)
a723baf1 484 {
c162c75e 485 ++token;
0c5e4866
NS
486 if (token == lexer->last_token)
487 {
76aebc9f 488 token = (cp_token *)&eof_token;
0c5e4866
NS
489 break;
490 }
491
c162c75e
MA
492 if (token->type != CPP_PURGED)
493 --n;
a723baf1
MM
494 }
495
2cfe82fe
ZW
496 if (cp_lexer_debugging_p (lexer))
497 {
498 cp_lexer_print_token (cp_lexer_debug_stream, token);
499 putc ('\n', cp_lexer_debug_stream);
500 }
501
a723baf1
MM
502 return token;
503}
504
2cfe82fe
ZW
505/* Return the next token, and advance the lexer's next_token pointer
506 to point to the next non-purged token. */
a723baf1
MM
507
508static cp_token *
94edc4ab 509cp_lexer_consume_token (cp_lexer* lexer)
a723baf1 510{
2cfe82fe 511 cp_token *token = lexer->next_token;
a723baf1 512
0c5e4866
NS
513 gcc_assert (token != &eof_token);
514
2cfe82fe 515 do
0c5e4866
NS
516 {
517 lexer->next_token++;
518 if (lexer->next_token == lexer->last_token)
519 {
76aebc9f 520 lexer->next_token = (cp_token *)&eof_token;
0c5e4866
NS
521 break;
522 }
523
524 }
2cfe82fe 525 while (lexer->next_token->type == CPP_PURGED);
0c5e4866 526
2cfe82fe 527 cp_lexer_set_source_position_from_token (token);
0c5e4866 528
a723baf1
MM
529 /* Provide debugging output. */
530 if (cp_lexer_debugging_p (lexer))
531 {
2cfe82fe 532 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
a723baf1 533 cp_lexer_print_token (cp_lexer_debug_stream, token);
2cfe82fe 534 putc ('\n', cp_lexer_debug_stream);
a723baf1 535 }
0c5e4866 536
a723baf1
MM
537 return token;
538}
539
2cfe82fe
ZW
540/* Permanently remove the next token from the token stream, and
541 advance the next_token pointer to refer to the next non-purged
542 token. */
a723baf1
MM
543
544static void
545cp_lexer_purge_token (cp_lexer *lexer)
546{
c162c75e 547 cp_token *tok = lexer->next_token;
0c5e4866
NS
548
549 gcc_assert (tok != &eof_token);
c162c75e
MA
550 tok->type = CPP_PURGED;
551 tok->location = UNKNOWN_LOCATION;
552 tok->value = NULL_TREE;
553 tok->keyword = RID_MAX;
2cfe82fe
ZW
554
555 do
0c5e4866
NS
556 {
557 tok++;
558 if (tok == lexer->last_token)
559 {
76aebc9f 560 tok = (cp_token *)&eof_token;
0c5e4866
NS
561 break;
562 }
563 }
564 while (tok->type == CPP_PURGED);
565 lexer->next_token = tok;
a723baf1
MM
566}
567
c162c75e 568/* Permanently remove all tokens after TOK, up to, but not
a723baf1
MM
569 including, the token that will be returned next by
570 cp_lexer_peek_token. */
571
572static void
c162c75e 573cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
a723baf1 574{
0c5e4866 575 cp_token *peek = lexer->next_token;
a723baf1 576
0c5e4866
NS
577 if (peek == &eof_token)
578 peek = lexer->last_token;
579
c162c75e
MA
580 gcc_assert (tok < peek);
581
582 for ( tok += 1; tok != peek; tok += 1)
a723baf1 583 {
c162c75e
MA
584 tok->type = CPP_PURGED;
585 tok->location = UNKNOWN_LOCATION;
586 tok->value = NULL_TREE;
587 tok->keyword = RID_MAX;
a723baf1 588 }
c162c75e
MA
589}
590
03fd3f84 591/* Consume and handle a pragma token. */
c162c75e
MA
592static void
593cp_lexer_handle_pragma (cp_lexer *lexer)
594{
36952dea
ZW
595 cpp_string s;
596 cp_token *token = cp_lexer_consume_token (lexer);
597 gcc_assert (token->type == CPP_PRAGMA);
598 gcc_assert (token->value);
c162c75e 599
36952dea
ZW
600 s.len = TREE_STRING_LENGTH (token->value);
601 s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
c162c75e 602
36952dea 603 cpp_handle_deferred_pragma (parse_in, &s);
c162c75e 604
36952dea
ZW
605 /* Clearing token->value here means that we will get an ICE if we
606 try to process this #pragma again (which should be impossible). */
607 token->value = NULL;
a723baf1
MM
608}
609
610/* Begin saving tokens. All tokens consumed after this point will be
611 preserved. */
612
613static void
94edc4ab 614cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
615{
616 /* Provide debugging output. */
617 if (cp_lexer_debugging_p (lexer))
618 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
619
0c5e4866 620 VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
a723baf1
MM
621}
622
623/* Commit to the portion of the token stream most recently saved. */
624
625static void
94edc4ab 626cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
627{
628 /* Provide debugging output. */
629 if (cp_lexer_debugging_p (lexer))
630 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
631
0c5e4866 632 VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
633}
634
635/* Return all tokens saved since the last call to cp_lexer_save_tokens
636 to the token stream. Stop saving tokens. */
637
638static void
94edc4ab 639cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1 640{
a723baf1
MM
641 /* Provide debugging output. */
642 if (cp_lexer_debugging_p (lexer))
643 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
644
0c5e4866 645 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
a723baf1
MM
646}
647
a723baf1
MM
648/* Print a representation of the TOKEN on the STREAM. */
649
6983ea08
MA
650#ifdef ENABLE_CHECKING
651
a723baf1 652static void
c162c75e
MA
653cp_lexer_print_token (FILE * stream, cp_token *token)
654{
655 /* We don't use cpp_type2name here because the parser defines
656 a few tokens of its own. */
657 static const char *const token_names[] = {
658 /* cpplib-defined token types */
659#define OP(e, s) #e,
660#define TK(e, s) #e,
661 TTYPE_TABLE
662#undef OP
663#undef TK
664 /* C++ parser token types - see "Manifest constants", above. */
665 "KEYWORD",
666 "TEMPLATE_ID",
667 "NESTED_NAME_SPECIFIER",
668 "PURGED"
669 };
670
671 /* If we have a name for the token, print it out. Otherwise, we
672 simply give the numeric code. */
673 gcc_assert (token->type < ARRAY_SIZE(token_names));
674 fputs (token_names[token->type], stream);
a723baf1 675
c162c75e 676 /* For some tokens, print the associated data. */
a723baf1
MM
677 switch (token->type)
678 {
c162c75e
MA
679 case CPP_KEYWORD:
680 /* Some keywords have a value that is not an IDENTIFIER_NODE.
681 For example, `struct' is mapped to an INTEGER_CST. */
682 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
683 break;
684 /* else fall through */
a723baf1 685 case CPP_NAME:
c162c75e 686 fputs (IDENTIFIER_POINTER (token->value), stream);
a723baf1
MM
687 break;
688
c162c75e
MA
689 case CPP_STRING:
690 case CPP_WSTRING:
691 case CPP_PRAGMA:
692 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
a723baf1
MM
693 break;
694
a723baf1
MM
695 default:
696 break;
697 }
a723baf1
MM
698}
699
a723baf1
MM
700/* Start emitting debugging information. */
701
702static void
94edc4ab 703cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
704{
705 ++lexer->debugging_p;
706}
21526606 707
a723baf1
MM
708/* Stop emitting debugging information. */
709
710static void
94edc4ab 711cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
712{
713 --lexer->debugging_p;
714}
715
6983ea08
MA
716#endif /* ENABLE_CHECKING */
717
03fd3f84 718/* Create a new cp_token_cache, representing a range of tokens. */
c162c75e
MA
719
720static cp_token_cache *
721cp_token_cache_new (cp_token *first, cp_token *last)
722{
723 cp_token_cache *cache = GGC_NEW (cp_token_cache);
724 cache->first = first;
725 cache->last = last;
726 return cache;
727}
728
a723baf1 729\f
62d1db17
MM
730/* Decl-specifiers. */
731
732static void clear_decl_specs
733 (cp_decl_specifier_seq *);
734
735/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
736
737static void
738clear_decl_specs (cp_decl_specifier_seq *decl_specs)
739{
740 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
741}
742
058b15c1
MM
743/* Declarators. */
744
745/* Nothing other than the parser should be creating declarators;
746 declarators are a semi-syntactic representation of C++ entities.
747 Other parts of the front end that need to create entities (like
748 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
749
98ca843c 750static cp_declarator *make_id_declarator
058b15c1 751 (tree);
98ca843c 752static cp_declarator *make_call_declarator
3c01e5df 753 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
98ca843c 754static cp_declarator *make_array_declarator
058b15c1 755 (cp_declarator *, tree);
98ca843c 756static cp_declarator *make_pointer_declarator
3c01e5df 757 (cp_cv_quals, cp_declarator *);
98ca843c 758static cp_declarator *make_reference_declarator
3c01e5df 759 (cp_cv_quals, cp_declarator *);
98ca843c 760static cp_parameter_declarator *make_parameter_declarator
62d1db17 761 (cp_decl_specifier_seq *, cp_declarator *, tree);
98ca843c 762static cp_declarator *make_ptrmem_declarator
3c01e5df 763 (cp_cv_quals, tree, cp_declarator *);
058b15c1
MM
764
765cp_declarator *cp_error_declarator;
766
767/* The obstack on which declarators and related data structures are
768 allocated. */
769static struct obstack declarator_obstack;
770
771/* Alloc BYTES from the declarator memory pool. */
772
773static inline void *
774alloc_declarator (size_t bytes)
775{
776 return obstack_alloc (&declarator_obstack, bytes);
777}
778
779/* Allocate a declarator of the indicated KIND. Clear fields that are
780 common to all declarators. */
781
782static cp_declarator *
783make_declarator (cp_declarator_kind kind)
784{
785 cp_declarator *declarator;
786
787 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
788 declarator->kind = kind;
789 declarator->attributes = NULL_TREE;
790 declarator->declarator = NULL;
791
792 return declarator;
793}
794
795/* Make a declarator for a generalized identifier. */
796
797cp_declarator *
798make_id_declarator (tree id)
799{
800 cp_declarator *declarator;
98ca843c 801
058b15c1
MM
802 declarator = make_declarator (cdk_id);
803 declarator->u.id.name = id;
804 declarator->u.id.sfk = sfk_none;
805
806 return declarator;
807}
808
809/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
810 of modifiers such as const or volatile to apply to the pointer
811 type, represented as identifiers. */
812
813cp_declarator *
3c01e5df 814make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
815{
816 cp_declarator *declarator;
817
818 declarator = make_declarator (cdk_pointer);
819 declarator->declarator = target;
820 declarator->u.pointer.qualifiers = cv_qualifiers;
821 declarator->u.pointer.class_type = NULL_TREE;
822
823 return declarator;
824}
825
826/* Like make_pointer_declarator -- but for references. */
827
828cp_declarator *
3c01e5df 829make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
058b15c1
MM
830{
831 cp_declarator *declarator;
832
833 declarator = make_declarator (cdk_reference);
834 declarator->declarator = target;
835 declarator->u.pointer.qualifiers = cv_qualifiers;
836 declarator->u.pointer.class_type = NULL_TREE;
837
838 return declarator;
839}
840
841/* Like make_pointer_declarator -- but for a pointer to a non-static
842 member of CLASS_TYPE. */
843
844cp_declarator *
3c01e5df 845make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
058b15c1
MM
846 cp_declarator *pointee)
847{
848 cp_declarator *declarator;
849
850 declarator = make_declarator (cdk_ptrmem);
851 declarator->declarator = pointee;
852 declarator->u.pointer.qualifiers = cv_qualifiers;
853 declarator->u.pointer.class_type = class_type;
854
855 return declarator;
856}
857
858/* Make a declarator for the function given by TARGET, with the
859 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
860 "const"-qualified member function. The EXCEPTION_SPECIFICATION
861 indicates what exceptions can be thrown. */
862
863cp_declarator *
98ca843c 864make_call_declarator (cp_declarator *target,
058b15c1 865 cp_parameter_declarator *parms,
3c01e5df 866 cp_cv_quals cv_qualifiers,
058b15c1
MM
867 tree exception_specification)
868{
869 cp_declarator *declarator;
870
871 declarator = make_declarator (cdk_function);
872 declarator->declarator = target;
873 declarator->u.function.parameters = parms;
874 declarator->u.function.qualifiers = cv_qualifiers;
875 declarator->u.function.exception_specification = exception_specification;
876
877 return declarator;
878}
879
880/* Make a declarator for an array of BOUNDS elements, each of which is
881 defined by ELEMENT. */
882
883cp_declarator *
884make_array_declarator (cp_declarator *element, tree bounds)
885{
886 cp_declarator *declarator;
887
888 declarator = make_declarator (cdk_array);
889 declarator->declarator = element;
890 declarator->u.array.bounds = bounds;
891
892 return declarator;
893}
894
895cp_parameter_declarator *no_parameters;
896
897/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
898 DECLARATOR and DEFAULT_ARGUMENT. */
899
900cp_parameter_declarator *
98ca843c 901make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
058b15c1
MM
902 cp_declarator *declarator,
903 tree default_argument)
904{
905 cp_parameter_declarator *parameter;
906
98ca843c 907 parameter = ((cp_parameter_declarator *)
058b15c1
MM
908 alloc_declarator (sizeof (cp_parameter_declarator)));
909 parameter->next = NULL;
62d1db17
MM
910 if (decl_specifiers)
911 parameter->decl_specifiers = *decl_specifiers;
912 else
913 clear_decl_specs (&parameter->decl_specifiers);
058b15c1
MM
914 parameter->declarator = declarator;
915 parameter->default_argument = default_argument;
916 parameter->ellipsis_p = false;
917
918 return parameter;
919}
920
a723baf1
MM
921/* The parser. */
922
923/* Overview
924 --------
925
926 A cp_parser parses the token stream as specified by the C++
927 grammar. Its job is purely parsing, not semantic analysis. For
928 example, the parser breaks the token stream into declarators,
929 expressions, statements, and other similar syntactic constructs.
930 It does not check that the types of the expressions on either side
931 of an assignment-statement are compatible, or that a function is
932 not declared with a parameter of type `void'.
933
934 The parser invokes routines elsewhere in the compiler to perform
935 semantic analysis and to build up the abstract syntax tree for the
21526606 936 code processed.
a723baf1
MM
937
938 The parser (and the template instantiation code, which is, in a
939 way, a close relative of parsing) are the only parts of the
940 compiler that should be calling push_scope and pop_scope, or
941 related functions. The parser (and template instantiation code)
942 keeps track of what scope is presently active; everything else
943 should simply honor that. (The code that generates static
944 initializers may also need to set the scope, in order to check
945 access control correctly when emitting the initializers.)
946
947 Methodology
948 -----------
21526606 949
a723baf1
MM
950 The parser is of the standard recursive-descent variety. Upcoming
951 tokens in the token stream are examined in order to determine which
952 production to use when parsing a non-terminal. Some C++ constructs
953 require arbitrary look ahead to disambiguate. For example, it is
954 impossible, in the general case, to tell whether a statement is an
955 expression or declaration without scanning the entire statement.
956 Therefore, the parser is capable of "parsing tentatively." When the
957 parser is not sure what construct comes next, it enters this mode.
958 Then, while we attempt to parse the construct, the parser queues up
959 error messages, rather than issuing them immediately, and saves the
960 tokens it consumes. If the construct is parsed successfully, the
961 parser "commits", i.e., it issues any queued error messages and
962 the tokens that were being preserved are permanently discarded.
963 If, however, the construct is not parsed successfully, the parser
964 rolls back its state completely so that it can resume parsing using
965 a different alternative.
966
967 Future Improvements
968 -------------------
21526606 969
b8b94c5b
PB
970 The performance of the parser could probably be improved substantially.
971 We could often eliminate the need to parse tentatively by looking ahead
972 a little bit. In some places, this approach might not entirely eliminate
973 the need to parse tentatively, but it might still speed up the average
974 case. */
a723baf1
MM
975
976/* Flags that are passed to some parsing functions. These values can
977 be bitwise-ored together. */
978
979typedef enum cp_parser_flags
980{
981 /* No flags. */
982 CP_PARSER_FLAGS_NONE = 0x0,
983 /* The construct is optional. If it is not present, then no error
984 should be issued. */
985 CP_PARSER_FLAGS_OPTIONAL = 0x1,
986 /* When parsing a type-specifier, do not allow user-defined types. */
987 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
988} cp_parser_flags;
989
62b8a44e
NS
990/* The different kinds of declarators we want to parse. */
991
992typedef enum cp_parser_declarator_kind
993{
852dcbdd 994 /* We want an abstract declarator. */
62b8a44e
NS
995 CP_PARSER_DECLARATOR_ABSTRACT,
996 /* We want a named declarator. */
997 CP_PARSER_DECLARATOR_NAMED,
04c06002 998 /* We don't mind, but the name must be an unqualified-id. */
62b8a44e
NS
999 CP_PARSER_DECLARATOR_EITHER
1000} cp_parser_declarator_kind;
1001
b8b94c5b
PB
1002/* The precedence values used to parse binary expressions. The minimum value
1003 of PREC must be 1, because zero is reserved to quickly discriminate
1004 binary operators from other tokens. */
a723baf1 1005
b8b94c5b 1006enum cp_parser_prec
a723baf1 1007{
b8b94c5b
PB
1008 PREC_NOT_OPERATOR,
1009 PREC_LOGICAL_OR_EXPRESSION,
1010 PREC_LOGICAL_AND_EXPRESSION,
1011 PREC_INCLUSIVE_OR_EXPRESSION,
1012 PREC_EXCLUSIVE_OR_EXPRESSION,
1013 PREC_AND_EXPRESSION,
b8b94c5b 1014 PREC_EQUALITY_EXPRESSION,
69475123 1015 PREC_RELATIONAL_EXPRESSION,
b8b94c5b
PB
1016 PREC_SHIFT_EXPRESSION,
1017 PREC_ADDITIVE_EXPRESSION,
1018 PREC_MULTIPLICATIVE_EXPRESSION,
1019 PREC_PM_EXPRESSION,
1020 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1021};
a723baf1 1022
b8b94c5b
PB
1023/* A mapping from a token type to a corresponding tree node type, with a
1024 precedence value. */
a723baf1 1025
b8b94c5b
PB
1026typedef struct cp_parser_binary_operations_map_node
1027{
1028 /* The token type. */
1029 enum cpp_ttype token_type;
1030 /* The corresponding tree code. */
1031 enum tree_code tree_type;
1032 /* The precedence of this operator. */
1033 enum cp_parser_prec prec;
1034} cp_parser_binary_operations_map_node;
a723baf1
MM
1035
1036/* The status of a tentative parse. */
1037
1038typedef enum cp_parser_status_kind
1039{
1040 /* No errors have occurred. */
1041 CP_PARSER_STATUS_KIND_NO_ERROR,
1042 /* An error has occurred. */
1043 CP_PARSER_STATUS_KIND_ERROR,
1044 /* We are committed to this tentative parse, whether or not an error
1045 has occurred. */
1046 CP_PARSER_STATUS_KIND_COMMITTED
1047} cp_parser_status_kind;
1048
b8b94c5b
PB
1049typedef struct cp_parser_expression_stack_entry
1050{
1051 tree lhs;
1052 enum tree_code tree_type;
1053 int prec;
1054} cp_parser_expression_stack_entry;
1055
43c2a69a
PB
1056/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1057 entries because precedence levels on the stack are monotonically
1058 increasing. */
b8b94c5b
PB
1059typedef struct cp_parser_expression_stack_entry
1060 cp_parser_expression_stack[NUM_PREC_VALUES];
a723baf1 1061
b8b94c5b 1062/* Context that is saved and restored when parsing tentatively. */
a723baf1
MM
1063typedef struct cp_parser_context GTY (())
1064{
1065 /* If this is a tentative parsing context, the status of the
1066 tentative parse. */
1067 enum cp_parser_status_kind status;
1068 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1069 that are looked up in this context must be looked up both in the
1070 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1071 the context of the containing expression. */
1072 tree object_type;
b8b94c5b 1073
a723baf1
MM
1074 /* The next parsing context in the stack. */
1075 struct cp_parser_context *next;
1076} cp_parser_context;
1077
1078/* Prototypes. */
1079
1080/* Constructors and destructors. */
1081
1082static cp_parser_context *cp_parser_context_new
94edc4ab 1083 (cp_parser_context *);
a723baf1 1084
e5976695
MM
1085/* Class variables. */
1086
1431042e 1087static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
e5976695 1088
b8b94c5b
PB
1089/* The operator-precedence table used by cp_parser_binary_expression.
1090 Transformed into an associative array (binops_by_token) by
1091 cp_parser_new. */
1092
1093static const cp_parser_binary_operations_map_node binops[] = {
1094 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1095 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1096
1097 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1098 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1099 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1100
1101 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1102 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1103
1104 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1105 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1106
1107 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1108 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1109 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1110 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1111 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1112 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1113
1114 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1115 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1116
1117 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1118
1119 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1120
1121 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1122
1123 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1124
1125 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1126};
1127
1128/* The same as binops, but initialized by cp_parser_new so that
1129 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1130 for speed. */
1131static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1132
a723baf1
MM
1133/* Constructors and destructors. */
1134
1135/* Construct a new context. The context below this one on the stack
1136 is given by NEXT. */
1137
1138static cp_parser_context *
94edc4ab 1139cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1140{
1141 cp_parser_context *context;
1142
1143 /* Allocate the storage. */
e5976695
MM
1144 if (cp_parser_context_free_list != NULL)
1145 {
1146 /* Pull the first entry from the free list. */
1147 context = cp_parser_context_free_list;
1148 cp_parser_context_free_list = context->next;
c68b0a84 1149 memset (context, 0, sizeof (*context));
e5976695
MM
1150 }
1151 else
99dd239f 1152 context = GGC_CNEW (cp_parser_context);
b8b94c5b 1153
a723baf1
MM
1154 /* No errors have occurred yet in this context. */
1155 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1156 /* If this is not the bottomost context, copy information that we
1157 need from the previous context. */
1158 if (next)
1159 {
1160 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1161 expression, then we are parsing one in this context, too. */
1162 context->object_type = next->object_type;
a723baf1
MM
1163 /* Thread the stack. */
1164 context->next = next;
1165 }
1166
1167 return context;
1168}
1169
1170/* The cp_parser structure represents the C++ parser. */
1171
1172typedef struct cp_parser GTY(())
1173{
1174 /* The lexer from which we are obtaining tokens. */
1175 cp_lexer *lexer;
1176
1177 /* The scope in which names should be looked up. If NULL_TREE, then
1178 we look up names in the scope that is currently open in the
1179 source program. If non-NULL, this is either a TYPE or
21526606 1180 NAMESPACE_DECL for the scope in which we should look.
a723baf1
MM
1181
1182 This value is not cleared automatically after a name is looked
1183 up, so we must be careful to clear it before starting a new look
1184 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1185 will look up `Z' in the scope of `X', rather than the current
1186 scope.) Unfortunately, it is difficult to tell when name lookup
1187 is complete, because we sometimes peek at a token, look it up,
1188 and then decide not to consume it. */
1189 tree scope;
1190
1191 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1192 last lookup took place. OBJECT_SCOPE is used if an expression
1193 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
21526606 1194 respectively. QUALIFYING_SCOPE is used for an expression of the
a723baf1
MM
1195 form "X::Y"; it refers to X. */
1196 tree object_scope;
1197 tree qualifying_scope;
1198
1199 /* A stack of parsing contexts. All but the bottom entry on the
1200 stack will be tentative contexts.
1201
1202 We parse tentatively in order to determine which construct is in
1203 use in some situations. For example, in order to determine
1204 whether a statement is an expression-statement or a
1205 declaration-statement we parse it tentatively as a
1206 declaration-statement. If that fails, we then reparse the same
1207 token stream as an expression-statement. */
1208 cp_parser_context *context;
1209
1210 /* True if we are parsing GNU C++. If this flag is not set, then
1211 GNU extensions are not recognized. */
1212 bool allow_gnu_extensions_p;
1213
1214 /* TRUE if the `>' token should be interpreted as the greater-than
1215 operator. FALSE if it is the end of a template-id or
1216 template-parameter-list. */
1217 bool greater_than_is_operator_p;
1218
1219 /* TRUE if default arguments are allowed within a parameter list
1220 that starts at this point. FALSE if only a gnu extension makes
cd0be382 1221 them permissible. */
a723baf1 1222 bool default_arg_ok_p;
21526606 1223
a723baf1
MM
1224 /* TRUE if we are parsing an integral constant-expression. See
1225 [expr.const] for a precise definition. */
67c03833 1226 bool integral_constant_expression_p;
a723baf1 1227
14d22dd6
MM
1228 /* TRUE if we are parsing an integral constant-expression -- but a
1229 non-constant expression should be permitted as well. This flag
1230 is used when parsing an array bound so that GNU variable-length
1231 arrays are tolerated. */
67c03833 1232 bool allow_non_integral_constant_expression_p;
14d22dd6
MM
1233
1234 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1235 been seen that makes the expression non-constant. */
67c03833 1236 bool non_integral_constant_expression_p;
14d22dd6 1237
a723baf1
MM
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1241
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1246
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1250
4bb8ca28
MM
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1253
0e59b3fb
MM
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1257
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1261
4f8163b1
MM
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1266
7d381002 1267 /* TRUE if we are currently in a header file where declarations are
03fd3f84 1268 implicitly extern "C". */
7d381002
MA
1269 bool implicit_extern_c;
1270
c162c75e
MA
1271 /* TRUE if strings in expressions should be translated to the execution
1272 character set. */
1273 bool translate_strings_p;
1274
a723baf1
MM
1275 /* If non-NULL, then we are parsing a construct where new type
1276 definitions are not permitted. The string stored here will be
1277 issued as an error message if a type is defined. */
1278 const char *type_definition_forbidden_message;
1279
8db1028e
NS
1280 /* A list of lists. The outer list is a stack, used for member
1281 functions of local classes. At each level there are two sub-list,
1282 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1283 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1284 TREE_VALUE's. The functions are chained in reverse declaration
1285 order.
1286
1287 The TREE_PURPOSE sublist contains those functions with default
1288 arguments that need post processing, and the TREE_VALUE sublist
1289 contains those functions with definitions that need post
1290 processing.
1291
1292 These lists can only be processed once the outermost class being
9bcb9aae 1293 defined is complete. */
a723baf1
MM
1294 tree unparsed_functions_queues;
1295
1296 /* The number of classes whose definitions are currently in
1297 progress. */
1298 unsigned num_classes_being_defined;
1299
1300 /* The number of template parameter lists that apply directly to the
1301 current declaration. */
1302 unsigned num_template_parameter_lists;
1303} cp_parser;
1304
04c06002 1305/* The type of a function that parses some kind of expression. */
94edc4ab 1306typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1307
1308/* Prototypes. */
1309
1310/* Constructors and destructors. */
1311
1312static cp_parser *cp_parser_new
94edc4ab 1313 (void);
a723baf1 1314
21526606 1315/* Routines to parse various constructs.
a723baf1
MM
1316
1317 Those that return `tree' will return the error_mark_node (rather
1318 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1319 Sometimes, they will return an ordinary node if error-recovery was
34cd5ae7 1320 attempted, even though a parse error occurred. So, to check
a723baf1
MM
1321 whether or not a parse error occurred, you should always use
1322 cp_parser_error_occurred. If the construct is optional (indicated
1323 either by an `_opt' in the name of the function that does the
1324 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1325 the construct is not present. */
1326
1327/* Lexical conventions [gram.lex] */
1328
1329static tree cp_parser_identifier
94edc4ab 1330 (cp_parser *);
c162c75e
MA
1331static tree cp_parser_string_literal
1332 (cp_parser *, bool, bool);
a723baf1
MM
1333
1334/* Basic concepts [gram.basic] */
1335
1336static bool cp_parser_translation_unit
94edc4ab 1337 (cp_parser *);
a723baf1
MM
1338
1339/* Expressions [gram.expr] */
1340
1341static tree cp_parser_primary_expression
b3445994 1342 (cp_parser *, cp_id_kind *, tree *);
a723baf1 1343static tree cp_parser_id_expression
f3c2dfc6 1344 (cp_parser *, bool, bool, bool *, bool);
a723baf1 1345static tree cp_parser_unqualified_id
f3c2dfc6 1346 (cp_parser *, bool, bool, bool);
a723baf1 1347static tree cp_parser_nested_name_specifier_opt
a668c6ad 1348 (cp_parser *, bool, bool, bool, bool);
a723baf1 1349static tree cp_parser_nested_name_specifier
a723baf1 1350 (cp_parser *, bool, bool, bool, bool);
a668c6ad
MM
1351static tree cp_parser_class_or_namespace_name
1352 (cp_parser *, bool, bool, bool, bool, bool);
a723baf1
MM
1353static tree cp_parser_postfix_expression
1354 (cp_parser *, bool);
7a3ea201
RH
1355static tree cp_parser_postfix_open_square_expression
1356 (cp_parser *, tree, bool);
1357static tree cp_parser_postfix_dot_deref_expression
1358 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
7efa3e22 1359static tree cp_parser_parenthesized_expression_list
39703eb9 1360 (cp_parser *, bool, bool *);
a723baf1 1361static void cp_parser_pseudo_destructor_name
94edc4ab 1362 (cp_parser *, tree *, tree *);
a723baf1
MM
1363static tree cp_parser_unary_expression
1364 (cp_parser *, bool);
1365static enum tree_code cp_parser_unary_operator
94edc4ab 1366 (cp_token *);
a723baf1 1367static tree cp_parser_new_expression
94edc4ab 1368 (cp_parser *);
a723baf1 1369static tree cp_parser_new_placement
94edc4ab 1370 (cp_parser *);
a723baf1 1371static tree cp_parser_new_type_id
058b15c1
MM
1372 (cp_parser *, tree *);
1373static cp_declarator *cp_parser_new_declarator_opt
94edc4ab 1374 (cp_parser *);
058b15c1 1375static cp_declarator *cp_parser_direct_new_declarator
94edc4ab 1376 (cp_parser *);
a723baf1 1377static tree cp_parser_new_initializer
94edc4ab 1378 (cp_parser *);
a723baf1 1379static tree cp_parser_delete_expression
94edc4ab 1380 (cp_parser *);
21526606 1381static tree cp_parser_cast_expression
a723baf1 1382 (cp_parser *, bool);
b8b94c5b 1383static tree cp_parser_binary_expression
94edc4ab 1384 (cp_parser *);
a723baf1 1385static tree cp_parser_question_colon_clause
94edc4ab 1386 (cp_parser *, tree);
a723baf1 1387static tree cp_parser_assignment_expression
94edc4ab 1388 (cp_parser *);
a723baf1 1389static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1390 (cp_parser *);
a723baf1 1391static tree cp_parser_expression
94edc4ab 1392 (cp_parser *);
a723baf1 1393static tree cp_parser_constant_expression
14d22dd6 1394 (cp_parser *, bool, bool *);
7a3ea201
RH
1395static tree cp_parser_builtin_offsetof
1396 (cp_parser *);
a723baf1
MM
1397
1398/* Statements [gram.stmt.stmt] */
1399
1400static void cp_parser_statement
325c3691 1401 (cp_parser *, tree);
a723baf1 1402static tree cp_parser_labeled_statement
325c3691 1403 (cp_parser *, tree);
a723baf1 1404static tree cp_parser_expression_statement
325c3691 1405 (cp_parser *, tree);
a723baf1 1406static tree cp_parser_compound_statement
325c3691 1407 (cp_parser *, tree, bool);
a723baf1 1408static void cp_parser_statement_seq_opt
325c3691 1409 (cp_parser *, tree);
a723baf1 1410static tree cp_parser_selection_statement
94edc4ab 1411 (cp_parser *);
a723baf1 1412static tree cp_parser_condition
94edc4ab 1413 (cp_parser *);
a723baf1 1414static tree cp_parser_iteration_statement
94edc4ab 1415 (cp_parser *);
a723baf1 1416static void cp_parser_for_init_statement
94edc4ab 1417 (cp_parser *);
a723baf1 1418static tree cp_parser_jump_statement
94edc4ab 1419 (cp_parser *);
a723baf1 1420static void cp_parser_declaration_statement
94edc4ab 1421 (cp_parser *);
a723baf1
MM
1422
1423static tree cp_parser_implicitly_scoped_statement
94edc4ab 1424 (cp_parser *);
a723baf1 1425static void cp_parser_already_scoped_statement
94edc4ab 1426 (cp_parser *);
a723baf1
MM
1427
1428/* Declarations [gram.dcl.dcl] */
1429
1430static void cp_parser_declaration_seq_opt
94edc4ab 1431 (cp_parser *);
a723baf1 1432static void cp_parser_declaration
94edc4ab 1433 (cp_parser *);
a723baf1 1434static void cp_parser_block_declaration
94edc4ab 1435 (cp_parser *, bool);
a723baf1 1436static void cp_parser_simple_declaration
94edc4ab 1437 (cp_parser *, bool);
62d1db17
MM
1438static void cp_parser_decl_specifier_seq
1439 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
a723baf1 1440static tree cp_parser_storage_class_specifier_opt
94edc4ab 1441 (cp_parser *);
a723baf1 1442static tree cp_parser_function_specifier_opt
62d1db17 1443 (cp_parser *, cp_decl_specifier_seq *);
a723baf1 1444static tree cp_parser_type_specifier
98ca843c 1445 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
62d1db17 1446 int *, bool *);
a723baf1 1447static tree cp_parser_simple_type_specifier
62d1db17 1448 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
a723baf1 1449static tree cp_parser_type_name
94edc4ab 1450 (cp_parser *);
a723baf1 1451static tree cp_parser_elaborated_type_specifier
94edc4ab 1452 (cp_parser *, bool, bool);
a723baf1 1453static tree cp_parser_enum_specifier
94edc4ab 1454 (cp_parser *);
a723baf1 1455static void cp_parser_enumerator_list
94edc4ab 1456 (cp_parser *, tree);
21526606 1457static void cp_parser_enumerator_definition
94edc4ab 1458 (cp_parser *, tree);
a723baf1 1459static tree cp_parser_namespace_name
94edc4ab 1460 (cp_parser *);
a723baf1 1461static void cp_parser_namespace_definition
94edc4ab 1462 (cp_parser *);
a723baf1 1463static void cp_parser_namespace_body
94edc4ab 1464 (cp_parser *);
a723baf1 1465static tree cp_parser_qualified_namespace_specifier
94edc4ab 1466 (cp_parser *);
a723baf1 1467static void cp_parser_namespace_alias_definition
94edc4ab 1468 (cp_parser *);
a723baf1 1469static void cp_parser_using_declaration
94edc4ab 1470 (cp_parser *);
a723baf1 1471static void cp_parser_using_directive
94edc4ab 1472 (cp_parser *);
a723baf1 1473static void cp_parser_asm_definition
94edc4ab 1474 (cp_parser *);
a723baf1 1475static void cp_parser_linkage_specification
94edc4ab 1476 (cp_parser *);
a723baf1
MM
1477
1478/* Declarators [gram.dcl.decl] */
1479
1480static tree cp_parser_init_declarator
62d1db17 1481 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
058b15c1 1482static cp_declarator *cp_parser_declarator
db86dd14 1483 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
058b15c1 1484static cp_declarator *cp_parser_direct_declarator
db86dd14 1485 (cp_parser *, cp_parser_declarator_kind, int *, bool);
a723baf1 1486static enum tree_code cp_parser_ptr_operator
3c01e5df
MM
1487 (cp_parser *, tree *, cp_cv_quals *);
1488static cp_cv_quals cp_parser_cv_qualifier_seq_opt
94edc4ab 1489 (cp_parser *);
a723baf1 1490static tree cp_parser_declarator_id
94edc4ab 1491 (cp_parser *);
a723baf1 1492static tree cp_parser_type_id
94edc4ab 1493 (cp_parser *);
62d1db17
MM
1494static void cp_parser_type_specifier_seq
1495 (cp_parser *, cp_decl_specifier_seq *);
058b15c1 1496static cp_parameter_declarator *cp_parser_parameter_declaration_clause
94edc4ab 1497 (cp_parser *);
058b15c1
MM
1498static cp_parameter_declarator *cp_parser_parameter_declaration_list
1499 (cp_parser *, bool *);
1500static cp_parameter_declarator *cp_parser_parameter_declaration
4bb8ca28 1501 (cp_parser *, bool, bool *);
a723baf1
MM
1502static void cp_parser_function_body
1503 (cp_parser *);
1504static tree cp_parser_initializer
39703eb9 1505 (cp_parser *, bool *, bool *);
a723baf1 1506static tree cp_parser_initializer_clause
39703eb9 1507 (cp_parser *, bool *);
a723baf1 1508static tree cp_parser_initializer_list
39703eb9 1509 (cp_parser *, bool *);
a723baf1
MM
1510
1511static bool cp_parser_ctor_initializer_opt_and_function_body
1512 (cp_parser *);
1513
1514/* Classes [gram.class] */
1515
1516static tree cp_parser_class_name
fc6a28d7 1517 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
a723baf1 1518static tree cp_parser_class_specifier
94edc4ab 1519 (cp_parser *);
a723baf1 1520static tree cp_parser_class_head
38b305d0 1521 (cp_parser *, bool *, tree *);
a723baf1 1522static enum tag_types cp_parser_class_key
94edc4ab 1523 (cp_parser *);
a723baf1 1524static void cp_parser_member_specification_opt
94edc4ab 1525 (cp_parser *);
a723baf1 1526static void cp_parser_member_declaration
94edc4ab 1527 (cp_parser *);
a723baf1 1528static tree cp_parser_pure_specifier
94edc4ab 1529 (cp_parser *);
a723baf1 1530static tree cp_parser_constant_initializer
94edc4ab 1531 (cp_parser *);
a723baf1
MM
1532
1533/* Derived classes [gram.class.derived] */
1534
1535static tree cp_parser_base_clause
94edc4ab 1536 (cp_parser *);
a723baf1 1537static tree cp_parser_base_specifier
94edc4ab 1538 (cp_parser *);
a723baf1
MM
1539
1540/* Special member functions [gram.special] */
1541
1542static tree cp_parser_conversion_function_id
94edc4ab 1543 (cp_parser *);
a723baf1 1544static tree cp_parser_conversion_type_id
94edc4ab 1545 (cp_parser *);
058b15c1 1546static cp_declarator *cp_parser_conversion_declarator_opt
94edc4ab 1547 (cp_parser *);
a723baf1 1548static bool cp_parser_ctor_initializer_opt
94edc4ab 1549 (cp_parser *);
a723baf1 1550static void cp_parser_mem_initializer_list
94edc4ab 1551 (cp_parser *);
a723baf1 1552static tree cp_parser_mem_initializer
94edc4ab 1553 (cp_parser *);
a723baf1 1554static tree cp_parser_mem_initializer_id
94edc4ab 1555 (cp_parser *);
a723baf1
MM
1556
1557/* Overloading [gram.over] */
1558
1559static tree cp_parser_operator_function_id
94edc4ab 1560 (cp_parser *);
a723baf1 1561static tree cp_parser_operator
94edc4ab 1562 (cp_parser *);
a723baf1
MM
1563
1564/* Templates [gram.temp] */
1565
1566static void cp_parser_template_declaration
94edc4ab 1567 (cp_parser *, bool);
a723baf1 1568static tree cp_parser_template_parameter_list
94edc4ab 1569 (cp_parser *);
a723baf1 1570static tree cp_parser_template_parameter
058b15c1 1571 (cp_parser *, bool *);
a723baf1 1572static tree cp_parser_type_parameter
94edc4ab 1573 (cp_parser *);
a723baf1 1574static tree cp_parser_template_id
a668c6ad 1575 (cp_parser *, bool, bool, bool);
a723baf1 1576static tree cp_parser_template_name
a668c6ad 1577 (cp_parser *, bool, bool, bool, bool *);
a723baf1 1578static tree cp_parser_template_argument_list
94edc4ab 1579 (cp_parser *);
a723baf1 1580static tree cp_parser_template_argument
94edc4ab 1581 (cp_parser *);
a723baf1 1582static void cp_parser_explicit_instantiation
94edc4ab 1583 (cp_parser *);
a723baf1 1584static void cp_parser_explicit_specialization
94edc4ab 1585 (cp_parser *);
a723baf1
MM
1586
1587/* Exception handling [gram.exception] */
1588
21526606 1589static tree cp_parser_try_block
94edc4ab 1590 (cp_parser *);
a723baf1 1591static bool cp_parser_function_try_block
94edc4ab 1592 (cp_parser *);
a723baf1 1593static void cp_parser_handler_seq
94edc4ab 1594 (cp_parser *);
a723baf1 1595static void cp_parser_handler
94edc4ab 1596 (cp_parser *);
a723baf1 1597static tree cp_parser_exception_declaration
94edc4ab 1598 (cp_parser *);
a723baf1 1599static tree cp_parser_throw_expression
94edc4ab 1600 (cp_parser *);
a723baf1 1601static tree cp_parser_exception_specification_opt
94edc4ab 1602 (cp_parser *);
a723baf1 1603static tree cp_parser_type_id_list
94edc4ab 1604 (cp_parser *);
a723baf1
MM
1605
1606/* GNU Extensions */
1607
1608static tree cp_parser_asm_specification_opt
94edc4ab 1609 (cp_parser *);
a723baf1 1610static tree cp_parser_asm_operand_list
94edc4ab 1611 (cp_parser *);
a723baf1 1612static tree cp_parser_asm_clobber_list
94edc4ab 1613 (cp_parser *);
a723baf1 1614static tree cp_parser_attributes_opt
94edc4ab 1615 (cp_parser *);
a723baf1 1616static tree cp_parser_attribute_list
94edc4ab 1617 (cp_parser *);
a723baf1 1618static bool cp_parser_extension_opt
94edc4ab 1619 (cp_parser *, int *);
a723baf1 1620static void cp_parser_label_declaration
94edc4ab 1621 (cp_parser *);
a723baf1
MM
1622
1623/* Utility Routines */
1624
1625static tree cp_parser_lookup_name
fc6a28d7 1626 (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
a723baf1 1627static tree cp_parser_lookup_name_simple
94edc4ab 1628 (cp_parser *, tree);
a723baf1
MM
1629static tree cp_parser_maybe_treat_template_as_class
1630 (tree, bool);
1631static bool cp_parser_check_declarator_template_parameters
058b15c1 1632 (cp_parser *, cp_declarator *);
a723baf1 1633static bool cp_parser_check_template_parameters
94edc4ab 1634 (cp_parser *, unsigned);
d6b4ea85
MM
1635static tree cp_parser_simple_cast_expression
1636 (cp_parser *);
a723baf1 1637static tree cp_parser_global_scope_opt
94edc4ab 1638 (cp_parser *, bool);
a723baf1
MM
1639static bool cp_parser_constructor_declarator_p
1640 (cp_parser *, bool);
1641static tree cp_parser_function_definition_from_specifiers_and_declarator
62d1db17 1642 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
a723baf1 1643static tree cp_parser_function_definition_after_declarator
94edc4ab 1644 (cp_parser *, bool);
a723baf1 1645static void cp_parser_template_declaration_after_export
94edc4ab 1646 (cp_parser *, bool);
a723baf1 1647static tree cp_parser_single_declaration
94edc4ab 1648 (cp_parser *, bool, bool *);
a723baf1 1649static tree cp_parser_functional_cast
94edc4ab 1650 (cp_parser *, tree);
4bb8ca28 1651static tree cp_parser_save_member_function_body
62d1db17 1652 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
ec75414f
MM
1653static tree cp_parser_enclosed_template_argument_list
1654 (cp_parser *);
8db1028e
NS
1655static void cp_parser_save_default_args
1656 (cp_parser *, tree);
a723baf1 1657static void cp_parser_late_parsing_for_member
94edc4ab 1658 (cp_parser *, tree);
a723baf1 1659static void cp_parser_late_parsing_default_args
8218bd34 1660 (cp_parser *, tree);
a723baf1 1661static tree cp_parser_sizeof_operand
94edc4ab 1662 (cp_parser *, enum rid);
a723baf1 1663static bool cp_parser_declares_only_class_p
94edc4ab 1664 (cp_parser *);
62d1db17
MM
1665static void cp_parser_set_storage_class
1666 (cp_decl_specifier_seq *, cp_storage_class);
98ca843c 1667static void cp_parser_set_decl_spec_type
62d1db17 1668 (cp_decl_specifier_seq *, tree, bool);
a723baf1 1669static bool cp_parser_friend_p
62d1db17 1670 (const cp_decl_specifier_seq *);
a723baf1 1671static cp_token *cp_parser_require
94edc4ab 1672 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1673static cp_token *cp_parser_require_keyword
94edc4ab 1674 (cp_parser *, enum rid, const char *);
21526606 1675static bool cp_parser_token_starts_function_definition_p
94edc4ab 1676 (cp_token *);
a723baf1
MM
1677static bool cp_parser_next_token_starts_class_definition_p
1678 (cp_parser *);
d17811fd
MM
1679static bool cp_parser_next_token_ends_template_argument_p
1680 (cp_parser *);
f4abade9
GB
1681static bool cp_parser_nth_token_starts_template_argument_list_p
1682 (cp_parser *, size_t);
a723baf1 1683static enum tag_types cp_parser_token_is_class_key
94edc4ab 1684 (cp_token *);
a723baf1
MM
1685static void cp_parser_check_class_key
1686 (enum tag_types, tree type);
37d407a1
KL
1687static void cp_parser_check_access_in_redeclaration
1688 (tree type);
a723baf1
MM
1689static bool cp_parser_optional_template_keyword
1690 (cp_parser *);
21526606 1691static void cp_parser_pre_parsed_nested_name_specifier
2050a1bb 1692 (cp_parser *);
a723baf1 1693static void cp_parser_cache_group
c162c75e 1694 (cp_parser *, enum cpp_ttype, unsigned);
21526606 1695static void cp_parser_parse_tentatively
94edc4ab 1696 (cp_parser *);
a723baf1 1697static void cp_parser_commit_to_tentative_parse
94edc4ab 1698 (cp_parser *);
a723baf1 1699static void cp_parser_abort_tentative_parse
94edc4ab 1700 (cp_parser *);
a723baf1 1701static bool cp_parser_parse_definitely
94edc4ab 1702 (cp_parser *);
f7b5ecd9 1703static inline bool cp_parser_parsing_tentatively
94edc4ab 1704 (cp_parser *);
0b16f8f4 1705static bool cp_parser_uncommitted_to_tentative_parse_p
94edc4ab 1706 (cp_parser *);
a723baf1 1707static void cp_parser_error
94edc4ab 1708 (cp_parser *, const char *);
4bb8ca28
MM
1709static void cp_parser_name_lookup_error
1710 (cp_parser *, tree, tree, const char *);
e5976695 1711static bool cp_parser_simulate_error
94edc4ab 1712 (cp_parser *);
a723baf1 1713static void cp_parser_check_type_definition
94edc4ab 1714 (cp_parser *);
560ad596 1715static void cp_parser_check_for_definition_in_return_type
fc6a28d7 1716 (cp_declarator *, tree);
ee43dab5
MM
1717static void cp_parser_check_for_invalid_template_id
1718 (cp_parser *, tree);
625cbf93
MM
1719static bool cp_parser_non_integral_constant_expression
1720 (cp_parser *, const char *);
2097b5f2
GB
1721static void cp_parser_diagnose_invalid_type_name
1722 (cp_parser *, tree, tree);
1723static bool cp_parser_parse_and_diagnose_invalid_type_name
8fbc5ae7 1724 (cp_parser *);
7efa3e22 1725static int cp_parser_skip_to_closing_parenthesis
a668c6ad 1726 (cp_parser *, bool, bool, bool);
a723baf1 1727static void cp_parser_skip_to_end_of_statement
94edc4ab 1728 (cp_parser *);
e0860732
MM
1729static void cp_parser_consume_semicolon_at_end_of_statement
1730 (cp_parser *);
a723baf1 1731static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1732 (cp_parser *);
a723baf1
MM
1733static void cp_parser_skip_to_closing_brace
1734 (cp_parser *);
1735static void cp_parser_skip_until_found
94edc4ab 1736 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1737static bool cp_parser_error_occurred
94edc4ab 1738 (cp_parser *);
a723baf1 1739static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1740 (cp_parser *);
a723baf1 1741static bool cp_parser_is_string_literal
94edc4ab 1742 (cp_token *);
21526606 1743static bool cp_parser_is_keyword
94edc4ab 1744 (cp_token *, enum rid);
2097b5f2
GB
1745static tree cp_parser_make_typename_type
1746 (cp_parser *, tree, tree);
a723baf1 1747
4de8668e 1748/* Returns nonzero if we are parsing tentatively. */
f7b5ecd9
MM
1749
1750static inline bool
94edc4ab 1751cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1752{
1753 return parser->context->next != NULL;
1754}
1755
4de8668e 1756/* Returns nonzero if TOKEN is a string literal. */
a723baf1
MM
1757
1758static bool
94edc4ab 1759cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1760{
1761 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1762}
1763
4de8668e 1764/* Returns nonzero if TOKEN is the indicated KEYWORD. */
a723baf1
MM
1765
1766static bool
94edc4ab 1767cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1768{
1769 return token->keyword == keyword;
1770}
1771
2cfe82fe
ZW
1772/* If not parsing tentatively, issue a diagnostic of the form
1773 FILE:LINE: MESSAGE before TOKEN
1774 where TOKEN is the next token in the input stream. MESSAGE
1775 (specified by the caller) is usually of the form "expected
1776 OTHER-TOKEN". */
a723baf1
MM
1777
1778static void
94edc4ab 1779cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1780{
e5976695 1781 if (!cp_parser_simulate_error (parser))
4bb8ca28 1782 {
2cfe82fe
ZW
1783 cp_token *token = cp_lexer_peek_token (parser->lexer);
1784 /* This diagnostic makes more sense if it is tagged to the line
1785 of the token we just peeked at. */
1786 cp_lexer_set_source_position_from_token (token);
21526606 1787 c_parse_error (message,
5c832178
MM
1788 /* Because c_parser_error does not understand
1789 CPP_KEYWORD, keywords are treated like
1790 identifiers. */
21526606 1791 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
5c832178 1792 token->value);
4bb8ca28
MM
1793 }
1794}
1795
1796/* Issue an error about name-lookup failing. NAME is the
1797 IDENTIFIER_NODE DECL is the result of
1798 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1799 the thing that we hoped to find. */
1800
1801static void
1802cp_parser_name_lookup_error (cp_parser* parser,
1803 tree name,
1804 tree decl,
1805 const char* desired)
1806{
1807 /* If name lookup completely failed, tell the user that NAME was not
1808 declared. */
1809 if (decl == error_mark_node)
1810 {
1811 if (parser->scope && parser->scope != global_namespace)
2a13a625 1812 error ("%<%D::%D%> has not been declared",
4bb8ca28
MM
1813 parser->scope, name);
1814 else if (parser->scope == global_namespace)
2a13a625 1815 error ("%<::%D%> has not been declared", name);
b14454ba
MM
1816 else if (parser->object_scope
1817 && !CLASS_TYPE_P (parser->object_scope))
2a13a625 1818 error ("request for member %qD in non-class type %qT",
b14454ba
MM
1819 name, parser->object_scope);
1820 else if (parser->object_scope)
2a13a625 1821 error ("%<%T::%D%> has not been declared",
b14454ba 1822 parser->object_scope, name);
4bb8ca28 1823 else
9e637a26 1824 error ("%qD has not been declared", name);
4bb8ca28
MM
1825 }
1826 else if (parser->scope && parser->scope != global_namespace)
2a13a625 1827 error ("%<%D::%D%> %s", parser->scope, name, desired);
4bb8ca28 1828 else if (parser->scope == global_namespace)
2a13a625 1829 error ("%<::%D%> %s", name, desired);
4bb8ca28 1830 else
2a13a625 1831 error ("%qD %s", name, desired);
a723baf1
MM
1832}
1833
1834/* If we are parsing tentatively, remember that an error has occurred
e5976695 1835 during this tentative parse. Returns true if the error was
77077b39 1836 simulated; false if a message should be issued by the caller. */
a723baf1 1837
e5976695 1838static bool
94edc4ab 1839cp_parser_simulate_error (cp_parser* parser)
a723baf1 1840{
0b16f8f4 1841 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
e5976695
MM
1842 {
1843 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1844 return true;
1845 }
1846 return false;
a723baf1
MM
1847}
1848
1849/* This function is called when a type is defined. If type
1850 definitions are forbidden at this point, an error message is
1851 issued. */
1852
1853static void
94edc4ab 1854cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1855{
1856 /* If types are forbidden here, issue a message. */
1857 if (parser->type_definition_forbidden_message)
1858 /* Use `%s' to print the string in case there are any escape
1859 characters in the message. */
1860 error ("%s", parser->type_definition_forbidden_message);
1861}
1862
fc6a28d7 1863/* This function is called when the DECLARATOR is processed. The TYPE
472c29c3 1864 was a type defined in the decl-specifiers. If it is invalid to
fc6a28d7
MM
1865 define a type in the decl-specifiers for DECLARATOR, an error is
1866 issued. */
560ad596
MM
1867
1868static void
058b15c1 1869cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
fc6a28d7 1870 tree type)
560ad596
MM
1871{
1872 /* [dcl.fct] forbids type definitions in return types.
1873 Unfortunately, it's not easy to know whether or not we are
1874 processing a return type until after the fact. */
1875 while (declarator
058b15c1
MM
1876 && (declarator->kind == cdk_pointer
1877 || declarator->kind == cdk_reference
1878 || declarator->kind == cdk_ptrmem))
1879 declarator = declarator->declarator;
560ad596 1880 if (declarator
fc6a28d7
MM
1881 && declarator->kind == cdk_function)
1882 {
1883 error ("new types may not be defined in a return type");
1884 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1885 type);
1886 }
560ad596
MM
1887}
1888
ee43dab5
MM
1889/* A type-specifier (TYPE) has been parsed which cannot be followed by
1890 "<" in any valid C++ program. If the next token is indeed "<",
1891 issue a message warning the user about what appears to be an
1892 invalid attempt to form a template-id. */
1893
1894static void
21526606 1895cp_parser_check_for_invalid_template_id (cp_parser* parser,
ee43dab5
MM
1896 tree type)
1897{
0c5e4866 1898 cp_token_position start = 0;
ee43dab5
MM
1899
1900 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1901 {
1902 if (TYPE_P (type))
2a13a625 1903 error ("%qT is not a template", type);
ee43dab5 1904 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2a13a625 1905 error ("%qE is not a template", type);
ee43dab5
MM
1906 else
1907 error ("invalid template-id");
1908 /* Remember the location of the invalid "<". */
0b16f8f4 1909 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 1910 start = cp_lexer_token_position (parser->lexer, true);
ee43dab5
MM
1911 /* Consume the "<". */
1912 cp_lexer_consume_token (parser->lexer);
1913 /* Parse the template arguments. */
1914 cp_parser_enclosed_template_argument_list (parser);
da1d7781 1915 /* Permanently remove the invalid template arguments so that
ee43dab5 1916 this error message is not issued again. */
0c5e4866
NS
1917 if (start)
1918 cp_lexer_purge_tokens_after (parser->lexer, start);
ee43dab5
MM
1919 }
1920}
1921
625cbf93
MM
1922/* If parsing an integral constant-expression, issue an error message
1923 about the fact that THING appeared and return true. Otherwise,
1924 return false, marking the current expression as non-constant. */
14d22dd6 1925
625cbf93
MM
1926static bool
1927cp_parser_non_integral_constant_expression (cp_parser *parser,
1928 const char *thing)
14d22dd6 1929{
625cbf93
MM
1930 if (parser->integral_constant_expression_p)
1931 {
1932 if (!parser->allow_non_integral_constant_expression_p)
1933 {
1934 error ("%s cannot appear in a constant-expression", thing);
1935 return true;
1936 }
1937 parser->non_integral_constant_expression_p = true;
1938 }
1939 return false;
14d22dd6
MM
1940}
1941
0c88d886
MM
1942/* Emit a diagnostic for an invalid type name. SCOPE is the
1943 qualifying scope (or NULL, if none) for ID. */
8fbc5ae7 1944
2097b5f2
GB
1945static void
1946cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
1947{
1948 tree decl, old_scope;
2097b5f2
GB
1949 /* Try to lookup the identifier. */
1950 old_scope = parser->scope;
1951 parser->scope = scope;
1952 decl = cp_parser_lookup_name_simple (parser, id);
1953 parser->scope = old_scope;
1954 /* If the lookup found a template-name, it means that the user forgot
1955 to specify an argument list. Emit an useful error message. */
1956 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 1957 error ("invalid use of template-name %qE without an argument list",
6c0cc713 1958 decl);
2097b5f2 1959 else if (!parser->scope)
8fbc5ae7 1960 {
8fbc5ae7 1961 /* Issue an error message. */
2a13a625 1962 error ("%qE does not name a type", id);
8fbc5ae7
MM
1963 /* If we're in a template class, it's possible that the user was
1964 referring to a type from a base class. For example:
1965
1966 template <typename T> struct A { typedef T X; };
1967 template <typename T> struct B : public A<T> { X x; };
1968
1969 The user should have said "typename A<T>::X". */
1970 if (processing_template_decl && current_class_type)
1971 {
1972 tree b;
1973
1974 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1975 b;
1976 b = TREE_CHAIN (b))
1977 {
1978 tree base_type = BINFO_TYPE (b);
21526606 1979 if (CLASS_TYPE_P (base_type)
1fb3244a 1980 && dependent_type_p (base_type))
8fbc5ae7
MM
1981 {
1982 tree field;
1983 /* Go from a particular instantiation of the
1984 template (which will have an empty TYPE_FIELDs),
1985 to the main version. */
353b4fc0 1986 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1987 for (field = TYPE_FIELDS (base_type);
1988 field;
1989 field = TREE_CHAIN (field))
1990 if (TREE_CODE (field) == TYPE_DECL
2097b5f2 1991 && DECL_NAME (field) == id)
8fbc5ae7 1992 {
c4f73174 1993 inform ("(perhaps %<typename %T::%E%> was intended)",
2097b5f2 1994 BINFO_TYPE (b), id);
8fbc5ae7
MM
1995 break;
1996 }
1997 if (field)
1998 break;
1999 }
2000 }
2001 }
8fbc5ae7 2002 }
2097b5f2
GB
2003 /* Here we diagnose qualified-ids where the scope is actually correct,
2004 but the identifier does not resolve to a valid type name. */
21526606 2005 else
2097b5f2
GB
2006 {
2007 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2a13a625 2008 error ("%qE in namespace %qE does not name a type",
2097b5f2
GB
2009 id, parser->scope);
2010 else if (TYPE_P (parser->scope))
2fbe4889 2011 error ("%qE in class %qT does not name a type", id, parser->scope);
2097b5f2 2012 else
315fb5db 2013 gcc_unreachable ();
2097b5f2
GB
2014 }
2015}
8fbc5ae7 2016
2097b5f2
GB
2017/* Check for a common situation where a type-name should be present,
2018 but is not, and issue a sensible error message. Returns true if an
2019 invalid type-name was detected.
21526606 2020
2097b5f2 2021 The situation handled by this function are variable declarations of the
21526606
EC
2022 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2023 Usually, `ID' should name a type, but if we got here it means that it
2097b5f2
GB
2024 does not. We try to emit the best possible error message depending on
2025 how exactly the id-expression looks like.
2026*/
2027
2028static bool
2029cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2030{
2031 tree id;
2032
2033 cp_parser_parse_tentatively (parser);
21526606 2034 id = cp_parser_id_expression (parser,
2097b5f2
GB
2035 /*template_keyword_p=*/false,
2036 /*check_dependency_p=*/true,
2037 /*template_p=*/NULL,
2038 /*declarator_p=*/true);
2039 /* After the id-expression, there should be a plain identifier,
2040 otherwise this is not a simple variable declaration. Also, if
2041 the scope is dependent, we cannot do much. */
2042 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 2043 || (parser->scope && TYPE_P (parser->scope)
2097b5f2
GB
2044 && dependent_type_p (parser->scope)))
2045 {
2046 cp_parser_abort_tentative_parse (parser);
2047 return false;
2048 }
3590f0a6
MM
2049 if (!cp_parser_parse_definitely (parser)
2050 || TREE_CODE (id) != IDENTIFIER_NODE)
2097b5f2
GB
2051 return false;
2052
2097b5f2
GB
2053 /* Emit a diagnostic for the invalid type. */
2054 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2055 /* Skip to the end of the declaration; there's no point in
2056 trying to process it. */
2057 cp_parser_skip_to_end_of_block_or_statement (parser);
2058 return true;
8fbc5ae7
MM
2059}
2060
21526606 2061/* Consume tokens up to, and including, the next non-nested closing `)'.
7efa3e22
NS
2062 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2063 are doing error recovery. Returns -1 if OR_COMMA is true and we
2064 found an unnested comma. */
a723baf1 2065
7efa3e22
NS
2066static int
2067cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
21526606 2068 bool recovering,
a668c6ad
MM
2069 bool or_comma,
2070 bool consume_paren)
a723baf1 2071{
7efa3e22
NS
2072 unsigned paren_depth = 0;
2073 unsigned brace_depth = 0;
0173bb6f 2074 int result;
a723baf1 2075
0b16f8f4
VR
2076 if (recovering && !or_comma
2077 && cp_parser_uncommitted_to_tentative_parse_p (parser))
7efa3e22 2078 return 0;
21526606 2079
a723baf1
MM
2080 while (true)
2081 {
2082 cp_token *token;
21526606 2083
a723baf1
MM
2084 /* If we've run out of tokens, then there is no closing `)'. */
2085 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0173bb6f
AO
2086 {
2087 result = 0;
2088 break;
2089 }
a723baf1 2090
a668c6ad 2091 token = cp_lexer_peek_token (parser->lexer);
21526606 2092
f4f206f4 2093 /* This matches the processing in skip_to_end_of_statement. */
a668c6ad 2094 if (token->type == CPP_SEMICOLON && !brace_depth)
0173bb6f
AO
2095 {
2096 result = 0;
2097 break;
2098 }
a668c6ad
MM
2099 if (token->type == CPP_OPEN_BRACE)
2100 ++brace_depth;
2101 if (token->type == CPP_CLOSE_BRACE)
7efa3e22 2102 {
a668c6ad 2103 if (!brace_depth--)
0173bb6f
AO
2104 {
2105 result = 0;
2106 break;
2107 }
7efa3e22 2108 }
a668c6ad
MM
2109 if (recovering && or_comma && token->type == CPP_COMMA
2110 && !brace_depth && !paren_depth)
0173bb6f
AO
2111 {
2112 result = -1;
2113 break;
2114 }
21526606 2115
7efa3e22
NS
2116 if (!brace_depth)
2117 {
2118 /* If it is an `(', we have entered another level of nesting. */
2119 if (token->type == CPP_OPEN_PAREN)
2120 ++paren_depth;
2121 /* If it is a `)', then we might be done. */
2122 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
a668c6ad
MM
2123 {
2124 if (consume_paren)
2125 cp_lexer_consume_token (parser->lexer);
0173bb6f
AO
2126 {
2127 result = 1;
2128 break;
2129 }
a668c6ad 2130 }
7efa3e22 2131 }
21526606 2132
a668c6ad
MM
2133 /* Consume the token. */
2134 cp_lexer_consume_token (parser->lexer);
a723baf1 2135 }
0173bb6f 2136
0173bb6f 2137 return result;
a723baf1
MM
2138}
2139
2140/* Consume tokens until we reach the end of the current statement.
2141 Normally, that will be just before consuming a `;'. However, if a
2142 non-nested `}' comes first, then we stop before consuming that. */
2143
2144static void
94edc4ab 2145cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2146{
2147 unsigned nesting_depth = 0;
2148
2149 while (true)
2150 {
2151 cp_token *token;
2152
2153 /* Peek at the next token. */
2154 token = cp_lexer_peek_token (parser->lexer);
2155 /* If we've run out of tokens, stop. */
2156 if (token->type == CPP_EOF)
2157 break;
2158 /* If the next token is a `;', we have reached the end of the
2159 statement. */
2160 if (token->type == CPP_SEMICOLON && !nesting_depth)
2161 break;
2162 /* If the next token is a non-nested `}', then we have reached
2163 the end of the current block. */
2164 if (token->type == CPP_CLOSE_BRACE)
2165 {
2166 /* If this is a non-nested `}', stop before consuming it.
2167 That way, when confronted with something like:
2168
21526606 2169 { 3 + }
a723baf1
MM
2170
2171 we stop before consuming the closing `}', even though we
2172 have not yet reached a `;'. */
2173 if (nesting_depth == 0)
2174 break;
2175 /* If it is the closing `}' for a block that we have
2176 scanned, stop -- but only after consuming the token.
2177 That way given:
2178
2179 void f g () { ... }
2180 typedef int I;
2181
2182 we will stop after the body of the erroneously declared
2183 function, but before consuming the following `typedef'
2184 declaration. */
2185 if (--nesting_depth == 0)
2186 {
2187 cp_lexer_consume_token (parser->lexer);
2188 break;
2189 }
2190 }
2191 /* If it the next token is a `{', then we are entering a new
2192 block. Consume the entire block. */
2193 else if (token->type == CPP_OPEN_BRACE)
2194 ++nesting_depth;
2195 /* Consume the token. */
2196 cp_lexer_consume_token (parser->lexer);
2197 }
2198}
2199
e0860732
MM
2200/* This function is called at the end of a statement or declaration.
2201 If the next token is a semicolon, it is consumed; otherwise, error
2202 recovery is attempted. */
2203
2204static void
2205cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2206{
2207 /* Look for the trailing `;'. */
2208 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2209 {
2210 /* If there is additional (erroneous) input, skip to the end of
2211 the statement. */
2212 cp_parser_skip_to_end_of_statement (parser);
2213 /* If the next token is now a `;', consume it. */
2214 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2215 cp_lexer_consume_token (parser->lexer);
2216 }
2217}
2218
a723baf1
MM
2219/* Skip tokens until we have consumed an entire block, or until we
2220 have consumed a non-nested `;'. */
2221
2222static void
94edc4ab 2223cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2224{
2225 unsigned nesting_depth = 0;
2226
2227 while (true)
2228 {
2229 cp_token *token;
2230
2231 /* Peek at the next token. */
2232 token = cp_lexer_peek_token (parser->lexer);
2233 /* If we've run out of tokens, stop. */
2234 if (token->type == CPP_EOF)
2235 break;
2236 /* If the next token is a `;', we have reached the end of the
2237 statement. */
2238 if (token->type == CPP_SEMICOLON && !nesting_depth)
2239 {
2240 /* Consume the `;'. */
2241 cp_lexer_consume_token (parser->lexer);
2242 break;
2243 }
2244 /* Consume the token. */
2245 token = cp_lexer_consume_token (parser->lexer);
2246 /* If the next token is a non-nested `}', then we have reached
2247 the end of the current block. */
21526606 2248 if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
2249 && (nesting_depth == 0 || --nesting_depth == 0))
2250 break;
2251 /* If it the next token is a `{', then we are entering a new
2252 block. Consume the entire block. */
2253 if (token->type == CPP_OPEN_BRACE)
2254 ++nesting_depth;
2255 }
2256}
2257
2258/* Skip tokens until a non-nested closing curly brace is the next
2259 token. */
2260
2261static void
2262cp_parser_skip_to_closing_brace (cp_parser *parser)
2263{
2264 unsigned nesting_depth = 0;
2265
2266 while (true)
2267 {
2268 cp_token *token;
2269
2270 /* Peek at the next token. */
2271 token = cp_lexer_peek_token (parser->lexer);
2272 /* If we've run out of tokens, stop. */
2273 if (token->type == CPP_EOF)
2274 break;
2275 /* If the next token is a non-nested `}', then we have reached
2276 the end of the current block. */
2277 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2278 break;
2279 /* If it the next token is a `{', then we are entering a new
2280 block. Consume the entire block. */
2281 else if (token->type == CPP_OPEN_BRACE)
2282 ++nesting_depth;
2283 /* Consume the token. */
2284 cp_lexer_consume_token (parser->lexer);
2285 }
2286}
2287
2097b5f2
GB
2288/* This is a simple wrapper around make_typename_type. When the id is
2289 an unresolved identifier node, we can provide a superior diagnostic
2290 using cp_parser_diagnose_invalid_type_name. */
2291
2292static tree
2293cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
6c0cc713
GB
2294{
2295 tree result;
2296 if (TREE_CODE (id) == IDENTIFIER_NODE)
2297 {
fc6a28d7
MM
2298 result = make_typename_type (scope, id, typename_type,
2299 /*complain=*/0);
6c0cc713
GB
2300 if (result == error_mark_node)
2301 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2302 return result;
2303 }
fc6a28d7 2304 return make_typename_type (scope, id, typename_type, tf_error);
2097b5f2
GB
2305}
2306
2307
a723baf1
MM
2308/* Create a new C++ parser. */
2309
2310static cp_parser *
94edc4ab 2311cp_parser_new (void)
a723baf1
MM
2312{
2313 cp_parser *parser;
17211ab5 2314 cp_lexer *lexer;
b8b94c5b 2315 unsigned i;
17211ab5
GK
2316
2317 /* cp_lexer_new_main is called before calling ggc_alloc because
2318 cp_lexer_new_main might load a PCH file. */
2319 lexer = cp_lexer_new_main ();
a723baf1 2320
b8b94c5b
PB
2321 /* Initialize the binops_by_token so that we can get the tree
2322 directly from the token. */
2323 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2324 binops_by_token[binops[i].token_type] = binops[i];
2325
99dd239f 2326 parser = GGC_CNEW (cp_parser);
17211ab5 2327 parser->lexer = lexer;
a723baf1
MM
2328 parser->context = cp_parser_context_new (NULL);
2329
2330 /* For now, we always accept GNU extensions. */
2331 parser->allow_gnu_extensions_p = 1;
2332
2333 /* The `>' token is a greater-than operator, not the end of a
2334 template-id. */
2335 parser->greater_than_is_operator_p = true;
2336
2337 parser->default_arg_ok_p = true;
21526606 2338
a723baf1 2339 /* We are not parsing a constant-expression. */
67c03833
JM
2340 parser->integral_constant_expression_p = false;
2341 parser->allow_non_integral_constant_expression_p = false;
2342 parser->non_integral_constant_expression_p = false;
a723baf1
MM
2343
2344 /* Local variable names are not forbidden. */
2345 parser->local_variables_forbidden_p = false;
2346
34cd5ae7 2347 /* We are not processing an `extern "C"' declaration. */
a723baf1
MM
2348 parser->in_unbraced_linkage_specification_p = false;
2349
2350 /* We are not processing a declarator. */
2351 parser->in_declarator_p = false;
2352
4bb8ca28
MM
2353 /* We are not processing a template-argument-list. */
2354 parser->in_template_argument_list_p = false;
2355
0e59b3fb
MM
2356 /* We are not in an iteration statement. */
2357 parser->in_iteration_statement_p = false;
2358
2359 /* We are not in a switch statement. */
2360 parser->in_switch_statement_p = false;
2361
4f8163b1
MM
2362 /* We are not parsing a type-id inside an expression. */
2363 parser->in_type_id_in_expr_p = false;
2364
03fd3f84 2365 /* Declarations aren't implicitly extern "C". */
7d381002
MA
2366 parser->implicit_extern_c = false;
2367
c162c75e
MA
2368 /* String literals should be translated to the execution character set. */
2369 parser->translate_strings_p = true;
2370
a723baf1
MM
2371 /* The unparsed function queue is empty. */
2372 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2373
2374 /* There are no classes being defined. */
2375 parser->num_classes_being_defined = 0;
2376
2377 /* No template parameters apply. */
2378 parser->num_template_parameter_lists = 0;
2379
2380 return parser;
2381}
2382
2cfe82fe
ZW
2383/* Create a cp_lexer structure which will emit the tokens in CACHE
2384 and push it onto the parser's lexer stack. This is used for delayed
2385 parsing of in-class method bodies and default arguments, and should
2386 not be confused with tentative parsing. */
2387static void
2388cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2389{
2390 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2391 lexer->next = parser->lexer;
2392 parser->lexer = lexer;
2393
2394 /* Move the current source position to that of the first token in the
2395 new lexer. */
2396 cp_lexer_set_source_position_from_token (lexer->next_token);
2397}
2398
2399/* Pop the top lexer off the parser stack. This is never used for the
2400 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2401static void
2402cp_parser_pop_lexer (cp_parser *parser)
2403{
2404 cp_lexer *lexer = parser->lexer;
2405 parser->lexer = lexer->next;
2406 cp_lexer_destroy (lexer);
2407
2408 /* Put the current source position back where it was before this
2409 lexer was pushed. */
2410 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2411}
2412
a723baf1
MM
2413/* Lexical conventions [gram.lex] */
2414
2415/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2416 identifier. */
2417
21526606 2418static tree
94edc4ab 2419cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2420{
2421 cp_token *token;
2422
2423 /* Look for the identifier. */
2424 token = cp_parser_require (parser, CPP_NAME, "identifier");
2425 /* Return the value. */
2426 return token ? token->value : error_mark_node;
2427}
2428
c162c75e
MA
2429/* Parse a sequence of adjacent string constants. Returns a
2430 TREE_STRING representing the combined, nul-terminated string
2431 constant. If TRANSLATE is true, translate the string to the
2432 execution character set. If WIDE_OK is true, a wide string is
2433 invalid here.
2434
2435 C++98 [lex.string] says that if a narrow string literal token is
2436 adjacent to a wide string literal token, the behavior is undefined.
2437 However, C99 6.4.5p4 says that this results in a wide string literal.
2438 We follow C99 here, for consistency with the C front end.
2439
2440 This code is largely lifted from lex_string() in c-lex.c.
2441
2442 FUTURE: ObjC++ will need to handle @-strings here. */
2443static tree
2444cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2445{
2446 tree value;
2447 bool wide = false;
2448 size_t count;
2449 struct obstack str_ob;
2450 cpp_string str, istr, *strs;
2451 cp_token *tok;
2452
2453 tok = cp_lexer_peek_token (parser->lexer);
2454 if (!cp_parser_is_string_literal (tok))
2455 {
2456 cp_parser_error (parser, "expected string-literal");
2457 return error_mark_node;
2458 }
2459
9688c3b8 2460 /* Try to avoid the overhead of creating and destroying an obstack
c162c75e 2461 for the common case of just one string. */
2cfe82fe
ZW
2462 if (!cp_parser_is_string_literal
2463 (cp_lexer_peek_nth_token (parser->lexer, 2)))
c162c75e 2464 {
2cfe82fe
ZW
2465 cp_lexer_consume_token (parser->lexer);
2466
c162c75e
MA
2467 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2468 str.len = TREE_STRING_LENGTH (tok->value);
2469 count = 1;
2470 if (tok->type == CPP_WSTRING)
2471 wide = true;
c162c75e
MA
2472
2473 strs = &str;
2474 }
2475 else
2476 {
2477 gcc_obstack_init (&str_ob);
2478 count = 0;
2479
2480 do
2481 {
2cfe82fe 2482 cp_lexer_consume_token (parser->lexer);
c162c75e
MA
2483 count++;
2484 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2485 str.len = TREE_STRING_LENGTH (tok->value);
2486 if (tok->type == CPP_WSTRING)
2487 wide = true;
2488
2489 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2490
2cfe82fe 2491 tok = cp_lexer_peek_token (parser->lexer);
c162c75e
MA
2492 }
2493 while (cp_parser_is_string_literal (tok));
2494
2495 strs = (cpp_string *) obstack_finish (&str_ob);
2496 }
2497
2498 if (wide && !wide_ok)
2499 {
2500 cp_parser_error (parser, "a wide string is invalid in this context");
2501 wide = false;
2502 }
2503
2504 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2505 (parse_in, strs, count, &istr, wide))
2506 {
2507 value = build_string (istr.len, (char *)istr.text);
2508 free ((void *)istr.text);
2509
2510 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2511 value = fix_string_type (value);
2512 }
2513 else
2514 /* cpp_interpret_string has issued an error. */
2515 value = error_mark_node;
2516
2517 if (count > 1)
2518 obstack_free (&str_ob, 0);
2519
2520 return value;
2521}
2522
2523
a723baf1
MM
2524/* Basic concepts [gram.basic] */
2525
2526/* Parse a translation-unit.
2527
2528 translation-unit:
21526606 2529 declaration-seq [opt]
a723baf1
MM
2530
2531 Returns TRUE if all went well. */
2532
2533static bool
94edc4ab 2534cp_parser_translation_unit (cp_parser* parser)
a723baf1 2535{
058b15c1
MM
2536 /* The address of the first non-permanent object on the declarator
2537 obstack. */
2538 static void *declarator_obstack_base;
2539
2540 bool success;
98ca843c 2541
058b15c1
MM
2542 /* Create the declarator obstack, if necessary. */
2543 if (!cp_error_declarator)
2544 {
2545 gcc_obstack_init (&declarator_obstack);
2546 /* Create the error declarator. */
2547 cp_error_declarator = make_declarator (cdk_error);
2548 /* Create the empty parameter list. */
62d1db17 2549 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
058b15c1
MM
2550 /* Remember where the base of the declarator obstack lies. */
2551 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2552 }
2553
a723baf1
MM
2554 while (true)
2555 {
2556 cp_parser_declaration_seq_opt (parser);
2557
2558 /* If there are no tokens left then all went well. */
2559 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
058b15c1 2560 {
03fd3f84 2561 /* Get rid of the token array; we don't need it any more. */
c162c75e
MA
2562 cp_lexer_destroy (parser->lexer);
2563 parser->lexer = NULL;
2564
7d381002
MA
2565 /* This file might have been a context that's implicitly extern
2566 "C". If so, pop the lang context. (Only relevant for PCH.) */
2567 if (parser->implicit_extern_c)
2568 {
2569 pop_lang_context ();
2570 parser->implicit_extern_c = false;
2571 }
2572
058b15c1
MM
2573 /* Finish up. */
2574 finish_translation_unit ();
21526606 2575
058b15c1
MM
2576 success = true;
2577 break;
2578 }
2579 else
2580 {
2581 cp_parser_error (parser, "expected declaration");
2582 success = false;
2583 break;
2584 }
a723baf1
MM
2585 }
2586
058b15c1 2587 /* Make sure the declarator obstack was fully cleaned up. */
50bc768d
NS
2588 gcc_assert (obstack_next_free (&declarator_obstack)
2589 == declarator_obstack_base);
a723baf1
MM
2590
2591 /* All went well. */
058b15c1 2592 return success;
a723baf1
MM
2593}
2594
2595/* Expressions [gram.expr] */
2596
2597/* Parse a primary-expression.
2598
2599 primary-expression:
2600 literal
2601 this
2602 ( expression )
2603 id-expression
2604
2605 GNU Extensions:
2606
2607 primary-expression:
2608 ( compound-statement )
2609 __builtin_va_arg ( assignment-expression , type-id )
2610
2611 literal:
2612 __null
2613
21526606 2614 Returns a representation of the expression.
a723baf1 2615
21526606 2616 *IDK indicates what kind of id-expression (if any) was present.
a723baf1
MM
2617
2618 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2619 used as the operand of a pointer-to-member. In that case,
2620 *QUALIFYING_CLASS gives the class that is used as the qualifying
2621 class in the pointer-to-member. */
2622
2623static tree
21526606 2624cp_parser_primary_expression (cp_parser *parser,
b3445994 2625 cp_id_kind *idk,
a723baf1
MM
2626 tree *qualifying_class)
2627{
2628 cp_token *token;
2629
2630 /* Assume the primary expression is not an id-expression. */
b3445994 2631 *idk = CP_ID_KIND_NONE;
a723baf1
MM
2632 /* And that it cannot be used as pointer-to-member. */
2633 *qualifying_class = NULL_TREE;
2634
2635 /* Peek at the next token. */
2636 token = cp_lexer_peek_token (parser->lexer);
2637 switch (token->type)
2638 {
2639 /* literal:
2640 integer-literal
2641 character-literal
2642 floating-literal
2643 string-literal
2644 boolean-literal */
2645 case CPP_CHAR:
2646 case CPP_WCHAR:
a723baf1
MM
2647 case CPP_NUMBER:
2648 token = cp_lexer_consume_token (parser->lexer);
2649 return token->value;
2650
0173bb6f
AO
2651 case CPP_STRING:
2652 case CPP_WSTRING:
c162c75e
MA
2653 /* ??? Should wide strings be allowed when parser->translate_strings_p
2654 is false (i.e. in attributes)? If not, we can kill the third
2655 argument to cp_parser_string_literal. */
2656 return cp_parser_string_literal (parser,
2657 parser->translate_strings_p,
2658 true);
0173bb6f 2659
a723baf1
MM
2660 case CPP_OPEN_PAREN:
2661 {
2662 tree expr;
2663 bool saved_greater_than_is_operator_p;
2664
2665 /* Consume the `('. */
2666 cp_lexer_consume_token (parser->lexer);
2667 /* Within a parenthesized expression, a `>' token is always
2668 the greater-than operator. */
21526606 2669 saved_greater_than_is_operator_p
a723baf1
MM
2670 = parser->greater_than_is_operator_p;
2671 parser->greater_than_is_operator_p = true;
2672 /* If we see `( { ' then we are looking at the beginning of
2673 a GNU statement-expression. */
2674 if (cp_parser_allow_gnu_extensions_p (parser)
2675 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2676 {
2677 /* Statement-expressions are not allowed by the standard. */
2678 if (pedantic)
21526606
EC
2679 pedwarn ("ISO C++ forbids braced-groups within expressions");
2680
a723baf1
MM
2681 /* And they're not allowed outside of a function-body; you
2682 cannot, for example, write:
21526606 2683
a723baf1 2684 int i = ({ int j = 3; j + 1; });
21526606 2685
a723baf1
MM
2686 at class or namespace scope. */
2687 if (!at_function_scope_p ())
2688 error ("statement-expressions are allowed only inside functions");
2689 /* Start the statement-expression. */
2690 expr = begin_stmt_expr ();
2691 /* Parse the compound-statement. */
325c3691 2692 cp_parser_compound_statement (parser, expr, false);
a723baf1 2693 /* Finish up. */
303b7406 2694 expr = finish_stmt_expr (expr, false);
a723baf1
MM
2695 }
2696 else
2697 {
2698 /* Parse the parenthesized expression. */
2699 expr = cp_parser_expression (parser);
2700 /* Let the front end know that this expression was
2701 enclosed in parentheses. This matters in case, for
2702 example, the expression is of the form `A::B', since
2703 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2704 not. */
2705 finish_parenthesized_expr (expr);
2706 }
2707 /* The `>' token might be the end of a template-id or
2708 template-parameter-list now. */
21526606 2709 parser->greater_than_is_operator_p
a723baf1
MM
2710 = saved_greater_than_is_operator_p;
2711 /* Consume the `)'. */
2712 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2713 cp_parser_skip_to_end_of_statement (parser);
2714
2715 return expr;
2716 }
2717
2718 case CPP_KEYWORD:
2719 switch (token->keyword)
2720 {
2721 /* These two are the boolean literals. */
2722 case RID_TRUE:
2723 cp_lexer_consume_token (parser->lexer);
2724 return boolean_true_node;
2725 case RID_FALSE:
2726 cp_lexer_consume_token (parser->lexer);
2727 return boolean_false_node;
21526606 2728
a723baf1
MM
2729 /* The `__null' literal. */
2730 case RID_NULL:
2731 cp_lexer_consume_token (parser->lexer);
2732 return null_node;
2733
2734 /* Recognize the `this' keyword. */
2735 case RID_THIS:
2736 cp_lexer_consume_token (parser->lexer);
2737 if (parser->local_variables_forbidden_p)
2738 {
2a13a625 2739 error ("%<this%> may not be used in this context");
a723baf1
MM
2740 return error_mark_node;
2741 }
14d22dd6 2742 /* Pointers cannot appear in constant-expressions. */
625cbf93
MM
2743 if (cp_parser_non_integral_constant_expression (parser,
2744 "`this'"))
2745 return error_mark_node;
a723baf1
MM
2746 return finish_this_expr ();
2747
2748 /* The `operator' keyword can be the beginning of an
2749 id-expression. */
2750 case RID_OPERATOR:
2751 goto id_expression;
2752
2753 case RID_FUNCTION_NAME:
2754 case RID_PRETTY_FUNCTION_NAME:
2755 case RID_C99_FUNCTION_NAME:
2756 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2757 __func__ are the names of variables -- but they are
2758 treated specially. Therefore, they are handled here,
2759 rather than relying on the generic id-expression logic
21526606 2760 below. Grammatically, these names are id-expressions.
a723baf1
MM
2761
2762 Consume the token. */
2763 token = cp_lexer_consume_token (parser->lexer);
2764 /* Look up the name. */
2765 return finish_fname (token->value);
2766
2767 case RID_VA_ARG:
2768 {
2769 tree expression;
2770 tree type;
2771
2772 /* The `__builtin_va_arg' construct is used to handle
2773 `va_arg'. Consume the `__builtin_va_arg' token. */
2774 cp_lexer_consume_token (parser->lexer);
2775 /* Look for the opening `('. */
2776 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2777 /* Now, parse the assignment-expression. */
2778 expression = cp_parser_assignment_expression (parser);
2779 /* Look for the `,'. */
2780 cp_parser_require (parser, CPP_COMMA, "`,'");
2781 /* Parse the type-id. */
2782 type = cp_parser_type_id (parser);
2783 /* Look for the closing `)'. */
2784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2785 /* Using `va_arg' in a constant-expression is not
2786 allowed. */
625cbf93
MM
2787 if (cp_parser_non_integral_constant_expression (parser,
2788 "`va_arg'"))
2789 return error_mark_node;
a723baf1
MM
2790 return build_x_va_arg (expression, type);
2791 }
2792
263ee052 2793 case RID_OFFSETOF:
7a3ea201 2794 return cp_parser_builtin_offsetof (parser);
263ee052 2795
a723baf1
MM
2796 default:
2797 cp_parser_error (parser, "expected primary-expression");
2798 return error_mark_node;
2799 }
a723baf1
MM
2800
2801 /* An id-expression can start with either an identifier, a
2802 `::' as the beginning of a qualified-id, or the "operator"
2803 keyword. */
2804 case CPP_NAME:
2805 case CPP_SCOPE:
2806 case CPP_TEMPLATE_ID:
2807 case CPP_NESTED_NAME_SPECIFIER:
2808 {
2809 tree id_expression;
2810 tree decl;
b3445994 2811 const char *error_msg;
a723baf1
MM
2812
2813 id_expression:
2814 /* Parse the id-expression. */
21526606
EC
2815 id_expression
2816 = cp_parser_id_expression (parser,
a723baf1
MM
2817 /*template_keyword_p=*/false,
2818 /*check_dependency_p=*/true,
f3c2dfc6
MM
2819 /*template_p=*/NULL,
2820 /*declarator_p=*/false);
a723baf1
MM
2821 if (id_expression == error_mark_node)
2822 return error_mark_node;
2823 /* If we have a template-id, then no further lookup is
2824 required. If the template-id was for a template-class, we
2825 will sometimes have a TYPE_DECL at this point. */
2826 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2827 || TREE_CODE (id_expression) == TYPE_DECL)
2828 decl = id_expression;
2829 /* Look up the name. */
21526606 2830 else
a723baf1 2831 {
8f78f01f
MM
2832 bool ambiguous_p;
2833
2834 decl = cp_parser_lookup_name (parser, id_expression,
fc6a28d7 2835 none_type,
8f78f01f
MM
2836 /*is_template=*/false,
2837 /*is_namespace=*/false,
2838 /*check_dependency=*/true,
2839 &ambiguous_p);
2840 /* If the lookup was ambiguous, an error will already have
2841 been issued. */
2842 if (ambiguous_p)
2843 return error_mark_node;
a723baf1
MM
2844 /* If name lookup gives us a SCOPE_REF, then the
2845 qualifying scope was dependent. Just propagate the
2846 name. */
2847 if (TREE_CODE (decl) == SCOPE_REF)
2848 {
2849 if (TYPE_P (TREE_OPERAND (decl, 0)))
2850 *qualifying_class = TREE_OPERAND (decl, 0);
2851 return decl;
2852 }
2853 /* Check to see if DECL is a local variable in a context
2854 where that is forbidden. */
2855 if (parser->local_variables_forbidden_p
2856 && local_variable_p (decl))
2857 {
2858 /* It might be that we only found DECL because we are
2859 trying to be generous with pre-ISO scoping rules.
2860 For example, consider:
2861
2862 int i;
2863 void g() {
2864 for (int i = 0; i < 10; ++i) {}
2865 extern void f(int j = i);
2866 }
2867
21526606 2868 Here, name look up will originally find the out
a723baf1
MM
2869 of scope `i'. We need to issue a warning message,
2870 but then use the global `i'. */
2871 decl = check_for_out_of_scope_variable (decl);
2872 if (local_variable_p (decl))
2873 {
2a13a625 2874 error ("local variable %qD may not appear in this context",
a723baf1
MM
2875 decl);
2876 return error_mark_node;
2877 }
2878 }
c006d942 2879 }
21526606
EC
2880
2881 decl = finish_id_expression (id_expression, decl, parser->scope,
b3445994 2882 idk, qualifying_class,
67c03833
JM
2883 parser->integral_constant_expression_p,
2884 parser->allow_non_integral_constant_expression_p,
2885 &parser->non_integral_constant_expression_p,
b3445994
MM
2886 &error_msg);
2887 if (error_msg)
2888 cp_parser_error (parser, error_msg);
a723baf1
MM
2889 return decl;
2890 }
2891
2892 /* Anything else is an error. */
2893 default:
2894 cp_parser_error (parser, "expected primary-expression");
2895 return error_mark_node;
2896 }
2897}
2898
2899/* Parse an id-expression.
2900
2901 id-expression:
2902 unqualified-id
2903 qualified-id
2904
2905 qualified-id:
2906 :: [opt] nested-name-specifier template [opt] unqualified-id
2907 :: identifier
2908 :: operator-function-id
2909 :: template-id
2910
2911 Return a representation of the unqualified portion of the
2912 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2913 a `::' or nested-name-specifier.
2914
2915 Often, if the id-expression was a qualified-id, the caller will
2916 want to make a SCOPE_REF to represent the qualified-id. This
2917 function does not do this in order to avoid wastefully creating
2918 SCOPE_REFs when they are not required.
2919
a723baf1
MM
2920 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2921 `template' keyword.
2922
2923 If CHECK_DEPENDENCY_P is false, then names are looked up inside
21526606 2924 uninstantiated templates.
a723baf1 2925
15d2cb19 2926 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1 2927 `template' keyword is used to explicitly indicate that the entity
21526606 2928 named is a template.
f3c2dfc6
MM
2929
2930 If DECLARATOR_P is true, the id-expression is appearing as part of
cd0be382 2931 a declarator, rather than as part of an expression. */
a723baf1
MM
2932
2933static tree
2934cp_parser_id_expression (cp_parser *parser,
2935 bool template_keyword_p,
2936 bool check_dependency_p,
f3c2dfc6
MM
2937 bool *template_p,
2938 bool declarator_p)
a723baf1
MM
2939{
2940 bool global_scope_p;
2941 bool nested_name_specifier_p;
2942
2943 /* Assume the `template' keyword was not used. */
2944 if (template_p)
2945 *template_p = false;
2946
2947 /* Look for the optional `::' operator. */
21526606
EC
2948 global_scope_p
2949 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
a723baf1
MM
2950 != NULL_TREE);
2951 /* Look for the optional nested-name-specifier. */
21526606 2952 nested_name_specifier_p
a723baf1
MM
2953 = (cp_parser_nested_name_specifier_opt (parser,
2954 /*typename_keyword_p=*/false,
2955 check_dependency_p,
a668c6ad 2956 /*type_p=*/false,
a52eb3bc 2957 declarator_p)
a723baf1
MM
2958 != NULL_TREE);
2959 /* If there is a nested-name-specifier, then we are looking at
2960 the first qualified-id production. */
2961 if (nested_name_specifier_p)
2962 {
2963 tree saved_scope;
2964 tree saved_object_scope;
2965 tree saved_qualifying_scope;
2966 tree unqualified_id;
2967 bool is_template;
2968
2969 /* See if the next token is the `template' keyword. */
2970 if (!template_p)
2971 template_p = &is_template;
2972 *template_p = cp_parser_optional_template_keyword (parser);
2973 /* Name lookup we do during the processing of the
2974 unqualified-id might obliterate SCOPE. */
2975 saved_scope = parser->scope;
2976 saved_object_scope = parser->object_scope;
2977 saved_qualifying_scope = parser->qualifying_scope;
2978 /* Process the final unqualified-id. */
2979 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
f3c2dfc6
MM
2980 check_dependency_p,
2981 declarator_p);
a723baf1
MM
2982 /* Restore the SAVED_SCOPE for our caller. */
2983 parser->scope = saved_scope;
2984 parser->object_scope = saved_object_scope;
2985 parser->qualifying_scope = saved_qualifying_scope;
2986
2987 return unqualified_id;
2988 }
2989 /* Otherwise, if we are in global scope, then we are looking at one
2990 of the other qualified-id productions. */
2991 else if (global_scope_p)
2992 {
2993 cp_token *token;
2994 tree id;
2995
e5976695
MM
2996 /* Peek at the next token. */
2997 token = cp_lexer_peek_token (parser->lexer);
2998
2999 /* If it's an identifier, and the next token is not a "<", then
3000 we can avoid the template-id case. This is an optimization
3001 for this common case. */
21526606
EC
3002 if (token->type == CPP_NAME
3003 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 3004 (parser, 2))
e5976695
MM
3005 return cp_parser_identifier (parser);
3006
a723baf1
MM
3007 cp_parser_parse_tentatively (parser);
3008 /* Try a template-id. */
21526606 3009 id = cp_parser_template_id (parser,
a723baf1 3010 /*template_keyword_p=*/false,
a668c6ad
MM
3011 /*check_dependency_p=*/true,
3012 declarator_p);
a723baf1
MM
3013 /* If that worked, we're done. */
3014 if (cp_parser_parse_definitely (parser))
3015 return id;
3016
e5976695
MM
3017 /* Peek at the next token. (Changes in the token buffer may
3018 have invalidated the pointer obtained above.) */
a723baf1
MM
3019 token = cp_lexer_peek_token (parser->lexer);
3020
3021 switch (token->type)
3022 {
3023 case CPP_NAME:
3024 return cp_parser_identifier (parser);
3025
3026 case CPP_KEYWORD:
3027 if (token->keyword == RID_OPERATOR)
3028 return cp_parser_operator_function_id (parser);
3029 /* Fall through. */
21526606 3030
a723baf1
MM
3031 default:
3032 cp_parser_error (parser, "expected id-expression");
3033 return error_mark_node;
3034 }
3035 }
3036 else
3037 return cp_parser_unqualified_id (parser, template_keyword_p,
f3c2dfc6
MM
3038 /*check_dependency_p=*/true,
3039 declarator_p);
a723baf1
MM
3040}
3041
3042/* Parse an unqualified-id.
3043
3044 unqualified-id:
3045 identifier
3046 operator-function-id
3047 conversion-function-id
3048 ~ class-name
3049 template-id
3050
3051 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3052 keyword, in a construct like `A::template ...'.
3053
3054 Returns a representation of unqualified-id. For the `identifier'
3055 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3056 production a BIT_NOT_EXPR is returned; the operand of the
3057 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3058 other productions, see the documentation accompanying the
3059 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
f3c2dfc6
MM
3060 names are looked up in uninstantiated templates. If DECLARATOR_P
3061 is true, the unqualified-id is appearing as part of a declarator,
3062 rather than as part of an expression. */
a723baf1
MM
3063
3064static tree
21526606 3065cp_parser_unqualified_id (cp_parser* parser,
94edc4ab 3066 bool template_keyword_p,
f3c2dfc6
MM
3067 bool check_dependency_p,
3068 bool declarator_p)
a723baf1
MM
3069{
3070 cp_token *token;
3071
3072 /* Peek at the next token. */
3073 token = cp_lexer_peek_token (parser->lexer);
21526606 3074
a723baf1
MM
3075 switch (token->type)
3076 {
3077 case CPP_NAME:
3078 {
3079 tree id;
3080
3081 /* We don't know yet whether or not this will be a
3082 template-id. */
3083 cp_parser_parse_tentatively (parser);
3084 /* Try a template-id. */
3085 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3086 check_dependency_p,
3087 declarator_p);
a723baf1
MM
3088 /* If it worked, we're done. */
3089 if (cp_parser_parse_definitely (parser))
3090 return id;
3091 /* Otherwise, it's an ordinary identifier. */
3092 return cp_parser_identifier (parser);
3093 }
3094
3095 case CPP_TEMPLATE_ID:
3096 return cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3097 check_dependency_p,
3098 declarator_p);
a723baf1
MM
3099
3100 case CPP_COMPL:
3101 {
3102 tree type_decl;
3103 tree qualifying_scope;
3104 tree object_scope;
3105 tree scope;
3106
3107 /* Consume the `~' token. */
3108 cp_lexer_consume_token (parser->lexer);
3109 /* Parse the class-name. The standard, as written, seems to
3110 say that:
3111
3112 template <typename T> struct S { ~S (); };
3113 template <typename T> S<T>::~S() {}
3114
3115 is invalid, since `~' must be followed by a class-name, but
3116 `S<T>' is dependent, and so not known to be a class.
3117 That's not right; we need to look in uninstantiated
3118 templates. A further complication arises from:
3119
3120 template <typename T> void f(T t) {
3121 t.T::~T();
21526606 3122 }
a723baf1
MM
3123
3124 Here, it is not possible to look up `T' in the scope of `T'
3125 itself. We must look in both the current scope, and the
21526606 3126 scope of the containing complete expression.
a723baf1
MM
3127
3128 Yet another issue is:
3129
3130 struct S {
3131 int S;
3132 ~S();
3133 };
3134
3135 S::~S() {}
3136
3137 The standard does not seem to say that the `S' in `~S'
3138 should refer to the type `S' and not the data member
3139 `S::S'. */
3140
3141 /* DR 244 says that we look up the name after the "~" in the
3142 same scope as we looked up the qualifying name. That idea
3143 isn't fully worked out; it's more complicated than that. */
3144 scope = parser->scope;
3145 object_scope = parser->object_scope;
3146 qualifying_scope = parser->qualifying_scope;
3147
3148 /* If the name is of the form "X::~X" it's OK. */
3149 if (scope && TYPE_P (scope)
3150 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3151 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3152 == CPP_OPEN_PAREN)
21526606 3153 && (cp_lexer_peek_token (parser->lexer)->value
a723baf1
MM
3154 == TYPE_IDENTIFIER (scope)))
3155 {
3156 cp_lexer_consume_token (parser->lexer);
3157 return build_nt (BIT_NOT_EXPR, scope);
3158 }
3159
3160 /* If there was an explicit qualification (S::~T), first look
3161 in the scope given by the qualification (i.e., S). */
3162 if (scope)
3163 {
3164 cp_parser_parse_tentatively (parser);
21526606 3165 type_decl = cp_parser_class_name (parser,
a723baf1
MM
3166 /*typename_keyword_p=*/false,
3167 /*template_keyword_p=*/false,
fc6a28d7 3168 none_type,
a723baf1 3169 /*check_dependency=*/false,
a668c6ad
MM
3170 /*class_head_p=*/false,
3171 declarator_p);
a723baf1
MM
3172 if (cp_parser_parse_definitely (parser))
3173 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3174 }
3175 /* In "N::S::~S", look in "N" as well. */
3176 if (scope && qualifying_scope)
3177 {
3178 cp_parser_parse_tentatively (parser);
3179 parser->scope = qualifying_scope;
3180 parser->object_scope = NULL_TREE;
3181 parser->qualifying_scope = NULL_TREE;
21526606
EC
3182 type_decl
3183 = cp_parser_class_name (parser,
a723baf1
MM
3184 /*typename_keyword_p=*/false,
3185 /*template_keyword_p=*/false,
fc6a28d7 3186 none_type,
a723baf1 3187 /*check_dependency=*/false,
a668c6ad
MM
3188 /*class_head_p=*/false,
3189 declarator_p);
a723baf1
MM
3190 if (cp_parser_parse_definitely (parser))
3191 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3192 }
3193 /* In "p->S::~T", look in the scope given by "*p" as well. */
3194 else if (object_scope)
3195 {
3196 cp_parser_parse_tentatively (parser);
3197 parser->scope = object_scope;
3198 parser->object_scope = NULL_TREE;
3199 parser->qualifying_scope = NULL_TREE;
21526606
EC
3200 type_decl
3201 = cp_parser_class_name (parser,
a723baf1
MM
3202 /*typename_keyword_p=*/false,
3203 /*template_keyword_p=*/false,
fc6a28d7 3204 none_type,
a723baf1 3205 /*check_dependency=*/false,
a668c6ad
MM
3206 /*class_head_p=*/false,
3207 declarator_p);
a723baf1
MM
3208 if (cp_parser_parse_definitely (parser))
3209 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3210 }
3211 /* Look in the surrounding context. */
3212 parser->scope = NULL_TREE;
3213 parser->object_scope = NULL_TREE;
3214 parser->qualifying_scope = NULL_TREE;
21526606
EC
3215 type_decl
3216 = cp_parser_class_name (parser,
a723baf1
MM
3217 /*typename_keyword_p=*/false,
3218 /*template_keyword_p=*/false,
fc6a28d7 3219 none_type,
a723baf1 3220 /*check_dependency=*/false,
a668c6ad
MM
3221 /*class_head_p=*/false,
3222 declarator_p);
a723baf1
MM
3223 /* If an error occurred, assume that the name of the
3224 destructor is the same as the name of the qualifying
3225 class. That allows us to keep parsing after running
3226 into ill-formed destructor names. */
3227 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3228 return build_nt (BIT_NOT_EXPR, scope);
3229 else if (type_decl == error_mark_node)
3230 return error_mark_node;
3231
f3c2dfc6
MM
3232 /* [class.dtor]
3233
3234 A typedef-name that names a class shall not be used as the
3235 identifier in the declarator for a destructor declaration. */
21526606 3236 if (declarator_p
f3c2dfc6
MM
3237 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3238 && !DECL_SELF_REFERENCE_P (type_decl))
2a13a625 3239 error ("typedef-name %qD used as destructor declarator",
f3c2dfc6
MM
3240 type_decl);
3241
a723baf1
MM
3242 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3243 }
3244
3245 case CPP_KEYWORD:
3246 if (token->keyword == RID_OPERATOR)
3247 {
3248 tree id;
3249
3250 /* This could be a template-id, so we try that first. */
3251 cp_parser_parse_tentatively (parser);
3252 /* Try a template-id. */
3253 id = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
3254 /*check_dependency_p=*/true,
3255 declarator_p);
a723baf1
MM
3256 /* If that worked, we're done. */
3257 if (cp_parser_parse_definitely (parser))
3258 return id;
3259 /* We still don't know whether we're looking at an
3260 operator-function-id or a conversion-function-id. */
3261 cp_parser_parse_tentatively (parser);
3262 /* Try an operator-function-id. */
3263 id = cp_parser_operator_function_id (parser);
3264 /* If that didn't work, try a conversion-function-id. */
3265 if (!cp_parser_parse_definitely (parser))
3266 id = cp_parser_conversion_function_id (parser);
3267
3268 return id;
3269 }
3270 /* Fall through. */
3271
3272 default:
3273 cp_parser_error (parser, "expected unqualified-id");
3274 return error_mark_node;
3275 }
3276}
3277
3278/* Parse an (optional) nested-name-specifier.
3279
3280 nested-name-specifier:
3281 class-or-namespace-name :: nested-name-specifier [opt]
3282 class-or-namespace-name :: template nested-name-specifier [opt]
3283
3284 PARSER->SCOPE should be set appropriately before this function is
3285 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3286 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3287 in name lookups.
3288
3289 Sets PARSER->SCOPE to the class (TYPE) or namespace
3290 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3291 it unchanged if there is no nested-name-specifier. Returns the new
21526606 3292 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
a668c6ad
MM
3293
3294 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3295 part of a declaration and/or decl-specifier. */
a723baf1
MM
3296
3297static tree
21526606
EC
3298cp_parser_nested_name_specifier_opt (cp_parser *parser,
3299 bool typename_keyword_p,
a723baf1 3300 bool check_dependency_p,
a668c6ad
MM
3301 bool type_p,
3302 bool is_declaration)
a723baf1
MM
3303{
3304 bool success = false;
3305 tree access_check = NULL_TREE;
0c5e4866
NS
3306 cp_token_position start = 0;
3307 cp_token *token;
a723baf1
MM
3308
3309 /* If the next token corresponds to a nested name specifier, there
2050a1bb 3310 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
21526606 3311 false, it may have been true before, in which case something
2050a1bb
MM
3312 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3313 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3314 CHECK_DEPENDENCY_P is false, we have to fall through into the
3315 main loop. */
3316 if (check_dependency_p
3317 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3318 {
3319 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3320 return parser->scope;
3321 }
3322
3323 /* Remember where the nested-name-specifier starts. */
0b16f8f4 3324 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 3325 start = cp_lexer_token_position (parser->lexer, false);
a723baf1 3326
8d241e0b 3327 push_deferring_access_checks (dk_deferred);
cf22909c 3328
a723baf1
MM
3329 while (true)
3330 {
3331 tree new_scope;
3332 tree old_scope;
3333 tree saved_qualifying_scope;
a723baf1
MM
3334 bool template_keyword_p;
3335
2050a1bb
MM
3336 /* Spot cases that cannot be the beginning of a
3337 nested-name-specifier. */
3338 token = cp_lexer_peek_token (parser->lexer);
3339
3340 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3341 the already parsed nested-name-specifier. */
3342 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3343 {
3344 /* Grab the nested-name-specifier and continue the loop. */
3345 cp_parser_pre_parsed_nested_name_specifier (parser);
3346 success = true;
3347 continue;
3348 }
3349
a723baf1
MM
3350 /* Spot cases that cannot be the beginning of a
3351 nested-name-specifier. On the second and subsequent times
3352 through the loop, we look for the `template' keyword. */
f7b5ecd9 3353 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3354 ;
3355 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3356 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3357 ;
3358 else
3359 {
3360 /* If the next token is not an identifier, then it is
3361 definitely not a class-or-namespace-name. */
f7b5ecd9 3362 if (token->type != CPP_NAME)
a723baf1
MM
3363 break;
3364 /* If the following token is neither a `<' (to begin a
3365 template-id), nor a `::', then we are not looking at a
3366 nested-name-specifier. */
3367 token = cp_lexer_peek_nth_token (parser->lexer, 2);
f4abade9
GB
3368 if (token->type != CPP_SCOPE
3369 && !cp_parser_nth_token_starts_template_argument_list_p
3370 (parser, 2))
a723baf1
MM
3371 break;
3372 }
3373
3374 /* The nested-name-specifier is optional, so we parse
3375 tentatively. */
3376 cp_parser_parse_tentatively (parser);
3377
3378 /* Look for the optional `template' keyword, if this isn't the
3379 first time through the loop. */
3380 if (success)
3381 template_keyword_p = cp_parser_optional_template_keyword (parser);
3382 else
3383 template_keyword_p = false;
3384
3385 /* Save the old scope since the name lookup we are about to do
3386 might destroy it. */
3387 old_scope = parser->scope;
3388 saved_qualifying_scope = parser->qualifying_scope;
a52eb3bc
MM
3389 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3390 look up names in "X<T>::I" in order to determine that "Y" is
3391 a template. So, if we have a typename at this point, we make
3392 an effort to look through it. */
67bcc252
MM
3393 if (is_declaration
3394 && !typename_keyword_p
3395 && parser->scope
a52eb3bc
MM
3396 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3397 parser->scope = resolve_typename_type (parser->scope,
3398 /*only_current_p=*/false);
a723baf1 3399 /* Parse the qualifying entity. */
21526606 3400 new_scope
a723baf1
MM
3401 = cp_parser_class_or_namespace_name (parser,
3402 typename_keyword_p,
3403 template_keyword_p,
3404 check_dependency_p,
a668c6ad
MM
3405 type_p,
3406 is_declaration);
a723baf1
MM
3407 /* Look for the `::' token. */
3408 cp_parser_require (parser, CPP_SCOPE, "`::'");
3409
3410 /* If we found what we wanted, we keep going; otherwise, we're
3411 done. */
3412 if (!cp_parser_parse_definitely (parser))
3413 {
3414 bool error_p = false;
3415
3416 /* Restore the OLD_SCOPE since it was valid before the
3417 failed attempt at finding the last
3418 class-or-namespace-name. */
3419 parser->scope = old_scope;
3420 parser->qualifying_scope = saved_qualifying_scope;
3421 /* If the next token is an identifier, and the one after
3422 that is a `::', then any valid interpretation would have
3423 found a class-or-namespace-name. */
3424 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21526606 3425 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1 3426 == CPP_SCOPE)
21526606 3427 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
3428 != CPP_COMPL))
3429 {
3430 token = cp_lexer_consume_token (parser->lexer);
21526606 3431 if (!error_p)
a723baf1
MM
3432 {
3433 tree decl;
3434
3435 decl = cp_parser_lookup_name_simple (parser, token->value);
3436 if (TREE_CODE (decl) == TEMPLATE_DECL)
2a13a625 3437 error ("%qD used without template parameters", decl);
a723baf1 3438 else
21526606
EC
3439 cp_parser_name_lookup_error
3440 (parser, token->value, decl,
4bb8ca28 3441 "is not a class or namespace");
a723baf1
MM
3442 parser->scope = NULL_TREE;
3443 error_p = true;
eea9800f
MM
3444 /* Treat this as a successful nested-name-specifier
3445 due to:
3446
3447 [basic.lookup.qual]
3448
3449 If the name found is not a class-name (clause
3450 _class_) or namespace-name (_namespace.def_), the
3451 program is ill-formed. */
3452 success = true;
a723baf1
MM
3453 }
3454 cp_lexer_consume_token (parser->lexer);
3455 }
3456 break;
3457 }
3458
3459 /* We've found one valid nested-name-specifier. */
3460 success = true;
3461 /* Make sure we look in the right scope the next time through
3462 the loop. */
21526606 3463 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
a723baf1
MM
3464 ? TREE_TYPE (new_scope)
3465 : new_scope);
3466 /* If it is a class scope, try to complete it; we are about to
3467 be looking up names inside the class. */
8fbc5ae7
MM
3468 if (TYPE_P (parser->scope)
3469 /* Since checking types for dependency can be expensive,
3470 avoid doing it if the type is already complete. */
3471 && !COMPLETE_TYPE_P (parser->scope)
3472 /* Do not try to complete dependent types. */
1fb3244a 3473 && !dependent_type_p (parser->scope))
a723baf1
MM
3474 complete_type (parser->scope);
3475 }
3476
cf22909c
KL
3477 /* Retrieve any deferred checks. Do not pop this access checks yet
3478 so the memory will not be reclaimed during token replacing below. */
3479 access_check = get_deferred_access_checks ();
3480
a723baf1
MM
3481 /* If parsing tentatively, replace the sequence of tokens that makes
3482 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3483 token. That way, should we re-parse the token stream, we will
3484 not have to repeat the effort required to do the parse, nor will
3485 we issue duplicate error messages. */
0c5e4866 3486 if (success && start)
a723baf1 3487 {
0c5e4866
NS
3488 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3489
a723baf1
MM
3490 /* Reset the contents of the START token. */
3491 token->type = CPP_NESTED_NAME_SPECIFIER;
3492 token->value = build_tree_list (access_check, parser->scope);
3493 TREE_TYPE (token->value) = parser->qualifying_scope;
3494 token->keyword = RID_MAX;
0c5e4866 3495
a723baf1 3496 /* Purge all subsequent tokens. */
0c5e4866 3497 cp_lexer_purge_tokens_after (parser->lexer, start);
a723baf1
MM
3498 }
3499
cf22909c 3500 pop_deferring_access_checks ();
a723baf1
MM
3501 return success ? parser->scope : NULL_TREE;
3502}
3503
3504/* Parse a nested-name-specifier. See
3505 cp_parser_nested_name_specifier_opt for details. This function
3506 behaves identically, except that it will an issue an error if no
3507 nested-name-specifier is present, and it will return
3508 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3509 is present. */
3510
3511static tree
21526606
EC
3512cp_parser_nested_name_specifier (cp_parser *parser,
3513 bool typename_keyword_p,
a723baf1 3514 bool check_dependency_p,
a668c6ad
MM
3515 bool type_p,
3516 bool is_declaration)
a723baf1
MM
3517{
3518 tree scope;
3519
3520 /* Look for the nested-name-specifier. */
3521 scope = cp_parser_nested_name_specifier_opt (parser,
3522 typename_keyword_p,
3523 check_dependency_p,
a668c6ad
MM
3524 type_p,
3525 is_declaration);
a723baf1
MM
3526 /* If it was not present, issue an error message. */
3527 if (!scope)
3528 {
3529 cp_parser_error (parser, "expected nested-name-specifier");
eb5abb39 3530 parser->scope = NULL_TREE;
a723baf1
MM
3531 return error_mark_node;
3532 }
3533
3534 return scope;
3535}
3536
3537/* Parse a class-or-namespace-name.
3538
3539 class-or-namespace-name:
3540 class-name
3541 namespace-name
3542
3543 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3544 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3545 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3546 TYPE_P is TRUE iff the next name should be taken as a class-name,
3547 even the same name is declared to be another entity in the same
3548 scope.
3549
3550 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3551 specified by the class-or-namespace-name. If neither is found the
3552 ERROR_MARK_NODE is returned. */
a723baf1
MM
3553
3554static tree
21526606 3555cp_parser_class_or_namespace_name (cp_parser *parser,
a723baf1
MM
3556 bool typename_keyword_p,
3557 bool template_keyword_p,
3558 bool check_dependency_p,
a668c6ad
MM
3559 bool type_p,
3560 bool is_declaration)
a723baf1
MM
3561{
3562 tree saved_scope;
3563 tree saved_qualifying_scope;
3564 tree saved_object_scope;
3565 tree scope;
eea9800f 3566 bool only_class_p;
a723baf1 3567
a723baf1
MM
3568 /* Before we try to parse the class-name, we must save away the
3569 current PARSER->SCOPE since cp_parser_class_name will destroy
3570 it. */
3571 saved_scope = parser->scope;
3572 saved_qualifying_scope = parser->qualifying_scope;
3573 saved_object_scope = parser->object_scope;
eea9800f
MM
3574 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3575 there is no need to look for a namespace-name. */
bbaab916 3576 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3577 if (!only_class_p)
3578 cp_parser_parse_tentatively (parser);
21526606 3579 scope = cp_parser_class_name (parser,
a723baf1
MM
3580 typename_keyword_p,
3581 template_keyword_p,
fc6a28d7 3582 type_p ? class_type : none_type,
a723baf1 3583 check_dependency_p,
a668c6ad
MM
3584 /*class_head_p=*/false,
3585 is_declaration);
a723baf1 3586 /* If that didn't work, try for a namespace-name. */
eea9800f 3587 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3588 {
3589 /* Restore the saved scope. */
3590 parser->scope = saved_scope;
3591 parser->qualifying_scope = saved_qualifying_scope;
3592 parser->object_scope = saved_object_scope;
eea9800f
MM
3593 /* If we are not looking at an identifier followed by the scope
3594 resolution operator, then this is not part of a
3595 nested-name-specifier. (Note that this function is only used
3596 to parse the components of a nested-name-specifier.) */
3597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3598 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3599 return error_mark_node;
a723baf1
MM
3600 scope = cp_parser_namespace_name (parser);
3601 }
3602
3603 return scope;
3604}
3605
3606/* Parse a postfix-expression.
3607
3608 postfix-expression:
3609 primary-expression
3610 postfix-expression [ expression ]
3611 postfix-expression ( expression-list [opt] )
3612 simple-type-specifier ( expression-list [opt] )
21526606 3613 typename :: [opt] nested-name-specifier identifier
a723baf1
MM
3614 ( expression-list [opt] )
3615 typename :: [opt] nested-name-specifier template [opt] template-id
3616 ( expression-list [opt] )
3617 postfix-expression . template [opt] id-expression
3618 postfix-expression -> template [opt] id-expression
3619 postfix-expression . pseudo-destructor-name
3620 postfix-expression -> pseudo-destructor-name
3621 postfix-expression ++
3622 postfix-expression --
3623 dynamic_cast < type-id > ( expression )
3624 static_cast < type-id > ( expression )
3625 reinterpret_cast < type-id > ( expression )
3626 const_cast < type-id > ( expression )
3627 typeid ( expression )
3628 typeid ( type-id )
3629
3630 GNU Extension:
21526606 3631
a723baf1
MM
3632 postfix-expression:
3633 ( type-id ) { initializer-list , [opt] }
3634
3635 This extension is a GNU version of the C99 compound-literal
3636 construct. (The C99 grammar uses `type-name' instead of `type-id',
3637 but they are essentially the same concept.)
3638
3639 If ADDRESS_P is true, the postfix expression is the operand of the
3640 `&' operator.
3641
3642 Returns a representation of the expression. */
3643
3644static tree
3645cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3646{
3647 cp_token *token;
3648 enum rid keyword;
b3445994 3649 cp_id_kind idk = CP_ID_KIND_NONE;
a723baf1
MM
3650 tree postfix_expression = NULL_TREE;
3651 /* Non-NULL only if the current postfix-expression can be used to
3652 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3653 class used to qualify the member. */
3654 tree qualifying_class = NULL_TREE;
a723baf1
MM
3655
3656 /* Peek at the next token. */
3657 token = cp_lexer_peek_token (parser->lexer);
3658 /* Some of the productions are determined by keywords. */
3659 keyword = token->keyword;
3660 switch (keyword)
3661 {
3662 case RID_DYNCAST:
3663 case RID_STATCAST:
3664 case RID_REINTCAST:
3665 case RID_CONSTCAST:
3666 {
3667 tree type;
3668 tree expression;
3669 const char *saved_message;
3670
3671 /* All of these can be handled in the same way from the point
3672 of view of parsing. Begin by consuming the token
3673 identifying the cast. */
3674 cp_lexer_consume_token (parser->lexer);
21526606 3675
a723baf1
MM
3676 /* New types cannot be defined in the cast. */
3677 saved_message = parser->type_definition_forbidden_message;
3678 parser->type_definition_forbidden_message
3679 = "types may not be defined in casts";
3680
3681 /* Look for the opening `<'. */
3682 cp_parser_require (parser, CPP_LESS, "`<'");
3683 /* Parse the type to which we are casting. */
3684 type = cp_parser_type_id (parser);
3685 /* Look for the closing `>'. */
3686 cp_parser_require (parser, CPP_GREATER, "`>'");
3687 /* Restore the old message. */
3688 parser->type_definition_forbidden_message = saved_message;
3689
3690 /* And the expression which is being cast. */
3691 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3692 expression = cp_parser_expression (parser);
3693 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3694
14d22dd6
MM
3695 /* Only type conversions to integral or enumeration types
3696 can be used in constant-expressions. */
67c03833 3697 if (parser->integral_constant_expression_p
14d22dd6 3698 && !dependent_type_p (type)
263ee052 3699 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 3700 && (cp_parser_non_integral_constant_expression
625cbf93
MM
3701 (parser,
3702 "a cast to a type other than an integral or "
3703 "enumeration type")))
3704 return error_mark_node;
14d22dd6 3705
a723baf1
MM
3706 switch (keyword)
3707 {
3708 case RID_DYNCAST:
3709 postfix_expression
3710 = build_dynamic_cast (type, expression);
3711 break;
3712 case RID_STATCAST:
3713 postfix_expression
3714 = build_static_cast (type, expression);
3715 break;
3716 case RID_REINTCAST:
3717 postfix_expression
3718 = build_reinterpret_cast (type, expression);
3719 break;
3720 case RID_CONSTCAST:
3721 postfix_expression
3722 = build_const_cast (type, expression);
3723 break;
3724 default:
315fb5db 3725 gcc_unreachable ();
a723baf1
MM
3726 }
3727 }
3728 break;
3729
3730 case RID_TYPEID:
3731 {
3732 tree type;
3733 const char *saved_message;
4f8163b1 3734 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3735
3736 /* Consume the `typeid' token. */
3737 cp_lexer_consume_token (parser->lexer);
3738 /* Look for the `(' token. */
3739 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3740 /* Types cannot be defined in a `typeid' expression. */
3741 saved_message = parser->type_definition_forbidden_message;
3742 parser->type_definition_forbidden_message
3743 = "types may not be defined in a `typeid\' expression";
3744 /* We can't be sure yet whether we're looking at a type-id or an
3745 expression. */
3746 cp_parser_parse_tentatively (parser);
3747 /* Try a type-id first. */
4f8163b1
MM
3748 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3749 parser->in_type_id_in_expr_p = true;
a723baf1 3750 type = cp_parser_type_id (parser);
4f8163b1 3751 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
3752 /* Look for the `)' token. Otherwise, we can't be sure that
3753 we're not looking at an expression: consider `typeid (int
3754 (3))', for example. */
3755 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3756 /* If all went well, simply lookup the type-id. */
3757 if (cp_parser_parse_definitely (parser))
3758 postfix_expression = get_typeid (type);
3759 /* Otherwise, fall back to the expression variant. */
3760 else
3761 {
3762 tree expression;
3763
3764 /* Look for an expression. */
3765 expression = cp_parser_expression (parser);
3766 /* Compute its typeid. */
3767 postfix_expression = build_typeid (expression);
3768 /* Look for the `)' token. */
3769 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3770 }
4424e0da 3771 /* `typeid' may not appear in an integral constant expression. */
98ca843c 3772 if (cp_parser_non_integral_constant_expression(parser,
4424e0da
GB
3773 "`typeid' operator"))
3774 return error_mark_node;
a723baf1
MM
3775 /* Restore the saved message. */
3776 parser->type_definition_forbidden_message = saved_message;
3777 }
3778 break;
21526606 3779
a723baf1
MM
3780 case RID_TYPENAME:
3781 {
3782 bool template_p = false;
3783 tree id;
3784 tree type;
3785
3786 /* Consume the `typename' token. */
3787 cp_lexer_consume_token (parser->lexer);
3788 /* Look for the optional `::' operator. */
21526606 3789 cp_parser_global_scope_opt (parser,
a723baf1
MM
3790 /*current_scope_valid_p=*/false);
3791 /* Look for the nested-name-specifier. */
3792 cp_parser_nested_name_specifier (parser,
3793 /*typename_keyword_p=*/true,
3794 /*check_dependency_p=*/true,
a668c6ad
MM
3795 /*type_p=*/true,
3796 /*is_declaration=*/true);
a723baf1
MM
3797 /* Look for the optional `template' keyword. */
3798 template_p = cp_parser_optional_template_keyword (parser);
3799 /* We don't know whether we're looking at a template-id or an
3800 identifier. */
3801 cp_parser_parse_tentatively (parser);
3802 /* Try a template-id. */
3803 id = cp_parser_template_id (parser, template_p,
a668c6ad
MM
3804 /*check_dependency_p=*/true,
3805 /*is_declaration=*/true);
a723baf1
MM
3806 /* If that didn't work, try an identifier. */
3807 if (!cp_parser_parse_definitely (parser))
3808 id = cp_parser_identifier (parser);
26bcf8fc
MM
3809 /* If we look up a template-id in a non-dependent qualifying
3810 scope, there's no need to create a dependent type. */
3811 if (TREE_CODE (id) == TYPE_DECL
3812 && !dependent_type_p (parser->scope))
3813 type = TREE_TYPE (id);
a723baf1
MM
3814 /* Create a TYPENAME_TYPE to represent the type to which the
3815 functional cast is being performed. */
26bcf8fc 3816 else
98ca843c 3817 type = make_typename_type (parser->scope, id,
fc6a28d7 3818 typename_type,
26bcf8fc 3819 /*complain=*/1);
a723baf1
MM
3820
3821 postfix_expression = cp_parser_functional_cast (parser, type);
3822 }
3823 break;
3824
3825 default:
3826 {
3827 tree type;
3828
3829 /* If the next thing is a simple-type-specifier, we may be
3830 looking at a functional cast. We could also be looking at
3831 an id-expression. So, we try the functional cast, and if
3832 that doesn't work we fall back to the primary-expression. */
3833 cp_parser_parse_tentatively (parser);
3834 /* Look for the simple-type-specifier. */
21526606 3835 type = cp_parser_simple_type_specifier (parser,
62d1db17
MM
3836 /*decl_specs=*/NULL,
3837 CP_PARSER_FLAGS_NONE);
a723baf1
MM
3838 /* Parse the cast itself. */
3839 if (!cp_parser_error_occurred (parser))
21526606 3840 postfix_expression
a723baf1
MM
3841 = cp_parser_functional_cast (parser, type);
3842 /* If that worked, we're done. */
3843 if (cp_parser_parse_definitely (parser))
3844 break;
3845
3846 /* If the functional-cast didn't work out, try a
3847 compound-literal. */
14d22dd6
MM
3848 if (cp_parser_allow_gnu_extensions_p (parser)
3849 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3850 {
3851 tree initializer_list = NULL_TREE;
4f8163b1 3852 bool saved_in_type_id_in_expr_p;
a723baf1
MM
3853
3854 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3855 /* Consume the `('. */
3856 cp_lexer_consume_token (parser->lexer);
3857 /* Parse the type. */
4f8163b1
MM
3858 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3859 parser->in_type_id_in_expr_p = true;
14d22dd6 3860 type = cp_parser_type_id (parser);
4f8163b1 3861 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14d22dd6
MM
3862 /* Look for the `)'. */
3863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3864 /* Look for the `{'. */
3865 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3866 /* If things aren't going well, there's no need to
3867 keep going. */
3868 if (!cp_parser_error_occurred (parser))
a723baf1 3869 {
39703eb9 3870 bool non_constant_p;
14d22dd6 3871 /* Parse the initializer-list. */
21526606 3872 initializer_list
39703eb9 3873 = cp_parser_initializer_list (parser, &non_constant_p);
14d22dd6
MM
3874 /* Allow a trailing `,'. */
3875 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3876 cp_lexer_consume_token (parser->lexer);
3877 /* Look for the final `}'. */
3878 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3879 }
3880 /* If that worked, we're definitely looking at a
3881 compound-literal expression. */
3882 if (cp_parser_parse_definitely (parser))
3883 {
3884 /* Warn the user that a compound literal is not
3885 allowed in standard C++. */
3886 if (pedantic)
3887 pedwarn ("ISO C++ forbids compound-literals");
3888 /* Form the representation of the compound-literal. */
21526606 3889 postfix_expression
a723baf1
MM
3890 = finish_compound_literal (type, initializer_list);
3891 break;
3892 }
3893 }
3894
3895 /* It must be a primary-expression. */
21526606 3896 postfix_expression = cp_parser_primary_expression (parser,
a723baf1
MM
3897 &idk,
3898 &qualifying_class);
3899 }
3900 break;
3901 }
3902
ee76b931
MM
3903 /* If we were avoiding committing to the processing of a
3904 qualified-id until we knew whether or not we had a
3905 pointer-to-member, we now know. */
089d6ea7 3906 if (qualifying_class)
a723baf1 3907 {
ee76b931 3908 bool done;
a723baf1 3909
ee76b931
MM
3910 /* Peek at the next token. */
3911 token = cp_lexer_peek_token (parser->lexer);
3912 done = (token->type != CPP_OPEN_SQUARE
3913 && token->type != CPP_OPEN_PAREN
3914 && token->type != CPP_DOT
3915 && token->type != CPP_DEREF
3916 && token->type != CPP_PLUS_PLUS
3917 && token->type != CPP_MINUS_MINUS);
3918
3919 postfix_expression = finish_qualified_id_expr (qualifying_class,
3920 postfix_expression,
3921 done,
3922 address_p);
3923 if (done)
3924 return postfix_expression;
a723baf1
MM
3925 }
3926
a723baf1
MM
3927 /* Keep looping until the postfix-expression is complete. */
3928 while (true)
3929 {
10b1d5e7
MM
3930 if (idk == CP_ID_KIND_UNQUALIFIED
3931 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
a723baf1 3932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
b3445994 3933 /* It is not a Koenig lookup function call. */
21526606 3934 postfix_expression
b3445994 3935 = unqualified_name_lookup_error (postfix_expression);
21526606 3936
a723baf1
MM
3937 /* Peek at the next token. */
3938 token = cp_lexer_peek_token (parser->lexer);
3939
3940 switch (token->type)
3941 {
3942 case CPP_OPEN_SQUARE:
7a3ea201
RH
3943 postfix_expression
3944 = cp_parser_postfix_open_square_expression (parser,
3945 postfix_expression,
3946 false);
3947 idk = CP_ID_KIND_NONE;
a723baf1
MM
3948 break;
3949
3950 case CPP_OPEN_PAREN:
3951 /* postfix-expression ( expression-list [opt] ) */
3952 {
6d80c4b9 3953 bool koenig_p;
21526606 3954 tree args = (cp_parser_parenthesized_expression_list
39703eb9 3955 (parser, false, /*non_constant_p=*/NULL));
a723baf1 3956
7efa3e22
NS
3957 if (args == error_mark_node)
3958 {
3959 postfix_expression = error_mark_node;
3960 break;
3961 }
21526606 3962
14d22dd6
MM
3963 /* Function calls are not permitted in
3964 constant-expressions. */
625cbf93
MM
3965 if (cp_parser_non_integral_constant_expression (parser,
3966 "a function call"))
14d22dd6 3967 {
625cbf93
MM
3968 postfix_expression = error_mark_node;
3969 break;
14d22dd6 3970 }
a723baf1 3971
6d80c4b9 3972 koenig_p = false;
399dedb9
NS
3973 if (idk == CP_ID_KIND_UNQUALIFIED)
3974 {
89d594a2
NS
3975 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3976 {
3977 if (args)
3978 {
3979 koenig_p = true;
3980 postfix_expression
3981 = perform_koenig_lookup (postfix_expression, args);
3982 }
3983 else
3984 postfix_expression
3985 = unqualified_fn_lookup_error (postfix_expression);
3986 }
676e33ca
MM
3987 /* We do not perform argument-dependent lookup if
3988 normal lookup finds a non-function, in accordance
3989 with the expected resolution of DR 218. */
89d594a2 3990 else if (args && is_overloaded_fn (postfix_expression))
6d80c4b9 3991 {
89d594a2
NS
3992 tree fn = get_first_fn (postfix_expression);
3993
3994 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3995 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3996
3997 /* Only do argument dependent lookup if regular
3998 lookup does not find a set of member functions.
3999 [basic.lookup.koenig]/2a */
4000 if (!DECL_FUNCTION_MEMBER_P (fn))
4001 {
4002 koenig_p = true;
4003 postfix_expression
4004 = perform_koenig_lookup (postfix_expression, args);
4005 }
6d80c4b9 4006 }
399dedb9 4007 }
21526606 4008
d17811fd 4009 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
a723baf1 4010 {
d17811fd
MM
4011 tree instance = TREE_OPERAND (postfix_expression, 0);
4012 tree fn = TREE_OPERAND (postfix_expression, 1);
4013
4014 if (processing_template_decl
4015 && (type_dependent_expression_p (instance)
4016 || (!BASELINK_P (fn)
4017 && TREE_CODE (fn) != FIELD_DECL)
584672ee 4018 || type_dependent_expression_p (fn)
d17811fd
MM
4019 || any_type_dependent_arguments_p (args)))
4020 {
4021 postfix_expression
6de9cd9a
DN
4022 = build_min_nt (CALL_EXPR, postfix_expression,
4023 args, NULL_TREE);
d17811fd
MM
4024 break;
4025 }
9f880ef9
MM
4026
4027 if (BASELINK_P (fn))
4028 postfix_expression
21526606
EC
4029 = (build_new_method_call
4030 (instance, fn, args, NULL_TREE,
4031 (idk == CP_ID_KIND_QUALIFIED
9f880ef9
MM
4032 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4033 else
4034 postfix_expression
4035 = finish_call_expr (postfix_expression, args,
4036 /*disallow_virtual=*/false,
4037 /*koenig_p=*/false);
a723baf1 4038 }
d17811fd
MM
4039 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4040 || TREE_CODE (postfix_expression) == MEMBER_REF
4041 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
a723baf1
MM
4042 postfix_expression = (build_offset_ref_call_from_tree
4043 (postfix_expression, args));
b3445994 4044 else if (idk == CP_ID_KIND_QUALIFIED)
2050a1bb
MM
4045 /* A call to a static class member, or a namespace-scope
4046 function. */
4047 postfix_expression
4048 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4049 /*disallow_virtual=*/true,
4050 koenig_p);
a723baf1 4051 else
2050a1bb 4052 /* All other function calls. */
21526606
EC
4053 postfix_expression
4054 = finish_call_expr (postfix_expression, args,
6d80c4b9
MM
4055 /*disallow_virtual=*/false,
4056 koenig_p);
a723baf1
MM
4057
4058 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
b3445994 4059 idk = CP_ID_KIND_NONE;
a723baf1
MM
4060 }
4061 break;
21526606 4062
a723baf1
MM
4063 case CPP_DOT:
4064 case CPP_DEREF:
21526606
EC
4065 /* postfix-expression . template [opt] id-expression
4066 postfix-expression . pseudo-destructor-name
a723baf1
MM
4067 postfix-expression -> template [opt] id-expression
4068 postfix-expression -> pseudo-destructor-name */
98ca843c 4069
7a3ea201
RH
4070 /* Consume the `.' or `->' operator. */
4071 cp_lexer_consume_token (parser->lexer);
a723baf1 4072
7a3ea201
RH
4073 postfix_expression
4074 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4075 postfix_expression,
4076 false, &idk);
a723baf1
MM
4077 break;
4078
4079 case CPP_PLUS_PLUS:
4080 /* postfix-expression ++ */
4081 /* Consume the `++' token. */
4082 cp_lexer_consume_token (parser->lexer);
a5ac3982 4083 /* Generate a representation for the complete expression. */
21526606
EC
4084 postfix_expression
4085 = finish_increment_expr (postfix_expression,
a5ac3982 4086 POSTINCREMENT_EXPR);
14d22dd6 4087 /* Increments may not appear in constant-expressions. */
625cbf93
MM
4088 if (cp_parser_non_integral_constant_expression (parser,
4089 "an increment"))
4090 postfix_expression = error_mark_node;
b3445994 4091 idk = CP_ID_KIND_NONE;
a723baf1
MM
4092 break;
4093
4094 case CPP_MINUS_MINUS:
4095 /* postfix-expression -- */
4096 /* Consume the `--' token. */
4097 cp_lexer_consume_token (parser->lexer);
a5ac3982 4098 /* Generate a representation for the complete expression. */
21526606
EC
4099 postfix_expression
4100 = finish_increment_expr (postfix_expression,
a5ac3982 4101 POSTDECREMENT_EXPR);
14d22dd6 4102 /* Decrements may not appear in constant-expressions. */
625cbf93
MM
4103 if (cp_parser_non_integral_constant_expression (parser,
4104 "a decrement"))
4105 postfix_expression = error_mark_node;
b3445994 4106 idk = CP_ID_KIND_NONE;
a723baf1
MM
4107 break;
4108
4109 default:
4110 return postfix_expression;
4111 }
4112 }
4113
4114 /* We should never get here. */
315fb5db 4115 gcc_unreachable ();
a723baf1
MM
4116 return error_mark_node;
4117}
4118
7a3ea201
RH
4119/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4120 by cp_parser_builtin_offsetof. We're looking for
4121
4122 postfix-expression [ expression ]
4123
4124 FOR_OFFSETOF is set if we're being called in that context, which
4125 changes how we deal with integer constant expressions. */
4126
4127static tree
4128cp_parser_postfix_open_square_expression (cp_parser *parser,
4129 tree postfix_expression,
4130 bool for_offsetof)
4131{
4132 tree index;
4133
4134 /* Consume the `[' token. */
4135 cp_lexer_consume_token (parser->lexer);
4136
4137 /* Parse the index expression. */
4138 /* ??? For offsetof, there is a question of what to allow here. If
4139 offsetof is not being used in an integral constant expression context,
4140 then we *could* get the right answer by computing the value at runtime.
4141 If we are in an integral constant expression context, then we might
4142 could accept any constant expression; hard to say without analysis.
4143 Rather than open the barn door too wide right away, allow only integer
77880ae4 4144 constant expressions here. */
7a3ea201
RH
4145 if (for_offsetof)
4146 index = cp_parser_constant_expression (parser, false, NULL);
4147 else
4148 index = cp_parser_expression (parser);
4149
4150 /* Look for the closing `]'. */
4151 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4152
4153 /* Build the ARRAY_REF. */
4154 postfix_expression = grok_array_decl (postfix_expression, index);
4155
4156 /* When not doing offsetof, array references are not permitted in
4157 constant-expressions. */
4158 if (!for_offsetof
4159 && (cp_parser_non_integral_constant_expression
4160 (parser, "an array reference")))
4161 postfix_expression = error_mark_node;
4162
4163 return postfix_expression;
4164}
4165
4166/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4167 by cp_parser_builtin_offsetof. We're looking for
4168
4169 postfix-expression . template [opt] id-expression
4170 postfix-expression . pseudo-destructor-name
4171 postfix-expression -> template [opt] id-expression
4172 postfix-expression -> pseudo-destructor-name
4173
4174 FOR_OFFSETOF is set if we're being called in that context. That sorta
4175 limits what of the above we'll actually accept, but nevermind.
4176 TOKEN_TYPE is the "." or "->" token, which will already have been
4177 removed from the stream. */
4178
4179static tree
4180cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4181 enum cpp_ttype token_type,
4182 tree postfix_expression,
4183 bool for_offsetof, cp_id_kind *idk)
4184{
4185 tree name;
4186 bool dependent_p;
4187 bool template_p;
17a27b4f 4188 bool pseudo_destructor_p;
7a3ea201
RH
4189 tree scope = NULL_TREE;
4190
4191 /* If this is a `->' operator, dereference the pointer. */
4192 if (token_type == CPP_DEREF)
4193 postfix_expression = build_x_arrow (postfix_expression);
4194 /* Check to see whether or not the expression is type-dependent. */
4195 dependent_p = type_dependent_expression_p (postfix_expression);
4196 /* The identifier following the `->' or `.' is not qualified. */
4197 parser->scope = NULL_TREE;
4198 parser->qualifying_scope = NULL_TREE;
4199 parser->object_scope = NULL_TREE;
4200 *idk = CP_ID_KIND_NONE;
4201 /* Enter the scope corresponding to the type of the object
4202 given by the POSTFIX_EXPRESSION. */
4203 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4204 {
4205 scope = TREE_TYPE (postfix_expression);
4206 /* According to the standard, no expression should ever have
4207 reference type. Unfortunately, we do not currently match
4208 the standard in this respect in that our internal representation
4209 of an expression may have reference type even when the standard
4210 says it does not. Therefore, we have to manually obtain the
4211 underlying type here. */
4212 scope = non_reference (scope);
4213 /* The type of the POSTFIX_EXPRESSION must be complete. */
4214 scope = complete_type_or_else (scope, NULL_TREE);
4215 /* Let the name lookup machinery know that we are processing a
4216 class member access expression. */
4217 parser->context->object_type = scope;
4218 /* If something went wrong, we want to be able to discern that case,
4219 as opposed to the case where there was no SCOPE due to the type
4220 of expression being dependent. */
4221 if (!scope)
4222 scope = error_mark_node;
4223 /* If the SCOPE was erroneous, make the various semantic analysis
4224 functions exit quickly -- and without issuing additional error
4225 messages. */
4226 if (scope == error_mark_node)
4227 postfix_expression = error_mark_node;
4228 }
4229
17a27b4f
MM
4230 /* Assume this expression is not a pseudo-destructor access. */
4231 pseudo_destructor_p = false;
4232
4233 /* If the SCOPE is a scalar type, then, if this is a valid program,
4234 we must be looking at a pseudo-destructor-name. */
4235 if (scope && SCALAR_TYPE_P (scope))
7a3ea201 4236 {
17a27b4f
MM
4237 tree s;
4238 tree type;
4239
4240 cp_parser_parse_tentatively (parser);
4241 /* Parse the pseudo-destructor-name. */
4242 s = NULL_TREE;
4243 cp_parser_pseudo_destructor_name (parser, &s, &type);
4244 if (cp_parser_parse_definitely (parser))
4245 {
4246 pseudo_destructor_p = true;
4247 postfix_expression
4248 = finish_pseudo_destructor_expr (postfix_expression,
4249 s, TREE_TYPE (type));
4250 }
4251 }
4252
4253 if (!pseudo_destructor_p)
4254 {
4255 /* If the SCOPE is not a scalar type, we are looking at an
4256 ordinary class member access expression, rather than a
4257 pseudo-destructor-name. */
7a3ea201
RH
4258 template_p = cp_parser_optional_template_keyword (parser);
4259 /* Parse the id-expression. */
4260 name = cp_parser_id_expression (parser, template_p,
4261 /*check_dependency_p=*/true,
4262 /*template_p=*/NULL,
4263 /*declarator_p=*/false);
4264 /* In general, build a SCOPE_REF if the member name is qualified.
4265 However, if the name was not dependent and has already been
4266 resolved; there is no need to build the SCOPE_REF. For example;
4267
4268 struct X { void f(); };
4269 template <typename T> void f(T* t) { t->X::f(); }
4270
4271 Even though "t" is dependent, "X::f" is not and has been resolved
4272 to a BASELINK; there is no need to include scope information. */
4273
4274 /* But we do need to remember that there was an explicit scope for
4275 virtual function calls. */
4276 if (parser->scope)
4277 *idk = CP_ID_KIND_QUALIFIED;
4278
fc6a28d7
MM
4279 /* If the name is a template-id that names a type, we will get a
4280 TYPE_DECL here. That is invalid code. */
4281 if (TREE_CODE (name) == TYPE_DECL)
7a3ea201 4282 {
fc6a28d7
MM
4283 error ("invalid use of %qD", name);
4284 postfix_expression = error_mark_node;
4285 }
4286 else
4287 {
4288 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4289 {
4290 name = build_nt (SCOPE_REF, parser->scope, name);
4291 parser->scope = NULL_TREE;
4292 parser->qualifying_scope = NULL_TREE;
4293 parser->object_scope = NULL_TREE;
4294 }
4295 if (scope && name && BASELINK_P (name))
4296 adjust_result_of_qualified_name_lookup
4297 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4298 postfix_expression
4299 = finish_class_member_access_expr (postfix_expression, name);
7a3ea201 4300 }
7a3ea201 4301 }
7a3ea201
RH
4302
4303 /* We no longer need to look up names in the scope of the object on
4304 the left-hand side of the `.' or `->' operator. */
4305 parser->context->object_type = NULL_TREE;
4306
4307 /* Outside of offsetof, these operators may not appear in
4308 constant-expressions. */
4309 if (!for_offsetof
98ca843c 4310 && (cp_parser_non_integral_constant_expression
7a3ea201
RH
4311 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4312 postfix_expression = error_mark_node;
4313
4314 return postfix_expression;
4315}
4316
7efa3e22 4317/* Parse a parenthesized expression-list.
a723baf1
MM
4318
4319 expression-list:
4320 assignment-expression
4321 expression-list, assignment-expression
4322
7efa3e22
NS
4323 attribute-list:
4324 expression-list
4325 identifier
4326 identifier, expression-list
4327
a723baf1
MM
4328 Returns a TREE_LIST. The TREE_VALUE of each node is a
4329 representation of an assignment-expression. Note that a TREE_LIST
7efa3e22
NS
4330 is returned even if there is only a single expression in the list.
4331 error_mark_node is returned if the ( and or ) are
4332 missing. NULL_TREE is returned on no expressions. The parentheses
4333 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
39703eb9
MM
4334 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4335 indicates whether or not all of the expressions in the list were
4336 constant. */
a723baf1
MM
4337
4338static tree
21526606 4339cp_parser_parenthesized_expression_list (cp_parser* parser,
39703eb9
MM
4340 bool is_attribute_list,
4341 bool *non_constant_p)
a723baf1
MM
4342{
4343 tree expression_list = NULL_TREE;
1ed3dfd5 4344 bool fold_expr_p = is_attribute_list;
7efa3e22 4345 tree identifier = NULL_TREE;
39703eb9
MM
4346
4347 /* Assume all the expressions will be constant. */
4348 if (non_constant_p)
4349 *non_constant_p = false;
4350
7efa3e22
NS
4351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4352 return error_mark_node;
21526606 4353
a723baf1 4354 /* Consume expressions until there are no more. */
7efa3e22
NS
4355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4356 while (true)
4357 {
4358 tree expr;
21526606 4359
7efa3e22
NS
4360 /* At the beginning of attribute lists, check to see if the
4361 next token is an identifier. */
4362 if (is_attribute_list
4363 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4364 {
4365 cp_token *token;
21526606 4366
7efa3e22
NS
4367 /* Consume the identifier. */
4368 token = cp_lexer_consume_token (parser->lexer);
4369 /* Save the identifier. */
4370 identifier = token->value;
4371 }
4372 else
4373 {
4374 /* Parse the next assignment-expression. */
39703eb9
MM
4375 if (non_constant_p)
4376 {
4377 bool expr_non_constant_p;
21526606 4378 expr = (cp_parser_constant_expression
39703eb9
MM
4379 (parser, /*allow_non_constant_p=*/true,
4380 &expr_non_constant_p));
4381 if (expr_non_constant_p)
4382 *non_constant_p = true;
4383 }
4384 else
4385 expr = cp_parser_assignment_expression (parser);
a723baf1 4386
1ed3dfd5
GB
4387 if (fold_expr_p)
4388 expr = fold_non_dependent_expr (expr);
4389
7efa3e22
NS
4390 /* Add it to the list. We add error_mark_node
4391 expressions to the list, so that we can still tell if
4392 the correct form for a parenthesized expression-list
4393 is found. That gives better errors. */
4394 expression_list = tree_cons (NULL_TREE, expr, expression_list);
a723baf1 4395
7efa3e22
NS
4396 if (expr == error_mark_node)
4397 goto skip_comma;
4398 }
a723baf1 4399
7efa3e22
NS
4400 /* After the first item, attribute lists look the same as
4401 expression lists. */
4402 is_attribute_list = false;
21526606 4403
7efa3e22
NS
4404 get_comma:;
4405 /* If the next token isn't a `,', then we are done. */
4406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4407 break;
4408
4409 /* Otherwise, consume the `,' and keep going. */
4410 cp_lexer_consume_token (parser->lexer);
4411 }
21526606 4412
7efa3e22
NS
4413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4414 {
4415 int ending;
21526606 4416
7efa3e22
NS
4417 skip_comma:;
4418 /* We try and resync to an unnested comma, as that will give the
4419 user better diagnostics. */
21526606
EC
4420 ending = cp_parser_skip_to_closing_parenthesis (parser,
4421 /*recovering=*/true,
4bb8ca28 4422 /*or_comma=*/true,
a668c6ad 4423 /*consume_paren=*/true);
7efa3e22
NS
4424 if (ending < 0)
4425 goto get_comma;
4426 if (!ending)
4427 return error_mark_node;
a723baf1
MM
4428 }
4429
4430 /* We built up the list in reverse order so we must reverse it now. */
7efa3e22
NS
4431 expression_list = nreverse (expression_list);
4432 if (identifier)
4433 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
21526606 4434
7efa3e22 4435 return expression_list;
a723baf1
MM
4436}
4437
4438/* Parse a pseudo-destructor-name.
4439
4440 pseudo-destructor-name:
4441 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4442 :: [opt] nested-name-specifier template template-id :: ~ type-name
4443 :: [opt] nested-name-specifier [opt] ~ type-name
4444
4445 If either of the first two productions is used, sets *SCOPE to the
4446 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4447 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
d6e57462 4448 or ERROR_MARK_NODE if the parse fails. */
a723baf1
MM
4449
4450static void
21526606
EC
4451cp_parser_pseudo_destructor_name (cp_parser* parser,
4452 tree* scope,
94edc4ab 4453 tree* type)
a723baf1
MM
4454{
4455 bool nested_name_specifier_p;
4456
b14454ba
MM
4457 /* Assume that things will not work out. */
4458 *type = error_mark_node;
4459
a723baf1
MM
4460 /* Look for the optional `::' operator. */
4461 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4462 /* Look for the optional nested-name-specifier. */
21526606 4463 nested_name_specifier_p
a723baf1
MM
4464 = (cp_parser_nested_name_specifier_opt (parser,
4465 /*typename_keyword_p=*/false,
4466 /*check_dependency_p=*/true,
a668c6ad 4467 /*type_p=*/false,
21526606 4468 /*is_declaration=*/true)
a723baf1
MM
4469 != NULL_TREE);
4470 /* Now, if we saw a nested-name-specifier, we might be doing the
4471 second production. */
21526606 4472 if (nested_name_specifier_p
a723baf1
MM
4473 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4474 {
4475 /* Consume the `template' keyword. */
4476 cp_lexer_consume_token (parser->lexer);
4477 /* Parse the template-id. */
21526606 4478 cp_parser_template_id (parser,
a723baf1 4479 /*template_keyword_p=*/true,
a668c6ad
MM
4480 /*check_dependency_p=*/false,
4481 /*is_declaration=*/true);
a723baf1
MM
4482 /* Look for the `::' token. */
4483 cp_parser_require (parser, CPP_SCOPE, "`::'");
4484 }
4485 /* If the next token is not a `~', then there might be some
9bcb9aae 4486 additional qualification. */
a723baf1
MM
4487 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4488 {
4489 /* Look for the type-name. */
4490 *scope = TREE_TYPE (cp_parser_type_name (parser));
d6e57462 4491
b14454ba
MM
4492 if (*scope == error_mark_node)
4493 return;
4494
4495 /* If we don't have ::~, then something has gone wrong. Since
4496 the only caller of this function is looking for something
4497 after `.' or `->' after a scalar type, most likely the
4498 program is trying to get a member of a non-aggregate
4499 type. */
4500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
d6e57462
ILT
4501 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4502 {
4503 cp_parser_error (parser, "request for member of non-aggregate type");
d6e57462
ILT
4504 return;
4505 }
4506
a723baf1
MM
4507 /* Look for the `::' token. */
4508 cp_parser_require (parser, CPP_SCOPE, "`::'");
4509 }
4510 else
4511 *scope = NULL_TREE;
4512
4513 /* Look for the `~'. */
4514 cp_parser_require (parser, CPP_COMPL, "`~'");
4515 /* Look for the type-name again. We are not responsible for
4516 checking that it matches the first type-name. */
4517 *type = cp_parser_type_name (parser);
4518}
4519
4520/* Parse a unary-expression.
4521
4522 unary-expression:
4523 postfix-expression
4524 ++ cast-expression
4525 -- cast-expression
4526 unary-operator cast-expression
4527 sizeof unary-expression
4528 sizeof ( type-id )
4529 new-expression
4530 delete-expression
4531
4532 GNU Extensions:
4533
4534 unary-expression:
4535 __extension__ cast-expression
4536 __alignof__ unary-expression
4537 __alignof__ ( type-id )
4538 __real__ cast-expression
4539 __imag__ cast-expression
4540 && identifier
4541
4542 ADDRESS_P is true iff the unary-expression is appearing as the
4543 operand of the `&' operator.
4544
34cd5ae7 4545 Returns a representation of the expression. */
a723baf1
MM
4546
4547static tree
4548cp_parser_unary_expression (cp_parser *parser, bool address_p)
4549{
4550 cp_token *token;
4551 enum tree_code unary_operator;
4552
4553 /* Peek at the next token. */
4554 token = cp_lexer_peek_token (parser->lexer);
4555 /* Some keywords give away the kind of expression. */
4556 if (token->type == CPP_KEYWORD)
4557 {
4558 enum rid keyword = token->keyword;
4559
4560 switch (keyword)
4561 {
4562 case RID_ALIGNOF:
a723baf1
MM
4563 case RID_SIZEOF:
4564 {
4565 tree operand;
7a18b933 4566 enum tree_code op;
21526606 4567
7a18b933
NS
4568 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4569 /* Consume the token. */
a723baf1
MM
4570 cp_lexer_consume_token (parser->lexer);
4571 /* Parse the operand. */
4572 operand = cp_parser_sizeof_operand (parser, keyword);
4573
7a18b933
NS
4574 if (TYPE_P (operand))
4575 return cxx_sizeof_or_alignof_type (operand, op, true);
a723baf1 4576 else
7a18b933 4577 return cxx_sizeof_or_alignof_expr (operand, op);
a723baf1
MM
4578 }
4579
4580 case RID_NEW:
4581 return cp_parser_new_expression (parser);
4582
4583 case RID_DELETE:
4584 return cp_parser_delete_expression (parser);
21526606 4585
a723baf1
MM
4586 case RID_EXTENSION:
4587 {
4588 /* The saved value of the PEDANTIC flag. */
4589 int saved_pedantic;
4590 tree expr;
4591
4592 /* Save away the PEDANTIC flag. */
4593 cp_parser_extension_opt (parser, &saved_pedantic);
4594 /* Parse the cast-expression. */
d6b4ea85 4595 expr = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4596 /* Restore the PEDANTIC flag. */
4597 pedantic = saved_pedantic;
4598
4599 return expr;
4600 }
4601
4602 case RID_REALPART:
4603 case RID_IMAGPART:
4604 {
4605 tree expression;
4606
4607 /* Consume the `__real__' or `__imag__' token. */
4608 cp_lexer_consume_token (parser->lexer);
4609 /* Parse the cast-expression. */
d6b4ea85 4610 expression = cp_parser_simple_cast_expression (parser);
a723baf1
MM
4611 /* Create the complete representation. */
4612 return build_x_unary_op ((keyword == RID_REALPART
4613 ? REALPART_EXPR : IMAGPART_EXPR),
4614 expression);
4615 }
4616 break;
4617
4618 default:
4619 break;
4620 }
4621 }
4622
4623 /* Look for the `:: new' and `:: delete', which also signal the
4624 beginning of a new-expression, or delete-expression,
4625 respectively. If the next token is `::', then it might be one of
4626 these. */
4627 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4628 {
4629 enum rid keyword;
4630
4631 /* See if the token after the `::' is one of the keywords in
4632 which we're interested. */
4633 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4634 /* If it's `new', we have a new-expression. */
4635 if (keyword == RID_NEW)
4636 return cp_parser_new_expression (parser);
4637 /* Similarly, for `delete'. */
4638 else if (keyword == RID_DELETE)
4639 return cp_parser_delete_expression (parser);
4640 }
4641
4642 /* Look for a unary operator. */
4643 unary_operator = cp_parser_unary_operator (token);
4644 /* The `++' and `--' operators can be handled similarly, even though
4645 they are not technically unary-operators in the grammar. */
4646 if (unary_operator == ERROR_MARK)
4647 {
4648 if (token->type == CPP_PLUS_PLUS)
4649 unary_operator = PREINCREMENT_EXPR;
4650 else if (token->type == CPP_MINUS_MINUS)
4651 unary_operator = PREDECREMENT_EXPR;
4652 /* Handle the GNU address-of-label extension. */
4653 else if (cp_parser_allow_gnu_extensions_p (parser)
4654 && token->type == CPP_AND_AND)
4655 {
4656 tree identifier;
4657
4658 /* Consume the '&&' token. */
4659 cp_lexer_consume_token (parser->lexer);
4660 /* Look for the identifier. */
4661 identifier = cp_parser_identifier (parser);
4662 /* Create an expression representing the address. */
4663 return finish_label_address_expr (identifier);
4664 }
4665 }
4666 if (unary_operator != ERROR_MARK)
4667 {
4668 tree cast_expression;
a5ac3982
MM
4669 tree expression = error_mark_node;
4670 const char *non_constant_p = NULL;
a723baf1
MM
4671
4672 /* Consume the operator token. */
4673 token = cp_lexer_consume_token (parser->lexer);
4674 /* Parse the cast-expression. */
21526606 4675 cast_expression
a723baf1
MM
4676 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4677 /* Now, build an appropriate representation. */
4678 switch (unary_operator)
4679 {
4680 case INDIRECT_REF:
a5ac3982
MM
4681 non_constant_p = "`*'";
4682 expression = build_x_indirect_ref (cast_expression, "unary *");
4683 break;
4684
a723baf1 4685 case ADDR_EXPR:
7a3ea201 4686 non_constant_p = "`&'";
a5ac3982 4687 /* Fall through. */
d17811fd 4688 case BIT_NOT_EXPR:
a5ac3982
MM
4689 expression = build_x_unary_op (unary_operator, cast_expression);
4690 break;
4691
14d22dd6
MM
4692 case PREINCREMENT_EXPR:
4693 case PREDECREMENT_EXPR:
a5ac3982
MM
4694 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4695 ? "`++'" : "`--'");
14d22dd6 4696 /* Fall through. */
a723baf1
MM
4697 case CONVERT_EXPR:
4698 case NEGATE_EXPR:
4699 case TRUTH_NOT_EXPR:
a5ac3982
MM
4700 expression = finish_unary_op_expr (unary_operator, cast_expression);
4701 break;
a723baf1 4702
a723baf1 4703 default:
315fb5db 4704 gcc_unreachable ();
a723baf1 4705 }
a5ac3982 4706
98ca843c 4707 if (non_constant_p
625cbf93
MM
4708 && cp_parser_non_integral_constant_expression (parser,
4709 non_constant_p))
4710 expression = error_mark_node;
a5ac3982
MM
4711
4712 return expression;
a723baf1
MM
4713 }
4714
4715 return cp_parser_postfix_expression (parser, address_p);
4716}
4717
4718/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4719 unary-operator, the corresponding tree code is returned. */
4720
4721static enum tree_code
94edc4ab 4722cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4723{
4724 switch (token->type)
4725 {
4726 case CPP_MULT:
4727 return INDIRECT_REF;
4728
4729 case CPP_AND:
4730 return ADDR_EXPR;
4731
4732 case CPP_PLUS:
4733 return CONVERT_EXPR;
4734
4735 case CPP_MINUS:
4736 return NEGATE_EXPR;
4737
4738 case CPP_NOT:
4739 return TRUTH_NOT_EXPR;
21526606 4740
a723baf1
MM
4741 case CPP_COMPL:
4742 return BIT_NOT_EXPR;
4743
4744 default:
4745 return ERROR_MARK;
4746 }
4747}
4748
4749/* Parse a new-expression.
4750
ca099ac8 4751 new-expression:
a723baf1
MM
4752 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4753 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4754
4755 Returns a representation of the expression. */
4756
4757static tree
94edc4ab 4758cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4759{
4760 bool global_scope_p;
4761 tree placement;
4762 tree type;
4763 tree initializer;
058b15c1 4764 tree nelts;
a723baf1
MM
4765
4766 /* Look for the optional `::' operator. */
21526606 4767 global_scope_p
a723baf1
MM
4768 = (cp_parser_global_scope_opt (parser,
4769 /*current_scope_valid_p=*/false)
4770 != NULL_TREE);
4771 /* Look for the `new' operator. */
4772 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4773 /* There's no easy way to tell a new-placement from the
4774 `( type-id )' construct. */
4775 cp_parser_parse_tentatively (parser);
4776 /* Look for a new-placement. */
4777 placement = cp_parser_new_placement (parser);
4778 /* If that didn't work out, there's no new-placement. */
4779 if (!cp_parser_parse_definitely (parser))
4780 placement = NULL_TREE;
4781
4782 /* If the next token is a `(', then we have a parenthesized
4783 type-id. */
4784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4785 {
4786 /* Consume the `('. */
4787 cp_lexer_consume_token (parser->lexer);
4788 /* Parse the type-id. */
4789 type = cp_parser_type_id (parser);
4790 /* Look for the closing `)'. */
4791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 4792 /* There should not be a direct-new-declarator in this production,
063e900f
GB
4793 but GCC used to allowed this, so we check and emit a sensible error
4794 message for this case. */
4795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0da99d4e
GB
4796 {
4797 error ("array bound forbidden after parenthesized type-id");
4798 inform ("try removing the parentheses around the type-id");
063e900f
GB
4799 cp_parser_direct_new_declarator (parser);
4800 }
17a27b4f 4801 nelts = NULL_TREE;
a723baf1
MM
4802 }
4803 /* Otherwise, there must be a new-type-id. */
4804 else
058b15c1 4805 type = cp_parser_new_type_id (parser, &nelts);
a723baf1
MM
4806
4807 /* If the next token is a `(', then we have a new-initializer. */
4808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4809 initializer = cp_parser_new_initializer (parser);
4810 else
4811 initializer = NULL_TREE;
4812
625cbf93
MM
4813 /* A new-expression may not appear in an integral constant
4814 expression. */
4815 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4816 return error_mark_node;
4817
a723baf1 4818 /* Create a representation of the new-expression. */
058b15c1 4819 return build_new (placement, type, nelts, initializer, global_scope_p);
a723baf1
MM
4820}
4821
4822/* Parse a new-placement.
4823
4824 new-placement:
4825 ( expression-list )
4826
4827 Returns the same representation as for an expression-list. */
4828
4829static tree
94edc4ab 4830cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4831{
4832 tree expression_list;
4833
a723baf1 4834 /* Parse the expression-list. */
21526606 4835 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 4836 (parser, false, /*non_constant_p=*/NULL));
a723baf1
MM
4837
4838 return expression_list;
4839}
4840
4841/* Parse a new-type-id.
4842
4843 new-type-id:
4844 type-specifier-seq new-declarator [opt]
4845
058b15c1
MM
4846 Returns the TYPE allocated. If the new-type-id indicates an array
4847 type, *NELTS is set to the number of elements in the last array
4848 bound; the TYPE will not include the last array bound. */
a723baf1
MM
4849
4850static tree
058b15c1 4851cp_parser_new_type_id (cp_parser* parser, tree *nelts)
a723baf1 4852{
62d1db17 4853 cp_decl_specifier_seq type_specifier_seq;
058b15c1
MM
4854 cp_declarator *new_declarator;
4855 cp_declarator *declarator;
4856 cp_declarator *outer_declarator;
a723baf1 4857 const char *saved_message;
058b15c1 4858 tree type;
a723baf1
MM
4859
4860 /* The type-specifier sequence must not contain type definitions.
4861 (It cannot contain declarations of new types either, but if they
4862 are not definitions we will catch that because they are not
4863 complete.) */
4864 saved_message = parser->type_definition_forbidden_message;
4865 parser->type_definition_forbidden_message
4866 = "types may not be defined in a new-type-id";
4867 /* Parse the type-specifier-seq. */
62d1db17 4868 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
a723baf1
MM
4869 /* Restore the old message. */
4870 parser->type_definition_forbidden_message = saved_message;
4871 /* Parse the new-declarator. */
058b15c1
MM
4872 new_declarator = cp_parser_new_declarator_opt (parser);
4873
4874 /* Determine the number of elements in the last array dimension, if
4875 any. */
4876 *nelts = NULL_TREE;
4877 /* Skip down to the last array dimension. */
4878 declarator = new_declarator;
4879 outer_declarator = NULL;
4880 while (declarator && (declarator->kind == cdk_pointer
4881 || declarator->kind == cdk_ptrmem))
4882 {
4883 outer_declarator = declarator;
4884 declarator = declarator->declarator;
4885 }
98ca843c 4886 while (declarator
058b15c1
MM
4887 && declarator->kind == cdk_array
4888 && declarator->declarator
4889 && declarator->declarator->kind == cdk_array)
4890 {
4891 outer_declarator = declarator;
4892 declarator = declarator->declarator;
4893 }
98ca843c 4894
058b15c1
MM
4895 if (declarator && declarator->kind == cdk_array)
4896 {
4897 *nelts = declarator->u.array.bounds;
4898 if (*nelts == error_mark_node)
4899 *nelts = integer_one_node;
ad1063d5 4900
058b15c1
MM
4901 if (outer_declarator)
4902 outer_declarator->declarator = declarator->declarator;
4903 else
4904 new_declarator = NULL;
4905 }
a723baf1 4906
62d1db17 4907 type = groktypename (&type_specifier_seq, new_declarator);
058b15c1
MM
4908 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4909 {
4910 *nelts = array_type_nelts_top (type);
4911 type = TREE_TYPE (type);
4912 }
4913 return type;
a723baf1
MM
4914}
4915
4916/* Parse an (optional) new-declarator.
4917
4918 new-declarator:
4919 ptr-operator new-declarator [opt]
4920 direct-new-declarator
4921
058b15c1 4922 Returns the declarator. */
a723baf1 4923
058b15c1 4924static cp_declarator *
94edc4ab 4925cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4926{
4927 enum tree_code code;
4928 tree type;
3c01e5df 4929 cp_cv_quals cv_quals;
a723baf1
MM
4930
4931 /* We don't know if there's a ptr-operator next, or not. */
4932 cp_parser_parse_tentatively (parser);
4933 /* Look for a ptr-operator. */
3c01e5df 4934 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
a723baf1
MM
4935 /* If that worked, look for more new-declarators. */
4936 if (cp_parser_parse_definitely (parser))
4937 {
058b15c1 4938 cp_declarator *declarator;
a723baf1
MM
4939
4940 /* Parse another optional declarator. */
4941 declarator = cp_parser_new_declarator_opt (parser);
4942
4943 /* Create the representation of the declarator. */
058b15c1 4944 if (type)
3c01e5df 4945 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
058b15c1 4946 else if (code == INDIRECT_REF)
3c01e5df 4947 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 4948 else
3c01e5df 4949 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1 4950
a723baf1
MM
4951 return declarator;
4952 }
4953
4954 /* If the next token is a `[', there is a direct-new-declarator. */
4955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4956 return cp_parser_direct_new_declarator (parser);
4957
058b15c1 4958 return NULL;
a723baf1
MM
4959}
4960
4961/* Parse a direct-new-declarator.
4962
4963 direct-new-declarator:
4964 [ expression ]
21526606 4965 direct-new-declarator [constant-expression]
a723baf1 4966
058b15c1 4967 */
a723baf1 4968
058b15c1 4969static cp_declarator *
94edc4ab 4970cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1 4971{
058b15c1 4972 cp_declarator *declarator = NULL;
a723baf1
MM
4973
4974 while (true)
4975 {
4976 tree expression;
4977
4978 /* Look for the opening `['. */
4979 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4980 /* The first expression is not required to be constant. */
4981 if (!declarator)
4982 {
4983 expression = cp_parser_expression (parser);
4984 /* The standard requires that the expression have integral
4985 type. DR 74 adds enumeration types. We believe that the
4986 real intent is that these expressions be handled like the
4987 expression in a `switch' condition, which also allows
4988 classes with a single conversion to integral or
4989 enumeration type. */
4990 if (!processing_template_decl)
4991 {
21526606 4992 expression
a723baf1
MM
4993 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4994 expression,
b746c5dc 4995 /*complain=*/true);
a723baf1
MM
4996 if (!expression)
4997 {
2a13a625
GDR
4998 error ("expression in new-declarator must have integral "
4999 "or enumeration type");
a723baf1
MM
5000 expression = error_mark_node;
5001 }
5002 }
5003 }
5004 /* But all the other expressions must be. */
5005 else
21526606
EC
5006 expression
5007 = cp_parser_constant_expression (parser,
14d22dd6
MM
5008 /*allow_non_constant=*/false,
5009 NULL);
a723baf1
MM
5010 /* Look for the closing `]'. */
5011 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5012
5013 /* Add this bound to the declarator. */
058b15c1 5014 declarator = make_array_declarator (declarator, expression);
a723baf1
MM
5015
5016 /* If the next token is not a `[', then there are no more
5017 bounds. */
5018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5019 break;
5020 }
5021
5022 return declarator;
5023}
5024
5025/* Parse a new-initializer.
5026
5027 new-initializer:
5028 ( expression-list [opt] )
5029
34cd5ae7 5030 Returns a representation of the expression-list. If there is no
a723baf1
MM
5031 expression-list, VOID_ZERO_NODE is returned. */
5032
5033static tree
94edc4ab 5034cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
5035{
5036 tree expression_list;
5037
21526606 5038 expression_list = (cp_parser_parenthesized_expression_list
39703eb9 5039 (parser, false, /*non_constant_p=*/NULL));
7efa3e22 5040 if (!expression_list)
a723baf1 5041 expression_list = void_zero_node;
a723baf1
MM
5042
5043 return expression_list;
5044}
5045
5046/* Parse a delete-expression.
5047
5048 delete-expression:
5049 :: [opt] delete cast-expression
5050 :: [opt] delete [ ] cast-expression
5051
5052 Returns a representation of the expression. */
5053
5054static tree
94edc4ab 5055cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
5056{
5057 bool global_scope_p;
5058 bool array_p;
5059 tree expression;
5060
5061 /* Look for the optional `::' operator. */
5062 global_scope_p
5063 = (cp_parser_global_scope_opt (parser,
5064 /*current_scope_valid_p=*/false)
5065 != NULL_TREE);
5066 /* Look for the `delete' keyword. */
5067 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5068 /* See if the array syntax is in use. */
5069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5070 {
5071 /* Consume the `[' token. */
5072 cp_lexer_consume_token (parser->lexer);
5073 /* Look for the `]' token. */
5074 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5075 /* Remember that this is the `[]' construct. */
5076 array_p = true;
5077 }
5078 else
5079 array_p = false;
5080
5081 /* Parse the cast-expression. */
d6b4ea85 5082 expression = cp_parser_simple_cast_expression (parser);
a723baf1 5083
625cbf93
MM
5084 /* A delete-expression may not appear in an integral constant
5085 expression. */
5086 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5087 return error_mark_node;
5088
a723baf1
MM
5089 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5090}
5091
5092/* Parse a cast-expression.
5093
5094 cast-expression:
5095 unary-expression
5096 ( type-id ) cast-expression
5097
5098 Returns a representation of the expression. */
5099
5100static tree
5101cp_parser_cast_expression (cp_parser *parser, bool address_p)
5102{
5103 /* If it's a `(', then we might be looking at a cast. */
5104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5105 {
5106 tree type = NULL_TREE;
5107 tree expr = NULL_TREE;
5108 bool compound_literal_p;
5109 const char *saved_message;
5110
5111 /* There's no way to know yet whether or not this is a cast.
5112 For example, `(int (3))' is a unary-expression, while `(int)
5113 3' is a cast. So, we resort to parsing tentatively. */
5114 cp_parser_parse_tentatively (parser);
5115 /* Types may not be defined in a cast. */
5116 saved_message = parser->type_definition_forbidden_message;
5117 parser->type_definition_forbidden_message
5118 = "types may not be defined in casts";
5119 /* Consume the `('. */
5120 cp_lexer_consume_token (parser->lexer);
5121 /* A very tricky bit is that `(struct S) { 3 }' is a
5122 compound-literal (which we permit in C++ as an extension).
5123 But, that construct is not a cast-expression -- it is a
5124 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5125 is legal; if the compound-literal were a cast-expression,
5126 you'd need an extra set of parentheses.) But, if we parse
5127 the type-id, and it happens to be a class-specifier, then we
5128 will commit to the parse at that point, because we cannot
5129 undo the action that is done when creating a new class. So,
21526606 5130 then we cannot back up and do a postfix-expression.
a723baf1
MM
5131
5132 Therefore, we scan ahead to the closing `)', and check to see
5133 if the token after the `)' is a `{'. If so, we are not
21526606 5134 looking at a cast-expression.
a723baf1
MM
5135
5136 Save tokens so that we can put them back. */
5137 cp_lexer_save_tokens (parser->lexer);
5138 /* Skip tokens until the next token is a closing parenthesis.
5139 If we find the closing `)', and the next token is a `{', then
5140 we are looking at a compound-literal. */
21526606 5141 compound_literal_p
a668c6ad
MM
5142 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5143 /*consume_paren=*/true)
a723baf1
MM
5144 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5145 /* Roll back the tokens we skipped. */
5146 cp_lexer_rollback_tokens (parser->lexer);
5147 /* If we were looking at a compound-literal, simulate an error
5148 so that the call to cp_parser_parse_definitely below will
5149 fail. */
5150 if (compound_literal_p)
5151 cp_parser_simulate_error (parser);
5152 else
5153 {
4f8163b1
MM
5154 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5155 parser->in_type_id_in_expr_p = true;
a723baf1
MM
5156 /* Look for the type-id. */
5157 type = cp_parser_type_id (parser);
5158 /* Look for the closing `)'. */
5159 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4f8163b1 5160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1
MM
5161 }
5162
5163 /* Restore the saved message. */
5164 parser->type_definition_forbidden_message = saved_message;
5165
bbaab916
NS
5166 /* If ok so far, parse the dependent expression. We cannot be
5167 sure it is a cast. Consider `(T ())'. It is a parenthesized
5168 ctor of T, but looks like a cast to function returning T
5169 without a dependent expression. */
5170 if (!cp_parser_error_occurred (parser))
d6b4ea85 5171 expr = cp_parser_simple_cast_expression (parser);
bbaab916 5172
a723baf1
MM
5173 if (cp_parser_parse_definitely (parser))
5174 {
a723baf1 5175 /* Warn about old-style casts, if so requested. */
21526606
EC
5176 if (warn_old_style_cast
5177 && !in_system_header
5178 && !VOID_TYPE_P (type)
a723baf1
MM
5179 && current_lang_name != lang_name_c)
5180 warning ("use of old-style cast");
14d22dd6
MM
5181
5182 /* Only type conversions to integral or enumeration types
5183 can be used in constant-expressions. */
67c03833 5184 if (parser->integral_constant_expression_p
14d22dd6 5185 && !dependent_type_p (type)
625cbf93 5186 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
98ca843c 5187 && (cp_parser_non_integral_constant_expression
625cbf93
MM
5188 (parser,
5189 "a cast to a type other than an integral or "
5190 "enumeration type")))
5191 return error_mark_node;
5192
a723baf1
MM
5193 /* Perform the cast. */
5194 expr = build_c_cast (type, expr);
bbaab916 5195 return expr;
a723baf1 5196 }
a723baf1
MM
5197 }
5198
5199 /* If we get here, then it's not a cast, so it must be a
5200 unary-expression. */
5201 return cp_parser_unary_expression (parser, address_p);
5202}
5203
b8b94c5b 5204/* Parse a binary expression of the general form:
a723baf1
MM
5205
5206 pm-expression:
5207 cast-expression
5208 pm-expression .* cast-expression
5209 pm-expression ->* cast-expression
5210
77077b39 5211 multiplicative-expression:
a723baf1
MM
5212 pm-expression
5213 multiplicative-expression * pm-expression
5214 multiplicative-expression / pm-expression
5215 multiplicative-expression % pm-expression
5216
a723baf1
MM
5217 additive-expression:
5218 multiplicative-expression
5219 additive-expression + multiplicative-expression
5220 additive-expression - multiplicative-expression
5221
a723baf1
MM
5222 shift-expression:
5223 additive-expression
5224 shift-expression << additive-expression
5225 shift-expression >> additive-expression
5226
a723baf1
MM
5227 relational-expression:
5228 shift-expression
5229 relational-expression < shift-expression
5230 relational-expression > shift-expression
5231 relational-expression <= shift-expression
5232 relational-expression >= shift-expression
5233
b8b94c5b
PB
5234 GNU Extension:
5235
a723baf1
MM
5236 relational-expression:
5237 relational-expression <? shift-expression
5238 relational-expression >? shift-expression
5239
a723baf1
MM
5240 equality-expression:
5241 relational-expression
5242 equality-expression == relational-expression
5243 equality-expression != relational-expression
5244
a723baf1
MM
5245 and-expression:
5246 equality-expression
5247 and-expression & equality-expression
5248
a723baf1
MM
5249 exclusive-or-expression:
5250 and-expression
5251 exclusive-or-expression ^ and-expression
5252
b8b94c5b
PB
5253 inclusive-or-expression:
5254 exclusive-or-expression
5255 inclusive-or-expression | exclusive-or-expression
a723baf1 5256
b8b94c5b
PB
5257 logical-and-expression:
5258 inclusive-or-expression
5259 logical-and-expression && inclusive-or-expression
a723baf1 5260
b8b94c5b
PB
5261 logical-or-expression:
5262 logical-and-expression
5263 logical-or-expression || logical-and-expression
a723baf1 5264
b8b94c5b 5265 All these are implemented with a single function like:
a723baf1 5266
b8b94c5b
PB
5267 binary-expression:
5268 simple-cast-expression
5269 binary-expression <token> binary-expression
a723baf1 5270
b8b94c5b
PB
5271 The binops_by_token map is used to get the tree codes for each <token> type.
5272 binary-expressions are associated according to a precedence table. */
a723baf1 5273
b8b94c5b
PB
5274#define TOKEN_PRECEDENCE(token) \
5275 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5276 ? PREC_NOT_OPERATOR \
5277 : binops_by_token[token->type].prec)
a723baf1
MM
5278
5279static tree
b8b94c5b 5280cp_parser_binary_expression (cp_parser* parser)
a723baf1 5281{
b8b94c5b
PB
5282 cp_parser_expression_stack stack;
5283 cp_parser_expression_stack_entry *sp = &stack[0];
5284 tree lhs, rhs;
5285 cp_token *token;
5286 enum tree_code tree_type;
5287 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5288 bool overloaded_p;
a723baf1 5289
b8b94c5b
PB
5290 /* Parse the first expression. */
5291 lhs = cp_parser_simple_cast_expression (parser);
a723baf1 5292
b8b94c5b
PB
5293 for (;;)
5294 {
5295 /* Get an operator token. */
5296 token = cp_lexer_peek_token (parser->lexer);
5297 new_prec = TOKEN_PRECEDENCE (token);
5298
5299 /* Popping an entry off the stack means we completed a subexpression:
5300 - either we found a token which is not an operator (`>' where it is not
5301 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5302 will happen repeatedly;
5303 - or, we found an operator which has lower priority. This is the case
5304 where the recursive descent *ascends*, as in `3 * 4 + 5' after
03fd3f84 5305 parsing `3 * 4'. */
b8b94c5b
PB
5306 if (new_prec <= prec)
5307 {
5308 if (sp == stack)
5309 break;
5310 else
5311 goto pop;
5312 }
a723baf1 5313
b8b94c5b
PB
5314 get_rhs:
5315 tree_type = binops_by_token[token->type].tree_type;
a723baf1 5316
03fd3f84 5317 /* We used the operator token. */
b8b94c5b 5318 cp_lexer_consume_token (parser->lexer);
a723baf1 5319
b8b94c5b
PB
5320 /* Extract another operand. It may be the RHS of this expression
5321 or the LHS of a new, higher priority expression. */
5322 rhs = cp_parser_simple_cast_expression (parser);
a723baf1 5323
b8b94c5b
PB
5324 /* Get another operator token. Look up its precedence to avoid
5325 building a useless (immediately popped) stack entry for common
03fd3f84 5326 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
b8b94c5b
PB
5327 token = cp_lexer_peek_token (parser->lexer);
5328 lookahead_prec = TOKEN_PRECEDENCE (token);
5329 if (lookahead_prec > new_prec)
5330 {
5331 /* ... and prepare to parse the RHS of the new, higher priority
43c2a69a
PB
5332 expression. Since precedence levels on the stack are
5333 monotonically increasing, we do not have to care about
5334 stack overflows. */
b8b94c5b
PB
5335 sp->prec = prec;
5336 sp->tree_type = tree_type;
5337 sp->lhs = lhs;
5338 sp++;
5339 lhs = rhs;
5340 prec = new_prec;
5341 new_prec = lookahead_prec;
5342 goto get_rhs;
5343
5344 pop:
5345 /* If the stack is not empty, we have parsed into LHS the right side
5346 (`4' in the example above) of an expression we had suspended.
5347 We can use the information on the stack to recover the LHS (`3')
5348 from the stack together with the tree code (`MULT_EXPR'), and
5349 the precedence of the higher level subexpression
5350 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5351 which will be used to actually build the additive expression. */
5352 --sp;
5353 prec = sp->prec;
5354 tree_type = sp->tree_type;
5355 rhs = lhs;
5356 lhs = sp->lhs;
5357 }
a723baf1 5358
b8b94c5b
PB
5359 overloaded_p = false;
5360 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
a723baf1 5361
b8b94c5b
PB
5362 /* If the binary operator required the use of an overloaded operator,
5363 then this expression cannot be an integral constant-expression.
5364 An overloaded operator can be used even if both operands are
5365 otherwise permissible in an integral constant-expression if at
5366 least one of the operands is of enumeration type. */
a723baf1 5367
b8b94c5b
PB
5368 if (overloaded_p
5369 && (cp_parser_non_integral_constant_expression
5370 (parser, "calls to overloaded operators")))
5371 return error_mark_node;
5372 }
a723baf1 5373
b8b94c5b 5374 return lhs;
a723baf1
MM
5375}
5376
b8b94c5b 5377
a723baf1
MM
5378/* Parse the `? expression : assignment-expression' part of a
5379 conditional-expression. The LOGICAL_OR_EXPR is the
5380 logical-or-expression that started the conditional-expression.
5381 Returns a representation of the entire conditional-expression.
5382
39703eb9 5383 This routine is used by cp_parser_assignment_expression.
a723baf1
MM
5384
5385 ? expression : assignment-expression
21526606 5386
a723baf1 5387 GNU Extensions:
21526606 5388
a723baf1
MM
5389 ? : assignment-expression */
5390
5391static tree
94edc4ab 5392cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5393{
5394 tree expr;
5395 tree assignment_expr;
5396
5397 /* Consume the `?' token. */
5398 cp_lexer_consume_token (parser->lexer);
5399 if (cp_parser_allow_gnu_extensions_p (parser)
5400 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5401 /* Implicit true clause. */
5402 expr = NULL_TREE;
5403 else
5404 /* Parse the expression. */
5405 expr = cp_parser_expression (parser);
21526606 5406
a723baf1
MM
5407 /* The next token should be a `:'. */
5408 cp_parser_require (parser, CPP_COLON, "`:'");
5409 /* Parse the assignment-expression. */
5410 assignment_expr = cp_parser_assignment_expression (parser);
5411
5412 /* Build the conditional-expression. */
5413 return build_x_conditional_expr (logical_or_expr,
5414 expr,
5415 assignment_expr);
5416}
5417
5418/* Parse an assignment-expression.
5419
5420 assignment-expression:
5421 conditional-expression
5422 logical-or-expression assignment-operator assignment_expression
5423 throw-expression
5424
5425 Returns a representation for the expression. */
5426
5427static tree
94edc4ab 5428cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5429{
5430 tree expr;
5431
5432 /* If the next token is the `throw' keyword, then we're looking at
5433 a throw-expression. */
5434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5435 expr = cp_parser_throw_expression (parser);
5436 /* Otherwise, it must be that we are looking at a
5437 logical-or-expression. */
5438 else
5439 {
b8b94c5b
PB
5440 /* Parse the binary expressions (logical-or-expression). */
5441 expr = cp_parser_binary_expression (parser);
a723baf1
MM
5442 /* If the next token is a `?' then we're actually looking at a
5443 conditional-expression. */
5444 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5445 return cp_parser_question_colon_clause (parser, expr);
21526606 5446 else
a723baf1
MM
5447 {
5448 enum tree_code assignment_operator;
5449
5450 /* If it's an assignment-operator, we're using the second
5451 production. */
21526606 5452 assignment_operator
a723baf1
MM
5453 = cp_parser_assignment_operator_opt (parser);
5454 if (assignment_operator != ERROR_MARK)
5455 {
5456 tree rhs;
5457
5458 /* Parse the right-hand side of the assignment. */
5459 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5460 /* An assignment may not appear in a
5461 constant-expression. */
625cbf93
MM
5462 if (cp_parser_non_integral_constant_expression (parser,
5463 "an assignment"))
5464 return error_mark_node;
34cd5ae7 5465 /* Build the assignment expression. */
21526606
EC
5466 expr = build_x_modify_expr (expr,
5467 assignment_operator,
a723baf1
MM
5468 rhs);
5469 }
5470 }
5471 }
5472
5473 return expr;
5474}
5475
5476/* Parse an (optional) assignment-operator.
5477
21526606
EC
5478 assignment-operator: one of
5479 = *= /= %= += -= >>= <<= &= ^= |=
a723baf1
MM
5480
5481 GNU Extension:
21526606 5482
a723baf1
MM
5483 assignment-operator: one of
5484 <?= >?=
5485
5486 If the next token is an assignment operator, the corresponding tree
5487 code is returned, and the token is consumed. For example, for
5488 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5489 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5490 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5491 operator, ERROR_MARK is returned. */
5492
5493static enum tree_code
94edc4ab 5494cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5495{
5496 enum tree_code op;
5497 cp_token *token;
5498
5499 /* Peek at the next toen. */
5500 token = cp_lexer_peek_token (parser->lexer);
5501
5502 switch (token->type)
5503 {
5504 case CPP_EQ:
5505 op = NOP_EXPR;
5506 break;
5507
5508 case CPP_MULT_EQ:
5509 op = MULT_EXPR;
5510 break;
5511
5512 case CPP_DIV_EQ:
5513 op = TRUNC_DIV_EXPR;
5514 break;
5515
5516 case CPP_MOD_EQ:
5517 op = TRUNC_MOD_EXPR;
5518 break;
5519
5520 case CPP_PLUS_EQ:
5521 op = PLUS_EXPR;
5522 break;
5523
5524 case CPP_MINUS_EQ:
5525 op = MINUS_EXPR;
5526 break;
5527
5528 case CPP_RSHIFT_EQ:
5529 op = RSHIFT_EXPR;
5530 break;
5531
5532 case CPP_LSHIFT_EQ:
5533 op = LSHIFT_EXPR;
5534 break;
5535
5536 case CPP_AND_EQ:
5537 op = BIT_AND_EXPR;
5538 break;
5539
5540 case CPP_XOR_EQ:
5541 op = BIT_XOR_EXPR;
5542 break;
5543
5544 case CPP_OR_EQ:
5545 op = BIT_IOR_EXPR;
5546 break;
5547
5548 case CPP_MIN_EQ:
5549 op = MIN_EXPR;
5550 break;
5551
5552 case CPP_MAX_EQ:
5553 op = MAX_EXPR;
5554 break;
5555
21526606 5556 default:
a723baf1
MM
5557 /* Nothing else is an assignment operator. */
5558 op = ERROR_MARK;
5559 }
5560
5561 /* If it was an assignment operator, consume it. */
5562 if (op != ERROR_MARK)
5563 cp_lexer_consume_token (parser->lexer);
5564
5565 return op;
5566}
5567
5568/* Parse an expression.
5569
5570 expression:
5571 assignment-expression
5572 expression , assignment-expression
5573
5574 Returns a representation of the expression. */
5575
5576static tree
94edc4ab 5577cp_parser_expression (cp_parser* parser)
a723baf1
MM
5578{
5579 tree expression = NULL_TREE;
a723baf1
MM
5580
5581 while (true)
5582 {
5583 tree assignment_expression;
5584
5585 /* Parse the next assignment-expression. */
21526606 5586 assignment_expression
a723baf1
MM
5587 = cp_parser_assignment_expression (parser);
5588 /* If this is the first assignment-expression, we can just
5589 save it away. */
5590 if (!expression)
5591 expression = assignment_expression;
a723baf1 5592 else
d17811fd
MM
5593 expression = build_x_compound_expr (expression,
5594 assignment_expression);
a723baf1
MM
5595 /* If the next token is not a comma, then we are done with the
5596 expression. */
5597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5598 break;
5599 /* Consume the `,'. */
5600 cp_lexer_consume_token (parser->lexer);
14d22dd6 5601 /* A comma operator cannot appear in a constant-expression. */
625cbf93
MM
5602 if (cp_parser_non_integral_constant_expression (parser,
5603 "a comma operator"))
5604 expression = error_mark_node;
14d22dd6 5605 }
a723baf1
MM
5606
5607 return expression;
5608}
5609
21526606 5610/* Parse a constant-expression.
a723baf1
MM
5611
5612 constant-expression:
21526606 5613 conditional-expression
14d22dd6
MM
5614
5615 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
d17811fd
MM
5616 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5617 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5618 is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5619
5620static tree
21526606 5621cp_parser_constant_expression (cp_parser* parser,
14d22dd6
MM
5622 bool allow_non_constant_p,
5623 bool *non_constant_p)
a723baf1 5624{
67c03833
JM
5625 bool saved_integral_constant_expression_p;
5626 bool saved_allow_non_integral_constant_expression_p;
5627 bool saved_non_integral_constant_expression_p;
a723baf1
MM
5628 tree expression;
5629
5630 /* It might seem that we could simply parse the
5631 conditional-expression, and then check to see if it were
5632 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5633 one that the compiler can figure out is constant, possibly after
5634 doing some simplifications or optimizations. The standard has a
5635 precise definition of constant-expression, and we must honor
5636 that, even though it is somewhat more restrictive.
5637
5638 For example:
5639
5640 int i[(2, 3)];
5641
5642 is not a legal declaration, because `(2, 3)' is not a
5643 constant-expression. The `,' operator is forbidden in a
5644 constant-expression. However, GCC's constant-folding machinery
5645 will fold this operation to an INTEGER_CST for `3'. */
5646
14d22dd6 5647 /* Save the old settings. */
67c03833 5648 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
21526606 5649 saved_allow_non_integral_constant_expression_p
67c03833
JM
5650 = parser->allow_non_integral_constant_expression_p;
5651 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
a723baf1 5652 /* We are now parsing a constant-expression. */
67c03833
JM
5653 parser->integral_constant_expression_p = true;
5654 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5655 parser->non_integral_constant_expression_p = false;
39703eb9
MM
5656 /* Although the grammar says "conditional-expression", we parse an
5657 "assignment-expression", which also permits "throw-expression"
5658 and the use of assignment operators. In the case that
5659 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5660 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5661 actually essential that we look for an assignment-expression.
5662 For example, cp_parser_initializer_clauses uses this function to
5663 determine whether a particular assignment-expression is in fact
5664 constant. */
5665 expression = cp_parser_assignment_expression (parser);
14d22dd6 5666 /* Restore the old settings. */
67c03833 5667 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
21526606 5668 parser->allow_non_integral_constant_expression_p
67c03833 5669 = saved_allow_non_integral_constant_expression_p;
14d22dd6 5670 if (allow_non_constant_p)
67c03833
JM
5671 *non_constant_p = parser->non_integral_constant_expression_p;
5672 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
a723baf1
MM
5673
5674 return expression;
5675}
5676
7a3ea201
RH
5677/* Parse __builtin_offsetof.
5678
5679 offsetof-expression:
5680 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5681
5682 offsetof-member-designator:
5683 id-expression
5684 | offsetof-member-designator "." id-expression
5685 | offsetof-member-designator "[" expression "]"
5686*/
5687
5688static tree
5689cp_parser_builtin_offsetof (cp_parser *parser)
5690{
5691 int save_ice_p, save_non_ice_p;
5692 tree type, expr;
5693 cp_id_kind dummy;
5694
5695 /* We're about to accept non-integral-constant things, but will
5696 definitely yield an integral constant expression. Save and
5697 restore these values around our local parsing. */
5698 save_ice_p = parser->integral_constant_expression_p;
5699 save_non_ice_p = parser->non_integral_constant_expression_p;
5700
5701 /* Consume the "__builtin_offsetof" token. */
5702 cp_lexer_consume_token (parser->lexer);
5703 /* Consume the opening `('. */
5704 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5705 /* Parse the type-id. */
5706 type = cp_parser_type_id (parser);
5707 /* Look for the `,'. */
5708 cp_parser_require (parser, CPP_COMMA, "`,'");
5709
5710 /* Build the (type *)null that begins the traditional offsetof macro. */
5711 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5712
5713 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5714 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5715 true, &dummy);
5716 while (true)
5717 {
5718 cp_token *token = cp_lexer_peek_token (parser->lexer);
5719 switch (token->type)
5720 {
5721 case CPP_OPEN_SQUARE:
5722 /* offsetof-member-designator "[" expression "]" */
5723 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5724 break;
5725
5726 case CPP_DOT:
5727 /* offsetof-member-designator "." identifier */
5728 cp_lexer_consume_token (parser->lexer);
5729 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5730 true, &dummy);
5731 break;
5732
5733 case CPP_CLOSE_PAREN:
5734 /* Consume the ")" token. */
5735 cp_lexer_consume_token (parser->lexer);
5736 goto success;
5737
5738 default:
5739 /* Error. We know the following require will fail, but
5740 that gives the proper error message. */
5741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5742 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5743 expr = error_mark_node;
5744 goto failure;
5745 }
5746 }
5747
5748 success:
42c244d8
RH
5749 /* If we're processing a template, we can't finish the semantics yet.
5750 Otherwise we can fold the entire expression now. */
5751 if (processing_template_decl)
5752 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5753 else
5754 expr = fold_offsetof (expr);
7a3ea201
RH
5755
5756 failure:
5757 parser->integral_constant_expression_p = save_ice_p;
5758 parser->non_integral_constant_expression_p = save_non_ice_p;
5759
5760 return expr;
5761}
5762
a723baf1
MM
5763/* Statements [gram.stmt.stmt] */
5764
21526606 5765/* Parse a statement.
a723baf1
MM
5766
5767 statement:
5768 labeled-statement
5769 expression-statement
5770 compound-statement
5771 selection-statement
5772 iteration-statement
5773 jump-statement
5774 declaration-statement
5775 try-block */
5776
5777static void
325c3691 5778cp_parser_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5779{
5780 tree statement;
5781 cp_token *token;
93409b8c 5782 location_t statement_location;
a723baf1
MM
5783
5784 /* There is no statement yet. */
5785 statement = NULL_TREE;
5786 /* Peek at the next token. */
5787 token = cp_lexer_peek_token (parser->lexer);
6de9cd9a 5788 /* Remember the location of the first token in the statement. */
93409b8c 5789 statement_location = token->location;
a723baf1
MM
5790 /* If this is a keyword, then that will often determine what kind of
5791 statement we have. */
5792 if (token->type == CPP_KEYWORD)
5793 {
5794 enum rid keyword = token->keyword;
5795
5796 switch (keyword)
5797 {
5798 case RID_CASE:
5799 case RID_DEFAULT:
a5bcc582 5800 statement = cp_parser_labeled_statement (parser,
325c3691 5801 in_statement_expr);
a723baf1
MM
5802 break;
5803
5804 case RID_IF:
5805 case RID_SWITCH:
5806 statement = cp_parser_selection_statement (parser);
5807 break;
5808
5809 case RID_WHILE:
5810 case RID_DO:
5811 case RID_FOR:
5812 statement = cp_parser_iteration_statement (parser);
5813 break;
5814
5815 case RID_BREAK:
5816 case RID_CONTINUE:
5817 case RID_RETURN:
5818 case RID_GOTO:
5819 statement = cp_parser_jump_statement (parser);
5820 break;
5821
5822 case RID_TRY:
5823 statement = cp_parser_try_block (parser);
5824 break;
5825
5826 default:
5827 /* It might be a keyword like `int' that can start a
5828 declaration-statement. */
5829 break;
5830 }
5831 }
5832 else if (token->type == CPP_NAME)
5833 {
5834 /* If the next token is a `:', then we are looking at a
5835 labeled-statement. */
5836 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5837 if (token->type == CPP_COLON)
325c3691 5838 statement = cp_parser_labeled_statement (parser, in_statement_expr);
a723baf1
MM
5839 }
5840 /* Anything that starts with a `{' must be a compound-statement. */
5841 else if (token->type == CPP_OPEN_BRACE)
325c3691 5842 statement = cp_parser_compound_statement (parser, NULL, false);
36952dea
ZW
5843 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5844 a statement all its own. */
5845 else if (token->type == CPP_PRAGMA)
5846 {
5847 cp_lexer_handle_pragma (parser->lexer);
5848 return;
5849 }
a723baf1
MM
5850
5851 /* Everything else must be a declaration-statement or an
21526606 5852 expression-statement. Try for the declaration-statement
a723baf1
MM
5853 first, unless we are looking at a `;', in which case we know that
5854 we have an expression-statement. */
5855 if (!statement)
5856 {
5857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5858 {
5859 cp_parser_parse_tentatively (parser);
5860 /* Try to parse the declaration-statement. */
5861 cp_parser_declaration_statement (parser);
5862 /* If that worked, we're done. */
5863 if (cp_parser_parse_definitely (parser))
5864 return;
5865 }
5866 /* Look for an expression-statement instead. */
325c3691 5867 statement = cp_parser_expression_statement (parser, in_statement_expr);
a723baf1
MM
5868 }
5869
5870 /* Set the line number for the statement. */
009ed910 5871 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
93409b8c 5872 SET_EXPR_LOCATION (statement, statement_location);
a723baf1
MM
5873}
5874
5875/* Parse a labeled-statement.
5876
5877 labeled-statement:
5878 identifier : statement
5879 case constant-expression : statement
98ce043b
MM
5880 default : statement
5881
5882 GNU Extension:
21526606 5883
98ce043b
MM
5884 labeled-statement:
5885 case constant-expression ... constant-expression : statement
a723baf1 5886
8c161995
RH
5887 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5888 For an ordinary label, returns a LABEL_EXPR. */
a723baf1
MM
5889
5890static tree
325c3691 5891cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
5892{
5893 cp_token *token;
0e59b3fb 5894 tree statement = error_mark_node;
a723baf1
MM
5895
5896 /* The next token should be an identifier. */
5897 token = cp_lexer_peek_token (parser->lexer);
5898 if (token->type != CPP_NAME
5899 && token->type != CPP_KEYWORD)
5900 {
5901 cp_parser_error (parser, "expected labeled-statement");
5902 return error_mark_node;
5903 }
5904
5905 switch (token->keyword)
5906 {
5907 case RID_CASE:
5908 {
98ce043b
MM
5909 tree expr, expr_hi;
5910 cp_token *ellipsis;
a723baf1
MM
5911
5912 /* Consume the `case' token. */
5913 cp_lexer_consume_token (parser->lexer);
5914 /* Parse the constant-expression. */
21526606 5915 expr = cp_parser_constant_expression (parser,
d17811fd 5916 /*allow_non_constant_p=*/false,
14d22dd6 5917 NULL);
98ce043b
MM
5918
5919 ellipsis = cp_lexer_peek_token (parser->lexer);
5920 if (ellipsis->type == CPP_ELLIPSIS)
5921 {
5922 /* Consume the `...' token. */
5923 cp_lexer_consume_token (parser->lexer);
5924 expr_hi =
5925 cp_parser_constant_expression (parser,
5926 /*allow_non_constant_p=*/false,
5927 NULL);
5928 /* We don't need to emit warnings here, as the common code
5929 will do this for us. */
5930 }
5931 else
5932 expr_hi = NULL_TREE;
5933
0e59b3fb 5934 if (!parser->in_switch_statement_p)
2a13a625 5935 error ("case label %qE not within a switch statement", expr);
0e59b3fb 5936 else
98ce043b 5937 statement = finish_case_label (expr, expr_hi);
a723baf1
MM
5938 }
5939 break;
5940
5941 case RID_DEFAULT:
5942 /* Consume the `default' token. */
5943 cp_lexer_consume_token (parser->lexer);
0e59b3fb
MM
5944 if (!parser->in_switch_statement_p)
5945 error ("case label not within a switch statement");
5946 else
5947 statement = finish_case_label (NULL_TREE, NULL_TREE);
a723baf1
MM
5948 break;
5949
5950 default:
5951 /* Anything else must be an ordinary label. */
5952 statement = finish_label_stmt (cp_parser_identifier (parser));
5953 break;
5954 }
5955
5956 /* Require the `:' token. */
5957 cp_parser_require (parser, CPP_COLON, "`:'");
5958 /* Parse the labeled statement. */
325c3691 5959 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
5960
5961 /* Return the label, in the case of a `case' or `default' label. */
5962 return statement;
5963}
5964
5965/* Parse an expression-statement.
5966
5967 expression-statement:
5968 expression [opt] ;
5969
5970 Returns the new EXPR_STMT -- or NULL_TREE if the expression
a5bcc582
NS
5971 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5972 indicates whether this expression-statement is part of an
5973 expression statement. */
a723baf1
MM
5974
5975static tree
325c3691 5976cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
a723baf1 5977{
a5bcc582 5978 tree statement = NULL_TREE;
a723baf1 5979
a5bcc582 5980 /* If the next token is a ';', then there is no expression
04c06002 5981 statement. */
a723baf1 5982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
a5bcc582 5983 statement = cp_parser_expression (parser);
21526606 5984
a723baf1 5985 /* Consume the final `;'. */
e0860732 5986 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1 5987
325c3691 5988 if (in_statement_expr
a5bcc582
NS
5989 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5990 {
5991 /* This is the final expression statement of a statement
5992 expression. */
325c3691 5993 statement = finish_stmt_expr_expr (statement, in_statement_expr);
a5bcc582
NS
5994 }
5995 else if (statement)
5996 statement = finish_expr_stmt (statement);
5997 else
5998 finish_stmt ();
21526606 5999
a723baf1
MM
6000 return statement;
6001}
6002
6003/* Parse a compound-statement.
6004
6005 compound-statement:
6006 { statement-seq [opt] }
21526606 6007
5882f0f3 6008 Returns a tree representing the statement. */
a723baf1
MM
6009
6010static tree
325c3691
RH
6011cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6012 bool in_try)
a723baf1
MM
6013{
6014 tree compound_stmt;
6015
6016 /* Consume the `{'. */
6017 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6018 return error_mark_node;
6019 /* Begin the compound-statement. */
325c3691 6020 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
a723baf1 6021 /* Parse an (optional) statement-seq. */
325c3691 6022 cp_parser_statement_seq_opt (parser, in_statement_expr);
a723baf1 6023 /* Finish the compound-statement. */
7a3397c7 6024 finish_compound_stmt (compound_stmt);
a723baf1
MM
6025 /* Consume the `}'. */
6026 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6027
6028 return compound_stmt;
6029}
6030
6031/* Parse an (optional) statement-seq.
6032
6033 statement-seq:
6034 statement
6035 statement-seq [opt] statement */
6036
6037static void
325c3691 6038cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
a723baf1
MM
6039{
6040 /* Scan statements until there aren't any more. */
6041 while (true)
6042 {
6043 /* If we're looking at a `}', then we've run out of statements. */
6044 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6045 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6046 break;
6047
6048 /* Parse the statement. */
325c3691 6049 cp_parser_statement (parser, in_statement_expr);
a723baf1
MM
6050 }
6051}
6052
6053/* Parse a selection-statement.
6054
6055 selection-statement:
6056 if ( condition ) statement
6057 if ( condition ) statement else statement
21526606 6058 switch ( condition ) statement
a723baf1
MM
6059
6060 Returns the new IF_STMT or SWITCH_STMT. */
6061
6062static tree
94edc4ab 6063cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
6064{
6065 cp_token *token;
6066 enum rid keyword;
6067
6068 /* Peek at the next token. */
6069 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6070
6071 /* See what kind of keyword it is. */
6072 keyword = token->keyword;
6073 switch (keyword)
6074 {
6075 case RID_IF:
6076 case RID_SWITCH:
6077 {
6078 tree statement;
6079 tree condition;
6080
6081 /* Look for the `('. */
6082 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6083 {
6084 cp_parser_skip_to_end_of_statement (parser);
6085 return error_mark_node;
6086 }
6087
6088 /* Begin the selection-statement. */
6089 if (keyword == RID_IF)
6090 statement = begin_if_stmt ();
6091 else
6092 statement = begin_switch_stmt ();
6093
6094 /* Parse the condition. */
6095 condition = cp_parser_condition (parser);
6096 /* Look for the `)'. */
6097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
6098 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6099 /*consume_paren=*/true);
a723baf1
MM
6100
6101 if (keyword == RID_IF)
6102 {
a723baf1
MM
6103 /* Add the condition. */
6104 finish_if_stmt_cond (condition, statement);
6105
6106 /* Parse the then-clause. */
325c3691 6107 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6108 finish_then_clause (statement);
6109
6110 /* If the next token is `else', parse the else-clause. */
6111 if (cp_lexer_next_token_is_keyword (parser->lexer,
6112 RID_ELSE))
6113 {
a723baf1
MM
6114 /* Consume the `else' keyword. */
6115 cp_lexer_consume_token (parser->lexer);
325c3691 6116 begin_else_clause (statement);
a723baf1 6117 /* Parse the else-clause. */
325c3691 6118 cp_parser_implicitly_scoped_statement (parser);
a723baf1
MM
6119 finish_else_clause (statement);
6120 }
6121
6122 /* Now we're all done with the if-statement. */
325c3691 6123 finish_if_stmt (statement);
a723baf1
MM
6124 }
6125 else
6126 {
0e59b3fb 6127 bool in_switch_statement_p;
a723baf1
MM
6128
6129 /* Add the condition. */
6130 finish_switch_cond (condition, statement);
6131
6132 /* Parse the body of the switch-statement. */
0e59b3fb
MM
6133 in_switch_statement_p = parser->in_switch_statement_p;
6134 parser->in_switch_statement_p = true;
325c3691 6135 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6136 parser->in_switch_statement_p = in_switch_statement_p;
a723baf1
MM
6137
6138 /* Now we're all done with the switch-statement. */
6139 finish_switch_stmt (statement);
6140 }
6141
6142 return statement;
6143 }
6144 break;
6145
6146 default:
6147 cp_parser_error (parser, "expected selection-statement");
6148 return error_mark_node;
6149 }
6150}
6151
21526606 6152/* Parse a condition.
a723baf1
MM
6153
6154 condition:
6155 expression
21526606 6156 type-specifier-seq declarator = assignment-expression
a723baf1
MM
6157
6158 GNU Extension:
21526606 6159
a723baf1 6160 condition:
21526606 6161 type-specifier-seq declarator asm-specification [opt]
a723baf1 6162 attributes [opt] = assignment-expression
21526606 6163
a723baf1
MM
6164 Returns the expression that should be tested. */
6165
6166static tree
94edc4ab 6167cp_parser_condition (cp_parser* parser)
a723baf1 6168{
62d1db17 6169 cp_decl_specifier_seq type_specifiers;
a723baf1
MM
6170 const char *saved_message;
6171
6172 /* Try the declaration first. */
6173 cp_parser_parse_tentatively (parser);
6174 /* New types are not allowed in the type-specifier-seq for a
6175 condition. */
6176 saved_message = parser->type_definition_forbidden_message;
6177 parser->type_definition_forbidden_message
6178 = "types may not be defined in conditions";
6179 /* Parse the type-specifier-seq. */
62d1db17 6180 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
6181 /* Restore the saved message. */
6182 parser->type_definition_forbidden_message = saved_message;
6183 /* If all is well, we might be looking at a declaration. */
6184 if (!cp_parser_error_occurred (parser))
6185 {
6186 tree decl;
6187 tree asm_specification;
6188 tree attributes;
058b15c1 6189 cp_declarator *declarator;
a723baf1 6190 tree initializer = NULL_TREE;
21526606 6191
a723baf1 6192 /* Parse the declarator. */
62b8a44e 6193 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 6194 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
6195 /*parenthesized_p=*/NULL,
6196 /*member_p=*/false);
a723baf1
MM
6197 /* Parse the attributes. */
6198 attributes = cp_parser_attributes_opt (parser);
6199 /* Parse the asm-specification. */
6200 asm_specification = cp_parser_asm_specification_opt (parser);
6201 /* If the next token is not an `=', then we might still be
6202 looking at an expression. For example:
21526606 6203
a723baf1 6204 if (A(a).x)
21526606 6205
a723baf1
MM
6206 looks like a decl-specifier-seq and a declarator -- but then
6207 there is no `=', so this is an expression. */
6208 cp_parser_require (parser, CPP_EQ, "`='");
6209 /* If we did see an `=', then we are looking at a declaration
6210 for sure. */
6211 if (cp_parser_parse_definitely (parser))
6212 {
c162c75e 6213 bool pop_p;
73a8adb6 6214
a723baf1 6215 /* Create the declaration. */
62d1db17 6216 decl = start_decl (declarator, &type_specifiers,
a723baf1 6217 /*initialized_p=*/true,
73a8adb6
MM
6218 attributes, /*prefix_attributes=*/NULL_TREE,
6219 &pop_p);
a723baf1
MM
6220 /* Parse the assignment-expression. */
6221 initializer = cp_parser_assignment_expression (parser);
21526606 6222
a723baf1 6223 /* Process the initializer. */
21526606
EC
6224 cp_finish_decl (decl,
6225 initializer,
6226 asm_specification,
a723baf1 6227 LOOKUP_ONLYCONVERTING);
c162c75e 6228
73a8adb6
MM
6229 if (pop_p)
6230 pop_scope (DECL_CONTEXT (decl));
21526606 6231
a723baf1
MM
6232 return convert_from_reference (decl);
6233 }
6234 }
6235 /* If we didn't even get past the declarator successfully, we are
6236 definitely not looking at a declaration. */
6237 else
6238 cp_parser_abort_tentative_parse (parser);
6239
6240 /* Otherwise, we are looking at an expression. */
6241 return cp_parser_expression (parser);
6242}
6243
6244/* Parse an iteration-statement.
6245
6246 iteration-statement:
6247 while ( condition ) statement
6248 do statement while ( expression ) ;
6249 for ( for-init-statement condition [opt] ; expression [opt] )
6250 statement
6251
6252 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6253
6254static tree
94edc4ab 6255cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6256{
6257 cp_token *token;
6258 enum rid keyword;
6259 tree statement;
0e59b3fb
MM
6260 bool in_iteration_statement_p;
6261
a723baf1
MM
6262
6263 /* Peek at the next token. */
6264 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6265 if (!token)
6266 return error_mark_node;
6267
0e59b3fb 6268 /* Remember whether or not we are already within an iteration
21526606 6269 statement. */
0e59b3fb
MM
6270 in_iteration_statement_p = parser->in_iteration_statement_p;
6271
a723baf1
MM
6272 /* See what kind of keyword it is. */
6273 keyword = token->keyword;
6274 switch (keyword)
6275 {
6276 case RID_WHILE:
6277 {
6278 tree condition;
6279
6280 /* Begin the while-statement. */
6281 statement = begin_while_stmt ();
6282 /* Look for the `('. */
6283 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6284 /* Parse the condition. */
6285 condition = cp_parser_condition (parser);
6286 finish_while_stmt_cond (condition, statement);
6287 /* Look for the `)'. */
6288 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6289 /* Parse the dependent statement. */
0e59b3fb 6290 parser->in_iteration_statement_p = true;
a723baf1 6291 cp_parser_already_scoped_statement (parser);
0e59b3fb 6292 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6293 /* We're done with the while-statement. */
6294 finish_while_stmt (statement);
6295 }
6296 break;
6297
6298 case RID_DO:
6299 {
6300 tree expression;
6301
6302 /* Begin the do-statement. */
6303 statement = begin_do_stmt ();
6304 /* Parse the body of the do-statement. */
0e59b3fb 6305 parser->in_iteration_statement_p = true;
a723baf1 6306 cp_parser_implicitly_scoped_statement (parser);
0e59b3fb 6307 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6308 finish_do_body (statement);
6309 /* Look for the `while' keyword. */
6310 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6311 /* Look for the `('. */
6312 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6313 /* Parse the expression. */
6314 expression = cp_parser_expression (parser);
6315 /* We're done with the do-statement. */
6316 finish_do_stmt (expression, statement);
6317 /* Look for the `)'. */
6318 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6319 /* Look for the `;'. */
6320 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6321 }
6322 break;
6323
6324 case RID_FOR:
6325 {
6326 tree condition = NULL_TREE;
6327 tree expression = NULL_TREE;
6328
6329 /* Begin the for-statement. */
6330 statement = begin_for_stmt ();
6331 /* Look for the `('. */
6332 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6333 /* Parse the initialization. */
6334 cp_parser_for_init_statement (parser);
6335 finish_for_init_stmt (statement);
6336
6337 /* If there's a condition, process it. */
6338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6339 condition = cp_parser_condition (parser);
6340 finish_for_cond (condition, statement);
6341 /* Look for the `;'. */
6342 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6343
6344 /* If there's an expression, process it. */
6345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6346 expression = cp_parser_expression (parser);
6347 finish_for_expr (expression, statement);
6348 /* Look for the `)'. */
d5a10cf0 6349 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
98ca843c 6350
a723baf1 6351 /* Parse the body of the for-statement. */
0e59b3fb 6352 parser->in_iteration_statement_p = true;
a723baf1 6353 cp_parser_already_scoped_statement (parser);
0e59b3fb 6354 parser->in_iteration_statement_p = in_iteration_statement_p;
a723baf1
MM
6355
6356 /* We're done with the for-statement. */
6357 finish_for_stmt (statement);
6358 }
6359 break;
6360
6361 default:
6362 cp_parser_error (parser, "expected iteration-statement");
6363 statement = error_mark_node;
6364 break;
6365 }
6366
6367 return statement;
6368}
6369
6370/* Parse a for-init-statement.
6371
6372 for-init-statement:
6373 expression-statement
6374 simple-declaration */
6375
6376static void
94edc4ab 6377cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6378{
6379 /* If the next token is a `;', then we have an empty
34cd5ae7 6380 expression-statement. Grammatically, this is also a
a723baf1
MM
6381 simple-declaration, but an invalid one, because it does not
6382 declare anything. Therefore, if we did not handle this case
6383 specially, we would issue an error message about an invalid
6384 declaration. */
6385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6386 {
6387 /* We're going to speculatively look for a declaration, falling back
6388 to an expression, if necessary. */
6389 cp_parser_parse_tentatively (parser);
6390 /* Parse the declaration. */
6391 cp_parser_simple_declaration (parser,
6392 /*function_definition_allowed_p=*/false);
6393 /* If the tentative parse failed, then we shall need to look for an
6394 expression-statement. */
6395 if (cp_parser_parse_definitely (parser))
6396 return;
6397 }
6398
a5bcc582 6399 cp_parser_expression_statement (parser, false);
a723baf1
MM
6400}
6401
6402/* Parse a jump-statement.
6403
6404 jump-statement:
6405 break ;
6406 continue ;
6407 return expression [opt] ;
21526606 6408 goto identifier ;
a723baf1
MM
6409
6410 GNU extension:
6411
6412 jump-statement:
6413 goto * expression ;
6414
5088b058 6415 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
a723baf1
MM
6416
6417static tree
94edc4ab 6418cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6419{
6420 tree statement = error_mark_node;
6421 cp_token *token;
6422 enum rid keyword;
6423
6424 /* Peek at the next token. */
6425 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6426 if (!token)
6427 return error_mark_node;
6428
6429 /* See what kind of keyword it is. */
6430 keyword = token->keyword;
6431 switch (keyword)
6432 {
6433 case RID_BREAK:
0e59b3fb
MM
6434 if (!parser->in_switch_statement_p
6435 && !parser->in_iteration_statement_p)
6436 {
6437 error ("break statement not within loop or switch");
6438 statement = error_mark_node;
6439 }
6440 else
6441 statement = finish_break_stmt ();
2a13a625 6442 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6443 break;
6444
6445 case RID_CONTINUE:
0e59b3fb
MM
6446 if (!parser->in_iteration_statement_p)
6447 {
6448 error ("continue statement not within a loop");
6449 statement = error_mark_node;
6450 }
6451 else
6452 statement = finish_continue_stmt ();
2a13a625 6453 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6454 break;
6455
6456 case RID_RETURN:
6457 {
6458 tree expr;
6459
21526606 6460 /* If the next token is a `;', then there is no
a723baf1
MM
6461 expression. */
6462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6463 expr = cp_parser_expression (parser);
6464 else
6465 expr = NULL_TREE;
6466 /* Build the return-statement. */
6467 statement = finish_return_stmt (expr);
6468 /* Look for the final `;'. */
2a13a625 6469 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6470 }
6471 break;
6472
6473 case RID_GOTO:
6474 /* Create the goto-statement. */
6475 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6476 {
6477 /* Issue a warning about this use of a GNU extension. */
6478 if (pedantic)
6479 pedwarn ("ISO C++ forbids computed gotos");
6480 /* Consume the '*' token. */
6481 cp_lexer_consume_token (parser->lexer);
6482 /* Parse the dependent expression. */
6483 finish_goto_stmt (cp_parser_expression (parser));
6484 }
6485 else
6486 finish_goto_stmt (cp_parser_identifier (parser));
6487 /* Look for the final `;'. */
2a13a625 6488 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
a723baf1
MM
6489 break;
6490
6491 default:
6492 cp_parser_error (parser, "expected jump-statement");
6493 break;
6494 }
6495
6496 return statement;
6497}
6498
6499/* Parse a declaration-statement.
6500
6501 declaration-statement:
6502 block-declaration */
6503
6504static void
94edc4ab 6505cp_parser_declaration_statement (cp_parser* parser)
a723baf1 6506{
058b15c1
MM
6507 void *p;
6508
6509 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6510 p = obstack_alloc (&declarator_obstack, 0);
6511
6512 /* Parse the block-declaration. */
a723baf1
MM
6513 cp_parser_block_declaration (parser, /*statement_p=*/true);
6514
058b15c1
MM
6515 /* Free any declarators allocated. */
6516 obstack_free (&declarator_obstack, p);
6517
a723baf1
MM
6518 /* Finish off the statement. */
6519 finish_stmt ();
6520}
6521
6522/* Some dependent statements (like `if (cond) statement'), are
6523 implicitly in their own scope. In other words, if the statement is
6524 a single statement (as opposed to a compound-statement), it is
6525 none-the-less treated as if it were enclosed in braces. Any
6526 declarations appearing in the dependent statement are out of scope
6527 after control passes that point. This function parses a statement,
6528 but ensures that is in its own scope, even if it is not a
21526606 6529 compound-statement.
a723baf1
MM
6530
6531 Returns the new statement. */
6532
6533static tree
94edc4ab 6534cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6535{
6536 tree statement;
6537
6538 /* If the token is not a `{', then we must take special action. */
6539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6540 {
6541 /* Create a compound-statement. */
325c3691 6542 statement = begin_compound_stmt (0);
a723baf1 6543 /* Parse the dependent-statement. */
a5bcc582 6544 cp_parser_statement (parser, false);
a723baf1 6545 /* Finish the dummy compound-statement. */
7a3397c7 6546 finish_compound_stmt (statement);
a723baf1
MM
6547 }
6548 /* Otherwise, we simply parse the statement directly. */
6549 else
325c3691 6550 statement = cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
6551
6552 /* Return the statement. */
6553 return statement;
6554}
6555
6556/* For some dependent statements (like `while (cond) statement'), we
6557 have already created a scope. Therefore, even if the dependent
6558 statement is a compound-statement, we do not want to create another
6559 scope. */
6560
6561static void
94edc4ab 6562cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1 6563{
325c3691
RH
6564 /* If the token is a `{', then we must take special action. */
6565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6566 cp_parser_statement (parser, false);
6567 else
a723baf1 6568 {
325c3691
RH
6569 /* Avoid calling cp_parser_compound_statement, so that we
6570 don't create a new scope. Do everything else by hand. */
6571 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6572 cp_parser_statement_seq_opt (parser, false);
6573 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1 6574 }
a723baf1
MM
6575}
6576
6577/* Declarations [gram.dcl.dcl] */
6578
6579/* Parse an optional declaration-sequence.
6580
6581 declaration-seq:
6582 declaration
6583 declaration-seq declaration */
6584
6585static void
94edc4ab 6586cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6587{
6588 while (true)
6589 {
6590 cp_token *token;
6591
6592 token = cp_lexer_peek_token (parser->lexer);
6593
6594 if (token->type == CPP_CLOSE_BRACE
6595 || token->type == CPP_EOF)
6596 break;
6597
21526606 6598 if (token->type == CPP_SEMICOLON)
a723baf1
MM
6599 {
6600 /* A declaration consisting of a single semicolon is
6601 invalid. Allow it unless we're being pedantic. */
a723baf1 6602 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
6603 if (pedantic && !in_system_header)
6604 pedwarn ("extra %<;%>");
a723baf1
MM
6605 continue;
6606 }
6607
7d381002 6608 /* If we're entering or exiting a region that's implicitly
03fd3f84 6609 extern "C", modify the lang context appropriately. */
7d381002
MA
6610 if (!parser->implicit_extern_c && token->implicit_extern_c)
6611 {
6612 push_lang_context (lang_name_c);
6613 parser->implicit_extern_c = true;
6614 }
6615 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6616 {
6617 pop_lang_context ();
6618 parser->implicit_extern_c = false;
6619 }
6620
36952dea
ZW
6621 if (token->type == CPP_PRAGMA)
6622 {
6623 /* A top-level declaration can consist solely of a #pragma.
6624 A nested declaration cannot, so this is done here and not
6625 in cp_parser_declaration. (A #pragma at block scope is
6626 handled in cp_parser_statement.) */
6627 cp_lexer_handle_pragma (parser->lexer);
6628 continue;
6629 }
6630
c838d82f 6631 /* Parse the declaration itself. */
a723baf1
MM
6632 cp_parser_declaration (parser);
6633 }
6634}
6635
6636/* Parse a declaration.
6637
6638 declaration:
6639 block-declaration
6640 function-definition
6641 template-declaration
6642 explicit-instantiation
6643 explicit-specialization
6644 linkage-specification
21526606 6645 namespace-definition
1092805d
MM
6646
6647 GNU extension:
6648
6649 declaration:
6650 __extension__ declaration */
a723baf1
MM
6651
6652static void
94edc4ab 6653cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6654{
6655 cp_token token1;
6656 cp_token token2;
1092805d 6657 int saved_pedantic;
058b15c1 6658 void *p;
1092805d
MM
6659
6660 /* Check for the `__extension__' keyword. */
6661 if (cp_parser_extension_opt (parser, &saved_pedantic))
6662 {
6663 /* Parse the qualified declaration. */
6664 cp_parser_declaration (parser);
6665 /* Restore the PEDANTIC flag. */
6666 pedantic = saved_pedantic;
6667
6668 return;
6669 }
a723baf1
MM
6670
6671 /* Try to figure out what kind of declaration is present. */
6672 token1 = *cp_lexer_peek_token (parser->lexer);
21526606 6673
a723baf1
MM
6674 if (token1.type != CPP_EOF)
6675 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6676
058b15c1
MM
6677 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6678 p = obstack_alloc (&declarator_obstack, 0);
6679
a723baf1
MM
6680 /* If the next token is `extern' and the following token is a string
6681 literal, then we have a linkage specification. */
6682 if (token1.keyword == RID_EXTERN
6683 && cp_parser_is_string_literal (&token2))
6684 cp_parser_linkage_specification (parser);
6685 /* If the next token is `template', then we have either a template
6686 declaration, an explicit instantiation, or an explicit
6687 specialization. */
6688 else if (token1.keyword == RID_TEMPLATE)
6689 {
6690 /* `template <>' indicates a template specialization. */
6691 if (token2.type == CPP_LESS
6692 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6693 cp_parser_explicit_specialization (parser);
6694 /* `template <' indicates a template declaration. */
6695 else if (token2.type == CPP_LESS)
6696 cp_parser_template_declaration (parser, /*member_p=*/false);
6697 /* Anything else must be an explicit instantiation. */
6698 else
6699 cp_parser_explicit_instantiation (parser);
6700 }
6701 /* If the next token is `export', then we have a template
6702 declaration. */
6703 else if (token1.keyword == RID_EXPORT)
6704 cp_parser_template_declaration (parser, /*member_p=*/false);
6705 /* If the next token is `extern', 'static' or 'inline' and the one
6706 after that is `template', we have a GNU extended explicit
6707 instantiation directive. */
6708 else if (cp_parser_allow_gnu_extensions_p (parser)
6709 && (token1.keyword == RID_EXTERN
6710 || token1.keyword == RID_STATIC
6711 || token1.keyword == RID_INLINE)
6712 && token2.keyword == RID_TEMPLATE)
6713 cp_parser_explicit_instantiation (parser);
6714 /* If the next token is `namespace', check for a named or unnamed
6715 namespace definition. */
6716 else if (token1.keyword == RID_NAMESPACE
6717 && (/* A named namespace definition. */
6718 (token2.type == CPP_NAME
21526606 6719 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
a723baf1
MM
6720 == CPP_OPEN_BRACE))
6721 /* An unnamed namespace definition. */
6722 || token2.type == CPP_OPEN_BRACE))
6723 cp_parser_namespace_definition (parser);
6724 /* We must have either a block declaration or a function
6725 definition. */
6726 else
6727 /* Try to parse a block-declaration, or a function-definition. */
6728 cp_parser_block_declaration (parser, /*statement_p=*/false);
058b15c1
MM
6729
6730 /* Free any declarators allocated. */
6731 obstack_free (&declarator_obstack, p);
a723baf1
MM
6732}
6733
21526606 6734/* Parse a block-declaration.
a723baf1
MM
6735
6736 block-declaration:
6737 simple-declaration
6738 asm-definition
6739 namespace-alias-definition
6740 using-declaration
21526606 6741 using-directive
a723baf1
MM
6742
6743 GNU Extension:
6744
6745 block-declaration:
21526606 6746 __extension__ block-declaration
a723baf1
MM
6747 label-declaration
6748
34cd5ae7 6749 If STATEMENT_P is TRUE, then this block-declaration is occurring as
a723baf1
MM
6750 part of a declaration-statement. */
6751
6752static void
21526606 6753cp_parser_block_declaration (cp_parser *parser,
a723baf1
MM
6754 bool statement_p)
6755{
6756 cp_token *token1;
6757 int saved_pedantic;
6758
6759 /* Check for the `__extension__' keyword. */
6760 if (cp_parser_extension_opt (parser, &saved_pedantic))
6761 {
6762 /* Parse the qualified declaration. */
6763 cp_parser_block_declaration (parser, statement_p);
6764 /* Restore the PEDANTIC flag. */
6765 pedantic = saved_pedantic;
6766
6767 return;
6768 }
6769
6770 /* Peek at the next token to figure out which kind of declaration is
6771 present. */
6772 token1 = cp_lexer_peek_token (parser->lexer);
6773
6774 /* If the next keyword is `asm', we have an asm-definition. */
6775 if (token1->keyword == RID_ASM)
6776 {
6777 if (statement_p)
6778 cp_parser_commit_to_tentative_parse (parser);
6779 cp_parser_asm_definition (parser);
6780 }
6781 /* If the next keyword is `namespace', we have a
6782 namespace-alias-definition. */
6783 else if (token1->keyword == RID_NAMESPACE)
6784 cp_parser_namespace_alias_definition (parser);
6785 /* If the next keyword is `using', we have either a
6786 using-declaration or a using-directive. */
6787 else if (token1->keyword == RID_USING)
6788 {
6789 cp_token *token2;
6790
6791 if (statement_p)
6792 cp_parser_commit_to_tentative_parse (parser);
6793 /* If the token after `using' is `namespace', then we have a
6794 using-directive. */
6795 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6796 if (token2->keyword == RID_NAMESPACE)
6797 cp_parser_using_directive (parser);
6798 /* Otherwise, it's a using-declaration. */
6799 else
6800 cp_parser_using_declaration (parser);
6801 }
6802 /* If the next keyword is `__label__' we have a label declaration. */
6803 else if (token1->keyword == RID_LABEL)
6804 {
6805 if (statement_p)
6806 cp_parser_commit_to_tentative_parse (parser);
6807 cp_parser_label_declaration (parser);
6808 }
6809 /* Anything else must be a simple-declaration. */
6810 else
6811 cp_parser_simple_declaration (parser, !statement_p);
6812}
6813
6814/* Parse a simple-declaration.
6815
6816 simple-declaration:
21526606 6817 decl-specifier-seq [opt] init-declarator-list [opt] ;
a723baf1
MM
6818
6819 init-declarator-list:
6820 init-declarator
21526606 6821 init-declarator-list , init-declarator
a723baf1 6822
34cd5ae7 6823 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9bcb9aae 6824 function-definition as a simple-declaration. */
a723baf1
MM
6825
6826static void
21526606 6827cp_parser_simple_declaration (cp_parser* parser,
94edc4ab 6828 bool function_definition_allowed_p)
a723baf1 6829{
62d1db17 6830 cp_decl_specifier_seq decl_specifiers;
560ad596 6831 int declares_class_or_enum;
a723baf1
MM
6832 bool saw_declarator;
6833
6834 /* Defer access checks until we know what is being declared; the
6835 checks for names appearing in the decl-specifier-seq should be
6836 done as if we were in the scope of the thing being declared. */
8d241e0b 6837 push_deferring_access_checks (dk_deferred);
cf22909c 6838
a723baf1
MM
6839 /* Parse the decl-specifier-seq. We have to keep track of whether
6840 or not the decl-specifier-seq declares a named class or
6841 enumeration type, since that is the only case in which the
21526606 6842 init-declarator-list is allowed to be empty.
a723baf1
MM
6843
6844 [dcl.dcl]
6845
6846 In a simple-declaration, the optional init-declarator-list can be
6847 omitted only when declaring a class or enumeration, that is when
6848 the decl-specifier-seq contains either a class-specifier, an
6849 elaborated-type-specifier, or an enum-specifier. */
62d1db17
MM
6850 cp_parser_decl_specifier_seq (parser,
6851 CP_PARSER_FLAGS_OPTIONAL,
6852 &decl_specifiers,
6853 &declares_class_or_enum);
a723baf1 6854 /* We no longer need to defer access checks. */
cf22909c 6855 stop_deferring_access_checks ();
24c0ef37 6856
39703eb9
MM
6857 /* In a block scope, a valid declaration must always have a
6858 decl-specifier-seq. By not trying to parse declarators, we can
6859 resolve the declaration/expression ambiguity more quickly. */
98ca843c 6860 if (!function_definition_allowed_p
62d1db17 6861 && !decl_specifiers.any_specifiers_p)
39703eb9
MM
6862 {
6863 cp_parser_error (parser, "expected declaration");
6864 goto done;
6865 }
6866
8fbc5ae7
MM
6867 /* If the next two tokens are both identifiers, the code is
6868 erroneous. The usual cause of this situation is code like:
6869
6870 T t;
6871
6872 where "T" should name a type -- but does not. */
de3fe73c
MM
6873 if (!decl_specifiers.type
6874 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 6875 {
8d241e0b 6876 /* If parsing tentatively, we should commit; we really are
8fbc5ae7
MM
6877 looking at a declaration. */
6878 cp_parser_commit_to_tentative_parse (parser);
6879 /* Give up. */
39703eb9 6880 goto done;
8fbc5ae7 6881 }
996c2b52
MM
6882
6883 /* If we have seen at least one decl-specifier, and the next token
6884 is not a parenthesis, then we must be looking at a declaration.
6885 (After "int (" we might be looking at a functional cast.) */
6886 if (decl_specifiers.any_specifiers_p
6887 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6888 cp_parser_commit_to_tentative_parse (parser);
8fbc5ae7 6889
a723baf1
MM
6890 /* Keep going until we hit the `;' at the end of the simple
6891 declaration. */
6892 saw_declarator = false;
21526606 6893 while (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
6894 CPP_SEMICOLON))
6895 {
6896 cp_token *token;
6897 bool function_definition_p;
560ad596 6898 tree decl;
a723baf1
MM
6899
6900 saw_declarator = true;
6901 /* Parse the init-declarator. */
62d1db17 6902 decl = cp_parser_init_declarator (parser, &decl_specifiers,
560ad596
MM
6903 function_definition_allowed_p,
6904 /*member_p=*/false,
6905 declares_class_or_enum,
6906 &function_definition_p);
1fb3244a
MM
6907 /* If an error occurred while parsing tentatively, exit quickly.
6908 (That usually happens when in the body of a function; each
6909 statement is treated as a declaration-statement until proven
6910 otherwise.) */
6911 if (cp_parser_error_occurred (parser))
39703eb9 6912 goto done;
a723baf1
MM
6913 /* Handle function definitions specially. */
6914 if (function_definition_p)
6915 {
6916 /* If the next token is a `,', then we are probably
6917 processing something like:
6918
6919 void f() {}, *p;
6920
6921 which is erroneous. */
6922 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6923 error ("mixing declarations and function-definitions is forbidden");
6924 /* Otherwise, we're done with the list of declarators. */
6925 else
24c0ef37 6926 {
cf22909c 6927 pop_deferring_access_checks ();
24c0ef37
GS
6928 return;
6929 }
a723baf1
MM
6930 }
6931 /* The next token should be either a `,' or a `;'. */
6932 token = cp_lexer_peek_token (parser->lexer);
6933 /* If it's a `,', there are more declarators to come. */
6934 if (token->type == CPP_COMMA)
6935 cp_lexer_consume_token (parser->lexer);
6936 /* If it's a `;', we are done. */
6937 else if (token->type == CPP_SEMICOLON)
6938 break;
6939 /* Anything else is an error. */
6940 else
6941 {
996c2b52
MM
6942 /* If we have already issued an error message we don't need
6943 to issue another one. */
6944 if (decl != error_mark_node
0b16f8f4 6945 || cp_parser_uncommitted_to_tentative_parse_p (parser))
2a13a625 6946 cp_parser_error (parser, "expected %<,%> or %<;%>");
a723baf1
MM
6947 /* Skip tokens until we reach the end of the statement. */
6948 cp_parser_skip_to_end_of_statement (parser);
5a98fa7b
MM
6949 /* If the next token is now a `;', consume it. */
6950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6951 cp_lexer_consume_token (parser->lexer);
39703eb9 6952 goto done;
a723baf1
MM
6953 }
6954 /* After the first time around, a function-definition is not
6955 allowed -- even if it was OK at first. For example:
6956
6957 int i, f() {}
6958
6959 is not valid. */
6960 function_definition_allowed_p = false;
6961 }
6962
6963 /* Issue an error message if no declarators are present, and the
6964 decl-specifier-seq does not itself declare a class or
6965 enumeration. */
6966 if (!saw_declarator)
6967 {
6968 if (cp_parser_declares_only_class_p (parser))
62d1db17 6969 shadow_tag (&decl_specifiers);
a723baf1 6970 /* Perform any deferred access checks. */
cf22909c 6971 perform_deferred_access_checks ();
a723baf1
MM
6972 }
6973
6974 /* Consume the `;'. */
6975 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6976
39703eb9
MM
6977 done:
6978 pop_deferring_access_checks ();
a723baf1
MM
6979}
6980
6981/* Parse a decl-specifier-seq.
6982
6983 decl-specifier-seq:
6984 decl-specifier-seq [opt] decl-specifier
6985
6986 decl-specifier:
6987 storage-class-specifier
6988 type-specifier
6989 function-specifier
6990 friend
21526606 6991 typedef
a723baf1
MM
6992
6993 GNU Extension:
6994
15077df5
MM
6995 decl-specifier:
6996 attributes
a723baf1 6997
62d1db17 6998 Set *DECL_SPECS to a representation of the decl-specifier-seq.
a723baf1 6999
eb1aef53 7000 The parser flags FLAGS is used to control type-specifier parsing.
560ad596
MM
7001
7002 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
543ca912 7003 flags:
560ad596
MM
7004
7005 1: one of the decl-specifiers is an elaborated-type-specifier
543ca912 7006 (i.e., a type declaration)
560ad596 7007 2: one of the decl-specifiers is an enum-specifier or a
543ca912 7008 class-specifier (i.e., a type definition)
98ca843c 7009
560ad596 7010 */
a723baf1 7011
62d1db17 7012static void
21526606 7013cp_parser_decl_specifier_seq (cp_parser* parser,
62d1db17
MM
7014 cp_parser_flags flags,
7015 cp_decl_specifier_seq *decl_specs,
560ad596 7016 int* declares_class_or_enum)
a723baf1 7017{
f2ce60b8 7018 bool constructor_possible_p = !parser->in_declarator_p;
21526606 7019
62d1db17
MM
7020 /* Clear DECL_SPECS. */
7021 clear_decl_specs (decl_specs);
7022
a723baf1 7023 /* Assume no class or enumeration type is declared. */
560ad596 7024 *declares_class_or_enum = 0;
a723baf1 7025
a723baf1
MM
7026 /* Keep reading specifiers until there are no more to read. */
7027 while (true)
7028 {
a723baf1 7029 bool constructor_p;
62d1db17 7030 bool found_decl_spec;
a723baf1
MM
7031 cp_token *token;
7032
7033 /* Peek at the next token. */
7034 token = cp_lexer_peek_token (parser->lexer);
7035 /* Handle attributes. */
7036 if (token->keyword == RID_ATTRIBUTE)
7037 {
7038 /* Parse the attributes. */
98ca843c 7039 decl_specs->attributes
62d1db17
MM
7040 = chainon (decl_specs->attributes,
7041 cp_parser_attributes_opt (parser));
a723baf1
MM
7042 continue;
7043 }
62d1db17
MM
7044 /* Assume we will find a decl-specifier keyword. */
7045 found_decl_spec = true;
a723baf1
MM
7046 /* If the next token is an appropriate keyword, we can simply
7047 add it to the list. */
7048 switch (token->keyword)
7049 {
a723baf1
MM
7050 /* decl-specifier:
7051 friend */
62d1db17
MM
7052 case RID_FRIEND:
7053 if (decl_specs->specs[(int) ds_friend]++)
2a13a625 7054 error ("duplicate %<friend%>");
a723baf1
MM
7055 /* Consume the token. */
7056 cp_lexer_consume_token (parser->lexer);
7057 break;
7058
7059 /* function-specifier:
7060 inline
7061 virtual
7062 explicit */
7063 case RID_INLINE:
7064 case RID_VIRTUAL:
7065 case RID_EXPLICIT:
62d1db17 7066 cp_parser_function_specifier_opt (parser, decl_specs);
a723baf1 7067 break;
21526606 7068
a723baf1
MM
7069 /* decl-specifier:
7070 typedef */
7071 case RID_TYPEDEF:
62d1db17 7072 ++decl_specs->specs[(int) ds_typedef];
a723baf1
MM
7073 /* Consume the token. */
7074 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
7075 /* A constructor declarator cannot appear in a typedef. */
7076 constructor_possible_p = false;
c006d942
MM
7077 /* The "typedef" keyword can only occur in a declaration; we
7078 may as well commit at this point. */
7079 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
7080 break;
7081
7082 /* storage-class-specifier:
7083 auto
7084 register
7085 static
7086 extern
21526606 7087 mutable
a723baf1
MM
7088
7089 GNU Extension:
7090 thread */
7091 case RID_AUTO:
62d1db17
MM
7092 /* Consume the token. */
7093 cp_lexer_consume_token (parser->lexer);
7094 cp_parser_set_storage_class (decl_specs, sc_auto);
7095 break;
a723baf1 7096 case RID_REGISTER:
62d1db17
MM
7097 /* Consume the token. */
7098 cp_lexer_consume_token (parser->lexer);
7099 cp_parser_set_storage_class (decl_specs, sc_register);
7100 break;
a723baf1 7101 case RID_STATIC:
62d1db17
MM
7102 /* Consume the token. */
7103 cp_lexer_consume_token (parser->lexer);
7104 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7105 {
2d01edd7 7106 error ("%<__thread%> before %<static%>");
f1b90a04
MM
7107 decl_specs->specs[(int) ds_thread] = 0;
7108 }
7109 cp_parser_set_storage_class (decl_specs, sc_static);
62d1db17 7110 break;
a723baf1 7111 case RID_EXTERN:
62d1db17
MM
7112 /* Consume the token. */
7113 cp_lexer_consume_token (parser->lexer);
7114 if (decl_specs->specs[(int) ds_thread])
f1b90a04 7115 {
2d01edd7 7116 error ("%<__thread%> before %<extern%>");
f1b90a04
MM
7117 decl_specs->specs[(int) ds_thread] = 0;
7118 }
7119 cp_parser_set_storage_class (decl_specs, sc_extern);
62d1db17 7120 break;
a723baf1 7121 case RID_MUTABLE:
62d1db17
MM
7122 /* Consume the token. */
7123 cp_lexer_consume_token (parser->lexer);
7124 cp_parser_set_storage_class (decl_specs, sc_mutable);
7125 break;
a723baf1 7126 case RID_THREAD:
62d1db17
MM
7127 /* Consume the token. */
7128 cp_lexer_consume_token (parser->lexer);
7129 ++decl_specs->specs[(int) ds_thread];
a723baf1 7130 break;
21526606 7131
a723baf1 7132 default:
62d1db17
MM
7133 /* We did not yet find a decl-specifier yet. */
7134 found_decl_spec = false;
a723baf1
MM
7135 break;
7136 }
7137
7138 /* Constructors are a special case. The `S' in `S()' is not a
7139 decl-specifier; it is the beginning of the declarator. */
98ca843c 7140 constructor_p
62d1db17
MM
7141 = (!found_decl_spec
7142 && constructor_possible_p
98ca843c 7143 && (cp_parser_constructor_declarator_p
62d1db17 7144 (parser, decl_specs->specs[(int) ds_friend] != 0)));
a723baf1
MM
7145
7146 /* If we don't have a DECL_SPEC yet, then we must be looking at
7147 a type-specifier. */
62d1db17 7148 if (!found_decl_spec && !constructor_p)
a723baf1 7149 {
560ad596 7150 int decl_spec_declares_class_or_enum;
a723baf1 7151 bool is_cv_qualifier;
62d1db17 7152 tree type_spec;
a723baf1 7153
62d1db17 7154 type_spec
a723baf1 7155 = cp_parser_type_specifier (parser, flags,
62d1db17 7156 decl_specs,
a723baf1
MM
7157 /*is_declaration=*/true,
7158 &decl_spec_declares_class_or_enum,
7159 &is_cv_qualifier);
7160
7161 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7162
7163 /* If this type-specifier referenced a user-defined type
7164 (a typedef, class-name, etc.), then we can't allow any
7165 more such type-specifiers henceforth.
7166
7167 [dcl.spec]
7168
7169 The longest sequence of decl-specifiers that could
7170 possibly be a type name is taken as the
7171 decl-specifier-seq of a declaration. The sequence shall
7172 be self-consistent as described below.
7173
7174 [dcl.type]
7175
7176 As a general rule, at most one type-specifier is allowed
7177 in the complete decl-specifier-seq of a declaration. The
7178 only exceptions are the following:
7179
7180 -- const or volatile can be combined with any other
21526606 7181 type-specifier.
a723baf1
MM
7182
7183 -- signed or unsigned can be combined with char, long,
7184 short, or int.
7185
7186 -- ..
7187
7188 Example:
7189
7190 typedef char* Pc;
7191 void g (const int Pc);
7192
7193 Here, Pc is *not* part of the decl-specifier seq; it's
7194 the declarator. Therefore, once we see a type-specifier
7195 (other than a cv-qualifier), we forbid any additional
7196 user-defined types. We *do* still allow things like `int
7197 int' to be considered a decl-specifier-seq, and issue the
7198 error message later. */
62d1db17 7199 if (type_spec && !is_cv_qualifier)
a723baf1 7200 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb 7201 /* A constructor declarator cannot follow a type-specifier. */
62d1db17 7202 if (type_spec)
a723baf1 7203 {
62d1db17
MM
7204 constructor_possible_p = false;
7205 found_decl_spec = true;
a723baf1 7206 }
a723baf1
MM
7207 }
7208
62d1db17
MM
7209 /* If we still do not have a DECL_SPEC, then there are no more
7210 decl-specifiers. */
7211 if (!found_decl_spec)
7212 break;
a723baf1 7213
62d1db17 7214 decl_specs->any_specifiers_p = true;
a723baf1
MM
7215 /* After we see one decl-specifier, further decl-specifiers are
7216 always optional. */
7217 flags |= CP_PARSER_FLAGS_OPTIONAL;
7218 }
7219
0426c4ca 7220 /* Don't allow a friend specifier with a class definition. */
62d1db17
MM
7221 if (decl_specs->specs[(int) ds_friend] != 0
7222 && (*declares_class_or_enum & 2))
0426c4ca 7223 error ("class definition may not be declared a friend");
a723baf1
MM
7224}
7225
21526606 7226/* Parse an (optional) storage-class-specifier.
a723baf1
MM
7227
7228 storage-class-specifier:
7229 auto
7230 register
7231 static
7232 extern
21526606 7233 mutable
a723baf1
MM
7234
7235 GNU Extension:
7236
7237 storage-class-specifier:
7238 thread
7239
7240 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
21526606 7241
a723baf1 7242static tree
94edc4ab 7243cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
7244{
7245 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7246 {
7247 case RID_AUTO:
7248 case RID_REGISTER:
7249 case RID_STATIC:
7250 case RID_EXTERN:
7251 case RID_MUTABLE:
7252 case RID_THREAD:
7253 /* Consume the token. */
7254 return cp_lexer_consume_token (parser->lexer)->value;
7255
7256 default:
7257 return NULL_TREE;
7258 }
7259}
7260
21526606 7261/* Parse an (optional) function-specifier.
a723baf1
MM
7262
7263 function-specifier:
7264 inline
7265 virtual
7266 explicit
7267
62d1db17
MM
7268 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7269 Updates DECL_SPECS, if it is non-NULL. */
21526606 7270
a723baf1 7271static tree
62d1db17
MM
7272cp_parser_function_specifier_opt (cp_parser* parser,
7273 cp_decl_specifier_seq *decl_specs)
a723baf1
MM
7274{
7275 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7276 {
7277 case RID_INLINE:
62d1db17
MM
7278 if (decl_specs)
7279 ++decl_specs->specs[(int) ds_inline];
7280 break;
7281
a723baf1 7282 case RID_VIRTUAL:
62d1db17
MM
7283 if (decl_specs)
7284 ++decl_specs->specs[(int) ds_virtual];
7285 break;
7286
a723baf1 7287 case RID_EXPLICIT:
62d1db17
MM
7288 if (decl_specs)
7289 ++decl_specs->specs[(int) ds_explicit];
7290 break;
a723baf1
MM
7291
7292 default:
7293 return NULL_TREE;
7294 }
62d1db17
MM
7295
7296 /* Consume the token. */
7297 return cp_lexer_consume_token (parser->lexer)->value;
a723baf1
MM
7298}
7299
7300/* Parse a linkage-specification.
7301
7302 linkage-specification:
7303 extern string-literal { declaration-seq [opt] }
7304 extern string-literal declaration */
7305
7306static void
94edc4ab 7307cp_parser_linkage_specification (cp_parser* parser)
a723baf1 7308{
a723baf1
MM
7309 tree linkage;
7310
7311 /* Look for the `extern' keyword. */
7312 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7313
c162c75e
MA
7314 /* Look for the string-literal. */
7315 linkage = cp_parser_string_literal (parser, false, false);
a723baf1
MM
7316
7317 /* Transform the literal into an identifier. If the literal is a
7318 wide-character string, or contains embedded NULs, then we can't
7319 handle it as the user wants. */
c162c75e
MA
7320 if (strlen (TREE_STRING_POINTER (linkage))
7321 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
a723baf1
MM
7322 {
7323 cp_parser_error (parser, "invalid linkage-specification");
7324 /* Assume C++ linkage. */
c162c75e 7325 linkage = lang_name_cplusplus;
a723baf1 7326 }
a723baf1 7327 else
c162c75e 7328 linkage = get_identifier (TREE_STRING_POINTER (linkage));
a723baf1
MM
7329
7330 /* We're now using the new linkage. */
7331 push_lang_context (linkage);
7332
7333 /* If the next token is a `{', then we're using the first
7334 production. */
7335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7336 {
7337 /* Consume the `{' token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 /* Parse the declarations. */
7340 cp_parser_declaration_seq_opt (parser);
7341 /* Look for the closing `}'. */
7342 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7343 }
7344 /* Otherwise, there's just one declaration. */
7345 else
7346 {
7347 bool saved_in_unbraced_linkage_specification_p;
7348
21526606 7349 saved_in_unbraced_linkage_specification_p
a723baf1
MM
7350 = parser->in_unbraced_linkage_specification_p;
7351 parser->in_unbraced_linkage_specification_p = true;
7352 have_extern_spec = true;
7353 cp_parser_declaration (parser);
7354 have_extern_spec = false;
21526606 7355 parser->in_unbraced_linkage_specification_p
a723baf1
MM
7356 = saved_in_unbraced_linkage_specification_p;
7357 }
7358
7359 /* We're done with the linkage-specification. */
7360 pop_lang_context ();
7361}
7362
7363/* Special member functions [gram.special] */
7364
7365/* Parse a conversion-function-id.
7366
7367 conversion-function-id:
21526606 7368 operator conversion-type-id
a723baf1
MM
7369
7370 Returns an IDENTIFIER_NODE representing the operator. */
7371
21526606 7372static tree
94edc4ab 7373cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7374{
7375 tree type;
7376 tree saved_scope;
7377 tree saved_qualifying_scope;
7378 tree saved_object_scope;
91b004e5 7379 bool pop_p = false;
a723baf1
MM
7380
7381 /* Look for the `operator' token. */
7382 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7383 return error_mark_node;
7384 /* When we parse the conversion-type-id, the current scope will be
7385 reset. However, we need that information in able to look up the
7386 conversion function later, so we save it here. */
7387 saved_scope = parser->scope;
7388 saved_qualifying_scope = parser->qualifying_scope;
7389 saved_object_scope = parser->object_scope;
7390 /* We must enter the scope of the class so that the names of
7391 entities declared within the class are available in the
7392 conversion-type-id. For example, consider:
7393
21526606 7394 struct S {
a723baf1
MM
7395 typedef int I;
7396 operator I();
7397 };
7398
7399 S::operator I() { ... }
7400
7401 In order to see that `I' is a type-name in the definition, we
7402 must be in the scope of `S'. */
7403 if (saved_scope)
91b004e5 7404 pop_p = push_scope (saved_scope);
a723baf1
MM
7405 /* Parse the conversion-type-id. */
7406 type = cp_parser_conversion_type_id (parser);
7407 /* Leave the scope of the class, if any. */
91b004e5 7408 if (pop_p)
a723baf1
MM
7409 pop_scope (saved_scope);
7410 /* Restore the saved scope. */
7411 parser->scope = saved_scope;
7412 parser->qualifying_scope = saved_qualifying_scope;
7413 parser->object_scope = saved_object_scope;
7414 /* If the TYPE is invalid, indicate failure. */
7415 if (type == error_mark_node)
7416 return error_mark_node;
7417 return mangle_conv_op_name_for_type (type);
7418}
7419
7420/* Parse a conversion-type-id:
7421
7422 conversion-type-id:
7423 type-specifier-seq conversion-declarator [opt]
7424
7425 Returns the TYPE specified. */
7426
7427static tree
94edc4ab 7428cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7429{
7430 tree attributes;
62d1db17 7431 cp_decl_specifier_seq type_specifiers;
058b15c1 7432 cp_declarator *declarator;
037cc9c5 7433 tree type_specified;
a723baf1
MM
7434
7435 /* Parse the attributes. */
7436 attributes = cp_parser_attributes_opt (parser);
7437 /* Parse the type-specifiers. */
62d1db17 7438 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1 7439 /* If that didn't work, stop. */
62d1db17 7440 if (type_specifiers.type == error_mark_node)
a723baf1
MM
7441 return error_mark_node;
7442 /* Parse the conversion-declarator. */
7443 declarator = cp_parser_conversion_declarator_opt (parser);
7444
037cc9c5
FJ
7445 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7446 /*initialized=*/0, &attributes);
7447 if (attributes)
7448 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7449 return type_specified;
a723baf1
MM
7450}
7451
7452/* Parse an (optional) conversion-declarator.
7453
7454 conversion-declarator:
21526606 7455 ptr-operator conversion-declarator [opt]
a723baf1 7456
058b15c1 7457 */
a723baf1 7458
058b15c1 7459static cp_declarator *
94edc4ab 7460cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7461{
7462 enum tree_code code;
7463 tree class_type;
3c01e5df 7464 cp_cv_quals cv_quals;
a723baf1
MM
7465
7466 /* We don't know if there's a ptr-operator next, or not. */
7467 cp_parser_parse_tentatively (parser);
7468 /* Try the ptr-operator. */
3c01e5df 7469 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
a723baf1
MM
7470 /* If it worked, look for more conversion-declarators. */
7471 if (cp_parser_parse_definitely (parser))
7472 {
058b15c1 7473 cp_declarator *declarator;
98ca843c 7474
058b15c1
MM
7475 /* Parse another optional declarator. */
7476 declarator = cp_parser_conversion_declarator_opt (parser);
98ca843c 7477
058b15c1
MM
7478 /* Create the representation of the declarator. */
7479 if (class_type)
3c01e5df 7480 declarator = make_ptrmem_declarator (cv_quals, class_type,
a723baf1 7481 declarator);
058b15c1 7482 else if (code == INDIRECT_REF)
3c01e5df 7483 declarator = make_pointer_declarator (cv_quals, declarator);
058b15c1 7484 else
3c01e5df 7485 declarator = make_reference_declarator (cv_quals, declarator);
98ca843c 7486
058b15c1 7487 return declarator;
a723baf1
MM
7488 }
7489
058b15c1 7490 return NULL;
a723baf1
MM
7491}
7492
7493/* Parse an (optional) ctor-initializer.
7494
7495 ctor-initializer:
21526606 7496 : mem-initializer-list
a723baf1
MM
7497
7498 Returns TRUE iff the ctor-initializer was actually present. */
7499
7500static bool
94edc4ab 7501cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7502{
7503 /* If the next token is not a `:', then there is no
7504 ctor-initializer. */
7505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7506 {
7507 /* Do default initialization of any bases and members. */
7508 if (DECL_CONSTRUCTOR_P (current_function_decl))
7509 finish_mem_initializers (NULL_TREE);
7510
7511 return false;
7512 }
7513
7514 /* Consume the `:' token. */
7515 cp_lexer_consume_token (parser->lexer);
7516 /* And the mem-initializer-list. */
7517 cp_parser_mem_initializer_list (parser);
7518
7519 return true;
7520}
7521
7522/* Parse a mem-initializer-list.
7523
7524 mem-initializer-list:
7525 mem-initializer
7526 mem-initializer , mem-initializer-list */
7527
7528static void
94edc4ab 7529cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7530{
7531 tree mem_initializer_list = NULL_TREE;
7532
7533 /* Let the semantic analysis code know that we are starting the
7534 mem-initializer-list. */
0e136342
MM
7535 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7536 error ("only constructors take base initializers");
a723baf1
MM
7537
7538 /* Loop through the list. */
7539 while (true)
7540 {
7541 tree mem_initializer;
7542
7543 /* Parse the mem-initializer. */
7544 mem_initializer = cp_parser_mem_initializer (parser);
7545 /* Add it to the list, unless it was erroneous. */
7546 if (mem_initializer)
7547 {
7548 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7549 mem_initializer_list = mem_initializer;
7550 }
7551 /* If the next token is not a `,', we're done. */
7552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7553 break;
7554 /* Consume the `,' token. */
7555 cp_lexer_consume_token (parser->lexer);
7556 }
7557
7558 /* Perform semantic analysis. */
0e136342
MM
7559 if (DECL_CONSTRUCTOR_P (current_function_decl))
7560 finish_mem_initializers (mem_initializer_list);
a723baf1
MM
7561}
7562
7563/* Parse a mem-initializer.
7564
7565 mem-initializer:
21526606 7566 mem-initializer-id ( expression-list [opt] )
a723baf1
MM
7567
7568 GNU extension:
21526606 7569
a723baf1 7570 mem-initializer:
34cd5ae7 7571 ( expression-list [opt] )
a723baf1
MM
7572
7573 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7574 class) or FIELD_DECL (for a non-static data member) to initialize;
7575 the TREE_VALUE is the expression-list. */
7576
7577static tree
94edc4ab 7578cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7579{
7580 tree mem_initializer_id;
7581 tree expression_list;
1f5a253a 7582 tree member;
21526606 7583
a723baf1
MM
7584 /* Find out what is being initialized. */
7585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7586 {
7587 pedwarn ("anachronistic old-style base class initializer");
7588 mem_initializer_id = NULL_TREE;
7589 }
7590 else
7591 mem_initializer_id = cp_parser_mem_initializer_id (parser);
1f5a253a
NS
7592 member = expand_member_init (mem_initializer_id);
7593 if (member && !DECL_P (member))
7594 in_base_initializer = 1;
7efa3e22 7595
21526606 7596 expression_list
39703eb9
MM
7597 = cp_parser_parenthesized_expression_list (parser, false,
7598 /*non_constant_p=*/NULL);
7efa3e22 7599 if (!expression_list)
a723baf1 7600 expression_list = void_type_node;
a723baf1 7601
1f5a253a 7602 in_base_initializer = 0;
21526606 7603
1f5a253a 7604 return member ? build_tree_list (member, expression_list) : NULL_TREE;
a723baf1
MM
7605}
7606
7607/* Parse a mem-initializer-id.
7608
7609 mem-initializer-id:
7610 :: [opt] nested-name-specifier [opt] class-name
21526606 7611 identifier
a723baf1
MM
7612
7613 Returns a TYPE indicating the class to be initializer for the first
7614 production. Returns an IDENTIFIER_NODE indicating the data member
7615 to be initialized for the second production. */
7616
7617static tree
94edc4ab 7618cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7619{
7620 bool global_scope_p;
7621 bool nested_name_specifier_p;
8a83a693 7622 bool template_p = false;
a723baf1
MM
7623 tree id;
7624
8a83a693
GB
7625 /* `typename' is not allowed in this context ([temp.res]). */
7626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7627 {
2a13a625 7628 error ("keyword %<typename%> not allowed in this context (a qualified "
8a83a693
GB
7629 "member initializer is implicitly a type)");
7630 cp_lexer_consume_token (parser->lexer);
7631 }
a723baf1 7632 /* Look for the optional `::' operator. */
21526606
EC
7633 global_scope_p
7634 = (cp_parser_global_scope_opt (parser,
7635 /*current_scope_valid_p=*/false)
a723baf1
MM
7636 != NULL_TREE);
7637 /* Look for the optional nested-name-specifier. The simplest way to
7638 implement:
7639
7640 [temp.res]
7641
7642 The keyword `typename' is not permitted in a base-specifier or
7643 mem-initializer; in these contexts a qualified name that
7644 depends on a template-parameter is implicitly assumed to be a
7645 type name.
7646
7647 is to assume that we have seen the `typename' keyword at this
7648 point. */
21526606 7649 nested_name_specifier_p
a723baf1
MM
7650 = (cp_parser_nested_name_specifier_opt (parser,
7651 /*typename_keyword_p=*/true,
7652 /*check_dependency_p=*/true,
a668c6ad
MM
7653 /*type_p=*/true,
7654 /*is_declaration=*/true)
a723baf1 7655 != NULL_TREE);
8a83a693
GB
7656 if (nested_name_specifier_p)
7657 template_p = cp_parser_optional_template_keyword (parser);
a723baf1
MM
7658 /* If there is a `::' operator or a nested-name-specifier, then we
7659 are definitely looking for a class-name. */
7660 if (global_scope_p || nested_name_specifier_p)
7661 return cp_parser_class_name (parser,
7662 /*typename_keyword_p=*/true,
8a83a693 7663 /*template_keyword_p=*/template_p,
fc6a28d7 7664 none_type,
a723baf1 7665 /*check_dependency_p=*/true,
a668c6ad
MM
7666 /*class_head_p=*/false,
7667 /*is_declaration=*/true);
a723baf1
MM
7668 /* Otherwise, we could also be looking for an ordinary identifier. */
7669 cp_parser_parse_tentatively (parser);
7670 /* Try a class-name. */
21526606 7671 id = cp_parser_class_name (parser,
a723baf1
MM
7672 /*typename_keyword_p=*/true,
7673 /*template_keyword_p=*/false,
fc6a28d7 7674 none_type,
a723baf1 7675 /*check_dependency_p=*/true,
a668c6ad
MM
7676 /*class_head_p=*/false,
7677 /*is_declaration=*/true);
a723baf1
MM
7678 /* If we found one, we're done. */
7679 if (cp_parser_parse_definitely (parser))
7680 return id;
7681 /* Otherwise, look for an ordinary identifier. */
7682 return cp_parser_identifier (parser);
7683}
7684
7685/* Overloading [gram.over] */
7686
7687/* Parse an operator-function-id.
7688
7689 operator-function-id:
21526606 7690 operator operator
a723baf1
MM
7691
7692 Returns an IDENTIFIER_NODE for the operator which is a
7693 human-readable spelling of the identifier, e.g., `operator +'. */
7694
21526606 7695static tree
94edc4ab 7696cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7697{
7698 /* Look for the `operator' keyword. */
7699 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7700 return error_mark_node;
7701 /* And then the name of the operator itself. */
7702 return cp_parser_operator (parser);
7703}
7704
7705/* Parse an operator.
7706
7707 operator:
7708 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7709 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7710 || ++ -- , ->* -> () []
7711
7712 GNU Extensions:
21526606 7713
a723baf1
MM
7714 operator:
7715 <? >? <?= >?=
7716
7717 Returns an IDENTIFIER_NODE for the operator which is a
7718 human-readable spelling of the identifier, e.g., `operator +'. */
21526606 7719
a723baf1 7720static tree
94edc4ab 7721cp_parser_operator (cp_parser* parser)
a723baf1
MM
7722{
7723 tree id = NULL_TREE;
7724 cp_token *token;
7725
7726 /* Peek at the next token. */
7727 token = cp_lexer_peek_token (parser->lexer);
7728 /* Figure out which operator we have. */
7729 switch (token->type)
7730 {
7731 case CPP_KEYWORD:
7732 {
7733 enum tree_code op;
7734
7735 /* The keyword should be either `new' or `delete'. */
7736 if (token->keyword == RID_NEW)
7737 op = NEW_EXPR;
7738 else if (token->keyword == RID_DELETE)
7739 op = DELETE_EXPR;
7740 else
7741 break;
7742
7743 /* Consume the `new' or `delete' token. */
7744 cp_lexer_consume_token (parser->lexer);
7745
7746 /* Peek at the next token. */
7747 token = cp_lexer_peek_token (parser->lexer);
7748 /* If it's a `[' token then this is the array variant of the
7749 operator. */
7750 if (token->type == CPP_OPEN_SQUARE)
7751 {
7752 /* Consume the `[' token. */
7753 cp_lexer_consume_token (parser->lexer);
7754 /* Look for the `]' token. */
7755 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
21526606 7756 id = ansi_opname (op == NEW_EXPR
a723baf1
MM
7757 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7758 }
7759 /* Otherwise, we have the non-array variant. */
7760 else
7761 id = ansi_opname (op);
7762
7763 return id;
7764 }
7765
7766 case CPP_PLUS:
7767 id = ansi_opname (PLUS_EXPR);
7768 break;
7769
7770 case CPP_MINUS:
7771 id = ansi_opname (MINUS_EXPR);
7772 break;
7773
7774 case CPP_MULT:
7775 id = ansi_opname (MULT_EXPR);
7776 break;
7777
7778 case CPP_DIV:
7779 id = ansi_opname (TRUNC_DIV_EXPR);
7780 break;
7781
7782 case CPP_MOD:
7783 id = ansi_opname (TRUNC_MOD_EXPR);
7784 break;
7785
7786 case CPP_XOR:
7787 id = ansi_opname (BIT_XOR_EXPR);
7788 break;
7789
7790 case CPP_AND:
7791 id = ansi_opname (BIT_AND_EXPR);
7792 break;
7793
7794 case CPP_OR:
7795 id = ansi_opname (BIT_IOR_EXPR);
7796 break;
7797
7798 case CPP_COMPL:
7799 id = ansi_opname (BIT_NOT_EXPR);
7800 break;
21526606 7801
a723baf1
MM
7802 case CPP_NOT:
7803 id = ansi_opname (TRUTH_NOT_EXPR);
7804 break;
7805
7806 case CPP_EQ:
7807 id = ansi_assopname (NOP_EXPR);
7808 break;
7809
7810 case CPP_LESS:
7811 id = ansi_opname (LT_EXPR);
7812 break;
7813
7814 case CPP_GREATER:
7815 id = ansi_opname (GT_EXPR);
7816 break;
7817
7818 case CPP_PLUS_EQ:
7819 id = ansi_assopname (PLUS_EXPR);
7820 break;
7821
7822 case CPP_MINUS_EQ:
7823 id = ansi_assopname (MINUS_EXPR);
7824 break;
7825
7826 case CPP_MULT_EQ:
7827 id = ansi_assopname (MULT_EXPR);
7828 break;
7829
7830 case CPP_DIV_EQ:
7831 id = ansi_assopname (TRUNC_DIV_EXPR);
7832 break;
7833
7834 case CPP_MOD_EQ:
7835 id = ansi_assopname (TRUNC_MOD_EXPR);
7836 break;
7837
7838 case CPP_XOR_EQ:
7839 id = ansi_assopname (BIT_XOR_EXPR);
7840 break;
7841
7842 case CPP_AND_EQ:
7843 id = ansi_assopname (BIT_AND_EXPR);
7844 break;
7845
7846 case CPP_OR_EQ:
7847 id = ansi_assopname (BIT_IOR_EXPR);
7848 break;
7849
7850 case CPP_LSHIFT:
7851 id = ansi_opname (LSHIFT_EXPR);
7852 break;
7853
7854 case CPP_RSHIFT:
7855 id = ansi_opname (RSHIFT_EXPR);
7856 break;
7857
7858 case CPP_LSHIFT_EQ:
7859 id = ansi_assopname (LSHIFT_EXPR);
7860 break;
7861
7862 case CPP_RSHIFT_EQ:
7863 id = ansi_assopname (RSHIFT_EXPR);
7864 break;
7865
7866 case CPP_EQ_EQ:
7867 id = ansi_opname (EQ_EXPR);
7868 break;
7869
7870 case CPP_NOT_EQ:
7871 id = ansi_opname (NE_EXPR);
7872 break;
7873
7874 case CPP_LESS_EQ:
7875 id = ansi_opname (LE_EXPR);
7876 break;
7877
7878 case CPP_GREATER_EQ:
7879 id = ansi_opname (GE_EXPR);
7880 break;
7881
7882 case CPP_AND_AND:
7883 id = ansi_opname (TRUTH_ANDIF_EXPR);
7884 break;
7885
7886 case CPP_OR_OR:
7887 id = ansi_opname (TRUTH_ORIF_EXPR);
7888 break;
21526606 7889
a723baf1
MM
7890 case CPP_PLUS_PLUS:
7891 id = ansi_opname (POSTINCREMENT_EXPR);
7892 break;
7893
7894 case CPP_MINUS_MINUS:
7895 id = ansi_opname (PREDECREMENT_EXPR);
7896 break;
7897
7898 case CPP_COMMA:
7899 id = ansi_opname (COMPOUND_EXPR);
7900 break;
7901
7902 case CPP_DEREF_STAR:
7903 id = ansi_opname (MEMBER_REF);
7904 break;
7905
7906 case CPP_DEREF:
7907 id = ansi_opname (COMPONENT_REF);
7908 break;
7909
7910 case CPP_OPEN_PAREN:
7911 /* Consume the `('. */
7912 cp_lexer_consume_token (parser->lexer);
7913 /* Look for the matching `)'. */
7914 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7915 return ansi_opname (CALL_EXPR);
7916
7917 case CPP_OPEN_SQUARE:
7918 /* Consume the `['. */
7919 cp_lexer_consume_token (parser->lexer);
7920 /* Look for the matching `]'. */
7921 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7922 return ansi_opname (ARRAY_REF);
7923
7924 /* Extensions. */
7925 case CPP_MIN:
7926 id = ansi_opname (MIN_EXPR);
7927 break;
7928
7929 case CPP_MAX:
7930 id = ansi_opname (MAX_EXPR);
7931 break;
7932
7933 case CPP_MIN_EQ:
7934 id = ansi_assopname (MIN_EXPR);
7935 break;
7936
7937 case CPP_MAX_EQ:
7938 id = ansi_assopname (MAX_EXPR);
7939 break;
7940
7941 default:
7942 /* Anything else is an error. */
7943 break;
7944 }
7945
7946 /* If we have selected an identifier, we need to consume the
7947 operator token. */
7948 if (id)
7949 cp_lexer_consume_token (parser->lexer);
7950 /* Otherwise, no valid operator name was present. */
7951 else
7952 {
7953 cp_parser_error (parser, "expected operator");
7954 id = error_mark_node;
7955 }
7956
7957 return id;
7958}
7959
7960/* Parse a template-declaration.
7961
7962 template-declaration:
21526606 7963 export [opt] template < template-parameter-list > declaration
a723baf1
MM
7964
7965 If MEMBER_P is TRUE, this template-declaration occurs within a
21526606 7966 class-specifier.
a723baf1
MM
7967
7968 The grammar rule given by the standard isn't correct. What
7969 is really meant is:
7970
7971 template-declaration:
21526606 7972 export [opt] template-parameter-list-seq
a723baf1 7973 decl-specifier-seq [opt] init-declarator [opt] ;
21526606 7974 export [opt] template-parameter-list-seq
a723baf1
MM
7975 function-definition
7976
7977 template-parameter-list-seq:
7978 template-parameter-list-seq [opt]
7979 template < template-parameter-list > */
7980
7981static void
94edc4ab 7982cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7983{
7984 /* Check for `export'. */
7985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7986 {
7987 /* Consume the `export' token. */
7988 cp_lexer_consume_token (parser->lexer);
7989 /* Warn that we do not support `export'. */
2a13a625 7990 warning ("keyword %<export%> not implemented, and will be ignored");
a723baf1
MM
7991 }
7992
7993 cp_parser_template_declaration_after_export (parser, member_p);
7994}
7995
7996/* Parse a template-parameter-list.
7997
7998 template-parameter-list:
7999 template-parameter
8000 template-parameter-list , template-parameter
8001
8002 Returns a TREE_LIST. Each node represents a template parameter.
8003 The nodes are connected via their TREE_CHAINs. */
8004
8005static tree
94edc4ab 8006cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
8007{
8008 tree parameter_list = NULL_TREE;
8009
8010 while (true)
8011 {
8012 tree parameter;
8013 cp_token *token;
058b15c1 8014 bool is_non_type;
a723baf1
MM
8015
8016 /* Parse the template-parameter. */
058b15c1 8017 parameter = cp_parser_template_parameter (parser, &is_non_type);
a723baf1 8018 /* Add it to the list. */
943e3ede
MM
8019 if (parameter != error_mark_node)
8020 parameter_list = process_template_parm (parameter_list,
8021 parameter,
8022 is_non_type);
a723baf1
MM
8023 /* Peek at the next token. */
8024 token = cp_lexer_peek_token (parser->lexer);
8025 /* If it's not a `,', we're done. */
8026 if (token->type != CPP_COMMA)
8027 break;
8028 /* Otherwise, consume the `,' token. */
8029 cp_lexer_consume_token (parser->lexer);
8030 }
8031
8032 return parameter_list;
8033}
8034
8035/* Parse a template-parameter.
8036
8037 template-parameter:
8038 type-parameter
8039 parameter-declaration
8040
943e3ede
MM
8041 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8042 the parameter. The TREE_PURPOSE is the default value, if any.
8043 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8044 iff this parameter is a non-type parameter. */
a723baf1
MM
8045
8046static tree
058b15c1 8047cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
a723baf1
MM
8048{
8049 cp_token *token;
62d1db17 8050 cp_parameter_declarator *parameter_declarator;
943e3ede 8051 tree parm;
a723baf1 8052
058b15c1
MM
8053 /* Assume it is a type parameter or a template parameter. */
8054 *is_non_type = false;
a723baf1
MM
8055 /* Peek at the next token. */
8056 token = cp_lexer_peek_token (parser->lexer);
8057 /* If it is `class' or `template', we have a type-parameter. */
8058 if (token->keyword == RID_TEMPLATE)
8059 return cp_parser_type_parameter (parser);
8060 /* If it is `class' or `typename' we do not know yet whether it is a
8061 type parameter or a non-type parameter. Consider:
8062
8063 template <typename T, typename T::X X> ...
8064
8065 or:
21526606 8066
a723baf1
MM
8067 template <class C, class D*> ...
8068
8069 Here, the first parameter is a type parameter, and the second is
8070 a non-type parameter. We can tell by looking at the token after
8071 the identifier -- if it is a `,', `=', or `>' then we have a type
8072 parameter. */
8073 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8074 {
8075 /* Peek at the token after `class' or `typename'. */
8076 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8077 /* If it's an identifier, skip it. */
8078 if (token->type == CPP_NAME)
8079 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8080 /* Now, see if the token looks like the end of a template
8081 parameter. */
21526606 8082 if (token->type == CPP_COMMA
a723baf1
MM
8083 || token->type == CPP_EQ
8084 || token->type == CPP_GREATER)
8085 return cp_parser_type_parameter (parser);
8086 }
8087
21526606 8088 /* Otherwise, it is a non-type parameter.
a723baf1
MM
8089
8090 [temp.param]
8091
8092 When parsing a default template-argument for a non-type
8093 template-parameter, the first non-nested `>' is taken as the end
8094 of the template parameter-list rather than a greater-than
8095 operator. */
058b15c1
MM
8096 *is_non_type = true;
8097 parameter_declarator
8098 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8099 /*parenthesized_p=*/NULL);
943e3ede
MM
8100 parm = grokdeclarator (parameter_declarator->declarator,
8101 &parameter_declarator->decl_specifiers,
8102 PARM, /*initialized=*/0,
8103 /*attrlist=*/NULL);
8104 if (parm == error_mark_node)
8105 return error_mark_node;
8106 return build_tree_list (parameter_declarator->default_argument, parm);
a723baf1
MM
8107}
8108
8109/* Parse a type-parameter.
8110
8111 type-parameter:
8112 class identifier [opt]
8113 class identifier [opt] = type-id
8114 typename identifier [opt]
8115 typename identifier [opt] = type-id
8116 template < template-parameter-list > class identifier [opt]
21526606
EC
8117 template < template-parameter-list > class identifier [opt]
8118 = id-expression
a723baf1
MM
8119
8120 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8121 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8122 the declaration of the parameter. */
8123
8124static tree
94edc4ab 8125cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
8126{
8127 cp_token *token;
8128 tree parameter;
8129
8130 /* Look for a keyword to tell us what kind of parameter this is. */
21526606 8131 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 8132 "`class', `typename', or `template'");
a723baf1
MM
8133 if (!token)
8134 return error_mark_node;
8135
8136 switch (token->keyword)
8137 {
8138 case RID_CLASS:
8139 case RID_TYPENAME:
8140 {
8141 tree identifier;
8142 tree default_argument;
8143
8144 /* If the next token is an identifier, then it names the
8145 parameter. */
8146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8147 identifier = cp_parser_identifier (parser);
8148 else
8149 identifier = NULL_TREE;
8150
8151 /* Create the parameter. */
8152 parameter = finish_template_type_parm (class_type_node, identifier);
8153
8154 /* If the next token is an `=', we have a default argument. */
8155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8156 {
8157 /* Consume the `=' token. */
8158 cp_lexer_consume_token (parser->lexer);
34cd5ae7 8159 /* Parse the default-argument. */
a723baf1
MM
8160 default_argument = cp_parser_type_id (parser);
8161 }
8162 else
8163 default_argument = NULL_TREE;
8164
8165 /* Create the combined representation of the parameter and the
8166 default argument. */
c67d36d0 8167 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8168 }
8169 break;
8170
8171 case RID_TEMPLATE:
8172 {
8173 tree parameter_list;
8174 tree identifier;
8175 tree default_argument;
8176
8177 /* Look for the `<'. */
8178 cp_parser_require (parser, CPP_LESS, "`<'");
8179 /* Parse the template-parameter-list. */
8180 begin_template_parm_list ();
21526606 8181 parameter_list
a723baf1
MM
8182 = cp_parser_template_parameter_list (parser);
8183 parameter_list = end_template_parm_list (parameter_list);
8184 /* Look for the `>'. */
8185 cp_parser_require (parser, CPP_GREATER, "`>'");
8186 /* Look for the `class' keyword. */
8187 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8188 /* If the next token is an `=', then there is a
8189 default-argument. If the next token is a `>', we are at
8190 the end of the parameter-list. If the next token is a `,',
8191 then we are at the end of this parameter. */
8192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8193 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8194 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
71bd7186
MM
8195 {
8196 identifier = cp_parser_identifier (parser);
03fd3f84 8197 /* Treat invalid names as if the parameter were nameless. */
71bd7186
MM
8198 if (identifier == error_mark_node)
8199 identifier = NULL_TREE;
8200 }
a723baf1
MM
8201 else
8202 identifier = NULL_TREE;
71bd7186 8203
a723baf1
MM
8204 /* Create the template parameter. */
8205 parameter = finish_template_template_parm (class_type_node,
8206 identifier);
21526606 8207
a723baf1
MM
8208 /* If the next token is an `=', then there is a
8209 default-argument. */
8210 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8211 {
b0bc6e8e
KL
8212 bool is_template;
8213
a723baf1
MM
8214 /* Consume the `='. */
8215 cp_lexer_consume_token (parser->lexer);
8216 /* Parse the id-expression. */
21526606 8217 default_argument
a723baf1
MM
8218 = cp_parser_id_expression (parser,
8219 /*template_keyword_p=*/false,
8220 /*check_dependency_p=*/true,
b0bc6e8e 8221 /*template_p=*/&is_template,
f3c2dfc6 8222 /*declarator_p=*/false);
a3a503a5
GB
8223 if (TREE_CODE (default_argument) == TYPE_DECL)
8224 /* If the id-expression was a template-id that refers to
8225 a template-class, we already have the declaration here,
8226 so no further lookup is needed. */
8227 ;
8228 else
8229 /* Look up the name. */
21526606 8230 default_argument
a3a503a5 8231 = cp_parser_lookup_name (parser, default_argument,
fc6a28d7
MM
8232 none_type,
8233 /*is_template=*/is_template,
8234 /*is_namespace=*/false,
8235 /*check_dependency=*/true,
8236 /*ambiguous_p=*/NULL);
a723baf1
MM
8237 /* See if the default argument is valid. */
8238 default_argument
8239 = check_template_template_default_arg (default_argument);
8240 }
8241 else
8242 default_argument = NULL_TREE;
8243
8244 /* Create the combined representation of the parameter and the
8245 default argument. */
71bd7186 8246 parameter = build_tree_list (default_argument, parameter);
a723baf1
MM
8247 }
8248 break;
8249
8250 default:
71bd7186
MM
8251 gcc_unreachable ();
8252 break;
a723baf1 8253 }
21526606 8254
a723baf1
MM
8255 return parameter;
8256}
8257
8258/* Parse a template-id.
8259
8260 template-id:
8261 template-name < template-argument-list [opt] >
8262
8263 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8264 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8265 returned. Otherwise, if the template-name names a function, or set
8266 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
21526606 8267 names a class, returns a TYPE_DECL for the specialization.
a723baf1
MM
8268
8269 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8270 uninstantiated templates. */
8271
8272static tree
21526606
EC
8273cp_parser_template_id (cp_parser *parser,
8274 bool template_keyword_p,
a668c6ad
MM
8275 bool check_dependency_p,
8276 bool is_declaration)
a723baf1
MM
8277{
8278 tree template;
8279 tree arguments;
a723baf1 8280 tree template_id;
0c5e4866 8281 cp_token_position start_of_id = 0;
a723baf1 8282 tree access_check = NULL_TREE;
f4abade9 8283 cp_token *next_token, *next_token_2;
a668c6ad 8284 bool is_identifier;
a723baf1
MM
8285
8286 /* If the next token corresponds to a template-id, there is no need
8287 to reparse it. */
2050a1bb
MM
8288 next_token = cp_lexer_peek_token (parser->lexer);
8289 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8290 {
8291 tree value;
8292 tree check;
8293
8294 /* Get the stored value. */
8295 value = cp_lexer_consume_token (parser->lexer)->value;
8296 /* Perform any access checks that were deferred. */
8297 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8298 perform_or_defer_access_check (TREE_PURPOSE (check),
8299 TREE_VALUE (check));
a723baf1
MM
8300 /* Return the stored value. */
8301 return TREE_VALUE (value);
8302 }
8303
2050a1bb
MM
8304 /* Avoid performing name lookup if there is no possibility of
8305 finding a template-id. */
8306 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8307 || (next_token->type == CPP_NAME
21526606 8308 && !cp_parser_nth_token_starts_template_argument_list_p
f4abade9 8309 (parser, 2)))
2050a1bb
MM
8310 {
8311 cp_parser_error (parser, "expected template-id");
8312 return error_mark_node;
8313 }
8314
a723baf1 8315 /* Remember where the template-id starts. */
0b16f8f4 8316 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
0c5e4866 8317 start_of_id = cp_lexer_token_position (parser->lexer, false);
a723baf1 8318
8d241e0b 8319 push_deferring_access_checks (dk_deferred);
cf22909c 8320
a723baf1 8321 /* Parse the template-name. */
a668c6ad 8322 is_identifier = false;
a723baf1 8323 template = cp_parser_template_name (parser, template_keyword_p,
a668c6ad
MM
8324 check_dependency_p,
8325 is_declaration,
8326 &is_identifier);
8327 if (template == error_mark_node || is_identifier)
cf22909c
KL
8328 {
8329 pop_deferring_access_checks ();
a668c6ad 8330 return template;
cf22909c 8331 }
a723baf1 8332
21526606 8333 /* If we find the sequence `[:' after a template-name, it's probably
f4abade9
GB
8334 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8335 parse correctly the argument list. */
2cfe82fe 8336 next_token = cp_lexer_peek_token (parser->lexer);
f4abade9 8337 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21526606 8338 if (next_token->type == CPP_OPEN_SQUARE
f4abade9 8339 && next_token->flags & DIGRAPH
21526606 8340 && next_token_2->type == CPP_COLON
f4abade9 8341 && !(next_token_2->flags & PREV_WHITE))
cf22909c 8342 {
f4abade9
GB
8343 cp_parser_parse_tentatively (parser);
8344 /* Change `:' into `::'. */
8345 next_token_2->type = CPP_SCOPE;
8346 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8347 CPP_LESS. */
8348 cp_lexer_consume_token (parser->lexer);
8349 /* Parse the arguments. */
8350 arguments = cp_parser_enclosed_template_argument_list (parser);
8351 if (!cp_parser_parse_definitely (parser))
8352 {
8353 /* If we couldn't parse an argument list, then we revert our changes
8354 and return simply an error. Maybe this is not a template-id
8355 after all. */
8356 next_token_2->type = CPP_COLON;
2a13a625 8357 cp_parser_error (parser, "expected %<<%>");
f4abade9
GB
8358 pop_deferring_access_checks ();
8359 return error_mark_node;
8360 }
8361 /* Otherwise, emit an error about the invalid digraph, but continue
8362 parsing because we got our argument list. */
2a13a625
GDR
8363 pedwarn ("%<<::%> cannot begin a template-argument list");
8364 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8365 "between %<<%> and %<::%>");
f4abade9
GB
8366 if (!flag_permissive)
8367 {
8368 static bool hint;
8369 if (!hint)
8370 {
2a13a625 8371 inform ("(if you use -fpermissive G++ will accept your code)");
f4abade9
GB
8372 hint = true;
8373 }
8374 }
8375 }
8376 else
8377 {
8378 /* Look for the `<' that starts the template-argument-list. */
8379 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8380 {
8381 pop_deferring_access_checks ();
8382 return error_mark_node;
8383 }
8384 /* Parse the arguments. */
8385 arguments = cp_parser_enclosed_template_argument_list (parser);
cf22909c 8386 }
a723baf1
MM
8387
8388 /* Build a representation of the specialization. */
8389 if (TREE_CODE (template) == IDENTIFIER_NODE)
8390 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8391 else if (DECL_CLASS_TEMPLATE_P (template)
8392 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
21526606
EC
8393 template_id
8394 = finish_template_type (template, arguments,
8395 cp_lexer_next_token_is (parser->lexer,
a723baf1
MM
8396 CPP_SCOPE));
8397 else
8398 {
8399 /* If it's not a class-template or a template-template, it should be
8400 a function-template. */
50bc768d
NS
8401 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8402 || TREE_CODE (template) == OVERLOAD
8403 || BASELINK_P (template)));
21526606 8404
a723baf1
MM
8405 template_id = lookup_template_function (template, arguments);
8406 }
21526606 8407
cf22909c
KL
8408 /* Retrieve any deferred checks. Do not pop this access checks yet
8409 so the memory will not be reclaimed during token replacing below. */
8410 access_check = get_deferred_access_checks ();
8411
a723baf1
MM
8412 /* If parsing tentatively, replace the sequence of tokens that makes
8413 up the template-id with a CPP_TEMPLATE_ID token. That way,
8414 should we re-parse the token stream, we will not have to repeat
8415 the effort required to do the parse, nor will we issue duplicate
8416 error messages about problems during instantiation of the
354e22e1
AO
8417 template. Do so only if parsing succeeded, otherwise we may
8418 silently accept template arguments with syntax errors. */
8419 if (start_of_id && !cp_parser_error_occurred (parser))
a723baf1 8420 {
0c5e4866
NS
8421 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8422
a723baf1
MM
8423 /* Reset the contents of the START_OF_ID token. */
8424 token->type = CPP_TEMPLATE_ID;
8425 token->value = build_tree_list (access_check, template_id);
8426 token->keyword = RID_MAX;
0c5e4866 8427
a723baf1 8428 /* Purge all subsequent tokens. */
0c5e4866 8429 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
a723baf1
MM
8430 }
8431
cf22909c 8432 pop_deferring_access_checks ();
a723baf1
MM
8433 return template_id;
8434}
8435
8436/* Parse a template-name.
8437
8438 template-name:
8439 identifier
21526606 8440
a723baf1
MM
8441 The standard should actually say:
8442
8443 template-name:
8444 identifier
8445 operator-function-id
a723baf1
MM
8446
8447 A defect report has been filed about this issue.
8448
0d956474
GB
8449 A conversion-function-id cannot be a template name because they cannot
8450 be part of a template-id. In fact, looking at this code:
8451
8452 a.operator K<int>()
8453
8454 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
21526606 8455 It is impossible to call a templated conversion-function-id with an
0d956474
GB
8456 explicit argument list, since the only allowed template parameter is
8457 the type to which it is converting.
8458
a723baf1
MM
8459 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8460 `template' keyword, in a construction like:
8461
8462 T::template f<3>()
8463
8464 In that case `f' is taken to be a template-name, even though there
8465 is no way of knowing for sure.
8466
8467 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8468 name refers to a set of overloaded functions, at least one of which
8469 is a template, or an IDENTIFIER_NODE with the name of the template,
8470 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8471 names are looked up inside uninstantiated templates. */
8472
8473static tree
21526606
EC
8474cp_parser_template_name (cp_parser* parser,
8475 bool template_keyword_p,
a668c6ad
MM
8476 bool check_dependency_p,
8477 bool is_declaration,
8478 bool *is_identifier)
a723baf1
MM
8479{
8480 tree identifier;
8481 tree decl;
8482 tree fns;
8483
8484 /* If the next token is `operator', then we have either an
8485 operator-function-id or a conversion-function-id. */
8486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8487 {
8488 /* We don't know whether we're looking at an
8489 operator-function-id or a conversion-function-id. */
8490 cp_parser_parse_tentatively (parser);
8491 /* Try an operator-function-id. */
8492 identifier = cp_parser_operator_function_id (parser);
8493 /* If that didn't work, try a conversion-function-id. */
8494 if (!cp_parser_parse_definitely (parser))
0d956474
GB
8495 {
8496 cp_parser_error (parser, "expected template-name");
8497 return error_mark_node;
8498 }
a723baf1
MM
8499 }
8500 /* Look for the identifier. */
8501 else
8502 identifier = cp_parser_identifier (parser);
21526606 8503
a723baf1
MM
8504 /* If we didn't find an identifier, we don't have a template-id. */
8505 if (identifier == error_mark_node)
8506 return error_mark_node;
8507
8508 /* If the name immediately followed the `template' keyword, then it
8509 is a template-name. However, if the next token is not `<', then
8510 we do not treat it as a template-name, since it is not being used
8511 as part of a template-id. This enables us to handle constructs
8512 like:
8513
8514 template <typename T> struct S { S(); };
8515 template <typename T> S<T>::S();
8516
8517 correctly. We would treat `S' as a template -- if it were `S<T>'
8518 -- but we do not if there is no `<'. */
a668c6ad
MM
8519
8520 if (processing_template_decl
f4abade9 8521 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
a668c6ad
MM
8522 {
8523 /* In a declaration, in a dependent context, we pretend that the
8524 "template" keyword was present in order to improve error
8525 recovery. For example, given:
21526606 8526
a668c6ad 8527 template <typename T> void f(T::X<int>);
21526606 8528
a668c6ad 8529 we want to treat "X<int>" as a template-id. */
21526606
EC
8530 if (is_declaration
8531 && !template_keyword_p
a668c6ad 8532 && parser->scope && TYPE_P (parser->scope)
a52eb3bc 8533 && check_dependency_p
4e0f4df5
GB
8534 && dependent_type_p (parser->scope)
8535 /* Do not do this for dtors (or ctors), since they never
8536 need the template keyword before their name. */
8537 && !constructor_name_p (identifier, parser->scope))
a668c6ad 8538 {
0c5e4866
NS
8539 cp_token_position start = 0;
8540
a668c6ad 8541 /* Explain what went wrong. */
2a13a625
GDR
8542 error ("non-template %qD used as template", identifier);
8543 inform ("use %<%T::template %D%> to indicate that it is a template",
4e0f4df5 8544 parser->scope, identifier);
0b16f8f4
VR
8545 /* If parsing tentatively, find the location of the "<" token. */
8546 if (cp_parser_simulate_error (parser))
8547 start = cp_lexer_token_position (parser->lexer, true);
a668c6ad
MM
8548 /* Parse the template arguments so that we can issue error
8549 messages about them. */
8550 cp_lexer_consume_token (parser->lexer);
8551 cp_parser_enclosed_template_argument_list (parser);
8552 /* Skip tokens until we find a good place from which to
8553 continue parsing. */
8554 cp_parser_skip_to_closing_parenthesis (parser,
8555 /*recovering=*/true,
8556 /*or_comma=*/true,
8557 /*consume_paren=*/false);
8558 /* If parsing tentatively, permanently remove the
8559 template argument list. That will prevent duplicate
8560 error messages from being issued about the missing
8561 "template" keyword. */
0c5e4866
NS
8562 if (start)
8563 cp_lexer_purge_tokens_after (parser->lexer, start);
a668c6ad
MM
8564 if (is_identifier)
8565 *is_identifier = true;
8566 return identifier;
8567 }
9d363a56
MM
8568
8569 /* If the "template" keyword is present, then there is generally
8570 no point in doing name-lookup, so we just return IDENTIFIER.
8571 But, if the qualifying scope is non-dependent then we can
8572 (and must) do name-lookup normally. */
8573 if (template_keyword_p
8574 && (!parser->scope
98ca843c 8575 || (TYPE_P (parser->scope)
9d363a56 8576 && dependent_type_p (parser->scope))))
a668c6ad
MM
8577 return identifier;
8578 }
a723baf1
MM
8579
8580 /* Look up the name. */
8581 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 8582 none_type,
b0bc6e8e 8583 /*is_template=*/false,
eea9800f 8584 /*is_namespace=*/false,
8f78f01f
MM
8585 check_dependency_p,
8586 /*ambiguous_p=*/NULL);
a723baf1
MM
8587 decl = maybe_get_template_decl_from_type_decl (decl);
8588
8589 /* If DECL is a template, then the name was a template-name. */
8590 if (TREE_CODE (decl) == TEMPLATE_DECL)
8591 ;
21526606 8592 else
a723baf1
MM
8593 {
8594 /* The standard does not explicitly indicate whether a name that
8595 names a set of overloaded declarations, some of which are
8596 templates, is a template-name. However, such a name should
8597 be a template-name; otherwise, there is no way to form a
8598 template-id for the overloaded templates. */
8599 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8600 if (TREE_CODE (fns) == OVERLOAD)
8601 {
8602 tree fn;
21526606 8603
a723baf1
MM
8604 for (fn = fns; fn; fn = OVL_NEXT (fn))
8605 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8606 break;
8607 }
8608 else
8609 {
8610 /* Otherwise, the name does not name a template. */
8611 cp_parser_error (parser, "expected template-name");
8612 return error_mark_node;
8613 }
8614 }
8615
8616 /* If DECL is dependent, and refers to a function, then just return
8617 its name; we will look it up again during template instantiation. */
8618 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8619 {
8620 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8621 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8622 return identifier;
8623 }
8624
8625 return decl;
8626}
8627
8628/* Parse a template-argument-list.
8629
8630 template-argument-list:
8631 template-argument
8632 template-argument-list , template-argument
8633
04c06002 8634 Returns a TREE_VEC containing the arguments. */
a723baf1
MM
8635
8636static tree
94edc4ab 8637cp_parser_template_argument_list (cp_parser* parser)
a723baf1 8638{
bf12d54d
NS
8639 tree fixed_args[10];
8640 unsigned n_args = 0;
8641 unsigned alloced = 10;
8642 tree *arg_ary = fixed_args;
8643 tree vec;
4bb8ca28 8644 bool saved_in_template_argument_list_p;
a723baf1 8645
4bb8ca28
MM
8646 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8647 parser->in_template_argument_list_p = true;
bf12d54d 8648 do
a723baf1
MM
8649 {
8650 tree argument;
8651
bf12d54d 8652 if (n_args)
04c06002 8653 /* Consume the comma. */
bf12d54d 8654 cp_lexer_consume_token (parser->lexer);
21526606 8655
a723baf1
MM
8656 /* Parse the template-argument. */
8657 argument = cp_parser_template_argument (parser);
bf12d54d
NS
8658 if (n_args == alloced)
8659 {
8660 alloced *= 2;
21526606 8661
bf12d54d
NS
8662 if (arg_ary == fixed_args)
8663 {
8664 arg_ary = xmalloc (sizeof (tree) * alloced);
8665 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8666 }
8667 else
8668 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8669 }
8670 arg_ary[n_args++] = argument;
a723baf1 8671 }
bf12d54d
NS
8672 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8673
8674 vec = make_tree_vec (n_args);
a723baf1 8675
bf12d54d
NS
8676 while (n_args--)
8677 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
21526606 8678
bf12d54d
NS
8679 if (arg_ary != fixed_args)
8680 free (arg_ary);
4bb8ca28 8681 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
bf12d54d 8682 return vec;
a723baf1
MM
8683}
8684
8685/* Parse a template-argument.
8686
8687 template-argument:
8688 assignment-expression
8689 type-id
8690 id-expression
8691
8692 The representation is that of an assignment-expression, type-id, or
8693 id-expression -- except that the qualified id-expression is
8694 evaluated, so that the value returned is either a DECL or an
21526606 8695 OVERLOAD.
d17811fd
MM
8696
8697 Although the standard says "assignment-expression", it forbids
8698 throw-expressions or assignments in the template argument.
8699 Therefore, we use "conditional-expression" instead. */
a723baf1
MM
8700
8701static tree
94edc4ab 8702cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8703{
8704 tree argument;
8705 bool template_p;
d17811fd 8706 bool address_p;
4d5297fa 8707 bool maybe_type_id = false;
d17811fd 8708 cp_token *token;
b3445994 8709 cp_id_kind idk;
d17811fd 8710 tree qualifying_class;
a723baf1
MM
8711
8712 /* There's really no way to know what we're looking at, so we just
21526606 8713 try each alternative in order.
a723baf1
MM
8714
8715 [temp.arg]
8716
8717 In a template-argument, an ambiguity between a type-id and an
8718 expression is resolved to a type-id, regardless of the form of
21526606 8719 the corresponding template-parameter.
a723baf1
MM
8720
8721 Therefore, we try a type-id first. */
8722 cp_parser_parse_tentatively (parser);
a723baf1 8723 argument = cp_parser_type_id (parser);
4d5297fa 8724 /* If there was no error parsing the type-id but the next token is a '>>',
21526606 8725 we probably found a typo for '> >'. But there are type-id which are
4d5297fa
GB
8726 also valid expressions. For instance:
8727
8728 struct X { int operator >> (int); };
8729 template <int V> struct Foo {};
8730 Foo<X () >> 5> r;
8731
8732 Here 'X()' is a valid type-id of a function type, but the user just
8733 wanted to write the expression "X() >> 5". Thus, we remember that we
8734 found a valid type-id, but we still try to parse the argument as an
8735 expression to see what happens. */
8736 if (!cp_parser_error_occurred (parser)
8737 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8738 {
8739 maybe_type_id = true;
8740 cp_parser_abort_tentative_parse (parser);
8741 }
8742 else
8743 {
8744 /* If the next token isn't a `,' or a `>', then this argument wasn't
8745 really finished. This means that the argument is not a valid
8746 type-id. */
8747 if (!cp_parser_next_token_ends_template_argument_p (parser))
8748 cp_parser_error (parser, "expected template-argument");
8749 /* If that worked, we're done. */
8750 if (cp_parser_parse_definitely (parser))
8751 return argument;
8752 }
a723baf1
MM
8753 /* We're still not sure what the argument will be. */
8754 cp_parser_parse_tentatively (parser);
8755 /* Try a template. */
21526606 8756 argument = cp_parser_id_expression (parser,
a723baf1
MM
8757 /*template_keyword_p=*/false,
8758 /*check_dependency_p=*/true,
f3c2dfc6
MM
8759 &template_p,
8760 /*declarator_p=*/false);
a723baf1
MM
8761 /* If the next token isn't a `,' or a `>', then this argument wasn't
8762 really finished. */
d17811fd 8763 if (!cp_parser_next_token_ends_template_argument_p (parser))
a723baf1
MM
8764 cp_parser_error (parser, "expected template-argument");
8765 if (!cp_parser_error_occurred (parser))
8766 {
f746161e
MM
8767 /* Figure out what is being referred to. If the id-expression
8768 was for a class template specialization, then we will have a
8769 TYPE_DECL at this point. There is no need to do name lookup
8770 at this point in that case. */
8771 if (TREE_CODE (argument) != TYPE_DECL)
8772 argument = cp_parser_lookup_name (parser, argument,
fc6a28d7 8773 none_type,
f746161e
MM
8774 /*is_template=*/template_p,
8775 /*is_namespace=*/false,
8f78f01f
MM
8776 /*check_dependency=*/true,
8777 /*ambiguous_p=*/NULL);
5b4acce1
KL
8778 if (TREE_CODE (argument) != TEMPLATE_DECL
8779 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
a723baf1
MM
8780 cp_parser_error (parser, "expected template-name");
8781 }
8782 if (cp_parser_parse_definitely (parser))
8783 return argument;
d17811fd
MM
8784 /* It must be a non-type argument. There permitted cases are given
8785 in [temp.arg.nontype]:
8786
8787 -- an integral constant-expression of integral or enumeration
8788 type; or
8789
8790 -- the name of a non-type template-parameter; or
8791
8792 -- the name of an object or function with external linkage...
8793
8794 -- the address of an object or function with external linkage...
8795
04c06002 8796 -- a pointer to member... */
d17811fd
MM
8797 /* Look for a non-type template parameter. */
8798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8799 {
8800 cp_parser_parse_tentatively (parser);
8801 argument = cp_parser_primary_expression (parser,
8802 &idk,
8803 &qualifying_class);
8804 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8805 || !cp_parser_next_token_ends_template_argument_p (parser))
8806 cp_parser_simulate_error (parser);
8807 if (cp_parser_parse_definitely (parser))
8808 return argument;
8809 }
db24eb1f 8810
d17811fd
MM
8811 /* If the next token is "&", the argument must be the address of an
8812 object or function with external linkage. */
8813 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8814 if (address_p)
8815 cp_lexer_consume_token (parser->lexer);
8816 /* See if we might have an id-expression. */
8817 token = cp_lexer_peek_token (parser->lexer);
8818 if (token->type == CPP_NAME
8819 || token->keyword == RID_OPERATOR
8820 || token->type == CPP_SCOPE
8821 || token->type == CPP_TEMPLATE_ID
8822 || token->type == CPP_NESTED_NAME_SPECIFIER)
8823 {
8824 cp_parser_parse_tentatively (parser);
8825 argument = cp_parser_primary_expression (parser,
8826 &idk,
8827 &qualifying_class);
8828 if (cp_parser_error_occurred (parser)
8829 || !cp_parser_next_token_ends_template_argument_p (parser))
8830 cp_parser_abort_tentative_parse (parser);
8831 else
8832 {
db24eb1f
NS
8833 if (TREE_CODE (argument) == INDIRECT_REF)
8834 {
8835 gcc_assert (REFERENCE_REF_P (argument));
8836 argument = TREE_OPERAND (argument, 0);
8837 }
8838
d17811fd
MM
8839 if (qualifying_class)
8840 argument = finish_qualified_id_expr (qualifying_class,
8841 argument,
8842 /*done=*/true,
8843 address_p);
8844 if (TREE_CODE (argument) == VAR_DECL)
8845 {
8846 /* A variable without external linkage might still be a
8847 valid constant-expression, so no error is issued here
8848 if the external-linkage check fails. */
8849 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8850 cp_parser_simulate_error (parser);
8851 }
8852 else if (is_overloaded_fn (argument))
8853 /* All overloaded functions are allowed; if the external
8854 linkage test does not pass, an error will be issued
8855 later. */
8856 ;
8857 else if (address_p
21526606 8858 && (TREE_CODE (argument) == OFFSET_REF
d17811fd
MM
8859 || TREE_CODE (argument) == SCOPE_REF))
8860 /* A pointer-to-member. */
8861 ;
db24eb1f
NS
8862 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
8863 ;
d17811fd
MM
8864 else
8865 cp_parser_simulate_error (parser);
8866
8867 if (cp_parser_parse_definitely (parser))
8868 {
8869 if (address_p)
8870 argument = build_x_unary_op (ADDR_EXPR, argument);
8871 return argument;
8872 }
8873 }
8874 }
8875 /* If the argument started with "&", there are no other valid
8876 alternatives at this point. */
8877 if (address_p)
8878 {
8879 cp_parser_error (parser, "invalid non-type template argument");
8880 return error_mark_node;
8881 }
db24eb1f 8882
4d5297fa 8883 /* If the argument wasn't successfully parsed as a type-id followed
21526606 8884 by '>>', the argument can only be a constant expression now.
4d5297fa
GB
8885 Otherwise, we try parsing the constant-expression tentatively,
8886 because the argument could really be a type-id. */
8887 if (maybe_type_id)
8888 cp_parser_parse_tentatively (parser);
21526606 8889 argument = cp_parser_constant_expression (parser,
d17811fd
MM
8890 /*allow_non_constant_p=*/false,
8891 /*non_constant_p=*/NULL);
9baa27a9 8892 argument = fold_non_dependent_expr (argument);
4d5297fa
GB
8893 if (!maybe_type_id)
8894 return argument;
8895 if (!cp_parser_next_token_ends_template_argument_p (parser))
8896 cp_parser_error (parser, "expected template-argument");
8897 if (cp_parser_parse_definitely (parser))
8898 return argument;
8899 /* We did our best to parse the argument as a non type-id, but that
8900 was the only alternative that matched (albeit with a '>' after
21526606 8901 it). We can assume it's just a typo from the user, and a
4d5297fa
GB
8902 diagnostic will then be issued. */
8903 return cp_parser_type_id (parser);
a723baf1
MM
8904}
8905
8906/* Parse an explicit-instantiation.
8907
8908 explicit-instantiation:
21526606 8909 template declaration
a723baf1
MM
8910
8911 Although the standard says `declaration', what it really means is:
8912
8913 explicit-instantiation:
21526606 8914 template decl-specifier-seq [opt] declarator [opt] ;
a723baf1
MM
8915
8916 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8917 supposed to be allowed. A defect report has been filed about this
21526606 8918 issue.
a723baf1
MM
8919
8920 GNU Extension:
21526606 8921
a723baf1 8922 explicit-instantiation:
21526606 8923 storage-class-specifier template
a723baf1 8924 decl-specifier-seq [opt] declarator [opt] ;
21526606 8925 function-specifier template
a723baf1
MM
8926 decl-specifier-seq [opt] declarator [opt] ; */
8927
8928static void
94edc4ab 8929cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1 8930{
560ad596 8931 int declares_class_or_enum;
62d1db17 8932 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
8933 tree extension_specifier = NULL_TREE;
8934
8935 /* Look for an (optional) storage-class-specifier or
8936 function-specifier. */
8937 if (cp_parser_allow_gnu_extensions_p (parser))
8938 {
21526606 8939 extension_specifier
a723baf1
MM
8940 = cp_parser_storage_class_specifier_opt (parser);
8941 if (!extension_specifier)
98ca843c 8942 extension_specifier
62d1db17
MM
8943 = cp_parser_function_specifier_opt (parser,
8944 /*decl_specs=*/NULL);
a723baf1
MM
8945 }
8946
8947 /* Look for the `template' keyword. */
8948 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8949 /* Let the front end know that we are processing an explicit
8950 instantiation. */
8951 begin_explicit_instantiation ();
8952 /* [temp.explicit] says that we are supposed to ignore access
8953 control while processing explicit instantiation directives. */
78757caa 8954 push_deferring_access_checks (dk_no_check);
a723baf1 8955 /* Parse a decl-specifier-seq. */
62d1db17
MM
8956 cp_parser_decl_specifier_seq (parser,
8957 CP_PARSER_FLAGS_OPTIONAL,
8958 &decl_specifiers,
8959 &declares_class_or_enum);
a723baf1
MM
8960 /* If there was exactly one decl-specifier, and it declared a class,
8961 and there's no declarator, then we have an explicit type
8962 instantiation. */
8963 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8964 {
8965 tree type;
8966
62d1db17 8967 type = check_tag_decl (&decl_specifiers);
b7fc8b57
KL
8968 /* Turn access control back on for names used during
8969 template instantiation. */
8970 pop_deferring_access_checks ();
a723baf1
MM
8971 if (type)
8972 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8973 }
8974 else
8975 {
058b15c1 8976 cp_declarator *declarator;
a723baf1
MM
8977 tree decl;
8978
8979 /* Parse the declarator. */
21526606 8980 declarator
62b8a44e 8981 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 8982 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
8983 /*parenthesized_p=*/NULL,
8984 /*member_p=*/false);
fc6a28d7
MM
8985 if (declares_class_or_enum & 2)
8986 cp_parser_check_for_definition_in_return_type (declarator,
8987 decl_specifiers.type);
058b15c1 8988 if (declarator != cp_error_declarator)
216bb6e1 8989 {
62d1db17 8990 decl = grokdeclarator (declarator, &decl_specifiers,
216bb6e1
MM
8991 NORMAL, 0, NULL);
8992 /* Turn access control back on for names used during
8993 template instantiation. */
8994 pop_deferring_access_checks ();
8995 /* Do the explicit instantiation. */
8996 do_decl_instantiation (decl, extension_specifier);
8997 }
8998 else
8999 {
9000 pop_deferring_access_checks ();
9001 /* Skip the body of the explicit instantiation. */
9002 cp_parser_skip_to_end_of_statement (parser);
9003 }
a723baf1
MM
9004 }
9005 /* We're done with the instantiation. */
9006 end_explicit_instantiation ();
a723baf1 9007
e0860732 9008 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
9009}
9010
9011/* Parse an explicit-specialization.
9012
9013 explicit-specialization:
21526606 9014 template < > declaration
a723baf1
MM
9015
9016 Although the standard says `declaration', what it really means is:
9017
9018 explicit-specialization:
9019 template <> decl-specifier [opt] init-declarator [opt] ;
21526606 9020 template <> function-definition
a723baf1
MM
9021 template <> explicit-specialization
9022 template <> template-declaration */
9023
9024static void
94edc4ab 9025cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
9026{
9027 /* Look for the `template' keyword. */
9028 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9029 /* Look for the `<'. */
9030 cp_parser_require (parser, CPP_LESS, "`<'");
9031 /* Look for the `>'. */
9032 cp_parser_require (parser, CPP_GREATER, "`>'");
9033 /* We have processed another parameter list. */
9034 ++parser->num_template_parameter_lists;
9035 /* Let the front end know that we are beginning a specialization. */
9036 begin_specialization ();
9037
9038 /* If the next keyword is `template', we need to figure out whether
9039 or not we're looking a template-declaration. */
9040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9041 {
9042 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9043 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9044 cp_parser_template_declaration_after_export (parser,
9045 /*member_p=*/false);
9046 else
9047 cp_parser_explicit_specialization (parser);
9048 }
9049 else
9050 /* Parse the dependent declaration. */
21526606 9051 cp_parser_single_declaration (parser,
a723baf1
MM
9052 /*member_p=*/false,
9053 /*friend_p=*/NULL);
9054
9055 /* We're done with the specialization. */
9056 end_specialization ();
9057 /* We're done with this parameter list. */
9058 --parser->num_template_parameter_lists;
9059}
9060
9061/* Parse a type-specifier.
9062
9063 type-specifier:
9064 simple-type-specifier
9065 class-specifier
9066 enum-specifier
9067 elaborated-type-specifier
9068 cv-qualifier
9069
9070 GNU Extension:
9071
9072 type-specifier:
9073 __complex__
9074
62d1db17
MM
9075 Returns a representation of the type-specifier. For a
9076 class-specifier, enum-specifier, or elaborated-type-specifier, a
9077 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
a723baf1 9078
eb1aef53
KL
9079 The parser flags FLAGS is used to control type-specifier parsing.
9080
9081 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9082 in a decl-specifier-seq.
a723baf1
MM
9083
9084 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9085 class-specifier, enum-specifier, or elaborated-type-specifier, then
83a00410 9086 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
560ad596
MM
9087 if a type is declared; 2 if it is defined. Otherwise, it is set to
9088 zero.
a723baf1
MM
9089
9090 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9091 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9092 is set to FALSE. */
9093
9094static tree
21526606
EC
9095cp_parser_type_specifier (cp_parser* parser,
9096 cp_parser_flags flags,
62d1db17 9097 cp_decl_specifier_seq *decl_specs,
94edc4ab 9098 bool is_declaration,
560ad596 9099 int* declares_class_or_enum,
94edc4ab 9100 bool* is_cv_qualifier)
a723baf1
MM
9101{
9102 tree type_spec = NULL_TREE;
9103 cp_token *token;
9104 enum rid keyword;
62d1db17 9105 cp_decl_spec ds = ds_last;
a723baf1
MM
9106
9107 /* Assume this type-specifier does not declare a new type. */
9108 if (declares_class_or_enum)
543ca912 9109 *declares_class_or_enum = 0;
a723baf1
MM
9110 /* And that it does not specify a cv-qualifier. */
9111 if (is_cv_qualifier)
9112 *is_cv_qualifier = false;
9113 /* Peek at the next token. */
9114 token = cp_lexer_peek_token (parser->lexer);
9115
9116 /* If we're looking at a keyword, we can use that to guide the
9117 production we choose. */
9118 keyword = token->keyword;
9119 switch (keyword)
9120 {
ff4eb0b5
ZW
9121 case RID_ENUM:
9122 /* 'enum' [identifier] '{' introduces an enum-specifier;
9123 'enum' <anything else> introduces an elaborated-type-specifier. */
9124 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9125 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9126 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9127 == CPP_OPEN_BRACE))
9128 {
a5e51518
KL
9129 if (parser->num_template_parameter_lists)
9130 {
9131 error ("template declaration of %qs", "enum");
9132 cp_parser_skip_to_end_of_block_or_statement (parser);
9133 type_spec = error_mark_node;
9134 }
9135 else
9136 type_spec = cp_parser_enum_specifier (parser);
9137
ff4eb0b5
ZW
9138 if (declares_class_or_enum)
9139 *declares_class_or_enum = 2;
9140 if (decl_specs)
9141 cp_parser_set_decl_spec_type (decl_specs,
9142 type_spec,
9143 /*user_defined_p=*/true);
9144 return type_spec;
9145 }
9146 else
9147 goto elaborated_type_specifier;
9148
a723baf1
MM
9149 /* Any of these indicate either a class-specifier, or an
9150 elaborated-type-specifier. */
9151 case RID_CLASS:
9152 case RID_STRUCT:
9153 case RID_UNION:
a723baf1 9154 /* Parse tentatively so that we can back up if we don't find a
ff4eb0b5 9155 class-specifier. */
a723baf1 9156 cp_parser_parse_tentatively (parser);
ff4eb0b5
ZW
9157 /* Look for the class-specifier. */
9158 type_spec = cp_parser_class_specifier (parser);
a723baf1
MM
9159 /* If that worked, we're done. */
9160 if (cp_parser_parse_definitely (parser))
9161 {
9162 if (declares_class_or_enum)
560ad596 9163 *declares_class_or_enum = 2;
62d1db17
MM
9164 if (decl_specs)
9165 cp_parser_set_decl_spec_type (decl_specs,
9166 type_spec,
9167 /*user_defined_p=*/true);
a723baf1
MM
9168 return type_spec;
9169 }
9170
9171 /* Fall through. */
ff4eb0b5
ZW
9172 elaborated_type_specifier:
9173 /* We're declaring (not defining) a class or enum. */
9174 if (declares_class_or_enum)
9175 *declares_class_or_enum = 1;
a723baf1 9176
ff4eb0b5 9177 /* Fall through. */
a723baf1
MM
9178 case RID_TYPENAME:
9179 /* Look for an elaborated-type-specifier. */
98ca843c
EC
9180 type_spec
9181 = (cp_parser_elaborated_type_specifier
62d1db17
MM
9182 (parser,
9183 decl_specs && decl_specs->specs[(int) ds_friend],
9184 is_declaration));
62d1db17
MM
9185 if (decl_specs)
9186 cp_parser_set_decl_spec_type (decl_specs,
9187 type_spec,
9188 /*user_defined_p=*/true);
a723baf1
MM
9189 return type_spec;
9190
9191 case RID_CONST:
62d1db17
MM
9192 ds = ds_const;
9193 if (is_cv_qualifier)
9194 *is_cv_qualifier = true;
9195 break;
98ca843c 9196
a723baf1 9197 case RID_VOLATILE:
62d1db17 9198 ds = ds_volatile;
a723baf1
MM
9199 if (is_cv_qualifier)
9200 *is_cv_qualifier = true;
62d1db17 9201 break;
a723baf1 9202
62d1db17
MM
9203 case RID_RESTRICT:
9204 ds = ds_restrict;
9205 if (is_cv_qualifier)
9206 *is_cv_qualifier = true;
9207 break;
a723baf1
MM
9208
9209 case RID_COMPLEX:
9210 /* The `__complex__' keyword is a GNU extension. */
62d1db17
MM
9211 ds = ds_complex;
9212 break;
a723baf1
MM
9213
9214 default:
9215 break;
9216 }
9217
62d1db17
MM
9218 /* Handle simple keywords. */
9219 if (ds != ds_last)
9220 {
9221 if (decl_specs)
9222 {
9223 ++decl_specs->specs[(int)ds];
9224 decl_specs->any_specifiers_p = true;
9225 }
9226 return cp_lexer_consume_token (parser->lexer)->value;
9227 }
9228
a723baf1
MM
9229 /* If we do not already have a type-specifier, assume we are looking
9230 at a simple-type-specifier. */
98ca843c 9231 type_spec = cp_parser_simple_type_specifier (parser,
62d1db17
MM
9232 decl_specs,
9233 flags);
a723baf1
MM
9234
9235 /* If we didn't find a type-specifier, and a type-specifier was not
9236 optional in this context, issue an error message. */
9237 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9238 {
9239 cp_parser_error (parser, "expected type specifier");
9240 return error_mark_node;
9241 }
9242
9243 return type_spec;
9244}
9245
9246/* Parse a simple-type-specifier.
9247
9248 simple-type-specifier:
9249 :: [opt] nested-name-specifier [opt] type-name
9250 :: [opt] nested-name-specifier template template-id
9251 char
9252 wchar_t
9253 bool
9254 short
9255 int
9256 long
9257 signed
9258 unsigned
9259 float
9260 double
21526606 9261 void
a723baf1
MM
9262
9263 GNU Extension:
9264
9265 simple-type-specifier:
9266 __typeof__ unary-expression
9267 __typeof__ ( type-id )
9268
62d1db17
MM
9269 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9270 appropriately updated. */
a723baf1
MM
9271
9272static tree
98ca843c 9273cp_parser_simple_type_specifier (cp_parser* parser,
62d1db17
MM
9274 cp_decl_specifier_seq *decl_specs,
9275 cp_parser_flags flags)
a723baf1
MM
9276{
9277 tree type = NULL_TREE;
9278 cp_token *token;
9279
9280 /* Peek at the next token. */
9281 token = cp_lexer_peek_token (parser->lexer);
9282
9283 /* If we're looking at a keyword, things are easy. */
9284 switch (token->keyword)
9285 {
9286 case RID_CHAR:
62d1db17
MM
9287 if (decl_specs)
9288 decl_specs->explicit_char_p = true;
4b0d3cbe
MM
9289 type = char_type_node;
9290 break;
a723baf1 9291 case RID_WCHAR:
4b0d3cbe
MM
9292 type = wchar_type_node;
9293 break;
a723baf1 9294 case RID_BOOL:
4b0d3cbe
MM
9295 type = boolean_type_node;
9296 break;
a723baf1 9297 case RID_SHORT:
62d1db17
MM
9298 if (decl_specs)
9299 ++decl_specs->specs[(int) ds_short];
4b0d3cbe
MM
9300 type = short_integer_type_node;
9301 break;
a723baf1 9302 case RID_INT:
62d1db17
MM
9303 if (decl_specs)
9304 decl_specs->explicit_int_p = true;
4b0d3cbe
MM
9305 type = integer_type_node;
9306 break;
a723baf1 9307 case RID_LONG:
62d1db17
MM
9308 if (decl_specs)
9309 ++decl_specs->specs[(int) ds_long];
4b0d3cbe
MM
9310 type = long_integer_type_node;
9311 break;
a723baf1 9312 case RID_SIGNED:
62d1db17
MM
9313 if (decl_specs)
9314 ++decl_specs->specs[(int) ds_signed];
4b0d3cbe
MM
9315 type = integer_type_node;
9316 break;
a723baf1 9317 case RID_UNSIGNED:
62d1db17
MM
9318 if (decl_specs)
9319 ++decl_specs->specs[(int) ds_unsigned];
4b0d3cbe
MM
9320 type = unsigned_type_node;
9321 break;
a723baf1 9322 case RID_FLOAT:
4b0d3cbe
MM
9323 type = float_type_node;
9324 break;
a723baf1 9325 case RID_DOUBLE:
4b0d3cbe
MM
9326 type = double_type_node;
9327 break;
a723baf1 9328 case RID_VOID:
4b0d3cbe
MM
9329 type = void_type_node;
9330 break;
a723baf1
MM
9331
9332 case RID_TYPEOF:
62d1db17
MM
9333 /* Consume the `typeof' token. */
9334 cp_lexer_consume_token (parser->lexer);
9335 /* Parse the operand to `typeof'. */
9336 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9337 /* If it is not already a TYPE, take its type. */
9338 if (!TYPE_P (type))
9339 type = finish_typeof (type);
9340
9341 if (decl_specs)
9342 cp_parser_set_decl_spec_type (decl_specs, type,
9343 /*user_defined_p=*/true);
98ca843c 9344
62d1db17 9345 return type;
a723baf1
MM
9346
9347 default:
9348 break;
9349 }
9350
4b0d3cbe
MM
9351 /* If the type-specifier was for a built-in type, we're done. */
9352 if (type)
9353 {
9354 tree id;
9355
62d1db17
MM
9356 /* Record the type. */
9357 if (decl_specs
9358 && (token->keyword != RID_SIGNED
9359 && token->keyword != RID_UNSIGNED
9360 && token->keyword != RID_SHORT
9361 && token->keyword != RID_LONG))
98ca843c 9362 cp_parser_set_decl_spec_type (decl_specs,
62d1db17
MM
9363 type,
9364 /*user_defined=*/false);
9365 if (decl_specs)
9366 decl_specs->any_specifiers_p = true;
9367
4b0d3cbe
MM
9368 /* Consume the token. */
9369 id = cp_lexer_consume_token (parser->lexer)->value;
0d956474
GB
9370
9371 /* There is no valid C++ program where a non-template type is
9372 followed by a "<". That usually indicates that the user thought
9373 that the type was a template. */
9374 cp_parser_check_for_invalid_template_id (parser, type);
9375
62d1db17 9376 return TYPE_NAME (type);
4b0d3cbe
MM
9377 }
9378
a723baf1 9379 /* The type-specifier must be a user-defined type. */
21526606 9380 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
a723baf1 9381 {
0c1a1ecd 9382 bool qualified_p;
f68e4dc8 9383 bool global_p;
0c1a1ecd 9384
a723baf1
MM
9385 /* Don't gobble tokens or issue error messages if this is an
9386 optional type-specifier. */
9387 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9388 cp_parser_parse_tentatively (parser);
9389
9390 /* Look for the optional `::' operator. */
f68e4dc8 9391 global_p
da740453
MM
9392 = (cp_parser_global_scope_opt (parser,
9393 /*current_scope_valid_p=*/false)
9394 != NULL_TREE);
a723baf1 9395 /* Look for the nested-name specifier. */
0c1a1ecd
MM
9396 qualified_p
9397 = (cp_parser_nested_name_specifier_opt (parser,
9398 /*typename_keyword_p=*/false,
9399 /*check_dependency_p=*/true,
9400 /*type_p=*/false,
6661a85f
EB
9401 /*is_declaration=*/false)
9402 != NULL_TREE);
a723baf1
MM
9403 /* If we have seen a nested-name-specifier, and the next token
9404 is `template', then we are using the template-id production. */
21526606 9405 if (parser->scope
a723baf1
MM
9406 && cp_parser_optional_template_keyword (parser))
9407 {
9408 /* Look for the template-id. */
21526606 9409 type = cp_parser_template_id (parser,
a723baf1 9410 /*template_keyword_p=*/true,
a668c6ad
MM
9411 /*check_dependency_p=*/true,
9412 /*is_declaration=*/false);
a723baf1
MM
9413 /* If the template-id did not name a type, we are out of
9414 luck. */
9415 if (TREE_CODE (type) != TYPE_DECL)
9416 {
9417 cp_parser_error (parser, "expected template-id for type");
9418 type = NULL_TREE;
9419 }
9420 }
9421 /* Otherwise, look for a type-name. */
9422 else
4bb8ca28 9423 type = cp_parser_type_name (parser);
0c1a1ecd 9424 /* Keep track of all name-lookups performed in class scopes. */
98ca843c 9425 if (type
f68e4dc8 9426 && !global_p
0c1a1ecd 9427 && !qualified_p
98ca843c 9428 && TREE_CODE (type) == TYPE_DECL
0c1a1ecd
MM
9429 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9430 maybe_note_name_used_in_class (DECL_NAME (type), type);
a723baf1 9431 /* If it didn't work out, we don't have a TYPE. */
21526606 9432 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
a723baf1
MM
9433 && !cp_parser_parse_definitely (parser))
9434 type = NULL_TREE;
62d1db17
MM
9435 if (type && decl_specs)
9436 cp_parser_set_decl_spec_type (decl_specs, type,
9437 /*user_defined=*/true);
a723baf1
MM
9438 }
9439
9440 /* If we didn't get a type-name, issue an error message. */
9441 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9442 {
9443 cp_parser_error (parser, "expected type-name");
9444 return error_mark_node;
9445 }
9446
a668c6ad
MM
9447 /* There is no valid C++ program where a non-template type is
9448 followed by a "<". That usually indicates that the user thought
9449 that the type was a template. */
4bb8ca28 9450 if (type && type != error_mark_node)
ee43dab5 9451 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
ec75414f 9452
a723baf1
MM
9453 return type;
9454}
9455
9456/* Parse a type-name.
9457
9458 type-name:
9459 class-name
9460 enum-name
21526606 9461 typedef-name
a723baf1
MM
9462
9463 enum-name:
9464 identifier
9465
9466 typedef-name:
21526606 9467 identifier
a723baf1
MM
9468
9469 Returns a TYPE_DECL for the the type. */
9470
9471static tree
94edc4ab 9472cp_parser_type_name (cp_parser* parser)
a723baf1
MM
9473{
9474 tree type_decl;
9475 tree identifier;
9476
9477 /* We can't know yet whether it is a class-name or not. */
9478 cp_parser_parse_tentatively (parser);
9479 /* Try a class-name. */
21526606 9480 type_decl = cp_parser_class_name (parser,
a723baf1
MM
9481 /*typename_keyword_p=*/false,
9482 /*template_keyword_p=*/false,
fc6a28d7 9483 none_type,
a723baf1 9484 /*check_dependency_p=*/true,
a668c6ad
MM
9485 /*class_head_p=*/false,
9486 /*is_declaration=*/false);
a723baf1
MM
9487 /* If it's not a class-name, keep looking. */
9488 if (!cp_parser_parse_definitely (parser))
9489 {
9490 /* It must be a typedef-name or an enum-name. */
9491 identifier = cp_parser_identifier (parser);
9492 if (identifier == error_mark_node)
9493 return error_mark_node;
21526606 9494
a723baf1
MM
9495 /* Look up the type-name. */
9496 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9497 /* Issue an error if we did not find a type-name. */
9498 if (TREE_CODE (type_decl) != TYPE_DECL)
9499 {
4bb8ca28 9500 if (!cp_parser_simulate_error (parser))
21526606 9501 cp_parser_name_lookup_error (parser, identifier, type_decl,
4bb8ca28 9502 "is not a type");
a723baf1
MM
9503 type_decl = error_mark_node;
9504 }
9505 /* Remember that the name was used in the definition of the
9506 current class so that we can check later to see if the
9507 meaning would have been different after the class was
9508 entirely defined. */
9509 else if (type_decl != error_mark_node
9510 && !parser->scope)
9511 maybe_note_name_used_in_class (identifier, type_decl);
9512 }
21526606 9513
a723baf1
MM
9514 return type_decl;
9515}
9516
9517
9518/* Parse an elaborated-type-specifier. Note that the grammar given
9519 here incorporates the resolution to DR68.
9520
9521 elaborated-type-specifier:
9522 class-key :: [opt] nested-name-specifier [opt] identifier
9523 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9524 enum :: [opt] nested-name-specifier [opt] identifier
9525 typename :: [opt] nested-name-specifier identifier
21526606
EC
9526 typename :: [opt] nested-name-specifier template [opt]
9527 template-id
a723baf1 9528
360d1b99
MM
9529 GNU extension:
9530
9531 elaborated-type-specifier:
9532 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21526606 9533 class-key attributes :: [opt] nested-name-specifier [opt]
360d1b99
MM
9534 template [opt] template-id
9535 enum attributes :: [opt] nested-name-specifier [opt] identifier
9536
a723baf1
MM
9537 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9538 declared `friend'. If IS_DECLARATION is TRUE, then this
9539 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9540 something is being declared.
9541
9542 Returns the TYPE specified. */
9543
9544static tree
21526606
EC
9545cp_parser_elaborated_type_specifier (cp_parser* parser,
9546 bool is_friend,
94edc4ab 9547 bool is_declaration)
a723baf1
MM
9548{
9549 enum tag_types tag_type;
9550 tree identifier;
9551 tree type = NULL_TREE;
360d1b99 9552 tree attributes = NULL_TREE;
a723baf1
MM
9553
9554 /* See if we're looking at the `enum' keyword. */
9555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9556 {
9557 /* Consume the `enum' token. */
9558 cp_lexer_consume_token (parser->lexer);
9559 /* Remember that it's an enumeration type. */
9560 tag_type = enum_type;
360d1b99
MM
9561 /* Parse the attributes. */
9562 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9563 }
9564 /* Or, it might be `typename'. */
9565 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9566 RID_TYPENAME))
9567 {
9568 /* Consume the `typename' token. */
9569 cp_lexer_consume_token (parser->lexer);
9570 /* Remember that it's a `typename' type. */
9571 tag_type = typename_type;
9572 /* The `typename' keyword is only allowed in templates. */
9573 if (!processing_template_decl)
2a13a625 9574 pedwarn ("using %<typename%> outside of template");
a723baf1
MM
9575 }
9576 /* Otherwise it must be a class-key. */
9577 else
9578 {
9579 tag_type = cp_parser_class_key (parser);
9580 if (tag_type == none_type)
9581 return error_mark_node;
360d1b99
MM
9582 /* Parse the attributes. */
9583 attributes = cp_parser_attributes_opt (parser);
a723baf1
MM
9584 }
9585
9586 /* Look for the `::' operator. */
21526606 9587 cp_parser_global_scope_opt (parser,
a723baf1
MM
9588 /*current_scope_valid_p=*/false);
9589 /* Look for the nested-name-specifier. */
9590 if (tag_type == typename_type)
8fa1ad0e
MM
9591 {
9592 if (cp_parser_nested_name_specifier (parser,
9593 /*typename_keyword_p=*/true,
9594 /*check_dependency_p=*/true,
a668c6ad 9595 /*type_p=*/true,
21526606 9596 is_declaration)
8fa1ad0e
MM
9597 == error_mark_node)
9598 return error_mark_node;
9599 }
a723baf1
MM
9600 else
9601 /* Even though `typename' is not present, the proposed resolution
9602 to Core Issue 180 says that in `class A<T>::B', `B' should be
9603 considered a type-name, even if `A<T>' is dependent. */
9604 cp_parser_nested_name_specifier_opt (parser,
9605 /*typename_keyword_p=*/true,
9606 /*check_dependency_p=*/true,
a668c6ad
MM
9607 /*type_p=*/true,
9608 is_declaration);
a723baf1
MM
9609 /* For everything but enumeration types, consider a template-id. */
9610 if (tag_type != enum_type)
9611 {
9612 bool template_p = false;
9613 tree decl;
9614
9615 /* Allow the `template' keyword. */
9616 template_p = cp_parser_optional_template_keyword (parser);
9617 /* If we didn't see `template', we don't know if there's a
9618 template-id or not. */
9619 if (!template_p)
9620 cp_parser_parse_tentatively (parser);
9621 /* Parse the template-id. */
9622 decl = cp_parser_template_id (parser, template_p,
a668c6ad
MM
9623 /*check_dependency_p=*/true,
9624 is_declaration);
a723baf1
MM
9625 /* If we didn't find a template-id, look for an ordinary
9626 identifier. */
9627 if (!template_p && !cp_parser_parse_definitely (parser))
9628 ;
9629 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9630 in effect, then we must assume that, upon instantiation, the
9631 template will correspond to a class. */
9632 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9633 && tag_type == typename_type)
9634 type = make_typename_type (parser->scope, decl,
fc6a28d7 9635 typename_type,
a723baf1 9636 /*complain=*/1);
21526606 9637 else
a723baf1
MM
9638 type = TREE_TYPE (decl);
9639 }
9640
9641 /* For an enumeration type, consider only a plain identifier. */
9642 if (!type)
9643 {
9644 identifier = cp_parser_identifier (parser);
9645
9646 if (identifier == error_mark_node)
eb5abb39
NS
9647 {
9648 parser->scope = NULL_TREE;
9649 return error_mark_node;
9650 }
a723baf1
MM
9651
9652 /* For a `typename', we needn't call xref_tag. */
0c88d886
MM
9653 if (tag_type == typename_type
9654 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21526606 9655 return cp_parser_make_typename_type (parser, parser->scope,
2097b5f2 9656 identifier);
a723baf1
MM
9657 /* Look up a qualified name in the usual way. */
9658 if (parser->scope)
9659 {
9660 tree decl;
9661
21526606 9662 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9663 tag_type,
b0bc6e8e 9664 /*is_template=*/false,
eea9800f 9665 /*is_namespace=*/false,
8f78f01f
MM
9666 /*check_dependency=*/true,
9667 /*ambiguous_p=*/NULL);
710b73e6
KL
9668
9669 /* If we are parsing friend declaration, DECL may be a
9670 TEMPLATE_DECL tree node here. However, we need to check
9671 whether this TEMPLATE_DECL results in valid code. Consider
9672 the following example:
9673
9674 namespace N {
9675 template <class T> class C {};
9676 }
9677 class X {
9678 template <class T> friend class N::C; // #1, valid code
9679 };
9680 template <class T> class Y {
9681 friend class N::C; // #2, invalid code
9682 };
9683
9684 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9685 name lookup of `N::C'. We see that friend declaration must
9686 be template for the code to be valid. Note that
9687 processing_template_decl does not work here since it is
9688 always 1 for the above two cases. */
9689
21526606 9690 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
9691 (decl, /*tag_name_p=*/is_friend
9692 && parser->num_template_parameter_lists));
a723baf1
MM
9693
9694 if (TREE_CODE (decl) != TYPE_DECL)
9695 {
0c88d886
MM
9696 cp_parser_diagnose_invalid_type_name (parser,
9697 parser->scope,
9698 identifier);
a723baf1
MM
9699 return error_mark_node;
9700 }
560ad596
MM
9701
9702 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21526606 9703 check_elaborated_type_specifier
4b0d3cbe 9704 (tag_type, decl,
560ad596
MM
9705 (parser->num_template_parameter_lists
9706 || DECL_SELF_REFERENCE_P (decl)));
a723baf1
MM
9707
9708 type = TREE_TYPE (decl);
9709 }
21526606 9710 else
a723baf1
MM
9711 {
9712 /* An elaborated-type-specifier sometimes introduces a new type and
9713 sometimes names an existing type. Normally, the rule is that it
9714 introduces a new type only if there is not an existing type of
9715 the same name already in scope. For example, given:
9716
9717 struct S {};
9718 void f() { struct S s; }
9719
9720 the `struct S' in the body of `f' is the same `struct S' as in
9721 the global scope; the existing definition is used. However, if
21526606 9722 there were no global declaration, this would introduce a new
a723baf1
MM
9723 local class named `S'.
9724
9725 An exception to this rule applies to the following code:
9726
9727 namespace N { struct S; }
9728
9729 Here, the elaborated-type-specifier names a new type
9730 unconditionally; even if there is already an `S' in the
9731 containing scope this declaration names a new type.
9732 This exception only applies if the elaborated-type-specifier
9733 forms the complete declaration:
9734
21526606 9735 [class.name]
a723baf1
MM
9736
9737 A declaration consisting solely of `class-key identifier ;' is
9738 either a redeclaration of the name in the current scope or a
9739 forward declaration of the identifier as a class name. It
9740 introduces the name into the current scope.
9741
9742 We are in this situation precisely when the next token is a `;'.
9743
9744 An exception to the exception is that a `friend' declaration does
9745 *not* name a new type; i.e., given:
9746
9747 struct S { friend struct T; };
9748
21526606 9749 `T' is not a new type in the scope of `S'.
a723baf1
MM
9750
9751 Also, `new struct S' or `sizeof (struct S)' never results in the
9752 definition of a new type; a new type can only be declared in a
9bcb9aae 9753 declaration context. */
a723baf1 9754
29ef83de
KL
9755 tag_scope ts;
9756 if (is_friend)
9757 /* Friends have special name lookup rules. */
9758 ts = ts_within_enclosing_non_class;
9759 else if (is_declaration
9760 && cp_lexer_next_token_is (parser->lexer,
9761 CPP_SEMICOLON))
9762 /* This is a `class-key identifier ;' */
9763 ts = ts_current;
9764 else
9765 ts = ts_global;
9766
e0fed25b
DS
9767 /* Warn about attributes. They are ignored. */
9768 if (attributes)
9769 warning ("type attributes are honored only at type definition");
9770
29ef83de 9771 type = xref_tag (tag_type, identifier, ts,
cbd63935 9772 parser->num_template_parameter_lists);
a723baf1
MM
9773 }
9774 }
9775 if (tag_type != enum_type)
9776 cp_parser_check_class_key (tag_type, type);
ee43dab5
MM
9777
9778 /* A "<" cannot follow an elaborated type specifier. If that
9779 happens, the user was probably trying to form a template-id. */
9780 cp_parser_check_for_invalid_template_id (parser, type);
9781
a723baf1
MM
9782 return type;
9783}
9784
9785/* Parse an enum-specifier.
9786
9787 enum-specifier:
9788 enum identifier [opt] { enumerator-list [opt] }
9789
f6af9a15
MA
9790 GNU Extensions:
9791 enum identifier [opt] { enumerator-list [opt] } attributes
9792
a723baf1
MM
9793 Returns an ENUM_TYPE representing the enumeration. */
9794
9795static tree
94edc4ab 9796cp_parser_enum_specifier (cp_parser* parser)
a723baf1 9797{
ff4eb0b5 9798 tree identifier;
a723baf1
MM
9799 tree type;
9800
ff4eb0b5
ZW
9801 /* Caller guarantees that the current token is 'enum', an identifier
9802 possibly follows, and the token after that is an opening brace.
9803 If we don't have an identifier, fabricate an anonymous name for
9804 the enumeration being defined. */
9805 cp_lexer_consume_token (parser->lexer);
a723baf1 9806
ff4eb0b5 9807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
a723baf1 9808 identifier = cp_parser_identifier (parser);
ff4eb0b5
ZW
9809 else
9810 identifier = make_anon_name ();
a723baf1 9811
a723baf1
MM
9812 /* Issue an error message if type-definitions are forbidden here. */
9813 cp_parser_check_type_definition (parser);
9814
2cfe82fe
ZW
9815 /* Create the new type. We do this before consuming the opening brace
9816 so the enum will be recorded as being on the line of its tag (or the
9817 'enum' keyword, if there is no tag). */
ff4eb0b5 9818 type = start_enum (identifier);
a723baf1 9819
2cfe82fe
ZW
9820 /* Consume the opening brace. */
9821 cp_lexer_consume_token (parser->lexer);
9822
ff4eb0b5
ZW
9823 /* If the next token is not '}', then there are some enumerators. */
9824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
a723baf1 9825 cp_parser_enumerator_list (parser, type);
ff4eb0b5
ZW
9826
9827 /* Consume the final '}'. */
a723baf1
MM
9828 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9829
f6af9a15 9830 /* Look for trailing attributes to apply to this enumeration, and
03fd3f84 9831 apply them if appropriate. */
f6af9a15
MA
9832 if (cp_parser_allow_gnu_extensions_p (parser))
9833 {
9834 tree trailing_attr = cp_parser_attributes_opt (parser);
9835 cplus_decl_attributes (&type,
9836 trailing_attr,
9837 (int) ATTR_FLAG_TYPE_IN_PLACE);
9838 }
9839
a723baf1
MM
9840 /* Finish up the enumeration. */
9841 finish_enum (type);
9842
9843 return type;
9844}
9845
9846/* Parse an enumerator-list. The enumerators all have the indicated
21526606 9847 TYPE.
a723baf1
MM
9848
9849 enumerator-list:
9850 enumerator-definition
9851 enumerator-list , enumerator-definition */
9852
9853static void
94edc4ab 9854cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9855{
9856 while (true)
9857 {
a723baf1
MM
9858 /* Parse an enumerator-definition. */
9859 cp_parser_enumerator_definition (parser, type);
ff4eb0b5
ZW
9860
9861 /* If the next token is not a ',', we've reached the end of
9862 the list. */
9863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
a723baf1
MM
9864 break;
9865 /* Otherwise, consume the `,' and keep going. */
9866 cp_lexer_consume_token (parser->lexer);
9867 /* If the next token is a `}', there is a trailing comma. */
9868 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9869 {
9870 if (pedantic && !in_system_header)
9871 pedwarn ("comma at end of enumerator list");
9872 break;
9873 }
9874 }
9875}
9876
9877/* Parse an enumerator-definition. The enumerator has the indicated
9878 TYPE.
9879
9880 enumerator-definition:
9881 enumerator
9882 enumerator = constant-expression
21526606 9883
a723baf1
MM
9884 enumerator:
9885 identifier */
9886
9887static void
94edc4ab 9888cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1 9889{
a723baf1
MM
9890 tree identifier;
9891 tree value;
9892
9893 /* Look for the identifier. */
9894 identifier = cp_parser_identifier (parser);
9895 if (identifier == error_mark_node)
9896 return;
21526606 9897
ff4eb0b5
ZW
9898 /* If the next token is an '=', then there is an explicit value. */
9899 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
a723baf1
MM
9900 {
9901 /* Consume the `=' token. */
9902 cp_lexer_consume_token (parser->lexer);
9903 /* Parse the value. */
21526606 9904 value = cp_parser_constant_expression (parser,
d17811fd 9905 /*allow_non_constant_p=*/false,
14d22dd6 9906 NULL);
a723baf1
MM
9907 }
9908 else
9909 value = NULL_TREE;
9910
9911 /* Create the enumerator. */
9912 build_enumerator (identifier, value, type);
9913}
9914
9915/* Parse a namespace-name.
9916
9917 namespace-name:
9918 original-namespace-name
9919 namespace-alias
9920
9921 Returns the NAMESPACE_DECL for the namespace. */
9922
9923static tree
94edc4ab 9924cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9925{
9926 tree identifier;
9927 tree namespace_decl;
9928
9929 /* Get the name of the namespace. */
9930 identifier = cp_parser_identifier (parser);
9931 if (identifier == error_mark_node)
9932 return error_mark_node;
9933
eea9800f
MM
9934 /* Look up the identifier in the currently active scope. Look only
9935 for namespaces, due to:
9936
9937 [basic.lookup.udir]
9938
9939 When looking up a namespace-name in a using-directive or alias
21526606 9940 definition, only namespace names are considered.
eea9800f
MM
9941
9942 And:
9943
9944 [basic.lookup.qual]
9945
9946 During the lookup of a name preceding the :: scope resolution
21526606 9947 operator, object, function, and enumerator names are ignored.
eea9800f
MM
9948
9949 (Note that cp_parser_class_or_namespace_name only calls this
9950 function if the token after the name is the scope resolution
9951 operator.) */
9952 namespace_decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 9953 none_type,
b0bc6e8e 9954 /*is_template=*/false,
eea9800f 9955 /*is_namespace=*/true,
8f78f01f
MM
9956 /*check_dependency=*/true,
9957 /*ambiguous_p=*/NULL);
a723baf1
MM
9958 /* If it's not a namespace, issue an error. */
9959 if (namespace_decl == error_mark_node
9960 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9961 {
9962 cp_parser_error (parser, "expected namespace-name");
9963 namespace_decl = error_mark_node;
9964 }
21526606 9965
a723baf1
MM
9966 return namespace_decl;
9967}
9968
9969/* Parse a namespace-definition.
9970
9971 namespace-definition:
9972 named-namespace-definition
21526606 9973 unnamed-namespace-definition
a723baf1
MM
9974
9975 named-namespace-definition:
9976 original-namespace-definition
9977 extension-namespace-definition
9978
9979 original-namespace-definition:
9980 namespace identifier { namespace-body }
21526606 9981
a723baf1
MM
9982 extension-namespace-definition:
9983 namespace original-namespace-name { namespace-body }
21526606 9984
a723baf1
MM
9985 unnamed-namespace-definition:
9986 namespace { namespace-body } */
9987
9988static void
94edc4ab 9989cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9990{
9991 tree identifier;
9992
9993 /* Look for the `namespace' keyword. */
9994 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9995
9996 /* Get the name of the namespace. We do not attempt to distinguish
9997 between an original-namespace-definition and an
9998 extension-namespace-definition at this point. The semantic
9999 analysis routines are responsible for that. */
10000 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10001 identifier = cp_parser_identifier (parser);
10002 else
10003 identifier = NULL_TREE;
10004
10005 /* Look for the `{' to start the namespace. */
10006 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10007 /* Start the namespace. */
10008 push_namespace (identifier);
10009 /* Parse the body of the namespace. */
10010 cp_parser_namespace_body (parser);
10011 /* Finish the namespace. */
10012 pop_namespace ();
10013 /* Look for the final `}'. */
10014 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10015}
10016
10017/* Parse a namespace-body.
10018
10019 namespace-body:
10020 declaration-seq [opt] */
10021
10022static void
94edc4ab 10023cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
10024{
10025 cp_parser_declaration_seq_opt (parser);
10026}
10027
10028/* Parse a namespace-alias-definition.
10029
10030 namespace-alias-definition:
10031 namespace identifier = qualified-namespace-specifier ; */
10032
10033static void
94edc4ab 10034cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
10035{
10036 tree identifier;
10037 tree namespace_specifier;
10038
10039 /* Look for the `namespace' keyword. */
10040 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10041 /* Look for the identifier. */
10042 identifier = cp_parser_identifier (parser);
10043 if (identifier == error_mark_node)
10044 return;
10045 /* Look for the `=' token. */
10046 cp_parser_require (parser, CPP_EQ, "`='");
10047 /* Look for the qualified-namespace-specifier. */
21526606 10048 namespace_specifier
a723baf1
MM
10049 = cp_parser_qualified_namespace_specifier (parser);
10050 /* Look for the `;' token. */
10051 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10052
10053 /* Register the alias in the symbol table. */
10054 do_namespace_alias (identifier, namespace_specifier);
10055}
10056
10057/* Parse a qualified-namespace-specifier.
10058
10059 qualified-namespace-specifier:
10060 :: [opt] nested-name-specifier [opt] namespace-name
10061
10062 Returns a NAMESPACE_DECL corresponding to the specified
10063 namespace. */
10064
10065static tree
94edc4ab 10066cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
10067{
10068 /* Look for the optional `::'. */
21526606 10069 cp_parser_global_scope_opt (parser,
a723baf1
MM
10070 /*current_scope_valid_p=*/false);
10071
10072 /* Look for the optional nested-name-specifier. */
10073 cp_parser_nested_name_specifier_opt (parser,
10074 /*typename_keyword_p=*/false,
10075 /*check_dependency_p=*/true,
a668c6ad
MM
10076 /*type_p=*/false,
10077 /*is_declaration=*/true);
a723baf1
MM
10078
10079 return cp_parser_namespace_name (parser);
10080}
10081
10082/* Parse a using-declaration.
10083
10084 using-declaration:
10085 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10086 using :: unqualified-id ; */
10087
10088static void
94edc4ab 10089cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
10090{
10091 cp_token *token;
10092 bool typename_p = false;
10093 bool global_scope_p;
10094 tree decl;
10095 tree identifier;
ed5f054f 10096 tree qscope;
a723baf1
MM
10097
10098 /* Look for the `using' keyword. */
10099 cp_parser_require_keyword (parser, RID_USING, "`using'");
21526606 10100
a723baf1
MM
10101 /* Peek at the next token. */
10102 token = cp_lexer_peek_token (parser->lexer);
10103 /* See if it's `typename'. */
10104 if (token->keyword == RID_TYPENAME)
10105 {
10106 /* Remember that we've seen it. */
10107 typename_p = true;
10108 /* Consume the `typename' token. */
10109 cp_lexer_consume_token (parser->lexer);
10110 }
10111
10112 /* Look for the optional global scope qualification. */
21526606 10113 global_scope_p
a723baf1 10114 = (cp_parser_global_scope_opt (parser,
21526606 10115 /*current_scope_valid_p=*/false)
a723baf1
MM
10116 != NULL_TREE);
10117
10118 /* If we saw `typename', or didn't see `::', then there must be a
10119 nested-name-specifier present. */
10120 if (typename_p || !global_scope_p)
21526606 10121 qscope = cp_parser_nested_name_specifier (parser, typename_p,
ed5f054f
AO
10122 /*check_dependency_p=*/true,
10123 /*type_p=*/false,
10124 /*is_declaration=*/true);
a723baf1
MM
10125 /* Otherwise, we could be in either of the two productions. In that
10126 case, treat the nested-name-specifier as optional. */
10127 else
ed5f054f
AO
10128 qscope = cp_parser_nested_name_specifier_opt (parser,
10129 /*typename_keyword_p=*/false,
10130 /*check_dependency_p=*/true,
10131 /*type_p=*/false,
10132 /*is_declaration=*/true);
10133 if (!qscope)
10134 qscope = global_namespace;
a723baf1
MM
10135
10136 /* Parse the unqualified-id. */
21526606 10137 identifier = cp_parser_unqualified_id (parser,
a723baf1 10138 /*template_keyword_p=*/false,
f3c2dfc6
MM
10139 /*check_dependency_p=*/true,
10140 /*declarator_p=*/true);
a723baf1
MM
10141
10142 /* The function we call to handle a using-declaration is different
10143 depending on what scope we are in. */
f3c2dfc6
MM
10144 if (identifier == error_mark_node)
10145 ;
10146 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10147 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10148 /* [namespace.udecl]
10149
10150 A using declaration shall not name a template-id. */
10151 error ("a template-id may not appear in a using-declaration");
a723baf1
MM
10152 else
10153 {
a5201a91 10154 if (at_class_scope_p ())
4eb6d609 10155 {
f3c2dfc6
MM
10156 /* Create the USING_DECL. */
10157 decl = do_class_using_decl (build_nt (SCOPE_REF,
10158 parser->scope,
10159 identifier));
10160 /* Add it to the list of members in this class. */
10161 finish_member_declaration (decl);
4eb6d609 10162 }
a723baf1 10163 else
f3c2dfc6
MM
10164 {
10165 decl = cp_parser_lookup_name_simple (parser, identifier);
10166 if (decl == error_mark_node)
4bb8ca28 10167 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
a5201a91 10168 else if (!at_namespace_scope_p ())
ed5f054f 10169 do_local_using_decl (decl, qscope, identifier);
f3c2dfc6 10170 else
ed5f054f 10171 do_toplevel_using_decl (decl, qscope, identifier);
f3c2dfc6 10172 }
a723baf1
MM
10173 }
10174
10175 /* Look for the final `;'. */
10176 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10177}
10178
21526606
EC
10179/* Parse a using-directive.
10180
a723baf1
MM
10181 using-directive:
10182 using namespace :: [opt] nested-name-specifier [opt]
10183 namespace-name ; */
10184
10185static void
94edc4ab 10186cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
10187{
10188 tree namespace_decl;
86098eb8 10189 tree attribs;
a723baf1
MM
10190
10191 /* Look for the `using' keyword. */
10192 cp_parser_require_keyword (parser, RID_USING, "`using'");
10193 /* And the `namespace' keyword. */
10194 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10195 /* Look for the optional `::' operator. */
10196 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
34cd5ae7 10197 /* And the optional nested-name-specifier. */
a723baf1
MM
10198 cp_parser_nested_name_specifier_opt (parser,
10199 /*typename_keyword_p=*/false,
10200 /*check_dependency_p=*/true,
a668c6ad
MM
10201 /*type_p=*/false,
10202 /*is_declaration=*/true);
a723baf1
MM
10203 /* Get the namespace being used. */
10204 namespace_decl = cp_parser_namespace_name (parser);
86098eb8
JM
10205 /* And any specified attributes. */
10206 attribs = cp_parser_attributes_opt (parser);
a723baf1 10207 /* Update the symbol table. */
86098eb8 10208 parse_using_directive (namespace_decl, attribs);
a723baf1
MM
10209 /* Look for the final `;'. */
10210 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10211}
10212
10213/* Parse an asm-definition.
10214
10215 asm-definition:
21526606 10216 asm ( string-literal ) ;
a723baf1
MM
10217
10218 GNU Extension:
10219
10220 asm-definition:
10221 asm volatile [opt] ( string-literal ) ;
10222 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10223 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10224 : asm-operand-list [opt] ) ;
21526606
EC
10225 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10226 : asm-operand-list [opt]
a723baf1
MM
10227 : asm-operand-list [opt] ) ; */
10228
10229static void
94edc4ab 10230cp_parser_asm_definition (cp_parser* parser)
a723baf1 10231{
a723baf1
MM
10232 tree string;
10233 tree outputs = NULL_TREE;
10234 tree inputs = NULL_TREE;
10235 tree clobbers = NULL_TREE;
10236 tree asm_stmt;
10237 bool volatile_p = false;
10238 bool extended_p = false;
10239
10240 /* Look for the `asm' keyword. */
10241 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10242 /* See if the next token is `volatile'. */
10243 if (cp_parser_allow_gnu_extensions_p (parser)
10244 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10245 {
10246 /* Remember that we saw the `volatile' keyword. */
10247 volatile_p = true;
10248 /* Consume the token. */
10249 cp_lexer_consume_token (parser->lexer);
10250 }
10251 /* Look for the opening `('. */
c162c75e
MA
10252 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10253 return;
a723baf1 10254 /* Look for the string. */
c162c75e
MA
10255 string = cp_parser_string_literal (parser, false, false);
10256 if (string == error_mark_node)
10257 {
10258 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10259 /*consume_paren=*/true);
10260 return;
10261 }
10262
a723baf1 10263 /* If we're allowing GNU extensions, check for the extended assembly
21526606 10264 syntax. Unfortunately, the `:' tokens need not be separated by
a723baf1
MM
10265 a space in C, and so, for compatibility, we tolerate that here
10266 too. Doing that means that we have to treat the `::' operator as
10267 two `:' tokens. */
10268 if (cp_parser_allow_gnu_extensions_p (parser)
10269 && at_function_scope_p ()
10270 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10271 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10272 {
10273 bool inputs_p = false;
10274 bool clobbers_p = false;
10275
10276 /* The extended syntax was used. */
10277 extended_p = true;
10278
10279 /* Look for outputs. */
10280 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10281 {
10282 /* Consume the `:'. */
10283 cp_lexer_consume_token (parser->lexer);
10284 /* Parse the output-operands. */
21526606 10285 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1
MM
10286 CPP_COLON)
10287 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
10288 CPP_SCOPE)
10289 && cp_lexer_next_token_is_not (parser->lexer,
10290 CPP_CLOSE_PAREN))
a723baf1
MM
10291 outputs = cp_parser_asm_operand_list (parser);
10292 }
10293 /* If the next token is `::', there are no outputs, and the
10294 next token is the beginning of the inputs. */
10295 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
bf5930d4
JB
10296 /* The inputs are coming next. */
10297 inputs_p = true;
a723baf1
MM
10298
10299 /* Look for inputs. */
10300 if (inputs_p
10301 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10302 {
bf5930d4
JB
10303 /* Consume the `:' or `::'. */
10304 cp_lexer_consume_token (parser->lexer);
a723baf1 10305 /* Parse the output-operands. */
21526606 10306 if (cp_lexer_next_token_is_not (parser->lexer,
a723baf1 10307 CPP_COLON)
8caf4c38
MM
10308 && cp_lexer_next_token_is_not (parser->lexer,
10309 CPP_CLOSE_PAREN))
a723baf1
MM
10310 inputs = cp_parser_asm_operand_list (parser);
10311 }
10312 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10313 /* The clobbers are coming next. */
10314 clobbers_p = true;
10315
10316 /* Look for clobbers. */
21526606 10317 if (clobbers_p
a723baf1
MM
10318 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10319 {
bf5930d4
JB
10320 /* Consume the `:' or `::'. */
10321 cp_lexer_consume_token (parser->lexer);
a723baf1 10322 /* Parse the clobbers. */
8caf4c38
MM
10323 if (cp_lexer_next_token_is_not (parser->lexer,
10324 CPP_CLOSE_PAREN))
10325 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
10326 }
10327 }
10328 /* Look for the closing `)'. */
10329 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
a668c6ad
MM
10330 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10331 /*consume_paren=*/true);
a723baf1
MM
10332 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10333
e130a54b 10334 /* Create the ASM_EXPR. */
a723baf1
MM
10335 if (at_function_scope_p ())
10336 {
6de9cd9a
DN
10337 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10338 inputs, clobbers);
e130a54b 10339 /* If the extended syntax was not used, mark the ASM_EXPR. */
a723baf1 10340 if (!extended_p)
ca059043
AP
10341 {
10342 tree temp = asm_stmt;
10343 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10344 temp = TREE_OPERAND (temp, 0);
10345
10346 ASM_INPUT_P (temp) = 1;
10347 }
a723baf1
MM
10348 }
10349 else
10350 assemble_asm (string);
10351}
10352
10353/* Declarators [gram.dcl.decl] */
10354
10355/* Parse an init-declarator.
10356
10357 init-declarator:
10358 declarator initializer [opt]
10359
10360 GNU Extension:
10361
10362 init-declarator:
10363 declarator asm-specification [opt] attributes [opt] initializer [opt]
10364
4bb8ca28
MM
10365 function-definition:
10366 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21526606
EC
10367 function-body
10368 decl-specifier-seq [opt] declarator function-try-block
4bb8ca28
MM
10369
10370 GNU Extension:
10371
10372 function-definition:
21526606 10373 __extension__ function-definition
4bb8ca28 10374
a723baf1 10375 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 10376 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
10377 then this declarator appears in a class scope. The new DECL created
10378 by this declarator is returned.
a723baf1
MM
10379
10380 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10381 for a function-definition here as well. If the declarator is a
10382 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10383 be TRUE upon return. By that point, the function-definition will
10384 have been completely parsed.
10385
10386 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10387 is FALSE. */
10388
10389static tree
21526606 10390cp_parser_init_declarator (cp_parser* parser,
62d1db17 10391 cp_decl_specifier_seq *decl_specifiers,
94edc4ab
NN
10392 bool function_definition_allowed_p,
10393 bool member_p,
560ad596 10394 int declares_class_or_enum,
94edc4ab 10395 bool* function_definition_p)
a723baf1
MM
10396{
10397 cp_token *token;
058b15c1 10398 cp_declarator *declarator;
62d1db17 10399 tree prefix_attributes;
a723baf1
MM
10400 tree attributes;
10401 tree asm_specification;
10402 tree initializer;
10403 tree decl = NULL_TREE;
10404 tree scope;
a723baf1
MM
10405 bool is_initialized;
10406 bool is_parenthesized_init;
39703eb9 10407 bool is_non_constant_init;
7efa3e22 10408 int ctor_dtor_or_conv_p;
a723baf1 10409 bool friend_p;
91b004e5 10410 bool pop_p = false;
a723baf1 10411
62d1db17
MM
10412 /* Gather the attributes that were provided with the
10413 decl-specifiers. */
10414 prefix_attributes = decl_specifiers->attributes;
62d1db17 10415
a723baf1
MM
10416 /* Assume that this is not the declarator for a function
10417 definition. */
10418 if (function_definition_p)
10419 *function_definition_p = false;
10420
10421 /* Defer access checks while parsing the declarator; we cannot know
21526606 10422 what names are accessible until we know what is being
a723baf1 10423 declared. */
cf22909c
KL
10424 resume_deferring_access_checks ();
10425
a723baf1 10426 /* Parse the declarator. */
21526606 10427 declarator
62b8a44e 10428 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 10429 &ctor_dtor_or_conv_p,
db86dd14
MM
10430 /*parenthesized_p=*/NULL,
10431 /*member_p=*/false);
a723baf1 10432 /* Gather up the deferred checks. */
cf22909c 10433 stop_deferring_access_checks ();
24c0ef37 10434
a723baf1
MM
10435 /* If the DECLARATOR was erroneous, there's no need to go
10436 further. */
058b15c1 10437 if (declarator == cp_error_declarator)
cf22909c 10438 return error_mark_node;
a723baf1 10439
fc6a28d7
MM
10440 if (declares_class_or_enum & 2)
10441 cp_parser_check_for_definition_in_return_type (declarator,
10442 decl_specifiers->type);
560ad596 10443
a723baf1
MM
10444 /* Figure out what scope the entity declared by the DECLARATOR is
10445 located in. `grokdeclarator' sometimes changes the scope, so
10446 we compute it now. */
10447 scope = get_scope_of_declarator (declarator);
10448
10449 /* If we're allowing GNU extensions, look for an asm-specification
10450 and attributes. */
10451 if (cp_parser_allow_gnu_extensions_p (parser))
10452 {
10453 /* Look for an asm-specification. */
10454 asm_specification = cp_parser_asm_specification_opt (parser);
10455 /* And attributes. */
10456 attributes = cp_parser_attributes_opt (parser);
10457 }
10458 else
10459 {
10460 asm_specification = NULL_TREE;
10461 attributes = NULL_TREE;
10462 }
10463
10464 /* Peek at the next token. */
10465 token = cp_lexer_peek_token (parser->lexer);
10466 /* Check to see if the token indicates the start of a
10467 function-definition. */
10468 if (cp_parser_token_starts_function_definition_p (token))
10469 {
10470 if (!function_definition_allowed_p)
10471 {
10472 /* If a function-definition should not appear here, issue an
10473 error message. */
10474 cp_parser_error (parser,
10475 "a function-definition is not allowed here");
10476 return error_mark_node;
10477 }
10478 else
10479 {
a723baf1
MM
10480 /* Neither attributes nor an asm-specification are allowed
10481 on a function-definition. */
10482 if (asm_specification)
10483 error ("an asm-specification is not allowed on a function-definition");
10484 if (attributes)
10485 error ("attributes are not allowed on a function-definition");
10486 /* This is a function-definition. */
10487 *function_definition_p = true;
10488
a723baf1 10489 /* Parse the function definition. */
4bb8ca28
MM
10490 if (member_p)
10491 decl = cp_parser_save_member_function_body (parser,
10492 decl_specifiers,
10493 declarator,
10494 prefix_attributes);
10495 else
21526606 10496 decl
4bb8ca28
MM
10497 = (cp_parser_function_definition_from_specifiers_and_declarator
10498 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 10499
a723baf1
MM
10500 return decl;
10501 }
10502 }
10503
10504 /* [dcl.dcl]
10505
10506 Only in function declarations for constructors, destructors, and
21526606 10507 type conversions can the decl-specifier-seq be omitted.
a723baf1
MM
10508
10509 We explicitly postpone this check past the point where we handle
10510 function-definitions because we tolerate function-definitions
10511 that are missing their return types in some modes. */
62d1db17 10512 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
a723baf1 10513 {
21526606 10514 cp_parser_error (parser,
a723baf1
MM
10515 "expected constructor, destructor, or type conversion");
10516 return error_mark_node;
10517 }
10518
10519 /* An `=' or an `(' indicates an initializer. */
21526606 10520 is_initialized = (token->type == CPP_EQ
a723baf1
MM
10521 || token->type == CPP_OPEN_PAREN);
10522 /* If the init-declarator isn't initialized and isn't followed by a
10523 `,' or `;', it's not a valid init-declarator. */
21526606 10524 if (!is_initialized
a723baf1
MM
10525 && token->type != CPP_COMMA
10526 && token->type != CPP_SEMICOLON)
10527 {
996c2b52 10528 cp_parser_error (parser, "expected initializer");
a723baf1
MM
10529 return error_mark_node;
10530 }
10531
10532 /* Because start_decl has side-effects, we should only call it if we
10533 know we're going ahead. By this point, we know that we cannot
10534 possibly be looking at any other construct. */
10535 cp_parser_commit_to_tentative_parse (parser);
10536
e90c7b84
ILT
10537 /* If the decl specifiers were bad, issue an error now that we're
10538 sure this was intended to be a declarator. Then continue
10539 declaring the variable(s), as int, to try to cut down on further
10540 errors. */
62d1db17
MM
10541 if (decl_specifiers->any_specifiers_p
10542 && decl_specifiers->type == error_mark_node)
e90c7b84
ILT
10543 {
10544 cp_parser_error (parser, "invalid type in declaration");
62d1db17 10545 decl_specifiers->type = integer_type_node;
e90c7b84
ILT
10546 }
10547
a723baf1
MM
10548 /* Check to see whether or not this declaration is a friend. */
10549 friend_p = cp_parser_friend_p (decl_specifiers);
10550
10551 /* Check that the number of template-parameter-lists is OK. */
ee3071ef 10552 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
cf22909c 10553 return error_mark_node;
a723baf1
MM
10554
10555 /* Enter the newly declared entry in the symbol table. If we're
10556 processing a declaration in a class-specifier, we wait until
10557 after processing the initializer. */
10558 if (!member_p)
10559 {
10560 if (parser->in_unbraced_linkage_specification_p)
10561 {
62d1db17 10562 decl_specifiers->storage_class = sc_extern;
a723baf1
MM
10563 have_extern_spec = false;
10564 }
ee3071ef 10565 decl = start_decl (declarator, decl_specifiers,
73a8adb6
MM
10566 is_initialized, attributes, prefix_attributes,
10567 &pop_p);
a723baf1 10568 }
73a8adb6
MM
10569 else if (scope)
10570 /* Enter the SCOPE. That way unqualified names appearing in the
10571 initializer will be looked up in SCOPE. */
91b004e5 10572 pop_p = push_scope (scope);
a723baf1
MM
10573
10574 /* Perform deferred access control checks, now that we know in which
10575 SCOPE the declared entity resides. */
21526606 10576 if (!member_p && decl)
a723baf1
MM
10577 {
10578 tree saved_current_function_decl = NULL_TREE;
10579
10580 /* If the entity being declared is a function, pretend that we
10581 are in its scope. If it is a `friend', it may have access to
9bcb9aae 10582 things that would not otherwise be accessible. */
a723baf1
MM
10583 if (TREE_CODE (decl) == FUNCTION_DECL)
10584 {
10585 saved_current_function_decl = current_function_decl;
10586 current_function_decl = decl;
10587 }
21526606 10588
cf22909c
KL
10589 /* Perform the access control checks for the declarator and the
10590 the decl-specifiers. */
10591 perform_deferred_access_checks ();
a723baf1
MM
10592
10593 /* Restore the saved value. */
10594 if (TREE_CODE (decl) == FUNCTION_DECL)
10595 current_function_decl = saved_current_function_decl;
10596 }
10597
10598 /* Parse the initializer. */
10599 if (is_initialized)
21526606 10600 initializer = cp_parser_initializer (parser,
39703eb9
MM
10601 &is_parenthesized_init,
10602 &is_non_constant_init);
a723baf1
MM
10603 else
10604 {
10605 initializer = NULL_TREE;
10606 is_parenthesized_init = false;
39703eb9 10607 is_non_constant_init = true;
a723baf1
MM
10608 }
10609
10610 /* The old parser allows attributes to appear after a parenthesized
10611 initializer. Mark Mitchell proposed removing this functionality
10612 on the GCC mailing lists on 2002-08-13. This parser accepts the
10613 attributes -- but ignores them. */
10614 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10615 if (cp_parser_attributes_opt (parser))
10616 warning ("attributes after parenthesized initializer ignored");
10617
a723baf1
MM
10618 /* For an in-class declaration, use `grokfield' to create the
10619 declaration. */
10620 if (member_p)
8db1028e 10621 {
73a8adb6 10622 if (pop_p)
62a4d942
MM
10623 {
10624 pop_scope (scope);
10625 pop_p = false;
10626 }
8db1028e
NS
10627 decl = grokfield (declarator, decl_specifiers,
10628 initializer, /*asmspec=*/NULL_TREE,
a723baf1 10629 /*attributes=*/NULL_TREE);
8db1028e
NS
10630 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10631 cp_parser_save_default_args (parser, decl);
10632 }
21526606 10633
a723baf1
MM
10634 /* Finish processing the declaration. But, skip friend
10635 declarations. */
550205c3 10636 if (!friend_p && decl && decl != error_mark_node)
73a8adb6
MM
10637 {
10638 cp_finish_decl (decl,
10639 initializer,
10640 asm_specification,
10641 /* If the initializer is in parentheses, then this is
10642 a direct-initialization, which means that an
10643 `explicit' constructor is OK. Otherwise, an
10644 `explicit' constructor cannot be used. */
10645 ((is_parenthesized_init || !is_initialized)
a723baf1 10646 ? 0 : LOOKUP_ONLYCONVERTING));
73a8adb6
MM
10647 if (pop_p)
10648 pop_scope (DECL_CONTEXT (decl));
10649 }
a723baf1 10650
39703eb9
MM
10651 /* Remember whether or not variables were initialized by
10652 constant-expressions. */
21526606 10653 if (decl && TREE_CODE (decl) == VAR_DECL
39703eb9
MM
10654 && is_initialized && !is_non_constant_init)
10655 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10656
a723baf1
MM
10657 return decl;
10658}
10659
10660/* Parse a declarator.
21526606 10661
a723baf1
MM
10662 declarator:
10663 direct-declarator
21526606 10664 ptr-operator declarator
a723baf1
MM
10665
10666 abstract-declarator:
10667 ptr-operator abstract-declarator [opt]
10668 direct-abstract-declarator
10669
10670 GNU Extensions:
10671
10672 declarator:
10673 attributes [opt] direct-declarator
21526606 10674 attributes [opt] ptr-operator declarator
a723baf1
MM
10675
10676 abstract-declarator:
10677 attributes [opt] ptr-operator abstract-declarator [opt]
10678 attributes [opt] direct-abstract-declarator
21526606 10679
7efa3e22
NS
10680 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10681 detect constructor, destructor or conversion operators. It is set
10682 to -1 if the declarator is a name, and +1 if it is a
10683 function. Otherwise it is set to zero. Usually you just want to
10684 test for >0, but internally the negative value is used.
21526606 10685
a723baf1
MM
10686 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10687 a decl-specifier-seq unless it declares a constructor, destructor,
10688 or conversion. It might seem that we could check this condition in
10689 semantic analysis, rather than parsing, but that makes it difficult
10690 to handle something like `f()'. We want to notice that there are
10691 no decl-specifiers, and therefore realize that this is an
21526606
EC
10692 expression, not a declaration.)
10693
4bb8ca28 10694 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
db86dd14
MM
10695 the declarator is a direct-declarator of the form "(...)".
10696
10697 MEMBER_P is true iff this declarator is a member-declarator. */
a723baf1 10698
058b15c1 10699static cp_declarator *
21526606
EC
10700cp_parser_declarator (cp_parser* parser,
10701 cp_parser_declarator_kind dcl_kind,
4bb8ca28 10702 int* ctor_dtor_or_conv_p,
db86dd14
MM
10703 bool* parenthesized_p,
10704 bool member_p)
a723baf1
MM
10705{
10706 cp_token *token;
058b15c1 10707 cp_declarator *declarator;
a723baf1 10708 enum tree_code code;
3c01e5df 10709 cp_cv_quals cv_quals;
a723baf1
MM
10710 tree class_type;
10711 tree attributes = NULL_TREE;
10712
10713 /* Assume this is not a constructor, destructor, or type-conversion
10714 operator. */
10715 if (ctor_dtor_or_conv_p)
7efa3e22 10716 *ctor_dtor_or_conv_p = 0;
a723baf1
MM
10717
10718 if (cp_parser_allow_gnu_extensions_p (parser))
10719 attributes = cp_parser_attributes_opt (parser);
21526606 10720
a723baf1
MM
10721 /* Peek at the next token. */
10722 token = cp_lexer_peek_token (parser->lexer);
21526606 10723
a723baf1
MM
10724 /* Check for the ptr-operator production. */
10725 cp_parser_parse_tentatively (parser);
10726 /* Parse the ptr-operator. */
21526606
EC
10727 code = cp_parser_ptr_operator (parser,
10728 &class_type,
3c01e5df 10729 &cv_quals);
a723baf1
MM
10730 /* If that worked, then we have a ptr-operator. */
10731 if (cp_parser_parse_definitely (parser))
10732 {
4bb8ca28
MM
10733 /* If a ptr-operator was found, then this declarator was not
10734 parenthesized. */
10735 if (parenthesized_p)
10736 *parenthesized_p = true;
a723baf1
MM
10737 /* The dependent declarator is optional if we are parsing an
10738 abstract-declarator. */
62b8a44e 10739 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10740 cp_parser_parse_tentatively (parser);
10741
10742 /* Parse the dependent declarator. */
62b8a44e 10743 declarator = cp_parser_declarator (parser, dcl_kind,
4bb8ca28 10744 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
10745 /*parenthesized_p=*/NULL,
10746 /*member_p=*/false);
a723baf1
MM
10747
10748 /* If we are parsing an abstract-declarator, we must handle the
10749 case where the dependent declarator is absent. */
62b8a44e
NS
10750 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10751 && !cp_parser_parse_definitely (parser))
058b15c1 10752 declarator = NULL;
21526606 10753
a723baf1 10754 /* Build the representation of the ptr-operator. */
058b15c1 10755 if (class_type)
3c01e5df 10756 declarator = make_ptrmem_declarator (cv_quals,
058b15c1
MM
10757 class_type,
10758 declarator);
10759 else if (code == INDIRECT_REF)
3c01e5df 10760 declarator = make_pointer_declarator (cv_quals, declarator);
a723baf1 10761 else
3c01e5df 10762 declarator = make_reference_declarator (cv_quals, declarator);
a723baf1
MM
10763 }
10764 /* Everything else is a direct-declarator. */
10765 else
4bb8ca28
MM
10766 {
10767 if (parenthesized_p)
10768 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10769 CPP_OPEN_PAREN);
10770 declarator = cp_parser_direct_declarator (parser, dcl_kind,
db86dd14
MM
10771 ctor_dtor_or_conv_p,
10772 member_p);
4bb8ca28 10773 }
a723baf1 10774
058b15c1
MM
10775 if (attributes && declarator != cp_error_declarator)
10776 declarator->attributes = attributes;
21526606 10777
a723baf1
MM
10778 return declarator;
10779}
10780
10781/* Parse a direct-declarator or direct-abstract-declarator.
10782
10783 direct-declarator:
10784 declarator-id
10785 direct-declarator ( parameter-declaration-clause )
21526606 10786 cv-qualifier-seq [opt]
a723baf1
MM
10787 exception-specification [opt]
10788 direct-declarator [ constant-expression [opt] ]
21526606 10789 ( declarator )
a723baf1
MM
10790
10791 direct-abstract-declarator:
10792 direct-abstract-declarator [opt]
21526606 10793 ( parameter-declaration-clause )
a723baf1
MM
10794 cv-qualifier-seq [opt]
10795 exception-specification [opt]
10796 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10797 ( abstract-declarator )
10798
62b8a44e
NS
10799 Returns a representation of the declarator. DCL_KIND is
10800 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10801 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10802 we are parsing a direct-declarator. It is
10803 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10804 of ambiguity we prefer an abstract declarator, as per
db86dd14 10805 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
058b15c1 10806 cp_parser_declarator. */
a723baf1 10807
058b15c1 10808static cp_declarator *
94edc4ab
NN
10809cp_parser_direct_declarator (cp_parser* parser,
10810 cp_parser_declarator_kind dcl_kind,
db86dd14
MM
10811 int* ctor_dtor_or_conv_p,
10812 bool member_p)
a723baf1
MM
10813{
10814 cp_token *token;
058b15c1 10815 cp_declarator *declarator = NULL;
a723baf1
MM
10816 tree scope = NULL_TREE;
10817 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10818 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e 10819 bool first = true;
91b004e5 10820 bool pop_p = false;
21526606 10821
62b8a44e 10822 while (true)
a723baf1 10823 {
62b8a44e
NS
10824 /* Peek at the next token. */
10825 token = cp_lexer_peek_token (parser->lexer);
10826 if (token->type == CPP_OPEN_PAREN)
a723baf1 10827 {
62b8a44e
NS
10828 /* This is either a parameter-declaration-clause, or a
10829 parenthesized declarator. When we know we are parsing a
34cd5ae7 10830 named declarator, it must be a parenthesized declarator
62b8a44e
NS
10831 if FIRST is true. For instance, `(int)' is a
10832 parameter-declaration-clause, with an omitted
10833 direct-abstract-declarator. But `((*))', is a
10834 parenthesized abstract declarator. Finally, when T is a
10835 template parameter `(T)' is a
34cd5ae7 10836 parameter-declaration-clause, and not a parenthesized
62b8a44e 10837 named declarator.
21526606 10838
62b8a44e
NS
10839 We first try and parse a parameter-declaration-clause,
10840 and then try a nested declarator (if FIRST is true).
a723baf1 10841
62b8a44e
NS
10842 It is not an error for it not to be a
10843 parameter-declaration-clause, even when FIRST is
10844 false. Consider,
10845
10846 int i (int);
10847 int i (3);
10848
10849 The first is the declaration of a function while the
10850 second is a the definition of a variable, including its
10851 initializer.
10852
10853 Having seen only the parenthesis, we cannot know which of
10854 these two alternatives should be selected. Even more
10855 complex are examples like:
10856
10857 int i (int (a));
10858 int i (int (3));
10859
10860 The former is a function-declaration; the latter is a
21526606 10861 variable initialization.
62b8a44e 10862
34cd5ae7 10863 Thus again, we try a parameter-declaration-clause, and if
62b8a44e
NS
10864 that fails, we back out and return. */
10865
10866 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10867 {
058b15c1 10868 cp_parameter_declarator *params;
4047b164 10869 unsigned saved_num_template_parameter_lists;
21526606 10870
db86dd14
MM
10871 /* In a member-declarator, the only valid interpretation
10872 of a parenthesis is the start of a
10873 parameter-declaration-clause. (It is invalid to
10874 initialize a static data member with a parenthesized
10875 initializer; only the "=" form of initialization is
10876 permitted.) */
10877 if (!member_p)
10878 cp_parser_parse_tentatively (parser);
a723baf1 10879
62b8a44e
NS
10880 /* Consume the `('. */
10881 cp_lexer_consume_token (parser->lexer);
10882 if (first)
10883 {
10884 /* If this is going to be an abstract declarator, we're
10885 in a declarator and we can't have default args. */
10886 parser->default_arg_ok_p = false;
10887 parser->in_declarator_p = true;
10888 }
21526606 10889
4047b164
KL
10890 /* Inside the function parameter list, surrounding
10891 template-parameter-lists do not apply. */
10892 saved_num_template_parameter_lists
10893 = parser->num_template_parameter_lists;
10894 parser->num_template_parameter_lists = 0;
10895
62b8a44e
NS
10896 /* Parse the parameter-declaration-clause. */
10897 params = cp_parser_parameter_declaration_clause (parser);
10898
4047b164
KL
10899 parser->num_template_parameter_lists
10900 = saved_num_template_parameter_lists;
10901
62b8a44e 10902 /* If all went well, parse the cv-qualifier-seq and the
34cd5ae7 10903 exception-specification. */
db86dd14 10904 if (member_p || cp_parser_parse_definitely (parser))
62b8a44e 10905 {
3c01e5df 10906 cp_cv_quals cv_quals;
62b8a44e 10907 tree exception_specification;
7efa3e22
NS
10908
10909 if (ctor_dtor_or_conv_p)
10910 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
62b8a44e
NS
10911 first = false;
10912 /* Consume the `)'. */
10913 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10914
10915 /* Parse the cv-qualifier-seq. */
3c01e5df 10916 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
62b8a44e 10917 /* And the exception-specification. */
21526606 10918 exception_specification
62b8a44e
NS
10919 = cp_parser_exception_specification_opt (parser);
10920
10921 /* Create the function-declarator. */
10922 declarator = make_call_declarator (declarator,
10923 params,
3c01e5df 10924 cv_quals,
62b8a44e
NS
10925 exception_specification);
10926 /* Any subsequent parameter lists are to do with
10927 return type, so are not those of the declared
10928 function. */
10929 parser->default_arg_ok_p = false;
21526606 10930
62b8a44e
NS
10931 /* Repeat the main loop. */
10932 continue;
10933 }
10934 }
21526606 10935
62b8a44e
NS
10936 /* If this is the first, we can try a parenthesized
10937 declarator. */
10938 if (first)
a723baf1 10939 {
a7324e75
MM
10940 bool saved_in_type_id_in_expr_p;
10941
a723baf1 10942 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e 10943 parser->in_declarator_p = saved_in_declarator_p;
21526606 10944
62b8a44e
NS
10945 /* Consume the `('. */
10946 cp_lexer_consume_token (parser->lexer);
10947 /* Parse the nested declarator. */
a7324e75
MM
10948 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10949 parser->in_type_id_in_expr_p = true;
21526606 10950 declarator
4bb8ca28 10951 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
db86dd14
MM
10952 /*parenthesized_p=*/NULL,
10953 member_p);
a7324e75 10954 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
62b8a44e
NS
10955 first = false;
10956 /* Expect a `)'. */
10957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
058b15c1
MM
10958 declarator = cp_error_declarator;
10959 if (declarator == cp_error_declarator)
62b8a44e 10960 break;
21526606 10961
62b8a44e 10962 goto handle_declarator;
a723baf1 10963 }
9bcb9aae 10964 /* Otherwise, we must be done. */
62b8a44e
NS
10965 else
10966 break;
a723baf1 10967 }
62b8a44e
NS
10968 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10969 && token->type == CPP_OPEN_SQUARE)
a723baf1 10970 {
62b8a44e 10971 /* Parse an array-declarator. */
a723baf1
MM
10972 tree bounds;
10973
7efa3e22
NS
10974 if (ctor_dtor_or_conv_p)
10975 *ctor_dtor_or_conv_p = 0;
21526606 10976
62b8a44e
NS
10977 first = false;
10978 parser->default_arg_ok_p = false;
10979 parser->in_declarator_p = true;
a723baf1
MM
10980 /* Consume the `['. */
10981 cp_lexer_consume_token (parser->lexer);
10982 /* Peek at the next token. */
10983 token = cp_lexer_peek_token (parser->lexer);
10984 /* If the next token is `]', then there is no
10985 constant-expression. */
10986 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10987 {
10988 bool non_constant_p;
10989
21526606 10990 bounds
14d22dd6
MM
10991 = cp_parser_constant_expression (parser,
10992 /*allow_non_constant=*/true,
10993 &non_constant_p);
d17811fd 10994 if (!non_constant_p)
9baa27a9 10995 bounds = fold_non_dependent_expr (bounds);
44370687
MM
10996 else if (!at_function_scope_p ())
10997 {
10998 error ("array bound is not an integer constant");
10999 bounds = error_mark_node;
11000 }
14d22dd6 11001 }
a723baf1
MM
11002 else
11003 bounds = NULL_TREE;
11004 /* Look for the closing `]'. */
62b8a44e
NS
11005 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11006 {
058b15c1 11007 declarator = cp_error_declarator;
62b8a44e
NS
11008 break;
11009 }
a723baf1 11010
058b15c1 11011 declarator = make_array_declarator (declarator, bounds);
a723baf1 11012 }
62b8a44e 11013 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 11014 {
058b15c1
MM
11015 tree id;
11016
a668c6ad 11017 /* Parse a declarator-id */
62b8a44e
NS
11018 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11019 cp_parser_parse_tentatively (parser);
058b15c1 11020 id = cp_parser_declarator_id (parser);
712becab
NS
11021 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11022 {
11023 if (!cp_parser_parse_definitely (parser))
058b15c1
MM
11024 id = error_mark_node;
11025 else if (TREE_CODE (id) != IDENTIFIER_NODE)
712becab
NS
11026 {
11027 cp_parser_error (parser, "expected unqualified-id");
058b15c1 11028 id = error_mark_node;
712becab
NS
11029 }
11030 }
21526606 11031
058b15c1
MM
11032 if (id == error_mark_node)
11033 {
11034 declarator = cp_error_declarator;
11035 break;
11036 }
21526606 11037
a5201a91 11038 if (TREE_CODE (id) == SCOPE_REF && at_namespace_scope_p ())
62b8a44e 11039 {
058b15c1 11040 tree scope = TREE_OPERAND (id, 0);
712becab 11041
62b8a44e
NS
11042 /* In the declaration of a member of a template class
11043 outside of the class itself, the SCOPE will sometimes
11044 be a TYPENAME_TYPE. For example, given:
21526606 11045
62b8a44e
NS
11046 template <typename T>
11047 int S<T>::R::i = 3;
21526606 11048
62b8a44e
NS
11049 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11050 this context, we must resolve S<T>::R to an ordinary
11051 type, rather than a typename type.
21526606 11052
62b8a44e
NS
11053 The reason we normally avoid resolving TYPENAME_TYPEs
11054 is that a specialization of `S' might render
11055 `S<T>::R' not a type. However, if `S' is
11056 specialized, then this `i' will not be used, so there
11057 is no harm in resolving the types here. */
11058 if (TREE_CODE (scope) == TYPENAME_TYPE)
11059 {
14d22dd6
MM
11060 tree type;
11061
62b8a44e 11062 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
11063 type = resolve_typename_type (scope,
11064 /*only_current_p=*/false);
62b8a44e 11065 /* If that failed, the declarator is invalid. */
109e0040 11066 if (type == error_mark_node)
2a13a625 11067 error ("%<%T::%D%> is not a type",
109e0040
MM
11068 TYPE_CONTEXT (scope),
11069 TYPE_IDENTIFIER (scope));
62b8a44e 11070 /* Build a new DECLARATOR. */
058b15c1 11071 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
62b8a44e
NS
11072 }
11073 }
21526606 11074
058b15c1
MM
11075 declarator = make_id_declarator (id);
11076 if (id)
a723baf1 11077 {
62b8a44e 11078 tree class_type;
058b15c1 11079 tree unqualified_name;
62b8a44e 11080
058b15c1
MM
11081 if (TREE_CODE (id) == SCOPE_REF
11082 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
62b8a44e 11083 {
058b15c1
MM
11084 class_type = TREE_OPERAND (id, 0);
11085 unqualified_name = TREE_OPERAND (id, 1);
62b8a44e
NS
11086 }
11087 else
11088 {
11089 class_type = current_class_type;
058b15c1 11090 unqualified_name = id;
62b8a44e
NS
11091 }
11092
058b15c1
MM
11093 if (class_type)
11094 {
11095 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11096 declarator->u.id.sfk = sfk_destructor;
11097 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11098 declarator->u.id.sfk = sfk_conversion;
11099 else if (constructor_name_p (unqualified_name,
11100 class_type)
11101 || (TREE_CODE (unqualified_name) == TYPE_DECL
11102 && same_type_p (TREE_TYPE (unqualified_name),
11103 class_type)))
11104 declarator->u.id.sfk = sfk_constructor;
11105
11106 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11107 *ctor_dtor_or_conv_p = -1;
11108 if (TREE_CODE (id) == SCOPE_REF
98ca843c 11109 && TREE_CODE (unqualified_name) == TYPE_DECL
058b15c1
MM
11110 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11111 {
11112 error ("invalid use of constructor as a template");
2a13a625
GDR
11113 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11114 "the constructor in a qualified name",
11115 class_type,
058b15c1
MM
11116 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11117 class_type, class_type);
11118 }
11119 }
a723baf1 11120 }
62b8a44e
NS
11121
11122 handle_declarator:;
11123 scope = get_scope_of_declarator (declarator);
11124 if (scope)
91b004e5
MM
11125 /* Any names that appear after the declarator-id for a
11126 member are looked up in the containing scope. */
11127 pop_p = push_scope (scope);
62b8a44e
NS
11128 parser->in_declarator_p = true;
11129 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
058b15c1 11130 || (declarator && declarator->kind == cdk_id))
62b8a44e
NS
11131 /* Default args are only allowed on function
11132 declarations. */
11133 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 11134 else
62b8a44e
NS
11135 parser->default_arg_ok_p = false;
11136
11137 first = false;
a723baf1 11138 }
62b8a44e 11139 /* We're done. */
a723baf1
MM
11140 else
11141 break;
a723baf1
MM
11142 }
11143
11144 /* For an abstract declarator, we might wind up with nothing at this
11145 point. That's an error; the declarator is not optional. */
11146 if (!declarator)
11147 cp_parser_error (parser, "expected declarator");
11148
11149 /* If we entered a scope, we must exit it now. */
91b004e5 11150 if (pop_p)
a723baf1
MM
11151 pop_scope (scope);
11152
11153 parser->default_arg_ok_p = saved_default_arg_ok_p;
11154 parser->in_declarator_p = saved_in_declarator_p;
21526606 11155
a723baf1
MM
11156 return declarator;
11157}
11158
21526606 11159/* Parse a ptr-operator.
a723baf1
MM
11160
11161 ptr-operator:
11162 * cv-qualifier-seq [opt]
11163 &
11164 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11165
11166 GNU Extension:
11167
11168 ptr-operator:
11169 & cv-qualifier-seq [opt]
11170
3c01e5df
MM
11171 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11172 Returns ADDR_EXPR if a reference was used. In the case of a
11173 pointer-to-member, *TYPE is filled in with the TYPE containing the
11174 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11175 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11176 ERROR_MARK if an error occurred. */
21526606 11177
a723baf1 11178static enum tree_code
21526606
EC
11179cp_parser_ptr_operator (cp_parser* parser,
11180 tree* type,
3c01e5df 11181 cp_cv_quals *cv_quals)
a723baf1
MM
11182{
11183 enum tree_code code = ERROR_MARK;
11184 cp_token *token;
11185
11186 /* Assume that it's not a pointer-to-member. */
11187 *type = NULL_TREE;
11188 /* And that there are no cv-qualifiers. */
3c01e5df 11189 *cv_quals = TYPE_UNQUALIFIED;
a723baf1
MM
11190
11191 /* Peek at the next token. */
11192 token = cp_lexer_peek_token (parser->lexer);
11193 /* If it's a `*' or `&' we have a pointer or reference. */
11194 if (token->type == CPP_MULT || token->type == CPP_AND)
11195 {
11196 /* Remember which ptr-operator we were processing. */
11197 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11198
11199 /* Consume the `*' or `&'. */
11200 cp_lexer_consume_token (parser->lexer);
11201
11202 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11203 `&', if we are allowing GNU extensions. (The only qualifier
11204 that can legally appear after `&' is `restrict', but that is
11205 enforced during semantic analysis. */
21526606 11206 if (code == INDIRECT_REF
a723baf1 11207 || cp_parser_allow_gnu_extensions_p (parser))
3c01e5df 11208 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11209 }
11210 else
11211 {
11212 /* Try the pointer-to-member case. */
11213 cp_parser_parse_tentatively (parser);
11214 /* Look for the optional `::' operator. */
11215 cp_parser_global_scope_opt (parser,
11216 /*current_scope_valid_p=*/false);
11217 /* Look for the nested-name specifier. */
11218 cp_parser_nested_name_specifier (parser,
11219 /*typename_keyword_p=*/false,
11220 /*check_dependency_p=*/true,
a668c6ad
MM
11221 /*type_p=*/false,
11222 /*is_declaration=*/false);
a723baf1
MM
11223 /* If we found it, and the next token is a `*', then we are
11224 indeed looking at a pointer-to-member operator. */
11225 if (!cp_parser_error_occurred (parser)
11226 && cp_parser_require (parser, CPP_MULT, "`*'"))
11227 {
11228 /* The type of which the member is a member is given by the
11229 current SCOPE. */
11230 *type = parser->scope;
11231 /* The next name will not be qualified. */
11232 parser->scope = NULL_TREE;
11233 parser->qualifying_scope = NULL_TREE;
11234 parser->object_scope = NULL_TREE;
11235 /* Indicate that the `*' operator was used. */
11236 code = INDIRECT_REF;
11237 /* Look for the optional cv-qualifier-seq. */
3c01e5df 11238 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
a723baf1
MM
11239 }
11240 /* If that didn't work we don't have a ptr-operator. */
11241 if (!cp_parser_parse_definitely (parser))
11242 cp_parser_error (parser, "expected ptr-operator");
11243 }
11244
11245 return code;
11246}
11247
11248/* Parse an (optional) cv-qualifier-seq.
11249
11250 cv-qualifier-seq:
21526606 11251 cv-qualifier cv-qualifier-seq [opt]
a723baf1 11252
a723baf1
MM
11253 cv-qualifier:
11254 const
21526606 11255 volatile
a723baf1
MM
11256
11257 GNU Extension:
11258
11259 cv-qualifier:
98ca843c 11260 __restrict__
a723baf1 11261
3c01e5df
MM
11262 Returns a bitmask representing the cv-qualifiers. */
11263
11264static cp_cv_quals
11265cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1 11266{
3c01e5df 11267 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
a723baf1 11268
3c01e5df 11269 while (true)
a723baf1 11270 {
3c01e5df
MM
11271 cp_token *token;
11272 cp_cv_quals cv_qualifier;
98ca843c 11273
3c01e5df
MM
11274 /* Peek at the next token. */
11275 token = cp_lexer_peek_token (parser->lexer);
11276 /* See if it's a cv-qualifier. */
11277 switch (token->keyword)
11278 {
11279 case RID_CONST:
11280 cv_qualifier = TYPE_QUAL_CONST;
11281 break;
98ca843c 11282
3c01e5df
MM
11283 case RID_VOLATILE:
11284 cv_qualifier = TYPE_QUAL_VOLATILE;
11285 break;
98ca843c 11286
3c01e5df
MM
11287 case RID_RESTRICT:
11288 cv_qualifier = TYPE_QUAL_RESTRICT;
11289 break;
98ca843c 11290
3c01e5df
MM
11291 default:
11292 cv_qualifier = TYPE_UNQUALIFIED;
11293 break;
11294 }
98ca843c 11295
3c01e5df
MM
11296 if (!cv_qualifier)
11297 break;
a723baf1 11298
3c01e5df
MM
11299 if (cv_quals & cv_qualifier)
11300 {
11301 error ("duplicate cv-qualifier");
11302 cp_lexer_purge_token (parser->lexer);
11303 }
11304 else
11305 {
11306 cp_lexer_consume_token (parser->lexer);
11307 cv_quals |= cv_qualifier;
11308 }
a723baf1
MM
11309 }
11310
3c01e5df 11311 return cv_quals;
a723baf1
MM
11312}
11313
11314/* Parse a declarator-id.
11315
11316 declarator-id:
11317 id-expression
21526606 11318 :: [opt] nested-name-specifier [opt] type-name
a723baf1
MM
11319
11320 In the `id-expression' case, the value returned is as for
11321 cp_parser_id_expression if the id-expression was an unqualified-id.
11322 If the id-expression was a qualified-id, then a SCOPE_REF is
11323 returned. The first operand is the scope (either a NAMESPACE_DECL
11324 or TREE_TYPE), but the second is still just a representation of an
11325 unqualified-id. */
11326
11327static tree
94edc4ab 11328cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
11329{
11330 tree id_expression;
11331
11332 /* The expression must be an id-expression. Assume that qualified
11333 names are the names of types so that:
11334
11335 template <class T>
11336 int S<T>::R::i = 3;
11337
11338 will work; we must treat `S<T>::R' as the name of a type.
11339 Similarly, assume that qualified names are templates, where
11340 required, so that:
11341
11342 template <class T>
11343 int S<T>::R<T>::i = 3;
11344
11345 will work, too. */
11346 id_expression = cp_parser_id_expression (parser,
11347 /*template_keyword_p=*/false,
11348 /*check_dependency_p=*/false,
f3c2dfc6
MM
11349 /*template_p=*/NULL,
11350 /*declarator_p=*/true);
21526606 11351 /* If the name was qualified, create a SCOPE_REF to represent
a723baf1
MM
11352 that. */
11353 if (parser->scope)
ec20aa6c
MM
11354 {
11355 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11356 parser->scope = NULL_TREE;
11357 }
a723baf1
MM
11358
11359 return id_expression;
11360}
11361
11362/* Parse a type-id.
11363
11364 type-id:
11365 type-specifier-seq abstract-declarator [opt]
11366
11367 Returns the TYPE specified. */
11368
11369static tree
94edc4ab 11370cp_parser_type_id (cp_parser* parser)
a723baf1 11371{
62d1db17 11372 cp_decl_specifier_seq type_specifier_seq;
058b15c1 11373 cp_declarator *abstract_declarator;
a723baf1
MM
11374
11375 /* Parse the type-specifier-seq. */
62d1db17
MM
11376 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11377 if (type_specifier_seq.type == error_mark_node)
a723baf1
MM
11378 return error_mark_node;
11379
11380 /* There might or might not be an abstract declarator. */
11381 cp_parser_parse_tentatively (parser);
11382 /* Look for the declarator. */
21526606 11383 abstract_declarator
4bb8ca28 11384 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
db86dd14
MM
11385 /*parenthesized_p=*/NULL,
11386 /*member_p=*/false);
a723baf1
MM
11387 /* Check to see if there really was a declarator. */
11388 if (!cp_parser_parse_definitely (parser))
058b15c1 11389 abstract_declarator = NULL;
a723baf1 11390
62d1db17 11391 return groktypename (&type_specifier_seq, abstract_declarator);
a723baf1
MM
11392}
11393
11394/* Parse a type-specifier-seq.
11395
11396 type-specifier-seq:
11397 type-specifier type-specifier-seq [opt]
11398
11399 GNU extension:
11400
11401 type-specifier-seq:
11402 attributes type-specifier-seq [opt]
11403
62d1db17 11404 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
a723baf1 11405
62d1db17
MM
11406static void
11407cp_parser_type_specifier_seq (cp_parser* parser,
11408 cp_decl_specifier_seq *type_specifier_seq)
a723baf1
MM
11409{
11410 bool seen_type_specifier = false;
62d1db17
MM
11411
11412 /* Clear the TYPE_SPECIFIER_SEQ. */
11413 clear_decl_specs (type_specifier_seq);
a723baf1
MM
11414
11415 /* Parse the type-specifiers and attributes. */
11416 while (true)
11417 {
11418 tree type_specifier;
11419
11420 /* Check for attributes first. */
11421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11422 {
98ca843c 11423 type_specifier_seq->attributes =
62d1db17
MM
11424 chainon (type_specifier_seq->attributes,
11425 cp_parser_attributes_opt (parser));
a723baf1
MM
11426 continue;
11427 }
11428
a723baf1 11429 /* Look for the type-specifier. */
21526606 11430 type_specifier = cp_parser_type_specifier (parser,
62d1db17
MM
11431 CP_PARSER_FLAGS_OPTIONAL,
11432 type_specifier_seq,
a723baf1
MM
11433 /*is_declaration=*/false,
11434 NULL,
11435 NULL);
11436 /* If the first type-specifier could not be found, this is not a
11437 type-specifier-seq at all. */
62d1db17
MM
11438 if (!seen_type_specifier && !type_specifier)
11439 {
11440 cp_parser_error (parser, "expected type-specifier");
11441 type_specifier_seq->type = error_mark_node;
11442 return;
11443 }
a723baf1
MM
11444 /* If subsequent type-specifiers could not be found, the
11445 type-specifier-seq is complete. */
62d1db17 11446 else if (seen_type_specifier && !type_specifier)
a723baf1
MM
11447 break;
11448
a723baf1
MM
11449 seen_type_specifier = true;
11450 }
11451
62d1db17 11452 return;
a723baf1
MM
11453}
11454
11455/* Parse a parameter-declaration-clause.
11456
11457 parameter-declaration-clause:
11458 parameter-declaration-list [opt] ... [opt]
11459 parameter-declaration-list , ...
11460
058b15c1
MM
11461 Returns a representation for the parameter declarations. A return
11462 value of NULL indicates a parameter-declaration-clause consisting
11463 only of an ellipsis. */
a723baf1 11464
058b15c1 11465static cp_parameter_declarator *
94edc4ab 11466cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1 11467{
058b15c1 11468 cp_parameter_declarator *parameters;
a723baf1
MM
11469 cp_token *token;
11470 bool ellipsis_p;
058b15c1 11471 bool is_error;
a723baf1
MM
11472
11473 /* Peek at the next token. */
11474 token = cp_lexer_peek_token (parser->lexer);
11475 /* Check for trivial parameter-declaration-clauses. */
11476 if (token->type == CPP_ELLIPSIS)
11477 {
11478 /* Consume the `...' token. */
11479 cp_lexer_consume_token (parser->lexer);
058b15c1 11480 return NULL;
a723baf1
MM
11481 }
11482 else if (token->type == CPP_CLOSE_PAREN)
11483 /* There are no parameters. */
c73aecdf
DE
11484 {
11485#ifndef NO_IMPLICIT_EXTERN_C
11486 if (in_system_header && current_class_type == NULL
11487 && current_lang_name == lang_name_c)
058b15c1 11488 return NULL;
c73aecdf
DE
11489 else
11490#endif
058b15c1 11491 return no_parameters;
c73aecdf 11492 }
a723baf1
MM
11493 /* Check for `(void)', too, which is a special case. */
11494 else if (token->keyword == RID_VOID
21526606 11495 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
11496 == CPP_CLOSE_PAREN))
11497 {
11498 /* Consume the `void' token. */
11499 cp_lexer_consume_token (parser->lexer);
11500 /* There are no parameters. */
058b15c1 11501 return no_parameters;
a723baf1 11502 }
21526606 11503
a723baf1 11504 /* Parse the parameter-declaration-list. */
058b15c1 11505 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
a723baf1
MM
11506 /* If a parse error occurred while parsing the
11507 parameter-declaration-list, then the entire
11508 parameter-declaration-clause is erroneous. */
058b15c1
MM
11509 if (is_error)
11510 return NULL;
a723baf1
MM
11511
11512 /* Peek at the next token. */
11513 token = cp_lexer_peek_token (parser->lexer);
11514 /* If it's a `,', the clause should terminate with an ellipsis. */
11515 if (token->type == CPP_COMMA)
11516 {
11517 /* Consume the `,'. */
11518 cp_lexer_consume_token (parser->lexer);
11519 /* Expect an ellipsis. */
21526606 11520 ellipsis_p
a723baf1
MM
11521 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11522 }
21526606 11523 /* It might also be `...' if the optional trailing `,' was
a723baf1
MM
11524 omitted. */
11525 else if (token->type == CPP_ELLIPSIS)
11526 {
11527 /* Consume the `...' token. */
11528 cp_lexer_consume_token (parser->lexer);
11529 /* And remember that we saw it. */
11530 ellipsis_p = true;
11531 }
11532 else
11533 ellipsis_p = false;
11534
11535 /* Finish the parameter list. */
058b15c1
MM
11536 if (parameters && ellipsis_p)
11537 parameters->ellipsis_p = true;
98ca843c 11538
058b15c1 11539 return parameters;
a723baf1
MM
11540}
11541
11542/* Parse a parameter-declaration-list.
11543
11544 parameter-declaration-list:
11545 parameter-declaration
11546 parameter-declaration-list , parameter-declaration
11547
11548 Returns a representation of the parameter-declaration-list, as for
11549 cp_parser_parameter_declaration_clause. However, the
058b15c1
MM
11550 `void_list_node' is never appended to the list. Upon return,
11551 *IS_ERROR will be true iff an error occurred. */
a723baf1 11552
058b15c1
MM
11553static cp_parameter_declarator *
11554cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
a723baf1 11555{
058b15c1
MM
11556 cp_parameter_declarator *parameters = NULL;
11557 cp_parameter_declarator **tail = &parameters;
11558
11559 /* Assume all will go well. */
11560 *is_error = false;
a723baf1
MM
11561
11562 /* Look for more parameters. */
11563 while (true)
11564 {
058b15c1 11565 cp_parameter_declarator *parameter;
4bb8ca28 11566 bool parenthesized_p;
a723baf1 11567 /* Parse the parameter. */
21526606
EC
11568 parameter
11569 = cp_parser_parameter_declaration (parser,
4bb8ca28
MM
11570 /*template_parm_p=*/false,
11571 &parenthesized_p);
ec194454 11572
34cd5ae7 11573 /* If a parse error occurred parsing the parameter declaration,
a723baf1 11574 then the entire parameter-declaration-list is erroneous. */
058b15c1 11575 if (!parameter)
a723baf1 11576 {
058b15c1
MM
11577 *is_error = true;
11578 parameters = NULL;
a723baf1
MM
11579 break;
11580 }
11581 /* Add the new parameter to the list. */
058b15c1
MM
11582 *tail = parameter;
11583 tail = &parameter->next;
a723baf1
MM
11584
11585 /* Peek at the next token. */
11586 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11587 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11588 /* The parameter-declaration-list is complete. */
11589 break;
11590 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11591 {
11592 cp_token *token;
11593
11594 /* Peek at the next token. */
11595 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11596 /* If it's an ellipsis, then the list is complete. */
11597 if (token->type == CPP_ELLIPSIS)
11598 break;
11599 /* Otherwise, there must be more parameters. Consume the
11600 `,'. */
11601 cp_lexer_consume_token (parser->lexer);
4bb8ca28
MM
11602 /* When parsing something like:
11603
11604 int i(float f, double d)
21526606 11605
4bb8ca28
MM
11606 we can tell after seeing the declaration for "f" that we
11607 are not looking at an initialization of a variable "i",
21526606 11608 but rather at the declaration of a function "i".
4bb8ca28
MM
11609
11610 Due to the fact that the parsing of template arguments
11611 (as specified to a template-id) requires backtracking we
11612 cannot use this technique when inside a template argument
11613 list. */
11614 if (!parser->in_template_argument_list_p
4d5fe289 11615 && !parser->in_type_id_in_expr_p
0b16f8f4 11616 && cp_parser_uncommitted_to_tentative_parse_p (parser)
4bb8ca28
MM
11617 /* However, a parameter-declaration of the form
11618 "foat(f)" (which is a valid declaration of a
11619 parameter "f") can also be interpreted as an
11620 expression (the conversion of "f" to "float"). */
11621 && !parenthesized_p)
11622 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
11623 }
11624 else
11625 {
2a13a625 11626 cp_parser_error (parser, "expected %<,%> or %<...%>");
0b16f8f4 11627 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21526606 11628 cp_parser_skip_to_closing_parenthesis (parser,
4bb8ca28 11629 /*recovering=*/true,
5c832178 11630 /*or_comma=*/false,
4bb8ca28 11631 /*consume_paren=*/false);
a723baf1
MM
11632 break;
11633 }
11634 }
11635
058b15c1 11636 return parameters;
a723baf1
MM
11637}
11638
11639/* Parse a parameter declaration.
11640
11641 parameter-declaration:
11642 decl-specifier-seq declarator
11643 decl-specifier-seq declarator = assignment-expression
11644 decl-specifier-seq abstract-declarator [opt]
11645 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11646
ec194454
MM
11647 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11648 declares a template parameter. (In that case, a non-nested `>'
11649 token encountered during the parsing of the assignment-expression
11650 is not interpreted as a greater-than operator.)
a723baf1 11651
058b15c1
MM
11652 Returns a representation of the parameter, or NULL if an error
11653 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11654 true iff the declarator is of the form "(p)". */
a723baf1 11655
058b15c1 11656static cp_parameter_declarator *
21526606 11657cp_parser_parameter_declaration (cp_parser *parser,
4bb8ca28
MM
11658 bool template_parm_p,
11659 bool *parenthesized_p)
a723baf1 11660{
560ad596 11661 int declares_class_or_enum;
ec194454 11662 bool greater_than_is_operator_p;
62d1db17 11663 cp_decl_specifier_seq decl_specifiers;
058b15c1 11664 cp_declarator *declarator;
a723baf1 11665 tree default_argument;
a723baf1
MM
11666 cp_token *token;
11667 const char *saved_message;
11668
ec194454
MM
11669 /* In a template parameter, `>' is not an operator.
11670
11671 [temp.param]
11672
11673 When parsing a default template-argument for a non-type
11674 template-parameter, the first non-nested `>' is taken as the end
11675 of the template parameter-list rather than a greater-than
11676 operator. */
11677 greater_than_is_operator_p = !template_parm_p;
11678
a723baf1
MM
11679 /* Type definitions may not appear in parameter types. */
11680 saved_message = parser->type_definition_forbidden_message;
21526606 11681 parser->type_definition_forbidden_message
a723baf1
MM
11682 = "types may not be defined in parameter types";
11683
11684 /* Parse the declaration-specifiers. */
62d1db17
MM
11685 cp_parser_decl_specifier_seq (parser,
11686 CP_PARSER_FLAGS_NONE,
11687 &decl_specifiers,
11688 &declares_class_or_enum);
a723baf1
MM
11689 /* If an error occurred, there's no reason to attempt to parse the
11690 rest of the declaration. */
11691 if (cp_parser_error_occurred (parser))
11692 {
11693 parser->type_definition_forbidden_message = saved_message;
058b15c1 11694 return NULL;
a723baf1
MM
11695 }
11696
11697 /* Peek at the next token. */
11698 token = cp_lexer_peek_token (parser->lexer);
11699 /* If the next token is a `)', `,', `=', `>', or `...', then there
11700 is no declarator. */
21526606 11701 if (token->type == CPP_CLOSE_PAREN
a723baf1
MM
11702 || token->type == CPP_COMMA
11703 || token->type == CPP_EQ
11704 || token->type == CPP_ELLIPSIS
11705 || token->type == CPP_GREATER)
4bb8ca28 11706 {
058b15c1 11707 declarator = NULL;
4bb8ca28
MM
11708 if (parenthesized_p)
11709 *parenthesized_p = false;
11710 }
a723baf1
MM
11711 /* Otherwise, there should be a declarator. */
11712 else
11713 {
11714 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11715 parser->default_arg_ok_p = false;
21526606 11716
5c832178
MM
11717 /* After seeing a decl-specifier-seq, if the next token is not a
11718 "(", there is no possibility that the code is a valid
4f8163b1
MM
11719 expression. Therefore, if parsing tentatively, we commit at
11720 this point. */
5c832178 11721 if (!parser->in_template_argument_list_p
643aee72 11722 /* In an expression context, having seen:
4f8163b1 11723
a7324e75 11724 (int((char ...
4f8163b1
MM
11725
11726 we cannot be sure whether we are looking at a
a7324e75
MM
11727 function-type (taking a "char" as a parameter) or a cast
11728 of some object of type "char" to "int". */
4f8163b1 11729 && !parser->in_type_id_in_expr_p
0b16f8f4 11730 && cp_parser_uncommitted_to_tentative_parse_p (parser)
5c832178
MM
11731 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11732 cp_parser_commit_to_tentative_parse (parser);
11733 /* Parse the declarator. */
a723baf1 11734 declarator = cp_parser_declarator (parser,
62b8a44e 11735 CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 11736 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
11737 parenthesized_p,
11738 /*member_p=*/false);
a723baf1 11739 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d 11740 /* After the declarator, allow more attributes. */
62d1db17 11741 decl_specifiers.attributes
98ca843c 11742 = chainon (decl_specifiers.attributes,
62d1db17 11743 cp_parser_attributes_opt (parser));
a723baf1
MM
11744 }
11745
62b8a44e 11746 /* The restriction on defining new types applies only to the type
a723baf1
MM
11747 of the parameter, not to the default argument. */
11748 parser->type_definition_forbidden_message = saved_message;
11749
11750 /* If the next token is `=', then process a default argument. */
11751 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11752 {
11753 bool saved_greater_than_is_operator_p;
11754 /* Consume the `='. */
11755 cp_lexer_consume_token (parser->lexer);
11756
11757 /* If we are defining a class, then the tokens that make up the
11758 default argument must be saved and processed later. */
21526606 11759 if (!template_parm_p && at_class_scope_p ()
ec194454 11760 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11761 {
11762 unsigned depth = 0;
c162c75e
MA
11763 cp_token *first_token;
11764 cp_token *token;
a723baf1
MM
11765
11766 /* Add tokens until we have processed the entire default
03fd3f84 11767 argument. We add the range [first_token, token). */
c162c75e 11768 first_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
11769 while (true)
11770 {
11771 bool done = false;
a723baf1
MM
11772
11773 /* Peek at the next token. */
11774 token = cp_lexer_peek_token (parser->lexer);
11775 /* What we do depends on what token we have. */
11776 switch (token->type)
11777 {
11778 /* In valid code, a default argument must be
11779 immediately followed by a `,' `)', or `...'. */
11780 case CPP_COMMA:
11781 case CPP_CLOSE_PAREN:
11782 case CPP_ELLIPSIS:
11783 /* If we run into a non-nested `;', `}', or `]',
11784 then the code is invalid -- but the default
11785 argument is certainly over. */
11786 case CPP_SEMICOLON:
11787 case CPP_CLOSE_BRACE:
11788 case CPP_CLOSE_SQUARE:
11789 if (depth == 0)
11790 done = true;
11791 /* Update DEPTH, if necessary. */
11792 else if (token->type == CPP_CLOSE_PAREN
11793 || token->type == CPP_CLOSE_BRACE
11794 || token->type == CPP_CLOSE_SQUARE)
11795 --depth;
11796 break;
11797
11798 case CPP_OPEN_PAREN:
11799 case CPP_OPEN_SQUARE:
11800 case CPP_OPEN_BRACE:
11801 ++depth;
11802 break;
11803
11804 case CPP_GREATER:
11805 /* If we see a non-nested `>', and `>' is not an
11806 operator, then it marks the end of the default
11807 argument. */
11808 if (!depth && !greater_than_is_operator_p)
11809 done = true;
11810 break;
11811
11812 /* If we run out of tokens, issue an error message. */
11813 case CPP_EOF:
11814 error ("file ends in default argument");
11815 done = true;
11816 break;
11817
11818 case CPP_NAME:
11819 case CPP_SCOPE:
11820 /* In these cases, we should look for template-ids.
21526606 11821 For example, if the default argument is
a723baf1
MM
11822 `X<int, double>()', we need to do name lookup to
11823 figure out whether or not `X' is a template; if
34cd5ae7 11824 so, the `,' does not end the default argument.
a723baf1
MM
11825
11826 That is not yet done. */
11827 break;
11828
11829 default:
11830 break;
11831 }
11832
11833 /* If we've reached the end, stop. */
11834 if (done)
11835 break;
21526606 11836
a723baf1
MM
11837 /* Add the token to the token block. */
11838 token = cp_lexer_consume_token (parser->lexer);
a723baf1 11839 }
c162c75e
MA
11840
11841 /* Create a DEFAULT_ARG to represented the unparsed default
11842 argument. */
11843 default_argument = make_node (DEFAULT_ARG);
11844 DEFARG_TOKENS (default_argument)
11845 = cp_token_cache_new (first_token, token);
a723baf1
MM
11846 }
11847 /* Outside of a class definition, we can just parse the
11848 assignment-expression. */
11849 else
11850 {
11851 bool saved_local_variables_forbidden_p;
11852
11853 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11854 set correctly. */
21526606 11855 saved_greater_than_is_operator_p
a723baf1
MM
11856 = parser->greater_than_is_operator_p;
11857 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11858 /* Local variable names (and the `this' keyword) may not
11859 appear in a default argument. */
21526606 11860 saved_local_variables_forbidden_p
a723baf1
MM
11861 = parser->local_variables_forbidden_p;
11862 parser->local_variables_forbidden_p = true;
11863 /* Parse the assignment-expression. */
11864 default_argument = cp_parser_assignment_expression (parser);
11865 /* Restore saved state. */
21526606 11866 parser->greater_than_is_operator_p
a723baf1 11867 = saved_greater_than_is_operator_p;
21526606
EC
11868 parser->local_variables_forbidden_p
11869 = saved_local_variables_forbidden_p;
a723baf1
MM
11870 }
11871 if (!parser->default_arg_ok_p)
11872 {
c67d36d0
NS
11873 if (!flag_pedantic_errors)
11874 warning ("deprecated use of default argument for parameter of non-function");
11875 else
11876 {
11877 error ("default arguments are only permitted for function parameters");
11878 default_argument = NULL_TREE;
11879 }
a723baf1
MM
11880 }
11881 }
11882 else
11883 default_argument = NULL_TREE;
21526606 11884
62d1db17 11885 return make_parameter_declarator (&decl_specifiers,
058b15c1
MM
11886 declarator,
11887 default_argument);
a723baf1
MM
11888}
11889
a723baf1
MM
11890/* Parse a function-body.
11891
11892 function-body:
11893 compound_statement */
11894
11895static void
11896cp_parser_function_body (cp_parser *parser)
11897{
325c3691 11898 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
11899}
11900
11901/* Parse a ctor-initializer-opt followed by a function-body. Return
11902 true if a ctor-initializer was present. */
11903
11904static bool
11905cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11906{
11907 tree body;
11908 bool ctor_initializer_p;
11909
11910 /* Begin the function body. */
11911 body = begin_function_body ();
11912 /* Parse the optional ctor-initializer. */
11913 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11914 /* Parse the function-body. */
11915 cp_parser_function_body (parser);
11916 /* Finish the function body. */
11917 finish_function_body (body);
11918
11919 return ctor_initializer_p;
11920}
11921
11922/* Parse an initializer.
11923
11924 initializer:
11925 = initializer-clause
21526606 11926 ( expression-list )
a723baf1
MM
11927
11928 Returns a expression representing the initializer. If no
21526606 11929 initializer is present, NULL_TREE is returned.
a723baf1
MM
11930
11931 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11932 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
39703eb9
MM
11933 set to FALSE if there is no initializer present. If there is an
11934 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11935 is set to true; otherwise it is set to false. */
a723baf1
MM
11936
11937static tree
39703eb9
MM
11938cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11939 bool* non_constant_p)
a723baf1
MM
11940{
11941 cp_token *token;
11942 tree init;
11943
11944 /* Peek at the next token. */
11945 token = cp_lexer_peek_token (parser->lexer);
11946
11947 /* Let our caller know whether or not this initializer was
11948 parenthesized. */
11949 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
39703eb9
MM
11950 /* Assume that the initializer is constant. */
11951 *non_constant_p = false;
a723baf1
MM
11952
11953 if (token->type == CPP_EQ)
11954 {
11955 /* Consume the `='. */
11956 cp_lexer_consume_token (parser->lexer);
11957 /* Parse the initializer-clause. */
39703eb9 11958 init = cp_parser_initializer_clause (parser, non_constant_p);
a723baf1
MM
11959 }
11960 else if (token->type == CPP_OPEN_PAREN)
39703eb9
MM
11961 init = cp_parser_parenthesized_expression_list (parser, false,
11962 non_constant_p);
a723baf1
MM
11963 else
11964 {
11965 /* Anything else is an error. */
11966 cp_parser_error (parser, "expected initializer");
11967 init = error_mark_node;
11968 }
11969
11970 return init;
11971}
11972
21526606 11973/* Parse an initializer-clause.
a723baf1
MM
11974
11975 initializer-clause:
11976 assignment-expression
11977 { initializer-list , [opt] }
11978 { }
11979
21526606 11980 Returns an expression representing the initializer.
a723baf1
MM
11981
11982 If the `assignment-expression' production is used the value
21526606 11983 returned is simply a representation for the expression.
a723baf1
MM
11984
11985 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11986 the elements of the initializer-list (or NULL_TREE, if the last
11987 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11988 NULL_TREE. There is no way to detect whether or not the optional
39703eb9
MM
11989 trailing `,' was provided. NON_CONSTANT_P is as for
11990 cp_parser_initializer. */
a723baf1
MM
11991
11992static tree
39703eb9 11993cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
11994{
11995 tree initializer;
11996
11997 /* If it is not a `{', then we are looking at an
11998 assignment-expression. */
11999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
0da99d4e 12000 {
98ca843c 12001 initializer
0da99d4e
GB
12002 = cp_parser_constant_expression (parser,
12003 /*allow_non_constant_p=*/true,
12004 non_constant_p);
12005 if (!*non_constant_p)
12006 initializer = fold_non_dependent_expr (initializer);
12007 }
a723baf1
MM
12008 else
12009 {
12010 /* Consume the `{' token. */
12011 cp_lexer_consume_token (parser->lexer);
12012 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12013 initializer = make_node (CONSTRUCTOR);
a723baf1
MM
12014 /* If it's not a `}', then there is a non-trivial initializer. */
12015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12016 {
12017 /* Parse the initializer list. */
12018 CONSTRUCTOR_ELTS (initializer)
39703eb9 12019 = cp_parser_initializer_list (parser, non_constant_p);
a723baf1
MM
12020 /* A trailing `,' token is allowed. */
12021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12022 cp_lexer_consume_token (parser->lexer);
12023 }
a723baf1
MM
12024 /* Now, there should be a trailing `}'. */
12025 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12026 }
12027
12028 return initializer;
12029}
12030
12031/* Parse an initializer-list.
12032
12033 initializer-list:
12034 initializer-clause
12035 initializer-list , initializer-clause
12036
12037 GNU Extension:
21526606 12038
a723baf1
MM
12039 initializer-list:
12040 identifier : initializer-clause
12041 initializer-list, identifier : initializer-clause
12042
12043 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12044 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
39703eb9
MM
12045 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12046 as for cp_parser_initializer. */
a723baf1
MM
12047
12048static tree
39703eb9 12049cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
a723baf1
MM
12050{
12051 tree initializers = NULL_TREE;
12052
39703eb9
MM
12053 /* Assume all of the expressions are constant. */
12054 *non_constant_p = false;
12055
a723baf1
MM
12056 /* Parse the rest of the list. */
12057 while (true)
12058 {
12059 cp_token *token;
12060 tree identifier;
12061 tree initializer;
39703eb9
MM
12062 bool clause_non_constant_p;
12063
a723baf1
MM
12064 /* If the next token is an identifier and the following one is a
12065 colon, we are looking at the GNU designated-initializer
12066 syntax. */
12067 if (cp_parser_allow_gnu_extensions_p (parser)
12068 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12069 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12070 {
12071 /* Consume the identifier. */
12072 identifier = cp_lexer_consume_token (parser->lexer)->value;
12073 /* Consume the `:'. */
12074 cp_lexer_consume_token (parser->lexer);
12075 }
12076 else
12077 identifier = NULL_TREE;
12078
12079 /* Parse the initializer. */
21526606 12080 initializer = cp_parser_initializer_clause (parser,
39703eb9
MM
12081 &clause_non_constant_p);
12082 /* If any clause is non-constant, so is the entire initializer. */
12083 if (clause_non_constant_p)
12084 *non_constant_p = true;
a723baf1
MM
12085 /* Add it to the list. */
12086 initializers = tree_cons (identifier, initializer, initializers);
12087
12088 /* If the next token is not a comma, we have reached the end of
12089 the list. */
12090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12091 break;
12092
12093 /* Peek at the next token. */
12094 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12095 /* If the next token is a `}', then we're still done. An
12096 initializer-clause can have a trailing `,' after the
12097 initializer-list and before the closing `}'. */
12098 if (token->type == CPP_CLOSE_BRACE)
12099 break;
12100
12101 /* Consume the `,' token. */
12102 cp_lexer_consume_token (parser->lexer);
12103 }
12104
12105 /* The initializers were built up in reverse order, so we need to
12106 reverse them now. */
12107 return nreverse (initializers);
12108}
12109
12110/* Classes [gram.class] */
12111
12112/* Parse a class-name.
12113
12114 class-name:
12115 identifier
12116 template-id
12117
12118 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12119 to indicate that names looked up in dependent types should be
12120 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12121 keyword has been used to indicate that the name that appears next
fc6a28d7
MM
12122 is a template. TAG_TYPE indicates the explicit tag given before
12123 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12124 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12125 is the class being defined in a class-head.
a723baf1
MM
12126
12127 Returns the TYPE_DECL representing the class. */
12128
12129static tree
21526606
EC
12130cp_parser_class_name (cp_parser *parser,
12131 bool typename_keyword_p,
12132 bool template_keyword_p,
fc6a28d7 12133 enum tag_types tag_type,
a723baf1 12134 bool check_dependency_p,
a668c6ad
MM
12135 bool class_head_p,
12136 bool is_declaration)
a723baf1
MM
12137{
12138 tree decl;
12139 tree scope;
12140 bool typename_p;
e5976695
MM
12141 cp_token *token;
12142
12143 /* All class-names start with an identifier. */
12144 token = cp_lexer_peek_token (parser->lexer);
12145 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12146 {
12147 cp_parser_error (parser, "expected class-name");
12148 return error_mark_node;
12149 }
21526606 12150
a723baf1
MM
12151 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12152 to a template-id, so we save it here. */
12153 scope = parser->scope;
3adee96c
KL
12154 if (scope == error_mark_node)
12155 return error_mark_node;
21526606 12156
a723baf1
MM
12157 /* Any name names a type if we're following the `typename' keyword
12158 in a qualified name where the enclosing scope is type-dependent. */
12159 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 12160 && dependent_type_p (scope));
e5976695
MM
12161 /* Handle the common case (an identifier, but not a template-id)
12162 efficiently. */
21526606 12163 if (token->type == CPP_NAME
f4abade9 12164 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
a723baf1 12165 {
a723baf1
MM
12166 tree identifier;
12167
12168 /* Look for the identifier. */
12169 identifier = cp_parser_identifier (parser);
12170 /* If the next token isn't an identifier, we are certainly not
12171 looking at a class-name. */
12172 if (identifier == error_mark_node)
12173 decl = error_mark_node;
12174 /* If we know this is a type-name, there's no need to look it
12175 up. */
12176 else if (typename_p)
12177 decl = identifier;
12178 else
12179 {
12180 /* If the next token is a `::', then the name must be a type
12181 name.
12182
12183 [basic.lookup.qual]
12184
12185 During the lookup for a name preceding the :: scope
12186 resolution operator, object, function, and enumerator
12187 names are ignored. */
12188 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
fc6a28d7 12189 tag_type = typename_type;
a723baf1 12190 /* Look up the name. */
21526606 12191 decl = cp_parser_lookup_name (parser, identifier,
fc6a28d7 12192 tag_type,
b0bc6e8e 12193 /*is_template=*/false,
eea9800f 12194 /*is_namespace=*/false,
8f78f01f
MM
12195 check_dependency_p,
12196 /*ambiguous_p=*/NULL);
a723baf1
MM
12197 }
12198 }
e5976695
MM
12199 else
12200 {
12201 /* Try a template-id. */
12202 decl = cp_parser_template_id (parser, template_keyword_p,
a668c6ad
MM
12203 check_dependency_p,
12204 is_declaration);
e5976695
MM
12205 if (decl == error_mark_node)
12206 return error_mark_node;
12207 }
a723baf1
MM
12208
12209 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12210
12211 /* If this is a typename, create a TYPENAME_TYPE. */
12212 if (typename_p && decl != error_mark_node)
4bfb8bba 12213 {
fc6a28d7 12214 decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
4bfb8bba
MM
12215 if (decl != error_mark_node)
12216 decl = TYPE_NAME (decl);
12217 }
a723baf1
MM
12218
12219 /* Check to see that it is really the name of a class. */
21526606 12220 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
a723baf1
MM
12221 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12222 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12223 /* Situations like this:
12224
12225 template <typename T> struct A {
21526606 12226 typename T::template X<int>::I i;
a723baf1
MM
12227 };
12228
12229 are problematic. Is `T::template X<int>' a class-name? The
12230 standard does not seem to be definitive, but there is no other
12231 valid interpretation of the following `::'. Therefore, those
12232 names are considered class-names. */
fc6a28d7 12233 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
a723baf1
MM
12234 else if (decl == error_mark_node
12235 || TREE_CODE (decl) != TYPE_DECL
07c65e00 12236 || TREE_TYPE (decl) == error_mark_node
a723baf1
MM
12237 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12238 {
12239 cp_parser_error (parser, "expected class-name");
12240 return error_mark_node;
12241 }
12242
12243 return decl;
12244}
12245
12246/* Parse a class-specifier.
12247
12248 class-specifier:
12249 class-head { member-specification [opt] }
12250
12251 Returns the TREE_TYPE representing the class. */
12252
12253static tree
94edc4ab 12254cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
12255{
12256 cp_token *token;
12257 tree type;
6de9cd9a 12258 tree attributes = NULL_TREE;
a723baf1
MM
12259 int has_trailing_semicolon;
12260 bool nested_name_specifier_p;
a723baf1 12261 unsigned saved_num_template_parameter_lists;
87c465f5 12262 tree old_scope = NULL_TREE;
2436b51f 12263 tree scope = NULL_TREE;
a723baf1 12264
8d241e0b 12265 push_deferring_access_checks (dk_no_deferred);
cf22909c 12266
a723baf1
MM
12267 /* Parse the class-head. */
12268 type = cp_parser_class_head (parser,
38b305d0
JM
12269 &nested_name_specifier_p,
12270 &attributes);
a723baf1
MM
12271 /* If the class-head was a semantic disaster, skip the entire body
12272 of the class. */
12273 if (!type)
12274 {
12275 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 12276 pop_deferring_access_checks ();
a723baf1
MM
12277 return error_mark_node;
12278 }
cf22909c 12279
a723baf1
MM
12280 /* Look for the `{'. */
12281 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
12282 {
12283 pop_deferring_access_checks ();
12284 return error_mark_node;
12285 }
12286
a723baf1
MM
12287 /* Issue an error message if type-definitions are forbidden here. */
12288 cp_parser_check_type_definition (parser);
12289 /* Remember that we are defining one more class. */
12290 ++parser->num_classes_being_defined;
12291 /* Inside the class, surrounding template-parameter-lists do not
12292 apply. */
21526606
EC
12293 saved_num_template_parameter_lists
12294 = parser->num_template_parameter_lists;
a723baf1 12295 parser->num_template_parameter_lists = 0;
78757caa 12296
a723baf1 12297 /* Start the class. */
eeb23c11 12298 if (nested_name_specifier_p)
2436b51f
MM
12299 {
12300 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
87c465f5 12301 old_scope = push_inner_scope (scope);
2436b51f 12302 }
a723baf1 12303 type = begin_class_definition (type);
98ca843c 12304
a723baf1 12305 if (type == error_mark_node)
9bcb9aae 12306 /* If the type is erroneous, skip the entire body of the class. */
a723baf1
MM
12307 cp_parser_skip_to_closing_brace (parser);
12308 else
12309 /* Parse the member-specification. */
12310 cp_parser_member_specification_opt (parser);
98ca843c 12311
a723baf1
MM
12312 /* Look for the trailing `}'. */
12313 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12314 /* We get better error messages by noticing a common problem: a
12315 missing trailing `;'. */
12316 token = cp_lexer_peek_token (parser->lexer);
12317 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
38b305d0 12318 /* Look for trailing attributes to apply to this class. */
a723baf1 12319 if (cp_parser_allow_gnu_extensions_p (parser))
560ad596 12320 {
38b305d0
JM
12321 tree sub_attr = cp_parser_attributes_opt (parser);
12322 attributes = chainon (attributes, sub_attr);
560ad596 12323 }
38b305d0
JM
12324 if (type != error_mark_node)
12325 type = finish_struct (type, attributes);
87c465f5
KL
12326 if (nested_name_specifier_p)
12327 pop_inner_scope (old_scope, scope);
a723baf1
MM
12328 /* If this class is not itself within the scope of another class,
12329 then we need to parse the bodies of all of the queued function
12330 definitions. Note that the queued functions defined in a class
12331 are not always processed immediately following the
12332 class-specifier for that class. Consider:
12333
12334 struct A {
12335 struct B { void f() { sizeof (A); } };
12336 };
12337
12338 If `f' were processed before the processing of `A' were
12339 completed, there would be no way to compute the size of `A'.
12340 Note that the nesting we are interested in here is lexical --
12341 not the semantic nesting given by TYPE_CONTEXT. In particular,
12342 for:
12343
12344 struct A { struct B; };
12345 struct A::B { void f() { } };
12346
12347 there is no need to delay the parsing of `A::B::f'. */
21526606 12348 if (--parser->num_classes_being_defined == 0)
a723baf1 12349 {
8218bd34
MM
12350 tree queue_entry;
12351 tree fn;
f44b0c8e
MM
12352 tree class_type;
12353 bool pop_p;
a723baf1 12354
8218bd34
MM
12355 /* In a first pass, parse default arguments to the functions.
12356 Then, in a second pass, parse the bodies of the functions.
12357 This two-phased approach handles cases like:
21526606
EC
12358
12359 struct S {
12360 void f() { g(); }
8218bd34
MM
12361 void g(int i = 3);
12362 };
12363
12364 */
f44b0c8e
MM
12365 class_type = NULL_TREE;
12366 pop_p = false;
8db1028e
NS
12367 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12368 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12369 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12370 TREE_PURPOSE (parser->unparsed_functions_queues)
12371 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
8218bd34
MM
12372 {
12373 fn = TREE_VALUE (queue_entry);
8218bd34
MM
12374 /* If there are default arguments that have not yet been processed,
12375 take care of them now. */
f44b0c8e
MM
12376 if (class_type != TREE_PURPOSE (queue_entry))
12377 {
12378 if (pop_p)
12379 pop_scope (class_type);
12380 class_type = TREE_PURPOSE (queue_entry);
12381 pop_p = push_scope (class_type);
12382 }
12383 /* Make sure that any template parameters are in scope. */
12384 maybe_begin_member_template_processing (fn);
12385 /* Parse the default argument expressions. */
8218bd34
MM
12386 cp_parser_late_parsing_default_args (parser, fn);
12387 /* Remove any template parameters from the symbol table. */
12388 maybe_end_member_template_processing ();
12389 }
f44b0c8e
MM
12390 if (pop_p)
12391 pop_scope (class_type);
8218bd34 12392 /* Now parse the body of the functions. */
8db1028e
NS
12393 for (TREE_VALUE (parser->unparsed_functions_queues)
12394 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12395 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12396 TREE_VALUE (parser->unparsed_functions_queues)
12397 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
a723baf1 12398 {
a723baf1 12399 /* Figure out which function we need to process. */
a723baf1
MM
12400 fn = TREE_VALUE (queue_entry);
12401
4543ee47
ZD
12402 /* A hack to prevent garbage collection. */
12403 function_depth++;
12404
a723baf1
MM
12405 /* Parse the function. */
12406 cp_parser_late_parsing_for_member (parser, fn);
4543ee47 12407 function_depth--;
a723baf1 12408 }
a723baf1
MM
12409 }
12410
12411 /* Put back any saved access checks. */
cf22909c 12412 pop_deferring_access_checks ();
a723baf1
MM
12413
12414 /* Restore the count of active template-parameter-lists. */
12415 parser->num_template_parameter_lists
12416 = saved_num_template_parameter_lists;
12417
12418 return type;
12419}
12420
12421/* Parse a class-head.
12422
12423 class-head:
12424 class-key identifier [opt] base-clause [opt]
12425 class-key nested-name-specifier identifier base-clause [opt]
21526606
EC
12426 class-key nested-name-specifier [opt] template-id
12427 base-clause [opt]
a723baf1
MM
12428
12429 GNU Extensions:
12430 class-key attributes identifier [opt] base-clause [opt]
12431 class-key attributes nested-name-specifier identifier base-clause [opt]
21526606
EC
12432 class-key attributes nested-name-specifier [opt] template-id
12433 base-clause [opt]
a723baf1
MM
12434
12435 Returns the TYPE of the indicated class. Sets
12436 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12437 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1 12438
55dcbc12
NS
12439 Returns error_mark_node if this is not a class-head.
12440
a723baf1
MM
12441 Returns NULL_TREE if the class-head is syntactically valid, but
12442 semantically invalid in a way that means we should skip the entire
12443 body of the class. */
12444
12445static tree
21526606 12446cp_parser_class_head (cp_parser* parser,
38b305d0
JM
12447 bool* nested_name_specifier_p,
12448 tree *attributes_p)
a723baf1 12449{
a723baf1
MM
12450 tree nested_name_specifier;
12451 enum tag_types class_key;
12452 tree id = NULL_TREE;
12453 tree type = NULL_TREE;
12454 tree attributes;
12455 bool template_id_p = false;
12456 bool qualified_p = false;
12457 bool invalid_nested_name_p = false;
afb0918a 12458 bool invalid_explicit_specialization_p = false;
91b004e5 12459 bool pop_p = false;
a723baf1 12460 unsigned num_templates;
cad7e87b 12461 tree bases;
a723baf1
MM
12462
12463 /* Assume no nested-name-specifier will be present. */
12464 *nested_name_specifier_p = false;
12465 /* Assume no template parameter lists will be used in defining the
12466 type. */
12467 num_templates = 0;
12468
12469 /* Look for the class-key. */
12470 class_key = cp_parser_class_key (parser);
12471 if (class_key == none_type)
12472 return error_mark_node;
12473
12474 /* Parse the attributes. */
12475 attributes = cp_parser_attributes_opt (parser);
12476
12477 /* If the next token is `::', that is invalid -- but sometimes
12478 people do try to write:
12479
21526606 12480 struct ::S {};
a723baf1
MM
12481
12482 Handle this gracefully by accepting the extra qualifier, and then
12483 issuing an error about it later if this really is a
2050a1bb 12484 class-head. If it turns out just to be an elaborated type
a723baf1
MM
12485 specifier, remain silent. */
12486 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12487 qualified_p = true;
12488
8d241e0b
KL
12489 push_deferring_access_checks (dk_no_check);
12490
a723baf1
MM
12491 /* Determine the name of the class. Begin by looking for an
12492 optional nested-name-specifier. */
21526606 12493 nested_name_specifier
a723baf1
MM
12494 = cp_parser_nested_name_specifier_opt (parser,
12495 /*typename_keyword_p=*/false,
66d418e6 12496 /*check_dependency_p=*/false,
a668c6ad
MM
12497 /*type_p=*/false,
12498 /*is_declaration=*/false);
a723baf1
MM
12499 /* If there was a nested-name-specifier, then there *must* be an
12500 identifier. */
12501 if (nested_name_specifier)
12502 {
12503 /* Although the grammar says `identifier', it really means
12504 `class-name' or `template-name'. You are only allowed to
12505 define a class that has already been declared with this
21526606 12506 syntax.
a723baf1
MM
12507
12508 The proposed resolution for Core Issue 180 says that whever
12509 you see `class T::X' you should treat `X' as a type-name.
21526606 12510
a723baf1 12511 It is OK to define an inaccessible class; for example:
21526606 12512
a723baf1
MM
12513 class A { class B; };
12514 class A::B {};
21526606 12515
a723baf1
MM
12516 We do not know if we will see a class-name, or a
12517 template-name. We look for a class-name first, in case the
12518 class-name is a template-id; if we looked for the
12519 template-name first we would stop after the template-name. */
12520 cp_parser_parse_tentatively (parser);
12521 type = cp_parser_class_name (parser,
12522 /*typename_keyword_p=*/false,
12523 /*template_keyword_p=*/false,
fc6a28d7 12524 class_type,
a723baf1 12525 /*check_dependency_p=*/false,
a668c6ad
MM
12526 /*class_head_p=*/true,
12527 /*is_declaration=*/false);
a723baf1
MM
12528 /* If that didn't work, ignore the nested-name-specifier. */
12529 if (!cp_parser_parse_definitely (parser))
12530 {
12531 invalid_nested_name_p = true;
12532 id = cp_parser_identifier (parser);
12533 if (id == error_mark_node)
12534 id = NULL_TREE;
12535 }
12536 /* If we could not find a corresponding TYPE, treat this
12537 declaration like an unqualified declaration. */
12538 if (type == error_mark_node)
12539 nested_name_specifier = NULL_TREE;
12540 /* Otherwise, count the number of templates used in TYPE and its
12541 containing scopes. */
21526606 12542 else
a723baf1
MM
12543 {
12544 tree scope;
12545
21526606 12546 for (scope = TREE_TYPE (type);
a723baf1 12547 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21526606 12548 scope = (TYPE_P (scope)
a723baf1 12549 ? TYPE_CONTEXT (scope)
21526606
EC
12550 : DECL_CONTEXT (scope)))
12551 if (TYPE_P (scope)
a723baf1
MM
12552 && CLASS_TYPE_P (scope)
12553 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
12554 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12555 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
12556 ++num_templates;
12557 }
12558 }
12559 /* Otherwise, the identifier is optional. */
12560 else
12561 {
12562 /* We don't know whether what comes next is a template-id,
12563 an identifier, or nothing at all. */
12564 cp_parser_parse_tentatively (parser);
12565 /* Check for a template-id. */
21526606 12566 id = cp_parser_template_id (parser,
a723baf1 12567 /*template_keyword_p=*/false,
a668c6ad
MM
12568 /*check_dependency_p=*/true,
12569 /*is_declaration=*/true);
a723baf1
MM
12570 /* If that didn't work, it could still be an identifier. */
12571 if (!cp_parser_parse_definitely (parser))
12572 {
12573 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12574 id = cp_parser_identifier (parser);
12575 else
12576 id = NULL_TREE;
12577 }
12578 else
12579 {
12580 template_id_p = true;
12581 ++num_templates;
12582 }
12583 }
12584
8d241e0b
KL
12585 pop_deferring_access_checks ();
12586
15077df5
MM
12587 if (id)
12588 cp_parser_check_for_invalid_template_id (parser, id);
ee43dab5 12589
a723baf1
MM
12590 /* If it's not a `:' or a `{' then we can't really be looking at a
12591 class-head, since a class-head only appears as part of a
12592 class-specifier. We have to detect this situation before calling
12593 xref_tag, since that has irreversible side-effects. */
12594 if (!cp_parser_next_token_starts_class_definition_p (parser))
12595 {
2a13a625 12596 cp_parser_error (parser, "expected %<{%> or %<:%>");
a723baf1
MM
12597 return error_mark_node;
12598 }
12599
12600 /* At this point, we're going ahead with the class-specifier, even
12601 if some other problem occurs. */
12602 cp_parser_commit_to_tentative_parse (parser);
12603 /* Issue the error about the overly-qualified name now. */
12604 if (qualified_p)
12605 cp_parser_error (parser,
12606 "global qualification of class name is invalid");
12607 else if (invalid_nested_name_p)
12608 cp_parser_error (parser,
12609 "qualified name does not name a class");
88081599
MM
12610 else if (nested_name_specifier)
12611 {
12612 tree scope;
9bf0e588
VR
12613
12614 /* Reject typedef-names in class heads. */
12615 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12616 {
12617 error ("invalid class name in declaration of %qD", type);
12618 type = NULL_TREE;
12619 goto done;
12620 }
12621
88081599
MM
12622 /* Figure out in what scope the declaration is being placed. */
12623 scope = current_scope ();
88081599
MM
12624 /* If that scope does not contain the scope in which the
12625 class was originally declared, the program is invalid. */
12626 if (scope && !is_ancestor (scope, nested_name_specifier))
12627 {
2a13a625
GDR
12628 error ("declaration of %qD in %qD which does not enclose %qD",
12629 type, scope, nested_name_specifier);
88081599
MM
12630 type = NULL_TREE;
12631 goto done;
12632 }
12633 /* [dcl.meaning]
12634
12635 A declarator-id shall not be qualified exception of the
12636 definition of a ... nested class outside of its class
12637 ... [or] a the definition or explicit instantiation of a
12638 class member of a namespace outside of its namespace. */
12639 if (scope == nested_name_specifier)
12640 {
12641 pedwarn ("extra qualification ignored");
12642 nested_name_specifier = NULL_TREE;
12643 num_templates = 0;
12644 }
12645 }
afb0918a
MM
12646 /* An explicit-specialization must be preceded by "template <>". If
12647 it is not, try to recover gracefully. */
21526606 12648 if (at_namespace_scope_p ()
afb0918a 12649 && parser->num_template_parameter_lists == 0
eeb23c11 12650 && template_id_p)
afb0918a 12651 {
2a13a625 12652 error ("an explicit specialization must be preceded by %<template <>%>");
afb0918a
MM
12653 invalid_explicit_specialization_p = true;
12654 /* Take the same action that would have been taken by
12655 cp_parser_explicit_specialization. */
12656 ++parser->num_template_parameter_lists;
12657 begin_specialization ();
12658 }
12659 /* There must be no "return" statements between this point and the
12660 end of this function; set "type "to the correct return value and
12661 use "goto done;" to return. */
a723baf1
MM
12662 /* Make sure that the right number of template parameters were
12663 present. */
12664 if (!cp_parser_check_template_parameters (parser, num_templates))
afb0918a
MM
12665 {
12666 /* If something went wrong, there is no point in even trying to
12667 process the class-definition. */
12668 type = NULL_TREE;
12669 goto done;
12670 }
a723baf1 12671
a723baf1
MM
12672 /* Look up the type. */
12673 if (template_id_p)
12674 {
12675 type = TREE_TYPE (id);
12676 maybe_process_partial_specialization (type);
12677 }
12678 else if (!nested_name_specifier)
12679 {
12680 /* If the class was unnamed, create a dummy name. */
12681 if (!id)
12682 id = make_anon_name ();
29ef83de 12683 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
cbd63935 12684 parser->num_template_parameter_lists);
a723baf1
MM
12685 }
12686 else
12687 {
a723baf1 12688 tree class_type;
91b004e5 12689 bool pop_p = false;
a723baf1
MM
12690
12691 /* Given:
12692
12693 template <typename T> struct S { struct T };
14d22dd6 12694 template <typename T> struct S<T>::T { };
a723baf1
MM
12695
12696 we will get a TYPENAME_TYPE when processing the definition of
12697 `S::T'. We need to resolve it to the actual type before we
12698 try to define it. */
12699 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12700 {
14d22dd6
MM
12701 class_type = resolve_typename_type (TREE_TYPE (type),
12702 /*only_current_p=*/false);
12703 if (class_type != error_mark_node)
12704 type = TYPE_NAME (class_type);
12705 else
12706 {
12707 cp_parser_error (parser, "could not resolve typename type");
12708 type = error_mark_node;
12709 }
a723baf1
MM
12710 }
12711
560ad596
MM
12712 maybe_process_partial_specialization (TREE_TYPE (type));
12713 class_type = current_class_type;
12714 /* Enter the scope indicated by the nested-name-specifier. */
12715 if (nested_name_specifier)
91b004e5 12716 pop_p = push_scope (nested_name_specifier);
560ad596
MM
12717 /* Get the canonical version of this type. */
12718 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12719 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12720 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
55dcbc12
NS
12721 {
12722 type = push_template_decl (type);
12723 if (type == error_mark_node)
12724 {
12725 type = NULL_TREE;
12726 goto done;
12727 }
12728 }
12729
560ad596
MM
12730 type = TREE_TYPE (type);
12731 if (nested_name_specifier)
eeb23c11
MM
12732 {
12733 *nested_name_specifier_p = true;
91b004e5
MM
12734 if (pop_p)
12735 pop_scope (nested_name_specifier);
eeb23c11 12736 }
a723baf1
MM
12737 }
12738 /* Indicate whether this class was declared as a `class' or as a
12739 `struct'. */
12740 if (TREE_CODE (type) == RECORD_TYPE)
12741 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12742 cp_parser_check_class_key (class_key, type);
12743
12744 /* Enter the scope containing the class; the names of base classes
12745 should be looked up in that context. For example, given:
12746
12747 struct A { struct B {}; struct C; };
12748 struct A::C : B {};
12749
12750 is valid. */
12751 if (nested_name_specifier)
91b004e5 12752 pop_p = push_scope (nested_name_specifier);
98ca843c 12753
cad7e87b 12754 bases = NULL_TREE;
98ca843c 12755
cad7e87b
NS
12756 /* Get the list of base-classes, if there is one. */
12757 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12758 bases = cp_parser_base_clause (parser);
98ca843c 12759
cad7e87b
NS
12760 /* Process the base classes. */
12761 xref_basetypes (type, bases);
a723baf1 12762
a723baf1
MM
12763 /* Leave the scope given by the nested-name-specifier. We will
12764 enter the class scope itself while processing the members. */
91b004e5 12765 if (pop_p)
a723baf1
MM
12766 pop_scope (nested_name_specifier);
12767
afb0918a
MM
12768 done:
12769 if (invalid_explicit_specialization_p)
12770 {
12771 end_specialization ();
12772 --parser->num_template_parameter_lists;
12773 }
38b305d0 12774 *attributes_p = attributes;
a723baf1
MM
12775 return type;
12776}
12777
12778/* Parse a class-key.
12779
12780 class-key:
12781 class
12782 struct
12783 union
12784
12785 Returns the kind of class-key specified, or none_type to indicate
12786 error. */
12787
12788static enum tag_types
94edc4ab 12789cp_parser_class_key (cp_parser* parser)
a723baf1
MM
12790{
12791 cp_token *token;
12792 enum tag_types tag_type;
12793
12794 /* Look for the class-key. */
12795 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12796 if (!token)
12797 return none_type;
12798
12799 /* Check to see if the TOKEN is a class-key. */
12800 tag_type = cp_parser_token_is_class_key (token);
12801 if (!tag_type)
12802 cp_parser_error (parser, "expected class-key");
12803 return tag_type;
12804}
12805
12806/* Parse an (optional) member-specification.
12807
12808 member-specification:
12809 member-declaration member-specification [opt]
12810 access-specifier : member-specification [opt] */
12811
12812static void
94edc4ab 12813cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
12814{
12815 while (true)
12816 {
12817 cp_token *token;
12818 enum rid keyword;
12819
12820 /* Peek at the next token. */
12821 token = cp_lexer_peek_token (parser->lexer);
12822 /* If it's a `}', or EOF then we've seen all the members. */
12823 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12824 break;
12825
12826 /* See if this token is a keyword. */
12827 keyword = token->keyword;
12828 switch (keyword)
12829 {
12830 case RID_PUBLIC:
12831 case RID_PROTECTED:
12832 case RID_PRIVATE:
12833 /* Consume the access-specifier. */
12834 cp_lexer_consume_token (parser->lexer);
12835 /* Remember which access-specifier is active. */
12836 current_access_specifier = token->value;
12837 /* Look for the `:'. */
12838 cp_parser_require (parser, CPP_COLON, "`:'");
12839 break;
12840
12841 default:
de3fe73c
MM
12842 /* Accept #pragmas at class scope. */
12843 if (token->type == CPP_PRAGMA)
12844 {
12845 cp_lexer_handle_pragma (parser->lexer);
12846 break;
12847 }
12848
a723baf1
MM
12849 /* Otherwise, the next construction must be a
12850 member-declaration. */
12851 cp_parser_member_declaration (parser);
a723baf1
MM
12852 }
12853 }
12854}
12855
21526606 12856/* Parse a member-declaration.
a723baf1
MM
12857
12858 member-declaration:
12859 decl-specifier-seq [opt] member-declarator-list [opt] ;
12860 function-definition ; [opt]
12861 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12862 using-declaration
21526606 12863 template-declaration
a723baf1
MM
12864
12865 member-declarator-list:
12866 member-declarator
12867 member-declarator-list , member-declarator
12868
12869 member-declarator:
21526606 12870 declarator pure-specifier [opt]
a723baf1 12871 declarator constant-initializer [opt]
21526606 12872 identifier [opt] : constant-expression
a723baf1
MM
12873
12874 GNU Extensions:
12875
12876 member-declaration:
12877 __extension__ member-declaration
12878
12879 member-declarator:
12880 declarator attributes [opt] pure-specifier [opt]
12881 declarator attributes [opt] constant-initializer [opt]
12882 identifier [opt] attributes [opt] : constant-expression */
12883
12884static void
94edc4ab 12885cp_parser_member_declaration (cp_parser* parser)
a723baf1 12886{
62d1db17 12887 cp_decl_specifier_seq decl_specifiers;
a723baf1
MM
12888 tree prefix_attributes;
12889 tree decl;
560ad596 12890 int declares_class_or_enum;
a723baf1
MM
12891 bool friend_p;
12892 cp_token *token;
12893 int saved_pedantic;
12894
12895 /* Check for the `__extension__' keyword. */
12896 if (cp_parser_extension_opt (parser, &saved_pedantic))
12897 {
12898 /* Recurse. */
12899 cp_parser_member_declaration (parser);
12900 /* Restore the old value of the PEDANTIC flag. */
12901 pedantic = saved_pedantic;
12902
12903 return;
12904 }
12905
12906 /* Check for a template-declaration. */
12907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12908 {
12909 /* Parse the template-declaration. */
12910 cp_parser_template_declaration (parser, /*member_p=*/true);
12911
12912 return;
12913 }
12914
12915 /* Check for a using-declaration. */
12916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12917 {
12918 /* Parse the using-declaration. */
12919 cp_parser_using_declaration (parser);
12920
12921 return;
12922 }
21526606 12923
a723baf1 12924 /* Parse the decl-specifier-seq. */
62d1db17
MM
12925 cp_parser_decl_specifier_seq (parser,
12926 CP_PARSER_FLAGS_OPTIONAL,
12927 &decl_specifiers,
12928 &declares_class_or_enum);
12929 prefix_attributes = decl_specifiers.attributes;
12930 decl_specifiers.attributes = NULL_TREE;
8fbc5ae7 12931 /* Check for an invalid type-name. */
de3fe73c
MM
12932 if (!decl_specifiers.type
12933 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8fbc5ae7 12934 return;
a723baf1
MM
12935 /* If there is no declarator, then the decl-specifier-seq should
12936 specify a type. */
12937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12938 {
12939 /* If there was no decl-specifier-seq, and the next token is a
12940 `;', then we have something like:
12941
12942 struct S { ; };
12943
12944 [class.mem]
12945
12946 Each member-declaration shall declare at least one member
12947 name of the class. */
62d1db17 12948 if (!decl_specifiers.any_specifiers_p)
a723baf1 12949 {
2cfe82fe
ZW
12950 cp_token *token = cp_lexer_peek_token (parser->lexer);
12951 if (pedantic && !token->in_system_header)
12952 pedwarn ("%Hextra %<;%>", &token->location);
a723baf1 12953 }
21526606 12954 else
a723baf1
MM
12955 {
12956 tree type;
21526606 12957
a723baf1 12958 /* See if this declaration is a friend. */
62d1db17 12959 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1
MM
12960 /* If there were decl-specifiers, check to see if there was
12961 a class-declaration. */
62d1db17 12962 type = check_tag_decl (&decl_specifiers);
a723baf1
MM
12963 /* Nested classes have already been added to the class, but
12964 a `friend' needs to be explicitly registered. */
12965 if (friend_p)
12966 {
12967 /* If the `friend' keyword was present, the friend must
12968 be introduced with a class-key. */
12969 if (!declares_class_or_enum)
12970 error ("a class-key must be used when declaring a friend");
12971 /* In this case:
12972
21526606
EC
12973 template <typename T> struct A {
12974 friend struct A<T>::B;
a723baf1 12975 };
21526606 12976
a723baf1
MM
12977 A<T>::B will be represented by a TYPENAME_TYPE, and
12978 therefore not recognized by check_tag_decl. */
98ca843c 12979 if (!type
62d1db17
MM
12980 && decl_specifiers.type
12981 && TYPE_P (decl_specifiers.type))
12982 type = decl_specifiers.type;
fdd09134 12983 if (!type || !TYPE_P (type))
a723baf1
MM
12984 error ("friend declaration does not name a class or "
12985 "function");
12986 else
19db77ce
KL
12987 make_friend_class (current_class_type, type,
12988 /*complain=*/true);
a723baf1
MM
12989 }
12990 /* If there is no TYPE, an error message will already have
12991 been issued. */
62d1db17 12992 else if (!type || type == error_mark_node)
a723baf1
MM
12993 ;
12994 /* An anonymous aggregate has to be handled specially; such
12995 a declaration really declares a data member (with a
12996 particular type), as opposed to a nested class. */
12997 else if (ANON_AGGR_TYPE_P (type))
12998 {
12999 /* Remove constructors and such from TYPE, now that we
34cd5ae7 13000 know it is an anonymous aggregate. */
a723baf1
MM
13001 fixup_anonymous_aggr (type);
13002 /* And make the corresponding data member. */
13003 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13004 /* Add it to the class. */
13005 finish_member_declaration (decl);
13006 }
37d407a1
KL
13007 else
13008 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
a723baf1
MM
13009 }
13010 }
13011 else
13012 {
13013 /* See if these declarations will be friends. */
62d1db17 13014 friend_p = cp_parser_friend_p (&decl_specifiers);
a723baf1 13015
21526606 13016 /* Keep going until we hit the `;' at the end of the
a723baf1
MM
13017 declaration. */
13018 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13019 {
13020 tree attributes = NULL_TREE;
13021 tree first_attribute;
13022
13023 /* Peek at the next token. */
13024 token = cp_lexer_peek_token (parser->lexer);
13025
13026 /* Check for a bitfield declaration. */
13027 if (token->type == CPP_COLON
13028 || (token->type == CPP_NAME
21526606 13029 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
a723baf1
MM
13030 == CPP_COLON))
13031 {
13032 tree identifier;
13033 tree width;
13034
13035 /* Get the name of the bitfield. Note that we cannot just
13036 check TOKEN here because it may have been invalidated by
13037 the call to cp_lexer_peek_nth_token above. */
13038 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13039 identifier = cp_parser_identifier (parser);
13040 else
13041 identifier = NULL_TREE;
13042
13043 /* Consume the `:' token. */
13044 cp_lexer_consume_token (parser->lexer);
13045 /* Get the width of the bitfield. */
21526606 13046 width
14d22dd6
MM
13047 = cp_parser_constant_expression (parser,
13048 /*allow_non_constant=*/false,
13049 NULL);
a723baf1
MM
13050
13051 /* Look for attributes that apply to the bitfield. */
13052 attributes = cp_parser_attributes_opt (parser);
13053 /* Remember which attributes are prefix attributes and
13054 which are not. */
13055 first_attribute = attributes;
13056 /* Combine the attributes. */
13057 attributes = chainon (prefix_attributes, attributes);
13058
13059 /* Create the bitfield declaration. */
98ca843c 13060 decl = grokbitfield (identifier
058b15c1
MM
13061 ? make_id_declarator (identifier)
13062 : NULL,
62d1db17 13063 &decl_specifiers,
a723baf1
MM
13064 width);
13065 /* Apply the attributes. */
13066 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13067 }
13068 else
13069 {
058b15c1 13070 cp_declarator *declarator;
a723baf1
MM
13071 tree initializer;
13072 tree asm_specification;
7efa3e22 13073 int ctor_dtor_or_conv_p;
a723baf1
MM
13074
13075 /* Parse the declarator. */
21526606 13076 declarator
62b8a44e 13077 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
4bb8ca28 13078 &ctor_dtor_or_conv_p,
db86dd14
MM
13079 /*parenthesized_p=*/NULL,
13080 /*member_p=*/true);
a723baf1
MM
13081
13082 /* If something went wrong parsing the declarator, make sure
13083 that we at least consume some tokens. */
058b15c1 13084 if (declarator == cp_error_declarator)
a723baf1
MM
13085 {
13086 /* Skip to the end of the statement. */
13087 cp_parser_skip_to_end_of_statement (parser);
4bb8ca28
MM
13088 /* If the next token is not a semicolon, that is
13089 probably because we just skipped over the body of
13090 a function. So, we consume a semicolon if
13091 present, but do not issue an error message if it
13092 is not present. */
13093 if (cp_lexer_next_token_is (parser->lexer,
13094 CPP_SEMICOLON))
13095 cp_lexer_consume_token (parser->lexer);
13096 return;
a723baf1
MM
13097 }
13098
fc6a28d7
MM
13099 if (declares_class_or_enum & 2)
13100 cp_parser_check_for_definition_in_return_type
13101 (declarator, decl_specifiers.type);
560ad596 13102
a723baf1
MM
13103 /* Look for an asm-specification. */
13104 asm_specification = cp_parser_asm_specification_opt (parser);
13105 /* Look for attributes that apply to the declaration. */
13106 attributes = cp_parser_attributes_opt (parser);
13107 /* Remember which attributes are prefix attributes and
13108 which are not. */
13109 first_attribute = attributes;
13110 /* Combine the attributes. */
13111 attributes = chainon (prefix_attributes, attributes);
13112
13113 /* If it's an `=', then we have a constant-initializer or a
13114 pure-specifier. It is not correct to parse the
13115 initializer before registering the member declaration
13116 since the member declaration should be in scope while
13117 its initializer is processed. However, the rest of the
13118 front end does not yet provide an interface that allows
13119 us to handle this correctly. */
13120 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13121 {
13122 /* In [class.mem]:
13123
13124 A pure-specifier shall be used only in the declaration of
21526606 13125 a virtual function.
a723baf1
MM
13126
13127 A member-declarator can contain a constant-initializer
13128 only if it declares a static member of integral or
21526606 13129 enumeration type.
a723baf1
MM
13130
13131 Therefore, if the DECLARATOR is for a function, we look
13132 for a pure-specifier; otherwise, we look for a
13133 constant-initializer. When we call `grokfield', it will
13134 perform more stringent semantics checks. */
058b15c1 13135 if (declarator->kind == cdk_function)
a723baf1
MM
13136 initializer = cp_parser_pure_specifier (parser);
13137 else
4bb8ca28
MM
13138 /* Parse the initializer. */
13139 initializer = cp_parser_constant_initializer (parser);
a723baf1
MM
13140 }
13141 /* Otherwise, there is no initializer. */
13142 else
13143 initializer = NULL_TREE;
13144
13145 /* See if we are probably looking at a function
5a19910e 13146 definition. We are certainly not looking at a
a723baf1
MM
13147 member-declarator. Calling `grokfield' has
13148 side-effects, so we must not do it unless we are sure
13149 that we are looking at a member-declarator. */
21526606 13150 if (cp_parser_token_starts_function_definition_p
a723baf1 13151 (cp_lexer_peek_token (parser->lexer)))
4bb8ca28
MM
13152 {
13153 /* The grammar does not allow a pure-specifier to be
13154 used when a member function is defined. (It is
13155 possible that this fact is an oversight in the
13156 standard, since a pure function may be defined
13157 outside of the class-specifier. */
13158 if (initializer)
13159 error ("pure-specifier on function-definition");
13160 decl = cp_parser_save_member_function_body (parser,
62d1db17 13161 &decl_specifiers,
4bb8ca28
MM
13162 declarator,
13163 attributes);
13164 /* If the member was not a friend, declare it here. */
13165 if (!friend_p)
13166 finish_member_declaration (decl);
13167 /* Peek at the next token. */
13168 token = cp_lexer_peek_token (parser->lexer);
13169 /* If the next token is a semicolon, consume it. */
13170 if (token->type == CPP_SEMICOLON)
13171 cp_lexer_consume_token (parser->lexer);
13172 return;
13173 }
a723baf1 13174 else
39703eb9
MM
13175 {
13176 /* Create the declaration. */
62d1db17 13177 decl = grokfield (declarator, &decl_specifiers,
ee3071ef 13178 initializer, asm_specification,
39703eb9
MM
13179 attributes);
13180 /* Any initialization must have been from a
13181 constant-expression. */
13182 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13183 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13184 }
a723baf1
MM
13185 }
13186
13187 /* Reset PREFIX_ATTRIBUTES. */
13188 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13189 attributes = TREE_CHAIN (attributes);
13190 if (attributes)
13191 TREE_CHAIN (attributes) = NULL_TREE;
13192
13193 /* If there is any qualification still in effect, clear it
13194 now; we will be starting fresh with the next declarator. */
13195 parser->scope = NULL_TREE;
13196 parser->qualifying_scope = NULL_TREE;
13197 parser->object_scope = NULL_TREE;
13198 /* If it's a `,', then there are more declarators. */
13199 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13200 cp_lexer_consume_token (parser->lexer);
13201 /* If the next token isn't a `;', then we have a parse error. */
13202 else if (cp_lexer_next_token_is_not (parser->lexer,
13203 CPP_SEMICOLON))
13204 {
2a13a625 13205 cp_parser_error (parser, "expected %<;%>");
04c06002 13206 /* Skip tokens until we find a `;'. */
a723baf1
MM
13207 cp_parser_skip_to_end_of_statement (parser);
13208
13209 break;
13210 }
13211
13212 if (decl)
13213 {
13214 /* Add DECL to the list of members. */
13215 if (!friend_p)
13216 finish_member_declaration (decl);
13217
a723baf1 13218 if (TREE_CODE (decl) == FUNCTION_DECL)
8db1028e 13219 cp_parser_save_default_args (parser, decl);
a723baf1
MM
13220 }
13221 }
13222 }
13223
4bb8ca28 13224 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
a723baf1
MM
13225}
13226
13227/* Parse a pure-specifier.
13228
13229 pure-specifier:
13230 = 0
13231
13232 Returns INTEGER_ZERO_NODE if a pure specifier is found.
cd0be382 13233 Otherwise, ERROR_MARK_NODE is returned. */
a723baf1
MM
13234
13235static tree
94edc4ab 13236cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
13237{
13238 cp_token *token;
13239
13240 /* Look for the `=' token. */
13241 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13242 return error_mark_node;
13243 /* Look for the `0' token. */
13244 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13245 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13246 to get information from the lexer about how the number was
13247 spelled in order to fix this problem. */
13248 if (!token || !integer_zerop (token->value))
13249 return error_mark_node;
13250
13251 return integer_zero_node;
13252}
13253
13254/* Parse a constant-initializer.
13255
13256 constant-initializer:
13257 = constant-expression
13258
13259 Returns a representation of the constant-expression. */
13260
13261static tree
94edc4ab 13262cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
13263{
13264 /* Look for the `=' token. */
13265 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13266 return error_mark_node;
13267
13268 /* It is invalid to write:
13269
13270 struct S { static const int i = { 7 }; };
13271
13272 */
13273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13274 {
13275 cp_parser_error (parser,
13276 "a brace-enclosed initializer is not allowed here");
13277 /* Consume the opening brace. */
13278 cp_lexer_consume_token (parser->lexer);
13279 /* Skip the initializer. */
13280 cp_parser_skip_to_closing_brace (parser);
13281 /* Look for the trailing `}'. */
13282 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
21526606 13283
a723baf1
MM
13284 return error_mark_node;
13285 }
13286
21526606 13287 return cp_parser_constant_expression (parser,
14d22dd6
MM
13288 /*allow_non_constant=*/false,
13289 NULL);
a723baf1
MM
13290}
13291
13292/* Derived classes [gram.class.derived] */
13293
13294/* Parse a base-clause.
13295
13296 base-clause:
21526606 13297 : base-specifier-list
a723baf1
MM
13298
13299 base-specifier-list:
13300 base-specifier
13301 base-specifier-list , base-specifier
13302
13303 Returns a TREE_LIST representing the base-classes, in the order in
13304 which they were declared. The representation of each node is as
21526606 13305 described by cp_parser_base_specifier.
a723baf1
MM
13306
13307 In the case that no bases are specified, this function will return
13308 NULL_TREE, not ERROR_MARK_NODE. */
13309
13310static tree
94edc4ab 13311cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
13312{
13313 tree bases = NULL_TREE;
13314
13315 /* Look for the `:' that begins the list. */
13316 cp_parser_require (parser, CPP_COLON, "`:'");
13317
13318 /* Scan the base-specifier-list. */
13319 while (true)
13320 {
13321 cp_token *token;
13322 tree base;
13323
13324 /* Look for the base-specifier. */
13325 base = cp_parser_base_specifier (parser);
13326 /* Add BASE to the front of the list. */
13327 if (base != error_mark_node)
13328 {
13329 TREE_CHAIN (base) = bases;
13330 bases = base;
13331 }
13332 /* Peek at the next token. */
13333 token = cp_lexer_peek_token (parser->lexer);
13334 /* If it's not a comma, then the list is complete. */
13335 if (token->type != CPP_COMMA)
13336 break;
13337 /* Consume the `,'. */
13338 cp_lexer_consume_token (parser->lexer);
13339 }
13340
13341 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13342 base class had a qualified name. However, the next name that
13343 appears is certainly not qualified. */
13344 parser->scope = NULL_TREE;
13345 parser->qualifying_scope = NULL_TREE;
13346 parser->object_scope = NULL_TREE;
13347
13348 return nreverse (bases);
13349}
13350
13351/* Parse a base-specifier.
13352
13353 base-specifier:
13354 :: [opt] nested-name-specifier [opt] class-name
13355 virtual access-specifier [opt] :: [opt] nested-name-specifier
13356 [opt] class-name
13357 access-specifier virtual [opt] :: [opt] nested-name-specifier
13358 [opt] class-name
13359
13360 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13361 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13362 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13363 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21526606 13364
a723baf1 13365static tree
94edc4ab 13366cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
13367{
13368 cp_token *token;
13369 bool done = false;
13370 bool virtual_p = false;
13371 bool duplicate_virtual_error_issued_p = false;
13372 bool duplicate_access_error_issued_p = false;
bbaab916 13373 bool class_scope_p, template_p;
dbbf88d1 13374 tree access = access_default_node;
a723baf1
MM
13375 tree type;
13376
13377 /* Process the optional `virtual' and `access-specifier'. */
13378 while (!done)
13379 {
13380 /* Peek at the next token. */
13381 token = cp_lexer_peek_token (parser->lexer);
13382 /* Process `virtual'. */
13383 switch (token->keyword)
13384 {
13385 case RID_VIRTUAL:
13386 /* If `virtual' appears more than once, issue an error. */
13387 if (virtual_p && !duplicate_virtual_error_issued_p)
13388 {
13389 cp_parser_error (parser,
2a13a625 13390 "%<virtual%> specified more than once in base-specified");
a723baf1
MM
13391 duplicate_virtual_error_issued_p = true;
13392 }
13393
13394 virtual_p = true;
13395
13396 /* Consume the `virtual' token. */
13397 cp_lexer_consume_token (parser->lexer);
13398
13399 break;
13400
13401 case RID_PUBLIC:
13402 case RID_PROTECTED:
13403 case RID_PRIVATE:
13404 /* If more than one access specifier appears, issue an
13405 error. */
dbbf88d1
NS
13406 if (access != access_default_node
13407 && !duplicate_access_error_issued_p)
a723baf1
MM
13408 {
13409 cp_parser_error (parser,
13410 "more than one access specifier in base-specified");
13411 duplicate_access_error_issued_p = true;
13412 }
13413
dbbf88d1 13414 access = ridpointers[(int) token->keyword];
a723baf1
MM
13415
13416 /* Consume the access-specifier. */
13417 cp_lexer_consume_token (parser->lexer);
13418
13419 break;
13420
13421 default:
13422 done = true;
13423 break;
13424 }
13425 }
852dcbdd 13426 /* It is not uncommon to see programs mechanically, erroneously, use
a3a503a5 13427 the 'typename' keyword to denote (dependent) qualified types
1ed53ef3
GB
13428 as base classes. */
13429 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13430 {
13431 if (!processing_template_decl)
2a13a625 13432 error ("keyword %<typename%> not allowed outside of templates");
1ed53ef3 13433 else
2a13a625 13434 error ("keyword %<typename%> not allowed in this context "
1ed53ef3
GB
13435 "(the base class is implicitly a type)");
13436 cp_lexer_consume_token (parser->lexer);
13437 }
a723baf1 13438
a723baf1
MM
13439 /* Look for the optional `::' operator. */
13440 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13441 /* Look for the nested-name-specifier. The simplest way to
13442 implement:
13443
13444 [temp.res]
13445
13446 The keyword `typename' is not permitted in a base-specifier or
13447 mem-initializer; in these contexts a qualified name that
13448 depends on a template-parameter is implicitly assumed to be a
13449 type name.
13450
13451 is to pretend that we have seen the `typename' keyword at this
21526606 13452 point. */
a723baf1
MM
13453 cp_parser_nested_name_specifier_opt (parser,
13454 /*typename_keyword_p=*/true,
13455 /*check_dependency_p=*/true,
fc6a28d7 13456 typename_type,
a668c6ad 13457 /*is_declaration=*/true);
a723baf1
MM
13458 /* If the base class is given by a qualified name, assume that names
13459 we see are type names or templates, as appropriate. */
13460 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916 13461 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21526606 13462
a723baf1 13463 /* Finally, look for the class-name. */
21526606 13464 type = cp_parser_class_name (parser,
a723baf1 13465 class_scope_p,
bbaab916 13466 template_p,
fc6a28d7 13467 typename_type,
a723baf1 13468 /*check_dependency_p=*/true,
a668c6ad
MM
13469 /*class_head_p=*/false,
13470 /*is_declaration=*/true);
a723baf1
MM
13471
13472 if (type == error_mark_node)
13473 return error_mark_node;
13474
dbbf88d1 13475 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
13476}
13477
13478/* Exception handling [gram.exception] */
13479
13480/* Parse an (optional) exception-specification.
13481
13482 exception-specification:
13483 throw ( type-id-list [opt] )
13484
13485 Returns a TREE_LIST representing the exception-specification. The
13486 TREE_VALUE of each node is a type. */
13487
13488static tree
94edc4ab 13489cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
13490{
13491 cp_token *token;
13492 tree type_id_list;
13493
13494 /* Peek at the next token. */
13495 token = cp_lexer_peek_token (parser->lexer);
13496 /* If it's not `throw', then there's no exception-specification. */
13497 if (!cp_parser_is_keyword (token, RID_THROW))
13498 return NULL_TREE;
13499
13500 /* Consume the `throw'. */
13501 cp_lexer_consume_token (parser->lexer);
13502
13503 /* Look for the `('. */
13504 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13505
13506 /* Peek at the next token. */
13507 token = cp_lexer_peek_token (parser->lexer);
13508 /* If it's not a `)', then there is a type-id-list. */
13509 if (token->type != CPP_CLOSE_PAREN)
13510 {
13511 const char *saved_message;
13512
13513 /* Types may not be defined in an exception-specification. */
13514 saved_message = parser->type_definition_forbidden_message;
13515 parser->type_definition_forbidden_message
13516 = "types may not be defined in an exception-specification";
13517 /* Parse the type-id-list. */
13518 type_id_list = cp_parser_type_id_list (parser);
13519 /* Restore the saved message. */
13520 parser->type_definition_forbidden_message = saved_message;
13521 }
13522 else
13523 type_id_list = empty_except_spec;
13524
13525 /* Look for the `)'. */
13526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13527
13528 return type_id_list;
13529}
13530
13531/* Parse an (optional) type-id-list.
13532
13533 type-id-list:
13534 type-id
13535 type-id-list , type-id
13536
13537 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13538 in the order that the types were presented. */
13539
13540static tree
94edc4ab 13541cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
13542{
13543 tree types = NULL_TREE;
13544
13545 while (true)
13546 {
13547 cp_token *token;
13548 tree type;
13549
13550 /* Get the next type-id. */
13551 type = cp_parser_type_id (parser);
13552 /* Add it to the list. */
13553 types = add_exception_specifier (types, type, /*complain=*/1);
13554 /* Peek at the next token. */
13555 token = cp_lexer_peek_token (parser->lexer);
13556 /* If it is not a `,', we are done. */
13557 if (token->type != CPP_COMMA)
13558 break;
13559 /* Consume the `,'. */
13560 cp_lexer_consume_token (parser->lexer);
13561 }
13562
13563 return nreverse (types);
13564}
13565
13566/* Parse a try-block.
13567
13568 try-block:
13569 try compound-statement handler-seq */
13570
13571static tree
94edc4ab 13572cp_parser_try_block (cp_parser* parser)
a723baf1
MM
13573{
13574 tree try_block;
13575
13576 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13577 try_block = begin_try_block ();
325c3691 13578 cp_parser_compound_statement (parser, NULL, true);
a723baf1
MM
13579 finish_try_block (try_block);
13580 cp_parser_handler_seq (parser);
13581 finish_handler_sequence (try_block);
13582
13583 return try_block;
13584}
13585
13586/* Parse a function-try-block.
13587
13588 function-try-block:
13589 try ctor-initializer [opt] function-body handler-seq */
13590
13591static bool
94edc4ab 13592cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
13593{
13594 tree try_block;
13595 bool ctor_initializer_p;
13596
13597 /* Look for the `try' keyword. */
13598 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13599 return false;
13600 /* Let the rest of the front-end know where we are. */
13601 try_block = begin_function_try_block ();
13602 /* Parse the function-body. */
21526606 13603 ctor_initializer_p
a723baf1
MM
13604 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13605 /* We're done with the `try' part. */
13606 finish_function_try_block (try_block);
13607 /* Parse the handlers. */
13608 cp_parser_handler_seq (parser);
13609 /* We're done with the handlers. */
13610 finish_function_handler_sequence (try_block);
13611
13612 return ctor_initializer_p;
13613}
13614
13615/* Parse a handler-seq.
13616
13617 handler-seq:
13618 handler handler-seq [opt] */
13619
13620static void
94edc4ab 13621cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
13622{
13623 while (true)
13624 {
13625 cp_token *token;
13626
13627 /* Parse the handler. */
13628 cp_parser_handler (parser);
13629 /* Peek at the next token. */
13630 token = cp_lexer_peek_token (parser->lexer);
13631 /* If it's not `catch' then there are no more handlers. */
13632 if (!cp_parser_is_keyword (token, RID_CATCH))
13633 break;
13634 }
13635}
13636
13637/* Parse a handler.
13638
13639 handler:
13640 catch ( exception-declaration ) compound-statement */
13641
13642static void
94edc4ab 13643cp_parser_handler (cp_parser* parser)
a723baf1
MM
13644{
13645 tree handler;
13646 tree declaration;
13647
13648 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13649 handler = begin_handler ();
13650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13651 declaration = cp_parser_exception_declaration (parser);
13652 finish_handler_parms (declaration, handler);
13653 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
325c3691 13654 cp_parser_compound_statement (parser, NULL, false);
a723baf1
MM
13655 finish_handler (handler);
13656}
13657
13658/* Parse an exception-declaration.
13659
13660 exception-declaration:
13661 type-specifier-seq declarator
13662 type-specifier-seq abstract-declarator
13663 type-specifier-seq
21526606 13664 ...
a723baf1
MM
13665
13666 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13667 ellipsis variant is used. */
13668
13669static tree
94edc4ab 13670cp_parser_exception_declaration (cp_parser* parser)
a723baf1 13671{
058b15c1 13672 tree decl;
62d1db17 13673 cp_decl_specifier_seq type_specifiers;
058b15c1 13674 cp_declarator *declarator;
a723baf1
MM
13675 const char *saved_message;
13676
13677 /* If it's an ellipsis, it's easy to handle. */
13678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13679 {
13680 /* Consume the `...' token. */
13681 cp_lexer_consume_token (parser->lexer);
13682 return NULL_TREE;
13683 }
13684
13685 /* Types may not be defined in exception-declarations. */
13686 saved_message = parser->type_definition_forbidden_message;
13687 parser->type_definition_forbidden_message
13688 = "types may not be defined in exception-declarations";
13689
13690 /* Parse the type-specifier-seq. */
62d1db17 13691 cp_parser_type_specifier_seq (parser, &type_specifiers);
a723baf1
MM
13692 /* If it's a `)', then there is no declarator. */
13693 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
058b15c1 13694 declarator = NULL;
a723baf1 13695 else
62b8a44e 13696 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
4bb8ca28 13697 /*ctor_dtor_or_conv_p=*/NULL,
db86dd14
MM
13698 /*parenthesized_p=*/NULL,
13699 /*member_p=*/false);
a723baf1
MM
13700
13701 /* Restore the saved message. */
13702 parser->type_definition_forbidden_message = saved_message;
13703
62d1db17 13704 if (type_specifiers.any_specifiers_p)
058b15c1 13705 {
62d1db17 13706 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
058b15c1
MM
13707 if (decl == NULL_TREE)
13708 error ("invalid catch parameter");
13709 }
13710 else
13711 decl = NULL_TREE;
13712
13713 return decl;
a723baf1
MM
13714}
13715
21526606 13716/* Parse a throw-expression.
a723baf1
MM
13717
13718 throw-expression:
34cd5ae7 13719 throw assignment-expression [opt]
a723baf1
MM
13720
13721 Returns a THROW_EXPR representing the throw-expression. */
13722
13723static tree
94edc4ab 13724cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
13725{
13726 tree expression;
89f1a6ec 13727 cp_token* token;
a723baf1
MM
13728
13729 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
89f1a6ec
MM
13730 token = cp_lexer_peek_token (parser->lexer);
13731 /* Figure out whether or not there is an assignment-expression
13732 following the "throw" keyword. */
13733 if (token->type == CPP_COMMA
13734 || token->type == CPP_SEMICOLON
13735 || token->type == CPP_CLOSE_PAREN
13736 || token->type == CPP_CLOSE_SQUARE
13737 || token->type == CPP_CLOSE_BRACE
13738 || token->type == CPP_COLON)
a723baf1 13739 expression = NULL_TREE;
89f1a6ec
MM
13740 else
13741 expression = cp_parser_assignment_expression (parser);
a723baf1
MM
13742
13743 return build_throw (expression);
13744}
13745
13746/* GNU Extensions */
13747
13748/* Parse an (optional) asm-specification.
13749
13750 asm-specification:
13751 asm ( string-literal )
13752
13753 If the asm-specification is present, returns a STRING_CST
13754 corresponding to the string-literal. Otherwise, returns
13755 NULL_TREE. */
13756
13757static tree
94edc4ab 13758cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
13759{
13760 cp_token *token;
13761 tree asm_specification;
13762
13763 /* Peek at the next token. */
13764 token = cp_lexer_peek_token (parser->lexer);
21526606 13765 /* If the next token isn't the `asm' keyword, then there's no
a723baf1
MM
13766 asm-specification. */
13767 if (!cp_parser_is_keyword (token, RID_ASM))
13768 return NULL_TREE;
13769
13770 /* Consume the `asm' token. */
13771 cp_lexer_consume_token (parser->lexer);
13772 /* Look for the `('. */
13773 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13774
13775 /* Look for the string-literal. */
c162c75e 13776 asm_specification = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13777
13778 /* Look for the `)'. */
13779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13780
13781 return asm_specification;
13782}
13783
21526606 13784/* Parse an asm-operand-list.
a723baf1
MM
13785
13786 asm-operand-list:
13787 asm-operand
13788 asm-operand-list , asm-operand
21526606 13789
a723baf1 13790 asm-operand:
21526606 13791 string-literal ( expression )
a723baf1
MM
13792 [ string-literal ] string-literal ( expression )
13793
13794 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13795 each node is the expression. The TREE_PURPOSE is itself a
13796 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13797 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13798 is a STRING_CST for the string literal before the parenthesis. */
13799
13800static tree
94edc4ab 13801cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
13802{
13803 tree asm_operands = NULL_TREE;
13804
13805 while (true)
13806 {
13807 tree string_literal;
13808 tree expression;
13809 tree name;
21526606 13810
21526606 13811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
a723baf1
MM
13812 {
13813 /* Consume the `[' token. */
13814 cp_lexer_consume_token (parser->lexer);
13815 /* Read the operand name. */
13816 name = cp_parser_identifier (parser);
21526606 13817 if (name != error_mark_node)
a723baf1
MM
13818 name = build_string (IDENTIFIER_LENGTH (name),
13819 IDENTIFIER_POINTER (name));
13820 /* Look for the closing `]'. */
13821 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13822 }
13823 else
13824 name = NULL_TREE;
13825 /* Look for the string-literal. */
c162c75e
MA
13826 string_literal = cp_parser_string_literal (parser, false, false);
13827
a723baf1
MM
13828 /* Look for the `('. */
13829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13830 /* Parse the expression. */
13831 expression = cp_parser_expression (parser);
13832 /* Look for the `)'. */
13833 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
c162c75e 13834
a723baf1
MM
13835 /* Add this operand to the list. */
13836 asm_operands = tree_cons (build_tree_list (name, string_literal),
21526606 13837 expression,
a723baf1 13838 asm_operands);
21526606 13839 /* If the next token is not a `,', there are no more
a723baf1
MM
13840 operands. */
13841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13842 break;
13843 /* Consume the `,'. */
13844 cp_lexer_consume_token (parser->lexer);
13845 }
13846
13847 return nreverse (asm_operands);
13848}
13849
21526606 13850/* Parse an asm-clobber-list.
a723baf1
MM
13851
13852 asm-clobber-list:
13853 string-literal
21526606 13854 asm-clobber-list , string-literal
a723baf1
MM
13855
13856 Returns a TREE_LIST, indicating the clobbers in the order that they
13857 appeared. The TREE_VALUE of each node is a STRING_CST. */
13858
13859static tree
94edc4ab 13860cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
13861{
13862 tree clobbers = NULL_TREE;
13863
13864 while (true)
13865 {
a723baf1
MM
13866 tree string_literal;
13867
13868 /* Look for the string literal. */
c162c75e 13869 string_literal = cp_parser_string_literal (parser, false, false);
a723baf1
MM
13870 /* Add it to the list. */
13871 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21526606 13872 /* If the next token is not a `,', then the list is
a723baf1
MM
13873 complete. */
13874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13875 break;
13876 /* Consume the `,' token. */
13877 cp_lexer_consume_token (parser->lexer);
13878 }
13879
13880 return clobbers;
13881}
13882
13883/* Parse an (optional) series of attributes.
13884
13885 attributes:
13886 attributes attribute
13887
13888 attribute:
21526606 13889 __attribute__ (( attribute-list [opt] ))
a723baf1
MM
13890
13891 The return value is as for cp_parser_attribute_list. */
21526606 13892
a723baf1 13893static tree
94edc4ab 13894cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
13895{
13896 tree attributes = NULL_TREE;
13897
13898 while (true)
13899 {
13900 cp_token *token;
13901 tree attribute_list;
13902
13903 /* Peek at the next token. */
13904 token = cp_lexer_peek_token (parser->lexer);
13905 /* If it's not `__attribute__', then we're done. */
13906 if (token->keyword != RID_ATTRIBUTE)
13907 break;
13908
13909 /* Consume the `__attribute__' keyword. */
13910 cp_lexer_consume_token (parser->lexer);
13911 /* Look for the two `(' tokens. */
13912 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13913 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13914
13915 /* Peek at the next token. */
13916 token = cp_lexer_peek_token (parser->lexer);
13917 if (token->type != CPP_CLOSE_PAREN)
13918 /* Parse the attribute-list. */
13919 attribute_list = cp_parser_attribute_list (parser);
13920 else
13921 /* If the next token is a `)', then there is no attribute
13922 list. */
13923 attribute_list = NULL;
13924
13925 /* Look for the two `)' tokens. */
13926 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13927 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13928
13929 /* Add these new attributes to the list. */
13930 attributes = chainon (attributes, attribute_list);
13931 }
13932
13933 return attributes;
13934}
13935
21526606 13936/* Parse an attribute-list.
a723baf1 13937
21526606
EC
13938 attribute-list:
13939 attribute
a723baf1
MM
13940 attribute-list , attribute
13941
13942 attribute:
21526606 13943 identifier
a723baf1
MM
13944 identifier ( identifier )
13945 identifier ( identifier , expression-list )
21526606 13946 identifier ( expression-list )
a723baf1
MM
13947
13948 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13949 TREE_PURPOSE of each node is the identifier indicating which
13950 attribute is in use. The TREE_VALUE represents the arguments, if
13951 any. */
13952
13953static tree
94edc4ab 13954cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
13955{
13956 tree attribute_list = NULL_TREE;
c162c75e 13957 bool save_translate_strings_p = parser->translate_strings_p;
a723baf1 13958
c162c75e 13959 parser->translate_strings_p = false;
a723baf1
MM
13960 while (true)
13961 {
13962 cp_token *token;
13963 tree identifier;
13964 tree attribute;
13965
13966 /* Look for the identifier. We also allow keywords here; for
13967 example `__attribute__ ((const))' is legal. */
13968 token = cp_lexer_peek_token (parser->lexer);
21526606 13969 if (token->type != CPP_NAME
a723baf1
MM
13970 && token->type != CPP_KEYWORD)
13971 return error_mark_node;
13972 /* Consume the token. */
13973 token = cp_lexer_consume_token (parser->lexer);
21526606 13974
a723baf1
MM
13975 /* Save away the identifier that indicates which attribute this is. */
13976 identifier = token->value;
13977 attribute = build_tree_list (identifier, NULL_TREE);
13978
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981 /* If it's an `(', then parse the attribute arguments. */
13982 if (token->type == CPP_OPEN_PAREN)
13983 {
13984 tree arguments;
a723baf1 13985
21526606 13986 arguments = (cp_parser_parenthesized_expression_list
39703eb9 13987 (parser, true, /*non_constant_p=*/NULL));
a723baf1
MM
13988 /* Save the identifier and arguments away. */
13989 TREE_VALUE (attribute) = arguments;
a723baf1
MM
13990 }
13991
13992 /* Add this attribute to the list. */
13993 TREE_CHAIN (attribute) = attribute_list;
13994 attribute_list = attribute;
13995
13996 /* Now, look for more attributes. */
13997 token = cp_lexer_peek_token (parser->lexer);
13998 /* If the next token isn't a `,', we're done. */
13999 if (token->type != CPP_COMMA)
14000 break;
14001
cd0be382 14002 /* Consume the comma and keep going. */
a723baf1
MM
14003 cp_lexer_consume_token (parser->lexer);
14004 }
c162c75e 14005 parser->translate_strings_p = save_translate_strings_p;
a723baf1
MM
14006
14007 /* We built up the list in reverse order. */
14008 return nreverse (attribute_list);
14009}
14010
14011/* Parse an optional `__extension__' keyword. Returns TRUE if it is
14012 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14013 current value of the PEDANTIC flag, regardless of whether or not
14014 the `__extension__' keyword is present. The caller is responsible
14015 for restoring the value of the PEDANTIC flag. */
14016
14017static bool
94edc4ab 14018cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
14019{
14020 /* Save the old value of the PEDANTIC flag. */
14021 *saved_pedantic = pedantic;
14022
14023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14024 {
14025 /* Consume the `__extension__' token. */
14026 cp_lexer_consume_token (parser->lexer);
14027 /* We're not being pedantic while the `__extension__' keyword is
14028 in effect. */
14029 pedantic = 0;
14030
14031 return true;
14032 }
14033
14034 return false;
14035}
14036
14037/* Parse a label declaration.
14038
14039 label-declaration:
14040 __label__ label-declarator-seq ;
14041
14042 label-declarator-seq:
14043 identifier , label-declarator-seq
14044 identifier */
14045
14046static void
94edc4ab 14047cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
14048{
14049 /* Look for the `__label__' keyword. */
14050 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14051
14052 while (true)
14053 {
14054 tree identifier;
14055
14056 /* Look for an identifier. */
14057 identifier = cp_parser_identifier (parser);
14058 /* Declare it as a lobel. */
14059 finish_label_decl (identifier);
14060 /* If the next token is a `;', stop. */
14061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14062 break;
14063 /* Look for the `,' separating the label declarations. */
14064 cp_parser_require (parser, CPP_COMMA, "`,'");
14065 }
14066
14067 /* Look for the final `;'. */
14068 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14069}
14070
14071/* Support Functions */
14072
14073/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14074 NAME should have one of the representations used for an
14075 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14076 is returned. If PARSER->SCOPE is a dependent type, then a
14077 SCOPE_REF is returned.
14078
14079 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14080 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14081 was formed. Abstractly, such entities should not be passed to this
14082 function, because they do not need to be looked up, but it is
14083 simpler to check for this special case here, rather than at the
14084 call-sites.
14085
14086 In cases not explicitly covered above, this function returns a
14087 DECL, OVERLOAD, or baselink representing the result of the lookup.
14088 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14089 is returned.
14090
472c29c3 14091 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
fc6a28d7
MM
14092 (e.g., "struct") that was used. In that case bindings that do not
14093 refer to types are ignored.
a723baf1 14094
b0bc6e8e
KL
14095 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14096 ignored.
14097
eea9800f
MM
14098 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14099 are ignored.
14100
a723baf1 14101 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
8f78f01f
MM
14102 types.
14103
14104 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14105 results in an ambiguity, and false otherwise. */
a723baf1
MM
14106
14107static tree
21526606 14108cp_parser_lookup_name (cp_parser *parser, tree name,
fc6a28d7
MM
14109 enum tag_types tag_type,
14110 bool is_template, bool is_namespace,
8f78f01f
MM
14111 bool check_dependency,
14112 bool *ambiguous_p)
a723baf1
MM
14113{
14114 tree decl;
14115 tree object_type = parser->context->object_type;
14116
8f78f01f
MM
14117 /* Assume that the lookup will be unambiguous. */
14118 if (ambiguous_p)
14119 *ambiguous_p = false;
14120
a723baf1
MM
14121 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14122 no longer valid. Note that if we are parsing tentatively, and
14123 the parse fails, OBJECT_TYPE will be automatically restored. */
14124 parser->context->object_type = NULL_TREE;
14125
14126 if (name == error_mark_node)
14127 return error_mark_node;
14128
14129 /* A template-id has already been resolved; there is no lookup to
14130 do. */
14131 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14132 return name;
14133 if (BASELINK_P (name))
14134 {
50bc768d
NS
14135 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14136 == TEMPLATE_ID_EXPR);
a723baf1
MM
14137 return name;
14138 }
14139
14140 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14141 it should already have been checked to make sure that the name
14142 used matches the type being destroyed. */
14143 if (TREE_CODE (name) == BIT_NOT_EXPR)
14144 {
14145 tree type;
14146
14147 /* Figure out to which type this destructor applies. */
14148 if (parser->scope)
14149 type = parser->scope;
14150 else if (object_type)
14151 type = object_type;
14152 else
14153 type = current_class_type;
14154 /* If that's not a class type, there is no destructor. */
14155 if (!type || !CLASS_TYPE_P (type))
14156 return error_mark_node;
fd6e3cce
GB
14157 if (!CLASSTYPE_DESTRUCTORS (type))
14158 return error_mark_node;
a723baf1
MM
14159 /* If it was a class type, return the destructor. */
14160 return CLASSTYPE_DESTRUCTORS (type);
14161 }
14162
14163 /* By this point, the NAME should be an ordinary identifier. If
14164 the id-expression was a qualified name, the qualifying scope is
14165 stored in PARSER->SCOPE at this point. */
50bc768d 14166 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21526606 14167
a723baf1
MM
14168 /* Perform the lookup. */
14169 if (parser->scope)
21526606 14170 {
1fb3244a 14171 bool dependent_p;
a723baf1
MM
14172
14173 if (parser->scope == error_mark_node)
14174 return error_mark_node;
14175
14176 /* If the SCOPE is dependent, the lookup must be deferred until
14177 the template is instantiated -- unless we are explicitly
14178 looking up names in uninstantiated templates. Even then, we
14179 cannot look up the name if the scope is not a class type; it
14180 might, for example, be a template type parameter. */
1fb3244a
MM
14181 dependent_p = (TYPE_P (parser->scope)
14182 && !(parser->in_declarator_p
14183 && currently_open_class (parser->scope))
14184 && dependent_type_p (parser->scope));
a723baf1 14185 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 14186 && dependent_p)
a723baf1 14187 {
fc6a28d7
MM
14188 if (tag_type)
14189 {
14190 tree type;
14191
14192 /* The resolution to Core Issue 180 says that `struct
14193 A::B' should be considered a type-name, even if `A'
14194 is dependent. */
14195 type = make_typename_type (parser->scope, name, tag_type,
14196 /*complain=*/1);
fc6a28d7
MM
14197 decl = TYPE_NAME (type);
14198 }
b0bc6e8e 14199 else if (is_template)
5b4acce1 14200 decl = make_unbound_class_template (parser->scope,
b939a023 14201 name, NULL_TREE,
5b4acce1 14202 /*complain=*/1);
b0bc6e8e
KL
14203 else
14204 decl = build_nt (SCOPE_REF, parser->scope, name);
a723baf1
MM
14205 }
14206 else
14207 {
91b004e5
MM
14208 bool pop_p = false;
14209
a723baf1
MM
14210 /* If PARSER->SCOPE is a dependent type, then it must be a
14211 class type, and we must not be checking dependencies;
14212 otherwise, we would have processed this lookup above. So
14213 that PARSER->SCOPE is not considered a dependent base by
14214 lookup_member, we must enter the scope here. */
1fb3244a 14215 if (dependent_p)
91b004e5 14216 pop_p = push_scope (parser->scope);
a723baf1
MM
14217 /* If the PARSER->SCOPE is a a template specialization, it
14218 may be instantiated during name lookup. In that case,
14219 errors may be issued. Even if we rollback the current
14220 tentative parse, those errors are valid. */
fc6a28d7
MM
14221 decl = lookup_qualified_name (parser->scope, name,
14222 tag_type != none_type,
5e08432e 14223 /*complain=*/true);
91b004e5 14224 if (pop_p)
a723baf1
MM
14225 pop_scope (parser->scope);
14226 }
14227 parser->qualifying_scope = parser->scope;
14228 parser->object_scope = NULL_TREE;
14229 }
14230 else if (object_type)
14231 {
14232 tree object_decl = NULL_TREE;
14233 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14234 OBJECT_TYPE is not a class. */
14235 if (CLASS_TYPE_P (object_type))
14236 /* If the OBJECT_TYPE is a template specialization, it may
14237 be instantiated during name lookup. In that case, errors
14238 may be issued. Even if we rollback the current tentative
14239 parse, those errors are valid. */
14240 object_decl = lookup_member (object_type,
14241 name,
fc6a28d7
MM
14242 /*protect=*/0,
14243 tag_type != none_type);
a723baf1 14244 /* Look it up in the enclosing context, too. */
fc6a28d7
MM
14245 decl = lookup_name_real (name, tag_type != none_type,
14246 /*nonclass=*/0,
12cf89fa 14247 /*block_p=*/true, is_namespace,
a723baf1
MM
14248 /*flags=*/0);
14249 parser->object_scope = object_type;
14250 parser->qualifying_scope = NULL_TREE;
14251 if (object_decl)
14252 decl = object_decl;
14253 }
14254 else
14255 {
fc6a28d7
MM
14256 decl = lookup_name_real (name, tag_type != none_type,
14257 /*nonclass=*/0,
12cf89fa 14258 /*block_p=*/true, is_namespace,
a723baf1
MM
14259 /*flags=*/0);
14260 parser->qualifying_scope = NULL_TREE;
14261 parser->object_scope = NULL_TREE;
14262 }
14263
14264 /* If the lookup failed, let our caller know. */
21526606 14265 if (!decl
a723baf1 14266 || decl == error_mark_node
21526606 14267 || (TREE_CODE (decl) == FUNCTION_DECL
a723baf1
MM
14268 && DECL_ANTICIPATED (decl)))
14269 return error_mark_node;
14270
14271 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14272 if (TREE_CODE (decl) == TREE_LIST)
14273 {
8f78f01f
MM
14274 if (ambiguous_p)
14275 *ambiguous_p = true;
a723baf1
MM
14276 /* The error message we have to print is too complicated for
14277 cp_parser_error, so we incorporate its actions directly. */
e5976695 14278 if (!cp_parser_simulate_error (parser))
a723baf1 14279 {
2a13a625 14280 error ("reference to %qD is ambiguous", name);
a723baf1
MM
14281 print_candidates (decl);
14282 }
14283 return error_mark_node;
14284 }
14285
50bc768d
NS
14286 gcc_assert (DECL_P (decl)
14287 || TREE_CODE (decl) == OVERLOAD
14288 || TREE_CODE (decl) == SCOPE_REF
14289 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14290 || BASELINK_P (decl));
a723baf1
MM
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,
fc6a28d7 14313 none_type,
b0bc6e8e 14314 /*is_template=*/false,
eea9800f 14315 /*is_namespace=*/false,
8f78f01f
MM
14316 /*check_dependency=*/true,
14317 /*ambiguous_p=*/NULL);
a723baf1
MM
14318}
14319
a723baf1
MM
14320/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14321 the current context, return the TYPE_DECL. If TAG_NAME_P is
14322 true, the DECL indicates the class being defined in a class-head,
14323 or declared in an elaborated-type-specifier.
14324
14325 Otherwise, return DECL. */
14326
14327static tree
14328cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14329{
710b73e6
KL
14330 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14331 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1 14332
21526606 14333 struct A {
a723baf1
MM
14334 template <typename T> struct B;
14335 };
14336
21526606
EC
14337 template <typename T> struct A::B {};
14338
a723baf1
MM
14339 Similarly, in a elaborated-type-specifier:
14340
14341 namespace N { struct X{}; }
14342
14343 struct A {
14344 template <typename T> friend struct N::X;
14345 };
14346
710b73e6
KL
14347 However, if the DECL refers to a class type, and we are in
14348 the scope of the class, then the name lookup automatically
14349 finds the TYPE_DECL created by build_self_reference rather
14350 than a TEMPLATE_DECL. For example, in:
14351
14352 template <class T> struct S {
14353 S s;
14354 };
14355
14356 there is no need to handle such case. */
14357
14358 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
14359 return DECL_TEMPLATE_RESULT (decl);
14360
14361 return decl;
14362}
14363
14364/* If too many, or too few, template-parameter lists apply to the
14365 declarator, issue an error message. Returns TRUE if all went well,
14366 and FALSE otherwise. */
14367
14368static bool
21526606 14369cp_parser_check_declarator_template_parameters (cp_parser* parser,
058b15c1 14370 cp_declarator *declarator)
a723baf1
MM
14371{
14372 unsigned num_templates;
14373
14374 /* We haven't seen any classes that involve template parameters yet. */
14375 num_templates = 0;
14376
058b15c1 14377 switch (declarator->kind)
a723baf1 14378 {
058b15c1
MM
14379 case cdk_id:
14380 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14381 {
14382 tree scope;
14383 tree member;
a723baf1 14384
058b15c1
MM
14385 scope = TREE_OPERAND (declarator->u.id.name, 0);
14386 member = TREE_OPERAND (declarator->u.id.name, 1);
a723baf1 14387
058b15c1
MM
14388 while (scope && CLASS_TYPE_P (scope))
14389 {
14390 /* You're supposed to have one `template <...>'
14391 for every template class, but you don't need one
14392 for a full specialization. For example:
14393
14394 template <class T> struct S{};
14395 template <> struct S<int> { void f(); };
14396 void S<int>::f () {}
14397
14398 is correct; there shouldn't be a `template <>' for
14399 the definition of `S<int>::f'. */
14400 if (CLASSTYPE_TEMPLATE_INFO (scope)
14401 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14402 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14404 ++num_templates;
14405
14406 scope = TYPE_CONTEXT (scope);
14407 }
14408 }
a723baf1 14409
a723baf1
MM
14410 /* If the DECLARATOR has the form `X<y>' then it uses one
14411 additional level of template parameters. */
058b15c1 14412 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
a723baf1
MM
14413 ++num_templates;
14414
21526606 14415 return cp_parser_check_template_parameters (parser,
a723baf1 14416 num_templates);
058b15c1
MM
14417
14418 case cdk_function:
14419 case cdk_array:
14420 case cdk_pointer:
14421 case cdk_reference:
14422 case cdk_ptrmem:
98ca843c 14423 return (cp_parser_check_declarator_template_parameters
058b15c1
MM
14424 (parser, declarator->declarator));
14425
14426 case cdk_error:
14427 return true;
14428
14429 default:
315fb5db 14430 gcc_unreachable ();
a723baf1 14431 }
315fb5db 14432 return false;
a723baf1
MM
14433}
14434
14435/* NUM_TEMPLATES were used in the current declaration. If that is
14436 invalid, return FALSE and issue an error messages. Otherwise,
14437 return TRUE. */
14438
14439static bool
94edc4ab
NN
14440cp_parser_check_template_parameters (cp_parser* parser,
14441 unsigned num_templates)
a723baf1
MM
14442{
14443 /* If there are more template classes than parameter lists, we have
14444 something like:
21526606 14445
a723baf1
MM
14446 template <class T> void S<T>::R<T>::f (); */
14447 if (parser->num_template_parameter_lists < num_templates)
14448 {
14449 error ("too few template-parameter-lists");
14450 return false;
14451 }
14452 /* If there are the same number of template classes and parameter
14453 lists, that's OK. */
14454 if (parser->num_template_parameter_lists == num_templates)
14455 return true;
14456 /* If there are more, but only one more, then we are referring to a
14457 member template. That's OK too. */
14458 if (parser->num_template_parameter_lists == num_templates + 1)
14459 return true;
14460 /* Otherwise, there are too many template parameter lists. We have
14461 something like:
14462
14463 template <class T> template <class U> void S::f(); */
14464 error ("too many template-parameter-lists");
14465 return false;
14466}
14467
a723baf1
MM
14468/* Parse an optional `::' token indicating that the following name is
14469 from the global namespace. If so, PARSER->SCOPE is set to the
14470 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14471 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14472 Returns the new value of PARSER->SCOPE, if the `::' token is
14473 present, and NULL_TREE otherwise. */
14474
14475static tree
94edc4ab 14476cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
14477{
14478 cp_token *token;
14479
14480 /* Peek at the next token. */
14481 token = cp_lexer_peek_token (parser->lexer);
14482 /* If we're looking at a `::' token then we're starting from the
14483 global namespace, not our current location. */
14484 if (token->type == CPP_SCOPE)
14485 {
14486 /* Consume the `::' token. */
14487 cp_lexer_consume_token (parser->lexer);
14488 /* Set the SCOPE so that we know where to start the lookup. */
14489 parser->scope = global_namespace;
14490 parser->qualifying_scope = global_namespace;
14491 parser->object_scope = NULL_TREE;
14492
14493 return parser->scope;
14494 }
14495 else if (!current_scope_valid_p)
14496 {
14497 parser->scope = NULL_TREE;
14498 parser->qualifying_scope = NULL_TREE;
14499 parser->object_scope = NULL_TREE;
14500 }
14501
14502 return NULL_TREE;
14503}
14504
14505/* Returns TRUE if the upcoming token sequence is the start of a
14506 constructor declarator. If FRIEND_P is true, the declarator is
14507 preceded by the `friend' specifier. */
14508
14509static bool
14510cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14511{
14512 bool constructor_p;
14513 tree type_decl = NULL_TREE;
14514 bool nested_name_p;
2050a1bb
MM
14515 cp_token *next_token;
14516
14517 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
14518 try to avoid doing lots of work if at all possible. It's not
14519 valid declare a constructor at function scope. */
14520 if (at_function_scope_p ())
14521 return false;
14522 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
14523 next_token = cp_lexer_peek_token (parser->lexer);
14524 if (next_token->type != CPP_NAME
14525 && next_token->type != CPP_SCOPE
14526 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14527 && next_token->type != CPP_TEMPLATE_ID)
14528 return false;
a723baf1
MM
14529
14530 /* Parse tentatively; we are going to roll back all of the tokens
14531 consumed here. */
14532 cp_parser_parse_tentatively (parser);
14533 /* Assume that we are looking at a constructor declarator. */
14534 constructor_p = true;
8d241e0b 14535
a723baf1
MM
14536 /* Look for the optional `::' operator. */
14537 cp_parser_global_scope_opt (parser,
14538 /*current_scope_valid_p=*/false);
14539 /* Look for the nested-name-specifier. */
21526606 14540 nested_name_p
a723baf1
MM
14541 = (cp_parser_nested_name_specifier_opt (parser,
14542 /*typename_keyword_p=*/false,
14543 /*check_dependency_p=*/false,
a668c6ad
MM
14544 /*type_p=*/false,
14545 /*is_declaration=*/false)
a723baf1
MM
14546 != NULL_TREE);
14547 /* Outside of a class-specifier, there must be a
14548 nested-name-specifier. */
21526606 14549 if (!nested_name_p &&
a723baf1
MM
14550 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14551 || friend_p))
14552 constructor_p = false;
14553 /* If we still think that this might be a constructor-declarator,
14554 look for a class-name. */
14555 if (constructor_p)
14556 {
14557 /* If we have:
14558
8fbc5ae7 14559 template <typename T> struct S { S(); };
a723baf1
MM
14560 template <typename T> S<T>::S ();
14561
14562 we must recognize that the nested `S' names a class.
14563 Similarly, for:
14564
14565 template <typename T> S<T>::S<T> ();
14566
14567 we must recognize that the nested `S' names a template. */
14568 type_decl = cp_parser_class_name (parser,
14569 /*typename_keyword_p=*/false,
14570 /*template_keyword_p=*/false,
fc6a28d7 14571 none_type,
a723baf1 14572 /*check_dependency_p=*/false,
a668c6ad
MM
14573 /*class_head_p=*/false,
14574 /*is_declaration=*/false);
a723baf1
MM
14575 /* If there was no class-name, then this is not a constructor. */
14576 constructor_p = !cp_parser_error_occurred (parser);
14577 }
8d241e0b 14578
a723baf1
MM
14579 /* If we're still considering a constructor, we have to see a `(',
14580 to begin the parameter-declaration-clause, followed by either a
14581 `)', an `...', or a decl-specifier. We need to check for a
14582 type-specifier to avoid being fooled into thinking that:
14583
14584 S::S (f) (int);
14585
14586 is a constructor. (It is actually a function named `f' that
14587 takes one parameter (of type `int') and returns a value of type
14588 `S::S'. */
21526606 14589 if (constructor_p
a723baf1
MM
14590 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14591 {
14592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14593 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15077df5
MM
14594 /* A parameter declaration begins with a decl-specifier,
14595 which is either the "attribute" keyword, a storage class
14596 specifier, or (usually) a type-specifier. */
14597 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
a723baf1
MM
14598 && !cp_parser_storage_class_specifier_opt (parser))
14599 {
5dae1114 14600 tree type;
91b004e5 14601 bool pop_p = false;
4047b164 14602 unsigned saved_num_template_parameter_lists;
5dae1114
MM
14603
14604 /* Names appearing in the type-specifier should be looked up
14605 in the scope of the class. */
14606 if (current_class_type)
14607 type = NULL_TREE;
a723baf1
MM
14608 else
14609 {
5dae1114
MM
14610 type = TREE_TYPE (type_decl);
14611 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6 14612 {
21526606 14613 type = resolve_typename_type (type,
14d22dd6
MM
14614 /*only_current_p=*/false);
14615 if (type == error_mark_node)
14616 {
14617 cp_parser_abort_tentative_parse (parser);
14618 return false;
14619 }
14620 }
91b004e5 14621 pop_p = push_scope (type);
a723baf1 14622 }
4047b164
KL
14623
14624 /* Inside the constructor parameter list, surrounding
14625 template-parameter-lists do not apply. */
14626 saved_num_template_parameter_lists
14627 = parser->num_template_parameter_lists;
14628 parser->num_template_parameter_lists = 0;
14629
5dae1114
MM
14630 /* Look for the type-specifier. */
14631 cp_parser_type_specifier (parser,
14632 CP_PARSER_FLAGS_NONE,
62d1db17 14633 /*decl_specs=*/NULL,
5dae1114
MM
14634 /*is_declarator=*/true,
14635 /*declares_class_or_enum=*/NULL,
14636 /*is_cv_qualifier=*/NULL);
4047b164
KL
14637
14638 parser->num_template_parameter_lists
14639 = saved_num_template_parameter_lists;
14640
5dae1114 14641 /* Leave the scope of the class. */
91b004e5 14642 if (pop_p)
5dae1114
MM
14643 pop_scope (type);
14644
14645 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
14646 }
14647 }
14648 else
14649 constructor_p = false;
14650 /* We did not really want to consume any tokens. */
14651 cp_parser_abort_tentative_parse (parser);
14652
14653 return constructor_p;
14654}
14655
14656/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 14657 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
14658 they must be performed once we are in the scope of the function.
14659
14660 Returns the function defined. */
14661
14662static tree
14663cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 14664 (cp_parser* parser,
62d1db17 14665 cp_decl_specifier_seq *decl_specifiers,
94edc4ab 14666 tree attributes,
058b15c1 14667 const cp_declarator *declarator)
a723baf1
MM
14668{
14669 tree fn;
14670 bool success_p;
14671
14672 /* Begin the function-definition. */
058b15c1
MM
14673 success_p = start_function (decl_specifiers, declarator, attributes);
14674
14675 /* The things we're about to see are not directly qualified by any
14676 template headers we've seen thus far. */
14677 reset_specialization ();
a723baf1
MM
14678
14679 /* If there were names looked up in the decl-specifier-seq that we
14680 did not check, check them now. We must wait until we are in the
14681 scope of the function to perform the checks, since the function
14682 might be a friend. */
cf22909c 14683 perform_deferred_access_checks ();
a723baf1
MM
14684
14685 if (!success_p)
14686 {
058b15c1 14687 /* Skip the entire function. */
a723baf1
MM
14688 error ("invalid function declaration");
14689 cp_parser_skip_to_end_of_block_or_statement (parser);
14690 fn = error_mark_node;
14691 }
14692 else
14693 fn = cp_parser_function_definition_after_declarator (parser,
14694 /*inline_p=*/false);
14695
14696 return fn;
14697}
14698
14699/* Parse the part of a function-definition that follows the
14700 declarator. INLINE_P is TRUE iff this function is an inline
14701 function defined with a class-specifier.
14702
14703 Returns the function defined. */
14704
21526606
EC
14705static tree
14706cp_parser_function_definition_after_declarator (cp_parser* parser,
94edc4ab 14707 bool inline_p)
a723baf1
MM
14708{
14709 tree fn;
14710 bool ctor_initializer_p = false;
14711 bool saved_in_unbraced_linkage_specification_p;
14712 unsigned saved_num_template_parameter_lists;
14713
14714 /* If the next token is `return', then the code may be trying to
14715 make use of the "named return value" extension that G++ used to
14716 support. */
14717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14718 {
14719 /* Consume the `return' keyword. */
14720 cp_lexer_consume_token (parser->lexer);
14721 /* Look for the identifier that indicates what value is to be
14722 returned. */
14723 cp_parser_identifier (parser);
14724 /* Issue an error message. */
14725 error ("named return values are no longer supported");
14726 /* Skip tokens until we reach the start of the function body. */
21eb631b
MM
14727 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14728 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
a723baf1
MM
14729 cp_lexer_consume_token (parser->lexer);
14730 }
14731 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14732 anything declared inside `f'. */
21526606 14733 saved_in_unbraced_linkage_specification_p
a723baf1
MM
14734 = parser->in_unbraced_linkage_specification_p;
14735 parser->in_unbraced_linkage_specification_p = false;
14736 /* Inside the function, surrounding template-parameter-lists do not
14737 apply. */
21526606
EC
14738 saved_num_template_parameter_lists
14739 = parser->num_template_parameter_lists;
a723baf1
MM
14740 parser->num_template_parameter_lists = 0;
14741 /* If the next token is `try', then we are looking at a
14742 function-try-block. */
14743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14744 ctor_initializer_p = cp_parser_function_try_block (parser);
14745 /* A function-try-block includes the function-body, so we only do
14746 this next part if we're not processing a function-try-block. */
14747 else
21526606 14748 ctor_initializer_p
a723baf1
MM
14749 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14750
14751 /* Finish the function. */
21526606 14752 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
a723baf1
MM
14753 (inline_p ? 2 : 0));
14754 /* Generate code for it, if necessary. */
8cd2462c 14755 expand_or_defer_fn (fn);
a723baf1 14756 /* Restore the saved values. */
21526606 14757 parser->in_unbraced_linkage_specification_p
a723baf1 14758 = saved_in_unbraced_linkage_specification_p;
21526606 14759 parser->num_template_parameter_lists
a723baf1
MM
14760 = saved_num_template_parameter_lists;
14761
14762 return fn;
14763}
14764
14765/* Parse a template-declaration, assuming that the `export' (and
14766 `extern') keywords, if present, has already been scanned. MEMBER_P
14767 is as for cp_parser_template_declaration. */
14768
14769static void
94edc4ab 14770cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
14771{
14772 tree decl = NULL_TREE;
14773 tree parameter_list;
14774 bool friend_p = false;
14775
14776 /* Look for the `template' keyword. */
14777 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14778 return;
21526606 14779
a723baf1
MM
14780 /* And the `<'. */
14781 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14782 return;
21526606 14783
a723baf1
MM
14784 /* If the next token is `>', then we have an invalid
14785 specialization. Rather than complain about an invalid template
14786 parameter, issue an error message here. */
14787 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14788 {
14789 cp_parser_error (parser, "invalid explicit specialization");
2f9afd51 14790 begin_specialization ();
a723baf1
MM
14791 parameter_list = NULL_TREE;
14792 }
14793 else
2f9afd51
KL
14794 {
14795 /* Parse the template parameters. */
14796 begin_template_parm_list ();
14797 parameter_list = cp_parser_template_parameter_list (parser);
14798 parameter_list = end_template_parm_list (parameter_list);
14799 }
14800
a723baf1
MM
14801 /* Look for the `>'. */
14802 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14803 /* We just processed one more parameter list. */
14804 ++parser->num_template_parameter_lists;
14805 /* If the next token is `template', there are more template
14806 parameters. */
21526606 14807 if (cp_lexer_next_token_is_keyword (parser->lexer,
a723baf1
MM
14808 RID_TEMPLATE))
14809 cp_parser_template_declaration_after_export (parser, member_p);
14810 else
14811 {
fe88415f
NS
14812 /* There are no access checks when parsing a template, as we do not
14813 know if a specialization will be a friend. */
14814 push_deferring_access_checks (dk_no_check);
98ca843c 14815
a723baf1
MM
14816 decl = cp_parser_single_declaration (parser,
14817 member_p,
14818 &friend_p);
14819
fe88415f 14820 pop_deferring_access_checks ();
98ca843c 14821
a723baf1
MM
14822 /* If this is a member template declaration, let the front
14823 end know. */
14824 if (member_p && !friend_p && decl)
37d407a1
KL
14825 {
14826 if (TREE_CODE (decl) == TYPE_DECL)
14827 cp_parser_check_access_in_redeclaration (decl);
14828
14829 decl = finish_member_template_decl (decl);
14830 }
a723baf1 14831 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19db77ce
KL
14832 make_friend_class (current_class_type, TREE_TYPE (decl),
14833 /*complain=*/true);
a723baf1
MM
14834 }
14835 /* We are done with the current parameter list. */
14836 --parser->num_template_parameter_lists;
14837
14838 /* Finish up. */
14839 finish_template_decl (parameter_list);
14840
14841 /* Register member declarations. */
14842 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14843 finish_member_declaration (decl);
14844
14845 /* If DECL is a function template, we must return to parse it later.
14846 (Even though there is no definition, there might be default
14847 arguments that need handling.) */
21526606 14848 if (member_p && decl
a723baf1
MM
14849 && (TREE_CODE (decl) == FUNCTION_DECL
14850 || DECL_FUNCTION_TEMPLATE_P (decl)))
14851 TREE_VALUE (parser->unparsed_functions_queues)
21526606 14852 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14853 TREE_VALUE (parser->unparsed_functions_queues));
14854}
14855
14856/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14857 `function-definition' sequence. MEMBER_P is true, this declaration
14858 appears in a class scope.
14859
14860 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14861 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14862
14863static tree
21526606 14864cp_parser_single_declaration (cp_parser* parser,
94edc4ab
NN
14865 bool member_p,
14866 bool* friend_p)
a723baf1 14867{
560ad596 14868 int declares_class_or_enum;
a723baf1 14869 tree decl = NULL_TREE;
62d1db17 14870 cp_decl_specifier_seq decl_specifiers;
4bb8ca28 14871 bool function_definition_p = false;
a723baf1 14872
71bd7186
MM
14873 /* This function is only used when processing a template
14874 declaration. */
14875 gcc_assert (innermost_scope_kind () == sk_template_parms
14876 || innermost_scope_kind () == sk_template_spec);
14877
a723baf1 14878 /* Defer access checks until we know what is being declared. */
8d241e0b 14879 push_deferring_access_checks (dk_deferred);
cf22909c 14880
a723baf1
MM
14881 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14882 alternative. */
62d1db17
MM
14883 cp_parser_decl_specifier_seq (parser,
14884 CP_PARSER_FLAGS_OPTIONAL,
14885 &decl_specifiers,
14886 &declares_class_or_enum);
4bb8ca28 14887 if (friend_p)
62d1db17 14888 *friend_p = cp_parser_friend_p (&decl_specifiers);
71bd7186
MM
14889
14890 /* There are no template typedefs. */
14891 if (decl_specifiers.specs[(int) ds_typedef])
14892 {
14893 error ("template declaration of %qs", "typedef");
14894 decl = error_mark_node;
14895 }
14896
a723baf1
MM
14897 /* Gather up the access checks that occurred the
14898 decl-specifier-seq. */
cf22909c
KL
14899 stop_deferring_access_checks ();
14900
a723baf1
MM
14901 /* Check for the declaration of a template class. */
14902 if (declares_class_or_enum)
14903 {
14904 if (cp_parser_declares_only_class_p (parser))
14905 {
62d1db17 14906 decl = shadow_tag (&decl_specifiers);
b939a023
KL
14907
14908 /* In this case:
14909
14910 struct C {
14911 friend template <typename T> struct A<T>::B;
14912 };
14913
14914 A<T>::B will be represented by a TYPENAME_TYPE, and
14915 therefore not recognized by shadow_tag. */
14916 if (friend_p && *friend_p
14917 && !decl
14918 && decl_specifiers.type
14919 && TYPE_P (decl_specifiers.type))
14920 decl = decl_specifiers.type;
14921
62d1db17 14922 if (decl && decl != error_mark_node)
a723baf1
MM
14923 decl = TYPE_NAME (decl);
14924 else
14925 decl = error_mark_node;
14926 }
14927 }
a723baf1
MM
14928 /* If it's not a template class, try for a template function. If
14929 the next token is a `;', then this declaration does not declare
14930 anything. But, if there were errors in the decl-specifiers, then
14931 the error might well have come from an attempted class-specifier.
14932 In that case, there's no need to warn about a missing declarator. */
14933 if (!decl
14934 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
62d1db17 14935 || decl_specifiers.type != error_mark_node))
21526606 14936 decl = cp_parser_init_declarator (parser,
62d1db17 14937 &decl_specifiers,
4bb8ca28 14938 /*function_definition_allowed_p=*/true,
a723baf1 14939 member_p,
560ad596 14940 declares_class_or_enum,
4bb8ca28 14941 &function_definition_p);
cf22909c
KL
14942
14943 pop_deferring_access_checks ();
14944
a723baf1
MM
14945 /* Clear any current qualification; whatever comes next is the start
14946 of something new. */
14947 parser->scope = NULL_TREE;
14948 parser->qualifying_scope = NULL_TREE;
14949 parser->object_scope = NULL_TREE;
14950 /* Look for a trailing `;' after the declaration. */
4bb8ca28 14951 if (!function_definition_p
71bd7186
MM
14952 && (decl == error_mark_node
14953 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
a723baf1 14954 cp_parser_skip_to_end_of_block_or_statement (parser);
a723baf1
MM
14955
14956 return decl;
14957}
14958
d6b4ea85
MM
14959/* Parse a cast-expression that is not the operand of a unary "&". */
14960
14961static tree
14962cp_parser_simple_cast_expression (cp_parser *parser)
14963{
14964 return cp_parser_cast_expression (parser, /*address_p=*/false);
14965}
14966
a723baf1
MM
14967/* Parse a functional cast to TYPE. Returns an expression
14968 representing the cast. */
14969
14970static tree
94edc4ab 14971cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14972{
14973 tree expression_list;
d36d5600 14974 tree cast;
a723baf1 14975
21526606 14976 expression_list
39703eb9
MM
14977 = cp_parser_parenthesized_expression_list (parser, false,
14978 /*non_constant_p=*/NULL);
a723baf1 14979
d36d5600
GB
14980 cast = build_functional_cast (type, expression_list);
14981 /* [expr.const]/1: In an integral constant expression "only type
14982 conversions to integral or enumeration type can be used". */
98ca843c 14983 if (cast != error_mark_node && !type_dependent_expression_p (type)
d36d5600
GB
14984 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14985 {
98ca843c 14986 if (cp_parser_non_integral_constant_expression
d36d5600
GB
14987 (parser, "a call to a constructor"))
14988 return error_mark_node;
14989 }
14990 return cast;
a723baf1
MM
14991}
14992
4bb8ca28
MM
14993/* Save the tokens that make up the body of a member function defined
14994 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14995 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14996 specifiers applied to the declaration. Returns the FUNCTION_DECL
14997 for the member function. */
14998
7ce27103 14999static tree
4bb8ca28 15000cp_parser_save_member_function_body (cp_parser* parser,
62d1db17 15001 cp_decl_specifier_seq *decl_specifiers,
058b15c1 15002 cp_declarator *declarator,
4bb8ca28
MM
15003 tree attributes)
15004{
c162c75e
MA
15005 cp_token *first;
15006 cp_token *last;
4bb8ca28
MM
15007 tree fn;
15008
15009 /* Create the function-declaration. */
15010 fn = start_method (decl_specifiers, declarator, attributes);
15011 /* If something went badly wrong, bail out now. */
15012 if (fn == error_mark_node)
15013 {
15014 /* If there's a function-body, skip it. */
21526606 15015 if (cp_parser_token_starts_function_definition_p
4bb8ca28
MM
15016 (cp_lexer_peek_token (parser->lexer)))
15017 cp_parser_skip_to_end_of_block_or_statement (parser);
15018 return error_mark_node;
15019 }
15020
15021 /* Remember it, if there default args to post process. */
15022 cp_parser_save_default_args (parser, fn);
15023
21526606 15024 /* Save away the tokens that make up the body of the
4bb8ca28 15025 function. */
c162c75e
MA
15026 first = parser->lexer->next_token;
15027 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
4bb8ca28
MM
15028 /* Handle function try blocks. */
15029 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
c162c75e
MA
15030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15031 last = parser->lexer->next_token;
4bb8ca28
MM
15032
15033 /* Save away the inline definition; we will process it when the
15034 class is complete. */
c162c75e 15035 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
4bb8ca28
MM
15036 DECL_PENDING_INLINE_P (fn) = 1;
15037
15038 /* We need to know that this was defined in the class, so that
15039 friend templates are handled correctly. */
15040 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15041
15042 /* We're done with the inline definition. */
15043 finish_method (fn);
15044
15045 /* Add FN to the queue of functions to be parsed later. */
15046 TREE_VALUE (parser->unparsed_functions_queues)
21526606 15047 = tree_cons (NULL_TREE, fn,
4bb8ca28
MM
15048 TREE_VALUE (parser->unparsed_functions_queues));
15049
15050 return fn;
15051}
15052
ec75414f
MM
15053/* Parse a template-argument-list, as well as the trailing ">" (but
15054 not the opening ">"). See cp_parser_template_argument_list for the
15055 return value. */
15056
15057static tree
15058cp_parser_enclosed_template_argument_list (cp_parser* parser)
15059{
15060 tree arguments;
15061 tree saved_scope;
15062 tree saved_qualifying_scope;
15063 tree saved_object_scope;
15064 bool saved_greater_than_is_operator_p;
15065
15066 /* [temp.names]
15067
15068 When parsing a template-id, the first non-nested `>' is taken as
15069 the end of the template-argument-list rather than a greater-than
15070 operator. */
21526606 15071 saved_greater_than_is_operator_p
ec75414f
MM
15072 = parser->greater_than_is_operator_p;
15073 parser->greater_than_is_operator_p = false;
15074 /* Parsing the argument list may modify SCOPE, so we save it
15075 here. */
15076 saved_scope = parser->scope;
15077 saved_qualifying_scope = parser->qualifying_scope;
15078 saved_object_scope = parser->object_scope;
15079 /* Parse the template-argument-list itself. */
15080 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15081 arguments = NULL_TREE;
15082 else
15083 arguments = cp_parser_template_argument_list (parser);
4d5297fa
GB
15084 /* Look for the `>' that ends the template-argument-list. If we find
15085 a '>>' instead, it's probably just a typo. */
15086 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15087 {
15088 if (!saved_greater_than_is_operator_p)
15089 {
2cfe82fe
ZW
15090 /* If we're in a nested template argument list, the '>>' has
15091 to be a typo for '> >'. We emit the error message, but we
15092 continue parsing and we push a '>' as next token, so that
15093 the argument list will be parsed correctly. Note that the
15094 global source location is still on the token before the
15095 '>>', so we need to say explicitly where we want it. */
15096 cp_token *token = cp_lexer_peek_token (parser->lexer);
15097 error ("%H%<>>%> should be %<> >%> "
15098 "within a nested template argument list",
15099 &token->location);
15100
15101 /* ??? Proper recovery should terminate two levels of
15102 template argument list here. */
4d5297fa
GB
15103 token->type = CPP_GREATER;
15104 }
15105 else
15106 {
2cfe82fe
ZW
15107 /* If this is not a nested template argument list, the '>>'
15108 is a typo for '>'. Emit an error message and continue.
15109 Same deal about the token location, but here we can get it
15110 right by consuming the '>>' before issuing the diagnostic. */
4d5297fa 15111 cp_lexer_consume_token (parser->lexer);
2cfe82fe
ZW
15112 error ("spurious %<>>%>, use %<>%> to terminate "
15113 "a template argument list");
4d5297fa
GB
15114 }
15115 }
2cfe82fe
ZW
15116 else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15117 error ("missing %<>%> to terminate the template argument list");
15118 else
15119 /* It's what we want, a '>'; consume it. */
15120 cp_lexer_consume_token (parser->lexer);
ec75414f 15121 /* The `>' token might be a greater-than operator again now. */
21526606 15122 parser->greater_than_is_operator_p
ec75414f
MM
15123 = saved_greater_than_is_operator_p;
15124 /* Restore the SAVED_SCOPE. */
15125 parser->scope = saved_scope;
15126 parser->qualifying_scope = saved_qualifying_scope;
15127 parser->object_scope = saved_object_scope;
15128
15129 return arguments;
15130}
15131
a723baf1
MM
15132/* MEMBER_FUNCTION is a member function, or a friend. If default
15133 arguments, or the body of the function have not yet been parsed,
15134 parse them now. */
15135
15136static void
94edc4ab 15137cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1 15138{
a723baf1
MM
15139 /* If this member is a template, get the underlying
15140 FUNCTION_DECL. */
15141 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15142 member_function = DECL_TEMPLATE_RESULT (member_function);
15143
15144 /* There should not be any class definitions in progress at this
15145 point; the bodies of members are only parsed outside of all class
15146 definitions. */
50bc768d 15147 gcc_assert (parser->num_classes_being_defined == 0);
a723baf1
MM
15148 /* While we're parsing the member functions we might encounter more
15149 classes. We want to handle them right away, but we don't want
15150 them getting mixed up with functions that are currently in the
15151 queue. */
15152 parser->unparsed_functions_queues
15153 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15154
15155 /* Make sure that any template parameters are in scope. */
15156 maybe_begin_member_template_processing (member_function);
15157
a723baf1
MM
15158 /* If the body of the function has not yet been parsed, parse it
15159 now. */
15160 if (DECL_PENDING_INLINE_P (member_function))
15161 {
15162 tree function_scope;
15163 cp_token_cache *tokens;
15164
15165 /* The function is no longer pending; we are processing it. */
15166 tokens = DECL_PENDING_INLINE_INFO (member_function);
15167 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15168 DECL_PENDING_INLINE_P (member_function) = 0;
15169 /* If this was an inline function in a local class, enter the scope
15170 of the containing function. */
15171 function_scope = decl_function_context (member_function);
15172 if (function_scope)
15173 push_function_context_to (function_scope);
21526606 15174
2cfe82fe
ZW
15175 /* Push the body of the function onto the lexer stack. */
15176 cp_parser_push_lexer_for_tokens (parser, tokens);
21526606 15177
a723baf1
MM
15178 /* Let the front end know that we going to be defining this
15179 function. */
058b15c1
MM
15180 start_preparsed_function (member_function, NULL_TREE,
15181 SF_PRE_PARSED | SF_INCLASS_INLINE);
21526606 15182
a723baf1
MM
15183 /* Now, parse the body of the function. */
15184 cp_parser_function_definition_after_declarator (parser,
15185 /*inline_p=*/true);
21526606 15186
a723baf1
MM
15187 /* Leave the scope of the containing function. */
15188 if (function_scope)
15189 pop_function_context_from (function_scope);
2cfe82fe 15190 cp_parser_pop_lexer (parser);
a723baf1
MM
15191 }
15192
15193 /* Remove any template parameters from the symbol table. */
15194 maybe_end_member_template_processing ();
15195
15196 /* Restore the queue. */
21526606 15197 parser->unparsed_functions_queues
a723baf1
MM
15198 = TREE_CHAIN (parser->unparsed_functions_queues);
15199}
15200
cd0be382 15201/* If DECL contains any default args, remember it on the unparsed
8db1028e
NS
15202 functions queue. */
15203
15204static void
15205cp_parser_save_default_args (cp_parser* parser, tree decl)
15206{
15207 tree probe;
15208
15209 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15210 probe;
15211 probe = TREE_CHAIN (probe))
15212 if (TREE_PURPOSE (probe))
15213 {
15214 TREE_PURPOSE (parser->unparsed_functions_queues)
f44b0c8e 15215 = tree_cons (current_class_type, decl,
8db1028e
NS
15216 TREE_PURPOSE (parser->unparsed_functions_queues));
15217 break;
15218 }
15219 return;
15220}
15221
8218bd34 15222/* FN is a FUNCTION_DECL which may contains a parameter with an
f44b0c8e
MM
15223 unparsed DEFAULT_ARG. Parse the default args now. This function
15224 assumes that the current scope is the scope in which the default
15225 argument should be processed. */
a723baf1
MM
15226
15227static void
8218bd34 15228cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1 15229{
a723baf1 15230 bool saved_local_variables_forbidden_p;
2cfe82fe 15231 tree parm;
8218bd34 15232
b92bc2a0
NS
15233 /* While we're parsing the default args, we might (due to the
15234 statement expression extension) encounter more classes. We want
15235 to handle them right away, but we don't want them getting mixed
15236 up with default args that are currently in the queue. */
15237 parser->unparsed_functions_queues
15238 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15239
2cfe82fe
ZW
15240 /* Local variable names (and the `this' keyword) may not appear
15241 in a default argument. */
15242 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15243 parser->local_variables_forbidden_p = true;
15244
15245 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15246 parm;
15247 parm = TREE_CHAIN (parm))
a723baf1 15248 {
2cfe82fe 15249 cp_token_cache *tokens;
21526606 15250
2cfe82fe
ZW
15251 if (!TREE_PURPOSE (parm)
15252 || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15253 continue;
a723baf1 15254
2cfe82fe
ZW
15255 /* Push the saved tokens for the default argument onto the parser's
15256 lexer stack. */
15257 tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15258 cp_parser_push_lexer_for_tokens (parser, tokens);
a723baf1 15259
2cfe82fe
ZW
15260 /* Parse the assignment-expression. */
15261 TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
a723baf1 15262
676e33ca
MM
15263 /* If the token stream has not been completely used up, then
15264 there was extra junk after the end of the default
15265 argument. */
15266 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2a13a625 15267 cp_parser_error (parser, "expected %<,%>");
676e33ca 15268
2cfe82fe
ZW
15269 /* Revert to the main lexer. */
15270 cp_parser_pop_lexer (parser);
a723baf1 15271 }
b92bc2a0 15272
2cfe82fe
ZW
15273 /* Restore the state of local_variables_forbidden_p. */
15274 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15275
b92bc2a0 15276 /* Restore the queue. */
21526606 15277 parser->unparsed_functions_queues
b92bc2a0 15278 = TREE_CHAIN (parser->unparsed_functions_queues);
a723baf1
MM
15279}
15280
15281/* Parse the operand of `sizeof' (or a similar operator). Returns
15282 either a TYPE or an expression, depending on the form of the
15283 input. The KEYWORD indicates which kind of expression we have
15284 encountered. */
15285
15286static tree
94edc4ab 15287cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
15288{
15289 static const char *format;
15290 tree expr = NULL_TREE;
15291 const char *saved_message;
67c03833 15292 bool saved_integral_constant_expression_p;
a723baf1
MM
15293
15294 /* Initialize FORMAT the first time we get here. */
15295 if (!format)
9e637a26 15296 format = "types may not be defined in '%s' expressions";
a723baf1
MM
15297
15298 /* Types cannot be defined in a `sizeof' expression. Save away the
15299 old message. */
15300 saved_message = parser->type_definition_forbidden_message;
15301 /* And create the new one. */
21526606
EC
15302 parser->type_definition_forbidden_message
15303 = xmalloc (strlen (format)
c68b0a84
KG
15304 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15305 + 1 /* `\0' */);
a723baf1
MM
15306 sprintf ((char *) parser->type_definition_forbidden_message,
15307 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15308
15309 /* The restrictions on constant-expressions do not apply inside
15310 sizeof expressions. */
67c03833
JM
15311 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15312 parser->integral_constant_expression_p = false;
a723baf1 15313
3beb3abf
MM
15314 /* Do not actually evaluate the expression. */
15315 ++skip_evaluation;
a723baf1
MM
15316 /* If it's a `(', then we might be looking at the type-id
15317 construction. */
15318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15319 {
15320 tree type;
4f8163b1 15321 bool saved_in_type_id_in_expr_p;
a723baf1
MM
15322
15323 /* We can't be sure yet whether we're looking at a type-id or an
15324 expression. */
15325 cp_parser_parse_tentatively (parser);
15326 /* Consume the `('. */
15327 cp_lexer_consume_token (parser->lexer);
15328 /* Parse the type-id. */
4f8163b1
MM
15329 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15330 parser->in_type_id_in_expr_p = true;
a723baf1 15331 type = cp_parser_type_id (parser);
4f8163b1 15332 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
a723baf1 15333 /* Now, look for the trailing `)'. */
9e637a26 15334 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
a723baf1
MM
15335 /* If all went well, then we're done. */
15336 if (cp_parser_parse_definitely (parser))
15337 {
62d1db17
MM
15338 cp_decl_specifier_seq decl_specs;
15339
15340 /* Build a trivial decl-specifier-seq. */
15341 clear_decl_specs (&decl_specs);
15342 decl_specs.type = type;
a723baf1
MM
15343
15344 /* Call grokdeclarator to figure out what type this is. */
058b15c1 15345 expr = grokdeclarator (NULL,
62d1db17 15346 &decl_specs,
a723baf1
MM
15347 TYPENAME,
15348 /*initialized=*/0,
15349 /*attrlist=*/NULL);
15350 }
15351 }
15352
15353 /* If the type-id production did not work out, then we must be
15354 looking at the unary-expression production. */
15355 if (!expr)
15356 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
15357 /* Go back to evaluating expressions. */
15358 --skip_evaluation;
a723baf1
MM
15359
15360 /* Free the message we created. */
15361 free ((char *) parser->type_definition_forbidden_message);
15362 /* And restore the old one. */
15363 parser->type_definition_forbidden_message = saved_message;
67c03833 15364 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
a723baf1
MM
15365
15366 return expr;
15367}
15368
15369/* If the current declaration has no declarator, return true. */
15370
15371static bool
15372cp_parser_declares_only_class_p (cp_parser *parser)
15373{
21526606 15374 /* If the next token is a `;' or a `,' then there is no
a723baf1
MM
15375 declarator. */
15376 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15377 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15378}
15379
62d1db17 15380/* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
a723baf1 15381
62d1db17
MM
15382static void
15383cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15384 cp_storage_class storage_class)
a723baf1 15385{
62d1db17
MM
15386 if (decl_specs->storage_class != sc_none)
15387 decl_specs->multiple_storage_classes_p = true;
15388 else
15389 decl_specs->storage_class = storage_class;
15390}
15391
15392/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15393 is true, the type is a user-defined type; otherwise it is a
15394 built-in type specified by a keyword. */
a723baf1 15395
62d1db17
MM
15396static void
15397cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15398 tree type_spec,
15399 bool user_defined_p)
15400{
15401 decl_specs->any_specifiers_p = true;
98ca843c 15402
9306cccb
MM
15403 /* If the user tries to redeclare bool or wchar_t (with, for
15404 example, in "typedef int wchar_t;") we remember that this is what
f84b6c96
MM
15405 happened. In system headers, we ignore these declarations so
15406 that G++ can work with system headers that are not C++-safe. */
98ca843c 15407 if (decl_specs->specs[(int) ds_typedef]
f84b6c96 15408 && !user_defined_p
9306cccb
MM
15409 && (type_spec == boolean_type_node
15410 || type_spec == wchar_type_node)
f84b6c96
MM
15411 && (decl_specs->type
15412 || decl_specs->specs[(int) ds_long]
15413 || decl_specs->specs[(int) ds_short]
15414 || decl_specs->specs[(int) ds_unsigned]
15415 || decl_specs->specs[(int) ds_signed]))
0a73e37f
MM
15416 {
15417 decl_specs->redefined_builtin_type = type_spec;
15418 if (!decl_specs->type)
15419 {
15420 decl_specs->type = type_spec;
15421 decl_specs->user_defined_type_p = false;
15422 }
15423 }
f84b6c96
MM
15424 else if (decl_specs->type)
15425 decl_specs->multiple_types_p = true;
62d1db17
MM
15426 else
15427 {
15428 decl_specs->type = type_spec;
15429 decl_specs->user_defined_type_p = user_defined_p;
0a73e37f 15430 decl_specs->redefined_builtin_type = NULL_TREE;
a723baf1 15431 }
62d1db17 15432}
a723baf1 15433
62d1db17
MM
15434/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15435 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15436
15437static bool
15438cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15439{
15440 return decl_specifiers->specs[(int) ds_friend] != 0;
a723baf1
MM
15441}
15442
15443/* If the next token is of the indicated TYPE, consume it. Otherwise,
15444 issue an error message indicating that TOKEN_DESC was expected.
21526606 15445
a723baf1
MM
15446 Returns the token consumed, if the token had the appropriate type.
15447 Otherwise, returns NULL. */
15448
15449static cp_token *
94edc4ab
NN
15450cp_parser_require (cp_parser* parser,
15451 enum cpp_ttype type,
15452 const char* token_desc)
a723baf1
MM
15453{
15454 if (cp_lexer_next_token_is (parser->lexer, type))
15455 return cp_lexer_consume_token (parser->lexer);
15456 else
15457 {
e5976695
MM
15458 /* Output the MESSAGE -- unless we're parsing tentatively. */
15459 if (!cp_parser_simulate_error (parser))
216bb6e1
MM
15460 {
15461 char *message = concat ("expected ", token_desc, NULL);
15462 cp_parser_error (parser, message);
15463 free (message);
15464 }
a723baf1
MM
15465 return NULL;
15466 }
15467}
15468
15469/* Like cp_parser_require, except that tokens will be skipped until
15470 the desired token is found. An error message is still produced if
15471 the next token is not as expected. */
15472
15473static void
21526606
EC
15474cp_parser_skip_until_found (cp_parser* parser,
15475 enum cpp_ttype type,
94edc4ab 15476 const char* token_desc)
a723baf1
MM
15477{
15478 cp_token *token;
15479 unsigned nesting_depth = 0;
15480
15481 if (cp_parser_require (parser, type, token_desc))
15482 return;
15483
15484 /* Skip tokens until the desired token is found. */
15485 while (true)
15486 {
15487 /* Peek at the next token. */
15488 token = cp_lexer_peek_token (parser->lexer);
21526606 15489 /* If we've reached the token we want, consume it and
a723baf1
MM
15490 stop. */
15491 if (token->type == type && !nesting_depth)
15492 {
15493 cp_lexer_consume_token (parser->lexer);
15494 return;
15495 }
15496 /* If we've run out of tokens, stop. */
15497 if (token->type == CPP_EOF)
15498 return;
21526606 15499 if (token->type == CPP_OPEN_BRACE
a723baf1
MM
15500 || token->type == CPP_OPEN_PAREN
15501 || token->type == CPP_OPEN_SQUARE)
15502 ++nesting_depth;
21526606 15503 else if (token->type == CPP_CLOSE_BRACE
a723baf1
MM
15504 || token->type == CPP_CLOSE_PAREN
15505 || token->type == CPP_CLOSE_SQUARE)
15506 {
15507 if (nesting_depth-- == 0)
15508 return;
15509 }
15510 /* Consume this token. */
15511 cp_lexer_consume_token (parser->lexer);
15512 }
15513}
15514
15515/* If the next token is the indicated keyword, consume it. Otherwise,
15516 issue an error message indicating that TOKEN_DESC was expected.
21526606 15517
a723baf1
MM
15518 Returns the token consumed, if the token had the appropriate type.
15519 Otherwise, returns NULL. */
15520
15521static cp_token *
94edc4ab
NN
15522cp_parser_require_keyword (cp_parser* parser,
15523 enum rid keyword,
15524 const char* token_desc)
a723baf1
MM
15525{
15526 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15527
15528 if (token && token->keyword != keyword)
15529 {
15530 dyn_string_t error_msg;
15531
15532 /* Format the error message. */
15533 error_msg = dyn_string_new (0);
15534 dyn_string_append_cstr (error_msg, "expected ");
15535 dyn_string_append_cstr (error_msg, token_desc);
15536 cp_parser_error (parser, error_msg->s);
15537 dyn_string_delete (error_msg);
15538 return NULL;
15539 }
15540
15541 return token;
15542}
15543
15544/* Returns TRUE iff TOKEN is a token that can begin the body of a
15545 function-definition. */
15546
21526606 15547static bool
94edc4ab 15548cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
15549{
15550 return (/* An ordinary function-body begins with an `{'. */
15551 token->type == CPP_OPEN_BRACE
15552 /* A ctor-initializer begins with a `:'. */
15553 || token->type == CPP_COLON
15554 /* A function-try-block begins with `try'. */
15555 || token->keyword == RID_TRY
15556 /* The named return value extension begins with `return'. */
15557 || token->keyword == RID_RETURN);
15558}
15559
15560/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15561 definition. */
15562
15563static bool
15564cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15565{
15566 cp_token *token;
15567
15568 token = cp_lexer_peek_token (parser->lexer);
15569 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15570}
15571
d17811fd 15572/* Returns TRUE iff the next token is the "," or ">" ending a
03fd3f84 15573 template-argument. */
d17811fd
MM
15574
15575static bool
15576cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15577{
15578 cp_token *token;
15579
15580 token = cp_lexer_peek_token (parser->lexer);
391c4bc5 15581 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
d17811fd 15582}
f4abade9
GB
15583
15584/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15585 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15586
15587static bool
21526606 15588cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
f4abade9
GB
15589 size_t n)
15590{
15591 cp_token *token;
15592
15593 token = cp_lexer_peek_nth_token (parser->lexer, n);
15594 if (token->type == CPP_LESS)
15595 return true;
15596 /* Check for the sequence `<::' in the original code. It would be lexed as
15597 `[:', where `[' is a digraph, and there is no whitespace before
15598 `:'. */
15599 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15600 {
15601 cp_token *token2;
15602 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15603 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15604 return true;
15605 }
15606 return false;
15607}
21526606 15608
a723baf1
MM
15609/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15610 or none_type otherwise. */
15611
15612static enum tag_types
94edc4ab 15613cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
15614{
15615 switch (token->keyword)
15616 {
15617 case RID_CLASS:
15618 return class_type;
15619 case RID_STRUCT:
15620 return record_type;
15621 case RID_UNION:
15622 return union_type;
21526606 15623
a723baf1
MM
15624 default:
15625 return none_type;
15626 }
15627}
15628
15629/* Issue an error message if the CLASS_KEY does not match the TYPE. */
15630
15631static void
15632cp_parser_check_class_key (enum tag_types class_key, tree type)
15633{
15634 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2a13a625 15635 pedwarn ("%qs tag used in naming %q#T",
a723baf1 15636 class_key == union_type ? "union"
21526606 15637 : class_key == record_type ? "struct" : "class",
a723baf1
MM
15638 type);
15639}
21526606 15640
cd0be382 15641/* Issue an error message if DECL is redeclared with different
37d407a1
KL
15642 access than its original declaration [class.access.spec/3].
15643 This applies to nested classes and nested class templates.
15644 [class.mem/1]. */
15645
2a13a625
GDR
15646static void
15647cp_parser_check_access_in_redeclaration (tree decl)
37d407a1
KL
15648{
15649 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15650 return;
15651
15652 if ((TREE_PRIVATE (decl)
15653 != (current_access_specifier == access_private_node))
15654 || (TREE_PROTECTED (decl)
15655 != (current_access_specifier == access_protected_node)))
2a13a625 15656 error ("%qD redeclared with different access", decl);
37d407a1
KL
15657}
15658
a723baf1 15659/* Look for the `template' keyword, as a syntactic disambiguator.
21526606 15660 Return TRUE iff it is present, in which case it will be
a723baf1
MM
15661 consumed. */
15662
15663static bool
15664cp_parser_optional_template_keyword (cp_parser *parser)
15665{
15666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15667 {
15668 /* The `template' keyword can only be used within templates;
15669 outside templates the parser can always figure out what is a
15670 template and what is not. */
15671 if (!processing_template_decl)
15672 {
2a13a625 15673 error ("%<template%> (as a disambiguator) is only allowed "
a723baf1
MM
15674 "within templates");
15675 /* If this part of the token stream is rescanned, the same
15676 error message would be generated. So, we purge the token
15677 from the stream. */
15678 cp_lexer_purge_token (parser->lexer);
15679 return false;
15680 }
15681 else
15682 {
15683 /* Consume the `template' keyword. */
15684 cp_lexer_consume_token (parser->lexer);
15685 return true;
15686 }
15687 }
15688
15689 return false;
15690}
15691
2050a1bb
MM
15692/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15693 set PARSER->SCOPE, and perform other related actions. */
15694
15695static void
15696cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15697{
15698 tree value;
15699 tree check;
15700
15701 /* Get the stored value. */
15702 value = cp_lexer_consume_token (parser->lexer)->value;
15703 /* Perform any access checks that were deferred. */
15704 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 15705 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
15706 /* Set the scope from the stored value. */
15707 parser->scope = TREE_VALUE (value);
15708 parser->qualifying_scope = TREE_TYPE (value);
15709 parser->object_scope = NULL_TREE;
15710}
15711
03fd3f84 15712/* Consume tokens up through a non-nested END token. */
a723baf1
MM
15713
15714static void
c162c75e
MA
15715cp_parser_cache_group (cp_parser *parser,
15716 enum cpp_ttype end,
15717 unsigned depth)
a723baf1
MM
15718{
15719 while (true)
15720 {
15721 cp_token *token;
15722
15723 /* Abort a parenthesized expression if we encounter a brace. */
15724 if ((end == CPP_CLOSE_PAREN || depth == 0)
15725 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15726 return;
a723baf1 15727 /* If we've reached the end of the file, stop. */
4bfb8bba 15728 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a723baf1 15729 return;
4bfb8bba
MM
15730 /* Consume the next token. */
15731 token = cp_lexer_consume_token (parser->lexer);
a723baf1
MM
15732 /* See if it starts a new group. */
15733 if (token->type == CPP_OPEN_BRACE)
15734 {
c162c75e 15735 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
a723baf1
MM
15736 if (depth == 0)
15737 return;
15738 }
15739 else if (token->type == CPP_OPEN_PAREN)
c162c75e 15740 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
a723baf1
MM
15741 else if (token->type == end)
15742 return;
15743 }
15744}
15745
15746/* Begin parsing tentatively. We always save tokens while parsing
15747 tentatively so that if the tentative parsing fails we can restore the
15748 tokens. */
15749
15750static void
94edc4ab 15751cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
15752{
15753 /* Enter a new parsing context. */
15754 parser->context = cp_parser_context_new (parser->context);
15755 /* Begin saving tokens. */
15756 cp_lexer_save_tokens (parser->lexer);
15757 /* In order to avoid repetitive access control error messages,
15758 access checks are queued up until we are no longer parsing
15759 tentatively. */
8d241e0b 15760 push_deferring_access_checks (dk_deferred);
a723baf1
MM
15761}
15762
15763/* Commit to the currently active tentative parse. */
15764
15765static void
94edc4ab 15766cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
15767{
15768 cp_parser_context *context;
15769 cp_lexer *lexer;
15770
15771 /* Mark all of the levels as committed. */
15772 lexer = parser->lexer;
15773 for (context = parser->context; context->next; context = context->next)
15774 {
15775 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15776 break;
15777 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15778 while (!cp_lexer_saving_tokens (lexer))
15779 lexer = lexer->next;
15780 cp_lexer_commit_tokens (lexer);
15781 }
15782}
15783
15784/* Abort the currently active tentative parse. All consumed tokens
15785 will be rolled back, and no diagnostics will be issued. */
15786
15787static void
94edc4ab 15788cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
15789{
15790 cp_parser_simulate_error (parser);
15791 /* Now, pretend that we want to see if the construct was
15792 successfully parsed. */
15793 cp_parser_parse_definitely (parser);
15794}
15795
34cd5ae7 15796/* Stop parsing tentatively. If a parse error has occurred, restore the
a723baf1
MM
15797 token stream. Otherwise, commit to the tokens we have consumed.
15798 Returns true if no error occurred; false otherwise. */
15799
15800static bool
94edc4ab 15801cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
15802{
15803 bool error_occurred;
15804 cp_parser_context *context;
15805
34cd5ae7 15806 /* Remember whether or not an error occurred, since we are about to
a723baf1
MM
15807 destroy that information. */
15808 error_occurred = cp_parser_error_occurred (parser);
15809 /* Remove the topmost context from the stack. */
15810 context = parser->context;
15811 parser->context = context->next;
15812 /* If no parse errors occurred, commit to the tentative parse. */
15813 if (!error_occurred)
15814 {
15815 /* Commit to the tokens read tentatively, unless that was
15816 already done. */
15817 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15818 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
15819
15820 pop_to_parent_deferring_access_checks ();
a723baf1
MM
15821 }
15822 /* Otherwise, if errors occurred, roll back our state so that things
15823 are just as they were before we began the tentative parse. */
15824 else
cf22909c
KL
15825 {
15826 cp_lexer_rollback_tokens (parser->lexer);
15827 pop_deferring_access_checks ();
15828 }
e5976695
MM
15829 /* Add the context to the front of the free list. */
15830 context->next = cp_parser_context_free_list;
15831 cp_parser_context_free_list = context;
15832
15833 return !error_occurred;
a723baf1
MM
15834}
15835
0b16f8f4
VR
15836/* Returns true if we are parsing tentatively and are not committed to
15837 this tentative parse. */
a723baf1
MM
15838
15839static bool
0b16f8f4 15840cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
a723baf1
MM
15841{
15842 return (cp_parser_parsing_tentatively (parser)
0b16f8f4 15843 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
a723baf1
MM
15844}
15845
4de8668e 15846/* Returns nonzero iff an error has occurred during the most recent
a723baf1 15847 tentative parse. */
21526606 15848
a723baf1 15849static bool
94edc4ab 15850cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
15851{
15852 return (cp_parser_parsing_tentatively (parser)
15853 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15854}
15855
4de8668e 15856/* Returns nonzero if GNU extensions are allowed. */
a723baf1
MM
15857
15858static bool
94edc4ab 15859cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
15860{
15861 return parser->allow_gnu_extensions_p;
15862}
15863
15864\f
a723baf1
MM
15865/* The parser. */
15866
15867static GTY (()) cp_parser *the_parser;
15868
15869/* External interface. */
15870
d1bd0ded 15871/* Parse one entire translation unit. */
a723baf1 15872
d1bd0ded
GK
15873void
15874c_parse_file (void)
a723baf1
MM
15875{
15876 bool error_occurred;
f75fbaf7
ZW
15877 static bool already_called = false;
15878
15879 if (already_called)
15880 {
15881 sorry ("inter-module optimizations not implemented for C++");
15882 return;
15883 }
15884 already_called = true;
a723baf1
MM
15885
15886 the_parser = cp_parser_new ();
78757caa
KL
15887 push_deferring_access_checks (flag_access_control
15888 ? dk_no_deferred : dk_no_check);
a723baf1
MM
15889 error_occurred = cp_parser_translation_unit (the_parser);
15890 the_parser = NULL;
a723baf1
MM
15891}
15892
a723baf1
MM
15893/* This variable must be provided by every front end. */
15894
15895int yydebug;
15896
15897#include "gt-cp-parser.h"